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