xref: /onnv-gate/usr/src/uts/common/fs/autofs/auto_vfsops.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/param.h>
30*0Sstevel@tonic-gate #include <sys/errno.h>
31*0Sstevel@tonic-gate #include <sys/proc.h>
32*0Sstevel@tonic-gate #include <sys/disp.h>
33*0Sstevel@tonic-gate #include <sys/vfs.h>
34*0Sstevel@tonic-gate #include <sys/vnode.h>
35*0Sstevel@tonic-gate #include <sys/uio.h>
36*0Sstevel@tonic-gate #include <sys/kmem.h>
37*0Sstevel@tonic-gate #include <sys/cred.h>
38*0Sstevel@tonic-gate #include <sys/statvfs.h>
39*0Sstevel@tonic-gate #include <sys/mount.h>
40*0Sstevel@tonic-gate #include <sys/tiuser.h>
41*0Sstevel@tonic-gate #include <sys/cmn_err.h>
42*0Sstevel@tonic-gate #include <sys/debug.h>
43*0Sstevel@tonic-gate #include <sys/mkdev.h>
44*0Sstevel@tonic-gate #include <sys/systm.h>
45*0Sstevel@tonic-gate #include <sys/sysmacros.h>
46*0Sstevel@tonic-gate #include <sys/pathname.h>
47*0Sstevel@tonic-gate #include <rpc/types.h>
48*0Sstevel@tonic-gate #include <rpc/auth.h>
49*0Sstevel@tonic-gate #include <rpc/clnt.h>
50*0Sstevel@tonic-gate #include <sys/dnlc.h>
51*0Sstevel@tonic-gate #include <fs/fs_subr.h>
52*0Sstevel@tonic-gate #include <sys/fs/autofs.h>
53*0Sstevel@tonic-gate #include <rpcsvc/autofs_prot.h>
54*0Sstevel@tonic-gate #include <sys/note.h>
55*0Sstevel@tonic-gate #include <sys/modctl.h>
56*0Sstevel@tonic-gate #include <sys/mntent.h>
57*0Sstevel@tonic-gate #include <sys/policy.h>
58*0Sstevel@tonic-gate #include <sys/zone.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static int autofs_init(int, char *);
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate static major_t autofs_major;
63*0Sstevel@tonic-gate static minor_t autofs_minor;
64*0Sstevel@tonic-gate static kmutex_t autofs_minor_lock;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate zone_key_t autofs_key;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate static mntopts_t auto_mntopts;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate  * The AUTOFS system call.
72*0Sstevel@tonic-gate  */
73*0Sstevel@tonic-gate static struct sysent autofssysent = {
74*0Sstevel@tonic-gate 	2,
75*0Sstevel@tonic-gate 	SE_32RVAL1 | SE_ARGC | SE_NOUNLOAD,
76*0Sstevel@tonic-gate 	autofssys
77*0Sstevel@tonic-gate };
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate static struct modlsys modlsys = {
80*0Sstevel@tonic-gate 	&mod_syscallops,
81*0Sstevel@tonic-gate 	"AUTOFS syscall",
82*0Sstevel@tonic-gate 	&autofssysent
83*0Sstevel@tonic-gate };
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
86*0Sstevel@tonic-gate static struct modlsys  modlsys32 = {
87*0Sstevel@tonic-gate 	&mod_syscallops32,
88*0Sstevel@tonic-gate 	"AUTOFS syscall (32-bit)",
89*0Sstevel@tonic-gate 	&autofssysent
90*0Sstevel@tonic-gate };
91*0Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate static vfsdef_t vfw = {
94*0Sstevel@tonic-gate 	VFSDEF_VERSION,
95*0Sstevel@tonic-gate 	"autofs",
96*0Sstevel@tonic-gate 	autofs_init,
97*0Sstevel@tonic-gate 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT,
98*0Sstevel@tonic-gate 	&auto_mntopts
99*0Sstevel@tonic-gate };
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate /*
102*0Sstevel@tonic-gate  * Module linkage information for the kernel.
103*0Sstevel@tonic-gate  */
104*0Sstevel@tonic-gate static struct modlfs modlfs = {
105*0Sstevel@tonic-gate 	&mod_fsops, "filesystem for autofs", &vfw
106*0Sstevel@tonic-gate };
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
109*0Sstevel@tonic-gate 	MODREV_1,
110*0Sstevel@tonic-gate 	&modlsys,
111*0Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
112*0Sstevel@tonic-gate 	&modlsys32,
113*0Sstevel@tonic-gate #endif
114*0Sstevel@tonic-gate 	&modlfs,
115*0Sstevel@tonic-gate 	NULL
116*0Sstevel@tonic-gate };
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /*
119*0Sstevel@tonic-gate  * There are not enough stubs for rpcmod so we must force load it
120*0Sstevel@tonic-gate  */
121*0Sstevel@tonic-gate char _depends_on[] = "strmod/rpcmod misc/rpcsec fs/mntfs";
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate  * This is the module initialization routine.
125*0Sstevel@tonic-gate  */
126*0Sstevel@tonic-gate int
127*0Sstevel@tonic-gate _init(void)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate int
133*0Sstevel@tonic-gate _fini(void)
134*0Sstevel@tonic-gate {
135*0Sstevel@tonic-gate 	/*
136*0Sstevel@tonic-gate 	 * Don't allow the autofs module to be unloaded for now.
137*0Sstevel@tonic-gate 	 */
138*0Sstevel@tonic-gate 	return (EBUSY);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate int
142*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate static int autofs_fstype;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate /*
150*0Sstevel@tonic-gate  * autofs VFS operations
151*0Sstevel@tonic-gate  */
152*0Sstevel@tonic-gate static int auto_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
153*0Sstevel@tonic-gate static int auto_unmount(vfs_t *, int, cred_t *);
154*0Sstevel@tonic-gate static int auto_root(vfs_t *, vnode_t **);
155*0Sstevel@tonic-gate static int auto_statvfs(vfs_t *, struct statvfs64 *);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * Auto Mount options table
159*0Sstevel@tonic-gate  */
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate static char *direct_cancel[] = { MNTOPT_INDIRECT, NULL };
162*0Sstevel@tonic-gate static char *indirect_cancel[] = { MNTOPT_DIRECT, NULL };
163*0Sstevel@tonic-gate static char *browse_cancel[] = { MNTOPT_NOBROWSE, NULL };
164*0Sstevel@tonic-gate static char *nobrowse_cancel[] = { MNTOPT_BROWSE, NULL };
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate static mntopt_t mntopts[] = {
167*0Sstevel@tonic-gate /*
168*0Sstevel@tonic-gate  *	option name		cancel options	default arg	flags
169*0Sstevel@tonic-gate  */
170*0Sstevel@tonic-gate 	{ MNTOPT_DIRECT,	direct_cancel,	NULL,		0,
171*0Sstevel@tonic-gate 		NULL },
172*0Sstevel@tonic-gate 	{ MNTOPT_INDIRECT,	indirect_cancel, NULL,		0,
173*0Sstevel@tonic-gate 		NULL },
174*0Sstevel@tonic-gate 	{ MNTOPT_IGNORE,	NULL,		NULL,
175*0Sstevel@tonic-gate 		MO_DEFAULT|MO_TAG,	NULL },
176*0Sstevel@tonic-gate 	{ "nest",		NULL,		NULL,		MO_TAG,
177*0Sstevel@tonic-gate 		NULL },
178*0Sstevel@tonic-gate 	{ MNTOPT_BROWSE,	browse_cancel,	NULL,		MO_TAG,
179*0Sstevel@tonic-gate 		NULL },
180*0Sstevel@tonic-gate 	{ MNTOPT_NOBROWSE,	nobrowse_cancel, NULL,		MO_TAG,
181*0Sstevel@tonic-gate 		NULL },
182*0Sstevel@tonic-gate 	{ MNTOPT_RESTRICT,	NULL,		NULL,		MO_TAG,
183*0Sstevel@tonic-gate 		NULL },
184*0Sstevel@tonic-gate };
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate static mntopts_t auto_mntopts = {
187*0Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
188*0Sstevel@tonic-gate 	mntopts
189*0Sstevel@tonic-gate };
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate /*ARGSUSED*/
192*0Sstevel@tonic-gate static void
193*0Sstevel@tonic-gate autofs_zone_destructor(zoneid_t zoneid, void *arg)
194*0Sstevel@tonic-gate {
195*0Sstevel@tonic-gate 	struct autofs_globals *fngp = arg;
196*0Sstevel@tonic-gate 	vnode_t *vp;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	if (fngp == NULL)
199*0Sstevel@tonic-gate 		return;
200*0Sstevel@tonic-gate 	ASSERT(fngp->fng_fnnode_count == 1);
201*0Sstevel@tonic-gate 	ASSERT(fngp->fng_unmount_threads == 0);
202*0Sstevel@tonic-gate 	/*
203*0Sstevel@tonic-gate 	 * vn_alloc() initialized the rootnode with a count of 1; we need to
204*0Sstevel@tonic-gate 	 * make this 0 to placate auto_freefnnode().
205*0Sstevel@tonic-gate 	 */
206*0Sstevel@tonic-gate 	vp = fntovn(fngp->fng_rootfnnodep);
207*0Sstevel@tonic-gate 	ASSERT(vp->v_count == 1);
208*0Sstevel@tonic-gate 	vp->v_count--;
209*0Sstevel@tonic-gate 	auto_freefnnode(fngp->fng_rootfnnodep);
210*0Sstevel@tonic-gate 	mutex_destroy(&fngp->fng_unmount_threads_lock);
211*0Sstevel@tonic-gate 	kmem_free(fngp, sizeof (*fngp));
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate /*
215*0Sstevel@tonic-gate  * rootfnnodep is allocated here.  Its sole purpose is to provide
216*0Sstevel@tonic-gate  * read/write locking for top level fnnodes.  This object is
217*0Sstevel@tonic-gate  * persistent and will not be deallocated until the zone is destroyed.
218*0Sstevel@tonic-gate  *
219*0Sstevel@tonic-gate  * The current zone is implied as the zone of interest, since we will be
220*0Sstevel@tonic-gate  * calling zthread_create() which must be called from the correct zone.
221*0Sstevel@tonic-gate  */
222*0Sstevel@tonic-gate static struct autofs_globals *
223*0Sstevel@tonic-gate autofs_zone_init(void)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate 	char rootname[sizeof ("root_fnnode_zone_") + ZONEID_WIDTH];
226*0Sstevel@tonic-gate 	struct autofs_globals *fngp;
227*0Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	fngp = kmem_zalloc(sizeof (*fngp), KM_SLEEP);
230*0Sstevel@tonic-gate 	(void) snprintf(rootname, sizeof (rootname), "root_fnnode_zone_%d",
231*0Sstevel@tonic-gate 	    zoneid);
232*0Sstevel@tonic-gate 	fngp->fng_rootfnnodep = auto_makefnnode(VNON, NULL, rootname, CRED(),
233*0Sstevel@tonic-gate 	    fngp);
234*0Sstevel@tonic-gate 	/*
235*0Sstevel@tonic-gate 	 * Don't need to hold fng_rootfnnodep as it's never really used for
236*0Sstevel@tonic-gate 	 * anything.
237*0Sstevel@tonic-gate 	 */
238*0Sstevel@tonic-gate 	fngp->fng_fnnode_count = 1;
239*0Sstevel@tonic-gate 	fngp->fng_printed_not_running_msg = 0;
240*0Sstevel@tonic-gate 	fngp->fng_zoneid = zoneid;
241*0Sstevel@tonic-gate 	mutex_init(&fngp->fng_unmount_threads_lock, NULL, MUTEX_DEFAULT,
242*0Sstevel@tonic-gate 	    NULL);
243*0Sstevel@tonic-gate 	fngp->fng_unmount_threads = 0;
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	/*
246*0Sstevel@tonic-gate 	 * Start the unmounter thread for this zone.
247*0Sstevel@tonic-gate 	 */
248*0Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, auto_do_unmount, fngp, 0, minclsyspri);
249*0Sstevel@tonic-gate 	return (fngp);
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate int
253*0Sstevel@tonic-gate autofs_init(int fstype, char *name)
254*0Sstevel@tonic-gate {
255*0Sstevel@tonic-gate 	static const fs_operation_def_t auto_vfsops_template[] = {
256*0Sstevel@tonic-gate 		VFSNAME_MOUNT, auto_mount,
257*0Sstevel@tonic-gate 		VFSNAME_UNMOUNT, auto_unmount,
258*0Sstevel@tonic-gate 		VFSNAME_ROOT, auto_root,
259*0Sstevel@tonic-gate 		VFSNAME_STATVFS, auto_statvfs,
260*0Sstevel@tonic-gate 		NULL, NULL
261*0Sstevel@tonic-gate 	};
262*0Sstevel@tonic-gate 	int error;
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	autofs_fstype = fstype;
265*0Sstevel@tonic-gate 	ASSERT(autofs_fstype != 0);
266*0Sstevel@tonic-gate 	/*
267*0Sstevel@tonic-gate 	 * Associate VFS ops vector with this fstype
268*0Sstevel@tonic-gate 	 */
269*0Sstevel@tonic-gate 	error = vfs_setfsops(fstype, auto_vfsops_template, NULL);
270*0Sstevel@tonic-gate 	if (error != 0) {
271*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "autofs_init: bad vfs ops template");
272*0Sstevel@tonic-gate 		return (error);
273*0Sstevel@tonic-gate 	}
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	error = vn_make_ops(name, auto_vnodeops_template, &auto_vnodeops);
276*0Sstevel@tonic-gate 	if (error != 0) {
277*0Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
278*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "autofs_init: bad vnode ops template");
279*0Sstevel@tonic-gate 		return (error);
280*0Sstevel@tonic-gate 	}
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	mutex_init(&autofs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
283*0Sstevel@tonic-gate 	/*
284*0Sstevel@tonic-gate 	 * Assign unique major number for all autofs mounts
285*0Sstevel@tonic-gate 	 */
286*0Sstevel@tonic-gate 	if ((autofs_major = getudev()) == (major_t)-1) {
287*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
288*0Sstevel@tonic-gate 		    "autofs: autofs_init: can't get unique device number");
289*0Sstevel@tonic-gate 		mutex_destroy(&autofs_minor_lock);
290*0Sstevel@tonic-gate 		return (1);
291*0Sstevel@tonic-gate 	}
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	/*
294*0Sstevel@tonic-gate 	 * We'd like to be able to provide a constructor here, but we can't
295*0Sstevel@tonic-gate 	 * since it wants to zthread_create(), something it can't do in a ZSD
296*0Sstevel@tonic-gate 	 * constructor.
297*0Sstevel@tonic-gate 	 */
298*0Sstevel@tonic-gate 	zone_key_create(&autofs_key, NULL, NULL, autofs_zone_destructor);
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	return (0);
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate static char *restropts[] = {
304*0Sstevel@tonic-gate 	RESTRICTED_MNTOPTS
305*0Sstevel@tonic-gate };
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate /*
308*0Sstevel@tonic-gate  * This routine adds those options to the option string `buf' which are
309*0Sstevel@tonic-gate  * forced by secpolicy_fs_mount.  If the automatic "security" options
310*0Sstevel@tonic-gate  * are set, the option string gets them added if they aren't already
311*0Sstevel@tonic-gate  * there.  We search the string with "strstr" and make sure that
312*0Sstevel@tonic-gate  * the string we find is bracketed with <start|",">MNTOPT<","|"\0">
313*0Sstevel@tonic-gate  *
314*0Sstevel@tonic-gate  * This is one half of the option inheritence algorithm which
315*0Sstevel@tonic-gate  * implements the "restrict" option.  The other half is implemented
316*0Sstevel@tonic-gate  * in automountd; it takes its cue from the options we add here.
317*0Sstevel@tonic-gate  */
318*0Sstevel@tonic-gate static int
319*0Sstevel@tonic-gate autofs_restrict_opts(struct vfs *vfsp, char *buf, size_t maxlen, size_t *curlen)
320*0Sstevel@tonic-gate {
321*0Sstevel@tonic-gate 	int i;
322*0Sstevel@tonic-gate 	char *p;
323*0Sstevel@tonic-gate 	size_t len = *curlen - 1;
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	/* Unrestricted */
326*0Sstevel@tonic-gate 	if (!vfs_optionisset(vfsp, restropts[0], NULL))
327*0Sstevel@tonic-gate 		return (0);
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	for (i = 0; i < sizeof (restropts)/sizeof (restropts[0]); i++) {
330*0Sstevel@tonic-gate 		size_t olen = strlen(restropts[i]);
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 		/* Add "restrict" always and the others insofar set */
333*0Sstevel@tonic-gate 		if ((i == 0 || vfs_optionisset(vfsp, restropts[i], NULL)) &&
334*0Sstevel@tonic-gate 		    ((p = strstr(buf, restropts[i])) == NULL ||
335*0Sstevel@tonic-gate 		    !((p == buf || p[-1] == ',') &&
336*0Sstevel@tonic-gate 		    (p[olen] == '\0' || p[olen] == ',')))) {
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 			if (len + olen + 1 > maxlen)
339*0Sstevel@tonic-gate 				return (-1);
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 			if (*buf != '\0')
342*0Sstevel@tonic-gate 				buf[len++] = ',';
343*0Sstevel@tonic-gate 			(void) strcpy(&buf[len], restropts[i]);
344*0Sstevel@tonic-gate 			len += olen;
345*0Sstevel@tonic-gate 		}
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate 	*curlen = len + 1;
348*0Sstevel@tonic-gate 	return (0);
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate /* ARGSUSED */
352*0Sstevel@tonic-gate static int
353*0Sstevel@tonic-gate auto_mount(vfs_t *vfsp, vnode_t *vp, struct mounta *uap, cred_t *cr)
354*0Sstevel@tonic-gate {
355*0Sstevel@tonic-gate 	int error;
356*0Sstevel@tonic-gate 	size_t len = 0;
357*0Sstevel@tonic-gate 	struct autofs_args args;
358*0Sstevel@tonic-gate 	fninfo_t *fnip = NULL;
359*0Sstevel@tonic-gate 	vnode_t *rootvp = NULL;
360*0Sstevel@tonic-gate 	fnnode_t *rootfnp = NULL;
361*0Sstevel@tonic-gate 	char *data = uap->dataptr;
362*0Sstevel@tonic-gate 	char datalen = uap->datalen;
363*0Sstevel@tonic-gate 	dev_t autofs_dev;
364*0Sstevel@tonic-gate 	char strbuff[MAXPATHLEN+1];
365*0Sstevel@tonic-gate 	vnode_t *kvp;
366*0Sstevel@tonic-gate 	struct autofs_globals *fngp;
367*0Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_mount: vfs %p vp %p\n", (void *)vfsp,
370*0Sstevel@tonic-gate 	    (void *)vp));
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, vp, vfsp)) != 0)
373*0Sstevel@tonic-gate 		return (EPERM);
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	if (zone == global_zone) {
376*0Sstevel@tonic-gate 		zone_t *mntzone;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
379*0Sstevel@tonic-gate 		ASSERT(mntzone != NULL);
380*0Sstevel@tonic-gate 		zone_rele(mntzone);
381*0Sstevel@tonic-gate 		if (mntzone != zone) {
382*0Sstevel@tonic-gate 			return (EBUSY);
383*0Sstevel@tonic-gate 		}
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	/*
387*0Sstevel@tonic-gate 	 * Stop the mount from going any further if the zone is going away.
388*0Sstevel@tonic-gate 	 */
389*0Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN)
390*0Sstevel@tonic-gate 		return (EBUSY);
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	/*
393*0Sstevel@tonic-gate 	 * We need a lock to serialize this; minor_lock is as good as any.
394*0Sstevel@tonic-gate 	 */
395*0Sstevel@tonic-gate 	mutex_enter(&autofs_minor_lock);
396*0Sstevel@tonic-gate 	if ((fngp = zone_getspecific(autofs_key, zone)) == NULL) {
397*0Sstevel@tonic-gate 		fngp = autofs_zone_init();
398*0Sstevel@tonic-gate 		(void) zone_setspecific(autofs_key, zone, fngp);
399*0Sstevel@tonic-gate 	}
400*0Sstevel@tonic-gate 	mutex_exit(&autofs_minor_lock);
401*0Sstevel@tonic-gate 	ASSERT(fngp != NULL);
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	/*
404*0Sstevel@tonic-gate 	 * Get arguments
405*0Sstevel@tonic-gate 	 */
406*0Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE) {
407*0Sstevel@tonic-gate 		if (datalen != sizeof (args))
408*0Sstevel@tonic-gate 			return (EINVAL);
409*0Sstevel@tonic-gate 		error = kcopy(data, &args, sizeof (args));
410*0Sstevel@tonic-gate 	} else {
411*0Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
412*0Sstevel@tonic-gate 			if (datalen != sizeof (args))
413*0Sstevel@tonic-gate 				return (EINVAL);
414*0Sstevel@tonic-gate 			error = copyin(data, &args, sizeof (args));
415*0Sstevel@tonic-gate 		} else {
416*0Sstevel@tonic-gate 			struct autofs_args32 args32;
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 			if (datalen != sizeof (args32))
419*0Sstevel@tonic-gate 				return (EINVAL);
420*0Sstevel@tonic-gate 			error = copyin(data, &args32, sizeof (args32));
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 			args.addr.maxlen = args32.addr.maxlen;
423*0Sstevel@tonic-gate 			args.addr.len = args32.addr.len;
424*0Sstevel@tonic-gate 			args.addr.buf = (char *)(uintptr_t)args32.addr.buf;
425*0Sstevel@tonic-gate 			args.path = (char *)(uintptr_t)args32.path;
426*0Sstevel@tonic-gate 			args.opts = (char *)(uintptr_t)args32.opts;
427*0Sstevel@tonic-gate 			args.map = (char *)(uintptr_t)args32.map;
428*0Sstevel@tonic-gate 			args.subdir = (char *)(uintptr_t)args32.subdir;
429*0Sstevel@tonic-gate 			args.key = (char *)(uintptr_t)args32.key;
430*0Sstevel@tonic-gate 			args.mount_to = args32.mount_to;
431*0Sstevel@tonic-gate 			args.rpc_to = args32.rpc_to;
432*0Sstevel@tonic-gate 			args.direct = args32.direct;
433*0Sstevel@tonic-gate 		}
434*0Sstevel@tonic-gate 	}
435*0Sstevel@tonic-gate 	if (error)
436*0Sstevel@tonic-gate 		return (EFAULT);
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	/*
439*0Sstevel@tonic-gate 	 * For a remount, only update mount information
440*0Sstevel@tonic-gate 	 * i.e. default mount options, map name, etc.
441*0Sstevel@tonic-gate 	 */
442*0Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT) {
443*0Sstevel@tonic-gate 		fnip = vfstofni(vfsp);
444*0Sstevel@tonic-gate 		if (fnip == NULL)
445*0Sstevel@tonic-gate 			return (EINVAL);
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 		if (args.direct == 1)
448*0Sstevel@tonic-gate 			fnip->fi_flags |= MF_DIRECT;
449*0Sstevel@tonic-gate 		else
450*0Sstevel@tonic-gate 			fnip->fi_flags &= ~MF_DIRECT;
451*0Sstevel@tonic-gate 		fnip->fi_mount_to = args.mount_to;
452*0Sstevel@tonic-gate 		fnip->fi_rpc_to = args.rpc_to;
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 		/*
455*0Sstevel@tonic-gate 		 * Get default options
456*0Sstevel@tonic-gate 		 */
457*0Sstevel@tonic-gate 		if (uap->flags & MS_SYSSPACE)
458*0Sstevel@tonic-gate 			error = copystr(args.opts, strbuff, sizeof (strbuff),
459*0Sstevel@tonic-gate 			    &len);
460*0Sstevel@tonic-gate 		else
461*0Sstevel@tonic-gate 			error = copyinstr(args.opts, strbuff, sizeof (strbuff),
462*0Sstevel@tonic-gate 			    &len);
463*0Sstevel@tonic-gate 		if (error)
464*0Sstevel@tonic-gate 			return (EFAULT);
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 		if (autofs_restrict_opts(vfsp, strbuff, sizeof (strbuff), &len)
467*0Sstevel@tonic-gate 		    != 0) {
468*0Sstevel@tonic-gate 			return (EFAULT);
469*0Sstevel@tonic-gate 		}
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 		kmem_free(fnip->fi_opts, fnip->fi_optslen);
472*0Sstevel@tonic-gate 		fnip->fi_opts = kmem_alloc(len, KM_SLEEP);
473*0Sstevel@tonic-gate 		fnip->fi_optslen = (int)len;
474*0Sstevel@tonic-gate 		bcopy(strbuff, fnip->fi_opts, len);
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 		/*
477*0Sstevel@tonic-gate 		 * Get context/map name
478*0Sstevel@tonic-gate 		 */
479*0Sstevel@tonic-gate 		if (uap->flags & MS_SYSSPACE)
480*0Sstevel@tonic-gate 			error = copystr(args.map, strbuff, sizeof (strbuff),
481*0Sstevel@tonic-gate 			    &len);
482*0Sstevel@tonic-gate 		else
483*0Sstevel@tonic-gate 			error = copyinstr(args.map, strbuff, sizeof (strbuff),
484*0Sstevel@tonic-gate 			    &len);
485*0Sstevel@tonic-gate 		if (error)
486*0Sstevel@tonic-gate 			return (EFAULT);
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 		kmem_free(fnip->fi_map, fnip->fi_maplen);
489*0Sstevel@tonic-gate 		fnip->fi_map = kmem_alloc(len, KM_SLEEP);
490*0Sstevel@tonic-gate 		fnip->fi_maplen = (int)len;
491*0Sstevel@tonic-gate 		bcopy(strbuff, fnip->fi_map, len);
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 		return (0);
494*0Sstevel@tonic-gate 	}
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 	/*
497*0Sstevel@tonic-gate 	 * Allocate fninfo struct and attach it to vfs
498*0Sstevel@tonic-gate 	 */
499*0Sstevel@tonic-gate 	fnip = kmem_zalloc(sizeof (*fnip), KM_SLEEP);
500*0Sstevel@tonic-gate 	fnip->fi_mountvfs = vfsp;
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	fnip->fi_mount_to = args.mount_to;
503*0Sstevel@tonic-gate 	fnip->fi_rpc_to = args.rpc_to;
504*0Sstevel@tonic-gate 	fnip->fi_refcnt = 0;
505*0Sstevel@tonic-gate 	vfsp->vfs_bsize = AUTOFS_BLOCKSIZE;
506*0Sstevel@tonic-gate 	vfsp->vfs_fstype = autofs_fstype;
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 	/*
509*0Sstevel@tonic-gate 	 * Assign a unique device id to the mount
510*0Sstevel@tonic-gate 	 */
511*0Sstevel@tonic-gate 	mutex_enter(&autofs_minor_lock);
512*0Sstevel@tonic-gate 	do {
513*0Sstevel@tonic-gate 		autofs_minor = (autofs_minor + 1) & L_MAXMIN32;
514*0Sstevel@tonic-gate 		autofs_dev = makedevice(autofs_major, autofs_minor);
515*0Sstevel@tonic-gate 	} while (vfs_devismounted(autofs_dev));
516*0Sstevel@tonic-gate 	mutex_exit(&autofs_minor_lock);
517*0Sstevel@tonic-gate 	vfsp->vfs_dev = autofs_dev;
518*0Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, autofs_dev, autofs_fstype);
519*0Sstevel@tonic-gate 	vfsp->vfs_data = (void *)fnip;
520*0Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	/*
523*0Sstevel@tonic-gate 	 * Get daemon address
524*0Sstevel@tonic-gate 	 */
525*0Sstevel@tonic-gate 	fnip->fi_addr.len = args.addr.len;
526*0Sstevel@tonic-gate 	fnip->fi_addr.maxlen = fnip->fi_addr.len;
527*0Sstevel@tonic-gate 	fnip->fi_addr.buf = kmem_alloc(args.addr.len, KM_SLEEP);
528*0Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
529*0Sstevel@tonic-gate 		error = kcopy(args.addr.buf, fnip->fi_addr.buf, args.addr.len);
530*0Sstevel@tonic-gate 	else
531*0Sstevel@tonic-gate 		error = copyin(args.addr.buf, fnip->fi_addr.buf, args.addr.len);
532*0Sstevel@tonic-gate 	if (error) {
533*0Sstevel@tonic-gate 		error = EFAULT;
534*0Sstevel@tonic-gate 		goto errout;
535*0Sstevel@tonic-gate 	}
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	fnip->fi_zoneid = getzoneid();
538*0Sstevel@tonic-gate 	/*
539*0Sstevel@tonic-gate 	 * Get path for mountpoint
540*0Sstevel@tonic-gate 	 */
541*0Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
542*0Sstevel@tonic-gate 		error = copystr(args.path, strbuff, sizeof (strbuff), &len);
543*0Sstevel@tonic-gate 	else
544*0Sstevel@tonic-gate 		error = copyinstr(args.path, strbuff, sizeof (strbuff), &len);
545*0Sstevel@tonic-gate 	if (error) {
546*0Sstevel@tonic-gate 		error = EFAULT;
547*0Sstevel@tonic-gate 		goto errout;
548*0Sstevel@tonic-gate 	}
549*0Sstevel@tonic-gate 	fnip->fi_path = kmem_alloc(len, KM_SLEEP);
550*0Sstevel@tonic-gate 	fnip->fi_pathlen = (int)len;
551*0Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_path, len);
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 	/*
554*0Sstevel@tonic-gate 	 * Get default options
555*0Sstevel@tonic-gate 	 */
556*0Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
557*0Sstevel@tonic-gate 		error = copystr(args.opts, strbuff, sizeof (strbuff), &len);
558*0Sstevel@tonic-gate 	else
559*0Sstevel@tonic-gate 		error = copyinstr(args.opts, strbuff, sizeof (strbuff), &len);
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	if (error != 0 ||
562*0Sstevel@tonic-gate 	    autofs_restrict_opts(vfsp, strbuff, sizeof (strbuff), &len) != 0) {
563*0Sstevel@tonic-gate 		error = EFAULT;
564*0Sstevel@tonic-gate 		goto errout;
565*0Sstevel@tonic-gate 	}
566*0Sstevel@tonic-gate 	fnip->fi_opts = kmem_alloc(len, KM_SLEEP);
567*0Sstevel@tonic-gate 	fnip->fi_optslen = (int)len;
568*0Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_opts, len);
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	/*
571*0Sstevel@tonic-gate 	 * Get context/map name
572*0Sstevel@tonic-gate 	 */
573*0Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
574*0Sstevel@tonic-gate 		error = copystr(args.map, strbuff, sizeof (strbuff), &len);
575*0Sstevel@tonic-gate 	else
576*0Sstevel@tonic-gate 		error = copyinstr(args.map, strbuff, sizeof (strbuff), &len);
577*0Sstevel@tonic-gate 	if (error) {
578*0Sstevel@tonic-gate 		error = EFAULT;
579*0Sstevel@tonic-gate 		goto errout;
580*0Sstevel@tonic-gate 	}
581*0Sstevel@tonic-gate 	fnip->fi_map = kmem_alloc(len, KM_SLEEP);
582*0Sstevel@tonic-gate 	fnip->fi_maplen = (int)len;
583*0Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_map, len);
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	/*
586*0Sstevel@tonic-gate 	 * Get subdirectory within map
587*0Sstevel@tonic-gate 	 */
588*0Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
589*0Sstevel@tonic-gate 		error = copystr(args.subdir, strbuff, sizeof (strbuff), &len);
590*0Sstevel@tonic-gate 	else
591*0Sstevel@tonic-gate 		error = copyinstr(args.subdir, strbuff, sizeof (strbuff), &len);
592*0Sstevel@tonic-gate 	if (error) {
593*0Sstevel@tonic-gate 		error = EFAULT;
594*0Sstevel@tonic-gate 		goto errout;
595*0Sstevel@tonic-gate 	}
596*0Sstevel@tonic-gate 	fnip->fi_subdir = kmem_alloc(len, KM_SLEEP);
597*0Sstevel@tonic-gate 	fnip->fi_subdirlen = (int)len;
598*0Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_subdir, len);
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate 	/*
601*0Sstevel@tonic-gate 	 * Get the key
602*0Sstevel@tonic-gate 	 */
603*0Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
604*0Sstevel@tonic-gate 		error = copystr(args.key, strbuff, sizeof (strbuff), &len);
605*0Sstevel@tonic-gate 	else
606*0Sstevel@tonic-gate 		error = copyinstr(args.key, strbuff, sizeof (strbuff), &len);
607*0Sstevel@tonic-gate 	if (error) {
608*0Sstevel@tonic-gate 		error = EFAULT;
609*0Sstevel@tonic-gate 		goto errout;
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 	fnip->fi_key = kmem_alloc(len, KM_SLEEP);
612*0Sstevel@tonic-gate 	fnip->fi_keylen = (int)len;
613*0Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_key, len);
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	/*
616*0Sstevel@tonic-gate 	 * Is this a direct mount?
617*0Sstevel@tonic-gate 	 */
618*0Sstevel@tonic-gate 	if (args.direct == 1)
619*0Sstevel@tonic-gate 		fnip->fi_flags |= MF_DIRECT;
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	/*
622*0Sstevel@tonic-gate 	 * Setup netconfig.
623*0Sstevel@tonic-gate 	 * Can I pass in knconf as mount argument? what
624*0Sstevel@tonic-gate 	 * happens when the daemon gets restarted?
625*0Sstevel@tonic-gate 	 */
626*0Sstevel@tonic-gate 	if ((error = lookupname("/dev/ticotsord", UIO_SYSSPACE, FOLLOW,
627*0Sstevel@tonic-gate 	    NULLVPP, &kvp)) != 0) {
628*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "autofs: lookupname: %d", error);
629*0Sstevel@tonic-gate 		goto errout;
630*0Sstevel@tonic-gate 	}
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	fnip->fi_knconf.knc_rdev = kvp->v_rdev;
633*0Sstevel@tonic-gate 	fnip->fi_knconf.knc_protofmly = NC_LOOPBACK;
634*0Sstevel@tonic-gate 	fnip->fi_knconf.knc_semantics = NC_TPI_COTS_ORD;
635*0Sstevel@tonic-gate 	VN_RELE(kvp);
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	/*
638*0Sstevel@tonic-gate 	 * Make the root vnode
639*0Sstevel@tonic-gate 	 */
640*0Sstevel@tonic-gate 	rootfnp = auto_makefnnode(VDIR, vfsp, fnip->fi_path, cr, fngp);
641*0Sstevel@tonic-gate 	if (rootfnp == NULL) {
642*0Sstevel@tonic-gate 		error = ENOMEM;
643*0Sstevel@tonic-gate 		goto errout;
644*0Sstevel@tonic-gate 	}
645*0Sstevel@tonic-gate 	rootvp = fntovn(rootfnp);
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate 	rootvp->v_flag |= VROOT;
648*0Sstevel@tonic-gate 	rootfnp->fn_mode = AUTOFS_MODE;
649*0Sstevel@tonic-gate 	rootfnp->fn_parent = rootfnp;
650*0Sstevel@tonic-gate 	/* account for ".." entry */
651*0Sstevel@tonic-gate 	rootfnp->fn_linkcnt = rootfnp->fn_size = 1;
652*0Sstevel@tonic-gate 	fnip->fi_rootvp = rootvp;
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 	/*
655*0Sstevel@tonic-gate 	 * Add to list of top level AUTOFS' if it is being mounted by
656*0Sstevel@tonic-gate 	 * a user level process.
657*0Sstevel@tonic-gate 	 */
658*0Sstevel@tonic-gate 	if (!(uap->flags & MS_SYSSPACE)) {
659*0Sstevel@tonic-gate 		rw_enter(&fngp->fng_rootfnnodep->fn_rwlock, RW_WRITER);
660*0Sstevel@tonic-gate 		rootfnp->fn_parent = fngp->fng_rootfnnodep;
661*0Sstevel@tonic-gate 		rootfnp->fn_next = fngp->fng_rootfnnodep->fn_dirents;
662*0Sstevel@tonic-gate 		fngp->fng_rootfnnodep->fn_dirents = rootfnp;
663*0Sstevel@tonic-gate 		rw_exit(&fngp->fng_rootfnnodep->fn_rwlock);
664*0Sstevel@tonic-gate 	}
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mount: vfs %p root %p fnip %p return %d\n",
667*0Sstevel@tonic-gate 	    (void *)vfsp, (void *)rootvp, (void *)fnip, error));
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	return (0);
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate errout:
672*0Sstevel@tonic-gate 	ASSERT(fnip != NULL);
673*0Sstevel@tonic-gate 	ASSERT((uap->flags & MS_REMOUNT) == 0);
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 	if (fnip->fi_addr.buf != NULL)
676*0Sstevel@tonic-gate 		kmem_free(fnip->fi_addr.buf, fnip->fi_addr.len);
677*0Sstevel@tonic-gate 	if (fnip->fi_path != NULL)
678*0Sstevel@tonic-gate 		kmem_free(fnip->fi_path, fnip->fi_pathlen);
679*0Sstevel@tonic-gate 	if (fnip->fi_opts != NULL)
680*0Sstevel@tonic-gate 		kmem_free(fnip->fi_opts, fnip->fi_optslen);
681*0Sstevel@tonic-gate 	if (fnip->fi_map != NULL)
682*0Sstevel@tonic-gate 		kmem_free(fnip->fi_map, fnip->fi_maplen);
683*0Sstevel@tonic-gate 	if (fnip->fi_subdir != NULL)
684*0Sstevel@tonic-gate 		kmem_free(fnip->fi_subdir, fnip->fi_subdirlen);
685*0Sstevel@tonic-gate 	if (fnip->fi_key != NULL)
686*0Sstevel@tonic-gate 		kmem_free(fnip->fi_key, fnip->fi_keylen);
687*0Sstevel@tonic-gate 	kmem_free(fnip, sizeof (*fnip));
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mount: vfs %p root %p fnip %p return %d\n",
690*0Sstevel@tonic-gate 	    (void *)vfsp, (void *)rootvp, (void *)fnip, error));
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	return (error);
693*0Sstevel@tonic-gate }
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate /* ARGSUSED */
696*0Sstevel@tonic-gate static int
697*0Sstevel@tonic-gate auto_unmount(vfs_t *vfsp, int flag, cred_t *cr)
698*0Sstevel@tonic-gate {
699*0Sstevel@tonic-gate 	fninfo_t *fnip;
700*0Sstevel@tonic-gate 	vnode_t *rvp;
701*0Sstevel@tonic-gate 	fnnode_t *rfnp, *fnp, *pfnp;
702*0Sstevel@tonic-gate 	fnnode_t *myrootfnnodep;
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 	fnip = vfstofni(vfsp);
705*0Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_unmount vfsp %p fnip %p\n", (void *)vfsp,
706*0Sstevel@tonic-gate 			(void *)fnip));
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
709*0Sstevel@tonic-gate 		return (EPERM);
710*0Sstevel@tonic-gate 	/*
711*0Sstevel@tonic-gate 	 * forced unmount is not supported by this file system
712*0Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
713*0Sstevel@tonic-gate 	 */
714*0Sstevel@tonic-gate 	if (flag & MS_FORCE)
715*0Sstevel@tonic-gate 		return (ENOTSUP);
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(vfsp->vfs_vnodecovered));
718*0Sstevel@tonic-gate 	rvp = fnip->fi_rootvp;
719*0Sstevel@tonic-gate 	rfnp = vntofn(rvp);
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 	if (rvp->v_count > 1 || rfnp->fn_dirents != NULL)
722*0Sstevel@tonic-gate 		return (EBUSY);
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate 	/*
725*0Sstevel@tonic-gate 	 * The root vnode is on the linked list of root fnnodes only if
726*0Sstevel@tonic-gate 	 * this was not a trigger node. Since we have no way of knowing,
727*0Sstevel@tonic-gate 	 * if we don't find it, then we assume it was a trigger node.
728*0Sstevel@tonic-gate 	 */
729*0Sstevel@tonic-gate 	myrootfnnodep = rfnp->fn_globals->fng_rootfnnodep;
730*0Sstevel@tonic-gate 	pfnp = NULL;
731*0Sstevel@tonic-gate 	rw_enter(&myrootfnnodep->fn_rwlock, RW_WRITER);
732*0Sstevel@tonic-gate 	fnp = myrootfnnodep->fn_dirents;
733*0Sstevel@tonic-gate 	while (fnp != NULL) {
734*0Sstevel@tonic-gate 		if (fnp == rfnp) {
735*0Sstevel@tonic-gate 			/*
736*0Sstevel@tonic-gate 			 * A check here is made to see if rvp is busy.  If
737*0Sstevel@tonic-gate 			 * so, return EBUSY.  Otherwise proceed with
738*0Sstevel@tonic-gate 			 * disconnecting it from the list.
739*0Sstevel@tonic-gate 			 */
740*0Sstevel@tonic-gate 			if (rvp->v_count > 1 || rfnp->fn_dirents != NULL) {
741*0Sstevel@tonic-gate 				rw_exit(&myrootfnnodep->fn_rwlock);
742*0Sstevel@tonic-gate 				return (EBUSY);
743*0Sstevel@tonic-gate 			}
744*0Sstevel@tonic-gate 			if (pfnp)
745*0Sstevel@tonic-gate 				pfnp->fn_next = fnp->fn_next;
746*0Sstevel@tonic-gate 			else
747*0Sstevel@tonic-gate 				myrootfnnodep->fn_dirents = fnp->fn_next;
748*0Sstevel@tonic-gate 			fnp->fn_next = NULL;
749*0Sstevel@tonic-gate 			break;
750*0Sstevel@tonic-gate 		}
751*0Sstevel@tonic-gate 		pfnp = fnp;
752*0Sstevel@tonic-gate 		fnp = fnp->fn_next;
753*0Sstevel@tonic-gate 	}
754*0Sstevel@tonic-gate 	rw_exit(&myrootfnnodep->fn_rwlock);
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate 	ASSERT(rvp->v_count == 1);
757*0Sstevel@tonic-gate 	ASSERT(rfnp->fn_size == 1);
758*0Sstevel@tonic-gate 	ASSERT(rfnp->fn_linkcnt == 1);
759*0Sstevel@tonic-gate 	/*
760*0Sstevel@tonic-gate 	 * The following drops linkcnt to 0, therefore the disconnect is
761*0Sstevel@tonic-gate 	 * not attempted when auto_inactive() is called by
762*0Sstevel@tonic-gate 	 * vn_rele(). This is necessary because we have nothing to get
763*0Sstevel@tonic-gate 	 * disconnected from since we're the root of the filesystem. As a
764*0Sstevel@tonic-gate 	 * side effect the node is not freed, therefore I should free the
765*0Sstevel@tonic-gate 	 * node here.
766*0Sstevel@tonic-gate 	 *
767*0Sstevel@tonic-gate 	 * XXX - I really need to think of a better way of doing this.
768*0Sstevel@tonic-gate 	 */
769*0Sstevel@tonic-gate 	rfnp->fn_size--;
770*0Sstevel@tonic-gate 	rfnp->fn_linkcnt--;
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 	/*
773*0Sstevel@tonic-gate 	 * release of last reference causes node
774*0Sstevel@tonic-gate 	 * to be freed
775*0Sstevel@tonic-gate 	 */
776*0Sstevel@tonic-gate 	VN_RELE(rvp);
777*0Sstevel@tonic-gate 	rfnp->fn_parent = NULL;
778*0Sstevel@tonic-gate 
779*0Sstevel@tonic-gate 	auto_freefnnode(rfnp);
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	kmem_free(fnip->fi_addr.buf, fnip->fi_addr.len);
782*0Sstevel@tonic-gate 	kmem_free(fnip->fi_path, fnip->fi_pathlen);
783*0Sstevel@tonic-gate 	kmem_free(fnip->fi_map, fnip->fi_maplen);
784*0Sstevel@tonic-gate 	kmem_free(fnip->fi_subdir, fnip->fi_subdirlen);
785*0Sstevel@tonic-gate 	kmem_free(fnip->fi_key, fnip->fi_keylen);
786*0Sstevel@tonic-gate 	kmem_free(fnip->fi_opts, fnip->fi_optslen);
787*0Sstevel@tonic-gate 	kmem_free(fnip, sizeof (*fnip));
788*0Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_unmount: return=0\n"));
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	return (0);
791*0Sstevel@tonic-gate }
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 
794*0Sstevel@tonic-gate /*
795*0Sstevel@tonic-gate  * find root of autofs
796*0Sstevel@tonic-gate  */
797*0Sstevel@tonic-gate static int
798*0Sstevel@tonic-gate auto_root(vfs_t *vfsp, vnode_t **vpp)
799*0Sstevel@tonic-gate {
800*0Sstevel@tonic-gate 	*vpp = (vnode_t *)vfstofni(vfsp)->fi_rootvp;
801*0Sstevel@tonic-gate 	VN_HOLD(*vpp);
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_root: vfs %p, *vpp %p\n", (void *)vfsp,
804*0Sstevel@tonic-gate 	    (void *)*vpp));
805*0Sstevel@tonic-gate 	return (0);
806*0Sstevel@tonic-gate }
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate /*
809*0Sstevel@tonic-gate  * Get file system statistics.
810*0Sstevel@tonic-gate  */
811*0Sstevel@tonic-gate static int
812*0Sstevel@tonic-gate auto_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
813*0Sstevel@tonic-gate {
814*0Sstevel@tonic-gate 	dev32_t d32;
815*0Sstevel@tonic-gate 
816*0Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_statvfs %p\n", (void *)vfsp));
817*0Sstevel@tonic-gate 
818*0Sstevel@tonic-gate 	bzero(sbp, sizeof (*sbp));
819*0Sstevel@tonic-gate 	sbp->f_bsize	= vfsp->vfs_bsize;
820*0Sstevel@tonic-gate 	sbp->f_frsize	= sbp->f_bsize;
821*0Sstevel@tonic-gate 	sbp->f_blocks	= (fsblkcnt64_t)0;
822*0Sstevel@tonic-gate 	sbp->f_bfree	= (fsblkcnt64_t)0;
823*0Sstevel@tonic-gate 	sbp->f_bavail	= (fsblkcnt64_t)0;
824*0Sstevel@tonic-gate 	sbp->f_files	= (fsfilcnt64_t)0;
825*0Sstevel@tonic-gate 	sbp->f_ffree	= (fsfilcnt64_t)0;
826*0Sstevel@tonic-gate 	sbp->f_favail	= (fsfilcnt64_t)0;
827*0Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
828*0Sstevel@tonic-gate 	sbp->f_fsid	= d32;
829*0Sstevel@tonic-gate 	(void) strcpy(sbp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
830*0Sstevel@tonic-gate 	sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
831*0Sstevel@tonic-gate 	sbp->f_namemax = MAXNAMELEN;
832*0Sstevel@tonic-gate 	(void) strcpy(sbp->f_fstr, MNTTYPE_AUTOFS);
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 	return (0);
835*0Sstevel@tonic-gate }
836