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