xref: /onnv-gate/usr/src/uts/common/fs/autofs/auto_vnops.c (revision 4863:7b14ad153d91)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52170Sevanl  * Common Development and Distribution License (the "License").
62170Sevanl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223391Ssemery  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/errno.h>
310Sstevel@tonic-gate #include <sys/proc.h>
320Sstevel@tonic-gate #include <sys/vnode.h>
330Sstevel@tonic-gate #include <sys/vfs.h>
343898Srsb #include <sys/vfs_opreg.h>
350Sstevel@tonic-gate #include <sys/uio.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/pathname.h>
380Sstevel@tonic-gate #include <sys/dirent.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/sysmacros.h>
410Sstevel@tonic-gate #include <sys/tiuser.h>
420Sstevel@tonic-gate #include <sys/cmn_err.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/mode.h>
450Sstevel@tonic-gate #include <sys/policy.h>
460Sstevel@tonic-gate #include <rpc/types.h>
470Sstevel@tonic-gate #include <rpc/auth.h>
480Sstevel@tonic-gate #include <rpc/clnt.h>
490Sstevel@tonic-gate #include <sys/fs/autofs.h>
500Sstevel@tonic-gate #include <rpcsvc/autofs_prot.h>
510Sstevel@tonic-gate #include <fs/fs_subr.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  *  Vnode ops for autofs
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate static int auto_open(vnode_t **, int, cred_t *);
570Sstevel@tonic-gate static int auto_close(vnode_t *, int, int, offset_t, cred_t *);
580Sstevel@tonic-gate static int auto_getattr(vnode_t *, vattr_t *, int, cred_t *);
590Sstevel@tonic-gate static int auto_setattr(vnode_t *, vattr_t *, int, cred_t *,
600Sstevel@tonic-gate 	caller_context_t *);
610Sstevel@tonic-gate static int auto_access(vnode_t *, int, int, cred_t *);
620Sstevel@tonic-gate static int auto_lookup(vnode_t *, char *, vnode_t **,
630Sstevel@tonic-gate 	pathname_t *, int, vnode_t *, cred_t *);
640Sstevel@tonic-gate static int auto_create(vnode_t *, char *, vattr_t *, vcexcl_t,
650Sstevel@tonic-gate 	int, vnode_t **, cred_t *, int);
660Sstevel@tonic-gate static int auto_remove(vnode_t *, char *, cred_t *);
670Sstevel@tonic-gate static int auto_link(vnode_t *, vnode_t *, char *, cred_t *);
680Sstevel@tonic-gate static int auto_rename(vnode_t *, char *, vnode_t *, char *, cred_t *);
690Sstevel@tonic-gate static int auto_mkdir(vnode_t *, char *, vattr_t *, vnode_t **, cred_t *);
700Sstevel@tonic-gate static int auto_rmdir(vnode_t *, char *, vnode_t *, cred_t *);
710Sstevel@tonic-gate static int auto_readdir(vnode_t *, uio_t *, cred_t *, int *);
720Sstevel@tonic-gate static int auto_symlink(vnode_t *, char *, vattr_t *, char *, cred_t *);
730Sstevel@tonic-gate static int auto_readlink(vnode_t *, struct uio *, cred_t *);
740Sstevel@tonic-gate static int auto_fsync(vnode_t *, int, cred_t *);
750Sstevel@tonic-gate static void auto_inactive(vnode_t *, cred_t *);
760Sstevel@tonic-gate static int auto_rwlock(vnode_t *, int, caller_context_t *);
770Sstevel@tonic-gate static void auto_rwunlock(vnode_t *vp, int, caller_context_t *);
780Sstevel@tonic-gate static int auto_seek(vnode_t *vp, offset_t, offset_t *);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static int auto_trigger_mount(vnode_t *, cred_t *, vnode_t **);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate vnodeops_t *auto_vnodeops;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate const fs_operation_def_t auto_vnodeops_template[] = {
853898Srsb 	VOPNAME_OPEN,		{ .vop_open = auto_open },
863898Srsb 	VOPNAME_CLOSE,		{ .vop_close = auto_close },
873898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = auto_getattr },
883898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = auto_setattr },
893898Srsb 	VOPNAME_ACCESS,		{ .vop_access = auto_access },
903898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = auto_lookup },
913898Srsb 	VOPNAME_CREATE,		{ .vop_create = auto_create },
923898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = auto_remove },
933898Srsb 	VOPNAME_LINK,		{ .vop_link = auto_link },
943898Srsb 	VOPNAME_RENAME,		{ .vop_rename = auto_rename },
953898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = auto_mkdir },
963898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = auto_rmdir },
973898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = auto_readdir },
983898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = auto_symlink },
993898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = auto_readlink },
1003898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = auto_fsync },
1013898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = auto_inactive },
1023898Srsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = auto_rwlock },
1033898Srsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = auto_rwunlock },
1043898Srsb 	VOPNAME_SEEK,		{ .vop_seek = auto_seek },
1053898Srsb 	VOPNAME_FRLOCK,		{ .error = fs_error },
1063898Srsb 	VOPNAME_DISPOSE,	{ .error = fs_error },
1073898Srsb 	VOPNAME_SHRLOCK,	{ .error = fs_error },
108*4863Spraks 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
1093898Srsb 	NULL,			NULL
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1122170Sevanl 
1132170Sevanl 
1140Sstevel@tonic-gate /* ARGSUSED */
1150Sstevel@tonic-gate static int
1160Sstevel@tonic-gate auto_open(vnode_t **vpp, int flag, cred_t *cred)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	vnode_t *newvp;
1190Sstevel@tonic-gate 	int error;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_open: *vpp=%p\n", (void *)*vpp));
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	error = auto_trigger_mount(*vpp, cred, &newvp);
1240Sstevel@tonic-gate 	if (error)
1250Sstevel@tonic-gate 		goto done;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (newvp != NULL) {
1280Sstevel@tonic-gate 		/*
1290Sstevel@tonic-gate 		 * Node is now mounted on.
1300Sstevel@tonic-gate 		 */
1310Sstevel@tonic-gate 		VN_RELE(*vpp);
1320Sstevel@tonic-gate 		*vpp = newvp;
1330Sstevel@tonic-gate 		error = VOP_ACCESS(*vpp, VREAD, 0, cred);
1340Sstevel@tonic-gate 		if (!error)
1350Sstevel@tonic-gate 			error = VOP_OPEN(vpp, flag, cred);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate done:
1390Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_open: *vpp=%p error=%d\n", (void *)*vpp,
1400Sstevel@tonic-gate 	    error));
1410Sstevel@tonic-gate 	return (error);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /* ARGSUSED */
1450Sstevel@tonic-gate static int
1460Sstevel@tonic-gate auto_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cred)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	return (0);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static int
1520Sstevel@tonic-gate auto_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cred)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
1550Sstevel@tonic-gate 	vnode_t *newvp;
1560Sstevel@tonic-gate 	vfs_t *vfsp;
1570Sstevel@tonic-gate 	int error;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_getattr vp %p\n", (void *)vp));
1600Sstevel@tonic-gate 
161149Scasper 	/*
162149Scasper 	 * Recursive auto_getattr/mount; go to the vfsp == NULL
163149Scasper 	 * case.
164149Scasper 	 */
165149Scasper 	if (vn_vfswlock_held(vp))
166149Scasper 		goto defattr;
167149Scasper 
1681153Snr123932 	if (error = vn_vfsrlock_wait(vp))
1690Sstevel@tonic-gate 		return (error);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	vfsp = vn_mountedvfs(vp);
1720Sstevel@tonic-gate 	if (vfsp != NULL) {
1730Sstevel@tonic-gate 		/*
1740Sstevel@tonic-gate 		 * Node is mounted on.
1750Sstevel@tonic-gate 		 */
1761153Snr123932 		error = VFS_ROOT(vfsp, &newvp);
1770Sstevel@tonic-gate 		vn_vfsunlock(vp);
1780Sstevel@tonic-gate 		if (error)
1790Sstevel@tonic-gate 			return (error);
1800Sstevel@tonic-gate 		mutex_enter(&fnp->fn_lock);
1810Sstevel@tonic-gate 		if (fnp->fn_seen == newvp && fnp->fn_thread == curthread) {
1820Sstevel@tonic-gate 			/*
1830Sstevel@tonic-gate 			 * Recursive auto_getattr(); just release newvp and drop
1840Sstevel@tonic-gate 			 * into the vfsp == NULL case.
1850Sstevel@tonic-gate 			 */
1860Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
1870Sstevel@tonic-gate 			VN_RELE(newvp);
1880Sstevel@tonic-gate 		} else {
1890Sstevel@tonic-gate 			while (fnp->fn_thread && fnp->fn_thread != curthread) {
1900Sstevel@tonic-gate 				fnp->fn_flags |= MF_ATTR_WAIT;
1910Sstevel@tonic-gate 				cv_wait(&fnp->fn_cv_mount, &fnp->fn_lock);
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 			fnp->fn_thread = curthread;
1940Sstevel@tonic-gate 			fnp->fn_seen = newvp;
1950Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
1960Sstevel@tonic-gate 			error = VOP_GETATTR(newvp, vap, flags, cred);
1970Sstevel@tonic-gate 			VN_RELE(newvp);
1980Sstevel@tonic-gate 			mutex_enter(&fnp->fn_lock);
1990Sstevel@tonic-gate 			fnp->fn_seen = 0;
2000Sstevel@tonic-gate 			fnp->fn_thread = 0;
2010Sstevel@tonic-gate 			if (fnp->fn_flags & MF_ATTR_WAIT) {
2020Sstevel@tonic-gate 				fnp->fn_flags &= ~MF_ATTR_WAIT;
2030Sstevel@tonic-gate 				cv_broadcast(&fnp->fn_cv_mount);
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
2060Sstevel@tonic-gate 			return (error);
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 	} else {
2090Sstevel@tonic-gate 		vn_vfsunlock(vp);
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
212149Scasper defattr:
2130Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
2140Sstevel@tonic-gate 	vap->va_uid	= 0;
2150Sstevel@tonic-gate 	vap->va_gid	= 0;
2160Sstevel@tonic-gate 	vap->va_nlink	= fnp->fn_linkcnt;
2170Sstevel@tonic-gate 	vap->va_nodeid	= (u_longlong_t)fnp->fn_nodeid;
2180Sstevel@tonic-gate 	vap->va_size	= fnp->fn_size;
2190Sstevel@tonic-gate 	vap->va_atime	= fnp->fn_atime;
2200Sstevel@tonic-gate 	vap->va_mtime	= fnp->fn_mtime;
2210Sstevel@tonic-gate 	vap->va_ctime	= fnp->fn_ctime;
2220Sstevel@tonic-gate 	vap->va_type	= vp->v_type;
2230Sstevel@tonic-gate 	vap->va_mode	= fnp->fn_mode;
2240Sstevel@tonic-gate 	vap->va_fsid	= vp->v_vfsp->vfs_dev;
2250Sstevel@tonic-gate 	vap->va_rdev	= 0;
2260Sstevel@tonic-gate 	vap->va_blksize	= MAXBSIZE;
2270Sstevel@tonic-gate 	vap->va_nblocks	= (fsblkcnt64_t)btod(vap->va_size);
2280Sstevel@tonic-gate 	vap->va_seq	= 0;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	return (0);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate /*ARGSUSED4*/
2340Sstevel@tonic-gate static int
2350Sstevel@tonic-gate auto_setattr(
2360Sstevel@tonic-gate 	vnode_t *vp,
2370Sstevel@tonic-gate 	struct vattr *vap,
2380Sstevel@tonic-gate 	int flags,
2390Sstevel@tonic-gate 	cred_t *cred,
2400Sstevel@tonic-gate 	caller_context_t *ct)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	vnode_t *newvp;
2430Sstevel@tonic-gate 	int error;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_setattr vp %p\n", (void *)vp));
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if (error = auto_trigger_mount(vp, cred, &newvp))
2480Sstevel@tonic-gate 		goto done;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (newvp != NULL) {
2510Sstevel@tonic-gate 		/*
2520Sstevel@tonic-gate 		 * Node is mounted on.
2530Sstevel@tonic-gate 		 */
2540Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
2550Sstevel@tonic-gate 			error = EROFS;
2560Sstevel@tonic-gate 		else
2570Sstevel@tonic-gate 			error = VOP_SETATTR(newvp, vap, flags, cred, NULL);
2580Sstevel@tonic-gate 		VN_RELE(newvp);
2590Sstevel@tonic-gate 	} else
2600Sstevel@tonic-gate 		error = ENOSYS;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate done:
2630Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_setattr: error=%d\n", error));
2640Sstevel@tonic-gate 	return (error);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /* ARGSUSED */
2680Sstevel@tonic-gate static int
2690Sstevel@tonic-gate auto_access(vnode_t *vp, int mode, int flags, cred_t *cred)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
2720Sstevel@tonic-gate 	vnode_t *newvp;
2730Sstevel@tonic-gate 	int error;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_access: vp=%p\n", (void *)vp));
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (error = auto_trigger_mount(vp, cred, &newvp))
2780Sstevel@tonic-gate 		goto done;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if (newvp != NULL) {
2810Sstevel@tonic-gate 		/*
2820Sstevel@tonic-gate 		 * Node is mounted on.
2830Sstevel@tonic-gate 		 */
2840Sstevel@tonic-gate 		error = VOP_ACCESS(newvp, mode, 0, cred);
2850Sstevel@tonic-gate 		VN_RELE(newvp);
2860Sstevel@tonic-gate 	} else {
2870Sstevel@tonic-gate 		int shift = 0;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		/*
2900Sstevel@tonic-gate 		 * really interested in the autofs node, check the
2910Sstevel@tonic-gate 		 * access on it
2920Sstevel@tonic-gate 		 */
2930Sstevel@tonic-gate 		ASSERT(error == 0);
2940Sstevel@tonic-gate 		if (crgetuid(cred) != fnp->fn_uid) {
2950Sstevel@tonic-gate 			shift += 3;
2960Sstevel@tonic-gate 			if (groupmember(fnp->fn_gid, cred) == 0)
2970Sstevel@tonic-gate 				shift += 3;
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 		mode &= ~(fnp->fn_mode << shift);
3000Sstevel@tonic-gate 		if (mode != 0)
3010Sstevel@tonic-gate 			error = secpolicy_vnode_access(cred, vp, fnp->fn_uid,
3020Sstevel@tonic-gate 									mode);
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate done:
3060Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_access: error=%d\n", error));
3070Sstevel@tonic-gate 	return (error);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate static int
3110Sstevel@tonic-gate auto_lookup(
3120Sstevel@tonic-gate 	vnode_t *dvp,
3130Sstevel@tonic-gate 	char *nm,
3140Sstevel@tonic-gate 	vnode_t **vpp,
3150Sstevel@tonic-gate 	pathname_t *pnp,
3160Sstevel@tonic-gate 	int flags,
3170Sstevel@tonic-gate 	vnode_t *rdir,
3180Sstevel@tonic-gate 	cred_t *cred)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	int error = 0;
3210Sstevel@tonic-gate 	vnode_t *newvp = NULL;
3220Sstevel@tonic-gate 	vfs_t *vfsp;
3230Sstevel@tonic-gate 	fninfo_t *dfnip;
3240Sstevel@tonic-gate 	fnnode_t *dfnp = NULL;
3250Sstevel@tonic-gate 	fnnode_t *fnp = NULL;
3260Sstevel@tonic-gate 	char *searchnm;
3270Sstevel@tonic-gate 	int operation;		/* either AUTOFS_LOOKUP or AUTOFS_MOUNT */
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	dfnip = vfstofni(dvp->v_vfsp);
3300Sstevel@tonic-gate 	AUTOFS_DPRINT((3, "auto_lookup: dvp=%p (%s) name=%s\n",
3310Sstevel@tonic-gate 	    (void *)dvp, dfnip->fi_map, nm));
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	if (nm[0] == 0) {
3340Sstevel@tonic-gate 		VN_HOLD(dvp);
3350Sstevel@tonic-gate 		*vpp = dvp;
3360Sstevel@tonic-gate 		return (0);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (error = VOP_ACCESS(dvp, VEXEC, 0, cred))
3400Sstevel@tonic-gate 		return (error);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if (nm[0] == '.' && nm[1] == 0) {
3430Sstevel@tonic-gate 		VN_HOLD(dvp);
3440Sstevel@tonic-gate 		*vpp = dvp;
3450Sstevel@tonic-gate 		return (0);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (nm[0] == '.' && nm[1] == '.' && nm[2] == 0) {
3490Sstevel@tonic-gate 		fnnode_t *pdfnp;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		pdfnp = (vntofn(dvp))->fn_parent;
3520Sstevel@tonic-gate 		ASSERT(pdfnp != NULL);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		/*
3550Sstevel@tonic-gate 		 * Since it is legitimate to have the VROOT flag set for the
3560Sstevel@tonic-gate 		 * subdirectories of the indirect map in autofs filesystem,
3570Sstevel@tonic-gate 		 * rootfnnodep is checked against fnnode of dvp instead of
3580Sstevel@tonic-gate 		 * just checking whether VROOT flag is set in dvp
3590Sstevel@tonic-gate 		 */
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 		if (pdfnp == pdfnp->fn_globals->fng_rootfnnodep) {
3620Sstevel@tonic-gate 			vnode_t *vp;
3630Sstevel@tonic-gate 
3641153Snr123932 			vfs_rlock_wait(dvp->v_vfsp);
3650Sstevel@tonic-gate 			if (dvp->v_vfsp->vfs_flag & VFS_UNMOUNTED) {
3660Sstevel@tonic-gate 				vfs_unlock(dvp->v_vfsp);
3670Sstevel@tonic-gate 				return (EIO);
3680Sstevel@tonic-gate 			}
3690Sstevel@tonic-gate 			vp = dvp->v_vfsp->vfs_vnodecovered;
3700Sstevel@tonic-gate 			VN_HOLD(vp);
3710Sstevel@tonic-gate 			vfs_unlock(dvp->v_vfsp);
3720Sstevel@tonic-gate 			error = VOP_LOOKUP(vp, nm, vpp, pnp, flags, rdir, cred);
3730Sstevel@tonic-gate 			VN_RELE(vp);
3740Sstevel@tonic-gate 			return (error);
3750Sstevel@tonic-gate 		} else {
3760Sstevel@tonic-gate 			*vpp = fntovn(pdfnp);
3770Sstevel@tonic-gate 			VN_HOLD(*vpp);
3780Sstevel@tonic-gate 			return (0);
3790Sstevel@tonic-gate 		}
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate top:
3830Sstevel@tonic-gate 	dfnp = vntofn(dvp);
3840Sstevel@tonic-gate 	searchnm = nm;
3850Sstevel@tonic-gate 	operation = 0;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	ASSERT(vn_matchops(dvp, auto_vnodeops));
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	AUTOFS_DPRINT((3, "auto_lookup: dvp=%p dfnp=%p\n", (void *)dvp,
3900Sstevel@tonic-gate 	    (void *)dfnp));
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	/*
3930Sstevel@tonic-gate 	 * If a lookup or mount of this node is in progress, wait for it
3940Sstevel@tonic-gate 	 * to finish, and return whatever result it got.
3950Sstevel@tonic-gate 	 */
3960Sstevel@tonic-gate 	mutex_enter(&dfnp->fn_lock);
3970Sstevel@tonic-gate 	if (dfnp->fn_flags & (MF_LOOKUP | MF_INPROG)) {
3980Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
3990Sstevel@tonic-gate 		error = auto_wait4mount(dfnp);
4000Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN)
4010Sstevel@tonic-gate 			error = ENOENT;
4020Sstevel@tonic-gate 		if (error == EAGAIN)
4030Sstevel@tonic-gate 			goto top;
4040Sstevel@tonic-gate 		if (error)
4050Sstevel@tonic-gate 			return (error);
4060Sstevel@tonic-gate 	} else
4070Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 
4101153Snr123932 	error = vn_vfsrlock_wait(dvp);
4110Sstevel@tonic-gate 	if (error)
4120Sstevel@tonic-gate 		return (error);
4130Sstevel@tonic-gate 	vfsp = vn_mountedvfs(dvp);
4140Sstevel@tonic-gate 	if (vfsp != NULL) {
4151153Snr123932 		error = VFS_ROOT(vfsp, &newvp);
4160Sstevel@tonic-gate 		vn_vfsunlock(dvp);
4170Sstevel@tonic-gate 		if (!error) {
4180Sstevel@tonic-gate 			error = VOP_LOOKUP(newvp, nm, vpp, pnp,
4190Sstevel@tonic-gate 			    flags, rdir, cred);
4200Sstevel@tonic-gate 			VN_RELE(newvp);
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 		return (error);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	vn_vfsunlock(dvp);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	rw_enter(&dfnp->fn_rwlock, RW_READER);
4270Sstevel@tonic-gate 	error = auto_search(dfnp, nm, &fnp, cred);
4280Sstevel@tonic-gate 	if (error) {
4290Sstevel@tonic-gate 		if (dfnip->fi_flags & MF_DIRECT) {
4300Sstevel@tonic-gate 			/*
4310Sstevel@tonic-gate 			 * direct map.
4320Sstevel@tonic-gate 			 */
4330Sstevel@tonic-gate 			if (dfnp->fn_dirents) {
4340Sstevel@tonic-gate 				/*
4350Sstevel@tonic-gate 				 * Mount previously triggered.
4360Sstevel@tonic-gate 				 * 'nm' not found
4370Sstevel@tonic-gate 				 */
4380Sstevel@tonic-gate 				error = ENOENT;
4390Sstevel@tonic-gate 			} else {
4400Sstevel@tonic-gate 				/*
4410Sstevel@tonic-gate 				 * I need to contact the daemon to trigger
4420Sstevel@tonic-gate 				 * the mount. 'dfnp' will be the mountpoint.
4430Sstevel@tonic-gate 				 */
4440Sstevel@tonic-gate 				operation = AUTOFS_MOUNT;
4450Sstevel@tonic-gate 				VN_HOLD(fntovn(dfnp));
4460Sstevel@tonic-gate 				fnp = dfnp;
4470Sstevel@tonic-gate 				error = 0;
4480Sstevel@tonic-gate 			}
4490Sstevel@tonic-gate 		} else if (dvp == dfnip->fi_rootvp) {
4500Sstevel@tonic-gate 			/*
4510Sstevel@tonic-gate 			 * 'dfnp' is the root of the indirect AUTOFS.
4520Sstevel@tonic-gate 			 */
4530Sstevel@tonic-gate 			if (rw_tryupgrade(&dfnp->fn_rwlock) == 0) {
4540Sstevel@tonic-gate 				/*
4550Sstevel@tonic-gate 				 * Could not acquire writer lock, release
4560Sstevel@tonic-gate 				 * reader, and wait until available. We
4570Sstevel@tonic-gate 				 * need to search for 'nm' again, since we
4580Sstevel@tonic-gate 				 * had to release the lock before reacquiring
4590Sstevel@tonic-gate 				 * it.
4600Sstevel@tonic-gate 				 */
4610Sstevel@tonic-gate 				rw_exit(&dfnp->fn_rwlock);
4620Sstevel@tonic-gate 				rw_enter(&dfnp->fn_rwlock, RW_WRITER);
4630Sstevel@tonic-gate 				error = auto_search(dfnp, nm, &fnp, cred);
4640Sstevel@tonic-gate 			}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 			ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
4670Sstevel@tonic-gate 			if (error) {
4680Sstevel@tonic-gate 				/*
4690Sstevel@tonic-gate 				 * create node being looked-up and request
4700Sstevel@tonic-gate 				 * mount on it.
4710Sstevel@tonic-gate 				 */
4720Sstevel@tonic-gate 				error = auto_enter(dfnp, nm, &fnp, kcred);
4730Sstevel@tonic-gate 				if (!error)
4740Sstevel@tonic-gate 					operation = AUTOFS_LOOKUP;
4750Sstevel@tonic-gate 			}
4760Sstevel@tonic-gate 		} else if ((dfnp->fn_dirents == NULL) &&
4770Sstevel@tonic-gate 		    ((dvp->v_flag & VROOT) == 0) &&
4780Sstevel@tonic-gate 		    ((fntovn(dfnp->fn_parent))->v_flag & VROOT)) {
4790Sstevel@tonic-gate 			/*
4800Sstevel@tonic-gate 			 * dfnp is the actual 'mountpoint' of indirect map,
4810Sstevel@tonic-gate 			 * it is the equivalent of a direct mount,
4820Sstevel@tonic-gate 			 * ie, /home/'user1'
4830Sstevel@tonic-gate 			 */
4840Sstevel@tonic-gate 			operation = AUTOFS_MOUNT;
4850Sstevel@tonic-gate 			VN_HOLD(fntovn(dfnp));
4860Sstevel@tonic-gate 			fnp = dfnp;
4870Sstevel@tonic-gate 			error = 0;
4880Sstevel@tonic-gate 			searchnm = dfnp->fn_name;
4890Sstevel@tonic-gate 		}
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	if (error == EAGAIN) {
4930Sstevel@tonic-gate 		rw_exit(&dfnp->fn_rwlock);
4940Sstevel@tonic-gate 		goto top;
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 	if (error) {
4970Sstevel@tonic-gate 		rw_exit(&dfnp->fn_rwlock);
4980Sstevel@tonic-gate 		return (error);
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/*
5020Sstevel@tonic-gate 	 * We now have the actual fnnode we're interested in.
5030Sstevel@tonic-gate 	 * The 'MF_LOOKUP' indicates another thread is currently
5040Sstevel@tonic-gate 	 * performing a daemon lookup of this node, therefore we
5050Sstevel@tonic-gate 	 * wait for its completion.
5060Sstevel@tonic-gate 	 * The 'MF_INPROG' indicates another thread is currently
5070Sstevel@tonic-gate 	 * performing a daemon mount of this node, we wait for it
5080Sstevel@tonic-gate 	 * to be done if we are performing a MOUNT. We don't
5090Sstevel@tonic-gate 	 * wait for it if we are performing a LOOKUP.
5100Sstevel@tonic-gate 	 * We can release the reader/writer lock as soon as we acquire
5110Sstevel@tonic-gate 	 * the mutex, since the state of the lock can only change by
5120Sstevel@tonic-gate 	 * first acquiring the mutex.
5130Sstevel@tonic-gate 	 */
5140Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
5150Sstevel@tonic-gate 	rw_exit(&dfnp->fn_rwlock);
5160Sstevel@tonic-gate 	if ((fnp->fn_flags & MF_LOOKUP) ||
5170Sstevel@tonic-gate 	    ((operation == AUTOFS_MOUNT) && (fnp->fn_flags & MF_INPROG))) {
5180Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
5190Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
5200Sstevel@tonic-gate 		VN_RELE(fntovn(fnp));
5210Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN)
5220Sstevel@tonic-gate 			error = ENOENT;
5230Sstevel@tonic-gate 		if (error && error != EAGAIN)
5240Sstevel@tonic-gate 			return (error);
5250Sstevel@tonic-gate 		goto top;
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if (operation == 0) {
5290Sstevel@tonic-gate 		/*
5300Sstevel@tonic-gate 		 * got the fnnode, check for any errors
5310Sstevel@tonic-gate 		 * on the previous operation on that node.
5320Sstevel@tonic-gate 		 */
5330Sstevel@tonic-gate 		error = fnp->fn_error;
5340Sstevel@tonic-gate 		if ((error == EINTR) || (error == EAGAIN)) {
5350Sstevel@tonic-gate 			/*
5360Sstevel@tonic-gate 			 * previous operation on this node was
5370Sstevel@tonic-gate 			 * not completed, do a lookup now.
5380Sstevel@tonic-gate 			 */
5390Sstevel@tonic-gate 			operation = AUTOFS_LOOKUP;
5400Sstevel@tonic-gate 		} else {
5410Sstevel@tonic-gate 			/*
5420Sstevel@tonic-gate 			 * previous operation completed. Return
5430Sstevel@tonic-gate 			 * a pointer to the node only if there was
5440Sstevel@tonic-gate 			 * no error.
5450Sstevel@tonic-gate 			 */
5460Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
5470Sstevel@tonic-gate 			if (!error)
5480Sstevel@tonic-gate 				*vpp = fntovn(fnp);
5490Sstevel@tonic-gate 			else
5500Sstevel@tonic-gate 				VN_RELE(fntovn(fnp));
5510Sstevel@tonic-gate 			return (error);
5520Sstevel@tonic-gate 		}
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	/*
5560Sstevel@tonic-gate 	 * Since I got to this point, it means I'm the one
5570Sstevel@tonic-gate 	 * responsible for triggering the mount/look-up of this node.
5580Sstevel@tonic-gate 	 */
5590Sstevel@tonic-gate 	switch (operation) {
5600Sstevel@tonic-gate 	case AUTOFS_LOOKUP:
5610Sstevel@tonic-gate 		AUTOFS_BLOCK_OTHERS(fnp, MF_LOOKUP);
5620Sstevel@tonic-gate 		fnp->fn_error = 0;
5630Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
5640Sstevel@tonic-gate 		error = auto_lookup_aux(fnp, searchnm, cred);
5650Sstevel@tonic-gate 		if (!error) {
5660Sstevel@tonic-gate 			/*
5670Sstevel@tonic-gate 			 * Return this vnode
5680Sstevel@tonic-gate 			 */
5690Sstevel@tonic-gate 			*vpp = fntovn(fnp);
5700Sstevel@tonic-gate 		} else {
5710Sstevel@tonic-gate 			/*
5720Sstevel@tonic-gate 			 * release our reference to this vnode
5730Sstevel@tonic-gate 			 * and return error
5740Sstevel@tonic-gate 			 */
5750Sstevel@tonic-gate 			VN_RELE(fntovn(fnp));
5760Sstevel@tonic-gate 		}
5770Sstevel@tonic-gate 		break;
5780Sstevel@tonic-gate 	case AUTOFS_MOUNT:
5790Sstevel@tonic-gate 		AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
5800Sstevel@tonic-gate 		fnp->fn_error = 0;
5810Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
5820Sstevel@tonic-gate 		/*
5830Sstevel@tonic-gate 		 * auto_new_mount_thread fires up a new thread which
5840Sstevel@tonic-gate 		 * calls automountd finishing up the work
5850Sstevel@tonic-gate 		 */
5860Sstevel@tonic-gate 		auto_new_mount_thread(fnp, searchnm, cred);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 		/*
5890Sstevel@tonic-gate 		 * At this point, we are simply another thread
5900Sstevel@tonic-gate 		 * waiting for the mount to complete
5910Sstevel@tonic-gate 		 */
5920Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
5930Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN)
5940Sstevel@tonic-gate 			error = ENOENT;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		/*
5970Sstevel@tonic-gate 		 * now release our reference to this vnode
5980Sstevel@tonic-gate 		 */
5990Sstevel@tonic-gate 		VN_RELE(fntovn(fnp));
6000Sstevel@tonic-gate 		if (!error)
6010Sstevel@tonic-gate 			goto top;
6020Sstevel@tonic-gate 		break;
6030Sstevel@tonic-gate 	default:
6042170Sevanl 		auto_log(dfnp->fn_globals->fng_verbose,
6052170Sevanl 			dfnp->fn_globals->fng_zoneid, CE_WARN,
6062170Sevanl 			"auto_lookup: unknown "
6070Sstevel@tonic-gate 		    "operation %d", operation);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_lookup: name=%s *vpp=%p return=%d\n",
6110Sstevel@tonic-gate 	    nm, (void *)*vpp, error));
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	return (error);
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate static int
6170Sstevel@tonic-gate auto_create(
6180Sstevel@tonic-gate 	vnode_t *dvp,
6190Sstevel@tonic-gate 	char *nm,
6200Sstevel@tonic-gate 	vattr_t *va,
6210Sstevel@tonic-gate 	vcexcl_t excl,
6220Sstevel@tonic-gate 	int mode,
6230Sstevel@tonic-gate 	vnode_t **vpp,
6240Sstevel@tonic-gate 	cred_t *cred,
6250Sstevel@tonic-gate 	int flag)
6260Sstevel@tonic-gate {
6270Sstevel@tonic-gate 	vnode_t *newvp;
6280Sstevel@tonic-gate 	int error;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_create dvp %p nm %s\n", (void *)dvp, nm));
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
6330Sstevel@tonic-gate 		goto done;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (newvp != NULL) {
6360Sstevel@tonic-gate 		/*
6370Sstevel@tonic-gate 		 * Node is now mounted on.
6380Sstevel@tonic-gate 		 */
6390Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
6400Sstevel@tonic-gate 			error = EROFS;
6410Sstevel@tonic-gate 		else
6420Sstevel@tonic-gate 			error = VOP_CREATE(newvp, nm, va, excl,
6430Sstevel@tonic-gate 			    mode, vpp, cred, flag);
6440Sstevel@tonic-gate 		VN_RELE(newvp);
6450Sstevel@tonic-gate 	} else
6460Sstevel@tonic-gate 		error = ENOSYS;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate done:
6490Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_create: error=%d\n", error));
6500Sstevel@tonic-gate 	return (error);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate static int
6540Sstevel@tonic-gate auto_remove(vnode_t *dvp, char *nm, cred_t *cred)
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate 	vnode_t *newvp;
6570Sstevel@tonic-gate 	int error;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_remove dvp %p nm %s\n", (void *)dvp, nm));
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
6620Sstevel@tonic-gate 		goto done;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if (newvp != NULL) {
6650Sstevel@tonic-gate 		/*
6660Sstevel@tonic-gate 		 * Node is now mounted on.
6670Sstevel@tonic-gate 		 */
6680Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
6690Sstevel@tonic-gate 			error = EROFS;
6700Sstevel@tonic-gate 		else
6710Sstevel@tonic-gate 			error = VOP_REMOVE(newvp, nm, cred);
6720Sstevel@tonic-gate 		VN_RELE(newvp);
6730Sstevel@tonic-gate 	} else
6740Sstevel@tonic-gate 		error = ENOSYS;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate done:
6770Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_remove: error=%d\n", error));
6780Sstevel@tonic-gate 	return (error);
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate static int
6820Sstevel@tonic-gate auto_link(vnode_t *tdvp, vnode_t *svp, char *nm, cred_t *cred)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate 	vnode_t *newvp;
6850Sstevel@tonic-gate 	int error;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_link tdvp %p svp %p nm %s\n", (void *)tdvp,
6880Sstevel@tonic-gate 	    (void *)svp, nm));
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	if (error = auto_trigger_mount(tdvp, cred, &newvp))
6910Sstevel@tonic-gate 		goto done;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if (newvp == NULL) {
6940Sstevel@tonic-gate 		/*
6950Sstevel@tonic-gate 		 * an autonode can not be a link to another node
6960Sstevel@tonic-gate 		 */
6970Sstevel@tonic-gate 		error = ENOSYS;
6980Sstevel@tonic-gate 		goto done;
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	if (vn_is_readonly(newvp)) {
7020Sstevel@tonic-gate 		error = EROFS;
7030Sstevel@tonic-gate 		VN_RELE(newvp);
7040Sstevel@tonic-gate 		goto done;
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if (vn_matchops(svp, auto_vnodeops)) {
7080Sstevel@tonic-gate 		/*
7090Sstevel@tonic-gate 		 * source vp can't be an autonode
7100Sstevel@tonic-gate 		 */
7110Sstevel@tonic-gate 		error = ENOSYS;
7120Sstevel@tonic-gate 		VN_RELE(newvp);
7130Sstevel@tonic-gate 		goto done;
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	error = VOP_LINK(newvp, svp, nm, cred);
7170Sstevel@tonic-gate 	VN_RELE(newvp);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate done:
7200Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_link error=%d\n", error));
7210Sstevel@tonic-gate 	return (error);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate static int
7250Sstevel@tonic-gate auto_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate 	vnode_t *o_newvp, *n_newvp;
7280Sstevel@tonic-gate 	int error;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_rename odvp %p onm %s to ndvp %p nnm %s\n",
7310Sstevel@tonic-gate 	    (void *)odvp, onm, (void *)ndvp, nnm));
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	/*
7340Sstevel@tonic-gate 	 * we know odvp is an autonode, otherwise this function
7350Sstevel@tonic-gate 	 * could not have ever been called.
7360Sstevel@tonic-gate 	 */
7370Sstevel@tonic-gate 	ASSERT(vn_matchops(odvp, auto_vnodeops));
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if (error = auto_trigger_mount(odvp, cr, &o_newvp))
7400Sstevel@tonic-gate 		goto done;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if (o_newvp == NULL) {
7430Sstevel@tonic-gate 		/*
7440Sstevel@tonic-gate 		 * can't rename an autonode
7450Sstevel@tonic-gate 		 */
7460Sstevel@tonic-gate 		error = ENOSYS;
7470Sstevel@tonic-gate 		goto done;
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	if (vn_matchops(ndvp, auto_vnodeops)) {
7510Sstevel@tonic-gate 		/*
7520Sstevel@tonic-gate 		 * directory is AUTOFS, need to trigger the
7530Sstevel@tonic-gate 		 * mount of the real filesystem.
7540Sstevel@tonic-gate 		 */
7550Sstevel@tonic-gate 		if (error = auto_trigger_mount(ndvp, cr, &n_newvp)) {
7560Sstevel@tonic-gate 			VN_RELE(o_newvp);
7570Sstevel@tonic-gate 			goto done;
7580Sstevel@tonic-gate 		}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		if (n_newvp == NULL) {
7610Sstevel@tonic-gate 			/*
7620Sstevel@tonic-gate 			 * target can't be an autonode
7630Sstevel@tonic-gate 			 */
7640Sstevel@tonic-gate 			error = ENOSYS;
7650Sstevel@tonic-gate 			VN_RELE(o_newvp);
7660Sstevel@tonic-gate 			goto done;
7670Sstevel@tonic-gate 		}
7680Sstevel@tonic-gate 	} else {
7690Sstevel@tonic-gate 		/*
7700Sstevel@tonic-gate 		 * destination directory mount had been
7710Sstevel@tonic-gate 		 * triggered prior to the call to this function.
7720Sstevel@tonic-gate 		 */
7730Sstevel@tonic-gate 		n_newvp = ndvp;
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	ASSERT(!vn_matchops(n_newvp, auto_vnodeops));
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	if (vn_is_readonly(n_newvp)) {
7790Sstevel@tonic-gate 		error = EROFS;
7800Sstevel@tonic-gate 		VN_RELE(o_newvp);
7810Sstevel@tonic-gate 		if (n_newvp != ndvp)
7820Sstevel@tonic-gate 			VN_RELE(n_newvp);
7830Sstevel@tonic-gate 		goto done;
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	error = VOP_RENAME(o_newvp, onm, n_newvp, nnm, cr);
7870Sstevel@tonic-gate 	VN_RELE(o_newvp);
7880Sstevel@tonic-gate 	if (n_newvp != ndvp)
7890Sstevel@tonic-gate 		VN_RELE(n_newvp);
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate done:
7920Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_rename error=%d\n", error));
7930Sstevel@tonic-gate 	return (error);
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate static int
7970Sstevel@tonic-gate auto_mkdir(vnode_t *dvp, char *nm, vattr_t *va, vnode_t **vpp, cred_t *cred)
7980Sstevel@tonic-gate {
7990Sstevel@tonic-gate 	vnode_t *newvp;
8000Sstevel@tonic-gate 	int error;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_mkdir dvp %p nm %s\n", (void *)dvp, nm));
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
8050Sstevel@tonic-gate 		goto done;
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	if (newvp != NULL) {
8080Sstevel@tonic-gate 		/*
8090Sstevel@tonic-gate 		 * Node is now mounted on.
8100Sstevel@tonic-gate 		 */
8110Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
8120Sstevel@tonic-gate 			error = EROFS;
8130Sstevel@tonic-gate 		else
8140Sstevel@tonic-gate 			error = VOP_MKDIR(newvp, nm, va, vpp, cred);
8150Sstevel@tonic-gate 		VN_RELE(newvp);
8160Sstevel@tonic-gate 	} else
8170Sstevel@tonic-gate 		error = ENOSYS;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate done:
8200Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mkdir: error=%d\n", error));
8210Sstevel@tonic-gate 	return (error);
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate static int
8250Sstevel@tonic-gate auto_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cred)
8260Sstevel@tonic-gate {
8270Sstevel@tonic-gate 	vnode_t *newvp;
8280Sstevel@tonic-gate 	int error;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_rmdir: vp=%p nm=%s\n", (void *)dvp, nm));
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
8330Sstevel@tonic-gate 		goto done;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	if (newvp != NULL) {
8360Sstevel@tonic-gate 		/*
8370Sstevel@tonic-gate 		 * Node is now mounted on.
8380Sstevel@tonic-gate 		 */
8390Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
8400Sstevel@tonic-gate 			error = EROFS;
8410Sstevel@tonic-gate 		else
8420Sstevel@tonic-gate 			error = VOP_RMDIR(newvp, nm, cdir, cred);
8430Sstevel@tonic-gate 		VN_RELE(newvp);
8440Sstevel@tonic-gate 	} else
8450Sstevel@tonic-gate 		error = ENOSYS;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate done:
8480Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_rmdir: error=%d\n", error));
8490Sstevel@tonic-gate 	return (error);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate static int autofs_nobrowse = 0;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate #ifdef nextdp
8550Sstevel@tonic-gate #undef nextdp
8560Sstevel@tonic-gate #endif
8570Sstevel@tonic-gate #define	nextdp(dp)	((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate static int
8600Sstevel@tonic-gate auto_readdir(vnode_t *vp, uio_t *uiop, cred_t *cred, int *eofp)
8610Sstevel@tonic-gate {
8622170Sevanl 	struct autofs_rddirargs	rda;
8632170Sevanl 	autofs_rddirres rd;
8640Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
8650Sstevel@tonic-gate 	fnnode_t *cfnp, *nfnp;
8660Sstevel@tonic-gate 	dirent64_t *dp;
8670Sstevel@tonic-gate 	ulong_t offset;
8680Sstevel@tonic-gate 	ulong_t outcount = 0, count = 0;
8690Sstevel@tonic-gate 	size_t namelen;
8700Sstevel@tonic-gate 	ulong_t alloc_count;
8712170Sevanl 	void *outbuf = NULL;
8720Sstevel@tonic-gate 	fninfo_t *fnip = vfstofni(vp->v_vfsp);
8730Sstevel@tonic-gate 	struct iovec *iovp;
8740Sstevel@tonic-gate 	int error = 0;
8750Sstevel@tonic-gate 	int reached_max = 0;
8760Sstevel@tonic-gate 	int myeof = 0;
8770Sstevel@tonic-gate 	int this_reclen;
8782170Sevanl 	struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_readdir vp=%p offset=%lld\n",
8810Sstevel@tonic-gate 	    (void *)vp, uiop->uio_loffset));
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (eofp != NULL)
8840Sstevel@tonic-gate 		*eofp = 0;
8850Sstevel@tonic-gate 
8862170Sevanl 	if (uiop->uio_iovcnt != 1)
8872170Sevanl 		return (EINVAL);
8882170Sevanl 
8890Sstevel@tonic-gate 	iovp = uiop->uio_iov;
8900Sstevel@tonic-gate 	alloc_count = iovp->iov_len;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	gethrestime(&fnp->fn_atime);
8930Sstevel@tonic-gate 	fnp->fn_ref_time = fnp->fn_atime.tv_sec;
8940Sstevel@tonic-gate 
8952170Sevanl 	dp = outbuf = kmem_zalloc(alloc_count, KM_SLEEP);
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/*
8980Sstevel@tonic-gate 	 * Held when getdents calls VOP_RWLOCK....
8990Sstevel@tonic-gate 	 */
9000Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&fnp->fn_rwlock));
9010Sstevel@tonic-gate 	if (uiop->uio_offset >= AUTOFS_DAEMONCOOKIE) {
9020Sstevel@tonic-gate again:
9030Sstevel@tonic-gate 		/*
9040Sstevel@tonic-gate 		 * Do readdir of daemon contents only
9050Sstevel@tonic-gate 		 * Drop readers lock and reacquire after reply.
9060Sstevel@tonic-gate 		 */
9070Sstevel@tonic-gate 		rw_exit(&fnp->fn_rwlock);
9082170Sevanl 		bzero(&rd, sizeof (struct autofs_rddirres));
9090Sstevel@tonic-gate 		count = 0;
9100Sstevel@tonic-gate 		rda.rda_map = fnip->fi_map;
9110Sstevel@tonic-gate 		rda.rda_offset = (uint_t)uiop->uio_offset;
9120Sstevel@tonic-gate 		rd.rd_rddir.rddir_entries = dp;
9130Sstevel@tonic-gate 		rda.rda_count = rd.rd_rddir.rddir_size = (uint_t)alloc_count;
9143391Ssemery 		rda.uid = crgetuid(cred);
9152170Sevanl 
9162170Sevanl 		error = auto_calldaemon(fngp->fng_zoneid,
9172170Sevanl 			AUTOFS_READDIR,
9182170Sevanl 			xdr_autofs_rddirargs,
9192170Sevanl 			&rda,
9202170Sevanl 			xdr_autofs_rddirres,
9212170Sevanl 			(void *)&rd,
9222170Sevanl 			sizeof (autofs_rddirres),
9232170Sevanl 			TRUE);
9242170Sevanl 
9250Sstevel@tonic-gate 		/*
9260Sstevel@tonic-gate 		 * reacquire previously dropped lock
9270Sstevel@tonic-gate 		 */
9280Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_READER);
9290Sstevel@tonic-gate 
9302170Sevanl 		if (!error) {
9310Sstevel@tonic-gate 			error = rd.rd_status;
9322170Sevanl 			dp = rd.rd_rddir.rddir_entries;
9332170Sevanl 		}
9342170Sevanl 
9350Sstevel@tonic-gate 		if (error) {
9360Sstevel@tonic-gate 			if (error == AUTOFS_SHUTDOWN) {
9370Sstevel@tonic-gate 				/*
9380Sstevel@tonic-gate 				 * treat as empty directory
9390Sstevel@tonic-gate 				 */
9400Sstevel@tonic-gate 				error = 0;
9410Sstevel@tonic-gate 				myeof = 1;
9420Sstevel@tonic-gate 				if (eofp)
9430Sstevel@tonic-gate 					*eofp = 1;
9440Sstevel@tonic-gate 			}
9450Sstevel@tonic-gate 			goto done;
9460Sstevel@tonic-gate 		}
9470Sstevel@tonic-gate 		if (rd.rd_rddir.rddir_size) {
9482170Sevanl 			dirent64_t *odp = dp;   /* next in output buffer */
9492170Sevanl 			dirent64_t *cdp = dp;   /* current examined entry */
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 			/*
9520Sstevel@tonic-gate 			 * Check for duplicates here
9530Sstevel@tonic-gate 			 */
9540Sstevel@tonic-gate 			do {
9550Sstevel@tonic-gate 				this_reclen = cdp->d_reclen;
9562170Sevanl 				if (auto_search(fnp, cdp->d_name,
9572170Sevanl 					NULL, cred)) {
9580Sstevel@tonic-gate 					/*
9590Sstevel@tonic-gate 					 * entry not found in kernel list,
9600Sstevel@tonic-gate 					 * include it in readdir output.
9610Sstevel@tonic-gate 					 *
9620Sstevel@tonic-gate 					 * If we are skipping entries. then
9630Sstevel@tonic-gate 					 * we need to copy this entry to the
9640Sstevel@tonic-gate 					 * correct position in the buffer
9650Sstevel@tonic-gate 					 * to be copied out.
9660Sstevel@tonic-gate 					 */
9670Sstevel@tonic-gate 					if (cdp != odp)
9680Sstevel@tonic-gate 						bcopy(cdp, odp,
9692170Sevanl 							(size_t)this_reclen);
9700Sstevel@tonic-gate 					odp = nextdp(odp);
9710Sstevel@tonic-gate 					outcount += this_reclen;
9720Sstevel@tonic-gate 				} else {
9730Sstevel@tonic-gate 					/*
9740Sstevel@tonic-gate 					 * Entry was found in the kernel
9750Sstevel@tonic-gate 					 * list. If it is the first entry
9760Sstevel@tonic-gate 					 * in this buffer, then just skip it
9770Sstevel@tonic-gate 					 */
9780Sstevel@tonic-gate 					if (odp == dp) {
9790Sstevel@tonic-gate 						dp = nextdp(dp);
9800Sstevel@tonic-gate 						odp = dp;
9810Sstevel@tonic-gate 					}
9820Sstevel@tonic-gate 				}
9830Sstevel@tonic-gate 				count += this_reclen;
9840Sstevel@tonic-gate 				cdp = (struct dirent64 *)
9852170Sevanl 					((char *)cdp + this_reclen);
9860Sstevel@tonic-gate 			} while (count < rd.rd_rddir.rddir_size);
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 			if (outcount)
9890Sstevel@tonic-gate 				error = uiomove(dp, outcount, UIO_READ, uiop);
9900Sstevel@tonic-gate 			uiop->uio_offset = rd.rd_rddir.rddir_offset;
9910Sstevel@tonic-gate 		} else {
9920Sstevel@tonic-gate 			if (rd.rd_rddir.rddir_eof == 0) {
9930Sstevel@tonic-gate 				/*
9940Sstevel@tonic-gate 				 * alloc_count not large enough for one
9950Sstevel@tonic-gate 				 * directory entry
9960Sstevel@tonic-gate 				 */
9970Sstevel@tonic-gate 				error = EINVAL;
9980Sstevel@tonic-gate 			}
9990Sstevel@tonic-gate 		}
10000Sstevel@tonic-gate 		if (rd.rd_rddir.rddir_eof && !error) {
10010Sstevel@tonic-gate 			myeof = 1;
10020Sstevel@tonic-gate 			if (eofp)
10030Sstevel@tonic-gate 				*eofp = 1;
10040Sstevel@tonic-gate 		}
10050Sstevel@tonic-gate 		if (!error && !myeof && outcount == 0) {
10060Sstevel@tonic-gate 			/*
10070Sstevel@tonic-gate 			 * call daemon with new cookie, all previous
10080Sstevel@tonic-gate 			 * elements happened to be duplicates
10090Sstevel@tonic-gate 			 */
10100Sstevel@tonic-gate 			dp = outbuf;
10110Sstevel@tonic-gate 			goto again;
10120Sstevel@tonic-gate 		}
10130Sstevel@tonic-gate 		goto done;
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (uiop->uio_offset == 0) {
10170Sstevel@tonic-gate 		/*
10180Sstevel@tonic-gate 		 * first time: so fudge the . and ..
10190Sstevel@tonic-gate 		 */
10200Sstevel@tonic-gate 		this_reclen = DIRENT64_RECLEN(1);
10210Sstevel@tonic-gate 		if (alloc_count < this_reclen) {
10220Sstevel@tonic-gate 			error = EINVAL;
10230Sstevel@tonic-gate 			goto done;
10240Sstevel@tonic-gate 		}
10250Sstevel@tonic-gate 		dp->d_ino = (ino64_t)fnp->fn_nodeid;
10260Sstevel@tonic-gate 		dp->d_off = (off64_t)1;
10270Sstevel@tonic-gate 		dp->d_reclen = (ushort_t)this_reclen;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 		(void) strncpy(dp->d_name, ".",
10322170Sevanl 			DIRENT64_NAMELEN(this_reclen));
10330Sstevel@tonic-gate 		outcount += dp->d_reclen;
10340Sstevel@tonic-gate 		dp = nextdp(dp);
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 		this_reclen = DIRENT64_RECLEN(2);
10370Sstevel@tonic-gate 		if (alloc_count < outcount + this_reclen) {
10380Sstevel@tonic-gate 			error = EINVAL;
10390Sstevel@tonic-gate 			goto done;
10400Sstevel@tonic-gate 		}
10410Sstevel@tonic-gate 		dp->d_reclen = (ushort_t)this_reclen;
10420Sstevel@tonic-gate 		dp->d_ino = (ino64_t)fnp->fn_parent->fn_nodeid;
10430Sstevel@tonic-gate 		dp->d_off = (off64_t)2;
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 		(void) strncpy(dp->d_name, "..",
10482170Sevanl 			DIRENT64_NAMELEN(this_reclen));
10490Sstevel@tonic-gate 		outcount += dp->d_reclen;
10500Sstevel@tonic-gate 		dp = nextdp(dp);
10510Sstevel@tonic-gate 	}
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	offset = 2;
10540Sstevel@tonic-gate 	cfnp = fnp->fn_dirents;
10550Sstevel@tonic-gate 	while (cfnp != NULL) {
10560Sstevel@tonic-gate 		nfnp = cfnp->fn_next;
10570Sstevel@tonic-gate 		offset = cfnp->fn_offset;
10580Sstevel@tonic-gate 		if ((offset >= uiop->uio_offset) &&
10592170Sevanl 			(!(cfnp->fn_flags & MF_LOOKUP))) {
10600Sstevel@tonic-gate 			int reclen;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 			/*
10630Sstevel@tonic-gate 			 * include node only if its offset is greater or
10640Sstevel@tonic-gate 			 * equal to the one required and it is not in
10650Sstevel@tonic-gate 			 * transient state (not being looked-up)
10660Sstevel@tonic-gate 			 */
10670Sstevel@tonic-gate 			namelen = strlen(cfnp->fn_name);
10680Sstevel@tonic-gate 			reclen = (int)DIRENT64_RECLEN(namelen);
10690Sstevel@tonic-gate 			if (outcount + reclen > alloc_count) {
10700Sstevel@tonic-gate 				reached_max = 1;
10710Sstevel@tonic-gate 				break;
10720Sstevel@tonic-gate 			}
10730Sstevel@tonic-gate 			dp->d_reclen = (ushort_t)reclen;
10740Sstevel@tonic-gate 			dp->d_ino = (ino64_t)cfnp->fn_nodeid;
10750Sstevel@tonic-gate 			if (nfnp != NULL) {
10760Sstevel@tonic-gate 				/*
10770Sstevel@tonic-gate 				 * get the offset of the next element
10780Sstevel@tonic-gate 				 */
10790Sstevel@tonic-gate 				dp->d_off = (off64_t)nfnp->fn_offset;
10800Sstevel@tonic-gate 			} else {
10810Sstevel@tonic-gate 				/*
10820Sstevel@tonic-gate 				 * This is the last element, make
10830Sstevel@tonic-gate 				 * offset one plus the current
10840Sstevel@tonic-gate 				 */
10850Sstevel@tonic-gate 				dp->d_off = (off64_t)cfnp->fn_offset + 1;
10860Sstevel@tonic-gate 			}
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 			/* use strncpy(9f) to zero out uninitialized bytes */
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 			(void) strncpy(dp->d_name, cfnp->fn_name,
10912170Sevanl 				DIRENT64_NAMELEN(reclen));
10920Sstevel@tonic-gate 			outcount += dp->d_reclen;
10930Sstevel@tonic-gate 			dp = nextdp(dp);
10940Sstevel@tonic-gate 		}
10950Sstevel@tonic-gate 		cfnp = nfnp;
10960Sstevel@tonic-gate 	}
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	if (outcount)
10990Sstevel@tonic-gate 		error = uiomove(outbuf, outcount, UIO_READ, uiop);
11000Sstevel@tonic-gate 	if (!error) {
11010Sstevel@tonic-gate 		if (reached_max) {
11020Sstevel@tonic-gate 			/*
11030Sstevel@tonic-gate 			 * This entry did not get added to the buffer on this,
11040Sstevel@tonic-gate 			 * call. We need to add it on the next call therefore
11050Sstevel@tonic-gate 			 * set uio_offset to this entry's offset.  If there
11060Sstevel@tonic-gate 			 * wasn't enough space for one dirent, return EINVAL.
11070Sstevel@tonic-gate 			 */
11080Sstevel@tonic-gate 			uiop->uio_offset = offset;
11090Sstevel@tonic-gate 			if (outcount == 0)
11100Sstevel@tonic-gate 				error = EINVAL;
11110Sstevel@tonic-gate 		} else if (autofs_nobrowse ||
11122170Sevanl 			auto_nobrowse_option(fnip->fi_opts) ||
11132170Sevanl 			(fnip->fi_flags & MF_DIRECT) ||
11142170Sevanl 			(fnp->fn_trigger != NULL) ||
11152170Sevanl 			(((vp->v_flag & VROOT) == 0) &&
11162170Sevanl 				((fntovn(fnp->fn_parent))->v_flag & VROOT) &&
11172170Sevanl 				(fnp->fn_dirents == NULL))) {
11180Sstevel@tonic-gate 			/*
11190Sstevel@tonic-gate 			 * done reading directory entries
11200Sstevel@tonic-gate 			 */
11210Sstevel@tonic-gate 			uiop->uio_offset = offset + 1;
11220Sstevel@tonic-gate 			if (eofp)
11230Sstevel@tonic-gate 				*eofp = 1;
11240Sstevel@tonic-gate 		} else {
11250Sstevel@tonic-gate 			/*
11260Sstevel@tonic-gate 			 * Need to get the rest of the entries from the daemon.
11270Sstevel@tonic-gate 			 */
11280Sstevel@tonic-gate 			uiop->uio_offset = AUTOFS_DAEMONCOOKIE;
11290Sstevel@tonic-gate 		}
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate done:
11330Sstevel@tonic-gate 	kmem_free(outbuf, alloc_count);
11340Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_readdir vp=%p offset=%lld eof=%d\n",
11352170Sevanl 		(void *)vp, uiop->uio_loffset, myeof));
11360Sstevel@tonic-gate 	return (error);
11370Sstevel@tonic-gate }
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate static int
11400Sstevel@tonic-gate auto_symlink(
11410Sstevel@tonic-gate 	vnode_t *dvp,
11420Sstevel@tonic-gate 	char *lnknm,		/* new entry */
11430Sstevel@tonic-gate 	vattr_t *tva,
11440Sstevel@tonic-gate 	char *tnm,		/* existing entry */
11450Sstevel@tonic-gate 	cred_t *cred)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate 	vnode_t *newvp;
11480Sstevel@tonic-gate 	int error;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_symlink: dvp=%p lnknm=%s tnm=%s\n",
11510Sstevel@tonic-gate 	    (void *)dvp, lnknm, tnm));
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
11540Sstevel@tonic-gate 		goto done;
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	if (newvp != NULL) {
11570Sstevel@tonic-gate 		/*
11580Sstevel@tonic-gate 		 * Node is mounted on.
11590Sstevel@tonic-gate 		 */
11600Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
11610Sstevel@tonic-gate 			error = EROFS;
11620Sstevel@tonic-gate 		else
11630Sstevel@tonic-gate 			error = VOP_SYMLINK(newvp, lnknm, tva, tnm, cred);
11640Sstevel@tonic-gate 		VN_RELE(newvp);
11650Sstevel@tonic-gate 	} else
11660Sstevel@tonic-gate 		error = ENOSYS;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate done:
11690Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_symlink: error=%d\n", error));
11700Sstevel@tonic-gate 	return (error);
11710Sstevel@tonic-gate }
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate /* ARGSUSED */
11740Sstevel@tonic-gate static int
11750Sstevel@tonic-gate auto_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr)
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
11780Sstevel@tonic-gate 	int error;
11790Sstevel@tonic-gate 	timestruc_t now;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_readlink: vp=%p\n", (void *)vp));
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 	gethrestime(&now);
11840Sstevel@tonic-gate 	fnp->fn_ref_time = now.tv_sec;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	if (vp->v_type != VLNK)
11870Sstevel@tonic-gate 		error = EINVAL;
11880Sstevel@tonic-gate 	else {
11890Sstevel@tonic-gate 		ASSERT(!(fnp->fn_flags & (MF_INPROG | MF_LOOKUP)));
11900Sstevel@tonic-gate 		fnp->fn_atime = now;
11910Sstevel@tonic-gate 		error = uiomove(fnp->fn_symlink, MIN(fnp->fn_symlinklen,
11920Sstevel@tonic-gate 		    uiop->uio_resid), UIO_READ, uiop);
11930Sstevel@tonic-gate 	}
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_readlink: error=%d\n", error));
11960Sstevel@tonic-gate 	return (error);
11970Sstevel@tonic-gate }
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate /* ARGSUSED */
12000Sstevel@tonic-gate static int
12010Sstevel@tonic-gate auto_fsync(vnode_t *cp, int syncflag, cred_t *cred)
12020Sstevel@tonic-gate {
12030Sstevel@tonic-gate 	return (0);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /* ARGSUSED */
12070Sstevel@tonic-gate static void
12080Sstevel@tonic-gate auto_inactive(vnode_t *vp, cred_t *cred)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
12110Sstevel@tonic-gate 	fnnode_t *dfnp = fnp->fn_parent;
12120Sstevel@tonic-gate 	int count;
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_inactive: vp=%p v_count=%u fn_link=%d\n",
12150Sstevel@tonic-gate 	    (void *)vp, vp->v_count, fnp->fn_linkcnt));
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	/*
12180Sstevel@tonic-gate 	 * The rwlock should not be already held by this thread.
12190Sstevel@tonic-gate 	 * The assert relies on the fact that the owner field is cleared
12200Sstevel@tonic-gate 	 * when the lock is released.
12210Sstevel@tonic-gate 	 */
12220Sstevel@tonic-gate 	ASSERT(dfnp != NULL);
12230Sstevel@tonic-gate 	ASSERT(rw_owner(&dfnp->fn_rwlock) != curthread);
12240Sstevel@tonic-gate 	rw_enter(&dfnp->fn_rwlock, RW_WRITER);
12250Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
12260Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
12270Sstevel@tonic-gate 	count = --vp->v_count;
12280Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
12290Sstevel@tonic-gate 	if (count == 0) {
12300Sstevel@tonic-gate 		/*
12310Sstevel@tonic-gate 		 * Free only if node has no subdirectories.
12320Sstevel@tonic-gate 		 */
12330Sstevel@tonic-gate 		if (fnp->fn_linkcnt == 1) {
12340Sstevel@tonic-gate 			auto_disconnect(dfnp, fnp);
12350Sstevel@tonic-gate 			rw_exit(&dfnp->fn_rwlock);
12360Sstevel@tonic-gate 			auto_freefnnode(fnp);
12370Sstevel@tonic-gate 			AUTOFS_DPRINT((5, "auto_inactive: (exit) vp=%p freed\n",
12380Sstevel@tonic-gate 			    (void *)vp));
12390Sstevel@tonic-gate 			return;
12400Sstevel@tonic-gate 		}
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 	rw_exit(&dfnp->fn_rwlock);
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_inactive: (exit) vp=%p v_count=%u fn_link=%d\n",
12450Sstevel@tonic-gate 	    (void *)vp, vp->v_count, fnp->fn_linkcnt));
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate /* ARGSUSED2 */
12490Sstevel@tonic-gate static int
12500Sstevel@tonic-gate auto_rwlock(vnode_t *vp, int write_lock, caller_context_t *ct)
12510Sstevel@tonic-gate {
12520Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
12530Sstevel@tonic-gate 	if (write_lock)
12540Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_WRITER);
12550Sstevel@tonic-gate 	else
12560Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_READER);
12570Sstevel@tonic-gate 	return (write_lock);
12580Sstevel@tonic-gate }
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate /* ARGSUSED */
12610Sstevel@tonic-gate static void
12620Sstevel@tonic-gate auto_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ct)
12630Sstevel@tonic-gate {
12640Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
12650Sstevel@tonic-gate 	rw_exit(&fnp->fn_rwlock);
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate /* ARGSUSED */
12700Sstevel@tonic-gate static int
12710Sstevel@tonic-gate auto_seek(struct vnode *vp, offset_t ooff, offset_t *noffp)
12720Sstevel@tonic-gate {
12730Sstevel@tonic-gate 	/*
12740Sstevel@tonic-gate 	 * Return 0 unconditionally, since we expect
12750Sstevel@tonic-gate 	 * a VDIR all the time
12760Sstevel@tonic-gate 	 */
12770Sstevel@tonic-gate 	return (0);
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate /*
12810Sstevel@tonic-gate  * Triggers the mount if needed. If the mount has been triggered by
12820Sstevel@tonic-gate  * another thread, it will wait for its return status, and return it.
12830Sstevel@tonic-gate  * Whether the mount is triggered by this thread, another thread, or
12840Sstevel@tonic-gate  * if the vnode was already covered, '*newvp' is a
12850Sstevel@tonic-gate  * VN_HELD vnode pointing to the root of the filesystem covering 'vp'.
12860Sstevel@tonic-gate  * If the node is not mounted on, and should not be mounted on, '*newvp'
12870Sstevel@tonic-gate  * will be NULL.
12880Sstevel@tonic-gate  * The calling routine may use '*newvp' to do the filesystem jump.
12890Sstevel@tonic-gate  */
12900Sstevel@tonic-gate static int
12910Sstevel@tonic-gate auto_trigger_mount(vnode_t *vp, cred_t *cred, vnode_t **newvp)
12920Sstevel@tonic-gate {
12930Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
12940Sstevel@tonic-gate 	fninfo_t *fnip = vfstofni(vp->v_vfsp);
12950Sstevel@tonic-gate 	vnode_t *dvp;
12960Sstevel@tonic-gate 	vfs_t *vfsp;
12970Sstevel@tonic-gate 	int delayed_ind;
12980Sstevel@tonic-gate 	char name[AUTOFS_MAXPATHLEN];
12990Sstevel@tonic-gate 	int error;
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_trigger_mount: vp=%p\n", (void *)vp));
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	*newvp = NULL;
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	/*
13060Sstevel@tonic-gate 	 * Cross-zone mount triggering is disallowed.
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	if (fnip->fi_zoneid != getzoneid())
13090Sstevel@tonic-gate 		return (EPERM);	/* Not owner of mount */
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate retry:
13120Sstevel@tonic-gate 	error = 0;
13130Sstevel@tonic-gate 	delayed_ind = 0;
13140Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
13150Sstevel@tonic-gate 	while (fnp->fn_flags & (MF_LOOKUP | MF_INPROG)) {
13160Sstevel@tonic-gate 		/*
13170Sstevel@tonic-gate 		 * Mount or lookup in progress,
13180Sstevel@tonic-gate 		 * wait for it before proceeding.
13190Sstevel@tonic-gate 		 */
13200Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
13210Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
13220Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN) {
13230Sstevel@tonic-gate 			error = 0;
13240Sstevel@tonic-gate 			goto done;
13250Sstevel@tonic-gate 		}
13260Sstevel@tonic-gate 		if (error && error != EAGAIN)
13270Sstevel@tonic-gate 			goto done;
13280Sstevel@tonic-gate 		error = 0;
13290Sstevel@tonic-gate 		mutex_enter(&fnp->fn_lock);
13300Sstevel@tonic-gate 	}
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	/*
13330Sstevel@tonic-gate 	 * If the vfslock can't be acquired for the first time.
13340Sstevel@tonic-gate 	 * drop the fn_lock and retry next time in blocking mode.
13350Sstevel@tonic-gate 	 */
13360Sstevel@tonic-gate 	if (vn_vfswlock(vp)) {
13370Sstevel@tonic-gate 		/*
13380Sstevel@tonic-gate 		 * Lock held by another thread.
13390Sstevel@tonic-gate 		 * Perform blocking by dropping the
13400Sstevel@tonic-gate 		 * fn_lock.
13410Sstevel@tonic-gate 		 */
13420Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
13430Sstevel@tonic-gate 		error = vn_vfswlock_wait(vp);
13440Sstevel@tonic-gate 		if (error)
13450Sstevel@tonic-gate 			goto done;
13460Sstevel@tonic-gate 		/*
13470Sstevel@tonic-gate 		 * Because fn_lock wasn't held, the state
13480Sstevel@tonic-gate 		 * of the trigger node might have changed.
13490Sstevel@tonic-gate 		 * Need to run through the checks on trigger
13500Sstevel@tonic-gate 		 * node again.
13510Sstevel@tonic-gate 		 */
13520Sstevel@tonic-gate 		vn_vfsunlock(vp);
13530Sstevel@tonic-gate 		goto retry;
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	vfsp = vn_mountedvfs(vp);
13570Sstevel@tonic-gate 	if (vfsp != NULL) {
13580Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
13591153Snr123932 		error = VFS_ROOT(vfsp, newvp);
13600Sstevel@tonic-gate 		vn_vfsunlock(vp);
13610Sstevel@tonic-gate 		goto done;
13620Sstevel@tonic-gate 	} else {
13630Sstevel@tonic-gate 		vn_vfsunlock(vp);
13640Sstevel@tonic-gate 		if ((fnp->fn_flags & MF_MOUNTPOINT) &&
13650Sstevel@tonic-gate 		    fnp->fn_trigger != NULL) {
13660Sstevel@tonic-gate 			ASSERT(fnp->fn_dirents == NULL);
13670Sstevel@tonic-gate 			/*
13680Sstevel@tonic-gate 			 * The filesystem that used to sit here has been
13690Sstevel@tonic-gate 			 * forcibly unmounted.
13700Sstevel@tonic-gate 			 */
13710Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
13720Sstevel@tonic-gate 			error = EIO;
13730Sstevel@tonic-gate 			goto done;
13740Sstevel@tonic-gate 		}
13750Sstevel@tonic-gate 	}
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
13780Sstevel@tonic-gate 	dvp = fntovn(fnp->fn_parent);
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	if ((fnp->fn_dirents == NULL) &&
13810Sstevel@tonic-gate 	    ((fnip->fi_flags & MF_DIRECT) == 0) &&
13820Sstevel@tonic-gate 	    ((vp->v_flag & VROOT) == 0) &&
13830Sstevel@tonic-gate 	    (dvp->v_flag & VROOT)) {
13840Sstevel@tonic-gate 		/*
13850Sstevel@tonic-gate 		 * If the parent of this node is the root of an indirect
13860Sstevel@tonic-gate 		 * AUTOFS filesystem, this node is remountable.
13870Sstevel@tonic-gate 		 */
13880Sstevel@tonic-gate 		delayed_ind = 1;
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	if (delayed_ind ||
13920Sstevel@tonic-gate 	    ((fnip->fi_flags & MF_DIRECT) && (fnp->fn_dirents == NULL))) {
13930Sstevel@tonic-gate 		/*
13940Sstevel@tonic-gate 		 * Trigger mount since:
13950Sstevel@tonic-gate 		 * direct mountpoint with no subdirs or
13960Sstevel@tonic-gate 		 * delayed indirect.
13970Sstevel@tonic-gate 		 */
13980Sstevel@tonic-gate 		AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
13990Sstevel@tonic-gate 		fnp->fn_error = 0;
14000Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
14010Sstevel@tonic-gate 		if (delayed_ind)
14020Sstevel@tonic-gate 			(void) strcpy(name, fnp->fn_name);
14030Sstevel@tonic-gate 		else
14040Sstevel@tonic-gate 			(void) strcpy(name, ".");
14050Sstevel@tonic-gate 		fnp->fn_ref_time = gethrestime_sec();
14060Sstevel@tonic-gate 		auto_new_mount_thread(fnp, name, cred);
14070Sstevel@tonic-gate 		/*
14080Sstevel@tonic-gate 		 * At this point we're simply another thread waiting
14090Sstevel@tonic-gate 		 * for the mount to finish.
14100Sstevel@tonic-gate 		 */
14110Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
14120Sstevel@tonic-gate 		if (error == EAGAIN)
14130Sstevel@tonic-gate 			goto retry;
14140Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN) {
14150Sstevel@tonic-gate 			error = 0;
14160Sstevel@tonic-gate 			goto done;
14170Sstevel@tonic-gate 		}
14180Sstevel@tonic-gate 		if (error == 0) {
14191153Snr123932 			if (error = vn_vfsrlock_wait(vp))
14200Sstevel@tonic-gate 				goto done;
14210Sstevel@tonic-gate 			/* Reacquire after dropping locks */
14220Sstevel@tonic-gate 			vfsp = vn_mountedvfs(vp);
14230Sstevel@tonic-gate 			if (vfsp != NULL) {
14241153Snr123932 				error = VFS_ROOT(vfsp, newvp);
14250Sstevel@tonic-gate 				vn_vfsunlock(vp);
14260Sstevel@tonic-gate 			} else {
14270Sstevel@tonic-gate 				vn_vfsunlock(vp);
14280Sstevel@tonic-gate 				goto retry;
14290Sstevel@tonic-gate 			}
14300Sstevel@tonic-gate 		}
14310Sstevel@tonic-gate 	} else
14320Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate done:
14350Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_trigger_mount: error=%d\n", error));
14360Sstevel@tonic-gate 	return (error);
14370Sstevel@tonic-gate }
1438