xref: /onnv-gate/usr/src/uts/common/fs/autofs/auto_vnops.c (revision 5302:eec6aeacde6e)
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 },
1084863Spraks 	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 
161*5302Sth199096 	if (flags & ATTR_TRIGGER) {
162*5302Sth199096 		/*
163*5302Sth199096 		 * Pre-trigger the mount
164*5302Sth199096 		 */
165*5302Sth199096 		error = auto_trigger_mount(vp, cred, &newvp);
166*5302Sth199096 		if (error)
167*5302Sth199096 			return (error);
168*5302Sth199096 
169*5302Sth199096 		if (newvp == NULL)
170*5302Sth199096 			goto defattr;
171*5302Sth199096 
172*5302Sth199096 		if (error = vn_vfsrlock_wait(vp))
173*5302Sth199096 			return (error);
174149Scasper 
175*5302Sth199096 		vfsp = newvp->v_vfsp;
176*5302Sth199096 	} else {
177*5302Sth199096 		/*
178*5302Sth199096 		 * Recursive auto_getattr/mount; go to the vfsp == NULL
179*5302Sth199096 		 * case.
180*5302Sth199096 		 */
181*5302Sth199096 		if (vn_vfswlock_held(vp))
182*5302Sth199096 			goto defattr;
1830Sstevel@tonic-gate 
184*5302Sth199096 		if (error = vn_vfsrlock_wait(vp))
185*5302Sth199096 			return (error);
186*5302Sth199096 
187*5302Sth199096 		vfsp = vn_mountedvfs(vp);
188*5302Sth199096 	}
189*5302Sth199096 
1900Sstevel@tonic-gate 	if (vfsp != NULL) {
1910Sstevel@tonic-gate 		/*
1920Sstevel@tonic-gate 		 * Node is mounted on.
1930Sstevel@tonic-gate 		 */
1941153Snr123932 		error = VFS_ROOT(vfsp, &newvp);
1950Sstevel@tonic-gate 		vn_vfsunlock(vp);
1960Sstevel@tonic-gate 		if (error)
1970Sstevel@tonic-gate 			return (error);
1980Sstevel@tonic-gate 		mutex_enter(&fnp->fn_lock);
1990Sstevel@tonic-gate 		if (fnp->fn_seen == newvp && fnp->fn_thread == curthread) {
2000Sstevel@tonic-gate 			/*
2010Sstevel@tonic-gate 			 * Recursive auto_getattr(); just release newvp and drop
2020Sstevel@tonic-gate 			 * into the vfsp == NULL case.
2030Sstevel@tonic-gate 			 */
2040Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
2050Sstevel@tonic-gate 			VN_RELE(newvp);
2060Sstevel@tonic-gate 		} else {
2070Sstevel@tonic-gate 			while (fnp->fn_thread && fnp->fn_thread != curthread) {
2080Sstevel@tonic-gate 				fnp->fn_flags |= MF_ATTR_WAIT;
2090Sstevel@tonic-gate 				cv_wait(&fnp->fn_cv_mount, &fnp->fn_lock);
2100Sstevel@tonic-gate 			}
2110Sstevel@tonic-gate 			fnp->fn_thread = curthread;
2120Sstevel@tonic-gate 			fnp->fn_seen = newvp;
2130Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
2140Sstevel@tonic-gate 			error = VOP_GETATTR(newvp, vap, flags, cred);
2150Sstevel@tonic-gate 			VN_RELE(newvp);
2160Sstevel@tonic-gate 			mutex_enter(&fnp->fn_lock);
2170Sstevel@tonic-gate 			fnp->fn_seen = 0;
2180Sstevel@tonic-gate 			fnp->fn_thread = 0;
2190Sstevel@tonic-gate 			if (fnp->fn_flags & MF_ATTR_WAIT) {
2200Sstevel@tonic-gate 				fnp->fn_flags &= ~MF_ATTR_WAIT;
2210Sstevel@tonic-gate 				cv_broadcast(&fnp->fn_cv_mount);
2220Sstevel@tonic-gate 			}
2230Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
2240Sstevel@tonic-gate 			return (error);
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 	} else {
2270Sstevel@tonic-gate 		vn_vfsunlock(vp);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
230149Scasper defattr:
2310Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
2320Sstevel@tonic-gate 	vap->va_uid	= 0;
2330Sstevel@tonic-gate 	vap->va_gid	= 0;
2340Sstevel@tonic-gate 	vap->va_nlink	= fnp->fn_linkcnt;
2350Sstevel@tonic-gate 	vap->va_nodeid	= (u_longlong_t)fnp->fn_nodeid;
2360Sstevel@tonic-gate 	vap->va_size	= fnp->fn_size;
2370Sstevel@tonic-gate 	vap->va_atime	= fnp->fn_atime;
2380Sstevel@tonic-gate 	vap->va_mtime	= fnp->fn_mtime;
2390Sstevel@tonic-gate 	vap->va_ctime	= fnp->fn_ctime;
2400Sstevel@tonic-gate 	vap->va_type	= vp->v_type;
2410Sstevel@tonic-gate 	vap->va_mode	= fnp->fn_mode;
2420Sstevel@tonic-gate 	vap->va_fsid	= vp->v_vfsp->vfs_dev;
2430Sstevel@tonic-gate 	vap->va_rdev	= 0;
2440Sstevel@tonic-gate 	vap->va_blksize	= MAXBSIZE;
2450Sstevel@tonic-gate 	vap->va_nblocks	= (fsblkcnt64_t)btod(vap->va_size);
2460Sstevel@tonic-gate 	vap->va_seq	= 0;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	return (0);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*ARGSUSED4*/
2520Sstevel@tonic-gate static int
2530Sstevel@tonic-gate auto_setattr(
2540Sstevel@tonic-gate 	vnode_t *vp,
2550Sstevel@tonic-gate 	struct vattr *vap,
2560Sstevel@tonic-gate 	int flags,
2570Sstevel@tonic-gate 	cred_t *cred,
2580Sstevel@tonic-gate 	caller_context_t *ct)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	vnode_t *newvp;
2610Sstevel@tonic-gate 	int error;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_setattr vp %p\n", (void *)vp));
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	if (error = auto_trigger_mount(vp, cred, &newvp))
2660Sstevel@tonic-gate 		goto done;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (newvp != NULL) {
2690Sstevel@tonic-gate 		/*
2700Sstevel@tonic-gate 		 * Node is mounted on.
2710Sstevel@tonic-gate 		 */
2720Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
2730Sstevel@tonic-gate 			error = EROFS;
2740Sstevel@tonic-gate 		else
2750Sstevel@tonic-gate 			error = VOP_SETATTR(newvp, vap, flags, cred, NULL);
2760Sstevel@tonic-gate 		VN_RELE(newvp);
2770Sstevel@tonic-gate 	} else
2780Sstevel@tonic-gate 		error = ENOSYS;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate done:
2810Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_setattr: error=%d\n", error));
2820Sstevel@tonic-gate 	return (error);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /* ARGSUSED */
2860Sstevel@tonic-gate static int
2870Sstevel@tonic-gate auto_access(vnode_t *vp, int mode, int flags, cred_t *cred)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
2900Sstevel@tonic-gate 	vnode_t *newvp;
2910Sstevel@tonic-gate 	int error;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_access: vp=%p\n", (void *)vp));
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (error = auto_trigger_mount(vp, cred, &newvp))
2960Sstevel@tonic-gate 		goto done;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	if (newvp != NULL) {
2990Sstevel@tonic-gate 		/*
3000Sstevel@tonic-gate 		 * Node is mounted on.
3010Sstevel@tonic-gate 		 */
3020Sstevel@tonic-gate 		error = VOP_ACCESS(newvp, mode, 0, cred);
3030Sstevel@tonic-gate 		VN_RELE(newvp);
3040Sstevel@tonic-gate 	} else {
3050Sstevel@tonic-gate 		int shift = 0;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		/*
3080Sstevel@tonic-gate 		 * really interested in the autofs node, check the
3090Sstevel@tonic-gate 		 * access on it
3100Sstevel@tonic-gate 		 */
3110Sstevel@tonic-gate 		ASSERT(error == 0);
3120Sstevel@tonic-gate 		if (crgetuid(cred) != fnp->fn_uid) {
3130Sstevel@tonic-gate 			shift += 3;
3140Sstevel@tonic-gate 			if (groupmember(fnp->fn_gid, cred) == 0)
3150Sstevel@tonic-gate 				shift += 3;
3160Sstevel@tonic-gate 		}
3170Sstevel@tonic-gate 		mode &= ~(fnp->fn_mode << shift);
3180Sstevel@tonic-gate 		if (mode != 0)
3190Sstevel@tonic-gate 			error = secpolicy_vnode_access(cred, vp, fnp->fn_uid,
320*5302Sth199096 			    mode);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate done:
3240Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_access: error=%d\n", error));
3250Sstevel@tonic-gate 	return (error);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate static int
3290Sstevel@tonic-gate auto_lookup(
3300Sstevel@tonic-gate 	vnode_t *dvp,
3310Sstevel@tonic-gate 	char *nm,
3320Sstevel@tonic-gate 	vnode_t **vpp,
3330Sstevel@tonic-gate 	pathname_t *pnp,
3340Sstevel@tonic-gate 	int flags,
3350Sstevel@tonic-gate 	vnode_t *rdir,
3360Sstevel@tonic-gate 	cred_t *cred)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	int error = 0;
3390Sstevel@tonic-gate 	vnode_t *newvp = NULL;
3400Sstevel@tonic-gate 	vfs_t *vfsp;
3410Sstevel@tonic-gate 	fninfo_t *dfnip;
3420Sstevel@tonic-gate 	fnnode_t *dfnp = NULL;
3430Sstevel@tonic-gate 	fnnode_t *fnp = NULL;
3440Sstevel@tonic-gate 	char *searchnm;
3450Sstevel@tonic-gate 	int operation;		/* either AUTOFS_LOOKUP or AUTOFS_MOUNT */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	dfnip = vfstofni(dvp->v_vfsp);
3480Sstevel@tonic-gate 	AUTOFS_DPRINT((3, "auto_lookup: dvp=%p (%s) name=%s\n",
3490Sstevel@tonic-gate 	    (void *)dvp, dfnip->fi_map, nm));
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if (nm[0] == 0) {
3520Sstevel@tonic-gate 		VN_HOLD(dvp);
3530Sstevel@tonic-gate 		*vpp = dvp;
3540Sstevel@tonic-gate 		return (0);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	if (error = VOP_ACCESS(dvp, VEXEC, 0, cred))
3580Sstevel@tonic-gate 		return (error);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	if (nm[0] == '.' && nm[1] == 0) {
3610Sstevel@tonic-gate 		VN_HOLD(dvp);
3620Sstevel@tonic-gate 		*vpp = dvp;
3630Sstevel@tonic-gate 		return (0);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if (nm[0] == '.' && nm[1] == '.' && nm[2] == 0) {
3670Sstevel@tonic-gate 		fnnode_t *pdfnp;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 		pdfnp = (vntofn(dvp))->fn_parent;
3700Sstevel@tonic-gate 		ASSERT(pdfnp != NULL);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		/*
3730Sstevel@tonic-gate 		 * Since it is legitimate to have the VROOT flag set for the
3740Sstevel@tonic-gate 		 * subdirectories of the indirect map in autofs filesystem,
3750Sstevel@tonic-gate 		 * rootfnnodep is checked against fnnode of dvp instead of
3760Sstevel@tonic-gate 		 * just checking whether VROOT flag is set in dvp
3770Sstevel@tonic-gate 		 */
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 		if (pdfnp == pdfnp->fn_globals->fng_rootfnnodep) {
3800Sstevel@tonic-gate 			vnode_t *vp;
3810Sstevel@tonic-gate 
3821153Snr123932 			vfs_rlock_wait(dvp->v_vfsp);
3830Sstevel@tonic-gate 			if (dvp->v_vfsp->vfs_flag & VFS_UNMOUNTED) {
3840Sstevel@tonic-gate 				vfs_unlock(dvp->v_vfsp);
3850Sstevel@tonic-gate 				return (EIO);
3860Sstevel@tonic-gate 			}
3870Sstevel@tonic-gate 			vp = dvp->v_vfsp->vfs_vnodecovered;
3880Sstevel@tonic-gate 			VN_HOLD(vp);
3890Sstevel@tonic-gate 			vfs_unlock(dvp->v_vfsp);
3900Sstevel@tonic-gate 			error = VOP_LOOKUP(vp, nm, vpp, pnp, flags, rdir, cred);
3910Sstevel@tonic-gate 			VN_RELE(vp);
3920Sstevel@tonic-gate 			return (error);
3930Sstevel@tonic-gate 		} else {
3940Sstevel@tonic-gate 			*vpp = fntovn(pdfnp);
3950Sstevel@tonic-gate 			VN_HOLD(*vpp);
3960Sstevel@tonic-gate 			return (0);
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate top:
4010Sstevel@tonic-gate 	dfnp = vntofn(dvp);
4020Sstevel@tonic-gate 	searchnm = nm;
4030Sstevel@tonic-gate 	operation = 0;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	ASSERT(vn_matchops(dvp, auto_vnodeops));
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	AUTOFS_DPRINT((3, "auto_lookup: dvp=%p dfnp=%p\n", (void *)dvp,
4080Sstevel@tonic-gate 	    (void *)dfnp));
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/*
4110Sstevel@tonic-gate 	 * If a lookup or mount of this node is in progress, wait for it
4120Sstevel@tonic-gate 	 * to finish, and return whatever result it got.
4130Sstevel@tonic-gate 	 */
4140Sstevel@tonic-gate 	mutex_enter(&dfnp->fn_lock);
4150Sstevel@tonic-gate 	if (dfnp->fn_flags & (MF_LOOKUP | MF_INPROG)) {
4160Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
4170Sstevel@tonic-gate 		error = auto_wait4mount(dfnp);
4180Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN)
4190Sstevel@tonic-gate 			error = ENOENT;
4200Sstevel@tonic-gate 		if (error == EAGAIN)
4210Sstevel@tonic-gate 			goto top;
4220Sstevel@tonic-gate 		if (error)
4230Sstevel@tonic-gate 			return (error);
4240Sstevel@tonic-gate 	} else
4250Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 
4281153Snr123932 	error = vn_vfsrlock_wait(dvp);
4290Sstevel@tonic-gate 	if (error)
4300Sstevel@tonic-gate 		return (error);
4310Sstevel@tonic-gate 	vfsp = vn_mountedvfs(dvp);
4320Sstevel@tonic-gate 	if (vfsp != NULL) {
4331153Snr123932 		error = VFS_ROOT(vfsp, &newvp);
4340Sstevel@tonic-gate 		vn_vfsunlock(dvp);
4350Sstevel@tonic-gate 		if (!error) {
4360Sstevel@tonic-gate 			error = VOP_LOOKUP(newvp, nm, vpp, pnp,
4370Sstevel@tonic-gate 			    flags, rdir, cred);
4380Sstevel@tonic-gate 			VN_RELE(newvp);
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 		return (error);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 	vn_vfsunlock(dvp);
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	rw_enter(&dfnp->fn_rwlock, RW_READER);
4450Sstevel@tonic-gate 	error = auto_search(dfnp, nm, &fnp, cred);
4460Sstevel@tonic-gate 	if (error) {
4470Sstevel@tonic-gate 		if (dfnip->fi_flags & MF_DIRECT) {
4480Sstevel@tonic-gate 			/*
4490Sstevel@tonic-gate 			 * direct map.
4500Sstevel@tonic-gate 			 */
4510Sstevel@tonic-gate 			if (dfnp->fn_dirents) {
4520Sstevel@tonic-gate 				/*
4530Sstevel@tonic-gate 				 * Mount previously triggered.
4540Sstevel@tonic-gate 				 * 'nm' not found
4550Sstevel@tonic-gate 				 */
4560Sstevel@tonic-gate 				error = ENOENT;
4570Sstevel@tonic-gate 			} else {
4580Sstevel@tonic-gate 				/*
4590Sstevel@tonic-gate 				 * I need to contact the daemon to trigger
4600Sstevel@tonic-gate 				 * the mount. 'dfnp' will be the mountpoint.
4610Sstevel@tonic-gate 				 */
4620Sstevel@tonic-gate 				operation = AUTOFS_MOUNT;
4630Sstevel@tonic-gate 				VN_HOLD(fntovn(dfnp));
4640Sstevel@tonic-gate 				fnp = dfnp;
4650Sstevel@tonic-gate 				error = 0;
4660Sstevel@tonic-gate 			}
4670Sstevel@tonic-gate 		} else if (dvp == dfnip->fi_rootvp) {
4680Sstevel@tonic-gate 			/*
4690Sstevel@tonic-gate 			 * 'dfnp' is the root of the indirect AUTOFS.
4700Sstevel@tonic-gate 			 */
4710Sstevel@tonic-gate 			if (rw_tryupgrade(&dfnp->fn_rwlock) == 0) {
4720Sstevel@tonic-gate 				/*
4730Sstevel@tonic-gate 				 * Could not acquire writer lock, release
4740Sstevel@tonic-gate 				 * reader, and wait until available. We
4750Sstevel@tonic-gate 				 * need to search for 'nm' again, since we
4760Sstevel@tonic-gate 				 * had to release the lock before reacquiring
4770Sstevel@tonic-gate 				 * it.
4780Sstevel@tonic-gate 				 */
4790Sstevel@tonic-gate 				rw_exit(&dfnp->fn_rwlock);
4800Sstevel@tonic-gate 				rw_enter(&dfnp->fn_rwlock, RW_WRITER);
4810Sstevel@tonic-gate 				error = auto_search(dfnp, nm, &fnp, cred);
4820Sstevel@tonic-gate 			}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 			ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
4850Sstevel@tonic-gate 			if (error) {
4860Sstevel@tonic-gate 				/*
4870Sstevel@tonic-gate 				 * create node being looked-up and request
4880Sstevel@tonic-gate 				 * mount on it.
4890Sstevel@tonic-gate 				 */
4900Sstevel@tonic-gate 				error = auto_enter(dfnp, nm, &fnp, kcred);
4910Sstevel@tonic-gate 				if (!error)
4920Sstevel@tonic-gate 					operation = AUTOFS_LOOKUP;
4930Sstevel@tonic-gate 			}
4940Sstevel@tonic-gate 		} else if ((dfnp->fn_dirents == NULL) &&
4950Sstevel@tonic-gate 		    ((dvp->v_flag & VROOT) == 0) &&
4960Sstevel@tonic-gate 		    ((fntovn(dfnp->fn_parent))->v_flag & VROOT)) {
4970Sstevel@tonic-gate 			/*
4980Sstevel@tonic-gate 			 * dfnp is the actual 'mountpoint' of indirect map,
4990Sstevel@tonic-gate 			 * it is the equivalent of a direct mount,
5000Sstevel@tonic-gate 			 * ie, /home/'user1'
5010Sstevel@tonic-gate 			 */
5020Sstevel@tonic-gate 			operation = AUTOFS_MOUNT;
5030Sstevel@tonic-gate 			VN_HOLD(fntovn(dfnp));
5040Sstevel@tonic-gate 			fnp = dfnp;
5050Sstevel@tonic-gate 			error = 0;
5060Sstevel@tonic-gate 			searchnm = dfnp->fn_name;
5070Sstevel@tonic-gate 		}
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	if (error == EAGAIN) {
5110Sstevel@tonic-gate 		rw_exit(&dfnp->fn_rwlock);
5120Sstevel@tonic-gate 		goto top;
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate 	if (error) {
5150Sstevel@tonic-gate 		rw_exit(&dfnp->fn_rwlock);
5160Sstevel@tonic-gate 		return (error);
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	/*
5200Sstevel@tonic-gate 	 * We now have the actual fnnode we're interested in.
5210Sstevel@tonic-gate 	 * The 'MF_LOOKUP' indicates another thread is currently
5220Sstevel@tonic-gate 	 * performing a daemon lookup of this node, therefore we
5230Sstevel@tonic-gate 	 * wait for its completion.
5240Sstevel@tonic-gate 	 * The 'MF_INPROG' indicates another thread is currently
5250Sstevel@tonic-gate 	 * performing a daemon mount of this node, we wait for it
5260Sstevel@tonic-gate 	 * to be done if we are performing a MOUNT. We don't
5270Sstevel@tonic-gate 	 * wait for it if we are performing a LOOKUP.
5280Sstevel@tonic-gate 	 * We can release the reader/writer lock as soon as we acquire
5290Sstevel@tonic-gate 	 * the mutex, since the state of the lock can only change by
5300Sstevel@tonic-gate 	 * first acquiring the mutex.
5310Sstevel@tonic-gate 	 */
5320Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
5330Sstevel@tonic-gate 	rw_exit(&dfnp->fn_rwlock);
5340Sstevel@tonic-gate 	if ((fnp->fn_flags & MF_LOOKUP) ||
5350Sstevel@tonic-gate 	    ((operation == AUTOFS_MOUNT) && (fnp->fn_flags & MF_INPROG))) {
5360Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
5370Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
5380Sstevel@tonic-gate 		VN_RELE(fntovn(fnp));
5390Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN)
5400Sstevel@tonic-gate 			error = ENOENT;
5410Sstevel@tonic-gate 		if (error && error != EAGAIN)
5420Sstevel@tonic-gate 			return (error);
5430Sstevel@tonic-gate 		goto top;
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	if (operation == 0) {
5470Sstevel@tonic-gate 		/*
5480Sstevel@tonic-gate 		 * got the fnnode, check for any errors
5490Sstevel@tonic-gate 		 * on the previous operation on that node.
5500Sstevel@tonic-gate 		 */
5510Sstevel@tonic-gate 		error = fnp->fn_error;
5520Sstevel@tonic-gate 		if ((error == EINTR) || (error == EAGAIN)) {
5530Sstevel@tonic-gate 			/*
5540Sstevel@tonic-gate 			 * previous operation on this node was
5550Sstevel@tonic-gate 			 * not completed, do a lookup now.
5560Sstevel@tonic-gate 			 */
5570Sstevel@tonic-gate 			operation = AUTOFS_LOOKUP;
5580Sstevel@tonic-gate 		} else {
5590Sstevel@tonic-gate 			/*
5600Sstevel@tonic-gate 			 * previous operation completed. Return
5610Sstevel@tonic-gate 			 * a pointer to the node only if there was
5620Sstevel@tonic-gate 			 * no error.
5630Sstevel@tonic-gate 			 */
5640Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
5650Sstevel@tonic-gate 			if (!error)
5660Sstevel@tonic-gate 				*vpp = fntovn(fnp);
5670Sstevel@tonic-gate 			else
5680Sstevel@tonic-gate 				VN_RELE(fntovn(fnp));
5690Sstevel@tonic-gate 			return (error);
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	/*
5740Sstevel@tonic-gate 	 * Since I got to this point, it means I'm the one
5750Sstevel@tonic-gate 	 * responsible for triggering the mount/look-up of this node.
5760Sstevel@tonic-gate 	 */
5770Sstevel@tonic-gate 	switch (operation) {
5780Sstevel@tonic-gate 	case AUTOFS_LOOKUP:
5790Sstevel@tonic-gate 		AUTOFS_BLOCK_OTHERS(fnp, MF_LOOKUP);
5800Sstevel@tonic-gate 		fnp->fn_error = 0;
5810Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
5820Sstevel@tonic-gate 		error = auto_lookup_aux(fnp, searchnm, cred);
5830Sstevel@tonic-gate 		if (!error) {
5840Sstevel@tonic-gate 			/*
5850Sstevel@tonic-gate 			 * Return this vnode
5860Sstevel@tonic-gate 			 */
5870Sstevel@tonic-gate 			*vpp = fntovn(fnp);
5880Sstevel@tonic-gate 		} else {
5890Sstevel@tonic-gate 			/*
5900Sstevel@tonic-gate 			 * release our reference to this vnode
5910Sstevel@tonic-gate 			 * and return error
5920Sstevel@tonic-gate 			 */
5930Sstevel@tonic-gate 			VN_RELE(fntovn(fnp));
5940Sstevel@tonic-gate 		}
5950Sstevel@tonic-gate 		break;
5960Sstevel@tonic-gate 	case AUTOFS_MOUNT:
5970Sstevel@tonic-gate 		AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
5980Sstevel@tonic-gate 		fnp->fn_error = 0;
5990Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
6000Sstevel@tonic-gate 		/*
6010Sstevel@tonic-gate 		 * auto_new_mount_thread fires up a new thread which
6020Sstevel@tonic-gate 		 * calls automountd finishing up the work
6030Sstevel@tonic-gate 		 */
6040Sstevel@tonic-gate 		auto_new_mount_thread(fnp, searchnm, cred);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 		/*
6070Sstevel@tonic-gate 		 * At this point, we are simply another thread
6080Sstevel@tonic-gate 		 * waiting for the mount to complete
6090Sstevel@tonic-gate 		 */
6100Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
6110Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN)
6120Sstevel@tonic-gate 			error = ENOENT;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 		/*
6150Sstevel@tonic-gate 		 * now release our reference to this vnode
6160Sstevel@tonic-gate 		 */
6170Sstevel@tonic-gate 		VN_RELE(fntovn(fnp));
6180Sstevel@tonic-gate 		if (!error)
6190Sstevel@tonic-gate 			goto top;
6200Sstevel@tonic-gate 		break;
6210Sstevel@tonic-gate 	default:
6222170Sevanl 		auto_log(dfnp->fn_globals->fng_verbose,
623*5302Sth199096 		    dfnp->fn_globals->fng_zoneid, CE_WARN,
624*5302Sth199096 		    "auto_lookup: unknown operation %d",
625*5302Sth199096 		    operation);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_lookup: name=%s *vpp=%p return=%d\n",
6290Sstevel@tonic-gate 	    nm, (void *)*vpp, error));
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	return (error);
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate static int
6350Sstevel@tonic-gate auto_create(
6360Sstevel@tonic-gate 	vnode_t *dvp,
6370Sstevel@tonic-gate 	char *nm,
6380Sstevel@tonic-gate 	vattr_t *va,
6390Sstevel@tonic-gate 	vcexcl_t excl,
6400Sstevel@tonic-gate 	int mode,
6410Sstevel@tonic-gate 	vnode_t **vpp,
6420Sstevel@tonic-gate 	cred_t *cred,
6430Sstevel@tonic-gate 	int flag)
6440Sstevel@tonic-gate {
6450Sstevel@tonic-gate 	vnode_t *newvp;
6460Sstevel@tonic-gate 	int error;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_create dvp %p nm %s\n", (void *)dvp, nm));
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
6510Sstevel@tonic-gate 		goto done;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	if (newvp != NULL) {
6540Sstevel@tonic-gate 		/*
6550Sstevel@tonic-gate 		 * Node is now mounted on.
6560Sstevel@tonic-gate 		 */
6570Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
6580Sstevel@tonic-gate 			error = EROFS;
6590Sstevel@tonic-gate 		else
6600Sstevel@tonic-gate 			error = VOP_CREATE(newvp, nm, va, excl,
6610Sstevel@tonic-gate 			    mode, vpp, cred, flag);
6620Sstevel@tonic-gate 		VN_RELE(newvp);
6630Sstevel@tonic-gate 	} else
6640Sstevel@tonic-gate 		error = ENOSYS;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate done:
6670Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_create: error=%d\n", error));
6680Sstevel@tonic-gate 	return (error);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate static int
6720Sstevel@tonic-gate auto_remove(vnode_t *dvp, char *nm, cred_t *cred)
6730Sstevel@tonic-gate {
6740Sstevel@tonic-gate 	vnode_t *newvp;
6750Sstevel@tonic-gate 	int error;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_remove dvp %p nm %s\n", (void *)dvp, nm));
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
6800Sstevel@tonic-gate 		goto done;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	if (newvp != NULL) {
6830Sstevel@tonic-gate 		/*
6840Sstevel@tonic-gate 		 * Node is now mounted on.
6850Sstevel@tonic-gate 		 */
6860Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
6870Sstevel@tonic-gate 			error = EROFS;
6880Sstevel@tonic-gate 		else
6890Sstevel@tonic-gate 			error = VOP_REMOVE(newvp, nm, cred);
6900Sstevel@tonic-gate 		VN_RELE(newvp);
6910Sstevel@tonic-gate 	} else
6920Sstevel@tonic-gate 		error = ENOSYS;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate done:
6950Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_remove: error=%d\n", error));
6960Sstevel@tonic-gate 	return (error);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate static int
7000Sstevel@tonic-gate auto_link(vnode_t *tdvp, vnode_t *svp, char *nm, cred_t *cred)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	vnode_t *newvp;
7030Sstevel@tonic-gate 	int error;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_link tdvp %p svp %p nm %s\n", (void *)tdvp,
7060Sstevel@tonic-gate 	    (void *)svp, nm));
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	if (error = auto_trigger_mount(tdvp, cred, &newvp))
7090Sstevel@tonic-gate 		goto done;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	if (newvp == NULL) {
7120Sstevel@tonic-gate 		/*
7130Sstevel@tonic-gate 		 * an autonode can not be a link to another node
7140Sstevel@tonic-gate 		 */
7150Sstevel@tonic-gate 		error = ENOSYS;
7160Sstevel@tonic-gate 		goto done;
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	if (vn_is_readonly(newvp)) {
7200Sstevel@tonic-gate 		error = EROFS;
7210Sstevel@tonic-gate 		VN_RELE(newvp);
7220Sstevel@tonic-gate 		goto done;
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	if (vn_matchops(svp, auto_vnodeops)) {
7260Sstevel@tonic-gate 		/*
7270Sstevel@tonic-gate 		 * source vp can't be an autonode
7280Sstevel@tonic-gate 		 */
7290Sstevel@tonic-gate 		error = ENOSYS;
7300Sstevel@tonic-gate 		VN_RELE(newvp);
7310Sstevel@tonic-gate 		goto done;
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	error = VOP_LINK(newvp, svp, nm, cred);
7350Sstevel@tonic-gate 	VN_RELE(newvp);
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate done:
7380Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_link error=%d\n", error));
7390Sstevel@tonic-gate 	return (error);
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate static int
7430Sstevel@tonic-gate auto_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate 	vnode_t *o_newvp, *n_newvp;
7460Sstevel@tonic-gate 	int error;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_rename odvp %p onm %s to ndvp %p nnm %s\n",
7490Sstevel@tonic-gate 	    (void *)odvp, onm, (void *)ndvp, nnm));
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/*
7520Sstevel@tonic-gate 	 * we know odvp is an autonode, otherwise this function
7530Sstevel@tonic-gate 	 * could not have ever been called.
7540Sstevel@tonic-gate 	 */
7550Sstevel@tonic-gate 	ASSERT(vn_matchops(odvp, auto_vnodeops));
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	if (error = auto_trigger_mount(odvp, cr, &o_newvp))
7580Sstevel@tonic-gate 		goto done;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	if (o_newvp == NULL) {
7610Sstevel@tonic-gate 		/*
7620Sstevel@tonic-gate 		 * can't rename an autonode
7630Sstevel@tonic-gate 		 */
7640Sstevel@tonic-gate 		error = ENOSYS;
7650Sstevel@tonic-gate 		goto done;
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	if (vn_matchops(ndvp, auto_vnodeops)) {
7690Sstevel@tonic-gate 		/*
7700Sstevel@tonic-gate 		 * directory is AUTOFS, need to trigger the
7710Sstevel@tonic-gate 		 * mount of the real filesystem.
7720Sstevel@tonic-gate 		 */
7730Sstevel@tonic-gate 		if (error = auto_trigger_mount(ndvp, cr, &n_newvp)) {
7740Sstevel@tonic-gate 			VN_RELE(o_newvp);
7750Sstevel@tonic-gate 			goto done;
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 		if (n_newvp == NULL) {
7790Sstevel@tonic-gate 			/*
7800Sstevel@tonic-gate 			 * target can't be an autonode
7810Sstevel@tonic-gate 			 */
7820Sstevel@tonic-gate 			error = ENOSYS;
7830Sstevel@tonic-gate 			VN_RELE(o_newvp);
7840Sstevel@tonic-gate 			goto done;
7850Sstevel@tonic-gate 		}
7860Sstevel@tonic-gate 	} else {
7870Sstevel@tonic-gate 		/*
7880Sstevel@tonic-gate 		 * destination directory mount had been
7890Sstevel@tonic-gate 		 * triggered prior to the call to this function.
7900Sstevel@tonic-gate 		 */
7910Sstevel@tonic-gate 		n_newvp = ndvp;
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	ASSERT(!vn_matchops(n_newvp, auto_vnodeops));
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	if (vn_is_readonly(n_newvp)) {
7970Sstevel@tonic-gate 		error = EROFS;
7980Sstevel@tonic-gate 		VN_RELE(o_newvp);
7990Sstevel@tonic-gate 		if (n_newvp != ndvp)
8000Sstevel@tonic-gate 			VN_RELE(n_newvp);
8010Sstevel@tonic-gate 		goto done;
8020Sstevel@tonic-gate 	}
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	error = VOP_RENAME(o_newvp, onm, n_newvp, nnm, cr);
8050Sstevel@tonic-gate 	VN_RELE(o_newvp);
8060Sstevel@tonic-gate 	if (n_newvp != ndvp)
8070Sstevel@tonic-gate 		VN_RELE(n_newvp);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate done:
8100Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_rename error=%d\n", error));
8110Sstevel@tonic-gate 	return (error);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate static int
8150Sstevel@tonic-gate auto_mkdir(vnode_t *dvp, char *nm, vattr_t *va, vnode_t **vpp, cred_t *cred)
8160Sstevel@tonic-gate {
8170Sstevel@tonic-gate 	vnode_t *newvp;
8180Sstevel@tonic-gate 	int error;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_mkdir dvp %p nm %s\n", (void *)dvp, nm));
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
8230Sstevel@tonic-gate 		goto done;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	if (newvp != NULL) {
8260Sstevel@tonic-gate 		/*
8270Sstevel@tonic-gate 		 * Node is now mounted on.
8280Sstevel@tonic-gate 		 */
8290Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
8300Sstevel@tonic-gate 			error = EROFS;
8310Sstevel@tonic-gate 		else
8320Sstevel@tonic-gate 			error = VOP_MKDIR(newvp, nm, va, vpp, cred);
8330Sstevel@tonic-gate 		VN_RELE(newvp);
8340Sstevel@tonic-gate 	} else
8350Sstevel@tonic-gate 		error = ENOSYS;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate done:
8380Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mkdir: error=%d\n", error));
8390Sstevel@tonic-gate 	return (error);
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate static int
8430Sstevel@tonic-gate auto_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cred)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate 	vnode_t *newvp;
8460Sstevel@tonic-gate 	int error;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_rmdir: vp=%p nm=%s\n", (void *)dvp, nm));
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
8510Sstevel@tonic-gate 		goto done;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	if (newvp != NULL) {
8540Sstevel@tonic-gate 		/*
8550Sstevel@tonic-gate 		 * Node is now mounted on.
8560Sstevel@tonic-gate 		 */
8570Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
8580Sstevel@tonic-gate 			error = EROFS;
8590Sstevel@tonic-gate 		else
8600Sstevel@tonic-gate 			error = VOP_RMDIR(newvp, nm, cdir, cred);
8610Sstevel@tonic-gate 		VN_RELE(newvp);
8620Sstevel@tonic-gate 	} else
8630Sstevel@tonic-gate 		error = ENOSYS;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate done:
8660Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_rmdir: error=%d\n", error));
8670Sstevel@tonic-gate 	return (error);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate static int autofs_nobrowse = 0;
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate #ifdef nextdp
8730Sstevel@tonic-gate #undef nextdp
8740Sstevel@tonic-gate #endif
8750Sstevel@tonic-gate #define	nextdp(dp)	((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate static int
8780Sstevel@tonic-gate auto_readdir(vnode_t *vp, uio_t *uiop, cred_t *cred, int *eofp)
8790Sstevel@tonic-gate {
8802170Sevanl 	struct autofs_rddirargs	rda;
8812170Sevanl 	autofs_rddirres rd;
8820Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
8830Sstevel@tonic-gate 	fnnode_t *cfnp, *nfnp;
8840Sstevel@tonic-gate 	dirent64_t *dp;
8850Sstevel@tonic-gate 	ulong_t offset;
8860Sstevel@tonic-gate 	ulong_t outcount = 0, count = 0;
8870Sstevel@tonic-gate 	size_t namelen;
8880Sstevel@tonic-gate 	ulong_t alloc_count;
8892170Sevanl 	void *outbuf = NULL;
8900Sstevel@tonic-gate 	fninfo_t *fnip = vfstofni(vp->v_vfsp);
8910Sstevel@tonic-gate 	struct iovec *iovp;
8920Sstevel@tonic-gate 	int error = 0;
8930Sstevel@tonic-gate 	int reached_max = 0;
8940Sstevel@tonic-gate 	int myeof = 0;
8950Sstevel@tonic-gate 	int this_reclen;
8962170Sevanl 	struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_readdir vp=%p offset=%lld\n",
8990Sstevel@tonic-gate 	    (void *)vp, uiop->uio_loffset));
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	if (eofp != NULL)
9020Sstevel@tonic-gate 		*eofp = 0;
9030Sstevel@tonic-gate 
9042170Sevanl 	if (uiop->uio_iovcnt != 1)
9052170Sevanl 		return (EINVAL);
9062170Sevanl 
9070Sstevel@tonic-gate 	iovp = uiop->uio_iov;
9080Sstevel@tonic-gate 	alloc_count = iovp->iov_len;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	gethrestime(&fnp->fn_atime);
9110Sstevel@tonic-gate 	fnp->fn_ref_time = fnp->fn_atime.tv_sec;
9120Sstevel@tonic-gate 
9132170Sevanl 	dp = outbuf = kmem_zalloc(alloc_count, KM_SLEEP);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	/*
9160Sstevel@tonic-gate 	 * Held when getdents calls VOP_RWLOCK....
9170Sstevel@tonic-gate 	 */
9180Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&fnp->fn_rwlock));
9190Sstevel@tonic-gate 	if (uiop->uio_offset >= AUTOFS_DAEMONCOOKIE) {
9200Sstevel@tonic-gate again:
9210Sstevel@tonic-gate 		/*
9220Sstevel@tonic-gate 		 * Do readdir of daemon contents only
9230Sstevel@tonic-gate 		 * Drop readers lock and reacquire after reply.
9240Sstevel@tonic-gate 		 */
9250Sstevel@tonic-gate 		rw_exit(&fnp->fn_rwlock);
9262170Sevanl 		bzero(&rd, sizeof (struct autofs_rddirres));
9270Sstevel@tonic-gate 		count = 0;
9280Sstevel@tonic-gate 		rda.rda_map = fnip->fi_map;
9290Sstevel@tonic-gate 		rda.rda_offset = (uint_t)uiop->uio_offset;
9300Sstevel@tonic-gate 		rd.rd_rddir.rddir_entries = dp;
9310Sstevel@tonic-gate 		rda.rda_count = rd.rd_rddir.rddir_size = (uint_t)alloc_count;
9323391Ssemery 		rda.uid = crgetuid(cred);
9332170Sevanl 
9342170Sevanl 		error = auto_calldaemon(fngp->fng_zoneid,
935*5302Sth199096 		    AUTOFS_READDIR,
936*5302Sth199096 		    xdr_autofs_rddirargs,
937*5302Sth199096 		    &rda,
938*5302Sth199096 		    xdr_autofs_rddirres,
939*5302Sth199096 		    (void *)&rd,
940*5302Sth199096 		    sizeof (autofs_rddirres),
941*5302Sth199096 		    TRUE);
9422170Sevanl 
9430Sstevel@tonic-gate 		/*
9440Sstevel@tonic-gate 		 * reacquire previously dropped lock
9450Sstevel@tonic-gate 		 */
9460Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_READER);
9470Sstevel@tonic-gate 
9482170Sevanl 		if (!error) {
9490Sstevel@tonic-gate 			error = rd.rd_status;
9502170Sevanl 			dp = rd.rd_rddir.rddir_entries;
9512170Sevanl 		}
9522170Sevanl 
9530Sstevel@tonic-gate 		if (error) {
9540Sstevel@tonic-gate 			if (error == AUTOFS_SHUTDOWN) {
9550Sstevel@tonic-gate 				/*
9560Sstevel@tonic-gate 				 * treat as empty directory
9570Sstevel@tonic-gate 				 */
9580Sstevel@tonic-gate 				error = 0;
9590Sstevel@tonic-gate 				myeof = 1;
9600Sstevel@tonic-gate 				if (eofp)
9610Sstevel@tonic-gate 					*eofp = 1;
9620Sstevel@tonic-gate 			}
9630Sstevel@tonic-gate 			goto done;
9640Sstevel@tonic-gate 		}
9650Sstevel@tonic-gate 		if (rd.rd_rddir.rddir_size) {
9662170Sevanl 			dirent64_t *odp = dp;   /* next in output buffer */
9672170Sevanl 			dirent64_t *cdp = dp;   /* current examined entry */
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 			/*
9700Sstevel@tonic-gate 			 * Check for duplicates here
9710Sstevel@tonic-gate 			 */
9720Sstevel@tonic-gate 			do {
9730Sstevel@tonic-gate 				this_reclen = cdp->d_reclen;
9742170Sevanl 				if (auto_search(fnp, cdp->d_name,
975*5302Sth199096 				    NULL, cred)) {
9760Sstevel@tonic-gate 					/*
9770Sstevel@tonic-gate 					 * entry not found in kernel list,
9780Sstevel@tonic-gate 					 * include it in readdir output.
9790Sstevel@tonic-gate 					 *
9800Sstevel@tonic-gate 					 * If we are skipping entries. then
9810Sstevel@tonic-gate 					 * we need to copy this entry to the
9820Sstevel@tonic-gate 					 * correct position in the buffer
9830Sstevel@tonic-gate 					 * to be copied out.
9840Sstevel@tonic-gate 					 */
9850Sstevel@tonic-gate 					if (cdp != odp)
9860Sstevel@tonic-gate 						bcopy(cdp, odp,
987*5302Sth199096 						    (size_t)this_reclen);
9880Sstevel@tonic-gate 					odp = nextdp(odp);
9890Sstevel@tonic-gate 					outcount += this_reclen;
9900Sstevel@tonic-gate 				} else {
9910Sstevel@tonic-gate 					/*
9920Sstevel@tonic-gate 					 * Entry was found in the kernel
9930Sstevel@tonic-gate 					 * list. If it is the first entry
9940Sstevel@tonic-gate 					 * in this buffer, then just skip it
9950Sstevel@tonic-gate 					 */
9960Sstevel@tonic-gate 					if (odp == dp) {
9970Sstevel@tonic-gate 						dp = nextdp(dp);
9980Sstevel@tonic-gate 						odp = dp;
9990Sstevel@tonic-gate 					}
10000Sstevel@tonic-gate 				}
10010Sstevel@tonic-gate 				count += this_reclen;
10020Sstevel@tonic-gate 				cdp = (struct dirent64 *)
1003*5302Sth199096 				    ((char *)cdp + this_reclen);
10040Sstevel@tonic-gate 			} while (count < rd.rd_rddir.rddir_size);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 			if (outcount)
10070Sstevel@tonic-gate 				error = uiomove(dp, outcount, UIO_READ, uiop);
10080Sstevel@tonic-gate 			uiop->uio_offset = rd.rd_rddir.rddir_offset;
10090Sstevel@tonic-gate 		} else {
10100Sstevel@tonic-gate 			if (rd.rd_rddir.rddir_eof == 0) {
10110Sstevel@tonic-gate 				/*
10120Sstevel@tonic-gate 				 * alloc_count not large enough for one
10130Sstevel@tonic-gate 				 * directory entry
10140Sstevel@tonic-gate 				 */
10150Sstevel@tonic-gate 				error = EINVAL;
10160Sstevel@tonic-gate 			}
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 		if (rd.rd_rddir.rddir_eof && !error) {
10190Sstevel@tonic-gate 			myeof = 1;
10200Sstevel@tonic-gate 			if (eofp)
10210Sstevel@tonic-gate 				*eofp = 1;
10220Sstevel@tonic-gate 		}
10230Sstevel@tonic-gate 		if (!error && !myeof && outcount == 0) {
10240Sstevel@tonic-gate 			/*
10250Sstevel@tonic-gate 			 * call daemon with new cookie, all previous
10260Sstevel@tonic-gate 			 * elements happened to be duplicates
10270Sstevel@tonic-gate 			 */
10280Sstevel@tonic-gate 			dp = outbuf;
10290Sstevel@tonic-gate 			goto again;
10300Sstevel@tonic-gate 		}
10310Sstevel@tonic-gate 		goto done;
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	if (uiop->uio_offset == 0) {
10350Sstevel@tonic-gate 		/*
10360Sstevel@tonic-gate 		 * first time: so fudge the . and ..
10370Sstevel@tonic-gate 		 */
10380Sstevel@tonic-gate 		this_reclen = DIRENT64_RECLEN(1);
10390Sstevel@tonic-gate 		if (alloc_count < this_reclen) {
10400Sstevel@tonic-gate 			error = EINVAL;
10410Sstevel@tonic-gate 			goto done;
10420Sstevel@tonic-gate 		}
10430Sstevel@tonic-gate 		dp->d_ino = (ino64_t)fnp->fn_nodeid;
10440Sstevel@tonic-gate 		dp->d_off = (off64_t)1;
10450Sstevel@tonic-gate 		dp->d_reclen = (ushort_t)this_reclen;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 		(void) strncpy(dp->d_name, ".",
1050*5302Sth199096 		    DIRENT64_NAMELEN(this_reclen));
10510Sstevel@tonic-gate 		outcount += dp->d_reclen;
10520Sstevel@tonic-gate 		dp = nextdp(dp);
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 		this_reclen = DIRENT64_RECLEN(2);
10550Sstevel@tonic-gate 		if (alloc_count < outcount + this_reclen) {
10560Sstevel@tonic-gate 			error = EINVAL;
10570Sstevel@tonic-gate 			goto done;
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 		dp->d_reclen = (ushort_t)this_reclen;
10600Sstevel@tonic-gate 		dp->d_ino = (ino64_t)fnp->fn_parent->fn_nodeid;
10610Sstevel@tonic-gate 		dp->d_off = (off64_t)2;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 		(void) strncpy(dp->d_name, "..",
1066*5302Sth199096 		    DIRENT64_NAMELEN(this_reclen));
10670Sstevel@tonic-gate 		outcount += dp->d_reclen;
10680Sstevel@tonic-gate 		dp = nextdp(dp);
10690Sstevel@tonic-gate 	}
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	offset = 2;
10720Sstevel@tonic-gate 	cfnp = fnp->fn_dirents;
10730Sstevel@tonic-gate 	while (cfnp != NULL) {
10740Sstevel@tonic-gate 		nfnp = cfnp->fn_next;
10750Sstevel@tonic-gate 		offset = cfnp->fn_offset;
10760Sstevel@tonic-gate 		if ((offset >= uiop->uio_offset) &&
1077*5302Sth199096 		    (!(cfnp->fn_flags & MF_LOOKUP))) {
10780Sstevel@tonic-gate 			int reclen;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 			/*
10810Sstevel@tonic-gate 			 * include node only if its offset is greater or
10820Sstevel@tonic-gate 			 * equal to the one required and it is not in
10830Sstevel@tonic-gate 			 * transient state (not being looked-up)
10840Sstevel@tonic-gate 			 */
10850Sstevel@tonic-gate 			namelen = strlen(cfnp->fn_name);
10860Sstevel@tonic-gate 			reclen = (int)DIRENT64_RECLEN(namelen);
10870Sstevel@tonic-gate 			if (outcount + reclen > alloc_count) {
10880Sstevel@tonic-gate 				reached_max = 1;
10890Sstevel@tonic-gate 				break;
10900Sstevel@tonic-gate 			}
10910Sstevel@tonic-gate 			dp->d_reclen = (ushort_t)reclen;
10920Sstevel@tonic-gate 			dp->d_ino = (ino64_t)cfnp->fn_nodeid;
10930Sstevel@tonic-gate 			if (nfnp != NULL) {
10940Sstevel@tonic-gate 				/*
10950Sstevel@tonic-gate 				 * get the offset of the next element
10960Sstevel@tonic-gate 				 */
10970Sstevel@tonic-gate 				dp->d_off = (off64_t)nfnp->fn_offset;
10980Sstevel@tonic-gate 			} else {
10990Sstevel@tonic-gate 				/*
11000Sstevel@tonic-gate 				 * This is the last element, make
11010Sstevel@tonic-gate 				 * offset one plus the current
11020Sstevel@tonic-gate 				 */
11030Sstevel@tonic-gate 				dp->d_off = (off64_t)cfnp->fn_offset + 1;
11040Sstevel@tonic-gate 			}
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 			/* use strncpy(9f) to zero out uninitialized bytes */
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 			(void) strncpy(dp->d_name, cfnp->fn_name,
1109*5302Sth199096 			    DIRENT64_NAMELEN(reclen));
11100Sstevel@tonic-gate 			outcount += dp->d_reclen;
11110Sstevel@tonic-gate 			dp = nextdp(dp);
11120Sstevel@tonic-gate 		}
11130Sstevel@tonic-gate 		cfnp = nfnp;
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	if (outcount)
11170Sstevel@tonic-gate 		error = uiomove(outbuf, outcount, UIO_READ, uiop);
1118*5302Sth199096 
11190Sstevel@tonic-gate 	if (!error) {
11200Sstevel@tonic-gate 		if (reached_max) {
11210Sstevel@tonic-gate 			/*
11220Sstevel@tonic-gate 			 * This entry did not get added to the buffer on this,
11230Sstevel@tonic-gate 			 * call. We need to add it on the next call therefore
11240Sstevel@tonic-gate 			 * set uio_offset to this entry's offset.  If there
11250Sstevel@tonic-gate 			 * wasn't enough space for one dirent, return EINVAL.
11260Sstevel@tonic-gate 			 */
11270Sstevel@tonic-gate 			uiop->uio_offset = offset;
11280Sstevel@tonic-gate 			if (outcount == 0)
11290Sstevel@tonic-gate 				error = EINVAL;
11300Sstevel@tonic-gate 		} else if (autofs_nobrowse ||
1131*5302Sth199096 		    auto_nobrowse_option(fnip->fi_opts) ||
1132*5302Sth199096 		    (fnip->fi_flags & MF_DIRECT) ||
1133*5302Sth199096 		    (fnp->fn_trigger != NULL) ||
1134*5302Sth199096 		    (((vp->v_flag & VROOT) == 0) &&
1135*5302Sth199096 		    ((fntovn(fnp->fn_parent))->v_flag & VROOT) &&
1136*5302Sth199096 		    (fnp->fn_dirents == NULL))) {
11370Sstevel@tonic-gate 			/*
11380Sstevel@tonic-gate 			 * done reading directory entries
11390Sstevel@tonic-gate 			 */
11400Sstevel@tonic-gate 			uiop->uio_offset = offset + 1;
11410Sstevel@tonic-gate 			if (eofp)
11420Sstevel@tonic-gate 				*eofp = 1;
11430Sstevel@tonic-gate 		} else {
11440Sstevel@tonic-gate 			/*
11450Sstevel@tonic-gate 			 * Need to get the rest of the entries from the daemon.
11460Sstevel@tonic-gate 			 */
11470Sstevel@tonic-gate 			uiop->uio_offset = AUTOFS_DAEMONCOOKIE;
11480Sstevel@tonic-gate 		}
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate done:
11520Sstevel@tonic-gate 	kmem_free(outbuf, alloc_count);
11530Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_readdir vp=%p offset=%lld eof=%d\n",
1154*5302Sth199096 	    (void *)vp, uiop->uio_loffset, myeof));
11550Sstevel@tonic-gate 	return (error);
11560Sstevel@tonic-gate }
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate static int
11590Sstevel@tonic-gate auto_symlink(
11600Sstevel@tonic-gate 	vnode_t *dvp,
11610Sstevel@tonic-gate 	char *lnknm,		/* new entry */
11620Sstevel@tonic-gate 	vattr_t *tva,
11630Sstevel@tonic-gate 	char *tnm,		/* existing entry */
11640Sstevel@tonic-gate 	cred_t *cred)
11650Sstevel@tonic-gate {
11660Sstevel@tonic-gate 	vnode_t *newvp;
11670Sstevel@tonic-gate 	int error;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_symlink: dvp=%p lnknm=%s tnm=%s\n",
11700Sstevel@tonic-gate 	    (void *)dvp, lnknm, tnm));
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
11730Sstevel@tonic-gate 		goto done;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	if (newvp != NULL) {
11760Sstevel@tonic-gate 		/*
11770Sstevel@tonic-gate 		 * Node is mounted on.
11780Sstevel@tonic-gate 		 */
11790Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
11800Sstevel@tonic-gate 			error = EROFS;
11810Sstevel@tonic-gate 		else
11820Sstevel@tonic-gate 			error = VOP_SYMLINK(newvp, lnknm, tva, tnm, cred);
11830Sstevel@tonic-gate 		VN_RELE(newvp);
11840Sstevel@tonic-gate 	} else
11850Sstevel@tonic-gate 		error = ENOSYS;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate done:
11880Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_symlink: error=%d\n", error));
11890Sstevel@tonic-gate 	return (error);
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate /* ARGSUSED */
11930Sstevel@tonic-gate static int
11940Sstevel@tonic-gate auto_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr)
11950Sstevel@tonic-gate {
11960Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
11970Sstevel@tonic-gate 	int error;
11980Sstevel@tonic-gate 	timestruc_t now;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_readlink: vp=%p\n", (void *)vp));
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	gethrestime(&now);
12030Sstevel@tonic-gate 	fnp->fn_ref_time = now.tv_sec;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	if (vp->v_type != VLNK)
12060Sstevel@tonic-gate 		error = EINVAL;
12070Sstevel@tonic-gate 	else {
12080Sstevel@tonic-gate 		ASSERT(!(fnp->fn_flags & (MF_INPROG | MF_LOOKUP)));
12090Sstevel@tonic-gate 		fnp->fn_atime = now;
12100Sstevel@tonic-gate 		error = uiomove(fnp->fn_symlink, MIN(fnp->fn_symlinklen,
12110Sstevel@tonic-gate 		    uiop->uio_resid), UIO_READ, uiop);
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_readlink: error=%d\n", error));
12150Sstevel@tonic-gate 	return (error);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate /* ARGSUSED */
12190Sstevel@tonic-gate static int
12200Sstevel@tonic-gate auto_fsync(vnode_t *cp, int syncflag, cred_t *cred)
12210Sstevel@tonic-gate {
12220Sstevel@tonic-gate 	return (0);
12230Sstevel@tonic-gate }
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate /* ARGSUSED */
12260Sstevel@tonic-gate static void
12270Sstevel@tonic-gate auto_inactive(vnode_t *vp, cred_t *cred)
12280Sstevel@tonic-gate {
12290Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
12300Sstevel@tonic-gate 	fnnode_t *dfnp = fnp->fn_parent;
12310Sstevel@tonic-gate 	int count;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_inactive: vp=%p v_count=%u fn_link=%d\n",
12340Sstevel@tonic-gate 	    (void *)vp, vp->v_count, fnp->fn_linkcnt));
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	/*
12370Sstevel@tonic-gate 	 * The rwlock should not be already held by this thread.
12380Sstevel@tonic-gate 	 * The assert relies on the fact that the owner field is cleared
12390Sstevel@tonic-gate 	 * when the lock is released.
12400Sstevel@tonic-gate 	 */
12410Sstevel@tonic-gate 	ASSERT(dfnp != NULL);
12420Sstevel@tonic-gate 	ASSERT(rw_owner(&dfnp->fn_rwlock) != curthread);
12430Sstevel@tonic-gate 	rw_enter(&dfnp->fn_rwlock, RW_WRITER);
12440Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
12450Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
12460Sstevel@tonic-gate 	count = --vp->v_count;
12470Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
12480Sstevel@tonic-gate 	if (count == 0) {
12490Sstevel@tonic-gate 		/*
12500Sstevel@tonic-gate 		 * Free only if node has no subdirectories.
12510Sstevel@tonic-gate 		 */
12520Sstevel@tonic-gate 		if (fnp->fn_linkcnt == 1) {
12530Sstevel@tonic-gate 			auto_disconnect(dfnp, fnp);
12540Sstevel@tonic-gate 			rw_exit(&dfnp->fn_rwlock);
12550Sstevel@tonic-gate 			auto_freefnnode(fnp);
12560Sstevel@tonic-gate 			AUTOFS_DPRINT((5, "auto_inactive: (exit) vp=%p freed\n",
12570Sstevel@tonic-gate 			    (void *)vp));
12580Sstevel@tonic-gate 			return;
12590Sstevel@tonic-gate 		}
12600Sstevel@tonic-gate 	}
12610Sstevel@tonic-gate 	rw_exit(&dfnp->fn_rwlock);
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_inactive: (exit) vp=%p v_count=%u fn_link=%d\n",
12640Sstevel@tonic-gate 	    (void *)vp, vp->v_count, fnp->fn_linkcnt));
12650Sstevel@tonic-gate }
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate /* ARGSUSED2 */
12680Sstevel@tonic-gate static int
12690Sstevel@tonic-gate auto_rwlock(vnode_t *vp, int write_lock, caller_context_t *ct)
12700Sstevel@tonic-gate {
12710Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
12720Sstevel@tonic-gate 	if (write_lock)
12730Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_WRITER);
12740Sstevel@tonic-gate 	else
12750Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_READER);
12760Sstevel@tonic-gate 	return (write_lock);
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate /* ARGSUSED */
12800Sstevel@tonic-gate static void
12810Sstevel@tonic-gate auto_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ct)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
12840Sstevel@tonic-gate 	rw_exit(&fnp->fn_rwlock);
12850Sstevel@tonic-gate }
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate /* ARGSUSED */
12890Sstevel@tonic-gate static int
12900Sstevel@tonic-gate auto_seek(struct vnode *vp, offset_t ooff, offset_t *noffp)
12910Sstevel@tonic-gate {
12920Sstevel@tonic-gate 	/*
12930Sstevel@tonic-gate 	 * Return 0 unconditionally, since we expect
12940Sstevel@tonic-gate 	 * a VDIR all the time
12950Sstevel@tonic-gate 	 */
12960Sstevel@tonic-gate 	return (0);
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate /*
13000Sstevel@tonic-gate  * Triggers the mount if needed. If the mount has been triggered by
13010Sstevel@tonic-gate  * another thread, it will wait for its return status, and return it.
13020Sstevel@tonic-gate  * Whether the mount is triggered by this thread, another thread, or
13030Sstevel@tonic-gate  * if the vnode was already covered, '*newvp' is a
13040Sstevel@tonic-gate  * VN_HELD vnode pointing to the root of the filesystem covering 'vp'.
13050Sstevel@tonic-gate  * If the node is not mounted on, and should not be mounted on, '*newvp'
13060Sstevel@tonic-gate  * will be NULL.
13070Sstevel@tonic-gate  * The calling routine may use '*newvp' to do the filesystem jump.
13080Sstevel@tonic-gate  */
13090Sstevel@tonic-gate static int
13100Sstevel@tonic-gate auto_trigger_mount(vnode_t *vp, cred_t *cred, vnode_t **newvp)
13110Sstevel@tonic-gate {
13120Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
13130Sstevel@tonic-gate 	fninfo_t *fnip = vfstofni(vp->v_vfsp);
13140Sstevel@tonic-gate 	vnode_t *dvp;
13150Sstevel@tonic-gate 	vfs_t *vfsp;
13160Sstevel@tonic-gate 	int delayed_ind;
13170Sstevel@tonic-gate 	char name[AUTOFS_MAXPATHLEN];
13180Sstevel@tonic-gate 	int error;
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_trigger_mount: vp=%p\n", (void *)vp));
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	*newvp = NULL;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/*
13250Sstevel@tonic-gate 	 * Cross-zone mount triggering is disallowed.
13260Sstevel@tonic-gate 	 */
13270Sstevel@tonic-gate 	if (fnip->fi_zoneid != getzoneid())
13280Sstevel@tonic-gate 		return (EPERM);	/* Not owner of mount */
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate retry:
13310Sstevel@tonic-gate 	error = 0;
13320Sstevel@tonic-gate 	delayed_ind = 0;
13330Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
13340Sstevel@tonic-gate 	while (fnp->fn_flags & (MF_LOOKUP | MF_INPROG)) {
13350Sstevel@tonic-gate 		/*
13360Sstevel@tonic-gate 		 * Mount or lookup in progress,
13370Sstevel@tonic-gate 		 * wait for it before proceeding.
13380Sstevel@tonic-gate 		 */
13390Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
13400Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
13410Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN) {
13420Sstevel@tonic-gate 			error = 0;
13430Sstevel@tonic-gate 			goto done;
13440Sstevel@tonic-gate 		}
13450Sstevel@tonic-gate 		if (error && error != EAGAIN)
13460Sstevel@tonic-gate 			goto done;
13470Sstevel@tonic-gate 		error = 0;
13480Sstevel@tonic-gate 		mutex_enter(&fnp->fn_lock);
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	/*
13520Sstevel@tonic-gate 	 * If the vfslock can't be acquired for the first time.
13530Sstevel@tonic-gate 	 * drop the fn_lock and retry next time in blocking mode.
13540Sstevel@tonic-gate 	 */
13550Sstevel@tonic-gate 	if (vn_vfswlock(vp)) {
13560Sstevel@tonic-gate 		/*
13570Sstevel@tonic-gate 		 * Lock held by another thread.
13580Sstevel@tonic-gate 		 * Perform blocking by dropping the
13590Sstevel@tonic-gate 		 * fn_lock.
13600Sstevel@tonic-gate 		 */
13610Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
13620Sstevel@tonic-gate 		error = vn_vfswlock_wait(vp);
13630Sstevel@tonic-gate 		if (error)
13640Sstevel@tonic-gate 			goto done;
13650Sstevel@tonic-gate 		/*
13660Sstevel@tonic-gate 		 * Because fn_lock wasn't held, the state
13670Sstevel@tonic-gate 		 * of the trigger node might have changed.
13680Sstevel@tonic-gate 		 * Need to run through the checks on trigger
13690Sstevel@tonic-gate 		 * node again.
13700Sstevel@tonic-gate 		 */
13710Sstevel@tonic-gate 		vn_vfsunlock(vp);
13720Sstevel@tonic-gate 		goto retry;
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	vfsp = vn_mountedvfs(vp);
13760Sstevel@tonic-gate 	if (vfsp != NULL) {
13770Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
13781153Snr123932 		error = VFS_ROOT(vfsp, newvp);
13790Sstevel@tonic-gate 		vn_vfsunlock(vp);
13800Sstevel@tonic-gate 		goto done;
13810Sstevel@tonic-gate 	} else {
13820Sstevel@tonic-gate 		vn_vfsunlock(vp);
13830Sstevel@tonic-gate 		if ((fnp->fn_flags & MF_MOUNTPOINT) &&
13840Sstevel@tonic-gate 		    fnp->fn_trigger != NULL) {
13850Sstevel@tonic-gate 			ASSERT(fnp->fn_dirents == NULL);
13860Sstevel@tonic-gate 			/*
13870Sstevel@tonic-gate 			 * The filesystem that used to sit here has been
13880Sstevel@tonic-gate 			 * forcibly unmounted.
13890Sstevel@tonic-gate 			 */
13900Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
13910Sstevel@tonic-gate 			error = EIO;
13920Sstevel@tonic-gate 			goto done;
13930Sstevel@tonic-gate 		}
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
13970Sstevel@tonic-gate 	dvp = fntovn(fnp->fn_parent);
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	if ((fnp->fn_dirents == NULL) &&
14000Sstevel@tonic-gate 	    ((fnip->fi_flags & MF_DIRECT) == 0) &&
14010Sstevel@tonic-gate 	    ((vp->v_flag & VROOT) == 0) &&
14020Sstevel@tonic-gate 	    (dvp->v_flag & VROOT)) {
14030Sstevel@tonic-gate 		/*
14040Sstevel@tonic-gate 		 * If the parent of this node is the root of an indirect
14050Sstevel@tonic-gate 		 * AUTOFS filesystem, this node is remountable.
14060Sstevel@tonic-gate 		 */
14070Sstevel@tonic-gate 		delayed_ind = 1;
14080Sstevel@tonic-gate 	}
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	if (delayed_ind ||
14110Sstevel@tonic-gate 	    ((fnip->fi_flags & MF_DIRECT) && (fnp->fn_dirents == NULL))) {
14120Sstevel@tonic-gate 		/*
14130Sstevel@tonic-gate 		 * Trigger mount since:
14140Sstevel@tonic-gate 		 * direct mountpoint with no subdirs or
14150Sstevel@tonic-gate 		 * delayed indirect.
14160Sstevel@tonic-gate 		 */
14170Sstevel@tonic-gate 		AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
14180Sstevel@tonic-gate 		fnp->fn_error = 0;
14190Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
14200Sstevel@tonic-gate 		if (delayed_ind)
14210Sstevel@tonic-gate 			(void) strcpy(name, fnp->fn_name);
14220Sstevel@tonic-gate 		else
14230Sstevel@tonic-gate 			(void) strcpy(name, ".");
14240Sstevel@tonic-gate 		fnp->fn_ref_time = gethrestime_sec();
14250Sstevel@tonic-gate 		auto_new_mount_thread(fnp, name, cred);
14260Sstevel@tonic-gate 		/*
14270Sstevel@tonic-gate 		 * At this point we're simply another thread waiting
14280Sstevel@tonic-gate 		 * for the mount to finish.
14290Sstevel@tonic-gate 		 */
14300Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
14310Sstevel@tonic-gate 		if (error == EAGAIN)
14320Sstevel@tonic-gate 			goto retry;
14330Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN) {
14340Sstevel@tonic-gate 			error = 0;
14350Sstevel@tonic-gate 			goto done;
14360Sstevel@tonic-gate 		}
14370Sstevel@tonic-gate 		if (error == 0) {
14381153Snr123932 			if (error = vn_vfsrlock_wait(vp))
14390Sstevel@tonic-gate 				goto done;
14400Sstevel@tonic-gate 			/* Reacquire after dropping locks */
14410Sstevel@tonic-gate 			vfsp = vn_mountedvfs(vp);
14420Sstevel@tonic-gate 			if (vfsp != NULL) {
14431153Snr123932 				error = VFS_ROOT(vfsp, newvp);
14440Sstevel@tonic-gate 				vn_vfsunlock(vp);
14450Sstevel@tonic-gate 			} else {
14460Sstevel@tonic-gate 				vn_vfsunlock(vp);
14470Sstevel@tonic-gate 				goto retry;
14480Sstevel@tonic-gate 			}
14490Sstevel@tonic-gate 		}
14500Sstevel@tonic-gate 	} else
14510Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate done:
14540Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_trigger_mount: error=%d\n", error));
14550Sstevel@tonic-gate 	return (error);
14560Sstevel@tonic-gate }
1457