xref: /titanic_44/usr/src/cmd/fs.d/cachefs/fsck/res.c (revision 47644099886aa8d3f43120b9eede3044342be473)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*47644099Sgt29601  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*47644099Sgt29601  * Use is subject to license terms.
25*47644099Sgt29601  */
26*47644099Sgt29601 /*
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  *			res.c
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * Implements routines to create a cache resource file.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <assert.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
427c478bd9Sstevel@tonic-gate #include <sys/mman.h>
437c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
447c478bd9Sstevel@tonic-gate #include "res.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate struct res {
477c478bd9Sstevel@tonic-gate 	int			 p_magic;	/* magic number */
487c478bd9Sstevel@tonic-gate 	int			 p_done:1;	/* 1 if res_done called */
497c478bd9Sstevel@tonic-gate 	int			 p_verbose:1;	/* 1 means print errors */
507c478bd9Sstevel@tonic-gate 	void			*p_addrp;	/* address of mapped file */
517c478bd9Sstevel@tonic-gate 	long			 p_size;	/* size of mapped file */
527c478bd9Sstevel@tonic-gate 	struct cache_usage	*p_cusagep;	/* ptr to cache_usage */
537c478bd9Sstevel@tonic-gate 	struct cachefs_rl_info	*p_linfop;	/* ptr to rl_info */
547c478bd9Sstevel@tonic-gate 	rl_entry_t		*p_rlentp;	/* ptr to first rl_entry */
557c478bd9Sstevel@tonic-gate 	int			 p_totentries;	/* max number of rl entries */
567c478bd9Sstevel@tonic-gate 	char		 p_name[MAXPATHLEN];	/* name of resource file */
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	MAGIC 8272
607c478bd9Sstevel@tonic-gate #define	precond(A) assert(A)
617c478bd9Sstevel@tonic-gate #define	MININDEX 1
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	RL_HEAD(resp, type) \
647c478bd9Sstevel@tonic-gate 	(&(resp->p_linfop->rl_items[CACHEFS_RL_INDEX(type)]))
657c478bd9Sstevel@tonic-gate #define	CVBLKS(nbytes) ((nbytes + MAXBSIZE - 1) / MAXBSIZE)
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /* forward references */
68*47644099Sgt29601 void res_rlent_moveto(res *resp, enum cachefs_rl_type type, uint_t entno,
697c478bd9Sstevel@tonic-gate     long blks);
707c478bd9Sstevel@tonic-gate void res_reset(res *resp);
717c478bd9Sstevel@tonic-gate void res_clear(res *resp);
727c478bd9Sstevel@tonic-gate int res_listcheck(res *, enum cachefs_rl_type);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  *			res_create
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  * Description:
797c478bd9Sstevel@tonic-gate  *	Creates a res object and returns a pointer to it.
807c478bd9Sstevel@tonic-gate  *	The specified file is used to store resource file data.
817c478bd9Sstevel@tonic-gate  * Arguments:
827c478bd9Sstevel@tonic-gate  *	namep	name of the resource file
837c478bd9Sstevel@tonic-gate  *	entries	max number of rl entries in the file
847c478bd9Sstevel@tonic-gate  *	verbose 1 means print out error messages
857c478bd9Sstevel@tonic-gate  * Returns:
867c478bd9Sstevel@tonic-gate  *	Returns a pointer to the object or NULL if an error occurred.
877c478bd9Sstevel@tonic-gate  * Preconditions:
887c478bd9Sstevel@tonic-gate  *	precond(namep)
897c478bd9Sstevel@tonic-gate  *	precond(entries > 3)
907c478bd9Sstevel@tonic-gate  *	precond(strlen(namep) < MAXPATHLEN)
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate res *
res_create(char * namep,int entries,int verbose)947c478bd9Sstevel@tonic-gate res_create(char *namep, int entries, int verbose)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	int xx;
977c478bd9Sstevel@tonic-gate 	long size;
987c478bd9Sstevel@tonic-gate 	int fd;
997c478bd9Sstevel@tonic-gate 	char buf[1024];
1007c478bd9Sstevel@tonic-gate 	long cnt;
1017c478bd9Sstevel@tonic-gate 	unsigned int amt;
1027c478bd9Sstevel@tonic-gate 	ssize_t result;
1037c478bd9Sstevel@tonic-gate 	void *addrp;
1047c478bd9Sstevel@tonic-gate 	res *resp;
1057c478bd9Sstevel@tonic-gate 	struct stat64 statinfo;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	precond(namep);
1087c478bd9Sstevel@tonic-gate 	precond(entries > MININDEX);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* determine the size needed for the resource file */
1117c478bd9Sstevel@tonic-gate 	size = MAXBSIZE;
1127c478bd9Sstevel@tonic-gate 	size += MAXBSIZE * (entries / CACHEFS_RLPMBS);
1137c478bd9Sstevel@tonic-gate 	if ((entries %  CACHEFS_RLPMBS) != 0)
1147c478bd9Sstevel@tonic-gate 		size += MAXBSIZE;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/* if the file does not exist or is the wrong size/type */
1177c478bd9Sstevel@tonic-gate 	xx = lstat64(namep, &statinfo);
1187c478bd9Sstevel@tonic-gate 	/* resource file will be <2GB */
1197c478bd9Sstevel@tonic-gate 	if ((xx == -1) || (statinfo.st_size != (offset_t)size) ||
1207c478bd9Sstevel@tonic-gate 	    !(S_ISREG(statinfo.st_mode))) {
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		/* remove the resource file */
1237c478bd9Sstevel@tonic-gate 		xx = unlink(namep);
1247c478bd9Sstevel@tonic-gate 		if ((xx == -1) && (errno != ENOENT))
1257c478bd9Sstevel@tonic-gate 			return (NULL);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		/* create and open the file */
1287c478bd9Sstevel@tonic-gate 		fd = open(namep, O_CREAT | O_RDWR, 0600);
1297c478bd9Sstevel@tonic-gate 		if (fd == -1)
1307c478bd9Sstevel@tonic-gate 			return (NULL);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		/* fill the file with zeros */
1337c478bd9Sstevel@tonic-gate 		memset(buf, 0, sizeof (buf));
1347c478bd9Sstevel@tonic-gate 		for (cnt = size; cnt > 0; cnt -= result) {
1357c478bd9Sstevel@tonic-gate 			amt = sizeof (buf);
1367c478bd9Sstevel@tonic-gate 			if (amt > cnt)
1377c478bd9Sstevel@tonic-gate 				amt = cnt;
1387c478bd9Sstevel@tonic-gate 			result = write(fd, buf, amt);
1397c478bd9Sstevel@tonic-gate 			if (result == -1) {
1407c478bd9Sstevel@tonic-gate 				close(fd);
1417c478bd9Sstevel@tonic-gate 				return (NULL);
1427c478bd9Sstevel@tonic-gate 			}
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/* else open the file */
1477c478bd9Sstevel@tonic-gate 	else {
1487c478bd9Sstevel@tonic-gate 		fd = open(namep, O_RDWR);
1497c478bd9Sstevel@tonic-gate 		if (fd == -1)
1507c478bd9Sstevel@tonic-gate 			return (NULL);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/* mmap the file into our address space */
1547c478bd9Sstevel@tonic-gate 	addrp = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1557c478bd9Sstevel@tonic-gate 	if (addrp == (void *)-1) {
1567c478bd9Sstevel@tonic-gate 		close(fd);
1577c478bd9Sstevel@tonic-gate 		return (NULL);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/* close the file descriptor, we do not need it anymore */
1617c478bd9Sstevel@tonic-gate 	close(fd);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/* allocate memory for the res object */
1647c478bd9Sstevel@tonic-gate 	resp = malloc(sizeof (res));
1657c478bd9Sstevel@tonic-gate 	if (resp == NULL) {
1667c478bd9Sstevel@tonic-gate 		munmap(addrp, size);
1677c478bd9Sstevel@tonic-gate 		return (NULL);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/* initialize the object */
1717c478bd9Sstevel@tonic-gate 	resp->p_magic = MAGIC;
1727c478bd9Sstevel@tonic-gate 	resp->p_done = 0;
1737c478bd9Sstevel@tonic-gate 	resp->p_addrp = addrp;
1747c478bd9Sstevel@tonic-gate 	resp->p_size = size;
1757c478bd9Sstevel@tonic-gate 	resp->p_verbose = verbose;
1767c478bd9Sstevel@tonic-gate 	resp->p_cusagep = (struct cache_usage *)addrp;
1777c478bd9Sstevel@tonic-gate 	resp->p_linfop = (struct cachefs_rl_info *)((char *)addrp +
1787c478bd9Sstevel@tonic-gate 	    sizeof (struct cache_usage));
1797c478bd9Sstevel@tonic-gate 	resp->p_rlentp = (rl_entry_t *)((char *)addrp + MAXBSIZE);
1807c478bd9Sstevel@tonic-gate 	resp->p_totentries = entries;
1817c478bd9Sstevel@tonic-gate 	strcpy(resp->p_name, namep);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* reset the resource file in preperation to rebuild it */
1847c478bd9Sstevel@tonic-gate 	res_reset(resp);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/* return the object */
1877c478bd9Sstevel@tonic-gate 	return (resp);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  *
1927c478bd9Sstevel@tonic-gate  *			res_destroy
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  * Description:
1957c478bd9Sstevel@tonic-gate  *	Destroys the specifed res object.
1967c478bd9Sstevel@tonic-gate  *	If res_done has not been called on the object or if res_done
1977c478bd9Sstevel@tonic-gate  *	failed, then the resource file will be deleted.
1987c478bd9Sstevel@tonic-gate  * Arguments:
1997c478bd9Sstevel@tonic-gate  *	resp	object to destroy
2007c478bd9Sstevel@tonic-gate  * Returns:
2017c478bd9Sstevel@tonic-gate  * Preconditions:
2027c478bd9Sstevel@tonic-gate  *	precond(resp is a valid res object)
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate void
res_destroy(res * resp)2067c478bd9Sstevel@tonic-gate res_destroy(res *resp)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	precond(resp);
2097c478bd9Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/* unmap the file */
2127c478bd9Sstevel@tonic-gate 	munmap(resp->p_addrp, resp->p_size);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* if res_done not performed */
2157c478bd9Sstevel@tonic-gate 	if (resp->p_done == 0) {
2167c478bd9Sstevel@tonic-gate 		/* remove the resource file */
2177c478bd9Sstevel@tonic-gate 		unlink(resp->p_name);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/* destroy the object */
2217c478bd9Sstevel@tonic-gate 	resp->p_magic = -MAGIC;
2227c478bd9Sstevel@tonic-gate 	free(resp);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate rl_entry_t *
res_rlent_get(res * resp,uint_t entno)226*47644099Sgt29601 res_rlent_get(res *resp, uint_t entno)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	rl_entry_t *rlentp, *window;
229*47644099Sgt29601 	uint_t whichwindow, winoffset;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	precond((entno >= MININDEX) && (entno < resp->p_totentries));
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	whichwindow = entno / CACHEFS_RLPMBS;
2347c478bd9Sstevel@tonic-gate 	winoffset = entno % CACHEFS_RLPMBS;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	window = (rl_entry_t *)
2377c478bd9Sstevel@tonic-gate 	    (((caddr_t)resp->p_rlentp) + (MAXBSIZE * whichwindow));
2387c478bd9Sstevel@tonic-gate 	rlentp = window + winoffset;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	return (rlentp);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  *
2457c478bd9Sstevel@tonic-gate  *			res_reset
2467c478bd9Sstevel@tonic-gate  *
2477c478bd9Sstevel@tonic-gate  * Description:
2487c478bd9Sstevel@tonic-gate  *	Resets the resource file in preparation to rebuild it.
2497c478bd9Sstevel@tonic-gate  * Arguments:
2507c478bd9Sstevel@tonic-gate  *	resp	res object
2517c478bd9Sstevel@tonic-gate  * Returns:
2527c478bd9Sstevel@tonic-gate  * Preconditions:
2537c478bd9Sstevel@tonic-gate  *	precond(resp is a valid res object)
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate void
res_reset(res * resp)2577c478bd9Sstevel@tonic-gate res_reset(res *resp)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	int index;
2607c478bd9Sstevel@tonic-gate 	rl_entry_t *rlentp;
2617c478bd9Sstevel@tonic-gate 	int ret;
2627c478bd9Sstevel@tonic-gate 	cachefs_rl_listhead_t *lhp;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	precond(resp);
2657c478bd9Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	resp->p_cusagep->cu_blksused = 0;
2687c478bd9Sstevel@tonic-gate 	resp->p_cusagep->cu_filesused = 0;
2697c478bd9Sstevel@tonic-gate 	resp->p_cusagep->cu_flags = CUSAGE_ACTIVE;	/* dirty cache */
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/* clear out the non-pointer info */
2727c478bd9Sstevel@tonic-gate 	for (index = MININDEX; index < resp->p_totentries; index++) {
2737c478bd9Sstevel@tonic-gate 		rlentp = res_rlent_get(resp, index);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		rlentp->rl_attrc = 0;
2767c478bd9Sstevel@tonic-gate 		rlentp->rl_fsck = 0;
2777c478bd9Sstevel@tonic-gate 		rlentp->rl_local = 0;
2787c478bd9Sstevel@tonic-gate 		rlentp->rl_fsid = 0LL;
2797c478bd9Sstevel@tonic-gate 		rlentp->rl_fileno = 0;
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/* verify validity of the various lists */
2837c478bd9Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_GC);
2847c478bd9Sstevel@tonic-gate 	if (ret == 1) {
2857c478bd9Sstevel@tonic-gate 		ret = res_listcheck(resp, CACHEFS_RL_ATTRFILE);
2867c478bd9Sstevel@tonic-gate 		if (ret == 1) {
2877c478bd9Sstevel@tonic-gate 			ret = res_listcheck(resp, CACHEFS_RL_MODIFIED);
2887c478bd9Sstevel@tonic-gate 			if (ret == 1) {
2897c478bd9Sstevel@tonic-gate 				ret = res_listcheck(resp, CACHEFS_RL_PACKED);
2907c478bd9Sstevel@tonic-gate 				if (ret == 1) {
2917c478bd9Sstevel@tonic-gate 					ret = res_listcheck(resp,
2927c478bd9Sstevel@tonic-gate 					    CACHEFS_RL_PACKED_PENDING);
2937c478bd9Sstevel@tonic-gate 				}
2947c478bd9Sstevel@tonic-gate 			}
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/* if an error occurred on one of the lists */
2997c478bd9Sstevel@tonic-gate 	if (ret == 0) {
3007c478bd9Sstevel@tonic-gate 		res_clear(resp);
3017c478bd9Sstevel@tonic-gate 		return;
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* zero out total sizes, they get fixed up as we add items */
3057c478bd9Sstevel@tonic-gate 	RL_HEAD(resp, CACHEFS_RL_GC)->rli_blkcnt = 0;
3067c478bd9Sstevel@tonic-gate 	RL_HEAD(resp, CACHEFS_RL_ATTRFILE)->rli_blkcnt = 0;
3077c478bd9Sstevel@tonic-gate 	RL_HEAD(resp, CACHEFS_RL_MODIFIED)->rli_blkcnt = 0;
3087c478bd9Sstevel@tonic-gate 	RL_HEAD(resp, CACHEFS_RL_PACKED)->rli_blkcnt = 0;
3097c478bd9Sstevel@tonic-gate 	RL_HEAD(resp, CACHEFS_RL_PACKED_PENDING)->rli_blkcnt = 0;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/* null out the heads of the lists we do not want to preserve */
3127c478bd9Sstevel@tonic-gate 	lhp = RL_HEAD(resp, CACHEFS_RL_FREE);
3137c478bd9Sstevel@tonic-gate 	memset(lhp, 0, sizeof (cachefs_rl_listhead_t));
3147c478bd9Sstevel@tonic-gate 	lhp = RL_HEAD(resp, CACHEFS_RL_NONE);
3157c478bd9Sstevel@tonic-gate 	memset(lhp, 0, sizeof (cachefs_rl_listhead_t));
3167c478bd9Sstevel@tonic-gate 	lhp = RL_HEAD(resp, CACHEFS_RL_MF);
3177c478bd9Sstevel@tonic-gate 	memset(lhp, 0, sizeof (cachefs_rl_listhead_t));
3187c478bd9Sstevel@tonic-gate 	lhp = RL_HEAD(resp, CACHEFS_RL_ACTIVE);
3197c478bd9Sstevel@tonic-gate 	memset(lhp, 0, sizeof (cachefs_rl_listhead_t));
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  *
3247c478bd9Sstevel@tonic-gate  *			res_listcheck
3257c478bd9Sstevel@tonic-gate  *
3267c478bd9Sstevel@tonic-gate  * Description:
3277c478bd9Sstevel@tonic-gate  *	Checks the specified list.
3287c478bd9Sstevel@tonic-gate  * Arguments:
3297c478bd9Sstevel@tonic-gate  *	resp	res object
3307c478bd9Sstevel@tonic-gate  *	type	list to check
3317c478bd9Sstevel@tonic-gate  * Returns:
3327c478bd9Sstevel@tonic-gate  *	Returns 1 if the list is ok, 0 if there is a problem.
3337c478bd9Sstevel@tonic-gate  * Preconditions:
3347c478bd9Sstevel@tonic-gate  *	precond(resp is a valid res object)
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate int
res_listcheck(res * resp,enum cachefs_rl_type type)3387c478bd9Sstevel@tonic-gate res_listcheck(res *resp, enum cachefs_rl_type type)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	rl_entry_t *rlentp;
3417c478bd9Sstevel@tonic-gate 	int previndex, index;
3427c478bd9Sstevel@tonic-gate 	cachefs_rl_listhead_t *lhp;
3437c478bd9Sstevel@tonic-gate 	int itemcnt = 0;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	lhp = RL_HEAD(resp, type);
3467c478bd9Sstevel@tonic-gate 	index = lhp->rli_front;
3477c478bd9Sstevel@tonic-gate 	previndex = 0;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/* walk the list */
3507c478bd9Sstevel@tonic-gate 	while (index != 0) {
3517c478bd9Sstevel@tonic-gate 		itemcnt++;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		/* make sure offset is in bounds */
3547c478bd9Sstevel@tonic-gate 		if ((index < MININDEX) || (index >= resp->p_totentries)) {
3557c478bd9Sstevel@tonic-gate 			if (resp->p_verbose)
3567c478bd9Sstevel@tonic-gate 				pr_err("index out of bounds %d", index);
3577c478bd9Sstevel@tonic-gate 			return (0);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		/* get pointer to rl_entry object */
3617c478bd9Sstevel@tonic-gate 		rlentp = res_rlent_get(resp, index);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		/* check forward pointer */
3647c478bd9Sstevel@tonic-gate 		if (rlentp->rl_fwd_idx != previndex) {
3657c478bd9Sstevel@tonic-gate 			/* bad back pointer in rl list */
3667c478bd9Sstevel@tonic-gate 			if (resp->p_verbose)
3677c478bd9Sstevel@tonic-gate 				pr_err(gettext("bad forward pointer %d %d"),
3687c478bd9Sstevel@tonic-gate 				    rlentp->rl_fwd_idx, previndex);
3697c478bd9Sstevel@tonic-gate 			return (0);
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		/* check for cycle */
3737c478bd9Sstevel@tonic-gate 		if (rlentp->rl_fsck) {
3747c478bd9Sstevel@tonic-gate 			/* cycle found in list */
3757c478bd9Sstevel@tonic-gate 			if (resp->p_verbose)
3767c478bd9Sstevel@tonic-gate 				pr_err(gettext("cycle found in list %d"),
3777c478bd9Sstevel@tonic-gate 				    index);
3787c478bd9Sstevel@tonic-gate 			return (0);
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		/* check type */
3827c478bd9Sstevel@tonic-gate 		if (rlentp->rl_current != type) {
3837c478bd9Sstevel@tonic-gate 			/* entry doesn't belong here */
3847c478bd9Sstevel@tonic-gate 			if (resp->p_verbose)
3857c478bd9Sstevel@tonic-gate 				pr_err(gettext(
3867c478bd9Sstevel@tonic-gate 				    "bad entry %d type %d in list type %d"),
3877c478bd9Sstevel@tonic-gate 				    index, (int)rlentp->rl_current, (int)type);
3887c478bd9Sstevel@tonic-gate 			return (0);
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		/* indicate we have seen this pointer */
3927c478bd9Sstevel@tonic-gate 		rlentp->rl_fsck = 1;
3937c478bd9Sstevel@tonic-gate 		previndex = index;
3947c478bd9Sstevel@tonic-gate 		index = rlentp->rl_bkwd_idx;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/* verify number of items match */
3987c478bd9Sstevel@tonic-gate 	if (itemcnt != lhp->rli_itemcnt) {
3997c478bd9Sstevel@tonic-gate 		if (resp->p_verbose)
4007c478bd9Sstevel@tonic-gate 			pr_err(gettext("itemcnt wrong old %d  new %d"),
4017c478bd9Sstevel@tonic-gate 			    lhp->rli_itemcnt, itemcnt);
4027c478bd9Sstevel@tonic-gate 		return (0);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	return (1);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate  *
4107c478bd9Sstevel@tonic-gate  *			res_clear
4117c478bd9Sstevel@tonic-gate  *
4127c478bd9Sstevel@tonic-gate  * Description:
4137c478bd9Sstevel@tonic-gate  *	Deletes all information from the resource file.
4147c478bd9Sstevel@tonic-gate  * Arguments:
4157c478bd9Sstevel@tonic-gate  *	resp	res object
4167c478bd9Sstevel@tonic-gate  * Returns:
4177c478bd9Sstevel@tonic-gate  * Preconditions:
4187c478bd9Sstevel@tonic-gate  *	precond(resp is a valid res object)
4197c478bd9Sstevel@tonic-gate  */
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate void
res_clear(res * resp)4227c478bd9Sstevel@tonic-gate res_clear(res *resp)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	memset(resp->p_addrp, 0, resp->p_size);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate /*
4297c478bd9Sstevel@tonic-gate  *
4307c478bd9Sstevel@tonic-gate  *			res_done
4317c478bd9Sstevel@tonic-gate  *
4327c478bd9Sstevel@tonic-gate  * Description:
4337c478bd9Sstevel@tonic-gate  *	Called when through performing res_addfile and res_addident
4347c478bd9Sstevel@tonic-gate  *	to complete the resource file and flush the contents to
4357c478bd9Sstevel@tonic-gate  *	the disk file.
4367c478bd9Sstevel@tonic-gate  * Arguments:
4377c478bd9Sstevel@tonic-gate  *	resp	res object
4387c478bd9Sstevel@tonic-gate  * Returns:
4397c478bd9Sstevel@tonic-gate  *	Returns 0 for success, -1 for an error with errno set
4407c478bd9Sstevel@tonic-gate  *	appropriatly.
4417c478bd9Sstevel@tonic-gate  * Preconditions:
4427c478bd9Sstevel@tonic-gate  *	precond(resp is a valid res object)
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate int
res_done(res * resp)4467c478bd9Sstevel@tonic-gate res_done(res *resp)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	rl_entry_t *rlentp;
4497c478bd9Sstevel@tonic-gate 	int index;
4507c478bd9Sstevel@tonic-gate 	int xx;
4517c478bd9Sstevel@tonic-gate 	int ret;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	precond(resp);
4547c478bd9Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	/* scan the ident list to find the max allocated entry */
4577c478bd9Sstevel@tonic-gate 	resp->p_linfop->rl_entries = 0;
4587c478bd9Sstevel@tonic-gate 	for (index = MININDEX; index < resp->p_totentries; index++) {
4597c478bd9Sstevel@tonic-gate 		rlentp = res_rlent_get(resp, index);
4607c478bd9Sstevel@tonic-gate 		if (rlentp->rl_fsid && (ino64_t)rlentp->rl_fsck) {
4617c478bd9Sstevel@tonic-gate 			resp->p_linfop->rl_entries = index;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/* scan the ident list to fix up the free list */
4667c478bd9Sstevel@tonic-gate 	for (index = MININDEX; index < resp->p_totentries; index++) {
4677c478bd9Sstevel@tonic-gate 		rlentp = res_rlent_get(resp, index);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		/* if entry is not valid */
4707c478bd9Sstevel@tonic-gate 		if ((rlentp->rl_fsid == 0LL) || (rlentp->rl_fsck == 0)) {
4717c478bd9Sstevel@tonic-gate 			/* if entry should appear on the free list */
4727c478bd9Sstevel@tonic-gate 			if (index <= resp->p_linfop->rl_entries) {
4737c478bd9Sstevel@tonic-gate 				res_rlent_moveto(resp,
4747c478bd9Sstevel@tonic-gate 				    CACHEFS_RL_FREE, index, 0);
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 		rlentp->rl_fsck = 0; /* prepare to re-check */
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Sanity check that we do not have an internal error in
4827c478bd9Sstevel@tonic-gate 	 * fsck.  Eventually turn this stuff off.
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate #if 1
4857c478bd9Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_GC);
4867c478bd9Sstevel@tonic-gate 	assert(ret == 1);
4877c478bd9Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_ATTRFILE);
4887c478bd9Sstevel@tonic-gate 	assert(ret == 1);
4897c478bd9Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_MODIFIED);
4907c478bd9Sstevel@tonic-gate 	assert(ret == 1);
4917c478bd9Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_PACKED);
4927c478bd9Sstevel@tonic-gate 	assert(ret == 1);
4937c478bd9Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_PACKED_PENDING);
4947c478bd9Sstevel@tonic-gate 	assert(ret == 1);
4957c478bd9Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_FREE);
4967c478bd9Sstevel@tonic-gate 	assert(ret == 1);
4977c478bd9Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_NONE);
4987c478bd9Sstevel@tonic-gate 	assert(ret == 1);
4997c478bd9Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_MF);
5007c478bd9Sstevel@tonic-gate 	assert(ret == 1);
5017c478bd9Sstevel@tonic-gate 	ret = res_listcheck(resp, CACHEFS_RL_ACTIVE);
5027c478bd9Sstevel@tonic-gate 	assert(ret == 1);
5037c478bd9Sstevel@tonic-gate #endif
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/* indicate the cache is clean */
5067c478bd9Sstevel@tonic-gate 	resp->p_cusagep->cu_flags &= ~CUSAGE_ACTIVE;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/* sync the data to the file */
5097c478bd9Sstevel@tonic-gate 	xx = msync(resp->p_addrp, resp->p_size, MS_SYNC);
5107c478bd9Sstevel@tonic-gate 	if (xx == -1)
5117c478bd9Sstevel@tonic-gate 		return (-1);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	resp->p_done = 1;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/* return success */
5167c478bd9Sstevel@tonic-gate 	return (0);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /*
5207c478bd9Sstevel@tonic-gate  *
5217c478bd9Sstevel@tonic-gate  *			res_addfile
5227c478bd9Sstevel@tonic-gate  *
5237c478bd9Sstevel@tonic-gate  * Description:
5247c478bd9Sstevel@tonic-gate  *	Increments the number of files and blocks resource counts.
5257c478bd9Sstevel@tonic-gate  * Arguments:
5267c478bd9Sstevel@tonic-gate  *	resp	res object
5277c478bd9Sstevel@tonic-gate  *	nbytes	number of bytes in the file
5287c478bd9Sstevel@tonic-gate  * Returns:
5297c478bd9Sstevel@tonic-gate  * Preconditions:
5307c478bd9Sstevel@tonic-gate  *	precond(resp is a valid res object)
5317c478bd9Sstevel@tonic-gate  */
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate void
res_addfile(res * resp,long nbytes)5347c478bd9Sstevel@tonic-gate res_addfile(res *resp, long nbytes)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	precond(resp);
5377c478bd9Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/* update resource counts */
5407c478bd9Sstevel@tonic-gate 	resp->p_cusagep->cu_blksused += CVBLKS(nbytes);
5417c478bd9Sstevel@tonic-gate 	resp->p_cusagep->cu_filesused += 1;
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate  *
5467c478bd9Sstevel@tonic-gate  *			res_addident
5477c478bd9Sstevel@tonic-gate  *
5487c478bd9Sstevel@tonic-gate  * Description:
5497c478bd9Sstevel@tonic-gate  *	Adds the specified file to the ident list.
5507c478bd9Sstevel@tonic-gate  *	Updates resource counts.
5517c478bd9Sstevel@tonic-gate  * Arguments:
5527c478bd9Sstevel@tonic-gate  *	resp	res object
5537c478bd9Sstevel@tonic-gate  *	index	index into idents/pointers tables
5547c478bd9Sstevel@tonic-gate  *	dp	ident information
5557c478bd9Sstevel@tonic-gate  *	nbytes	number of bytes of item
5567c478bd9Sstevel@tonic-gate  *	file	number of files of item
5577c478bd9Sstevel@tonic-gate  * Returns:
5587c478bd9Sstevel@tonic-gate  *	Returns 0 for success or -1 if the index is already in use
5597c478bd9Sstevel@tonic-gate  *	or is not valid.
5607c478bd9Sstevel@tonic-gate  * Preconditions:
5617c478bd9Sstevel@tonic-gate  *	precond(resp is a valid res object)
5627c478bd9Sstevel@tonic-gate  *	precond(dp)
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate int
res_addident(res * resp,int index,rl_entry_t * dp,long nbytes,int file)5667c478bd9Sstevel@tonic-gate res_addident(res *resp, int index, rl_entry_t *dp, long nbytes, int file)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	rl_entry_t *rlentp;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	precond(resp);
5717c478bd9Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
5727c478bd9Sstevel@tonic-gate 	precond(dp);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	/* check index for sanity */
5757c478bd9Sstevel@tonic-gate 	if ((index < MININDEX) || (index >= resp->p_totentries)) {
5767c478bd9Sstevel@tonic-gate 		return (-1);
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	/* get pointer to ident */
5807c478bd9Sstevel@tonic-gate 	rlentp = res_rlent_get(resp, index);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	/* if something already there */
5837c478bd9Sstevel@tonic-gate 	if (rlentp->rl_fsid != 0LL) {
5847c478bd9Sstevel@tonic-gate 		return (-1);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	/* if not on the right list, move it there */
5887c478bd9Sstevel@tonic-gate 	if ((rlentp->rl_fsck == 0) || (rlentp->rl_current != dp->rl_current))
5897c478bd9Sstevel@tonic-gate 		res_rlent_moveto(resp, dp->rl_current, index, CVBLKS(nbytes));
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	rlentp->rl_fsck = 1;
5927c478bd9Sstevel@tonic-gate 	rlentp->rl_local = dp->rl_local;
5937c478bd9Sstevel@tonic-gate 	rlentp->rl_attrc = dp->rl_attrc;
5947c478bd9Sstevel@tonic-gate 	rlentp->rl_fsid = dp->rl_fsid;
5957c478bd9Sstevel@tonic-gate 	rlentp->rl_fileno = dp->rl_fileno;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/* update resource counts */
5987c478bd9Sstevel@tonic-gate 	resp->p_cusagep->cu_blksused += CVBLKS(nbytes);
5997c478bd9Sstevel@tonic-gate 	resp->p_cusagep->cu_filesused += file;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	/* return success */
6027c478bd9Sstevel@tonic-gate 	return (0);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  *
6077c478bd9Sstevel@tonic-gate  *			res_clearident
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  * Description:
6107c478bd9Sstevel@tonic-gate  *	Removes the specified file from the ident list.
6117c478bd9Sstevel@tonic-gate  *	Updates resource counts.
6127c478bd9Sstevel@tonic-gate  * Arguments:
6137c478bd9Sstevel@tonic-gate  *	resp	res object
6147c478bd9Sstevel@tonic-gate  *	index	index into idents/pointers tables
6157c478bd9Sstevel@tonic-gate  *	nbytes	number of bytes in the file
6167c478bd9Sstevel@tonic-gate  *	file	number of files
6177c478bd9Sstevel@tonic-gate  * Returns:
6187c478bd9Sstevel@tonic-gate  *	Returns 0.
6197c478bd9Sstevel@tonic-gate  * Preconditions:
6207c478bd9Sstevel@tonic-gate  *	precond(resp is a valid res object)
6217c478bd9Sstevel@tonic-gate  *	precond(index is valid)
6227c478bd9Sstevel@tonic-gate  *	precond(ident is in use)
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate 
625*47644099Sgt29601 void
res_clearident(res * resp,int index,int nbytes,int file)6267c478bd9Sstevel@tonic-gate res_clearident(res *resp, int index, int nbytes, int file)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 	rl_entry_t *rlentp;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	precond(resp);
6317c478bd9Sstevel@tonic-gate 	precond(resp->p_magic == MAGIC);
6327c478bd9Sstevel@tonic-gate 	precond((index >= MININDEX) && (index < resp->p_totentries));
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/* get pointer to ident */
6357c478bd9Sstevel@tonic-gate 	rlentp = res_rlent_get(resp, index);
6367c478bd9Sstevel@tonic-gate 	precond(rlentp->rl_fsid != 0LL);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/* clear the ident */
6397c478bd9Sstevel@tonic-gate 	rlentp->rl_fsid = 0LL;
6407c478bd9Sstevel@tonic-gate 	rlentp->rl_fileno = 0;
6417c478bd9Sstevel@tonic-gate 	rlentp->rl_attrc = 0;
6427c478bd9Sstevel@tonic-gate 	rlentp->rl_local = 0;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/* update resource counts */
6457c478bd9Sstevel@tonic-gate 	resp->p_cusagep->cu_blksused -= CVBLKS(nbytes);
6467c478bd9Sstevel@tonic-gate 	resp->p_cusagep->cu_filesused -= file;
6477c478bd9Sstevel@tonic-gate 	assert(resp->p_cusagep->cu_blksused >= 0);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*
6517c478bd9Sstevel@tonic-gate  * This function moves an RL entry from whereever it currently is to
6527c478bd9Sstevel@tonic-gate  * the requested list.
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate void
res_rlent_moveto(res * resp,enum cachefs_rl_type type,uint_t entno,long blks)656*47644099Sgt29601 res_rlent_moveto(res *resp, enum cachefs_rl_type type, uint_t entno, long blks)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	rl_entry_t *rl_ent;
659*47644099Sgt29601 	uint_t prev, next;
6607c478bd9Sstevel@tonic-gate 	cachefs_rl_listhead_t *lhp;
6617c478bd9Sstevel@tonic-gate 	enum cachefs_rl_type otype;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	precond((CACHEFS_RL_START <= type) && (type <= CACHEFS_RL_END));
6647c478bd9Sstevel@tonic-gate 	precond((entno >= MININDEX) && (entno < resp->p_totentries));
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	rl_ent = res_rlent_get(resp, entno);
6677c478bd9Sstevel@tonic-gate 	if (rl_ent->rl_fsck) {
6687c478bd9Sstevel@tonic-gate 		/* remove entry from its previous list */
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		next = rl_ent->rl_fwd_idx;
6717c478bd9Sstevel@tonic-gate 		prev = rl_ent->rl_bkwd_idx;
6727c478bd9Sstevel@tonic-gate 		otype = rl_ent->rl_current;
6737c478bd9Sstevel@tonic-gate 		assert((CACHEFS_RL_START <= otype) &&
6747c478bd9Sstevel@tonic-gate 		    (otype <= CACHEFS_RL_END));
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		lhp = RL_HEAD(resp, otype);
6777c478bd9Sstevel@tonic-gate 		if ((lhp->rli_back == 0) || (lhp->rli_front == 0))
6787c478bd9Sstevel@tonic-gate 			assert((lhp->rli_back == 0) && (lhp->rli_front == 0));
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		if (lhp->rli_back == entno)
6817c478bd9Sstevel@tonic-gate 			lhp->rli_back = next;
6827c478bd9Sstevel@tonic-gate 		if (lhp->rli_front == entno)
6837c478bd9Sstevel@tonic-gate 			lhp->rli_front = prev;
6847c478bd9Sstevel@tonic-gate 		if (prev != 0) {
6857c478bd9Sstevel@tonic-gate 			rl_ent = res_rlent_get(resp, prev);
6867c478bd9Sstevel@tonic-gate 			rl_ent->rl_fwd_idx = next;
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 		if (next != 0) {
6897c478bd9Sstevel@tonic-gate 			rl_ent = res_rlent_get(resp, next);
6907c478bd9Sstevel@tonic-gate 			rl_ent->rl_bkwd_idx = prev;
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 		lhp->rli_blkcnt -= blks;
6937c478bd9Sstevel@tonic-gate 		lhp->rli_itemcnt--;
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	/* add entry to its new list */
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	lhp = RL_HEAD(resp, type);
6997c478bd9Sstevel@tonic-gate 	rl_ent = res_rlent_get(resp, entno);
7007c478bd9Sstevel@tonic-gate 	rl_ent->rl_current = type;
7017c478bd9Sstevel@tonic-gate 	rl_ent->rl_bkwd_idx = 0;
7027c478bd9Sstevel@tonic-gate 	rl_ent->rl_fwd_idx = lhp->rli_back;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	if (lhp->rli_back != 0) {
7057c478bd9Sstevel@tonic-gate 		assert(lhp->rli_front != 0);
7067c478bd9Sstevel@tonic-gate 		rl_ent = res_rlent_get(resp, lhp->rli_back);
7077c478bd9Sstevel@tonic-gate 		rl_ent->rl_bkwd_idx = entno;
7087c478bd9Sstevel@tonic-gate 	} else {
7097c478bd9Sstevel@tonic-gate 		assert(lhp->rli_front == 0);
7107c478bd9Sstevel@tonic-gate 		lhp->rli_front = entno;
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 	lhp->rli_back = entno;
7137c478bd9Sstevel@tonic-gate 	lhp->rli_blkcnt += blks;
7147c478bd9Sstevel@tonic-gate 	lhp->rli_itemcnt++;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	rl_ent = res_rlent_get(resp, entno);
7177c478bd9Sstevel@tonic-gate 	rl_ent->rl_current = type;
7187c478bd9Sstevel@tonic-gate 	rl_ent->rl_fsck = 1;
7197c478bd9Sstevel@tonic-gate }
720