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 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*      Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
  27 /*        All Rights Reserved   */
  28 
  29 /*
  30  * University Copyright- Copyright (c) 1982, 1986, 1988
  31  * The Regents of the University of California
  32  * All Rights Reserved
  33  *
  34  * University Acknowledgment- Portions of this document are derived from
  35  * software developed by the University of California, Berkeley, and its
  36  * contributors.
  37  */
  38 
  39 
  40 #pragma ident   "@(#)ufs_vfsops.c       2.276   08/05/07 SMI"
  41 
  42 #include <sys/types.h>
  43 #include <sys/t_lock.h>
  44 #include <sys/param.h>
  45 #include <sys/systm.h>
  46 #include <sys/bitmap.h>
  47 #include <sys/sysmacros.h>
  48 #include <sys/kmem.h>
  49 #include <sys/signal.h>
  50 #include <sys/user.h>
  51 #include <sys/proc.h>
  52 #include <sys/disp.h>
  53 #include <sys/buf.h>
  54 #include <sys/pathname.h>
  55 #include <sys/vfs.h>
  56 #include <sys/vfs_opreg.h>
  57 #include <sys/vnode.h>
  58 #include <sys/file.h>
  59 #include <sys/atomic.h>
  60 #include <sys/uio.h>
  61 #include <sys/dkio.h>
  62 #include <sys/cred.h>
  63 #include <sys/conf.h>
  64 #include <sys/dnlc.h>
  65 #include <sys/kstat.h>
  66 #include <sys/acl.h>
  67 #include <sys/fs/ufs_fsdir.h>
  68 #include <sys/fs/ufs_fs.h>
  69 #include <sys/fs/ufs_inode.h>
  70 #include <sys/fs/ufs_mount.h>
  71 #include <sys/fs/ufs_acl.h>
  72 #include <sys/fs/ufs_panic.h>
  73 #include <sys/fs/ufs_bio.h>
  74 #include <sys/fs/ufs_quota.h>
  75 #include <sys/fs/ufs_log.h>
  76 #undef NFS
  77 #include <sys/statvfs.h>
  78 #include <sys/mount.h>
  79 #include <sys/mntent.h>
  80 #include <sys/swap.h>
  81 #include <sys/errno.h>
  82 #include <sys/debug.h>
  83 #include "fs/fs_subr.h"
  84 #include <sys/cmn_err.h>
  85 #include <sys/dnlc.h>
  86 #include <sys/fssnap_if.h>
  87 #include <sys/sunddi.h>
  88 #include <sys/bootconf.h>
  89 #include <sys/policy.h>
  90 #include <sys/zone.h>
  91 
  92 /*
  93  * This is the loadable module wrapper.
  94  */
  95 #include <sys/modctl.h>
  96 
  97 int                     ufsfstype;
  98 vfsops_t                *ufs_vfsops;
  99 static int              ufsinit(int, char *);
 100 static int              mountfs();
 101 extern int              highbit();
 102 extern struct instats   ins;
 103 extern struct vnode *common_specvp(struct vnode *vp);
 104 extern vfs_t            EIO_vfs;
 105 
 106 struct  dquot *dquot, *dquotNDQUOT;
 107 
 108 /*
 109  * Cylinder group summary information handling tunable.
 110  * This defines when these deltas get logged.
 111  * If the number of cylinders in the file system is over the
 112  * tunable then we log csum updates. Otherwise the updates are only
 113  * done for performance on unmount. After a panic they can be
 114  * quickly constructed during mounting. See ufs_construct_si()
 115  * called from ufs_getsummaryinfo().
 116  *
 117  * This performance feature can of course be disabled by setting
 118  * ufs_ncg_log to 0, and fully enabled by setting it to 0xffffffff.
 119  */
 120 #define UFS_LOG_NCG_DEFAULT 10000
 121 uint32_t ufs_ncg_log = UFS_LOG_NCG_DEFAULT;
 122 
 123 /*
 124  * ufs_clean_root indicates whether the root fs went down cleanly
 125  */
 126 static int ufs_clean_root = 0;
 127 
 128 /*
 129  * UFS Mount options table
 130  */
 131 static char *intr_cancel[] = { MNTOPT_NOINTR, NULL };
 132 static char *nointr_cancel[] = { MNTOPT_INTR, NULL };
 133 static char *forcedirectio_cancel[] = { MNTOPT_NOFORCEDIRECTIO, NULL };
 134 static char *noforcedirectio_cancel[] = { MNTOPT_FORCEDIRECTIO, NULL };
 135 static char *largefiles_cancel[] = { MNTOPT_NOLARGEFILES, NULL };
 136 static char *nolargefiles_cancel[] = { MNTOPT_LARGEFILES, NULL };
 137 static char *logging_cancel[] = { MNTOPT_NOLOGGING, NULL };
 138 static char *nologging_cancel[] = { MNTOPT_LOGGING, NULL };
 139 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
 140 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
 141 static char *quota_cancel[] = { MNTOPT_NOQUOTA, NULL };
 142 static char *noquota_cancel[] = { MNTOPT_QUOTA, NULL };
 143 static char *dfratime_cancel[] = { MNTOPT_NODFRATIME, NULL };
 144 static char *nodfratime_cancel[] = { MNTOPT_DFRATIME, NULL };
 145 
 146 static mntopt_t mntopts[] = {
 147 /*
 148  *      option name             cancel option   default arg     flags
 149  *              ufs arg flag
 150  */
 151         { MNTOPT_INTR,          intr_cancel,    NULL,           MO_DEFAULT,
 152                 (void *)0 },
 153         { MNTOPT_NOINTR,        nointr_cancel,  NULL,           0,
 154                 (void *)UFSMNT_NOINTR },
 155         { MNTOPT_SYNCDIR,       NULL,           NULL,           0,
 156                 (void *)UFSMNT_SYNCDIR },
 157         { MNTOPT_FORCEDIRECTIO, forcedirectio_cancel, NULL,     0,
 158                 (void *)UFSMNT_FORCEDIRECTIO },
 159         { MNTOPT_NOFORCEDIRECTIO, noforcedirectio_cancel, NULL, 0,
 160                 (void *)UFSMNT_NOFORCEDIRECTIO },
 161         { MNTOPT_NOSETSEC,      NULL,           NULL,           0,
 162                 (void *)UFSMNT_NOSETSEC },
 163         { MNTOPT_LARGEFILES,    largefiles_cancel, NULL,        MO_DEFAULT,
 164                 (void *)UFSMNT_LARGEFILES },
 165         { MNTOPT_NOLARGEFILES,  nolargefiles_cancel, NULL,      0,
 166                 (void *)0 },
 167         { MNTOPT_LOGGING,       logging_cancel, NULL,           MO_TAG,
 168                 (void *)UFSMNT_LOGGING },
 169         { MNTOPT_NOLOGGING,     nologging_cancel, NULL,
 170                 MO_NODISPLAY|MO_DEFAULT|MO_TAG, (void *)0 },
 171         { MNTOPT_QUOTA,         quota_cancel, NULL,             MO_IGNORE,
 172                 (void *)0 },
 173         { MNTOPT_NOQUOTA,       noquota_cancel, NULL,
 174                 MO_NODISPLAY|MO_DEFAULT, (void *)0 },
 175         { MNTOPT_GLOBAL,        NULL,           NULL,           0,
 176                 (void *)0 },
 177         { MNTOPT_XATTR, xattr_cancel,           NULL,           MO_DEFAULT,
 178                 (void *)0 },
 179         { MNTOPT_NOXATTR,       noxattr_cancel,         NULL,           0,
 180                 (void *)0 },
 181         { MNTOPT_NOATIME,       NULL,           NULL,           0,
 182                 (void *)UFSMNT_NOATIME },
 183         { MNTOPT_DFRATIME,      dfratime_cancel, NULL,          0,
 184                 (void *)0 },
 185         { MNTOPT_NODFRATIME,    nodfratime_cancel, NULL,
 186                 MO_NODISPLAY|MO_DEFAULT, (void *)UFSMNT_NODFRATIME },
 187         { MNTOPT_ONERROR,       NULL,           UFSMNT_ONERROR_PANIC_STR,
 188                 MO_DEFAULT|MO_HASVALUE, (void *)0 },
 189 };
 190 
 191 static mntopts_t ufs_mntopts = {
 192         sizeof (mntopts) / sizeof (mntopt_t),
 193         mntopts
 194 };
 195 
 196 static vfsdef_t vfw = {
 197         VFSDEF_VERSION,
 198         "ufs",
 199         ufsinit,
 200         VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS,
 201         &ufs_mntopts
 202 };
 203 
 204 /*
 205  * Module linkage information for the kernel.
 206  */
 207 extern struct mod_ops mod_fsops;
 208 
 209 static struct modlfs modlfs = {
 210         &mod_fsops, "filesystem for ufs", &vfw
 211 };
 212 
 213 static struct modlinkage modlinkage = {
 214         MODREV_1, (void *)&modlfs, NULL
 215 };
 216 
 217 /*
 218  * An attempt has been made to make this module unloadable.  In order to
 219  * test it, we need a system in which the root fs is NOT ufs.  THIS HAS NOT
 220  * BEEN DONE
 221  */
 222 
 223 extern kstat_t *ufs_inode_kstat;
 224 extern uint_t ufs_lockfs_key;
 225 extern void ufs_lockfs_tsd_destructor(void *);
 226 extern uint_t bypass_snapshot_throttle_key;
 227 
 228 int
 229 _init(void)
 230 {
 231         /*
 232          * Create an index into the per thread array so that any thread doing
 233          * VOP will have a lockfs mark on it.
 234          */
 235         tsd_create(&ufs_lockfs_key, ufs_lockfs_tsd_destructor);
 236         tsd_create(&bypass_snapshot_throttle_key, NULL);
 237         return (mod_install(&modlinkage));
 238 }
 239 
 240 int
 241 _fini(void)
 242 {
 243         return (EBUSY);
 244 }
 245 
 246 int
 247 _info(struct modinfo *modinfop)
 248 {
 249         return (mod_info(&modlinkage, modinfop));
 250 }
 251 
 252 extern struct vnode *makespecvp(dev_t dev, vtype_t type);
 253 
 254 extern kmutex_t ufs_scan_lock;
 255 
 256 static int mountfs(struct vfs *, enum whymountroot, struct vnode *, char *,
 257                 struct cred *, int, void *, int);
 258 
 259 
 260 static int
 261 ufs_mount(struct vfs *vfsp, struct vnode *mvp, struct mounta *uap,
 262         struct cred *cr)
 263 
 264 {
 265         char *data = uap->dataptr;
 266         int datalen = uap->datalen;
 267         dev_t dev;
 268         struct vnode *bvp;
 269         struct vnode *lvp = NULL;
 270         struct vnode *svp = NULL;
 271         struct pathname dpn;
 272         int error;
 273         enum whymountroot why = ROOT_INIT;
 274         struct ufs_args args;
 275         int oflag, aflag;
 276         int fromspace = (uap->flags & MS_SYSSPACE) ?
 277             UIO_SYSSPACE : UIO_USERSPACE;
 278 
 279         if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
 280                 return (error);
 281 
 282         if (mvp->v_type != VDIR)
 283                 return (ENOTDIR);
 284 
 285         mutex_enter(&mvp->v_lock);
 286         if ((uap->flags & MS_REMOUNT) == 0 &&
 287             (uap->flags & MS_OVERLAY) == 0 &&
 288             (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
 289                 mutex_exit(&mvp->v_lock);
 290                 return (EBUSY);
 291         }
 292         mutex_exit(&mvp->v_lock);
 293 
 294         /*
 295          * Get arguments
 296          */
 297         bzero(&args, sizeof (args));
 298         if ((uap->flags & MS_DATA) && data != NULL && datalen != 0) {
 299                 int copy_result = 0;
 300 
 301                 if (datalen > sizeof (args))
 302                         return (EINVAL);
 303                 if (uap->flags & MS_SYSSPACE)
 304                         bcopy(data, &args, datalen);
 305                 else
 306                         copy_result = copyin(data, &args, datalen);
 307                 if (copy_result)
 308                         return (EFAULT);
 309                 datalen = sizeof (struct ufs_args);
 310         } else {
 311                 datalen = 0;
 312         }
 313 
 314         if ((vfsp->vfs_flag & VFS_RDONLY) != 0 ||
 315             (uap->flags & MS_RDONLY) != 0) {
 316                 oflag = FREAD;
 317                 aflag = VREAD;
 318         } else {
 319                 oflag = FREAD | FWRITE;
 320                 aflag = VREAD | VWRITE;
 321         }
 322 
 323         /*
 324          * Read in the mount point pathname
 325          * (so we can record the directory the file system was last mounted on).
 326          */
 327         if (error = pn_get(uap->dir, fromspace, &dpn))
 328                 return (error);
 329 
 330         /*
 331          * Resolve path name of special file being mounted.
 332          */
 333         if (error = lookupname(uap->spec, fromspace, FOLLOW, NULL, &svp)) {
 334                 pn_free(&dpn);
 335                 return (error);
 336         }
 337 
 338         error = vfs_get_lofi(vfsp, &lvp);
 339 
 340         if (error > 0) {
 341                 VN_RELE(svp);
 342                 pn_free(&dpn);
 343                 return (error);
 344         } else if (error == 0) {
 345                 bvp = lvp;
 346         } else {
 347                 bvp = svp;
 348 
 349                 if (svp->v_type != VBLK) {
 350                         VN_RELE(svp);
 351                         pn_free(&dpn);
 352                         return (ENOTBLK);
 353                 }
 354 
 355                 if ((error = secpolicy_spec_open(cr, svp, oflag)) != 0) {
 356                         VN_RELE(svp);
 357                         pn_free(&dpn);
 358                         return (error);
 359                 }
 360         }
 361 
 362         dev = bvp->v_rdev;
 363         if (getmajor(dev) >= devcnt) {
 364                 pn_free(&dpn);
 365                 if (lvp != NULL)
 366                         VN_RELE(lvp);
 367                 if (svp != NULL)
 368                         VN_RELE(svp);
 369                 return (ENXIO);
 370         }
 371         if (uap->flags & MS_REMOUNT)
 372                 why = ROOT_REMOUNT;
 373 
 374         /*
 375          * In SunCluster, requests to a global device are satisfied by a
 376          * local device. We substitute the global pxfs node with a local
 377          * spec node here.
 378          *
 379          * Open device/file mounted on.  We need this to check whether
 380          * the caller has sufficient rights to access the resource in
 381          * question.  When bio is fixed for vnodes this can all be vnode
 382          * operations.
 383          */
 384         if (IS_PXFSVP(bvp)) {
 385                 ASSERT(lvp == NULL);
 386                 VN_RELE(svp);
 387                 svp = NULL;
 388                 bvp = makespecvp(dev, VBLK);
 389                 error = VOP_ACCESS(bvp, aflag, 0, cr, NULL);
 390         } else {
 391                 error = VOP_ACCESS(svp, aflag, 0, cr, NULL);
 392         }
 393 
 394         if (error != 0) {
 395                 pn_free(&dpn);
 396                 if (lvp != NULL)
 397                         VN_RELE(lvp);
 398                 if (svp != NULL)
 399                         VN_RELE(svp);
 400                 return (error);
 401         }
 402 
 403         /*
 404          * Ensure that this device isn't already mounted or in progress on a
 405          * mount unless this is a REMOUNT request or we are told to suppress
 406          * mount checks. Global mounts require special handling.
 407          */
 408         if ((uap->flags & MS_NOCHECK) == 0) {
 409                 if ((uap->flags & MS_GLOBAL) == 0 &&
 410                     vfs_devmounting(dev, vfsp)) {
 411                         pn_free(&dpn);
 412                         if (lvp != NULL)
 413                                 VN_RELE(lvp);
 414                         if (svp != NULL)
 415                                 VN_RELE(svp);
 416                         return (EBUSY);
 417                 }
 418                 if (vfs_devismounted(dev)) {
 419                         if ((uap->flags & MS_REMOUNT) == 0) {
 420                                 pn_free(&dpn);
 421                                 if (lvp != NULL)
 422                                         VN_RELE(lvp);
 423                                 if (svp != NULL)
 424                                         VN_RELE(svp);
 425                                 return (EBUSY);
 426                         }
 427                 }
 428         }
 429 
 430         /*
 431          * If the device is a tape, mount it read only
 432          */
 433         if (devopsp[getmajor(dev)]->devo_cb_ops->cb_flag & D_TAPE) {
 434                 vfsp->vfs_flag |= VFS_RDONLY;
 435                 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
 436         }
 437         if (uap->flags & MS_RDONLY)
 438                 vfsp->vfs_flag |= VFS_RDONLY;
 439 
 440         /*
 441          * Mount the filesystem, free the device vnode on error.
 442          */
 443         error = mountfs(vfsp, why, bvp, dpn.pn_path, cr, 0, &args, datalen);
 444         pn_free(&dpn);
 445 
 446         if (error) {
 447                 if (lvp != NULL)
 448                         VN_RELE(lvp);
 449                 if (svp != NULL)
 450                         VN_RELE(svp);
 451         } else {
 452                 /*
 453                  * If lofi, drop our reference to the original file.
 454                  */
 455                 if (lvp != NULL)
 456                         VN_RELE(svp);
 457         }
 458 
 459         if (error == 0)
 460                 vfs_set_feature(vfsp, VFSFT_XVATTR);
 461         return (error);
 462 }
 463 /*
 464  * Mount root file system.
 465  * "why" is ROOT_INIT on initial call ROOT_REMOUNT if called to
 466  * remount the root file system, and ROOT_UNMOUNT if called to
 467  * unmount the root (e.g., as part of a system shutdown).
 468  *
 469  * XXX - this may be partially machine-dependent; it, along with the VFS_SWAPVP
 470  * operation, goes along with auto-configuration.  A mechanism should be
 471  * provided by which machine-INdependent code in the kernel can say "get me the
 472  * right root file system" and "get me the right initial swap area", and have
 473  * that done in what may well be a machine-dependent fashion.
 474  * Unfortunately, it is also file-system-type dependent (NFS gets it via
 475  * bootparams calls, UFS gets it from various and sundry machine-dependent
 476  * mechanisms, as SPECFS does for swap).
 477  */
 478 static int
 479 ufs_mountroot(struct vfs *vfsp, enum whymountroot why)
 480 {
 481         struct fs *fsp;
 482         int error;
 483         static int ufsrootdone = 0;
 484         dev_t rootdev;
 485         struct vnode *vp;
 486         struct vnode *devvp = 0;
 487         int ovflags;
 488         int doclkset;
 489         ufsvfs_t *ufsvfsp;
 490 
 491         if (why == ROOT_INIT) {
 492                 if (ufsrootdone++)
 493                         return (EBUSY);
 494                 rootdev = getrootdev();
 495                 if (rootdev == (dev_t)NODEV)
 496                         return (ENODEV);
 497                 vfsp->vfs_dev = rootdev;
 498                 vfsp->vfs_flag |= VFS_RDONLY;
 499         } else if (why == ROOT_REMOUNT) {
 500                 vp = ((struct ufsvfs *)vfsp->vfs_data)->vfs_devvp;
 501                 (void) dnlc_purge_vfsp(vfsp, 0);
 502                 vp = common_specvp(vp);
 503                 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_INVAL,
 504                     CRED(), NULL);
 505                 (void) bfinval(vfsp->vfs_dev, 0);
 506                 fsp = getfs(vfsp);
 507 
 508                 ovflags = vfsp->vfs_flag;
 509                 vfsp->vfs_flag &= ~VFS_RDONLY;
 510                 vfsp->vfs_flag |= VFS_REMOUNT;
 511                 rootdev = vfsp->vfs_dev;
 512         } else if (why == ROOT_UNMOUNT) {
 513                 if (vfs_lock(vfsp) == 0) {
 514                         (void) ufs_flush(vfsp);
 515                         /*
 516                          * Mark the log as fully rolled
 517                          */
 518                         ufsvfsp = (ufsvfs_t *)vfsp->vfs_data;
 519                         fsp = ufsvfsp->vfs_fs;
 520                         if (TRANS_ISTRANS(ufsvfsp) &&
 521                             !TRANS_ISERROR(ufsvfsp) &&
 522                             (fsp->fs_rolled == FS_NEED_ROLL)) {
 523                                 ml_unit_t *ul = ufsvfsp->vfs_log;
 524 
 525                                 error = ufs_putsummaryinfo(ul->un_dev,
 526                                     ufsvfsp, fsp);
 527                                 if (error == 0) {
 528                                         fsp->fs_rolled = FS_ALL_ROLLED;
 529                                         UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
 530                                 }
 531                         }
 532                         vfs_unlock(vfsp);
 533                 } else {
 534                         ufs_update(0);
 535                 }
 536 
 537                 vp = ((struct ufsvfs *)vfsp->vfs_data)->vfs_devvp;
 538                 (void) VOP_CLOSE(vp, FREAD|FWRITE, 1,
 539                     (offset_t)0, CRED(), NULL);
 540                 return (0);
 541         }
 542         error = vfs_lock(vfsp);
 543         if (error)
 544                 return (error);
 545 
 546         devvp = makespecvp(rootdev, VBLK);
 547 
 548         /* If RO media, don't call clkset() (see below) */
 549         doclkset = 1;
 550         if (why == ROOT_INIT) {
 551                 error = VOP_OPEN(&devvp, FREAD|FWRITE, CRED(), NULL);
 552                 if (error == 0) {
 553                         (void) VOP_CLOSE(devvp, FREAD|FWRITE, 1,
 554                             (offset_t)0, CRED(), NULL);
 555                 } else {
 556                         doclkset = 0;
 557                 }
 558         }
 559 
 560         error = mountfs(vfsp, why, devvp, "/", CRED(), 1, NULL, 0);
 561         /*
 562          * XXX - assumes root device is not indirect, because we don't set
 563          * rootvp.  Is rootvp used for anything?  If so, make another arg
 564          * to mountfs.
 565          */
 566         if (error) {
 567                 vfs_unlock(vfsp);
 568                 if (why == ROOT_REMOUNT)
 569                         vfsp->vfs_flag = ovflags;
 570                 if (rootvp) {
 571                         VN_RELE(rootvp);
 572                         rootvp = (struct vnode *)0;
 573                 }
 574                 VN_RELE(devvp);
 575                 return (error);
 576         }
 577         if (why == ROOT_INIT)
 578                 vfs_add((struct vnode *)0, vfsp,
 579                     (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
 580         vfs_unlock(vfsp);
 581         fsp = getfs(vfsp);
 582         clkset(doclkset ? fsp->fs_time : -1);
 583         ufsvfsp = (ufsvfs_t *)vfsp->vfs_data;
 584         if (ufsvfsp->vfs_log) {
 585                 vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
 586         }
 587         return (0);
 588 }
 589 
 590 static int
 591 remountfs(struct vfs *vfsp, dev_t dev, void *raw_argsp, int args_len)
 592 {
 593         struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
 594         struct ulockfs *ulp = &ufsvfsp->vfs_ulockfs;
 595         struct buf *bp = ufsvfsp->vfs_bufp;
 596         struct fs *fsp = (struct fs *)bp->b_un.b_addr;
 597         struct fs *fspt;
 598         struct buf *tpt = 0;
 599         int error = 0;
 600         int flags = 0;
 601 
 602         if (args_len == sizeof (struct ufs_args) && raw_argsp)
 603                 flags = ((struct ufs_args *)raw_argsp)->flags;
 604 
 605         /* cannot remount to RDONLY */
 606         if (vfsp->vfs_flag & VFS_RDONLY)
 607                 return (ENOTSUP);
 608 
 609         /* whoops, wrong dev */
 610         if (vfsp->vfs_dev != dev)
 611                 return (EINVAL);
 612 
 613         /*
 614          * synchronize w/ufs ioctls
 615          */
 616         mutex_enter(&ulp->ul_lock);
 617         atomic_add_long(&ufs_quiesce_pend, 1);
 618 
 619         /*
 620          * reset options
 621          */
 622         ufsvfsp->vfs_nointr  = flags & UFSMNT_NOINTR;
 623         ufsvfsp->vfs_syncdir = flags & UFSMNT_SYNCDIR;
 624         ufsvfsp->vfs_nosetsec = flags & UFSMNT_NOSETSEC;
 625         ufsvfsp->vfs_noatime = flags & UFSMNT_NOATIME;
 626         if ((flags & UFSMNT_NODFRATIME) || ufsvfsp->vfs_noatime)
 627                 ufsvfsp->vfs_dfritime &= ~UFS_DFRATIME;
 628         else    /* dfratime, default behavior */
 629                 ufsvfsp->vfs_dfritime |= UFS_DFRATIME;
 630         if (flags & UFSMNT_FORCEDIRECTIO)
 631                 ufsvfsp->vfs_forcedirectio = 1;
 632         else    /* default is no direct I/O */
 633                 ufsvfsp->vfs_forcedirectio = 0;
 634         ufsvfsp->vfs_iotstamp = lbolt;
 635 
 636         /*
 637          * set largefiles flag in ufsvfs equal to the
 638          * value passed in by the mount command. If
 639          * it is "nolargefiles", and the flag is set
 640          * in the superblock, the mount fails.
 641          */
 642         if (!(flags & UFSMNT_LARGEFILES)) {  /* "nolargefiles" */
 643                 if (fsp->fs_flags & FSLARGEFILES) {
 644                         error = EFBIG;
 645                         goto remounterr;
 646                 }
 647                 ufsvfsp->vfs_lfflags &= ~UFS_LARGEFILES;
 648         } else  /* "largefiles" */
 649                 ufsvfsp->vfs_lfflags |= UFS_LARGEFILES;
 650         /*
 651          * read/write to read/write; all done
 652          */
 653         if (fsp->fs_ronly == 0)
 654                 goto remounterr;
 655 
 656         /*
 657          * fix-on-panic assumes RO->RW remount implies system-critical fs
 658          * if it is shortly after boot; so, don't attempt to lock and fix
 659          * (unless the user explicitly asked for another action on error)
 660          * XXX UFSMNT_ONERROR_RDONLY rather than UFSMNT_ONERROR_PANIC
 661          */
 662 #define BOOT_TIME_LIMIT (180*hz)
 663         if (!(flags & UFSMNT_ONERROR_FLGMASK) && lbolt < BOOT_TIME_LIMIT) {
 664                 cmn_err(CE_WARN, "%s is required to be mounted onerror=%s",
 665                     ufsvfsp->vfs_fs->fs_fsmnt, UFSMNT_ONERROR_PANIC_STR);
 666                 flags |= UFSMNT_ONERROR_PANIC;
 667         }
 668 
 669         if ((error = ufsfx_mount(ufsvfsp, flags)) != 0)
 670                 goto remounterr;
 671 
 672         /*
 673          * quiesce the file system
 674          */
 675         error = ufs_quiesce(ulp);
 676         if (error)
 677                 goto remounterr;
 678 
 679         tpt = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, SBLOCK, SBSIZE);
 680         if (tpt->b_flags & B_ERROR) {
 681                 error = EIO;
 682                 goto remounterr;
 683         }
 684         fspt = (struct fs *)tpt->b_un.b_addr;
 685         if (((fspt->fs_magic != FS_MAGIC) &&
 686             (fspt->fs_magic != MTB_UFS_MAGIC)) ||
 687             (fspt->fs_magic == FS_MAGIC &&
 688             (fspt->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
 689             fspt->fs_version != UFS_VERSION_MIN)) ||
 690             (fspt->fs_magic == MTB_UFS_MAGIC &&
 691             (fspt->fs_version > MTB_UFS_VERSION_1 ||
 692             fspt->fs_version < MTB_UFS_VERSION_MIN)) ||
 693             fspt->fs_bsize > MAXBSIZE || fspt->fs_frag > MAXFRAG ||
 694             fspt->fs_bsize < sizeof (struct fs) || fspt->fs_bsize < PAGESIZE) {
 695                 tpt->b_flags |= B_STALE | B_AGE;
 696                 error = EINVAL;
 697                 goto remounterr;
 698         }
 699 
 700         if (ufsvfsp->vfs_log && (ufsvfsp->vfs_log->un_flags & LDL_NOROLL)) {
 701                 ufsvfsp->vfs_log->un_flags &= ~LDL_NOROLL;
 702                 logmap_start_roll(ufsvfsp->vfs_log);
 703         }
 704 
 705         if (TRANS_ISERROR(ufsvfsp))
 706                 goto remounterr;
 707         TRANS_DOMATAMAP(ufsvfsp);
 708 
 709         if ((fspt->fs_state + fspt->fs_time == FSOKAY) &&
 710             fspt->fs_clean == FSLOG && !TRANS_ISTRANS(ufsvfsp)) {
 711                 ufsvfsp->vfs_log = NULL;
 712                 ufsvfsp->vfs_domatamap = 0;
 713                 error = ENOSPC;
 714                 goto remounterr;
 715         }
 716 
 717         if (fspt->fs_state + fspt->fs_time == FSOKAY &&
 718             (fspt->fs_clean == FSCLEAN ||
 719             fspt->fs_clean == FSSTABLE ||
 720             fspt->fs_clean == FSLOG)) {
 721 
 722                 /*
 723                  * Ensure that ufs_getsummaryinfo doesn't reconstruct
 724                  * the summary info.
 725                  */
 726                 error = ufs_getsummaryinfo(vfsp->vfs_dev, ufsvfsp, fspt);
 727                 if (error)
 728                         goto remounterr;
 729 
 730                 /* preserve mount name */
 731                 (void) strncpy(fspt->fs_fsmnt, fsp->fs_fsmnt, MAXMNTLEN);
 732                 /* free the old cg space */
 733                 kmem_free(fsp->fs_u.fs_csp, fsp->fs_cssize);
 734                 /* switch in the new superblock */
 735                 fspt->fs_rolled = FS_NEED_ROLL;
 736                 bcopy(tpt->b_un.b_addr, bp->b_un.b_addr, fspt->fs_sbsize);
 737 
 738                 fsp->fs_clean = FSSTABLE;
 739         } /* superblock updated in memory */
 740         tpt->b_flags |= B_STALE | B_AGE;
 741         brelse(tpt);
 742         tpt = 0;
 743 
 744         if (fsp->fs_clean != FSSTABLE) {
 745                 error = ENOSPC;
 746                 goto remounterr;
 747         }
 748 
 749 
 750         if (TRANS_ISTRANS(ufsvfsp)) {
 751                 fsp->fs_clean = FSLOG;
 752                 ufsvfsp->vfs_dio = 0;
 753         } else
 754                 if (ufsvfsp->vfs_dio)
 755                         fsp->fs_clean = FSSUSPEND;
 756 
 757         TRANS_MATA_MOUNT(ufsvfsp);
 758 
 759         fsp->fs_fmod = 0;
 760         fsp->fs_ronly = 0;
 761 
 762         atomic_add_long(&ufs_quiesce_pend, -1);
 763         cv_broadcast(&ulp->ul_cv);
 764         mutex_exit(&ulp->ul_lock);
 765 
 766         if (TRANS_ISTRANS(ufsvfsp)) {
 767 
 768                 /*
 769                  * start the delete thread
 770                  */
 771                 ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
 772 
 773                 /*
 774                  * start the reclaim thread
 775                  */
 776                 if (fsp->fs_reclaim & (FS_RECLAIM|FS_RECLAIMING)) {
 777                         fsp->fs_reclaim &= ~FS_RECLAIM;
 778                         fsp->fs_reclaim |=  FS_RECLAIMING;
 779                         ufs_thread_start(&ufsvfsp->vfs_reclaim,
 780                             ufs_thread_reclaim, vfsp);
 781                 }
 782         }
 783 
 784         TRANS_SBWRITE(ufsvfsp, TOP_MOUNT);
 785 
 786         return (0);
 787 
 788 remounterr:
 789         if (tpt)
 790                 brelse(tpt);
 791         atomic_add_long(&ufs_quiesce_pend, -1);
 792         cv_broadcast(&ulp->ul_cv);
 793         mutex_exit(&ulp->ul_lock);
 794         return (error);
 795 }
 796 
 797 /*
 798  * If the device maxtransfer size is not available, we use ufs_maxmaxphys
 799  * along with the system value for maxphys to determine the value for
 800  * maxtransfer.
 801  */
 802 int ufs_maxmaxphys = (1024 * 1024);
 803 
 804 #include <sys/ddi.h>              /* for delay(9f) */
 805 
 806 int ufs_mount_error_delay = 20; /* default to 20ms */
 807 int ufs_mount_timeout = 60000;  /* default to 1 minute */
 808 
 809 static int
 810 mountfs(struct vfs *vfsp, enum whymountroot why, struct vnode *devvp,
 811         char *path, cred_t *cr, int isroot, void *raw_argsp, int args_len)
 812 {
 813         dev_t dev = devvp->v_rdev;
 814         struct fs *fsp;
 815         struct ufsvfs *ufsvfsp = 0;
 816         struct buf *bp = 0;
 817         struct buf *tp = 0;
 818         struct dk_cinfo ci;
 819         int error = 0;
 820         size_t len;
 821         int needclose = 0;
 822         int needtrans = 0;
 823         struct inode *rip;
 824         struct vnode *rvp = NULL;
 825         int flags = 0;
 826         kmutex_t *ihm;
 827         int elapsed;
 828         int status;
 829         extern  int     maxphys;
 830 
 831         if (args_len == sizeof (struct ufs_args) && raw_argsp)
 832                 flags = ((struct ufs_args *)raw_argsp)->flags;
 833 
 834         ASSERT(vfs_lock_held(vfsp));
 835 
 836         if (why == ROOT_INIT) {
 837                 /*
 838                  * Open block device mounted on.
 839                  * When bio is fixed for vnodes this can all be vnode
 840                  * operations.
 841                  */
 842                 error = VOP_OPEN(&devvp,
 843                     (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
 844                     cr, NULL);
 845                 if (error)
 846                         goto out;
 847                 needclose = 1;
 848 
 849                 /*
 850                  * Refuse to go any further if this
 851                  * device is being used for swapping.
 852                  */
 853                 if (IS_SWAPVP(devvp)) {
 854                         error = EBUSY;
 855                         goto out;
 856                 }
 857         }
 858 
 859         /*
 860          * check for dev already mounted on
 861          */
 862         if (vfsp->vfs_flag & VFS_REMOUNT) {
 863                 error = remountfs(vfsp, dev, raw_argsp, args_len);
 864                 if (error == 0)
 865                         VN_RELE(devvp);
 866                 return (error);
 867         }
 868 
 869         ASSERT(devvp != 0);
 870 
 871         /*
 872          * Flush back any dirty pages on the block device to
 873          * try and keep the buffer cache in sync with the page
 874          * cache if someone is trying to use block devices when
 875          * they really should be using the raw device.
 876          */
 877         (void) VOP_PUTPAGE(common_specvp(devvp), (offset_t)0,
 878             (size_t)0, B_INVAL, cr, NULL);
 879 
 880         /*
 881          * read in superblock
 882          */
 883         ufsvfsp = kmem_zalloc(sizeof (struct ufsvfs), KM_SLEEP);
 884         tp = UFS_BREAD(ufsvfsp, dev, SBLOCK, SBSIZE);
 885         if (tp->b_flags & B_ERROR)
 886                 goto out;
 887         fsp = (struct fs *)tp->b_un.b_addr;
 888 
 889         if ((fsp->fs_magic != FS_MAGIC) && (fsp->fs_magic != MTB_UFS_MAGIC)) {
 890                 cmn_err(CE_NOTE,
 891                     "mount: not a UFS magic number (0x%x)", fsp->fs_magic);
 892                 error = EINVAL;
 893                 goto out;
 894         }
 895 
 896         if ((fsp->fs_magic == FS_MAGIC) &&
 897             (fsp->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
 898             fsp->fs_version != UFS_VERSION_MIN)) {
 899                 cmn_err(CE_NOTE,
 900                     "mount: unrecognized version of UFS on-disk format: %d",
 901                     fsp->fs_version);
 902                 error = EINVAL;
 903                 goto out;
 904         }
 905 
 906         if ((fsp->fs_magic == MTB_UFS_MAGIC) &&
 907             (fsp->fs_version > MTB_UFS_VERSION_1 ||
 908             fsp->fs_version < MTB_UFS_VERSION_MIN)) {
 909                 cmn_err(CE_NOTE,
 910                     "mount: unrecognized version of UFS on-disk format: %d",
 911                     fsp->fs_version);
 912                 error = EINVAL;
 913                 goto out;
 914         }
 915 
 916 #ifndef _LP64
 917         if (fsp->fs_magic == MTB_UFS_MAGIC) {
 918                 /*
 919                  * Find the size of the device in sectors.  If the
 920                  * the size in sectors is greater than INT_MAX, it's
 921                  * a multi-terabyte file system, which can't be
 922                  * mounted by a 32-bit kernel.  We can't use the
 923                  * fsbtodb() macro in the next line because the macro
 924                  * casts the intermediate values to daddr_t, which is
 925                  * a 32-bit quantity in a 32-bit kernel.  Here we
 926                  * really do need the intermediate values to be held
 927                  * in 64-bit quantities because we're checking for
 928                  * overflow of a 32-bit field.
 929                  */
 930                 if ((((diskaddr_t)(fsp->fs_size)) << fsp->fs_fsbtodb)
 931                     > INT_MAX) {
 932                         cmn_err(CE_NOTE,
 933                             "mount: multi-terabyte UFS cannot be"
 934                             " mounted by a 32-bit kernel");
 935                         error = EINVAL;
 936                         goto out;
 937                 }
 938 
 939         }
 940 #endif
 941 
 942         if (fsp->fs_bsize > MAXBSIZE || fsp->fs_frag > MAXFRAG ||
 943             fsp->fs_bsize < sizeof (struct fs) || fsp->fs_bsize < PAGESIZE) {
 944                 error = EINVAL; /* also needs translation */
 945                 goto out;
 946         }
 947 
 948         /*
 949          * Allocate VFS private data.
 950          */
 951         vfsp->vfs_bcount = 0;
 952         vfsp->vfs_data = (caddr_t)ufsvfsp;
 953         vfsp->vfs_fstype = ufsfstype;
 954         vfsp->vfs_dev = dev;
 955         vfsp->vfs_flag |= VFS_NOTRUNC;
 956         vfs_make_fsid(&vfsp->vfs_fsid, dev, ufsfstype);
 957         ufsvfsp->vfs_devvp = devvp;
 958 
 959         /*
 960          * Cross-link with vfs and add to instance list.
 961          */
 962         ufsvfsp->vfs_vfs = vfsp;
 963         ufs_vfs_add(ufsvfsp);
 964 
 965         ufsvfsp->vfs_dev = dev;
 966         ufsvfsp->vfs_bufp = tp;
 967 
 968         ufsvfsp->vfs_dirsize = INODESIZE + (4 * ALLOCSIZE) + fsp->fs_fsize;
 969         ufsvfsp->vfs_minfrags =
 970             (int)((int64_t)fsp->fs_dsize * fsp->fs_minfree / 100);
 971         /*
 972          * if mount allows largefiles, indicate so in ufsvfs
 973          */
 974         if (flags & UFSMNT_LARGEFILES)
 975                 ufsvfsp->vfs_lfflags |= UFS_LARGEFILES;
 976         /*
 977          * Initialize threads
 978          */
 979         ufs_delete_init(ufsvfsp, 1);
 980         ufs_thread_init(&ufsvfsp->vfs_reclaim, 0);
 981 
 982         /*
 983          * Chicken and egg problem. The superblock may have deltas
 984          * in the log.  So after the log is scanned we reread the
 985          * superblock. We guarantee that the fields needed to
 986          * scan the log will not be in the log.
 987          */
 988         if (fsp->fs_logbno && fsp->fs_clean == FSLOG &&
 989             (fsp->fs_state + fsp->fs_time == FSOKAY)) {
 990                 error = lufs_snarf(ufsvfsp, fsp, (vfsp->vfs_flag & VFS_RDONLY));
 991                 if (error) {
 992                         /*
 993                          * Allow a ro mount to continue even if the
 994                          * log cannot be processed - yet.
 995                          */
 996                         if (!(vfsp->vfs_flag & VFS_RDONLY)) {
 997                                 cmn_err(CE_WARN, "Error accessing ufs "
 998                                     "log for %s; Please run fsck(1M)", path);
 999                                 goto out;
1000                         }
1001                 }
1002                 tp->b_flags |= (B_AGE | B_STALE);
1003                 brelse(tp);
1004                 tp = UFS_BREAD(ufsvfsp, dev, SBLOCK, SBSIZE);
1005                 fsp = (struct fs *)tp->b_un.b_addr;
1006                 ufsvfsp->vfs_bufp = tp;
1007                 if (tp->b_flags & B_ERROR)
1008                         goto out;
1009         }
1010 
1011         /*
1012          * Set logging mounted flag used by lockfs
1013          */
1014         ufsvfsp->vfs_validfs = UT_MOUNTED;
1015 
1016         /*
1017          * Copy the super block into a buffer in its native size.
1018          * Use ngeteblk to allocate the buffer
1019          */
1020         bp = ngeteblk(fsp->fs_bsize);
1021         ufsvfsp->vfs_bufp = bp;
1022         bp->b_edev = dev;
1023         bp->b_dev = cmpdev(dev);
1024         bp->b_blkno = SBLOCK;
1025         bp->b_bcount = fsp->fs_sbsize;
1026         bcopy(tp->b_un.b_addr, bp->b_un.b_addr, fsp->fs_sbsize);
1027         tp->b_flags |= B_STALE | B_AGE;
1028         brelse(tp);
1029         tp = 0;
1030 
1031         fsp = (struct fs *)bp->b_un.b_addr;
1032         /*
1033          * Mount fails if superblock flag indicates presence of large
1034          * files and filesystem is attempted to be mounted 'nolargefiles'.
1035          * The exception is for a read only mount of root, which we
1036          * always want to succeed, so fsck can fix potential problems.
1037          * The assumption is that we will remount root at some point,
1038          * and the remount will enforce the mount option.
1039          */
1040         if (!(isroot & (vfsp->vfs_flag & VFS_RDONLY)) &&
1041             (fsp->fs_flags & FSLARGEFILES) &&
1042             !(flags & UFSMNT_LARGEFILES)) {
1043                 error = EFBIG;
1044                 goto out;
1045         }
1046 
1047         if (vfsp->vfs_flag & VFS_RDONLY) {
1048                 fsp->fs_ronly = 1;
1049                 fsp->fs_fmod = 0;
1050                 if (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
1051                     ((fsp->fs_clean == FSCLEAN) ||
1052                     (fsp->fs_clean == FSSTABLE) ||
1053                     (fsp->fs_clean == FSLOG))) {
1054                         if (isroot) {
1055                                 if (fsp->fs_clean == FSLOG) {
1056                                         if (fsp->fs_rolled == FS_ALL_ROLLED) {
1057                                                 ufs_clean_root = 1;
1058                                         }
1059                                 } else {
1060                                         ufs_clean_root = 1;
1061                                 }
1062                         }
1063                         fsp->fs_clean = FSSTABLE;
1064                 } else {
1065                         fsp->fs_clean = FSBAD;
1066                 }
1067         } else {
1068 
1069                 fsp->fs_fmod = 0;
1070                 fsp->fs_ronly = 0;
1071 
1072                 TRANS_DOMATAMAP(ufsvfsp);
1073 
1074                 if ((TRANS_ISERROR(ufsvfsp)) ||
1075                     (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
1076                     fsp->fs_clean == FSLOG && !TRANS_ISTRANS(ufsvfsp))) {
1077                         ufsvfsp->vfs_log = NULL;
1078                         ufsvfsp->vfs_domatamap = 0;
1079                         error = ENOSPC;
1080                         goto out;
1081                 }
1082 
1083                 if (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
1084                     (fsp->fs_clean == FSCLEAN ||
1085                     fsp->fs_clean == FSSTABLE ||
1086                     fsp->fs_clean == FSLOG))
1087                         fsp->fs_clean = FSSTABLE;
1088                 else {
1089                         if (isroot) {
1090                                 /*
1091                                  * allow root partition to be mounted even
1092                                  * when fs_state is not ok
1093                                  * will be fixed later by a remount root
1094                                  */
1095                                 fsp->fs_clean = FSBAD;
1096                                 ufsvfsp->vfs_log = NULL;
1097                                 ufsvfsp->vfs_domatamap = 0;
1098                         } else {
1099                                 error = ENOSPC;
1100                                 goto out;
1101                         }
1102                 }
1103 
1104                 if (fsp->fs_clean == FSSTABLE && TRANS_ISTRANS(ufsvfsp))
1105                         fsp->fs_clean = FSLOG;
1106         }
1107         TRANS_MATA_MOUNT(ufsvfsp);
1108         needtrans = 1;
1109 
1110         vfsp->vfs_bsize = fsp->fs_bsize;
1111 
1112         /*
1113          * Read in summary info
1114          */
1115         if (error = ufs_getsummaryinfo(dev, ufsvfsp, fsp))
1116                 goto out;
1117 
1118         /*
1119          * lastwhinetime is set to zero rather than lbolt, so that after
1120          * mounting if the filesystem is found to be full, then immediately the
1121          * "file system message" will be logged.
1122          */
1123         ufsvfsp->vfs_lastwhinetime = 0L;
1124 
1125 
1126         mutex_init(&ufsvfsp->vfs_lock, NULL, MUTEX_DEFAULT, NULL);
1127         (void) copystr(path, fsp->fs_fsmnt, sizeof (fsp->fs_fsmnt) - 1, &len);
1128         bzero(fsp->fs_fsmnt + len, sizeof (fsp->fs_fsmnt) - len);
1129 
1130         /*
1131          * Sanity checks for old file systems
1132          */
1133         if (fsp->fs_postblformat == FS_42POSTBLFMT)
1134                 ufsvfsp->vfs_nrpos = 8;
1135         else
1136                 ufsvfsp->vfs_nrpos = fsp->fs_nrpos;
1137 
1138         /*
1139          * Initialize lockfs structure to support file system locking
1140          */
1141         bzero(&ufsvfsp->vfs_ulockfs.ul_lockfs,
1142             sizeof (struct lockfs));
1143         ufsvfsp->vfs_ulockfs.ul_fs_lock = ULOCKFS_ULOCK;
1144         mutex_init(&ufsvfsp->vfs_ulockfs.ul_lock, NULL,
1145             MUTEX_DEFAULT, NULL);
1146         cv_init(&ufsvfsp->vfs_ulockfs.ul_cv, NULL, CV_DEFAULT, NULL);
1147 
1148         /*
1149          * We don't need to grab vfs_dqrwlock for this ufs_iget() call.
1150          * We are in the process of mounting the file system so there
1151          * is no need to grab the quota lock. If a quota applies to the
1152          * root inode, then it will be updated when quotas are enabled.
1153          *
1154          * However, we have an ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock))
1155          * in getinoquota() that we want to keep so grab it anyway.
1156          */
1157         rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1158 
1159         error = ufs_iget_alloced(vfsp, UFSROOTINO, &rip, cr);
1160 
1161         rw_exit(&ufsvfsp->vfs_dqrwlock);
1162 
1163         if (error)
1164                 goto out;
1165 
1166         /*
1167          * make sure root inode is a directory.  Returning ENOTDIR might
1168          * be confused with the mount point not being a directory, so
1169          * we use EIO instead.
1170          */
1171         if ((rip->i_mode & IFMT) != IFDIR) {
1172                 /*
1173                  * Mark this inode as subject for cleanup
1174                  * to avoid stray inodes in the cache.
1175                  */
1176                 rvp = ITOV(rip);
1177                 error = EIO;
1178                 goto out;
1179         }
1180 
1181         rvp = ITOV(rip);
1182         mutex_enter(&rvp->v_lock);
1183         rvp->v_flag |= VROOT;
1184         mutex_exit(&rvp->v_lock);
1185         ufsvfsp->vfs_root = rvp;
1186         /* The buffer for the root inode does not contain a valid b_vp */
1187         (void) bfinval(dev, 0);
1188 
1189         /* options */
1190         ufsvfsp->vfs_nosetsec = flags & UFSMNT_NOSETSEC;
1191         ufsvfsp->vfs_nointr  = flags & UFSMNT_NOINTR;
1192         ufsvfsp->vfs_syncdir = flags & UFSMNT_SYNCDIR;
1193         ufsvfsp->vfs_noatime = flags & UFSMNT_NOATIME;
1194         if ((flags & UFSMNT_NODFRATIME) || ufsvfsp->vfs_noatime)
1195                 ufsvfsp->vfs_dfritime &= ~UFS_DFRATIME;
1196         else    /* dfratime, default behavior */
1197                 ufsvfsp->vfs_dfritime |= UFS_DFRATIME;
1198         if (flags & UFSMNT_FORCEDIRECTIO)
1199                 ufsvfsp->vfs_forcedirectio = 1;
1200         else if (flags & UFSMNT_NOFORCEDIRECTIO)
1201                 ufsvfsp->vfs_forcedirectio = 0;
1202         ufsvfsp->vfs_iotstamp = lbolt;
1203 
1204         ufsvfsp->vfs_nindiroffset = fsp->fs_nindir - 1;
1205         ufsvfsp->vfs_nindirshift = highbit(ufsvfsp->vfs_nindiroffset);
1206         ufsvfsp->vfs_ioclustsz = fsp->fs_bsize * fsp->fs_maxcontig;
1207 
1208         if (cdev_ioctl(dev, DKIOCINFO, (intptr_t)&ci,
1209             FKIOCTL|FNATIVE|FREAD, CRED(), &status) == 0) {
1210                 ufsvfsp->vfs_iotransz = ci.dki_maxtransfer * DEV_BSIZE;
1211         } else {
1212                 ufsvfsp->vfs_iotransz = MIN(maxphys, ufs_maxmaxphys);
1213         }
1214 
1215         if (ufsvfsp->vfs_iotransz <= 0) {
1216                 ufsvfsp->vfs_iotransz = MIN(maxphys, ufs_maxmaxphys);
1217         }
1218 
1219         /*
1220          * When logging, used to reserve log space for writes and truncs
1221          */
1222         ufsvfsp->vfs_avgbfree = fsp->fs_cstotal.cs_nbfree / fsp->fs_ncg;
1223 
1224         /*
1225          * Determine whether to log cylinder group summary info.
1226          */
1227         ufsvfsp->vfs_nolog_si = (fsp->fs_ncg < ufs_ncg_log);
1228 
1229         if (TRANS_ISTRANS(ufsvfsp)) {
1230                 /*
1231                  * start the delete thread
1232                  */
1233                 ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
1234 
1235                 /*
1236                  * start reclaim thread if the filesystem was not mounted
1237                  * read only.
1238                  */
1239                 if (!fsp->fs_ronly && (fsp->fs_reclaim &
1240                     (FS_RECLAIM|FS_RECLAIMING))) {
1241                         fsp->fs_reclaim &= ~FS_RECLAIM;
1242                         fsp->fs_reclaim |=  FS_RECLAIMING;
1243                         ufs_thread_start(&ufsvfsp->vfs_reclaim,
1244                             ufs_thread_reclaim, vfsp);
1245                 }
1246 
1247                 /* Mark the fs as unrolled */
1248                 fsp->fs_rolled = FS_NEED_ROLL;
1249         } else if (!fsp->fs_ronly && (fsp->fs_reclaim &
1250             (FS_RECLAIM|FS_RECLAIMING))) {
1251                 /*
1252                  * If a file system that is mounted nologging, after
1253                  * having previously been mounted logging, becomes
1254                  * unmounted whilst the reclaim thread is in the throes
1255                  * of reclaiming open/deleted inodes, a subsequent mount
1256                  * of such a file system with logging disabled could lead
1257                  * to inodes becoming lost.  So, start reclaim now, even
1258                  * though logging was disabled for the previous mount, to
1259                  * tidy things up.
1260                  */
1261                 fsp->fs_reclaim &= ~FS_RECLAIM;
1262                 fsp->fs_reclaim |=  FS_RECLAIMING;
1263                 ufs_thread_start(&ufsvfsp->vfs_reclaim,
1264                     ufs_thread_reclaim, vfsp);
1265         }
1266 
1267         if (!fsp->fs_ronly) {
1268                 TRANS_SBWRITE(ufsvfsp, TOP_MOUNT);
1269                 if (error = geterror(ufsvfsp->vfs_bufp))
1270                         goto out;
1271         }
1272 
1273         /* fix-on-panic initialization */
1274         if (isroot && !(flags & UFSMNT_ONERROR_FLGMASK))
1275                 flags |= UFSMNT_ONERROR_PANIC;  /* XXX ..._RDONLY */
1276 
1277         if ((error = ufsfx_mount(ufsvfsp, flags)) != 0)
1278                 goto out;
1279 
1280         if (why == ROOT_INIT && isroot)
1281                 rootvp = devvp;
1282 
1283         return (0);
1284 out:
1285         if (error == 0)
1286                 error = EIO;
1287         if (rvp) {
1288                 /* the following sequence is similar to ufs_unmount() */
1289 
1290                 /*
1291                  * There's a problem that ufs_iget() puts inodes into
1292                  * the inode cache before it returns them.  If someone
1293                  * traverses that cache and gets a reference to our
1294                  * inode, there's a chance they'll still be using it
1295                  * after we've destroyed it.  This is a hard race to
1296                  * hit, but it's happened (putting in a medium delay
1297                  * here, and a large delay in ufs_scan_inodes() for
1298                  * inodes on the device we're bailing out on, makes
1299                  * the race easy to demonstrate).  The symptom is some
1300                  * other part of UFS faulting on bad inode contents,
1301                  * or when grabbing one of the locks inside the inode,
1302                  * etc.  The usual victim is ufs_scan_inodes() or
1303                  * someone called by it.
1304                  */
1305 
1306                 /*
1307                  * First, isolate it so that no new references can be
1308                  * gotten via the inode cache.
1309                  */
1310                 ihm = &ih_lock[INOHASH(UFSROOTINO)];
1311                 mutex_enter(ihm);
1312                 remque(rip);
1313                 mutex_exit(ihm);
1314 
1315                 /*
1316                  * Now wait for all outstanding references except our
1317                  * own to drain.  This could, in theory, take forever,
1318                  * so don't wait *too* long.  If we time out, mark
1319                  * it stale and leak it, so we don't hit the problem
1320                  * described above.
1321                  *
1322                  * Note that v_count is an int, which means we can read
1323                  * it in one operation.  Thus, there's no need to lock
1324                  * around our tests.
1325                  */
1326                 elapsed = 0;
1327                 while ((rvp->v_count > 1) && (elapsed < ufs_mount_timeout)) {
1328                         delay(ufs_mount_error_delay * drv_usectohz(1000));
1329                         elapsed += ufs_mount_error_delay;
1330                 }
1331 
1332                 if (rvp->v_count > 1) {
1333                         mutex_enter(&rip->i_tlock);
1334                         rip->i_flag |= ISTALE;
1335                         mutex_exit(&rip->i_tlock);
1336                         cmn_err(CE_WARN,
1337                             "Timed out while cleaning up after "
1338                             "failed mount of %s", path);
1339                 } else {
1340 
1341                         /*
1342                          * Now we're the only one with a handle left, so tear
1343                          * it down the rest of the way.
1344                          */
1345                         if (ufs_rmidle(rip))
1346                                 VN_RELE(rvp);
1347                         ufs_si_del(rip);
1348                         rip->i_ufsvfs = NULL;
1349                         rvp->v_vfsp = NULL;
1350                         rvp->v_type = VBAD;
1351                         VN_RELE(rvp);
1352                 }
1353         }
1354         if (needtrans) {
1355                 TRANS_MATA_UMOUNT(ufsvfsp);
1356         }
1357         if (ufsvfsp) {
1358                 ufs_vfs_remove(ufsvfsp);
1359                 ufs_thread_exit(&ufsvfsp->vfs_delete);
1360                 ufs_thread_exit(&ufsvfsp->vfs_reclaim);
1361                 mutex_destroy(&ufsvfsp->vfs_lock);
1362                 if (ufsvfsp->vfs_log) {
1363                         lufs_unsnarf(ufsvfsp);
1364                 }
1365                 kmem_free(ufsvfsp, sizeof (struct ufsvfs));
1366         }
1367         if (bp) {
1368                 bp->b_flags |= (B_STALE|B_AGE);
1369                 brelse(bp);
1370         }
1371         if (tp) {
1372                 tp->b_flags |= (B_STALE|B_AGE);
1373                 brelse(tp);
1374         }
1375         if (needclose) {
1376                 (void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ?
1377                     FREAD : FREAD|FWRITE, 1, (offset_t)0, cr, NULL);
1378                 bflush(dev);
1379                 (void) bfinval(dev, 1);
1380         }
1381         return (error);
1382 }
1383 
1384 /*
1385  * vfs operations
1386  */
1387 static int
1388 ufs_unmount(struct vfs *vfsp, int fflag, struct cred *cr)
1389 {
1390         dev_t           dev             = vfsp->vfs_dev;
1391         struct ufsvfs   *ufsvfsp        = (struct ufsvfs *)vfsp->vfs_data;
1392         struct fs       *fs             = ufsvfsp->vfs_fs;
1393         struct ulockfs  *ulp            = &ufsvfsp->vfs_ulockfs;
1394         struct vnode    *bvp, *vp;
1395         struct buf      *bp;
1396         struct inode    *ip, *inext, *rip;
1397         union ihead     *ih;
1398         int             error, flag, i;
1399         struct lockfs   lockfs;
1400         int             poll_events = POLLPRI;
1401         extern struct pollhead ufs_pollhd;
1402         refstr_t        *mountpoint;
1403 
1404         ASSERT(vfs_lock_held(vfsp));
1405 
1406         if (secpolicy_fs_unmount(cr, vfsp) != 0)
1407                 return (EPERM);
1408         /*
1409          * Forced unmount is now supported through the
1410          * lockfs protocol.
1411          */
1412         if (fflag & MS_FORCE) {
1413                 /*
1414                  * Mark the filesystem as being unmounted now in
1415                  * case of a forcible umount before we take any
1416                  * locks inside UFS to prevent racing with a VFS_VGET()
1417                  * request. Throw these VFS_VGET() requests away for
1418                  * the duration of the forcible umount so they won't
1419                  * use stale or even freed data later on when we're done.
1420                  * It may happen that the VFS has had a additional hold
1421                  * placed on it by someone other than UFS and thus will
1422                  * not get freed immediately once we're done with the
1423                  * umount by dounmount() - use VFS_UNMOUNTED to inform
1424                  * users of this still-alive VFS that its corresponding
1425                  * filesystem being gone so they can detect that and error
1426                  * out.
1427                  */
1428                 vfsp->vfs_flag |= VFS_UNMOUNTED;
1429 
1430                 ufs_thread_suspend(&ufsvfsp->vfs_delete);
1431                 mutex_enter(&ulp->ul_lock);
1432                 /*
1433                  * If file system is already hard locked,
1434                  * unmount the file system, otherwise
1435                  * hard lock it before unmounting.
1436                  */
1437                 if (!ULOCKFS_IS_HLOCK(ulp)) {
1438                         atomic_add_long(&ufs_quiesce_pend, 1);
1439                         lockfs.lf_lock = LOCKFS_HLOCK;
1440                         lockfs.lf_flags = 0;
1441                         lockfs.lf_key = ulp->ul_lockfs.lf_key + 1;
1442                         lockfs.lf_comlen = 0;
1443                         lockfs.lf_comment = NULL;
1444                         ufs_freeze(ulp, &lockfs);
1445                         ULOCKFS_SET_BUSY(ulp);
1446                         LOCKFS_SET_BUSY(&ulp->ul_lockfs);
1447                         (void) ufs_quiesce(ulp);
1448                         (void) ufs_flush(vfsp);
1449                         (void) ufs_thaw(vfsp, ufsvfsp, ulp);
1450                         atomic_add_long(&ufs_quiesce_pend, -1);
1451                         ULOCKFS_CLR_BUSY(ulp);
1452                         LOCKFS_CLR_BUSY(&ulp->ul_lockfs);
1453                         poll_events |= POLLERR;
1454                         pollwakeup(&ufs_pollhd, poll_events);
1455                 }
1456                 ufs_thread_continue(&ufsvfsp->vfs_delete);
1457                 mutex_exit(&ulp->ul_lock);
1458         }
1459 
1460         /* let all types of writes go through */
1461         ufsvfsp->vfs_iotstamp = lbolt;
1462 
1463         /* coordinate with global hlock thread */
1464         if (TRANS_ISTRANS(ufsvfsp) && (ufsvfsp->vfs_validfs == UT_HLOCKING)) {
1465                 /*
1466                  * last possibility for a forced umount to fail hence clear
1467                  * VFS_UNMOUNTED if appropriate.
1468                  */
1469                 if (fflag & MS_FORCE)
1470                         vfsp->vfs_flag &= ~VFS_UNMOUNTED;
1471                 return (EAGAIN);
1472         }
1473 
1474         ufsvfsp->vfs_validfs = UT_UNMOUNTED;
1475 
1476         /* kill the reclaim thread */
1477         ufs_thread_exit(&ufsvfsp->vfs_reclaim);
1478 
1479         /* suspend the delete thread */
1480         ufs_thread_suspend(&ufsvfsp->vfs_delete);
1481 
1482         /*
1483          * drain the delete and idle queues
1484          */
1485         ufs_delete_drain(vfsp, -1, 1);
1486         ufs_idle_drain(vfsp);
1487 
1488         /*
1489          * use the lockfs protocol to prevent new ops from starting
1490          * a forcible umount can not fail beyond this point as
1491          * we hard-locked the filesystem and drained all current consumers
1492          * before.
1493          */
1494         mutex_enter(&ulp->ul_lock);
1495 
1496         /*
1497          * if the file system is busy; return EBUSY
1498          */
1499         if (ulp->ul_vnops_cnt || ulp->ul_falloc_cnt || ULOCKFS_IS_SLOCK(ulp)) {
1500                 error = EBUSY;
1501                 goto out;
1502         }
1503 
1504         /*
1505          * if this is not a forced unmount (!hard/error locked), then
1506          * get rid of every inode except the root and quota inodes
1507          * also, commit any outstanding transactions
1508          */
1509         if (!ULOCKFS_IS_HLOCK(ulp) && !ULOCKFS_IS_ELOCK(ulp))
1510                 if (error = ufs_flush(vfsp))
1511                         goto out;
1512 
1513         /*
1514          * ignore inodes in the cache if fs is hard locked or error locked
1515          */
1516         rip = VTOI(ufsvfsp->vfs_root);
1517         if (!ULOCKFS_IS_HLOCK(ulp) && !ULOCKFS_IS_ELOCK(ulp)) {
1518                 /*
1519                  * Otherwise, only the quota and root inodes are in the cache.
1520                  *
1521                  * Avoid racing with ufs_update() and ufs_sync().
1522                  */
1523                 mutex_enter(&ufs_scan_lock);
1524 
1525                 for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
1526                         mutex_enter(&ih_lock[i]);
1527                         for (ip = ih->ih_chain[0];
1528                             ip != (struct inode *)ih;
1529                             ip = ip->i_forw) {
1530                                 if (ip->i_ufsvfs != ufsvfsp)
1531                                         continue;
1532                                 if (ip == ufsvfsp->vfs_qinod)
1533                                         continue;
1534                                 if (ip == rip && ITOV(ip)->v_count == 1)
1535                                         continue;
1536                                 mutex_exit(&ih_lock[i]);
1537                                 mutex_exit(&ufs_scan_lock);
1538                                 error = EBUSY;
1539                                 goto out;
1540                         }
1541                         mutex_exit(&ih_lock[i]);
1542                 }
1543                 mutex_exit(&ufs_scan_lock);
1544         }
1545 
1546         /*
1547          * if a snapshot exists and this is a forced unmount, then delete
1548          * the snapshot.  Otherwise return EBUSY.  This will insure the
1549          * snapshot always belongs to a valid file system.
1550          */
1551         if (ufsvfsp->vfs_snapshot) {
1552                 if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp)) {
1553                         (void) fssnap_delete(&ufsvfsp->vfs_snapshot);
1554                 } else {
1555                         error = EBUSY;
1556                         goto out;
1557                 }
1558         }
1559 
1560         /*
1561          * Close the quota file and invalidate anything left in the quota
1562          * cache for this file system.  Pass kcred to allow all quota
1563          * manipulations.
1564          */
1565         (void) closedq(ufsvfsp, kcred);
1566         invalidatedq(ufsvfsp);
1567         /*
1568          * drain the delete and idle queues
1569          */
1570         ufs_delete_drain(vfsp, -1, 0);
1571         ufs_idle_drain(vfsp);
1572 
1573         /*
1574          * discard the inodes for this fs (including root, shadow, and quota)
1575          */
1576         for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
1577                 mutex_enter(&ih_lock[i]);
1578                 for (inext = 0, ip = ih->ih_chain[0];
1579                     ip != (struct inode *)ih;
1580                     ip = inext) {
1581                         inext = ip->i_forw;
1582                         if (ip->i_ufsvfs != ufsvfsp)
1583                                 continue;
1584 
1585                         /*
1586                          * We've found the inode in the cache and as we
1587                          * hold the hash mutex the inode can not
1588                          * disappear from underneath us.
1589                          * We also know it must have at least a vnode
1590                          * reference count of 1.
1591                          * We perform an additional VN_HOLD so the VN_RELE
1592                          * in case we take the inode off the idle queue
1593                          * can not be the last one.
1594                          * It is safe to grab the writer contents lock here
1595                          * to prevent a race with ufs_iinactive() putting
1596                          * inodes into the idle queue while we operate on
1597                          * this inode.
1598                          */
1599                         rw_enter(&ip->i_contents, RW_WRITER);
1600 
1601                         vp = ITOV(ip);
1602                         VN_HOLD(vp)
1603                         remque(ip);
1604                         if (ufs_rmidle(ip))
1605                                 VN_RELE(vp);
1606                         ufs_si_del(ip);
1607                         /*
1608                          * rip->i_ufsvfsp is needed by bflush()
1609                          */
1610                         if (ip != rip)
1611                                 ip->i_ufsvfs = NULL;
1612                         /*
1613                          * Set vnode's vfsops to dummy ops, which return
1614                          * EIO. This is needed to forced unmounts to work
1615                          * with lofs/nfs properly.
1616                          */
1617                         if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp))
1618                                 vp->v_vfsp = &EIO_vfs;
1619                         else
1620                                 vp->v_vfsp = NULL;
1621                         vp->v_type = VBAD;
1622 
1623                         rw_exit(&ip->i_contents);
1624 
1625                         VN_RELE(vp);
1626                 }
1627                 mutex_exit(&ih_lock[i]);
1628         }
1629         ufs_si_cache_flush(dev);
1630 
1631         /*
1632          * kill the delete thread and drain the idle queue
1633          */
1634         ufs_thread_exit(&ufsvfsp->vfs_delete);
1635         ufs_idle_drain(vfsp);
1636 
1637         bp = ufsvfsp->vfs_bufp;
1638         bvp = ufsvfsp->vfs_devvp;
1639         flag = !fs->fs_ronly;
1640         if (flag) {
1641                 bflush(dev);
1642                 if (fs->fs_clean != FSBAD) {
1643                         if (fs->fs_clean == FSSTABLE)
1644                                 fs->fs_clean = FSCLEAN;
1645                         fs->fs_reclaim &= ~FS_RECLAIM;
1646                 }
1647                 if (TRANS_ISTRANS(ufsvfsp) &&
1648                     !TRANS_ISERROR(ufsvfsp) &&
1649                     !ULOCKFS_IS_HLOCK(ulp) &&
1650                     (fs->fs_rolled == FS_NEED_ROLL)) {
1651                         /*
1652                          * ufs_flush() above has flushed the last Moby.
1653                          * This is needed to ensure the following superblock
1654                          * update really is the last metadata update
1655                          */
1656                         error = ufs_putsummaryinfo(dev, ufsvfsp, fs);
1657                         if (error == 0) {
1658                                 fs->fs_rolled = FS_ALL_ROLLED;
1659                         }
1660                 }
1661                 TRANS_SBUPDATE(ufsvfsp, vfsp, TOP_SBUPDATE_UNMOUNT);
1662                 /*
1663                  * push this last transaction
1664                  */
1665                 curthread->t_flag |= T_DONTBLOCK;
1666                 TRANS_BEGIN_SYNC(ufsvfsp, TOP_COMMIT_UNMOUNT, TOP_COMMIT_SIZE,
1667                     error);
1668                 if (!error)
1669                         TRANS_END_SYNC(ufsvfsp, error, TOP_COMMIT_UNMOUNT,
1670                             TOP_COMMIT_SIZE);
1671                 curthread->t_flag &= ~T_DONTBLOCK;
1672         }
1673 
1674         TRANS_MATA_UMOUNT(ufsvfsp);
1675         lufs_unsnarf(ufsvfsp);          /* Release the in-memory structs */
1676         ufsfx_unmount(ufsvfsp);         /* fix-on-panic bookkeeping */
1677         kmem_free(fs->fs_u.fs_csp, fs->fs_cssize);
1678 
1679         bp->b_flags |= B_STALE|B_AGE;
1680         ufsvfsp->vfs_bufp = NULL;    /* don't point at freed buf */
1681         brelse(bp);                     /* free the superblock buf */
1682 
1683         (void) VOP_PUTPAGE(common_specvp(bvp), (offset_t)0, (size_t)0,
1684             B_INVAL, cr, NULL);
1685         (void) VOP_CLOSE(bvp, flag, 1, (offset_t)0, cr, NULL);
1686         bflush(dev);
1687         (void) bfinval(dev, 1);
1688         VN_RELE(bvp);
1689 
1690         /*
1691          * It is now safe to NULL out the ufsvfs pointer and discard
1692          * the root inode.
1693          */
1694         rip->i_ufsvfs = NULL;
1695         VN_RELE(ITOV(rip));
1696 
1697         /* free up lockfs comment structure, if any */
1698         if (ulp->ul_lockfs.lf_comlen && ulp->ul_lockfs.lf_comment)
1699                 kmem_free(ulp->ul_lockfs.lf_comment, ulp->ul_lockfs.lf_comlen);
1700 
1701         /*
1702          * Remove from instance list.
1703          */
1704         ufs_vfs_remove(ufsvfsp);
1705 
1706         /*
1707          * For a forcible unmount, threads may be asleep in
1708          * ufs_lockfs_begin/ufs_check_lockfs.  These threads will need
1709          * the ufsvfs structure so we don't free it, yet.  ufs_update
1710          * will free it up after awhile.
1711          */
1712         if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp)) {
1713                 extern kmutex_t         ufsvfs_mutex;
1714                 extern struct ufsvfs    *ufsvfslist;
1715 
1716                 mutex_enter(&ufsvfs_mutex);
1717                 ufsvfsp->vfs_dontblock = 1;
1718                 ufsvfsp->vfs_next = ufsvfslist;
1719                 ufsvfslist = ufsvfsp;
1720                 mutex_exit(&ufsvfs_mutex);
1721                 /* wakeup any suspended threads */
1722                 cv_broadcast(&ulp->ul_cv);
1723                 mutex_exit(&ulp->ul_lock);
1724         } else {
1725                 mutex_destroy(&ufsvfsp->vfs_lock);
1726                 kmem_free(ufsvfsp, sizeof (struct ufsvfs));
1727         }
1728 
1729         /*
1730          * Now mark the filesystem as unmounted since we're done with it.
1731          */
1732         vfsp->vfs_flag |= VFS_UNMOUNTED;
1733 
1734         return (0);
1735 out:
1736         /* open the fs to new ops */
1737         cv_broadcast(&ulp->ul_cv);
1738         mutex_exit(&ulp->ul_lock);
1739 
1740         if (TRANS_ISTRANS(ufsvfsp)) {
1741                 /* allow the delete thread to continue */
1742                 ufs_thread_continue(&ufsvfsp->vfs_delete);
1743                 /* restart the reclaim thread */
1744                 ufs_thread_start(&ufsvfsp->vfs_reclaim, ufs_thread_reclaim,
1745                     vfsp);
1746                 /* coordinate with global hlock thread */
1747                 ufsvfsp->vfs_validfs = UT_MOUNTED;
1748                 /* check for trans errors during umount */
1749                 ufs_trans_onerror();
1750 
1751                 /*
1752                  * if we have a separate /usr it will never unmount
1753                  * when halting. In order to not re-read all the
1754                  * cylinder group summary info on mounting after
1755                  * reboot the logging of summary info is re-enabled
1756                  * and the super block written out.
1757                  */
1758                 mountpoint = vfs_getmntpoint(vfsp);
1759                 if ((fs->fs_si == FS_SI_OK) &&
1760                     (strcmp("/usr", refstr_value(mountpoint)) == 0)) {
1761                         ufsvfsp->vfs_nolog_si = 0;
1762                         UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
1763                 }
1764                 refstr_rele(mountpoint);
1765         }
1766 
1767         return (error);
1768 }
1769 
1770 static int
1771 ufs_root(struct vfs *vfsp, struct vnode **vpp)
1772 {
1773         struct ufsvfs *ufsvfsp;
1774         struct vnode *vp;
1775 
1776         if (!vfsp)
1777                 return (EIO);
1778 
1779         ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1780         if (!ufsvfsp || !ufsvfsp->vfs_root)
1781                 return (EIO);   /* forced unmount */
1782 
1783         vp = ufsvfsp->vfs_root;
1784         VN_HOLD(vp);
1785         *vpp = vp;
1786         return (0);
1787 }
1788 
1789 /*
1790  * Get file system statistics.
1791  */
1792 static int
1793 ufs_statvfs(struct vfs *vfsp, struct statvfs64 *sp)
1794 {
1795         struct fs *fsp;
1796         struct ufsvfs *ufsvfsp;
1797         int blk, i;
1798         long max_avail, used;
1799         dev32_t d32;
1800 
1801         if (vfsp->vfs_flag & VFS_UNMOUNTED)
1802                 return (EIO);
1803 
1804         ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1805         fsp = ufsvfsp->vfs_fs;
1806         if ((fsp->fs_magic != FS_MAGIC) && (fsp->fs_magic != MTB_UFS_MAGIC))
1807                 return (EINVAL);
1808         if (fsp->fs_magic == FS_MAGIC &&
1809             (fsp->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
1810             fsp->fs_version != UFS_VERSION_MIN))
1811                 return (EINVAL);
1812         if (fsp->fs_magic == MTB_UFS_MAGIC &&
1813             (fsp->fs_version > MTB_UFS_VERSION_1 ||
1814             fsp->fs_version < MTB_UFS_VERSION_MIN))
1815                 return (EINVAL);
1816 
1817         /*
1818          * get the basic numbers
1819          */
1820         (void) bzero(sp, sizeof (*sp));
1821 
1822         sp->f_bsize = fsp->fs_bsize;
1823         sp->f_frsize = fsp->fs_fsize;
1824         sp->f_blocks = (fsblkcnt64_t)fsp->fs_dsize;
1825         sp->f_bfree = (fsblkcnt64_t)fsp->fs_cstotal.cs_nbfree * fsp->fs_frag +
1826             fsp->fs_cstotal.cs_nffree;
1827 
1828         sp->f_files = (fsfilcnt64_t)fsp->fs_ncg * fsp->fs_ipg;
1829         sp->f_ffree = (fsfilcnt64_t)fsp->fs_cstotal.cs_nifree;
1830 
1831         /*
1832          * Adjust the numbers based on things waiting to be deleted.
1833          * modifies f_bfree and f_ffree.  Afterwards, everything we
1834          * come up with will be self-consistent.  By definition, this
1835          * is a point-in-time snapshot, so the fact that the delete
1836          * thread's probably already invalidated the results is not a
1837          * problem.  Note that if the delete thread is ever extended to
1838          * non-logging ufs, this adjustment must always be made.
1839          */
1840         if (TRANS_ISTRANS(ufsvfsp))
1841                 ufs_delete_adjust_stats(ufsvfsp, sp);
1842 
1843         /*
1844          * avail = MAX(max_avail - used, 0)
1845          */
1846         max_avail = fsp->fs_dsize - ufsvfsp->vfs_minfrags;
1847 
1848         used = (fsp->fs_dsize - sp->f_bfree);
1849 
1850         if (max_avail > used)
1851                 sp->f_bavail = (fsblkcnt64_t)max_avail - used;
1852         else
1853                 sp->f_bavail = (fsblkcnt64_t)0;
1854 
1855         sp->f_favail = sp->f_ffree;
1856         (void) cmpldev(&d32, vfsp->vfs_dev);
1857         sp->f_fsid = d32;
1858         (void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1859         sp->f_flag = vf_to_stf(vfsp->vfs_flag);
1860 
1861         /* keep coordinated with ufs_l_pathconf() */
1862         sp->f_namemax = MAXNAMLEN;
1863 
1864         if (fsp->fs_cpc == 0) {
1865                 bzero(sp->f_fstr, 14);
1866                 return (0);
1867         }
1868         blk = fsp->fs_spc * fsp->fs_cpc / NSPF(fsp);
1869         for (i = 0; i < blk; i += fsp->fs_frag) /* CSTYLED */
1870                 /* void */;
1871         i -= fsp->fs_frag;
1872         blk = i / fsp->fs_frag;
1873         bcopy(&(fs_rotbl(fsp)[blk]), sp->f_fstr, 14);
1874         return (0);
1875 }
1876 
1877 /*
1878  * Flush any pending I/O to file system vfsp.
1879  * The ufs_update() routine will only flush *all* ufs files.
1880  * If vfsp is non-NULL, only sync this ufs (in preparation
1881  * for a umount).
1882  */
1883 /*ARGSUSED*/
1884 static int
1885 ufs_sync(struct vfs *vfsp, short flag, struct cred *cr)
1886 {
1887         struct ufsvfs *ufsvfsp;
1888         struct fs *fs;
1889         int cheap = flag & SYNC_ATTR;
1890         int error;
1891 
1892         /*
1893          * SYNC_CLOSE means we're rebooting.  Toss everything
1894          * on the idle queue so we don't have to slog through
1895          * a bunch of uninteresting inodes over and over again.
1896          */
1897         if (flag & SYNC_CLOSE)
1898                 ufs_idle_drain(NULL);
1899 
1900         if (vfsp == NULL) {
1901                 ufs_update(flag);
1902                 return (0);
1903         }
1904 
1905         /* Flush a single ufs */
1906         if (!vfs_matchops(vfsp, ufs_vfsops) || vfs_lock(vfsp) != 0)
1907                 return (0);
1908 
1909         ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1910         if (!ufsvfsp)
1911                 return (EIO);
1912         fs = ufsvfsp->vfs_fs;
1913         mutex_enter(&ufsvfsp->vfs_lock);
1914 
1915         if (ufsvfsp->vfs_dio &&
1916             fs->fs_ronly == 0 &&
1917             fs->fs_clean != FSBAD &&
1918             fs->fs_clean != FSLOG) {
1919                 /* turn off fast-io on unmount, so no fsck needed (4029401) */
1920                 ufsvfsp->vfs_dio = 0;
1921                 fs->fs_clean = FSACTIVE;
1922                 fs->fs_fmod = 1;
1923         }
1924 
1925         /* Write back modified superblock */
1926         if (fs->fs_fmod == 0) {
1927                 mutex_exit(&ufsvfsp->vfs_lock);
1928         } else {
1929                 if (fs->fs_ronly != 0) {
1930                         mutex_exit(&ufsvfsp->vfs_lock);
1931                         vfs_unlock(vfsp);
1932                         return (ufs_fault(ufsvfsp->vfs_root,
1933                             "fs = %s update: ro fs mod\n", fs->fs_fsmnt));
1934                 }
1935                 fs->fs_fmod = 0;
1936                 mutex_exit(&ufsvfsp->vfs_lock);
1937 
1938                 TRANS_SBUPDATE(ufsvfsp, vfsp, TOP_SBUPDATE_UPDATE);
1939         }
1940         vfs_unlock(vfsp);
1941 
1942         /*
1943          * Avoid racing with ufs_update() and ufs_unmount().
1944          *
1945          */
1946         mutex_enter(&ufs_scan_lock);
1947 
1948         (void) ufs_scan_inodes(1, ufs_sync_inode,
1949             (void *)(uintptr_t)cheap, ufsvfsp);
1950 
1951         mutex_exit(&ufs_scan_lock);
1952 
1953         bflush((dev_t)vfsp->vfs_dev);
1954 
1955         /*
1956          * commit any outstanding async transactions
1957          */
1958         curthread->t_flag |= T_DONTBLOCK;
1959         TRANS_BEGIN_SYNC(ufsvfsp, TOP_COMMIT_UPDATE, TOP_COMMIT_SIZE, error);
1960         if (!error) {
1961                 TRANS_END_SYNC(ufsvfsp, error, TOP_COMMIT_UPDATE,
1962                     TOP_COMMIT_SIZE);
1963         }
1964         curthread->t_flag &= ~T_DONTBLOCK;
1965 
1966         return (0);
1967 }
1968 
1969 
1970 void
1971 sbupdate(struct vfs *vfsp)
1972 {
1973         struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1974         struct fs *fs = ufsvfsp->vfs_fs;
1975         struct buf *bp;
1976         int blks;
1977         caddr_t space;
1978         int i;
1979         size_t size;
1980 
1981         /*
1982          * for ulockfs processing, limit the superblock writes
1983          */
1984         if ((ufsvfsp->vfs_ulockfs.ul_sbowner) &&
1985             (curthread != ufsvfsp->vfs_ulockfs.ul_sbowner)) {
1986                 /* process later */
1987                 fs->fs_fmod = 1;
1988                 return;
1989         }
1990         ULOCKFS_SET_MOD((&ufsvfsp->vfs_ulockfs));
1991 
1992         if (TRANS_ISTRANS(ufsvfsp)) {
1993                 mutex_enter(&ufsvfsp->vfs_lock);
1994                 ufs_sbwrite(ufsvfsp);
1995                 mutex_exit(&ufsvfsp->vfs_lock);
1996                 return;
1997         }
1998 
1999         blks = howmany(fs->fs_cssize, fs->fs_fsize);
2000         space = (caddr_t)fs->fs_u.fs_csp;
2001         for (i = 0; i < blks; i += fs->fs_frag) {
2002                 size = fs->fs_bsize;
2003                 if (i + fs->fs_frag > blks)
2004                         size = (blks - i) * fs->fs_fsize;
2005                 bp = UFS_GETBLK(ufsvfsp, ufsvfsp->vfs_dev,
2006                     (daddr_t)(fsbtodb(fs, fs->fs_csaddr + i)),
2007                     fs->fs_bsize);
2008                 bcopy(space, bp->b_un.b_addr, size);
2009                 space += size;
2010                 bp->b_bcount = size;
2011                 UFS_BRWRITE(ufsvfsp, bp);
2012         }
2013         mutex_enter(&ufsvfsp->vfs_lock);
2014         ufs_sbwrite(ufsvfsp);
2015         mutex_exit(&ufsvfsp->vfs_lock);
2016 }
2017 
2018 int ufs_vget_idle_count = 2;    /* Number of inodes to idle each time */
2019 static int
2020 ufs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
2021 {
2022         int error = 0;
2023         struct ufid *ufid;
2024         struct inode *ip;
2025         struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
2026         struct ulockfs *ulp;
2027 
2028         /*
2029          * Check for unmounted filesystem.
2030          */
2031         if (vfsp->vfs_flag & VFS_UNMOUNTED) {
2032                 error = EIO;
2033                 goto errout;
2034         }
2035 
2036         /*
2037          * Keep the idle queue from getting too long by
2038          * idling an inode before attempting to allocate another.
2039          *    This operation must be performed before entering
2040          *    lockfs or a transaction.
2041          */
2042         if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
2043                 if ((curthread->t_flag & T_DONTBLOCK) == 0) {
2044                         ins.in_vidles.value.ul += ufs_vget_idle_count;
2045                         ufs_idle_some(ufs_vget_idle_count);
2046                 }
2047 
2048         ufid = (struct ufid *)fidp;
2049 
2050         if (error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_VGET_MASK))
2051                 goto errout;
2052 
2053         rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2054 
2055         error = ufs_iget(vfsp, ufid->ufid_ino, &ip, CRED());
2056 
2057         rw_exit(&ufsvfsp->vfs_dqrwlock);
2058 
2059         ufs_lockfs_end(ulp);
2060 
2061         if (error)
2062                 goto errout;
2063 
2064         /*
2065          * Check if the inode has been deleted or freed or is in transient state
2066          * since the last VFS_VGET() request for it, release it and don't return
2067          * it to the caller, presumably NFS, as it's no longer valid.
2068          */
2069         if (ip->i_gen != ufid->ufid_gen || ip->i_mode == 0 ||
2070             (ip->i_flag & IDEL)) {
2071                 VN_RELE(ITOV(ip));
2072                 error = EINVAL;
2073                 goto errout;
2074         }
2075 
2076         *vpp = ITOV(ip);
2077         return (0);
2078 
2079 errout:
2080         *vpp = NULL;
2081         return (error);
2082 }
2083 
2084 static int
2085 ufsinit(int fstype, char *name)
2086 {
2087         static const fs_operation_def_t ufs_vfsops_template[] = {
2088                 VFSNAME_MOUNT,          { .vfs_mount = ufs_mount },
2089                 VFSNAME_UNMOUNT,        { .vfs_unmount = ufs_unmount },
2090                 VFSNAME_ROOT,           { .vfs_root = ufs_root },
2091                 VFSNAME_STATVFS,        { .vfs_statvfs = ufs_statvfs },
2092                 VFSNAME_SYNC,           { .vfs_sync = ufs_sync },
2093                 VFSNAME_VGET,           { .vfs_vget = ufs_vget },
2094                 VFSNAME_MOUNTROOT,      { .vfs_mountroot = ufs_mountroot },
2095                 NULL,                   NULL
2096         };
2097         int error;
2098 
2099         ufsfstype = fstype;
2100 
2101         error = vfs_setfsops(fstype, ufs_vfsops_template, &ufs_vfsops);
2102         if (error != 0) {
2103                 cmn_err(CE_WARN, "ufsinit: bad vfs ops template");
2104                 return (error);
2105         }
2106 
2107         error = vn_make_ops(name, ufs_vnodeops_template, &ufs_vnodeops);
2108         if (error != 0) {
2109                 (void) vfs_freevfsops_by_type(fstype);
2110                 cmn_err(CE_WARN, "ufsinit: bad vnode ops template");
2111                 return (error);
2112         }
2113 
2114         ufs_iinit();
2115         return (0);
2116 }
2117 
2118 #ifdef __sparc
2119 
2120 /*
2121  * Mounting a mirrored SVM volume is only supported on ufs,
2122  * this is special-case boot code to support that configuration.
2123  * At this point, we have booted and mounted root on a
2124  * single component of the mirror.  Complete the boot
2125  * by configuring SVM and converting the root to the
2126  * dev_t of the mirrored root device.  This dev_t conversion
2127  * only works because the underlying device doesn't change.
2128  */
2129 int
2130 ufs_remountroot(struct vfs *vfsp)
2131 {
2132         struct ufsvfs *ufsvfsp;
2133         struct ulockfs *ulp;
2134         dev_t new_rootdev;
2135         dev_t old_rootdev;
2136         struct vnode *old_rootvp;
2137         struct vnode *new_rootvp;
2138         int error, sberror = 0;
2139         struct inode    *ip;
2140         union ihead     *ih;
2141         struct buf      *bp;
2142         int i;
2143 
2144         old_rootdev = rootdev;
2145         old_rootvp = rootvp;
2146 
2147         new_rootdev = getrootdev();
2148         if (new_rootdev == (dev_t)NODEV) {
2149                 return (ENODEV);
2150         }
2151 
2152         new_rootvp = makespecvp(new_rootdev, VBLK);
2153 
2154         error = VOP_OPEN(&new_rootvp,
2155             (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE, CRED(), NULL);
2156         if (error) {
2157                 cmn_err(CE_CONT,
2158                     "Cannot open mirrored root device, error %d\n", error);
2159                 return (error);
2160         }
2161 
2162         if (vfs_lock(vfsp) != 0) {
2163                 return (EBUSY);
2164         }
2165 
2166         ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
2167         ulp = &ufsvfsp->vfs_ulockfs;
2168 
2169         mutex_enter(&ulp->ul_lock);
2170         atomic_add_long(&ufs_quiesce_pend, 1);
2171 
2172         (void) ufs_quiesce(ulp);
2173         (void) ufs_flush(vfsp);
2174 
2175         /*
2176          * Convert root vfs to new dev_t, including vfs hash
2177          * table and fs id.
2178          */
2179         vfs_root_redev(vfsp, new_rootdev, ufsfstype);
2180 
2181         ufsvfsp->vfs_devvp = new_rootvp;
2182         ufsvfsp->vfs_dev = new_rootdev;
2183 
2184         bp = ufsvfsp->vfs_bufp;
2185         bp->b_edev = new_rootdev;
2186         bp->b_dev = cmpdev(new_rootdev);
2187 
2188         /*
2189          * The buffer for the root inode does not contain a valid b_vp
2190          */
2191         (void) bfinval(new_rootdev, 0);
2192 
2193         /*
2194          * Here we hand-craft inodes with old root device
2195          * references to refer to the new device instead.
2196          */
2197         mutex_enter(&ufs_scan_lock);
2198 
2199         for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
2200                 mutex_enter(&ih_lock[i]);
2201                 for (ip = ih->ih_chain[0];
2202                     ip != (struct inode *)ih;
2203                     ip = ip->i_forw) {
2204                         if (ip->i_ufsvfs != ufsvfsp)
2205                                 continue;
2206                         if (ip == ufsvfsp->vfs_qinod)
2207                                 continue;
2208                         if (ip->i_dev == old_rootdev) {
2209                                 ip->i_dev = new_rootdev;
2210                         }
2211 
2212                         if (ip->i_devvp == old_rootvp) {
2213                                 ip->i_devvp = new_rootvp;
2214                         }
2215                 }
2216                 mutex_exit(&ih_lock[i]);
2217         }
2218 
2219         mutex_exit(&ufs_scan_lock);
2220 
2221         /*
2222          * Make Sure logging structures are using the new device
2223          * if logging is enabled.  Also start any logging thread that
2224          * needs to write to the device and couldn't earlier.
2225          */
2226         if (ufsvfsp->vfs_log) {
2227                 buf_t           *bp, *tbp;
2228                 ml_unit_t       *ul = ufsvfsp->vfs_log;
2229                 struct fs       *fsp = ufsvfsp->vfs_fs;
2230 
2231                 /*
2232                  * Update the main logging structure.
2233                  */
2234                 ul->un_dev = new_rootdev;
2235 
2236                 /*
2237                  * Get a new bp for the on disk structures.
2238                  */
2239                 bp = ul->un_bp;
2240                 tbp = ngeteblk(dbtob(LS_SECTORS));
2241                 tbp->b_edev = new_rootdev;
2242                 tbp->b_dev = cmpdev(new_rootdev);
2243                 tbp->b_blkno = bp->b_blkno;
2244                 bcopy(bp->b_un.b_addr, tbp->b_un.b_addr, DEV_BSIZE);
2245                 bcopy(bp->b_un.b_addr, tbp->b_un.b_addr + DEV_BSIZE, DEV_BSIZE);
2246                 bp->b_flags |= (B_STALE | B_AGE);
2247                 brelse(bp);
2248                 ul->un_bp = tbp;
2249 
2250                 /*
2251                  * Allocate new circular buffers.
2252                  */
2253                 alloc_rdbuf(&ul->un_rdbuf, MAPBLOCKSIZE, MAPBLOCKSIZE);
2254                 alloc_wrbuf(&ul->un_wrbuf, ldl_bufsize(ul));
2255 
2256                 /*
2257                  * Clear the noroll bit which indicates that logging
2258                  * can't roll the log yet and start the logmap roll thread
2259                  * unless the filesystem is still read-only in which case
2260                  * remountfs() will do it when going to read-write.
2261                  */
2262                 ASSERT(ul->un_flags & LDL_NOROLL);
2263 
2264                 if (!fsp->fs_ronly) {
2265                         ul->un_flags &= ~LDL_NOROLL;
2266                         logmap_start_roll(ul);
2267                 }
2268 
2269                 /*
2270                  * Start the reclaim thread if needed.
2271                  */
2272                 if (!fsp->fs_ronly && (fsp->fs_reclaim &
2273                     (FS_RECLAIM|FS_RECLAIMING))) {
2274                         fsp->fs_reclaim &= ~FS_RECLAIM;
2275                         fsp->fs_reclaim |= FS_RECLAIMING;
2276                         ufs_thread_start(&ufsvfsp->vfs_reclaim,
2277                             ufs_thread_reclaim, vfsp);
2278                         TRANS_SBWRITE(ufsvfsp, TOP_SBUPDATE_UPDATE);
2279                         if (sberror = geterror(ufsvfsp->vfs_bufp)) {
2280                                 refstr_t        *mntpt;
2281                                 mntpt = vfs_getmntpoint(vfsp);
2282                                 cmn_err(CE_WARN,
2283                                     "Remountroot failed to update Reclaim"
2284                                     "state for filesystem %s "
2285                                     "Error writing SuperBlock %d",
2286                                     refstr_value(mntpt), error);
2287                                 refstr_rele(mntpt);
2288                         }
2289                 }
2290         }
2291 
2292         rootdev = new_rootdev;
2293         rootvp = new_rootvp;
2294 
2295         atomic_add_long(&ufs_quiesce_pend, -1);
2296         cv_broadcast(&ulp->ul_cv);
2297         mutex_exit(&ulp->ul_lock);
2298 
2299         vfs_unlock(vfsp);
2300 
2301         error = VOP_CLOSE(old_rootvp, FREAD, 1, (offset_t)0, CRED(), NULL);
2302         if (error) {
2303                 cmn_err(CE_CONT,
2304                     "close of root device component failed, error %d\n",
2305                     error);
2306         }
2307         VN_RELE(old_rootvp);
2308 
2309         return (sberror ? sberror : error);
2310 }
2311 
2312 #endif  /* __sparc */