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 /*
2212184SJan.Kryl@Sun.COM * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <sys/param.h>
260Sstevel@tonic-gate #include <sys/systm.h>
270Sstevel@tonic-gate #include <sys/errno.h>
280Sstevel@tonic-gate #include <sys/proc.h>
290Sstevel@tonic-gate #include <sys/vnode.h>
300Sstevel@tonic-gate #include <sys/vfs.h>
313898Srsb #include <sys/vfs_opreg.h>
320Sstevel@tonic-gate #include <sys/uio.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/pathname.h>
350Sstevel@tonic-gate #include <sys/dirent.h>
360Sstevel@tonic-gate #include <sys/debug.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/tiuser.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include <sys/mode.h>
420Sstevel@tonic-gate #include <sys/policy.h>
430Sstevel@tonic-gate #include <rpc/types.h>
440Sstevel@tonic-gate #include <rpc/auth.h>
450Sstevel@tonic-gate #include <rpc/clnt.h>
460Sstevel@tonic-gate #include <sys/fs/autofs.h>
470Sstevel@tonic-gate #include <rpcsvc/autofs_prot.h>
480Sstevel@tonic-gate #include <fs/fs_subr.h>
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * Vnode ops for autofs
520Sstevel@tonic-gate */
535331Samw static int auto_open(vnode_t **, int, cred_t *, caller_context_t *);
545331Samw static int auto_close(vnode_t *, int, int, offset_t, cred_t *,
555331Samw caller_context_t *);
565331Samw static int auto_getattr(vnode_t *, vattr_t *, int, cred_t *,
575331Samw caller_context_t *);
580Sstevel@tonic-gate static int auto_setattr(vnode_t *, vattr_t *, int, cred_t *,
590Sstevel@tonic-gate caller_context_t *);
605331Samw static int auto_access(vnode_t *, int, int, cred_t *, caller_context_t *);
610Sstevel@tonic-gate static int auto_lookup(vnode_t *, char *, vnode_t **,
625331Samw pathname_t *, int, vnode_t *, cred_t *, caller_context_t *, int *,
635331Samw pathname_t *);
640Sstevel@tonic-gate static int auto_create(vnode_t *, char *, vattr_t *, vcexcl_t,
655331Samw int, vnode_t **, cred_t *, int, caller_context_t *, vsecattr_t *);
665331Samw static int auto_remove(vnode_t *, char *, cred_t *, caller_context_t *, int);
675331Samw static int auto_link(vnode_t *, vnode_t *, char *, cred_t *,
685331Samw caller_context_t *, int);
695331Samw static int auto_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
705331Samw caller_context_t *, int);
715331Samw static int auto_mkdir(vnode_t *, char *, vattr_t *, vnode_t **, cred_t *,
725331Samw caller_context_t *, int, vsecattr_t *);
735331Samw static int auto_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
745331Samw caller_context_t *, int);
755331Samw static int auto_readdir(vnode_t *, uio_t *, cred_t *, int *,
765331Samw caller_context_t *, int);
775331Samw static int auto_symlink(vnode_t *, char *, vattr_t *, char *, cred_t *,
785331Samw caller_context_t *, int);
795331Samw static int auto_readlink(vnode_t *, struct uio *, cred_t *,
805331Samw caller_context_t *);
815331Samw static int auto_fsync(vnode_t *, int, cred_t *, caller_context_t *);
825331Samw static void auto_inactive(vnode_t *, cred_t *, caller_context_t *);
830Sstevel@tonic-gate static int auto_rwlock(vnode_t *, int, caller_context_t *);
840Sstevel@tonic-gate static void auto_rwunlock(vnode_t *vp, int, caller_context_t *);
855331Samw static int auto_seek(vnode_t *vp, offset_t, offset_t *, caller_context_t *);
860Sstevel@tonic-gate
870Sstevel@tonic-gate static int auto_trigger_mount(vnode_t *, cred_t *, vnode_t **);
880Sstevel@tonic-gate
890Sstevel@tonic-gate vnodeops_t *auto_vnodeops;
900Sstevel@tonic-gate
910Sstevel@tonic-gate const fs_operation_def_t auto_vnodeops_template[] = {
923898Srsb VOPNAME_OPEN, { .vop_open = auto_open },
933898Srsb VOPNAME_CLOSE, { .vop_close = auto_close },
943898Srsb VOPNAME_GETATTR, { .vop_getattr = auto_getattr },
953898Srsb VOPNAME_SETATTR, { .vop_setattr = auto_setattr },
963898Srsb VOPNAME_ACCESS, { .vop_access = auto_access },
973898Srsb VOPNAME_LOOKUP, { .vop_lookup = auto_lookup },
983898Srsb VOPNAME_CREATE, { .vop_create = auto_create },
993898Srsb VOPNAME_REMOVE, { .vop_remove = auto_remove },
1003898Srsb VOPNAME_LINK, { .vop_link = auto_link },
1013898Srsb VOPNAME_RENAME, { .vop_rename = auto_rename },
1023898Srsb VOPNAME_MKDIR, { .vop_mkdir = auto_mkdir },
1033898Srsb VOPNAME_RMDIR, { .vop_rmdir = auto_rmdir },
1043898Srsb VOPNAME_READDIR, { .vop_readdir = auto_readdir },
1053898Srsb VOPNAME_SYMLINK, { .vop_symlink = auto_symlink },
1063898Srsb VOPNAME_READLINK, { .vop_readlink = auto_readlink },
1073898Srsb VOPNAME_FSYNC, { .vop_fsync = auto_fsync },
1083898Srsb VOPNAME_INACTIVE, { .vop_inactive = auto_inactive },
1093898Srsb VOPNAME_RWLOCK, { .vop_rwlock = auto_rwlock },
1103898Srsb VOPNAME_RWUNLOCK, { .vop_rwunlock = auto_rwunlock },
1113898Srsb VOPNAME_SEEK, { .vop_seek = auto_seek },
1123898Srsb VOPNAME_FRLOCK, { .error = fs_error },
1133898Srsb VOPNAME_DISPOSE, { .error = fs_error },
1143898Srsb VOPNAME_SHRLOCK, { .error = fs_error },
1154863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
1163898Srsb NULL, NULL
1170Sstevel@tonic-gate };
1180Sstevel@tonic-gate
1192170Sevanl
1202170Sevanl
1210Sstevel@tonic-gate /* ARGSUSED */
1220Sstevel@tonic-gate static int
auto_open(vnode_t ** vpp,int flag,cred_t * cred,caller_context_t * ct)1235331Samw auto_open(vnode_t **vpp, int flag, cred_t *cred, caller_context_t *ct)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate vnode_t *newvp;
1260Sstevel@tonic-gate int error;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_open: *vpp=%p\n", (void *)*vpp));
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate error = auto_trigger_mount(*vpp, cred, &newvp);
1310Sstevel@tonic-gate if (error)
1320Sstevel@tonic-gate goto done;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate if (newvp != NULL) {
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate * Node is now mounted on.
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate VN_RELE(*vpp);
1390Sstevel@tonic-gate *vpp = newvp;
1405331Samw error = VOP_ACCESS(*vpp, VREAD, 0, cred, ct);
1410Sstevel@tonic-gate if (!error)
1425331Samw error = VOP_OPEN(vpp, flag, cred, ct);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate done:
1460Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_open: *vpp=%p error=%d\n", (void *)*vpp,
1470Sstevel@tonic-gate error));
1480Sstevel@tonic-gate return (error);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate /* ARGSUSED */
1520Sstevel@tonic-gate static int
auto_close(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cred,caller_context_t * ct)1535331Samw auto_close(
1545331Samw vnode_t *vp,
1555331Samw int flag,
1565331Samw int count,
1575331Samw offset_t offset,
1585331Samw cred_t *cred,
1595331Samw caller_context_t *ct)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate return (0);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate static int
auto_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cred,caller_context_t * ct)1655331Samw auto_getattr(
1665331Samw vnode_t *vp,
1675331Samw vattr_t *vap,
1685331Samw int flags,
1695331Samw cred_t *cred,
1705331Samw caller_context_t *ct)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate fnnode_t *fnp = vntofn(vp);
1730Sstevel@tonic-gate vnode_t *newvp;
1740Sstevel@tonic-gate vfs_t *vfsp;
1750Sstevel@tonic-gate int error;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_getattr vp %p\n", (void *)vp));
1780Sstevel@tonic-gate
1795302Sth199096 if (flags & ATTR_TRIGGER) {
1805302Sth199096 /*
1815302Sth199096 * Pre-trigger the mount
1825302Sth199096 */
1835302Sth199096 error = auto_trigger_mount(vp, cred, &newvp);
1845302Sth199096 if (error)
1855302Sth199096 return (error);
1865302Sth199096
1875302Sth199096 if (newvp == NULL)
1885302Sth199096 goto defattr;
1895302Sth199096
190*12892Sdai.ngo@sun.com if (error = vn_vfsrlock_wait(vp)) {
191*12892Sdai.ngo@sun.com VN_RELE(newvp);
1925302Sth199096 return (error);
193*12892Sdai.ngo@sun.com }
194149Scasper
1955302Sth199096 vfsp = newvp->v_vfsp;
196*12892Sdai.ngo@sun.com VN_RELE(newvp);
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);
2355331Samw 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
auto_setattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cred,caller_context_t * ct)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
2965331Samw 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
auto_access(vnode_t * vp,int mode,int flags,cred_t * cred,caller_context_t * ct)3085331Samw auto_access(
3095331Samw vnode_t *vp,
3105331Samw int mode,
3115331Samw int flags,
3125331Samw cred_t *cred,
3135331Samw 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 */
3285331Samw 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 }
34312273SCasper.Dik@Sun.COM error = secpolicy_vnode_access2(cred, vp, fnp->fn_uid,
34412273SCasper.Dik@Sun.COM fnp->fn_mode << shift, mode);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate done:
3480Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_access: error=%d\n", error));
3490Sstevel@tonic-gate return (error);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate static int
auto_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,pathname_t * pnp,int flags,vnode_t * rdir,cred_t * cred,caller_context_t * ct,int * direntflags,pathname_t * realpnp)3530Sstevel@tonic-gate auto_lookup(
3540Sstevel@tonic-gate vnode_t *dvp,
3550Sstevel@tonic-gate char *nm,
3560Sstevel@tonic-gate vnode_t **vpp,
3570Sstevel@tonic-gate pathname_t *pnp,
3580Sstevel@tonic-gate int flags,
3590Sstevel@tonic-gate vnode_t *rdir,
3605331Samw cred_t *cred,
3615331Samw caller_context_t *ct,
3625331Samw int *direntflags,
3635331Samw pathname_t *realpnp)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate int error = 0;
3660Sstevel@tonic-gate vnode_t *newvp = NULL;
3670Sstevel@tonic-gate vfs_t *vfsp;
3680Sstevel@tonic-gate fninfo_t *dfnip;
3690Sstevel@tonic-gate fnnode_t *dfnp = NULL;
3700Sstevel@tonic-gate fnnode_t *fnp = NULL;
3710Sstevel@tonic-gate char *searchnm;
3720Sstevel@tonic-gate int operation; /* either AUTOFS_LOOKUP or AUTOFS_MOUNT */
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate dfnip = vfstofni(dvp->v_vfsp);
3750Sstevel@tonic-gate AUTOFS_DPRINT((3, "auto_lookup: dvp=%p (%s) name=%s\n",
3760Sstevel@tonic-gate (void *)dvp, dfnip->fi_map, nm));
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate if (nm[0] == 0) {
3790Sstevel@tonic-gate VN_HOLD(dvp);
3800Sstevel@tonic-gate *vpp = dvp;
3810Sstevel@tonic-gate return (0);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate
3845331Samw if (error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct))
3850Sstevel@tonic-gate return (error);
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate if (nm[0] == '.' && nm[1] == 0) {
3880Sstevel@tonic-gate VN_HOLD(dvp);
3890Sstevel@tonic-gate *vpp = dvp;
3900Sstevel@tonic-gate return (0);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate if (nm[0] == '.' && nm[1] == '.' && nm[2] == 0) {
3940Sstevel@tonic-gate fnnode_t *pdfnp;
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate pdfnp = (vntofn(dvp))->fn_parent;
3970Sstevel@tonic-gate ASSERT(pdfnp != NULL);
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate * Since it is legitimate to have the VROOT flag set for the
4010Sstevel@tonic-gate * subdirectories of the indirect map in autofs filesystem,
4020Sstevel@tonic-gate * rootfnnodep is checked against fnnode of dvp instead of
4030Sstevel@tonic-gate * just checking whether VROOT flag is set in dvp
4040Sstevel@tonic-gate */
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate if (pdfnp == pdfnp->fn_globals->fng_rootfnnodep) {
4070Sstevel@tonic-gate vnode_t *vp;
4080Sstevel@tonic-gate
4091153Snr123932 vfs_rlock_wait(dvp->v_vfsp);
4100Sstevel@tonic-gate if (dvp->v_vfsp->vfs_flag & VFS_UNMOUNTED) {
4110Sstevel@tonic-gate vfs_unlock(dvp->v_vfsp);
4120Sstevel@tonic-gate return (EIO);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate vp = dvp->v_vfsp->vfs_vnodecovered;
4150Sstevel@tonic-gate VN_HOLD(vp);
4160Sstevel@tonic-gate vfs_unlock(dvp->v_vfsp);
4175331Samw error = VOP_LOOKUP(vp, nm, vpp, pnp, flags, rdir, cred,
4185331Samw ct, direntflags, realpnp);
4190Sstevel@tonic-gate VN_RELE(vp);
4200Sstevel@tonic-gate return (error);
4210Sstevel@tonic-gate } else {
4220Sstevel@tonic-gate *vpp = fntovn(pdfnp);
4230Sstevel@tonic-gate VN_HOLD(*vpp);
4240Sstevel@tonic-gate return (0);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate top:
4290Sstevel@tonic-gate dfnp = vntofn(dvp);
4300Sstevel@tonic-gate searchnm = nm;
4310Sstevel@tonic-gate operation = 0;
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate ASSERT(vn_matchops(dvp, auto_vnodeops));
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate AUTOFS_DPRINT((3, "auto_lookup: dvp=%p dfnp=%p\n", (void *)dvp,
4360Sstevel@tonic-gate (void *)dfnp));
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate /*
4390Sstevel@tonic-gate * If a lookup or mount of this node is in progress, wait for it
4400Sstevel@tonic-gate * to finish, and return whatever result it got.
4410Sstevel@tonic-gate */
4420Sstevel@tonic-gate mutex_enter(&dfnp->fn_lock);
4430Sstevel@tonic-gate if (dfnp->fn_flags & (MF_LOOKUP | MF_INPROG)) {
4440Sstevel@tonic-gate mutex_exit(&dfnp->fn_lock);
4450Sstevel@tonic-gate error = auto_wait4mount(dfnp);
4460Sstevel@tonic-gate if (error == AUTOFS_SHUTDOWN)
4470Sstevel@tonic-gate error = ENOENT;
4480Sstevel@tonic-gate if (error == EAGAIN)
4490Sstevel@tonic-gate goto top;
4500Sstevel@tonic-gate if (error)
4510Sstevel@tonic-gate return (error);
4520Sstevel@tonic-gate } else
4530Sstevel@tonic-gate mutex_exit(&dfnp->fn_lock);
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate
4561153Snr123932 error = vn_vfsrlock_wait(dvp);
4570Sstevel@tonic-gate if (error)
4580Sstevel@tonic-gate return (error);
4590Sstevel@tonic-gate vfsp = vn_mountedvfs(dvp);
4600Sstevel@tonic-gate if (vfsp != NULL) {
4611153Snr123932 error = VFS_ROOT(vfsp, &newvp);
4620Sstevel@tonic-gate vn_vfsunlock(dvp);
4630Sstevel@tonic-gate if (!error) {
4640Sstevel@tonic-gate error = VOP_LOOKUP(newvp, nm, vpp, pnp,
4655331Samw flags, rdir, cred, ct, direntflags, realpnp);
4660Sstevel@tonic-gate VN_RELE(newvp);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate return (error);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate vn_vfsunlock(dvp);
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate rw_enter(&dfnp->fn_rwlock, RW_READER);
4730Sstevel@tonic-gate error = auto_search(dfnp, nm, &fnp, cred);
4740Sstevel@tonic-gate if (error) {
4750Sstevel@tonic-gate if (dfnip->fi_flags & MF_DIRECT) {
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate * direct map.
4780Sstevel@tonic-gate */
4790Sstevel@tonic-gate if (dfnp->fn_dirents) {
4800Sstevel@tonic-gate /*
4810Sstevel@tonic-gate * Mount previously triggered.
4820Sstevel@tonic-gate * 'nm' not found
4830Sstevel@tonic-gate */
4840Sstevel@tonic-gate error = ENOENT;
4850Sstevel@tonic-gate } else {
4860Sstevel@tonic-gate /*
4870Sstevel@tonic-gate * I need to contact the daemon to trigger
4880Sstevel@tonic-gate * the mount. 'dfnp' will be the mountpoint.
4890Sstevel@tonic-gate */
4900Sstevel@tonic-gate operation = AUTOFS_MOUNT;
4910Sstevel@tonic-gate VN_HOLD(fntovn(dfnp));
4920Sstevel@tonic-gate fnp = dfnp;
4930Sstevel@tonic-gate error = 0;
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate } else if (dvp == dfnip->fi_rootvp) {
4960Sstevel@tonic-gate /*
4970Sstevel@tonic-gate * 'dfnp' is the root of the indirect AUTOFS.
4980Sstevel@tonic-gate */
4990Sstevel@tonic-gate if (rw_tryupgrade(&dfnp->fn_rwlock) == 0) {
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate * Could not acquire writer lock, release
5020Sstevel@tonic-gate * reader, and wait until available. We
5030Sstevel@tonic-gate * need to search for 'nm' again, since we
5040Sstevel@tonic-gate * had to release the lock before reacquiring
5050Sstevel@tonic-gate * it.
5060Sstevel@tonic-gate */
5070Sstevel@tonic-gate rw_exit(&dfnp->fn_rwlock);
5080Sstevel@tonic-gate rw_enter(&dfnp->fn_rwlock, RW_WRITER);
5090Sstevel@tonic-gate error = auto_search(dfnp, nm, &fnp, cred);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
5130Sstevel@tonic-gate if (error) {
5140Sstevel@tonic-gate /*
5150Sstevel@tonic-gate * create node being looked-up and request
5160Sstevel@tonic-gate * mount on it.
5170Sstevel@tonic-gate */
5180Sstevel@tonic-gate error = auto_enter(dfnp, nm, &fnp, kcred);
5190Sstevel@tonic-gate if (!error)
5200Sstevel@tonic-gate operation = AUTOFS_LOOKUP;
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate } else if ((dfnp->fn_dirents == NULL) &&
5230Sstevel@tonic-gate ((dvp->v_flag & VROOT) == 0) &&
5240Sstevel@tonic-gate ((fntovn(dfnp->fn_parent))->v_flag & VROOT)) {
5250Sstevel@tonic-gate /*
5260Sstevel@tonic-gate * dfnp is the actual 'mountpoint' of indirect map,
5270Sstevel@tonic-gate * it is the equivalent of a direct mount,
5280Sstevel@tonic-gate * ie, /home/'user1'
5290Sstevel@tonic-gate */
5300Sstevel@tonic-gate operation = AUTOFS_MOUNT;
5310Sstevel@tonic-gate VN_HOLD(fntovn(dfnp));
5320Sstevel@tonic-gate fnp = dfnp;
5330Sstevel@tonic-gate error = 0;
5340Sstevel@tonic-gate searchnm = dfnp->fn_name;
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate if (error == EAGAIN) {
5390Sstevel@tonic-gate rw_exit(&dfnp->fn_rwlock);
5400Sstevel@tonic-gate goto top;
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate if (error) {
5430Sstevel@tonic-gate rw_exit(&dfnp->fn_rwlock);
5440Sstevel@tonic-gate return (error);
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate * We now have the actual fnnode we're interested in.
5490Sstevel@tonic-gate * The 'MF_LOOKUP' indicates another thread is currently
5500Sstevel@tonic-gate * performing a daemon lookup of this node, therefore we
5510Sstevel@tonic-gate * wait for its completion.
5520Sstevel@tonic-gate * The 'MF_INPROG' indicates another thread is currently
5530Sstevel@tonic-gate * performing a daemon mount of this node, we wait for it
5540Sstevel@tonic-gate * to be done if we are performing a MOUNT. We don't
5550Sstevel@tonic-gate * wait for it if we are performing a LOOKUP.
5560Sstevel@tonic-gate * We can release the reader/writer lock as soon as we acquire
5570Sstevel@tonic-gate * the mutex, since the state of the lock can only change by
5580Sstevel@tonic-gate * first acquiring the mutex.
5590Sstevel@tonic-gate */
5600Sstevel@tonic-gate mutex_enter(&fnp->fn_lock);
5610Sstevel@tonic-gate rw_exit(&dfnp->fn_rwlock);
5620Sstevel@tonic-gate if ((fnp->fn_flags & MF_LOOKUP) ||
5630Sstevel@tonic-gate ((operation == AUTOFS_MOUNT) && (fnp->fn_flags & MF_INPROG))) {
5640Sstevel@tonic-gate mutex_exit(&fnp->fn_lock);
5650Sstevel@tonic-gate error = auto_wait4mount(fnp);
5660Sstevel@tonic-gate VN_RELE(fntovn(fnp));
5670Sstevel@tonic-gate if (error == AUTOFS_SHUTDOWN)
5680Sstevel@tonic-gate error = ENOENT;
5690Sstevel@tonic-gate if (error && error != EAGAIN)
5700Sstevel@tonic-gate return (error);
5710Sstevel@tonic-gate goto top;
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate if (operation == 0) {
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate * got the fnnode, check for any errors
5770Sstevel@tonic-gate * on the previous operation on that node.
5780Sstevel@tonic-gate */
5790Sstevel@tonic-gate error = fnp->fn_error;
5800Sstevel@tonic-gate if ((error == EINTR) || (error == EAGAIN)) {
5810Sstevel@tonic-gate /*
5820Sstevel@tonic-gate * previous operation on this node was
5830Sstevel@tonic-gate * not completed, do a lookup now.
5840Sstevel@tonic-gate */
5850Sstevel@tonic-gate operation = AUTOFS_LOOKUP;
5860Sstevel@tonic-gate } else {
5870Sstevel@tonic-gate /*
5880Sstevel@tonic-gate * previous operation completed. Return
5890Sstevel@tonic-gate * a pointer to the node only if there was
5900Sstevel@tonic-gate * no error.
5910Sstevel@tonic-gate */
5920Sstevel@tonic-gate mutex_exit(&fnp->fn_lock);
5930Sstevel@tonic-gate if (!error)
5940Sstevel@tonic-gate *vpp = fntovn(fnp);
5950Sstevel@tonic-gate else
5960Sstevel@tonic-gate VN_RELE(fntovn(fnp));
5970Sstevel@tonic-gate return (error);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate * Since I got to this point, it means I'm the one
6030Sstevel@tonic-gate * responsible for triggering the mount/look-up of this node.
6040Sstevel@tonic-gate */
6050Sstevel@tonic-gate switch (operation) {
6060Sstevel@tonic-gate case AUTOFS_LOOKUP:
6070Sstevel@tonic-gate AUTOFS_BLOCK_OTHERS(fnp, MF_LOOKUP);
6080Sstevel@tonic-gate fnp->fn_error = 0;
6090Sstevel@tonic-gate mutex_exit(&fnp->fn_lock);
6100Sstevel@tonic-gate error = auto_lookup_aux(fnp, searchnm, cred);
6110Sstevel@tonic-gate if (!error) {
6120Sstevel@tonic-gate /*
6130Sstevel@tonic-gate * Return this vnode
6140Sstevel@tonic-gate */
6150Sstevel@tonic-gate *vpp = fntovn(fnp);
6160Sstevel@tonic-gate } else {
6170Sstevel@tonic-gate /*
6180Sstevel@tonic-gate * release our reference to this vnode
6190Sstevel@tonic-gate * and return error
6200Sstevel@tonic-gate */
6210Sstevel@tonic-gate VN_RELE(fntovn(fnp));
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate break;
6240Sstevel@tonic-gate case AUTOFS_MOUNT:
6250Sstevel@tonic-gate AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
6260Sstevel@tonic-gate fnp->fn_error = 0;
6270Sstevel@tonic-gate mutex_exit(&fnp->fn_lock);
6280Sstevel@tonic-gate /*
6290Sstevel@tonic-gate * auto_new_mount_thread fires up a new thread which
6300Sstevel@tonic-gate * calls automountd finishing up the work
6310Sstevel@tonic-gate */
6320Sstevel@tonic-gate auto_new_mount_thread(fnp, searchnm, cred);
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate * At this point, we are simply another thread
6360Sstevel@tonic-gate * waiting for the mount to complete
6370Sstevel@tonic-gate */
6380Sstevel@tonic-gate error = auto_wait4mount(fnp);
6390Sstevel@tonic-gate if (error == AUTOFS_SHUTDOWN)
6400Sstevel@tonic-gate error = ENOENT;
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate /*
6430Sstevel@tonic-gate * now release our reference to this vnode
6440Sstevel@tonic-gate */
6450Sstevel@tonic-gate VN_RELE(fntovn(fnp));
6460Sstevel@tonic-gate if (!error)
6470Sstevel@tonic-gate goto top;
6480Sstevel@tonic-gate break;
6490Sstevel@tonic-gate default:
6502170Sevanl auto_log(dfnp->fn_globals->fng_verbose,
6515302Sth199096 dfnp->fn_globals->fng_zoneid, CE_WARN,
6525302Sth199096 "auto_lookup: unknown operation %d",
6535302Sth199096 operation);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_lookup: name=%s *vpp=%p return=%d\n",
6570Sstevel@tonic-gate nm, (void *)*vpp, error));
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate return (error);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate static int
auto_create(vnode_t * dvp,char * nm,vattr_t * va,vcexcl_t excl,int mode,vnode_t ** vpp,cred_t * cred,int flag,caller_context_t * ct,vsecattr_t * vsecp)6630Sstevel@tonic-gate auto_create(
6640Sstevel@tonic-gate vnode_t *dvp,
6650Sstevel@tonic-gate char *nm,
6660Sstevel@tonic-gate vattr_t *va,
6670Sstevel@tonic-gate vcexcl_t excl,
6680Sstevel@tonic-gate int mode,
6690Sstevel@tonic-gate vnode_t **vpp,
6700Sstevel@tonic-gate cred_t *cred,
6715331Samw int flag,
6725331Samw caller_context_t *ct,
6735331Samw vsecattr_t *vsecp)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate vnode_t *newvp;
6760Sstevel@tonic-gate int error;
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_create dvp %p nm %s\n", (void *)dvp, nm));
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate if (error = auto_trigger_mount(dvp, cred, &newvp))
6810Sstevel@tonic-gate goto done;
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate if (newvp != NULL) {
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate * Node is now mounted on.
6860Sstevel@tonic-gate */
6870Sstevel@tonic-gate if (vn_is_readonly(newvp))
6880Sstevel@tonic-gate error = EROFS;
6890Sstevel@tonic-gate else
6900Sstevel@tonic-gate error = VOP_CREATE(newvp, nm, va, excl,
6915331Samw mode, vpp, cred, flag, ct, vsecp);
6920Sstevel@tonic-gate VN_RELE(newvp);
6930Sstevel@tonic-gate } else
6940Sstevel@tonic-gate error = ENOSYS;
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate done:
6970Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_create: error=%d\n", error));
6980Sstevel@tonic-gate return (error);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate
7010Sstevel@tonic-gate static int
auto_remove(vnode_t * dvp,char * nm,cred_t * cred,caller_context_t * ct,int flags)7025331Samw auto_remove(
7035331Samw vnode_t *dvp,
7045331Samw char *nm,
7055331Samw cred_t *cred,
7065331Samw caller_context_t *ct,
7075331Samw int flags)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate vnode_t *newvp;
7100Sstevel@tonic-gate int error;
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_remove dvp %p nm %s\n", (void *)dvp, nm));
7130Sstevel@tonic-gate
7140Sstevel@tonic-gate if (error = auto_trigger_mount(dvp, cred, &newvp))
7150Sstevel@tonic-gate goto done;
7160Sstevel@tonic-gate
7170Sstevel@tonic-gate if (newvp != NULL) {
7180Sstevel@tonic-gate /*
7190Sstevel@tonic-gate * Node is now mounted on.
7200Sstevel@tonic-gate */
7210Sstevel@tonic-gate if (vn_is_readonly(newvp))
7220Sstevel@tonic-gate error = EROFS;
7230Sstevel@tonic-gate else
7245331Samw error = VOP_REMOVE(newvp, nm, cred, ct, flags);
7250Sstevel@tonic-gate VN_RELE(newvp);
7260Sstevel@tonic-gate } else
7270Sstevel@tonic-gate error = ENOSYS;
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate done:
7300Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_remove: error=%d\n", error));
7310Sstevel@tonic-gate return (error);
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate static int
auto_link(vnode_t * tdvp,vnode_t * svp,char * nm,cred_t * cred,caller_context_t * ct,int flags)7355331Samw auto_link(
7365331Samw vnode_t *tdvp,
7375331Samw vnode_t *svp,
7385331Samw char *nm,
7395331Samw cred_t *cred,
7405331Samw caller_context_t *ct,
7415331Samw int flags)
7420Sstevel@tonic-gate {
7430Sstevel@tonic-gate vnode_t *newvp;
7440Sstevel@tonic-gate int error;
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_link tdvp %p svp %p nm %s\n", (void *)tdvp,
7470Sstevel@tonic-gate (void *)svp, nm));
7480Sstevel@tonic-gate
7490Sstevel@tonic-gate if (error = auto_trigger_mount(tdvp, cred, &newvp))
7500Sstevel@tonic-gate goto done;
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate if (newvp == NULL) {
7530Sstevel@tonic-gate /*
7540Sstevel@tonic-gate * an autonode can not be a link to another node
7550Sstevel@tonic-gate */
7560Sstevel@tonic-gate error = ENOSYS;
7570Sstevel@tonic-gate goto done;
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate if (vn_is_readonly(newvp)) {
7610Sstevel@tonic-gate error = EROFS;
7620Sstevel@tonic-gate VN_RELE(newvp);
7630Sstevel@tonic-gate goto done;
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate if (vn_matchops(svp, auto_vnodeops)) {
7670Sstevel@tonic-gate /*
7680Sstevel@tonic-gate * source vp can't be an autonode
7690Sstevel@tonic-gate */
7700Sstevel@tonic-gate error = ENOSYS;
7710Sstevel@tonic-gate VN_RELE(newvp);
7720Sstevel@tonic-gate goto done;
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate
7755331Samw error = VOP_LINK(newvp, svp, nm, cred, ct, flags);
7760Sstevel@tonic-gate VN_RELE(newvp);
7770Sstevel@tonic-gate
7780Sstevel@tonic-gate done:
7790Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_link error=%d\n", error));
7800Sstevel@tonic-gate return (error);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate
7830Sstevel@tonic-gate static int
auto_rename(vnode_t * odvp,char * onm,vnode_t * ndvp,char * nnm,cred_t * cr,caller_context_t * ct,int flags)7845331Samw auto_rename(
7855331Samw vnode_t *odvp,
7865331Samw char *onm,
7875331Samw vnode_t *ndvp,
7885331Samw char *nnm,
7895331Samw cred_t *cr,
7905331Samw caller_context_t *ct,
7915331Samw int flags)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate vnode_t *o_newvp, *n_newvp;
7940Sstevel@tonic-gate int error;
7950Sstevel@tonic-gate
7960Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_rename odvp %p onm %s to ndvp %p nnm %s\n",
7970Sstevel@tonic-gate (void *)odvp, onm, (void *)ndvp, nnm));
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate /*
8000Sstevel@tonic-gate * we know odvp is an autonode, otherwise this function
8010Sstevel@tonic-gate * could not have ever been called.
8020Sstevel@tonic-gate */
8030Sstevel@tonic-gate ASSERT(vn_matchops(odvp, auto_vnodeops));
8040Sstevel@tonic-gate
8050Sstevel@tonic-gate if (error = auto_trigger_mount(odvp, cr, &o_newvp))
8060Sstevel@tonic-gate goto done;
8070Sstevel@tonic-gate
8080Sstevel@tonic-gate if (o_newvp == NULL) {
8090Sstevel@tonic-gate /*
8100Sstevel@tonic-gate * can't rename an autonode
8110Sstevel@tonic-gate */
8120Sstevel@tonic-gate error = ENOSYS;
8130Sstevel@tonic-gate goto done;
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate if (vn_matchops(ndvp, auto_vnodeops)) {
8170Sstevel@tonic-gate /*
8180Sstevel@tonic-gate * directory is AUTOFS, need to trigger the
8190Sstevel@tonic-gate * mount of the real filesystem.
8200Sstevel@tonic-gate */
8210Sstevel@tonic-gate if (error = auto_trigger_mount(ndvp, cr, &n_newvp)) {
8220Sstevel@tonic-gate VN_RELE(o_newvp);
8230Sstevel@tonic-gate goto done;
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate if (n_newvp == NULL) {
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate * target can't be an autonode
8290Sstevel@tonic-gate */
8300Sstevel@tonic-gate error = ENOSYS;
8310Sstevel@tonic-gate VN_RELE(o_newvp);
8320Sstevel@tonic-gate goto done;
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate } else {
8350Sstevel@tonic-gate /*
8360Sstevel@tonic-gate * destination directory mount had been
8370Sstevel@tonic-gate * triggered prior to the call to this function.
8380Sstevel@tonic-gate */
8390Sstevel@tonic-gate n_newvp = ndvp;
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate ASSERT(!vn_matchops(n_newvp, auto_vnodeops));
8430Sstevel@tonic-gate
8440Sstevel@tonic-gate if (vn_is_readonly(n_newvp)) {
8450Sstevel@tonic-gate error = EROFS;
8460Sstevel@tonic-gate VN_RELE(o_newvp);
8470Sstevel@tonic-gate if (n_newvp != ndvp)
8480Sstevel@tonic-gate VN_RELE(n_newvp);
8490Sstevel@tonic-gate goto done;
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate
8525331Samw error = VOP_RENAME(o_newvp, onm, n_newvp, nnm, cr, ct, flags);
8530Sstevel@tonic-gate VN_RELE(o_newvp);
8540Sstevel@tonic-gate if (n_newvp != ndvp)
8550Sstevel@tonic-gate VN_RELE(n_newvp);
8560Sstevel@tonic-gate
8570Sstevel@tonic-gate done:
8580Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_rename error=%d\n", error));
8590Sstevel@tonic-gate return (error);
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate
8620Sstevel@tonic-gate static int
auto_mkdir(vnode_t * dvp,char * nm,vattr_t * va,vnode_t ** vpp,cred_t * cred,caller_context_t * ct,int flags,vsecattr_t * vsecp)8635331Samw auto_mkdir(
8645331Samw vnode_t *dvp,
8655331Samw char *nm,
8665331Samw vattr_t *va,
8675331Samw vnode_t **vpp,
8685331Samw cred_t *cred,
8695331Samw caller_context_t *ct,
8705331Samw int flags,
8715331Samw vsecattr_t *vsecp)
8720Sstevel@tonic-gate {
8730Sstevel@tonic-gate vnode_t *newvp;
8740Sstevel@tonic-gate int error;
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_mkdir dvp %p nm %s\n", (void *)dvp, nm));
8770Sstevel@tonic-gate
8780Sstevel@tonic-gate if (error = auto_trigger_mount(dvp, cred, &newvp))
8790Sstevel@tonic-gate goto done;
8800Sstevel@tonic-gate
8810Sstevel@tonic-gate if (newvp != NULL) {
8820Sstevel@tonic-gate /*
8830Sstevel@tonic-gate * Node is now mounted on.
8840Sstevel@tonic-gate */
8850Sstevel@tonic-gate if (vn_is_readonly(newvp))
8860Sstevel@tonic-gate error = EROFS;
8870Sstevel@tonic-gate else
8885331Samw error = VOP_MKDIR(newvp, nm, va, vpp, cred, ct,
8895331Samw flags, vsecp);
8900Sstevel@tonic-gate VN_RELE(newvp);
8910Sstevel@tonic-gate } else
8920Sstevel@tonic-gate error = ENOSYS;
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate done:
8950Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_mkdir: error=%d\n", error));
8960Sstevel@tonic-gate return (error);
8970Sstevel@tonic-gate }
8980Sstevel@tonic-gate
8990Sstevel@tonic-gate static int
auto_rmdir(vnode_t * dvp,char * nm,vnode_t * cdir,cred_t * cred,caller_context_t * ct,int flags)9005331Samw auto_rmdir(
9015331Samw vnode_t *dvp,
9025331Samw char *nm,
9035331Samw vnode_t *cdir,
9045331Samw cred_t *cred,
9055331Samw caller_context_t *ct,
9065331Samw int flags)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate vnode_t *newvp;
9090Sstevel@tonic-gate int error;
9100Sstevel@tonic-gate
9110Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_rmdir: vp=%p nm=%s\n", (void *)dvp, nm));
9120Sstevel@tonic-gate
9130Sstevel@tonic-gate if (error = auto_trigger_mount(dvp, cred, &newvp))
9140Sstevel@tonic-gate goto done;
9150Sstevel@tonic-gate
9160Sstevel@tonic-gate if (newvp != NULL) {
9170Sstevel@tonic-gate /*
9180Sstevel@tonic-gate * Node is now mounted on.
9190Sstevel@tonic-gate */
9200Sstevel@tonic-gate if (vn_is_readonly(newvp))
9210Sstevel@tonic-gate error = EROFS;
9220Sstevel@tonic-gate else
9235331Samw error = VOP_RMDIR(newvp, nm, cdir, cred, ct, flags);
9240Sstevel@tonic-gate VN_RELE(newvp);
9250Sstevel@tonic-gate } else
9260Sstevel@tonic-gate error = ENOSYS;
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate done:
9290Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_rmdir: error=%d\n", error));
9300Sstevel@tonic-gate return (error);
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate
9330Sstevel@tonic-gate static int autofs_nobrowse = 0;
9340Sstevel@tonic-gate
9350Sstevel@tonic-gate #ifdef nextdp
9360Sstevel@tonic-gate #undef nextdp
9370Sstevel@tonic-gate #endif
9380Sstevel@tonic-gate #define nextdp(dp) ((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
9390Sstevel@tonic-gate
9405331Samw /* ARGSUSED */
9410Sstevel@tonic-gate static int
auto_readdir(vnode_t * vp,uio_t * uiop,cred_t * cred,int * eofp,caller_context_t * ct,int flags)9425331Samw auto_readdir(
9435331Samw vnode_t *vp,
9445331Samw uio_t *uiop,
9455331Samw cred_t *cred,
9465331Samw int *eofp,
9475331Samw caller_context_t *ct,
9485331Samw int flags)
9490Sstevel@tonic-gate {
9502170Sevanl struct autofs_rddirargs rda;
9512170Sevanl autofs_rddirres rd;
9520Sstevel@tonic-gate fnnode_t *fnp = vntofn(vp);
9530Sstevel@tonic-gate fnnode_t *cfnp, *nfnp;
9540Sstevel@tonic-gate dirent64_t *dp;
9550Sstevel@tonic-gate ulong_t offset;
9560Sstevel@tonic-gate ulong_t outcount = 0, count = 0;
9570Sstevel@tonic-gate size_t namelen;
9580Sstevel@tonic-gate ulong_t alloc_count;
9592170Sevanl void *outbuf = NULL;
9600Sstevel@tonic-gate fninfo_t *fnip = vfstofni(vp->v_vfsp);
9610Sstevel@tonic-gate struct iovec *iovp;
9620Sstevel@tonic-gate int error = 0;
9630Sstevel@tonic-gate int reached_max = 0;
9640Sstevel@tonic-gate int myeof = 0;
9650Sstevel@tonic-gate int this_reclen;
9662170Sevanl struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_readdir vp=%p offset=%lld\n",
9690Sstevel@tonic-gate (void *)vp, uiop->uio_loffset));
9700Sstevel@tonic-gate
9710Sstevel@tonic-gate if (eofp != NULL)
9720Sstevel@tonic-gate *eofp = 0;
9730Sstevel@tonic-gate
9742170Sevanl if (uiop->uio_iovcnt != 1)
9752170Sevanl return (EINVAL);
9762170Sevanl
9770Sstevel@tonic-gate iovp = uiop->uio_iov;
9780Sstevel@tonic-gate alloc_count = iovp->iov_len;
9790Sstevel@tonic-gate
9800Sstevel@tonic-gate gethrestime(&fnp->fn_atime);
9810Sstevel@tonic-gate fnp->fn_ref_time = fnp->fn_atime.tv_sec;
9820Sstevel@tonic-gate
9832170Sevanl dp = outbuf = kmem_zalloc(alloc_count, KM_SLEEP);
9840Sstevel@tonic-gate
9850Sstevel@tonic-gate /*
9860Sstevel@tonic-gate * Held when getdents calls VOP_RWLOCK....
9870Sstevel@tonic-gate */
9880Sstevel@tonic-gate ASSERT(RW_READ_HELD(&fnp->fn_rwlock));
9890Sstevel@tonic-gate if (uiop->uio_offset >= AUTOFS_DAEMONCOOKIE) {
9900Sstevel@tonic-gate again:
9910Sstevel@tonic-gate /*
9920Sstevel@tonic-gate * Do readdir of daemon contents only
9930Sstevel@tonic-gate * Drop readers lock and reacquire after reply.
9940Sstevel@tonic-gate */
9950Sstevel@tonic-gate rw_exit(&fnp->fn_rwlock);
9962170Sevanl bzero(&rd, sizeof (struct autofs_rddirres));
9970Sstevel@tonic-gate count = 0;
9980Sstevel@tonic-gate rda.rda_map = fnip->fi_map;
9990Sstevel@tonic-gate rda.rda_offset = (uint_t)uiop->uio_offset;
10000Sstevel@tonic-gate rd.rd_rddir.rddir_entries = dp;
10010Sstevel@tonic-gate rda.rda_count = rd.rd_rddir.rddir_size = (uint_t)alloc_count;
10023391Ssemery rda.uid = crgetuid(cred);
10032170Sevanl
10042170Sevanl error = auto_calldaemon(fngp->fng_zoneid,
10055302Sth199096 AUTOFS_READDIR,
10065302Sth199096 xdr_autofs_rddirargs,
10075302Sth199096 &rda,
10085302Sth199096 xdr_autofs_rddirres,
10095302Sth199096 (void *)&rd,
10105302Sth199096 sizeof (autofs_rddirres),
10115302Sth199096 TRUE);
10122170Sevanl
10130Sstevel@tonic-gate /*
10140Sstevel@tonic-gate * reacquire previously dropped lock
10150Sstevel@tonic-gate */
10160Sstevel@tonic-gate rw_enter(&fnp->fn_rwlock, RW_READER);
10170Sstevel@tonic-gate
10182170Sevanl if (!error) {
10190Sstevel@tonic-gate error = rd.rd_status;
10202170Sevanl dp = rd.rd_rddir.rddir_entries;
10212170Sevanl }
10222170Sevanl
10230Sstevel@tonic-gate if (error) {
10240Sstevel@tonic-gate if (error == AUTOFS_SHUTDOWN) {
10250Sstevel@tonic-gate /*
10260Sstevel@tonic-gate * treat as empty directory
10270Sstevel@tonic-gate */
10280Sstevel@tonic-gate error = 0;
10290Sstevel@tonic-gate myeof = 1;
10300Sstevel@tonic-gate if (eofp)
10310Sstevel@tonic-gate *eofp = 1;
10320Sstevel@tonic-gate }
10330Sstevel@tonic-gate goto done;
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate if (rd.rd_rddir.rddir_size) {
10362170Sevanl dirent64_t *odp = dp; /* next in output buffer */
10372170Sevanl dirent64_t *cdp = dp; /* current examined entry */
10380Sstevel@tonic-gate
10390Sstevel@tonic-gate /*
10400Sstevel@tonic-gate * Check for duplicates here
10410Sstevel@tonic-gate */
10420Sstevel@tonic-gate do {
10430Sstevel@tonic-gate this_reclen = cdp->d_reclen;
10442170Sevanl if (auto_search(fnp, cdp->d_name,
10455302Sth199096 NULL, cred)) {
10460Sstevel@tonic-gate /*
10470Sstevel@tonic-gate * entry not found in kernel list,
10480Sstevel@tonic-gate * include it in readdir output.
10490Sstevel@tonic-gate *
10500Sstevel@tonic-gate * If we are skipping entries. then
10510Sstevel@tonic-gate * we need to copy this entry to the
10520Sstevel@tonic-gate * correct position in the buffer
10530Sstevel@tonic-gate * to be copied out.
10540Sstevel@tonic-gate */
10550Sstevel@tonic-gate if (cdp != odp)
10560Sstevel@tonic-gate bcopy(cdp, odp,
10575302Sth199096 (size_t)this_reclen);
10580Sstevel@tonic-gate odp = nextdp(odp);
10590Sstevel@tonic-gate outcount += this_reclen;
10600Sstevel@tonic-gate } else {
10610Sstevel@tonic-gate /*
10620Sstevel@tonic-gate * Entry was found in the kernel
10630Sstevel@tonic-gate * list. If it is the first entry
10640Sstevel@tonic-gate * in this buffer, then just skip it
10650Sstevel@tonic-gate */
10660Sstevel@tonic-gate if (odp == dp) {
10670Sstevel@tonic-gate dp = nextdp(dp);
10680Sstevel@tonic-gate odp = dp;
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate count += this_reclen;
10720Sstevel@tonic-gate cdp = (struct dirent64 *)
10735302Sth199096 ((char *)cdp + this_reclen);
10740Sstevel@tonic-gate } while (count < rd.rd_rddir.rddir_size);
10750Sstevel@tonic-gate
10760Sstevel@tonic-gate if (outcount)
10770Sstevel@tonic-gate error = uiomove(dp, outcount, UIO_READ, uiop);
10780Sstevel@tonic-gate uiop->uio_offset = rd.rd_rddir.rddir_offset;
10790Sstevel@tonic-gate } else {
10800Sstevel@tonic-gate if (rd.rd_rddir.rddir_eof == 0) {
10810Sstevel@tonic-gate /*
10820Sstevel@tonic-gate * alloc_count not large enough for one
10830Sstevel@tonic-gate * directory entry
10840Sstevel@tonic-gate */
10850Sstevel@tonic-gate error = EINVAL;
10860Sstevel@tonic-gate }
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate if (rd.rd_rddir.rddir_eof && !error) {
10890Sstevel@tonic-gate myeof = 1;
10900Sstevel@tonic-gate if (eofp)
10910Sstevel@tonic-gate *eofp = 1;
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate if (!error && !myeof && outcount == 0) {
10940Sstevel@tonic-gate /*
10950Sstevel@tonic-gate * call daemon with new cookie, all previous
10960Sstevel@tonic-gate * elements happened to be duplicates
10970Sstevel@tonic-gate */
10980Sstevel@tonic-gate dp = outbuf;
10990Sstevel@tonic-gate goto again;
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate goto done;
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate
11040Sstevel@tonic-gate if (uiop->uio_offset == 0) {
11050Sstevel@tonic-gate /*
11060Sstevel@tonic-gate * first time: so fudge the . and ..
11070Sstevel@tonic-gate */
11080Sstevel@tonic-gate this_reclen = DIRENT64_RECLEN(1);
11090Sstevel@tonic-gate if (alloc_count < this_reclen) {
11100Sstevel@tonic-gate error = EINVAL;
11110Sstevel@tonic-gate goto done;
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate dp->d_ino = (ino64_t)fnp->fn_nodeid;
11140Sstevel@tonic-gate dp->d_off = (off64_t)1;
11150Sstevel@tonic-gate dp->d_reclen = (ushort_t)this_reclen;
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate /* use strncpy(9f) to zero out uninitialized bytes */
11180Sstevel@tonic-gate
11190Sstevel@tonic-gate (void) strncpy(dp->d_name, ".",
11205302Sth199096 DIRENT64_NAMELEN(this_reclen));
11210Sstevel@tonic-gate outcount += dp->d_reclen;
11220Sstevel@tonic-gate dp = nextdp(dp);
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate this_reclen = DIRENT64_RECLEN(2);
11250Sstevel@tonic-gate if (alloc_count < outcount + this_reclen) {
11260Sstevel@tonic-gate error = EINVAL;
11270Sstevel@tonic-gate goto done;
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate dp->d_reclen = (ushort_t)this_reclen;
11300Sstevel@tonic-gate dp->d_ino = (ino64_t)fnp->fn_parent->fn_nodeid;
11310Sstevel@tonic-gate dp->d_off = (off64_t)2;
11320Sstevel@tonic-gate
11330Sstevel@tonic-gate /* use strncpy(9f) to zero out uninitialized bytes */
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate (void) strncpy(dp->d_name, "..",
11365302Sth199096 DIRENT64_NAMELEN(this_reclen));
11370Sstevel@tonic-gate outcount += dp->d_reclen;
11380Sstevel@tonic-gate dp = nextdp(dp);
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate offset = 2;
11420Sstevel@tonic-gate cfnp = fnp->fn_dirents;
11430Sstevel@tonic-gate while (cfnp != NULL) {
11440Sstevel@tonic-gate nfnp = cfnp->fn_next;
11450Sstevel@tonic-gate offset = cfnp->fn_offset;
11460Sstevel@tonic-gate if ((offset >= uiop->uio_offset) &&
11475302Sth199096 (!(cfnp->fn_flags & MF_LOOKUP))) {
11480Sstevel@tonic-gate int reclen;
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate /*
11510Sstevel@tonic-gate * include node only if its offset is greater or
11520Sstevel@tonic-gate * equal to the one required and it is not in
11530Sstevel@tonic-gate * transient state (not being looked-up)
11540Sstevel@tonic-gate */
11550Sstevel@tonic-gate namelen = strlen(cfnp->fn_name);
11560Sstevel@tonic-gate reclen = (int)DIRENT64_RECLEN(namelen);
11570Sstevel@tonic-gate if (outcount + reclen > alloc_count) {
11580Sstevel@tonic-gate reached_max = 1;
11590Sstevel@tonic-gate break;
11600Sstevel@tonic-gate }
11610Sstevel@tonic-gate dp->d_reclen = (ushort_t)reclen;
11620Sstevel@tonic-gate dp->d_ino = (ino64_t)cfnp->fn_nodeid;
11630Sstevel@tonic-gate if (nfnp != NULL) {
11640Sstevel@tonic-gate /*
11650Sstevel@tonic-gate * get the offset of the next element
11660Sstevel@tonic-gate */
11670Sstevel@tonic-gate dp->d_off = (off64_t)nfnp->fn_offset;
11680Sstevel@tonic-gate } else {
11690Sstevel@tonic-gate /*
11700Sstevel@tonic-gate * This is the last element, make
11710Sstevel@tonic-gate * offset one plus the current
11720Sstevel@tonic-gate */
11730Sstevel@tonic-gate dp->d_off = (off64_t)cfnp->fn_offset + 1;
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate
11760Sstevel@tonic-gate /* use strncpy(9f) to zero out uninitialized bytes */
11770Sstevel@tonic-gate
11780Sstevel@tonic-gate (void) strncpy(dp->d_name, cfnp->fn_name,
11795302Sth199096 DIRENT64_NAMELEN(reclen));
11800Sstevel@tonic-gate outcount += dp->d_reclen;
11810Sstevel@tonic-gate dp = nextdp(dp);
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate cfnp = nfnp;
11840Sstevel@tonic-gate }
11850Sstevel@tonic-gate
11860Sstevel@tonic-gate if (outcount)
11870Sstevel@tonic-gate error = uiomove(outbuf, outcount, UIO_READ, uiop);
11885302Sth199096
11890Sstevel@tonic-gate if (!error) {
11900Sstevel@tonic-gate if (reached_max) {
11910Sstevel@tonic-gate /*
11920Sstevel@tonic-gate * This entry did not get added to the buffer on this,
11930Sstevel@tonic-gate * call. We need to add it on the next call therefore
11940Sstevel@tonic-gate * set uio_offset to this entry's offset. If there
11950Sstevel@tonic-gate * wasn't enough space for one dirent, return EINVAL.
11960Sstevel@tonic-gate */
11970Sstevel@tonic-gate uiop->uio_offset = offset;
11980Sstevel@tonic-gate if (outcount == 0)
11990Sstevel@tonic-gate error = EINVAL;
12000Sstevel@tonic-gate } else if (autofs_nobrowse ||
12015302Sth199096 auto_nobrowse_option(fnip->fi_opts) ||
12025302Sth199096 (fnip->fi_flags & MF_DIRECT) ||
12035302Sth199096 (fnp->fn_trigger != NULL) ||
12045302Sth199096 (((vp->v_flag & VROOT) == 0) &&
12055302Sth199096 ((fntovn(fnp->fn_parent))->v_flag & VROOT) &&
12065302Sth199096 (fnp->fn_dirents == NULL))) {
12070Sstevel@tonic-gate /*
12080Sstevel@tonic-gate * done reading directory entries
12090Sstevel@tonic-gate */
12100Sstevel@tonic-gate uiop->uio_offset = offset + 1;
12110Sstevel@tonic-gate if (eofp)
12120Sstevel@tonic-gate *eofp = 1;
12130Sstevel@tonic-gate } else {
12140Sstevel@tonic-gate /*
12150Sstevel@tonic-gate * Need to get the rest of the entries from the daemon.
12160Sstevel@tonic-gate */
12170Sstevel@tonic-gate uiop->uio_offset = AUTOFS_DAEMONCOOKIE;
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate }
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate done:
12220Sstevel@tonic-gate kmem_free(outbuf, alloc_count);
12230Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_readdir vp=%p offset=%lld eof=%d\n",
12245302Sth199096 (void *)vp, uiop->uio_loffset, myeof));
12250Sstevel@tonic-gate return (error);
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate
12280Sstevel@tonic-gate static int
auto_symlink(vnode_t * dvp,char * lnknm,vattr_t * tva,char * tnm,cred_t * cred,caller_context_t * ct,int flags)12290Sstevel@tonic-gate auto_symlink(
12300Sstevel@tonic-gate vnode_t *dvp,
12310Sstevel@tonic-gate char *lnknm, /* new entry */
12320Sstevel@tonic-gate vattr_t *tva,
12330Sstevel@tonic-gate char *tnm, /* existing entry */
12345331Samw cred_t *cred,
12355331Samw caller_context_t *ct,
12365331Samw int flags)
12370Sstevel@tonic-gate {
12380Sstevel@tonic-gate vnode_t *newvp;
12390Sstevel@tonic-gate int error;
12400Sstevel@tonic-gate
12410Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_symlink: dvp=%p lnknm=%s tnm=%s\n",
12420Sstevel@tonic-gate (void *)dvp, lnknm, tnm));
12430Sstevel@tonic-gate
12440Sstevel@tonic-gate if (error = auto_trigger_mount(dvp, cred, &newvp))
12450Sstevel@tonic-gate goto done;
12460Sstevel@tonic-gate
12470Sstevel@tonic-gate if (newvp != NULL) {
12480Sstevel@tonic-gate /*
12490Sstevel@tonic-gate * Node is mounted on.
12500Sstevel@tonic-gate */
12510Sstevel@tonic-gate if (vn_is_readonly(newvp))
12520Sstevel@tonic-gate error = EROFS;
12530Sstevel@tonic-gate else
12545331Samw error = VOP_SYMLINK(newvp, lnknm, tva, tnm, cred,
12555331Samw ct, flags);
12560Sstevel@tonic-gate VN_RELE(newvp);
12570Sstevel@tonic-gate } else
12580Sstevel@tonic-gate error = ENOSYS;
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate done:
12610Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_symlink: error=%d\n", error));
12620Sstevel@tonic-gate return (error);
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate
12650Sstevel@tonic-gate /* ARGSUSED */
12660Sstevel@tonic-gate static int
auto_readlink(vnode_t * vp,struct uio * uiop,cred_t * cr,caller_context_t * ct)12675331Samw auto_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
12680Sstevel@tonic-gate {
12690Sstevel@tonic-gate fnnode_t *fnp = vntofn(vp);
12700Sstevel@tonic-gate int error;
12710Sstevel@tonic-gate timestruc_t now;
12720Sstevel@tonic-gate
12730Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_readlink: vp=%p\n", (void *)vp));
12740Sstevel@tonic-gate
12750Sstevel@tonic-gate gethrestime(&now);
12760Sstevel@tonic-gate fnp->fn_ref_time = now.tv_sec;
12770Sstevel@tonic-gate
12780Sstevel@tonic-gate if (vp->v_type != VLNK)
12790Sstevel@tonic-gate error = EINVAL;
12800Sstevel@tonic-gate else {
12810Sstevel@tonic-gate ASSERT(!(fnp->fn_flags & (MF_INPROG | MF_LOOKUP)));
12820Sstevel@tonic-gate fnp->fn_atime = now;
12830Sstevel@tonic-gate error = uiomove(fnp->fn_symlink, MIN(fnp->fn_symlinklen,
12840Sstevel@tonic-gate uiop->uio_resid), UIO_READ, uiop);
12850Sstevel@tonic-gate }
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_readlink: error=%d\n", error));
12880Sstevel@tonic-gate return (error);
12890Sstevel@tonic-gate }
12900Sstevel@tonic-gate
12910Sstevel@tonic-gate /* ARGSUSED */
12920Sstevel@tonic-gate static int
auto_fsync(vnode_t * cp,int syncflag,cred_t * cred,caller_context_t * ct)12935331Samw auto_fsync(vnode_t *cp, int syncflag, cred_t *cred, caller_context_t *ct)
12940Sstevel@tonic-gate {
12950Sstevel@tonic-gate return (0);
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate
12980Sstevel@tonic-gate /* ARGSUSED */
12990Sstevel@tonic-gate static void
auto_inactive(vnode_t * vp,cred_t * cred,caller_context_t * ct)13005331Samw auto_inactive(vnode_t *vp, cred_t *cred, caller_context_t *ct)
13010Sstevel@tonic-gate {
13020Sstevel@tonic-gate fnnode_t *fnp = vntofn(vp);
13030Sstevel@tonic-gate fnnode_t *dfnp = fnp->fn_parent;
13040Sstevel@tonic-gate int count;
13050Sstevel@tonic-gate
13060Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_inactive: vp=%p v_count=%u fn_link=%d\n",
13070Sstevel@tonic-gate (void *)vp, vp->v_count, fnp->fn_linkcnt));
13080Sstevel@tonic-gate
13090Sstevel@tonic-gate /*
13100Sstevel@tonic-gate * The rwlock should not be already held by this thread.
13110Sstevel@tonic-gate * The assert relies on the fact that the owner field is cleared
13120Sstevel@tonic-gate * when the lock is released.
13130Sstevel@tonic-gate */
13140Sstevel@tonic-gate ASSERT(dfnp != NULL);
13150Sstevel@tonic-gate ASSERT(rw_owner(&dfnp->fn_rwlock) != curthread);
13160Sstevel@tonic-gate rw_enter(&dfnp->fn_rwlock, RW_WRITER);
13170Sstevel@tonic-gate mutex_enter(&vp->v_lock);
13180Sstevel@tonic-gate ASSERT(vp->v_count > 0);
13190Sstevel@tonic-gate count = --vp->v_count;
13200Sstevel@tonic-gate mutex_exit(&vp->v_lock);
13210Sstevel@tonic-gate if (count == 0) {
13220Sstevel@tonic-gate /*
13230Sstevel@tonic-gate * Free only if node has no subdirectories.
13240Sstevel@tonic-gate */
13250Sstevel@tonic-gate if (fnp->fn_linkcnt == 1) {
13260Sstevel@tonic-gate auto_disconnect(dfnp, fnp);
13270Sstevel@tonic-gate rw_exit(&dfnp->fn_rwlock);
13280Sstevel@tonic-gate auto_freefnnode(fnp);
13290Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_inactive: (exit) vp=%p freed\n",
13300Sstevel@tonic-gate (void *)vp));
13310Sstevel@tonic-gate return;
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate rw_exit(&dfnp->fn_rwlock);
13350Sstevel@tonic-gate
13360Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_inactive: (exit) vp=%p v_count=%u fn_link=%d\n",
13370Sstevel@tonic-gate (void *)vp, vp->v_count, fnp->fn_linkcnt));
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate
13400Sstevel@tonic-gate /* ARGSUSED2 */
13410Sstevel@tonic-gate static int
auto_rwlock(vnode_t * vp,int write_lock,caller_context_t * ct)13420Sstevel@tonic-gate auto_rwlock(vnode_t *vp, int write_lock, caller_context_t *ct)
13430Sstevel@tonic-gate {
13440Sstevel@tonic-gate fnnode_t *fnp = vntofn(vp);
13450Sstevel@tonic-gate if (write_lock)
13460Sstevel@tonic-gate rw_enter(&fnp->fn_rwlock, RW_WRITER);
13470Sstevel@tonic-gate else
13480Sstevel@tonic-gate rw_enter(&fnp->fn_rwlock, RW_READER);
13490Sstevel@tonic-gate return (write_lock);
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate /* ARGSUSED */
13530Sstevel@tonic-gate static void
auto_rwunlock(vnode_t * vp,int write_lock,caller_context_t * ct)13540Sstevel@tonic-gate auto_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ct)
13550Sstevel@tonic-gate {
13560Sstevel@tonic-gate fnnode_t *fnp = vntofn(vp);
13570Sstevel@tonic-gate rw_exit(&fnp->fn_rwlock);
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate
13600Sstevel@tonic-gate
13610Sstevel@tonic-gate /* ARGSUSED */
13620Sstevel@tonic-gate static int
auto_seek(struct vnode * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)13635331Samw auto_seek(
13645331Samw struct vnode *vp,
13655331Samw offset_t ooff,
13665331Samw offset_t *noffp,
13675331Samw caller_context_t *ct)
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate /*
13700Sstevel@tonic-gate * Return 0 unconditionally, since we expect
13710Sstevel@tonic-gate * a VDIR all the time
13720Sstevel@tonic-gate */
13730Sstevel@tonic-gate return (0);
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate
13760Sstevel@tonic-gate /*
13770Sstevel@tonic-gate * Triggers the mount if needed. If the mount has been triggered by
13780Sstevel@tonic-gate * another thread, it will wait for its return status, and return it.
13790Sstevel@tonic-gate * Whether the mount is triggered by this thread, another thread, or
13800Sstevel@tonic-gate * if the vnode was already covered, '*newvp' is a
13810Sstevel@tonic-gate * VN_HELD vnode pointing to the root of the filesystem covering 'vp'.
13820Sstevel@tonic-gate * If the node is not mounted on, and should not be mounted on, '*newvp'
13830Sstevel@tonic-gate * will be NULL.
13840Sstevel@tonic-gate * The calling routine may use '*newvp' to do the filesystem jump.
13850Sstevel@tonic-gate */
13860Sstevel@tonic-gate static int
auto_trigger_mount(vnode_t * vp,cred_t * cred,vnode_t ** newvp)13870Sstevel@tonic-gate auto_trigger_mount(vnode_t *vp, cred_t *cred, vnode_t **newvp)
13880Sstevel@tonic-gate {
13890Sstevel@tonic-gate fnnode_t *fnp = vntofn(vp);
13900Sstevel@tonic-gate fninfo_t *fnip = vfstofni(vp->v_vfsp);
13910Sstevel@tonic-gate vnode_t *dvp;
13920Sstevel@tonic-gate vfs_t *vfsp;
13930Sstevel@tonic-gate int delayed_ind;
13940Sstevel@tonic-gate char name[AUTOFS_MAXPATHLEN];
13950Sstevel@tonic-gate int error;
13960Sstevel@tonic-gate
13970Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_trigger_mount: vp=%p\n", (void *)vp));
13980Sstevel@tonic-gate
13990Sstevel@tonic-gate *newvp = NULL;
14000Sstevel@tonic-gate
14010Sstevel@tonic-gate /*
14020Sstevel@tonic-gate * Cross-zone mount triggering is disallowed.
14030Sstevel@tonic-gate */
14040Sstevel@tonic-gate if (fnip->fi_zoneid != getzoneid())
14050Sstevel@tonic-gate return (EPERM); /* Not owner of mount */
14060Sstevel@tonic-gate
14070Sstevel@tonic-gate retry:
14080Sstevel@tonic-gate error = 0;
14090Sstevel@tonic-gate delayed_ind = 0;
14100Sstevel@tonic-gate mutex_enter(&fnp->fn_lock);
14110Sstevel@tonic-gate while (fnp->fn_flags & (MF_LOOKUP | MF_INPROG)) {
14120Sstevel@tonic-gate /*
14130Sstevel@tonic-gate * Mount or lookup in progress,
14140Sstevel@tonic-gate * wait for it before proceeding.
14150Sstevel@tonic-gate */
14160Sstevel@tonic-gate mutex_exit(&fnp->fn_lock);
14170Sstevel@tonic-gate error = auto_wait4mount(fnp);
14180Sstevel@tonic-gate if (error == AUTOFS_SHUTDOWN) {
14190Sstevel@tonic-gate error = 0;
14200Sstevel@tonic-gate goto done;
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate if (error && error != EAGAIN)
14230Sstevel@tonic-gate goto done;
14240Sstevel@tonic-gate error = 0;
14250Sstevel@tonic-gate mutex_enter(&fnp->fn_lock);
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate
14280Sstevel@tonic-gate /*
14290Sstevel@tonic-gate * If the vfslock can't be acquired for the first time.
14300Sstevel@tonic-gate * drop the fn_lock and retry next time in blocking mode.
14310Sstevel@tonic-gate */
14320Sstevel@tonic-gate if (vn_vfswlock(vp)) {
14330Sstevel@tonic-gate /*
14340Sstevel@tonic-gate * Lock held by another thread.
14350Sstevel@tonic-gate * Perform blocking by dropping the
14360Sstevel@tonic-gate * fn_lock.
14370Sstevel@tonic-gate */
14380Sstevel@tonic-gate mutex_exit(&fnp->fn_lock);
14390Sstevel@tonic-gate error = vn_vfswlock_wait(vp);
14400Sstevel@tonic-gate if (error)
14410Sstevel@tonic-gate goto done;
14420Sstevel@tonic-gate /*
14430Sstevel@tonic-gate * Because fn_lock wasn't held, the state
14440Sstevel@tonic-gate * of the trigger node might have changed.
14450Sstevel@tonic-gate * Need to run through the checks on trigger
14460Sstevel@tonic-gate * node again.
14470Sstevel@tonic-gate */
14480Sstevel@tonic-gate vn_vfsunlock(vp);
14490Sstevel@tonic-gate goto retry;
14500Sstevel@tonic-gate }
14510Sstevel@tonic-gate
14520Sstevel@tonic-gate vfsp = vn_mountedvfs(vp);
14530Sstevel@tonic-gate if (vfsp != NULL) {
14540Sstevel@tonic-gate mutex_exit(&fnp->fn_lock);
14551153Snr123932 error = VFS_ROOT(vfsp, newvp);
14560Sstevel@tonic-gate vn_vfsunlock(vp);
14570Sstevel@tonic-gate goto done;
14580Sstevel@tonic-gate } else {
14590Sstevel@tonic-gate vn_vfsunlock(vp);
14600Sstevel@tonic-gate if ((fnp->fn_flags & MF_MOUNTPOINT) &&
14610Sstevel@tonic-gate fnp->fn_trigger != NULL) {
14620Sstevel@tonic-gate ASSERT(fnp->fn_dirents == NULL);
146312184SJan.Kryl@Sun.COM mutex_exit(&fnp->fn_lock);
14640Sstevel@tonic-gate /*
14650Sstevel@tonic-gate * The filesystem that used to sit here has been
146612184SJan.Kryl@Sun.COM * forcibly unmounted. Do our best to recover.
146712184SJan.Kryl@Sun.COM * Try to unmount autofs subtree below this node
146812184SJan.Kryl@Sun.COM * and retry the action.
14690Sstevel@tonic-gate */
147012184SJan.Kryl@Sun.COM if (unmount_subtree(fnp, B_TRUE) != 0) {
147112184SJan.Kryl@Sun.COM error = EIO;
147212184SJan.Kryl@Sun.COM goto done;
147312184SJan.Kryl@Sun.COM }
147412184SJan.Kryl@Sun.COM goto retry;
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate }
14770Sstevel@tonic-gate
14780Sstevel@tonic-gate ASSERT(vp->v_type == VDIR);
14790Sstevel@tonic-gate dvp = fntovn(fnp->fn_parent);
14800Sstevel@tonic-gate
14810Sstevel@tonic-gate if ((fnp->fn_dirents == NULL) &&
14820Sstevel@tonic-gate ((fnip->fi_flags & MF_DIRECT) == 0) &&
14830Sstevel@tonic-gate ((vp->v_flag & VROOT) == 0) &&
14840Sstevel@tonic-gate (dvp->v_flag & VROOT)) {
14850Sstevel@tonic-gate /*
14860Sstevel@tonic-gate * If the parent of this node is the root of an indirect
14870Sstevel@tonic-gate * AUTOFS filesystem, this node is remountable.
14880Sstevel@tonic-gate */
14890Sstevel@tonic-gate delayed_ind = 1;
14900Sstevel@tonic-gate }
14910Sstevel@tonic-gate
14920Sstevel@tonic-gate if (delayed_ind ||
14930Sstevel@tonic-gate ((fnip->fi_flags & MF_DIRECT) && (fnp->fn_dirents == NULL))) {
14940Sstevel@tonic-gate /*
14950Sstevel@tonic-gate * Trigger mount since:
14960Sstevel@tonic-gate * direct mountpoint with no subdirs or
14970Sstevel@tonic-gate * delayed indirect.
14980Sstevel@tonic-gate */
14990Sstevel@tonic-gate AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
15000Sstevel@tonic-gate fnp->fn_error = 0;
15010Sstevel@tonic-gate mutex_exit(&fnp->fn_lock);
15020Sstevel@tonic-gate if (delayed_ind)
15030Sstevel@tonic-gate (void) strcpy(name, fnp->fn_name);
15040Sstevel@tonic-gate else
15050Sstevel@tonic-gate (void) strcpy(name, ".");
15060Sstevel@tonic-gate fnp->fn_ref_time = gethrestime_sec();
15070Sstevel@tonic-gate auto_new_mount_thread(fnp, name, cred);
15080Sstevel@tonic-gate /*
15090Sstevel@tonic-gate * At this point we're simply another thread waiting
15100Sstevel@tonic-gate * for the mount to finish.
15110Sstevel@tonic-gate */
15120Sstevel@tonic-gate error = auto_wait4mount(fnp);
15130Sstevel@tonic-gate if (error == EAGAIN)
15140Sstevel@tonic-gate goto retry;
15150Sstevel@tonic-gate if (error == AUTOFS_SHUTDOWN) {
15160Sstevel@tonic-gate error = 0;
15170Sstevel@tonic-gate goto done;
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate if (error == 0) {
15201153Snr123932 if (error = vn_vfsrlock_wait(vp))
15210Sstevel@tonic-gate goto done;
15220Sstevel@tonic-gate /* Reacquire after dropping locks */
15230Sstevel@tonic-gate vfsp = vn_mountedvfs(vp);
15240Sstevel@tonic-gate if (vfsp != NULL) {
15251153Snr123932 error = VFS_ROOT(vfsp, newvp);
15260Sstevel@tonic-gate vn_vfsunlock(vp);
15270Sstevel@tonic-gate } else {
15280Sstevel@tonic-gate vn_vfsunlock(vp);
15290Sstevel@tonic-gate goto retry;
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate } else
15330Sstevel@tonic-gate mutex_exit(&fnp->fn_lock);
15340Sstevel@tonic-gate
15350Sstevel@tonic-gate done:
15360Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_trigger_mount: error=%d\n", error));
15370Sstevel@tonic-gate return (error);
15380Sstevel@tonic-gate }
1539