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) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2016 Toomas Soome <tsoome@me.com>
26 */
27
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
30
31 /*
32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 * The Regents of the University of California
34 * All Rights Reserved
35 *
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
38 * contributors.
39 */
40
41 #include <sys/types.h>
42 #include <sys/t_lock.h>
43 #include <sys/param.h>
44 #include <sys/errno.h>
45 #include <sys/user.h>
46 #include <sys/fstyp.h>
47 #include <sys/kmem.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/vfs.h>
52 #include <sys/vfs_opreg.h>
53 #include <sys/fem.h>
54 #include <sys/mntent.h>
55 #include <sys/stat.h>
56 #include <sys/statvfs.h>
57 #include <sys/statfs.h>
58 #include <sys/cred.h>
59 #include <sys/vnode.h>
60 #include <sys/rwstlock.h>
61 #include <sys/dnlc.h>
62 #include <sys/file.h>
63 #include <sys/time.h>
64 #include <sys/atomic.h>
65 #include <sys/cmn_err.h>
66 #include <sys/buf.h>
67 #include <sys/swap.h>
68 #include <sys/debug.h>
69 #include <sys/vnode.h>
70 #include <sys/modctl.h>
71 #include <sys/ddi.h>
72 #include <sys/pathname.h>
73 #include <sys/bootconf.h>
74 #include <sys/dumphdr.h>
75 #include <sys/dc_ki.h>
76 #include <sys/poll.h>
77 #include <sys/sunddi.h>
78 #include <sys/sysmacros.h>
79 #include <sys/zone.h>
80 #include <sys/policy.h>
81 #include <sys/ctfs.h>
82 #include <sys/objfs.h>
83 #include <sys/console.h>
84 #include <sys/reboot.h>
85 #include <sys/attr.h>
86 #include <sys/zio.h>
87 #include <sys/spa.h>
88 #include <sys/lofi.h>
89 #include <sys/bootprops.h>
90
91 #include <vm/page.h>
92
93 #include <fs/fs_subr.h>
94 /* Private interfaces to create vopstats-related data structures */
95 extern void initialize_vopstats(vopstats_t *);
96 extern vopstats_t *get_fstype_vopstats(struct vfs *, struct vfssw *);
97 extern vsk_anchor_t *get_vskstat_anchor(struct vfs *);
98
99 static void vfs_clearmntopt_nolock(mntopts_t *, const char *, int);
100 static void vfs_setmntopt_nolock(mntopts_t *, const char *,
101 const char *, int, int);
102 static int vfs_optionisset_nolock(const mntopts_t *, const char *, char **);
103 static void vfs_freemnttab(struct vfs *);
104 static void vfs_freeopt(mntopt_t *);
105 static void vfs_swapopttbl_nolock(mntopts_t *, mntopts_t *);
106 static void vfs_swapopttbl(mntopts_t *, mntopts_t *);
107 static void vfs_copyopttbl_extend(const mntopts_t *, mntopts_t *, int);
108 static void vfs_createopttbl_extend(mntopts_t *, const char *,
109 const mntopts_t *);
110 static char **vfs_copycancelopt_extend(char **const, int);
111 static void vfs_freecancelopt(char **);
112 static void getrootfs(char **, char **);
113 static int getmacpath(dev_info_t *, void *);
114 static void vfs_mnttabvp_setup(void);
115
116 struct ipmnt {
117 struct ipmnt *mip_next;
118 dev_t mip_dev;
119 struct vfs *mip_vfsp;
120 };
121
122 static kmutex_t vfs_miplist_mutex;
123 static struct ipmnt *vfs_miplist = NULL;
124 static struct ipmnt *vfs_miplist_end = NULL;
125
126 static kmem_cache_t *vfs_cache; /* Pointer to VFS kmem cache */
127
128 /*
129 * VFS global data.
130 */
131 vnode_t *rootdir; /* pointer to root inode vnode. */
132 vnode_t *devicesdir; /* pointer to inode of devices root */
133 vnode_t *devdir; /* pointer to inode of dev root */
134
135 char *server_rootpath; /* root path for diskless clients */
136 char *server_hostname; /* hostname of diskless server */
137
138 static struct vfs root;
139 static struct vfs devices;
140 static struct vfs dev;
141 struct vfs *rootvfs = &root; /* pointer to root vfs; head of VFS list. */
142 rvfs_t *rvfs_list; /* array of vfs ptrs for vfs hash list */
143 int vfshsz = 512; /* # of heads/locks in vfs hash arrays */
144 /* must be power of 2! */
145 timespec_t vfs_mnttab_ctime; /* mnttab created time */
146 timespec_t vfs_mnttab_mtime; /* mnttab last modified time */
147 char *vfs_dummyfstype = "\0";
148 struct pollhead vfs_pollhd; /* for mnttab pollers */
149 struct vnode *vfs_mntdummyvp; /* to fake mnttab read/write for file events */
150 int mntfstype; /* will be set once mnt fs is mounted */
151
152 /*
153 * Table for generic options recognized in the VFS layer and acted
154 * on at this level before parsing file system specific options.
155 * The nosuid option is stronger than any of the devices and setuid
156 * options, so those are canceled when nosuid is seen.
157 *
158 * All options which are added here need to be added to the
159 * list of standard options in usr/src/cmd/fs.d/fslib.c as well.
160 */
161 /*
162 * VFS Mount options table
163 */
164 static char *ro_cancel[] = { MNTOPT_RW, NULL };
165 static char *rw_cancel[] = { MNTOPT_RO, NULL };
166 static char *suid_cancel[] = { MNTOPT_NOSUID, NULL };
167 static char *nosuid_cancel[] = { MNTOPT_SUID, MNTOPT_DEVICES, MNTOPT_NODEVICES,
168 MNTOPT_NOSETUID, MNTOPT_SETUID, NULL };
169 static char *devices_cancel[] = { MNTOPT_NODEVICES, NULL };
170 static char *nodevices_cancel[] = { MNTOPT_DEVICES, NULL };
171 static char *setuid_cancel[] = { MNTOPT_NOSETUID, NULL };
172 static char *nosetuid_cancel[] = { MNTOPT_SETUID, NULL };
173 static char *nbmand_cancel[] = { MNTOPT_NONBMAND, NULL };
174 static char *nonbmand_cancel[] = { MNTOPT_NBMAND, NULL };
175 static char *exec_cancel[] = { MNTOPT_NOEXEC, NULL };
176 static char *noexec_cancel[] = { MNTOPT_EXEC, NULL };
177
178 static const mntopt_t mntopts[] = {
179 /*
180 * option name cancel options default arg flags
181 */
182 { MNTOPT_REMOUNT, NULL, NULL,
183 MO_NODISPLAY, (void *)0 },
184 { MNTOPT_RO, ro_cancel, NULL, 0,
185 (void *)0 },
186 { MNTOPT_RW, rw_cancel, NULL, 0,
187 (void *)0 },
188 { MNTOPT_SUID, suid_cancel, NULL, 0,
189 (void *)0 },
190 { MNTOPT_NOSUID, nosuid_cancel, NULL, 0,
191 (void *)0 },
192 { MNTOPT_DEVICES, devices_cancel, NULL, 0,
193 (void *)0 },
194 { MNTOPT_NODEVICES, nodevices_cancel, NULL, 0,
195 (void *)0 },
196 { MNTOPT_SETUID, setuid_cancel, NULL, 0,
197 (void *)0 },
198 { MNTOPT_NOSETUID, nosetuid_cancel, NULL, 0,
199 (void *)0 },
200 { MNTOPT_NBMAND, nbmand_cancel, NULL, 0,
201 (void *)0 },
202 { MNTOPT_NONBMAND, nonbmand_cancel, NULL, 0,
203 (void *)0 },
204 { MNTOPT_EXEC, exec_cancel, NULL, 0,
205 (void *)0 },
206 { MNTOPT_NOEXEC, noexec_cancel, NULL, 0,
207 (void *)0 },
208 };
209
210 const mntopts_t vfs_mntopts = {
211 sizeof (mntopts) / sizeof (mntopt_t),
212 (mntopt_t *)&mntopts[0]
213 };
214
215 /*
216 * File system operation dispatch functions.
217 */
218
219 int
220 fsop_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
221 {
222 return (*(vfsp)->vfs_op->vfs_mount)(vfsp, mvp, uap, cr);
223 }
224
225 int
226 fsop_unmount(vfs_t *vfsp, int flag, cred_t *cr)
227 {
228 return (*(vfsp)->vfs_op->vfs_unmount)(vfsp, flag, cr);
229 }
230
231 int
232 fsop_root(vfs_t *vfsp, vnode_t **vpp)
233 {
234 refstr_t *mntpt;
235 int ret = (*(vfsp)->vfs_op->vfs_root)(vfsp, vpp);
236 /*
237 * Make sure this root has a path. With lofs, it is possible to have
238 * a NULL mountpoint.
239 */
240 if (ret == 0 && vfsp->vfs_mntpt != NULL && (*vpp)->v_path == NULL) {
241 mntpt = vfs_getmntpoint(vfsp);
242 vn_setpath_str(*vpp, refstr_value(mntpt),
243 strlen(refstr_value(mntpt)));
244 refstr_rele(mntpt);
245 }
246
247 return (ret);
248 }
249
250 int
251 fsop_statfs(vfs_t *vfsp, statvfs64_t *sp)
252 {
253 return (*(vfsp)->vfs_op->vfs_statvfs)(vfsp, sp);
254 }
255
256 int
257 fsop_sync(vfs_t *vfsp, short flag, cred_t *cr)
258 {
259 return (*(vfsp)->vfs_op->vfs_sync)(vfsp, flag, cr);
260 }
261
262 int
263 fsop_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
264 {
265 /*
266 * In order to handle system attribute fids in a manner
267 * transparent to the underlying fs, we embed the fid for
268 * the sysattr parent object in the sysattr fid and tack on
269 * some extra bytes that only the sysattr layer knows about.
270 *
271 * This guarantees that sysattr fids are larger than other fids
272 * for this vfs. If the vfs supports the sysattr view interface
273 * (as indicated by VFSFT_SYSATTR_VIEWS), we cannot have a size
274 * collision with XATTR_FIDSZ.
275 */
276 if (vfs_has_feature(vfsp, VFSFT_SYSATTR_VIEWS) &&
277 fidp->fid_len == XATTR_FIDSZ)
278 return (xattr_dir_vget(vfsp, vpp, fidp));
279
280 return (*(vfsp)->vfs_op->vfs_vget)(vfsp, vpp, fidp);
281 }
282
283 int
284 fsop_mountroot(vfs_t *vfsp, enum whymountroot reason)
285 {
286 return (*(vfsp)->vfs_op->vfs_mountroot)(vfsp, reason);
287 }
288
289 void
290 fsop_freefs(vfs_t *vfsp)
291 {
292 (*(vfsp)->vfs_op->vfs_freevfs)(vfsp);
293 }
294
295 int
296 fsop_vnstate(vfs_t *vfsp, vnode_t *vp, vntrans_t nstate)
297 {
298 return ((*(vfsp)->vfs_op->vfs_vnstate)(vfsp, vp, nstate));
299 }
300
301 int
302 fsop_sync_by_kind(int fstype, short flag, cred_t *cr)
303 {
304 ASSERT((fstype >= 0) && (fstype < nfstype));
305
306 if (ALLOCATED_VFSSW(&vfssw[fstype]) && VFS_INSTALLED(&vfssw[fstype]))
307 return (*vfssw[fstype].vsw_vfsops.vfs_sync) (NULL, flag, cr);
308 else
309 return (ENOTSUP);
310 }
311
312 /*
313 * File system initialization. vfs_setfsops() must be called from a file
314 * system's init routine.
315 */
316
317 static int
318 fs_copyfsops(const fs_operation_def_t *template, vfsops_t *actual,
319 int *unused_ops)
320 {
321 static const fs_operation_trans_def_t vfs_ops_table[] = {
322 VFSNAME_MOUNT, offsetof(vfsops_t, vfs_mount),
323 fs_nosys, fs_nosys,
324
325 VFSNAME_UNMOUNT, offsetof(vfsops_t, vfs_unmount),
326 fs_nosys, fs_nosys,
327
328 VFSNAME_ROOT, offsetof(vfsops_t, vfs_root),
329 fs_nosys, fs_nosys,
330
331 VFSNAME_STATVFS, offsetof(vfsops_t, vfs_statvfs),
332 fs_nosys, fs_nosys,
333
334 VFSNAME_SYNC, offsetof(vfsops_t, vfs_sync),
335 (fs_generic_func_p) fs_sync,
336 (fs_generic_func_p) fs_sync, /* No errors allowed */
337
338 VFSNAME_VGET, offsetof(vfsops_t, vfs_vget),
339 fs_nosys, fs_nosys,
340
341 VFSNAME_MOUNTROOT, offsetof(vfsops_t, vfs_mountroot),
342 fs_nosys, fs_nosys,
343
344 VFSNAME_FREEVFS, offsetof(vfsops_t, vfs_freevfs),
345 (fs_generic_func_p)fs_freevfs,
346 (fs_generic_func_p)fs_freevfs, /* Shouldn't fail */
347
348 VFSNAME_VNSTATE, offsetof(vfsops_t, vfs_vnstate),
349 (fs_generic_func_p)fs_nosys,
350 (fs_generic_func_p)fs_nosys,
351
352 NULL, 0, NULL, NULL
353 };
354
355 return (fs_build_vector(actual, unused_ops, vfs_ops_table, template));
356 }
357
358 void
359 zfs_boot_init(void)
360 {
361 if (strcmp(rootfs.bo_fstype, MNTTYPE_ZFS) == 0)
362 spa_boot_init();
363 }
364
365 int
366 vfs_setfsops(int fstype, const fs_operation_def_t *template, vfsops_t **actual)
367 {
368 int error;
369 int unused_ops;
370
371 /*
372 * Verify that fstype refers to a valid fs. Note that
373 * 0 is valid since it's used to set "stray" ops.
374 */
375 if ((fstype < 0) || (fstype >= nfstype))
376 return (EINVAL);
377
378 if (!ALLOCATED_VFSSW(&vfssw[fstype]))
379 return (EINVAL);
380
381 /* Set up the operations vector. */
382
383 error = fs_copyfsops(template, &vfssw[fstype].vsw_vfsops, &unused_ops);
384
385 if (error != 0)
386 return (error);
387
388 vfssw[fstype].vsw_flag |= VSW_INSTALLED;
389
390 if (actual != NULL)
391 *actual = &vfssw[fstype].vsw_vfsops;
392
393 #if DEBUG
394 if (unused_ops != 0)
395 cmn_err(CE_WARN, "vfs_setfsops: %s: %d operations supplied "
396 "but not used", vfssw[fstype].vsw_name, unused_ops);
397 #endif
398
399 return (0);
400 }
401
402 int
403 vfs_makefsops(const fs_operation_def_t *template, vfsops_t **actual)
404 {
405 int error;
406 int unused_ops;
407
408 *actual = (vfsops_t *)kmem_alloc(sizeof (vfsops_t), KM_SLEEP);
409
410 error = fs_copyfsops(template, *actual, &unused_ops);
411 if (error != 0) {
412 kmem_free(*actual, sizeof (vfsops_t));
413 *actual = NULL;
414 return (error);
415 }
416
417 return (0);
418 }
419
420 /*
421 * Free a vfsops structure created as a result of vfs_makefsops().
422 * NOTE: For a vfsops structure initialized by vfs_setfsops(), use
423 * vfs_freevfsops_by_type().
424 */
425 void
426 vfs_freevfsops(vfsops_t *vfsops)
427 {
428 kmem_free(vfsops, sizeof (vfsops_t));
429 }
430
431 /*
432 * Since the vfsops structure is part of the vfssw table and wasn't
433 * really allocated, we're not really freeing anything. We keep
434 * the name for consistency with vfs_freevfsops(). We do, however,
435 * need to take care of a little bookkeeping.
436 * NOTE: For a vfsops structure created by vfs_setfsops(), use
437 * vfs_freevfsops_by_type().
438 */
439 int
440 vfs_freevfsops_by_type(int fstype)
441 {
442
443 /* Verify that fstype refers to a loaded fs (and not fsid 0). */
444 if ((fstype <= 0) || (fstype >= nfstype))
445 return (EINVAL);
446
447 WLOCK_VFSSW();
448 if ((vfssw[fstype].vsw_flag & VSW_INSTALLED) == 0) {
449 WUNLOCK_VFSSW();
450 return (EINVAL);
451 }
452
453 vfssw[fstype].vsw_flag &= ~VSW_INSTALLED;
454 WUNLOCK_VFSSW();
455
456 return (0);
457 }
458
459 /* Support routines used to reference vfs_op */
460
461 /* Set the operations vector for a vfs */
462 void
463 vfs_setops(vfs_t *vfsp, vfsops_t *vfsops)
464 {
465 vfsops_t *op;
466
467 ASSERT(vfsp != NULL);
468 ASSERT(vfsops != NULL);
469
470 op = vfsp->vfs_op;
471 membar_consumer();
472 if (vfsp->vfs_femhead == NULL &&
473 atomic_cas_ptr(&vfsp->vfs_op, op, vfsops) == op) {
474 return;
475 }
476 fsem_setvfsops(vfsp, vfsops);
477 }
478
479 /* Retrieve the operations vector for a vfs */
480 vfsops_t *
481 vfs_getops(vfs_t *vfsp)
482 {
483 vfsops_t *op;
484
485 ASSERT(vfsp != NULL);
486
487 op = vfsp->vfs_op;
488 membar_consumer();
489 if (vfsp->vfs_femhead == NULL && op == vfsp->vfs_op) {
490 return (op);
491 } else {
492 return (fsem_getvfsops(vfsp));
493 }
494 }
495
496 /*
497 * Returns non-zero (1) if the vfsops matches that of the vfs.
498 * Returns zero (0) if not.
499 */
500 int
501 vfs_matchops(vfs_t *vfsp, vfsops_t *vfsops)
502 {
503 return (vfs_getops(vfsp) == vfsops);
504 }
505
506 /*
507 * Returns non-zero (1) if the file system has installed a non-default,
508 * non-error vfs_sync routine. Returns zero (0) otherwise.
509 */
510 int
511 vfs_can_sync(vfs_t *vfsp)
512 {
513 /* vfs_sync() routine is not the default/error function */
514 return (vfs_getops(vfsp)->vfs_sync != fs_sync);
515 }
516
517 /*
518 * Initialize a vfs structure.
519 */
520 void
521 vfs_init(vfs_t *vfsp, vfsops_t *op, void *data)
522 {
523 /* Other initialization has been moved to vfs_alloc() */
524 vfsp->vfs_count = 0;
525 vfsp->vfs_next = vfsp;
526 vfsp->vfs_prev = vfsp;
527 vfsp->vfs_zone_next = vfsp;
528 vfsp->vfs_zone_prev = vfsp;
529 vfsp->vfs_lofi_id = 0;
530 sema_init(&vfsp->vfs_reflock, 1, NULL, SEMA_DEFAULT, NULL);
531 vfsimpl_setup(vfsp);
532 vfsp->vfs_data = (data);
533 vfs_setops((vfsp), (op));
534 }
535
536 /*
537 * Allocate and initialize the vfs implementation private data
538 * structure, vfs_impl_t.
539 */
540 void
541 vfsimpl_setup(vfs_t *vfsp)
542 {
543 int i;
544
545 if (vfsp->vfs_implp != NULL) {
546 return;
547 }
548
549 vfsp->vfs_implp = kmem_alloc(sizeof (vfs_impl_t), KM_SLEEP);
550 /* Note that these are #define'd in vfs.h */
551 vfsp->vfs_vskap = NULL;
552 vfsp->vfs_fstypevsp = NULL;
553
554 /* Set size of counted array, then zero the array */
555 vfsp->vfs_featureset[0] = VFS_FEATURE_MAXSZ - 1;
556 for (i = 1; i < VFS_FEATURE_MAXSZ; i++) {
557 vfsp->vfs_featureset[i] = 0;
558 }
559 }
560
561 /*
562 * Release the vfs_impl_t structure, if it exists. Some unbundled
563 * filesystems may not use the newer version of vfs and thus
564 * would not contain this implementation private data structure.
565 */
566 void
567 vfsimpl_teardown(vfs_t *vfsp)
568 {
569 vfs_impl_t *vip = vfsp->vfs_implp;
570
571 if (vip == NULL)
572 return;
573
574 kmem_free(vfsp->vfs_implp, sizeof (vfs_impl_t));
575 vfsp->vfs_implp = NULL;
576 }
577
578 /*
579 * VFS system calls: mount, umount, syssync, statfs, fstatfs, statvfs,
580 * fstatvfs, and sysfs moved to common/syscall.
581 */
582
583 /*
584 * Update every mounted file system. We call the vfs_sync operation of
585 * each file system type, passing it a NULL vfsp to indicate that all
586 * mounted file systems of that type should be updated.
587 */
588 void
589 vfs_sync(int flag)
590 {
591 struct vfssw *vswp;
592 RLOCK_VFSSW();
593 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
594 if (ALLOCATED_VFSSW(vswp) && VFS_INSTALLED(vswp)) {
595 vfs_refvfssw(vswp);
596 RUNLOCK_VFSSW();
597 (void) (*vswp->vsw_vfsops.vfs_sync)(NULL, flag,
598 CRED());
599 vfs_unrefvfssw(vswp);
600 RLOCK_VFSSW();
601 }
602 }
603 RUNLOCK_VFSSW();
604 }
605
606 void
607 sync(void)
608 {
609 vfs_sync(0);
610 }
611
612 /*
613 * External routines.
614 */
615
616 krwlock_t vfssw_lock; /* lock accesses to vfssw */
617
618 /*
619 * Lock for accessing the vfs linked list. Initialized in vfs_mountroot(),
620 * but otherwise should be accessed only via vfs_list_lock() and
621 * vfs_list_unlock(). Also used to protect the timestamp for mods to the list.
622 */
623 static krwlock_t vfslist;
624
625 /*
626 * Mount devfs on /devices. This is done right after root is mounted
627 * to provide device access support for the system
628 */
629 static void
630 vfs_mountdevices(void)
631 {
632 struct vfssw *vsw;
633 struct vnode *mvp;
634 struct mounta mounta = { /* fake mounta for devfs_mount() */
635 NULL,
636 NULL,
637 MS_SYSSPACE,
638 NULL,
639 NULL,
640 0,
641 NULL,
642 0
643 };
644
645 /*
646 * _init devfs module to fill in the vfssw
647 */
648 if (modload("fs", "devfs") == -1)
649 panic("Cannot _init devfs module");
650
651 /*
652 * Hold vfs
653 */
654 RLOCK_VFSSW();
655 vsw = vfs_getvfsswbyname("devfs");
656 VFS_INIT(&devices, &vsw->vsw_vfsops, NULL);
657 VFS_HOLD(&devices);
658
659 /*
660 * Locate mount point
661 */
662 if (lookupname("/devices", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
663 panic("Cannot find /devices");
664
665 /*
666 * Perform the mount of /devices
667 */
668 if (VFS_MOUNT(&devices, mvp, &mounta, CRED()))
669 panic("Cannot mount /devices");
670
671 RUNLOCK_VFSSW();
672
673 /*
674 * Set appropriate members and add to vfs list for mnttab display
675 */
676 vfs_setresource(&devices, "/devices", 0);
677 vfs_setmntpoint(&devices, "/devices", 0);
678
679 /*
680 * Hold the root of /devices so it won't go away
681 */
682 if (VFS_ROOT(&devices, &devicesdir))
683 panic("vfs_mountdevices: not devices root");
684
685 if (vfs_lock(&devices) != 0) {
686 VN_RELE(devicesdir);
687 cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /devices");
688 return;
689 }
690
691 if (vn_vfswlock(mvp) != 0) {
692 vfs_unlock(&devices);
693 VN_RELE(devicesdir);
694 cmn_err(CE_NOTE, "Cannot acquire vfswlock of /devices");
695 return;
696 }
697
698 vfs_add(mvp, &devices, 0);
699 vn_vfsunlock(mvp);
700 vfs_unlock(&devices);
701 VN_RELE(devicesdir);
702 }
703
704 /*
705 * mount the first instance of /dev to root and remain mounted
706 */
707 static void
708 vfs_mountdev1(void)
709 {
710 struct vfssw *vsw;
711 struct vnode *mvp;
712 struct mounta mounta = { /* fake mounta for sdev_mount() */
713 NULL,
714 NULL,
715 MS_SYSSPACE | MS_OVERLAY,
716 NULL,
717 NULL,
718 0,
719 NULL,
720 0
721 };
722
723 /*
724 * _init dev module to fill in the vfssw
725 */
726 if (modload("fs", "dev") == -1)
727 cmn_err(CE_PANIC, "Cannot _init dev module\n");
728
729 /*
730 * Hold vfs
731 */
732 RLOCK_VFSSW();
733 vsw = vfs_getvfsswbyname("dev");
734 VFS_INIT(&dev, &vsw->vsw_vfsops, NULL);
735 VFS_HOLD(&dev);
736
737 /*
738 * Locate mount point
739 */
740 if (lookupname("/dev", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
741 cmn_err(CE_PANIC, "Cannot find /dev\n");
742
743 /*
744 * Perform the mount of /dev
745 */
746 if (VFS_MOUNT(&dev, mvp, &mounta, CRED()))
747 cmn_err(CE_PANIC, "Cannot mount /dev 1\n");
748
749 RUNLOCK_VFSSW();
750
751 /*
752 * Set appropriate members and add to vfs list for mnttab display
753 */
754 vfs_setresource(&dev, "/dev", 0);
755 vfs_setmntpoint(&dev, "/dev", 0);
756
757 /*
758 * Hold the root of /dev so it won't go away
759 */
760 if (VFS_ROOT(&dev, &devdir))
761 cmn_err(CE_PANIC, "vfs_mountdev1: not dev root");
762
763 if (vfs_lock(&dev) != 0) {
764 VN_RELE(devdir);
765 cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /dev");
766 return;
767 }
768
769 if (vn_vfswlock(mvp) != 0) {
770 vfs_unlock(&dev);
771 VN_RELE(devdir);
772 cmn_err(CE_NOTE, "Cannot acquire vfswlock of /dev");
773 return;
774 }
775
776 vfs_add(mvp, &dev, 0);
777 vn_vfsunlock(mvp);
778 vfs_unlock(&dev);
779 VN_RELE(devdir);
780 }
781
782 /*
783 * Mount required filesystem. This is done right after root is mounted.
784 */
785 static void
786 vfs_mountfs(char *module, char *spec, char *path)
787 {
788 struct vnode *mvp;
789 struct mounta mounta;
790 vfs_t *vfsp;
791
792 mounta.flags = MS_SYSSPACE | MS_DATA;
793 mounta.fstype = module;
794 mounta.spec = spec;
795 mounta.dir = path;
796 if (lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp)) {
797 cmn_err(CE_WARN, "Cannot find %s", path);
798 return;
799 }
800 if (domount(NULL, &mounta, mvp, CRED(), &vfsp))
801 cmn_err(CE_WARN, "Cannot mount %s", path);
802 else
803 VFS_RELE(vfsp);
804 VN_RELE(mvp);
805 }
806
807 /*
808 * vfs_mountroot is called by main() to mount the root filesystem.
809 */
810 void
811 vfs_mountroot(void)
812 {
813 struct vnode *rvp = NULL;
814 char *path;
815 size_t plen;
816 struct vfssw *vswp;
817 proc_t *p;
818
819 rw_init(&vfssw_lock, NULL, RW_DEFAULT, NULL);
820 rw_init(&vfslist, NULL, RW_DEFAULT, NULL);
821
822 /*
823 * Alloc the vfs hash bucket array and locks
824 */
825 rvfs_list = kmem_zalloc(vfshsz * sizeof (rvfs_t), KM_SLEEP);
826
827 /*
828 * Call machine-dependent routine "rootconf" to choose a root
829 * file system type.
830 */
831 if (rootconf())
832 panic("vfs_mountroot: cannot mount root");
833 /*
834 * Get vnode for '/'. Set up rootdir, u.u_rdir and u.u_cdir
835 * to point to it. These are used by lookuppn() so that it
836 * knows where to start from ('/' or '.').
837 */
838 vfs_setmntpoint(rootvfs, "/", 0);
839 if (VFS_ROOT(rootvfs, &rootdir))
840 panic("vfs_mountroot: no root vnode");
841
842 /*
843 * At this point, the process tree consists of p0 and possibly some
844 * direct children of p0. (i.e. there are no grandchildren)
845 *
846 * Walk through them all, setting their current directory.
847 */
848 mutex_enter(&pidlock);
849 for (p = practive; p != NULL; p = p->p_next) {
850 ASSERT(p == &p0 || p->p_parent == &p0);
851
852 PTOU(p)->u_cdir = rootdir;
853 VN_HOLD(PTOU(p)->u_cdir);
854 PTOU(p)->u_rdir = NULL;
855 }
856 mutex_exit(&pidlock);
857
858 /*
859 * Setup the global zone's rootvp, now that it exists.
860 */
861 global_zone->zone_rootvp = rootdir;
862 VN_HOLD(global_zone->zone_rootvp);
863
864 /*
865 * Notify the module code that it can begin using the
866 * root filesystem instead of the boot program's services.
867 */
868 modrootloaded = 1;
869
870 /*
871 * Special handling for a ZFS root file system.
872 */
873 zfs_boot_init();
874
875 /*
876 * Set up mnttab information for root
877 */
878 vfs_setresource(rootvfs, rootfs.bo_name, 0);
879
880 /*
881 * Notify cluster software that the root filesystem is available.
882 */
883 clboot_mountroot();
884
885 /* Now that we're all done with the root FS, set up its vopstats */
886 if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) != NULL) {
887 /* Set flag for statistics collection */
888 if (vswp->vsw_flag & VSW_STATS) {
889 initialize_vopstats(&rootvfs->vfs_vopstats);
890 rootvfs->vfs_flag |= VFS_STATS;
891 rootvfs->vfs_fstypevsp =
892 get_fstype_vopstats(rootvfs, vswp);
893 rootvfs->vfs_vskap = get_vskstat_anchor(rootvfs);
894 }
895 vfs_unrefvfssw(vswp);
896 }
897
898 /*
899 * Mount /devices, /dev instance 1, /system/contract, /etc/mnttab,
900 * /etc/svc/volatile, /etc/dfs/sharetab, /system/object, and /proc.
901 */
902 vfs_mountdevices();
903 vfs_mountdev1();
904
905 vfs_mountfs("ctfs", "ctfs", CTFS_ROOT);
906 vfs_mountfs("proc", "/proc", "/proc");
907 vfs_mountfs("mntfs", "/etc/mnttab", "/etc/mnttab");
908 vfs_mountfs("tmpfs", "/etc/svc/volatile", "/etc/svc/volatile");
909 vfs_mountfs("objfs", "objfs", OBJFS_ROOT);
910 vfs_mountfs("bootfs", "bootfs", "/system/boot");
911
912 if (getzoneid() == GLOBAL_ZONEID) {
913 vfs_mountfs("sharefs", "sharefs", "/etc/dfs/sharetab");
914 }
915
916 #ifdef __sparc
917 /*
918 * This bit of magic can go away when we convert sparc to
919 * the new boot architecture based on ramdisk.
920 *
921 * Booting off a mirrored root volume:
922 * At this point, we have booted and mounted root on a
923 * single component of the mirror. Complete the boot
924 * by configuring SVM and converting the root to the
925 * dev_t of the mirrored root device. This dev_t conversion
926 * only works because the underlying device doesn't change.
927 */
928 if (root_is_svm) {
929 if (svm_rootconf()) {
930 panic("vfs_mountroot: cannot remount root");
931 }
932
933 /*
934 * mnttab should reflect the new root device
935 */
936 vfs_lock_wait(rootvfs);
937 vfs_setresource(rootvfs, rootfs.bo_name, 0);
938 vfs_unlock(rootvfs);
939 }
940 #endif /* __sparc */
941
942 if (strcmp(rootfs.bo_fstype, "zfs") != 0) {
943 /*
944 * Look up the root device via devfs so that a dv_node is
945 * created for it. The vnode is never VN_RELE()ed.
946 * We allocate more than MAXPATHLEN so that the
947 * buffer passed to i_ddi_prompath_to_devfspath() is
948 * exactly MAXPATHLEN (the function expects a buffer
949 * of that length).
950 */
951 plen = strlen("/devices");
952 path = kmem_alloc(plen + MAXPATHLEN, KM_SLEEP);
953 (void) strcpy(path, "/devices");
954
955 if (i_ddi_prompath_to_devfspath(rootfs.bo_name, path + plen)
956 != DDI_SUCCESS ||
957 lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &rvp)) {
958
959 /* NUL terminate in case "path" has garbage */
960 path[plen + MAXPATHLEN - 1] = '\0';
961 #ifdef DEBUG
962 cmn_err(CE_WARN, "!Cannot lookup root device: %s",
963 path);
964 #endif
965 }
966 kmem_free(path, plen + MAXPATHLEN);
967 }
968
969 vfs_mnttabvp_setup();
970 }
971
972 /*
973 * Check to see if our "block device" is actually a file. If so,
974 * automatically add a lofi device, and keep track of this fact.
975 */
976 static int
977 lofi_add(const char *fsname, struct vfs *vfsp,
978 mntopts_t *mntopts, struct mounta *uap)
979 {
980 int fromspace = (uap->flags & MS_SYSSPACE) ?
981 UIO_SYSSPACE : UIO_USERSPACE;
982 struct lofi_ioctl *li = NULL;
983 struct vnode *vp = NULL;
984 struct pathname pn = { NULL };
985 ldi_ident_t ldi_id;
986 ldi_handle_t ldi_hdl;
987 vfssw_t *vfssw;
988 int id;
989 int err = 0;
990
991 if ((vfssw = vfs_getvfssw(fsname)) == NULL)
992 return (0);
993
994 if (!(vfssw->vsw_flag & VSW_CANLOFI)) {
995 vfs_unrefvfssw(vfssw);
996 return (0);
997 }
998
999 vfs_unrefvfssw(vfssw);
1000 vfssw = NULL;
1001
1002 if (pn_get(uap->spec, fromspace, &pn) != 0)
1003 return (0);
1004
1005 if (lookupname(uap->spec, fromspace, FOLLOW, NULL, &vp) != 0)
1006 goto out;
1007
1008 if (vp->v_type != VREG)
1009 goto out;
1010
1011 /* OK, this is a lofi mount. */
1012
1013 if ((uap->flags & (MS_REMOUNT|MS_GLOBAL)) ||
1014 vfs_optionisset_nolock(mntopts, MNTOPT_SUID, NULL) ||
1015 vfs_optionisset_nolock(mntopts, MNTOPT_SETUID, NULL) ||
1016 vfs_optionisset_nolock(mntopts, MNTOPT_DEVICES, NULL)) {
1017 err = EINVAL;
1018 goto out;
1019 }
1020
1021 ldi_id = ldi_ident_from_anon();
1022 li = kmem_zalloc(sizeof (*li), KM_SLEEP);
1023 (void) strlcpy(li->li_filename, pn.pn_path, MAXPATHLEN);
1024
1025 err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
1026 &ldi_hdl, ldi_id);
1027
1028 if (err)
1029 goto out2;
1030
1031 err = ldi_ioctl(ldi_hdl, LOFI_MAP_FILE, (intptr_t)li,
1032 FREAD | FWRITE | FKIOCTL, kcred, &id);
1033
1034 (void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
1035
1036 if (!err)
1037 vfsp->vfs_lofi_id = id;
1038
1039 out2:
1040 ldi_ident_release(ldi_id);
1041 out:
1042 if (li != NULL)
1043 kmem_free(li, sizeof (*li));
1044 if (vp != NULL)
1045 VN_RELE(vp);
1046 pn_free(&pn);
1047 return (err);
1048 }
1049
1050 static void
1051 lofi_remove(struct vfs *vfsp)
1052 {
1053 struct lofi_ioctl *li = NULL;
1054 ldi_ident_t ldi_id;
1055 ldi_handle_t ldi_hdl;
1056 int err;
1057
1058 if (vfsp->vfs_lofi_id == 0)
1059 return;
1060
1061 ldi_id = ldi_ident_from_anon();
1062
1063 li = kmem_zalloc(sizeof (*li), KM_SLEEP);
1064 li->li_id = vfsp->vfs_lofi_id;
1065 li->li_cleanup = B_TRUE;
1066
1067 err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
1068 &ldi_hdl, ldi_id);
1069
1070 if (err)
1071 goto out;
1072
1073 err = ldi_ioctl(ldi_hdl, LOFI_UNMAP_FILE_MINOR, (intptr_t)li,
1074 FREAD | FWRITE | FKIOCTL, kcred, NULL);
1075
1076 (void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
1077
1078 if (!err)
1079 vfsp->vfs_lofi_id = 0;
1080
1081 out:
1082 ldi_ident_release(ldi_id);
1083 if (li != NULL)
1084 kmem_free(li, sizeof (*li));
1085 }
1086
1087 /*
1088 * Common mount code. Called from the system call entry point, from autofs,
1089 * nfsv4 trigger mounts, and from pxfs.
1090 *
1091 * Takes the effective file system type, mount arguments, the mount point
1092 * vnode, flags specifying whether the mount is a remount and whether it
1093 * should be entered into the vfs list, and credentials. Fills in its vfspp
1094 * parameter with the mounted file system instance's vfs.
1095 *
1096 * Note that the effective file system type is specified as a string. It may
1097 * be null, in which case it's determined from the mount arguments, and may
1098 * differ from the type specified in the mount arguments; this is a hook to
1099 * allow interposition when instantiating file system instances.
1100 *
1101 * The caller is responsible for releasing its own hold on the mount point
1102 * vp (this routine does its own hold when necessary).
1103 * Also note that for remounts, the mount point vp should be the vnode for
1104 * the root of the file system rather than the vnode that the file system
1105 * is mounted on top of.
1106 */
1107 int
1108 domount(char *fsname, struct mounta *uap, vnode_t *vp, struct cred *credp,
1109 struct vfs **vfspp)
1110 {
1111 struct vfssw *vswp;
1112 vfsops_t *vfsops;
1113 struct vfs *vfsp;
1114 struct vnode *bvp;
1115 dev_t bdev = 0;
1116 mntopts_t mnt_mntopts;
1117 int error = 0;
1118 int copyout_error = 0;
1119 int ovflags;
1120 char *opts = uap->optptr;
1121 char *inargs = opts;
1122 int optlen = uap->optlen;
1123 int remount;
1124 int rdonly;
1125 int nbmand = 0;
1126 int delmip = 0;
1127 int addmip = 0;
1128 int splice = ((uap->flags & MS_NOSPLICE) == 0);
1129 int fromspace = (uap->flags & MS_SYSSPACE) ?
1130 UIO_SYSSPACE : UIO_USERSPACE;
1131 char *resource = NULL, *mountpt = NULL;
1132 refstr_t *oldresource, *oldmntpt;
1133 struct pathname pn, rpn;
1134 vsk_anchor_t *vskap;
1135 char fstname[FSTYPSZ];
1136 zone_t *zone;
1137
1138 /*
1139 * The v_flag value for the mount point vp is permanently set
1140 * to VVFSLOCK so that no one bypasses the vn_vfs*locks routine
1141 * for mount point locking.
1142 */
1143 mutex_enter(&vp->v_lock);
1144 vp->v_flag |= VVFSLOCK;
1145 mutex_exit(&vp->v_lock);
1146
1147 mnt_mntopts.mo_count = 0;
1148 /*
1149 * Find the ops vector to use to invoke the file system-specific mount
1150 * method. If the fsname argument is non-NULL, use it directly.
1151 * Otherwise, dig the file system type information out of the mount
1152 * arguments.
1153 *
1154 * A side effect is to hold the vfssw entry.
1155 *
1156 * Mount arguments can be specified in several ways, which are
1157 * distinguished by flag bit settings. The preferred way is to set
1158 * MS_OPTIONSTR, indicating an 8 argument mount with the file system
1159 * type supplied as a character string and the last two arguments
1160 * being a pointer to a character buffer and the size of the buffer.
1161 * On entry, the buffer holds a null terminated list of options; on
1162 * return, the string is the list of options the file system
1163 * recognized. If MS_DATA is set arguments five and six point to a
1164 * block of binary data which the file system interprets.
1165 * A further wrinkle is that some callers don't set MS_FSS and MS_DATA
1166 * consistently with these conventions. To handle them, we check to
1167 * see whether the pointer to the file system name has a numeric value
1168 * less than 256. If so, we treat it as an index.
1169 */
1170 if (fsname != NULL) {
1171 if ((vswp = vfs_getvfssw(fsname)) == NULL) {
1172 return (EINVAL);
1173 }
1174 } else if (uap->flags & (MS_OPTIONSTR | MS_DATA | MS_FSS)) {
1175 size_t n;
1176 uint_t fstype;
1177
1178 fsname = fstname;
1179
1180 if ((fstype = (uintptr_t)uap->fstype) < 256) {
1181 RLOCK_VFSSW();
1182 if (fstype == 0 || fstype >= nfstype ||
1183 !ALLOCATED_VFSSW(&vfssw[fstype])) {
1184 RUNLOCK_VFSSW();
1185 return (EINVAL);
1186 }
1187 (void) strcpy(fsname, vfssw[fstype].vsw_name);
1188 RUNLOCK_VFSSW();
1189 if ((vswp = vfs_getvfssw(fsname)) == NULL)
1190 return (EINVAL);
1191 } else {
1192 /*
1193 * Handle either kernel or user address space.
1194 */
1195 if (uap->flags & MS_SYSSPACE) {
1196 error = copystr(uap->fstype, fsname,
1197 FSTYPSZ, &n);
1198 } else {
1199 error = copyinstr(uap->fstype, fsname,
1200 FSTYPSZ, &n);
1201 }
1202 if (error) {
1203 if (error == ENAMETOOLONG)
1204 return (EINVAL);
1205 return (error);
1206 }
1207 if ((vswp = vfs_getvfssw(fsname)) == NULL)
1208 return (EINVAL);
1209 }
1210 } else {
1211 if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) == NULL)
1212 return (EINVAL);
1213 fsname = vswp->vsw_name;
1214 }
1215 if (!VFS_INSTALLED(vswp))
1216 return (EINVAL);
1217
1218 if ((error = secpolicy_fs_allowed_mount(fsname)) != 0) {
1219 vfs_unrefvfssw(vswp);
1220 return (error);
1221 }
1222
1223 vfsops = &vswp->vsw_vfsops;
1224
1225 vfs_copyopttbl(&vswp->vsw_optproto, &mnt_mntopts);
1226 /*
1227 * Fetch mount options and parse them for generic vfs options
1228 */
1229 if (uap->flags & MS_OPTIONSTR) {
1230 /*
1231 * Limit the buffer size
1232 */
1233 if (optlen < 0 || optlen > MAX_MNTOPT_STR) {
1234 error = EINVAL;
1235 goto errout;
1236 }
1237 if ((uap->flags & MS_SYSSPACE) == 0) {
1238 inargs = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
1239 inargs[0] = '\0';
1240 if (optlen) {
1241 error = copyinstr(opts, inargs, (size_t)optlen,
1242 NULL);
1243 if (error) {
1244 goto errout;
1245 }
1246 }
1247 }
1248 vfs_parsemntopts(&mnt_mntopts, inargs, 0);
1249 }
1250 /*
1251 * Flag bits override the options string.
1252 */
1253 if (uap->flags & MS_REMOUNT)
1254 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_REMOUNT, NULL, 0, 0);
1255 if (uap->flags & MS_RDONLY)
1256 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_RO, NULL, 0, 0);
1257 if (uap->flags & MS_NOSUID)
1258 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1259
1260 /*
1261 * Check if this is a remount; must be set in the option string and
1262 * the file system must support a remount option.
1263 */
1264 if (remount = vfs_optionisset_nolock(&mnt_mntopts,
1265 MNTOPT_REMOUNT, NULL)) {
1266 if (!(vswp->vsw_flag & VSW_CANREMOUNT)) {
1267 error = ENOTSUP;
1268 goto errout;
1269 }
1270 uap->flags |= MS_REMOUNT;
1271 }
1272
1273 /*
1274 * uap->flags and vfs_optionisset() should agree.
1275 */
1276 if (rdonly = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_RO, NULL)) {
1277 uap->flags |= MS_RDONLY;
1278 }
1279 if (vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL)) {
1280 uap->flags |= MS_NOSUID;
1281 }
1282 nbmand = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NBMAND, NULL);
1283 ASSERT(splice || !remount);
1284 /*
1285 * If we are splicing the fs into the namespace,
1286 * perform mount point checks.
1287 *
1288 * We want to resolve the path for the mount point to eliminate
1289 * '.' and ".." and symlinks in mount points; we can't do the
1290 * same for the resource string, since it would turn
1291 * "/dev/dsk/c0t0d0s0" into "/devices/pci@...". We need to do
1292 * this before grabbing vn_vfswlock(), because otherwise we
1293 * would deadlock with lookuppn().
1294 */
1295 if (splice) {
1296 ASSERT(vp->v_count > 0);
1297
1298 /*
1299 * Pick up mount point and device from appropriate space.
1300 */
1301 if (pn_get(uap->spec, fromspace, &pn) == 0) {
1302 resource = kmem_alloc(pn.pn_pathlen + 1,
1303 KM_SLEEP);
1304 (void) strcpy(resource, pn.pn_path);
1305 pn_free(&pn);
1306 }
1307 /*
1308 * Do a lookupname prior to taking the
1309 * writelock. Mark this as completed if
1310 * successful for later cleanup and addition to
1311 * the mount in progress table.
1312 */
1313 if ((uap->flags & MS_GLOBAL) == 0 &&
1314 lookupname(uap->spec, fromspace,
1315 FOLLOW, NULL, &bvp) == 0) {
1316 addmip = 1;
1317 }
1318
1319 if ((error = pn_get(uap->dir, fromspace, &pn)) == 0) {
1320 pathname_t *pnp;
1321
1322 if (*pn.pn_path != '/') {
1323 error = EINVAL;
1324 pn_free(&pn);
1325 goto errout;
1326 }
1327 pn_alloc(&rpn);
1328 /*
1329 * Kludge to prevent autofs from deadlocking with
1330 * itself when it calls domount().
1331 *
1332 * If autofs is calling, it is because it is doing
1333 * (autofs) mounts in the process of an NFS mount. A
1334 * lookuppn() here would cause us to block waiting for
1335 * said NFS mount to complete, which can't since this
1336 * is the thread that was supposed to doing it.
1337 */
1338 if (fromspace == UIO_USERSPACE) {
1339 if ((error = lookuppn(&pn, &rpn, FOLLOW, NULL,
1340 NULL)) == 0) {
1341 pnp = &rpn;
1342 } else {
1343 /*
1344 * The file disappeared or otherwise
1345 * became inaccessible since we opened
1346 * it; might as well fail the mount
1347 * since the mount point is no longer
1348 * accessible.
1349 */
1350 pn_free(&rpn);
1351 pn_free(&pn);
1352 goto errout;
1353 }
1354 } else {
1355 pnp = &pn;
1356 }
1357 mountpt = kmem_alloc(pnp->pn_pathlen + 1, KM_SLEEP);
1358 (void) strcpy(mountpt, pnp->pn_path);
1359
1360 /*
1361 * If the addition of the zone's rootpath
1362 * would push us over a total path length
1363 * of MAXPATHLEN, we fail the mount with
1364 * ENAMETOOLONG, which is what we would have
1365 * gotten if we were trying to perform the same
1366 * mount in the global zone.
1367 *
1368 * strlen() doesn't count the trailing
1369 * '\0', but zone_rootpathlen counts both a
1370 * trailing '/' and the terminating '\0'.
1371 */
1372 if ((curproc->p_zone->zone_rootpathlen - 1 +
1373 strlen(mountpt)) > MAXPATHLEN ||
1374 (resource != NULL &&
1375 (curproc->p_zone->zone_rootpathlen - 1 +
1376 strlen(resource)) > MAXPATHLEN)) {
1377 error = ENAMETOOLONG;
1378 }
1379
1380 pn_free(&rpn);
1381 pn_free(&pn);
1382 }
1383
1384 if (error)
1385 goto errout;
1386
1387 /*
1388 * Prevent path name resolution from proceeding past
1389 * the mount point.
1390 */
1391 if (vn_vfswlock(vp) != 0) {
1392 error = EBUSY;
1393 goto errout;
1394 }
1395
1396 /*
1397 * Verify that it's legitimate to establish a mount on
1398 * the prospective mount point.
1399 */
1400 if (vn_mountedvfs(vp) != NULL) {
1401 /*
1402 * The mount point lock was obtained after some
1403 * other thread raced through and established a mount.
1404 */
1405 vn_vfsunlock(vp);
1406 error = EBUSY;
1407 goto errout;
1408 }
1409 if (vp->v_flag & VNOMOUNT) {
1410 vn_vfsunlock(vp);
1411 error = EINVAL;
1412 goto errout;
1413 }
1414 }
1415 if ((uap->flags & (MS_DATA | MS_OPTIONSTR)) == 0) {
1416 uap->dataptr = NULL;
1417 uap->datalen = 0;
1418 }
1419
1420 /*
1421 * If this is a remount, we don't want to create a new VFS.
1422 * Instead, we pass the existing one with a remount flag.
1423 */
1424 if (remount) {
1425 /*
1426 * Confirm that the mount point is the root vnode of the
1427 * file system that is being remounted.
1428 * This can happen if the user specifies a different
1429 * mount point directory pathname in the (re)mount command.
1430 *
1431 * Code below can only be reached if splice is true, so it's
1432 * safe to do vn_vfsunlock() here.
1433 */
1434 if ((vp->v_flag & VROOT) == 0) {
1435 vn_vfsunlock(vp);
1436 error = ENOENT;
1437 goto errout;
1438 }
1439 /*
1440 * Disallow making file systems read-only unless file system
1441 * explicitly allows it in its vfssw. Ignore other flags.
1442 */
1443 if (rdonly && vn_is_readonly(vp) == 0 &&
1444 (vswp->vsw_flag & VSW_CANRWRO) == 0) {
1445 vn_vfsunlock(vp);
1446 error = EINVAL;
1447 goto errout;
1448 }
1449 /*
1450 * Disallow changing the NBMAND disposition of the file
1451 * system on remounts.
1452 */
1453 if ((nbmand && ((vp->v_vfsp->vfs_flag & VFS_NBMAND) == 0)) ||
1454 (!nbmand && (vp->v_vfsp->vfs_flag & VFS_NBMAND))) {
1455 vn_vfsunlock(vp);
1456 error = EINVAL;
1457 goto errout;
1458 }
1459 vfsp = vp->v_vfsp;
1460 ovflags = vfsp->vfs_flag;
1461 vfsp->vfs_flag |= VFS_REMOUNT;
1462 vfsp->vfs_flag &= ~VFS_RDONLY;
1463 } else {
1464 vfsp = vfs_alloc(KM_SLEEP);
1465 VFS_INIT(vfsp, vfsops, NULL);
1466 }
1467
1468 VFS_HOLD(vfsp);
1469
1470 if ((error = lofi_add(fsname, vfsp, &mnt_mntopts, uap)) != 0) {
1471 if (!remount) {
1472 if (splice)
1473 vn_vfsunlock(vp);
1474 vfs_free(vfsp);
1475 } else {
1476 vn_vfsunlock(vp);
1477 VFS_RELE(vfsp);
1478 }
1479 goto errout;
1480 }
1481
1482 /*
1483 * PRIV_SYS_MOUNT doesn't mean you can become root.
1484 */
1485 if (vfsp->vfs_lofi_id != 0) {
1486 uap->flags |= MS_NOSUID;
1487 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1488 }
1489
1490 /*
1491 * The vfs_reflock is not used anymore the code below explicitly
1492 * holds it preventing others accesing it directly.
1493 */
1494 if ((sema_tryp(&vfsp->vfs_reflock) == 0) &&
1495 !(vfsp->vfs_flag & VFS_REMOUNT))
1496 cmn_err(CE_WARN,
1497 "mount type %s couldn't get vfs_reflock", vswp->vsw_name);
1498
1499 /*
1500 * Lock the vfs. If this is a remount we want to avoid spurious umount
1501 * failures that happen as a side-effect of fsflush() and other mount
1502 * and unmount operations that might be going on simultaneously and
1503 * may have locked the vfs currently. To not return EBUSY immediately
1504 * here we use vfs_lock_wait() instead vfs_lock() for the remount case.
1505 */
1506 if (!remount) {
1507 if (error = vfs_lock(vfsp)) {
1508 vfsp->vfs_flag = ovflags;
1509
1510 lofi_remove(vfsp);
1511
1512 if (splice)
1513 vn_vfsunlock(vp);
1514 vfs_free(vfsp);
1515 goto errout;
1516 }
1517 } else {
1518 vfs_lock_wait(vfsp);
1519 }
1520
1521 /*
1522 * Add device to mount in progress table, global mounts require special
1523 * handling. It is possible that we have already done the lookupname
1524 * on a spliced, non-global fs. If so, we don't want to do it again
1525 * since we cannot do a lookupname after taking the
1526 * wlock above. This case is for a non-spliced, non-global filesystem.
1527 */
1528 if (!addmip) {
1529 if ((uap->flags & MS_GLOBAL) == 0 &&
1530 lookupname(uap->spec, fromspace, FOLLOW, NULL, &bvp) == 0) {
1531 addmip = 1;
1532 }
1533 }
1534
1535 if (addmip) {
1536 vnode_t *lvp = NULL;
1537
1538 error = vfs_get_lofi(vfsp, &lvp);
1539 if (error > 0) {
1540 lofi_remove(vfsp);
1541
1542 if (splice)
1543 vn_vfsunlock(vp);
1544 vfs_unlock(vfsp);
1545
1546 if (remount) {
1547 VFS_RELE(vfsp);
1548 } else {
1549 vfs_free(vfsp);
1550 }
1551
1552 goto errout;
1553 } else if (error == -1) {
1554 bdev = bvp->v_rdev;
1555 VN_RELE(bvp);
1556 } else {
1557 bdev = lvp->v_rdev;
1558 VN_RELE(lvp);
1559 VN_RELE(bvp);
1560 }
1561
1562 vfs_addmip(bdev, vfsp);
1563 addmip = 0;
1564 delmip = 1;
1565 }
1566 /*
1567 * Invalidate cached entry for the mount point.
1568 */
1569 if (splice)
1570 dnlc_purge_vp(vp);
1571
1572 /*
1573 * If have an option string but the filesystem doesn't supply a
1574 * prototype options table, create a table with the global
1575 * options and sufficient room to accept all the options in the
1576 * string. Then parse the passed in option string
1577 * accepting all the options in the string. This gives us an
1578 * option table with all the proper cancel properties for the
1579 * global options.
1580 *
1581 * Filesystems that supply a prototype options table are handled
1582 * earlier in this function.
1583 */
1584 if (uap->flags & MS_OPTIONSTR) {
1585 if (!(vswp->vsw_flag & VSW_HASPROTO)) {
1586 mntopts_t tmp_mntopts;
1587
1588 tmp_mntopts.mo_count = 0;
1589 vfs_createopttbl_extend(&tmp_mntopts, inargs,
1590 &mnt_mntopts);
1591 vfs_parsemntopts(&tmp_mntopts, inargs, 1);
1592 vfs_swapopttbl_nolock(&mnt_mntopts, &tmp_mntopts);
1593 vfs_freeopttbl(&tmp_mntopts);
1594 }
1595 }
1596
1597 /*
1598 * Serialize with zone state transitions.
1599 * See vfs_list_add; zone mounted into is:
1600 * zone_find_by_path(refstr_value(vfsp->vfs_mntpt))
1601 * not the zone doing the mount (curproc->p_zone), but if we're already
1602 * inside a NGZ, then we know what zone we are.
1603 */
1604 if (INGLOBALZONE(curproc)) {
1605 zone = zone_find_by_path(mountpt);
1606 ASSERT(zone != NULL);
1607 } else {
1608 zone = curproc->p_zone;
1609 /*
1610 * zone_find_by_path does a hold, so do one here too so that
1611 * we can do a zone_rele after mount_completed.
1612 */
1613 zone_hold(zone);
1614 }
1615 mount_in_progress(zone);
1616 /*
1617 * Instantiate (or reinstantiate) the file system. If appropriate,
1618 * splice it into the file system name space.
1619 *
1620 * We want VFS_MOUNT() to be able to override the vfs_resource
1621 * string if necessary (ie, mntfs), and also for a remount to
1622 * change the same (necessary when remounting '/' during boot).
1623 * So we set up vfs_mntpt and vfs_resource to what we think they
1624 * should be, then hand off control to VFS_MOUNT() which can
1625 * override this.
1626 *
1627 * For safety's sake, when changing vfs_resource or vfs_mntpt of
1628 * a vfs which is on the vfs list (i.e. during a remount), we must
1629 * never set those fields to NULL. Several bits of code make
1630 * assumptions that the fields are always valid.
1631 */
1632 vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1633 if (remount) {
1634 if ((oldresource = vfsp->vfs_resource) != NULL)
1635 refstr_hold(oldresource);
1636 if ((oldmntpt = vfsp->vfs_mntpt) != NULL)
1637 refstr_hold(oldmntpt);
1638 }
1639 vfs_setresource(vfsp, resource, 0);
1640 vfs_setmntpoint(vfsp, mountpt, 0);
1641
1642 /*
1643 * going to mount on this vnode, so notify.
1644 */
1645 vnevent_mountedover(vp, NULL);
1646 error = VFS_MOUNT(vfsp, vp, uap, credp);
1647
1648 if (uap->flags & MS_RDONLY)
1649 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1650 if (uap->flags & MS_NOSUID)
1651 vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
1652 if (uap->flags & MS_GLOBAL)
1653 vfs_setmntopt(vfsp, MNTOPT_GLOBAL, NULL, 0);
1654
1655 if (error) {
1656 lofi_remove(vfsp);
1657
1658 if (remount) {
1659 /* put back pre-remount options */
1660 vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1661 vfs_setmntpoint(vfsp, refstr_value(oldmntpt),
1662 VFSSP_VERBATIM);
1663 if (oldmntpt)
1664 refstr_rele(oldmntpt);
1665 vfs_setresource(vfsp, refstr_value(oldresource),
1666 VFSSP_VERBATIM);
1667 if (oldresource)
1668 refstr_rele(oldresource);
1669 vfsp->vfs_flag = ovflags;
1670 vfs_unlock(vfsp);
1671 VFS_RELE(vfsp);
1672 } else {
1673 vfs_unlock(vfsp);
1674 vfs_freemnttab(vfsp);
1675 vfs_free(vfsp);
1676 }
1677 } else {
1678 /*
1679 * Set the mount time to now
1680 */
1681 vfsp->vfs_mtime = ddi_get_time();
1682 if (remount) {
1683 vfsp->vfs_flag &= ~VFS_REMOUNT;
1684 if (oldresource)
1685 refstr_rele(oldresource);
1686 if (oldmntpt)
1687 refstr_rele(oldmntpt);
1688 } else if (splice) {
1689 /*
1690 * Link vfsp into the name space at the mount
1691 * point. Vfs_add() is responsible for
1692 * holding the mount point which will be
1693 * released when vfs_remove() is called.
1694 */
1695 vfs_add(vp, vfsp, uap->flags);
1696 } else {
1697 /*
1698 * Hold the reference to file system which is
1699 * not linked into the name space.
1700 */
1701 vfsp->vfs_zone = NULL;
1702 VFS_HOLD(vfsp);
1703 vfsp->vfs_vnodecovered = NULL;
1704 }
1705 /*
1706 * Set flags for global options encountered
1707 */
1708 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
1709 vfsp->vfs_flag |= VFS_RDONLY;
1710 else
1711 vfsp->vfs_flag &= ~VFS_RDONLY;
1712 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
1713 vfsp->vfs_flag |= (VFS_NOSETUID|VFS_NODEVICES);
1714 } else {
1715 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
1716 vfsp->vfs_flag |= VFS_NODEVICES;
1717 else
1718 vfsp->vfs_flag &= ~VFS_NODEVICES;
1719 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
1720 vfsp->vfs_flag |= VFS_NOSETUID;
1721 else
1722 vfsp->vfs_flag &= ~VFS_NOSETUID;
1723 }
1724 if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
1725 vfsp->vfs_flag |= VFS_NBMAND;
1726 else
1727 vfsp->vfs_flag &= ~VFS_NBMAND;
1728
1729 if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
1730 vfsp->vfs_flag |= VFS_XATTR;
1731 else
1732 vfsp->vfs_flag &= ~VFS_XATTR;
1733
1734 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
1735 vfsp->vfs_flag |= VFS_NOEXEC;
1736 else
1737 vfsp->vfs_flag &= ~VFS_NOEXEC;
1738
1739 /*
1740 * Now construct the output option string of options
1741 * we recognized.
1742 */
1743 if (uap->flags & MS_OPTIONSTR) {
1744 vfs_list_read_lock();
1745 copyout_error = vfs_buildoptionstr(
1746 &vfsp->vfs_mntopts, inargs, optlen);
1747 vfs_list_unlock();
1748 if (copyout_error == 0 &&
1749 (uap->flags & MS_SYSSPACE) == 0) {
1750 copyout_error = copyoutstr(inargs, opts,
1751 optlen, NULL);
1752 }
1753 }
1754
1755 /*
1756 * If this isn't a remount, set up the vopstats before
1757 * anyone can touch this. We only allow spliced file
1758 * systems (file systems which are in the namespace) to
1759 * have the VFS_STATS flag set.
1760 * NOTE: PxFS mounts the underlying file system with
1761 * MS_NOSPLICE set and copies those vfs_flags to its private
1762 * vfs structure. As a result, PxFS should never have
1763 * the VFS_STATS flag or else we might access the vfs
1764 * statistics-related fields prior to them being
1765 * properly initialized.
1766 */
1767 if (!remount && (vswp->vsw_flag & VSW_STATS) && splice) {
1768 initialize_vopstats(&vfsp->vfs_vopstats);
1769 /*
1770 * We need to set vfs_vskap to NULL because there's
1771 * a chance it won't be set below. This is checked
1772 * in teardown_vopstats() so we can't have garbage.
1773 */
1774 vfsp->vfs_vskap = NULL;
1775 vfsp->vfs_flag |= VFS_STATS;
1776 vfsp->vfs_fstypevsp = get_fstype_vopstats(vfsp, vswp);
1777 }
1778
1779 if (vswp->vsw_flag & VSW_XID)
1780 vfsp->vfs_flag |= VFS_XID;
1781
1782 vfs_unlock(vfsp);
1783 }
1784 mount_completed(zone);
1785 zone_rele(zone);
1786 if (splice)
1787 vn_vfsunlock(vp);
1788
1789 if ((error == 0) && (copyout_error == 0)) {
1790 if (!remount) {
1791 /*
1792 * Don't call get_vskstat_anchor() while holding
1793 * locks since it allocates memory and calls
1794 * VFS_STATVFS(). For NFS, the latter can generate
1795 * an over-the-wire call.
1796 */
1797 vskap = get_vskstat_anchor(vfsp);
1798 /* Only take the lock if we have something to do */
1799 if (vskap != NULL) {
1800 vfs_lock_wait(vfsp);
1801 if (vfsp->vfs_flag & VFS_STATS) {
1802 vfsp->vfs_vskap = vskap;
1803 }
1804 vfs_unlock(vfsp);
1805 }
1806 }
1807 /* Return vfsp to caller. */
1808 *vfspp = vfsp;
1809 }
1810 errout:
1811 vfs_freeopttbl(&mnt_mntopts);
1812 if (resource != NULL)
1813 kmem_free(resource, strlen(resource) + 1);
1814 if (mountpt != NULL)
1815 kmem_free(mountpt, strlen(mountpt) + 1);
1816 /*
1817 * It is possible we errored prior to adding to mount in progress
1818 * table. Must free vnode we acquired with successful lookupname.
1819 */
1820 if (addmip)
1821 VN_RELE(bvp);
1822 if (delmip)
1823 vfs_delmip(vfsp);
1824 ASSERT(vswp != NULL);
1825 vfs_unrefvfssw(vswp);
1826 if (inargs != opts)
1827 kmem_free(inargs, MAX_MNTOPT_STR);
1828 if (copyout_error) {
1829 lofi_remove(vfsp);
1830 VFS_RELE(vfsp);
1831 error = copyout_error;
1832 }
1833 return (error);
1834 }
1835
1836 static void
1837 vfs_setpath(
1838 struct vfs *vfsp, /* vfs being updated */
1839 refstr_t **refp, /* Ref-count string to contain the new path */
1840 const char *newpath, /* Path to add to refp (above) */
1841 uint32_t flag) /* flag */
1842 {
1843 size_t len;
1844 refstr_t *ref;
1845 zone_t *zone = curproc->p_zone;
1846 char *sp;
1847 int have_list_lock = 0;
1848
1849 ASSERT(!VFS_ON_LIST(vfsp) || vfs_lock_held(vfsp));
1850
1851 /*
1852 * New path must be less than MAXPATHLEN because mntfs
1853 * will only display up to MAXPATHLEN bytes. This is currently
1854 * safe, because domount() uses pn_get(), and other callers
1855 * similarly cap the size to fewer than MAXPATHLEN bytes.
1856 */
1857
1858 ASSERT(strlen(newpath) < MAXPATHLEN);
1859
1860 /* mntfs requires consistency while vfs list lock is held */
1861
1862 if (VFS_ON_LIST(vfsp)) {
1863 have_list_lock = 1;
1864 vfs_list_lock();
1865 }
1866
1867 if (*refp != NULL)
1868 refstr_rele(*refp);
1869
1870 /*
1871 * If we are in a non-global zone then we prefix the supplied path,
1872 * newpath, with the zone's root path, with two exceptions. The first
1873 * is where we have been explicitly directed to avoid doing so; this
1874 * will be the case following a failed remount, where the path supplied
1875 * will be a saved version which must now be restored. The second
1876 * exception is where newpath is not a pathname but a descriptive name,
1877 * e.g. "procfs".
1878 */
1879 if (zone == global_zone || (flag & VFSSP_VERBATIM) || *newpath != '/') {
1880 ref = refstr_alloc(newpath);
1881 goto out;
1882 }
1883
1884 /*
1885 * Truncate the trailing '/' in the zoneroot, and merge
1886 * in the zone's rootpath with the "newpath" (resource
1887 * or mountpoint) passed in.
1888 *
1889 * The size of the required buffer is thus the size of
1890 * the buffer required for the passed-in newpath
1891 * (strlen(newpath) + 1), plus the size of the buffer
1892 * required to hold zone_rootpath (zone_rootpathlen)
1893 * minus one for one of the now-superfluous NUL
1894 * terminations, minus one for the trailing '/'.
1895 *
1896 * That gives us:
1897 *
1898 * (strlen(newpath) + 1) + zone_rootpathlen - 1 - 1
1899 *
1900 * Which is what we have below.
1901 */
1902
1903 len = strlen(newpath) + zone->zone_rootpathlen - 1;
1904 sp = kmem_alloc(len, KM_SLEEP);
1905
1906 /*
1907 * Copy everything including the trailing slash, which
1908 * we then overwrite with the NUL character.
1909 */
1910
1911 (void) strcpy(sp, zone->zone_rootpath);
1912 sp[zone->zone_rootpathlen - 2] = '\0';
1913 (void) strcat(sp, newpath);
1914
1915 ref = refstr_alloc(sp);
1916 kmem_free(sp, len);
1917 out:
1918 *refp = ref;
1919
1920 if (have_list_lock) {
1921 vfs_mnttab_modtimeupd();
1922 vfs_list_unlock();
1923 }
1924 }
1925
1926 /*
1927 * Record a mounted resource name in a vfs structure.
1928 * If vfsp is already mounted, caller must hold the vfs lock.
1929 */
1930 void
1931 vfs_setresource(struct vfs *vfsp, const char *resource, uint32_t flag)
1932 {
1933 if (resource == NULL || resource[0] == '\0')
1934 resource = VFS_NORESOURCE;
1935 vfs_setpath(vfsp, &vfsp->vfs_resource, resource, flag);
1936 }
1937
1938 /*
1939 * Record a mount point name in a vfs structure.
1940 * If vfsp is already mounted, caller must hold the vfs lock.
1941 */
1942 void
1943 vfs_setmntpoint(struct vfs *vfsp, const char *mntpt, uint32_t flag)
1944 {
1945 if (mntpt == NULL || mntpt[0] == '\0')
1946 mntpt = VFS_NOMNTPT;
1947 vfs_setpath(vfsp, &vfsp->vfs_mntpt, mntpt, flag);
1948 }
1949
1950 /* Returns the vfs_resource. Caller must call refstr_rele() when finished. */
1951
1952 refstr_t *
1953 vfs_getresource(const struct vfs *vfsp)
1954 {
1955 refstr_t *resource;
1956
1957 vfs_list_read_lock();
1958 resource = vfsp->vfs_resource;
1959 refstr_hold(resource);
1960 vfs_list_unlock();
1961
1962 return (resource);
1963 }
1964
1965 /* Returns the vfs_mntpt. Caller must call refstr_rele() when finished. */
1966
1967 refstr_t *
1968 vfs_getmntpoint(const struct vfs *vfsp)
1969 {
1970 refstr_t *mntpt;
1971
1972 vfs_list_read_lock();
1973 mntpt = vfsp->vfs_mntpt;
1974 refstr_hold(mntpt);
1975 vfs_list_unlock();
1976
1977 return (mntpt);
1978 }
1979
1980 /*
1981 * Create an empty options table with enough empty slots to hold all
1982 * The options in the options string passed as an argument.
1983 * Potentially prepend another options table.
1984 *
1985 * Note: caller is responsible for locking the vfs list, if needed,
1986 * to protect mops.
1987 */
1988 static void
1989 vfs_createopttbl_extend(mntopts_t *mops, const char *opts,
1990 const mntopts_t *mtmpl)
1991 {
1992 const char *s = opts;
1993 uint_t count;
1994
1995 if (opts == NULL || *opts == '\0') {
1996 count = 0;
1997 } else {
1998 count = 1;
1999
2000 /*
2001 * Count number of options in the string
2002 */
2003 for (s = strchr(s, ','); s != NULL; s = strchr(s, ',')) {
2004 count++;
2005 s++;
2006 }
2007 }
2008 vfs_copyopttbl_extend(mtmpl, mops, count);
2009 }
2010
2011 /*
2012 * Create an empty options table with enough empty slots to hold all
2013 * The options in the options string passed as an argument.
2014 *
2015 * This function is *not* for general use by filesystems.
2016 *
2017 * Note: caller is responsible for locking the vfs list, if needed,
2018 * to protect mops.
2019 */
2020 void
2021 vfs_createopttbl(mntopts_t *mops, const char *opts)
2022 {
2023 vfs_createopttbl_extend(mops, opts, NULL);
2024 }
2025
2026
2027 /*
2028 * Swap two mount options tables
2029 */
2030 static void
2031 vfs_swapopttbl_nolock(mntopts_t *optbl1, mntopts_t *optbl2)
2032 {
2033 uint_t tmpcnt;
2034 mntopt_t *tmplist;
2035
2036 tmpcnt = optbl2->mo_count;
2037 tmplist = optbl2->mo_list;
2038 optbl2->mo_count = optbl1->mo_count;
2039 optbl2->mo_list = optbl1->mo_list;
2040 optbl1->mo_count = tmpcnt;
2041 optbl1->mo_list = tmplist;
2042 }
2043
2044 static void
2045 vfs_swapopttbl(mntopts_t *optbl1, mntopts_t *optbl2)
2046 {
2047 vfs_list_lock();
2048 vfs_swapopttbl_nolock(optbl1, optbl2);
2049 vfs_mnttab_modtimeupd();
2050 vfs_list_unlock();
2051 }
2052
2053 static char **
2054 vfs_copycancelopt_extend(char **const moc, int extend)
2055 {
2056 int i = 0;
2057 int j;
2058 char **result;
2059
2060 if (moc != NULL) {
2061 for (; moc[i] != NULL; i++)
2062 /* count number of options to cancel */;
2063 }
2064
2065 if (i + extend == 0)
2066 return (NULL);
2067
2068 result = kmem_alloc((i + extend + 1) * sizeof (char *), KM_SLEEP);
2069
2070 for (j = 0; j < i; j++) {
2071 result[j] = kmem_alloc(strlen(moc[j]) + 1, KM_SLEEP);
2072 (void) strcpy(result[j], moc[j]);
2073 }
2074 for (; j <= i + extend; j++)
2075 result[j] = NULL;
2076
2077 return (result);
2078 }
2079
2080 static void
2081 vfs_copyopt(const mntopt_t *s, mntopt_t *d)
2082 {
2083 char *sp, *dp;
2084
2085 d->mo_flags = s->mo_flags;
2086 d->mo_data = s->mo_data;
2087 sp = s->mo_name;
2088 if (sp != NULL) {
2089 dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
2090 (void) strcpy(dp, sp);
2091 d->mo_name = dp;
2092 } else {
2093 d->mo_name = NULL; /* should never happen */
2094 }
2095
2096 d->mo_cancel = vfs_copycancelopt_extend(s->mo_cancel, 0);
2097
2098 sp = s->mo_arg;
2099 if (sp != NULL) {
2100 dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
2101 (void) strcpy(dp, sp);
2102 d->mo_arg = dp;
2103 } else {
2104 d->mo_arg = NULL;
2105 }
2106 }
2107
2108 /*
2109 * Copy a mount options table, possibly allocating some spare
2110 * slots at the end. It is permissible to copy_extend the NULL table.
2111 */
2112 static void
2113 vfs_copyopttbl_extend(const mntopts_t *smo, mntopts_t *dmo, int extra)
2114 {
2115 uint_t i, count;
2116 mntopt_t *motbl;
2117
2118 /*
2119 * Clear out any existing stuff in the options table being initialized
2120 */
2121 vfs_freeopttbl(dmo);
2122 count = (smo == NULL) ? 0 : smo->mo_count;
2123 if ((count + extra) == 0) /* nothing to do */
2124 return;
2125 dmo->mo_count = count + extra;
2126 motbl = kmem_zalloc((count + extra) * sizeof (mntopt_t), KM_SLEEP);
2127 dmo->mo_list = motbl;
2128 for (i = 0; i < count; i++) {
2129 vfs_copyopt(&smo->mo_list[i], &motbl[i]);
2130 }
2131 for (i = count; i < count + extra; i++) {
2132 motbl[i].mo_flags = MO_EMPTY;
2133 }
2134 }
2135
2136 /*
2137 * Copy a mount options table.
2138 *
2139 * This function is *not* for general use by filesystems.
2140 *
2141 * Note: caller is responsible for locking the vfs list, if needed,
2142 * to protect smo and dmo.
2143 */
2144 void
2145 vfs_copyopttbl(const mntopts_t *smo, mntopts_t *dmo)
2146 {
2147 vfs_copyopttbl_extend(smo, dmo, 0);
2148 }
2149
2150 static char **
2151 vfs_mergecancelopts(const mntopt_t *mop1, const mntopt_t *mop2)
2152 {
2153 int c1 = 0;
2154 int c2 = 0;
2155 char **result;
2156 char **sp1, **sp2, **dp;
2157
2158 /*
2159 * First we count both lists of cancel options.
2160 * If either is NULL or has no elements, we return a copy of
2161 * the other.
2162 */
2163 if (mop1->mo_cancel != NULL) {
2164 for (; mop1->mo_cancel[c1] != NULL; c1++)
2165 /* count cancel options in mop1 */;
2166 }
2167
2168 if (c1 == 0)
2169 return (vfs_copycancelopt_extend(mop2->mo_cancel, 0));
2170
2171 if (mop2->mo_cancel != NULL) {
2172 for (; mop2->mo_cancel[c2] != NULL; c2++)
2173 /* count cancel options in mop2 */;
2174 }
2175
2176 result = vfs_copycancelopt_extend(mop1->mo_cancel, c2);
2177
2178 if (c2 == 0)
2179 return (result);
2180
2181 /*
2182 * When we get here, we've got two sets of cancel options;
2183 * we need to merge the two sets. We know that the result
2184 * array has "c1+c2+1" entries and in the end we might shrink
2185 * it.
2186 * Result now has a copy of the c1 entries from mop1; we'll
2187 * now lookup all the entries of mop2 in mop1 and copy it if
2188 * it is unique.
2189 * This operation is O(n^2) but it's only called once per
2190 * filesystem per duplicate option. This is a situation
2191 * which doesn't arise with the filesystems in ON and
2192 * n is generally 1.
2193 */
2194
2195 dp = &result[c1];
2196 for (sp2 = mop2->mo_cancel; *sp2 != NULL; sp2++) {
2197 for (sp1 = mop1->mo_cancel; *sp1 != NULL; sp1++) {
2198 if (strcmp(*sp1, *sp2) == 0)
2199 break;
2200 }
2201 if (*sp1 == NULL) {
2202 /*
2203 * Option *sp2 not found in mop1, so copy it.
2204 * The calls to vfs_copycancelopt_extend()
2205 * guarantee that there's enough room.
2206 */
2207 *dp = kmem_alloc(strlen(*sp2) + 1, KM_SLEEP);
2208 (void) strcpy(*dp++, *sp2);
2209 }
2210 }
2211 if (dp != &result[c1+c2]) {
2212 size_t bytes = (dp - result + 1) * sizeof (char *);
2213 char **nres = kmem_alloc(bytes, KM_SLEEP);
2214
2215 bcopy(result, nres, bytes);
2216 kmem_free(result, (c1 + c2 + 1) * sizeof (char *));
2217 result = nres;
2218 }
2219 return (result);
2220 }
2221
2222 /*
2223 * Merge two mount option tables (outer and inner) into one. This is very
2224 * similar to "merging" global variables and automatic variables in C.
2225 *
2226 * This isn't (and doesn't have to be) fast.
2227 *
2228 * This function is *not* for general use by filesystems.
2229 *
2230 * Note: caller is responsible for locking the vfs list, if needed,
2231 * to protect omo, imo & dmo.
2232 */
2233 void
2234 vfs_mergeopttbl(const mntopts_t *omo, const mntopts_t *imo, mntopts_t *dmo)
2235 {
2236 uint_t i, count;
2237 mntopt_t *mop, *motbl;
2238 uint_t freeidx;
2239
2240 /*
2241 * First determine how much space we need to allocate.
2242 */
2243 count = omo->mo_count;
2244 for (i = 0; i < imo->mo_count; i++) {
2245 if (imo->mo_list[i].mo_flags & MO_EMPTY)
2246 continue;
2247 if (vfs_hasopt(omo, imo->mo_list[i].mo_name) == NULL)
2248 count++;
2249 }
2250 ASSERT(count >= omo->mo_count &&
2251 count <= omo->mo_count + imo->mo_count);
2252 motbl = kmem_alloc(count * sizeof (mntopt_t), KM_SLEEP);
2253 for (i = 0; i < omo->mo_count; i++)
2254 vfs_copyopt(&omo->mo_list[i], &motbl[i]);
2255 freeidx = omo->mo_count;
2256 for (i = 0; i < imo->mo_count; i++) {
2257 if (imo->mo_list[i].mo_flags & MO_EMPTY)
2258 continue;
2259 if ((mop = vfs_hasopt(omo, imo->mo_list[i].mo_name)) != NULL) {
2260 char **newcanp;
2261 uint_t index = mop - omo->mo_list;
2262
2263 newcanp = vfs_mergecancelopts(mop, &motbl[index]);
2264
2265 vfs_freeopt(&motbl[index]);
2266 vfs_copyopt(&imo->mo_list[i], &motbl[index]);
2267
2268 vfs_freecancelopt(motbl[index].mo_cancel);
2269 motbl[index].mo_cancel = newcanp;
2270 } else {
2271 /*
2272 * If it's a new option, just copy it over to the first
2273 * free location.
2274 */
2275 vfs_copyopt(&imo->mo_list[i], &motbl[freeidx++]);
2276 }
2277 }
2278 dmo->mo_count = count;
2279 dmo->mo_list = motbl;
2280 }
2281
2282 /*
2283 * Functions to set and clear mount options in a mount options table.
2284 */
2285
2286 /*
2287 * Clear a mount option, if it exists.
2288 *
2289 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2290 * the vfs list.
2291 */
2292 static void
2293 vfs_clearmntopt_nolock(mntopts_t *mops, const char *opt, int update_mnttab)
2294 {
2295 struct mntopt *mop;
2296 uint_t i, count;
2297
2298 ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2299
2300 count = mops->mo_count;
2301 for (i = 0; i < count; i++) {
2302 mop = &mops->mo_list[i];
2303
2304 if (mop->mo_flags & MO_EMPTY)
2305 continue;
2306 if (strcmp(opt, mop->mo_name))
2307 continue;
2308 mop->mo_flags &= ~MO_SET;
2309 if (mop->mo_arg != NULL) {
2310 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2311 }
2312 mop->mo_arg = NULL;
2313 if (update_mnttab)
2314 vfs_mnttab_modtimeupd();
2315 break;
2316 }
2317 }
2318
2319 void
2320 vfs_clearmntopt(struct vfs *vfsp, const char *opt)
2321 {
2322 int gotlock = 0;
2323
2324 if (VFS_ON_LIST(vfsp)) {
2325 gotlock = 1;
2326 vfs_list_lock();
2327 }
2328 vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, opt, gotlock);
2329 if (gotlock)
2330 vfs_list_unlock();
2331 }
2332
2333
2334 /*
2335 * Set a mount option on. If it's not found in the table, it's silently
2336 * ignored. If the option has MO_IGNORE set, it is still set unless the
2337 * VFS_NOFORCEOPT bit is set in the flags. Also, VFS_DISPLAY/VFS_NODISPLAY flag
2338 * bits can be used to toggle the MO_NODISPLAY bit for the option.
2339 * If the VFS_CREATEOPT flag bit is set then the first option slot with
2340 * MO_EMPTY set is created as the option passed in.
2341 *
2342 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2343 * the vfs list.
2344 */
2345 static void
2346 vfs_setmntopt_nolock(mntopts_t *mops, const char *opt,
2347 const char *arg, int flags, int update_mnttab)
2348 {
2349 mntopt_t *mop;
2350 uint_t i, count;
2351 char *sp;
2352
2353 ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2354
2355 if (flags & VFS_CREATEOPT) {
2356 if (vfs_hasopt(mops, opt) != NULL) {
2357 flags &= ~VFS_CREATEOPT;
2358 }
2359 }
2360 count = mops->mo_count;
2361 for (i = 0; i < count; i++) {
2362 mop = &mops->mo_list[i];
2363
2364 if (mop->mo_flags & MO_EMPTY) {
2365 if ((flags & VFS_CREATEOPT) == 0)
2366 continue;
2367 sp = kmem_alloc(strlen(opt) + 1, KM_SLEEP);
2368 (void) strcpy(sp, opt);
2369 mop->mo_name = sp;
2370 if (arg != NULL)
2371 mop->mo_flags = MO_HASVALUE;
2372 else
2373 mop->mo_flags = 0;
2374 } else if (strcmp(opt, mop->mo_name)) {
2375 continue;
2376 }
2377 if ((mop->mo_flags & MO_IGNORE) && (flags & VFS_NOFORCEOPT))
2378 break;
2379 if (arg != NULL && (mop->mo_flags & MO_HASVALUE) != 0) {
2380 sp = kmem_alloc(strlen(arg) + 1, KM_SLEEP);
2381 (void) strcpy(sp, arg);
2382 } else {
2383 sp = NULL;
2384 }
2385 if (mop->mo_arg != NULL)
2386 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2387 mop->mo_arg = sp;
2388 if (flags & VFS_DISPLAY)
2389 mop->mo_flags &= ~MO_NODISPLAY;
2390 if (flags & VFS_NODISPLAY)
2391 mop->mo_flags |= MO_NODISPLAY;
2392 mop->mo_flags |= MO_SET;
2393 if (mop->mo_cancel != NULL) {
2394 char **cp;
2395
2396 for (cp = mop->mo_cancel; *cp != NULL; cp++)
2397 vfs_clearmntopt_nolock(mops, *cp, 0);
2398 }
2399 if (update_mnttab)
2400 vfs_mnttab_modtimeupd();
2401 break;
2402 }
2403 }
2404
2405 void
2406 vfs_setmntopt(struct vfs *vfsp, const char *opt, const char *arg, int flags)
2407 {
2408 int gotlock = 0;
2409
2410 if (VFS_ON_LIST(vfsp)) {
2411 gotlock = 1;
2412 vfs_list_lock();
2413 }
2414 vfs_setmntopt_nolock(&vfsp->vfs_mntopts, opt, arg, flags, gotlock);
2415 if (gotlock)
2416 vfs_list_unlock();
2417 }
2418
2419
2420 /*
2421 * Add a "tag" option to a mounted file system's options list.
2422 *
2423 * Note: caller is responsible for locking the vfs list, if needed,
2424 * to protect mops.
2425 */
2426 static mntopt_t *
2427 vfs_addtag(mntopts_t *mops, const char *tag)
2428 {
2429 uint_t count;
2430 mntopt_t *mop, *motbl;
2431
2432 count = mops->mo_count + 1;
2433 motbl = kmem_zalloc(count * sizeof (mntopt_t), KM_SLEEP);
2434 if (mops->mo_count) {
2435 size_t len = (count - 1) * sizeof (mntopt_t);
2436
2437 bcopy(mops->mo_list, motbl, len);
2438 kmem_free(mops->mo_list, len);
2439 }
2440 mops->mo_count = count;
2441 mops->mo_list = motbl;
2442 mop = &motbl[count - 1];
2443 mop->mo_flags = MO_TAG;
2444 mop->mo_name = kmem_alloc(strlen(tag) + 1, KM_SLEEP);
2445 (void) strcpy(mop->mo_name, tag);
2446 return (mop);
2447 }
2448
2449 /*
2450 * Allow users to set arbitrary "tags" in a vfs's mount options.
2451 * Broader use within the kernel is discouraged.
2452 */
2453 int
2454 vfs_settag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2455 cred_t *cr)
2456 {
2457 vfs_t *vfsp;
2458 mntopts_t *mops;
2459 mntopt_t *mop;
2460 int found = 0;
2461 dev_t dev = makedevice(major, minor);
2462 int err = 0;
2463 char *buf = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
2464
2465 /*
2466 * Find the desired mounted file system
2467 */
2468 vfs_list_lock();
2469 vfsp = rootvfs;
2470 do {
2471 if (vfsp->vfs_dev == dev &&
2472 strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2473 found = 1;
2474 break;
2475 }
2476 vfsp = vfsp->vfs_next;
2477 } while (vfsp != rootvfs);
2478
2479 if (!found) {
2480 err = EINVAL;
2481 goto out;
2482 }
2483 err = secpolicy_fs_config(cr, vfsp);
2484 if (err != 0)
2485 goto out;
2486
2487 mops = &vfsp->vfs_mntopts;
2488 /*
2489 * Add tag if it doesn't already exist
2490 */
2491 if ((mop = vfs_hasopt(mops, tag)) == NULL) {
2492 int len;
2493
2494 (void) vfs_buildoptionstr(mops, buf, MAX_MNTOPT_STR);
2495 len = strlen(buf);
2496 if (len + strlen(tag) + 2 > MAX_MNTOPT_STR) {
2497 err = ENAMETOOLONG;
2498 goto out;
2499 }
2500 mop = vfs_addtag(mops, tag);
2501 }
2502 if ((mop->mo_flags & MO_TAG) == 0) {
2503 err = EINVAL;
2504 goto out;
2505 }
2506 vfs_setmntopt_nolock(mops, tag, NULL, 0, 1);
2507 out:
2508 vfs_list_unlock();
2509 kmem_free(buf, MAX_MNTOPT_STR);
2510 return (err);
2511 }
2512
2513 /*
2514 * Allow users to remove arbitrary "tags" in a vfs's mount options.
2515 * Broader use within the kernel is discouraged.
2516 */
2517 int
2518 vfs_clrtag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2519 cred_t *cr)
2520 {
2521 vfs_t *vfsp;
2522 mntopt_t *mop;
2523 int found = 0;
2524 dev_t dev = makedevice(major, minor);
2525 int err = 0;
2526
2527 /*
2528 * Find the desired mounted file system
2529 */
2530 vfs_list_lock();
2531 vfsp = rootvfs;
2532 do {
2533 if (vfsp->vfs_dev == dev &&
2534 strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2535 found = 1;
2536 break;
2537 }
2538 vfsp = vfsp->vfs_next;
2539 } while (vfsp != rootvfs);
2540
2541 if (!found) {
2542 err = EINVAL;
2543 goto out;
2544 }
2545 err = secpolicy_fs_config(cr, vfsp);
2546 if (err != 0)
2547 goto out;
2548
2549 if ((mop = vfs_hasopt(&vfsp->vfs_mntopts, tag)) == NULL) {
2550 err = EINVAL;
2551 goto out;
2552 }
2553 if ((mop->mo_flags & MO_TAG) == 0) {
2554 err = EINVAL;
2555 goto out;
2556 }
2557 vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, tag, 1);
2558 out:
2559 vfs_list_unlock();
2560 return (err);
2561 }
2562
2563 /*
2564 * Function to parse an option string and fill in a mount options table.
2565 * Unknown options are silently ignored. The input option string is modified
2566 * by replacing separators with nulls. If the create flag is set, options
2567 * not found in the table are just added on the fly. The table must have
2568 * an option slot marked MO_EMPTY to add an option on the fly.
2569 *
2570 * This function is *not* for general use by filesystems.
2571 *
2572 * Note: caller is responsible for locking the vfs list, if needed,
2573 * to protect mops..
2574 */
2575 void
2576 vfs_parsemntopts(mntopts_t *mops, char *osp, int create)
2577 {
2578 char *s = osp, *p, *nextop, *valp, *cp, *ep;
2579 int setflg = VFS_NOFORCEOPT;
2580
2581 if (osp == NULL)
2582 return;
2583 while (*s != '\0') {
2584 p = strchr(s, ','); /* find next option */
2585 if (p == NULL) {
2586 cp = NULL;
2587 p = s + strlen(s);
2588 } else {
2589 cp = p; /* save location of comma */
2590 *p++ = '\0'; /* mark end and point to next option */
2591 }
2592 nextop = p;
2593 p = strchr(s, '='); /* look for value */
2594 if (p == NULL) {
2595 valp = NULL; /* no value supplied */
2596 } else {
2597 ep = p; /* save location of equals */
2598 *p++ = '\0'; /* end option and point to value */
2599 valp = p;
2600 }
2601 /*
2602 * set option into options table
2603 */
2604 if (create)
2605 setflg |= VFS_CREATEOPT;
2606 vfs_setmntopt_nolock(mops, s, valp, setflg, 0);
2607 if (cp != NULL)
2608 *cp = ','; /* restore the comma */
2609 if (valp != NULL)
2610 *ep = '='; /* restore the equals */
2611 s = nextop;
2612 }
2613 }
2614
2615 /*
2616 * Function to inquire if an option exists in a mount options table.
2617 * Returns a pointer to the option if it exists, else NULL.
2618 *
2619 * This function is *not* for general use by filesystems.
2620 *
2621 * Note: caller is responsible for locking the vfs list, if needed,
2622 * to protect mops.
2623 */
2624 struct mntopt *
2625 vfs_hasopt(const mntopts_t *mops, const char *opt)
2626 {
2627 struct mntopt *mop;
2628 uint_t i, count;
2629
2630 count = mops->mo_count;
2631 for (i = 0; i < count; i++) {
2632 mop = &mops->mo_list[i];
2633
2634 if (mop->mo_flags & MO_EMPTY)
2635 continue;
2636 if (strcmp(opt, mop->mo_name) == 0)
2637 return (mop);
2638 }
2639 return (NULL);
2640 }
2641
2642 /*
2643 * Function to inquire if an option is set in a mount options table.
2644 * Returns non-zero if set and fills in the arg pointer with a pointer to
2645 * the argument string or NULL if there is no argument string.
2646 */
2647 static int
2648 vfs_optionisset_nolock(const mntopts_t *mops, const char *opt, char **argp)
2649 {
2650 struct mntopt *mop;
2651 uint_t i, count;
2652
2653 count = mops->mo_count;
2654 for (i = 0; i < count; i++) {
2655 mop = &mops->mo_list[i];
2656
2657 if (mop->mo_flags & MO_EMPTY)
2658 continue;
2659 if (strcmp(opt, mop->mo_name))
2660 continue;
2661 if ((mop->mo_flags & MO_SET) == 0)
2662 return (0);
2663 if (argp != NULL && (mop->mo_flags & MO_HASVALUE) != 0)
2664 *argp = mop->mo_arg;
2665 return (1);
2666 }
2667 return (0);
2668 }
2669
2670
2671 int
2672 vfs_optionisset(const struct vfs *vfsp, const char *opt, char **argp)
2673 {
2674 int ret;
2675
2676 vfs_list_read_lock();
2677 ret = vfs_optionisset_nolock(&vfsp->vfs_mntopts, opt, argp);
2678 vfs_list_unlock();
2679 return (ret);
2680 }
2681
2682
2683 /*
2684 * Construct a comma separated string of the options set in the given
2685 * mount table, return the string in the given buffer. Return non-zero if
2686 * the buffer would overflow.
2687 *
2688 * This function is *not* for general use by filesystems.
2689 *
2690 * Note: caller is responsible for locking the vfs list, if needed,
2691 * to protect mp.
2692 */
2693 int
2694 vfs_buildoptionstr(const mntopts_t *mp, char *buf, int len)
2695 {
2696 char *cp;
2697 uint_t i;
2698
2699 buf[0] = '\0';
2700 cp = buf;
2701 for (i = 0; i < mp->mo_count; i++) {
2702 struct mntopt *mop;
2703
2704 mop = &mp->mo_list[i];
2705 if (mop->mo_flags & MO_SET) {
2706 int optlen, comma = 0;
2707
2708 if (buf[0] != '\0')
2709 comma = 1;
2710 optlen = strlen(mop->mo_name);
2711 if (strlen(buf) + comma + optlen + 1 > len)
2712 goto err;
2713 if (comma)
2714 *cp++ = ',';
2715 (void) strcpy(cp, mop->mo_name);
2716 cp += optlen;
2717 /*
2718 * Append option value if there is one
2719 */
2720 if (mop->mo_arg != NULL) {
2721 int arglen;
2722
2723 arglen = strlen(mop->mo_arg);
2724 if (strlen(buf) + arglen + 2 > len)
2725 goto err;
2726 *cp++ = '=';
2727 (void) strcpy(cp, mop->mo_arg);
2728 cp += arglen;
2729 }
2730 }
2731 }
2732 return (0);
2733 err:
2734 return (EOVERFLOW);
2735 }
2736
2737 static void
2738 vfs_freecancelopt(char **moc)
2739 {
2740 if (moc != NULL) {
2741 int ccnt = 0;
2742 char **cp;
2743
2744 for (cp = moc; *cp != NULL; cp++) {
2745 kmem_free(*cp, strlen(*cp) + 1);
2746 ccnt++;
2747 }
2748 kmem_free(moc, (ccnt + 1) * sizeof (char *));
2749 }
2750 }
2751
2752 static void
2753 vfs_freeopt(mntopt_t *mop)
2754 {
2755 if (mop->mo_name != NULL)
2756 kmem_free(mop->mo_name, strlen(mop->mo_name) + 1);
2757
2758 vfs_freecancelopt(mop->mo_cancel);
2759
2760 if (mop->mo_arg != NULL)
2761 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2762 }
2763
2764 /*
2765 * Free a mount options table
2766 *
2767 * This function is *not* for general use by filesystems.
2768 *
2769 * Note: caller is responsible for locking the vfs list, if needed,
2770 * to protect mp.
2771 */
2772 void
2773 vfs_freeopttbl(mntopts_t *mp)
2774 {
2775 uint_t i, count;
2776
2777 count = mp->mo_count;
2778 for (i = 0; i < count; i++) {
2779 vfs_freeopt(&mp->mo_list[i]);
2780 }
2781 if (count) {
2782 kmem_free(mp->mo_list, sizeof (mntopt_t) * count);
2783 mp->mo_count = 0;
2784 mp->mo_list = NULL;
2785 }
2786 }
2787
2788
2789 /* ARGSUSED */
2790 static int
2791 vfs_mntdummyread(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2792 caller_context_t *ct)
2793 {
2794 return (0);
2795 }
2796
2797 /* ARGSUSED */
2798 static int
2799 vfs_mntdummywrite(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2800 caller_context_t *ct)
2801 {
2802 return (0);
2803 }
2804
2805 /*
2806 * The dummy vnode is currently used only by file events notification
2807 * module which is just interested in the timestamps.
2808 */
2809 /* ARGSUSED */
2810 static int
2811 vfs_mntdummygetattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2812 caller_context_t *ct)
2813 {
2814 bzero(vap, sizeof (vattr_t));
2815 vap->va_type = VREG;
2816 vap->va_nlink = 1;
2817 vap->va_ctime = vfs_mnttab_ctime;
2818 /*
2819 * it is ok to just copy mtime as the time will be monotonically
2820 * increasing.
2821 */
2822 vap->va_mtime = vfs_mnttab_mtime;
2823 vap->va_atime = vap->va_mtime;
2824 return (0);
2825 }
2826
2827 static void
2828 vfs_mnttabvp_setup(void)
2829 {
2830 vnode_t *tvp;
2831 vnodeops_t *vfs_mntdummyvnops;
2832 const fs_operation_def_t mnt_dummyvnodeops_template[] = {
2833 VOPNAME_READ, { .vop_read = vfs_mntdummyread },
2834 VOPNAME_WRITE, { .vop_write = vfs_mntdummywrite },
2835 VOPNAME_GETATTR, { .vop_getattr = vfs_mntdummygetattr },
2836 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
2837 NULL, NULL
2838 };
2839
2840 if (vn_make_ops("mnttab", mnt_dummyvnodeops_template,
2841 &vfs_mntdummyvnops) != 0) {
2842 cmn_err(CE_WARN, "vfs_mnttabvp_setup: vn_make_ops failed");
2843 /* Shouldn't happen, but not bad enough to panic */
2844 return;
2845 }
2846
2847 /*
2848 * A global dummy vnode is allocated to represent mntfs files.
2849 * The mntfs file (/etc/mnttab) can be monitored for file events
2850 * and receive an event when mnttab changes. Dummy VOP calls
2851 * will be made on this vnode. The file events notification module
2852 * intercepts this vnode and delivers relevant events.
2853 */
2854 tvp = vn_alloc(KM_SLEEP);
2855 tvp->v_flag = VNOMOUNT|VNOMAP|VNOSWAP|VNOCACHE;
2856 vn_setops(tvp, vfs_mntdummyvnops);
2857 tvp->v_type = VREG;
2858 /*
2859 * The mnt dummy ops do not reference v_data.
2860 * No other module intercepting this vnode should either.
2861 * Just set it to point to itself.
2862 */
2863 tvp->v_data = (caddr_t)tvp;
2864 tvp->v_vfsp = rootvfs;
2865 vfs_mntdummyvp = tvp;
2866 }
2867
2868 /*
2869 * performs fake read/write ops
2870 */
2871 static void
2872 vfs_mnttab_rwop(int rw)
2873 {
2874 struct uio uio;
2875 struct iovec iov;
2876 char buf[1];
2877
2878 if (vfs_mntdummyvp == NULL)
2879 return;
2880
2881 bzero(&uio, sizeof (uio));
2882 bzero(&iov, sizeof (iov));
2883 iov.iov_base = buf;
2884 iov.iov_len = 0;
2885 uio.uio_iov = &iov;
2886 uio.uio_iovcnt = 1;
2887 uio.uio_loffset = 0;
2888 uio.uio_segflg = UIO_SYSSPACE;
2889 uio.uio_resid = 0;
2890 if (rw) {
2891 (void) VOP_WRITE(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2892 } else {
2893 (void) VOP_READ(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2894 }
2895 }
2896
2897 /*
2898 * Generate a write operation.
2899 */
2900 void
2901 vfs_mnttab_writeop(void)
2902 {
2903 vfs_mnttab_rwop(1);
2904 }
2905
2906 /*
2907 * Generate a read operation.
2908 */
2909 void
2910 vfs_mnttab_readop(void)
2911 {
2912 vfs_mnttab_rwop(0);
2913 }
2914
2915 /*
2916 * Free any mnttab information recorded in the vfs struct.
2917 * The vfs must not be on the vfs list.
2918 */
2919 static void
2920 vfs_freemnttab(struct vfs *vfsp)
2921 {
2922 ASSERT(!VFS_ON_LIST(vfsp));
2923
2924 /*
2925 * Free device and mount point information
2926 */
2927 if (vfsp->vfs_mntpt != NULL) {
2928 refstr_rele(vfsp->vfs_mntpt);
2929 vfsp->vfs_mntpt = NULL;
2930 }
2931 if (vfsp->vfs_resource != NULL) {
2932 refstr_rele(vfsp->vfs_resource);
2933 vfsp->vfs_resource = NULL;
2934 }
2935 /*
2936 * Now free mount options information
2937 */
2938 vfs_freeopttbl(&vfsp->vfs_mntopts);
2939 }
2940
2941 /*
2942 * Return the last mnttab modification time
2943 */
2944 void
2945 vfs_mnttab_modtime(timespec_t *ts)
2946 {
2947 ASSERT(RW_LOCK_HELD(&vfslist));
2948 *ts = vfs_mnttab_mtime;
2949 }
2950
2951 /*
2952 * See if mnttab is changed
2953 */
2954 void
2955 vfs_mnttab_poll(timespec_t *old, struct pollhead **phpp)
2956 {
2957 int changed;
2958
2959 *phpp = (struct pollhead *)NULL;
2960
2961 /*
2962 * Note: don't grab vfs list lock before accessing vfs_mnttab_mtime.
2963 * Can lead to deadlock against vfs_mnttab_modtimeupd(). It is safe
2964 * to not grab the vfs list lock because tv_sec is monotonically
2965 * increasing.
2966 */
2967
2968 changed = (old->tv_nsec != vfs_mnttab_mtime.tv_nsec) ||
2969 (old->tv_sec != vfs_mnttab_mtime.tv_sec);
2970 if (!changed) {
2971 *phpp = &vfs_pollhd;
2972 }
2973 }
2974
2975 /* Provide a unique and monotonically-increasing timestamp. */
2976 void
2977 vfs_mono_time(timespec_t *ts)
2978 {
2979 static volatile hrtime_t hrt; /* The saved time. */
2980 hrtime_t newhrt, oldhrt; /* For effecting the CAS. */
2981 timespec_t newts;
2982
2983 /*
2984 * Try gethrestime() first, but be prepared to fabricate a sensible
2985 * answer at the first sign of any trouble.
2986 */
2987 gethrestime(&newts);
2988 newhrt = ts2hrt(&newts);
2989 for (;;) {
2990 oldhrt = hrt;
2991 if (newhrt <= hrt)
2992 newhrt = hrt + 1;
2993 if (atomic_cas_64((uint64_t *)&hrt, oldhrt, newhrt) == oldhrt)
2994 break;
2995 }
2996 hrt2ts(newhrt, ts);
2997 }
2998
2999 /*
3000 * Update the mnttab modification time and wake up any waiters for
3001 * mnttab changes
3002 */
3003 void
3004 vfs_mnttab_modtimeupd()
3005 {
3006 hrtime_t oldhrt, newhrt;
3007
3008 ASSERT(RW_WRITE_HELD(&vfslist));
3009 oldhrt = ts2hrt(&vfs_mnttab_mtime);
3010 gethrestime(&vfs_mnttab_mtime);
3011 newhrt = ts2hrt(&vfs_mnttab_mtime);
3012 if (oldhrt == (hrtime_t)0)
3013 vfs_mnttab_ctime = vfs_mnttab_mtime;
3014 /*
3015 * Attempt to provide unique mtime (like uniqtime but not).
3016 */
3017 if (newhrt == oldhrt) {
3018 newhrt++;
3019 hrt2ts(newhrt, &vfs_mnttab_mtime);
3020 }
3021 pollwakeup(&vfs_pollhd, (short)POLLRDBAND);
3022 vfs_mnttab_writeop();
3023 }
3024
3025 int
3026 dounmount(struct vfs *vfsp, int flag, cred_t *cr)
3027 {
3028 vnode_t *coveredvp;
3029 int error;
3030 extern void teardown_vopstats(vfs_t *);
3031
3032 /*
3033 * Get covered vnode. This will be NULL if the vfs is not linked
3034 * into the file system name space (i.e., domount() with MNT_NOSPICE).
3035 */
3036 coveredvp = vfsp->vfs_vnodecovered;
3037 ASSERT(coveredvp == NULL || vn_vfswlock_held(coveredvp));
3038
3039 /*
3040 * Purge all dnlc entries for this vfs.
3041 */
3042 (void) dnlc_purge_vfsp(vfsp, 0);
3043
3044 /* For forcible umount, skip VFS_SYNC() since it may hang */
3045 if ((flag & MS_FORCE) == 0)
3046 (void) VFS_SYNC(vfsp, 0, cr);
3047
3048 /*
3049 * Lock the vfs to maintain fs status quo during unmount. This
3050 * has to be done after the sync because ufs_update tries to acquire
3051 * the vfs_reflock.
3052 */
3053 vfs_lock_wait(vfsp);
3054
3055 if (error = VFS_UNMOUNT(vfsp, flag, cr)) {
3056 vfs_unlock(vfsp);
3057 if (coveredvp != NULL)
3058 vn_vfsunlock(coveredvp);
3059 } else if (coveredvp != NULL) {
3060 teardown_vopstats(vfsp);
3061 /*
3062 * vfs_remove() will do a VN_RELE(vfsp->vfs_vnodecovered)
3063 * when it frees vfsp so we do a VN_HOLD() so we can
3064 * continue to use coveredvp afterwards.
3065 */
3066 VN_HOLD(coveredvp);
3067 vfs_remove(vfsp);
3068 vn_vfsunlock(coveredvp);
3069 VN_RELE(coveredvp);
3070 } else {
3071 teardown_vopstats(vfsp);
3072 /*
3073 * Release the reference to vfs that is not linked
3074 * into the name space.
3075 */
3076 vfs_unlock(vfsp);
3077 VFS_RELE(vfsp);
3078 }
3079 return (error);
3080 }
3081
3082
3083 /*
3084 * Vfs_unmountall() is called by uadmin() to unmount all
3085 * mounted file systems (except the root file system) during shutdown.
3086 * It follows the existing locking protocol when traversing the vfs list
3087 * to sync and unmount vfses. Even though there should be no
3088 * other thread running while the system is shutting down, it is prudent
3089 * to still follow the locking protocol.
3090 */
3091 void
3092 vfs_unmountall(void)
3093 {
3094 struct vfs *vfsp;
3095 struct vfs *prev_vfsp = NULL;
3096 int error;
3097
3098 /*
3099 * Toss all dnlc entries now so that the per-vfs sync
3100 * and unmount operations don't have to slog through
3101 * a bunch of uninteresting vnodes over and over again.
3102 */
3103 dnlc_purge();
3104
3105 vfs_list_lock();
3106 for (vfsp = rootvfs->vfs_prev; vfsp != rootvfs; vfsp = prev_vfsp) {
3107 prev_vfsp = vfsp->vfs_prev;
3108
3109 if (vfs_lock(vfsp) != 0)
3110 continue;
3111 error = vn_vfswlock(vfsp->vfs_vnodecovered);
3112 vfs_unlock(vfsp);
3113 if (error)
3114 continue;
3115
3116 vfs_list_unlock();
3117
3118 (void) VFS_SYNC(vfsp, SYNC_CLOSE, CRED());
3119 (void) dounmount(vfsp, 0, CRED());
3120
3121 /*
3122 * Since we dropped the vfslist lock above we must
3123 * verify that next_vfsp still exists, else start over.
3124 */
3125 vfs_list_lock();
3126 for (vfsp = rootvfs->vfs_prev;
3127 vfsp != rootvfs; vfsp = vfsp->vfs_prev)
3128 if (vfsp == prev_vfsp)
3129 break;
3130 if (vfsp == rootvfs && prev_vfsp != rootvfs)
3131 prev_vfsp = rootvfs->vfs_prev;
3132 }
3133 vfs_list_unlock();
3134 }
3135
3136 /*
3137 * Called to add an entry to the end of the vfs mount in progress list
3138 */
3139 void
3140 vfs_addmip(dev_t dev, struct vfs *vfsp)
3141 {
3142 struct ipmnt *mipp;
3143
3144 mipp = (struct ipmnt *)kmem_alloc(sizeof (struct ipmnt), KM_SLEEP);
3145 mipp->mip_next = NULL;
3146 mipp->mip_dev = dev;
3147 mipp->mip_vfsp = vfsp;
3148 mutex_enter(&vfs_miplist_mutex);
3149 if (vfs_miplist_end != NULL)
3150 vfs_miplist_end->mip_next = mipp;
3151 else
3152 vfs_miplist = mipp;
3153 vfs_miplist_end = mipp;
3154 mutex_exit(&vfs_miplist_mutex);
3155 }
3156
3157 /*
3158 * Called to remove an entry from the mount in progress list
3159 * Either because the mount completed or it failed.
3160 */
3161 void
3162 vfs_delmip(struct vfs *vfsp)
3163 {
3164 struct ipmnt *mipp, *mipprev;
3165
3166 mutex_enter(&vfs_miplist_mutex);
3167 mipprev = NULL;
3168 for (mipp = vfs_miplist;
3169 mipp && mipp->mip_vfsp != vfsp; mipp = mipp->mip_next) {
3170 mipprev = mipp;
3171 }
3172 if (mipp == NULL)
3173 return; /* shouldn't happen */
3174 if (mipp == vfs_miplist_end)
3175 vfs_miplist_end = mipprev;
3176 if (mipprev == NULL)
3177 vfs_miplist = mipp->mip_next;
3178 else
3179 mipprev->mip_next = mipp->mip_next;
3180 mutex_exit(&vfs_miplist_mutex);
3181 kmem_free(mipp, sizeof (struct ipmnt));
3182 }
3183
3184 /*
3185 * vfs_add is called by a specific filesystem's mount routine to add
3186 * the new vfs into the vfs list/hash and to cover the mounted-on vnode.
3187 * The vfs should already have been locked by the caller.
3188 *
3189 * coveredvp is NULL if this is the root.
3190 */
3191 void
3192 vfs_add(vnode_t *coveredvp, struct vfs *vfsp, int mflag)
3193 {
3194 int newflag;
3195
3196 ASSERT(vfs_lock_held(vfsp));
3197 VFS_HOLD(vfsp);
3198 newflag = vfsp->vfs_flag;
3199 if (mflag & MS_RDONLY)
3200 newflag |= VFS_RDONLY;
3201 else
3202 newflag &= ~VFS_RDONLY;
3203 if (mflag & MS_NOSUID)
3204 newflag |= (VFS_NOSETUID|VFS_NODEVICES);
3205 else
3206 newflag &= ~(VFS_NOSETUID|VFS_NODEVICES);
3207 if (mflag & MS_NOMNTTAB)
3208 newflag |= VFS_NOMNTTAB;
3209 else
3210 newflag &= ~VFS_NOMNTTAB;
3211
3212 if (coveredvp != NULL) {
3213 ASSERT(vn_vfswlock_held(coveredvp));
3214 coveredvp->v_vfsmountedhere = vfsp;
3215 VN_HOLD(coveredvp);
3216 }
3217 vfsp->vfs_vnodecovered = coveredvp;
3218 vfsp->vfs_flag = newflag;
3219
3220 vfs_list_add(vfsp);
3221 }
3222
3223 /*
3224 * Remove a vfs from the vfs list, null out the pointer from the
3225 * covered vnode to the vfs (v_vfsmountedhere), and null out the pointer
3226 * from the vfs to the covered vnode (vfs_vnodecovered). Release the
3227 * reference to the vfs and to the covered vnode.
3228 *
3229 * Called from dounmount after it's confirmed with the file system
3230 * that the unmount is legal.
3231 */
3232 void
3233 vfs_remove(struct vfs *vfsp)
3234 {
3235 vnode_t *vp;
3236
3237 ASSERT(vfs_lock_held(vfsp));
3238
3239 /*
3240 * Can't unmount root. Should never happen because fs will
3241 * be busy.
3242 */
3243 if (vfsp == rootvfs)
3244 panic("vfs_remove: unmounting root");
3245
3246 vfs_list_remove(vfsp);
3247
3248 /*
3249 * Unhook from the file system name space.
3250 */
3251 vp = vfsp->vfs_vnodecovered;
3252 ASSERT(vn_vfswlock_held(vp));
3253 vp->v_vfsmountedhere = NULL;
3254 vfsp->vfs_vnodecovered = NULL;
3255 VN_RELE(vp);
3256
3257 /*
3258 * Release lock and wakeup anybody waiting.
3259 */
3260 vfs_unlock(vfsp);
3261 VFS_RELE(vfsp);
3262 }
3263
3264 /*
3265 * Lock a filesystem to prevent access to it while mounting,
3266 * unmounting and syncing. Return EBUSY immediately if lock
3267 * can't be acquired.
3268 */
3269 int
3270 vfs_lock(vfs_t *vfsp)
3271 {
3272 vn_vfslocks_entry_t *vpvfsentry;
3273
3274 vpvfsentry = vn_vfslocks_getlock(vfsp);
3275 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
3276 return (0);
3277
3278 vn_vfslocks_rele(vpvfsentry);
3279 return (EBUSY);
3280 }
3281
3282 int
3283 vfs_rlock(vfs_t *vfsp)
3284 {
3285 vn_vfslocks_entry_t *vpvfsentry;
3286
3287 vpvfsentry = vn_vfslocks_getlock(vfsp);
3288
3289 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
3290 return (0);
3291
3292 vn_vfslocks_rele(vpvfsentry);
3293 return (EBUSY);
3294 }
3295
3296 void
3297 vfs_lock_wait(vfs_t *vfsp)
3298 {
3299 vn_vfslocks_entry_t *vpvfsentry;
3300
3301 vpvfsentry = vn_vfslocks_getlock(vfsp);
3302 rwst_enter(&vpvfsentry->ve_lock, RW_WRITER);
3303 }
3304
3305 void
3306 vfs_rlock_wait(vfs_t *vfsp)
3307 {
3308 vn_vfslocks_entry_t *vpvfsentry;
3309
3310 vpvfsentry = vn_vfslocks_getlock(vfsp);
3311 rwst_enter(&vpvfsentry->ve_lock, RW_READER);
3312 }
3313
3314 /*
3315 * Unlock a locked filesystem.
3316 */
3317 void
3318 vfs_unlock(vfs_t *vfsp)
3319 {
3320 vn_vfslocks_entry_t *vpvfsentry;
3321
3322 /*
3323 * vfs_unlock will mimic sema_v behaviour to fix 4748018.
3324 * And these changes should remain for the patch changes as it is.
3325 */
3326 if (panicstr)
3327 return;
3328
3329 /*
3330 * ve_refcount needs to be dropped twice here.
3331 * 1. To release refernce after a call to vfs_locks_getlock()
3332 * 2. To release the reference from the locking routines like
3333 * vfs_rlock_wait/vfs_wlock_wait/vfs_wlock etc,.
3334 */
3335
3336 vpvfsentry = vn_vfslocks_getlock(vfsp);
3337 vn_vfslocks_rele(vpvfsentry);
3338
3339 rwst_exit(&vpvfsentry->ve_lock);
3340 vn_vfslocks_rele(vpvfsentry);
3341 }
3342
3343 /*
3344 * Utility routine that allows a filesystem to construct its
3345 * fsid in "the usual way" - by munging some underlying dev_t and
3346 * the filesystem type number into the 64-bit fsid. Note that
3347 * this implicitly relies on dev_t persistence to make filesystem
3348 * id's persistent.
3349 *
3350 * There's nothing to prevent an individual fs from constructing its
3351 * fsid in a different way, and indeed they should.
3352 *
3353 * Since we want fsids to be 32-bit quantities (so that they can be
3354 * exported identically by either 32-bit or 64-bit APIs, as well as
3355 * the fact that fsid's are "known" to NFS), we compress the device
3356 * number given down to 32-bits, and panic if that isn't possible.
3357 */
3358 void
3359 vfs_make_fsid(fsid_t *fsi, dev_t dev, int val)
3360 {
3361 if (!cmpldev((dev32_t *)&fsi->val[0], dev))
3362 panic("device number too big for fsid!");
3363 fsi->val[1] = val;
3364 }
3365
3366 int
3367 vfs_lock_held(vfs_t *vfsp)
3368 {
3369 int held;
3370 vn_vfslocks_entry_t *vpvfsentry;
3371
3372 /*
3373 * vfs_lock_held will mimic sema_held behaviour
3374 * if panicstr is set. And these changes should remain
3375 * for the patch changes as it is.
3376 */
3377 if (panicstr)
3378 return (1);
3379
3380 vpvfsentry = vn_vfslocks_getlock(vfsp);
3381 held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
3382
3383 vn_vfslocks_rele(vpvfsentry);
3384 return (held);
3385 }
3386
3387 struct _kthread *
3388 vfs_lock_owner(vfs_t *vfsp)
3389 {
3390 struct _kthread *owner;
3391 vn_vfslocks_entry_t *vpvfsentry;
3392
3393 /*
3394 * vfs_wlock_held will mimic sema_held behaviour
3395 * if panicstr is set. And these changes should remain
3396 * for the patch changes as it is.
3397 */
3398 if (panicstr)
3399 return (NULL);
3400
3401 vpvfsentry = vn_vfslocks_getlock(vfsp);
3402 owner = rwst_owner(&vpvfsentry->ve_lock);
3403
3404 vn_vfslocks_rele(vpvfsentry);
3405 return (owner);
3406 }
3407
3408 /*
3409 * vfs list locking.
3410 *
3411 * Rather than manipulate the vfslist lock directly, we abstract into lock
3412 * and unlock routines to allow the locking implementation to be changed for
3413 * clustering.
3414 *
3415 * Whenever the vfs list is modified through its hash links, the overall list
3416 * lock must be obtained before locking the relevant hash bucket. But to see
3417 * whether a given vfs is on the list, it suffices to obtain the lock for the
3418 * hash bucket without getting the overall list lock. (See getvfs() below.)
3419 */
3420
3421 void
3422 vfs_list_lock()
3423 {
3424 rw_enter(&vfslist, RW_WRITER);
3425 }
3426
3427 void
3428 vfs_list_read_lock()
3429 {
3430 rw_enter(&vfslist, RW_READER);
3431 }
3432
3433 void
3434 vfs_list_unlock()
3435 {
3436 rw_exit(&vfslist);
3437 }
3438
3439 /*
3440 * Low level worker routines for adding entries to and removing entries from
3441 * the vfs list.
3442 */
3443
3444 static void
3445 vfs_hash_add(struct vfs *vfsp, int insert_at_head)
3446 {
3447 int vhno;
3448 struct vfs **hp;
3449 dev_t dev;
3450
3451 ASSERT(RW_WRITE_HELD(&vfslist));
3452
3453 dev = expldev(vfsp->vfs_fsid.val[0]);
3454 vhno = VFSHASH(getmajor(dev), getminor(dev));
3455
3456 mutex_enter(&rvfs_list[vhno].rvfs_lock);
3457
3458 /*
3459 * Link into the hash table, inserting it at the end, so that LOFS
3460 * with the same fsid as UFS (or other) file systems will not hide the
3461 * UFS.
3462 */
3463 if (insert_at_head) {
3464 vfsp->vfs_hash = rvfs_list[vhno].rvfs_head;
3465 rvfs_list[vhno].rvfs_head = vfsp;
3466 } else {
3467 for (hp = &rvfs_list[vhno].rvfs_head; *hp != NULL;
3468 hp = &(*hp)->vfs_hash)
3469 continue;
3470 /*
3471 * hp now contains the address of the pointer to update
3472 * to effect the insertion.
3473 */
3474 vfsp->vfs_hash = NULL;
3475 *hp = vfsp;
3476 }
3477
3478 rvfs_list[vhno].rvfs_len++;
3479 mutex_exit(&rvfs_list[vhno].rvfs_lock);
3480 }
3481
3482
3483 static void
3484 vfs_hash_remove(struct vfs *vfsp)
3485 {
3486 int vhno;
3487 struct vfs *tvfsp;
3488 dev_t dev;
3489
3490 ASSERT(RW_WRITE_HELD(&vfslist));
3491
3492 dev = expldev(vfsp->vfs_fsid.val[0]);
3493 vhno = VFSHASH(getmajor(dev), getminor(dev));
3494
3495 mutex_enter(&rvfs_list[vhno].rvfs_lock);
3496
3497 /*
3498 * Remove from hash.
3499 */
3500 if (rvfs_list[vhno].rvfs_head == vfsp) {
3501 rvfs_list[vhno].rvfs_head = vfsp->vfs_hash;
3502 rvfs_list[vhno].rvfs_len--;
3503 goto foundit;
3504 }
3505 for (tvfsp = rvfs_list[vhno].rvfs_head; tvfsp != NULL;
3506 tvfsp = tvfsp->vfs_hash) {
3507 if (tvfsp->vfs_hash == vfsp) {
3508 tvfsp->vfs_hash = vfsp->vfs_hash;
3509 rvfs_list[vhno].rvfs_len--;
3510 goto foundit;
3511 }
3512 }
3513 cmn_err(CE_WARN, "vfs_list_remove: vfs not found in hash");
3514
3515 foundit:
3516
3517 mutex_exit(&rvfs_list[vhno].rvfs_lock);
3518 }
3519
3520
3521 void
3522 vfs_list_add(struct vfs *vfsp)
3523 {
3524 zone_t *zone;
3525
3526 /*
3527 * Typically, the vfs_t will have been created on behalf of the file
3528 * system in vfs_init, where it will have been provided with a
3529 * vfs_impl_t. This, however, might be lacking if the vfs_t was created
3530 * by an unbundled file system. We therefore check for such an example
3531 * before stamping the vfs_t with its creation time for the benefit of
3532 * mntfs.
3533 */
3534 if (vfsp->vfs_implp == NULL)
3535 vfsimpl_setup(vfsp);
3536 vfs_mono_time(&vfsp->vfs_hrctime);
3537
3538 /*
3539 * The zone that owns the mount is the one that performed the mount.
3540 * Note that this isn't necessarily the same as the zone mounted into.
3541 * The corresponding zone_rele_ref() will be done when the vfs_t
3542 * is being free'd.
3543 */
3544 vfsp->vfs_zone = curproc->p_zone;
3545 zone_init_ref(&vfsp->vfs_implp->vi_zone_ref);
3546 zone_hold_ref(vfsp->vfs_zone, &vfsp->vfs_implp->vi_zone_ref,
3547 ZONE_REF_VFS);
3548
3549 /*
3550 * Find the zone mounted into, and put this mount on its vfs list.
3551 */
3552 zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3553 ASSERT(zone != NULL);
3554 /*
3555 * Special casing for the root vfs. This structure is allocated
3556 * statically and hooked onto rootvfs at link time. During the
3557 * vfs_mountroot call at system startup time, the root file system's
3558 * VFS_MOUNTROOT routine will call vfs_add with this root vfs struct
3559 * as argument. The code below must detect and handle this special
3560 * case. The only apparent justification for this special casing is
3561 * to ensure that the root file system appears at the head of the
3562 * list.
3563 *
3564 * XXX: I'm assuming that it's ok to do normal list locking when
3565 * adding the entry for the root file system (this used to be
3566 * done with no locks held).
3567 */
3568 vfs_list_lock();
3569 /*
3570 * Link into the vfs list proper.
3571 */
3572 if (vfsp == &root) {
3573 /*
3574 * Assert: This vfs is already on the list as its first entry.
3575 * Thus, there's nothing to do.
3576 */
3577 ASSERT(rootvfs == vfsp);
3578 /*
3579 * Add it to the head of the global zone's vfslist.
3580 */
3581 ASSERT(zone == global_zone);
3582 ASSERT(zone->zone_vfslist == NULL);
3583 zone->zone_vfslist = vfsp;
3584 } else {
3585 /*
3586 * Link to end of list using vfs_prev (as rootvfs is now a
3587 * doubly linked circular list) so list is in mount order for
3588 * mnttab use.
3589 */
3590 rootvfs->vfs_prev->vfs_next = vfsp;
3591 vfsp->vfs_prev = rootvfs->vfs_prev;
3592 rootvfs->vfs_prev = vfsp;
3593 vfsp->vfs_next = rootvfs;
3594
3595 /*
3596 * Do it again for the zone-private list (which may be NULL).
3597 */
3598 if (zone->zone_vfslist == NULL) {
3599 ASSERT(zone != global_zone);
3600 zone->zone_vfslist = vfsp;
3601 } else {
3602 zone->zone_vfslist->vfs_zone_prev->vfs_zone_next = vfsp;
3603 vfsp->vfs_zone_prev = zone->zone_vfslist->vfs_zone_prev;
3604 zone->zone_vfslist->vfs_zone_prev = vfsp;
3605 vfsp->vfs_zone_next = zone->zone_vfslist;
3606 }
3607 }
3608
3609 /*
3610 * Link into the hash table, inserting it at the end, so that LOFS
3611 * with the same fsid as UFS (or other) file systems will not hide
3612 * the UFS.
3613 */
3614 vfs_hash_add(vfsp, 0);
3615
3616 /*
3617 * update the mnttab modification time
3618 */
3619 vfs_mnttab_modtimeupd();
3620 vfs_list_unlock();
3621 zone_rele(zone);
3622 }
3623
3624 void
3625 vfs_list_remove(struct vfs *vfsp)
3626 {
3627 zone_t *zone;
3628
3629 zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3630 ASSERT(zone != NULL);
3631 /*
3632 * Callers are responsible for preventing attempts to unmount the
3633 * root.
3634 */
3635 ASSERT(vfsp != rootvfs);
3636
3637 vfs_list_lock();
3638
3639 /*
3640 * Remove from hash.
3641 */
3642 vfs_hash_remove(vfsp);
3643
3644 /*
3645 * Remove from vfs list.
3646 */
3647 vfsp->vfs_prev->vfs_next = vfsp->vfs_next;
3648 vfsp->vfs_next->vfs_prev = vfsp->vfs_prev;
3649 vfsp->vfs_next = vfsp->vfs_prev = NULL;
3650
3651 /*
3652 * Remove from zone-specific vfs list.
3653 */
3654 if (zone->zone_vfslist == vfsp)
3655 zone->zone_vfslist = vfsp->vfs_zone_next;
3656
3657 if (vfsp->vfs_zone_next == vfsp) {
3658 ASSERT(vfsp->vfs_zone_prev == vfsp);
3659 ASSERT(zone->zone_vfslist == vfsp);
3660 zone->zone_vfslist = NULL;
3661 }
3662
3663 vfsp->vfs_zone_prev->vfs_zone_next = vfsp->vfs_zone_next;
3664 vfsp->vfs_zone_next->vfs_zone_prev = vfsp->vfs_zone_prev;
3665 vfsp->vfs_zone_next = vfsp->vfs_zone_prev = NULL;
3666
3667 /*
3668 * update the mnttab modification time
3669 */
3670 vfs_mnttab_modtimeupd();
3671 vfs_list_unlock();
3672 zone_rele(zone);
3673 }
3674
3675 struct vfs *
3676 getvfs(fsid_t *fsid)
3677 {
3678 struct vfs *vfsp;
3679 int val0 = fsid->val[0];
3680 int val1 = fsid->val[1];
3681 dev_t dev = expldev(val0);
3682 int vhno = VFSHASH(getmajor(dev), getminor(dev));
3683 kmutex_t *hmp = &rvfs_list[vhno].rvfs_lock;
3684
3685 mutex_enter(hmp);
3686 for (vfsp = rvfs_list[vhno].rvfs_head; vfsp; vfsp = vfsp->vfs_hash) {
3687 if (vfsp->vfs_fsid.val[0] == val0 &&
3688 vfsp->vfs_fsid.val[1] == val1) {
3689 VFS_HOLD(vfsp);
3690 mutex_exit(hmp);
3691 return (vfsp);
3692 }
3693 }
3694 mutex_exit(hmp);
3695 return (NULL);
3696 }
3697
3698 /*
3699 * Search the vfs mount in progress list for a specified device/vfs entry.
3700 * Returns 0 if the first entry in the list that the device matches has the
3701 * given vfs pointer as well. If the device matches but a different vfs
3702 * pointer is encountered in the list before the given vfs pointer then
3703 * a 1 is returned.
3704 */
3705
3706 int
3707 vfs_devmounting(dev_t dev, struct vfs *vfsp)
3708 {
3709 int retval = 0;
3710 struct ipmnt *mipp;
3711
3712 mutex_enter(&vfs_miplist_mutex);
3713 for (mipp = vfs_miplist; mipp != NULL; mipp = mipp->mip_next) {
3714 if (mipp->mip_dev == dev) {
3715 if (mipp->mip_vfsp != vfsp)
3716 retval = 1;
3717 break;
3718 }
3719 }
3720 mutex_exit(&vfs_miplist_mutex);
3721 return (retval);
3722 }
3723
3724 /*
3725 * Search the vfs list for a specified device. Returns 1, if entry is found
3726 * or 0 if no suitable entry is found.
3727 */
3728
3729 int
3730 vfs_devismounted(dev_t dev)
3731 {
3732 struct vfs *vfsp;
3733 int found;
3734
3735 vfs_list_read_lock();
3736 vfsp = rootvfs;
3737 found = 0;
3738 do {
3739 if (vfsp->vfs_dev == dev) {
3740 found = 1;
3741 break;
3742 }
3743 vfsp = vfsp->vfs_next;
3744 } while (vfsp != rootvfs);
3745
3746 vfs_list_unlock();
3747 return (found);
3748 }
3749
3750 /*
3751 * Search the vfs list for a specified device. Returns a pointer to it
3752 * or NULL if no suitable entry is found. The caller of this routine
3753 * is responsible for releasing the returned vfs pointer.
3754 */
3755 struct vfs *
3756 vfs_dev2vfsp(dev_t dev)
3757 {
3758 struct vfs *vfsp;
3759 int found;
3760
3761 vfs_list_read_lock();
3762 vfsp = rootvfs;
3763 found = 0;
3764 do {
3765 /*
3766 * The following could be made more efficient by making
3767 * the entire loop use vfs_zone_next if the call is from
3768 * a zone. The only callers, however, ustat(2) and
3769 * umount2(2), don't seem to justify the added
3770 * complexity at present.
3771 */
3772 if (vfsp->vfs_dev == dev &&
3773 ZONE_PATH_VISIBLE(refstr_value(vfsp->vfs_mntpt),
3774 curproc->p_zone)) {
3775 VFS_HOLD(vfsp);
3776 found = 1;
3777 break;
3778 }
3779 vfsp = vfsp->vfs_next;
3780 } while (vfsp != rootvfs);
3781 vfs_list_unlock();
3782 return (found ? vfsp: NULL);
3783 }
3784
3785 /*
3786 * Search the vfs list for a specified mntpoint. Returns a pointer to it
3787 * or NULL if no suitable entry is found. The caller of this routine
3788 * is responsible for releasing the returned vfs pointer.
3789 *
3790 * Note that if multiple mntpoints match, the last one matching is
3791 * returned in an attempt to return the "top" mount when overlay
3792 * mounts are covering the same mount point. This is accomplished by starting
3793 * at the end of the list and working our way backwards, stopping at the first
3794 * matching mount.
3795 */
3796 struct vfs *
3797 vfs_mntpoint2vfsp(const char *mp)
3798 {
3799 struct vfs *vfsp;
3800 struct vfs *retvfsp = NULL;
3801 zone_t *zone = curproc->p_zone;
3802 struct vfs *list;
3803
3804 vfs_list_read_lock();
3805 if (getzoneid() == GLOBAL_ZONEID) {
3806 /*
3807 * The global zone may see filesystems in any zone.
3808 */
3809 vfsp = rootvfs->vfs_prev;
3810 do {
3811 if (strcmp(refstr_value(vfsp->vfs_mntpt), mp) == 0) {
3812 retvfsp = vfsp;
3813 break;
3814 }
3815 vfsp = vfsp->vfs_prev;
3816 } while (vfsp != rootvfs->vfs_prev);
3817 } else if ((list = zone->zone_vfslist) != NULL) {
3818 const char *mntpt;
3819
3820 vfsp = list->vfs_zone_prev;
3821 do {
3822 mntpt = refstr_value(vfsp->vfs_mntpt);
3823 mntpt = ZONE_PATH_TRANSLATE(mntpt, zone);
3824 if (strcmp(mntpt, mp) == 0) {
3825 retvfsp = vfsp;
3826 break;
3827 }
3828 vfsp = vfsp->vfs_zone_prev;
3829 } while (vfsp != list->vfs_zone_prev);
3830 }
3831 if (retvfsp)
3832 VFS_HOLD(retvfsp);
3833 vfs_list_unlock();
3834 return (retvfsp);
3835 }
3836
3837 /*
3838 * Search the vfs list for a specified vfsops.
3839 * if vfs entry is found then return 1, else 0.
3840 */
3841 int
3842 vfs_opsinuse(vfsops_t *ops)
3843 {
3844 struct vfs *vfsp;
3845 int found;
3846
3847 vfs_list_read_lock();
3848 vfsp = rootvfs;
3849 found = 0;
3850 do {
3851 if (vfs_getops(vfsp) == ops) {
3852 found = 1;
3853 break;
3854 }
3855 vfsp = vfsp->vfs_next;
3856 } while (vfsp != rootvfs);
3857 vfs_list_unlock();
3858 return (found);
3859 }
3860
3861 /*
3862 * Allocate an entry in vfssw for a file system type
3863 */
3864 struct vfssw *
3865 allocate_vfssw(const char *type)
3866 {
3867 struct vfssw *vswp;
3868
3869 if (type[0] == '\0' || strlen(type) + 1 > _ST_FSTYPSZ) {
3870 /*
3871 * The vfssw table uses the empty string to identify an
3872 * available entry; we cannot add any type which has
3873 * a leading NUL. The string length is limited to
3874 * the size of the st_fstype array in struct stat.
3875 */
3876 return (NULL);
3877 }
3878
3879 ASSERT(VFSSW_WRITE_LOCKED());
3880 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++)
3881 if (!ALLOCATED_VFSSW(vswp)) {
3882 vswp->vsw_name = kmem_alloc(strlen(type) + 1, KM_SLEEP);
3883 (void) strcpy(vswp->vsw_name, type);
3884 ASSERT(vswp->vsw_count == 0);
3885 vswp->vsw_count = 1;
3886 mutex_init(&vswp->vsw_lock, NULL, MUTEX_DEFAULT, NULL);
3887 return (vswp);
3888 }
3889 return (NULL);
3890 }
3891
3892 /*
3893 * Impose additional layer of translation between vfstype names
3894 * and module names in the filesystem.
3895 */
3896 static const char *
3897 vfs_to_modname(const char *vfstype)
3898 {
3899 if (strcmp(vfstype, "proc") == 0) {
3900 vfstype = "procfs";
3901 } else if (strcmp(vfstype, "fd") == 0) {
3902 vfstype = "fdfs";
3903 } else if (strncmp(vfstype, "nfs", 3) == 0) {
3904 vfstype = "nfs";
3905 }
3906
3907 return (vfstype);
3908 }
3909
3910 /*
3911 * Find a vfssw entry given a file system type name.
3912 * Try to autoload the filesystem if it's not found.
3913 * If it's installed, return the vfssw locked to prevent unloading.
3914 */
3915 struct vfssw *
3916 vfs_getvfssw(const char *type)
3917 {
3918 struct vfssw *vswp;
3919 const char *modname;
3920
3921 RLOCK_VFSSW();
3922 vswp = vfs_getvfsswbyname(type);
3923 modname = vfs_to_modname(type);
3924
3925 if (rootdir == NULL) {
3926 /*
3927 * If we haven't yet loaded the root file system, then our
3928 * _init won't be called until later. Allocate vfssw entry,
3929 * because mod_installfs won't be called.
3930 */
3931 if (vswp == NULL) {
3932 RUNLOCK_VFSSW();
3933 WLOCK_VFSSW();
3934 if ((vswp = vfs_getvfsswbyname(type)) == NULL) {
3935 if ((vswp = allocate_vfssw(type)) == NULL) {
3936 WUNLOCK_VFSSW();
3937 return (NULL);
3938 }
3939 }
3940 WUNLOCK_VFSSW();
3941 RLOCK_VFSSW();
3942 }
3943 if (!VFS_INSTALLED(vswp)) {
3944 RUNLOCK_VFSSW();
3945 (void) modloadonly("fs", modname);
3946 } else
3947 RUNLOCK_VFSSW();
3948 return (vswp);
3949 }
3950
3951 /*
3952 * Try to load the filesystem. Before calling modload(), we drop
3953 * our lock on the VFS switch table, and pick it up after the
3954 * module is loaded. However, there is a potential race: the
3955 * module could be unloaded after the call to modload() completes
3956 * but before we pick up the lock and drive on. Therefore,
3957 * we keep reloading the module until we've loaded the module
3958 * _and_ we have the lock on the VFS switch table.
3959 */
3960 while (vswp == NULL || !VFS_INSTALLED(vswp)) {
3961 RUNLOCK_VFSSW();
3962 if (modload("fs", modname) == -1)
3963 return (NULL);
3964 RLOCK_VFSSW();
3965 if (vswp == NULL)
3966 if ((vswp = vfs_getvfsswbyname(type)) == NULL)
3967 break;
3968 }
3969 RUNLOCK_VFSSW();
3970
3971 return (vswp);
3972 }
3973
3974 /*
3975 * Find a vfssw entry given a file system type name.
3976 */
3977 struct vfssw *
3978 vfs_getvfsswbyname(const char *type)
3979 {
3980 struct vfssw *vswp;
3981
3982 ASSERT(VFSSW_LOCKED());
3983 if (type == NULL || *type == '\0')
3984 return (NULL);
3985
3986 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
3987 if (strcmp(type, vswp->vsw_name) == 0) {
3988 vfs_refvfssw(vswp);
3989 return (vswp);
3990 }
3991 }
3992
3993 return (NULL);
3994 }
3995
3996 /*
3997 * Find a vfssw entry given a set of vfsops.
3998 */
3999 struct vfssw *
4000 vfs_getvfsswbyvfsops(vfsops_t *vfsops)
4001 {
4002 struct vfssw *vswp;
4003
4004 RLOCK_VFSSW();
4005 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4006 if (ALLOCATED_VFSSW(vswp) && &vswp->vsw_vfsops == vfsops) {
4007 vfs_refvfssw(vswp);
4008 RUNLOCK_VFSSW();
4009 return (vswp);
4010 }
4011 }
4012 RUNLOCK_VFSSW();
4013
4014 return (NULL);
4015 }
4016
4017 /*
4018 * Reference a vfssw entry.
4019 */
4020 void
4021 vfs_refvfssw(struct vfssw *vswp)
4022 {
4023
4024 mutex_enter(&vswp->vsw_lock);
4025 vswp->vsw_count++;
4026 mutex_exit(&vswp->vsw_lock);
4027 }
4028
4029 /*
4030 * Unreference a vfssw entry.
4031 */
4032 void
4033 vfs_unrefvfssw(struct vfssw *vswp)
4034 {
4035
4036 mutex_enter(&vswp->vsw_lock);
4037 vswp->vsw_count--;
4038 mutex_exit(&vswp->vsw_lock);
4039 }
4040
4041 int sync_timeout = 30; /* timeout for syncing a page during panic */
4042 int sync_timeleft; /* portion of sync_timeout remaining */
4043
4044 static int sync_retries = 20; /* number of retries when not making progress */
4045 static int sync_triesleft; /* portion of sync_retries remaining */
4046
4047 static pgcnt_t old_pgcnt, new_pgcnt;
4048 static int new_bufcnt, old_bufcnt;
4049
4050 /*
4051 * Sync all of the mounted filesystems, and then wait for the actual i/o to
4052 * complete. We wait by counting the number of dirty pages and buffers,
4053 * pushing them out using bio_busy() and page_busy(), and then counting again.
4054 * This routine is used during both the uadmin A_SHUTDOWN code as well as
4055 * the SYNC phase of the panic code (see comments in panic.c). It should only
4056 * be used after some higher-level mechanism has quiesced the system so that
4057 * new writes are not being initiated while we are waiting for completion.
4058 *
4059 * To ensure finite running time, our algorithm uses two timeout mechanisms:
4060 * sync_timeleft (a timer implemented by the omnipresent deadman() cyclic), and
4061 * sync_triesleft (a progress counter used by the vfs_syncall() loop below).
4062 * Together these ensure that syncing completes if our i/o paths are stuck.
4063 * The counters are declared above so they can be found easily in the debugger.
4064 *
4065 * The sync_timeleft counter is reset by bio_busy() and page_busy() using the
4066 * vfs_syncprogress() subroutine whenever we make progress through the lists of
4067 * pages and buffers. It is decremented and expired by the deadman() cyclic.
4068 * When vfs_syncall() decides it is done, we disable the deadman() counter by
4069 * setting sync_timeleft to zero. This timer guards against vfs_syncall()
4070 * deadlocking or hanging inside of a broken filesystem or driver routine.
4071 *
4072 * The sync_triesleft counter is updated by vfs_syncall() itself. If we make
4073 * sync_retries consecutive calls to bio_busy() and page_busy() without
4074 * decreasing either the number of dirty buffers or dirty pages below the
4075 * lowest count we have seen so far, we give up and return from vfs_syncall().
4076 *
4077 * Each loop iteration ends with a call to delay() one second to allow time for
4078 * i/o completion and to permit the user time to read our progress messages.
4079 */
4080 void
4081 vfs_syncall(void)
4082 {
4083 if (rootdir == NULL && !modrootloaded)
4084 return; /* panic during boot - no filesystems yet */
4085
4086 printf("syncing file systems...");
4087 vfs_syncprogress();
4088 sync();
4089
4090 vfs_syncprogress();
4091 sync_triesleft = sync_retries;
4092
4093 old_bufcnt = new_bufcnt = INT_MAX;
4094 old_pgcnt = new_pgcnt = ULONG_MAX;
4095
4096 while (sync_triesleft > 0) {
4097 old_bufcnt = MIN(old_bufcnt, new_bufcnt);
4098 old_pgcnt = MIN(old_pgcnt, new_pgcnt);
4099
4100 new_bufcnt = bio_busy(B_TRUE);
4101 new_pgcnt = page_busy(B_TRUE);
4102 vfs_syncprogress();
4103
4104 if (new_bufcnt == 0 && new_pgcnt == 0)
4105 break;
4106
4107 if (new_bufcnt < old_bufcnt || new_pgcnt < old_pgcnt)
4108 sync_triesleft = sync_retries;
4109 else
4110 sync_triesleft--;
4111
4112 if (new_bufcnt)
4113 printf(" [%d]", new_bufcnt);
4114 if (new_pgcnt)
4115 printf(" %lu", new_pgcnt);
4116
4117 delay(hz);
4118 }
4119
4120 if (new_bufcnt != 0 || new_pgcnt != 0)
4121 printf(" done (not all i/o completed)\n");
4122 else
4123 printf(" done\n");
4124
4125 sync_timeleft = 0;
4126 delay(hz);
4127 }
4128
4129 /*
4130 * If we are in the middle of the sync phase of panic, reset sync_timeleft to
4131 * sync_timeout to indicate that we are making progress and the deadman()
4132 * omnipresent cyclic should not yet time us out. Note that it is safe to
4133 * store to sync_timeleft here since the deadman() is firing at high-level
4134 * on top of us. If we are racing with the deadman(), either the deadman()
4135 * will decrement the old value and then we will reset it, or we will
4136 * reset it and then the deadman() will immediately decrement it. In either
4137 * case, correct behavior results.
4138 */
4139 void
4140 vfs_syncprogress(void)
4141 {
4142 if (panicstr)
4143 sync_timeleft = sync_timeout;
4144 }
4145
4146 /*
4147 * Map VFS flags to statvfs flags. These shouldn't really be separate
4148 * flags at all.
4149 */
4150 uint_t
4151 vf_to_stf(uint_t vf)
4152 {
4153 uint_t stf = 0;
4154
4155 if (vf & VFS_RDONLY)
4156 stf |= ST_RDONLY;
4157 if (vf & VFS_NOSETUID)
4158 stf |= ST_NOSUID;
4159 if (vf & VFS_NOTRUNC)
4160 stf |= ST_NOTRUNC;
4161
4162 return (stf);
4163 }
4164
4165 /*
4166 * Entries for (illegal) fstype 0.
4167 */
4168 /* ARGSUSED */
4169 int
4170 vfsstray_sync(struct vfs *vfsp, short arg, struct cred *cr)
4171 {
4172 cmn_err(CE_PANIC, "stray vfs operation");
4173 return (0);
4174 }
4175
4176 /*
4177 * Entries for (illegal) fstype 0.
4178 */
4179 int
4180 vfsstray(void)
4181 {
4182 cmn_err(CE_PANIC, "stray vfs operation");
4183 return (0);
4184 }
4185
4186 /*
4187 * Support for dealing with forced UFS unmount and its interaction with
4188 * LOFS. Could be used by any filesystem.
4189 * See bug 1203132.
4190 */
4191 int
4192 vfs_EIO(void)
4193 {
4194 return (EIO);
4195 }
4196
4197 /*
4198 * We've gotta define the op for sync separately, since the compiler gets
4199 * confused if we mix and match ANSI and normal style prototypes when
4200 * a "short" argument is present and spits out a warning.
4201 */
4202 /*ARGSUSED*/
4203 int
4204 vfs_EIO_sync(struct vfs *vfsp, short arg, struct cred *cr)
4205 {
4206 return (EIO);
4207 }
4208
4209 vfs_t EIO_vfs;
4210 vfsops_t *EIO_vfsops;
4211
4212 /*
4213 * Called from startup() to initialize all loaded vfs's
4214 */
4215 void
4216 vfsinit(void)
4217 {
4218 struct vfssw *vswp;
4219 int error;
4220 extern int vopstats_enabled;
4221 extern void vopstats_startup();
4222
4223 static const fs_operation_def_t EIO_vfsops_template[] = {
4224 VFSNAME_MOUNT, { .error = vfs_EIO },
4225 VFSNAME_UNMOUNT, { .error = vfs_EIO },
4226 VFSNAME_ROOT, { .error = vfs_EIO },
4227 VFSNAME_STATVFS, { .error = vfs_EIO },
4228 VFSNAME_SYNC, { .vfs_sync = vfs_EIO_sync },
4229 VFSNAME_VGET, { .error = vfs_EIO },
4230 VFSNAME_MOUNTROOT, { .error = vfs_EIO },
4231 VFSNAME_FREEVFS, { .error = vfs_EIO },
4232 VFSNAME_VNSTATE, { .error = vfs_EIO },
4233 NULL, NULL
4234 };
4235
4236 static const fs_operation_def_t stray_vfsops_template[] = {
4237 VFSNAME_MOUNT, { .error = vfsstray },
4238 VFSNAME_UNMOUNT, { .error = vfsstray },
4239 VFSNAME_ROOT, { .error = vfsstray },
4240 VFSNAME_STATVFS, { .error = vfsstray },
4241 VFSNAME_SYNC, { .vfs_sync = vfsstray_sync },
4242 VFSNAME_VGET, { .error = vfsstray },
4243 VFSNAME_MOUNTROOT, { .error = vfsstray },
4244 VFSNAME_FREEVFS, { .error = vfsstray },
4245 VFSNAME_VNSTATE, { .error = vfsstray },
4246 NULL, NULL
4247 };
4248
4249 /* Create vfs cache */
4250 vfs_cache = kmem_cache_create("vfs_cache", sizeof (struct vfs),
4251 sizeof (uintptr_t), NULL, NULL, NULL, NULL, NULL, 0);
4252
4253 /* Initialize the vnode cache (file systems may use it during init). */
4254 vn_create_cache();
4255
4256 /* Setup event monitor framework */
4257 fem_init();
4258
4259 /* Initialize the dummy stray file system type. */
4260 error = vfs_setfsops(0, stray_vfsops_template, NULL);
4261
4262 /* Initialize the dummy EIO file system. */
4263 error = vfs_makefsops(EIO_vfsops_template, &EIO_vfsops);
4264 if (error != 0) {
4265 cmn_err(CE_WARN, "vfsinit: bad EIO vfs ops template");
4266 /* Shouldn't happen, but not bad enough to panic */
4267 }
4268
4269 VFS_INIT(&EIO_vfs, EIO_vfsops, (caddr_t)NULL);
4270
4271 /*
4272 * Default EIO_vfs.vfs_flag to VFS_UNMOUNTED so a lookup
4273 * on this vfs can immediately notice it's invalid.
4274 */
4275 EIO_vfs.vfs_flag |= VFS_UNMOUNTED;
4276
4277 /*
4278 * Call the init routines of non-loadable filesystems only.
4279 * Filesystems which are loaded as separate modules will be
4280 * initialized by the module loading code instead.
4281 */
4282
4283 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4284 RLOCK_VFSSW();
4285 if (vswp->vsw_init != NULL)
4286 (*vswp->vsw_init)(vswp - vfssw, vswp->vsw_name);
4287 RUNLOCK_VFSSW();
4288 }
4289
4290 vopstats_startup();
4291
4292 if (vopstats_enabled) {
4293 /* EIO_vfs can collect stats, but we don't retrieve them */
4294 initialize_vopstats(&EIO_vfs.vfs_vopstats);
4295 EIO_vfs.vfs_fstypevsp = NULL;
4296 EIO_vfs.vfs_vskap = NULL;
4297 EIO_vfs.vfs_flag |= VFS_STATS;
4298 }
4299
4300 xattr_init();
4301
4302 reparse_point_init();
4303 }
4304
4305 vfs_t *
4306 vfs_alloc(int kmflag)
4307 {
4308 vfs_t *vfsp;
4309
4310 vfsp = kmem_cache_alloc(vfs_cache, kmflag);
4311
4312 /*
4313 * Do the simplest initialization here.
4314 * Everything else gets done in vfs_init()
4315 */
4316 bzero(vfsp, sizeof (vfs_t));
4317 return (vfsp);
4318 }
4319
4320 void
4321 vfs_free(vfs_t *vfsp)
4322 {
4323 /*
4324 * One would be tempted to assert that "vfsp->vfs_count == 0".
4325 * The problem is that this gets called out of domount() with
4326 * a partially initialized vfs and a vfs_count of 1. This is
4327 * also called from vfs_rele() with a vfs_count of 0. We can't
4328 * call VFS_RELE() from domount() if VFS_MOUNT() hasn't successfully
4329 * returned. This is because VFS_MOUNT() fully initializes the
4330 * vfs structure and its associated data. VFS_RELE() will call
4331 * VFS_FREEVFS() which may panic the system if the data structures
4332 * aren't fully initialized from a successful VFS_MOUNT()).
4333 */
4334
4335 /* If FEM was in use, make sure everything gets cleaned up */
4336 if (vfsp->vfs_femhead) {
4337 ASSERT(vfsp->vfs_femhead->femh_list == NULL);
4338 mutex_destroy(&vfsp->vfs_femhead->femh_lock);
4339 kmem_free(vfsp->vfs_femhead, sizeof (*(vfsp->vfs_femhead)));
4340 vfsp->vfs_femhead = NULL;
4341 }
4342
4343 if (vfsp->vfs_implp)
4344 vfsimpl_teardown(vfsp);
4345 sema_destroy(&vfsp->vfs_reflock);
4346 kmem_cache_free(vfs_cache, vfsp);
4347 }
4348
4349 /*
4350 * Increments the vfs reference count by one atomically.
4351 */
4352 void
4353 vfs_hold(vfs_t *vfsp)
4354 {
4355 atomic_inc_32(&vfsp->vfs_count);
4356 ASSERT(vfsp->vfs_count != 0);
4357 }
4358
4359 /*
4360 * Decrements the vfs reference count by one atomically. When
4361 * vfs reference count becomes zero, it calls the file system
4362 * specific vfs_freevfs() to free up the resources.
4363 */
4364 void
4365 vfs_rele(vfs_t *vfsp)
4366 {
4367 ASSERT(vfsp->vfs_count != 0);
4368 if (atomic_dec_32_nv(&vfsp->vfs_count) == 0) {
4369 VFS_FREEVFS(vfsp);
4370 lofi_remove(vfsp);
4371 if (vfsp->vfs_zone)
4372 zone_rele_ref(&vfsp->vfs_implp->vi_zone_ref,
4373 ZONE_REF_VFS);
4374 vfs_freemnttab(vfsp);
4375 vfs_free(vfsp);
4376 }
4377 }
4378
4379 /*
4380 * Generic operations vector support.
4381 *
4382 * This is used to build operations vectors for both the vfs and vnode.
4383 * It's normally called only when a file system is loaded.
4384 *
4385 * There are many possible algorithms for this, including the following:
4386 *
4387 * (1) scan the list of known operations; for each, see if the file system
4388 * includes an entry for it, and fill it in as appropriate.
4389 *
4390 * (2) set up defaults for all known operations. scan the list of ops
4391 * supplied by the file system; for each which is both supplied and
4392 * known, fill it in.
4393 *
4394 * (3) sort the lists of known ops & supplied ops; scan the list, filling
4395 * in entries as we go.
4396 *
4397 * we choose (1) for simplicity, and because performance isn't critical here.
4398 * note that (2) could be sped up using a precomputed hash table on known ops.
4399 * (3) could be faster than either, but only if the lists were very large or
4400 * supplied in sorted order.
4401 *
4402 */
4403
4404 int
4405 fs_build_vector(void *vector, int *unused_ops,
4406 const fs_operation_trans_def_t *translation,
4407 const fs_operation_def_t *operations)
4408 {
4409 int i, num_trans, num_ops, used;
4410
4411 /*
4412 * Count the number of translations and the number of supplied
4413 * operations.
4414 */
4415
4416 {
4417 const fs_operation_trans_def_t *p;
4418
4419 for (num_trans = 0, p = translation;
4420 p->name != NULL;
4421 num_trans++, p++)
4422 ;
4423 }
4424
4425 {
4426 const fs_operation_def_t *p;
4427
4428 for (num_ops = 0, p = operations;
4429 p->name != NULL;
4430 num_ops++, p++)
4431 ;
4432 }
4433
4434 /* Walk through each operation known to our caller. There will be */
4435 /* one entry in the supplied "translation table" for each. */
4436
4437 used = 0;
4438
4439 for (i = 0; i < num_trans; i++) {
4440 int j, found;
4441 char *curname;
4442 fs_generic_func_p result;
4443 fs_generic_func_p *location;
4444
4445 curname = translation[i].name;
4446
4447 /* Look for a matching operation in the list supplied by the */
4448 /* file system. */
4449
4450 found = 0;
4451
4452 for (j = 0; j < num_ops; j++) {
4453 if (strcmp(operations[j].name, curname) == 0) {
4454 used++;
4455 found = 1;
4456 break;
4457 }
4458 }
4459
4460 /*
4461 * If the file system is using a "placeholder" for default
4462 * or error functions, grab the appropriate function out of
4463 * the translation table. If the file system didn't supply
4464 * this operation at all, use the default function.
4465 */
4466
4467 if (found) {
4468 result = operations[j].func.fs_generic;
4469 if (result == fs_default) {
4470 result = translation[i].defaultFunc;
4471 } else if (result == fs_error) {
4472 result = translation[i].errorFunc;
4473 } else if (result == NULL) {
4474 /* Null values are PROHIBITED */
4475 return (EINVAL);
4476 }
4477 } else {
4478 result = translation[i].defaultFunc;
4479 }
4480
4481 /* Now store the function into the operations vector. */
4482
4483 location = (fs_generic_func_p *)
4484 (((char *)vector) + translation[i].offset);
4485
4486 *location = result;
4487 }
4488
4489 *unused_ops = num_ops - used;
4490
4491 return (0);
4492 }
4493
4494 /* Placeholder functions, should never be called. */
4495
4496 int
4497 fs_error(void)
4498 {
4499 cmn_err(CE_PANIC, "fs_error called");
4500 return (0);
4501 }
4502
4503 int
4504 fs_default(void)
4505 {
4506 cmn_err(CE_PANIC, "fs_default called");
4507 return (0);
4508 }
4509
4510 #ifdef __sparc
4511
4512 /*
4513 * Part of the implementation of booting off a mirrored root
4514 * involves a change of dev_t for the root device. To
4515 * accomplish this, first remove the existing hash table
4516 * entry for the root device, convert to the new dev_t,
4517 * then re-insert in the hash table at the head of the list.
4518 */
4519 void
4520 vfs_root_redev(vfs_t *vfsp, dev_t ndev, int fstype)
4521 {
4522 vfs_list_lock();
4523
4524 vfs_hash_remove(vfsp);
4525
4526 vfsp->vfs_dev = ndev;
4527 vfs_make_fsid(&vfsp->vfs_fsid, ndev, fstype);
4528
4529 vfs_hash_add(vfsp, 1);
4530
4531 vfs_list_unlock();
4532 }
4533
4534 #else /* x86 NEWBOOT */
4535
4536 #if defined(__x86)
4537 extern int hvmboot_rootconf();
4538 #endif /* __x86 */
4539
4540 extern ib_boot_prop_t *iscsiboot_prop;
4541
4542 int
4543 rootconf()
4544 {
4545 int error;
4546 struct vfssw *vsw;
4547 extern void pm_init();
4548 char *fstyp, *fsmod;
4549 int ret = -1;
4550
4551 getrootfs(&fstyp, &fsmod);
4552
4553 #if defined(__x86)
4554 /*
4555 * hvmboot_rootconf() is defined in the hvm_bootstrap misc module,
4556 * which lives in /platform/i86hvm, and hence is only available when
4557 * booted in an x86 hvm environment. If the hvm_bootstrap misc module
4558 * is not available then the modstub for this function will return 0.
4559 * If the hvm_bootstrap misc module is available it will be loaded
4560 * and hvmboot_rootconf() will be invoked.
4561 */
4562 if (error = hvmboot_rootconf())
4563 return (error);
4564 #endif /* __x86 */
4565
4566 if (error = clboot_rootconf())
4567 return (error);
4568
4569 if (modload("fs", fsmod) == -1)
4570 panic("Cannot _init %s module", fsmod);
4571
4572 RLOCK_VFSSW();
4573 vsw = vfs_getvfsswbyname(fstyp);
4574 RUNLOCK_VFSSW();
4575 if (vsw == NULL) {
4576 cmn_err(CE_CONT, "Cannot find %s filesystem\n", fstyp);
4577 return (ENXIO);
4578 }
4579 VFS_INIT(rootvfs, &vsw->vsw_vfsops, 0);
4580 VFS_HOLD(rootvfs);
4581
4582 /* always mount readonly first */
4583 rootvfs->vfs_flag |= VFS_RDONLY;
4584
4585 pm_init();
4586
4587 if (netboot && iscsiboot_prop) {
4588 cmn_err(CE_WARN, "NFS boot and iSCSI boot"
4589 " shouldn't happen in the same time");
4590 return (EINVAL);
4591 }
4592
4593 if (netboot || iscsiboot_prop) {
4594 ret = strplumb();
4595 if (ret != 0) {
4596 cmn_err(CE_WARN, "Cannot plumb network device %d", ret);
4597 return (EFAULT);
4598 }
4599 }
4600
4601 if ((ret == 0) && iscsiboot_prop) {
4602 ret = modload("drv", "iscsi");
4603 /* -1 indicates fail */
4604 if (ret == -1) {
4605 cmn_err(CE_WARN, "Failed to load iscsi module");
4606 iscsi_boot_prop_free();
4607 return (EINVAL);
4608 } else {
4609 if (!i_ddi_attach_pseudo_node("iscsi")) {
4610 cmn_err(CE_WARN,
4611 "Failed to attach iscsi driver");
4612 iscsi_boot_prop_free();
4613 return (ENODEV);
4614 }
4615 }
4616 }
4617
4618 error = VFS_MOUNTROOT(rootvfs, ROOT_INIT);
4619 vfs_unrefvfssw(vsw);
4620 rootdev = rootvfs->vfs_dev;
4621
4622 if (error)
4623 cmn_err(CE_CONT, "Cannot mount root on %s fstype %s\n",
4624 rootfs.bo_name, fstyp);
4625 else
4626 cmn_err(CE_CONT, "?root on %s fstype %s\n",
4627 rootfs.bo_name, fstyp);
4628 return (error);
4629 }
4630
4631 /*
4632 * XXX this is called by nfs only and should probably be removed
4633 * If booted with ASKNAME, prompt on the console for a filesystem
4634 * name and return it.
4635 */
4636 void
4637 getfsname(char *askfor, char *name, size_t namelen)
4638 {
4639 if (boothowto & RB_ASKNAME) {
4640 printf("%s name: ", askfor);
4641 console_gets(name, namelen);
4642 }
4643 }
4644
4645 /*
4646 * Init the root filesystem type (rootfs.bo_fstype) from the "fstype"
4647 * property.
4648 *
4649 * Filesystem types starting with the prefix "nfs" are diskless clients;
4650 * init the root filename name (rootfs.bo_name), too.
4651 *
4652 * If we are booting via NFS we currently have these options:
4653 * nfs - dynamically choose NFS V2, V3, or V4 (default)
4654 * nfs2 - force NFS V2
4655 * nfs3 - force NFS V3
4656 * nfs4 - force NFS V4
4657 * Because we need to maintain backward compatibility with the naming
4658 * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
4659 * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs". The dynamic
4660 * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
4661 * This is only for root filesystems, all other uses will expect
4662 * that "nfs" == NFS V2.
4663 */
4664 static void
4665 getrootfs(char **fstypp, char **fsmodp)
4666 {
4667 extern char *strplumb_get_netdev_path(void);
4668 char *propstr = NULL;
4669
4670 /*
4671 * Check fstype property; for diskless it should be one of "nfs",
4672 * "nfs2", "nfs3" or "nfs4".
4673 */
4674 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4675 DDI_PROP_DONTPASS, "fstype", &propstr)
4676 == DDI_SUCCESS) {
4677 (void) strncpy(rootfs.bo_fstype, propstr, BO_MAXFSNAME);
4678 ddi_prop_free(propstr);
4679
4680 /*
4681 * if the boot property 'fstype' is not set, but 'zfs-bootfs' is set,
4682 * assume the type of this root filesystem is 'zfs'.
4683 */
4684 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4685 DDI_PROP_DONTPASS, "zfs-bootfs", &propstr)
4686 == DDI_SUCCESS) {
4687 (void) strncpy(rootfs.bo_fstype, "zfs", BO_MAXFSNAME);
4688 ddi_prop_free(propstr);
4689 }
4690
4691 if (strncmp(rootfs.bo_fstype, "nfs", 3) != 0) {
4692 *fstypp = *fsmodp = rootfs.bo_fstype;
4693 return;
4694 }
4695
4696 ++netboot;
4697
4698 if (strcmp(rootfs.bo_fstype, "nfs2") == 0)
4699 (void) strcpy(rootfs.bo_fstype, "nfs");
4700 else if (strcmp(rootfs.bo_fstype, "nfs") == 0)
4701 (void) strcpy(rootfs.bo_fstype, "nfsdyn");
4702
4703 /*
4704 * check if path to network interface is specified in bootpath
4705 * or by a hypervisor domain configuration file.
4706 * XXPV - enable strlumb_get_netdev_path()
4707 */
4708 if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), DDI_PROP_DONTPASS,
4709 "xpv-nfsroot")) {
4710 (void) strcpy(rootfs.bo_name, "/xpvd/xnf@0");
4711 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4712 DDI_PROP_DONTPASS, "bootpath", &propstr)
4713 == DDI_SUCCESS) {
4714 (void) strncpy(rootfs.bo_name, propstr, BO_MAXOBJNAME);
4715 ddi_prop_free(propstr);
4716 } else {
4717 /* attempt to determine netdev_path via boot_mac address */
4718 netdev_path = strplumb_get_netdev_path();
4719 if (netdev_path == NULL)
4720 panic("cannot find boot network interface");
4721 (void) strncpy(rootfs.bo_name, netdev_path, BO_MAXOBJNAME);
4722 }
4723 *fstypp = rootfs.bo_fstype;
4724 *fsmodp = "nfs";
4725 }
4726 #endif
4727
4728 /*
4729 * VFS feature routines
4730 */
4731
4732 #define VFTINDEX(feature) (((feature) >> 32) & 0xFFFFFFFF)
4733 #define VFTBITS(feature) ((feature) & 0xFFFFFFFFLL)
4734
4735 /* Register a feature in the vfs */
4736 void
4737 vfs_set_feature(vfs_t *vfsp, vfs_feature_t feature)
4738 {
4739 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4740 if (vfsp->vfs_implp == NULL)
4741 return;
4742
4743 vfsp->vfs_featureset[VFTINDEX(feature)] |= VFTBITS(feature);
4744 }
4745
4746 void
4747 vfs_clear_feature(vfs_t *vfsp, vfs_feature_t feature)
4748 {
4749 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4750 if (vfsp->vfs_implp == NULL)
4751 return;
4752 vfsp->vfs_featureset[VFTINDEX(feature)] &= VFTBITS(~feature);
4753 }
4754
4755 /*
4756 * Query a vfs for a feature.
4757 * Returns 1 if feature is present, 0 if not
4758 */
4759 int
4760 vfs_has_feature(vfs_t *vfsp, vfs_feature_t feature)
4761 {
4762 int ret = 0;
4763
4764 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4765 if (vfsp->vfs_implp == NULL)
4766 return (ret);
4767
4768 if (vfsp->vfs_featureset[VFTINDEX(feature)] & VFTBITS(feature))
4769 ret = 1;
4770
4771 return (ret);
4772 }
4773
4774 /*
4775 * Propagate feature set from one vfs to another
4776 */
4777 void
4778 vfs_propagate_features(vfs_t *from, vfs_t *to)
4779 {
4780 int i;
4781
4782 if (to->vfs_implp == NULL || from->vfs_implp == NULL)
4783 return;
4784
4785 for (i = 1; i <= to->vfs_featureset[0]; i++) {
4786 to->vfs_featureset[i] = from->vfs_featureset[i];
4787 }
4788 }
4789
4790 #define LOFINODE_PATH "/dev/lofi/%d"
4791
4792 /*
4793 * Return the vnode for the lofi node if there's a lofi mount in place.
4794 * Returns -1 when there's no lofi node, 0 on success, and > 0 on
4795 * failure.
4796 */
4797 int
4798 vfs_get_lofi(vfs_t *vfsp, vnode_t **vpp)
4799 {
4800 char *path = NULL;
4801 int strsize;
4802 int err;
4803
4804 if (vfsp->vfs_lofi_id == 0) {
4805 *vpp = NULL;
4806 return (-1);
4807 }
4808
4809 strsize = snprintf(NULL, 0, LOFINODE_PATH, vfsp->vfs_lofi_id);
4810 path = kmem_alloc(strsize + 1, KM_SLEEP);
4811 (void) snprintf(path, strsize + 1, LOFINODE_PATH, vfsp->vfs_lofi_id);
4812
4813 /*
4814 * We may be inside a zone, so we need to use the /dev path, but
4815 * it's created asynchronously, so we wait here.
4816 */
4817 for (;;) {
4818 err = lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, vpp);
4819
4820 if (err != ENOENT)
4821 break;
4822
4823 if ((err = delay_sig(hz / 8)) == EINTR)
4824 break;
4825 }
4826
4827 if (err)
4828 *vpp = NULL;
4829
4830 kmem_free(path, strsize + 1);
4831 return (err);
4832 }