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