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