xref: /titanic_44/usr/src/uts/common/fs/cachefs/cachefs_vfsops.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
5aa59c4cbSrsb  * Common Development and Distribution License (the "License").
6aa59c4cbSrsb  * 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 /*
22aa59c4cbSrsb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/cred.h>
327c478bd9Sstevel@tonic-gate #include <sys/proc.h>
337c478bd9Sstevel@tonic-gate #include <sys/user.h>
347c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
35aa59c4cbSrsb #include <sys/vfs_opreg.h>
367c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
377c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
387c478bd9Sstevel@tonic-gate #include <sys/uio.h>
397c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
427c478bd9Sstevel@tonic-gate #include <sys/mount.h>
437c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
447c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <sys/errno.h>
477c478bd9Sstevel@tonic-gate #include <sys/debug.h>
487c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
497c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
507c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
517c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
527c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
537c478bd9Sstevel@tonic-gate #include <rpc/types.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
567c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_log.h>
577c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
587c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
597c478bd9Sstevel@tonic-gate #include <sys/policy.h>
607c478bd9Sstevel@tonic-gate #include "fs/fs_subr.h"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate extern kmutex_t cachefs_kmem_lock;
637c478bd9Sstevel@tonic-gate kmutex_t cachefs_kstat_key_lock;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* forward declarations */
667c478bd9Sstevel@tonic-gate static int cachefs_remount(struct vfs *, struct mounta *);
677c478bd9Sstevel@tonic-gate static void cachefs_delete_cachep(cachefscache_t *);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	CFS_MAPSIZE	256
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate kmutex_t cachefs_cachelock;			/* Cache list mutex */
727c478bd9Sstevel@tonic-gate cachefscache_t *cachefs_cachelist = NULL;		/* Cache struct list */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate int cachefs_mount_retries = 3;
757c478bd9Sstevel@tonic-gate kmutex_t cachefs_minor_lock;		/* Lock for minor device map */
767c478bd9Sstevel@tonic-gate major_t cachefs_major = 0;
777c478bd9Sstevel@tonic-gate minor_t cachefs_minor = 0;
787c478bd9Sstevel@tonic-gate cachefs_kstat_key_t *cachefs_kstat_key = NULL;
797c478bd9Sstevel@tonic-gate int cachefs_kstat_key_n = 0;
807c478bd9Sstevel@tonic-gate static uint32_t cachefs_nfsv4_warnmsg = FALSE;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * cachefs vfs operations.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate static	int cachefs_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
867c478bd9Sstevel@tonic-gate static	int cachefs_unmount(vfs_t *, int, cred_t *);
877c478bd9Sstevel@tonic-gate static	int cachefs_root(vfs_t *, vnode_t **);
887c478bd9Sstevel@tonic-gate static	int cachefs_statvfs(register vfs_t *, struct statvfs64 *);
897c478bd9Sstevel@tonic-gate static	int cachefs_sync(vfs_t *, short, cred_t *);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Initialize the vfs structure
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate int cachefsfstyp;
957c478bd9Sstevel@tonic-gate int cnodesize = 0;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate int
cachefs_init_vfsops(int fstype)987c478bd9Sstevel@tonic-gate cachefs_init_vfsops(int fstype)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t cachefs_vfsops_template[] = {
101aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .vfs_mount = cachefs_mount },
102aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = cachefs_unmount },
103aa59c4cbSrsb 		VFSNAME_ROOT,		{ .vfs_root = cachefs_root },
104aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = cachefs_statvfs },
105aa59c4cbSrsb 		VFSNAME_SYNC,		{ .vfs_sync = cachefs_sync },
1067c478bd9Sstevel@tonic-gate 		NULL,			NULL
1077c478bd9Sstevel@tonic-gate 	};
1087c478bd9Sstevel@tonic-gate 	int error;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, cachefs_vfsops_template, NULL);
1117c478bd9Sstevel@tonic-gate 	if (error != 0)
1127c478bd9Sstevel@tonic-gate 		return (error);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	cachefsfstyp = fstype;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	return (0);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate dev_t
cachefs_mkmntdev(void)1207c478bd9Sstevel@tonic-gate cachefs_mkmntdev(void)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	dev_t cachefs_dev;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	mutex_enter(&cachefs_minor_lock);
1257c478bd9Sstevel@tonic-gate 	do {
1267c478bd9Sstevel@tonic-gate 		cachefs_minor = (cachefs_minor + 1) & MAXMIN32;
1277c478bd9Sstevel@tonic-gate 		cachefs_dev = makedevice(cachefs_major, cachefs_minor);
1287c478bd9Sstevel@tonic-gate 	} while (vfs_devismounted(cachefs_dev));
1297c478bd9Sstevel@tonic-gate 	mutex_exit(&cachefs_minor_lock);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	return (cachefs_dev);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * vfs operations
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate static int
cachefs_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)1387c478bd9Sstevel@tonic-gate cachefs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	char *data = uap->dataptr;
1417c478bd9Sstevel@tonic-gate 	STRUCT_DECL(cachefs_mountargs, map);
1427c478bd9Sstevel@tonic-gate 	struct cachefsoptions	*cfs_options;
1437c478bd9Sstevel@tonic-gate 	char			*backfs, *cacheid, *cachedir;
1447c478bd9Sstevel@tonic-gate 	vnode_t *cachedirvp = NULL;
1457c478bd9Sstevel@tonic-gate 	vnode_t *backrootvp = NULL;
1467c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = NULL;
1477c478bd9Sstevel@tonic-gate 	fscache_t *fscp = NULL;
1487c478bd9Sstevel@tonic-gate 	cnode_t *cp;
1497c478bd9Sstevel@tonic-gate 	struct fid *cookiep = NULL;
1507c478bd9Sstevel@tonic-gate 	struct vattr *attrp = NULL;
1517c478bd9Sstevel@tonic-gate 	dev_t cachefs_dev;			/* devid for this mount */
1527c478bd9Sstevel@tonic-gate 	int error = 0;
1537c478bd9Sstevel@tonic-gate 	int retries = cachefs_mount_retries;
1547c478bd9Sstevel@tonic-gate 	ino64_t fsid;
1557c478bd9Sstevel@tonic-gate 	cfs_cid_t cid;
1567c478bd9Sstevel@tonic-gate 	char *backmntpt;
1577c478bd9Sstevel@tonic-gate 	ino64_t backfileno;
1587c478bd9Sstevel@tonic-gate 	struct vfs *backvfsp;
1597c478bd9Sstevel@tonic-gate 	size_t strl;
1607c478bd9Sstevel@tonic-gate 	char tmpstr[MAXPATHLEN];
1617c478bd9Sstevel@tonic-gate 	vnode_t *tmpdirvp = NULL;
1627c478bd9Sstevel@tonic-gate 	ulong_t maxfilesizebits;
1637c478bd9Sstevel@tonic-gate 	uint32_t valid_fid;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
1667c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_VFSOP)
1677c478bd9Sstevel@tonic-gate 		printf("cachefs_mount: ENTER cachefs_mntargs %p\n", data);
1687c478bd9Sstevel@tonic-gate #endif
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Make sure we have sufficient privileges.
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
1747c478bd9Sstevel@tonic-gate 		goto out;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * make sure we're mounting on a directory
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	if (mvp->v_type != VDIR) {
1807c478bd9Sstevel@tonic-gate 		error = ENOTDIR;
1817c478bd9Sstevel@tonic-gate 		goto out;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * Determine the zone we're being mounted into, and make sure it's the
1867c478bd9Sstevel@tonic-gate 	 * global zone.
1877c478bd9Sstevel@tonic-gate 	 */
1887c478bd9Sstevel@tonic-gate 	if (getzoneid() == GLOBAL_ZONEID) {
1897c478bd9Sstevel@tonic-gate 		zone_t *mntzone;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
1927c478bd9Sstevel@tonic-gate 		ASSERT(mntzone != NULL);
1937c478bd9Sstevel@tonic-gate 		zone_rele(mntzone);
1947c478bd9Sstevel@tonic-gate 		if (mntzone != curproc->p_zone) {
1957c478bd9Sstevel@tonic-gate 			error = EBUSY;
1967c478bd9Sstevel@tonic-gate 			goto out;
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 	} else {
1997c478bd9Sstevel@tonic-gate 		error = EPERM;
2007c478bd9Sstevel@tonic-gate 		goto out;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT) {
2047c478bd9Sstevel@tonic-gate 		error = cachefs_remount(vfsp, uap);
2057c478bd9Sstevel@tonic-gate 		goto out;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * Assign a unique device id to the mount
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	cachefs_dev = cachefs_mkmntdev();
2127c478bd9Sstevel@tonic-gate #ifdef _LP64
2137c478bd9Sstevel@tonic-gate 	/*
2147c478bd9Sstevel@tonic-gate 	 * It's not a good idea to make fsid bigger since that'll
2157c478bd9Sstevel@tonic-gate 	 * have adverse effects on nfs filehandles.  For now assume that
2167c478bd9Sstevel@tonic-gate 	 * cachefs be used on devices that fit into dev32_t's.
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	if (cachefs_dev == NODEV) {
2197c478bd9Sstevel@tonic-gate 		error = EOVERFLOW;
2207c478bd9Sstevel@tonic-gate 		goto out;
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate #endif
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Copy in the arguments
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	STRUCT_INIT(map, get_udatamodel());
2287c478bd9Sstevel@tonic-gate 	error = copyin(data, STRUCT_BUF(map),
2297c478bd9Sstevel@tonic-gate 			SIZEOF_STRUCT(cachefs_mountargs, DATAMODEL_NATIVE));
2307c478bd9Sstevel@tonic-gate 	if (error) {
2317c478bd9Sstevel@tonic-gate 		goto out;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	cfs_options = (struct cachefsoptions *)STRUCT_FADDR(map, cfs_options);
2357c478bd9Sstevel@tonic-gate 	cacheid = (char *)STRUCT_FGETP(map, cfs_cacheid);
2367c478bd9Sstevel@tonic-gate 	if ((cfs_options->opt_flags &
2377c478bd9Sstevel@tonic-gate 	    (CFS_WRITE_AROUND|CFS_NONSHARED|CFS_BACKFS_NFSV4)) == 0) {
2387c478bd9Sstevel@tonic-gate 		error = EINVAL;
2397c478bd9Sstevel@tonic-gate 		goto out;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 	if ((cfs_options->opt_popsize % MAXBSIZE) != 0) {
2427c478bd9Sstevel@tonic-gate 		error = EINVAL;
2437c478bd9Sstevel@tonic-gate 		goto out;
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * Get the cache directory vp
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	/*LINTED 32-bit pointer casting okay*/
2497c478bd9Sstevel@tonic-gate 	cachedir = (char *)STRUCT_FGETP(map, cfs_cachedir);
2507c478bd9Sstevel@tonic-gate 	error = lookupname(cachedir, UIO_USERSPACE, FOLLOW,
2517c478bd9Sstevel@tonic-gate 			NULLVPP, &cachedirvp);
2527c478bd9Sstevel@tonic-gate 	if (error)
2537c478bd9Sstevel@tonic-gate 		goto out;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Make sure the thing we just looked up is a directory
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	if (cachedirvp->v_type != VDIR) {
2597c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs_mount: cachedir not a directory\n");
2607c478bd9Sstevel@tonic-gate 		error = EINVAL;
2617c478bd9Sstevel@tonic-gate 		goto out;
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * Make sure the cache doesn't live in cachefs!
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	if (vn_matchops(cachedirvp, cachefs_getvnodeops())) {
2687c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs_mount: cachedir in cachefs!\n");
2697c478bd9Sstevel@tonic-gate 		error = EINVAL;
2707c478bd9Sstevel@tonic-gate 		goto out;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/* if the backfs is mounted */
2747c478bd9Sstevel@tonic-gate 	/*LINTED 32-bit pointer casting okay*/
2757c478bd9Sstevel@tonic-gate 	if ((backfs = STRUCT_FGETP(map, cfs_backfs)) != NULL) {
2767c478bd9Sstevel@tonic-gate 		/*
2777c478bd9Sstevel@tonic-gate 		 * Get the back file system root vp
2787c478bd9Sstevel@tonic-gate 		 */
2797c478bd9Sstevel@tonic-gate 		error = lookupname(backfs, UIO_USERSPACE, FOLLOW,
2807c478bd9Sstevel@tonic-gate 			NULLVPP, &backrootvp);
2817c478bd9Sstevel@tonic-gate 		if (error)
2827c478bd9Sstevel@tonic-gate 			goto out;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		/*
2857c478bd9Sstevel@tonic-gate 		 * Make sure the thing we just looked up is a directory
2867c478bd9Sstevel@tonic-gate 		 * and a root of a file system
2877c478bd9Sstevel@tonic-gate 		 */
2887c478bd9Sstevel@tonic-gate 		if (backrootvp->v_type != VDIR ||
2897c478bd9Sstevel@tonic-gate 		    !(backrootvp->v_flag & VROOT)) {
2907c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
2917c478bd9Sstevel@tonic-gate 			    "cachefs_mount: backpath not a directory\n");
2927c478bd9Sstevel@tonic-gate 			error = EINVAL;
2937c478bd9Sstevel@tonic-gate 			goto out;
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		/*
2977c478bd9Sstevel@tonic-gate 		 * Get the fid and attributes for the root of the
2987c478bd9Sstevel@tonic-gate 		 * backfilesystem, except if NFSv4 is in use,
2997c478bd9Sstevel@tonic-gate 		 * in which case we get the attributes only (the
3007c478bd9Sstevel@tonic-gate 		 * (VOP_FID() operation called by cachefs_get_cookie()
3017c478bd9Sstevel@tonic-gate 		 * is not supported in NFSv4).
3027c478bd9Sstevel@tonic-gate 		 */
3037c478bd9Sstevel@tonic-gate 		cookiep = cachefs_kmem_alloc(sizeof (struct fid), KM_SLEEP);
3047c478bd9Sstevel@tonic-gate 		attrp = cachefs_kmem_alloc(sizeof (struct vattr), KM_SLEEP);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		if ((cfs_options->opt_flags & CFS_BACKFS_NFSV4)) {
3077c478bd9Sstevel@tonic-gate 			valid_fid = FALSE;
3087c478bd9Sstevel@tonic-gate 		} else {
3097c478bd9Sstevel@tonic-gate 			valid_fid = TRUE;
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 		error = cachefs_getcookie(backrootvp, cookiep, attrp, cr,
3127c478bd9Sstevel@tonic-gate 						valid_fid);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		if (error)
3157c478bd9Sstevel@tonic-gate 			goto out;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		backmntpt = backfs;
3187c478bd9Sstevel@tonic-gate 		backfileno = attrp->va_nodeid;
3197c478bd9Sstevel@tonic-gate 		backvfsp = backrootvp->v_vfsp;
3207c478bd9Sstevel@tonic-gate 	} else {
3217c478bd9Sstevel@tonic-gate 		backmntpt = NULL;
3227c478bd9Sstevel@tonic-gate 		backfileno = 0;
3237c478bd9Sstevel@tonic-gate 		backvfsp = NULL;
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate again:
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/*
3297c478bd9Sstevel@tonic-gate 	 * In SVR4 it's not acceptable to stack up mounts
3307c478bd9Sstevel@tonic-gate 	 * unless MS_OVERLAY specified.
3317c478bd9Sstevel@tonic-gate 	 */
3327c478bd9Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
3337c478bd9Sstevel@tonic-gate 	if (((uap->flags & MS_OVERLAY) == 0) &&
3347c478bd9Sstevel@tonic-gate 	    ((mvp->v_count != 1) || (mvp->v_flag & VROOT))) {
3357c478bd9Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
3367c478bd9Sstevel@tonic-gate 		error = EBUSY;
3377c478bd9Sstevel@tonic-gate 		goto out;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * Lock out other mounts and unmounts until we safely have
3437c478bd9Sstevel@tonic-gate 	 * a mounted fscache object.
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	mutex_enter(&cachefs_cachelock);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/*
3487c478bd9Sstevel@tonic-gate 	 * Find the cache structure
3497c478bd9Sstevel@tonic-gate 	 */
3507c478bd9Sstevel@tonic-gate 	for (cachep = cachefs_cachelist; cachep != NULL;
3517c478bd9Sstevel@tonic-gate 		cachep = cachep->c_next) {
3527c478bd9Sstevel@tonic-gate 		if (cachep->c_dirvp == cachedirvp)
3537c478bd9Sstevel@tonic-gate 			break;
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/* if the cache object does not exist, then create it */
3577c478bd9Sstevel@tonic-gate 	if (cachep == NULL) {
3587c478bd9Sstevel@tonic-gate 		cachep = cachefs_cache_create();
3597c478bd9Sstevel@tonic-gate 		error = cachefs_cache_activate_ro(cachep, cachedirvp);
3607c478bd9Sstevel@tonic-gate 		if (error) {
3617c478bd9Sstevel@tonic-gate 			cachefs_cache_destroy(cachep);
3627c478bd9Sstevel@tonic-gate 			cachep = NULL;
3637c478bd9Sstevel@tonic-gate 			goto out;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 		if ((cfs_options->opt_flags & CFS_NOFILL) == 0)
3667c478bd9Sstevel@tonic-gate 			cachefs_cache_activate_rw(cachep);
3677c478bd9Sstevel@tonic-gate 		else
3687c478bd9Sstevel@tonic-gate 			cfs_options->opt_flags &= ~CFS_NOFILL;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		cachep->c_next = cachefs_cachelist;
3717c478bd9Sstevel@tonic-gate 		cachefs_cachelist = cachep;
3727c478bd9Sstevel@tonic-gate 	} else if (cfs_options->opt_flags & CFS_NOFILL) {
3737c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
3747c478bd9Sstevel@tonic-gate 		    "CacheFS: attempt to convert nonempty cache "
3757c478bd9Sstevel@tonic-gate 		    "to NOFILL mode");
3767c478bd9Sstevel@tonic-gate 		error = EINVAL;
3777c478bd9Sstevel@tonic-gate 		goto out;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/* get the fscache id for this name */
3817c478bd9Sstevel@tonic-gate 	error = fscache_name_to_fsid(cachep, cacheid, &fsid);
3827c478bd9Sstevel@tonic-gate 	if (error) {
3837c478bd9Sstevel@tonic-gate 		fsid = 0;
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/* find the fscache object for this mount point or create it */
3877c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_fslistlock);
3887c478bd9Sstevel@tonic-gate 	fscp = fscache_list_find(cachep, fsid);
3897c478bd9Sstevel@tonic-gate 	if (fscp == NULL) {
3907c478bd9Sstevel@tonic-gate 		fscp = fscache_create(cachep);
3917c478bd9Sstevel@tonic-gate 		error = fscache_activate(fscp, fsid, cacheid,
3927c478bd9Sstevel@tonic-gate 			cfs_options, backfileno);
3937c478bd9Sstevel@tonic-gate 		if (error) {
3947c478bd9Sstevel@tonic-gate 			fscache_destroy(fscp);
3957c478bd9Sstevel@tonic-gate 			fscp = NULL;
3967c478bd9Sstevel@tonic-gate 			mutex_exit(&cachep->c_fslistlock);
3977c478bd9Sstevel@tonic-gate 			if ((error == ENOSPC) && (retries-- > 0)) {
3987c478bd9Sstevel@tonic-gate 				mutex_exit(&cachefs_cachelock);
3997c478bd9Sstevel@tonic-gate 				delay(6 * hz);
4007c478bd9Sstevel@tonic-gate 				goto again;
4017c478bd9Sstevel@tonic-gate 			}
4027c478bd9Sstevel@tonic-gate 			goto out;
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 		fscache_list_add(cachep, fscp);
4057c478bd9Sstevel@tonic-gate 	} else {
406*da6c28aaSamw 		/* compare the options to make sure they are compatible */
4077c478bd9Sstevel@tonic-gate 		error = fscache_compare_options(fscp, cfs_options);
4087c478bd9Sstevel@tonic-gate 		if (error) {
4097c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
4107c478bd9Sstevel@tonic-gate 				"CacheFS: mount failed, options do not match.");
4117c478bd9Sstevel@tonic-gate 			fscp = NULL;
4127c478bd9Sstevel@tonic-gate 			mutex_exit(&cachep->c_fslistlock);
4137c478bd9Sstevel@tonic-gate 			goto out;
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		/* copy options into the fscache */
4177c478bd9Sstevel@tonic-gate 		mutex_enter(&fscp->fs_fslock);
4187c478bd9Sstevel@tonic-gate 		fscp->fs_info.fi_mntflags = cfs_options->opt_flags;
4197c478bd9Sstevel@tonic-gate 		fscp->fs_info.fi_popsize = cfs_options->opt_popsize;
4207c478bd9Sstevel@tonic-gate 		fscp->fs_info.fi_fgsize = cfs_options->opt_fgsize;
4217c478bd9Sstevel@tonic-gate 		fscp->fs_flags |= CFS_FS_DIRTYINFO;
4227c478bd9Sstevel@tonic-gate 		mutex_exit(&fscp->fs_fslock);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	fscache_hold(fscp);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	error = 0;
4277c478bd9Sstevel@tonic-gate 	if (fscp->fs_fscdirvp) {
4287c478bd9Sstevel@tonic-gate 		error = VOP_LOOKUP(fscp->fs_fscdirvp, CACHEFS_DLOG_FILE,
429*da6c28aaSamw 		    &tmpdirvp, NULL, 0, NULL, kcred, NULL, NULL, NULL);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		/*
4327c478bd9Sstevel@tonic-gate 		 * If a log file exists and the cache is being mounted without
4337c478bd9Sstevel@tonic-gate 		 * the snr (aka disconnectable) option, return an error.
4347c478bd9Sstevel@tonic-gate 		 */
4357c478bd9Sstevel@tonic-gate 		if ((error == 0) &&
4367c478bd9Sstevel@tonic-gate 		    !(cfs_options->opt_flags & CFS_DISCONNECTABLE)) {
4377c478bd9Sstevel@tonic-gate 			mutex_exit(&cachep->c_fslistlock);
4387c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "cachefs: log exists and "
4397c478bd9Sstevel@tonic-gate 			    "disconnectable option not specified\n");
4407c478bd9Sstevel@tonic-gate 			error = EINVAL;
4417c478bd9Sstevel@tonic-gate 			goto out;
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/*
4467c478bd9Sstevel@tonic-gate 	 * Acquire the name of the mount point
4477c478bd9Sstevel@tonic-gate 	 */
4487c478bd9Sstevel@tonic-gate 	if (fscp->fs_mntpt == NULL) {
4497c478bd9Sstevel@tonic-gate 		/*
4507c478bd9Sstevel@tonic-gate 		 * the string length returned by copystr includes the
4517c478bd9Sstevel@tonic-gate 		 * terminating NULL character, unless a NULL string is
4527c478bd9Sstevel@tonic-gate 		 * passed in, then the string length is unchanged.
4537c478bd9Sstevel@tonic-gate 		 */
4547c478bd9Sstevel@tonic-gate 		strl = 0;
4557c478bd9Sstevel@tonic-gate 		tmpstr[0] = '\0';
4567c478bd9Sstevel@tonic-gate 		(void) copyinstr(uap->dir, tmpstr, MAXPATHLEN, &strl);
4577c478bd9Sstevel@tonic-gate 		if (strl > 1) {
4587c478bd9Sstevel@tonic-gate 			fscp->fs_mntpt = kmem_alloc(strl, KM_SLEEP);
4597c478bd9Sstevel@tonic-gate 			(void) strncpy(fscp->fs_mntpt, tmpstr, strl);
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 		/*
4627c478bd9Sstevel@tonic-gate 		 * else fscp->fs_mntpt is unchanged(still NULL) try again
4637c478bd9Sstevel@tonic-gate 		 * next time
4647c478bd9Sstevel@tonic-gate 		 */
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/*
4687c478bd9Sstevel@tonic-gate 	 * Acquire the name of the server
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	if (fscp->fs_hostname == NULL) {
4717c478bd9Sstevel@tonic-gate 		strl = 0;
4727c478bd9Sstevel@tonic-gate 		tmpstr[0] = '\0';
4737c478bd9Sstevel@tonic-gate 		/*LINTED 32-bit pointer casting okay*/
4747c478bd9Sstevel@tonic-gate 		(void) copyinstr((char *)STRUCT_FGETP(map, cfs_hostname),
4757c478bd9Sstevel@tonic-gate 				tmpstr, MAXPATHLEN, &strl);
4767c478bd9Sstevel@tonic-gate 		if (strl > 1) {
4777c478bd9Sstevel@tonic-gate 			fscp->fs_hostname = kmem_alloc(strl, KM_SLEEP);
4787c478bd9Sstevel@tonic-gate 			(void) strncpy(fscp->fs_hostname, tmpstr, strl);
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 		/*
4817c478bd9Sstevel@tonic-gate 		 * else fscp->fs_hostname remains unchanged (is still NULL)
4827c478bd9Sstevel@tonic-gate 		 */
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * Acquire name of the back filesystem
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	if (fscp->fs_backfsname == NULL) {
4897c478bd9Sstevel@tonic-gate 		strl = 0;
4907c478bd9Sstevel@tonic-gate 		tmpstr[0] = '\0';
4917c478bd9Sstevel@tonic-gate 		/*LINTED 32-bit pointer casting okay*/
4927c478bd9Sstevel@tonic-gate 		(void) copyinstr((char *)STRUCT_FGETP(map, cfs_backfsname),
4937c478bd9Sstevel@tonic-gate 				tmpstr, MAXPATHLEN, &strl);
4947c478bd9Sstevel@tonic-gate 		if (strl > 1) {
4957c478bd9Sstevel@tonic-gate 			fscp->fs_backfsname = kmem_alloc(strl, KM_SLEEP);
4967c478bd9Sstevel@tonic-gate 			(void) strncpy(fscp->fs_backfsname, tmpstr, strl);
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 		/*
4997c478bd9Sstevel@tonic-gate 		 * else fscp->fs_backfsname remains unchanged (is still NULL)
5007c478bd9Sstevel@tonic-gate 		 */
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	backfileno = fscp->fs_info.fi_root;
5047c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_fslistlock);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/* see if fscache object is already mounted, it not, make it so */
5077c478bd9Sstevel@tonic-gate 	error = fscache_mounted(fscp, vfsp, backvfsp);
5087c478bd9Sstevel@tonic-gate 	if (error) {
5097c478bd9Sstevel@tonic-gate 		/* fs cache was already mounted */
5107c478bd9Sstevel@tonic-gate 		error = EBUSY;
5117c478bd9Sstevel@tonic-gate 		goto out;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	cachefs_kstat_mount(fscp, uap->dir, backmntpt, cachedir, cacheid);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/* set nfs style time out parameters */
5177c478bd9Sstevel@tonic-gate 	fscache_acset(fscp, STRUCT_FGET(map, cfs_acregmin),
5187c478bd9Sstevel@tonic-gate 	    STRUCT_FGET(map, cfs_acregmax),
5197c478bd9Sstevel@tonic-gate 	    STRUCT_FGET(map, cfs_acdirmin), STRUCT_FGET(map, cfs_acdirmax));
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = cachefs_dev;
5227c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)fscp;
5237c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, cachefs_dev, cachefsfstyp);
5247c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = cachefsfstyp;
5257c478bd9Sstevel@tonic-gate 	if (backvfsp)
5267c478bd9Sstevel@tonic-gate 		vfsp->vfs_bsize = backvfsp->vfs_bsize;
5277c478bd9Sstevel@tonic-gate 	else
5287c478bd9Sstevel@tonic-gate 		vfsp->vfs_bsize = MAXBSIZE;	/* XXX */
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	/* make a cnode for the root of the file system */
5317c478bd9Sstevel@tonic-gate 	cid.cid_flags = 0;
5327c478bd9Sstevel@tonic-gate 	cid.cid_fileno = backfileno;
5337c478bd9Sstevel@tonic-gate 	error = cachefs_cnode_make(&cid, fscp, (valid_fid ? cookiep : NULL),
5347c478bd9Sstevel@tonic-gate 				attrp, backrootvp, cr, CN_ROOT, &cp);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (error) {
5377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cachefs_mount: can't create root cnode\n");
5387c478bd9Sstevel@tonic-gate 		goto out;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	/* stick the root cnode in the fscache object */
5427c478bd9Sstevel@tonic-gate 	mutex_enter(&fscp->fs_fslock);
5437c478bd9Sstevel@tonic-gate 	fscp->fs_rootvp = CTOV(cp);
5447c478bd9Sstevel@tonic-gate 	fscp->fs_rootvp->v_flag |= VROOT;
5457c478bd9Sstevel@tonic-gate 	fscp->fs_rootvp->v_type |= cp->c_attr.va_type;
5467c478bd9Sstevel@tonic-gate 	ASSERT(fscp->fs_rootvp->v_type == VDIR);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * Get the maxfilesize bits of the back file system.
5507c478bd9Sstevel@tonic-gate 	 */
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	error = VOP_PATHCONF(backrootvp, _PC_FILESIZEBITS, &maxfilesizebits,
553*da6c28aaSamw 		    kcred, NULL);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (error) {
5567c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
5577c478bd9Sstevel@tonic-gate 	"cachefs_mount: Can't get the FILESIZEBITS of the back root vnode \n");
5587c478bd9Sstevel@tonic-gate 		goto out;
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	fscp->fs_offmax = (1LL << (maxfilesizebits - 1)) - 1;
5627c478bd9Sstevel@tonic-gate 	mutex_exit(&fscp->fs_fslock);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* remove the unmount file if it is there */
565*da6c28aaSamw 	(void) VOP_REMOVE(fscp->fs_fscdirvp, CACHEFS_UNMNT_FILE, kcred, NULL,
566*da6c28aaSamw 	    0);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	/* wake up the cache worker if ANY packed pending work */
5697c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_contentslock);
5707c478bd9Sstevel@tonic-gate 	if (cachep->c_flags & CACHE_PACKED_PENDING)
5717c478bd9Sstevel@tonic-gate 		cv_signal(&cachep->c_cwcv);
5727c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_contentslock);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	/*
5757c478bd9Sstevel@tonic-gate 	 * Warn that caching is disabled with NFSv4 first time around.
5767c478bd9Sstevel@tonic-gate 	 */
5777c478bd9Sstevel@tonic-gate 	if (!cachefs_nfsv4_warnmsg && CFS_ISFS_BACKFS_NFSV4(fscp)) {
5787c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
5797c478bd9Sstevel@tonic-gate 			"Cachefs has detected a mount with NFSv4: caching will"
5807c478bd9Sstevel@tonic-gate 			" be disabled for this and other NFSv4 mounts\n");
5817c478bd9Sstevel@tonic-gate 		cachefs_nfsv4_warnmsg = TRUE;
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate out:
5857c478bd9Sstevel@tonic-gate 	/*
5867c478bd9Sstevel@tonic-gate 	 * make a log entry, if appropriate
5877c478bd9Sstevel@tonic-gate 	 */
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if ((cachep != NULL) &&
5907c478bd9Sstevel@tonic-gate 	    CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_MOUNT))
5917c478bd9Sstevel@tonic-gate 		cachefs_log_mount(cachep, error, vfsp, fscp,
5927c478bd9Sstevel@tonic-gate 		    uap->dir, UIO_USERSPACE,
5937c478bd9Sstevel@tonic-gate 		    (STRUCT_BUF(map) != NULL) ? cacheid : NULL);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/*
5967c478bd9Sstevel@tonic-gate 	 * Cleanup our mess
5977c478bd9Sstevel@tonic-gate 	 */
5987c478bd9Sstevel@tonic-gate 	if (cookiep != NULL)
5997c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(cookiep, sizeof (struct fid));
6007c478bd9Sstevel@tonic-gate 	if (cachedirvp != NULL)
6017c478bd9Sstevel@tonic-gate 		VN_RELE(cachedirvp);
6027c478bd9Sstevel@tonic-gate 	if (backrootvp != NULL)
6037c478bd9Sstevel@tonic-gate 		VN_RELE(backrootvp);
6047c478bd9Sstevel@tonic-gate 	if (fscp)
6057c478bd9Sstevel@tonic-gate 		fscache_rele(fscp);
6067c478bd9Sstevel@tonic-gate 	if (attrp)
6077c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(attrp, sizeof (struct vattr));
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (error) {
6107c478bd9Sstevel@tonic-gate 		if (cachep) {
6117c478bd9Sstevel@tonic-gate 			int xx;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 			/* lock the cachep's fslist */
6147c478bd9Sstevel@tonic-gate 			mutex_enter(&cachep->c_fslistlock);
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 			/*
6177c478bd9Sstevel@tonic-gate 			 * gc isn't necessary for list_mounted(), but
6187c478bd9Sstevel@tonic-gate 			 * we want to do it anyway.
6197c478bd9Sstevel@tonic-gate 			 */
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 			fscache_list_gc(cachep);
6227c478bd9Sstevel@tonic-gate 			xx = fscache_list_mounted(cachep);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 			mutex_exit(&cachep->c_fslistlock);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 			/* if no more references to this cachep, punt it. */
6277c478bd9Sstevel@tonic-gate 			if (xx == 0)
6287c478bd9Sstevel@tonic-gate 				cachefs_delete_cachep(cachep);
6297c478bd9Sstevel@tonic-gate 			mutex_exit(&cachefs_cachelock);
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 	} else {
6327c478bd9Sstevel@tonic-gate 		mutex_exit(&cachefs_cachelock);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
6367c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_VFSOP)
6377c478bd9Sstevel@tonic-gate 		printf("cachefs_mount: EXIT\n");
6387c478bd9Sstevel@tonic-gate #endif
6397c478bd9Sstevel@tonic-gate 	return (error);
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate void
cachefs_kstat_mount(struct fscache * fscp,char * umountpoint,char * ubackfs,char * ucachedir,char * cacheid)6437c478bd9Sstevel@tonic-gate cachefs_kstat_mount(struct fscache *fscp,
6447c478bd9Sstevel@tonic-gate     char *umountpoint, char *ubackfs, char *ucachedir, char *cacheid)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
6477c478bd9Sstevel@tonic-gate 	cachefs_kstat_key_t *key;
6487c478bd9Sstevel@tonic-gate 	char *mountpoint = NULL, *backfs = NULL, *cachedir = NULL;
6497c478bd9Sstevel@tonic-gate 	size_t len;
6507c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
6517c478bd9Sstevel@tonic-gate 	int i, rc;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	mountpoint = cachefs_kmem_alloc(MAXPATHLEN, KM_SLEEP);
6547c478bd9Sstevel@tonic-gate 	if (copyinstr(umountpoint, mountpoint, MAXPATHLEN, &len) != 0)
6557c478bd9Sstevel@tonic-gate 		goto out;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	cachedir = cachefs_kmem_alloc(MAXPATHLEN, KM_SLEEP);
6587c478bd9Sstevel@tonic-gate 	if (copyinstr(ucachedir, cachedir, MAXPATHLEN, &len) != 0)
6597c478bd9Sstevel@tonic-gate 		goto out;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	backfs = cachefs_kmem_alloc(MAXPATHLEN, KM_SLEEP);
6627c478bd9Sstevel@tonic-gate 	if (backfs) {
6637c478bd9Sstevel@tonic-gate 		if (copyinstr(ubackfs, backfs, MAXPATHLEN, &len) != 0)
6647c478bd9Sstevel@tonic-gate 			goto out;
6657c478bd9Sstevel@tonic-gate 	} else {
6667c478bd9Sstevel@tonic-gate 		(void) strcpy(backfs, "no back file system");
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	ASSERT(strlen(mountpoint) < MAXPATHLEN);
6707c478bd9Sstevel@tonic-gate 	ASSERT(strlen(backfs) < MAXPATHLEN);
6717c478bd9Sstevel@tonic-gate 	ASSERT(strlen(cachedir) < MAXPATHLEN);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/* protect cachefs_kstat_key */
6747c478bd9Sstevel@tonic-gate 	mutex_enter(&cachefs_kstat_key_lock);
6757c478bd9Sstevel@tonic-gate 	/*
6767c478bd9Sstevel@tonic-gate 	 * XXXX If already there, why not go straight to it?
6777c478bd9Sstevel@tonic-gate 	 * We know that fscp->fs_kstat_id == i + 1
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate 	i = fscp->fs_kstat_id - 1;
6807c478bd9Sstevel@tonic-gate 	if ((i >= 0) && (i < cachefs_kstat_key_n))
6817c478bd9Sstevel@tonic-gate 		rc = 1;
6827c478bd9Sstevel@tonic-gate 	else
6837c478bd9Sstevel@tonic-gate 		rc = i = 0;
6847c478bd9Sstevel@tonic-gate 	for (; i < cachefs_kstat_key_n; i++) {
6857c478bd9Sstevel@tonic-gate 		key = cachefs_kstat_key + i;
6867c478bd9Sstevel@tonic-gate 		if (strcmp((char *)(uintptr_t)key->ks_mountpoint,
6877c478bd9Sstevel@tonic-gate 		    mountpoint) == 0 &&
6887c478bd9Sstevel@tonic-gate 		    strcmp((char *)(uintptr_t)key->ks_cachedir,
6897c478bd9Sstevel@tonic-gate 		    cachedir) == 0 &&
6907c478bd9Sstevel@tonic-gate 		    strcmp((char *)(uintptr_t)key->ks_cacheid, cacheid) == 0)
6917c478bd9Sstevel@tonic-gate 			break;
6927c478bd9Sstevel@tonic-gate 		if (rc) {	/* direct key did not work - check all */
6937c478bd9Sstevel@tonic-gate 			i = -1;	/* will increment to zero in loop */
6947c478bd9Sstevel@tonic-gate 			rc = 0;
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (i >= cachefs_kstat_key_n) {
6997c478bd9Sstevel@tonic-gate 		key = cachefs_kmem_alloc((cachefs_kstat_key_n + 1) *
7007c478bd9Sstevel@tonic-gate 		    sizeof (cachefs_kstat_key_t), KM_SLEEP);
7017c478bd9Sstevel@tonic-gate 		if (cachefs_kstat_key != NULL) {
7027c478bd9Sstevel@tonic-gate 			bcopy(cachefs_kstat_key, key,
7037c478bd9Sstevel@tonic-gate 			    cachefs_kstat_key_n * sizeof (*key));
7047c478bd9Sstevel@tonic-gate 			cachefs_kmem_free(cachefs_kstat_key,
7057c478bd9Sstevel@tonic-gate 			    cachefs_kstat_key_n * sizeof (*key));
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 		cachefs_kstat_key = key;
7087c478bd9Sstevel@tonic-gate 		key = cachefs_kstat_key + cachefs_kstat_key_n;
7097c478bd9Sstevel@tonic-gate 		++cachefs_kstat_key_n;
7107c478bd9Sstevel@tonic-gate 		rc = key->ks_id = cachefs_kstat_key_n; /* offset + 1 */
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		key->ks_mountpoint = (uint64_t)(uintptr_t)
7137c478bd9Sstevel@tonic-gate 		    cachefs_strdup(mountpoint);
7147c478bd9Sstevel@tonic-gate 		key->ks_backfs = (uint64_t)(uintptr_t)cachefs_strdup(backfs);
7157c478bd9Sstevel@tonic-gate 		key->ks_cachedir = (uint64_t)(uintptr_t)
7167c478bd9Sstevel@tonic-gate 		    cachefs_strdup(cachedir);
7177c478bd9Sstevel@tonic-gate 		key->ks_cacheid = (uint64_t)(uintptr_t)cachefs_strdup(cacheid);
7187c478bd9Sstevel@tonic-gate 	} else
7197c478bd9Sstevel@tonic-gate 		rc = key->ks_id;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	mutex_enter(&fscp->fs_fslock); /* protect fscp */
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	fscp->fs_kstat_id = rc;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	mutex_exit(&fscp->fs_fslock); /* finished with fscp */
7267c478bd9Sstevel@tonic-gate 	/* finished cachefs_kstat_key */
7277c478bd9Sstevel@tonic-gate 	mutex_exit(&cachefs_kstat_key_lock);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	key->ks_vfsp = (uint64_t)(uintptr_t)fscp->fs_cfsvfsp;
7307c478bd9Sstevel@tonic-gate 	key->ks_mounted = 1;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/*
7337c478bd9Sstevel@tonic-gate 	 * we must not be holding any mutex that is a ks_lock field
7347c478bd9Sstevel@tonic-gate 	 * for one of the kstats when we invoke kstat_create,
7357c478bd9Sstevel@tonic-gate 	 * kstat_install, and friends.
7367c478bd9Sstevel@tonic-gate 	 */
7377c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&cachefs_kstat_key_lock));
7387c478bd9Sstevel@tonic-gate 	/* really should be EVERY cachep's c_log_mutex */
7397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&cachep->c_log_mutex));
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	/* cachefs.#.log */
7427c478bd9Sstevel@tonic-gate 	ksp = kstat_create("cachefs", fscp->fs_kstat_id, "log",
7437c478bd9Sstevel@tonic-gate 	    "misc", KSTAT_TYPE_RAW, 1,
7447c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_WRITABLE | KSTAT_FLAG_VIRTUAL);
7457c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
7467c478bd9Sstevel@tonic-gate 		ksp->ks_data = cachep->c_log_ctl;
7477c478bd9Sstevel@tonic-gate 		ksp->ks_data_size = sizeof (cachefs_log_control_t);
7487c478bd9Sstevel@tonic-gate 		ksp->ks_lock = &cachep->c_log_mutex;
7497c478bd9Sstevel@tonic-gate 		ksp->ks_snapshot = cachefs_log_kstat_snapshot;
7507c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 	/* cachefs.#.stats */
7537c478bd9Sstevel@tonic-gate 	ksp = kstat_create("cachefs", fscp->fs_kstat_id, "stats",
7547c478bd9Sstevel@tonic-gate 	    "misc", KSTAT_TYPE_RAW, 1,
7557c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_WRITABLE | KSTAT_FLAG_VIRTUAL);
7567c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
7577c478bd9Sstevel@tonic-gate 		ksp->ks_data = fscp;
7587c478bd9Sstevel@tonic-gate 		ksp->ks_data_size = sizeof (cachefs_stats_t);
7597c478bd9Sstevel@tonic-gate 		ksp->ks_snapshot = cachefs_stats_kstat_snapshot;
7607c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate out:
7647c478bd9Sstevel@tonic-gate 	if (mountpoint != NULL)
7657c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(mountpoint, MAXPATHLEN);
7667c478bd9Sstevel@tonic-gate 	if (backfs != NULL)
7677c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(backfs, MAXPATHLEN);
7687c478bd9Sstevel@tonic-gate 	if (cachedir != NULL)
7697c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(cachedir, MAXPATHLEN);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate void
cachefs_kstat_umount(int ksid)7737c478bd9Sstevel@tonic-gate cachefs_kstat_umount(int ksid)
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	cachefs_kstat_key_t *k = cachefs_kstat_key + (ksid - 1);
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	ASSERT(k->ks_id == ksid);
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	k->ks_mounted = 0;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	kstat_delete_byname("cachefs", ksid, "stats");
7827c478bd9Sstevel@tonic-gate 	kstat_delete_byname("cachefs", ksid, "log");
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate int
cachefs_kstat_key_update(kstat_t * ksp,int rw)7867c478bd9Sstevel@tonic-gate cachefs_kstat_key_update(kstat_t *ksp, int rw)
7877c478bd9Sstevel@tonic-gate {
7887c478bd9Sstevel@tonic-gate 	cachefs_kstat_key_t *key = *((cachefs_kstat_key_t **)ksp->ks_data);
7897c478bd9Sstevel@tonic-gate 	cachefs_kstat_key_t *k;
7907c478bd9Sstevel@tonic-gate 	int i;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7937c478bd9Sstevel@tonic-gate 		return (EIO);
7947c478bd9Sstevel@tonic-gate 	if (key == NULL)
7957c478bd9Sstevel@tonic-gate 		return (EIO);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	ksp->ks_data_size = cachefs_kstat_key_n * sizeof (*key);
7987c478bd9Sstevel@tonic-gate 	for (i = 0; i < cachefs_kstat_key_n; i++) {
7997c478bd9Sstevel@tonic-gate 		k = key + i;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		ksp->ks_data_size +=
8027c478bd9Sstevel@tonic-gate 		    strlen((char *)(uintptr_t)k->ks_mountpoint) + 1;
8037c478bd9Sstevel@tonic-gate 		ksp->ks_data_size +=
8047c478bd9Sstevel@tonic-gate 		    strlen((char *)(uintptr_t)k->ks_backfs) + 1;
8057c478bd9Sstevel@tonic-gate 		ksp->ks_data_size +=
8067c478bd9Sstevel@tonic-gate 		    strlen((char *)(uintptr_t)k->ks_cachedir) + 1;
8077c478bd9Sstevel@tonic-gate 		ksp->ks_data_size +=
8087c478bd9Sstevel@tonic-gate 		    strlen((char *)(uintptr_t)k->ks_cacheid) + 1;
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	ksp->ks_ndata = cachefs_kstat_key_n;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	return (0);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate int
cachefs_kstat_key_snapshot(kstat_t * ksp,void * buf,int rw)8177c478bd9Sstevel@tonic-gate cachefs_kstat_key_snapshot(kstat_t *ksp, void *buf, int rw)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	cachefs_kstat_key_t *key = *((cachefs_kstat_key_t **)ksp->ks_data);
8207c478bd9Sstevel@tonic-gate 	cachefs_kstat_key_t *k;
8217c478bd9Sstevel@tonic-gate 	caddr_t s;
8227c478bd9Sstevel@tonic-gate 	int i;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
8257c478bd9Sstevel@tonic-gate 		return (EIO);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (key == NULL)
8287c478bd9Sstevel@tonic-gate 		return (0); /* paranoid */
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	bcopy(key, buf, cachefs_kstat_key_n * sizeof (*key));
8317c478bd9Sstevel@tonic-gate 	key = buf;
8327c478bd9Sstevel@tonic-gate 	s = (caddr_t)(key + cachefs_kstat_key_n);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	for (i = 0; i < cachefs_kstat_key_n; i++) {
8357c478bd9Sstevel@tonic-gate 		k = key + i;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		(void) strcpy(s, (char *)(uintptr_t)k->ks_mountpoint);
8387c478bd9Sstevel@tonic-gate 		k->ks_mountpoint = (uint64_t)(uintptr_t)(s - (uintptr_t)buf);
8397c478bd9Sstevel@tonic-gate 		s += strlen(s) + 1;
8407c478bd9Sstevel@tonic-gate 		(void) strcpy(s, (char *)(uintptr_t)k->ks_backfs);
8417c478bd9Sstevel@tonic-gate 		k->ks_backfs = (uint64_t)(uintptr_t)(s - (uintptr_t)buf);
8427c478bd9Sstevel@tonic-gate 		s += strlen(s) + 1;
8437c478bd9Sstevel@tonic-gate 		(void) strcpy(s, (char *)(uintptr_t)k->ks_cachedir);
8447c478bd9Sstevel@tonic-gate 		k->ks_cachedir = (uint64_t)(uintptr_t)(s - (uintptr_t)buf);
8457c478bd9Sstevel@tonic-gate 		s += strlen(s) + 1;
8467c478bd9Sstevel@tonic-gate 		(void) strcpy(s, (char *)(uintptr_t)k->ks_cacheid);
8477c478bd9Sstevel@tonic-gate 		k->ks_cacheid = (uint64_t)(uintptr_t)(s - (uintptr_t)buf);
8487c478bd9Sstevel@tonic-gate 		s += strlen(s) + 1;
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	return (0);
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate extern void  cachefs_inactivate();
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate static int
cachefs_unmount(vfs_t * vfsp,int flag,cred_t * cr)8577c478bd9Sstevel@tonic-gate cachefs_unmount(vfs_t *vfsp, int flag, cred_t *cr)
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 	fscache_t *fscp = VFS_TO_FSCACHE(vfsp);
8607c478bd9Sstevel@tonic-gate 	struct cachefscache *cachep = fscp->fs_cache;
8617c478bd9Sstevel@tonic-gate 	int error;
8627c478bd9Sstevel@tonic-gate 	int xx;
8637c478bd9Sstevel@tonic-gate 	vnode_t *nmvp;
8647c478bd9Sstevel@tonic-gate 	struct vattr attr;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
8677c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_VFSOP)
8687c478bd9Sstevel@tonic-gate 		printf("cachefs_unmount: ENTER fscp %p\n", fscp);
8697c478bd9Sstevel@tonic-gate #endif
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_unmount(cr, vfsp)) != 0)
8727c478bd9Sstevel@tonic-gate 		goto out;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	/*
8757c478bd9Sstevel@tonic-gate 	 * forced unmount is not supported by this file system
8767c478bd9Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
8777c478bd9Sstevel@tonic-gate 	 */
8787c478bd9Sstevel@tonic-gate 	if (flag & MS_FORCE) {
8797c478bd9Sstevel@tonic-gate 		error = ENOTSUP;
8807c478bd9Sstevel@tonic-gate 		goto out;
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 	/* if a log file exists don't allow the unmount */
8837c478bd9Sstevel@tonic-gate 	if (fscp->fs_dlogfile) {
8847c478bd9Sstevel@tonic-gate 		error = EBUSY;
8857c478bd9Sstevel@tonic-gate 		goto out;
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	/*
8897c478bd9Sstevel@tonic-gate 	 * wait for the cache-wide async queue to drain.  Someone
8907c478bd9Sstevel@tonic-gate 	 * here may be trying to sync our fscache...
8917c478bd9Sstevel@tonic-gate 	 */
8927c478bd9Sstevel@tonic-gate 	while (cachefs_async_halt(&fscp->fs_cache->c_workq, 0) == EBUSY) {
8937c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
8947c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_VFSOP)
8957c478bd9Sstevel@tonic-gate 			printf("unmount: waiting for cache async queue...\n");
8967c478bd9Sstevel@tonic-gate #endif
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	error = cachefs_async_halt(&fscp->fs_workq, 1);
9007c478bd9Sstevel@tonic-gate 	if (error) {
9017c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
9027c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_VFSOP)
9037c478bd9Sstevel@tonic-gate 			printf("cachefs_unmount: "
9047c478bd9Sstevel@tonic-gate 			    "cachefs_async_halt error %d\n", error);
9057c478bd9Sstevel@tonic-gate #endif
9067c478bd9Sstevel@tonic-gate 		goto out;
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	/*
9107c478bd9Sstevel@tonic-gate 	 * No active cnodes on this cache && rootvp refcnt == 1
9117c478bd9Sstevel@tonic-gate 	 */
9127c478bd9Sstevel@tonic-gate 	mutex_enter(&fscp->fs_fslock);
9137c478bd9Sstevel@tonic-gate 	xx = fscp->fs_cnodecnt - fscp->fs_idlecnt;
9147c478bd9Sstevel@tonic-gate 	ASSERT(xx >= 1);
9157c478bd9Sstevel@tonic-gate 	if (xx > 1 || fscp->fs_rootvp->v_count != 1) {
9167c478bd9Sstevel@tonic-gate 		mutex_exit(&fscp->fs_fslock);
9177c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
9187c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_VFSOP)
9197c478bd9Sstevel@tonic-gate 			printf("cachefs_unmount: busy (cnodes active %d, idle "
9207c478bd9Sstevel@tonic-gate 				"%d)\n", fscp->fs_cnodecnt, fscp->fs_idlecnt);
9217c478bd9Sstevel@tonic-gate #endif
9227c478bd9Sstevel@tonic-gate 		error = EBUSY;
9237c478bd9Sstevel@tonic-gate 		goto out;
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 	mutex_exit(&fscp->fs_fslock);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/* get rid of anything on the idle list */
9287c478bd9Sstevel@tonic-gate 	ASSERT(fscp->fs_idleclean == 0);
9297c478bd9Sstevel@tonic-gate 	cachefs_cnode_idleclean(fscp, 1);
9307c478bd9Sstevel@tonic-gate 	if (fscp->fs_cnodecnt > 1) {
9317c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
9327c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_VFSOP)
9337c478bd9Sstevel@tonic-gate 			printf("cachefs_unmount: busy (cnode count %d)\n",
9347c478bd9Sstevel@tonic-gate 				fscp->fs_cnodecnt);
9357c478bd9Sstevel@tonic-gate #endif
9367c478bd9Sstevel@tonic-gate 		error = EBUSY;
9377c478bd9Sstevel@tonic-gate 		goto out;
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	fscache_hold(fscp);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	/* get rid of the root cnode */
9437c478bd9Sstevel@tonic-gate 	if (cachefs_cnode_inactive(fscp->fs_rootvp, cr) == EBUSY) {
9447c478bd9Sstevel@tonic-gate 		fscache_rele(fscp);
9457c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
9467c478bd9Sstevel@tonic-gate 		CFS_DEBUG(CFSDEBUG_VFSOP)
9477c478bd9Sstevel@tonic-gate 			printf("cachefs_unmount: busy (inactive failed)\n");
9487c478bd9Sstevel@tonic-gate #endif
9497c478bd9Sstevel@tonic-gate 		error = EBUSY;
9507c478bd9Sstevel@tonic-gate 		goto out;
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	/* create the file indicating not mounted */
9547c478bd9Sstevel@tonic-gate 	attr.va_mode = S_IFREG | 0666;
9557c478bd9Sstevel@tonic-gate 	attr.va_uid = 0;
9567c478bd9Sstevel@tonic-gate 	attr.va_gid = 0;
9577c478bd9Sstevel@tonic-gate 	attr.va_type = VREG;
9587c478bd9Sstevel@tonic-gate 	attr.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
9597c478bd9Sstevel@tonic-gate 	if (fscp->fs_fscdirvp != NULL)
9607c478bd9Sstevel@tonic-gate 		xx = VOP_CREATE(fscp->fs_fscdirvp, CACHEFS_UNMNT_FILE, &attr,
961*da6c28aaSamw 		    NONEXCL, 0600, &nmvp, kcred, 0, NULL, NULL);
9627c478bd9Sstevel@tonic-gate 	else
9637c478bd9Sstevel@tonic-gate 		xx = ENOENT; /* for unmounting when NOCACHE */
9647c478bd9Sstevel@tonic-gate 	if (xx == 0) {
9657c478bd9Sstevel@tonic-gate 		VN_RELE(nmvp);
9667c478bd9Sstevel@tonic-gate 	} else {
9677c478bd9Sstevel@tonic-gate 		printf("could not create %s %d\n", CACHEFS_UNMNT_FILE, xx);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	ASSERT(fscp->fs_cnodecnt == 0);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	/* sync the file system just in case */
9737c478bd9Sstevel@tonic-gate 	fscache_sync(fscp);
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	/* lock out other unmounts and mount */
9767c478bd9Sstevel@tonic-gate 	mutex_enter(&cachefs_cachelock);
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	/* mark the file system as not mounted */
9797c478bd9Sstevel@tonic-gate 	mutex_enter(&fscp->fs_fslock);
9807c478bd9Sstevel@tonic-gate 	fscp->fs_flags &= ~CFS_FS_MOUNTED;
9817c478bd9Sstevel@tonic-gate 	fscp->fs_rootvp = NULL;
9827c478bd9Sstevel@tonic-gate 	if (fscp->fs_kstat_id > 0)
9837c478bd9Sstevel@tonic-gate 		cachefs_kstat_umount(fscp->fs_kstat_id);
9847c478bd9Sstevel@tonic-gate 	fscp->fs_kstat_id = 0;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	/* drop the inum translation table */
9877c478bd9Sstevel@tonic-gate 	if (fscp->fs_inum_size > 0) {
9887c478bd9Sstevel@tonic-gate 		cachefs_kmem_free(fscp->fs_inum_trans,
9897c478bd9Sstevel@tonic-gate 		    fscp->fs_inum_size * sizeof (cachefs_inum_trans_t));
9907c478bd9Sstevel@tonic-gate 		fscp->fs_inum_size = 0;
9917c478bd9Sstevel@tonic-gate 		fscp->fs_inum_trans = NULL;
9927c478bd9Sstevel@tonic-gate 		fscp->fs_flags &= ~CFS_FS_HASHPRINT;
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 	mutex_exit(&fscp->fs_fslock);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	fscache_rele(fscp);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/* get rid of any unused fscache objects */
9997c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_fslistlock);
10007c478bd9Sstevel@tonic-gate 	fscache_list_gc(cachep);
10017c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_fslistlock);
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	/* get the number of mounts on this cache */
10047c478bd9Sstevel@tonic-gate 	mutex_enter(&cachep->c_fslistlock);
10057c478bd9Sstevel@tonic-gate 	xx = fscache_list_mounted(cachep);
10067c478bd9Sstevel@tonic-gate 	mutex_exit(&cachep->c_fslistlock);
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_UMOUNT))
10097c478bd9Sstevel@tonic-gate 		cachefs_log_umount(cachep, 0, vfsp);
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	/* if no mounts left, deactivate the cache */
10127c478bd9Sstevel@tonic-gate 	if (xx == 0)
10137c478bd9Sstevel@tonic-gate 		cachefs_delete_cachep(cachep);
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	mutex_exit(&cachefs_cachelock);
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate out:
10187c478bd9Sstevel@tonic-gate 	if (error) {
10197c478bd9Sstevel@tonic-gate 		if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_UMOUNT))
10207c478bd9Sstevel@tonic-gate 			cachefs_log_umount(cachep, error, vfsp);
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate #ifdef CFSDEBUG
10237c478bd9Sstevel@tonic-gate 	CFS_DEBUG(CFSDEBUG_VFSOP)
10247c478bd9Sstevel@tonic-gate 		printf("cachefs_unmount: EXIT\n");
10257c478bd9Sstevel@tonic-gate #endif
10267c478bd9Sstevel@tonic-gate 	return (error);
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate /*
10307c478bd9Sstevel@tonic-gate  * remove the cache from the list of caches
10317c478bd9Sstevel@tonic-gate  */
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate static void
cachefs_delete_cachep(cachefscache_t * cachep)10347c478bd9Sstevel@tonic-gate cachefs_delete_cachep(cachefscache_t *cachep)
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate 	struct cachefscache **cachepp;
10377c478bd9Sstevel@tonic-gate 	int found = 0;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cachefs_cachelock));
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	for (cachepp = &cachefs_cachelist;
10427c478bd9Sstevel@tonic-gate 	    *cachepp != NULL;
10437c478bd9Sstevel@tonic-gate 	    cachepp = &(*cachepp)->c_next) {
10447c478bd9Sstevel@tonic-gate 		if (*cachepp == cachep) {
10457c478bd9Sstevel@tonic-gate 			*cachepp = cachep->c_next;
10467c478bd9Sstevel@tonic-gate 			found++;
10477c478bd9Sstevel@tonic-gate 			break;
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 	ASSERT(found);
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	/* shut down the cache */
10537c478bd9Sstevel@tonic-gate 	cachefs_cache_destroy(cachep);
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate static int
cachefs_root(vfs_t * vfsp,vnode_t ** vpp)10577c478bd9Sstevel@tonic-gate cachefs_root(vfs_t *vfsp, vnode_t **vpp)
10587c478bd9Sstevel@tonic-gate {
10597c478bd9Sstevel@tonic-gate 	/*LINTED alignment okay*/
10607c478bd9Sstevel@tonic-gate 	struct fscache *fscp = (struct fscache *)vfsp->vfs_data;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	ASSERT(fscp != NULL);
10637c478bd9Sstevel@tonic-gate 	ASSERT(fscp->fs_rootvp != NULL);
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID)
10667c478bd9Sstevel@tonic-gate 		return (EPERM);
10677c478bd9Sstevel@tonic-gate 	*vpp = fscp->fs_rootvp;
10687c478bd9Sstevel@tonic-gate 	VN_HOLD(*vpp);
10697c478bd9Sstevel@tonic-gate 	return (0);
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate /*
10737c478bd9Sstevel@tonic-gate  * Get file system statistics.
10747c478bd9Sstevel@tonic-gate  */
10757c478bd9Sstevel@tonic-gate static int
cachefs_statvfs(register vfs_t * vfsp,struct statvfs64 * sbp)10767c478bd9Sstevel@tonic-gate cachefs_statvfs(register vfs_t *vfsp, struct statvfs64 *sbp)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	struct fscache *fscp = VFS_TO_FSCACHE(vfsp);
10797c478bd9Sstevel@tonic-gate 	struct cache_label *lp = &fscp->fs_cache->c_label;
10807c478bd9Sstevel@tonic-gate 	struct cache_usage *up = &fscp->fs_cache->c_usage;
10817c478bd9Sstevel@tonic-gate 	int error;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID)
10847c478bd9Sstevel@tonic-gate 		return (EPERM);
10857c478bd9Sstevel@tonic-gate 	error = cachefs_cd_access(fscp, 0, 0);
10867c478bd9Sstevel@tonic-gate 	if (error)
10877c478bd9Sstevel@tonic-gate 		return (error);
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
10907c478bd9Sstevel@tonic-gate 		/*
10917c478bd9Sstevel@tonic-gate 		 * When connected return backfs stats
10927c478bd9Sstevel@tonic-gate 		 */
10937c478bd9Sstevel@tonic-gate 		error = VFS_STATVFS(fscp->fs_backvfsp, sbp);
10947c478bd9Sstevel@tonic-gate 	} else {
10957c478bd9Sstevel@tonic-gate 		/*
10967c478bd9Sstevel@tonic-gate 		 * Otherwise, just return the frontfs stats
10977c478bd9Sstevel@tonic-gate 		 */
10987c478bd9Sstevel@tonic-gate 		ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
10997c478bd9Sstevel@tonic-gate 		error = VFS_STATVFS(fscp->fs_fscdirvp->v_vfsp, sbp);
11007c478bd9Sstevel@tonic-gate 		if (!error) {
11017c478bd9Sstevel@tonic-gate 			dev32_t	d32;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 			sbp->f_frsize = MAXBSIZE;
11047c478bd9Sstevel@tonic-gate 			sbp->f_blocks = lp->cl_maxblks;
11057c478bd9Sstevel@tonic-gate 			sbp->f_bfree = sbp->f_bavail =
11067c478bd9Sstevel@tonic-gate 			    lp->cl_maxblks - up->cu_blksused;
11077c478bd9Sstevel@tonic-gate 			sbp->f_files = lp->cl_maxinodes;
11087c478bd9Sstevel@tonic-gate 			sbp->f_ffree = sbp->f_favail =
11097c478bd9Sstevel@tonic-gate 			    lp->cl_maxinodes - up->cu_filesused;
11107c478bd9Sstevel@tonic-gate 			(void) cmpldev(&d32, vfsp->vfs_dev);
11117c478bd9Sstevel@tonic-gate 			sbp->f_fsid = d32;
11127c478bd9Sstevel@tonic-gate 		}
11137c478bd9Sstevel@tonic-gate 	}
11147c478bd9Sstevel@tonic-gate 	cachefs_cd_release(fscp);
11157c478bd9Sstevel@tonic-gate 	if (error)
11167c478bd9Sstevel@tonic-gate 		return (error);
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	/*
11197c478bd9Sstevel@tonic-gate 	 * Make sure fstype is CFS.
11207c478bd9Sstevel@tonic-gate 	 */
11217c478bd9Sstevel@tonic-gate 	(void) strcpy(sbp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
11227c478bd9Sstevel@tonic-gate 	bzero(sbp->f_fstr, sizeof (sbp->f_fstr));
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	return (0);
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate /*
11287c478bd9Sstevel@tonic-gate  * queue a request to sync the given fscache
11297c478bd9Sstevel@tonic-gate  */
11307c478bd9Sstevel@tonic-gate static void
queue_sync(struct cachefscache * cachep,cred_t * cr)11317c478bd9Sstevel@tonic-gate queue_sync(struct cachefscache *cachep, cred_t *cr)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate 	struct cachefs_req *rp;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	rp = kmem_cache_alloc(cachefs_req_cache, KM_SLEEP);
11367c478bd9Sstevel@tonic-gate 	rp->cfs_cmd = CFS_CACHE_SYNC;
11377c478bd9Sstevel@tonic-gate 	rp->cfs_cr = cr;
11387c478bd9Sstevel@tonic-gate 	rp->cfs_req_u.cu_fs_sync.cf_cachep = cachep;
11397c478bd9Sstevel@tonic-gate 	crhold(rp->cfs_cr);
11407c478bd9Sstevel@tonic-gate 	cachefs_addqueue(rp, &cachep->c_workq);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11447c478bd9Sstevel@tonic-gate static int
cachefs_sync(vfs_t * vfsp,short flag,cred_t * cr)11457c478bd9Sstevel@tonic-gate cachefs_sync(vfs_t *vfsp, short flag, cred_t *cr)
11467c478bd9Sstevel@tonic-gate {
11477c478bd9Sstevel@tonic-gate 	struct fscache *fscp;
11487c478bd9Sstevel@tonic-gate 	struct cachefscache *cachep;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID)
11517c478bd9Sstevel@tonic-gate 		return (EPERM);
11527c478bd9Sstevel@tonic-gate 	if (!(flag & SYNC_ATTR)) {
11537c478bd9Sstevel@tonic-gate 		/*
11547c478bd9Sstevel@tonic-gate 		 * queue an async request to do the sync.
11557c478bd9Sstevel@tonic-gate 		 * We always sync an entire cache (as opposed to an
11567c478bd9Sstevel@tonic-gate 		 * individual fscache) so that we have an opportunity
11577c478bd9Sstevel@tonic-gate 		 * to set the clean flag.
11587c478bd9Sstevel@tonic-gate 		 */
11597c478bd9Sstevel@tonic-gate 		if (vfsp) {
11607c478bd9Sstevel@tonic-gate 			/*LINTED alignment okay*/
11617c478bd9Sstevel@tonic-gate 			fscp = (struct fscache *)vfsp->vfs_data;
11627c478bd9Sstevel@tonic-gate 			queue_sync(fscp->fs_cache, cr);
11637c478bd9Sstevel@tonic-gate 		} else {
11647c478bd9Sstevel@tonic-gate 			mutex_enter(&cachefs_cachelock);
11657c478bd9Sstevel@tonic-gate 			for (cachep = cachefs_cachelist; cachep != NULL;
11667c478bd9Sstevel@tonic-gate 			    cachep = cachep->c_next) {
11677c478bd9Sstevel@tonic-gate 				queue_sync(cachep, cr);
11687c478bd9Sstevel@tonic-gate 			}
11697c478bd9Sstevel@tonic-gate 			mutex_exit(&cachefs_cachelock);
11707c478bd9Sstevel@tonic-gate 		}
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate 	return (0);
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate static int
cachefs_remount(struct vfs * vfsp,struct mounta * uap)11767c478bd9Sstevel@tonic-gate cachefs_remount(struct vfs *vfsp, struct mounta *uap)
11777c478bd9Sstevel@tonic-gate {
11787c478bd9Sstevel@tonic-gate 	fscache_t *fscp = VFS_TO_FSCACHE(vfsp);
11797c478bd9Sstevel@tonic-gate 	cachefscache_t *cachep = fscp->fs_cache;
11807c478bd9Sstevel@tonic-gate 	int error = 0;
11817c478bd9Sstevel@tonic-gate 	STRUCT_DECL(cachefs_mountargs, map);
11827c478bd9Sstevel@tonic-gate 	struct cachefsoptions	*cfs_options;
11837c478bd9Sstevel@tonic-gate 	char			*backfs, *cacheid, *cachedir;
11847c478bd9Sstevel@tonic-gate 	struct vnode *cachedirvp = NULL;
11857c478bd9Sstevel@tonic-gate 	ino64_t fsid;
11867c478bd9Sstevel@tonic-gate 	vnode_t *backrootvp = NULL;
11877c478bd9Sstevel@tonic-gate 	struct vnode *tmpdirvp = NULL;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	STRUCT_INIT(map, get_udatamodel());
11907c478bd9Sstevel@tonic-gate 	error = copyin(uap->dataptr, STRUCT_BUF(map),
11917c478bd9Sstevel@tonic-gate 			SIZEOF_STRUCT(cachefs_mountargs, DATAMODEL_NATIVE));
11927c478bd9Sstevel@tonic-gate 	if (error)
11937c478bd9Sstevel@tonic-gate 		goto out;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	/*
11967c478bd9Sstevel@tonic-gate 	 * get cache directory vp
11977c478bd9Sstevel@tonic-gate 	 */
11987c478bd9Sstevel@tonic-gate 	cachedir = (char *)STRUCT_FGETP(map, cfs_cachedir);
11997c478bd9Sstevel@tonic-gate 	error = lookupname(cachedir, UIO_USERSPACE, FOLLOW,
12007c478bd9Sstevel@tonic-gate 	    NULLVPP, &cachedirvp);
12017c478bd9Sstevel@tonic-gate 	if (error)
12027c478bd9Sstevel@tonic-gate 		goto out;
12037c478bd9Sstevel@tonic-gate 	if (cachedirvp->v_type != VDIR) {
12047c478bd9Sstevel@tonic-gate 		error = EINVAL;
12057c478bd9Sstevel@tonic-gate 		goto out;
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	error = 0;
12097c478bd9Sstevel@tonic-gate 	if (cachedirvp) {
12107c478bd9Sstevel@tonic-gate 		error = VOP_LOOKUP(cachedirvp, CACHEFS_DLOG_FILE,
1211*da6c28aaSamw 		    &tmpdirvp, NULL, 0, NULL, kcred, NULL, NULL, NULL);
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 	cfs_options = (struct cachefsoptions *)STRUCT_FADDR(map, cfs_options);
12147c478bd9Sstevel@tonic-gate 	cacheid = (char *)STRUCT_FGETP(map, cfs_cacheid);
12157c478bd9Sstevel@tonic-gate /* XXX not quite right */
12167c478bd9Sstevel@tonic-gate #if 0
12177c478bd9Sstevel@tonic-gate 	/*
12187c478bd9Sstevel@tonic-gate 	 * If a log file exists and the cache is being mounted without
12197c478bd9Sstevel@tonic-gate 	 * the snr (aka disconnectable) option, return an error.
12207c478bd9Sstevel@tonic-gate 	 */
12217c478bd9Sstevel@tonic-gate 	if ((error == 0) &&
12227c478bd9Sstevel@tonic-gate 	    !(cfs_options->opt_flags & CFS_DISCONNECTABLE)) {
12237c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
12247c478bd9Sstevel@tonic-gate 		    "cachefs_mount: log exists and disconnectable"
12257c478bd9Sstevel@tonic-gate 		    "option not specified\n");
12267c478bd9Sstevel@tonic-gate 		error = EINVAL;
12277c478bd9Sstevel@tonic-gate 		goto out;
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate #endif
12307c478bd9Sstevel@tonic-gate 	error = 0;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	/*
12337c478bd9Sstevel@tonic-gate 	 * If the user is using NFSv4 and there are other options
12347c478bd9Sstevel@tonic-gate 	 * specified, make sure we ignore the other options.
12357c478bd9Sstevel@tonic-gate 	 */
12367c478bd9Sstevel@tonic-gate 	if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
12377c478bd9Sstevel@tonic-gate 		cfs_options->opt_flags = CFS_BACKFS_NFSV4;
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	/* XXX need mount options "nocache" and "nofill" */
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	/* if nocache is being turned off */
12437c478bd9Sstevel@tonic-gate 	if (cachep->c_flags & CACHE_NOCACHE) {
12447c478bd9Sstevel@tonic-gate 		error = cachefs_cache_activate_ro(cachep, cachedirvp);
12457c478bd9Sstevel@tonic-gate 		if (error)
12467c478bd9Sstevel@tonic-gate 			goto out;
12477c478bd9Sstevel@tonic-gate 		cachefs_cache_activate_rw(cachep);
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 		/* get the fsid for the fscache */
12507c478bd9Sstevel@tonic-gate 		error = fscache_name_to_fsid(cachep, cacheid, &fsid);
12517c478bd9Sstevel@tonic-gate 		if (error)
12527c478bd9Sstevel@tonic-gate 			fsid = 0;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 		/* activate the fscache */
12557c478bd9Sstevel@tonic-gate 		mutex_enter(&cachep->c_fslistlock);
12567c478bd9Sstevel@tonic-gate 		error = fscache_enable(fscp, fsid, cacheid,
12577c478bd9Sstevel@tonic-gate 			cfs_options, fscp->fs_info.fi_root);
12587c478bd9Sstevel@tonic-gate 		mutex_exit(&cachep->c_fslistlock);
12597c478bd9Sstevel@tonic-gate 		if (error) {
12607c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "cachefs: cannot remount %s\n",
12617c478bd9Sstevel@tonic-gate 				cacheid);
12627c478bd9Sstevel@tonic-gate 			goto out;
12637c478bd9Sstevel@tonic-gate 		}
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 		/* enable the cache */
12667c478bd9Sstevel@tonic-gate 		cachefs_enable_caching(fscp);
12677c478bd9Sstevel@tonic-gate 		fscache_activate_rw(fscp);
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	/* else if nofill is being turn off */
12717c478bd9Sstevel@tonic-gate 	else if (cachep->c_flags & CACHE_NOFILL) {
12727c478bd9Sstevel@tonic-gate 		ASSERT(cachep->c_flags & CACHE_NOFILL);
12737c478bd9Sstevel@tonic-gate 		cachefs_cache_activate_rw(cachep);
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 		/* enable the cache */
12767c478bd9Sstevel@tonic-gate 		cachefs_enable_caching(fscp);
12777c478bd9Sstevel@tonic-gate 		fscache_activate_rw(fscp);
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	fscache_acset(fscp, STRUCT_FGET(map, cfs_acregmin),
12817c478bd9Sstevel@tonic-gate 	    STRUCT_FGET(map, cfs_acregmax),
12827c478bd9Sstevel@tonic-gate 	    STRUCT_FGET(map, cfs_acdirmin), STRUCT_FGET(map, cfs_acdirmax));
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	/* if the backfs is mounted now or we have a new backfs */
12857c478bd9Sstevel@tonic-gate 	backfs = (char *)STRUCT_FGETP(map, cfs_backfs);
12867c478bd9Sstevel@tonic-gate 	if (backfs && (cfs_options->opt_flags & CFS_SLIDE)) {
12877c478bd9Sstevel@tonic-gate 		/* get the back file system root vp */
12887c478bd9Sstevel@tonic-gate 		error = lookupname(backfs, UIO_USERSPACE, FOLLOW,
12897c478bd9Sstevel@tonic-gate 			NULLVPP, &backrootvp);
12907c478bd9Sstevel@tonic-gate 		if (error)
12917c478bd9Sstevel@tonic-gate 			goto out;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 		/*
12947c478bd9Sstevel@tonic-gate 		 * Make sure the thing we just looked up is a directory
12957c478bd9Sstevel@tonic-gate 		 * and a root of a file system
12967c478bd9Sstevel@tonic-gate 		 */
12977c478bd9Sstevel@tonic-gate 		if (backrootvp->v_type != VDIR ||
12987c478bd9Sstevel@tonic-gate 		    !(backrootvp->v_flag & VROOT)) {
12997c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
13007c478bd9Sstevel@tonic-gate 			    "cachefs_mount: backpath not a directory\n");
13017c478bd9Sstevel@tonic-gate 			error = EINVAL;
13027c478bd9Sstevel@tonic-gate 			goto out;
13037c478bd9Sstevel@tonic-gate 		}
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 		/*
13067c478bd9Sstevel@tonic-gate 		 * XXX
13077c478bd9Sstevel@tonic-gate 		 * Kind of dangerous to just set this but we do
13087c478bd9Sstevel@tonic-gate 		 * not have locks around usage of fs_backvfsp.
13097c478bd9Sstevel@tonic-gate 		 * Hope for the best for now.
13107c478bd9Sstevel@tonic-gate 		 * Probably should also spin through vnodes and fix them up.
13117c478bd9Sstevel@tonic-gate 		 * Krishna - fixed c_backvp to reflect the change.
13127c478bd9Sstevel@tonic-gate 		 */
13137c478bd9Sstevel@tonic-gate 		fscp->fs_backvfsp = backrootvp->v_vfsp;
13147c478bd9Sstevel@tonic-gate 		((cnode_t *)(fscp->fs_rootvp->v_data))->c_backvp = backrootvp;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 		/*
13177c478bd9Sstevel@tonic-gate 		 * Now the root cnode structure is an owner of
13187c478bd9Sstevel@tonic-gate 		 * the opened back root vnode structure; we must
13197c478bd9Sstevel@tonic-gate 		 * clear the pointer to back root vnode here as
13207c478bd9Sstevel@tonic-gate 		 * we don't need it since now, and the root cnode
13217c478bd9Sstevel@tonic-gate 		 * structure will control the vnode
13227c478bd9Sstevel@tonic-gate 		 */
13237c478bd9Sstevel@tonic-gate 		backrootvp = (vnode_t *)NULL;
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	if (fscp->fs_kstat_id > 0)
13277c478bd9Sstevel@tonic-gate 		cachefs_kstat_umount(fscp->fs_kstat_id);
13287c478bd9Sstevel@tonic-gate 	fscp->fs_kstat_id = 0;
13297c478bd9Sstevel@tonic-gate 	cachefs_kstat_mount(fscp, uap->dir, backfs, cachedir, cacheid);
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_MOUNT))
13327c478bd9Sstevel@tonic-gate 		cachefs_log_mount(cachep, error, vfsp, fscp,
13337c478bd9Sstevel@tonic-gate 		    uap->dir, UIO_USERSPACE,
13347c478bd9Sstevel@tonic-gate 		    (STRUCT_BUF(map) != NULL) ? cacheid : NULL);
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate out:
13377c478bd9Sstevel@tonic-gate 	if (cachedirvp)
13387c478bd9Sstevel@tonic-gate 		VN_RELE(cachedirvp);
13397c478bd9Sstevel@tonic-gate 	if (backrootvp)
13407c478bd9Sstevel@tonic-gate 		VN_RELE(backrootvp);
13417c478bd9Sstevel@tonic-gate 	return (error);
13427c478bd9Sstevel@tonic-gate }
1343