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