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