xref: /titanic_44/usr/src/uts/common/fs/cachefs/cachefs_subr.c (revision d3d50737e566cade9a08d73d2af95105ac7cd960)
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
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d3d50737SRafael Vanoni  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/cred.h>
307c478bd9Sstevel@tonic-gate #include <sys/proc.h>
317c478bd9Sstevel@tonic-gate #include <sys/user.h>
327c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
337c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
347c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
357c478bd9Sstevel@tonic-gate #include <sys/uio.h>
367c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/mount.h>
407c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
417c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
467c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
477c478bd9Sstevel@tonic-gate #include <sys/file.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
507c478bd9Sstevel@tonic-gate #include <sys/fbuf.h>
517c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
527c478bd9Sstevel@tonic-gate #include <sys/callb.h>
537c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
547c478bd9Sstevel@tonic-gate #include <sys/rwlock.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
577c478bd9Sstevel@tonic-gate #include <vm/hat.h>
587c478bd9Sstevel@tonic-gate #include <vm/as.h>
597c478bd9Sstevel@tonic-gate #include <vm/page.h>
607c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
617c478bd9Sstevel@tonic-gate #include <vm/seg.h>
627c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
637c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
647c478bd9Sstevel@tonic-gate #include <vm/rm.h>
657c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
667c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_log.h>
677c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_dir.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate extern struct seg *segkmap;
707c478bd9Sstevel@tonic-gate caddr_t segmap_getmap();
717c478bd9Sstevel@tonic-gate int segmap_release();
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate extern struct cnode *cachefs_freeback;
747c478bd9Sstevel@tonic-gate extern struct cnode *cachefs_freefront;
757c478bd9Sstevel@tonic-gate extern cachefscache_t *cachefs_cachelist;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
787c478bd9Sstevel@tonic-gate int cachefsdebug = 0;
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate int cachefs_max_threads = CFS_MAX_THREADS;
827c478bd9Sstevel@tonic-gate ino64_t cachefs_check_fileno = 0;
837c478bd9Sstevel@tonic-gate struct kmem_cache *cachefs_cache_kmcache = NULL;
847c478bd9Sstevel@tonic-gate struct kmem_cache *cachefs_req_cache = NULL;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static int
877c478bd9Sstevel@tonic-gate cachefs_async_populate_reg(struct cachefs_populate_req *, cred_t *,
887c478bd9Sstevel@tonic-gate     vnode_t *, vnode_t *);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Cache routines
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *		cachefs_cache_create
987c478bd9Sstevel@tonic-gate  *
997c478bd9Sstevel@tonic-gate  * Description:
1007c478bd9Sstevel@tonic-gate  *	Creates a cachefscache_t object and initializes it to
1017c478bd9Sstevel@tonic-gate  *	be NOCACHE and NOFILL mode.
1027c478bd9Sstevel@tonic-gate  * Arguments:
1037c478bd9Sstevel@tonic-gate  * Returns:
1047c478bd9Sstevel@tonic-gate  *	Returns a pointer to the created object or NULL if
1057c478bd9Sstevel@tonic-gate  *	threads could not be created.
1067c478bd9Sstevel@tonic-gate  * Preconditions:
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate cachefscache_t *
cachefs_cache_create(void)1107c478bd9Sstevel@tonic-gate cachefs_cache_create(void)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep;
1137c478bd9Sstevel@tonic-gate 	struct cachefs_req *rp;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/* allocate zeroed memory for the object */
1167c478bd9Sstevel@tonic-gate 	cachep = kmem_cache_alloc(cachefs_cache_kmcache, KM_SLEEP);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	bzero(cachep, sizeof (*cachep));
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	cv_init(&cachep->c_cwcv, NULL, CV_DEFAULT, NULL);
1217c478bd9Sstevel@tonic-gate 	cv_init(&cachep->c_cwhaltcv, NULL, CV_DEFAULT, NULL);
1227c478bd9Sstevel@tonic-gate 	mutex_init(&cachep->c_contentslock, NULL, MUTEX_DEFAULT, NULL);
1237c478bd9Sstevel@tonic-gate 	mutex_init(&cachep->c_fslistlock, NULL, MUTEX_DEFAULT, NULL);
1247c478bd9Sstevel@tonic-gate 	mutex_init(&cachep->c_log_mutex, NULL, MUTEX_DEFAULT, NULL);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/* set up the work queue and get the sync thread created */
1277c478bd9Sstevel@tonic-gate 	cachefs_workq_init(&cachep->c_workq);
1287c478bd9Sstevel@tonic-gate 	cachep->c_workq.wq_keepone = 1;
1297c478bd9Sstevel@tonic-gate 	cachep->c_workq.wq_cachep = cachep;
1307c478bd9Sstevel@tonic-gate 	rp = kmem_cache_alloc(cachefs_req_cache, KM_SLEEP);
1317c478bd9Sstevel@tonic-gate 	rp->cfs_cmd = CFS_NOOP;
1327c478bd9Sstevel@tonic-gate 	rp->cfs_cr = kcred;
1337c478bd9Sstevel@tonic-gate 	rp->cfs_req_u.cu_fs_sync.cf_cachep = cachep;
1347c478bd9Sstevel@tonic-gate 	crhold(rp->cfs_cr);
1357c478bd9Sstevel@tonic-gate 	cachefs_addqueue(rp, &cachep->c_workq);
1367c478bd9Sstevel@tonic-gate 	cachep->c_flags |= CACHE_NOCACHE | CACHE_NOFILL | CACHE_ALLOC_PENDING;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	return (cachep);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  *		cachefs_cache_destroy
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * Description:
1477c478bd9Sstevel@tonic-gate  *	Destroys the cachefscache_t object.
1487c478bd9Sstevel@tonic-gate  * Arguments:
1497c478bd9Sstevel@tonic-gate  *	cachep	the cachefscache_t object to destroy
1507c478bd9Sstevel@tonic-gate  * Returns:
1517c478bd9Sstevel@tonic-gate  * Preconditions:
1527c478bd9Sstevel@tonic-gate  *	precond(cachep)
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate void
cachefs_cache_destroy(cachefscache_t * cachep)1567c478bd9Sstevel@tonic-gate cachefs_cache_destroy(cachefscache_t *cachep)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	int error = 0;
1597c478bd9Sstevel@tonic-gate #ifdef CFSRLDEBUG
1607c478bd9Sstevel@tonic-gate 	uint_t index;
1617c478bd9Sstevel@tonic-gate #endif /* CFSRLDEBUG */
162*d3d50737SRafael Vanoni 	clock_t wakeup = (60 * hz);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/* stop async threads */
1657c478bd9Sstevel@tonic-gate 	while (cachep->c_workq.wq_thread_count > 0)
1667c478bd9Sstevel@tonic-gate 		(void) cachefs_async_halt(&cachep->c_workq, 1);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/* kill off the cachep worker thread */
1697c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_contentslock);
1707c478bd9Sstevel@tonic-gate 	while (cachep->c_flags & CACHE_CACHEW_THREADRUN) {
1717c478bd9Sstevel@tonic-gate 		cachep->c_flags |= CACHE_CACHEW_THREADEXIT;
1727c478bd9Sstevel@tonic-gate 		cv_signal(&cachep->c_cwcv);
173*d3d50737SRafael Vanoni 		(void) cv_reltimedwait(&cachep->c_cwhaltcv,
174*d3d50737SRafael Vanoni 		    &cachep->c_contentslock, wakeup, TR_CLOCK_TICK);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if ((cachep->c_flags & CACHE_ALLOC_PENDING) == 0) {
1787c478bd9Sstevel@tonic-gate 		cachep->c_usage.cu_flags &= ~CUSAGE_ACTIVE;
1797c478bd9Sstevel@tonic-gate 		(void) cachefs_cache_rssync(cachep);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_contentslock);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* if there is a cache */
1847c478bd9Sstevel@tonic-gate 	if ((cachep->c_flags & CACHE_NOCACHE) == 0) {
1857c478bd9Sstevel@tonic-gate 		if ((cachep->c_flags & CACHE_NOFILL) == 0) {
1867c478bd9Sstevel@tonic-gate #ifdef CFSRLDEBUG
1877c478bd9Sstevel@tonic-gate 			/* blow away dangling rl debugging info */
1887c478bd9Sstevel@tonic-gate 			mutex_enter(&cachep->c_contentslock);
1897c478bd9Sstevel@tonic-gate 			for (index = 0;
1907c478bd9Sstevel@tonic-gate 			    index <= cachep->c_rlinfo.rl_entries;
1917c478bd9Sstevel@tonic-gate 			    index++) {
1927c478bd9Sstevel@tonic-gate 				rl_entry_t *rlent;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 				error = cachefs_rl_entry_get(cachep, index,
1957c478bd9Sstevel@tonic-gate 				    rlent);
1967c478bd9Sstevel@tonic-gate 				/*
1977c478bd9Sstevel@tonic-gate 				 * Since we are destroying the cache,
1987c478bd9Sstevel@tonic-gate 				 * better to ignore and proceed
1997c478bd9Sstevel@tonic-gate 				 */
2007c478bd9Sstevel@tonic-gate 				if (error)
2017c478bd9Sstevel@tonic-gate 					break;
2027c478bd9Sstevel@tonic-gate 				cachefs_rl_debug_destroy(rlent);
2037c478bd9Sstevel@tonic-gate 			}
2047c478bd9Sstevel@tonic-gate 			mutex_exit(&cachep->c_contentslock);
2057c478bd9Sstevel@tonic-gate #endif /* CFSRLDEBUG */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 			/* sync the cache */
2087c478bd9Sstevel@tonic-gate 			if (!error)
2097c478bd9Sstevel@tonic-gate 				cachefs_cache_sync(cachep);
2107c478bd9Sstevel@tonic-gate 		} else {
2117c478bd9Sstevel@tonic-gate 			/* get rid of any unused fscache objects */
2127c478bd9Sstevel@tonic-gate 			mutex_enter(&cachep->c_fslistlock);
2137c478bd9Sstevel@tonic-gate 			fscache_list_gc(cachep);
2147c478bd9Sstevel@tonic-gate 			mutex_exit(&cachep->c_fslistlock);
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 		ASSERT(cachep->c_fslist == NULL);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		VN_RELE(cachep->c_resfilevp);
2197c478bd9Sstevel@tonic-gate 		VN_RELE(cachep->c_dirvp);
2207c478bd9Sstevel@tonic-gate 		VN_RELE(cachep->c_lockvp);
2217c478bd9Sstevel@tonic-gate 		VN_RELE(cachep->c_lostfoundvp);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if (cachep->c_log_ctl != NULL)
2257c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(cachep->c_log_ctl,
2267c478bd9Sstevel@tonic-gate 		    sizeof (cachefs_log_control_t));
2277c478bd9Sstevel@tonic-gate 	if (cachep->c_log != NULL)
2287c478bd9Sstevel@tonic-gate 		cachefs_log_destroy_cookie(cachep->c_log);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	cv_destroy(&cachep->c_cwcv);
2317c478bd9Sstevel@tonic-gate 	cv_destroy(&cachep->c_cwhaltcv);
2327c478bd9Sstevel@tonic-gate 	mutex_destroy(&cachep->c_contentslock);
2337c478bd9Sstevel@tonic-gate 	mutex_destroy(&cachep->c_fslistlock);
2347c478bd9Sstevel@tonic-gate 	mutex_destroy(&cachep->c_log_mutex);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	kmem_cache_free(cachefs_cache_kmcache, cachep);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------
2417c478bd9Sstevel@tonic-gate  *
2427c478bd9Sstevel@tonic-gate  *		cachefs_cache_active_ro
2437c478bd9Sstevel@tonic-gate  *
2447c478bd9Sstevel@tonic-gate  * Description:
2457c478bd9Sstevel@tonic-gate  *	Activates the cachefscache_t object for a read-only file system.
2467c478bd9Sstevel@tonic-gate  * Arguments:
2477c478bd9Sstevel@tonic-gate  *	cachep	the cachefscache_t object to activate
2487c478bd9Sstevel@tonic-gate  *	cdvp	the vnode of the cache directory
2497c478bd9Sstevel@tonic-gate  * Returns:
2507c478bd9Sstevel@tonic-gate  *	Returns 0 for success, !0 if there is a problem with the cache.
2517c478bd9Sstevel@tonic-gate  * Preconditions:
2527c478bd9Sstevel@tonic-gate  *	precond(cachep)
2537c478bd9Sstevel@tonic-gate  *	precond(cdvp)
2547c478bd9Sstevel@tonic-gate  *	precond(cachep->c_flags & CACHE_NOCACHE)
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate int
cachefs_cache_activate_ro(cachefscache_t * cachep,vnode_t * cdvp)2587c478bd9Sstevel@tonic-gate cachefs_cache_activate_ro(cachefscache_t *cachep, vnode_t *cdvp)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	cachefs_log_control_t *lc;
2617c478bd9Sstevel@tonic-gate 	vnode_t *labelvp = NULL;
2627c478bd9Sstevel@tonic-gate 	vnode_t *rifvp = NULL;
2637c478bd9Sstevel@tonic-gate 	vnode_t *lockvp = NULL;
2647c478bd9Sstevel@tonic-gate 	vnode_t *statevp = NULL;
2657c478bd9Sstevel@tonic-gate 	vnode_t *lostfoundvp = NULL;
2667c478bd9Sstevel@tonic-gate 	struct vattr *attrp = NULL;
2677c478bd9Sstevel@tonic-gate 	int error;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	ASSERT(cachep->c_flags & CACHE_NOCACHE);
2707c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_contentslock);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	attrp = cachefs_kmem_alloc(sizeof (struct vattr), KM_SLEEP);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/* get the mode bits of the cache directory */
2757c478bd9Sstevel@tonic-gate 	attrp->va_mask = AT_ALL;
276da6c28aaSamw 	error = VOP_GETATTR(cdvp, attrp, 0, kcred, NULL);
2777c478bd9Sstevel@tonic-gate 	if (error)
2787c478bd9Sstevel@tonic-gate 		goto out;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/* ensure the mode bits are 000 to keep out casual users */
2817c478bd9Sstevel@tonic-gate 	if (attrp->va_mode & S_IAMB) {
2827c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: Cache Directory Mode must be 000\n");
2837c478bd9Sstevel@tonic-gate 		error = EPERM;
2847c478bd9Sstevel@tonic-gate 		goto out;
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/* Get the lock file */
2887c478bd9Sstevel@tonic-gate 	error = VOP_LOOKUP(cdvp, CACHEFS_LOCK_FILE, &lockvp, NULL, 0, NULL,
289da6c28aaSamw 	    kcred, NULL, NULL, NULL);
2907c478bd9Sstevel@tonic-gate 	if (error) {
2917c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: activate_a: cache corruption"
2927c478bd9Sstevel@tonic-gate 		    " run fsck.\n");
2937c478bd9Sstevel@tonic-gate 		goto out;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/* Get the label file */
2977c478bd9Sstevel@tonic-gate 	error = VOP_LOOKUP(cdvp, CACHELABEL_NAME, &labelvp, NULL, 0, NULL,
298da6c28aaSamw 	    kcred, NULL, NULL, NULL);
2997c478bd9Sstevel@tonic-gate 	if (error) {
3007c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: activate_b: cache corruption"
3017c478bd9Sstevel@tonic-gate 		    " run fsck.\n");
3027c478bd9Sstevel@tonic-gate 		goto out;
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/* read in the label */
3067c478bd9Sstevel@tonic-gate 	error = vn_rdwr(UIO_READ, labelvp, (caddr_t)&cachep->c_label,
3077c478bd9Sstevel@tonic-gate 	    sizeof (struct cache_label), 0LL, UIO_SYSSPACE,
3087c478bd9Sstevel@tonic-gate 	    0, (rlim64_t)0, kcred, NULL);
3097c478bd9Sstevel@tonic-gate 	if (error) {
3107c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: activate_c: cache corruption"
3117c478bd9Sstevel@tonic-gate 		    " run fsck.\n");
3127c478bd9Sstevel@tonic-gate 		goto out;
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/* Verify that we can handle the version this cache was created under */
3167c478bd9Sstevel@tonic-gate 	if (cachep->c_label.cl_cfsversion != CFSVERSION) {
3177c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: Invalid Cache Version, run fsck\n");
3187c478bd9Sstevel@tonic-gate 		error = EINVAL;
3197c478bd9Sstevel@tonic-gate 		goto out;
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/* Open the resource file */
323da6c28aaSamw 	error = VOP_LOOKUP(cdvp, RESOURCE_NAME, &rifvp, NULL, 0, NULL, kcred,
324da6c28aaSamw 	    NULL, NULL, NULL);
3257c478bd9Sstevel@tonic-gate 	if (error) {
3267c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: activate_d: cache corruption"
3277c478bd9Sstevel@tonic-gate 		    " run fsck.\n");
3287c478bd9Sstevel@tonic-gate 		goto out;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/*  Read the usage struct for this cache */
3327c478bd9Sstevel@tonic-gate 	error = vn_rdwr(UIO_READ, rifvp, (caddr_t)&cachep->c_usage,
3337c478bd9Sstevel@tonic-gate 	    sizeof (struct cache_usage), 0LL, UIO_SYSSPACE, 0,
3347c478bd9Sstevel@tonic-gate 	    (rlim64_t)0, kcred, NULL);
3357c478bd9Sstevel@tonic-gate 	if (error) {
3367c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: activate_e: cache corruption"
3377c478bd9Sstevel@tonic-gate 		    " run fsck.\n");
3387c478bd9Sstevel@tonic-gate 		goto out;
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if (cachep->c_usage.cu_flags & CUSAGE_ACTIVE) {
3427c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: cache not clean.  Run fsck\n");
3437c478bd9Sstevel@tonic-gate 		/* ENOSPC is what UFS uses for clean flag check */
3447c478bd9Sstevel@tonic-gate 		error = ENOSPC;
3457c478bd9Sstevel@tonic-gate 		goto out;
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/*  Read the rlinfo for this cache */
3497c478bd9Sstevel@tonic-gate 	error = vn_rdwr(UIO_READ, rifvp, (caddr_t)&cachep->c_rlinfo,
3507c478bd9Sstevel@tonic-gate 	    sizeof (cachefs_rl_info_t), (offset_t)sizeof (struct cache_usage),
3517c478bd9Sstevel@tonic-gate 	    UIO_SYSSPACE, 0, 0, kcred, NULL);
3527c478bd9Sstevel@tonic-gate 	if (error) {
3537c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: activate_f: cache corruption"
3547c478bd9Sstevel@tonic-gate 		    " run fsck.\n");
3557c478bd9Sstevel@tonic-gate 		goto out;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/* Open the lost+found directory */
3597c478bd9Sstevel@tonic-gate 	error = VOP_LOOKUP(cdvp, CACHEFS_LOSTFOUND_NAME, &lostfoundvp,
360da6c28aaSamw 	    NULL, 0, NULL, kcred, NULL, NULL, NULL);
3617c478bd9Sstevel@tonic-gate 	if (error) {
3627c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: activate_g: cache corruption"
3637c478bd9Sstevel@tonic-gate 		    " run fsck.\n");
3647c478bd9Sstevel@tonic-gate 		goto out;
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	VN_HOLD(rifvp);
3687c478bd9Sstevel@tonic-gate 	VN_HOLD(cdvp);
3697c478bd9Sstevel@tonic-gate 	VN_HOLD(lockvp);
3707c478bd9Sstevel@tonic-gate 	VN_HOLD(lostfoundvp);
3717c478bd9Sstevel@tonic-gate 	cachep->c_resfilevp = rifvp;
3727c478bd9Sstevel@tonic-gate 	cachep->c_dirvp = cdvp;
3737c478bd9Sstevel@tonic-gate 	cachep->c_lockvp = lockvp;
3747c478bd9Sstevel@tonic-gate 	cachep->c_lostfoundvp = lostfoundvp;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/* get the cachep worker thread created */
3777c478bd9Sstevel@tonic-gate 	cachep->c_flags |= CACHE_CACHEW_THREADRUN;
3787c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, cachefs_cachep_worker_thread,
3797c478bd9Sstevel@tonic-gate 	    cachep, 0, &p0, TS_RUN, minclsyspri);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/* allocate the `logging control' field */
3827c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_log_mutex);
3837c478bd9Sstevel@tonic-gate 	cachep->c_log_ctl =
3847c478bd9Sstevel@tonic-gate 	    cachefs_kmem_zalloc(sizeof (cachefs_log_control_t), KM_SLEEP);
3857c478bd9Sstevel@tonic-gate 	lc = (cachefs_log_control_t *)cachep->c_log_ctl;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/* if the LOG_STATUS_NAME file exists, read it in and set up logging */
3887c478bd9Sstevel@tonic-gate 	error = VOP_LOOKUP(cachep->c_dirvp, LOG_STATUS_NAME, &statevp,
389da6c28aaSamw 	    NULL, 0, NULL, kcred, NULL, NULL, NULL);
3907c478bd9Sstevel@tonic-gate 	if (error == 0) {
3917c478bd9Sstevel@tonic-gate 		int vnrw_error;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		vnrw_error = vn_rdwr(UIO_READ, statevp, (caddr_t)lc,
3947c478bd9Sstevel@tonic-gate 		    sizeof (*lc), 0LL, UIO_SYSSPACE, 0, (rlim64_t)RLIM_INFINITY,
3957c478bd9Sstevel@tonic-gate 		    kcred, NULL);
3967c478bd9Sstevel@tonic-gate 		VN_RELE(statevp);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		if (vnrw_error == 0) {
3997c478bd9Sstevel@tonic-gate 			if ((cachep->c_log = cachefs_log_create_cookie(lc))
4007c478bd9Sstevel@tonic-gate 			    == NULL)
4017c478bd9Sstevel@tonic-gate 				cachefs_log_error(cachep, ENOMEM, 0);
4027c478bd9Sstevel@tonic-gate 			else if ((lc->lc_magic != CACHEFS_LOG_MAGIC) ||
4037c478bd9Sstevel@tonic-gate 			    (lc->lc_path[0] != '/') ||
4047c478bd9Sstevel@tonic-gate 			    (cachefs_log_logfile_open(cachep,
4057c478bd9Sstevel@tonic-gate 			    lc->lc_path) != 0))
4067c478bd9Sstevel@tonic-gate 				cachefs_log_error(cachep, EINVAL, 0);
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 	} else {
4097c478bd9Sstevel@tonic-gate 		error = 0;
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	lc->lc_magic = CACHEFS_LOG_MAGIC;
4127c478bd9Sstevel@tonic-gate 	lc->lc_cachep = (uint64_t)(uintptr_t)cachep;
4137c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_log_mutex);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate out:
4167c478bd9Sstevel@tonic-gate 	if (error == 0) {
4177c478bd9Sstevel@tonic-gate 		cachep->c_flags &= ~(CACHE_NOCACHE | CACHE_ALLOC_PENDING);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 	if (attrp)
4207c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(attrp, sizeof (struct vattr));
4217c478bd9Sstevel@tonic-gate 	if (labelvp != NULL)
4227c478bd9Sstevel@tonic-gate 		VN_RELE(labelvp);
4237c478bd9Sstevel@tonic-gate 	if (rifvp != NULL)
4247c478bd9Sstevel@tonic-gate 		VN_RELE(rifvp);
4257c478bd9Sstevel@tonic-gate 	if (lockvp)
4267c478bd9Sstevel@tonic-gate 		VN_RELE(lockvp);
4277c478bd9Sstevel@tonic-gate 	if (lostfoundvp)
4287c478bd9Sstevel@tonic-gate 		VN_RELE(lostfoundvp);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_contentslock);
4317c478bd9Sstevel@tonic-gate 	return (error);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate int
cachefs_stop_cache(cnode_t * cp)4357c478bd9Sstevel@tonic-gate cachefs_stop_cache(cnode_t *cp)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(cp);
4387c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
4397c478bd9Sstevel@tonic-gate 	filegrp_t *fgp;
4407c478bd9Sstevel@tonic-gate 	int i;
4417c478bd9Sstevel@tonic-gate 	int error = 0;
442*d3d50737SRafael Vanoni 	clock_t wakeup = (60 * hz);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/* XXX verify lock-ordering for this function */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_contentslock);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * no work if we're already in nocache mode.  hopefully this
4507c478bd9Sstevel@tonic-gate 	 * will be the usual case.
4517c478bd9Sstevel@tonic-gate 	 */
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (cachep->c_flags & CACHE_NOCACHE) {
4547c478bd9Sstevel@tonic-gate 		mutex_exit(&cachep->c_contentslock);
4557c478bd9Sstevel@tonic-gate 		return (0);
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if ((cachep->c_flags & CACHE_NOFILL) == 0) {
4597c478bd9Sstevel@tonic-gate 		mutex_exit(&cachep->c_contentslock);
4607c478bd9Sstevel@tonic-gate 		return (EINVAL);
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_contentslock);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/* We are already not caching if nfsv4 */
4667c478bd9Sstevel@tonic-gate 	if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
4677c478bd9Sstevel@tonic-gate 		return (0);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
4717c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_fslistlock);
4727c478bd9Sstevel@tonic-gate 	ASSERT(fscp == cachep->c_fslist);
4737c478bd9Sstevel@tonic-gate 	ASSERT(fscp->fs_next == NULL);
4747c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_fslistlock);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	printf("cachefs_stop_cache: resetting CACHE_NOCACHE\n");
4777c478bd9Sstevel@tonic-gate #endif
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/* XXX should i worry about disconnected during boot? */
4807c478bd9Sstevel@tonic-gate 	error = cachefs_cd_access(fscp, 1, 1);
4817c478bd9Sstevel@tonic-gate 	if (error)
4827c478bd9Sstevel@tonic-gate 		goto out;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	error = cachefs_async_halt(&fscp->fs_workq, 1);
4857c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
4867c478bd9Sstevel@tonic-gate 	error = cachefs_async_halt(&cachep->c_workq, 1);
4877c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
4887c478bd9Sstevel@tonic-gate 	/* sigh -- best to keep going if async_halt failed. */
4897c478bd9Sstevel@tonic-gate 	error = 0;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/* XXX current order: cnode, fgp, fscp, cache. okay? */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	cachefs_cnode_traverse(fscp, cachefs_cnode_disable_caching);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	for (i = 0; i < CFS_FS_FGP_BUCKET_SIZE; i++) {
4967c478bd9Sstevel@tonic-gate 		for (fgp = fscp->fs_filegrp[i]; fgp != NULL;
4977c478bd9Sstevel@tonic-gate 		    fgp = fgp->fg_next) {
4987c478bd9Sstevel@tonic-gate 			mutex_enter(&fgp->fg_mutex);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 			ASSERT((fgp->fg_flags &
5017c478bd9Sstevel@tonic-gate 			    (CFS_FG_WRITE | CFS_FG_UPDATED)) == 0);
5027c478bd9Sstevel@tonic-gate 			fgp->fg_flags |=
5037c478bd9Sstevel@tonic-gate 			    CFS_FG_ALLOC_FILE |
5047c478bd9Sstevel@tonic-gate 			    CFS_FG_ALLOC_ATTR;
5057c478bd9Sstevel@tonic-gate 			fgp->fg_flags &= ~CFS_FG_READ;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 			if (fgp->fg_dirvp) {
5087c478bd9Sstevel@tonic-gate 				fgp->fg_flags |= CFS_FG_ALLOC_FILE;
5097c478bd9Sstevel@tonic-gate 				VN_RELE(fgp->fg_dirvp);
5107c478bd9Sstevel@tonic-gate 				fgp->fg_dirvp = NULL;
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate 			if (fgp->fg_attrvp) {
5137c478bd9Sstevel@tonic-gate 				fgp->fg_flags |= CFS_FG_ALLOC_ATTR;
5147c478bd9Sstevel@tonic-gate 				VN_RELE(fgp->fg_attrvp);
5157c478bd9Sstevel@tonic-gate 				fgp->fg_attrvp = NULL;
5167c478bd9Sstevel@tonic-gate 			}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 			mutex_exit(&fgp->fg_mutex);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	mutex_enter(&fscp->fs_fslock);
5237c478bd9Sstevel@tonic-gate 	ASSERT((fscp->fs_flags & (CFS_FS_WRITE)) == 0);
5247c478bd9Sstevel@tonic-gate 	fscp->fs_flags &= ~(CFS_FS_READ | CFS_FS_DIRTYINFO);
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (fscp->fs_fscdirvp) {
5277c478bd9Sstevel@tonic-gate 		VN_RELE(fscp->fs_fscdirvp);
5287c478bd9Sstevel@tonic-gate 		fscp->fs_fscdirvp = NULL;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	if (fscp->fs_fsattrdir) {
5317c478bd9Sstevel@tonic-gate 		VN_RELE(fscp->fs_fsattrdir);
5327c478bd9Sstevel@tonic-gate 		fscp->fs_fsattrdir = NULL;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 	if (fscp->fs_infovp) {
5357c478bd9Sstevel@tonic-gate 		VN_RELE(fscp->fs_infovp);
5367c478bd9Sstevel@tonic-gate 		fscp->fs_infovp = NULL;
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 	/* XXX dlog stuff? */
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	mutex_exit(&fscp->fs_fslock);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * release resources grabbed in cachefs_cache_activate_ro
5447c478bd9Sstevel@tonic-gate 	 */
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_contentslock);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/* kill off the cachep worker thread */
5497c478bd9Sstevel@tonic-gate 	while (cachep->c_flags & CACHE_CACHEW_THREADRUN) {
5507c478bd9Sstevel@tonic-gate 		cachep->c_flags |= CACHE_CACHEW_THREADEXIT;
5517c478bd9Sstevel@tonic-gate 		cv_signal(&cachep->c_cwcv);
552*d3d50737SRafael Vanoni 		(void) cv_reltimedwait(&cachep->c_cwhaltcv,
553*d3d50737SRafael Vanoni 		    &cachep->c_contentslock, wakeup, TR_CLOCK_TICK);
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (cachep->c_resfilevp) {
5577c478bd9Sstevel@tonic-gate 		VN_RELE(cachep->c_resfilevp);
5587c478bd9Sstevel@tonic-gate 		cachep->c_resfilevp = NULL;
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	if (cachep->c_dirvp) {
5617c478bd9Sstevel@tonic-gate 		VN_RELE(cachep->c_dirvp);
5627c478bd9Sstevel@tonic-gate 		cachep->c_dirvp = NULL;
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 	if (cachep->c_lockvp) {
5657c478bd9Sstevel@tonic-gate 		VN_RELE(cachep->c_lockvp);
5667c478bd9Sstevel@tonic-gate 		cachep->c_lockvp = NULL;
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 	if (cachep->c_lostfoundvp) {
5697c478bd9Sstevel@tonic-gate 		VN_RELE(cachep->c_lostfoundvp);
5707c478bd9Sstevel@tonic-gate 		cachep->c_lostfoundvp = NULL;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_log_mutex);
5747c478bd9Sstevel@tonic-gate 	if (cachep->c_log_ctl) {
5757c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(cachep->c_log_ctl,
5767c478bd9Sstevel@tonic-gate 		    sizeof (cachefs_log_control_t));
5777c478bd9Sstevel@tonic-gate 		cachep->c_log_ctl = NULL;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 	if (cachep->c_log) {
5807c478bd9Sstevel@tonic-gate 		cachefs_log_destroy_cookie(cachep->c_log);
5817c478bd9Sstevel@tonic-gate 		cachep->c_log = NULL;
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_log_mutex);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	/* XXX do what mountroot_init does when ! foundcache */
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	cachep->c_flags |= CACHE_NOCACHE;
5887c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_contentslock);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/* XXX should i release this here? */
5917c478bd9Sstevel@tonic-gate 	cachefs_cd_release(fscp);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate out:
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	return (error);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*
5997c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------
6007c478bd9Sstevel@tonic-gate  *
6017c478bd9Sstevel@tonic-gate  *		cachefs_cache_active_rw
6027c478bd9Sstevel@tonic-gate  *
6037c478bd9Sstevel@tonic-gate  * Description:
6047c478bd9Sstevel@tonic-gate  *	Activates the cachefscache_t object for a read-write file system.
6057c478bd9Sstevel@tonic-gate  * Arguments:
6067c478bd9Sstevel@tonic-gate  *	cachep	the cachefscache_t object to activate
6077c478bd9Sstevel@tonic-gate  * Returns:
6087c478bd9Sstevel@tonic-gate  * Preconditions:
6097c478bd9Sstevel@tonic-gate  *	precond(cachep)
6107c478bd9Sstevel@tonic-gate  *	precond((cachep->c_flags & CACHE_NOCACHE) == 0)
6117c478bd9Sstevel@tonic-gate  *	precond(cachep->c_flags & CACHE_NOFILL)
6127c478bd9Sstevel@tonic-gate  */
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate void
cachefs_cache_activate_rw(cachefscache_t * cachep)6157c478bd9Sstevel@tonic-gate cachefs_cache_activate_rw(cachefscache_t *cachep)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate 	cachefs_rl_listhead_t *lhp;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	ASSERT((cachep->c_flags & CACHE_NOCACHE) == 0);
6207c478bd9Sstevel@tonic-gate 	ASSERT(cachep->c_flags & CACHE_NOFILL);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_contentslock);
6237c478bd9Sstevel@tonic-gate 	cachep->c_flags &= ~CACHE_NOFILL;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/* move the active list to the rl list */
6267c478bd9Sstevel@tonic-gate 	cachefs_rl_cleanup(cachep);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	lhp = &cachep->c_rlinfo.rl_items[
6297c478bd9Sstevel@tonic-gate 	    CACHEFS_RL_INDEX(CACHEFS_RL_PACKED_PENDING)];
6307c478bd9Sstevel@tonic-gate 	if (lhp->rli_itemcnt != 0)
6317c478bd9Sstevel@tonic-gate 		cachep->c_flags |= CACHE_PACKED_PENDING;
6327c478bd9Sstevel@tonic-gate 	cachefs_cache_dirty(cachep, 0);
6337c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_contentslock);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------
6387c478bd9Sstevel@tonic-gate  *
6397c478bd9Sstevel@tonic-gate  *		cachefs_cache_dirty
6407c478bd9Sstevel@tonic-gate  *
6417c478bd9Sstevel@tonic-gate  * Description:
6427c478bd9Sstevel@tonic-gate  *	Marks the cache as dirty (active).
6437c478bd9Sstevel@tonic-gate  * Arguments:
6447c478bd9Sstevel@tonic-gate  *	cachep	the cachefscache_t to mark as dirty
6457c478bd9Sstevel@tonic-gate  *	lockit	1 means grab contents lock, 0 means caller grabbed it
6467c478bd9Sstevel@tonic-gate  * Returns:
6477c478bd9Sstevel@tonic-gate  * Preconditions:
6487c478bd9Sstevel@tonic-gate  *	precond(cachep)
6497c478bd9Sstevel@tonic-gate  *	precond(cache is in rw mode)
6507c478bd9Sstevel@tonic-gate  */
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate void
cachefs_cache_dirty(struct cachefscache * cachep,int lockit)6537c478bd9Sstevel@tonic-gate cachefs_cache_dirty(struct cachefscache *cachep, int lockit)
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate 	int error;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	ASSERT((cachep->c_flags & (CACHE_NOCACHE | CACHE_NOFILL)) == 0);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if (lockit) {
6607c478bd9Sstevel@tonic-gate 		mutex_enter(&cachep->c_contentslock);
6617c478bd9Sstevel@tonic-gate 	} else {
6627c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&cachep->c_contentslock));
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 	if (cachep->c_flags & CACHE_DIRTY) {
6657c478bd9Sstevel@tonic-gate 		ASSERT(cachep->c_usage.cu_flags & CUSAGE_ACTIVE);
6667c478bd9Sstevel@tonic-gate 	} else {
6677c478bd9Sstevel@tonic-gate 		/*
6687c478bd9Sstevel@tonic-gate 		 * turn on the "cache active" (dirty) flag and write it
6697c478bd9Sstevel@tonic-gate 		 * synchronously to disk
6707c478bd9Sstevel@tonic-gate 		 */
6717c478bd9Sstevel@tonic-gate 		cachep->c_flags |= CACHE_DIRTY;
6727c478bd9Sstevel@tonic-gate 		cachep->c_usage.cu_flags |= CUSAGE_ACTIVE;
6737c478bd9Sstevel@tonic-gate 		if (error = vn_rdwr(UIO_WRITE, cachep->c_resfilevp,
6747c478bd9Sstevel@tonic-gate 		    (caddr_t)&cachep->c_usage, sizeof (struct cache_usage),
6757c478bd9Sstevel@tonic-gate 		    0LL, UIO_SYSSPACE, FSYNC, (rlim64_t)RLIM_INFINITY,
6767c478bd9Sstevel@tonic-gate 		    kcred, NULL)) {
6777c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
6787c478bd9Sstevel@tonic-gate 			    "cachefs: clean flag write error: %d\n", error);
6797c478bd9Sstevel@tonic-gate 		}
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	if (lockit)
6837c478bd9Sstevel@tonic-gate 		mutex_exit(&cachep->c_contentslock);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------
6887c478bd9Sstevel@tonic-gate  *
6897c478bd9Sstevel@tonic-gate  *		cachefs_cache_rssync
6907c478bd9Sstevel@tonic-gate  *
6917c478bd9Sstevel@tonic-gate  * Description:
6927c478bd9Sstevel@tonic-gate  *	Syncs out the resource file for the cachefscache_t object.
6937c478bd9Sstevel@tonic-gate  * Arguments:
6947c478bd9Sstevel@tonic-gate  *	cachep	the cachefscache_t object to operate on
6957c478bd9Sstevel@tonic-gate  * Returns:
6967c478bd9Sstevel@tonic-gate  *	Returns 0 for success, !0 on an error writing data.
6977c478bd9Sstevel@tonic-gate  * Preconditions:
6987c478bd9Sstevel@tonic-gate  *	precond(cachep)
6997c478bd9Sstevel@tonic-gate  *	precond(cache is in rw mode)
7007c478bd9Sstevel@tonic-gate  */
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate int
cachefs_cache_rssync(struct cachefscache * cachep)7037c478bd9Sstevel@tonic-gate cachefs_cache_rssync(struct cachefscache *cachep)
7047c478bd9Sstevel@tonic-gate {
7057c478bd9Sstevel@tonic-gate 	int error;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	ASSERT((cachep->c_flags & (CACHE_NOCACHE | CACHE_NOFILL |
7087c478bd9Sstevel@tonic-gate 	    CACHE_ALLOC_PENDING)) == 0);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	if (cachep->c_rl_entries != NULL) {
7117c478bd9Sstevel@tonic-gate 		error = vn_rdwr(UIO_WRITE, cachep->c_resfilevp,
7127c478bd9Sstevel@tonic-gate 		    (caddr_t)cachep->c_rl_entries, MAXBSIZE,
7137c478bd9Sstevel@tonic-gate 		    (offset_t)((cachep->c_rl_window + 1) * MAXBSIZE),
7147c478bd9Sstevel@tonic-gate 		    UIO_SYSSPACE, FSYNC, RLIM_INFINITY, kcred, NULL);
7157c478bd9Sstevel@tonic-gate 		if (error)
716*d3d50737SRafael Vanoni 			cmn_err(CE_WARN,
717*d3d50737SRafael Vanoni 			    "cachefs: Can't Write rl entries Info\n");
7187c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(cachep->c_rl_entries, MAXBSIZE);
7197c478bd9Sstevel@tonic-gate 		cachep->c_rl_entries = NULL;
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/* write the usage struct for this cache */
7237c478bd9Sstevel@tonic-gate 	error = vn_rdwr(UIO_WRITE, cachep->c_resfilevp,
7247c478bd9Sstevel@tonic-gate 	    (caddr_t)&cachep->c_usage, sizeof (struct cache_usage),
7257c478bd9Sstevel@tonic-gate 	    0LL, UIO_SYSSPACE, 0, (rlim64_t)RLIM_INFINITY, kcred, NULL);
7267c478bd9Sstevel@tonic-gate 	if (error) {
7277c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: Can't Write Cache Usage Info\n");
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/* write the rlinfo for this cache */
7317c478bd9Sstevel@tonic-gate 	error = vn_rdwr(UIO_WRITE, cachep->c_resfilevp,
7327c478bd9Sstevel@tonic-gate 	    (caddr_t)&cachep->c_rlinfo, sizeof (cachefs_rl_info_t),
7337c478bd9Sstevel@tonic-gate 	    (offset_t)sizeof (struct cache_usage), UIO_SYSSPACE,
7347c478bd9Sstevel@tonic-gate 	    0, (rlim64_t)RLIM_INFINITY, kcred, NULL);
7357c478bd9Sstevel@tonic-gate 	if (error) {
7367c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs: Can't Write Cache RL Info\n");
7377c478bd9Sstevel@tonic-gate 	}
738da6c28aaSamw 	error = VOP_FSYNC(cachep->c_resfilevp, FSYNC, kcred, NULL);
7397c478bd9Sstevel@tonic-gate 	return (error);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate /*
7437c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------
7447c478bd9Sstevel@tonic-gate  *
7457c478bd9Sstevel@tonic-gate  *		cachefs_cache_sync
7467c478bd9Sstevel@tonic-gate  *
7477c478bd9Sstevel@tonic-gate  * Description:
7487c478bd9Sstevel@tonic-gate  *	Sync a cache which includes all of its fscaches.
7497c478bd9Sstevel@tonic-gate  * Arguments:
7507c478bd9Sstevel@tonic-gate  *	cachep	the cachefscache_t object to sync
7517c478bd9Sstevel@tonic-gate  * Returns:
7527c478bd9Sstevel@tonic-gate  * Preconditions:
7537c478bd9Sstevel@tonic-gate  *	precond(cachep)
7547c478bd9Sstevel@tonic-gate  *	precond(cache is in rw mode)
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate void
cachefs_cache_sync(struct cachefscache * cachep)7587c478bd9Sstevel@tonic-gate cachefs_cache_sync(struct cachefscache *cachep)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	struct fscache *fscp;
7617c478bd9Sstevel@tonic-gate 	struct fscache **syncfsc;
7627c478bd9Sstevel@tonic-gate 	int nfscs, fscidx;
7637c478bd9Sstevel@tonic-gate 	int try;
7647c478bd9Sstevel@tonic-gate 	int done;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (cachep->c_flags & (CACHE_NOCACHE | CACHE_NOFILL))
7677c478bd9Sstevel@tonic-gate 		return;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	done = 0;
7707c478bd9Sstevel@tonic-gate 	for (try = 0; (try < 2) && !done; try++) {
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		nfscs = 0;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 		/*
7757c478bd9Sstevel@tonic-gate 		 * here we turn off the cache-wide DIRTY flag.  If it's still
7767c478bd9Sstevel@tonic-gate 		 * off when the sync completes we can write the clean flag to
7777c478bd9Sstevel@tonic-gate 		 * disk telling fsck it has no work to do.
7787c478bd9Sstevel@tonic-gate 		 */
7797c478bd9Sstevel@tonic-gate #ifdef CFSCLEANFLAG
7807c478bd9Sstevel@tonic-gate 		mutex_enter(&cachep->c_contentslock);
7817c478bd9Sstevel@tonic-gate 		cachep->c_flags &= ~CACHE_DIRTY;
7827c478bd9Sstevel@tonic-gate 		mutex_exit(&cachep->c_contentslock);
7837c478bd9Sstevel@tonic-gate #endif /* CFSCLEANFLAG */
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		cachefs_log_process_queue(cachep, 1);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		mutex_enter(&cachep->c_fslistlock);
7887c478bd9Sstevel@tonic-gate 		syncfsc = cachefs_kmem_alloc(
7897c478bd9Sstevel@tonic-gate 		    cachep->c_refcnt * sizeof (struct fscache *), KM_SLEEP);
7907c478bd9Sstevel@tonic-gate 		for (fscp = cachep->c_fslist; fscp; fscp = fscp->fs_next) {
7917c478bd9Sstevel@tonic-gate 			fscache_hold(fscp);
7927c478bd9Sstevel@tonic-gate 			ASSERT(nfscs < cachep->c_refcnt);
7937c478bd9Sstevel@tonic-gate 			syncfsc[nfscs++] = fscp;
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 		ASSERT(nfscs == cachep->c_refcnt);
7967c478bd9Sstevel@tonic-gate 		mutex_exit(&cachep->c_fslistlock);
7977c478bd9Sstevel@tonic-gate 		for (fscidx = 0; fscidx < nfscs; fscidx++) {
7987c478bd9Sstevel@tonic-gate 			fscp = syncfsc[fscidx];
7997c478bd9Sstevel@tonic-gate 			fscache_sync(fscp);
8007c478bd9Sstevel@tonic-gate 			fscache_rele(fscp);
8017c478bd9Sstevel@tonic-gate 		}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		/* get rid of any unused fscache objects */
8047c478bd9Sstevel@tonic-gate 		mutex_enter(&cachep->c_fslistlock);
8057c478bd9Sstevel@tonic-gate 		fscache_list_gc(cachep);
8067c478bd9Sstevel@tonic-gate 		mutex_exit(&cachep->c_fslistlock);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		/*
8097c478bd9Sstevel@tonic-gate 		 * here we check the cache-wide DIRTY flag.
8107c478bd9Sstevel@tonic-gate 		 * If it's off,
8117c478bd9Sstevel@tonic-gate 		 * we can write the clean flag to disk.
8127c478bd9Sstevel@tonic-gate 		 */
8137c478bd9Sstevel@tonic-gate #ifdef CFSCLEANFLAG
8147c478bd9Sstevel@tonic-gate 		mutex_enter(&cachep->c_contentslock);
8157c478bd9Sstevel@tonic-gate 		if ((cachep->c_flags & CACHE_DIRTY) == 0) {
8167c478bd9Sstevel@tonic-gate 			if (cachep->c_usage.cu_flags & CUSAGE_ACTIVE) {
8177c478bd9Sstevel@tonic-gate 				cachep->c_usage.cu_flags &= ~CUSAGE_ACTIVE;
8187c478bd9Sstevel@tonic-gate 				if (cachefs_cache_rssync(cachep) == 0) {
8197c478bd9Sstevel@tonic-gate 					done = 1;
8207c478bd9Sstevel@tonic-gate 				} else {
8217c478bd9Sstevel@tonic-gate 					cachep->c_usage.cu_flags |=
8227c478bd9Sstevel@tonic-gate 					    CUSAGE_ACTIVE;
8237c478bd9Sstevel@tonic-gate 				}
8247c478bd9Sstevel@tonic-gate 			} else {
8257c478bd9Sstevel@tonic-gate 				done = 1;
8267c478bd9Sstevel@tonic-gate 			}
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 		mutex_exit(&cachep->c_contentslock);
8297c478bd9Sstevel@tonic-gate #else /* CFSCLEANFLAG */
8307c478bd9Sstevel@tonic-gate 		mutex_enter(&cachep->c_contentslock);
8317c478bd9Sstevel@tonic-gate 		(void) cachefs_cache_rssync(cachep);
8327c478bd9Sstevel@tonic-gate 		mutex_exit(&cachep->c_contentslock);
8337c478bd9Sstevel@tonic-gate 		done = 1;
8347c478bd9Sstevel@tonic-gate #endif /* CFSCLEANFLAG */
8357c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(syncfsc, nfscs * sizeof (struct fscache *));
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate /*
8407c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------
8417c478bd9Sstevel@tonic-gate  *
8427c478bd9Sstevel@tonic-gate  *		cachefs_cache_unique
8437c478bd9Sstevel@tonic-gate  *
8447c478bd9Sstevel@tonic-gate  * Description:
8457c478bd9Sstevel@tonic-gate  * Arguments:
8467c478bd9Sstevel@tonic-gate  * Returns:
8477c478bd9Sstevel@tonic-gate  *	Returns a unique number.
8487c478bd9Sstevel@tonic-gate  * Preconditions:
8497c478bd9Sstevel@tonic-gate  *	precond(cachep)
8507c478bd9Sstevel@tonic-gate  */
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate uint_t
cachefs_cache_unique(cachefscache_t * cachep)8537c478bd9Sstevel@tonic-gate cachefs_cache_unique(cachefscache_t *cachep)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 	uint_t unique = 0;
8567c478bd9Sstevel@tonic-gate 	int error = 0;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_contentslock);
8597c478bd9Sstevel@tonic-gate 	if (cachep->c_usage.cu_flags & CUSAGE_NEED_ADJUST ||
8607c478bd9Sstevel@tonic-gate 	    ++(cachep->c_unique) == 0) {
8617c478bd9Sstevel@tonic-gate 		cachep->c_usage.cu_unique++;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		if (cachep->c_unique == 0)
8647c478bd9Sstevel@tonic-gate 			cachep->c_unique = 1;
8657c478bd9Sstevel@tonic-gate 		cachep->c_flags &= ~CUSAGE_NEED_ADJUST;
8667c478bd9Sstevel@tonic-gate 		error = cachefs_cache_rssync(cachep);
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 	if (error == 0)
8697c478bd9Sstevel@tonic-gate 		unique = (cachep->c_usage.cu_unique << 16) + cachep->c_unique;
8707c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_contentslock);
8717c478bd9Sstevel@tonic-gate 	return (unique);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate  * Called from c_getfrontfile. Shouldn't be called from anywhere else !
8767c478bd9Sstevel@tonic-gate  */
8777c478bd9Sstevel@tonic-gate static int
cachefs_createfrontfile(cnode_t * cp,struct filegrp * fgp)8787c478bd9Sstevel@tonic-gate cachefs_createfrontfile(cnode_t *cp, struct filegrp *fgp)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	char name[CFS_FRONTFILE_NAME_SIZE];
8817c478bd9Sstevel@tonic-gate 	struct vattr *attrp = NULL;
8827c478bd9Sstevel@tonic-gate 	int error = 0;
8837c478bd9Sstevel@tonic-gate 	int mode;
8847c478bd9Sstevel@tonic-gate 	int alloc = 0;
8857c478bd9Sstevel@tonic-gate 	int freefile = 0;
8867c478bd9Sstevel@tonic-gate 	int ffrele = 0;
8877c478bd9Sstevel@tonic-gate 	int rlfree = 0;
8887c478bd9Sstevel@tonic-gate 	rl_entry_t rl_ent;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
8917c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_FRONT)
8927c478bd9Sstevel@tonic-gate 		printf("c_createfrontfile: ENTER cp %p fgp %p\n",
8937c478bd9Sstevel@tonic-gate 		    (void *)cp, (void *)fgp);
8947c478bd9Sstevel@tonic-gate #endif
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	ASSERT(cp->c_frontvp == NULL);
8977c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fgp->fg_fscp) == 0);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	/* quit if we cannot write to the filegrp */
9007c478bd9Sstevel@tonic-gate 	if ((fgp->fg_flags & CFS_FG_WRITE) == 0) {
9017c478bd9Sstevel@tonic-gate 		error = ENOENT;
9027c478bd9Sstevel@tonic-gate 		goto out;
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	/* find or create the filegrp attrcache file if necessary */
9067c478bd9Sstevel@tonic-gate 	if (fgp->fg_flags & CFS_FG_ALLOC_ATTR) {
9077c478bd9Sstevel@tonic-gate 		error = filegrp_allocattr(fgp);
9087c478bd9Sstevel@tonic-gate 		if (error)
9097c478bd9Sstevel@tonic-gate 			goto out;
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	make_ascii_name(&cp->c_id, name);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/* set up attributes for the front file we want to create */
9157c478bd9Sstevel@tonic-gate 	attrp = cachefs_kmem_zalloc(sizeof (struct vattr), KM_SLEEP);
9167c478bd9Sstevel@tonic-gate 	alloc++;
9177c478bd9Sstevel@tonic-gate 	attrp->va_mode = S_IFREG | 0666;
9187c478bd9Sstevel@tonic-gate 	mode = 0666;
9197c478bd9Sstevel@tonic-gate 	attrp->va_uid = 0;
9207c478bd9Sstevel@tonic-gate 	attrp->va_gid = 0;
9217c478bd9Sstevel@tonic-gate 	attrp->va_type = VREG;
9227c478bd9Sstevel@tonic-gate 	attrp->va_size = 0;
9237c478bd9Sstevel@tonic-gate 	attrp->va_mask = AT_SIZE | AT_TYPE | AT_MODE | AT_UID | AT_GID;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/* get a file from the resource counts */
9267c478bd9Sstevel@tonic-gate 	error = cachefs_allocfile(fgp->fg_fscp->fs_cache);
9277c478bd9Sstevel@tonic-gate 	if (error) {
9287c478bd9Sstevel@tonic-gate 		error = EINVAL;
9297c478bd9Sstevel@tonic-gate 		goto out;
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate 	freefile++;
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	/* create the metadata slot if necessary */
9347c478bd9Sstevel@tonic-gate 	if (cp->c_flags & CN_ALLOC_PENDING) {
9357c478bd9Sstevel@tonic-gate 		error = filegrp_create_metadata(fgp, &cp->c_metadata,
9367c478bd9Sstevel@tonic-gate 		    &cp->c_id);
9377c478bd9Sstevel@tonic-gate 		if (error) {
9387c478bd9Sstevel@tonic-gate 			error = EINVAL;
9397c478bd9Sstevel@tonic-gate 			goto out;
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 		cp->c_flags &= ~CN_ALLOC_PENDING;
9427c478bd9Sstevel@tonic-gate 		cp->c_flags |= CN_UPDATED;
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	/* get an rl entry if necessary */
9467c478bd9Sstevel@tonic-gate 	if (cp->c_metadata.md_rlno == 0) {
9477c478bd9Sstevel@tonic-gate 		rl_ent.rl_fileno = cp->c_id.cid_fileno;
9487c478bd9Sstevel@tonic-gate 		rl_ent.rl_local = (cp->c_id.cid_flags & CFS_CID_LOCAL) ? 1 : 0;
9497c478bd9Sstevel@tonic-gate 		rl_ent.rl_fsid = fgp->fg_fscp->fs_cfsid;
9507c478bd9Sstevel@tonic-gate 		rl_ent.rl_attrc = 0;
9517c478bd9Sstevel@tonic-gate 		error = cachefs_rl_alloc(fgp->fg_fscp->fs_cache, &rl_ent,
9527c478bd9Sstevel@tonic-gate 		    &cp->c_metadata.md_rlno);
9537c478bd9Sstevel@tonic-gate 		if (error)
9547c478bd9Sstevel@tonic-gate 			goto out;
9557c478bd9Sstevel@tonic-gate 		cachefs_rlent_moveto(fgp->fg_fscp->fs_cache,
9567c478bd9Sstevel@tonic-gate 		    CACHEFS_RL_ACTIVE, cp->c_metadata.md_rlno,
9577c478bd9Sstevel@tonic-gate 		    cp->c_metadata.md_frontblks);
9587c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_rltype = CACHEFS_RL_ACTIVE;
9597c478bd9Sstevel@tonic-gate 		rlfree++;
9607c478bd9Sstevel@tonic-gate 		cp->c_flags |= CN_UPDATED; /* XXX sam: do we need this? */
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 		/* increment number of front files */
9637c478bd9Sstevel@tonic-gate 		error = filegrp_ffhold(fgp);
9647c478bd9Sstevel@tonic-gate 		if (error) {
9657c478bd9Sstevel@tonic-gate 			error = EINVAL;
9667c478bd9Sstevel@tonic-gate 			goto out;
9677c478bd9Sstevel@tonic-gate 		}
9687c478bd9Sstevel@tonic-gate 		ffrele++;
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	if (cp->c_flags & CN_ASYNC_POP_WORKING) {
9727c478bd9Sstevel@tonic-gate 		/* lookup the already created front file */
9737c478bd9Sstevel@tonic-gate 		error = VOP_LOOKUP(fgp->fg_dirvp, name, &cp->c_frontvp,
974da6c28aaSamw 		    NULL, 0, NULL, kcred, NULL, NULL, NULL);
9757c478bd9Sstevel@tonic-gate 	} else {
9767c478bd9Sstevel@tonic-gate 		/* create the front file */
9777c478bd9Sstevel@tonic-gate 		error = VOP_CREATE(fgp->fg_dirvp, name, attrp, EXCL, mode,
978da6c28aaSamw 		    &cp->c_frontvp, kcred, 0, NULL, NULL);
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 	if (error) {
9817c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
9827c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_FRONT)
9837c478bd9Sstevel@tonic-gate 			printf("c_createfrontfile: Can't create cached object"
9847c478bd9Sstevel@tonic-gate 			    " error %u, fileno %llx\n", error,
9857c478bd9Sstevel@tonic-gate 			    (u_longlong_t)cp->c_id.cid_fileno);
9867c478bd9Sstevel@tonic-gate #endif
9877c478bd9Sstevel@tonic-gate 		goto out;
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	/* get a copy of the fid of the front file */
9917c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_fid.fid_len = MAXFIDSZ;
992da6c28aaSamw 	error = VOP_FID(cp->c_frontvp, &cp->c_metadata.md_fid, NULL);
9937c478bd9Sstevel@tonic-gate 	if (error) {
9947c478bd9Sstevel@tonic-gate 		/*
9957c478bd9Sstevel@tonic-gate 		 * If we get back ENOSPC then the fid we passed in was too
9967c478bd9Sstevel@tonic-gate 		 * small.  For now we don't do anything and map to EINVAL.
9977c478bd9Sstevel@tonic-gate 		 */
9987c478bd9Sstevel@tonic-gate 		if (error == ENOSPC) {
9997c478bd9Sstevel@tonic-gate 			error = EINVAL;
10007c478bd9Sstevel@tonic-gate 		}
10017c478bd9Sstevel@tonic-gate 		goto out;
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	dnlc_purge_vp(cp->c_frontvp);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_FILE;
10077c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED | CN_NEED_FRONT_SYNC;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate out:
10107c478bd9Sstevel@tonic-gate 	if (error) {
10117c478bd9Sstevel@tonic-gate 		if (cp->c_frontvp) {
10127c478bd9Sstevel@tonic-gate 			VN_RELE(cp->c_frontvp);
1013da6c28aaSamw 			(void) VOP_REMOVE(fgp->fg_dirvp, name, kcred, NULL, 0);
10147c478bd9Sstevel@tonic-gate 			cp->c_frontvp = NULL;
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 		if (ffrele)
10177c478bd9Sstevel@tonic-gate 			filegrp_ffrele(fgp);
10187c478bd9Sstevel@tonic-gate 		if (freefile)
10197c478bd9Sstevel@tonic-gate 			cachefs_freefile(fgp->fg_fscp->fs_cache);
10207c478bd9Sstevel@tonic-gate 		if (rlfree) {
10217c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
10227c478bd9Sstevel@tonic-gate 			cachefs_rlent_verify(fgp->fg_fscp->fs_cache,
10237c478bd9Sstevel@tonic-gate 			    CACHEFS_RL_ACTIVE, cp->c_metadata.md_rlno);
10247c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
10257c478bd9Sstevel@tonic-gate 			cachefs_rlent_moveto(fgp->fg_fscp->fs_cache,
10267c478bd9Sstevel@tonic-gate 			    CACHEFS_RL_FREE, cp->c_metadata.md_rlno, 0);
10277c478bd9Sstevel@tonic-gate 			cp->c_metadata.md_rlno = 0;
10287c478bd9Sstevel@tonic-gate 			cp->c_metadata.md_rltype = CACHEFS_RL_NONE;
10297c478bd9Sstevel@tonic-gate 		}
10307c478bd9Sstevel@tonic-gate 		cachefs_nocache(cp);
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 	if (alloc)
10337c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(attrp, sizeof (struct vattr));
10347c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
10357c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_FRONT)
10367c478bd9Sstevel@tonic-gate 		printf("c_createfrontfile: EXIT error = %d name %s\n", error,
10377c478bd9Sstevel@tonic-gate 		    name);
10387c478bd9Sstevel@tonic-gate #endif
10397c478bd9Sstevel@tonic-gate 	return (error);
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate /*
10437c478bd9Sstevel@tonic-gate  * Releases resources associated with the front file.
10447c478bd9Sstevel@tonic-gate  * Only call this routine if a ffhold has been done.
10457c478bd9Sstevel@tonic-gate  * Its okay to call this routine if the front file does not exist.
10467c478bd9Sstevel@tonic-gate  * Note: this routine is used even if there is no front file.
10477c478bd9Sstevel@tonic-gate  */
10487c478bd9Sstevel@tonic-gate void
cachefs_removefrontfile(cachefs_metadata_t * mdp,cfs_cid_t * cidp,filegrp_t * fgp)10497c478bd9Sstevel@tonic-gate cachefs_removefrontfile(cachefs_metadata_t *mdp, cfs_cid_t *cidp,
10507c478bd9Sstevel@tonic-gate     filegrp_t *fgp)
10517c478bd9Sstevel@tonic-gate {
10527c478bd9Sstevel@tonic-gate 	int error, enoent;
10537c478bd9Sstevel@tonic-gate 	char name[CFS_FRONTFILE_NAME_SIZE + 2];
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fgp->fg_fscp) == 0);
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	enoent = 0;
10587c478bd9Sstevel@tonic-gate 	if (mdp->md_flags & MD_FILE) {
10597c478bd9Sstevel@tonic-gate 		if (fgp->fg_dirvp == NULL) {
10607c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "cachefs: remove error, run fsck\n");
10617c478bd9Sstevel@tonic-gate 			return;
10627c478bd9Sstevel@tonic-gate 		}
10637c478bd9Sstevel@tonic-gate 		make_ascii_name(cidp, name);
1064da6c28aaSamw 		error = VOP_REMOVE(fgp->fg_dirvp, name, kcred, NULL, 0);
10657c478bd9Sstevel@tonic-gate 		if (error == ENOENT)
10667c478bd9Sstevel@tonic-gate 			enoent = 1;
10677c478bd9Sstevel@tonic-gate 		if ((error) && (error != ENOENT)) {
10687c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "UFS remove error %s %d, run fsck\n",
10697c478bd9Sstevel@tonic-gate 			    name, error);
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 		if (mdp->md_flags & MD_ACLDIR) {
10727c478bd9Sstevel@tonic-gate 			(void) strcat(name, ".d");
10737c478bd9Sstevel@tonic-gate 			error = VOP_RMDIR(fgp->fg_dirvp, name, fgp->fg_dirvp,
1074da6c28aaSamw 			    kcred, NULL, 0);
10757c478bd9Sstevel@tonic-gate 			if ((error) && (error != ENOENT)) {
10767c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "frontfs rmdir error %s %d"
10777c478bd9Sstevel@tonic-gate 				    "; run fsck\n", name, error);
10787c478bd9Sstevel@tonic-gate 			}
10797c478bd9Sstevel@tonic-gate 		}
10807c478bd9Sstevel@tonic-gate 		mdp->md_flags &= ~(MD_FILE | MD_POPULATED | MD_ACL | MD_ACLDIR);
10817c478bd9Sstevel@tonic-gate 		bzero(&mdp->md_allocinfo, mdp->md_allocents *
10827c478bd9Sstevel@tonic-gate 		    sizeof (struct cachefs_allocmap));
10837c478bd9Sstevel@tonic-gate 		cachefs_freefile(fgp->fg_fscp->fs_cache);
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	/*
10877c478bd9Sstevel@tonic-gate 	 * Clear packed bit, fastsymlinks and special files
10887c478bd9Sstevel@tonic-gate 	 * do not have a front file.
10897c478bd9Sstevel@tonic-gate 	 */
10907c478bd9Sstevel@tonic-gate 	mdp->md_flags &= ~MD_PACKED;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/* XXX either rename routine or move this to caller */
10937c478bd9Sstevel@tonic-gate 	if (enoent == 0)
10947c478bd9Sstevel@tonic-gate 		filegrp_ffrele(fgp);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if (mdp->md_frontblks) {
10977c478bd9Sstevel@tonic-gate 		cachefs_freeblocks(fgp->fg_fscp->fs_cache, mdp->md_frontblks,
10987c478bd9Sstevel@tonic-gate 		    mdp->md_rltype);
10997c478bd9Sstevel@tonic-gate 		mdp->md_frontblks = 0;
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate /*
11047c478bd9Sstevel@tonic-gate  * This is the interface to the rest of CFS. This takes a cnode, and returns
11057c478bd9Sstevel@tonic-gate  * the frontvp (stuffs it in the cnode). This creates an attrcache slot and
11067c478bd9Sstevel@tonic-gate  * and frontfile if necessary.
11077c478bd9Sstevel@tonic-gate  */
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate int
cachefs_getfrontfile(cnode_t * cp)11107c478bd9Sstevel@tonic-gate cachefs_getfrontfile(cnode_t *cp)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	struct filegrp *fgp = cp->c_filegrp;
11137c478bd9Sstevel@tonic-gate 	int error;
11147c478bd9Sstevel@tonic-gate 	struct vattr va;
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
11177c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_SUBR)
11187c478bd9Sstevel@tonic-gate 		printf("c_getfrontfile: ENTER cp %p\n", (void *)cp);
11197c478bd9Sstevel@tonic-gate #endif
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fgp->fg_fscp) == 0);
11227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cp->c_statelock));
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	/*
11257c478bd9Sstevel@tonic-gate 	 * Now we check to see if there is a front file for this entry.
11267c478bd9Sstevel@tonic-gate 	 * If there is, we get the vnode for it and stick it in the cnode.
11277c478bd9Sstevel@tonic-gate 	 * Otherwise, we create a front file, get the vnode for it and stick
11287c478bd9Sstevel@tonic-gate 	 * it in the cnode.
11297c478bd9Sstevel@tonic-gate 	 */
11307c478bd9Sstevel@tonic-gate 	if (cp->c_flags & CN_STALE) {
11317c478bd9Sstevel@tonic-gate 		cp->c_flags |= CN_NOCACHE;
11327c478bd9Sstevel@tonic-gate 		error = ESTALE;
11337c478bd9Sstevel@tonic-gate 		goto out;
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	/*
11377c478bd9Sstevel@tonic-gate 	 * If the cnode is being populated, and we're not the populating
11387c478bd9Sstevel@tonic-gate 	 * thread, then block until the pop thread completes.  If we are the
11397c478bd9Sstevel@tonic-gate 	 * pop thread, then we may come in here, but not to nuke the directory
11407c478bd9Sstevel@tonic-gate 	 * cnode at a critical juncture.  If we return from a cv_wait and the
11417c478bd9Sstevel@tonic-gate 	 * cnode is now stale, don't bother trying to get the front file.
11427c478bd9Sstevel@tonic-gate 	 */
11437c478bd9Sstevel@tonic-gate 	while ((cp->c_flags & CN_ASYNC_POP_WORKING) &&
11447c478bd9Sstevel@tonic-gate 	    (cp->c_popthrp != curthread)) {
11457c478bd9Sstevel@tonic-gate 		cv_wait(&cp->c_popcv, &cp->c_statelock);
11467c478bd9Sstevel@tonic-gate 		if (cp->c_flags & CN_STALE) {
11477c478bd9Sstevel@tonic-gate 			cp->c_flags |= CN_NOCACHE;
11487c478bd9Sstevel@tonic-gate 			error = ESTALE;
11497c478bd9Sstevel@tonic-gate 			goto out;
11507c478bd9Sstevel@tonic-gate 		}
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	if ((cp->c_metadata.md_flags & MD_FILE) == 0) {
11547c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
11557c478bd9Sstevel@tonic-gate 		if (cp->c_frontvp != NULL)
11567c478bd9Sstevel@tonic-gate 			CFS_DEBUG(CFSDEBUG_FRONT)
1157*d3d50737SRafael Vanoni 				printf("c_getfrontfile: !MD_FILE and frontvp "
1158*d3d50737SRafael Vanoni 				    "not null cp %p\n", (void *)cp);
11597c478bd9Sstevel@tonic-gate #endif
11607c478bd9Sstevel@tonic-gate 		if (CTOV(cp)->v_type == VDIR)
11617c478bd9Sstevel@tonic-gate 			ASSERT((cp->c_metadata.md_flags & MD_POPULATED) == 0);
11627c478bd9Sstevel@tonic-gate 		error = cachefs_createfrontfile(cp, fgp);
11637c478bd9Sstevel@tonic-gate 		if (error)
11647c478bd9Sstevel@tonic-gate 			goto out;
11657c478bd9Sstevel@tonic-gate 	} else {
11667c478bd9Sstevel@tonic-gate 		/*
11677c478bd9Sstevel@tonic-gate 		 * A front file exists, all we need to do is to grab the fid,
11687c478bd9Sstevel@tonic-gate 		 * do a VFS_VGET() on the fid, stuff the vnode in the cnode,
11697c478bd9Sstevel@tonic-gate 		 * and return.
11707c478bd9Sstevel@tonic-gate 		 */
11717c478bd9Sstevel@tonic-gate 		if (fgp->fg_dirvp == NULL) {
11727c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "cachefs: gff0: corrupted file system"
11737c478bd9Sstevel@tonic-gate 			    " run fsck\n");
11747c478bd9Sstevel@tonic-gate 			cachefs_inval_object(cp);
11757c478bd9Sstevel@tonic-gate 			cp->c_flags |= CN_NOCACHE;
11767c478bd9Sstevel@tonic-gate 			error = ESTALE;
11777c478bd9Sstevel@tonic-gate 			goto out;
11787c478bd9Sstevel@tonic-gate 		}
11797c478bd9Sstevel@tonic-gate 		error = VFS_VGET(fgp->fg_dirvp->v_vfsp, &cp->c_frontvp,
11807c478bd9Sstevel@tonic-gate 		    &cp->c_metadata.md_fid);
11817c478bd9Sstevel@tonic-gate 		if (error || (cp->c_frontvp == NULL)) {
11827c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
11837c478bd9Sstevel@tonic-gate 			CFS_DEBUG(CFSDEBUG_FRONT)
11847c478bd9Sstevel@tonic-gate 				printf("cachefs: "
11857c478bd9Sstevel@tonic-gate 				    "gff1: front file system error %d\n",
11867c478bd9Sstevel@tonic-gate 				    error);
11877c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
11887c478bd9Sstevel@tonic-gate 			cachefs_inval_object(cp);
11897c478bd9Sstevel@tonic-gate 			cp->c_flags |= CN_NOCACHE;
11907c478bd9Sstevel@tonic-gate 			error = ESTALE;
11917c478bd9Sstevel@tonic-gate 			goto out;
11927c478bd9Sstevel@tonic-gate 		}
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 		/* don't need to check timestamps if need_front_sync is set */
11957c478bd9Sstevel@tonic-gate 		if (cp->c_flags & CN_NEED_FRONT_SYNC) {
11967c478bd9Sstevel@tonic-gate 			error = 0;
11977c478bd9Sstevel@tonic-gate 			goto out;
11987c478bd9Sstevel@tonic-gate 		}
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 		/* don't need to check empty directories */
12017c478bd9Sstevel@tonic-gate 		if (CTOV(cp)->v_type == VDIR &&
12027c478bd9Sstevel@tonic-gate 		    ((cp->c_metadata.md_flags & MD_POPULATED) == 0)) {
12037c478bd9Sstevel@tonic-gate 			error = 0;
12047c478bd9Sstevel@tonic-gate 			goto out;
12057c478bd9Sstevel@tonic-gate 		}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 		/* get modify time of the front file */
12087c478bd9Sstevel@tonic-gate 		va.va_mask = AT_MTIME;
1209da6c28aaSamw 		error = VOP_GETATTR(cp->c_frontvp, &va, 0, kcred, NULL);
12107c478bd9Sstevel@tonic-gate 		if (error) {
12117c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "cachefs: gff2: front file"
12127c478bd9Sstevel@tonic-gate 			    " system error %d", error);
12137c478bd9Sstevel@tonic-gate 			cachefs_inval_object(cp);
12147c478bd9Sstevel@tonic-gate 			error = (cp->c_flags & CN_NOCACHE) ? ESTALE : 0;
12157c478bd9Sstevel@tonic-gate 			goto out;
12167c478bd9Sstevel@tonic-gate 		}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 		/* compare with modify time stored in metadata */
12197c478bd9Sstevel@tonic-gate 		if (bcmp(&va.va_mtime, &cp->c_metadata.md_timestamp,
12207c478bd9Sstevel@tonic-gate 		    sizeof (timestruc_t)) != 0) {
12217c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
12227c478bd9Sstevel@tonic-gate 			CFS_DEBUG(CFSDEBUG_GENERAL | CFSDEBUG_INVALIDATE) {
12237c478bd9Sstevel@tonic-gate 				long sec, nsec;
12247c478bd9Sstevel@tonic-gate 				sec = cp->c_metadata.md_timestamp.tv_sec;
12257c478bd9Sstevel@tonic-gate 				nsec = cp->c_metadata.md_timestamp.tv_nsec;
12267c478bd9Sstevel@tonic-gate 				printf("c_getfrontfile: timestamps don't"
12277c478bd9Sstevel@tonic-gate 				    " match fileno %lld va %lx %lx"
12287c478bd9Sstevel@tonic-gate 				    " meta %lx %lx\n",
12297c478bd9Sstevel@tonic-gate 				    (u_longlong_t)cp->c_id.cid_fileno,
12307c478bd9Sstevel@tonic-gate 				    va.va_mtime.tv_sec,
12317c478bd9Sstevel@tonic-gate 				    va.va_mtime.tv_nsec, sec, nsec);
12327c478bd9Sstevel@tonic-gate 			}
12337c478bd9Sstevel@tonic-gate #endif
12347c478bd9Sstevel@tonic-gate 			cachefs_inval_object(cp);
12357c478bd9Sstevel@tonic-gate 			error = (cp->c_flags & CN_NOCACHE) ? ESTALE : 0;
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate out:
12397c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
12407c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_FRONT)
12417c478bd9Sstevel@tonic-gate 		printf("c_getfrontfile: EXIT error = %d\n", error);
12427c478bd9Sstevel@tonic-gate #endif
12437c478bd9Sstevel@tonic-gate 	return (error);
12447c478bd9Sstevel@tonic-gate }
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate void
cachefs_inval_object(cnode_t * cp)12477c478bd9Sstevel@tonic-gate cachefs_inval_object(cnode_t *cp)
12487c478bd9Sstevel@tonic-gate {
12497c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = C_TO_FSCACHE(cp)->fs_cache;
12507c478bd9Sstevel@tonic-gate 	struct filegrp *fgp = cp->c_filegrp;
12517c478bd9Sstevel@tonic-gate 	int error;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(C_TO_FSCACHE(cp)) == 0);
12547c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cp->c_statelock));
12557c478bd9Sstevel@tonic-gate 	ASSERT((cp->c_flags & CN_ASYNC_POP_WORKING) == 0 ||
12567c478bd9Sstevel@tonic-gate 	    cp->c_popthrp == curthread);
12577c478bd9Sstevel@tonic-gate #if 0
12587c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_SUBR)
12597c478bd9Sstevel@tonic-gate 		printf("c_inval_object: ENTER cp %p\n", (void *)cp);
12607c478bd9Sstevel@tonic-gate 	if (cp->c_flags & (CN_ASYNC_POPULATE | CN_ASYNC_POP_WORKING))
12617c478bd9Sstevel@tonic-gate 		debug_enter("inval object during async pop");
12627c478bd9Sstevel@tonic-gate #endif
12637c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_NOCACHE;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	/* if we cannot modify the cache */
12667c478bd9Sstevel@tonic-gate 	if (C_TO_FSCACHE(cp)->fs_cache->c_flags &
12677c478bd9Sstevel@tonic-gate 	    (CACHE_NOFILL | CACHE_NOCACHE)) {
12687c478bd9Sstevel@tonic-gate 		goto out;
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	/* if there is a front file */
12727c478bd9Sstevel@tonic-gate 	if (cp->c_metadata.md_flags & MD_FILE) {
12737c478bd9Sstevel@tonic-gate 		if (fgp->fg_dirvp == NULL)
12747c478bd9Sstevel@tonic-gate 			goto out;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 		/* get the front file vp if necessary */
12777c478bd9Sstevel@tonic-gate 		if (cp->c_frontvp == NULL) {
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 			error = VFS_VGET(fgp->fg_dirvp->v_vfsp, &cp->c_frontvp,
12807c478bd9Sstevel@tonic-gate 			    &cp->c_metadata.md_fid);
12817c478bd9Sstevel@tonic-gate 			if (error || (cp->c_frontvp == NULL)) {
12827c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
12837c478bd9Sstevel@tonic-gate 				CFS_DEBUG(CFSDEBUG_FRONT)
12847c478bd9Sstevel@tonic-gate 					printf("cachefs: "
12857c478bd9Sstevel@tonic-gate 					    "io: front file error %d\n", error);
12867c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
12877c478bd9Sstevel@tonic-gate 				goto out;
12887c478bd9Sstevel@tonic-gate 			}
12897c478bd9Sstevel@tonic-gate 		}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 		/* truncate the file to zero size */
12927c478bd9Sstevel@tonic-gate 		error = cachefs_frontfile_size(cp, 0);
12937c478bd9Sstevel@tonic-gate 		if (error)
12947c478bd9Sstevel@tonic-gate 			goto out;
12957c478bd9Sstevel@tonic-gate 		cp->c_flags &= ~CN_NOCACHE;
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 		/* if a directory, v_type is zero if called from initcnode */
12987c478bd9Sstevel@tonic-gate 		if (cp->c_attr.va_type == VDIR) {
12997c478bd9Sstevel@tonic-gate 			if (cp->c_usage < CFS_DIRCACHE_COST) {
13007c478bd9Sstevel@tonic-gate 				cp->c_invals++;
13017c478bd9Sstevel@tonic-gate 				if (cp->c_invals > CFS_DIRCACHE_INVAL) {
13027c478bd9Sstevel@tonic-gate 					cp->c_invals = 0;
13037c478bd9Sstevel@tonic-gate 				}
13047c478bd9Sstevel@tonic-gate 			} else
13057c478bd9Sstevel@tonic-gate 				cp->c_invals = 0;
13067c478bd9Sstevel@tonic-gate 			cp->c_usage = 0;
13077c478bd9Sstevel@tonic-gate 		}
13087c478bd9Sstevel@tonic-gate 	} else {
13097c478bd9Sstevel@tonic-gate 		cp->c_flags &= ~CN_NOCACHE;
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate out:
13137c478bd9Sstevel@tonic-gate 	if ((cp->c_metadata.md_flags & MD_PACKED) &&
13147c478bd9Sstevel@tonic-gate 	    (cp->c_metadata.md_rltype != CACHEFS_RL_MODIFIED) &&
13157c478bd9Sstevel@tonic-gate 	    ((cachep->c_flags & CACHE_NOFILL) == 0)) {
13167c478bd9Sstevel@tonic-gate 		ASSERT(cp->c_metadata.md_rlno != 0);
13177c478bd9Sstevel@tonic-gate 		if (cp->c_metadata.md_rltype != CACHEFS_RL_PACKED_PENDING) {
13187c478bd9Sstevel@tonic-gate 			cachefs_rlent_moveto(cachep,
13197c478bd9Sstevel@tonic-gate 			    CACHEFS_RL_PACKED_PENDING,
13207c478bd9Sstevel@tonic-gate 			    cp->c_metadata.md_rlno,
13217c478bd9Sstevel@tonic-gate 			    cp->c_metadata.md_frontblks);
13227c478bd9Sstevel@tonic-gate 			cp->c_metadata.md_rltype = CACHEFS_RL_PACKED_PENDING;
13237c478bd9Sstevel@tonic-gate 			/* unconditionally set CN_UPDATED below */
13247c478bd9Sstevel@tonic-gate 		}
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	cachefs_purgeacl(cp);
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	if (cp->c_flags & CN_ASYNC_POP_WORKING)
13307c478bd9Sstevel@tonic-gate 		cp->c_flags |= CN_NOCACHE;
13317c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags &= ~(MD_POPULATED | MD_INVALREADDIR |
13327c478bd9Sstevel@tonic-gate 	    MD_FASTSYMLNK);
13337c478bd9Sstevel@tonic-gate 	cp->c_flags &= ~CN_NEED_FRONT_SYNC;
13347c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	/*
13377c478bd9Sstevel@tonic-gate 	 * If the object invalidated is a directory, the dnlc should be purged
13387c478bd9Sstevel@tonic-gate 	 * to elide all references to this (directory) vnode.
13397c478bd9Sstevel@tonic-gate 	 */
13407c478bd9Sstevel@tonic-gate 	if (CTOV(cp)->v_type == VDIR)
13417c478bd9Sstevel@tonic-gate 		dnlc_purge_vp(CTOV(cp));
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
13447c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_SUBR)
13457c478bd9Sstevel@tonic-gate 		printf("c_inval_object: EXIT\n");
13467c478bd9Sstevel@tonic-gate #endif
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate void
make_ascii_name(cfs_cid_t * cidp,char * strp)13507c478bd9Sstevel@tonic-gate make_ascii_name(cfs_cid_t *cidp, char *strp)
13517c478bd9Sstevel@tonic-gate {
13527c478bd9Sstevel@tonic-gate 	int i = sizeof (uint_t) * 4;
13537c478bd9Sstevel@tonic-gate 	u_longlong_t index;
13547c478bd9Sstevel@tonic-gate 	ino64_t name;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	if (cidp->cid_flags & CFS_CID_LOCAL)
13577c478bd9Sstevel@tonic-gate 		*strp++ = 'L';
13587c478bd9Sstevel@tonic-gate 	name = (ino64_t)cidp->cid_fileno;
13597c478bd9Sstevel@tonic-gate 	do {
13607c478bd9Sstevel@tonic-gate 		index = (((u_longlong_t)name) & 0xf000000000000000) >> 60;
13617c478bd9Sstevel@tonic-gate 		index &= (u_longlong_t)0xf;
13627c478bd9Sstevel@tonic-gate 		ASSERT(index < (u_longlong_t)16);
13637c478bd9Sstevel@tonic-gate 		*strp++ = "0123456789abcdef"[index];
13647c478bd9Sstevel@tonic-gate 		name <<= 4;
13657c478bd9Sstevel@tonic-gate 	} while (--i);
13667c478bd9Sstevel@tonic-gate 	*strp = '\0';
13677c478bd9Sstevel@tonic-gate }
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate void
cachefs_nocache(cnode_t * cp)13707c478bd9Sstevel@tonic-gate cachefs_nocache(cnode_t *cp)
13717c478bd9Sstevel@tonic-gate {
13727c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(cp);
13737c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
13767c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_SUBR)
13777c478bd9Sstevel@tonic-gate 		printf("c_nocache: ENTER cp %p\n", (void *)cp);
13787c478bd9Sstevel@tonic-gate #endif
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
13817c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cp->c_statelock));
13827c478bd9Sstevel@tonic-gate 	if ((cp->c_flags & CN_NOCACHE) == 0) {
13837c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
13847c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_INVALIDATE)
13857c478bd9Sstevel@tonic-gate 			printf("cachefs_nocache: invalidating %llu\n",
13867c478bd9Sstevel@tonic-gate 			    (u_longlong_t)cp->c_id.cid_fileno);
13877c478bd9Sstevel@tonic-gate #endif
13887c478bd9Sstevel@tonic-gate 		/*
13897c478bd9Sstevel@tonic-gate 		 * Here we are waiting until inactive time to do
13907c478bd9Sstevel@tonic-gate 		 * the inval_object.  In case we don't get to inactive
13917c478bd9Sstevel@tonic-gate 		 * (because of a crash, say) we set up a timestamp mismatch
13927c478bd9Sstevel@tonic-gate 		 * such that getfrontfile will blow the front file away
13937c478bd9Sstevel@tonic-gate 		 * next time we try to use it.
13947c478bd9Sstevel@tonic-gate 		 */
13957c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_timestamp.tv_sec = 0;
13967c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_timestamp.tv_nsec = 0;
13977c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_flags &= ~(MD_POPULATED | MD_INVALREADDIR |
13987c478bd9Sstevel@tonic-gate 		    MD_FASTSYMLNK);
13997c478bd9Sstevel@tonic-gate 		cp->c_flags &= ~CN_NEED_FRONT_SYNC;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 		cachefs_purgeacl(cp);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 		/*
14047c478bd9Sstevel@tonic-gate 		 * It is possible we can nocache while disconnected.
14057c478bd9Sstevel@tonic-gate 		 * A directory could be nocached by running out of space.
14067c478bd9Sstevel@tonic-gate 		 * A regular file should only be nocached if an I/O error
14077c478bd9Sstevel@tonic-gate 		 * occurs to the front fs.
14087c478bd9Sstevel@tonic-gate 		 * We count on the item staying on the modified list
14097c478bd9Sstevel@tonic-gate 		 * so we do not loose the cid to fid mapping for directories.
14107c478bd9Sstevel@tonic-gate 		 */
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 		if ((cp->c_metadata.md_flags & MD_PACKED) &&
14137c478bd9Sstevel@tonic-gate 		    (cp->c_metadata.md_rltype != CACHEFS_RL_MODIFIED) &&
14147c478bd9Sstevel@tonic-gate 		    ((cachep->c_flags & CACHE_NOFILL) == 0)) {
14157c478bd9Sstevel@tonic-gate 			ASSERT(cp->c_metadata.md_rlno != 0);
14167c478bd9Sstevel@tonic-gate 			if (cp->c_metadata.md_rltype !=
14177c478bd9Sstevel@tonic-gate 			    CACHEFS_RL_PACKED_PENDING) {
14187c478bd9Sstevel@tonic-gate 				cachefs_rlent_moveto(cachep,
14197c478bd9Sstevel@tonic-gate 				    CACHEFS_RL_PACKED_PENDING,
14207c478bd9Sstevel@tonic-gate 				    cp->c_metadata.md_rlno,
14217c478bd9Sstevel@tonic-gate 				    cp->c_metadata.md_frontblks);
14227c478bd9Sstevel@tonic-gate 				cp->c_metadata.md_rltype =
14237c478bd9Sstevel@tonic-gate 				    CACHEFS_RL_PACKED_PENDING;
14247c478bd9Sstevel@tonic-gate 				/* unconditionally set CN_UPDATED below */
14257c478bd9Sstevel@tonic-gate 			}
14267c478bd9Sstevel@tonic-gate 		}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 		if (CTOV(cp)->v_type == VDIR)
14297c478bd9Sstevel@tonic-gate 			dnlc_purge_vp(CTOV(cp));
14307c478bd9Sstevel@tonic-gate 		cp->c_flags |= (CN_NOCACHE | CN_UPDATED);
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_NOCACHE))
14347c478bd9Sstevel@tonic-gate 		cachefs_log_nocache(cachep, 0, fscp->fs_cfsvfsp,
14357c478bd9Sstevel@tonic-gate 		    &cp->c_metadata.md_cookie, cp->c_id.cid_fileno);
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
14387c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_SUBR)
14397c478bd9Sstevel@tonic-gate 		printf("c_nocache: EXIT cp %p\n", (void *)cp);
14407c478bd9Sstevel@tonic-gate #endif
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate /*
14447c478bd9Sstevel@tonic-gate  * Checks to see if the page is in the disk cache, by checking the allocmap.
14457c478bd9Sstevel@tonic-gate  */
14467c478bd9Sstevel@tonic-gate int
cachefs_check_allocmap(cnode_t * cp,u_offset_t off)14477c478bd9Sstevel@tonic-gate cachefs_check_allocmap(cnode_t *cp, u_offset_t off)
14487c478bd9Sstevel@tonic-gate {
14497c478bd9Sstevel@tonic-gate 	int i;
14507c478bd9Sstevel@tonic-gate 	size_t dbl_size_to_look = cp->c_attr.va_size - off;
14517c478bd9Sstevel@tonic-gate 	uint_t	size_to_look;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	if (dbl_size_to_look > (u_offset_t)PAGESIZE)
14547c478bd9Sstevel@tonic-gate 		size_to_look = (uint_t)PAGESIZE;
14557c478bd9Sstevel@tonic-gate 	else
14567c478bd9Sstevel@tonic-gate 		/*LINTED alignment okay*/
14577c478bd9Sstevel@tonic-gate 		size_to_look = (uint_t)dbl_size_to_look;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	for (i = 0; i < cp->c_metadata.md_allocents; i++) {
14607c478bd9Sstevel@tonic-gate 		struct cachefs_allocmap *allocp =
14617c478bd9Sstevel@tonic-gate 		    cp->c_metadata.md_allocinfo + i;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 		if (off >= allocp->am_start_off) {
14647c478bd9Sstevel@tonic-gate 			if ((off + size_to_look) <=
14657c478bd9Sstevel@tonic-gate 			    (allocp->am_start_off + allocp->am_size)) {
14667c478bd9Sstevel@tonic-gate 				struct fscache *fscp = C_TO_FSCACHE(cp);
14677c478bd9Sstevel@tonic-gate 				cachefscache_t *cachep = fscp->fs_cache;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 				if (CACHEFS_LOG_LOGGING(cachep,
14707c478bd9Sstevel@tonic-gate 				    CACHEFS_LOG_CALLOC))
14717c478bd9Sstevel@tonic-gate 					cachefs_log_calloc(cachep, 0,
14727c478bd9Sstevel@tonic-gate 					    fscp->fs_cfsvfsp,
14737c478bd9Sstevel@tonic-gate 					    &cp->c_metadata.md_cookie,
14747c478bd9Sstevel@tonic-gate 					    cp->c_id.cid_fileno,
14757c478bd9Sstevel@tonic-gate 					    off, size_to_look);
14767c478bd9Sstevel@tonic-gate 			/*
14777c478bd9Sstevel@tonic-gate 			 * Found the page in the CFS disk cache.
14787c478bd9Sstevel@tonic-gate 			 */
14797c478bd9Sstevel@tonic-gate 				return (1);
14807c478bd9Sstevel@tonic-gate 			}
14817c478bd9Sstevel@tonic-gate 		} else {
14827c478bd9Sstevel@tonic-gate 			return (0);
14837c478bd9Sstevel@tonic-gate 		}
14847c478bd9Sstevel@tonic-gate 	}
14857c478bd9Sstevel@tonic-gate 	return (0);
14867c478bd9Sstevel@tonic-gate }
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate /*
14897c478bd9Sstevel@tonic-gate  * Merges adjacent allocmap entries together where possible, e.g.
14907c478bd9Sstevel@tonic-gate  *   offset=0x0,     size=0x40000
14917c478bd9Sstevel@tonic-gate  *   offset=0x40000, size=0x20000	becomes just offset=0x0, size-0x90000
14927c478bd9Sstevel@tonic-gate  *   offset=0x60000, size=0x30000
14937c478bd9Sstevel@tonic-gate  */
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate void
cachefs_coalesce_allocmap(struct cachefs_metadata * cmd)14977c478bd9Sstevel@tonic-gate cachefs_coalesce_allocmap(struct cachefs_metadata *cmd)
14987c478bd9Sstevel@tonic-gate {
14997c478bd9Sstevel@tonic-gate 	int i, reduced = 0;
15007c478bd9Sstevel@tonic-gate 	struct cachefs_allocmap *allocp, *nallocp;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	nallocp = allocp = cmd->md_allocinfo;
15037c478bd9Sstevel@tonic-gate 	allocp++;
15047c478bd9Sstevel@tonic-gate 	for (i = 1; i < cmd->md_allocents; i++, allocp++) {
15057c478bd9Sstevel@tonic-gate 		if (nallocp->am_start_off + nallocp->am_size ==
15067c478bd9Sstevel@tonic-gate 		    allocp->am_start_off) {
15077c478bd9Sstevel@tonic-gate 			nallocp->am_size += allocp->am_size;
15087c478bd9Sstevel@tonic-gate 			reduced++;
15097c478bd9Sstevel@tonic-gate 		} else {
15107c478bd9Sstevel@tonic-gate 			nallocp++;
15117c478bd9Sstevel@tonic-gate 			nallocp->am_start_off = allocp->am_start_off;
15127c478bd9Sstevel@tonic-gate 			nallocp->am_size = allocp->am_size;
15137c478bd9Sstevel@tonic-gate 		}
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 	cmd->md_allocents -= reduced;
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate /*
15197c478bd9Sstevel@tonic-gate  * Updates the allocmap to reflect a new chunk of data that has been
15207c478bd9Sstevel@tonic-gate  * populated.
15217c478bd9Sstevel@tonic-gate  */
15227c478bd9Sstevel@tonic-gate void
cachefs_update_allocmap(cnode_t * cp,u_offset_t off,size_t size)15237c478bd9Sstevel@tonic-gate cachefs_update_allocmap(cnode_t *cp, u_offset_t off, size_t size)
15247c478bd9Sstevel@tonic-gate {
15257c478bd9Sstevel@tonic-gate 	int i;
15267c478bd9Sstevel@tonic-gate 	struct cachefs_allocmap *allocp;
15277c478bd9Sstevel@tonic-gate 	struct fscache *fscp =  C_TO_FSCACHE(cp);
15287c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
15297c478bd9Sstevel@tonic-gate 	u_offset_t saveoff;
15307c478bd9Sstevel@tonic-gate 	u_offset_t savesize;
15317c478bd9Sstevel@tonic-gate 	u_offset_t logoff = off;
15327c478bd9Sstevel@tonic-gate 	size_t logsize = size;
15337c478bd9Sstevel@tonic-gate 	u_offset_t endoff;
15347c478bd9Sstevel@tonic-gate 	u_offset_t tmpendoff;
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	/*
15377c478bd9Sstevel@tonic-gate 	 * We try to see if we can coalesce the current block into an existing
15387c478bd9Sstevel@tonic-gate 	 * allocation and mark it as such.
15397c478bd9Sstevel@tonic-gate 	 * If we can't do that then we make a new entry in the allocmap.
15407c478bd9Sstevel@tonic-gate 	 * when we run out of allocmaps, put the cnode in NOCACHE mode.
15417c478bd9Sstevel@tonic-gate 	 */
15427c478bd9Sstevel@tonic-gate again:
15437c478bd9Sstevel@tonic-gate 	allocp = cp->c_metadata.md_allocinfo;
15447c478bd9Sstevel@tonic-gate 	for (i = 0; i < cp->c_metadata.md_allocents; i++, allocp++) {
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 		if (off <= (allocp->am_start_off)) {
15477c478bd9Sstevel@tonic-gate 			endoff = off + size;
15487c478bd9Sstevel@tonic-gate 			if (endoff >= allocp->am_start_off) {
15497c478bd9Sstevel@tonic-gate 				tmpendoff = allocp->am_start_off +
15507c478bd9Sstevel@tonic-gate 				    allocp->am_size;
15517c478bd9Sstevel@tonic-gate 				if (endoff < tmpendoff)
15527c478bd9Sstevel@tonic-gate 					endoff = tmpendoff;
15537c478bd9Sstevel@tonic-gate 				allocp->am_size = endoff - off;
15547c478bd9Sstevel@tonic-gate 				allocp->am_start_off = off;
15557c478bd9Sstevel@tonic-gate 				cachefs_coalesce_allocmap(&cp->c_metadata);
15567c478bd9Sstevel@tonic-gate 				allocp = cp->c_metadata.md_allocinfo;
15577c478bd9Sstevel@tonic-gate 				if (allocp->am_size >= cp->c_size)
15587c478bd9Sstevel@tonic-gate 					cp->c_metadata.md_flags |= MD_POPULATED;
15597c478bd9Sstevel@tonic-gate 				return;
15607c478bd9Sstevel@tonic-gate 			} else {
15617c478bd9Sstevel@tonic-gate 				saveoff = off;
15627c478bd9Sstevel@tonic-gate 				savesize = size;
15637c478bd9Sstevel@tonic-gate 				off = allocp->am_start_off;
15647c478bd9Sstevel@tonic-gate 				size = allocp->am_size;
15657c478bd9Sstevel@tonic-gate 				allocp->am_size = savesize;
15667c478bd9Sstevel@tonic-gate 				allocp->am_start_off = saveoff;
15677c478bd9Sstevel@tonic-gate 				goto again;
15687c478bd9Sstevel@tonic-gate 			}
15697c478bd9Sstevel@tonic-gate 		} else {
15707c478bd9Sstevel@tonic-gate 			endoff = allocp->am_start_off + allocp->am_size;
15717c478bd9Sstevel@tonic-gate 			if (off < endoff) {
15727c478bd9Sstevel@tonic-gate 				tmpendoff = off + size;
15737c478bd9Sstevel@tonic-gate 				if (endoff < tmpendoff)
15747c478bd9Sstevel@tonic-gate 					endoff = tmpendoff;
15757c478bd9Sstevel@tonic-gate 				allocp->am_size = endoff - allocp->am_start_off;
15767c478bd9Sstevel@tonic-gate 				cachefs_coalesce_allocmap(&cp->c_metadata);
15777c478bd9Sstevel@tonic-gate 				allocp = cp->c_metadata.md_allocinfo;
15787c478bd9Sstevel@tonic-gate 				if (allocp->am_size >= cp->c_size)
15797c478bd9Sstevel@tonic-gate 					cp->c_metadata.md_flags |= MD_POPULATED;
15807c478bd9Sstevel@tonic-gate 				return;
15817c478bd9Sstevel@tonic-gate 			}
15827c478bd9Sstevel@tonic-gate 			if (off == (allocp->am_start_off + allocp->am_size)) {
15837c478bd9Sstevel@tonic-gate 				allocp->am_size += size;
15847c478bd9Sstevel@tonic-gate 				cachefs_coalesce_allocmap(&cp->c_metadata);
15857c478bd9Sstevel@tonic-gate 				allocp = cp->c_metadata.md_allocinfo;
15867c478bd9Sstevel@tonic-gate 				if (allocp->am_size >= cp->c_size)
15877c478bd9Sstevel@tonic-gate 					cp->c_metadata.md_flags |= MD_POPULATED;
15887c478bd9Sstevel@tonic-gate 				return;
15897c478bd9Sstevel@tonic-gate 			}
15907c478bd9Sstevel@tonic-gate 		}
15917c478bd9Sstevel@tonic-gate 	}
15927c478bd9Sstevel@tonic-gate 	if (i == C_MAX_ALLOCINFO_SLOTS) {
15937c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
15947c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_ALLOCMAP)
15957c478bd9Sstevel@tonic-gate 			printf("c_update_alloc_map: "
15967c478bd9Sstevel@tonic-gate 			    "Too many allinfo entries cp %p fileno %llu %p\n",
15977c478bd9Sstevel@tonic-gate 			    (void *)cp, (u_longlong_t)cp->c_id.cid_fileno,
15987c478bd9Sstevel@tonic-gate 			    (void *)cp->c_metadata.md_allocinfo);
15997c478bd9Sstevel@tonic-gate #endif
16007c478bd9Sstevel@tonic-gate 		cachefs_nocache(cp);
16017c478bd9Sstevel@tonic-gate 		return;
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 	allocp->am_start_off = off;
16047c478bd9Sstevel@tonic-gate 	allocp->am_size = (u_offset_t)size;
16057c478bd9Sstevel@tonic-gate 	if (allocp->am_size >= cp->c_size)
16067c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_flags |= MD_POPULATED;
16077c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_allocents++;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_UALLOC))
16107c478bd9Sstevel@tonic-gate 		cachefs_log_ualloc(cachep, 0, fscp->fs_cfsvfsp,
16117c478bd9Sstevel@tonic-gate 		    &cp->c_metadata.md_cookie, cp->c_id.cid_fileno,
16127c478bd9Sstevel@tonic-gate 		    logoff, logsize);
16137c478bd9Sstevel@tonic-gate }
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate /*
16167c478bd9Sstevel@tonic-gate  * CFS population function
16177c478bd9Sstevel@tonic-gate  *
16187c478bd9Sstevel@tonic-gate  * before async population, this function used to turn on the cnode
16197c478bd9Sstevel@tonic-gate  * flags CN_UPDATED, CN_NEED_FRONT_SYNC, and CN_POPULATION_PENDING.
16207c478bd9Sstevel@tonic-gate  * now, however, it's the responsibility of the caller to do this if
16217c478bd9Sstevel@tonic-gate  * this function returns 0 (no error).
16227c478bd9Sstevel@tonic-gate  */
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate int
cachefs_populate(cnode_t * cp,u_offset_t off,size_t popsize,vnode_t * frontvp,vnode_t * backvp,u_offset_t cpsize,cred_t * cr)16257c478bd9Sstevel@tonic-gate cachefs_populate(cnode_t *cp, u_offset_t off, size_t popsize, vnode_t *frontvp,
16267c478bd9Sstevel@tonic-gate     vnode_t *backvp, u_offset_t cpsize, cred_t *cr)
16277c478bd9Sstevel@tonic-gate {
16287c478bd9Sstevel@tonic-gate 	int error = 0;
16297c478bd9Sstevel@tonic-gate 	caddr_t addr;
16307c478bd9Sstevel@tonic-gate 	u_offset_t upto;
16317c478bd9Sstevel@tonic-gate 	uint_t size;
16327c478bd9Sstevel@tonic-gate 	u_offset_t from = off;
16337c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = C_TO_FSCACHE(cp)->fs_cache;
16347c478bd9Sstevel@tonic-gate 	ssize_t resid;
16357c478bd9Sstevel@tonic-gate 	struct fbuf *fbp;
16367c478bd9Sstevel@tonic-gate 	caddr_t buf = kmem_alloc(MAXBSIZE, KM_SLEEP);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
16397c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_VOPS)
16407c478bd9Sstevel@tonic-gate 		printf("cachefs_populate: ENTER cp %p off %lld\n",
16417c478bd9Sstevel@tonic-gate 		    (void *)cp, off);
16427c478bd9Sstevel@tonic-gate #endif
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	upto = MIN((off + popsize), cpsize);
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	while (from < upto) {
16477c478bd9Sstevel@tonic-gate 		u_offset_t blkoff = (from & (offset_t)MAXBMASK);
16487c478bd9Sstevel@tonic-gate 		uint_t n = from - blkoff;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 		size = upto - from;
16517c478bd9Sstevel@tonic-gate 		if (upto > (blkoff + MAXBSIZE))
16527c478bd9Sstevel@tonic-gate 			size = MAXBSIZE - n;
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 		error = fbread(backvp, (offset_t)blkoff, n + size,
16557c478bd9Sstevel@tonic-gate 		    S_OTHER, &fbp);
16567c478bd9Sstevel@tonic-gate 		if (CFS_TIMEOUT(C_TO_FSCACHE(cp), error))
16577c478bd9Sstevel@tonic-gate 			goto out;
16587c478bd9Sstevel@tonic-gate 		else if (error) {
16597c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
16607c478bd9Sstevel@tonic-gate 			CFS_DEBUG(CFSDEBUG_BACK)
16617c478bd9Sstevel@tonic-gate 				printf("cachefs_populate: fbread error %d\n",
16627c478bd9Sstevel@tonic-gate 				    error);
16637c478bd9Sstevel@tonic-gate #endif
16647c478bd9Sstevel@tonic-gate 			goto out;
16657c478bd9Sstevel@tonic-gate 		}
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 		addr = fbp->fb_addr;
16687c478bd9Sstevel@tonic-gate 		ASSERT(addr != NULL);
16697c478bd9Sstevel@tonic-gate 		ASSERT(n + size <= MAXBSIZE);
16707c478bd9Sstevel@tonic-gate 		bcopy(addr, buf, n + size);
16717c478bd9Sstevel@tonic-gate 		fbrelse(fbp, S_OTHER);
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 		if (n == 0 || cachefs_check_allocmap(cp, blkoff) == 0) {
16747c478bd9Sstevel@tonic-gate 			if (error = cachefs_allocblocks(cachep, 1,
16757c478bd9Sstevel@tonic-gate 			    cp->c_metadata.md_rltype))
16767c478bd9Sstevel@tonic-gate 				goto out;
16777c478bd9Sstevel@tonic-gate 			cp->c_metadata.md_frontblks++;
16787c478bd9Sstevel@tonic-gate 		}
16797c478bd9Sstevel@tonic-gate 		resid = 0;
16807c478bd9Sstevel@tonic-gate 		error = vn_rdwr(UIO_WRITE, frontvp, buf + n, size,
16817c478bd9Sstevel@tonic-gate 		    (offset_t)from, UIO_SYSSPACE, 0,
16827c478bd9Sstevel@tonic-gate 		    (rlim64_t)RLIM64_INFINITY, cr, &resid);
16837c478bd9Sstevel@tonic-gate 		if (error) {
16847c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
16857c478bd9Sstevel@tonic-gate 			CFS_DEBUG(CFSDEBUG_FRONT)
16867c478bd9Sstevel@tonic-gate 				printf("cachefs_populate: "
16877c478bd9Sstevel@tonic-gate 				    "Got error = %d from vn_rdwr\n", error);
16887c478bd9Sstevel@tonic-gate #endif
16897c478bd9Sstevel@tonic-gate 			goto out;
16907c478bd9Sstevel@tonic-gate 		}
16917c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
16927c478bd9Sstevel@tonic-gate 		if (resid)
16937c478bd9Sstevel@tonic-gate 			CFS_DEBUG(CFSDEBUG_FRONT)
16947c478bd9Sstevel@tonic-gate 				printf("cachefs_populate: non-zero resid %ld\n",
16957c478bd9Sstevel@tonic-gate 				    resid);
16967c478bd9Sstevel@tonic-gate #endif
16977c478bd9Sstevel@tonic-gate 		from += size;
16987c478bd9Sstevel@tonic-gate 	}
16997c478bd9Sstevel@tonic-gate 	(void) cachefs_update_allocmap(cp, off, upto - off);
17007c478bd9Sstevel@tonic-gate out:
17017c478bd9Sstevel@tonic-gate 	if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_POPULATE))
17027c478bd9Sstevel@tonic-gate 		cachefs_log_populate(cachep, error,
17037c478bd9Sstevel@tonic-gate 		    C_TO_FSCACHE(cp)->fs_cfsvfsp,
17047c478bd9Sstevel@tonic-gate 		    &cp->c_metadata.md_cookie, cp->c_id.cid_fileno, off,
17057c478bd9Sstevel@tonic-gate 		    popsize);
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
17087c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_VOPS)
17097c478bd9Sstevel@tonic-gate 		printf("cachefs_populate: EXIT cp %p error %d\n",
17107c478bd9Sstevel@tonic-gate 		    (void *)cp, error);
17117c478bd9Sstevel@tonic-gate #endif
17127c478bd9Sstevel@tonic-gate 	kmem_free(buf, MAXBSIZE);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	return (error);
17157c478bd9Sstevel@tonic-gate }
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate /*
17187c478bd9Sstevel@tonic-gate  * due to compiler error we shifted cnode to the last argument slot.
1719da6c28aaSamw  * occurred during large files project - XXX.
17207c478bd9Sstevel@tonic-gate  */
17217c478bd9Sstevel@tonic-gate void
cachefs_cluster_allocmap(u_offset_t off,u_offset_t * popoffp,size_t * popsizep,size_t size,struct cnode * cp)1722*d3d50737SRafael Vanoni cachefs_cluster_allocmap(u_offset_t off, u_offset_t *popoffp, size_t *popsizep,
1723*d3d50737SRafael Vanoni     size_t size, struct cnode *cp)
17247c478bd9Sstevel@tonic-gate {
17257c478bd9Sstevel@tonic-gate 	int i;
17267c478bd9Sstevel@tonic-gate 	u_offset_t lastoff = 0;
17277c478bd9Sstevel@tonic-gate 	u_offset_t forward_diff = 0;
17287c478bd9Sstevel@tonic-gate 	u_offset_t backward_diff = 0;
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	ASSERT(size <= C_TO_FSCACHE(cp)->fs_info.fi_popsize);
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
17337c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_SUBR)
17347c478bd9Sstevel@tonic-gate 		printf("cachefs_cluster_allocmap: off %llx, size %llx, "
17357c478bd9Sstevel@tonic-gate 		    "c_size %llx\n", off, size, (longlong_t)cp->c_size);
17367c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
17377c478bd9Sstevel@tonic-gate 	for (i = 0; i < cp->c_metadata.md_allocents; i++) {
17387c478bd9Sstevel@tonic-gate 		struct cachefs_allocmap *allocp =
17397c478bd9Sstevel@tonic-gate 		    cp->c_metadata.md_allocinfo + i;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 		if (allocp->am_start_off > off) {
17427c478bd9Sstevel@tonic-gate 			if ((off + size) > allocp->am_start_off) {
17437c478bd9Sstevel@tonic-gate 				forward_diff = allocp->am_start_off - off;
17447c478bd9Sstevel@tonic-gate 				backward_diff = size - forward_diff;
17457c478bd9Sstevel@tonic-gate 				if (backward_diff > off)
17467c478bd9Sstevel@tonic-gate 					backward_diff = off;
17477c478bd9Sstevel@tonic-gate 				if (lastoff > (off - backward_diff))
17487c478bd9Sstevel@tonic-gate 					backward_diff = off - lastoff;
17497c478bd9Sstevel@tonic-gate 			} else {
17507c478bd9Sstevel@tonic-gate 				forward_diff = size;
17517c478bd9Sstevel@tonic-gate 			}
17527c478bd9Sstevel@tonic-gate 			*popoffp = (off - backward_diff) & (offset_t)PAGEMASK;
17537c478bd9Sstevel@tonic-gate 			*popsizep = ((off + forward_diff) - *popoffp) &
17547c478bd9Sstevel@tonic-gate 			    (offset_t)PAGEMASK;
17557c478bd9Sstevel@tonic-gate 			return;
17567c478bd9Sstevel@tonic-gate 		} else {
17577c478bd9Sstevel@tonic-gate 			lastoff = allocp->am_start_off + allocp->am_size;
17587c478bd9Sstevel@tonic-gate 		}
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 	if ((lastoff + size) > off) {
17617c478bd9Sstevel@tonic-gate 		*popoffp = (lastoff & (offset_t)PAGEMASK);
17627c478bd9Sstevel@tonic-gate 	} else {
17637c478bd9Sstevel@tonic-gate 		*popoffp = off & (offset_t)PAGEMASK;
17647c478bd9Sstevel@tonic-gate 	}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	/*
17677c478bd9Sstevel@tonic-gate 	 * 64bit project: popsize is the chunk size used to populate the
17687c478bd9Sstevel@tonic-gate 	 * cache (default 64K). As such, 32 bit should suffice.
17697c478bd9Sstevel@tonic-gate 	 */
17707c478bd9Sstevel@tonic-gate 	if ((*popoffp + size) > cp->c_size)
17717c478bd9Sstevel@tonic-gate 		*popsizep = (cp->c_size - *popoffp + PAGEOFFSET) &
17727c478bd9Sstevel@tonic-gate 		    (offset_t)PAGEMASK;
17737c478bd9Sstevel@tonic-gate 	else if (size < PAGESIZE)
1774*d3d50737SRafael Vanoni 		*popsizep = (size + PAGEOFFSET) & (offset_t)PAGEMASK;
17757c478bd9Sstevel@tonic-gate 	else
17767c478bd9Sstevel@tonic-gate 		*popsizep = size & (offset_t)PAGEMASK;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
17797c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_SUBR)
17807c478bd9Sstevel@tonic-gate 		printf("cachefs_cluster_allocmap: popoff %llx, popsize %llx\n",
17817c478bd9Sstevel@tonic-gate 		    (u_longlong_t)(*popoffp), (u_longlong_t)(*popsizep));
17827c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
17837c478bd9Sstevel@tonic-gate }
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate /*
17867c478bd9Sstevel@tonic-gate  * "populate" a symlink in the cache
17877c478bd9Sstevel@tonic-gate  */
17887c478bd9Sstevel@tonic-gate int
cachefs_stuffsymlink(cnode_t * cp,caddr_t buf,int buflen)17897c478bd9Sstevel@tonic-gate cachefs_stuffsymlink(cnode_t *cp, caddr_t buf, int buflen)
17907c478bd9Sstevel@tonic-gate {
17917c478bd9Sstevel@tonic-gate 	int error = 0;
17927c478bd9Sstevel@tonic-gate 	struct fscache *fscp = C_TO_FSCACHE(cp);
17937c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
17947c478bd9Sstevel@tonic-gate 	struct cachefs_metadata *mdp = &cp->c_metadata;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&cp->c_rwlock));
17977c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cp->c_statelock));
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	if (CFS_ISFS_BACKFS_NFSV4(fscp))
18007c478bd9Sstevel@tonic-gate 		goto out;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	if (cp->c_flags & CN_NOCACHE)
18037c478bd9Sstevel@tonic-gate 		return (ENOENT);
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	cp->c_size = (u_offset_t)buflen;
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	/* if can create a fast sym link */
18087c478bd9Sstevel@tonic-gate 	if (buflen <= C_FSL_SIZE) {
18097c478bd9Sstevel@tonic-gate 		/* give up the front file resources */
18107c478bd9Sstevel@tonic-gate 		if (mdp->md_rlno) {
18117c478bd9Sstevel@tonic-gate 			cachefs_removefrontfile(mdp, &cp->c_id, cp->c_filegrp);
18127c478bd9Sstevel@tonic-gate 			cachefs_rlent_moveto(cachep, CACHEFS_RL_FREE,
18137c478bd9Sstevel@tonic-gate 			    mdp->md_rlno, 0);
18147c478bd9Sstevel@tonic-gate 			mdp->md_rlno = 0;
18157c478bd9Sstevel@tonic-gate 			mdp->md_rltype = CACHEFS_RL_NONE;
18167c478bd9Sstevel@tonic-gate 		}
18177c478bd9Sstevel@tonic-gate 		/* put sym link contents in allocinfo in metadata */
18187c478bd9Sstevel@tonic-gate 		bzero(mdp->md_allocinfo, C_FSL_SIZE);
18197c478bd9Sstevel@tonic-gate 		bcopy(buf, mdp->md_allocinfo, buflen);
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 		mdp->md_flags |= MD_FASTSYMLNK;
18227c478bd9Sstevel@tonic-gate 		cp->c_flags &= ~CN_NEED_FRONT_SYNC;
18237c478bd9Sstevel@tonic-gate 		cp->c_flags |= CN_UPDATED;
18247c478bd9Sstevel@tonic-gate 		goto out;
18257c478bd9Sstevel@tonic-gate 	}
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	/* else create a sym link in a front file */
18287c478bd9Sstevel@tonic-gate 	if (cp->c_frontvp == NULL)
18297c478bd9Sstevel@tonic-gate 		error = cachefs_getfrontfile(cp);
18307c478bd9Sstevel@tonic-gate 	if (error)
18317c478bd9Sstevel@tonic-gate 		goto out;
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	/* truncate front file */
18347c478bd9Sstevel@tonic-gate 	error = cachefs_frontfile_size(cp, 0);
18357c478bd9Sstevel@tonic-gate 	mdp->md_flags &= ~(MD_FASTSYMLNK | MD_POPULATED);
18367c478bd9Sstevel@tonic-gate 	if (error)
18377c478bd9Sstevel@tonic-gate 		goto out;
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	/* get space for the sym link */
18407c478bd9Sstevel@tonic-gate 	error = cachefs_allocblocks(cachep, 1, cp->c_metadata.md_rltype);
18417c478bd9Sstevel@tonic-gate 	if (error)
18427c478bd9Sstevel@tonic-gate 		goto out;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	/* write the sym link to the front file */
18457c478bd9Sstevel@tonic-gate 	error = vn_rdwr(UIO_WRITE, cp->c_frontvp, buf, buflen, 0,
18467c478bd9Sstevel@tonic-gate 	    UIO_SYSSPACE, 0, RLIM_INFINITY, kcred, NULL);
18477c478bd9Sstevel@tonic-gate 	if (error) {
18487c478bd9Sstevel@tonic-gate 		cachefs_freeblocks(cachep, 1, cp->c_metadata.md_rltype);
18497c478bd9Sstevel@tonic-gate 		goto out;
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_POPULATED;
18537c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_NEED_FRONT_SYNC;
18547c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED;
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate out:
18577c478bd9Sstevel@tonic-gate 	if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_CSYMLINK))
18587c478bd9Sstevel@tonic-gate 		cachefs_log_csymlink(cachep, error, fscp->fs_cfsvfsp,
18597c478bd9Sstevel@tonic-gate 		    &cp->c_metadata.md_cookie, cp->c_id.cid_fileno, buflen);
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	return (error);
18627c478bd9Sstevel@tonic-gate }
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate /*
18657c478bd9Sstevel@tonic-gate  * Reads the full contents of the symbolic link from the back file system.
18667c478bd9Sstevel@tonic-gate  * *bufp is set to a MAXPATHLEN buffer that must be freed when done
18677c478bd9Sstevel@tonic-gate  * *buflenp is the length of the link
18687c478bd9Sstevel@tonic-gate  */
18697c478bd9Sstevel@tonic-gate int
cachefs_readlink_back(cnode_t * cp,cred_t * cr,caddr_t * bufp,int * buflenp)18707c478bd9Sstevel@tonic-gate cachefs_readlink_back(cnode_t *cp, cred_t *cr, caddr_t *bufp, int *buflenp)
18717c478bd9Sstevel@tonic-gate {
18727c478bd9Sstevel@tonic-gate 	int error;
18737c478bd9Sstevel@tonic-gate 	struct uio uio;
18747c478bd9Sstevel@tonic-gate 	struct iovec iov;
18757c478bd9Sstevel@tonic-gate 	caddr_t buf;
18767c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(cp);
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cp->c_statelock));
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	*bufp = NULL;
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	/* get back vnode */
18837c478bd9Sstevel@tonic-gate 	if (cp->c_backvp == NULL) {
18847c478bd9Sstevel@tonic-gate 		error = cachefs_getbackvp(fscp, cp);
18857c478bd9Sstevel@tonic-gate 		if (error)
18867c478bd9Sstevel@tonic-gate 			return (error);
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	/* set up for the readlink */
18907c478bd9Sstevel@tonic-gate 	bzero(&uio, sizeof (struct uio));
18917c478bd9Sstevel@tonic-gate 	bzero(&iov, sizeof (struct iovec));
18927c478bd9Sstevel@tonic-gate 	buf = cachefs_kmem_alloc(MAXPATHLEN, KM_SLEEP);
18937c478bd9Sstevel@tonic-gate 	iov.iov_base = buf;
18947c478bd9Sstevel@tonic-gate 	iov.iov_len = MAXPATHLEN;
18957c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
18967c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
18977c478bd9Sstevel@tonic-gate 	uio.uio_resid = MAXPATHLEN;
18987c478bd9Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
18997c478bd9Sstevel@tonic-gate 	uio.uio_loffset = 0;
19007c478bd9Sstevel@tonic-gate 	uio.uio_fmode = 0;
19017c478bd9Sstevel@tonic-gate 	uio.uio_extflg = UIO_COPY_CACHED;
19027c478bd9Sstevel@tonic-gate 	uio.uio_llimit = MAXOFFSET_T;
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	/* get the link data */
19057c478bd9Sstevel@tonic-gate 	CFS_DPRINT_BACKFS_NFSV4(fscp,
19067c478bd9Sstevel@tonic-gate 	    ("cachefs_readlink (nfsv4): cnode %p, backvp %p\n",
19077c478bd9Sstevel@tonic-gate 	    cp, cp->c_backvp));
1908da6c28aaSamw 	error = VOP_READLINK(cp->c_backvp, &uio, cr, NULL);
19097c478bd9Sstevel@tonic-gate 	if (error) {
19107c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(buf, MAXPATHLEN);
19117c478bd9Sstevel@tonic-gate 	} else {
19127c478bd9Sstevel@tonic-gate 		*bufp = buf;
19137c478bd9Sstevel@tonic-gate 		/*LINTED alignment okay*/
19147c478bd9Sstevel@tonic-gate 		*buflenp = MAXPATHLEN - (int)uio.uio_resid;
19157c478bd9Sstevel@tonic-gate 	}
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	return (error);
19187c478bd9Sstevel@tonic-gate }
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate int
cachefs_getbackvp(struct fscache * fscp,struct cnode * cp)19217c478bd9Sstevel@tonic-gate cachefs_getbackvp(struct fscache *fscp, struct cnode *cp)
19227c478bd9Sstevel@tonic-gate {
19237c478bd9Sstevel@tonic-gate 	int error = 0;
19247c478bd9Sstevel@tonic-gate 	int flag;
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
19277c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_CHEAT | CFSDEBUG_BACK)
19287c478bd9Sstevel@tonic-gate 		printf("cachefs_getbackvp: ENTER fscp %p cp %p\n",
19297c478bd9Sstevel@tonic-gate 		    (void *)fscp, (void *)cp);
19307c478bd9Sstevel@tonic-gate #endif
19317c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);
19327c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cp->c_statelock));
19337c478bd9Sstevel@tonic-gate 	ASSERT(cp->c_backvp == NULL);
19347c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	/*
19377c478bd9Sstevel@tonic-gate 	 * If destroy is set then the last link to a file has been
19387c478bd9Sstevel@tonic-gate 	 * removed.  Oddly enough NFS will still return a vnode
19397c478bd9Sstevel@tonic-gate 	 * for the file if the timeout has not expired.
19407c478bd9Sstevel@tonic-gate 	 * This causes headaches for cachefs_push because the
19417c478bd9Sstevel@tonic-gate 	 * vnode is really stale.
19427c478bd9Sstevel@tonic-gate 	 * So we just short circuit the problem here.
19437c478bd9Sstevel@tonic-gate 	 */
19447c478bd9Sstevel@tonic-gate 	if (cp->c_flags & CN_DESTROY)
19457c478bd9Sstevel@tonic-gate 		return (ESTALE);
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	ASSERT(fscp->fs_backvfsp);
19487c478bd9Sstevel@tonic-gate 	if (fscp->fs_backvfsp == NULL)
19497c478bd9Sstevel@tonic-gate 		return (ETIMEDOUT);
19507c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &cp->c_backvp,
19517c478bd9Sstevel@tonic-gate 	    (struct fid *)&cp->c_cookie);
19527c478bd9Sstevel@tonic-gate 	if (cp->c_backvp && cp->c_cred &&
19537c478bd9Sstevel@tonic-gate 	    ((cp->c_flags & CN_NEEDOPEN) || (cp->c_attr.va_type == VREG))) {
19547c478bd9Sstevel@tonic-gate 		/*
19557c478bd9Sstevel@tonic-gate 		 * XXX bob: really should pass in the correct flag,
19567c478bd9Sstevel@tonic-gate 		 * fortunately nobody pays attention to it
19577c478bd9Sstevel@tonic-gate 		 */
19587c478bd9Sstevel@tonic-gate 		flag = 0;
19597c478bd9Sstevel@tonic-gate 		/*
19607c478bd9Sstevel@tonic-gate 		 * If NEEDOOPEN is set, then this file was opened VOP_OPEN'd
19617c478bd9Sstevel@tonic-gate 		 * but the backvp was not.  So, for the sake of the vnode
19627c478bd9Sstevel@tonic-gate 		 * open counts used by delegation, we need to OPEN the backvp
19637c478bd9Sstevel@tonic-gate 		 * with the same flags that were used for this cnode.  That way
19647c478bd9Sstevel@tonic-gate 		 * when the file is VOP_CLOSE'd the counts won't go negative.
19657c478bd9Sstevel@tonic-gate 		 */
19667c478bd9Sstevel@tonic-gate 		if (cp->c_flags & CN_NEEDOPEN) {
19677c478bd9Sstevel@tonic-gate 			cp->c_flags &= ~CN_NEEDOPEN;
19687c478bd9Sstevel@tonic-gate 			if (cp->c_rdcnt > 0) {
19697c478bd9Sstevel@tonic-gate 				cp->c_rdcnt--;
19707c478bd9Sstevel@tonic-gate 				flag |= FREAD;
19717c478bd9Sstevel@tonic-gate 			}
19727c478bd9Sstevel@tonic-gate 			if (cp->c_wrcnt > 0) {
19737c478bd9Sstevel@tonic-gate 				cp->c_wrcnt--;
19747c478bd9Sstevel@tonic-gate 				flag |= FWRITE;
19757c478bd9Sstevel@tonic-gate 			}
19767c478bd9Sstevel@tonic-gate 		}
1977da6c28aaSamw 		error = VOP_OPEN(&cp->c_backvp, flag, cp->c_cred, NULL);
19787c478bd9Sstevel@tonic-gate 		if (error) {
19797c478bd9Sstevel@tonic-gate 			VN_RELE(cp->c_backvp);
19807c478bd9Sstevel@tonic-gate 			cp->c_backvp = NULL;
19817c478bd9Sstevel@tonic-gate 		}
19827c478bd9Sstevel@tonic-gate 	}
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
19857c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_GENERAL | CFSDEBUG_BACK) {
19867c478bd9Sstevel@tonic-gate 		if (error || cp->c_backvp == NULL) {
19877c478bd9Sstevel@tonic-gate 			printf("Stale cookie cp %p fileno %llu type %d \n",
19887c478bd9Sstevel@tonic-gate 			    (void *)cp, (u_longlong_t)cp->c_id.cid_fileno,
19897c478bd9Sstevel@tonic-gate 			    CTOV(cp)->v_type);
19907c478bd9Sstevel@tonic-gate 		}
19917c478bd9Sstevel@tonic-gate 	}
19927c478bd9Sstevel@tonic-gate #endif
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
19957c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_CHEAT | CFSDEBUG_BACK)
19967c478bd9Sstevel@tonic-gate 		printf("cachefs_getbackvp: EXIT error = %d\n", error);
19977c478bd9Sstevel@tonic-gate #endif
19987c478bd9Sstevel@tonic-gate 	return (error);
19997c478bd9Sstevel@tonic-gate }
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate int
cachefs_getcookie(vnode_t * vp,struct fid * cookiep,struct vattr * attrp,cred_t * cr,uint32_t valid_fid)20027c478bd9Sstevel@tonic-gate cachefs_getcookie(
20037c478bd9Sstevel@tonic-gate 	vnode_t *vp,
20047c478bd9Sstevel@tonic-gate 	struct fid *cookiep,
20057c478bd9Sstevel@tonic-gate 	struct vattr *attrp,
20067c478bd9Sstevel@tonic-gate 	cred_t *cr,
20077c478bd9Sstevel@tonic-gate 	uint32_t valid_fid)
20087c478bd9Sstevel@tonic-gate {
20097c478bd9Sstevel@tonic-gate 	int error = 0;
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
20127c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_CHEAT)
20137c478bd9Sstevel@tonic-gate 		printf("cachefs_getcookie: ENTER vp %p\n", (void *)vp);
20147c478bd9Sstevel@tonic-gate #endif
20157c478bd9Sstevel@tonic-gate 	/*
20167c478bd9Sstevel@tonic-gate 	 * Get the FID only if the caller has indicated it is valid,
20177c478bd9Sstevel@tonic-gate 	 * otherwise, zero the cookie.
20187c478bd9Sstevel@tonic-gate 	 */
20197c478bd9Sstevel@tonic-gate 	if (valid_fid) {
20207c478bd9Sstevel@tonic-gate 		/*
20217c478bd9Sstevel@tonic-gate 		 * This assumes that the cookie is a full size fid, if we go to
20227c478bd9Sstevel@tonic-gate 		 * variable length fids we will need to change this.
20237c478bd9Sstevel@tonic-gate 		 */
20247c478bd9Sstevel@tonic-gate 		cookiep->fid_len = MAXFIDSZ;
2025da6c28aaSamw 		error = VOP_FID(vp, cookiep, NULL);
20267c478bd9Sstevel@tonic-gate 	} else {
20277c478bd9Sstevel@tonic-gate 		bzero(cookiep, sizeof (*cookiep));
20287c478bd9Sstevel@tonic-gate 	}
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	if (!error) {
20317c478bd9Sstevel@tonic-gate 		if (attrp) {
20327c478bd9Sstevel@tonic-gate 			ASSERT(attrp != NULL);
20337c478bd9Sstevel@tonic-gate 			attrp->va_mask = AT_ALL;
2034da6c28aaSamw 			error = VOP_GETATTR(vp, attrp, 0, cr, NULL);
20357c478bd9Sstevel@tonic-gate 		}
20367c478bd9Sstevel@tonic-gate 	} else {
20377c478bd9Sstevel@tonic-gate 		if (error == ENOSPC) {
20387c478bd9Sstevel@tonic-gate 			/*
20397c478bd9Sstevel@tonic-gate 			 * This is an indication that the underlying filesystem
20407c478bd9Sstevel@tonic-gate 			 * needs a bigger fid.  For now just map to EINVAL.
20417c478bd9Sstevel@tonic-gate 			 */
20427c478bd9Sstevel@tonic-gate 			error = EINVAL;
20437c478bd9Sstevel@tonic-gate 		}
20447c478bd9Sstevel@tonic-gate 	}
20457c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
20467c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_CHEAT)
20477c478bd9Sstevel@tonic-gate 		printf("cachefs_getcookie: EXIT error = %d\n", error);
20487c478bd9Sstevel@tonic-gate #endif
20497c478bd9Sstevel@tonic-gate 	return (error);
20507c478bd9Sstevel@tonic-gate }
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate void
cachefs_workq_init(struct cachefs_workq * qp)20537c478bd9Sstevel@tonic-gate cachefs_workq_init(struct cachefs_workq *qp)
20547c478bd9Sstevel@tonic-gate {
20557c478bd9Sstevel@tonic-gate 	qp->wq_head = qp->wq_tail = NULL;
20567c478bd9Sstevel@tonic-gate 	qp->wq_length =
20577c478bd9Sstevel@tonic-gate 	    qp->wq_thread_count =
20587c478bd9Sstevel@tonic-gate 	    qp->wq_max_len =
20597c478bd9Sstevel@tonic-gate 	    qp->wq_halt_request = 0;
20607c478bd9Sstevel@tonic-gate 	qp->wq_keepone = 0;
20617c478bd9Sstevel@tonic-gate 	cv_init(&qp->wq_req_cv, NULL, CV_DEFAULT, NULL);
20627c478bd9Sstevel@tonic-gate 	cv_init(&qp->wq_halt_cv, NULL, CV_DEFAULT, NULL);
20637c478bd9Sstevel@tonic-gate 	mutex_init(&qp->wq_queue_lock, NULL, MUTEX_DEFAULT, NULL);
20647c478bd9Sstevel@tonic-gate }
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate /*
20677c478bd9Sstevel@tonic-gate  * return non-zero if it's `okay' to queue more requests (policy)
20687c478bd9Sstevel@tonic-gate  */
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate static int cachefs_async_max = 512;
20717c478bd9Sstevel@tonic-gate static int cachefs_async_count = 0;
20727c478bd9Sstevel@tonic-gate kmutex_t cachefs_async_lock;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate int
cachefs_async_okay(void)20757c478bd9Sstevel@tonic-gate cachefs_async_okay(void)
20767c478bd9Sstevel@tonic-gate {
20777c478bd9Sstevel@tonic-gate 	/*
20787c478bd9Sstevel@tonic-gate 	 * a value of -1 for max means to ignore freemem
20797c478bd9Sstevel@tonic-gate 	 */
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	if (cachefs_async_max == -1)
20827c478bd9Sstevel@tonic-gate 		return (1);
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	if (freemem < minfree)
20857c478bd9Sstevel@tonic-gate 		return (0);
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	/*
20887c478bd9Sstevel@tonic-gate 	 * a value of 0 for max means no arbitrary limit (only `freemen')
20897c478bd9Sstevel@tonic-gate 	 */
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	if (cachefs_async_max == 0)
20927c478bd9Sstevel@tonic-gate 		return (1);
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	ASSERT(cachefs_async_max > 0);
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	/*
20977c478bd9Sstevel@tonic-gate 	 * check the global count against the max.
20987c478bd9Sstevel@tonic-gate 	 *
20997c478bd9Sstevel@tonic-gate 	 * we don't need to grab cachefs_async_lock -- we're just
21007c478bd9Sstevel@tonic-gate 	 * looking, and a little bit of `fuzz' is okay.
21017c478bd9Sstevel@tonic-gate 	 */
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	if (cachefs_async_count >= cachefs_async_max)
21047c478bd9Sstevel@tonic-gate 		return (0);
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	return (1);
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate void
cachefs_async_start(struct cachefs_workq * qp)21107c478bd9Sstevel@tonic-gate cachefs_async_start(struct cachefs_workq *qp)
21117c478bd9Sstevel@tonic-gate {
21127c478bd9Sstevel@tonic-gate 	struct cachefs_req *rp;
21137c478bd9Sstevel@tonic-gate 	int left;
21147c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &qp->wq_queue_lock, callb_generic_cpr, "cas");
21177c478bd9Sstevel@tonic-gate 	mutex_enter(&qp->wq_queue_lock);
21187c478bd9Sstevel@tonic-gate 	left = 1;
21197c478bd9Sstevel@tonic-gate 	for (;;) {
21207c478bd9Sstevel@tonic-gate 		/* if there are no pending requests */
21217c478bd9Sstevel@tonic-gate 		if ((qp->wq_head == NULL) && (qp->wq_logwork == 0)) {
21227c478bd9Sstevel@tonic-gate 			/* see if thread should exit */
21237c478bd9Sstevel@tonic-gate 			if (qp->wq_halt_request || (left == -1)) {
21247c478bd9Sstevel@tonic-gate 				if ((qp->wq_thread_count > 1) ||
21257c478bd9Sstevel@tonic-gate 				    (qp->wq_keepone == 0))
21267c478bd9Sstevel@tonic-gate 					break;
21277c478bd9Sstevel@tonic-gate 			}
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 			/* wake up thread in async_halt if necessary */
21307c478bd9Sstevel@tonic-gate 			if (qp->wq_halt_request)
21317c478bd9Sstevel@tonic-gate 				cv_broadcast(&qp->wq_halt_cv);
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
21347c478bd9Sstevel@tonic-gate 			/* sleep until there is something to do */
2135*d3d50737SRafael Vanoni 			left = cv_reltimedwait(&qp->wq_req_cv,
2136*d3d50737SRafael Vanoni 			    &qp->wq_queue_lock, CFS_ASYNC_TIMEOUT,
2137*d3d50737SRafael Vanoni 			    TR_CLOCK_TICK);
2138*d3d50737SRafael Vanoni 			CALLB_CPR_SAFE_END(&cprinfo, &qp->wq_queue_lock);
21397c478bd9Sstevel@tonic-gate 			if ((qp->wq_head == NULL) && (qp->wq_logwork == 0))
21407c478bd9Sstevel@tonic-gate 				continue;
21417c478bd9Sstevel@tonic-gate 		}
21427c478bd9Sstevel@tonic-gate 		left = 1;
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 		if (qp->wq_logwork) {
21457c478bd9Sstevel@tonic-gate 			qp->wq_logwork = 0;
21467c478bd9Sstevel@tonic-gate 			mutex_exit(&qp->wq_queue_lock);
21477c478bd9Sstevel@tonic-gate 			cachefs_log_process_queue(qp->wq_cachep, 1);
21487c478bd9Sstevel@tonic-gate 			mutex_enter(&qp->wq_queue_lock);
21497c478bd9Sstevel@tonic-gate 			continue;
21507c478bd9Sstevel@tonic-gate 		}
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 		/* remove request from the list */
21537c478bd9Sstevel@tonic-gate 		rp = qp->wq_head;
21547c478bd9Sstevel@tonic-gate 		qp->wq_head = rp->cfs_next;
21557c478bd9Sstevel@tonic-gate 		if (rp->cfs_next == NULL)
21567c478bd9Sstevel@tonic-gate 			qp->wq_tail = NULL;
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 		/* do the request */
21597c478bd9Sstevel@tonic-gate 		mutex_exit(&qp->wq_queue_lock);
21607c478bd9Sstevel@tonic-gate 		cachefs_do_req(rp);
21617c478bd9Sstevel@tonic-gate 		mutex_enter(&qp->wq_queue_lock);
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 		/* decrement count of requests */
21647c478bd9Sstevel@tonic-gate 		qp->wq_length--;
21657c478bd9Sstevel@tonic-gate 		mutex_enter(&cachefs_async_lock);
21667c478bd9Sstevel@tonic-gate 		--cachefs_async_count;
21677c478bd9Sstevel@tonic-gate 		mutex_exit(&cachefs_async_lock);
21687c478bd9Sstevel@tonic-gate 	}
21697c478bd9Sstevel@tonic-gate 	ASSERT(qp->wq_head == NULL);
21707c478bd9Sstevel@tonic-gate 	qp->wq_thread_count--;
21717c478bd9Sstevel@tonic-gate 	if (qp->wq_halt_request && qp->wq_thread_count == 0)
21727c478bd9Sstevel@tonic-gate 		cv_broadcast(&qp->wq_halt_cv);
21737c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
21747c478bd9Sstevel@tonic-gate 	thread_exit();
21757c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
21767c478bd9Sstevel@tonic-gate }
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate /*
21797c478bd9Sstevel@tonic-gate  * attempt to halt all the async threads associated with a given workq
21807c478bd9Sstevel@tonic-gate  */
21817c478bd9Sstevel@tonic-gate int
cachefs_async_halt(struct cachefs_workq * qp,int force)21827c478bd9Sstevel@tonic-gate cachefs_async_halt(struct cachefs_workq *qp, int force)
21837c478bd9Sstevel@tonic-gate {
21847c478bd9Sstevel@tonic-gate 	int error = 0;
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	mutex_enter(&qp->wq_queue_lock);
21877c478bd9Sstevel@tonic-gate 	if (force)
21887c478bd9Sstevel@tonic-gate 		qp->wq_keepone = 0;
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	if (qp->wq_thread_count > 0) {
21917c478bd9Sstevel@tonic-gate 		qp->wq_halt_request++;
21927c478bd9Sstevel@tonic-gate 		cv_broadcast(&qp->wq_req_cv);
2193*d3d50737SRafael Vanoni 		(void) cv_reltimedwait(&qp->wq_halt_cv,
2194*d3d50737SRafael Vanoni 		    &qp->wq_queue_lock, (60 * hz), TR_CLOCK_TICK);
21957c478bd9Sstevel@tonic-gate 		qp->wq_halt_request--;
21967c478bd9Sstevel@tonic-gate 		if (qp->wq_thread_count > 0) {
21977c478bd9Sstevel@tonic-gate 			if ((qp->wq_thread_count == 1) &&
21987c478bd9Sstevel@tonic-gate 			    (qp->wq_length == 0) && qp->wq_keepone)
21997c478bd9Sstevel@tonic-gate 				error = EAGAIN;
22007c478bd9Sstevel@tonic-gate 			else
22017c478bd9Sstevel@tonic-gate 				error = EBUSY;
22027c478bd9Sstevel@tonic-gate 		} else {
22037c478bd9Sstevel@tonic-gate 			ASSERT(qp->wq_length == 0 && qp->wq_head == NULL);
22047c478bd9Sstevel@tonic-gate 		}
22057c478bd9Sstevel@tonic-gate 	}
22067c478bd9Sstevel@tonic-gate 	mutex_exit(&qp->wq_queue_lock);
22077c478bd9Sstevel@tonic-gate 	return (error);
22087c478bd9Sstevel@tonic-gate }
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate void
cachefs_addqueue(struct cachefs_req * rp,struct cachefs_workq * qp)22117c478bd9Sstevel@tonic-gate cachefs_addqueue(struct cachefs_req *rp, struct cachefs_workq *qp)
22127c478bd9Sstevel@tonic-gate {
22137c478bd9Sstevel@tonic-gate 	mutex_enter(&qp->wq_queue_lock);
22147c478bd9Sstevel@tonic-gate 	if (qp->wq_thread_count < cachefs_max_threads) {
22157c478bd9Sstevel@tonic-gate 		if (qp->wq_thread_count == 0 ||
22167c478bd9Sstevel@tonic-gate 		    (qp->wq_length >= (qp->wq_thread_count * 2))) {
22177c478bd9Sstevel@tonic-gate 			(void) thread_create(NULL, 0, cachefs_async_start,
22187c478bd9Sstevel@tonic-gate 			    qp, 0, &p0, TS_RUN, minclsyspri);
22197c478bd9Sstevel@tonic-gate 			qp->wq_thread_count++;
22207c478bd9Sstevel@tonic-gate 		}
22217c478bd9Sstevel@tonic-gate 	}
22227c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->cfs_req_lock);
22237c478bd9Sstevel@tonic-gate 	if (qp->wq_tail)
22247c478bd9Sstevel@tonic-gate 		qp->wq_tail->cfs_next = rp;
22257c478bd9Sstevel@tonic-gate 	else
22267c478bd9Sstevel@tonic-gate 		qp->wq_head = rp;
22277c478bd9Sstevel@tonic-gate 	qp->wq_tail = rp;
22287c478bd9Sstevel@tonic-gate 	rp->cfs_next = NULL;
22297c478bd9Sstevel@tonic-gate 	qp->wq_length++;
22307c478bd9Sstevel@tonic-gate 	if (qp->wq_length > qp->wq_max_len)
22317c478bd9Sstevel@tonic-gate 		qp->wq_max_len = qp->wq_length;
22327c478bd9Sstevel@tonic-gate 	mutex_enter(&cachefs_async_lock);
22337c478bd9Sstevel@tonic-gate 	++cachefs_async_count;
22347c478bd9Sstevel@tonic-gate 	mutex_exit(&cachefs_async_lock);
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	cv_signal(&qp->wq_req_cv);
22377c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->cfs_req_lock);
22387c478bd9Sstevel@tonic-gate 	mutex_exit(&qp->wq_queue_lock);
22397c478bd9Sstevel@tonic-gate }
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate void
cachefs_async_putpage(struct cachefs_putpage_req * prp,cred_t * cr)22427c478bd9Sstevel@tonic-gate cachefs_async_putpage(struct cachefs_putpage_req *prp, cred_t *cr)
22437c478bd9Sstevel@tonic-gate {
22447c478bd9Sstevel@tonic-gate 	struct cnode *cp = VTOC(prp->cp_vp);
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(C_TO_FSCACHE(cp)) == 0);
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	(void) VOP_PUTPAGE(prp->cp_vp, prp->cp_off, prp->cp_len,
2249da6c28aaSamw 	    prp->cp_flags, cr, NULL);
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_iomutex);
22527c478bd9Sstevel@tonic-gate 	if (--cp->c_nio == 0)
22537c478bd9Sstevel@tonic-gate 		cv_broadcast(&cp->c_iocv);
22547c478bd9Sstevel@tonic-gate 	if (prp->cp_off == 0 && prp->cp_len == 0 &&
22557c478bd9Sstevel@tonic-gate 	    (cp->c_ioflags & CIO_PUTPAGES)) {
22567c478bd9Sstevel@tonic-gate 		cp->c_ioflags &= ~CIO_PUTPAGES;
22577c478bd9Sstevel@tonic-gate 	}
22587c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->c_iomutex);
22597c478bd9Sstevel@tonic-gate }
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate void
cachefs_async_populate(struct cachefs_populate_req * pop,cred_t * cr)22627c478bd9Sstevel@tonic-gate cachefs_async_populate(struct cachefs_populate_req *pop, cred_t *cr)
22637c478bd9Sstevel@tonic-gate {
22647c478bd9Sstevel@tonic-gate 	struct cnode *cp = VTOC(pop->cpop_vp);
22657c478bd9Sstevel@tonic-gate 	struct fscache *fscp = C_TO_FSCACHE(cp);
22667c478bd9Sstevel@tonic-gate 	struct filegrp *fgp = cp->c_filegrp;
22677c478bd9Sstevel@tonic-gate 	int error = 0; /* not returned -- used as a place-holder */
22687c478bd9Sstevel@tonic-gate 	vnode_t *frontvp = NULL, *backvp = NULL;
22697c478bd9Sstevel@tonic-gate 	int havelock = 0;
22707c478bd9Sstevel@tonic-gate 	vattr_t va;
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 	if (((cp->c_filegrp->fg_flags & CFS_FG_WRITE) == 0) ||
22757c478bd9Sstevel@tonic-gate 	    (fscp->fs_cdconnected != CFS_CD_CONNECTED)) {
22767c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->c_statelock);
22777c478bd9Sstevel@tonic-gate 		cp->c_flags &= ~CN_ASYNC_POPULATE;
22787c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
22797c478bd9Sstevel@tonic-gate 		return; /* goto out */
22807c478bd9Sstevel@tonic-gate 	}
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 	error = cachefs_cd_access(fscp, 0, 0);
22837c478bd9Sstevel@tonic-gate 	if (error) {
22847c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
22857c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_ASYNCPOP)
22867c478bd9Sstevel@tonic-gate 			printf("async_pop: cd_access: err %d con %d\n",
22877c478bd9Sstevel@tonic-gate 			    error, fscp->fs_cdconnected);
22887c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
22897c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->c_statelock);
22907c478bd9Sstevel@tonic-gate 		cp->c_flags &= ~CN_ASYNC_POPULATE;
22917c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
22927c478bd9Sstevel@tonic-gate 		return; /* goto out */
22937c478bd9Sstevel@tonic-gate 	}
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 	/*
22967c478bd9Sstevel@tonic-gate 	 * grab the statelock for some minimal things
22977c478bd9Sstevel@tonic-gate 	 */
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 	rw_enter(&cp->c_rwlock, RW_WRITER);
23007c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_statelock);
23017c478bd9Sstevel@tonic-gate 	havelock = 1;
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	if ((cp->c_flags & CN_ASYNC_POPULATE) == 0)
23047c478bd9Sstevel@tonic-gate 		goto out;
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	/* there can be only one */
23077c478bd9Sstevel@tonic-gate 	ASSERT((cp->c_flags & CN_ASYNC_POP_WORKING) == 0);
23087c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_ASYNC_POP_WORKING;
23097c478bd9Sstevel@tonic-gate 	cp->c_popthrp = curthread;
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	if (cp->c_metadata.md_flags & MD_POPULATED)
23127c478bd9Sstevel@tonic-gate 		goto out;
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	if (cp->c_flags & CN_NOCACHE) {
23157c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
23167c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_ASYNCPOP)
23177c478bd9Sstevel@tonic-gate 			printf("cachefs_async_populate: nocache bit on\n");
23187c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
23197c478bd9Sstevel@tonic-gate 		error = EINVAL;
23207c478bd9Sstevel@tonic-gate 		goto out;
23217c478bd9Sstevel@tonic-gate 	}
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 	if (cp->c_frontvp == NULL) {
23247c478bd9Sstevel@tonic-gate 		if ((cp->c_metadata.md_flags & MD_FILE) == 0) {
23257c478bd9Sstevel@tonic-gate 			struct cfs_cid cid = cp->c_id;
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->c_statelock);
23287c478bd9Sstevel@tonic-gate 			havelock = 0;
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 			/*
23317c478bd9Sstevel@tonic-gate 			 * if frontfile doesn't exist, drop the lock
23327c478bd9Sstevel@tonic-gate 			 * to do some of the file creation stuff.
23337c478bd9Sstevel@tonic-gate 			 */
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 			if (fgp->fg_flags & CFS_FG_ALLOC_ATTR) {
23367c478bd9Sstevel@tonic-gate 				error = filegrp_allocattr(fgp);
23377c478bd9Sstevel@tonic-gate 				if (error != 0)
23387c478bd9Sstevel@tonic-gate 					goto out;
23397c478bd9Sstevel@tonic-gate 			}
23407c478bd9Sstevel@tonic-gate 			if (fgp->fg_flags & CFS_FG_ALLOC_FILE) {
23417c478bd9Sstevel@tonic-gate 				mutex_enter(&fgp->fg_mutex);
23427c478bd9Sstevel@tonic-gate 				if (fgp->fg_flags & CFS_FG_ALLOC_FILE) {
23437c478bd9Sstevel@tonic-gate 					if (fgp->fg_header->ach_nffs == 0)
23447c478bd9Sstevel@tonic-gate 						error = filegrpdir_create(fgp);
23457c478bd9Sstevel@tonic-gate 					else
23467c478bd9Sstevel@tonic-gate 						error = filegrpdir_find(fgp);
23477c478bd9Sstevel@tonic-gate 					if (error != 0) {
23487c478bd9Sstevel@tonic-gate 						mutex_exit(&fgp->fg_mutex);
23497c478bd9Sstevel@tonic-gate 						goto out;
23507c478bd9Sstevel@tonic-gate 					}
23517c478bd9Sstevel@tonic-gate 				}
23527c478bd9Sstevel@tonic-gate 				mutex_exit(&fgp->fg_mutex);
23537c478bd9Sstevel@tonic-gate 			}
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 			if (fgp->fg_dirvp != NULL) {
23567c478bd9Sstevel@tonic-gate 				char name[CFS_FRONTFILE_NAME_SIZE];
23577c478bd9Sstevel@tonic-gate 				struct vattr *attrp;
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 				attrp = cachefs_kmem_zalloc(
23607c478bd9Sstevel@tonic-gate 				    sizeof (struct vattr), KM_SLEEP);
23617c478bd9Sstevel@tonic-gate 				attrp->va_mode = S_IFREG | 0666;
23627c478bd9Sstevel@tonic-gate 				attrp->va_uid = 0;
23637c478bd9Sstevel@tonic-gate 				attrp->va_gid = 0;
23647c478bd9Sstevel@tonic-gate 				attrp->va_type = VREG;
23657c478bd9Sstevel@tonic-gate 				attrp->va_size = 0;
23667c478bd9Sstevel@tonic-gate 				attrp->va_mask =
23677c478bd9Sstevel@tonic-gate 				    AT_SIZE | AT_TYPE | AT_MODE |
23687c478bd9Sstevel@tonic-gate 				    AT_UID | AT_GID;
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 				make_ascii_name(&cid, name);
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 				(void) VOP_CREATE(fgp->fg_dirvp, name, attrp,
2373da6c28aaSamw 				    EXCL, 0666, &frontvp, kcred, 0, NULL, NULL);
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 				cachefs_kmem_free(attrp,
23767c478bd9Sstevel@tonic-gate 				    sizeof (struct vattr));
23777c478bd9Sstevel@tonic-gate 			}
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 			mutex_enter(&cp->c_statelock);
23807c478bd9Sstevel@tonic-gate 			havelock = 1;
23817c478bd9Sstevel@tonic-gate 		}
23827c478bd9Sstevel@tonic-gate 		error = cachefs_getfrontfile(cp);
23837c478bd9Sstevel@tonic-gate 		ASSERT((error != 0) ||
23847c478bd9Sstevel@tonic-gate 		    (frontvp == NULL) ||
23857c478bd9Sstevel@tonic-gate 		    (frontvp == cp->c_frontvp));
23867c478bd9Sstevel@tonic-gate 	}
23877c478bd9Sstevel@tonic-gate 	if ((error != 0) || (cp->c_frontvp == NULL))
23887c478bd9Sstevel@tonic-gate 		goto out;
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	if (frontvp != NULL)
23917c478bd9Sstevel@tonic-gate 		VN_RELE(frontvp);
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 	frontvp = cp->c_frontvp;
23947c478bd9Sstevel@tonic-gate 	VN_HOLD(frontvp);
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	if (cp->c_backvp == NULL) {
23977c478bd9Sstevel@tonic-gate 		error = cachefs_getbackvp(fscp, cp);
23987c478bd9Sstevel@tonic-gate 		if ((error != 0) || (cp->c_backvp == NULL))
23997c478bd9Sstevel@tonic-gate 			goto out;
24007c478bd9Sstevel@tonic-gate 	}
24017c478bd9Sstevel@tonic-gate 	backvp = cp->c_backvp;
24027c478bd9Sstevel@tonic-gate 	VN_HOLD(backvp);
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	switch (pop->cpop_vp->v_type) {
24057c478bd9Sstevel@tonic-gate 	case VREG:
24067c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
24077c478bd9Sstevel@tonic-gate 		havelock = 0;
24087c478bd9Sstevel@tonic-gate 		error = cachefs_async_populate_reg(pop, cr, backvp, frontvp);
24097c478bd9Sstevel@tonic-gate 		break;
24107c478bd9Sstevel@tonic-gate 	case VDIR:
24117c478bd9Sstevel@tonic-gate 		error = cachefs_async_populate_dir(pop, cr, backvp, frontvp);
24127c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
24137c478bd9Sstevel@tonic-gate 		havelock = 0;
24147c478bd9Sstevel@tonic-gate 		break;
24157c478bd9Sstevel@tonic-gate 	default:
24167c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
24177c478bd9Sstevel@tonic-gate 		printf("cachefs_async_populate: warning: vnode type = %d\n",
24187c478bd9Sstevel@tonic-gate 		    pop->cpop_vp->v_type);
24197c478bd9Sstevel@tonic-gate 		ASSERT(0);
24207c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
24217c478bd9Sstevel@tonic-gate 		error = EINVAL;
24227c478bd9Sstevel@tonic-gate 		break;
24237c478bd9Sstevel@tonic-gate 	}
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	if (error != 0)
24267c478bd9Sstevel@tonic-gate 		goto out;
24277c478bd9Sstevel@tonic-gate 
2428da6c28aaSamw 	error = VOP_FSYNC(frontvp, FSYNC, cr, NULL);
24297c478bd9Sstevel@tonic-gate 	if (error != 0) {
24307c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
24317c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_ASYNCPOP)
24327c478bd9Sstevel@tonic-gate 			printf("cachefs_async_populate: fsync\n");
24337c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
24347c478bd9Sstevel@tonic-gate 		goto out;
24357c478bd9Sstevel@tonic-gate 	}
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	/* grab the lock and finish up */
24387c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_statelock);
24397c478bd9Sstevel@tonic-gate 	havelock = 1;
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	/* if went nocache while lock was dropped, get out */
24427c478bd9Sstevel@tonic-gate 	if ((cp->c_flags & CN_NOCACHE) || (cp->c_frontvp == NULL)) {
24437c478bd9Sstevel@tonic-gate 		error = EINVAL;
24447c478bd9Sstevel@tonic-gate 		goto out;
24457c478bd9Sstevel@tonic-gate 	}
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MTIME;
2448da6c28aaSamw 	error = VOP_GETATTR(cp->c_frontvp, &va, 0, cr, NULL);
24497c478bd9Sstevel@tonic-gate 	if (error) {
24507c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
24517c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_ASYNCPOP)
24527c478bd9Sstevel@tonic-gate 			printf("cachefs_async_populate: getattr\n");
24537c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
24547c478bd9Sstevel@tonic-gate 		goto out;
24557c478bd9Sstevel@tonic-gate 	}
24567c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_timestamp = va.va_mtime;
24577c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_POPULATED;
24587c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags &= ~MD_INVALREADDIR;
24597c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED;
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate out:
24627c478bd9Sstevel@tonic-gate 	if (! havelock)
24637c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->c_statelock);
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	/* see if an error happened behind our backs */
24667c478bd9Sstevel@tonic-gate 	if ((error == 0) && (cp->c_flags & CN_NOCACHE)) {
24677c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
24687c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_ASYNCPOP)
24697c478bd9Sstevel@tonic-gate 			printf("cachefs_async_populate: "
24707c478bd9Sstevel@tonic-gate 			    "nocache behind our backs\n");
24717c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
24727c478bd9Sstevel@tonic-gate 		error = EINVAL;
24737c478bd9Sstevel@tonic-gate 	}
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	cp->c_flags &= ~(CN_NEED_FRONT_SYNC | CN_POPULATION_PENDING |
24767c478bd9Sstevel@tonic-gate 	    CN_ASYNC_POPULATE | CN_ASYNC_POP_WORKING);
24777c478bd9Sstevel@tonic-gate 	cp->c_popthrp = NULL;
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	if (error != 0)
24807c478bd9Sstevel@tonic-gate 		cachefs_nocache(cp);
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	/* unblock any threads waiting for populate to finish */
24837c478bd9Sstevel@tonic-gate 	cv_broadcast(&cp->c_popcv);
24847c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->c_statelock);
24857c478bd9Sstevel@tonic-gate 	rw_exit(&cp->c_rwlock);
24867c478bd9Sstevel@tonic-gate 	cachefs_cd_release(fscp);
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 	if (backvp != NULL) {
24897c478bd9Sstevel@tonic-gate 		VN_RELE(backvp);
24907c478bd9Sstevel@tonic-gate 	}
24917c478bd9Sstevel@tonic-gate 	if (frontvp != NULL) {
24927c478bd9Sstevel@tonic-gate 		VN_RELE(frontvp);
24937c478bd9Sstevel@tonic-gate 	}
24947c478bd9Sstevel@tonic-gate }
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate /*
24977c478bd9Sstevel@tonic-gate  * only to be called from cachefs_async_populate
24987c478bd9Sstevel@tonic-gate  */
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate static int
cachefs_async_populate_reg(struct cachefs_populate_req * pop,cred_t * cr,vnode_t * backvp,vnode_t * frontvp)25017c478bd9Sstevel@tonic-gate cachefs_async_populate_reg(struct cachefs_populate_req *pop, cred_t *cr,
25027c478bd9Sstevel@tonic-gate     vnode_t *backvp, vnode_t *frontvp)
25037c478bd9Sstevel@tonic-gate {
25047c478bd9Sstevel@tonic-gate 	struct cnode *cp = VTOC(pop->cpop_vp);
25057c478bd9Sstevel@tonic-gate 	int error = 0;
25067c478bd9Sstevel@tonic-gate 	u_offset_t popoff;
25077c478bd9Sstevel@tonic-gate 	size_t popsize;
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 	cachefs_cluster_allocmap(pop->cpop_off, &popoff,
25107c478bd9Sstevel@tonic-gate 	    &popsize, pop->cpop_size, cp);
25117c478bd9Sstevel@tonic-gate 	if (popsize == 0) {
25127c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
25137c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_ASYNCPOP)
25147c478bd9Sstevel@tonic-gate 			printf("cachefs_async_populate: popsize == 0\n");
25157c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
25167c478bd9Sstevel@tonic-gate 		goto out;
25177c478bd9Sstevel@tonic-gate 	}
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	error = cachefs_populate(cp, popoff, popsize, frontvp, backvp,
25207c478bd9Sstevel@tonic-gate 	    cp->c_size, cr);
25217c478bd9Sstevel@tonic-gate 	if (error != 0) {
25227c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
25237c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_ASYNCPOP)
25247c478bd9Sstevel@tonic-gate 			printf("cachefs_async_populate: cachefs_populate\n");
25257c478bd9Sstevel@tonic-gate #endif /* CFSDEBUG */
25267c478bd9Sstevel@tonic-gate 		goto out;
25277c478bd9Sstevel@tonic-gate 	}
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate out:
25307c478bd9Sstevel@tonic-gate 	return (error);
25317c478bd9Sstevel@tonic-gate }
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate void
cachefs_do_req(struct cachefs_req * rp)25347c478bd9Sstevel@tonic-gate cachefs_do_req(struct cachefs_req *rp)
25357c478bd9Sstevel@tonic-gate {
25367c478bd9Sstevel@tonic-gate 	struct cachefscache *cachep;
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->cfs_req_lock);
25397c478bd9Sstevel@tonic-gate 	switch (rp->cfs_cmd) {
25407c478bd9Sstevel@tonic-gate 	case CFS_INVALID:
25417c478bd9Sstevel@tonic-gate 		panic("cachefs_do_req: CFS_INVALID operation on queue");
25427c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
25437c478bd9Sstevel@tonic-gate 	case CFS_CACHE_SYNC:
25447c478bd9Sstevel@tonic-gate 		cachep = rp->cfs_req_u.cu_fs_sync.cf_cachep;
25457c478bd9Sstevel@tonic-gate 		cachefs_cache_sync(cachep);
25467c478bd9Sstevel@tonic-gate 		break;
25477c478bd9Sstevel@tonic-gate 	case CFS_IDLE:
25487c478bd9Sstevel@tonic-gate 		cachefs_cnode_idle(rp->cfs_req_u.cu_idle.ci_vp, rp->cfs_cr);
25497c478bd9Sstevel@tonic-gate 		break;
25507c478bd9Sstevel@tonic-gate 	case CFS_PUTPAGE:
25517c478bd9Sstevel@tonic-gate 		cachefs_async_putpage(&rp->cfs_req_u.cu_putpage, rp->cfs_cr);
25527c478bd9Sstevel@tonic-gate 		VN_RELE(rp->cfs_req_u.cu_putpage.cp_vp);
25537c478bd9Sstevel@tonic-gate 		break;
25547c478bd9Sstevel@tonic-gate 	case CFS_POPULATE:
25557c478bd9Sstevel@tonic-gate 		cachefs_async_populate(&rp->cfs_req_u.cu_populate, rp->cfs_cr);
25567c478bd9Sstevel@tonic-gate 		VN_RELE(rp->cfs_req_u.cu_populate.cpop_vp);
25577c478bd9Sstevel@tonic-gate 		break;
25587c478bd9Sstevel@tonic-gate 	case CFS_NOOP:
25597c478bd9Sstevel@tonic-gate 		break;
25607c478bd9Sstevel@tonic-gate 	default:
25617c478bd9Sstevel@tonic-gate 		panic("c_do_req: Invalid CFS async operation");
25627c478bd9Sstevel@tonic-gate 	}
25637c478bd9Sstevel@tonic-gate 	crfree(rp->cfs_cr);
25647c478bd9Sstevel@tonic-gate 	rp->cfs_cmd = CFS_INVALID;
25657c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->cfs_req_lock);
25667c478bd9Sstevel@tonic-gate 	kmem_cache_free(cachefs_req_cache, rp);
25677c478bd9Sstevel@tonic-gate }
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate ssize_t cachefs_mem_usage = 0;
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate struct km_wrap {
25757c478bd9Sstevel@tonic-gate 	size_t kw_size;
25767c478bd9Sstevel@tonic-gate 	struct km_wrap *kw_other;
25777c478bd9Sstevel@tonic-gate };
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate kmutex_t cachefs_kmem_lock;
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate void *
cachefs_kmem_alloc(size_t size,int flag)25827c478bd9Sstevel@tonic-gate cachefs_kmem_alloc(size_t size, int flag)
25837c478bd9Sstevel@tonic-gate {
25847c478bd9Sstevel@tonic-gate #ifdef DEBUG
25857c478bd9Sstevel@tonic-gate 	caddr_t mp = NULL;
25867c478bd9Sstevel@tonic-gate 	struct km_wrap *kwp;
25877c478bd9Sstevel@tonic-gate 	size_t n = (size + (2 * sizeof (struct km_wrap)) + 7) & ~7;
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	ASSERT(n >= (size + 8));
25907c478bd9Sstevel@tonic-gate 	mp = kmem_alloc(n, flag);
25917c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
25927c478bd9Sstevel@tonic-gate 		return (NULL);
25937c478bd9Sstevel@tonic-gate 	}
25947c478bd9Sstevel@tonic-gate 	/*LINTED alignment okay*/
25957c478bd9Sstevel@tonic-gate 	kwp = (struct km_wrap *)mp;
25967c478bd9Sstevel@tonic-gate 	kwp->kw_size = n;
25977c478bd9Sstevel@tonic-gate 	/*LINTED alignment okay*/
25987c478bd9Sstevel@tonic-gate 	kwp->kw_other = (struct km_wrap *)(mp + n - sizeof (struct km_wrap));
25997c478bd9Sstevel@tonic-gate 	kwp = (struct km_wrap *)kwp->kw_other;
26007c478bd9Sstevel@tonic-gate 	kwp->kw_size = n;
26017c478bd9Sstevel@tonic-gate 	/*LINTED alignment okay*/
26027c478bd9Sstevel@tonic-gate 	kwp->kw_other = (struct km_wrap *)mp;
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 	mutex_enter(&cachefs_kmem_lock);
26057c478bd9Sstevel@tonic-gate 	ASSERT(cachefs_mem_usage >= 0);
26067c478bd9Sstevel@tonic-gate 	cachefs_mem_usage += n;
26077c478bd9Sstevel@tonic-gate 	mutex_exit(&cachefs_kmem_lock);
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 	return (mp + sizeof (struct km_wrap));
26107c478bd9Sstevel@tonic-gate #else /* DEBUG */
26117c478bd9Sstevel@tonic-gate 	return (kmem_alloc(size, flag));
26127c478bd9Sstevel@tonic-gate #endif /* DEBUG */
26137c478bd9Sstevel@tonic-gate }
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate void *
cachefs_kmem_zalloc(size_t size,int flag)26167c478bd9Sstevel@tonic-gate cachefs_kmem_zalloc(size_t size, int flag)
26177c478bd9Sstevel@tonic-gate {
26187c478bd9Sstevel@tonic-gate #ifdef DEBUG
26197c478bd9Sstevel@tonic-gate 	caddr_t mp = NULL;
26207c478bd9Sstevel@tonic-gate 	struct km_wrap *kwp;
26217c478bd9Sstevel@tonic-gate 	size_t n = (size + (2 * sizeof (struct km_wrap)) + 7) & ~7;
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	ASSERT(n >= (size + 8));
26247c478bd9Sstevel@tonic-gate 	mp = kmem_zalloc(n, flag);
26257c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
26267c478bd9Sstevel@tonic-gate 		return (NULL);
26277c478bd9Sstevel@tonic-gate 	}
26287c478bd9Sstevel@tonic-gate 	/*LINTED alignment okay*/
26297c478bd9Sstevel@tonic-gate 	kwp = (struct km_wrap *)mp;
26307c478bd9Sstevel@tonic-gate 	kwp->kw_size = n;
26317c478bd9Sstevel@tonic-gate 	/*LINTED alignment okay*/
26327c478bd9Sstevel@tonic-gate 	kwp->kw_other = (struct km_wrap *)(mp + n - sizeof (struct km_wrap));
26337c478bd9Sstevel@tonic-gate 	kwp = (struct km_wrap *)kwp->kw_other;
26347c478bd9Sstevel@tonic-gate 	kwp->kw_size = n;
26357c478bd9Sstevel@tonic-gate 	/*LINTED alignment okay*/
26367c478bd9Sstevel@tonic-gate 	kwp->kw_other = (struct km_wrap *)mp;
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	mutex_enter(&cachefs_kmem_lock);
26397c478bd9Sstevel@tonic-gate 	ASSERT(cachefs_mem_usage >= 0);
26407c478bd9Sstevel@tonic-gate 	cachefs_mem_usage += n;
26417c478bd9Sstevel@tonic-gate 	mutex_exit(&cachefs_kmem_lock);
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 	return (mp + sizeof (struct km_wrap));
26447c478bd9Sstevel@tonic-gate #else /* DEBUG */
26457c478bd9Sstevel@tonic-gate 	return (kmem_zalloc(size, flag));
26467c478bd9Sstevel@tonic-gate #endif /* DEBUG */
26477c478bd9Sstevel@tonic-gate }
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate void
cachefs_kmem_free(void * mp,size_t size)26507c478bd9Sstevel@tonic-gate cachefs_kmem_free(void *mp, size_t size)
26517c478bd9Sstevel@tonic-gate {
26527c478bd9Sstevel@tonic-gate #ifdef DEBUG
26537c478bd9Sstevel@tonic-gate 	struct km_wrap *front_kwp;
26547c478bd9Sstevel@tonic-gate 	struct km_wrap *back_kwp;
26557c478bd9Sstevel@tonic-gate 	size_t n = (size + (2 * sizeof (struct km_wrap)) + 7) & ~7;
26567c478bd9Sstevel@tonic-gate 	void *p;
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 	ASSERT(n >= (size + 8));
26597c478bd9Sstevel@tonic-gate 	front_kwp = (struct km_wrap *)((uintptr_t)mp - sizeof (struct km_wrap));
26607c478bd9Sstevel@tonic-gate 	back_kwp = (struct km_wrap *)
26617c478bd9Sstevel@tonic-gate 	    ((uintptr_t)front_kwp + n - sizeof (struct km_wrap));
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 	ASSERT(front_kwp->kw_other == back_kwp);
26647c478bd9Sstevel@tonic-gate 	ASSERT(front_kwp->kw_size == n);
26657c478bd9Sstevel@tonic-gate 	ASSERT(back_kwp->kw_other == front_kwp);
26667c478bd9Sstevel@tonic-gate 	ASSERT(back_kwp->kw_size == n);
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	mutex_enter(&cachefs_kmem_lock);
26697c478bd9Sstevel@tonic-gate 	cachefs_mem_usage -= n;
26707c478bd9Sstevel@tonic-gate 	ASSERT(cachefs_mem_usage >= 0);
26717c478bd9Sstevel@tonic-gate 	mutex_exit(&cachefs_kmem_lock);
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 	p = front_kwp;
26747c478bd9Sstevel@tonic-gate 	front_kwp->kw_size = back_kwp->kw_size = 0;
26757c478bd9Sstevel@tonic-gate 	front_kwp->kw_other = back_kwp->kw_other = NULL;
26767c478bd9Sstevel@tonic-gate 	kmem_free(p, n);
26777c478bd9Sstevel@tonic-gate #else /* DEBUG */
26787c478bd9Sstevel@tonic-gate 	kmem_free(mp, size);
26797c478bd9Sstevel@tonic-gate #endif /* DEBUG */
26807c478bd9Sstevel@tonic-gate }
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate char *
cachefs_strdup(char * s)26837c478bd9Sstevel@tonic-gate cachefs_strdup(char *s)
26847c478bd9Sstevel@tonic-gate {
26857c478bd9Sstevel@tonic-gate 	char *rc;
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	ASSERT(s != NULL);
26887c478bd9Sstevel@tonic-gate 
26897c478bd9Sstevel@tonic-gate 	rc = cachefs_kmem_alloc(strlen(s) + 1, KM_SLEEP);
26907c478bd9Sstevel@tonic-gate 	(void) strcpy(rc, s);
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 	return (rc);
26937c478bd9Sstevel@tonic-gate }
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate int
cachefs_stats_kstat_snapshot(kstat_t * ksp,void * buf,int rw)26967c478bd9Sstevel@tonic-gate cachefs_stats_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
26977c478bd9Sstevel@tonic-gate {
26987c478bd9Sstevel@tonic-gate 	struct fscache *fscp = (struct fscache *)ksp->ks_data;
26997c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
27007c478bd9Sstevel@tonic-gate 	int	error = 0;
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
27037c478bd9Sstevel@tonic-gate 		bcopy(buf, &fscp->fs_stats, sizeof (fscp->fs_stats));
27047c478bd9Sstevel@tonic-gate 		cachep->c_gc_count = fscp->fs_stats.st_gc_count;
27057c478bd9Sstevel@tonic-gate 		CACHEFS_CFS_TIME_TO_TIME_COPY(fscp->fs_stats.st_gc_time,
27067c478bd9Sstevel@tonic-gate 		    cachep->c_gc_time);
27077c478bd9Sstevel@tonic-gate 		CACHEFS_CFS_TIME_TO_TIME_COPY(fscp->fs_stats.st_gc_before_atime,
27087c478bd9Sstevel@tonic-gate 		    cachep->c_gc_before);
27097c478bd9Sstevel@tonic-gate 		CACHEFS_CFS_TIME_TO_TIME_COPY(fscp->fs_stats.st_gc_after_atime,
27107c478bd9Sstevel@tonic-gate 		    cachep->c_gc_after);
27117c478bd9Sstevel@tonic-gate 		return (error);
27127c478bd9Sstevel@tonic-gate 	}
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 	fscp->fs_stats.st_gc_count = cachep->c_gc_count;
27157c478bd9Sstevel@tonic-gate 	CACHEFS_TIME_TO_CFS_TIME_COPY(cachep->c_gc_time,
27167c478bd9Sstevel@tonic-gate 	    fscp->fs_stats.st_gc_time, error);
27177c478bd9Sstevel@tonic-gate 	CACHEFS_TIME_TO_CFS_TIME_COPY(cachep->c_gc_before,
27187c478bd9Sstevel@tonic-gate 	    fscp->fs_stats.st_gc_before_atime, error);
27197c478bd9Sstevel@tonic-gate 	CACHEFS_TIME_TO_CFS_TIME_COPY(cachep->c_gc_after,
27207c478bd9Sstevel@tonic-gate 	    fscp->fs_stats.st_gc_after_atime, error);
27217c478bd9Sstevel@tonic-gate 	bcopy(&fscp->fs_stats, buf, sizeof (fscp->fs_stats));
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	return (error);
27247c478bd9Sstevel@tonic-gate }
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate #ifdef DEBUG
27277c478bd9Sstevel@tonic-gate cachefs_debug_info_t *
cachefs_debug_save(cachefs_debug_info_t * oldcdb,int chain,char * message,uint_t flags,int number,void * pointer,cachefscache_t * cachep,struct fscache * fscp,struct cnode * cp)27287c478bd9Sstevel@tonic-gate cachefs_debug_save(cachefs_debug_info_t *oldcdb, int chain,
27297c478bd9Sstevel@tonic-gate     char *message, uint_t flags, int number, void *pointer,
27307c478bd9Sstevel@tonic-gate     cachefscache_t *cachep, struct fscache *fscp, struct cnode *cp)
27317c478bd9Sstevel@tonic-gate {
27327c478bd9Sstevel@tonic-gate 	cachefs_debug_info_t *cdb;
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	if ((chain) || (oldcdb == NULL))
27357c478bd9Sstevel@tonic-gate 		cdb = cachefs_kmem_zalloc(sizeof (*cdb), KM_SLEEP);
27367c478bd9Sstevel@tonic-gate 	else
27377c478bd9Sstevel@tonic-gate 		cdb = oldcdb;
27387c478bd9Sstevel@tonic-gate 	if (chain)
27397c478bd9Sstevel@tonic-gate 		cdb->cdb_next = oldcdb;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	if (message != NULL) {
27427c478bd9Sstevel@tonic-gate 		if (cdb->cdb_message != NULL)
27437c478bd9Sstevel@tonic-gate 			cachefs_kmem_free(cdb->cdb_message,
27447c478bd9Sstevel@tonic-gate 			    strlen(cdb->cdb_message) + 1);
27457c478bd9Sstevel@tonic-gate 		cdb->cdb_message = cachefs_kmem_alloc(strlen(message) + 1,
27467c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
27477c478bd9Sstevel@tonic-gate 		(void) strcpy(cdb->cdb_message, message);
27487c478bd9Sstevel@tonic-gate 	}
27497c478bd9Sstevel@tonic-gate 	cdb->cdb_flags = flags;
27507c478bd9Sstevel@tonic-gate 	cdb->cdb_int = number;
27517c478bd9Sstevel@tonic-gate 	cdb->cdb_pointer = pointer;
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 	cdb->cdb_count++;
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 	cdb->cdb_cnode = cp;
27567c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
27577c478bd9Sstevel@tonic-gate 		cdb->cdb_frontvp = cp->c_frontvp;
27587c478bd9Sstevel@tonic-gate 		cdb->cdb_backvp = cp->c_backvp;
27597c478bd9Sstevel@tonic-gate 	}
27607c478bd9Sstevel@tonic-gate 	if (fscp != NULL)
27617c478bd9Sstevel@tonic-gate 		cdb->cdb_fscp = fscp;
27627c478bd9Sstevel@tonic-gate 	else if (cp != NULL)
27637c478bd9Sstevel@tonic-gate 		cdb->cdb_fscp = C_TO_FSCACHE(cp);
27647c478bd9Sstevel@tonic-gate 	if (cachep != NULL)
27657c478bd9Sstevel@tonic-gate 		cdb->cdb_cachep = cachep;
27667c478bd9Sstevel@tonic-gate 	else if (cdb->cdb_fscp != NULL)
27677c478bd9Sstevel@tonic-gate 		cdb->cdb_cachep = cdb->cdb_fscp->fs_cache;
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 	cdb->cdb_thread = curthread;
27707c478bd9Sstevel@tonic-gate 	cdb->cdb_timestamp = gethrtime();
27717c478bd9Sstevel@tonic-gate 	cdb->cdb_depth = getpcstack(cdb->cdb_stack, CACHEFS_DEBUG_DEPTH);
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate 	return (cdb);
27747c478bd9Sstevel@tonic-gate }
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate void
cachefs_debug_show(cachefs_debug_info_t * cdb)27777c478bd9Sstevel@tonic-gate cachefs_debug_show(cachefs_debug_info_t *cdb)
27787c478bd9Sstevel@tonic-gate {
27797c478bd9Sstevel@tonic-gate 	hrtime_t now = gethrtime();
27807c478bd9Sstevel@tonic-gate 	timestruc_t ts;
27817c478bd9Sstevel@tonic-gate 	int i;
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	while (cdb != NULL) {
27847c478bd9Sstevel@tonic-gate 		hrt2ts(now - cdb->cdb_timestamp, &ts);
27857c478bd9Sstevel@tonic-gate 		printf("cdb: %p count: %d timelapse: %ld.%9ld\n",
27867c478bd9Sstevel@tonic-gate 		    (void *)cdb, cdb->cdb_count, ts.tv_sec, ts.tv_nsec);
27877c478bd9Sstevel@tonic-gate 		if (cdb->cdb_message != NULL)
27887c478bd9Sstevel@tonic-gate 			printf("message: %s", cdb->cdb_message);
27897c478bd9Sstevel@tonic-gate 		printf("flags: %x int: %d pointer: %p\n",
27907c478bd9Sstevel@tonic-gate 		    cdb->cdb_flags, cdb->cdb_int, (void *)cdb->cdb_pointer);
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 		printf("cnode: %p fscp: %p cachep: %p\n",
27937c478bd9Sstevel@tonic-gate 		    (void *)cdb->cdb_cnode,
27947c478bd9Sstevel@tonic-gate 		    (void *)cdb->cdb_fscp, (void *)cdb->cdb_cachep);
27957c478bd9Sstevel@tonic-gate 		printf("frontvp: %p backvp: %p\n",
27967c478bd9Sstevel@tonic-gate 		    (void *)cdb->cdb_frontvp, (void *)cdb->cdb_backvp);
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 		printf("thread: %p stack...\n", (void *)cdb->cdb_thread);
27997c478bd9Sstevel@tonic-gate 		for (i = 0; i < cdb->cdb_depth; i++) {
28007c478bd9Sstevel@tonic-gate 			ulong_t off;
28017c478bd9Sstevel@tonic-gate 			char *sym;
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 			sym = kobj_getsymname(cdb->cdb_stack[i], &off);
28047c478bd9Sstevel@tonic-gate 			printf("%s+%lx\n", sym ? sym : "?", off);
28057c478bd9Sstevel@tonic-gate 		}
28067c478bd9Sstevel@tonic-gate 		delay(2*hz);
28077c478bd9Sstevel@tonic-gate 		cdb = cdb->cdb_next;
28087c478bd9Sstevel@tonic-gate 	}
28097c478bd9Sstevel@tonic-gate 	debug_enter(NULL);
28107c478bd9Sstevel@tonic-gate }
28117c478bd9Sstevel@tonic-gate #endif /* DEBUG */
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate /*
28147c478bd9Sstevel@tonic-gate  * Changes the size of the front file.
28157c478bd9Sstevel@tonic-gate  * Returns 0 for success or error if cannot set file size.
28167c478bd9Sstevel@tonic-gate  * NOCACHE bit is ignored.
28177c478bd9Sstevel@tonic-gate  * c_size is ignored.
28187c478bd9Sstevel@tonic-gate  * statelock must be held, frontvp must be set.
28197c478bd9Sstevel@tonic-gate  * File must be populated if setting to a size other than zero.
28207c478bd9Sstevel@tonic-gate  */
28217c478bd9Sstevel@tonic-gate int
cachefs_frontfile_size(cnode_t * cp,u_offset_t length)28227c478bd9Sstevel@tonic-gate cachefs_frontfile_size(cnode_t *cp, u_offset_t length)
28237c478bd9Sstevel@tonic-gate {
28247c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = C_TO_FSCACHE(cp)->fs_cache;
28257c478bd9Sstevel@tonic-gate 	vattr_t va;
28267c478bd9Sstevel@tonic-gate 	size_t nblks, blkdelta;
28277c478bd9Sstevel@tonic-gate 	int error = 0;
28287c478bd9Sstevel@tonic-gate 	int alloc = 0;
28297c478bd9Sstevel@tonic-gate 	struct cachefs_allocmap *allocp;
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cp->c_statelock));
28327c478bd9Sstevel@tonic-gate 	ASSERT(cp->c_frontvp);
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	/* if growing the file, allocate space first, we charge for holes */
28357c478bd9Sstevel@tonic-gate 	if (length) {
28367c478bd9Sstevel@tonic-gate 		ASSERT(cp->c_metadata.md_flags & MD_POPULATED);
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 		nblks = (length + MAXBSIZE - 1) / MAXBSIZE;
28397c478bd9Sstevel@tonic-gate 		if (nblks > cp->c_metadata.md_frontblks) {
28407c478bd9Sstevel@tonic-gate 			blkdelta = nblks - cp->c_metadata.md_frontblks;
28417c478bd9Sstevel@tonic-gate 			error = cachefs_allocblocks(cachep, blkdelta,
28427c478bd9Sstevel@tonic-gate 			    cp->c_metadata.md_rltype);
28437c478bd9Sstevel@tonic-gate 			if (error)
28447c478bd9Sstevel@tonic-gate 				goto out;
28457c478bd9Sstevel@tonic-gate 			alloc = 1;
28467c478bd9Sstevel@tonic-gate 		}
28477c478bd9Sstevel@tonic-gate 	}
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 	/* change the size of the front file */
28507c478bd9Sstevel@tonic-gate 	va.va_mask = AT_SIZE;
28517c478bd9Sstevel@tonic-gate 	va.va_size = length;
28527c478bd9Sstevel@tonic-gate 	error = VOP_SETATTR(cp->c_frontvp, &va, 0, kcred, NULL);
28537c478bd9Sstevel@tonic-gate 	if (error)
28547c478bd9Sstevel@tonic-gate 		goto out;
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 	/* zero out the alloc map */
28577c478bd9Sstevel@tonic-gate 	bzero(&cp->c_metadata.md_allocinfo,
28587c478bd9Sstevel@tonic-gate 	    cp->c_metadata.md_allocents * sizeof (struct cachefs_allocmap));
28597c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_allocents = 0;
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	if (length == 0) {
28627c478bd9Sstevel@tonic-gate 		/* free up blocks */
28637c478bd9Sstevel@tonic-gate 		if (cp->c_metadata.md_frontblks) {
28647c478bd9Sstevel@tonic-gate 			cachefs_freeblocks(cachep, cp->c_metadata.md_frontblks,
28657c478bd9Sstevel@tonic-gate 			    cp->c_metadata.md_rltype);
28667c478bd9Sstevel@tonic-gate 			cp->c_metadata.md_frontblks = 0;
28677c478bd9Sstevel@tonic-gate 		}
28687c478bd9Sstevel@tonic-gate 	} else {
28697c478bd9Sstevel@tonic-gate 		/* update number of blocks if shrinking file */
28707c478bd9Sstevel@tonic-gate 		nblks = (length + MAXBSIZE - 1) / MAXBSIZE;
28717c478bd9Sstevel@tonic-gate 		if (nblks < cp->c_metadata.md_frontblks) {
28727c478bd9Sstevel@tonic-gate 			blkdelta = cp->c_metadata.md_frontblks - nblks;
28737c478bd9Sstevel@tonic-gate 			cachefs_freeblocks(cachep, blkdelta,
28747c478bd9Sstevel@tonic-gate 			    cp->c_metadata.md_rltype);
28757c478bd9Sstevel@tonic-gate 			cp->c_metadata.md_frontblks = (uint_t)nblks;
28767c478bd9Sstevel@tonic-gate 		}
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 		/* fix up alloc map to reflect new size */
28797c478bd9Sstevel@tonic-gate 		allocp = cp->c_metadata.md_allocinfo;
28807c478bd9Sstevel@tonic-gate 		allocp->am_start_off = 0;
28817c478bd9Sstevel@tonic-gate 		allocp->am_size = length;
28827c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_allocents = 1;
28837c478bd9Sstevel@tonic-gate 	}
28847c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED | CN_NEED_FRONT_SYNC;
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate out:
28877c478bd9Sstevel@tonic-gate 	if (error && alloc)
28887c478bd9Sstevel@tonic-gate 		cachefs_freeblocks(cachep, blkdelta, cp->c_metadata.md_rltype);
28897c478bd9Sstevel@tonic-gate 	return (error);
28907c478bd9Sstevel@tonic-gate }
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
28937c478bd9Sstevel@tonic-gate int
cachefs_req_create(void * voidp,void * cdrarg,int kmflags)28947c478bd9Sstevel@tonic-gate cachefs_req_create(void *voidp, void *cdrarg, int kmflags)
28957c478bd9Sstevel@tonic-gate {
28967c478bd9Sstevel@tonic-gate 	struct cachefs_req *rp = (struct cachefs_req *)voidp;
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	/*
28997c478bd9Sstevel@tonic-gate 	 * XXX don't do this!  if you need this, you can't use this
29007c478bd9Sstevel@tonic-gate 	 * constructor.
29017c478bd9Sstevel@tonic-gate 	 */
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 	bzero(rp, sizeof (struct cachefs_req));
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	mutex_init(&rp->cfs_req_lock, NULL, MUTEX_DEFAULT, NULL);
29067c478bd9Sstevel@tonic-gate 	return (0);
29077c478bd9Sstevel@tonic-gate }
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29107c478bd9Sstevel@tonic-gate void
cachefs_req_destroy(void * voidp,void * cdrarg)29117c478bd9Sstevel@tonic-gate cachefs_req_destroy(void *voidp, void *cdrarg)
29127c478bd9Sstevel@tonic-gate {
29137c478bd9Sstevel@tonic-gate 	struct cachefs_req *rp = (struct cachefs_req *)voidp;
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	mutex_destroy(&rp->cfs_req_lock);
29167c478bd9Sstevel@tonic-gate }
2917