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