xref: /titanic_44/usr/src/uts/common/fs/cachefs/cachefs_ioctl.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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
5*da6c28aaSamw  * Common Development and Distribution License (the "License").
6*da6c28aaSamw  * 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*da6c28aaSamw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/cred.h>
317c478bd9Sstevel@tonic-gate #include <sys/proc.h>
327c478bd9Sstevel@tonic-gate #include <sys/user.h>
337c478bd9Sstevel@tonic-gate #include <sys/time.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
367c478bd9Sstevel@tonic-gate #include <sys/file.h>
377c478bd9Sstevel@tonic-gate #include <sys/filio.h>
387c478bd9Sstevel@tonic-gate #include <sys/uio.h>
397c478bd9Sstevel@tonic-gate #include <sys/buf.h>
407c478bd9Sstevel@tonic-gate #include <sys/mman.h>
417c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
427c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
437c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
447c478bd9Sstevel@tonic-gate #include <sys/conf.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
477c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
487c478bd9Sstevel@tonic-gate #include <sys/flock.h>
497c478bd9Sstevel@tonic-gate #include <sys/fbuf.h>
507c478bd9Sstevel@tonic-gate #include <sys/swap.h>
517c478bd9Sstevel@tonic-gate #include <sys/errno.h>
527c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
537c478bd9Sstevel@tonic-gate #include <sys/disp.h>
547c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
557c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
567c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
577c478bd9Sstevel@tonic-gate #include <sys/mount.h>
587c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
597c478bd9Sstevel@tonic-gate #include <sys/stat.h>
607c478bd9Sstevel@tonic-gate #include <rpc/types.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <vm/hat.h>
637c478bd9Sstevel@tonic-gate #include <vm/as.h>
647c478bd9Sstevel@tonic-gate #include <vm/page.h>
657c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
667c478bd9Sstevel@tonic-gate #include <vm/seg.h>
677c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
687c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
697c478bd9Sstevel@tonic-gate #include <vm/rm.h>
707c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
717c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_dlog.h>
727c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_ioctl.h>
737c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_dir.h>
747c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_dlog.h>
757c478bd9Sstevel@tonic-gate #include "fs/fs_subr.h"
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate void cachefs_addhash(struct cnode *);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Local functions
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate static void sync_metadata(cnode_t *);
847c478bd9Sstevel@tonic-gate static void drop_backvp(cnode_t *);
857c478bd9Sstevel@tonic-gate static void allow_pendrm(cnode_t *cp);
867c478bd9Sstevel@tonic-gate static int cachefs_unpack_common(vnode_t *vp);
877c478bd9Sstevel@tonic-gate static int cachefs_unpackall_list(cachefscache_t *cachep,
887c478bd9Sstevel@tonic-gate     enum cachefs_rl_type type);
897c478bd9Sstevel@tonic-gate static void cachefs_modified_fix(fscache_t *fscp);
907c478bd9Sstevel@tonic-gate static void cachefs_iosetneedattrs(fscache_t *fscp, cfs_cid_t *cidp);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #if (defined(_SYSCALL32_IMPL) || defined(_LP64))
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	CACHEFS_DECL(type, handle)					\
957c478bd9Sstevel@tonic-gate 	type	handle
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define	CACHEFS_TMPPTR_SET(in_addr, tmp_addr, tmp_ptr, type)		\
987c478bd9Sstevel@tonic-gate 	tmp_ptr = (type *)(tmp_addr)
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #define	CACHEFS_FID_COPYOUT(in_fidp, out_fidp)				\
1017c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPY((fid_t *)(in_fidp), (cfs_fid_t *)(out_fidp))
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	CACHEFS_FID_COPYIN(in_fidp, out_fidp)				\
1047c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPY((cfs_fid_t *)(in_fidp), (fid_t *)(out_fidp))
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define	CACHEFS_VATTR_COPYOUT(in_vattrp, out_vattrp, error)		\
1077c478bd9Sstevel@tonic-gate 	if (!error) {							\
1087c478bd9Sstevel@tonic-gate 		CACHEFS_VATTR_TO_CFS_VATTR_COPY((vattr_t *)(in_vattrp),	\
1097c478bd9Sstevel@tonic-gate 			(cfs_vattr_t *)(out_vattrp), error);		\
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #define	CACHEFS_VATTR_COPYIN(in_vattrp, out_vattrp)			\
1137c478bd9Sstevel@tonic-gate 	CACHEFS_CFS_VATTR_TO_VATTR_COPY((cfs_vattr_t *)(in_vattrp),	\
1147c478bd9Sstevel@tonic-gate 			(vattr_t *)(out_vattrp))
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #else /* not _SYSCALL32_IMPL || _LP64 */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #define	CACHEFS_DECL(type, handle)
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #define	CACHEFS_TMPPTR_SET(in_addr, tmp_addr, tmp_ptr, type)		\
1217c478bd9Sstevel@tonic-gate 	tmp_ptr = (type *)(in_addr)
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #define	CACHEFS_FID_COPYOUT(in_fidp, out_fidp)
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	CACHEFS_FID_COPYIN(in_fidp, out_fidp)
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #define	CACHEFS_VATTR_COPYOUT(in_vattrp, out_vattrp, error)
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	CACHEFS_VATTR_COPYIN(in_vattrp, out_vattrp)
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL || _LP64 */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * Conjure up a credential from the partial credential stored in
1357c478bd9Sstevel@tonic-gate  * a file.  This is bogus and cachefs should really be fixed, but
1367c478bd9Sstevel@tonic-gate  * this maintains maximum compatibility.
1377c478bd9Sstevel@tonic-gate  * dl_cred *cr points to a basic credential followed directly by a buffer that
1387c478bd9Sstevel@tonic-gate  * takes a number of groups.
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static cred_t *
conj_cred(dl_cred_t * cr)1427c478bd9Sstevel@tonic-gate conj_cred(dl_cred_t *cr)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	cred_t *newcr = crget();
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	(void) crsetresuid(newcr, cr->cr_ruid, cr->cr_uid, cr->cr_suid);
1477c478bd9Sstevel@tonic-gate 	(void) crsetresgid(newcr, cr->cr_rgid, cr->cr_gid, cr->cr_sgid);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	(void) crsetgroups(newcr, MIN(NGROUPS_MAX_DEFAULT, cr->cr_ngroups),
1507c478bd9Sstevel@tonic-gate 		cr->cr_groups);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	return (newcr);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Pack a file in the cache
1567c478bd9Sstevel@tonic-gate  *	dvp is the directory the file resides in.
1577c478bd9Sstevel@tonic-gate  *	name is the name of the file.
1587c478bd9Sstevel@tonic-gate  *	Returns 0 or an error if could not perform the operation.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate int
cachefs_pack(struct vnode * dvp,char * name,cred_t * cr)1617c478bd9Sstevel@tonic-gate cachefs_pack(struct vnode *dvp, char *name, cred_t *cr)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(dvp));
1647c478bd9Sstevel@tonic-gate 	int error = 0;
1657c478bd9Sstevel@tonic-gate 	int connected = 0;
1667c478bd9Sstevel@tonic-gate 	vnode_t *vp;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/*
1697c478bd9Sstevel@tonic-gate 	 * Return if NFSv4 is the backfs (no caching).
1707c478bd9Sstevel@tonic-gate 	 */
1717c478bd9Sstevel@tonic-gate 	if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
1727c478bd9Sstevel@tonic-gate 		goto out;
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	for (;;) {
1767c478bd9Sstevel@tonic-gate 		/* get access to the file system */
1777c478bd9Sstevel@tonic-gate 		error = cachefs_cd_access(fscp, connected, 0);
1787c478bd9Sstevel@tonic-gate 		if (error)
1797c478bd9Sstevel@tonic-gate 			break;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		/* lookup the file name */
1827c478bd9Sstevel@tonic-gate 		error = cachefs_lookup_common(dvp, name, &vp, NULL, 0, NULL,
1837c478bd9Sstevel@tonic-gate 		    cr);
1847c478bd9Sstevel@tonic-gate 		if (error == 0) {
1857c478bd9Sstevel@tonic-gate 			error = cachefs_pack_common(vp, cr);
1867c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		if (CFS_TIMEOUT(fscp, error)) {
1897c478bd9Sstevel@tonic-gate 			if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
1907c478bd9Sstevel@tonic-gate 				cachefs_cd_release(fscp);
1917c478bd9Sstevel@tonic-gate 				cachefs_cd_timedout(fscp);
1927c478bd9Sstevel@tonic-gate 				connected = 0;
1937c478bd9Sstevel@tonic-gate 				continue;
1947c478bd9Sstevel@tonic-gate 			} else {
1957c478bd9Sstevel@tonic-gate 				cachefs_cd_release(fscp);
1967c478bd9Sstevel@tonic-gate 				connected = 1;
1977c478bd9Sstevel@tonic-gate 				continue;
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		cachefs_cd_release(fscp);
2017c478bd9Sstevel@tonic-gate 		break;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate out:
2057c478bd9Sstevel@tonic-gate 	return (error);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * Packs the file belonging to the passed in vnode.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate int
cachefs_pack_common(vnode_t * vp,cred_t * cr)2117c478bd9Sstevel@tonic-gate cachefs_pack_common(vnode_t *vp, cred_t *cr)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	cnode_t *cp = VTOC(vp);
2147c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(cp);
2157c478bd9Sstevel@tonic-gate 	int error = 0;
2167c478bd9Sstevel@tonic-gate 	offset_t off;
2177c478bd9Sstevel@tonic-gate 	caddr_t buf;
2187c478bd9Sstevel@tonic-gate 	int buflen;
2197c478bd9Sstevel@tonic-gate 	rl_entry_t rl_ent;
2207c478bd9Sstevel@tonic-gate 	u_offset_t cnode_size;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	rw_enter(&cp->c_rwlock, RW_WRITER);
2237c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_statelock);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* done if cannot write to cache */
2267c478bd9Sstevel@tonic-gate 	if ((cp->c_filegrp->fg_flags & CFS_FG_WRITE) == 0) {
2277c478bd9Sstevel@tonic-gate 		error = EROFS;
2287c478bd9Sstevel@tonic-gate 		goto out;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/* done if not usable */
2327c478bd9Sstevel@tonic-gate 	if (cp->c_flags & (CN_STALE | CN_DESTROY)) {
2337c478bd9Sstevel@tonic-gate 		error = ESTALE;
2347c478bd9Sstevel@tonic-gate 		goto out;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* make sure up to date */
2387c478bd9Sstevel@tonic-gate 	error = CFSOP_CHECK_COBJECT(fscp, cp, C_BACK_CHECK, cr);
2397c478bd9Sstevel@tonic-gate 	if (error)
2407c478bd9Sstevel@tonic-gate 		goto out;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/* make it cachable */
2437c478bd9Sstevel@tonic-gate 	cp->c_flags &= ~CN_NOCACHE;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/* get a metadata slot if we do not have one yet */
2467c478bd9Sstevel@tonic-gate 	if (cp->c_flags & CN_ALLOC_PENDING) {
2477c478bd9Sstevel@tonic-gate 		if (cp->c_filegrp->fg_flags & CFS_FG_ALLOC_ATTR) {
2487c478bd9Sstevel@tonic-gate 			(void) filegrp_allocattr(cp->c_filegrp);
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 		error = filegrp_create_metadata(cp->c_filegrp,
2517c478bd9Sstevel@tonic-gate 		    &cp->c_metadata, &cp->c_id);
2527c478bd9Sstevel@tonic-gate 		if (error)
2537c478bd9Sstevel@tonic-gate 			goto out;
2547c478bd9Sstevel@tonic-gate 		cp->c_flags &= ~CN_ALLOC_PENDING;
2557c478bd9Sstevel@tonic-gate 		cp->c_flags |= CN_UPDATED;
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/* cache the ACL if necessary */
2597c478bd9Sstevel@tonic-gate 	if (((fscp->fs_info.fi_mntflags & CFS_NOACL) == 0) &&
2607c478bd9Sstevel@tonic-gate 	    (cachefs_vtype_aclok(vp)) &&
2617c478bd9Sstevel@tonic-gate 	    ((cp->c_metadata.md_flags & MD_ACL) == 0)) {
2627c478bd9Sstevel@tonic-gate 		error = cachefs_cacheacl(cp, NULL);
2637c478bd9Sstevel@tonic-gate 		if (error != 0)
2647c478bd9Sstevel@tonic-gate 			goto out;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* directory */
2687c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
2697c478bd9Sstevel@tonic-gate 		if (cp->c_metadata.md_flags & MD_POPULATED)
2707c478bd9Sstevel@tonic-gate 			goto out;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		if (error = cachefs_dir_fill(cp, cr))
2737c478bd9Sstevel@tonic-gate 			goto out;
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/* regular file */
2777c478bd9Sstevel@tonic-gate 	else if (vp->v_type == VREG) {
2787c478bd9Sstevel@tonic-gate 		if (cp->c_metadata.md_flags & MD_POPULATED)
2797c478bd9Sstevel@tonic-gate 			goto out;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		if (cp->c_backvp == NULL) {
2827c478bd9Sstevel@tonic-gate 			error = cachefs_getbackvp(fscp, cp);
2837c478bd9Sstevel@tonic-gate 			if (error)
2847c478bd9Sstevel@tonic-gate 				goto out;
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		if (cp->c_frontvp == NULL) {
2877c478bd9Sstevel@tonic-gate 			error = cachefs_getfrontfile(cp);
2887c478bd9Sstevel@tonic-gate 			if (error)
2897c478bd9Sstevel@tonic-gate 				goto out;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 		/* populate the file */
2927c478bd9Sstevel@tonic-gate 		off = (offset_t)0;
2937c478bd9Sstevel@tonic-gate 		cnode_size = cp->c_attr.va_size;
2947c478bd9Sstevel@tonic-gate 		while (off < cnode_size) {
2957c478bd9Sstevel@tonic-gate 			if (!cachefs_check_allocmap(cp, off)) {
2967c478bd9Sstevel@tonic-gate 				u_offset_t popoff;
2977c478bd9Sstevel@tonic-gate 				size_t popsize;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 				cachefs_cluster_allocmap(off, &popoff,
3007c478bd9Sstevel@tonic-gate 				    &popsize, (size_t)DEF_POP_SIZE, cp);
3017c478bd9Sstevel@tonic-gate 				if (popsize != 0) {
3027c478bd9Sstevel@tonic-gate 					error = cachefs_populate(cp, popoff,
3037c478bd9Sstevel@tonic-gate 					    popsize, cp->c_frontvp,
3047c478bd9Sstevel@tonic-gate 					    cp->c_backvp, cp->c_size, cr);
3057c478bd9Sstevel@tonic-gate 					if (error)
3067c478bd9Sstevel@tonic-gate 						goto out;
3077c478bd9Sstevel@tonic-gate 					else
3087c478bd9Sstevel@tonic-gate 						cp->c_flags |= (CN_UPDATED |
3097c478bd9Sstevel@tonic-gate 						    CN_NEED_FRONT_SYNC |
3107c478bd9Sstevel@tonic-gate 						    CN_POPULATION_PENDING);
3117c478bd9Sstevel@tonic-gate 					popsize = popsize - (off - popoff);
3127c478bd9Sstevel@tonic-gate 				}
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate 			off += PAGESIZE;
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/* symbolic link */
3197c478bd9Sstevel@tonic-gate 	else if (vp->v_type == VLNK) {
3207c478bd9Sstevel@tonic-gate 		if (cp->c_metadata.md_flags & (MD_POPULATED | MD_FASTSYMLNK))
3217c478bd9Sstevel@tonic-gate 			goto out;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		/* get the sym link contents from the back fs */
3247c478bd9Sstevel@tonic-gate 		error = cachefs_readlink_back(cp, cr, &buf, &buflen);
3257c478bd9Sstevel@tonic-gate 		if (error)
3267c478bd9Sstevel@tonic-gate 			goto out;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		/* try to cache the sym link */
3297c478bd9Sstevel@tonic-gate 		error = cachefs_stuffsymlink(cp, buf, buflen);
3307c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(buf, MAXPATHLEN);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/* assume that all other types fit in the attributes */
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate out:
3367c478bd9Sstevel@tonic-gate 	/* get the rl slot if needed */
3377c478bd9Sstevel@tonic-gate 	if ((error == 0) && (cp->c_metadata.md_rlno == 0)) {
3387c478bd9Sstevel@tonic-gate 		rl_ent.rl_fileno = cp->c_id.cid_fileno;
3397c478bd9Sstevel@tonic-gate 		rl_ent.rl_local = (cp->c_id.cid_flags & CFS_CID_LOCAL) ? 1 : 0;
3407c478bd9Sstevel@tonic-gate 		rl_ent.rl_fsid = fscp->fs_cfsid;
3417c478bd9Sstevel@tonic-gate 		rl_ent.rl_attrc = 0;
3427c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_rltype = CACHEFS_RL_NONE;
3437c478bd9Sstevel@tonic-gate 		error = cachefs_rl_alloc(fscp->fs_cache, &rl_ent,
3447c478bd9Sstevel@tonic-gate 		    &cp->c_metadata.md_rlno);
3457c478bd9Sstevel@tonic-gate 		if (error == 0)
3467c478bd9Sstevel@tonic-gate 			error = filegrp_ffhold(cp->c_filegrp);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/* mark the file as packed */
3507c478bd9Sstevel@tonic-gate 	if (error == 0) {
3517c478bd9Sstevel@tonic-gate 		/* modified takes precedence over packed */
3527c478bd9Sstevel@tonic-gate 		if (cp->c_metadata.md_rltype != CACHEFS_RL_MODIFIED) {
3537c478bd9Sstevel@tonic-gate 			cachefs_rlent_moveto(fscp->fs_cache,
3547c478bd9Sstevel@tonic-gate 			    CACHEFS_RL_PACKED, cp->c_metadata.md_rlno,
3557c478bd9Sstevel@tonic-gate 			    cp->c_metadata.md_frontblks);
3567c478bd9Sstevel@tonic-gate 			cp->c_metadata.md_rltype = CACHEFS_RL_PACKED;
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_flags |= MD_PACKED;
3597c478bd9Sstevel@tonic-gate 		cp->c_flags |= CN_UPDATED;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->c_statelock);
3637c478bd9Sstevel@tonic-gate 	rw_exit(&cp->c_rwlock);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	return (error);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * Unpack a file from the cache
3707c478bd9Sstevel@tonic-gate  *	dvp is the directory the file resides in.
3717c478bd9Sstevel@tonic-gate  *	name is the name of the file.
3727c478bd9Sstevel@tonic-gate  *	Returns 0 or an error if could not perform the operation.
3737c478bd9Sstevel@tonic-gate  */
3747c478bd9Sstevel@tonic-gate int
cachefs_unpack(struct vnode * dvp,char * name,cred_t * cr)3757c478bd9Sstevel@tonic-gate cachefs_unpack(struct vnode *dvp, char *name, cred_t *cr)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(dvp));
3787c478bd9Sstevel@tonic-gate 	int error = 0;
3797c478bd9Sstevel@tonic-gate 	int connected = 0;
3807c478bd9Sstevel@tonic-gate 	vnode_t *vp;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/* Return error if NFSv4 is the backfs (no caching) */
3837c478bd9Sstevel@tonic-gate 	if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
3847c478bd9Sstevel@tonic-gate 		goto out;
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	for (;;) {
3887c478bd9Sstevel@tonic-gate 		/* get access to the file system */
3897c478bd9Sstevel@tonic-gate 		error = cachefs_cd_access(fscp, connected, 0);
3907c478bd9Sstevel@tonic-gate 		if (error)
3917c478bd9Sstevel@tonic-gate 			break;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		/* lookup the file name */
3947c478bd9Sstevel@tonic-gate 		error = cachefs_lookup_common(dvp, name, &vp, NULL, 0, NULL,
3957c478bd9Sstevel@tonic-gate 		    cr);
3967c478bd9Sstevel@tonic-gate 		if (error == 0) {
3977c478bd9Sstevel@tonic-gate 			error = cachefs_unpack_common(vp);
3987c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 		if (CFS_TIMEOUT(fscp, error)) {
4017c478bd9Sstevel@tonic-gate 			if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
4027c478bd9Sstevel@tonic-gate 				cachefs_cd_release(fscp);
4037c478bd9Sstevel@tonic-gate 				cachefs_cd_timedout(fscp);
4047c478bd9Sstevel@tonic-gate 				connected = 0;
4057c478bd9Sstevel@tonic-gate 				continue;
4067c478bd9Sstevel@tonic-gate 			} else {
4077c478bd9Sstevel@tonic-gate 				cachefs_cd_release(fscp);
4087c478bd9Sstevel@tonic-gate 				connected = 1;
4097c478bd9Sstevel@tonic-gate 				continue;
4107c478bd9Sstevel@tonic-gate 			}
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 		cachefs_cd_release(fscp);
4137c478bd9Sstevel@tonic-gate 		break;
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate out:
4167c478bd9Sstevel@tonic-gate 	return (error);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * Unpacks the file belonging to the passed in vnode.
4217c478bd9Sstevel@tonic-gate  */
4227c478bd9Sstevel@tonic-gate static int
cachefs_unpack_common(vnode_t * vp)4237c478bd9Sstevel@tonic-gate cachefs_unpack_common(vnode_t *vp)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	cnode_t *cp = VTOC(vp);
4267c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(cp);
4277c478bd9Sstevel@tonic-gate 	int error = 0;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_statelock);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/* nothing to do if not packed */
4327c478bd9Sstevel@tonic-gate 	if ((cp->c_metadata.md_flags & MD_PACKED) == 0)
4337c478bd9Sstevel@tonic-gate 		goto out;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/* nothing to do if cannot modify cache */
4367c478bd9Sstevel@tonic-gate 	if ((cp->c_filegrp->fg_flags & CFS_FG_WRITE) == 0) {
4377c478bd9Sstevel@tonic-gate 		error = EROFS;
4387c478bd9Sstevel@tonic-gate 		goto out;
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	/* mark file as no longer packed */
4427c478bd9Sstevel@tonic-gate 	ASSERT(cp->c_metadata.md_rlno);
4437c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags &= ~MD_PACKED;
4447c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/* done if file has been modified */
4477c478bd9Sstevel@tonic-gate 	if (cp->c_metadata.md_rltype == CACHEFS_RL_MODIFIED)
4487c478bd9Sstevel@tonic-gate 		goto out;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* if there is no front file */
4517c478bd9Sstevel@tonic-gate 	if ((cp->c_metadata.md_flags & MD_FILE) == 0) {
4527c478bd9Sstevel@tonic-gate 		/* nuke front file resources */
4537c478bd9Sstevel@tonic-gate 		filegrp_ffrele(cp->c_filegrp);
4547c478bd9Sstevel@tonic-gate 		cachefs_rlent_moveto(fscp->fs_cache,
4557c478bd9Sstevel@tonic-gate 		    CACHEFS_RL_FREE, cp->c_metadata.md_rlno, 0);
4567c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_rlno = 0;
4577c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_rltype = CACHEFS_RL_NONE;
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	/* else move the front file to the active list */
4617c478bd9Sstevel@tonic-gate 	else {
4627c478bd9Sstevel@tonic-gate 		cachefs_rlent_moveto(fscp->fs_cache,
4637c478bd9Sstevel@tonic-gate 		    CACHEFS_RL_ACTIVE, cp->c_metadata.md_rlno,
4647c478bd9Sstevel@tonic-gate 		    cp->c_metadata.md_frontblks);
4657c478bd9Sstevel@tonic-gate 		cp->c_metadata.md_rltype = CACHEFS_RL_ACTIVE;
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate out:
4697c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->c_statelock);
4707c478bd9Sstevel@tonic-gate 	return (error);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * Returns packing information on a file.
4757c478bd9Sstevel@tonic-gate  *	dvp is the directory the file resides in.
4767c478bd9Sstevel@tonic-gate  *	name is the name of the file.
4777c478bd9Sstevel@tonic-gate  *	*statusp is set to the status of the file
4787c478bd9Sstevel@tonic-gate  *	Returns 0 or an error if could not perform the operation.
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate int
cachefs_packinfo(struct vnode * dvp,char * name,int * statusp,cred_t * cr)4817c478bd9Sstevel@tonic-gate cachefs_packinfo(struct vnode *dvp, char *name, int *statusp, cred_t *cr)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(dvp));
4847c478bd9Sstevel@tonic-gate 	struct vnode *vp;
4857c478bd9Sstevel@tonic-gate 	struct cnode *cp;
4867c478bd9Sstevel@tonic-gate 	int error;
4877c478bd9Sstevel@tonic-gate 	int connected = 0;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	*statusp = 0;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/*
4927c478bd9Sstevel@tonic-gate 	 * Return if NFSv4 is the backfs (no caching).
4937c478bd9Sstevel@tonic-gate 	 */
4947c478bd9Sstevel@tonic-gate 	if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
4957c478bd9Sstevel@tonic-gate 		goto out;
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	for (;;) {
4997c478bd9Sstevel@tonic-gate 		/* get access to the file system */
5007c478bd9Sstevel@tonic-gate 		error = cachefs_cd_access(fscp, connected, 0);
5017c478bd9Sstevel@tonic-gate 		if (error)
5027c478bd9Sstevel@tonic-gate 			break;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 		/* lookup the file name */
5057c478bd9Sstevel@tonic-gate 		error = cachefs_lookup_common(dvp, name, &vp, NULL, 0, NULL,
5067c478bd9Sstevel@tonic-gate 		    cr);
5077c478bd9Sstevel@tonic-gate 		if (CFS_TIMEOUT(fscp, error)) {
5087c478bd9Sstevel@tonic-gate 			if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
5097c478bd9Sstevel@tonic-gate 				cachefs_cd_release(fscp);
5107c478bd9Sstevel@tonic-gate 				cachefs_cd_timedout(fscp);
5117c478bd9Sstevel@tonic-gate 				connected = 0;
5127c478bd9Sstevel@tonic-gate 				continue;
5137c478bd9Sstevel@tonic-gate 			} else {
5147c478bd9Sstevel@tonic-gate 				cachefs_cd_release(fscp);
5157c478bd9Sstevel@tonic-gate 				connected = 1;
5167c478bd9Sstevel@tonic-gate 				continue;
5177c478bd9Sstevel@tonic-gate 			}
5187c478bd9Sstevel@tonic-gate 		}
5197c478bd9Sstevel@tonic-gate 		if (error)
5207c478bd9Sstevel@tonic-gate 			break;
5217c478bd9Sstevel@tonic-gate 		cp = VTOC(vp);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->c_statelock);
5247c478bd9Sstevel@tonic-gate 		if (cp->c_metadata.md_flags & MD_PACKED)
5257c478bd9Sstevel@tonic-gate 			*statusp |= CACHEFS_PACKED_FILE;
5267c478bd9Sstevel@tonic-gate 		if (cp->c_metadata.md_flags & (MD_POPULATED | MD_FASTSYMLNK))
5277c478bd9Sstevel@tonic-gate 			*statusp |= CACHEFS_PACKED_DATA;
5287c478bd9Sstevel@tonic-gate 		else if ((vp->v_type != VREG) &&
5297c478bd9Sstevel@tonic-gate 		    (vp->v_type != VDIR) &&
5307c478bd9Sstevel@tonic-gate 		    (vp->v_type != VLNK))
5317c478bd9Sstevel@tonic-gate 			*statusp |= CACHEFS_PACKED_DATA;
5327c478bd9Sstevel@tonic-gate 		else if (cp->c_size == 0)
5337c478bd9Sstevel@tonic-gate 			*statusp |= CACHEFS_PACKED_DATA;
5347c478bd9Sstevel@tonic-gate 		if (cp->c_flags & CN_NOCACHE)
5357c478bd9Sstevel@tonic-gate 			*statusp |= CACHEFS_PACKED_NOCACHE;
5367c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
5397c478bd9Sstevel@tonic-gate 		cachefs_cd_release(fscp);
5407c478bd9Sstevel@tonic-gate 		break;
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate out:
5447c478bd9Sstevel@tonic-gate 	return (error);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate /*
5487c478bd9Sstevel@tonic-gate  * Finds all packed files in the cache and unpacks them.
5497c478bd9Sstevel@tonic-gate  */
5507c478bd9Sstevel@tonic-gate int
cachefs_unpackall(vnode_t * vp)5517c478bd9Sstevel@tonic-gate cachefs_unpackall(vnode_t *vp)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
5547c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
5557c478bd9Sstevel@tonic-gate 	int error;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	/*
5587c478bd9Sstevel@tonic-gate 	 * Return if NFSv4 is the backfs (no caching).
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
5617c478bd9Sstevel@tonic-gate 		goto out;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	error = cachefs_unpackall_list(cachep, CACHEFS_RL_PACKED);
5657c478bd9Sstevel@tonic-gate 	if (error)
5667c478bd9Sstevel@tonic-gate 		goto out;
5677c478bd9Sstevel@tonic-gate 	error = cachefs_unpackall_list(cachep, CACHEFS_RL_PACKED_PENDING);
5687c478bd9Sstevel@tonic-gate out:
5697c478bd9Sstevel@tonic-gate 	return (error);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*
5737c478bd9Sstevel@tonic-gate  * Finds all packed files on the specified list and unpacks them.
5747c478bd9Sstevel@tonic-gate  */
5757c478bd9Sstevel@tonic-gate static int
cachefs_unpackall_list(cachefscache_t * cachep,enum cachefs_rl_type type)5767c478bd9Sstevel@tonic-gate cachefs_unpackall_list(cachefscache_t *cachep, enum cachefs_rl_type type)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	fscache_t *fscp = NULL;
5797c478bd9Sstevel@tonic-gate 	cnode_t *cp;
5807c478bd9Sstevel@tonic-gate 	int error = 0;
5817c478bd9Sstevel@tonic-gate 	rl_entry_t rl_ent;
5827c478bd9Sstevel@tonic-gate 	cfs_cid_t cid;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	rl_ent.rl_current = type;
5857c478bd9Sstevel@tonic-gate 	for (;;) {
5867c478bd9Sstevel@tonic-gate 		/* get the next entry on the specified resource list */
5877c478bd9Sstevel@tonic-gate 		error = cachefs_rlent_data(cachep, &rl_ent, NULL);
5887c478bd9Sstevel@tonic-gate 		if (error) {
5897c478bd9Sstevel@tonic-gate 			error = 0;
5907c478bd9Sstevel@tonic-gate 			break;
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		/* if the fscp we have does not match */
5947c478bd9Sstevel@tonic-gate 		if ((fscp == NULL) || (fscp->fs_cfsid != rl_ent.rl_fsid)) {
5957c478bd9Sstevel@tonic-gate 			if (fscp) {
5967c478bd9Sstevel@tonic-gate 				cachefs_cd_release(fscp);
5977c478bd9Sstevel@tonic-gate 				fscache_rele(fscp);
5987c478bd9Sstevel@tonic-gate 				fscp = NULL;
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 			/* get the file system cache object for this fsid */
6027c478bd9Sstevel@tonic-gate 			mutex_enter(&cachep->c_fslistlock);
6037c478bd9Sstevel@tonic-gate 			fscp = fscache_list_find(cachep, rl_ent.rl_fsid);
6047c478bd9Sstevel@tonic-gate 			if (fscp == NULL) {
6057c478bd9Sstevel@tonic-gate 				fscp = fscache_create(cachep);
6067c478bd9Sstevel@tonic-gate 				error = fscache_activate(fscp, rl_ent.rl_fsid,
6077c478bd9Sstevel@tonic-gate 				    NULL, NULL, 0);
6087c478bd9Sstevel@tonic-gate 				if (error) {
6097c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN,
6107c478bd9Sstevel@tonic-gate 					    "cachefs: cache error, run fsck\n");
6117c478bd9Sstevel@tonic-gate 					fscache_destroy(fscp);
6127c478bd9Sstevel@tonic-gate 					fscp = NULL;
6137c478bd9Sstevel@tonic-gate 					mutex_exit(&cachep->c_fslistlock);
6147c478bd9Sstevel@tonic-gate 					break;
6157c478bd9Sstevel@tonic-gate 				}
6167c478bd9Sstevel@tonic-gate 				fscache_list_add(cachep, fscp);
6177c478bd9Sstevel@tonic-gate 			}
6187c478bd9Sstevel@tonic-gate 			fscache_hold(fscp);
6197c478bd9Sstevel@tonic-gate 			mutex_exit(&cachep->c_fslistlock);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 			/* get access to the file system */
6227c478bd9Sstevel@tonic-gate 			error = cachefs_cd_access(fscp, 0, 0);
6237c478bd9Sstevel@tonic-gate 			if (error) {
6247c478bd9Sstevel@tonic-gate 				fscache_rele(fscp);
6257c478bd9Sstevel@tonic-gate 				fscp = NULL;
6267c478bd9Sstevel@tonic-gate 				break;
6277c478bd9Sstevel@tonic-gate 			}
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		/* get the cnode for the file */
6317c478bd9Sstevel@tonic-gate 		cid.cid_fileno = rl_ent.rl_fileno;
6327c478bd9Sstevel@tonic-gate 		cid.cid_flags = rl_ent.rl_local ? CFS_CID_LOCAL : 0;
6337c478bd9Sstevel@tonic-gate 		error = cachefs_cnode_make(&cid, fscp,
6347c478bd9Sstevel@tonic-gate 		    NULL, NULL, NULL, kcred, 0, &cp);
6357c478bd9Sstevel@tonic-gate 		if (error) {
6367c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
6377c478bd9Sstevel@tonic-gate 			CFS_DEBUG(CFSDEBUG_IOCTL)
6387c478bd9Sstevel@tonic-gate 				printf("cachefs: cul: could not find %llu\n",
6397c478bd9Sstevel@tonic-gate 				    (u_longlong_t)cid.cid_fileno);
6407c478bd9Sstevel@tonic-gate 			delay(5*hz);
6417c478bd9Sstevel@tonic-gate #endif
6427c478bd9Sstevel@tonic-gate 			continue;
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		/* unpack the file */
6467c478bd9Sstevel@tonic-gate 		(void) cachefs_unpack_common(CTOV(cp));
6477c478bd9Sstevel@tonic-gate 		VN_RELE(CTOV(cp));
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/* free up allocated resources */
6517c478bd9Sstevel@tonic-gate 	if (fscp) {
6527c478bd9Sstevel@tonic-gate 		cachefs_cd_release(fscp);
6537c478bd9Sstevel@tonic-gate 		fscache_rele(fscp);
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 	return (error);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate /*
6597c478bd9Sstevel@tonic-gate  * Identifies this process as the cachefsd.
6607c478bd9Sstevel@tonic-gate  * Stays this way until close is done.
6617c478bd9Sstevel@tonic-gate  */
6627c478bd9Sstevel@tonic-gate int
6637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
cachefs_io_daemonid(vnode_t * vp,void * dinp,void * doutp)6647c478bd9Sstevel@tonic-gate cachefs_io_daemonid(vnode_t *vp, void *dinp, void *doutp)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	int error = 0;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
6697c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	mutex_enter(&fscp->fs_cdlock);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/* can only do this on the root of the file system */
6747c478bd9Sstevel@tonic-gate 	if (vp != fscp->fs_rootvp)
6757c478bd9Sstevel@tonic-gate 		error = ENOENT;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/* else if there already is a daemon running */
6787c478bd9Sstevel@tonic-gate 	else if (fscp->fs_cddaemonid)
6797c478bd9Sstevel@tonic-gate 		error = EBUSY;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	/* else use the pid to identify the daemon */
6827c478bd9Sstevel@tonic-gate 	else {
6837c478bd9Sstevel@tonic-gate 		fscp->fs_cddaemonid = ttoproc(curthread)->p_pid;
6847c478bd9Sstevel@tonic-gate 		cv_broadcast(&fscp->fs_cdwaitcv);
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	mutex_exit(&fscp->fs_cdlock);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	if (error == 0) {
6907c478bd9Sstevel@tonic-gate 		/* the daemon that takes care of root is special */
6917c478bd9Sstevel@tonic-gate 		if (fscp->fs_flags & CFS_FS_ROOTFS) {
6927c478bd9Sstevel@tonic-gate 			mutex_enter(&cachep->c_contentslock);
6937c478bd9Sstevel@tonic-gate 			ASSERT(cachep->c_rootdaemonid == 0);
6947c478bd9Sstevel@tonic-gate 			cachep->c_rootdaemonid = fscp->fs_cddaemonid;
6957c478bd9Sstevel@tonic-gate 			mutex_exit(&cachep->c_contentslock);
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 	return (error);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * Returns the current state in doutp
7037c478bd9Sstevel@tonic-gate  */
7047c478bd9Sstevel@tonic-gate int
7057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
cachefs_io_stateget(vnode_t * vp,void * dinp,void * doutp)7067c478bd9Sstevel@tonic-gate cachefs_io_stateget(vnode_t *vp, void *dinp, void *doutp)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	fscache_t  *fscp = C_TO_FSCACHE(VTOC(vp));
7097c478bd9Sstevel@tonic-gate 	int *statep = (int *)doutp;
7107c478bd9Sstevel@tonic-gate 	int state;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	/*
7137c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
7147c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
7157c478bd9Sstevel@tonic-gate 	 */
7167c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	mutex_enter(&fscp->fs_cdlock);
7197c478bd9Sstevel@tonic-gate 	switch (fscp->fs_cdconnected) {
7207c478bd9Sstevel@tonic-gate 	case CFS_CD_CONNECTED:
7217c478bd9Sstevel@tonic-gate 		state = CFS_FS_CONNECTED;
7227c478bd9Sstevel@tonic-gate 		break;
7237c478bd9Sstevel@tonic-gate 	case CFS_CD_DISCONNECTED:
7247c478bd9Sstevel@tonic-gate 		state = CFS_FS_DISCONNECTED;
7257c478bd9Sstevel@tonic-gate 		break;
7267c478bd9Sstevel@tonic-gate 	case CFS_CD_RECONNECTING:
7277c478bd9Sstevel@tonic-gate 		state = CFS_FS_RECONNECTING;
7287c478bd9Sstevel@tonic-gate 		break;
7297c478bd9Sstevel@tonic-gate 	default:
7307c478bd9Sstevel@tonic-gate 		ASSERT(0);
7317c478bd9Sstevel@tonic-gate 		break;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	mutex_exit(&fscp->fs_cdlock);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	*statep = state;
7367c478bd9Sstevel@tonic-gate 	return (0);
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate /*
7407c478bd9Sstevel@tonic-gate  * Sets the state of the file system.
7417c478bd9Sstevel@tonic-gate  */
7427c478bd9Sstevel@tonic-gate int
7437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
cachefs_io_stateset(vnode_t * vp,void * dinp,void * doutp)7447c478bd9Sstevel@tonic-gate cachefs_io_stateset(vnode_t *vp, void *dinp, void *doutp)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
7477c478bd9Sstevel@tonic-gate 	int error = 0;
7487c478bd9Sstevel@tonic-gate 	int nosig = 1;
7497c478bd9Sstevel@tonic-gate 	int state = *(int *)dinp;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/*
7527c478bd9Sstevel@tonic-gate 	 * State should not be changeable and always be connected if
7537c478bd9Sstevel@tonic-gate 	 * NFSv4 is in use.
7547c478bd9Sstevel@tonic-gate 	 */
7557c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	/* wait until the file system is quiet */
7587c478bd9Sstevel@tonic-gate 	mutex_enter(&fscp->fs_cdlock);
7597c478bd9Sstevel@tonic-gate 	if (fscp->fs_cdtransition == 1) {
7607c478bd9Sstevel@tonic-gate 		/* if someone is already changing the state */
7617c478bd9Sstevel@tonic-gate 		mutex_exit(&fscp->fs_cdlock);
7627c478bd9Sstevel@tonic-gate 		return (0);
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	fscp->fs_cdtransition = 1;
7657c478bd9Sstevel@tonic-gate 	while (nosig && (fscp->fs_cdrefcnt != 0)) {
7667c478bd9Sstevel@tonic-gate 		nosig = cv_wait_sig(&fscp->fs_cdwaitcv, &fscp->fs_cdlock);
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 	if (!nosig) {
7697c478bd9Sstevel@tonic-gate 		fscp->fs_cdtransition = 0;
7707c478bd9Sstevel@tonic-gate 		cv_broadcast(&fscp->fs_cdwaitcv);
7717c478bd9Sstevel@tonic-gate 		mutex_exit(&fscp->fs_cdlock);
7727c478bd9Sstevel@tonic-gate 		return (EINTR);
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 	mutex_exit(&fscp->fs_cdlock);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	switch (state) {
7777c478bd9Sstevel@tonic-gate 	case CFS_FS_CONNECTED:
7787c478bd9Sstevel@tonic-gate 		/* done if already in this state */
7797c478bd9Sstevel@tonic-gate 		if (fscp->fs_cdconnected == CFS_CD_CONNECTED)
7807c478bd9Sstevel@tonic-gate 			break;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		mutex_enter(&fscp->fs_cdlock);
7837c478bd9Sstevel@tonic-gate 		fscp->fs_cdconnected = CFS_CD_CONNECTED;
7847c478bd9Sstevel@tonic-gate 		mutex_exit(&fscp->fs_cdlock);
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		/* fix up modified files */
7877c478bd9Sstevel@tonic-gate 		cachefs_modified_fix(fscp);
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate #if 0
7907c478bd9Sstevel@tonic-gate 		if (fscp->fs_hostname != NULL)
7917c478bd9Sstevel@tonic-gate 			printf("\ncachefs:server          - %s",
7927c478bd9Sstevel@tonic-gate 			    fscp->fs_hostname);
7937c478bd9Sstevel@tonic-gate 		if (fscp->fs_mntpt != NULL)
7947c478bd9Sstevel@tonic-gate 			printf("\ncachefs:mount point     - %s",
7957c478bd9Sstevel@tonic-gate 			    fscp->fs_mntpt);
7967c478bd9Sstevel@tonic-gate 		if (fscp->fs_backfsname != NULL)
7977c478bd9Sstevel@tonic-gate 			printf("\ncachefs:back filesystem - %s",
7987c478bd9Sstevel@tonic-gate 			    fscp->fs_backfsname);
7997c478bd9Sstevel@tonic-gate 		printf("\nok\n");
8007c478bd9Sstevel@tonic-gate #else
8017c478bd9Sstevel@tonic-gate 		if (fscp->fs_hostname && fscp->fs_backfsname)
8027c478bd9Sstevel@tonic-gate 			printf("cachefs: %s:%s ok\n",
8037c478bd9Sstevel@tonic-gate 			    fscp->fs_hostname, fscp->fs_backfsname);
8047c478bd9Sstevel@tonic-gate 		else
8057c478bd9Sstevel@tonic-gate 			printf("cachefs: server ok\n");
8067c478bd9Sstevel@tonic-gate #endif
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		/* allow deletion of renamed open files to proceed */
8097c478bd9Sstevel@tonic-gate 		cachefs_cnode_traverse(fscp, allow_pendrm);
8107c478bd9Sstevel@tonic-gate 		break;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	case CFS_FS_DISCONNECTED:
8137c478bd9Sstevel@tonic-gate 		/* done if already in this state */
8147c478bd9Sstevel@tonic-gate 		if (fscp->fs_cdconnected == CFS_CD_DISCONNECTED)
8157c478bd9Sstevel@tonic-gate 			break;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		/* drop all back vps */
8187c478bd9Sstevel@tonic-gate 		cachefs_cnode_traverse(fscp, drop_backvp);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 		mutex_enter(&fscp->fs_cdlock);
8227c478bd9Sstevel@tonic-gate 		fscp->fs_cdconnected = CFS_CD_DISCONNECTED;
8237c478bd9Sstevel@tonic-gate 		mutex_exit(&fscp->fs_cdlock);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate #if 0
8267c478bd9Sstevel@tonic-gate 		if (fscp->fs_hostname != NULL)
8277c478bd9Sstevel@tonic-gate 			printf("\ncachefs:server          - %s",
8287c478bd9Sstevel@tonic-gate 			    fscp->fs_hostname);
8297c478bd9Sstevel@tonic-gate 		if (fscp->fs_mntpt != NULL)
8307c478bd9Sstevel@tonic-gate 			printf("\ncachefs:mount point     - %s",
8317c478bd9Sstevel@tonic-gate 			    fscp->fs_mntpt);
8327c478bd9Sstevel@tonic-gate 		if (fscp->fs_backfsname != NULL)
8337c478bd9Sstevel@tonic-gate 			printf("\ncachefs:back filesystem - %s",
8347c478bd9Sstevel@tonic-gate 			    fscp->fs_backfsname);
8357c478bd9Sstevel@tonic-gate 		printf("\nnot responding still trying\n");
8367c478bd9Sstevel@tonic-gate #else
8377c478bd9Sstevel@tonic-gate 		if (fscp->fs_hostname && fscp->fs_backfsname)
8387c478bd9Sstevel@tonic-gate 			printf("cachefs: %s:%s not responding still trying\n",
8397c478bd9Sstevel@tonic-gate 			    fscp->fs_hostname, fscp->fs_backfsname);
8407c478bd9Sstevel@tonic-gate 		else
8417c478bd9Sstevel@tonic-gate 			printf("cachefs: server not responding still trying\n");
8427c478bd9Sstevel@tonic-gate #endif
8437c478bd9Sstevel@tonic-gate 		break;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	case CFS_FS_RECONNECTING:
8467c478bd9Sstevel@tonic-gate 		/* done if already in this state */
8477c478bd9Sstevel@tonic-gate 		if (fscp->fs_cdconnected == CFS_CD_RECONNECTING)
8487c478bd9Sstevel@tonic-gate 			break;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		/*
8517c478bd9Sstevel@tonic-gate 		 * Before we enter disconnected state we sync all metadata,
8527c478bd9Sstevel@tonic-gate 		 * this allows us to read metadata directly in subsequent
8537c478bd9Sstevel@tonic-gate 		 * calls so we don't need to allocate cnodes when
8547c478bd9Sstevel@tonic-gate 		 * we just need metadata information.
8557c478bd9Sstevel@tonic-gate 		 */
8567c478bd9Sstevel@tonic-gate 		/* XXX bob: need to eliminate this */
8577c478bd9Sstevel@tonic-gate 		cachefs_cnode_traverse(fscp, sync_metadata);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 		mutex_enter(&fscp->fs_cdlock);
8607c478bd9Sstevel@tonic-gate 		fscp->fs_cdconnected = CFS_CD_RECONNECTING;
8617c478bd9Sstevel@tonic-gate 		mutex_exit(&fscp->fs_cdlock);
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		/* no longer need dlog active */
8647c478bd9Sstevel@tonic-gate 		cachefs_dlog_teardown(fscp);
8657c478bd9Sstevel@tonic-gate 		break;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	default:
8687c478bd9Sstevel@tonic-gate 		error = ENOTTY;
8697c478bd9Sstevel@tonic-gate 		break;
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	mutex_enter(&fscp->fs_cdlock);
8737c478bd9Sstevel@tonic-gate 	fscp->fs_cdtransition = 0;
8747c478bd9Sstevel@tonic-gate 	cv_broadcast(&fscp->fs_cdwaitcv);
8757c478bd9Sstevel@tonic-gate 	mutex_exit(&fscp->fs_cdlock);
8767c478bd9Sstevel@tonic-gate 	return (error);
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate /*
8807c478bd9Sstevel@tonic-gate  * Blocks until the file system switches
8817c478bd9Sstevel@tonic-gate  * out of the connected state.
8827c478bd9Sstevel@tonic-gate  */
8837c478bd9Sstevel@tonic-gate int
8847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
cachefs_io_xwait(vnode_t * vp,void * dinp,void * doutp)8857c478bd9Sstevel@tonic-gate cachefs_io_xwait(vnode_t *vp, void *dinp, void *doutp)
8867c478bd9Sstevel@tonic-gate {
8877c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
8887c478bd9Sstevel@tonic-gate 	int nosig = 1;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
8927c478bd9Sstevel@tonic-gate 	 * that this is not used when NFSv4 is the backfilesytem.
8937c478bd9Sstevel@tonic-gate 	 */
8947c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	mutex_enter(&fscp->fs_cdlock);
8977c478bd9Sstevel@tonic-gate 	while (nosig &&
8987c478bd9Sstevel@tonic-gate 	    (fscp->fs_cdconnected == CFS_CD_CONNECTED)) {
8997c478bd9Sstevel@tonic-gate 		nosig = cv_wait_sig(&fscp->fs_cdwaitcv, &fscp->fs_cdlock);
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 	mutex_exit(&fscp->fs_cdlock);
9027c478bd9Sstevel@tonic-gate 	if (!nosig)
9037c478bd9Sstevel@tonic-gate 		return (EINTR);
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	return (0);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate #define	RL_HEAD(cachep, type) \
9097c478bd9Sstevel@tonic-gate 	(&(cachep->c_rlinfo.rl_items[CACHEFS_RL_INDEX(type)]))
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate /*
9127c478bd9Sstevel@tonic-gate  * Returns some statistics about the cache.
9137c478bd9Sstevel@tonic-gate  */
9147c478bd9Sstevel@tonic-gate #define	CFS_STAT_FACTOR		(MAXBSIZE / 1024)
9157c478bd9Sstevel@tonic-gate int
9167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
cachefs_io_getstats(vnode_t * vp,void * dinp,void * doutp)9177c478bd9Sstevel@tonic-gate cachefs_io_getstats(vnode_t *vp, void *dinp, void *doutp)
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
9207c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
9217c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
9227c478bd9Sstevel@tonic-gate 	fsblkcnt64_t avail = 0;
9237c478bd9Sstevel@tonic-gate 	fsblkcnt64_t blocks;
9247c478bd9Sstevel@tonic-gate 	int error;
9257c478bd9Sstevel@tonic-gate 	cachefsio_getstats_t *gsp = (cachefsio_getstats_t *)doutp;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/* determine number of blocks available to the cache */
9287c478bd9Sstevel@tonic-gate 	error = VFS_STATVFS(cachep->c_dirvp->v_vfsp, &sb);
9297c478bd9Sstevel@tonic-gate 	if (error == 0) {
9307c478bd9Sstevel@tonic-gate 		blocks = (fsblkcnt64_t)(cachep->c_label.cl_maxblks -
9317c478bd9Sstevel@tonic-gate 		    cachep->c_usage.cu_blksused);
9327c478bd9Sstevel@tonic-gate 		if ((longlong_t)blocks < (longlong_t)0)
9337c478bd9Sstevel@tonic-gate 			blocks = (fsblkcnt64_t)0;
9347c478bd9Sstevel@tonic-gate 		avail = (sb.f_bfree * sb.f_frsize) / MAXBSIZE;
9357c478bd9Sstevel@tonic-gate 		if (blocks < avail)
9367c478bd9Sstevel@tonic-gate 			avail = blocks;
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	gsp->gs_total = cachep->c_usage.cu_blksused * CFS_STAT_FACTOR;
9407c478bd9Sstevel@tonic-gate 	gsp->gs_gc = RL_HEAD(cachep, CACHEFS_RL_GC)->rli_blkcnt *
9417c478bd9Sstevel@tonic-gate 		CFS_STAT_FACTOR;
9427c478bd9Sstevel@tonic-gate 	gsp->gs_active = RL_HEAD(cachep, CACHEFS_RL_ACTIVE)->rli_blkcnt *
9437c478bd9Sstevel@tonic-gate 		CFS_STAT_FACTOR;
9447c478bd9Sstevel@tonic-gate 	gsp->gs_packed = RL_HEAD(cachep, CACHEFS_RL_PACKED)->rli_blkcnt *
9457c478bd9Sstevel@tonic-gate 		CFS_STAT_FACTOR;
9467c478bd9Sstevel@tonic-gate 	gsp->gs_free = (long)(avail * CFS_STAT_FACTOR);
9477c478bd9Sstevel@tonic-gate 	gsp->gs_gctime = cachep->c_rlinfo.rl_gctime;
9487c478bd9Sstevel@tonic-gate 	return (0);
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate  * This looks to see if the specified file exists in the cache.
9537c478bd9Sstevel@tonic-gate  * 	0 is returned if it exists
9547c478bd9Sstevel@tonic-gate  *	ENOENT is returned if it doesn't exist.
9557c478bd9Sstevel@tonic-gate  */
9567c478bd9Sstevel@tonic-gate int
9577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
cachefs_io_exists(vnode_t * vp,void * dinp,void * doutp)9587c478bd9Sstevel@tonic-gate cachefs_io_exists(vnode_t *vp, void *dinp, void *doutp)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 	fscache_t  *fscp = C_TO_FSCACHE(VTOC(vp));
9617c478bd9Sstevel@tonic-gate 	cnode_t *cp = NULL;
9627c478bd9Sstevel@tonic-gate 	int error;
9637c478bd9Sstevel@tonic-gate 	cfs_cid_t *cidp = (cfs_cid_t *)dinp;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/*
9667c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
9677c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
9687c478bd9Sstevel@tonic-gate 	 */
9697c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	/* find the cnode of the file */
9727c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(cidp, fscp,
9737c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, kcred, 0, &cp);
9747c478bd9Sstevel@tonic-gate 	if (error)
9757c478bd9Sstevel@tonic-gate 		return (ENOENT);
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	if ((cp->c_flags & (CN_DESTROY | CN_NOCACHE)) ||
9787c478bd9Sstevel@tonic-gate 	    !(cp->c_metadata.md_flags & (MD_POPULATED | MD_FASTSYMLNK)))
9797c478bd9Sstevel@tonic-gate 		error = ENOENT;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	VN_RELE(CTOV(cp));
9827c478bd9Sstevel@tonic-gate 	return	(error);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate /*
9877c478bd9Sstevel@tonic-gate  * Moves the specified file to the lost+found directory for the
9887c478bd9Sstevel@tonic-gate  * cached file system.
9897c478bd9Sstevel@tonic-gate  * Invalidates cached data and attributes.
9907c478bd9Sstevel@tonic-gate  * Returns 0 or an error if could not perform operation.
9917c478bd9Sstevel@tonic-gate  */
9927c478bd9Sstevel@tonic-gate int
cachefs_io_lostfound(vnode_t * vp,void * dinp,void * doutp)9937c478bd9Sstevel@tonic-gate cachefs_io_lostfound(vnode_t *vp, void *dinp, void *doutp)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	int error;
9967c478bd9Sstevel@tonic-gate 	cnode_t *cp = NULL;
9977c478bd9Sstevel@tonic-gate 	fscache_t *fscp;
9987c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep;
9997c478bd9Sstevel@tonic-gate 	cachefsio_lostfound_arg_t *lfp;
10007c478bd9Sstevel@tonic-gate 	cachefsio_lostfound_return_t *rp;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	lfp = (cachefsio_lostfound_arg_t *)dinp;
10037c478bd9Sstevel@tonic-gate 	rp = (cachefsio_lostfound_return_t *)doutp;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	fscp = C_TO_FSCACHE(VTOC(vp));
10067c478bd9Sstevel@tonic-gate 	cachep = fscp->fs_cache;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	ASSERT((cachep->c_flags & (CACHE_NOCACHE|CACHE_NOFILL)) == 0);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
10127c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
10137c478bd9Sstevel@tonic-gate 	 */
10147c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/* find the cnode of the file */
10177c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(&lfp->lf_cid, fscp,
10187c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, kcred, 0, &cp);
10197c478bd9Sstevel@tonic-gate 	if (error) {
10207c478bd9Sstevel@tonic-gate 		error = ENOENT;
10217c478bd9Sstevel@tonic-gate 		goto out;
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_statelock);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	/* must be regular file and modified */
10277c478bd9Sstevel@tonic-gate 	if ((cp->c_attr.va_type != VREG) ||
10287c478bd9Sstevel@tonic-gate 	    (cp->c_metadata.md_rltype != CACHEFS_RL_MODIFIED)) {
10297c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
10307c478bd9Sstevel@tonic-gate 		error = EINVAL;
10317c478bd9Sstevel@tonic-gate 		goto out;
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	/* move to lost+found */
10357c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_lostfound(cp, lfp->lf_name);
10367c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->c_statelock);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	if (error == 0)
10397c478bd9Sstevel@tonic-gate 		(void) strcpy(rp->lf_name, lfp->lf_name);
10407c478bd9Sstevel@tonic-gate out:
10417c478bd9Sstevel@tonic-gate 	if (cp)
10427c478bd9Sstevel@tonic-gate 		VN_RELE(CTOV(cp));
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	return (error);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate /*
10487c478bd9Sstevel@tonic-gate  * Given a cid, returns info about the file in the cache.
10497c478bd9Sstevel@tonic-gate  */
10507c478bd9Sstevel@tonic-gate int
cachefs_io_getinfo(vnode_t * vp,void * dinp,void * doutp)10517c478bd9Sstevel@tonic-gate cachefs_io_getinfo(vnode_t *vp, void *dinp, void *doutp)
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
10547c478bd9Sstevel@tonic-gate 	struct cnode *dcp = NULL;
10557c478bd9Sstevel@tonic-gate 	struct cnode *cp = NULL;
10567c478bd9Sstevel@tonic-gate 	struct vattr va;
10577c478bd9Sstevel@tonic-gate 	u_offset_t blockoff = 0;
10587c478bd9Sstevel@tonic-gate 	struct fbuf *fbp;
10597c478bd9Sstevel@tonic-gate 	int offset = 0;
10607c478bd9Sstevel@tonic-gate 	int error = 0;
10617c478bd9Sstevel@tonic-gate 	cfs_cid_t *fcidp;
10627c478bd9Sstevel@tonic-gate 	cachefsio_getinfo_t *infop;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	/*
10657c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
10667c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
10677c478bd9Sstevel@tonic-gate 	 */
10687c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	fcidp = (cfs_cid_t *)dinp;
10717c478bd9Sstevel@tonic-gate 	infop = (cachefsio_getinfo_t *)doutp;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	/* find the cnode of the file */
10747c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(fcidp, fscp, NULL, NULL, NULL,
10757c478bd9Sstevel@tonic-gate 	    kcred, 0, &cp);
10767c478bd9Sstevel@tonic-gate 	if (error) {
10777c478bd9Sstevel@tonic-gate 		error = ENOENT;
10787c478bd9Sstevel@tonic-gate 		goto out;
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	infop->gi_cid = *fcidp;
10827c478bd9Sstevel@tonic-gate 	infop->gi_modified = (cp->c_metadata.md_rltype == CACHEFS_RL_MODIFIED);
10837c478bd9Sstevel@tonic-gate 	CACHEFS_VATTR_TO_CFS_VATTR_COPY(&cp->c_attr, &infop->gi_attr, error);
10847c478bd9Sstevel@tonic-gate 	infop->gi_pcid = cp->c_metadata.md_parent;
10857c478bd9Sstevel@tonic-gate 	infop->gi_name[0] = '\0';
10867c478bd9Sstevel@tonic-gate 	infop->gi_seq = cp->c_metadata.md_seq;
10877c478bd9Sstevel@tonic-gate 	if (error || (cp->c_metadata.md_parent.cid_fileno == 0))
10887c478bd9Sstevel@tonic-gate 		goto out;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	/* try to get the cnode of the parent dir */
10917c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(&cp->c_metadata.md_parent, fscp,
10927c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, kcred, 0, &dcp);
10937c478bd9Sstevel@tonic-gate 	if (error) {
10947c478bd9Sstevel@tonic-gate 		error = 0;
10957c478bd9Sstevel@tonic-gate 		goto out;
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	/* make sure a directory and populated */
10997c478bd9Sstevel@tonic-gate 	if ((((dcp->c_flags & CN_ASYNC_POPULATE) == 0) ||
11007c478bd9Sstevel@tonic-gate 	    ((dcp->c_metadata.md_flags & MD_POPULATED) == 0)) &&
11017c478bd9Sstevel@tonic-gate 	    (CTOV(dcp)->v_type == VDIR)) {
11027c478bd9Sstevel@tonic-gate 		error = 0;
11037c478bd9Sstevel@tonic-gate 		goto out;
11047c478bd9Sstevel@tonic-gate 	}
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	/* get the front file */
11077c478bd9Sstevel@tonic-gate 	if (dcp->c_frontvp == NULL) {
11087c478bd9Sstevel@tonic-gate 		mutex_enter(&dcp->c_statelock);
11097c478bd9Sstevel@tonic-gate 		error = cachefs_getfrontfile(dcp);
11107c478bd9Sstevel@tonic-gate 		mutex_exit(&dcp->c_statelock);
11117c478bd9Sstevel@tonic-gate 		if (error) {
11127c478bd9Sstevel@tonic-gate 			error = 0;
11137c478bd9Sstevel@tonic-gate 			goto out;
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 		/* make sure frontvp is still populated */
11177c478bd9Sstevel@tonic-gate 		if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
11187c478bd9Sstevel@tonic-gate 			error = 0;
11197c478bd9Sstevel@tonic-gate 			goto out;
11207c478bd9Sstevel@tonic-gate 		}
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	/* Get the length of the directory */
11247c478bd9Sstevel@tonic-gate 	va.va_mask = AT_SIZE;
1125*da6c28aaSamw 	error = VOP_GETATTR(dcp->c_frontvp, &va, 0, kcred, NULL);
11267c478bd9Sstevel@tonic-gate 	if (error) {
11277c478bd9Sstevel@tonic-gate 		error = 0;
11287c478bd9Sstevel@tonic-gate 		goto out;
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	/* XXX bob: change this to use cachfs_dir_read */
11327c478bd9Sstevel@tonic-gate 	/* We have found the parent, now we open the dir and look for file */
11337c478bd9Sstevel@tonic-gate 	while (blockoff < va.va_size) {
11347c478bd9Sstevel@tonic-gate 		offset = 0;
11357c478bd9Sstevel@tonic-gate 		error = fbread(dcp->c_frontvp, (offset_t)blockoff, MAXBSIZE,
11367c478bd9Sstevel@tonic-gate 						S_OTHER, &fbp);
11377c478bd9Sstevel@tonic-gate 		if (error)
11387c478bd9Sstevel@tonic-gate 			goto out;
11397c478bd9Sstevel@tonic-gate 		while (offset < MAXBSIZE && (blockoff + offset) < va.va_size) {
11407c478bd9Sstevel@tonic-gate 			struct c_dirent	*dep;
11417c478bd9Sstevel@tonic-gate 			dep = (struct c_dirent *)((uintptr_t)fbp->fb_addr +
11427c478bd9Sstevel@tonic-gate 									offset);
11437c478bd9Sstevel@tonic-gate 			if ((dep->d_flag & CDE_VALID) &&
11447c478bd9Sstevel@tonic-gate 			    (bcmp(&dep->d_id, &infop->gi_cid,
11457c478bd9Sstevel@tonic-gate 			    sizeof (cfs_cid_t)) == 0)) {
11467c478bd9Sstevel@tonic-gate 				/* found the name */
11477c478bd9Sstevel@tonic-gate 				(void) strcpy(infop->gi_name, dep->d_name);
11487c478bd9Sstevel@tonic-gate 				fbrelse(fbp, S_OTHER);
11497c478bd9Sstevel@tonic-gate 				goto out;
11507c478bd9Sstevel@tonic-gate 			}
11517c478bd9Sstevel@tonic-gate 			offset += dep->d_length;
11527c478bd9Sstevel@tonic-gate 		}
11537c478bd9Sstevel@tonic-gate 		fbrelse(fbp, S_OTHER);
11547c478bd9Sstevel@tonic-gate 		fbp = NULL;
11557c478bd9Sstevel@tonic-gate 		blockoff += MAXBSIZE;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate out:
11597c478bd9Sstevel@tonic-gate 	if (cp)
11607c478bd9Sstevel@tonic-gate 		VN_RELE(CTOV(cp));
11617c478bd9Sstevel@tonic-gate 	if (dcp)
11627c478bd9Sstevel@tonic-gate 		VN_RELE(CTOV(dcp));
11637c478bd9Sstevel@tonic-gate 	return (error);
11647c478bd9Sstevel@tonic-gate }
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate /*
11677c478bd9Sstevel@tonic-gate  * Given a file number, this functions returns the fid
11687c478bd9Sstevel@tonic-gate  * for the back file system.
11697c478bd9Sstevel@tonic-gate  * Returns ENOENT if file does not exist.
11707c478bd9Sstevel@tonic-gate  * Returns ENOMSG if fid is not valid, ie: local file.
11717c478bd9Sstevel@tonic-gate  */
11727c478bd9Sstevel@tonic-gate int
cachefs_io_cidtofid(vnode_t * vp,void * dinp,void * doutp)11737c478bd9Sstevel@tonic-gate cachefs_io_cidtofid(vnode_t *vp, void *dinp, void *doutp)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
11767c478bd9Sstevel@tonic-gate 	cnode_t *cp = NULL;
11777c478bd9Sstevel@tonic-gate 	int error;
11787c478bd9Sstevel@tonic-gate 	cfs_cid_t *cidp = (cfs_cid_t *)dinp;
11797c478bd9Sstevel@tonic-gate 	cfs_fid_t *fidp = (cfs_fid_t *)doutp;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	/*
11827c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
11837c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
11847c478bd9Sstevel@tonic-gate 	 */
11857c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	/* get the cnode for the file */
11887c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(cidp, fscp, NULL, NULL, NULL, kcred, 0, &cp);
11897c478bd9Sstevel@tonic-gate 	if (error)
11907c478bd9Sstevel@tonic-gate 		goto out;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	/* if local file, fid is a local fid and is not valid */
11937c478bd9Sstevel@tonic-gate 	if (cp->c_id.cid_flags & CFS_CID_LOCAL) {
11947c478bd9Sstevel@tonic-gate 		error = ENOMSG;
11957c478bd9Sstevel@tonic-gate 		goto out;
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	/* copy out the fid */
11997c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPY(&cp->c_cookie, fidp);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate out:
12027c478bd9Sstevel@tonic-gate 	if (cp)
12037c478bd9Sstevel@tonic-gate 		VN_RELE(CTOV(cp));
12047c478bd9Sstevel@tonic-gate 	return	(error);
12057c478bd9Sstevel@tonic-gate }
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate /*
12087c478bd9Sstevel@tonic-gate  * This performs a getattr on the back file system given
12097c478bd9Sstevel@tonic-gate  * a fid that is passed in.
12107c478bd9Sstevel@tonic-gate  *
12117c478bd9Sstevel@tonic-gate  * The backfid is in gafid->cg_backfid, the creds to use for
12127c478bd9Sstevel@tonic-gate  * this operation are in gafid->cg_cred.  The attributes are
12137c478bd9Sstevel@tonic-gate  * returned in gafid->cg_attr
12147c478bd9Sstevel@tonic-gate  *
12157c478bd9Sstevel@tonic-gate  * the error returned is 0 if successful, nozero if not
12167c478bd9Sstevel@tonic-gate  */
12177c478bd9Sstevel@tonic-gate int
cachefs_io_getattrfid(vnode_t * vp,void * dinp,void * doutp)12187c478bd9Sstevel@tonic-gate cachefs_io_getattrfid(vnode_t *vp, void *dinp, void *doutp)
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate 	vnode_t	*backvp = NULL;
12217c478bd9Sstevel@tonic-gate 	fscache_t  *fscp = C_TO_FSCACHE(VTOC(vp));
12227c478bd9Sstevel@tonic-gate 	int error = 0;
12237c478bd9Sstevel@tonic-gate 	cred_t	*cr;
12247c478bd9Sstevel@tonic-gate 	cachefsio_getattrfid_t *gafid;
12257c478bd9Sstevel@tonic-gate 	fid_t	*tmpfidp;
12267c478bd9Sstevel@tonic-gate 	vattr_t *tmpvap;
12277c478bd9Sstevel@tonic-gate 	cfs_vattr_t *attrp;
12287c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(fid_t, tmpfid);
12297c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(vattr_t, va);
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	/*
12327c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
12337c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
12347c478bd9Sstevel@tonic-gate 	 */
12357c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	gafid = (cachefsio_getattrfid_t *)dinp;
12387c478bd9Sstevel@tonic-gate 	attrp = (cfs_vattr_t *)doutp;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	/* Get a vnode for the back file */
12417c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&gafid->cg_backfid, &tmpfid, tmpfidp, fid_t);
12427c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&gafid->cg_backfid, tmpfidp);
12437c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &backvp, tmpfidp);
12447c478bd9Sstevel@tonic-gate 	if (error)
12457c478bd9Sstevel@tonic-gate 		return (error);
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	cr = conj_cred(&gafid->cg_cred);
12487c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(attrp, &va, tmpvap, vattr_t);
12497c478bd9Sstevel@tonic-gate 	tmpvap->va_mask = AT_ALL;
1250*da6c28aaSamw 	error = VOP_GETATTR(backvp, tmpvap, 0, cr, NULL);
12517c478bd9Sstevel@tonic-gate 	CACHEFS_VATTR_COPYOUT(tmpvap, attrp, error);
12527c478bd9Sstevel@tonic-gate 	crfree(cr);
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	/* VFS_VGET performs a VN_HOLD on the vp */
12557c478bd9Sstevel@tonic-gate 	VN_RELE(backvp);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	return (error);
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate /*
12627c478bd9Sstevel@tonic-gate  * This performs a getattr on the back file system.  Instead of
12637c478bd9Sstevel@tonic-gate  * passing the fid to perform the gettr on we are given the
12647c478bd9Sstevel@tonic-gate  * parent directory fid and a name.
12657c478bd9Sstevel@tonic-gate  */
12667c478bd9Sstevel@tonic-gate int
cachefs_io_getattrname(vnode_t * vp,void * dinp,void * doutp)12677c478bd9Sstevel@tonic-gate cachefs_io_getattrname(vnode_t *vp, void *dinp, void *doutp)
12687c478bd9Sstevel@tonic-gate {
12697c478bd9Sstevel@tonic-gate 	vnode_t	*pbackvp = NULL;
12707c478bd9Sstevel@tonic-gate 	vnode_t	*cbackvp = NULL;
12717c478bd9Sstevel@tonic-gate 	fscache_t  *fscp = C_TO_FSCACHE(VTOC(vp));
12727c478bd9Sstevel@tonic-gate 	int error = 0;
12737c478bd9Sstevel@tonic-gate 	cred_t	*cr;
12747c478bd9Sstevel@tonic-gate 	fid_t	*tmpfidp;
12757c478bd9Sstevel@tonic-gate 	vattr_t	*tmpvap;
12767c478bd9Sstevel@tonic-gate 	cachefsio_getattrname_arg_t *gap;
12777c478bd9Sstevel@tonic-gate 	cachefsio_getattrname_return_t *retp;
12787c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(fid_t, tmpfid);
12797c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(vattr_t, va);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	/*
12827c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
12837c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
12847c478bd9Sstevel@tonic-gate 	 */
12857c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	gap = (cachefsio_getattrname_arg_t *)dinp;
12887c478bd9Sstevel@tonic-gate 	retp = (cachefsio_getattrname_return_t *)doutp;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	/* Get a vnode for the parent directory */
12917c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&gap->cg_dir, &tmpfid, tmpfidp, fid_t);
12927c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&gap->cg_dir, tmpfidp);
12937c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &pbackvp, tmpfidp);
12947c478bd9Sstevel@tonic-gate 	if (error)
12957c478bd9Sstevel@tonic-gate 		return (error);
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	/* lookup the file name */
12987c478bd9Sstevel@tonic-gate 	cr = conj_cred(&gap->cg_cred);
12997c478bd9Sstevel@tonic-gate 	error = VOP_LOOKUP(pbackvp, gap->cg_name, &cbackvp,
1300*da6c28aaSamw 	    (struct pathname *)NULL, 0, (vnode_t *)NULL, cr, NULL, NULL, NULL);
13017c478bd9Sstevel@tonic-gate 	if (error) {
13027c478bd9Sstevel@tonic-gate 		crfree(cr);
13037c478bd9Sstevel@tonic-gate 		VN_RELE(pbackvp);
13047c478bd9Sstevel@tonic-gate 		return (error);
13057c478bd9Sstevel@tonic-gate 	}
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&retp->cg_attr, &va, tmpvap, vattr_t);
13087c478bd9Sstevel@tonic-gate 	tmpvap->va_mask = AT_ALL;
1309*da6c28aaSamw 	error = VOP_GETATTR(cbackvp, tmpvap, 0, cr, NULL);
13107c478bd9Sstevel@tonic-gate 	CACHEFS_VATTR_COPYOUT(tmpvap, &retp->cg_attr, error);
13117c478bd9Sstevel@tonic-gate 	if (!error) {
13127c478bd9Sstevel@tonic-gate 		CACHEFS_TMPPTR_SET(&retp->cg_fid, &tmpfid, tmpfidp, fid_t);
13137c478bd9Sstevel@tonic-gate 		tmpfidp->fid_len = MAXFIDSZ;
1314*da6c28aaSamw 		error = VOP_FID(cbackvp, tmpfidp, NULL);
13157c478bd9Sstevel@tonic-gate 		CACHEFS_FID_COPYOUT(tmpfidp, &retp->cg_fid);
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	crfree(cr);
13197c478bd9Sstevel@tonic-gate 	VN_RELE(cbackvp);
13207c478bd9Sstevel@tonic-gate 	VN_RELE(pbackvp);
13217c478bd9Sstevel@tonic-gate 	return (error);
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate /*
13257c478bd9Sstevel@tonic-gate  * This will return the fid of the root of this mount point.
13267c478bd9Sstevel@tonic-gate  */
13277c478bd9Sstevel@tonic-gate int
13287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
cachefs_io_rootfid(vnode_t * vp,void * dinp,void * doutp)13297c478bd9Sstevel@tonic-gate cachefs_io_rootfid(vnode_t *vp, void *dinp, void *doutp)
13307c478bd9Sstevel@tonic-gate {
13317c478bd9Sstevel@tonic-gate 	fscache_t  *fscp = C_TO_FSCACHE(VTOC(vp));
13327c478bd9Sstevel@tonic-gate 	cfs_fid_t *rootfid = (cfs_fid_t *)doutp;
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	/*
13357c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
13367c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
13377c478bd9Sstevel@tonic-gate 	 */
13387c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPY(&VTOC(fscp->fs_rootvp)->c_metadata.md_cookie, rootfid);
13417c478bd9Sstevel@tonic-gate 	return (0);
13427c478bd9Sstevel@tonic-gate }
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate /*
13457c478bd9Sstevel@tonic-gate  * Pushes the data associated with a file back to the file server.
13467c478bd9Sstevel@tonic-gate  */
13477c478bd9Sstevel@tonic-gate int
cachefs_io_pushback(vnode_t * vp,void * dinp,void * doutp)13487c478bd9Sstevel@tonic-gate cachefs_io_pushback(vnode_t *vp, void *dinp, void *doutp)
13497c478bd9Sstevel@tonic-gate {
13507c478bd9Sstevel@tonic-gate 	vnode_t *backvp = NULL;
13517c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
13527c478bd9Sstevel@tonic-gate 	caddr_t	buffer = NULL;
13537c478bd9Sstevel@tonic-gate 	int error = 0;
13547c478bd9Sstevel@tonic-gate 	cnode_t	*cp;
13557c478bd9Sstevel@tonic-gate 	size_t amt;
13567c478bd9Sstevel@tonic-gate 	u_offset_t size;
13577c478bd9Sstevel@tonic-gate 	vattr_t	va;
13587c478bd9Sstevel@tonic-gate 	offset_t off;
13597c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
13607c478bd9Sstevel@tonic-gate 	fid_t	*tmpfidp;
13617c478bd9Sstevel@tonic-gate 	cachefsio_pushback_arg_t *pbp;
13627c478bd9Sstevel@tonic-gate 	cachefsio_pushback_return_t *retp;
13637c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(fid_t, tmpfid);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	/*
13667c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
13677c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
13687c478bd9Sstevel@tonic-gate 	 */
13697c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	pbp = (cachefsio_pushback_arg_t *)dinp;
13727c478bd9Sstevel@tonic-gate 	retp = (cachefsio_pushback_return_t *)doutp;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	cr = conj_cred(&pbp->pb_cred);
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	/* get the backvp to push to */
13777c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&pbp->pb_fid, &tmpfid, tmpfidp, fid_t);
13787c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&pbp->pb_fid, tmpfidp);
13797c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &backvp, tmpfidp);
13807c478bd9Sstevel@tonic-gate 	if (error) {
13817c478bd9Sstevel@tonic-gate 		backvp = NULL;
13827c478bd9Sstevel@tonic-gate 		goto out;
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	/* Get the cnode for the file we are to push back */
13867c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(&pbp->pb_cid, fscp,
13877c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, cr, 0, &cp);
13887c478bd9Sstevel@tonic-gate 	if (error) {
13897c478bd9Sstevel@tonic-gate 		goto out;
13907c478bd9Sstevel@tonic-gate 	}
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	/* must be a regular file */
13937c478bd9Sstevel@tonic-gate 	if (cp->c_attr.va_type != VREG) {
13947c478bd9Sstevel@tonic-gate 		error = EINVAL;
13957c478bd9Sstevel@tonic-gate 		goto out;
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_statelock);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	/* get the front file */
14017c478bd9Sstevel@tonic-gate 	if (cp->c_frontvp == NULL) {
14027c478bd9Sstevel@tonic-gate 		error = cachefs_getfrontfile(cp);
14037c478bd9Sstevel@tonic-gate 		if (error) {
14047c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->c_statelock);
14057c478bd9Sstevel@tonic-gate 			goto out;
14067c478bd9Sstevel@tonic-gate 		}
14077c478bd9Sstevel@tonic-gate 	}
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	/* better be populated */
14107c478bd9Sstevel@tonic-gate 	if ((cp->c_metadata.md_flags & MD_POPULATED) == 0) {
14117c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
14127c478bd9Sstevel@tonic-gate 		error = EINVAL;
14137c478bd9Sstevel@tonic-gate 		goto out;
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	/* do open so NFS gets correct creds on writes */
1417*da6c28aaSamw 	error = VOP_OPEN(&backvp, FWRITE, cr, NULL);
14187c478bd9Sstevel@tonic-gate 	if (error) {
14197c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
14207c478bd9Sstevel@tonic-gate 		goto out;
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	buffer = cachefs_kmem_alloc(MAXBSIZE, KM_SLEEP);
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	/* Read the data from the cache and write it to the server */
14267c478bd9Sstevel@tonic-gate 	/* XXX why not use segmapio? */
14277c478bd9Sstevel@tonic-gate 	off = 0;
14287c478bd9Sstevel@tonic-gate 	for (size = cp->c_size; size != 0; size -= amt) {
14297c478bd9Sstevel@tonic-gate 		if (size > MAXBSIZE)
14307c478bd9Sstevel@tonic-gate 			amt = MAXBSIZE;
14317c478bd9Sstevel@tonic-gate 		else
14327c478bd9Sstevel@tonic-gate 			amt = size;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 		/* read a block of data from the front file */
14357c478bd9Sstevel@tonic-gate 		error = vn_rdwr(UIO_READ, cp->c_frontvp, buffer,
14367c478bd9Sstevel@tonic-gate 			amt, off, UIO_SYSSPACE, 0, RLIM_INFINITY, cr, 0);
14377c478bd9Sstevel@tonic-gate 		if (error) {
14387c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->c_statelock);
14397c478bd9Sstevel@tonic-gate 			goto out;
14407c478bd9Sstevel@tonic-gate 		}
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 		/* write the block of data to the back file */
14437c478bd9Sstevel@tonic-gate 		error = vn_rdwr(UIO_WRITE, backvp, buffer, amt, off,
14447c478bd9Sstevel@tonic-gate 			UIO_SYSSPACE, 0, RLIM_INFINITY, cr, 0);
14457c478bd9Sstevel@tonic-gate 		if (error) {
14467c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->c_statelock);
14477c478bd9Sstevel@tonic-gate 			goto out;
14487c478bd9Sstevel@tonic-gate 		}
14497c478bd9Sstevel@tonic-gate 		off += amt;
14507c478bd9Sstevel@tonic-gate 	}
14517c478bd9Sstevel@tonic-gate 
1452*da6c28aaSamw 	error = VOP_FSYNC(backvp, FSYNC, cr, NULL);
14537c478bd9Sstevel@tonic-gate 	if (error == 0)
1454*da6c28aaSamw 		error = VOP_CLOSE(backvp, FWRITE, 1, (offset_t)0, cr, NULL);
14557c478bd9Sstevel@tonic-gate 	if (error) {
14567c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
14577c478bd9Sstevel@tonic-gate 		goto out;
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_PUSHDONE;
14617c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags &= ~MD_PUTPAGE;
14627c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_NEEDATTRS;
14637c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED;
14647c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->c_statelock);
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	/*
14677c478bd9Sstevel@tonic-gate 	 * if we have successfully stored the data, we need the
14687c478bd9Sstevel@tonic-gate 	 * new ctime and mtimes.
14697c478bd9Sstevel@tonic-gate 	 */
14707c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
1471*da6c28aaSamw 	error = VOP_GETATTR(backvp, &va, 0, cr, NULL);
14727c478bd9Sstevel@tonic-gate 	if (error)
14737c478bd9Sstevel@tonic-gate 		goto out;
14747c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_ctime, &retp->pb_ctime, error);
14757c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_mtime, &retp->pb_mtime, error);
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate out:
14787c478bd9Sstevel@tonic-gate 	if (buffer)
14797c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(buffer, MAXBSIZE);
14807c478bd9Sstevel@tonic-gate 	if (cp)
14817c478bd9Sstevel@tonic-gate 		VN_RELE(CTOV(cp));
14827c478bd9Sstevel@tonic-gate 	if (backvp)
14837c478bd9Sstevel@tonic-gate 		VN_RELE(backvp);
14847c478bd9Sstevel@tonic-gate 	if (cr)
14857c478bd9Sstevel@tonic-gate 		crfree(cr);
14867c478bd9Sstevel@tonic-gate 	return (error);
14877c478bd9Sstevel@tonic-gate }
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate /*
14907c478bd9Sstevel@tonic-gate  * Create a file on the back file system.
14917c478bd9Sstevel@tonic-gate  */
14927c478bd9Sstevel@tonic-gate int
cachefs_io_create(vnode_t * vp,void * dinp,void * doutp)14937c478bd9Sstevel@tonic-gate cachefs_io_create(vnode_t *vp, void *dinp, void *doutp)
14947c478bd9Sstevel@tonic-gate {
14957c478bd9Sstevel@tonic-gate 	vnode_t	*dvp = NULL;
14967c478bd9Sstevel@tonic-gate 	vnode_t	*cvp = NULL;
14977c478bd9Sstevel@tonic-gate 	cnode_t *cp = NULL;
14987c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
14997c478bd9Sstevel@tonic-gate 	vattr_t	va, *tmpvap;
15007c478bd9Sstevel@tonic-gate 	int error = 0;
15017c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
15027c478bd9Sstevel@tonic-gate 	fid_t	*tmpfidp;
15037c478bd9Sstevel@tonic-gate 	cachefsio_create_arg_t *crp;
15047c478bd9Sstevel@tonic-gate 	cachefsio_create_return_t *retp;
15057c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(fid_t, tmpfid);
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	/*
15087c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
15097c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
15107c478bd9Sstevel@tonic-gate 	 */
15117c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	crp = (cachefsio_create_arg_t *)dinp;
15147c478bd9Sstevel@tonic-gate 	retp = (cachefsio_create_return_t *)doutp;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	/* get a vnode for the parent directory  */
15177c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&crp->cr_backfid, &tmpfid, tmpfidp, fid_t);
15187c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&crp->cr_backfid, tmpfidp);
15197c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &dvp, tmpfidp);
15207c478bd9Sstevel@tonic-gate 	if (error)
15217c478bd9Sstevel@tonic-gate 		goto out;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	cr = conj_cred(&crp->cr_cred);
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	/* do the create */
15267c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&crp->cr_va, &va, tmpvap, vattr_t);
15277c478bd9Sstevel@tonic-gate 	CACHEFS_VATTR_COPYIN(&crp->cr_va, tmpvap);
15287c478bd9Sstevel@tonic-gate 	error = VOP_CREATE(dvp, crp->cr_name, tmpvap,
1529*da6c28aaSamw 	    crp->cr_exclusive, crp->cr_mode, &cvp, cr, 0, NULL, NULL);
15307c478bd9Sstevel@tonic-gate 	if (error)
15317c478bd9Sstevel@tonic-gate 		goto out;
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	/* get the fid of the file */
15347c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&retp->cr_newfid, &tmpfid, tmpfidp, fid_t);
15357c478bd9Sstevel@tonic-gate 	tmpfidp->fid_len = MAXFIDSZ;
1536*da6c28aaSamw 	error = VOP_FID(cvp, tmpfidp, NULL);
15377c478bd9Sstevel@tonic-gate 	if (error)
15387c478bd9Sstevel@tonic-gate 		goto out;
15397c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYOUT(tmpfidp, &retp->cr_newfid);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	/* get attributes for the file */
15427c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
1543*da6c28aaSamw 	error = VOP_GETATTR(cvp, &va, 0, cr, NULL);
15447c478bd9Sstevel@tonic-gate 	if (error)
15457c478bd9Sstevel@tonic-gate 		goto out;
15467c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_ctime, &retp->cr_ctime, error);
15477c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_mtime, &retp->cr_mtime, error);
15487c478bd9Sstevel@tonic-gate 	if (error)
15497c478bd9Sstevel@tonic-gate 		goto out;
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	/* update the cnode for this file with the new info */
15527c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(&crp->cr_cid, fscp,
15537c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, cr, 0, &cp);
15547c478bd9Sstevel@tonic-gate 	if (error) {
15557c478bd9Sstevel@tonic-gate 		error = 0;
15567c478bd9Sstevel@tonic-gate 		goto out;
15577c478bd9Sstevel@tonic-gate 	}
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_statelock);
15607c478bd9Sstevel@tonic-gate 	ASSERT(cp->c_id.cid_flags & CFS_CID_LOCAL);
15617c478bd9Sstevel@tonic-gate 	cp->c_attr.va_nodeid = va.va_nodeid;
15627c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_CREATEDONE;
15637c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_NEEDATTRS;
15647c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_cookie = *tmpfidp;
15657c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED;
15667c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->c_statelock);
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate out:
15697c478bd9Sstevel@tonic-gate 	if (cr)
15707c478bd9Sstevel@tonic-gate 		crfree(cr);
15717c478bd9Sstevel@tonic-gate 	if (dvp)
15727c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
15737c478bd9Sstevel@tonic-gate 	if (cvp)
15747c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
15757c478bd9Sstevel@tonic-gate 	if (cp)
15767c478bd9Sstevel@tonic-gate 		VN_RELE(CTOV(cp));
15777c478bd9Sstevel@tonic-gate 	return (error);
15787c478bd9Sstevel@tonic-gate }
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate /*
15817c478bd9Sstevel@tonic-gate  * Remove a file on the back file system.
15827c478bd9Sstevel@tonic-gate  * Returns 0 or an error if could not perform operation.
15837c478bd9Sstevel@tonic-gate  */
15847c478bd9Sstevel@tonic-gate int
cachefs_io_remove(vnode_t * vp,void * dinp,void * doutp)15857c478bd9Sstevel@tonic-gate cachefs_io_remove(vnode_t *vp, void *dinp, void *doutp)
15867c478bd9Sstevel@tonic-gate {
15877c478bd9Sstevel@tonic-gate 	vnode_t	*dvp = NULL;
15887c478bd9Sstevel@tonic-gate 	vnode_t	*cvp;
15897c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
15907c478bd9Sstevel@tonic-gate 	vattr_t	va;
15917c478bd9Sstevel@tonic-gate 	fscache_t  *fscp = C_TO_FSCACHE(VTOC(vp));
15927c478bd9Sstevel@tonic-gate 	int error;
15937c478bd9Sstevel@tonic-gate 	fid_t child_fid, *child_fidp;
15947c478bd9Sstevel@tonic-gate 	cachefsio_remove_t *rmp = (cachefsio_remove_t *)dinp;
15957c478bd9Sstevel@tonic-gate 	cfs_timestruc_t *ctimep = (cfs_timestruc_t *)doutp;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	/*
15987c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
15997c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
16007c478bd9Sstevel@tonic-gate 	 */
16017c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	/* Get a vnode for the directory */
16047c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&rmp->rm_fid, &child_fid, child_fidp, fid_t);
16057c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&rmp->rm_fid, child_fidp);
16067c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &dvp, child_fidp);
16077c478bd9Sstevel@tonic-gate 	if (error) {
16087c478bd9Sstevel@tonic-gate 		dvp = NULL;
16097c478bd9Sstevel@tonic-gate 		goto out;
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	cr = conj_cred(&rmp->rm_cred);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	/* if the caller wants the ctime after the remove */
16157c478bd9Sstevel@tonic-gate 	if (ctimep) {
1616*da6c28aaSamw 		error = VOP_LOOKUP(dvp, rmp->rm_name, &cvp, NULL, 0, NULL, cr,
1617*da6c28aaSamw 			NULL, NULL, NULL);
16187c478bd9Sstevel@tonic-gate 		if (error == 0) {
16197c478bd9Sstevel@tonic-gate 			child_fid.fid_len = MAXFIDSZ;
1620*da6c28aaSamw 			error = VOP_FID(cvp, &child_fid, NULL);
16217c478bd9Sstevel@tonic-gate 			VN_RELE(cvp);
16227c478bd9Sstevel@tonic-gate 		}
16237c478bd9Sstevel@tonic-gate 		if (error)
16247c478bd9Sstevel@tonic-gate 			goto out;
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	/* do the remove */
1628*da6c28aaSamw 	error = VOP_REMOVE(dvp, rmp->rm_name, cr, NULL, 0);
16297c478bd9Sstevel@tonic-gate 	if (error)
16307c478bd9Sstevel@tonic-gate 		goto out;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	/* get the new ctime if requested */
16337c478bd9Sstevel@tonic-gate 	if (ctimep) {
16347c478bd9Sstevel@tonic-gate 		error = VFS_VGET(fscp->fs_backvfsp, &cvp, &child_fid);
16357c478bd9Sstevel@tonic-gate 		if (error == 0) {
16367c478bd9Sstevel@tonic-gate 			va.va_mask = AT_ALL;
1637*da6c28aaSamw 			error = VOP_GETATTR(cvp, &va, 0, cr, NULL);
16387c478bd9Sstevel@tonic-gate 			if (error == 0) {
16397c478bd9Sstevel@tonic-gate 				CACHEFS_TS_TO_CFS_TS_COPY(&va.va_ctime,
16407c478bd9Sstevel@tonic-gate 					ctimep, error);
16417c478bd9Sstevel@tonic-gate 			}
16427c478bd9Sstevel@tonic-gate 			VN_RELE(cvp);
16437c478bd9Sstevel@tonic-gate 		}
16447c478bd9Sstevel@tonic-gate 		cachefs_iosetneedattrs(fscp, &rmp->rm_cid);
16457c478bd9Sstevel@tonic-gate 	}
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate out:
16487c478bd9Sstevel@tonic-gate 	if (cr)
16497c478bd9Sstevel@tonic-gate 		crfree(cr);
16507c478bd9Sstevel@tonic-gate 	if (dvp)
16517c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
16527c478bd9Sstevel@tonic-gate 	return (error);
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate /*
16567c478bd9Sstevel@tonic-gate  * Perform a link on the back file system.
16577c478bd9Sstevel@tonic-gate  * Returns 0 or an error if could not perform operation.
16587c478bd9Sstevel@tonic-gate  */
16597c478bd9Sstevel@tonic-gate int
cachefs_io_link(vnode_t * vp,void * dinp,void * doutp)16607c478bd9Sstevel@tonic-gate cachefs_io_link(vnode_t *vp, void *dinp, void *doutp)
16617c478bd9Sstevel@tonic-gate {
16627c478bd9Sstevel@tonic-gate 	vnode_t	*dvp = NULL;
16637c478bd9Sstevel@tonic-gate 	vnode_t	*lvp = NULL;
16647c478bd9Sstevel@tonic-gate 	vattr_t	va;
16657c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
16667c478bd9Sstevel@tonic-gate 	int error = 0;
16677c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
16687c478bd9Sstevel@tonic-gate 	fid_t *tmpfidp;
16697c478bd9Sstevel@tonic-gate 	cachefsio_link_t *linkp = (cachefsio_link_t *)dinp;
16707c478bd9Sstevel@tonic-gate 	cfs_timestruc_t *ctimep = (cfs_timestruc_t *)doutp;
16717c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(fid_t, tmpfid);
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	/*
16747c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
16757c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
16767c478bd9Sstevel@tonic-gate 	 */
16777c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	/* Get a vnode parent directory */
16807c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&linkp->ln_dirfid, &tmpfid, tmpfidp, fid_t);
16817c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&linkp->ln_dirfid, tmpfidp);
16827c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &dvp, tmpfidp);
16837c478bd9Sstevel@tonic-gate 	if (error) {
16847c478bd9Sstevel@tonic-gate 		dvp = NULL;
16857c478bd9Sstevel@tonic-gate 		goto out;
16867c478bd9Sstevel@tonic-gate 	}
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	/* Get a vnode file to link to */
16897c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&linkp->ln_filefid, &tmpfid, tmpfidp, fid_t);
16907c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&linkp->ln_filefid, tmpfidp);
16917c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &lvp, tmpfidp);
16927c478bd9Sstevel@tonic-gate 	if (error) {
16937c478bd9Sstevel@tonic-gate 		lvp = NULL;
16947c478bd9Sstevel@tonic-gate 		goto out;
16957c478bd9Sstevel@tonic-gate 	}
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	cr = conj_cred(&linkp->ln_cred);
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	/* do the link */
1700*da6c28aaSamw 	error = VOP_LINK(dvp, lvp, linkp->ln_name, cr, NULL, 0);
17017c478bd9Sstevel@tonic-gate 	if (error)
17027c478bd9Sstevel@tonic-gate 		goto out;
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	/* get the ctime */
17057c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
1706*da6c28aaSamw 	error = VOP_GETATTR(lvp, &va, 0, cr, NULL);
17077c478bd9Sstevel@tonic-gate 	if (error)
17087c478bd9Sstevel@tonic-gate 		goto out;
17097c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_ctime, ctimep, error);
17107c478bd9Sstevel@tonic-gate 	if (error)
17117c478bd9Sstevel@tonic-gate 		goto out;
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	cachefs_iosetneedattrs(fscp, &linkp->ln_cid);
17147c478bd9Sstevel@tonic-gate out:
17157c478bd9Sstevel@tonic-gate 	if (cr)
17167c478bd9Sstevel@tonic-gate 		crfree(cr);
17177c478bd9Sstevel@tonic-gate 	if (dvp)
17187c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
17197c478bd9Sstevel@tonic-gate 	if (lvp)
17207c478bd9Sstevel@tonic-gate 		VN_RELE(lvp);
17217c478bd9Sstevel@tonic-gate 	return (error);
17227c478bd9Sstevel@tonic-gate }
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate /*
17257c478bd9Sstevel@tonic-gate  * Rename the file on the back file system.
17267c478bd9Sstevel@tonic-gate  * Returns 0 or an error if could not perform operation.
17277c478bd9Sstevel@tonic-gate  */
17287c478bd9Sstevel@tonic-gate int
cachefs_io_rename(vnode_t * vp,void * dinp,void * doutp)17297c478bd9Sstevel@tonic-gate cachefs_io_rename(vnode_t *vp, void *dinp, void *doutp)
17307c478bd9Sstevel@tonic-gate {
17317c478bd9Sstevel@tonic-gate 	vnode_t	*odvp = NULL;
17327c478bd9Sstevel@tonic-gate 	vnode_t	*ndvp = NULL;
17337c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
17347c478bd9Sstevel@tonic-gate 	vnode_t	*cvp = NULL;
17357c478bd9Sstevel@tonic-gate 	vattr_t va;
17367c478bd9Sstevel@tonic-gate 	fscache_t  *fscp = C_TO_FSCACHE(VTOC(vp));
17377c478bd9Sstevel@tonic-gate 	int error = 0;
17387c478bd9Sstevel@tonic-gate 	fid_t child_fid, *child_fidp;
17397c478bd9Sstevel@tonic-gate 	cachefsio_rename_arg_t *rnp;
17407c478bd9Sstevel@tonic-gate 	cachefsio_rename_return_t *retp;
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	/*
17437c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
17447c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
17457c478bd9Sstevel@tonic-gate 	 */
17467c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	rnp = (cachefsio_rename_arg_t *)dinp;
17497c478bd9Sstevel@tonic-gate 	retp = (cachefsio_rename_return_t *)doutp;
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	/* Get vnode of old parent directory */
17527c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&rnp->rn_olddir, &child_fid, child_fidp, fid_t);
17537c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&rnp->rn_olddir, child_fidp);
17547c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &odvp, child_fidp);
17557c478bd9Sstevel@tonic-gate 	if (error) {
17567c478bd9Sstevel@tonic-gate 		odvp = NULL;
17577c478bd9Sstevel@tonic-gate 		goto out;
17587c478bd9Sstevel@tonic-gate 	}
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	/* Get vnode of new parent directory */
17617c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&rnp->rn_newdir, &child_fid, child_fidp, fid_t);
17627c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&rnp->rn_newdir, child_fidp);
17637c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &ndvp, child_fidp);
17647c478bd9Sstevel@tonic-gate 	if (error) {
17657c478bd9Sstevel@tonic-gate 		ndvp = NULL;
17667c478bd9Sstevel@tonic-gate 		goto out;
17677c478bd9Sstevel@tonic-gate 	}
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	cr = conj_cred(&rnp->rn_cred);
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	/* if the caller wants the ctime of the target after deletion */
17727c478bd9Sstevel@tonic-gate 	if (rnp->rn_del_getctime) {
17737c478bd9Sstevel@tonic-gate 		error = VOP_LOOKUP(ndvp, rnp->rn_newname, &cvp, NULL, 0,
1774*da6c28aaSamw 		    NULL, cr, NULL, NULL, NULL);
17757c478bd9Sstevel@tonic-gate 		if (error) {
17767c478bd9Sstevel@tonic-gate 			cvp = NULL; /* paranoia */
17777c478bd9Sstevel@tonic-gate 			goto out;
17787c478bd9Sstevel@tonic-gate 		}
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 		child_fid.fid_len = MAXFIDSZ;
1781*da6c28aaSamw 		error = VOP_FID(cvp, &child_fid, NULL);
17827c478bd9Sstevel@tonic-gate 		if (error)
17837c478bd9Sstevel@tonic-gate 			goto out;
17847c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
17857c478bd9Sstevel@tonic-gate 		cvp = NULL;
17867c478bd9Sstevel@tonic-gate 	}
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	/* do the rename */
1789*da6c28aaSamw 	error = VOP_RENAME(odvp, rnp->rn_oldname, ndvp, rnp->rn_newname, cr,
1790*da6c28aaSamw 		NULL, 0);
17917c478bd9Sstevel@tonic-gate 	if (error)
17927c478bd9Sstevel@tonic-gate 		goto out;
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	/* get the new ctime on the renamed file */
1795*da6c28aaSamw 	error = VOP_LOOKUP(ndvp, rnp->rn_newname, &cvp, NULL, 0, NULL, cr,
1796*da6c28aaSamw 		NULL, NULL, NULL);
17977c478bd9Sstevel@tonic-gate 	if (error)
17987c478bd9Sstevel@tonic-gate 		goto out;
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
1801*da6c28aaSamw 	error = VOP_GETATTR(cvp, &va, 0, cr, NULL);
18027c478bd9Sstevel@tonic-gate 	if (error)
18037c478bd9Sstevel@tonic-gate 		goto out;
18047c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_ctime, &retp->rn_ctime, error);
18057c478bd9Sstevel@tonic-gate 	VN_RELE(cvp);
18067c478bd9Sstevel@tonic-gate 	cvp = NULL;
18077c478bd9Sstevel@tonic-gate 	if (error)
18087c478bd9Sstevel@tonic-gate 		goto out;
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	cachefs_iosetneedattrs(fscp, &rnp->rn_cid);
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	/* get the new ctime if requested of the deleted target */
18137c478bd9Sstevel@tonic-gate 	if (rnp->rn_del_getctime) {
18147c478bd9Sstevel@tonic-gate 		error = VFS_VGET(fscp->fs_backvfsp, &cvp, &child_fid);
18157c478bd9Sstevel@tonic-gate 		if (error) {
18167c478bd9Sstevel@tonic-gate 			cvp = NULL;
18177c478bd9Sstevel@tonic-gate 			goto out;
18187c478bd9Sstevel@tonic-gate 		}
18197c478bd9Sstevel@tonic-gate 		va.va_mask = AT_ALL;
1820*da6c28aaSamw 		error = VOP_GETATTR(cvp, &va, 0, cr, NULL);
18217c478bd9Sstevel@tonic-gate 		if (error)
18227c478bd9Sstevel@tonic-gate 			goto out;
18237c478bd9Sstevel@tonic-gate 		CACHEFS_TS_TO_CFS_TS_COPY(&va.va_ctime, &retp->rn_del_ctime,
18247c478bd9Sstevel@tonic-gate 			error);
18257c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
18267c478bd9Sstevel@tonic-gate 		cvp = NULL;
18277c478bd9Sstevel@tonic-gate 		if (error)
18287c478bd9Sstevel@tonic-gate 			goto out;
18297c478bd9Sstevel@tonic-gate 		cachefs_iosetneedattrs(fscp, &rnp->rn_del_cid);
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate out:
18337c478bd9Sstevel@tonic-gate 	if (cr)
18347c478bd9Sstevel@tonic-gate 		crfree(cr);
18357c478bd9Sstevel@tonic-gate 	if (cvp)
18367c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
18377c478bd9Sstevel@tonic-gate 	if (odvp)
18387c478bd9Sstevel@tonic-gate 		VN_RELE(odvp);
18397c478bd9Sstevel@tonic-gate 	if (ndvp)
18407c478bd9Sstevel@tonic-gate 		VN_RELE(ndvp);
18417c478bd9Sstevel@tonic-gate 	return (error);
18427c478bd9Sstevel@tonic-gate }
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate /*
18457c478bd9Sstevel@tonic-gate  * Make a directory on the backfs.
18467c478bd9Sstevel@tonic-gate  * Returns 0 or an error if could not perform operation.
18477c478bd9Sstevel@tonic-gate  */
18487c478bd9Sstevel@tonic-gate int
cachefs_io_mkdir(vnode_t * vp,void * dinp,void * doutp)18497c478bd9Sstevel@tonic-gate cachefs_io_mkdir(vnode_t *vp, void *dinp, void *doutp)
18507c478bd9Sstevel@tonic-gate {
18517c478bd9Sstevel@tonic-gate 	vnode_t	*dvp = NULL;
18527c478bd9Sstevel@tonic-gate 	vnode_t	*cvp = NULL;
18537c478bd9Sstevel@tonic-gate 	cnode_t *cp = NULL;
18547c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
18557c478bd9Sstevel@tonic-gate 	int error = 0;
18567c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
18577c478bd9Sstevel@tonic-gate 	fid_t	*tmpfidp;
18587c478bd9Sstevel@tonic-gate 	vattr_t va, *tmpvap;
18597c478bd9Sstevel@tonic-gate 	cachefsio_mkdir_t *mdirp = (cachefsio_mkdir_t *)dinp;
18607c478bd9Sstevel@tonic-gate 	cfs_fid_t *fidp = (cfs_fid_t *)doutp;
18617c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(fid_t, tmpfid);
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	/*
18647c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
18657c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
18667c478bd9Sstevel@tonic-gate 	 */
18677c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	/* Get vnode of parent directory */
18707c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&mdirp->md_dirfid, &tmpfid, tmpfidp, fid_t);
18717c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&mdirp->md_dirfid, tmpfidp);
18727c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &dvp, tmpfidp);
18737c478bd9Sstevel@tonic-gate 	if (error) {
18747c478bd9Sstevel@tonic-gate 		dvp = NULL;
18757c478bd9Sstevel@tonic-gate 		goto out;
18767c478bd9Sstevel@tonic-gate 	}
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	cr = conj_cred(&mdirp->md_cred);
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	/* make the directory */
18817c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&mdirp->md_vattr, &va, tmpvap, vattr_t);
18827c478bd9Sstevel@tonic-gate 	CACHEFS_VATTR_COPYIN(&mdirp->md_vattr, tmpvap);
1883*da6c28aaSamw 	error = VOP_MKDIR(dvp, mdirp->md_name, tmpvap, &cvp, cr, NULL, 0, NULL);
18847c478bd9Sstevel@tonic-gate 	if (error) {
18857c478bd9Sstevel@tonic-gate 		if (error != EEXIST)
18867c478bd9Sstevel@tonic-gate 			goto out;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 		/* if the directory already exists, then use it */
18897c478bd9Sstevel@tonic-gate 		error = VOP_LOOKUP(dvp, mdirp->md_name, &cvp,
1890*da6c28aaSamw 		    NULL, 0, NULL, cr, NULL, NULL, NULL);
18917c478bd9Sstevel@tonic-gate 		if (error) {
18927c478bd9Sstevel@tonic-gate 			cvp = NULL;
18937c478bd9Sstevel@tonic-gate 			goto out;
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 		if (cvp->v_type != VDIR) {
18967c478bd9Sstevel@tonic-gate 			error = EINVAL;
18977c478bd9Sstevel@tonic-gate 			goto out;
18987c478bd9Sstevel@tonic-gate 		}
18997c478bd9Sstevel@tonic-gate 	}
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	/* get the fid of the directory */
19027c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(fidp, &tmpfid, tmpfidp, fid_t);
19037c478bd9Sstevel@tonic-gate 	tmpfidp->fid_len = MAXFIDSZ;
1904*da6c28aaSamw 	error = VOP_FID(cvp, tmpfidp, NULL);
19057c478bd9Sstevel@tonic-gate 	if (error)
19067c478bd9Sstevel@tonic-gate 		goto out;
19077c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYOUT(tmpfidp, fidp);
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	/* get attributes of the directory */
19107c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
1911*da6c28aaSamw 	error = VOP_GETATTR(cvp, &va, 0, cr, NULL);
19127c478bd9Sstevel@tonic-gate 	if (error)
19137c478bd9Sstevel@tonic-gate 		goto out;
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	/* update the cnode for this dir with the new fid */
19167c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(&mdirp->md_cid, fscp,
19177c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, cr, 0, &cp);
19187c478bd9Sstevel@tonic-gate 	if (error) {
19197c478bd9Sstevel@tonic-gate 		error = 0;
19207c478bd9Sstevel@tonic-gate 		goto out;
19217c478bd9Sstevel@tonic-gate 	}
19227c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_statelock);
19237c478bd9Sstevel@tonic-gate 	ASSERT(cp->c_id.cid_flags & CFS_CID_LOCAL);
19247c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_cookie = *tmpfidp;
19257c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_CREATEDONE;
19267c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_NEEDATTRS;
19277c478bd9Sstevel@tonic-gate 	cp->c_attr.va_nodeid = va.va_nodeid;
19287c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED;
19297c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->c_statelock);
19307c478bd9Sstevel@tonic-gate out:
19317c478bd9Sstevel@tonic-gate 	if (cr)
19327c478bd9Sstevel@tonic-gate 		crfree(cr);
19337c478bd9Sstevel@tonic-gate 	if (dvp)
19347c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
19357c478bd9Sstevel@tonic-gate 	if (cvp)
19367c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
19377c478bd9Sstevel@tonic-gate 	if (cp)
19387c478bd9Sstevel@tonic-gate 		VN_RELE(CTOV(cp));
19397c478bd9Sstevel@tonic-gate 	return (error);
19407c478bd9Sstevel@tonic-gate }
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate /*
19437c478bd9Sstevel@tonic-gate  * Perform a rmdir on the back file system.
19447c478bd9Sstevel@tonic-gate  * Returns 0 or an error if could not perform operation.
19457c478bd9Sstevel@tonic-gate  */
19467c478bd9Sstevel@tonic-gate int
19477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
cachefs_io_rmdir(vnode_t * vp,void * dinp,void * doutp)19487c478bd9Sstevel@tonic-gate cachefs_io_rmdir(vnode_t *vp, void *dinp, void *doutp)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate 	vnode_t	*dvp = NULL;
19517c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
19527c478bd9Sstevel@tonic-gate 	int error;
19537c478bd9Sstevel@tonic-gate 	cred_t *cr;
19547c478bd9Sstevel@tonic-gate 	fid_t	*tmpfidp;
19557c478bd9Sstevel@tonic-gate 	cachefsio_rmdir_t *rdp = (cachefsio_rmdir_t *)dinp;
19567c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(fid_t, tmpfid);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	/*
19597c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
19607c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
19617c478bd9Sstevel@tonic-gate 	 */
19627c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	/* Get a vnode for the back file */
19657c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&rdp->rd_dirfid, &tmpfid, tmpfidp, fid_t);
19667c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&rdp->rd_dirfid, tmpfidp);
19677c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &dvp, tmpfidp);
19687c478bd9Sstevel@tonic-gate 	if (error) {
19697c478bd9Sstevel@tonic-gate 		dvp = NULL;
19707c478bd9Sstevel@tonic-gate 		return (error);
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	cr = conj_cred(&rdp->rd_cred);
1974*da6c28aaSamw 	error = VOP_RMDIR(dvp, rdp->rd_name, dvp, cr, NULL, 0);
19757c478bd9Sstevel@tonic-gate 	crfree(cr);
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	VN_RELE(dvp);
19787c478bd9Sstevel@tonic-gate 	return (error);
19797c478bd9Sstevel@tonic-gate }
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate /*
19827c478bd9Sstevel@tonic-gate  * create a symlink on the back file system
19837c478bd9Sstevel@tonic-gate  * Returns 0 or an error if could not perform operation.
19847c478bd9Sstevel@tonic-gate  */
19857c478bd9Sstevel@tonic-gate int
cachefs_io_symlink(vnode_t * vp,void * dinp,void * doutp)19867c478bd9Sstevel@tonic-gate cachefs_io_symlink(vnode_t *vp, void *dinp, void *doutp)
19877c478bd9Sstevel@tonic-gate {
19887c478bd9Sstevel@tonic-gate 	vnode_t	*dvp = NULL;
19897c478bd9Sstevel@tonic-gate 	vnode_t	*svp = NULL;
19907c478bd9Sstevel@tonic-gate 	cnode_t *cp = NULL;
19917c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
19927c478bd9Sstevel@tonic-gate 	fid_t	*tmpfidp;
19937c478bd9Sstevel@tonic-gate 	vattr_t	va, *tmpvap;
19947c478bd9Sstevel@tonic-gate 	int error = 0;
19957c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
19967c478bd9Sstevel@tonic-gate 	cachefsio_symlink_arg_t *symp;
19977c478bd9Sstevel@tonic-gate 	cachefsio_symlink_return_t *retp;
19987c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(fid_t, tmpfid);
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	/*
20017c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
20027c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
20037c478bd9Sstevel@tonic-gate 	 */
20047c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	symp = (cachefsio_symlink_arg_t *)dinp;
20077c478bd9Sstevel@tonic-gate 	retp = (cachefsio_symlink_return_t *)doutp;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	/* get a vnode for the back directory */
20107c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&symp->sy_dirfid, &tmpfid, tmpfidp, fid_t);
20117c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&symp->sy_dirfid, tmpfidp);
20127c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &dvp, tmpfidp);
20137c478bd9Sstevel@tonic-gate 	if (error) {
20147c478bd9Sstevel@tonic-gate 		dvp = NULL;
20157c478bd9Sstevel@tonic-gate 		goto out;
20167c478bd9Sstevel@tonic-gate 	}
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	cr = conj_cred(&symp->sy_cred);
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	/* create the symlink */
20217c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&symp->sy_vattr, &va, tmpvap, vattr_t);
20227c478bd9Sstevel@tonic-gate 	CACHEFS_VATTR_COPYIN(&symp->sy_vattr, tmpvap);
20237c478bd9Sstevel@tonic-gate 	error = VOP_SYMLINK(dvp, symp->sy_name, tmpvap,
2024*da6c28aaSamw 	    symp->sy_link, cr, NULL, 0);
20257c478bd9Sstevel@tonic-gate 	if (error)
20267c478bd9Sstevel@tonic-gate 		goto out;
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	/* get the vnode for the symlink */
2029*da6c28aaSamw 	error = VOP_LOOKUP(dvp, symp->sy_name, &svp, NULL, 0, NULL, cr,
2030*da6c28aaSamw 		NULL, NULL, NULL);
20317c478bd9Sstevel@tonic-gate 	if (error)
20327c478bd9Sstevel@tonic-gate 		goto out;
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	/* get the attributes of the symlink */
20357c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
2036*da6c28aaSamw 	error = VOP_GETATTR(svp, &va, 0, cr, NULL);
20377c478bd9Sstevel@tonic-gate 	if (error)
20387c478bd9Sstevel@tonic-gate 		goto out;
20397c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_ctime, &retp->sy_ctime, error);
20407c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_mtime, &retp->sy_mtime, error);
20417c478bd9Sstevel@tonic-gate 	if (error)
20427c478bd9Sstevel@tonic-gate 		goto out;
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	/* get the fid */
20457c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&retp->sy_newfid, &tmpfid, tmpfidp, fid_t);
20467c478bd9Sstevel@tonic-gate 	tmpfidp->fid_len = MAXFIDSZ;
2047*da6c28aaSamw 	error = VOP_FID(svp, tmpfidp, NULL);
20487c478bd9Sstevel@tonic-gate 	if (error)
20497c478bd9Sstevel@tonic-gate 		goto out;
20507c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYOUT(tmpfidp, &retp->sy_newfid);
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	/* update the cnode for this file with the new info */
20537c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(&symp->sy_cid, fscp,
20547c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, cr, 0, &cp);
20557c478bd9Sstevel@tonic-gate 	if (error) {
20567c478bd9Sstevel@tonic-gate 		error = 0;
20577c478bd9Sstevel@tonic-gate 		goto out;
20587c478bd9Sstevel@tonic-gate 	}
20597c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_statelock);
20607c478bd9Sstevel@tonic-gate 	ASSERT(cp->c_id.cid_flags & CFS_CID_LOCAL);
20617c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_cookie = *tmpfidp;
20627c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_CREATEDONE;
20637c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_NEEDATTRS;
20647c478bd9Sstevel@tonic-gate 	cp->c_attr.va_nodeid = va.va_nodeid;
20657c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED;
20667c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->c_statelock);
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate out:
20697c478bd9Sstevel@tonic-gate 	if (cr)
20707c478bd9Sstevel@tonic-gate 		crfree(cr);
20717c478bd9Sstevel@tonic-gate 	if (dvp)
20727c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
20737c478bd9Sstevel@tonic-gate 	if (svp)
20747c478bd9Sstevel@tonic-gate 		VN_RELE(svp);
20757c478bd9Sstevel@tonic-gate 	if (cp)
20767c478bd9Sstevel@tonic-gate 		VN_RELE(CTOV(cp));
20777c478bd9Sstevel@tonic-gate 	return (error);
20787c478bd9Sstevel@tonic-gate }
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate /*
20817c478bd9Sstevel@tonic-gate  * Perform setattr on the back file system.
20827c478bd9Sstevel@tonic-gate  * Returns 0 or an error if could not perform operation.
20837c478bd9Sstevel@tonic-gate  */
20847c478bd9Sstevel@tonic-gate int
cachefs_io_setattr(vnode_t * vp,void * dinp,void * doutp)20857c478bd9Sstevel@tonic-gate cachefs_io_setattr(vnode_t *vp, void *dinp, void *doutp)
20867c478bd9Sstevel@tonic-gate {
20877c478bd9Sstevel@tonic-gate 	vnode_t	*cvp = NULL;
20887c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
20897c478bd9Sstevel@tonic-gate 	fid_t	*tmpfidp;
20907c478bd9Sstevel@tonic-gate 	vattr_t	va, *tmpvap;
20917c478bd9Sstevel@tonic-gate 	int error = 0;
20927c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
20937c478bd9Sstevel@tonic-gate 	cachefsio_setattr_arg_t *sap;
20947c478bd9Sstevel@tonic-gate 	cachefsio_setattr_return_t *retp;
20957c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(fid_t, tmpfid);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	/*
20987c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
20997c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
21007c478bd9Sstevel@tonic-gate 	 */
21017c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	sap = (cachefsio_setattr_arg_t *)dinp;
21047c478bd9Sstevel@tonic-gate 	retp = (cachefsio_setattr_return_t *)doutp;
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	/* get a vnode for the back directory */
21077c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&sap->sa_backfid, &tmpfid, tmpfidp, fid_t);
21087c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&sap->sa_backfid, tmpfidp);
21097c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &cvp, tmpfidp);
21107c478bd9Sstevel@tonic-gate 	if (error) {
21117c478bd9Sstevel@tonic-gate 		cvp = NULL;
21127c478bd9Sstevel@tonic-gate 		goto out;
21137c478bd9Sstevel@tonic-gate 	}
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	cr = conj_cred(&sap->sa_cred);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	/* perform the setattr */
21187c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&sap->sa_vattr, &va, tmpvap, vattr_t);
21197c478bd9Sstevel@tonic-gate 	CACHEFS_VATTR_COPYIN(&sap->sa_vattr, tmpvap);
21207c478bd9Sstevel@tonic-gate 	error = VOP_SETATTR(cvp, tmpvap, 0, cr, NULL);
21217c478bd9Sstevel@tonic-gate 	if (error)
21227c478bd9Sstevel@tonic-gate 		goto out;
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	/* get the new ctime and mtime */
21257c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
2126*da6c28aaSamw 	error = VOP_GETATTR(cvp, &va, 0, cr, NULL);
21277c478bd9Sstevel@tonic-gate 	if (error)
21287c478bd9Sstevel@tonic-gate 		goto out;
21297c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_ctime, &retp->sa_ctime, error);
21307c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_mtime, &retp->sa_mtime, error);
21317c478bd9Sstevel@tonic-gate 	if (error)
21327c478bd9Sstevel@tonic-gate 		goto out;
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	cachefs_iosetneedattrs(fscp, &sap->sa_cid);
21357c478bd9Sstevel@tonic-gate out:
21367c478bd9Sstevel@tonic-gate 	if (cr)
21377c478bd9Sstevel@tonic-gate 		crfree(cr);
21387c478bd9Sstevel@tonic-gate 	if (cvp)
21397c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
21407c478bd9Sstevel@tonic-gate 	return (error);
21417c478bd9Sstevel@tonic-gate }
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate int
cachefs_io_setsecattr(vnode_t * vp,void * dinp,void * doutp)21447c478bd9Sstevel@tonic-gate cachefs_io_setsecattr(vnode_t *vp, void *dinp, void *doutp)
21457c478bd9Sstevel@tonic-gate {
21467c478bd9Sstevel@tonic-gate 	int error = 0;
21477c478bd9Sstevel@tonic-gate 	fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
21487c478bd9Sstevel@tonic-gate 	vnode_t *tvp = NULL;
21497c478bd9Sstevel@tonic-gate 	vsecattr_t vsec;
21507c478bd9Sstevel@tonic-gate 	vattr_t va;
21517c478bd9Sstevel@tonic-gate 	cred_t *cr = NULL;
21527c478bd9Sstevel@tonic-gate 	fid_t	*tmpfidp;
21537c478bd9Sstevel@tonic-gate 	cachefsio_setsecattr_arg_t *ssap;
21547c478bd9Sstevel@tonic-gate 	cachefsio_setsecattr_return_t *retp;
21557c478bd9Sstevel@tonic-gate 	CACHEFS_DECL(fid_t, tmpfid);
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	/*
21587c478bd9Sstevel@tonic-gate 	 * Only called in support of disconnectable operation, so assert
21597c478bd9Sstevel@tonic-gate 	 * that this is not called when NFSv4 is the backfilesytem.
21607c478bd9Sstevel@tonic-gate 	 */
21617c478bd9Sstevel@tonic-gate 	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	ssap = (cachefsio_setsecattr_arg_t *)dinp;
21647c478bd9Sstevel@tonic-gate 	retp = (cachefsio_setsecattr_return_t *)doutp;
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	/* get vnode of back file to do VOP_SETSECATTR to */
21677c478bd9Sstevel@tonic-gate 	CACHEFS_TMPPTR_SET(&ssap->sc_backfid, &tmpfid, tmpfidp, fid_t);
21687c478bd9Sstevel@tonic-gate 	CACHEFS_FID_COPYIN(&ssap->sc_backfid, tmpfidp);
21697c478bd9Sstevel@tonic-gate 	error = VFS_VGET(fscp->fs_backvfsp, &tvp, tmpfidp);
21707c478bd9Sstevel@tonic-gate 	if (error != 0) {
21717c478bd9Sstevel@tonic-gate 		tvp = NULL;
21727c478bd9Sstevel@tonic-gate 		goto out;
21737c478bd9Sstevel@tonic-gate 	}
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	/* get the creds */
21767c478bd9Sstevel@tonic-gate 	cr = conj_cred(&ssap->sc_cred);
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	/* form the vsecattr_t */
21797c478bd9Sstevel@tonic-gate 	vsec.vsa_mask = ssap->sc_mask;
21807c478bd9Sstevel@tonic-gate 	vsec.vsa_aclcnt = ssap->sc_aclcnt;
21817c478bd9Sstevel@tonic-gate 	vsec.vsa_dfaclcnt = ssap->sc_dfaclcnt;
21827c478bd9Sstevel@tonic-gate 	vsec.vsa_aclentp = ssap->sc_acl;
21837c478bd9Sstevel@tonic-gate 	vsec.vsa_dfaclentp = ssap->sc_acl + ssap->sc_aclcnt;
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 	/* set the ACL */
21867c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(tvp, V_WRITELOCK_TRUE, NULL);
2187*da6c28aaSamw 	error = VOP_SETSECATTR(tvp, &vsec, 0, cr, NULL);
21887c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(tvp, V_WRITELOCK_TRUE, NULL);
21897c478bd9Sstevel@tonic-gate 	if (error != 0)
21907c478bd9Sstevel@tonic-gate 		goto out;
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	/* get the new ctime and mtime */
21937c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
2194*da6c28aaSamw 	error = VOP_GETATTR(tvp, &va, 0, cr, NULL);
21957c478bd9Sstevel@tonic-gate 	if (error)
21967c478bd9Sstevel@tonic-gate 		goto out;
21977c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_ctime, &retp->sc_ctime, error);
21987c478bd9Sstevel@tonic-gate 	CACHEFS_TS_TO_CFS_TS_COPY(&va.va_mtime, &retp->sc_mtime, error);
21997c478bd9Sstevel@tonic-gate 	if (error)
22007c478bd9Sstevel@tonic-gate 		goto out;
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	cachefs_iosetneedattrs(fscp, &ssap->sc_cid);
22037c478bd9Sstevel@tonic-gate out:
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	if (cr != NULL)
22067c478bd9Sstevel@tonic-gate 		crfree(cr);
22077c478bd9Sstevel@tonic-gate 	if (tvp != NULL)
22087c478bd9Sstevel@tonic-gate 		VN_RELE(tvp);
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	return (error);
22117c478bd9Sstevel@tonic-gate }
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate static void
sync_metadata(cnode_t * cp)22147c478bd9Sstevel@tonic-gate sync_metadata(cnode_t *cp)
22157c478bd9Sstevel@tonic-gate {
22167c478bd9Sstevel@tonic-gate 	if (cp->c_flags & (CN_STALE | CN_DESTROY))
22177c478bd9Sstevel@tonic-gate 		return;
22187c478bd9Sstevel@tonic-gate 	(void) cachefs_sync_metadata(cp);
22197c478bd9Sstevel@tonic-gate }
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate static void
drop_backvp(cnode_t * cp)22227c478bd9Sstevel@tonic-gate drop_backvp(cnode_t *cp)
22237c478bd9Sstevel@tonic-gate {
22247c478bd9Sstevel@tonic-gate 	if (cp->c_backvp) {
22257c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->c_statelock);
22267c478bd9Sstevel@tonic-gate 		if (cp->c_backvp) {
22277c478bd9Sstevel@tonic-gate 			/* dump any pages, may be a dirty one */
22287c478bd9Sstevel@tonic-gate 			(void) VOP_PUTPAGE(cp->c_backvp, (offset_t)0, 0,
2229*da6c28aaSamw 			    B_INVAL | B_TRUNC, kcred, NULL);
22307c478bd9Sstevel@tonic-gate 		}
22317c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
22327c478bd9Sstevel@tonic-gate 	}
22337c478bd9Sstevel@tonic-gate }
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate static void
allow_pendrm(cnode_t * cp)22367c478bd9Sstevel@tonic-gate allow_pendrm(cnode_t *cp)
22377c478bd9Sstevel@tonic-gate {
22387c478bd9Sstevel@tonic-gate 	if (cp->c_flags & CN_PENDRM) {
22397c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->c_statelock);
22407c478bd9Sstevel@tonic-gate 		if (cp->c_flags & CN_PENDRM) {
22417c478bd9Sstevel@tonic-gate 			cp->c_flags &= ~CN_PENDRM;
22427c478bd9Sstevel@tonic-gate 			VN_RELE(CTOV(cp));
22437c478bd9Sstevel@tonic-gate 		}
22447c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
22457c478bd9Sstevel@tonic-gate 	}
22467c478bd9Sstevel@tonic-gate }
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate static void
cachefs_modified_fix(fscache_t * fscp)22497c478bd9Sstevel@tonic-gate cachefs_modified_fix(fscache_t *fscp)
22507c478bd9Sstevel@tonic-gate {
22517c478bd9Sstevel@tonic-gate 	cnode_t *cp;
22527c478bd9Sstevel@tonic-gate 	int error = 0;
22537c478bd9Sstevel@tonic-gate 	rl_entry_t rl_ent;
22547c478bd9Sstevel@tonic-gate 	cfs_cid_t cid;
22557c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
22567c478bd9Sstevel@tonic-gate 	enum cachefs_rl_type type;
22577c478bd9Sstevel@tonic-gate 	cachefs_metadata_t *mdp;
22587c478bd9Sstevel@tonic-gate 	int timedout = 0;
22597c478bd9Sstevel@tonic-gate 	struct vattr va;
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 	/* XXX just return if fs is in error ro mode */
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	/* lock out other users of the MF list */
22647c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_mflock);
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 	/* move the modified entries for this file system to the MF list */
22677c478bd9Sstevel@tonic-gate 	cachefs_move_modified_to_mf(cachep, fscp);
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 	rl_ent.rl_current = CACHEFS_RL_MF;
22707c478bd9Sstevel@tonic-gate 	for (;;) {
22717c478bd9Sstevel@tonic-gate 		/* get the next entry on the MF list */
22727c478bd9Sstevel@tonic-gate 		error = cachefs_rlent_data(cachep, &rl_ent, NULL);
22737c478bd9Sstevel@tonic-gate 		if (error) {
22747c478bd9Sstevel@tonic-gate 			error = 0;
22757c478bd9Sstevel@tonic-gate 			break;
22767c478bd9Sstevel@tonic-gate 		}
22777c478bd9Sstevel@tonic-gate 		ASSERT(fscp->fs_cfsid == rl_ent.rl_fsid);
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 		/* get the cnode for the file */
22807c478bd9Sstevel@tonic-gate 		cid.cid_fileno = rl_ent.rl_fileno;
22817c478bd9Sstevel@tonic-gate 		cid.cid_flags = rl_ent.rl_local ? CFS_CID_LOCAL : 0;
22827c478bd9Sstevel@tonic-gate 		error = cachefs_cnode_make(&cid, fscp,
22837c478bd9Sstevel@tonic-gate 		    NULL, NULL, NULL, kcred, 0, &cp);
22847c478bd9Sstevel@tonic-gate 		if (error) {
22857c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
22867c478bd9Sstevel@tonic-gate 			CFS_DEBUG(CFSDEBUG_IOCTL)
22877c478bd9Sstevel@tonic-gate 				printf("cachefs: mf: could not find %llu\n",
22887c478bd9Sstevel@tonic-gate 				    (u_longlong_t)cid.cid_fileno);
22897c478bd9Sstevel@tonic-gate 			delay(5*hz);
22907c478bd9Sstevel@tonic-gate #endif
22917c478bd9Sstevel@tonic-gate 			/* XXX this will loop forever, maybe put fs in */
22927c478bd9Sstevel@tonic-gate 			/*   ro mode */
22937c478bd9Sstevel@tonic-gate 			continue;
22947c478bd9Sstevel@tonic-gate 		}
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->c_statelock);
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 		mdp = &cp->c_metadata;
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 		/* if a regular file that has not been pushed */
23017c478bd9Sstevel@tonic-gate 		if ((cp->c_attr.va_type == VREG) &&
23027c478bd9Sstevel@tonic-gate 		    (((mdp->md_flags & (MD_PUSHDONE | MD_PUTPAGE)) ==
23037c478bd9Sstevel@tonic-gate 		    MD_PUTPAGE))) {
23047c478bd9Sstevel@tonic-gate 			/* move the file to lost+found */
23057c478bd9Sstevel@tonic-gate 			error = cachefs_cnode_lostfound(cp, NULL);
23067c478bd9Sstevel@tonic-gate 			if (error) {
23077c478bd9Sstevel@tonic-gate 				/* XXX put fs in ro mode */
23087c478bd9Sstevel@tonic-gate 				/* XXX need to drain MF list */
23097c478bd9Sstevel@tonic-gate 				panic("lostfound failed %d", error);
23107c478bd9Sstevel@tonic-gate 			}
23117c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->c_statelock);
23127c478bd9Sstevel@tonic-gate 			VN_RELE(CTOV(cp));
23137c478bd9Sstevel@tonic-gate 			continue;
23147c478bd9Sstevel@tonic-gate 		}
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 		/* if a local file */
23177c478bd9Sstevel@tonic-gate 		if (cp->c_id.cid_flags & CFS_CID_LOCAL) {
23187c478bd9Sstevel@tonic-gate 			/* if the file was not created */
23197c478bd9Sstevel@tonic-gate 			if ((cp->c_metadata.md_flags & MD_CREATEDONE) == 0) {
23207c478bd9Sstevel@tonic-gate 				/* do not allow cnode to be used */
23217c478bd9Sstevel@tonic-gate 				cachefs_cnode_stale(cp);
23227c478bd9Sstevel@tonic-gate 				mutex_exit(&cp->c_statelock);
23237c478bd9Sstevel@tonic-gate 				VN_RELE(CTOV(cp));
23247c478bd9Sstevel@tonic-gate 				continue;
23257c478bd9Sstevel@tonic-gate 			}
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 			/* save the local fileno for later getattrs */
23287c478bd9Sstevel@tonic-gate 			mdp->md_localfileno = cp->c_id.cid_fileno;
23297c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->c_statelock);
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 			/* register the mapping from old to new fileno */
23327c478bd9Sstevel@tonic-gate 			mutex_enter(&fscp->fs_fslock);
23337c478bd9Sstevel@tonic-gate 			cachefs_inum_register(fscp, cp->c_attr.va_nodeid,
23347c478bd9Sstevel@tonic-gate 			    mdp->md_localfileno);
23357c478bd9Sstevel@tonic-gate 			cachefs_inum_register(fscp, mdp->md_localfileno, 0);
23367c478bd9Sstevel@tonic-gate 			mutex_exit(&fscp->fs_fslock);
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 			/* move to new location in the cache */
23397c478bd9Sstevel@tonic-gate 			cachefs_cnode_move(cp);
23407c478bd9Sstevel@tonic-gate 			mutex_enter(&cp->c_statelock);
23417c478bd9Sstevel@tonic-gate 		}
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 		/* else if a modified file that needs to have its mode fixed */
23447c478bd9Sstevel@tonic-gate 		else if ((cp->c_metadata.md_flags & MD_FILE) &&
23457c478bd9Sstevel@tonic-gate 		    (cp->c_attr.va_type == VREG)) {
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 			if (cp->c_frontvp == NULL)
23487c478bd9Sstevel@tonic-gate 				(void) cachefs_getfrontfile(cp);
23497c478bd9Sstevel@tonic-gate 			if (cp->c_frontvp) {
23507c478bd9Sstevel@tonic-gate 				/* mark file as no longer modified */
23517c478bd9Sstevel@tonic-gate 				va.va_mode = 0666;
23527c478bd9Sstevel@tonic-gate 				va.va_mask = AT_MODE;
23537c478bd9Sstevel@tonic-gate 				error = VOP_SETATTR(cp->c_frontvp, &va,
23547c478bd9Sstevel@tonic-gate 				    0, kcred, NULL);
23557c478bd9Sstevel@tonic-gate 				if (error) {
23567c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN,
23577c478bd9Sstevel@tonic-gate 					    "Cannot change ff mode.\n");
23587c478bd9Sstevel@tonic-gate 				}
23597c478bd9Sstevel@tonic-gate 			}
23607c478bd9Sstevel@tonic-gate 		}
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 		/* if there is a rl entry, put it on the correct list */
23647c478bd9Sstevel@tonic-gate 		if (mdp->md_rlno) {
23657c478bd9Sstevel@tonic-gate 			if (mdp->md_flags & MD_PACKED) {
23667c478bd9Sstevel@tonic-gate 				if ((mdp->md_flags & MD_POPULATED) ||
23677c478bd9Sstevel@tonic-gate 				    ((mdp->md_flags & MD_FILE) == 0))
23687c478bd9Sstevel@tonic-gate 					type = CACHEFS_RL_PACKED;
23697c478bd9Sstevel@tonic-gate 				else
23707c478bd9Sstevel@tonic-gate 					type = CACHEFS_RL_PACKED_PENDING;
23717c478bd9Sstevel@tonic-gate 				cachefs_rlent_moveto(fscp->fs_cache, type,
23727c478bd9Sstevel@tonic-gate 				    mdp->md_rlno, mdp->md_frontblks);
23737c478bd9Sstevel@tonic-gate 				mdp->md_rltype = type;
23747c478bd9Sstevel@tonic-gate 			} else if (mdp->md_flags & MD_FILE) {
23757c478bd9Sstevel@tonic-gate 				type = CACHEFS_RL_ACTIVE;
23767c478bd9Sstevel@tonic-gate 				cachefs_rlent_moveto(fscp->fs_cache, type,
23777c478bd9Sstevel@tonic-gate 				    mdp->md_rlno, mdp->md_frontblks);
23787c478bd9Sstevel@tonic-gate 				mdp->md_rltype = type;
23797c478bd9Sstevel@tonic-gate 			} else {
23807c478bd9Sstevel@tonic-gate 				type = CACHEFS_RL_FREE;
23817c478bd9Sstevel@tonic-gate 				cachefs_rlent_moveto(fscp->fs_cache, type,
23827c478bd9Sstevel@tonic-gate 				    mdp->md_rlno, 0);
23837c478bd9Sstevel@tonic-gate 				filegrp_ffrele(cp->c_filegrp);
23847c478bd9Sstevel@tonic-gate 				mdp->md_rlno = 0;
23857c478bd9Sstevel@tonic-gate 				mdp->md_rltype = CACHEFS_RL_NONE;
23867c478bd9Sstevel@tonic-gate 			}
23877c478bd9Sstevel@tonic-gate 		}
23887c478bd9Sstevel@tonic-gate 		mdp->md_flags &= ~(MD_CREATEDONE | MD_PUTPAGE |
23897c478bd9Sstevel@tonic-gate 		    MD_PUSHDONE | MD_MAPPING);
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 		/* if a directory, populate it */
23927c478bd9Sstevel@tonic-gate 		if (CTOV(cp)->v_type == VDIR) {
23937c478bd9Sstevel@tonic-gate 			/* XXX hack for now */
23947c478bd9Sstevel@tonic-gate 			mdp->md_flags |= MD_INVALREADDIR;
23957c478bd9Sstevel@tonic-gate 			dnlc_purge_vp(CTOV(cp));
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate 			mdp->md_flags |= MD_NEEDATTRS;
23987c478bd9Sstevel@tonic-gate 		}
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 		if (!timedout) {
24017c478bd9Sstevel@tonic-gate 			error = CFSOP_CHECK_COBJECT(fscp, cp, 0, kcred);
24027c478bd9Sstevel@tonic-gate 			if (CFS_TIMEOUT(fscp, error))
24037c478bd9Sstevel@tonic-gate 				timedout = 1;
24047c478bd9Sstevel@tonic-gate 			else if ((error == 0) &&
24057c478bd9Sstevel@tonic-gate 			    ((fscp->fs_info.fi_mntflags & CFS_NOACL) == 0)) {
24067c478bd9Sstevel@tonic-gate 				if (cachefs_vtype_aclok(CTOV(cp)) &&
24077c478bd9Sstevel@tonic-gate 				    ((cp->c_flags & CN_NOCACHE) == 0))
24087c478bd9Sstevel@tonic-gate 					(void) cachefs_cacheacl(cp, NULL);
24097c478bd9Sstevel@tonic-gate 			}
24107c478bd9Sstevel@tonic-gate 		}
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 		cp->c_flags |= CN_UPDATED;
24137c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->c_statelock);
24147c478bd9Sstevel@tonic-gate 		VN_RELE(CTOV(cp));
24157c478bd9Sstevel@tonic-gate 	}
24167c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_mflock);
24177c478bd9Sstevel@tonic-gate }
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate void
cachefs_inum_register(fscache_t * fscp,ino64_t real,ino64_t fake)24207c478bd9Sstevel@tonic-gate cachefs_inum_register(fscache_t *fscp, ino64_t real, ino64_t fake)
24217c478bd9Sstevel@tonic-gate {
24227c478bd9Sstevel@tonic-gate 	cachefs_inum_trans_t *tbl;
24237c478bd9Sstevel@tonic-gate 	int toff, thop;
24247c478bd9Sstevel@tonic-gate 	int i;
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fscp->fs_fslock));
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 	/*
24297c478bd9Sstevel@tonic-gate 	 * first, see if an empty slot exists.
24307c478bd9Sstevel@tonic-gate 	 */
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 	for (i = 0; i < fscp->fs_inum_size; i++)
24337c478bd9Sstevel@tonic-gate 		if (fscp->fs_inum_trans[i].cit_real == 0)
24347c478bd9Sstevel@tonic-gate 			break;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	/*
24377c478bd9Sstevel@tonic-gate 	 * if there are no empty slots, try to grow the table.
24387c478bd9Sstevel@tonic-gate 	 */
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 	if (i >= fscp->fs_inum_size) {
24417c478bd9Sstevel@tonic-gate 		cachefs_inum_trans_t *oldtbl;
24427c478bd9Sstevel@tonic-gate 		int oldsize, newsize = 0;
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 		/*
24457c478bd9Sstevel@tonic-gate 		 * try to fetch a new table size that's bigger than
24467c478bd9Sstevel@tonic-gate 		 * our current size
24477c478bd9Sstevel@tonic-gate 		 */
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate 		for (i = 0; cachefs_hash_sizes[i] != 0; i++)
24507c478bd9Sstevel@tonic-gate 			if (cachefs_hash_sizes[i] > fscp->fs_inum_size) {
24517c478bd9Sstevel@tonic-gate 				newsize = cachefs_hash_sizes[i];
24527c478bd9Sstevel@tonic-gate 				break;
24537c478bd9Sstevel@tonic-gate 			}
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 		/*
24567c478bd9Sstevel@tonic-gate 		 * if we're out of larger twin-primes, give up.  thus,
24577c478bd9Sstevel@tonic-gate 		 * the inode numbers in some directory entries might
24587c478bd9Sstevel@tonic-gate 		 * change at reconnect, and disagree with what stat()
24597c478bd9Sstevel@tonic-gate 		 * says.  this isn't worth panicing over, but it does
24607c478bd9Sstevel@tonic-gate 		 * merit a warning message.
24617c478bd9Sstevel@tonic-gate 		 */
24627c478bd9Sstevel@tonic-gate 		if (newsize == 0) {
24637c478bd9Sstevel@tonic-gate 			/* only print hash table warning once */
24647c478bd9Sstevel@tonic-gate 			if ((fscp->fs_flags & CFS_FS_HASHPRINT) == 0) {
24657c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
24667c478bd9Sstevel@tonic-gate 				    "cachefs: inode hash table full\n");
24677c478bd9Sstevel@tonic-gate 				fscp->fs_flags |= CFS_FS_HASHPRINT;
24687c478bd9Sstevel@tonic-gate 			}
24697c478bd9Sstevel@tonic-gate 			return;
24707c478bd9Sstevel@tonic-gate 		}
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 		/* set up this fscp with a new hash table */
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 		oldtbl = fscp->fs_inum_trans;
24757c478bd9Sstevel@tonic-gate 		oldsize = fscp->fs_inum_size;
24767c478bd9Sstevel@tonic-gate 		fscp->fs_inum_size = newsize;
24777c478bd9Sstevel@tonic-gate 		fscp->fs_inum_trans = (cachefs_inum_trans_t *)
24787c478bd9Sstevel@tonic-gate 		    cachefs_kmem_zalloc(sizeof (cachefs_inum_trans_t) * newsize,
24797c478bd9Sstevel@tonic-gate 			KM_SLEEP);
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 		/*
24827c478bd9Sstevel@tonic-gate 		 * re-insert all of the old values.  this will never
24837c478bd9Sstevel@tonic-gate 		 * go more than one level into recursion-land.
24847c478bd9Sstevel@tonic-gate 		 */
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 		for (i = 0; i < oldsize; i++) {
24877c478bd9Sstevel@tonic-gate 			tbl = oldtbl + i;
24887c478bd9Sstevel@tonic-gate 			if (tbl->cit_real != 0) {
24897c478bd9Sstevel@tonic-gate 				cachefs_inum_register(fscp, tbl->cit_real,
24907c478bd9Sstevel@tonic-gate 				    tbl->cit_fake);
24917c478bd9Sstevel@tonic-gate 			} else {
24927c478bd9Sstevel@tonic-gate 				ASSERT(0);
24937c478bd9Sstevel@tonic-gate 			}
24947c478bd9Sstevel@tonic-gate 		}
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 		if (oldsize > 0)
24977c478bd9Sstevel@tonic-gate 			cachefs_kmem_free(oldtbl, oldsize *
24987c478bd9Sstevel@tonic-gate 			    sizeof (cachefs_inum_trans_t));
24997c478bd9Sstevel@tonic-gate 	}
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	/*
25027c478bd9Sstevel@tonic-gate 	 * compute values for the hash table.  see ken rosen's
25037c478bd9Sstevel@tonic-gate 	 * `elementary number theory and its applications' for one
25047c478bd9Sstevel@tonic-gate 	 * description of double hashing.
25057c478bd9Sstevel@tonic-gate 	 */
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	toff = (int)(real % fscp->fs_inum_size);
25087c478bd9Sstevel@tonic-gate 	thop = (int)(real % (fscp->fs_inum_size - 2)) + 1;
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	/*
25117c478bd9Sstevel@tonic-gate 	 * since we know the hash table isn't full when we get here,
25127c478bd9Sstevel@tonic-gate 	 * this loop shouldn't terminate except via the `break'.
25137c478bd9Sstevel@tonic-gate 	 */
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	for (i = 0; i < fscp->fs_inum_size; i++) {
25167c478bd9Sstevel@tonic-gate 		tbl = fscp->fs_inum_trans + toff;
25177c478bd9Sstevel@tonic-gate 		if ((tbl->cit_real == 0) || (tbl->cit_real == real)) {
25187c478bd9Sstevel@tonic-gate 			tbl->cit_real = real;
25197c478bd9Sstevel@tonic-gate 			tbl->cit_fake = fake;
25207c478bd9Sstevel@tonic-gate 			break;
25217c478bd9Sstevel@tonic-gate 		}
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 		toff += thop;
25247c478bd9Sstevel@tonic-gate 		toff %= fscp->fs_inum_size;
25257c478bd9Sstevel@tonic-gate 	}
25267c478bd9Sstevel@tonic-gate 	ASSERT(i < fscp->fs_inum_size);
25277c478bd9Sstevel@tonic-gate }
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate /*
25307c478bd9Sstevel@tonic-gate  * given an inode number, map it to the inode number that should be
25317c478bd9Sstevel@tonic-gate  * put in a directory entry before its copied out.
25327c478bd9Sstevel@tonic-gate  *
25337c478bd9Sstevel@tonic-gate  * don't call this function unless there is a fscp->fs_inum_trans
25347c478bd9Sstevel@tonic-gate  * table that has real entries in it!
25357c478bd9Sstevel@tonic-gate  */
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate ino64_t
cachefs_inum_real2fake(fscache_t * fscp,ino64_t real)25387c478bd9Sstevel@tonic-gate cachefs_inum_real2fake(fscache_t *fscp, ino64_t real)
25397c478bd9Sstevel@tonic-gate {
25407c478bd9Sstevel@tonic-gate 	cachefs_inum_trans_t *tbl;
25417c478bd9Sstevel@tonic-gate 	ino64_t rc = real;
25427c478bd9Sstevel@tonic-gate 	int toff, thop;
25437c478bd9Sstevel@tonic-gate 	int i;
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 	ASSERT(fscp->fs_inum_size > 0);
25467c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fscp->fs_fslock));
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	toff = (int)(real % fscp->fs_inum_size);
25497c478bd9Sstevel@tonic-gate 	thop = (int)(real % (fscp->fs_inum_size - 2)) + 1;
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	for (i = 0; i < fscp->fs_inum_size; i++) {
25527c478bd9Sstevel@tonic-gate 		tbl = fscp->fs_inum_trans + toff;
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate 		if (tbl->cit_real == 0) {
25557c478bd9Sstevel@tonic-gate 			break;
25567c478bd9Sstevel@tonic-gate 		} else if (tbl->cit_real == real) {
25577c478bd9Sstevel@tonic-gate 			rc = tbl->cit_fake;
25587c478bd9Sstevel@tonic-gate 			break;
25597c478bd9Sstevel@tonic-gate 		}
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate 		toff += thop;
25627c478bd9Sstevel@tonic-gate 		toff %= fscp->fs_inum_size;
25637c478bd9Sstevel@tonic-gate 	}
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	return (rc);
25667c478bd9Sstevel@tonic-gate }
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate /*
25697c478bd9Sstevel@tonic-gate  * Passed a cid, finds the cnode and sets the MD_NEEDATTRS bit
25707c478bd9Sstevel@tonic-gate  * in the metadata.
25717c478bd9Sstevel@tonic-gate  */
25727c478bd9Sstevel@tonic-gate static void
cachefs_iosetneedattrs(fscache_t * fscp,cfs_cid_t * cidp)25737c478bd9Sstevel@tonic-gate cachefs_iosetneedattrs(fscache_t *fscp, cfs_cid_t *cidp)
25747c478bd9Sstevel@tonic-gate {
25757c478bd9Sstevel@tonic-gate 	int error;
25767c478bd9Sstevel@tonic-gate 	cnode_t *cp;
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(cidp, fscp,
25797c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, kcred, 0, &cp);
25807c478bd9Sstevel@tonic-gate 	if (error)
25817c478bd9Sstevel@tonic-gate 		return;
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->c_statelock);
25847c478bd9Sstevel@tonic-gate 	cp->c_metadata.md_flags |= MD_NEEDATTRS;
25857c478bd9Sstevel@tonic-gate 	cp->c_flags |= CN_UPDATED;
25867c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->c_statelock);
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate 	VN_RELE(CTOV(cp));
25897c478bd9Sstevel@tonic-gate }
2590