xref: /onnv-gate/usr/src/cmd/fs.d/cachefs/fsck/res.c (revision 633:04519cb7de3b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*633Sgt29601  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*633Sgt29601  * Use is subject to license terms.
25*633Sgt29601  */
26*633Sgt29601 /*
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  *			res.c
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * Implements routines to create a cache resource file.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <assert.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate #include <sys/param.h>
410Sstevel@tonic-gate #include <sys/fcntl.h>
420Sstevel@tonic-gate #include <sys/mman.h>
430Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
440Sstevel@tonic-gate #include "res.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate struct res {
470Sstevel@tonic-gate 	int			 p_magic;	/* magic number */
480Sstevel@tonic-gate 	int			 p_done:1;	/* 1 if res_done called */
490Sstevel@tonic-gate 	int			 p_verbose:1;	/* 1 means print errors */
500Sstevel@tonic-gate 	void			*p_addrp;	/* address of mapped file */
510Sstevel@tonic-gate 	long			 p_size;	/* size of mapped file */
520Sstevel@tonic-gate 	struct cache_usage	*p_cusagep;	/* ptr to cache_usage */
530Sstevel@tonic-gate 	struct cachefs_rl_info	*p_linfop;	/* ptr to rl_info */
540Sstevel@tonic-gate 	rl_entry_t		*p_rlentp;	/* ptr to first rl_entry */
550Sstevel@tonic-gate 	int			 p_totentries;	/* max number of rl entries */
560Sstevel@tonic-gate 	char		 p_name[MAXPATHLEN];	/* name of resource file */
570Sstevel@tonic-gate };
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define	MAGIC 8272
600Sstevel@tonic-gate #define	precond(A) assert(A)
610Sstevel@tonic-gate #define	MININDEX 1
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	RL_HEAD(resp, type) \
640Sstevel@tonic-gate 	(&(resp->p_linfop->rl_items[CACHEFS_RL_INDEX(type)]))
650Sstevel@tonic-gate #define	CVBLKS(nbytes) ((nbytes + MAXBSIZE - 1) / MAXBSIZE)
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /* forward references */
68*633Sgt29601 void res_rlent_moveto(res *resp, enum cachefs_rl_type type, uint_t entno,
690Sstevel@tonic-gate     long blks);
700Sstevel@tonic-gate void res_reset(res *resp);
710Sstevel@tonic-gate void res_clear(res *resp);
720Sstevel@tonic-gate int res_listcheck(res *, enum cachefs_rl_type);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  *			res_create
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * Description:
790Sstevel@tonic-gate  *	Creates a res object and returns a pointer to it.
800Sstevel@tonic-gate  *	The specified file is used to store resource file data.
810Sstevel@tonic-gate  * Arguments:
820Sstevel@tonic-gate  *	namep	name of the resource file
830Sstevel@tonic-gate  *	entries	max number of rl entries in the file
840Sstevel@tonic-gate  *	verbose 1 means print out error messages
850Sstevel@tonic-gate  * Returns:
860Sstevel@tonic-gate  *	Returns a pointer to the object or NULL if an error occurred.
870Sstevel@tonic-gate  * Preconditions:
880Sstevel@tonic-gate  *	precond(namep)
890Sstevel@tonic-gate  *	precond(entries > 3)
900Sstevel@tonic-gate  *	precond(strlen(namep) < MAXPATHLEN)
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate res *
res_create(char * namep,int entries,int verbose)940Sstevel@tonic-gate res_create(char *namep, int entries, int verbose)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	int xx;
970Sstevel@tonic-gate 	long size;
980Sstevel@tonic-gate 	int fd;
990Sstevel@tonic-gate 	char buf[1024];
1000Sstevel@tonic-gate 	long cnt;
1010Sstevel@tonic-gate 	unsigned int amt;
1020Sstevel@tonic-gate 	ssize_t result;
1030Sstevel@tonic-gate 	void *addrp;
1040Sstevel@tonic-gate 	res *resp;
1050Sstevel@tonic-gate 	struct stat64 statinfo;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	precond(namep);
1080Sstevel@tonic-gate 	precond(entries > MININDEX);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/* determine the size needed for the resource file */
1110Sstevel@tonic-gate 	size = MAXBSIZE;
1120Sstevel@tonic-gate 	size += MAXBSIZE * (entries / CACHEFS_RLPMBS);
1130Sstevel@tonic-gate 	if ((entries %  CACHEFS_RLPMBS) != 0)
1140Sstevel@tonic-gate 		size += MAXBSIZE;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/* if the file does not exist or is the wrong size/type */
1170Sstevel@tonic-gate 	xx = lstat64(namep, &statinfo);
1180Sstevel@tonic-gate 	/* resource file will be <2GB */
1190Sstevel@tonic-gate 	if ((xx == -1) || (statinfo.st_size != (offset_t)size) ||
1200Sstevel@tonic-gate 	    !(S_ISREG(statinfo.st_mode))) {
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 		/* remove the resource file */
1230Sstevel@tonic-gate 		xx = unlink(namep);
1240Sstevel@tonic-gate 		if ((xx == -1) && (errno != ENOENT))
1250Sstevel@tonic-gate 			return (NULL);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 		/* create and open the file */
1280Sstevel@tonic-gate 		fd = open(namep, O_CREAT | O_RDWR, 0600);
1290Sstevel@tonic-gate 		if (fd == -1)
1300Sstevel@tonic-gate 			return (NULL);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		/* fill the file with zeros */
1330Sstevel@tonic-gate 		memset(buf, 0, sizeof (buf));
1340Sstevel@tonic-gate 		for (cnt = size; cnt > 0; cnt -= result) {
1350Sstevel@tonic-gate 			amt = sizeof (buf);
1360Sstevel@tonic-gate 			if (amt > cnt)
1370Sstevel@tonic-gate 				amt = cnt;
1380Sstevel@tonic-gate 			result = write(fd, buf, amt);
1390Sstevel@tonic-gate 			if (result == -1) {
1400Sstevel@tonic-gate 				close(fd);
1410Sstevel@tonic-gate 				return (NULL);
1420Sstevel@tonic-gate 			}
1430Sstevel@tonic-gate 		}
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/* else open the file */
1470Sstevel@tonic-gate 	else {
1480Sstevel@tonic-gate 		fd = open(namep, O_RDWR);
1490Sstevel@tonic-gate 		if (fd == -1)
1500Sstevel@tonic-gate 			return (NULL);
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* mmap the file into our address space */
1540Sstevel@tonic-gate 	addrp = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1550Sstevel@tonic-gate 	if (addrp == (void *)-1) {
1560Sstevel@tonic-gate 		close(fd);
1570Sstevel@tonic-gate 		return (NULL);
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/* close the file descriptor, we do not need it anymore */
1610Sstevel@tonic-gate 	close(fd);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	/* allocate memory for the res object */
1640Sstevel@tonic-gate 	resp = malloc(sizeof (res));
1650Sstevel@tonic-gate 	if (resp == NULL) {
1660Sstevel@tonic-gate 		munmap(addrp, size);
1670Sstevel@tonic-gate 		return (NULL);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/* initialize the object */
1710Sstevel@tonic-gate 	resp->p_magic = MAGIC;
1720Sstevel@tonic-gate 	resp->p_done = 0;
1730Sstevel@tonic-gate 	resp->p_addrp = addrp;
1740Sstevel@tonic-gate 	resp->p_size = size;
1750Sstevel@tonic-gate 	resp->p_verbose = verbose;
1760Sstevel@tonic-gate 	resp->p_cusagep = (struct cache_usage *)addrp;
1770Sstevel@tonic-gate 	resp->p_linfop = (struct cachefs_rl_info *)((char *)addrp +
1780Sstevel@tonic-gate 	    sizeof (struct cache_usage));
1790Sstevel@tonic-gate 	resp->p_rlentp = (rl_entry_t *)((char *)addrp + MAXBSIZE);
1800Sstevel@tonic-gate 	resp->p_totentries = entries;
1810Sstevel@tonic-gate 	strcpy(resp->p_name, namep);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	/* reset the resource file in preperation to rebuild it */
1840Sstevel@tonic-gate 	res_reset(resp);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/* return the object */
1870Sstevel@tonic-gate 	return (resp);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate  *
1920Sstevel@tonic-gate  *			res_destroy
1930Sstevel@tonic-gate  *
1940Sstevel@tonic-gate  * Description:
1950Sstevel@tonic-gate  *	Destroys the specifed res object.
1960Sstevel@tonic-gate  *	If res_done has not been called on the object or if res_done
1970Sstevel@tonic-gate  *	failed, then the resource file will be deleted.
1980Sstevel@tonic-gate  * Arguments:
1990Sstevel@tonic-gate  *	resp	object to destroy
2000Sstevel@tonic-gate  * Returns:
2010Sstevel@tonic-gate  * Preconditions:
2020Sstevel@tonic-gate  *	precond(resp is a valid res object)
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate void
res_destroy(res * resp)2060Sstevel@tonic-gate res_destroy(res *resp)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	precond(resp);
2090Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/* unmap the file */
2120Sstevel@tonic-gate 	munmap(resp->p_addrp, resp->p_size);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	/* if res_done not performed */
2150Sstevel@tonic-gate 	if (resp->p_done == 0) {
2160Sstevel@tonic-gate 		/* remove the resource file */
2170Sstevel@tonic-gate 		unlink(resp->p_name);
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/* destroy the object */
2210Sstevel@tonic-gate 	resp->p_magic = -MAGIC;
2220Sstevel@tonic-gate 	free(resp);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate rl_entry_t *
res_rlent_get(res * resp,uint_t entno)226*633Sgt29601 res_rlent_get(res *resp, uint_t entno)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	rl_entry_t *rlentp, *window;
229*633Sgt29601 	uint_t whichwindow, winoffset;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	precond((entno >= MININDEX) && (entno < resp->p_totentries));
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	whichwindow = entno / CACHEFS_RLPMBS;
2340Sstevel@tonic-gate 	winoffset = entno % CACHEFS_RLPMBS;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	window = (rl_entry_t *)
237*633Sgt29601 	    (((caddr_t)resp->p_rlentp) + (MAXBSIZE * whichwindow));
2380Sstevel@tonic-gate 	rlentp = window + winoffset;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	return (rlentp);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  *
2450Sstevel@tonic-gate  *			res_reset
2460Sstevel@tonic-gate  *
2470Sstevel@tonic-gate  * Description:
2480Sstevel@tonic-gate  *	Resets the resource file in preparation to rebuild it.
2490Sstevel@tonic-gate  * Arguments:
2500Sstevel@tonic-gate  *	resp	res object
2510Sstevel@tonic-gate  * Returns:
2520Sstevel@tonic-gate  * Preconditions:
2530Sstevel@tonic-gate  *	precond(resp is a valid res object)
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate void
res_reset(res * resp)2570Sstevel@tonic-gate res_reset(res *resp)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	int index;
2600Sstevel@tonic-gate 	rl_entry_t *rlentp;
2610Sstevel@tonic-gate 	int ret;
2620Sstevel@tonic-gate 	cachefs_rl_listhead_t *lhp;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	precond(resp);
2650Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	resp->p_cusagep->cu_blksused = 0;
2680Sstevel@tonic-gate 	resp->p_cusagep->cu_filesused = 0;
2690Sstevel@tonic-gate 	resp->p_cusagep->cu_flags = CUSAGE_ACTIVE;	/* dirty cache */
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	/* clear out the non-pointer info */
2720Sstevel@tonic-gate 	for (index = MININDEX; index < resp->p_totentries; index++) {
2730Sstevel@tonic-gate 		rlentp = res_rlent_get(resp, index);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 		rlentp->rl_attrc = 0;
2760Sstevel@tonic-gate 		rlentp->rl_fsck = 0;
2770Sstevel@tonic-gate 		rlentp->rl_local = 0;
2780Sstevel@tonic-gate 		rlentp->rl_fsid = 0LL;
2790Sstevel@tonic-gate 		rlentp->rl_fileno = 0;
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/* verify validity of the various lists */
2830Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_GC);
2840Sstevel@tonic-gate 	if (ret == 1) {
2850Sstevel@tonic-gate 		ret = res_listcheck(resp, CACHEFS_RL_ATTRFILE);
2860Sstevel@tonic-gate 		if (ret == 1) {
2870Sstevel@tonic-gate 			ret = res_listcheck(resp, CACHEFS_RL_MODIFIED);
2880Sstevel@tonic-gate 			if (ret == 1) {
2890Sstevel@tonic-gate 				ret = res_listcheck(resp, CACHEFS_RL_PACKED);
2900Sstevel@tonic-gate 				if (ret == 1) {
2910Sstevel@tonic-gate 					ret = res_listcheck(resp,
2920Sstevel@tonic-gate 					    CACHEFS_RL_PACKED_PENDING);
2930Sstevel@tonic-gate 				}
2940Sstevel@tonic-gate 			}
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/* if an error occurred on one of the lists */
2990Sstevel@tonic-gate 	if (ret == 0) {
3000Sstevel@tonic-gate 		res_clear(resp);
3010Sstevel@tonic-gate 		return;
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/* zero out total sizes, they get fixed up as we add items */
3050Sstevel@tonic-gate 	RL_HEAD(resp, CACHEFS_RL_GC)->rli_blkcnt = 0;
3060Sstevel@tonic-gate 	RL_HEAD(resp, CACHEFS_RL_ATTRFILE)->rli_blkcnt = 0;
3070Sstevel@tonic-gate 	RL_HEAD(resp, CACHEFS_RL_MODIFIED)->rli_blkcnt = 0;
3080Sstevel@tonic-gate 	RL_HEAD(resp, CACHEFS_RL_PACKED)->rli_blkcnt = 0;
3090Sstevel@tonic-gate 	RL_HEAD(resp, CACHEFS_RL_PACKED_PENDING)->rli_blkcnt = 0;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/* null out the heads of the lists we do not want to preserve */
3120Sstevel@tonic-gate 	lhp = RL_HEAD(resp, CACHEFS_RL_FREE);
3130Sstevel@tonic-gate 	memset(lhp, 0, sizeof (cachefs_rl_listhead_t));
3140Sstevel@tonic-gate 	lhp = RL_HEAD(resp, CACHEFS_RL_NONE);
3150Sstevel@tonic-gate 	memset(lhp, 0, sizeof (cachefs_rl_listhead_t));
3160Sstevel@tonic-gate 	lhp = RL_HEAD(resp, CACHEFS_RL_MF);
3170Sstevel@tonic-gate 	memset(lhp, 0, sizeof (cachefs_rl_listhead_t));
3180Sstevel@tonic-gate 	lhp = RL_HEAD(resp, CACHEFS_RL_ACTIVE);
3190Sstevel@tonic-gate 	memset(lhp, 0, sizeof (cachefs_rl_listhead_t));
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate  *
3240Sstevel@tonic-gate  *			res_listcheck
3250Sstevel@tonic-gate  *
3260Sstevel@tonic-gate  * Description:
3270Sstevel@tonic-gate  *	Checks the specified list.
3280Sstevel@tonic-gate  * Arguments:
3290Sstevel@tonic-gate  *	resp	res object
3300Sstevel@tonic-gate  *	type	list to check
3310Sstevel@tonic-gate  * Returns:
3320Sstevel@tonic-gate  *	Returns 1 if the list is ok, 0 if there is a problem.
3330Sstevel@tonic-gate  * Preconditions:
3340Sstevel@tonic-gate  *	precond(resp is a valid res object)
3350Sstevel@tonic-gate  */
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate int
res_listcheck(res * resp,enum cachefs_rl_type type)3380Sstevel@tonic-gate res_listcheck(res *resp, enum cachefs_rl_type type)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	rl_entry_t *rlentp;
3410Sstevel@tonic-gate 	int previndex, index;
3420Sstevel@tonic-gate 	cachefs_rl_listhead_t *lhp;
3430Sstevel@tonic-gate 	int itemcnt = 0;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	lhp = RL_HEAD(resp, type);
3460Sstevel@tonic-gate 	index = lhp->rli_front;
3470Sstevel@tonic-gate 	previndex = 0;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	/* walk the list */
3500Sstevel@tonic-gate 	while (index != 0) {
3510Sstevel@tonic-gate 		itemcnt++;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		/* make sure offset is in bounds */
3540Sstevel@tonic-gate 		if ((index < MININDEX) || (index >= resp->p_totentries)) {
3550Sstevel@tonic-gate 			if (resp->p_verbose)
3560Sstevel@tonic-gate 				pr_err("index out of bounds %d", index);
3570Sstevel@tonic-gate 			return (0);
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 		/* get pointer to rl_entry object */
3610Sstevel@tonic-gate 		rlentp = res_rlent_get(resp, index);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 		/* check forward pointer */
3640Sstevel@tonic-gate 		if (rlentp->rl_fwd_idx != previndex) {
3650Sstevel@tonic-gate 			/* bad back pointer in rl list */
3660Sstevel@tonic-gate 			if (resp->p_verbose)
3670Sstevel@tonic-gate 				pr_err(gettext("bad forward pointer %d %d"),
3680Sstevel@tonic-gate 				    rlentp->rl_fwd_idx, previndex);
3690Sstevel@tonic-gate 			return (0);
3700Sstevel@tonic-gate 		}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		/* check for cycle */
3730Sstevel@tonic-gate 		if (rlentp->rl_fsck) {
3740Sstevel@tonic-gate 			/* cycle found in list */
3750Sstevel@tonic-gate 			if (resp->p_verbose)
3760Sstevel@tonic-gate 				pr_err(gettext("cycle found in list %d"),
3770Sstevel@tonic-gate 				    index);
3780Sstevel@tonic-gate 			return (0);
3790Sstevel@tonic-gate 		}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 		/* check type */
3820Sstevel@tonic-gate 		if (rlentp->rl_current != type) {
3830Sstevel@tonic-gate 			/* entry doesn't belong here */
3840Sstevel@tonic-gate 			if (resp->p_verbose)
3850Sstevel@tonic-gate 				pr_err(gettext(
3860Sstevel@tonic-gate 				    "bad entry %d type %d in list type %d"),
3870Sstevel@tonic-gate 				    index, (int)rlentp->rl_current, (int)type);
3880Sstevel@tonic-gate 			return (0);
3890Sstevel@tonic-gate 		}
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 		/* indicate we have seen this pointer */
3920Sstevel@tonic-gate 		rlentp->rl_fsck = 1;
3930Sstevel@tonic-gate 		previndex = index;
3940Sstevel@tonic-gate 		index = rlentp->rl_bkwd_idx;
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/* verify number of items match */
3980Sstevel@tonic-gate 	if (itemcnt != lhp->rli_itemcnt) {
3990Sstevel@tonic-gate 		if (resp->p_verbose)
4000Sstevel@tonic-gate 			pr_err(gettext("itemcnt wrong old %d  new %d"),
4010Sstevel@tonic-gate 			    lhp->rli_itemcnt, itemcnt);
4020Sstevel@tonic-gate 		return (0);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	return (1);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate /*
4090Sstevel@tonic-gate  *
4100Sstevel@tonic-gate  *			res_clear
4110Sstevel@tonic-gate  *
4120Sstevel@tonic-gate  * Description:
4130Sstevel@tonic-gate  *	Deletes all information from the resource file.
4140Sstevel@tonic-gate  * Arguments:
4150Sstevel@tonic-gate  *	resp	res object
4160Sstevel@tonic-gate  * Returns:
4170Sstevel@tonic-gate  * Preconditions:
4180Sstevel@tonic-gate  *	precond(resp is a valid res object)
4190Sstevel@tonic-gate  */
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate void
res_clear(res * resp)4220Sstevel@tonic-gate res_clear(res *resp)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	memset(resp->p_addrp, 0, resp->p_size);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate  *
4300Sstevel@tonic-gate  *			res_done
4310Sstevel@tonic-gate  *
4320Sstevel@tonic-gate  * Description:
4330Sstevel@tonic-gate  *	Called when through performing res_addfile and res_addident
4340Sstevel@tonic-gate  *	to complete the resource file and flush the contents to
4350Sstevel@tonic-gate  *	the disk file.
4360Sstevel@tonic-gate  * Arguments:
4370Sstevel@tonic-gate  *	resp	res object
4380Sstevel@tonic-gate  * Returns:
4390Sstevel@tonic-gate  *	Returns 0 for success, -1 for an error with errno set
4400Sstevel@tonic-gate  *	appropriatly.
4410Sstevel@tonic-gate  * Preconditions:
4420Sstevel@tonic-gate  *	precond(resp is a valid res object)
4430Sstevel@tonic-gate  */
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate int
res_done(res * resp)4460Sstevel@tonic-gate res_done(res *resp)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	rl_entry_t *rlentp;
4490Sstevel@tonic-gate 	int index;
4500Sstevel@tonic-gate 	int xx;
4510Sstevel@tonic-gate 	int ret;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	precond(resp);
4540Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/* scan the ident list to find the max allocated entry */
4570Sstevel@tonic-gate 	resp->p_linfop->rl_entries = 0;
4580Sstevel@tonic-gate 	for (index = MININDEX; index < resp->p_totentries; index++) {
4590Sstevel@tonic-gate 		rlentp = res_rlent_get(resp, index);
4600Sstevel@tonic-gate 		if (rlentp->rl_fsid && (ino64_t)rlentp->rl_fsck) {
4610Sstevel@tonic-gate 			resp->p_linfop->rl_entries = index;
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/* scan the ident list to fix up the free list */
4660Sstevel@tonic-gate 	for (index = MININDEX; index < resp->p_totentries; index++) {
4670Sstevel@tonic-gate 		rlentp = res_rlent_get(resp, index);
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		/* if entry is not valid */
4700Sstevel@tonic-gate 		if ((rlentp->rl_fsid == 0LL) || (rlentp->rl_fsck == 0)) {
4710Sstevel@tonic-gate 			/* if entry should appear on the free list */
4720Sstevel@tonic-gate 			if (index <= resp->p_linfop->rl_entries) {
4730Sstevel@tonic-gate 				res_rlent_moveto(resp,
4740Sstevel@tonic-gate 				    CACHEFS_RL_FREE, index, 0);
4750Sstevel@tonic-gate 			}
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 		rlentp->rl_fsck = 0; /* prepare to re-check */
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	/*
4810Sstevel@tonic-gate 	 * Sanity check that we do not have an internal error in
4820Sstevel@tonic-gate 	 * fsck.  Eventually turn this stuff off.
4830Sstevel@tonic-gate 	 */
4840Sstevel@tonic-gate #if 1
4850Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_GC);
4860Sstevel@tonic-gate 	assert(ret == 1);
4870Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_ATTRFILE);
4880Sstevel@tonic-gate 	assert(ret == 1);
4890Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_MODIFIED);
4900Sstevel@tonic-gate 	assert(ret == 1);
4910Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_PACKED);
4920Sstevel@tonic-gate 	assert(ret == 1);
4930Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_PACKED_PENDING);
4940Sstevel@tonic-gate 	assert(ret == 1);
4950Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_FREE);
4960Sstevel@tonic-gate 	assert(ret == 1);
4970Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_NONE);
4980Sstevel@tonic-gate 	assert(ret == 1);
4990Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_MF);
5000Sstevel@tonic-gate 	assert(ret == 1);
5010Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_ACTIVE);
5020Sstevel@tonic-gate 	assert(ret == 1);
5030Sstevel@tonic-gate #endif
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	/* indicate the cache is clean */
5060Sstevel@tonic-gate 	resp->p_cusagep->cu_flags &= ~CUSAGE_ACTIVE;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/* sync the data to the file */
5090Sstevel@tonic-gate 	xx = msync(resp->p_addrp, resp->p_size, MS_SYNC);
5100Sstevel@tonic-gate 	if (xx == -1)
5110Sstevel@tonic-gate 		return (-1);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	resp->p_done = 1;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	/* return success */
5160Sstevel@tonic-gate 	return (0);
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate /*
5200Sstevel@tonic-gate  *
5210Sstevel@tonic-gate  *			res_addfile
5220Sstevel@tonic-gate  *
5230Sstevel@tonic-gate  * Description:
5240Sstevel@tonic-gate  *	Increments the number of files and blocks resource counts.
5250Sstevel@tonic-gate  * Arguments:
5260Sstevel@tonic-gate  *	resp	res object
5270Sstevel@tonic-gate  *	nbytes	number of bytes in the file
5280Sstevel@tonic-gate  * Returns:
5290Sstevel@tonic-gate  * Preconditions:
5300Sstevel@tonic-gate  *	precond(resp is a valid res object)
5310Sstevel@tonic-gate  */
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate void
res_addfile(res * resp,long nbytes)5340Sstevel@tonic-gate res_addfile(res *resp, long nbytes)
5350Sstevel@tonic-gate {
5360Sstevel@tonic-gate 	precond(resp);
5370Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/* update resource counts */
5400Sstevel@tonic-gate 	resp->p_cusagep->cu_blksused += CVBLKS(nbytes);
5410Sstevel@tonic-gate 	resp->p_cusagep->cu_filesused += 1;
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate /*
5450Sstevel@tonic-gate  *
5460Sstevel@tonic-gate  *			res_addident
5470Sstevel@tonic-gate  *
5480Sstevel@tonic-gate  * Description:
5490Sstevel@tonic-gate  *	Adds the specified file to the ident list.
5500Sstevel@tonic-gate  *	Updates resource counts.
5510Sstevel@tonic-gate  * Arguments:
5520Sstevel@tonic-gate  *	resp	res object
5530Sstevel@tonic-gate  *	index	index into idents/pointers tables
5540Sstevel@tonic-gate  *	dp	ident information
5550Sstevel@tonic-gate  *	nbytes	number of bytes of item
5560Sstevel@tonic-gate  *	file	number of files of item
5570Sstevel@tonic-gate  * Returns:
5580Sstevel@tonic-gate  *	Returns 0 for success or -1 if the index is already in use
5590Sstevel@tonic-gate  *	or is not valid.
5600Sstevel@tonic-gate  * Preconditions:
5610Sstevel@tonic-gate  *	precond(resp is a valid res object)
5620Sstevel@tonic-gate  *	precond(dp)
5630Sstevel@tonic-gate  */
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate int
res_addident(res * resp,int index,rl_entry_t * dp,long nbytes,int file)5660Sstevel@tonic-gate res_addident(res *resp, int index, rl_entry_t *dp, long nbytes, int file)
5670Sstevel@tonic-gate {
5680Sstevel@tonic-gate 	rl_entry_t *rlentp;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	precond(resp);
5710Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
5720Sstevel@tonic-gate 	precond(dp);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	/* check index for sanity */
5750Sstevel@tonic-gate 	if ((index < MININDEX) || (index >= resp->p_totentries)) {
5760Sstevel@tonic-gate 		return (-1);
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	/* get pointer to ident */
5800Sstevel@tonic-gate 	rlentp = res_rlent_get(resp, index);
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	/* if something already there */
5830Sstevel@tonic-gate 	if (rlentp->rl_fsid != 0LL) {
5840Sstevel@tonic-gate 		return (-1);
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/* if not on the right list, move it there */
5880Sstevel@tonic-gate 	if ((rlentp->rl_fsck == 0) || (rlentp->rl_current != dp->rl_current))
5890Sstevel@tonic-gate 		res_rlent_moveto(resp, dp->rl_current, index, CVBLKS(nbytes));
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	rlentp->rl_fsck = 1;
5920Sstevel@tonic-gate 	rlentp->rl_local = dp->rl_local;
5930Sstevel@tonic-gate 	rlentp->rl_attrc = dp->rl_attrc;
5940Sstevel@tonic-gate 	rlentp->rl_fsid = dp->rl_fsid;
5950Sstevel@tonic-gate 	rlentp->rl_fileno = dp->rl_fileno;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/* update resource counts */
5980Sstevel@tonic-gate 	resp->p_cusagep->cu_blksused += CVBLKS(nbytes);
5990Sstevel@tonic-gate 	resp->p_cusagep->cu_filesused += file;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/* return success */
6020Sstevel@tonic-gate 	return (0);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate /*
6060Sstevel@tonic-gate  *
6070Sstevel@tonic-gate  *			res_clearident
6080Sstevel@tonic-gate  *
6090Sstevel@tonic-gate  * Description:
6100Sstevel@tonic-gate  *	Removes the specified file from the ident list.
6110Sstevel@tonic-gate  *	Updates resource counts.
6120Sstevel@tonic-gate  * Arguments:
6130Sstevel@tonic-gate  *	resp	res object
6140Sstevel@tonic-gate  *	index	index into idents/pointers tables
6150Sstevel@tonic-gate  *	nbytes	number of bytes in the file
6160Sstevel@tonic-gate  *	file	number of files
6170Sstevel@tonic-gate  * Returns:
6180Sstevel@tonic-gate  *	Returns 0.
6190Sstevel@tonic-gate  * Preconditions:
6200Sstevel@tonic-gate  *	precond(resp is a valid res object)
6210Sstevel@tonic-gate  *	precond(index is valid)
6220Sstevel@tonic-gate  *	precond(ident is in use)
6230Sstevel@tonic-gate  */
6240Sstevel@tonic-gate 
625*633Sgt29601 void
res_clearident(res * resp,int index,int nbytes,int file)6260Sstevel@tonic-gate res_clearident(res *resp, int index, int nbytes, int file)
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate 	rl_entry_t *rlentp;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	precond(resp);
6310Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
6320Sstevel@tonic-gate 	precond((index >= MININDEX) && (index < resp->p_totentries));
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/* get pointer to ident */
6350Sstevel@tonic-gate 	rlentp = res_rlent_get(resp, index);
6360Sstevel@tonic-gate 	precond(rlentp->rl_fsid != 0LL);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/* clear the ident */
6390Sstevel@tonic-gate 	rlentp->rl_fsid = 0LL;
6400Sstevel@tonic-gate 	rlentp->rl_fileno = 0;
6410Sstevel@tonic-gate 	rlentp->rl_attrc = 0;
6420Sstevel@tonic-gate 	rlentp->rl_local = 0;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	/* update resource counts */
6450Sstevel@tonic-gate 	resp->p_cusagep->cu_blksused -= CVBLKS(nbytes);
6460Sstevel@tonic-gate 	resp->p_cusagep->cu_filesused -= file;
6470Sstevel@tonic-gate 	assert(resp->p_cusagep->cu_blksused >= 0);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  * This function moves an RL entry from whereever it currently is to
6520Sstevel@tonic-gate  * the requested list.
6530Sstevel@tonic-gate  */
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate void
res_rlent_moveto(res * resp,enum cachefs_rl_type type,uint_t entno,long blks)656*633Sgt29601 res_rlent_moveto(res *resp, enum cachefs_rl_type type, uint_t entno, long blks)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	rl_entry_t *rl_ent;
659*633Sgt29601 	uint_t prev, next;
6600Sstevel@tonic-gate 	cachefs_rl_listhead_t *lhp;
6610Sstevel@tonic-gate 	enum cachefs_rl_type otype;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	precond((CACHEFS_RL_START <= type) && (type <= CACHEFS_RL_END));
6640Sstevel@tonic-gate 	precond((entno >= MININDEX) && (entno < resp->p_totentries));
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	rl_ent = res_rlent_get(resp, entno);
6670Sstevel@tonic-gate 	if (rl_ent->rl_fsck) {
6680Sstevel@tonic-gate 		/* remove entry from its previous list */
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 		next = rl_ent->rl_fwd_idx;
6710Sstevel@tonic-gate 		prev = rl_ent->rl_bkwd_idx;
6720Sstevel@tonic-gate 		otype = rl_ent->rl_current;
6730Sstevel@tonic-gate 		assert((CACHEFS_RL_START <= otype) &&
6740Sstevel@tonic-gate 		    (otype <= CACHEFS_RL_END));
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		lhp = RL_HEAD(resp, otype);
6770Sstevel@tonic-gate 		if ((lhp->rli_back == 0) || (lhp->rli_front == 0))
6780Sstevel@tonic-gate 			assert((lhp->rli_back == 0) && (lhp->rli_front == 0));
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		if (lhp->rli_back == entno)
6810Sstevel@tonic-gate 			lhp->rli_back = next;
6820Sstevel@tonic-gate 		if (lhp->rli_front == entno)
6830Sstevel@tonic-gate 			lhp->rli_front = prev;
6840Sstevel@tonic-gate 		if (prev != 0) {
6850Sstevel@tonic-gate 			rl_ent = res_rlent_get(resp, prev);
6860Sstevel@tonic-gate 			rl_ent->rl_fwd_idx = next;
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 		if (next != 0) {
6890Sstevel@tonic-gate 			rl_ent = res_rlent_get(resp, next);
6900Sstevel@tonic-gate 			rl_ent->rl_bkwd_idx = prev;
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 		lhp->rli_blkcnt -= blks;
6930Sstevel@tonic-gate 		lhp->rli_itemcnt--;
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	/* add entry to its new list */
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	lhp = RL_HEAD(resp, type);
6990Sstevel@tonic-gate 	rl_ent = res_rlent_get(resp, entno);
7000Sstevel@tonic-gate 	rl_ent->rl_current = type;
7010Sstevel@tonic-gate 	rl_ent->rl_bkwd_idx = 0;
7020Sstevel@tonic-gate 	rl_ent->rl_fwd_idx = lhp->rli_back;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	if (lhp->rli_back != 0) {
7050Sstevel@tonic-gate 		assert(lhp->rli_front != 0);
7060Sstevel@tonic-gate 		rl_ent = res_rlent_get(resp, lhp->rli_back);
7070Sstevel@tonic-gate 		rl_ent->rl_bkwd_idx = entno;
7080Sstevel@tonic-gate 	} else {
7090Sstevel@tonic-gate 		assert(lhp->rli_front == 0);
7100Sstevel@tonic-gate 		lhp->rli_front = entno;
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 	lhp->rli_back = entno;
7130Sstevel@tonic-gate 	lhp->rli_blkcnt += blks;
7140Sstevel@tonic-gate 	lhp->rli_itemcnt++;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	rl_ent = res_rlent_get(resp, entno);
7170Sstevel@tonic-gate 	rl_ent->rl_current = type;
7180Sstevel@tonic-gate 	rl_ent->rl_fsck = 1;
7190Sstevel@tonic-gate }
720