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