xref: /onnv-gate/usr/src/uts/common/fs/autofs/auto_vnops.c (revision 5331:3047ad28a67b)
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  */
56*5331Samw static int auto_open(vnode_t **, int, cred_t *, caller_context_t *);
57*5331Samw static int auto_close(vnode_t *, int, int, offset_t, cred_t *,
58*5331Samw 	caller_context_t *);
59*5331Samw static int auto_getattr(vnode_t *, vattr_t *, int, cred_t *,
60*5331Samw 	caller_context_t *);
610Sstevel@tonic-gate static int auto_setattr(vnode_t *, vattr_t *, int, cred_t *,
620Sstevel@tonic-gate 	caller_context_t *);
63*5331Samw static int auto_access(vnode_t *, int, int, cred_t *, caller_context_t *);
640Sstevel@tonic-gate static int auto_lookup(vnode_t *, char *, vnode_t **,
65*5331Samw 	pathname_t *, int, vnode_t *, cred_t *, caller_context_t *, int *,
66*5331Samw 	pathname_t *);
670Sstevel@tonic-gate static int auto_create(vnode_t *, char *, vattr_t *, vcexcl_t,
68*5331Samw 	int, vnode_t **, cred_t *, int, caller_context_t *,  vsecattr_t *);
69*5331Samw static int auto_remove(vnode_t *, char *, cred_t *, caller_context_t *, int);
70*5331Samw static int auto_link(vnode_t *, vnode_t *, char *, cred_t *,
71*5331Samw 	caller_context_t *, int);
72*5331Samw static int auto_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
73*5331Samw 	caller_context_t *, int);
74*5331Samw static int auto_mkdir(vnode_t *, char *, vattr_t *, vnode_t **, cred_t *,
75*5331Samw 	caller_context_t *, int, vsecattr_t *);
76*5331Samw static int auto_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
77*5331Samw 	caller_context_t *, int);
78*5331Samw static int auto_readdir(vnode_t *, uio_t *, cred_t *, int *,
79*5331Samw 	caller_context_t *, int);
80*5331Samw static int auto_symlink(vnode_t *, char *, vattr_t *, char *, cred_t *,
81*5331Samw 	caller_context_t *, int);
82*5331Samw static int auto_readlink(vnode_t *, struct uio *, cred_t *,
83*5331Samw 	caller_context_t *);
84*5331Samw static int auto_fsync(vnode_t *, int, cred_t *, caller_context_t *);
85*5331Samw static void auto_inactive(vnode_t *, cred_t *, caller_context_t *);
860Sstevel@tonic-gate static int auto_rwlock(vnode_t *, int, caller_context_t *);
870Sstevel@tonic-gate static void auto_rwunlock(vnode_t *vp, int, caller_context_t *);
88*5331Samw static int auto_seek(vnode_t *vp, offset_t, offset_t *, caller_context_t *);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static int auto_trigger_mount(vnode_t *, cred_t *, vnode_t **);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate vnodeops_t *auto_vnodeops;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate const fs_operation_def_t auto_vnodeops_template[] = {
953898Srsb 	VOPNAME_OPEN,		{ .vop_open = auto_open },
963898Srsb 	VOPNAME_CLOSE,		{ .vop_close = auto_close },
973898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = auto_getattr },
983898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = auto_setattr },
993898Srsb 	VOPNAME_ACCESS,		{ .vop_access = auto_access },
1003898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = auto_lookup },
1013898Srsb 	VOPNAME_CREATE,		{ .vop_create = auto_create },
1023898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = auto_remove },
1033898Srsb 	VOPNAME_LINK,		{ .vop_link = auto_link },
1043898Srsb 	VOPNAME_RENAME,		{ .vop_rename = auto_rename },
1053898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = auto_mkdir },
1063898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = auto_rmdir },
1073898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = auto_readdir },
1083898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = auto_symlink },
1093898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = auto_readlink },
1103898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = auto_fsync },
1113898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = auto_inactive },
1123898Srsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = auto_rwlock },
1133898Srsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = auto_rwunlock },
1143898Srsb 	VOPNAME_SEEK,		{ .vop_seek = auto_seek },
1153898Srsb 	VOPNAME_FRLOCK,		{ .error = fs_error },
1163898Srsb 	VOPNAME_DISPOSE,	{ .error = fs_error },
1173898Srsb 	VOPNAME_SHRLOCK,	{ .error = fs_error },
1184863Spraks 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
1193898Srsb 	NULL,			NULL
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate 
1222170Sevanl 
1232170Sevanl 
1240Sstevel@tonic-gate /* ARGSUSED */
1250Sstevel@tonic-gate static int
126*5331Samw auto_open(vnode_t **vpp, int flag, cred_t *cred, caller_context_t *ct)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	vnode_t *newvp;
1290Sstevel@tonic-gate 	int error;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_open: *vpp=%p\n", (void *)*vpp));
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	error = auto_trigger_mount(*vpp, cred, &newvp);
1340Sstevel@tonic-gate 	if (error)
1350Sstevel@tonic-gate 		goto done;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if (newvp != NULL) {
1380Sstevel@tonic-gate 		/*
1390Sstevel@tonic-gate 		 * Node is now mounted on.
1400Sstevel@tonic-gate 		 */
1410Sstevel@tonic-gate 		VN_RELE(*vpp);
1420Sstevel@tonic-gate 		*vpp = newvp;
143*5331Samw 		error = VOP_ACCESS(*vpp, VREAD, 0, cred, ct);
1440Sstevel@tonic-gate 		if (!error)
145*5331Samw 			error = VOP_OPEN(vpp, flag, cred, ct);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate done:
1490Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_open: *vpp=%p error=%d\n", (void *)*vpp,
1500Sstevel@tonic-gate 	    error));
1510Sstevel@tonic-gate 	return (error);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /* ARGSUSED */
1550Sstevel@tonic-gate static int
156*5331Samw auto_close(
157*5331Samw 	vnode_t *vp,
158*5331Samw 	int flag,
159*5331Samw 	int count,
160*5331Samw 	offset_t offset,
161*5331Samw 	cred_t *cred,
162*5331Samw 	caller_context_t *ct)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	return (0);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate static int
168*5331Samw auto_getattr(
169*5331Samw 	vnode_t *vp,
170*5331Samw 	vattr_t *vap,
171*5331Samw 	int flags,
172*5331Samw 	cred_t *cred,
173*5331Samw 	caller_context_t *ct)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
1760Sstevel@tonic-gate 	vnode_t *newvp;
1770Sstevel@tonic-gate 	vfs_t *vfsp;
1780Sstevel@tonic-gate 	int error;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_getattr vp %p\n", (void *)vp));
1810Sstevel@tonic-gate 
1825302Sth199096 	if (flags & ATTR_TRIGGER) {
1835302Sth199096 		/*
1845302Sth199096 		 * Pre-trigger the mount
1855302Sth199096 		 */
1865302Sth199096 		error = auto_trigger_mount(vp, cred, &newvp);
1875302Sth199096 		if (error)
1885302Sth199096 			return (error);
1895302Sth199096 
1905302Sth199096 		if (newvp == NULL)
1915302Sth199096 			goto defattr;
1925302Sth199096 
1935302Sth199096 		if (error = vn_vfsrlock_wait(vp))
1945302Sth199096 			return (error);
195149Scasper 
1965302Sth199096 		vfsp = newvp->v_vfsp;
1975302Sth199096 	} else {
1985302Sth199096 		/*
1995302Sth199096 		 * Recursive auto_getattr/mount; go to the vfsp == NULL
2005302Sth199096 		 * case.
2015302Sth199096 		 */
2025302Sth199096 		if (vn_vfswlock_held(vp))
2035302Sth199096 			goto defattr;
2040Sstevel@tonic-gate 
2055302Sth199096 		if (error = vn_vfsrlock_wait(vp))
2065302Sth199096 			return (error);
2075302Sth199096 
2085302Sth199096 		vfsp = vn_mountedvfs(vp);
2095302Sth199096 	}
2105302Sth199096 
2110Sstevel@tonic-gate 	if (vfsp != NULL) {
2120Sstevel@tonic-gate 		/*
2130Sstevel@tonic-gate 		 * Node is mounted on.
2140Sstevel@tonic-gate 		 */
2151153Snr123932 		error = VFS_ROOT(vfsp, &newvp);
2160Sstevel@tonic-gate 		vn_vfsunlock(vp);
2170Sstevel@tonic-gate 		if (error)
2180Sstevel@tonic-gate 			return (error);
2190Sstevel@tonic-gate 		mutex_enter(&fnp->fn_lock);
2200Sstevel@tonic-gate 		if (fnp->fn_seen == newvp && fnp->fn_thread == curthread) {
2210Sstevel@tonic-gate 			/*
2220Sstevel@tonic-gate 			 * Recursive auto_getattr(); just release newvp and drop
2230Sstevel@tonic-gate 			 * into the vfsp == NULL case.
2240Sstevel@tonic-gate 			 */
2250Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
2260Sstevel@tonic-gate 			VN_RELE(newvp);
2270Sstevel@tonic-gate 		} else {
2280Sstevel@tonic-gate 			while (fnp->fn_thread && fnp->fn_thread != curthread) {
2290Sstevel@tonic-gate 				fnp->fn_flags |= MF_ATTR_WAIT;
2300Sstevel@tonic-gate 				cv_wait(&fnp->fn_cv_mount, &fnp->fn_lock);
2310Sstevel@tonic-gate 			}
2320Sstevel@tonic-gate 			fnp->fn_thread = curthread;
2330Sstevel@tonic-gate 			fnp->fn_seen = newvp;
2340Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
235*5331Samw 			error = VOP_GETATTR(newvp, vap, flags, cred, ct);
2360Sstevel@tonic-gate 			VN_RELE(newvp);
2370Sstevel@tonic-gate 			mutex_enter(&fnp->fn_lock);
2380Sstevel@tonic-gate 			fnp->fn_seen = 0;
2390Sstevel@tonic-gate 			fnp->fn_thread = 0;
2400Sstevel@tonic-gate 			if (fnp->fn_flags & MF_ATTR_WAIT) {
2410Sstevel@tonic-gate 				fnp->fn_flags &= ~MF_ATTR_WAIT;
2420Sstevel@tonic-gate 				cv_broadcast(&fnp->fn_cv_mount);
2430Sstevel@tonic-gate 			}
2440Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
2450Sstevel@tonic-gate 			return (error);
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 	} else {
2480Sstevel@tonic-gate 		vn_vfsunlock(vp);
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
251149Scasper defattr:
2520Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
2530Sstevel@tonic-gate 	vap->va_uid	= 0;
2540Sstevel@tonic-gate 	vap->va_gid	= 0;
2550Sstevel@tonic-gate 	vap->va_nlink	= fnp->fn_linkcnt;
2560Sstevel@tonic-gate 	vap->va_nodeid	= (u_longlong_t)fnp->fn_nodeid;
2570Sstevel@tonic-gate 	vap->va_size	= fnp->fn_size;
2580Sstevel@tonic-gate 	vap->va_atime	= fnp->fn_atime;
2590Sstevel@tonic-gate 	vap->va_mtime	= fnp->fn_mtime;
2600Sstevel@tonic-gate 	vap->va_ctime	= fnp->fn_ctime;
2610Sstevel@tonic-gate 	vap->va_type	= vp->v_type;
2620Sstevel@tonic-gate 	vap->va_mode	= fnp->fn_mode;
2630Sstevel@tonic-gate 	vap->va_fsid	= vp->v_vfsp->vfs_dev;
2640Sstevel@tonic-gate 	vap->va_rdev	= 0;
2650Sstevel@tonic-gate 	vap->va_blksize	= MAXBSIZE;
2660Sstevel@tonic-gate 	vap->va_nblocks	= (fsblkcnt64_t)btod(vap->va_size);
2670Sstevel@tonic-gate 	vap->va_seq	= 0;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	return (0);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*ARGSUSED4*/
2730Sstevel@tonic-gate static int
2740Sstevel@tonic-gate auto_setattr(
2750Sstevel@tonic-gate 	vnode_t *vp,
2760Sstevel@tonic-gate 	struct vattr *vap,
2770Sstevel@tonic-gate 	int flags,
2780Sstevel@tonic-gate 	cred_t *cred,
2790Sstevel@tonic-gate 	caller_context_t *ct)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	vnode_t *newvp;
2820Sstevel@tonic-gate 	int error;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_setattr vp %p\n", (void *)vp));
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (error = auto_trigger_mount(vp, cred, &newvp))
2870Sstevel@tonic-gate 		goto done;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (newvp != NULL) {
2900Sstevel@tonic-gate 		/*
2910Sstevel@tonic-gate 		 * Node is mounted on.
2920Sstevel@tonic-gate 		 */
2930Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
2940Sstevel@tonic-gate 			error = EROFS;
2950Sstevel@tonic-gate 		else
296*5331Samw 			error = VOP_SETATTR(newvp, vap, flags, cred, ct);
2970Sstevel@tonic-gate 		VN_RELE(newvp);
2980Sstevel@tonic-gate 	} else
2990Sstevel@tonic-gate 		error = ENOSYS;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate done:
3020Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_setattr: error=%d\n", error));
3030Sstevel@tonic-gate 	return (error);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /* ARGSUSED */
3070Sstevel@tonic-gate static int
308*5331Samw auto_access(
309*5331Samw 	vnode_t *vp,
310*5331Samw 	int mode,
311*5331Samw 	int flags,
312*5331Samw 	cred_t *cred,
313*5331Samw 	caller_context_t *ct)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
3160Sstevel@tonic-gate 	vnode_t *newvp;
3170Sstevel@tonic-gate 	int error;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_access: vp=%p\n", (void *)vp));
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	if (error = auto_trigger_mount(vp, cred, &newvp))
3220Sstevel@tonic-gate 		goto done;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (newvp != NULL) {
3250Sstevel@tonic-gate 		/*
3260Sstevel@tonic-gate 		 * Node is mounted on.
3270Sstevel@tonic-gate 		 */
328*5331Samw 		error = VOP_ACCESS(newvp, mode, 0, cred, ct);
3290Sstevel@tonic-gate 		VN_RELE(newvp);
3300Sstevel@tonic-gate 	} else {
3310Sstevel@tonic-gate 		int shift = 0;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		/*
3340Sstevel@tonic-gate 		 * really interested in the autofs node, check the
3350Sstevel@tonic-gate 		 * access on it
3360Sstevel@tonic-gate 		 */
3370Sstevel@tonic-gate 		ASSERT(error == 0);
3380Sstevel@tonic-gate 		if (crgetuid(cred) != fnp->fn_uid) {
3390Sstevel@tonic-gate 			shift += 3;
3400Sstevel@tonic-gate 			if (groupmember(fnp->fn_gid, cred) == 0)
3410Sstevel@tonic-gate 				shift += 3;
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 		mode &= ~(fnp->fn_mode << shift);
3440Sstevel@tonic-gate 		if (mode != 0)
3450Sstevel@tonic-gate 			error = secpolicy_vnode_access(cred, vp, fnp->fn_uid,
3465302Sth199096 			    mode);
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate done:
3500Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_access: error=%d\n", error));
3510Sstevel@tonic-gate 	return (error);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate static int
3550Sstevel@tonic-gate auto_lookup(
3560Sstevel@tonic-gate 	vnode_t *dvp,
3570Sstevel@tonic-gate 	char *nm,
3580Sstevel@tonic-gate 	vnode_t **vpp,
3590Sstevel@tonic-gate 	pathname_t *pnp,
3600Sstevel@tonic-gate 	int flags,
3610Sstevel@tonic-gate 	vnode_t *rdir,
362*5331Samw 	cred_t *cred,
363*5331Samw 	caller_context_t *ct,
364*5331Samw 	int *direntflags,
365*5331Samw 	pathname_t *realpnp)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	int error = 0;
3680Sstevel@tonic-gate 	vnode_t *newvp = NULL;
3690Sstevel@tonic-gate 	vfs_t *vfsp;
3700Sstevel@tonic-gate 	fninfo_t *dfnip;
3710Sstevel@tonic-gate 	fnnode_t *dfnp = NULL;
3720Sstevel@tonic-gate 	fnnode_t *fnp = NULL;
3730Sstevel@tonic-gate 	char *searchnm;
3740Sstevel@tonic-gate 	int operation;		/* either AUTOFS_LOOKUP or AUTOFS_MOUNT */
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	dfnip = vfstofni(dvp->v_vfsp);
3770Sstevel@tonic-gate 	AUTOFS_DPRINT((3, "auto_lookup: dvp=%p (%s) name=%s\n",
3780Sstevel@tonic-gate 	    (void *)dvp, dfnip->fi_map, nm));
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	if (nm[0] == 0) {
3810Sstevel@tonic-gate 		VN_HOLD(dvp);
3820Sstevel@tonic-gate 		*vpp = dvp;
3830Sstevel@tonic-gate 		return (0);
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
386*5331Samw 	if (error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct))
3870Sstevel@tonic-gate 		return (error);
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (nm[0] == '.' && nm[1] == 0) {
3900Sstevel@tonic-gate 		VN_HOLD(dvp);
3910Sstevel@tonic-gate 		*vpp = dvp;
3920Sstevel@tonic-gate 		return (0);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	if (nm[0] == '.' && nm[1] == '.' && nm[2] == 0) {
3960Sstevel@tonic-gate 		fnnode_t *pdfnp;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 		pdfnp = (vntofn(dvp))->fn_parent;
3990Sstevel@tonic-gate 		ASSERT(pdfnp != NULL);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		/*
4020Sstevel@tonic-gate 		 * Since it is legitimate to have the VROOT flag set for the
4030Sstevel@tonic-gate 		 * subdirectories of the indirect map in autofs filesystem,
4040Sstevel@tonic-gate 		 * rootfnnodep is checked against fnnode of dvp instead of
4050Sstevel@tonic-gate 		 * just checking whether VROOT flag is set in dvp
4060Sstevel@tonic-gate 		 */
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 		if (pdfnp == pdfnp->fn_globals->fng_rootfnnodep) {
4090Sstevel@tonic-gate 			vnode_t *vp;
4100Sstevel@tonic-gate 
4111153Snr123932 			vfs_rlock_wait(dvp->v_vfsp);
4120Sstevel@tonic-gate 			if (dvp->v_vfsp->vfs_flag & VFS_UNMOUNTED) {
4130Sstevel@tonic-gate 				vfs_unlock(dvp->v_vfsp);
4140Sstevel@tonic-gate 				return (EIO);
4150Sstevel@tonic-gate 			}
4160Sstevel@tonic-gate 			vp = dvp->v_vfsp->vfs_vnodecovered;
4170Sstevel@tonic-gate 			VN_HOLD(vp);
4180Sstevel@tonic-gate 			vfs_unlock(dvp->v_vfsp);
419*5331Samw 			error = VOP_LOOKUP(vp, nm, vpp, pnp, flags, rdir, cred,
420*5331Samw 			    ct, direntflags, realpnp);
4210Sstevel@tonic-gate 			VN_RELE(vp);
4220Sstevel@tonic-gate 			return (error);
4230Sstevel@tonic-gate 		} else {
4240Sstevel@tonic-gate 			*vpp = fntovn(pdfnp);
4250Sstevel@tonic-gate 			VN_HOLD(*vpp);
4260Sstevel@tonic-gate 			return (0);
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate top:
4310Sstevel@tonic-gate 	dfnp = vntofn(dvp);
4320Sstevel@tonic-gate 	searchnm = nm;
4330Sstevel@tonic-gate 	operation = 0;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	ASSERT(vn_matchops(dvp, auto_vnodeops));
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	AUTOFS_DPRINT((3, "auto_lookup: dvp=%p dfnp=%p\n", (void *)dvp,
4380Sstevel@tonic-gate 	    (void *)dfnp));
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	/*
4410Sstevel@tonic-gate 	 * If a lookup or mount of this node is in progress, wait for it
4420Sstevel@tonic-gate 	 * to finish, and return whatever result it got.
4430Sstevel@tonic-gate 	 */
4440Sstevel@tonic-gate 	mutex_enter(&dfnp->fn_lock);
4450Sstevel@tonic-gate 	if (dfnp->fn_flags & (MF_LOOKUP | MF_INPROG)) {
4460Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
4470Sstevel@tonic-gate 		error = auto_wait4mount(dfnp);
4480Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN)
4490Sstevel@tonic-gate 			error = ENOENT;
4500Sstevel@tonic-gate 		if (error == EAGAIN)
4510Sstevel@tonic-gate 			goto top;
4520Sstevel@tonic-gate 		if (error)
4530Sstevel@tonic-gate 			return (error);
4540Sstevel@tonic-gate 	} else
4550Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 
4581153Snr123932 	error = vn_vfsrlock_wait(dvp);
4590Sstevel@tonic-gate 	if (error)
4600Sstevel@tonic-gate 		return (error);
4610Sstevel@tonic-gate 	vfsp = vn_mountedvfs(dvp);
4620Sstevel@tonic-gate 	if (vfsp != NULL) {
4631153Snr123932 		error = VFS_ROOT(vfsp, &newvp);
4640Sstevel@tonic-gate 		vn_vfsunlock(dvp);
4650Sstevel@tonic-gate 		if (!error) {
4660Sstevel@tonic-gate 			error = VOP_LOOKUP(newvp, nm, vpp, pnp,
467*5331Samw 			    flags, rdir, cred, ct, direntflags, realpnp);
4680Sstevel@tonic-gate 			VN_RELE(newvp);
4690Sstevel@tonic-gate 		}
4700Sstevel@tonic-gate 		return (error);
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 	vn_vfsunlock(dvp);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	rw_enter(&dfnp->fn_rwlock, RW_READER);
4750Sstevel@tonic-gate 	error = auto_search(dfnp, nm, &fnp, cred);
4760Sstevel@tonic-gate 	if (error) {
4770Sstevel@tonic-gate 		if (dfnip->fi_flags & MF_DIRECT) {
4780Sstevel@tonic-gate 			/*
4790Sstevel@tonic-gate 			 * direct map.
4800Sstevel@tonic-gate 			 */
4810Sstevel@tonic-gate 			if (dfnp->fn_dirents) {
4820Sstevel@tonic-gate 				/*
4830Sstevel@tonic-gate 				 * Mount previously triggered.
4840Sstevel@tonic-gate 				 * 'nm' not found
4850Sstevel@tonic-gate 				 */
4860Sstevel@tonic-gate 				error = ENOENT;
4870Sstevel@tonic-gate 			} else {
4880Sstevel@tonic-gate 				/*
4890Sstevel@tonic-gate 				 * I need to contact the daemon to trigger
4900Sstevel@tonic-gate 				 * the mount. 'dfnp' will be the mountpoint.
4910Sstevel@tonic-gate 				 */
4920Sstevel@tonic-gate 				operation = AUTOFS_MOUNT;
4930Sstevel@tonic-gate 				VN_HOLD(fntovn(dfnp));
4940Sstevel@tonic-gate 				fnp = dfnp;
4950Sstevel@tonic-gate 				error = 0;
4960Sstevel@tonic-gate 			}
4970Sstevel@tonic-gate 		} else if (dvp == dfnip->fi_rootvp) {
4980Sstevel@tonic-gate 			/*
4990Sstevel@tonic-gate 			 * 'dfnp' is the root of the indirect AUTOFS.
5000Sstevel@tonic-gate 			 */
5010Sstevel@tonic-gate 			if (rw_tryupgrade(&dfnp->fn_rwlock) == 0) {
5020Sstevel@tonic-gate 				/*
5030Sstevel@tonic-gate 				 * Could not acquire writer lock, release
5040Sstevel@tonic-gate 				 * reader, and wait until available. We
5050Sstevel@tonic-gate 				 * need to search for 'nm' again, since we
5060Sstevel@tonic-gate 				 * had to release the lock before reacquiring
5070Sstevel@tonic-gate 				 * it.
5080Sstevel@tonic-gate 				 */
5090Sstevel@tonic-gate 				rw_exit(&dfnp->fn_rwlock);
5100Sstevel@tonic-gate 				rw_enter(&dfnp->fn_rwlock, RW_WRITER);
5110Sstevel@tonic-gate 				error = auto_search(dfnp, nm, &fnp, cred);
5120Sstevel@tonic-gate 			}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 			ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
5150Sstevel@tonic-gate 			if (error) {
5160Sstevel@tonic-gate 				/*
5170Sstevel@tonic-gate 				 * create node being looked-up and request
5180Sstevel@tonic-gate 				 * mount on it.
5190Sstevel@tonic-gate 				 */
5200Sstevel@tonic-gate 				error = auto_enter(dfnp, nm, &fnp, kcred);
5210Sstevel@tonic-gate 				if (!error)
5220Sstevel@tonic-gate 					operation = AUTOFS_LOOKUP;
5230Sstevel@tonic-gate 			}
5240Sstevel@tonic-gate 		} else if ((dfnp->fn_dirents == NULL) &&
5250Sstevel@tonic-gate 		    ((dvp->v_flag & VROOT) == 0) &&
5260Sstevel@tonic-gate 		    ((fntovn(dfnp->fn_parent))->v_flag & VROOT)) {
5270Sstevel@tonic-gate 			/*
5280Sstevel@tonic-gate 			 * dfnp is the actual 'mountpoint' of indirect map,
5290Sstevel@tonic-gate 			 * it is the equivalent of a direct mount,
5300Sstevel@tonic-gate 			 * ie, /home/'user1'
5310Sstevel@tonic-gate 			 */
5320Sstevel@tonic-gate 			operation = AUTOFS_MOUNT;
5330Sstevel@tonic-gate 			VN_HOLD(fntovn(dfnp));
5340Sstevel@tonic-gate 			fnp = dfnp;
5350Sstevel@tonic-gate 			error = 0;
5360Sstevel@tonic-gate 			searchnm = dfnp->fn_name;
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	if (error == EAGAIN) {
5410Sstevel@tonic-gate 		rw_exit(&dfnp->fn_rwlock);
5420Sstevel@tonic-gate 		goto top;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 	if (error) {
5450Sstevel@tonic-gate 		rw_exit(&dfnp->fn_rwlock);
5460Sstevel@tonic-gate 		return (error);
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/*
5500Sstevel@tonic-gate 	 * We now have the actual fnnode we're interested in.
5510Sstevel@tonic-gate 	 * The 'MF_LOOKUP' indicates another thread is currently
5520Sstevel@tonic-gate 	 * performing a daemon lookup of this node, therefore we
5530Sstevel@tonic-gate 	 * wait for its completion.
5540Sstevel@tonic-gate 	 * The 'MF_INPROG' indicates another thread is currently
5550Sstevel@tonic-gate 	 * performing a daemon mount of this node, we wait for it
5560Sstevel@tonic-gate 	 * to be done if we are performing a MOUNT. We don't
5570Sstevel@tonic-gate 	 * wait for it if we are performing a LOOKUP.
5580Sstevel@tonic-gate 	 * We can release the reader/writer lock as soon as we acquire
5590Sstevel@tonic-gate 	 * the mutex, since the state of the lock can only change by
5600Sstevel@tonic-gate 	 * first acquiring the mutex.
5610Sstevel@tonic-gate 	 */
5620Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
5630Sstevel@tonic-gate 	rw_exit(&dfnp->fn_rwlock);
5640Sstevel@tonic-gate 	if ((fnp->fn_flags & MF_LOOKUP) ||
5650Sstevel@tonic-gate 	    ((operation == AUTOFS_MOUNT) && (fnp->fn_flags & MF_INPROG))) {
5660Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
5670Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
5680Sstevel@tonic-gate 		VN_RELE(fntovn(fnp));
5690Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN)
5700Sstevel@tonic-gate 			error = ENOENT;
5710Sstevel@tonic-gate 		if (error && error != EAGAIN)
5720Sstevel@tonic-gate 			return (error);
5730Sstevel@tonic-gate 		goto top;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if (operation == 0) {
5770Sstevel@tonic-gate 		/*
5780Sstevel@tonic-gate 		 * got the fnnode, check for any errors
5790Sstevel@tonic-gate 		 * on the previous operation on that node.
5800Sstevel@tonic-gate 		 */
5810Sstevel@tonic-gate 		error = fnp->fn_error;
5820Sstevel@tonic-gate 		if ((error == EINTR) || (error == EAGAIN)) {
5830Sstevel@tonic-gate 			/*
5840Sstevel@tonic-gate 			 * previous operation on this node was
5850Sstevel@tonic-gate 			 * not completed, do a lookup now.
5860Sstevel@tonic-gate 			 */
5870Sstevel@tonic-gate 			operation = AUTOFS_LOOKUP;
5880Sstevel@tonic-gate 		} else {
5890Sstevel@tonic-gate 			/*
5900Sstevel@tonic-gate 			 * previous operation completed. Return
5910Sstevel@tonic-gate 			 * a pointer to the node only if there was
5920Sstevel@tonic-gate 			 * no error.
5930Sstevel@tonic-gate 			 */
5940Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
5950Sstevel@tonic-gate 			if (!error)
5960Sstevel@tonic-gate 				*vpp = fntovn(fnp);
5970Sstevel@tonic-gate 			else
5980Sstevel@tonic-gate 				VN_RELE(fntovn(fnp));
5990Sstevel@tonic-gate 			return (error);
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	/*
6040Sstevel@tonic-gate 	 * Since I got to this point, it means I'm the one
6050Sstevel@tonic-gate 	 * responsible for triggering the mount/look-up of this node.
6060Sstevel@tonic-gate 	 */
6070Sstevel@tonic-gate 	switch (operation) {
6080Sstevel@tonic-gate 	case AUTOFS_LOOKUP:
6090Sstevel@tonic-gate 		AUTOFS_BLOCK_OTHERS(fnp, MF_LOOKUP);
6100Sstevel@tonic-gate 		fnp->fn_error = 0;
6110Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
6120Sstevel@tonic-gate 		error = auto_lookup_aux(fnp, searchnm, cred);
6130Sstevel@tonic-gate 		if (!error) {
6140Sstevel@tonic-gate 			/*
6150Sstevel@tonic-gate 			 * Return this vnode
6160Sstevel@tonic-gate 			 */
6170Sstevel@tonic-gate 			*vpp = fntovn(fnp);
6180Sstevel@tonic-gate 		} else {
6190Sstevel@tonic-gate 			/*
6200Sstevel@tonic-gate 			 * release our reference to this vnode
6210Sstevel@tonic-gate 			 * and return error
6220Sstevel@tonic-gate 			 */
6230Sstevel@tonic-gate 			VN_RELE(fntovn(fnp));
6240Sstevel@tonic-gate 		}
6250Sstevel@tonic-gate 		break;
6260Sstevel@tonic-gate 	case AUTOFS_MOUNT:
6270Sstevel@tonic-gate 		AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
6280Sstevel@tonic-gate 		fnp->fn_error = 0;
6290Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
6300Sstevel@tonic-gate 		/*
6310Sstevel@tonic-gate 		 * auto_new_mount_thread fires up a new thread which
6320Sstevel@tonic-gate 		 * calls automountd finishing up the work
6330Sstevel@tonic-gate 		 */
6340Sstevel@tonic-gate 		auto_new_mount_thread(fnp, searchnm, cred);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 		/*
6370Sstevel@tonic-gate 		 * At this point, we are simply another thread
6380Sstevel@tonic-gate 		 * waiting for the mount to complete
6390Sstevel@tonic-gate 		 */
6400Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
6410Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN)
6420Sstevel@tonic-gate 			error = ENOENT;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		/*
6450Sstevel@tonic-gate 		 * now release our reference to this vnode
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		VN_RELE(fntovn(fnp));
6480Sstevel@tonic-gate 		if (!error)
6490Sstevel@tonic-gate 			goto top;
6500Sstevel@tonic-gate 		break;
6510Sstevel@tonic-gate 	default:
6522170Sevanl 		auto_log(dfnp->fn_globals->fng_verbose,
6535302Sth199096 		    dfnp->fn_globals->fng_zoneid, CE_WARN,
6545302Sth199096 		    "auto_lookup: unknown operation %d",
6555302Sth199096 		    operation);
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_lookup: name=%s *vpp=%p return=%d\n",
6590Sstevel@tonic-gate 	    nm, (void *)*vpp, error));
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	return (error);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate static int
6650Sstevel@tonic-gate auto_create(
6660Sstevel@tonic-gate 	vnode_t *dvp,
6670Sstevel@tonic-gate 	char *nm,
6680Sstevel@tonic-gate 	vattr_t *va,
6690Sstevel@tonic-gate 	vcexcl_t excl,
6700Sstevel@tonic-gate 	int mode,
6710Sstevel@tonic-gate 	vnode_t **vpp,
6720Sstevel@tonic-gate 	cred_t *cred,
673*5331Samw 	int flag,
674*5331Samw 	caller_context_t *ct,
675*5331Samw 	vsecattr_t *vsecp)
6760Sstevel@tonic-gate {
6770Sstevel@tonic-gate 	vnode_t *newvp;
6780Sstevel@tonic-gate 	int error;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_create dvp %p nm %s\n", (void *)dvp, nm));
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
6830Sstevel@tonic-gate 		goto done;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	if (newvp != NULL) {
6860Sstevel@tonic-gate 		/*
6870Sstevel@tonic-gate 		 * Node is now mounted on.
6880Sstevel@tonic-gate 		 */
6890Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
6900Sstevel@tonic-gate 			error = EROFS;
6910Sstevel@tonic-gate 		else
6920Sstevel@tonic-gate 			error = VOP_CREATE(newvp, nm, va, excl,
693*5331Samw 			    mode, vpp, cred, flag, ct, vsecp);
6940Sstevel@tonic-gate 		VN_RELE(newvp);
6950Sstevel@tonic-gate 	} else
6960Sstevel@tonic-gate 		error = ENOSYS;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate done:
6990Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_create: error=%d\n", error));
7000Sstevel@tonic-gate 	return (error);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate static int
704*5331Samw auto_remove(
705*5331Samw 	vnode_t *dvp,
706*5331Samw 	char *nm,
707*5331Samw 	cred_t *cred,
708*5331Samw 	caller_context_t *ct,
709*5331Samw 	int flags)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	vnode_t *newvp;
7120Sstevel@tonic-gate 	int error;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_remove dvp %p nm %s\n", (void *)dvp, nm));
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
7170Sstevel@tonic-gate 		goto done;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	if (newvp != NULL) {
7200Sstevel@tonic-gate 		/*
7210Sstevel@tonic-gate 		 * Node is now mounted on.
7220Sstevel@tonic-gate 		 */
7230Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
7240Sstevel@tonic-gate 			error = EROFS;
7250Sstevel@tonic-gate 		else
726*5331Samw 			error = VOP_REMOVE(newvp, nm, cred, ct, flags);
7270Sstevel@tonic-gate 		VN_RELE(newvp);
7280Sstevel@tonic-gate 	} else
7290Sstevel@tonic-gate 		error = ENOSYS;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate done:
7320Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_remove: error=%d\n", error));
7330Sstevel@tonic-gate 	return (error);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static int
737*5331Samw auto_link(
738*5331Samw 	vnode_t *tdvp,
739*5331Samw 	vnode_t *svp,
740*5331Samw 	char *nm,
741*5331Samw 	cred_t *cred,
742*5331Samw 	caller_context_t *ct,
743*5331Samw 	int flags)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate 	vnode_t *newvp;
7460Sstevel@tonic-gate 	int error;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_link tdvp %p svp %p nm %s\n", (void *)tdvp,
7490Sstevel@tonic-gate 	    (void *)svp, nm));
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	if (error = auto_trigger_mount(tdvp, cred, &newvp))
7520Sstevel@tonic-gate 		goto done;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	if (newvp == NULL) {
7550Sstevel@tonic-gate 		/*
7560Sstevel@tonic-gate 		 * an autonode can not be a link to another node
7570Sstevel@tonic-gate 		 */
7580Sstevel@tonic-gate 		error = ENOSYS;
7590Sstevel@tonic-gate 		goto done;
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	if (vn_is_readonly(newvp)) {
7630Sstevel@tonic-gate 		error = EROFS;
7640Sstevel@tonic-gate 		VN_RELE(newvp);
7650Sstevel@tonic-gate 		goto done;
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	if (vn_matchops(svp, auto_vnodeops)) {
7690Sstevel@tonic-gate 		/*
7700Sstevel@tonic-gate 		 * source vp can't be an autonode
7710Sstevel@tonic-gate 		 */
7720Sstevel@tonic-gate 		error = ENOSYS;
7730Sstevel@tonic-gate 		VN_RELE(newvp);
7740Sstevel@tonic-gate 		goto done;
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 
777*5331Samw 	error = VOP_LINK(newvp, svp, nm, cred, ct, flags);
7780Sstevel@tonic-gate 	VN_RELE(newvp);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate done:
7810Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_link error=%d\n", error));
7820Sstevel@tonic-gate 	return (error);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate static int
786*5331Samw auto_rename(
787*5331Samw 	vnode_t *odvp,
788*5331Samw 	char *onm,
789*5331Samw 	vnode_t *ndvp,
790*5331Samw 	char *nnm,
791*5331Samw 	cred_t *cr,
792*5331Samw 	caller_context_t *ct,
793*5331Samw 	int flags)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	vnode_t *o_newvp, *n_newvp;
7960Sstevel@tonic-gate 	int error;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_rename odvp %p onm %s to ndvp %p nnm %s\n",
7990Sstevel@tonic-gate 	    (void *)odvp, onm, (void *)ndvp, nnm));
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	/*
8020Sstevel@tonic-gate 	 * we know odvp is an autonode, otherwise this function
8030Sstevel@tonic-gate 	 * could not have ever been called.
8040Sstevel@tonic-gate 	 */
8050Sstevel@tonic-gate 	ASSERT(vn_matchops(odvp, auto_vnodeops));
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	if (error = auto_trigger_mount(odvp, cr, &o_newvp))
8080Sstevel@tonic-gate 		goto done;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (o_newvp == NULL) {
8110Sstevel@tonic-gate 		/*
8120Sstevel@tonic-gate 		 * can't rename an autonode
8130Sstevel@tonic-gate 		 */
8140Sstevel@tonic-gate 		error = ENOSYS;
8150Sstevel@tonic-gate 		goto done;
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	if (vn_matchops(ndvp, auto_vnodeops)) {
8190Sstevel@tonic-gate 		/*
8200Sstevel@tonic-gate 		 * directory is AUTOFS, need to trigger the
8210Sstevel@tonic-gate 		 * mount of the real filesystem.
8220Sstevel@tonic-gate 		 */
8230Sstevel@tonic-gate 		if (error = auto_trigger_mount(ndvp, cr, &n_newvp)) {
8240Sstevel@tonic-gate 			VN_RELE(o_newvp);
8250Sstevel@tonic-gate 			goto done;
8260Sstevel@tonic-gate 		}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 		if (n_newvp == NULL) {
8290Sstevel@tonic-gate 			/*
8300Sstevel@tonic-gate 			 * target can't be an autonode
8310Sstevel@tonic-gate 			 */
8320Sstevel@tonic-gate 			error = ENOSYS;
8330Sstevel@tonic-gate 			VN_RELE(o_newvp);
8340Sstevel@tonic-gate 			goto done;
8350Sstevel@tonic-gate 		}
8360Sstevel@tonic-gate 	} else {
8370Sstevel@tonic-gate 		/*
8380Sstevel@tonic-gate 		 * destination directory mount had been
8390Sstevel@tonic-gate 		 * triggered prior to the call to this function.
8400Sstevel@tonic-gate 		 */
8410Sstevel@tonic-gate 		n_newvp = ndvp;
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	ASSERT(!vn_matchops(n_newvp, auto_vnodeops));
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	if (vn_is_readonly(n_newvp)) {
8470Sstevel@tonic-gate 		error = EROFS;
8480Sstevel@tonic-gate 		VN_RELE(o_newvp);
8490Sstevel@tonic-gate 		if (n_newvp != ndvp)
8500Sstevel@tonic-gate 			VN_RELE(n_newvp);
8510Sstevel@tonic-gate 		goto done;
8520Sstevel@tonic-gate 	}
8530Sstevel@tonic-gate 
854*5331Samw 	error = VOP_RENAME(o_newvp, onm, n_newvp, nnm, cr, ct, flags);
8550Sstevel@tonic-gate 	VN_RELE(o_newvp);
8560Sstevel@tonic-gate 	if (n_newvp != ndvp)
8570Sstevel@tonic-gate 		VN_RELE(n_newvp);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate done:
8600Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_rename error=%d\n", error));
8610Sstevel@tonic-gate 	return (error);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate static int
865*5331Samw auto_mkdir(
866*5331Samw 	vnode_t *dvp,
867*5331Samw 	char *nm,
868*5331Samw 	vattr_t *va,
869*5331Samw 	vnode_t **vpp,
870*5331Samw 	cred_t *cred,
871*5331Samw 	caller_context_t *ct,
872*5331Samw 	int flags,
873*5331Samw 	vsecattr_t *vsecp)
8740Sstevel@tonic-gate {
8750Sstevel@tonic-gate 	vnode_t *newvp;
8760Sstevel@tonic-gate 	int error;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_mkdir dvp %p nm %s\n", (void *)dvp, nm));
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
8810Sstevel@tonic-gate 		goto done;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (newvp != NULL) {
8840Sstevel@tonic-gate 		/*
8850Sstevel@tonic-gate 		 * Node is now mounted on.
8860Sstevel@tonic-gate 		 */
8870Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
8880Sstevel@tonic-gate 			error = EROFS;
8890Sstevel@tonic-gate 		else
890*5331Samw 			error = VOP_MKDIR(newvp, nm, va, vpp, cred, ct,
891*5331Samw 			    flags, vsecp);
8920Sstevel@tonic-gate 		VN_RELE(newvp);
8930Sstevel@tonic-gate 	} else
8940Sstevel@tonic-gate 		error = ENOSYS;
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate done:
8970Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mkdir: error=%d\n", error));
8980Sstevel@tonic-gate 	return (error);
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate static int
902*5331Samw auto_rmdir(
903*5331Samw 	vnode_t *dvp,
904*5331Samw 	char *nm,
905*5331Samw 	vnode_t *cdir,
906*5331Samw 	cred_t *cred,
907*5331Samw 	caller_context_t *ct,
908*5331Samw 	int flags)
9090Sstevel@tonic-gate {
9100Sstevel@tonic-gate 	vnode_t *newvp;
9110Sstevel@tonic-gate 	int error;
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_rmdir: vp=%p nm=%s\n", (void *)dvp, nm));
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
9160Sstevel@tonic-gate 		goto done;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	if (newvp != NULL) {
9190Sstevel@tonic-gate 		/*
9200Sstevel@tonic-gate 		 * Node is now mounted on.
9210Sstevel@tonic-gate 		 */
9220Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
9230Sstevel@tonic-gate 			error = EROFS;
9240Sstevel@tonic-gate 		else
925*5331Samw 			error = VOP_RMDIR(newvp, nm, cdir, cred, ct, flags);
9260Sstevel@tonic-gate 		VN_RELE(newvp);
9270Sstevel@tonic-gate 	} else
9280Sstevel@tonic-gate 		error = ENOSYS;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate done:
9310Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_rmdir: error=%d\n", error));
9320Sstevel@tonic-gate 	return (error);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate static int autofs_nobrowse = 0;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate #ifdef nextdp
9380Sstevel@tonic-gate #undef nextdp
9390Sstevel@tonic-gate #endif
9400Sstevel@tonic-gate #define	nextdp(dp)	((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
9410Sstevel@tonic-gate 
942*5331Samw /* ARGSUSED */
9430Sstevel@tonic-gate static int
944*5331Samw auto_readdir(
945*5331Samw 	vnode_t *vp,
946*5331Samw 	uio_t *uiop,
947*5331Samw 	cred_t *cred,
948*5331Samw 	int *eofp,
949*5331Samw 	caller_context_t *ct,
950*5331Samw 	int flags)
9510Sstevel@tonic-gate {
9522170Sevanl 	struct autofs_rddirargs	rda;
9532170Sevanl 	autofs_rddirres rd;
9540Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
9550Sstevel@tonic-gate 	fnnode_t *cfnp, *nfnp;
9560Sstevel@tonic-gate 	dirent64_t *dp;
9570Sstevel@tonic-gate 	ulong_t offset;
9580Sstevel@tonic-gate 	ulong_t outcount = 0, count = 0;
9590Sstevel@tonic-gate 	size_t namelen;
9600Sstevel@tonic-gate 	ulong_t alloc_count;
9612170Sevanl 	void *outbuf = NULL;
9620Sstevel@tonic-gate 	fninfo_t *fnip = vfstofni(vp->v_vfsp);
9630Sstevel@tonic-gate 	struct iovec *iovp;
9640Sstevel@tonic-gate 	int error = 0;
9650Sstevel@tonic-gate 	int reached_max = 0;
9660Sstevel@tonic-gate 	int myeof = 0;
9670Sstevel@tonic-gate 	int this_reclen;
9682170Sevanl 	struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_readdir vp=%p offset=%lld\n",
9710Sstevel@tonic-gate 	    (void *)vp, uiop->uio_loffset));
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	if (eofp != NULL)
9740Sstevel@tonic-gate 		*eofp = 0;
9750Sstevel@tonic-gate 
9762170Sevanl 	if (uiop->uio_iovcnt != 1)
9772170Sevanl 		return (EINVAL);
9782170Sevanl 
9790Sstevel@tonic-gate 	iovp = uiop->uio_iov;
9800Sstevel@tonic-gate 	alloc_count = iovp->iov_len;
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	gethrestime(&fnp->fn_atime);
9830Sstevel@tonic-gate 	fnp->fn_ref_time = fnp->fn_atime.tv_sec;
9840Sstevel@tonic-gate 
9852170Sevanl 	dp = outbuf = kmem_zalloc(alloc_count, KM_SLEEP);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	/*
9880Sstevel@tonic-gate 	 * Held when getdents calls VOP_RWLOCK....
9890Sstevel@tonic-gate 	 */
9900Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&fnp->fn_rwlock));
9910Sstevel@tonic-gate 	if (uiop->uio_offset >= AUTOFS_DAEMONCOOKIE) {
9920Sstevel@tonic-gate again:
9930Sstevel@tonic-gate 		/*
9940Sstevel@tonic-gate 		 * Do readdir of daemon contents only
9950Sstevel@tonic-gate 		 * Drop readers lock and reacquire after reply.
9960Sstevel@tonic-gate 		 */
9970Sstevel@tonic-gate 		rw_exit(&fnp->fn_rwlock);
9982170Sevanl 		bzero(&rd, sizeof (struct autofs_rddirres));
9990Sstevel@tonic-gate 		count = 0;
10000Sstevel@tonic-gate 		rda.rda_map = fnip->fi_map;
10010Sstevel@tonic-gate 		rda.rda_offset = (uint_t)uiop->uio_offset;
10020Sstevel@tonic-gate 		rd.rd_rddir.rddir_entries = dp;
10030Sstevel@tonic-gate 		rda.rda_count = rd.rd_rddir.rddir_size = (uint_t)alloc_count;
10043391Ssemery 		rda.uid = crgetuid(cred);
10052170Sevanl 
10062170Sevanl 		error = auto_calldaemon(fngp->fng_zoneid,
10075302Sth199096 		    AUTOFS_READDIR,
10085302Sth199096 		    xdr_autofs_rddirargs,
10095302Sth199096 		    &rda,
10105302Sth199096 		    xdr_autofs_rddirres,
10115302Sth199096 		    (void *)&rd,
10125302Sth199096 		    sizeof (autofs_rddirres),
10135302Sth199096 		    TRUE);
10142170Sevanl 
10150Sstevel@tonic-gate 		/*
10160Sstevel@tonic-gate 		 * reacquire previously dropped lock
10170Sstevel@tonic-gate 		 */
10180Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_READER);
10190Sstevel@tonic-gate 
10202170Sevanl 		if (!error) {
10210Sstevel@tonic-gate 			error = rd.rd_status;
10222170Sevanl 			dp = rd.rd_rddir.rddir_entries;
10232170Sevanl 		}
10242170Sevanl 
10250Sstevel@tonic-gate 		if (error) {
10260Sstevel@tonic-gate 			if (error == AUTOFS_SHUTDOWN) {
10270Sstevel@tonic-gate 				/*
10280Sstevel@tonic-gate 				 * treat as empty directory
10290Sstevel@tonic-gate 				 */
10300Sstevel@tonic-gate 				error = 0;
10310Sstevel@tonic-gate 				myeof = 1;
10320Sstevel@tonic-gate 				if (eofp)
10330Sstevel@tonic-gate 					*eofp = 1;
10340Sstevel@tonic-gate 			}
10350Sstevel@tonic-gate 			goto done;
10360Sstevel@tonic-gate 		}
10370Sstevel@tonic-gate 		if (rd.rd_rddir.rddir_size) {
10382170Sevanl 			dirent64_t *odp = dp;   /* next in output buffer */
10392170Sevanl 			dirent64_t *cdp = dp;   /* current examined entry */
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 			/*
10420Sstevel@tonic-gate 			 * Check for duplicates here
10430Sstevel@tonic-gate 			 */
10440Sstevel@tonic-gate 			do {
10450Sstevel@tonic-gate 				this_reclen = cdp->d_reclen;
10462170Sevanl 				if (auto_search(fnp, cdp->d_name,
10475302Sth199096 				    NULL, cred)) {
10480Sstevel@tonic-gate 					/*
10490Sstevel@tonic-gate 					 * entry not found in kernel list,
10500Sstevel@tonic-gate 					 * include it in readdir output.
10510Sstevel@tonic-gate 					 *
10520Sstevel@tonic-gate 					 * If we are skipping entries. then
10530Sstevel@tonic-gate 					 * we need to copy this entry to the
10540Sstevel@tonic-gate 					 * correct position in the buffer
10550Sstevel@tonic-gate 					 * to be copied out.
10560Sstevel@tonic-gate 					 */
10570Sstevel@tonic-gate 					if (cdp != odp)
10580Sstevel@tonic-gate 						bcopy(cdp, odp,
10595302Sth199096 						    (size_t)this_reclen);
10600Sstevel@tonic-gate 					odp = nextdp(odp);
10610Sstevel@tonic-gate 					outcount += this_reclen;
10620Sstevel@tonic-gate 				} else {
10630Sstevel@tonic-gate 					/*
10640Sstevel@tonic-gate 					 * Entry was found in the kernel
10650Sstevel@tonic-gate 					 * list. If it is the first entry
10660Sstevel@tonic-gate 					 * in this buffer, then just skip it
10670Sstevel@tonic-gate 					 */
10680Sstevel@tonic-gate 					if (odp == dp) {
10690Sstevel@tonic-gate 						dp = nextdp(dp);
10700Sstevel@tonic-gate 						odp = dp;
10710Sstevel@tonic-gate 					}
10720Sstevel@tonic-gate 				}
10730Sstevel@tonic-gate 				count += this_reclen;
10740Sstevel@tonic-gate 				cdp = (struct dirent64 *)
10755302Sth199096 				    ((char *)cdp + this_reclen);
10760Sstevel@tonic-gate 			} while (count < rd.rd_rddir.rddir_size);
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 			if (outcount)
10790Sstevel@tonic-gate 				error = uiomove(dp, outcount, UIO_READ, uiop);
10800Sstevel@tonic-gate 			uiop->uio_offset = rd.rd_rddir.rddir_offset;
10810Sstevel@tonic-gate 		} else {
10820Sstevel@tonic-gate 			if (rd.rd_rddir.rddir_eof == 0) {
10830Sstevel@tonic-gate 				/*
10840Sstevel@tonic-gate 				 * alloc_count not large enough for one
10850Sstevel@tonic-gate 				 * directory entry
10860Sstevel@tonic-gate 				 */
10870Sstevel@tonic-gate 				error = EINVAL;
10880Sstevel@tonic-gate 			}
10890Sstevel@tonic-gate 		}
10900Sstevel@tonic-gate 		if (rd.rd_rddir.rddir_eof && !error) {
10910Sstevel@tonic-gate 			myeof = 1;
10920Sstevel@tonic-gate 			if (eofp)
10930Sstevel@tonic-gate 				*eofp = 1;
10940Sstevel@tonic-gate 		}
10950Sstevel@tonic-gate 		if (!error && !myeof && outcount == 0) {
10960Sstevel@tonic-gate 			/*
10970Sstevel@tonic-gate 			 * call daemon with new cookie, all previous
10980Sstevel@tonic-gate 			 * elements happened to be duplicates
10990Sstevel@tonic-gate 			 */
11000Sstevel@tonic-gate 			dp = outbuf;
11010Sstevel@tonic-gate 			goto again;
11020Sstevel@tonic-gate 		}
11030Sstevel@tonic-gate 		goto done;
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	if (uiop->uio_offset == 0) {
11070Sstevel@tonic-gate 		/*
11080Sstevel@tonic-gate 		 * first time: so fudge the . and ..
11090Sstevel@tonic-gate 		 */
11100Sstevel@tonic-gate 		this_reclen = DIRENT64_RECLEN(1);
11110Sstevel@tonic-gate 		if (alloc_count < this_reclen) {
11120Sstevel@tonic-gate 			error = EINVAL;
11130Sstevel@tonic-gate 			goto done;
11140Sstevel@tonic-gate 		}
11150Sstevel@tonic-gate 		dp->d_ino = (ino64_t)fnp->fn_nodeid;
11160Sstevel@tonic-gate 		dp->d_off = (off64_t)1;
11170Sstevel@tonic-gate 		dp->d_reclen = (ushort_t)this_reclen;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 		(void) strncpy(dp->d_name, ".",
11225302Sth199096 		    DIRENT64_NAMELEN(this_reclen));
11230Sstevel@tonic-gate 		outcount += dp->d_reclen;
11240Sstevel@tonic-gate 		dp = nextdp(dp);
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 		this_reclen = DIRENT64_RECLEN(2);
11270Sstevel@tonic-gate 		if (alloc_count < outcount + this_reclen) {
11280Sstevel@tonic-gate 			error = EINVAL;
11290Sstevel@tonic-gate 			goto done;
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 		dp->d_reclen = (ushort_t)this_reclen;
11320Sstevel@tonic-gate 		dp->d_ino = (ino64_t)fnp->fn_parent->fn_nodeid;
11330Sstevel@tonic-gate 		dp->d_off = (off64_t)2;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 		(void) strncpy(dp->d_name, "..",
11385302Sth199096 		    DIRENT64_NAMELEN(this_reclen));
11390Sstevel@tonic-gate 		outcount += dp->d_reclen;
11400Sstevel@tonic-gate 		dp = nextdp(dp);
11410Sstevel@tonic-gate 	}
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	offset = 2;
11440Sstevel@tonic-gate 	cfnp = fnp->fn_dirents;
11450Sstevel@tonic-gate 	while (cfnp != NULL) {
11460Sstevel@tonic-gate 		nfnp = cfnp->fn_next;
11470Sstevel@tonic-gate 		offset = cfnp->fn_offset;
11480Sstevel@tonic-gate 		if ((offset >= uiop->uio_offset) &&
11495302Sth199096 		    (!(cfnp->fn_flags & MF_LOOKUP))) {
11500Sstevel@tonic-gate 			int reclen;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 			/*
11530Sstevel@tonic-gate 			 * include node only if its offset is greater or
11540Sstevel@tonic-gate 			 * equal to the one required and it is not in
11550Sstevel@tonic-gate 			 * transient state (not being looked-up)
11560Sstevel@tonic-gate 			 */
11570Sstevel@tonic-gate 			namelen = strlen(cfnp->fn_name);
11580Sstevel@tonic-gate 			reclen = (int)DIRENT64_RECLEN(namelen);
11590Sstevel@tonic-gate 			if (outcount + reclen > alloc_count) {
11600Sstevel@tonic-gate 				reached_max = 1;
11610Sstevel@tonic-gate 				break;
11620Sstevel@tonic-gate 			}
11630Sstevel@tonic-gate 			dp->d_reclen = (ushort_t)reclen;
11640Sstevel@tonic-gate 			dp->d_ino = (ino64_t)cfnp->fn_nodeid;
11650Sstevel@tonic-gate 			if (nfnp != NULL) {
11660Sstevel@tonic-gate 				/*
11670Sstevel@tonic-gate 				 * get the offset of the next element
11680Sstevel@tonic-gate 				 */
11690Sstevel@tonic-gate 				dp->d_off = (off64_t)nfnp->fn_offset;
11700Sstevel@tonic-gate 			} else {
11710Sstevel@tonic-gate 				/*
11720Sstevel@tonic-gate 				 * This is the last element, make
11730Sstevel@tonic-gate 				 * offset one plus the current
11740Sstevel@tonic-gate 				 */
11750Sstevel@tonic-gate 				dp->d_off = (off64_t)cfnp->fn_offset + 1;
11760Sstevel@tonic-gate 			}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 			/* use strncpy(9f) to zero out uninitialized bytes */
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 			(void) strncpy(dp->d_name, cfnp->fn_name,
11815302Sth199096 			    DIRENT64_NAMELEN(reclen));
11820Sstevel@tonic-gate 			outcount += dp->d_reclen;
11830Sstevel@tonic-gate 			dp = nextdp(dp);
11840Sstevel@tonic-gate 		}
11850Sstevel@tonic-gate 		cfnp = nfnp;
11860Sstevel@tonic-gate 	}
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	if (outcount)
11890Sstevel@tonic-gate 		error = uiomove(outbuf, outcount, UIO_READ, uiop);
11905302Sth199096 
11910Sstevel@tonic-gate 	if (!error) {
11920Sstevel@tonic-gate 		if (reached_max) {
11930Sstevel@tonic-gate 			/*
11940Sstevel@tonic-gate 			 * This entry did not get added to the buffer on this,
11950Sstevel@tonic-gate 			 * call. We need to add it on the next call therefore
11960Sstevel@tonic-gate 			 * set uio_offset to this entry's offset.  If there
11970Sstevel@tonic-gate 			 * wasn't enough space for one dirent, return EINVAL.
11980Sstevel@tonic-gate 			 */
11990Sstevel@tonic-gate 			uiop->uio_offset = offset;
12000Sstevel@tonic-gate 			if (outcount == 0)
12010Sstevel@tonic-gate 				error = EINVAL;
12020Sstevel@tonic-gate 		} else if (autofs_nobrowse ||
12035302Sth199096 		    auto_nobrowse_option(fnip->fi_opts) ||
12045302Sth199096 		    (fnip->fi_flags & MF_DIRECT) ||
12055302Sth199096 		    (fnp->fn_trigger != NULL) ||
12065302Sth199096 		    (((vp->v_flag & VROOT) == 0) &&
12075302Sth199096 		    ((fntovn(fnp->fn_parent))->v_flag & VROOT) &&
12085302Sth199096 		    (fnp->fn_dirents == NULL))) {
12090Sstevel@tonic-gate 			/*
12100Sstevel@tonic-gate 			 * done reading directory entries
12110Sstevel@tonic-gate 			 */
12120Sstevel@tonic-gate 			uiop->uio_offset = offset + 1;
12130Sstevel@tonic-gate 			if (eofp)
12140Sstevel@tonic-gate 				*eofp = 1;
12150Sstevel@tonic-gate 		} else {
12160Sstevel@tonic-gate 			/*
12170Sstevel@tonic-gate 			 * Need to get the rest of the entries from the daemon.
12180Sstevel@tonic-gate 			 */
12190Sstevel@tonic-gate 			uiop->uio_offset = AUTOFS_DAEMONCOOKIE;
12200Sstevel@tonic-gate 		}
12210Sstevel@tonic-gate 	}
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate done:
12240Sstevel@tonic-gate 	kmem_free(outbuf, alloc_count);
12250Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_readdir vp=%p offset=%lld eof=%d\n",
12265302Sth199096 	    (void *)vp, uiop->uio_loffset, myeof));
12270Sstevel@tonic-gate 	return (error);
12280Sstevel@tonic-gate }
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate static int
12310Sstevel@tonic-gate auto_symlink(
12320Sstevel@tonic-gate 	vnode_t *dvp,
12330Sstevel@tonic-gate 	char *lnknm,		/* new entry */
12340Sstevel@tonic-gate 	vattr_t *tva,
12350Sstevel@tonic-gate 	char *tnm,		/* existing entry */
1236*5331Samw 	cred_t *cred,
1237*5331Samw 	caller_context_t *ct,
1238*5331Samw 	int flags)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate 	vnode_t *newvp;
12410Sstevel@tonic-gate 	int error;
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_symlink: dvp=%p lnknm=%s tnm=%s\n",
12440Sstevel@tonic-gate 	    (void *)dvp, lnknm, tnm));
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	if (error = auto_trigger_mount(dvp, cred, &newvp))
12470Sstevel@tonic-gate 		goto done;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	if (newvp != NULL) {
12500Sstevel@tonic-gate 		/*
12510Sstevel@tonic-gate 		 * Node is mounted on.
12520Sstevel@tonic-gate 		 */
12530Sstevel@tonic-gate 		if (vn_is_readonly(newvp))
12540Sstevel@tonic-gate 			error = EROFS;
12550Sstevel@tonic-gate 		else
1256*5331Samw 			error = VOP_SYMLINK(newvp, lnknm, tva, tnm, cred,
1257*5331Samw 			    ct, flags);
12580Sstevel@tonic-gate 		VN_RELE(newvp);
12590Sstevel@tonic-gate 	} else
12600Sstevel@tonic-gate 		error = ENOSYS;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate done:
12630Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_symlink: error=%d\n", error));
12640Sstevel@tonic-gate 	return (error);
12650Sstevel@tonic-gate }
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate /* ARGSUSED */
12680Sstevel@tonic-gate static int
1269*5331Samw auto_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
12700Sstevel@tonic-gate {
12710Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
12720Sstevel@tonic-gate 	int error;
12730Sstevel@tonic-gate 	timestruc_t now;
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_readlink: vp=%p\n", (void *)vp));
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	gethrestime(&now);
12780Sstevel@tonic-gate 	fnp->fn_ref_time = now.tv_sec;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	if (vp->v_type != VLNK)
12810Sstevel@tonic-gate 		error = EINVAL;
12820Sstevel@tonic-gate 	else {
12830Sstevel@tonic-gate 		ASSERT(!(fnp->fn_flags & (MF_INPROG | MF_LOOKUP)));
12840Sstevel@tonic-gate 		fnp->fn_atime = now;
12850Sstevel@tonic-gate 		error = uiomove(fnp->fn_symlink, MIN(fnp->fn_symlinklen,
12860Sstevel@tonic-gate 		    uiop->uio_resid), UIO_READ, uiop);
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_readlink: error=%d\n", error));
12900Sstevel@tonic-gate 	return (error);
12910Sstevel@tonic-gate }
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate /* ARGSUSED */
12940Sstevel@tonic-gate static int
1295*5331Samw auto_fsync(vnode_t *cp, int syncflag, cred_t *cred, caller_context_t *ct)
12960Sstevel@tonic-gate {
12970Sstevel@tonic-gate 	return (0);
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate /* ARGSUSED */
13010Sstevel@tonic-gate static void
1302*5331Samw auto_inactive(vnode_t *vp, cred_t *cred, caller_context_t *ct)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
13050Sstevel@tonic-gate 	fnnode_t *dfnp = fnp->fn_parent;
13060Sstevel@tonic-gate 	int count;
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_inactive: vp=%p v_count=%u fn_link=%d\n",
13090Sstevel@tonic-gate 	    (void *)vp, vp->v_count, fnp->fn_linkcnt));
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	/*
13120Sstevel@tonic-gate 	 * The rwlock should not be already held by this thread.
13130Sstevel@tonic-gate 	 * The assert relies on the fact that the owner field is cleared
13140Sstevel@tonic-gate 	 * when the lock is released.
13150Sstevel@tonic-gate 	 */
13160Sstevel@tonic-gate 	ASSERT(dfnp != NULL);
13170Sstevel@tonic-gate 	ASSERT(rw_owner(&dfnp->fn_rwlock) != curthread);
13180Sstevel@tonic-gate 	rw_enter(&dfnp->fn_rwlock, RW_WRITER);
13190Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
13200Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
13210Sstevel@tonic-gate 	count = --vp->v_count;
13220Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
13230Sstevel@tonic-gate 	if (count == 0) {
13240Sstevel@tonic-gate 		/*
13250Sstevel@tonic-gate 		 * Free only if node has no subdirectories.
13260Sstevel@tonic-gate 		 */
13270Sstevel@tonic-gate 		if (fnp->fn_linkcnt == 1) {
13280Sstevel@tonic-gate 			auto_disconnect(dfnp, fnp);
13290Sstevel@tonic-gate 			rw_exit(&dfnp->fn_rwlock);
13300Sstevel@tonic-gate 			auto_freefnnode(fnp);
13310Sstevel@tonic-gate 			AUTOFS_DPRINT((5, "auto_inactive: (exit) vp=%p freed\n",
13320Sstevel@tonic-gate 			    (void *)vp));
13330Sstevel@tonic-gate 			return;
13340Sstevel@tonic-gate 		}
13350Sstevel@tonic-gate 	}
13360Sstevel@tonic-gate 	rw_exit(&dfnp->fn_rwlock);
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_inactive: (exit) vp=%p v_count=%u fn_link=%d\n",
13390Sstevel@tonic-gate 	    (void *)vp, vp->v_count, fnp->fn_linkcnt));
13400Sstevel@tonic-gate }
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate /* ARGSUSED2 */
13430Sstevel@tonic-gate static int
13440Sstevel@tonic-gate auto_rwlock(vnode_t *vp, int write_lock, caller_context_t *ct)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
13470Sstevel@tonic-gate 	if (write_lock)
13480Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_WRITER);
13490Sstevel@tonic-gate 	else
13500Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_READER);
13510Sstevel@tonic-gate 	return (write_lock);
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate /* ARGSUSED */
13550Sstevel@tonic-gate static void
13560Sstevel@tonic-gate auto_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ct)
13570Sstevel@tonic-gate {
13580Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
13590Sstevel@tonic-gate 	rw_exit(&fnp->fn_rwlock);
13600Sstevel@tonic-gate }
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate /* ARGSUSED */
13640Sstevel@tonic-gate static int
1365*5331Samw auto_seek(
1366*5331Samw 	struct vnode *vp,
1367*5331Samw 	offset_t ooff,
1368*5331Samw 	offset_t *noffp,
1369*5331Samw 	caller_context_t *ct)
13700Sstevel@tonic-gate {
13710Sstevel@tonic-gate 	/*
13720Sstevel@tonic-gate 	 * Return 0 unconditionally, since we expect
13730Sstevel@tonic-gate 	 * a VDIR all the time
13740Sstevel@tonic-gate 	 */
13750Sstevel@tonic-gate 	return (0);
13760Sstevel@tonic-gate }
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate /*
13790Sstevel@tonic-gate  * Triggers the mount if needed. If the mount has been triggered by
13800Sstevel@tonic-gate  * another thread, it will wait for its return status, and return it.
13810Sstevel@tonic-gate  * Whether the mount is triggered by this thread, another thread, or
13820Sstevel@tonic-gate  * if the vnode was already covered, '*newvp' is a
13830Sstevel@tonic-gate  * VN_HELD vnode pointing to the root of the filesystem covering 'vp'.
13840Sstevel@tonic-gate  * If the node is not mounted on, and should not be mounted on, '*newvp'
13850Sstevel@tonic-gate  * will be NULL.
13860Sstevel@tonic-gate  * The calling routine may use '*newvp' to do the filesystem jump.
13870Sstevel@tonic-gate  */
13880Sstevel@tonic-gate static int
13890Sstevel@tonic-gate auto_trigger_mount(vnode_t *vp, cred_t *cred, vnode_t **newvp)
13900Sstevel@tonic-gate {
13910Sstevel@tonic-gate 	fnnode_t *fnp = vntofn(vp);
13920Sstevel@tonic-gate 	fninfo_t *fnip = vfstofni(vp->v_vfsp);
13930Sstevel@tonic-gate 	vnode_t *dvp;
13940Sstevel@tonic-gate 	vfs_t *vfsp;
13950Sstevel@tonic-gate 	int delayed_ind;
13960Sstevel@tonic-gate 	char name[AUTOFS_MAXPATHLEN];
13970Sstevel@tonic-gate 	int error;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_trigger_mount: vp=%p\n", (void *)vp));
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	*newvp = NULL;
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	/*
14040Sstevel@tonic-gate 	 * Cross-zone mount triggering is disallowed.
14050Sstevel@tonic-gate 	 */
14060Sstevel@tonic-gate 	if (fnip->fi_zoneid != getzoneid())
14070Sstevel@tonic-gate 		return (EPERM);	/* Not owner of mount */
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate retry:
14100Sstevel@tonic-gate 	error = 0;
14110Sstevel@tonic-gate 	delayed_ind = 0;
14120Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
14130Sstevel@tonic-gate 	while (fnp->fn_flags & (MF_LOOKUP | MF_INPROG)) {
14140Sstevel@tonic-gate 		/*
14150Sstevel@tonic-gate 		 * Mount or lookup in progress,
14160Sstevel@tonic-gate 		 * wait for it before proceeding.
14170Sstevel@tonic-gate 		 */
14180Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
14190Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
14200Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN) {
14210Sstevel@tonic-gate 			error = 0;
14220Sstevel@tonic-gate 			goto done;
14230Sstevel@tonic-gate 		}
14240Sstevel@tonic-gate 		if (error && error != EAGAIN)
14250Sstevel@tonic-gate 			goto done;
14260Sstevel@tonic-gate 		error = 0;
14270Sstevel@tonic-gate 		mutex_enter(&fnp->fn_lock);
14280Sstevel@tonic-gate 	}
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	/*
14310Sstevel@tonic-gate 	 * If the vfslock can't be acquired for the first time.
14320Sstevel@tonic-gate 	 * drop the fn_lock and retry next time in blocking mode.
14330Sstevel@tonic-gate 	 */
14340Sstevel@tonic-gate 	if (vn_vfswlock(vp)) {
14350Sstevel@tonic-gate 		/*
14360Sstevel@tonic-gate 		 * Lock held by another thread.
14370Sstevel@tonic-gate 		 * Perform blocking by dropping the
14380Sstevel@tonic-gate 		 * fn_lock.
14390Sstevel@tonic-gate 		 */
14400Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
14410Sstevel@tonic-gate 		error = vn_vfswlock_wait(vp);
14420Sstevel@tonic-gate 		if (error)
14430Sstevel@tonic-gate 			goto done;
14440Sstevel@tonic-gate 		/*
14450Sstevel@tonic-gate 		 * Because fn_lock wasn't held, the state
14460Sstevel@tonic-gate 		 * of the trigger node might have changed.
14470Sstevel@tonic-gate 		 * Need to run through the checks on trigger
14480Sstevel@tonic-gate 		 * node again.
14490Sstevel@tonic-gate 		 */
14500Sstevel@tonic-gate 		vn_vfsunlock(vp);
14510Sstevel@tonic-gate 		goto retry;
14520Sstevel@tonic-gate 	}
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	vfsp = vn_mountedvfs(vp);
14550Sstevel@tonic-gate 	if (vfsp != NULL) {
14560Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
14571153Snr123932 		error = VFS_ROOT(vfsp, newvp);
14580Sstevel@tonic-gate 		vn_vfsunlock(vp);
14590Sstevel@tonic-gate 		goto done;
14600Sstevel@tonic-gate 	} else {
14610Sstevel@tonic-gate 		vn_vfsunlock(vp);
14620Sstevel@tonic-gate 		if ((fnp->fn_flags & MF_MOUNTPOINT) &&
14630Sstevel@tonic-gate 		    fnp->fn_trigger != NULL) {
14640Sstevel@tonic-gate 			ASSERT(fnp->fn_dirents == NULL);
14650Sstevel@tonic-gate 			/*
14660Sstevel@tonic-gate 			 * The filesystem that used to sit here has been
14670Sstevel@tonic-gate 			 * forcibly unmounted.
14680Sstevel@tonic-gate 			 */
14690Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
14700Sstevel@tonic-gate 			error = EIO;
14710Sstevel@tonic-gate 			goto done;
14720Sstevel@tonic-gate 		}
14730Sstevel@tonic-gate 	}
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
14760Sstevel@tonic-gate 	dvp = fntovn(fnp->fn_parent);
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	if ((fnp->fn_dirents == NULL) &&
14790Sstevel@tonic-gate 	    ((fnip->fi_flags & MF_DIRECT) == 0) &&
14800Sstevel@tonic-gate 	    ((vp->v_flag & VROOT) == 0) &&
14810Sstevel@tonic-gate 	    (dvp->v_flag & VROOT)) {
14820Sstevel@tonic-gate 		/*
14830Sstevel@tonic-gate 		 * If the parent of this node is the root of an indirect
14840Sstevel@tonic-gate 		 * AUTOFS filesystem, this node is remountable.
14850Sstevel@tonic-gate 		 */
14860Sstevel@tonic-gate 		delayed_ind = 1;
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	if (delayed_ind ||
14900Sstevel@tonic-gate 	    ((fnip->fi_flags & MF_DIRECT) && (fnp->fn_dirents == NULL))) {
14910Sstevel@tonic-gate 		/*
14920Sstevel@tonic-gate 		 * Trigger mount since:
14930Sstevel@tonic-gate 		 * direct mountpoint with no subdirs or
14940Sstevel@tonic-gate 		 * delayed indirect.
14950Sstevel@tonic-gate 		 */
14960Sstevel@tonic-gate 		AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
14970Sstevel@tonic-gate 		fnp->fn_error = 0;
14980Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
14990Sstevel@tonic-gate 		if (delayed_ind)
15000Sstevel@tonic-gate 			(void) strcpy(name, fnp->fn_name);
15010Sstevel@tonic-gate 		else
15020Sstevel@tonic-gate 			(void) strcpy(name, ".");
15030Sstevel@tonic-gate 		fnp->fn_ref_time = gethrestime_sec();
15040Sstevel@tonic-gate 		auto_new_mount_thread(fnp, name, cred);
15050Sstevel@tonic-gate 		/*
15060Sstevel@tonic-gate 		 * At this point we're simply another thread waiting
15070Sstevel@tonic-gate 		 * for the mount to finish.
15080Sstevel@tonic-gate 		 */
15090Sstevel@tonic-gate 		error = auto_wait4mount(fnp);
15100Sstevel@tonic-gate 		if (error == EAGAIN)
15110Sstevel@tonic-gate 			goto retry;
15120Sstevel@tonic-gate 		if (error == AUTOFS_SHUTDOWN) {
15130Sstevel@tonic-gate 			error = 0;
15140Sstevel@tonic-gate 			goto done;
15150Sstevel@tonic-gate 		}
15160Sstevel@tonic-gate 		if (error == 0) {
15171153Snr123932 			if (error = vn_vfsrlock_wait(vp))
15180Sstevel@tonic-gate 				goto done;
15190Sstevel@tonic-gate 			/* Reacquire after dropping locks */
15200Sstevel@tonic-gate 			vfsp = vn_mountedvfs(vp);
15210Sstevel@tonic-gate 			if (vfsp != NULL) {
15221153Snr123932 				error = VFS_ROOT(vfsp, newvp);
15230Sstevel@tonic-gate 				vn_vfsunlock(vp);
15240Sstevel@tonic-gate 			} else {
15250Sstevel@tonic-gate 				vn_vfsunlock(vp);
15260Sstevel@tonic-gate 				goto retry;
15270Sstevel@tonic-gate 			}
15280Sstevel@tonic-gate 		}
15290Sstevel@tonic-gate 	} else
15300Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate done:
15330Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_trigger_mount: error=%d\n", error));
15340Sstevel@tonic-gate 	return (error);
15350Sstevel@tonic-gate }
1536