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