xref: /freebsd/sys/ufs/ffs/ffs_vfsops.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*-
2  * Copyright (c) 1989, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_mac.h"
36 #include "opt_quota.h"
37 #include "opt_ufs.h"
38 #include "opt_ffs.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/proc.h>
44 #include <sys/kernel.h>
45 #include <sys/mac.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/conf.h>
51 #include <sys/fcntl.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54 
55 #include <ufs/ufs/extattr.h>
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/ufsmount.h>
58 #include <ufs/ufs/inode.h>
59 #include <ufs/ufs/ufs_extern.h>
60 
61 #include <ufs/ffs/fs.h>
62 #include <ufs/ffs/ffs_extern.h>
63 
64 #include <vm/vm.h>
65 #include <vm/uma.h>
66 #include <vm/vm_page.h>
67 
68 #include <geom/geom.h>
69 #include <geom/geom_vfs.h>
70 
71 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
72 
73 static int	ffs_sbupdate(struct ufsmount *, int);
74 static int	ffs_reload(struct mount *, struct thread *);
75 static int	ffs_mountfs(struct vnode *, struct mount *, struct thread *);
76 static void	ffs_oldfscompat_read(struct fs *, struct ufsmount *,
77 		    ufs2_daddr_t);
78 static void	ffs_oldfscompat_write(struct fs *, struct ufsmount *);
79 static void	ffs_ifree(struct ufsmount *ump, struct inode *ip);
80 static vfs_init_t ffs_init;
81 static vfs_uninit_t ffs_uninit;
82 static vfs_extattrctl_t ffs_extattrctl;
83 static vfs_cmount_t ffs_cmount;
84 static vfs_unmount_t ffs_unmount;
85 static vfs_mount_t ffs_mount;
86 static vfs_statfs_t ffs_statfs;
87 static vfs_fhtovp_t ffs_fhtovp;
88 static vfs_vptofh_t ffs_vptofh;
89 static vfs_sync_t ffs_sync;
90 
91 static struct vfsops ufs_vfsops = {
92 	.vfs_extattrctl =	ffs_extattrctl,
93 	.vfs_fhtovp =		ffs_fhtovp,
94 	.vfs_init =		ffs_init,
95 	.vfs_mount =		ffs_mount,
96 	.vfs_cmount =		ffs_cmount,
97 	.vfs_quotactl =		ufs_quotactl,
98 	.vfs_root =		ufs_root,
99 	.vfs_statfs =		ffs_statfs,
100 	.vfs_sync =		ffs_sync,
101 	.vfs_uninit =		ffs_uninit,
102 	.vfs_unmount =		ffs_unmount,
103 	.vfs_vget =		ffs_vget,
104 	.vfs_vptofh =		ffs_vptofh,
105 };
106 
107 VFS_SET(ufs_vfsops, ufs, 0);
108 
109 static b_strategy_t ffs_geom_strategy;
110 static b_write_t ffs_bufwrite;
111 
112 static struct buf_ops ffs_ops = {
113 	.bop_name =	"FFS",
114 	.bop_write =	ffs_bufwrite,
115 	.bop_strategy =	ffs_geom_strategy,
116 	.bop_sync =	bufsync,
117 };
118 
119 static const char *ffs_opts[] = { "from", "export", NULL };
120 
121 static int
122 ffs_mount(struct mount *mp, struct thread *td)
123 {
124 	struct vnode *devvp;
125 	struct ufsmount *ump = 0;
126 	struct fs *fs;
127 	int error, flags;
128 	mode_t accessmode;
129 	struct nameidata ndp;
130 	struct export_args export;
131 	char *fspec;
132 
133 	if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
134 		return (EINVAL);
135 	if (uma_inode == NULL) {
136 		uma_inode = uma_zcreate("FFS inode",
137 		    sizeof(struct inode), NULL, NULL, NULL, NULL,
138 		    UMA_ALIGN_PTR, 0);
139 		uma_ufs1 = uma_zcreate("FFS1 dinode",
140 		    sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
141 		    UMA_ALIGN_PTR, 0);
142 		uma_ufs2 = uma_zcreate("FFS2 dinode",
143 		    sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
144 		    UMA_ALIGN_PTR, 0);
145 	}
146 
147 	fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
148 	if (error)
149 		return (error);
150 
151 	/*
152 	 * If updating, check whether changing from read-only to
153 	 * read/write; if there is no device name, that's all we do.
154 	 */
155 	if (mp->mnt_flag & MNT_UPDATE) {
156 		ump = VFSTOUFS(mp);
157 		fs = ump->um_fs;
158 		devvp = ump->um_devvp;
159 		if (fs->fs_ronly == 0 &&
160 		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
161 			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
162 				return (error);
163 			/*
164 			 * Flush any dirty data.
165 			 */
166 			if ((error = ffs_sync(mp, MNT_WAIT, td)) != 0) {
167 				vn_finished_write(mp);
168 				return (error);
169 			}
170 			/*
171 			 * Check for and optionally get rid of files open
172 			 * for writing.
173 			 */
174 			flags = WRITECLOSE;
175 			if (mp->mnt_flag & MNT_FORCE)
176 				flags |= FORCECLOSE;
177 			if (mp->mnt_flag & MNT_SOFTDEP) {
178 				error = softdep_flushfiles(mp, flags, td);
179 			} else {
180 				error = ffs_flushfiles(mp, flags, td);
181 			}
182 			if (error) {
183 				vn_finished_write(mp);
184 				return (error);
185 			}
186 			if (fs->fs_pendingblocks != 0 ||
187 			    fs->fs_pendinginodes != 0) {
188 				printf("%s: %s: blocks %jd files %d\n",
189 				    fs->fs_fsmnt, "update error",
190 				    (intmax_t)fs->fs_pendingblocks,
191 				    fs->fs_pendinginodes);
192 				fs->fs_pendingblocks = 0;
193 				fs->fs_pendinginodes = 0;
194 			}
195 			if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
196 				fs->fs_clean = 1;
197 			if ((error = ffs_sbupdate(ump, MNT_WAIT)) != 0) {
198 				fs->fs_ronly = 0;
199 				fs->fs_clean = 0;
200 				vn_finished_write(mp);
201 				return (error);
202 			}
203 			vn_finished_write(mp);
204 			DROP_GIANT();
205 			g_topology_lock();
206 			g_access(ump->um_cp, 0, -1, 0);
207 			g_topology_unlock();
208 			PICKUP_GIANT();
209 			fs->fs_ronly = 1;
210 			mp->mnt_flag |= MNT_RDONLY;
211 		}
212 		if ((mp->mnt_flag & MNT_RELOAD) &&
213 		    (error = ffs_reload(mp, td)) != 0)
214 			return (error);
215 		if (fs->fs_ronly &&
216 		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
217 			/*
218 			 * If upgrade to read-write by non-root, then verify
219 			 * that user has necessary permissions on the device.
220 			 */
221 			if (suser(td)) {
222 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
223 				if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
224 				    td->td_ucred, td)) != 0) {
225 					VOP_UNLOCK(devvp, 0, td);
226 					return (error);
227 				}
228 				VOP_UNLOCK(devvp, 0, td);
229 			}
230 			fs->fs_flags &= ~FS_UNCLEAN;
231 			if (fs->fs_clean == 0) {
232 				fs->fs_flags |= FS_UNCLEAN;
233 				if ((mp->mnt_flag & MNT_FORCE) ||
234 				    ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
235 				     (fs->fs_flags & FS_DOSOFTDEP))) {
236 					printf("WARNING: %s was not %s\n",
237 					   fs->fs_fsmnt, "properly dismounted");
238 				} else {
239 					printf(
240 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
241 					    fs->fs_fsmnt);
242 					return (EPERM);
243 				}
244 			}
245 			DROP_GIANT();
246 			g_topology_lock();
247 			/*
248 			 * If we're the root device, we may not have an E count
249 			 * yet, get it now.
250 			 */
251 			if (ump->um_cp->ace == 0)
252 				error = g_access(ump->um_cp, 0, 1, 1);
253 			else
254 				error = g_access(ump->um_cp, 0, 1, 0);
255 			g_topology_unlock();
256 			PICKUP_GIANT();
257 			if (error)
258 				return (error);
259 			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
260 				return (error);
261 			fs->fs_ronly = 0;
262 			mp->mnt_flag &= ~MNT_RDONLY;
263 			fs->fs_clean = 0;
264 			if ((error = ffs_sbupdate(ump, MNT_WAIT)) != 0) {
265 				vn_finished_write(mp);
266 				return (error);
267 			}
268 			/* check to see if we need to start softdep */
269 			if ((fs->fs_flags & FS_DOSOFTDEP) &&
270 			    (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
271 				vn_finished_write(mp);
272 				return (error);
273 			}
274 			if (fs->fs_snapinum[0] != 0)
275 				ffs_snapshot_mount(mp);
276 			vn_finished_write(mp);
277 		}
278 		/*
279 		 * Soft updates is incompatible with "async",
280 		 * so if we are doing softupdates stop the user
281 		 * from setting the async flag in an update.
282 		 * Softdep_mount() clears it in an initial mount
283 		 * or ro->rw remount.
284 		 */
285 		if (mp->mnt_flag & MNT_SOFTDEP)
286 			mp->mnt_flag &= ~MNT_ASYNC;
287 		/*
288 		 * Keep MNT_ACLS flag if it is stored in superblock.
289 		 */
290 		if ((fs->fs_flags & FS_ACLS) != 0)
291 			mp->mnt_flag |= MNT_ACLS;
292 		/*
293 		 * If not updating name, process export requests.
294 		 */
295 		error = vfs_copyopt(mp->mnt_optnew, "export", &export, sizeof export);
296 		if (error == 0 && export.ex_flags != 0)
297 			return (vfs_export(mp, &export));
298 		/*
299 		 * If this is a snapshot request, take the snapshot.
300 		 */
301 		if (mp->mnt_flag & MNT_SNAPSHOT)
302 			return (ffs_snapshot(mp, fspec));
303 	}
304 
305 	/*
306 	 * Not an update, or updating the name: look up the name
307 	 * and verify that it refers to a sensible disk device.
308 	 */
309 	NDINIT(&ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
310 	if ((error = namei(&ndp)) != 0)
311 		return (error);
312 	NDFREE(&ndp, NDF_ONLY_PNBUF);
313 	devvp = ndp.ni_vp;
314 	if (!vn_isdisk(devvp, &error)) {
315 		vrele(devvp);
316 		return (error);
317 	}
318 
319 	/*
320 	 * If mount by non-root, then verify that user has necessary
321 	 * permissions on the device.
322 	 */
323 	if (suser(td)) {
324 		accessmode = VREAD;
325 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
326 			accessmode |= VWRITE;
327 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
328 		if ((error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td))!= 0){
329 			vput(devvp);
330 			return (error);
331 		}
332 		VOP_UNLOCK(devvp, 0, td);
333 	}
334 
335 	if (mp->mnt_flag & MNT_UPDATE) {
336 		/*
337 		 * Update only
338 		 *
339 		 * If it's not the same vnode, or at least the same device
340 		 * then it's not correct.
341 		 */
342 
343 		if (devvp->v_rdev != ump->um_devvp->v_rdev)
344 			error = EINVAL;	/* needs translation */
345 		vrele(devvp);
346 		if (error)
347 			return (error);
348 	} else {
349 		/*
350 		 * New mount
351 		 *
352 		 * We need the name for the mount point (also used for
353 		 * "last mounted on") copied in. If an error occurs,
354 		 * the mount point is discarded by the upper level code.
355 		 * Note that vfs_mount() populates f_mntonname for us.
356 		 */
357 		if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
358 			vrele(devvp);
359 			return (error);
360 		}
361 	}
362 	vfs_mountedfrom(mp, fspec);
363 	return (0);
364 }
365 
366 /*
367  * Compatibility with old mount system call.
368  */
369 
370 static int
371 ffs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
372 {
373 	struct ufs_args args;
374 	int error;
375 
376 	if (data == NULL)
377 		return (EINVAL);
378 	error = copyin(data, &args, sizeof args);
379 	if (error)
380 		return (error);
381 
382 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
383 	ma = mount_arg(ma, "export", &args.export, sizeof args.export);
384 	error = kernel_mount(ma, flags);
385 
386 	return (error);
387 }
388 
389 /*
390  * Reload all incore data for a filesystem (used after running fsck on
391  * the root filesystem and finding things to fix). The filesystem must
392  * be mounted read-only.
393  *
394  * Things to do to update the mount:
395  *	1) invalidate all cached meta-data.
396  *	2) re-read superblock from disk.
397  *	3) re-read summary information from disk.
398  *	4) invalidate all inactive vnodes.
399  *	5) invalidate all cached file data.
400  *	6) re-read inode data for all active vnodes.
401  */
402 static int
403 ffs_reload(struct mount *mp, struct thread *td)
404 {
405 	struct vnode *vp, *nvp, *devvp;
406 	struct inode *ip;
407 	void *space;
408 	struct buf *bp;
409 	struct fs *fs, *newfs;
410 	struct ufsmount *ump;
411 	ufs2_daddr_t sblockloc;
412 	int i, blks, size, error;
413 	int32_t *lp;
414 
415 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
416 		return (EINVAL);
417 	ump = VFSTOUFS(mp);
418 	/*
419 	 * Step 1: invalidate all cached meta-data.
420 	 */
421 	devvp = VFSTOUFS(mp)->um_devvp;
422 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
423 	if (vinvalbuf(devvp, 0, td, 0, 0) != 0)
424 		panic("ffs_reload: dirty1");
425 	VOP_UNLOCK(devvp, 0, td);
426 
427 	/*
428 	 * Step 2: re-read superblock from disk.
429 	 */
430 	fs = VFSTOUFS(mp)->um_fs;
431 	if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
432 	    NOCRED, &bp)) != 0)
433 		return (error);
434 	newfs = (struct fs *)bp->b_data;
435 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
436 	     newfs->fs_magic != FS_UFS2_MAGIC) ||
437 	    newfs->fs_bsize > MAXBSIZE ||
438 	    newfs->fs_bsize < sizeof(struct fs)) {
439 			brelse(bp);
440 			return (EIO);		/* XXX needs translation */
441 	}
442 	/*
443 	 * Copy pointer fields back into superblock before copying in	XXX
444 	 * new superblock. These should really be in the ufsmount.	XXX
445 	 * Note that important parameters (eg fs_ncg) are unchanged.
446 	 */
447 	newfs->fs_csp = fs->fs_csp;
448 	newfs->fs_maxcluster = fs->fs_maxcluster;
449 	newfs->fs_contigdirs = fs->fs_contigdirs;
450 	newfs->fs_active = fs->fs_active;
451 	/* The file system is still read-only. */
452 	newfs->fs_ronly = 1;
453 	sblockloc = fs->fs_sblockloc;
454 	bcopy(newfs, fs, (u_int)fs->fs_sbsize);
455 	brelse(bp);
456 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
457 	ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
458 	UFS_LOCK(ump);
459 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
460 		printf("%s: reload pending error: blocks %jd files %d\n",
461 		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
462 		    fs->fs_pendinginodes);
463 		fs->fs_pendingblocks = 0;
464 		fs->fs_pendinginodes = 0;
465 	}
466 	UFS_UNLOCK(ump);
467 
468 	/*
469 	 * Step 3: re-read summary information from disk.
470 	 */
471 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
472 	space = fs->fs_csp;
473 	for (i = 0; i < blks; i += fs->fs_frag) {
474 		size = fs->fs_bsize;
475 		if (i + fs->fs_frag > blks)
476 			size = (blks - i) * fs->fs_fsize;
477 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
478 		    NOCRED, &bp);
479 		if (error)
480 			return (error);
481 		bcopy(bp->b_data, space, (u_int)size);
482 		space = (char *)space + size;
483 		brelse(bp);
484 	}
485 	/*
486 	 * We no longer know anything about clusters per cylinder group.
487 	 */
488 	if (fs->fs_contigsumsize > 0) {
489 		lp = fs->fs_maxcluster;
490 		for (i = 0; i < fs->fs_ncg; i++)
491 			*lp++ = fs->fs_contigsumsize;
492 	}
493 
494 loop:
495 	MNT_ILOCK(mp);
496 	MNT_VNODE_FOREACH(vp, mp, nvp) {
497 		VI_LOCK(vp);
498 		if (vp->v_iflag & VI_XLOCK) {
499 			VI_UNLOCK(vp);
500 			continue;
501 		}
502 		MNT_IUNLOCK(mp);
503 		/*
504 		 * Step 4: invalidate all inactive vnodes.
505 		 */
506 		if (vp->v_usecount == 0) {
507 			vgonel(vp, td);
508 			goto loop;
509 		}
510 		/*
511 		 * Step 5: invalidate all cached file data.
512 		 */
513 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
514 			goto loop;
515 		}
516 		if (vinvalbuf(vp, 0, td, 0, 0))
517 			panic("ffs_reload: dirty2");
518 		/*
519 		 * Step 6: re-read inode data for all active vnodes.
520 		 */
521 		ip = VTOI(vp);
522 		error =
523 		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
524 		    (int)fs->fs_bsize, NOCRED, &bp);
525 		if (error) {
526 			VOP_UNLOCK(vp, 0, td);
527 			vrele(vp);
528 			return (error);
529 		}
530 		ffs_load_inode(bp, ip, fs, ip->i_number);
531 		ip->i_effnlink = ip->i_nlink;
532 		brelse(bp);
533 		VOP_UNLOCK(vp, 0, td);
534 		vrele(vp);
535 		MNT_ILOCK(mp);
536 	}
537 	MNT_IUNLOCK(mp);
538 	return (0);
539 }
540 
541 /*
542  * Possible superblock locations ordered from most to least likely.
543  */
544 static int sblock_try[] = SBLOCKSEARCH;
545 
546 /*
547  * Common code for mount and mountroot
548  */
549 static int
550 ffs_mountfs(devvp, mp, td)
551 	struct vnode *devvp;
552 	struct mount *mp;
553 	struct thread *td;
554 {
555 	struct ufsmount *ump;
556 	struct buf *bp;
557 	struct fs *fs;
558 	struct cdev *dev;
559 	void *space;
560 	ufs2_daddr_t sblockloc;
561 	int error, i, blks, size, ronly;
562 	int32_t *lp;
563 	struct ucred *cred;
564 	struct g_consumer *cp;
565 
566 	dev = devvp->v_rdev;
567 	cred = td ? td->td_ucred : NOCRED;
568 
569 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
570 	DROP_GIANT();
571 	g_topology_lock();
572 	error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
573 
574 	/*
575 	 * If we are a root mount, drop the E flag so fsck can do its magic.
576 	 * We will pick it up again when we remount R/W.
577 	 */
578 	if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
579 		error = g_access(cp, 0, 0, -1);
580 	g_topology_unlock();
581 	PICKUP_GIANT();
582 	VOP_UNLOCK(devvp, 0, td);
583 	if (error)
584 		return (error);
585 	if (devvp->v_rdev->si_iosize_max != 0)
586 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
587 	if (mp->mnt_iosize_max > MAXPHYS)
588 		mp->mnt_iosize_max = MAXPHYS;
589 
590 	devvp->v_bufobj.bo_private = cp;
591 	devvp->v_bufobj.bo_ops = &ffs_ops;
592 
593 	bp = NULL;
594 	ump = NULL;
595 	fs = NULL;
596 	sblockloc = 0;
597 	/*
598 	 * Try reading the superblock in each of its possible locations.
599 	 */
600 	for (i = 0; sblock_try[i] != -1; i++) {
601 		if ((error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
602 		    cred, &bp)) != 0)
603 			goto out;
604 		fs = (struct fs *)bp->b_data;
605 		sblockloc = sblock_try[i];
606 		if ((fs->fs_magic == FS_UFS1_MAGIC ||
607 		     (fs->fs_magic == FS_UFS2_MAGIC &&
608 		      (fs->fs_sblockloc == sblockloc ||
609 		       (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
610 		    fs->fs_bsize <= MAXBSIZE &&
611 		    fs->fs_bsize >= sizeof(struct fs))
612 			break;
613 		brelse(bp);
614 		bp = NULL;
615 	}
616 	if (sblock_try[i] == -1) {
617 		error = EINVAL;		/* XXX needs translation */
618 		goto out;
619 	}
620 	fs->fs_fmod = 0;
621 	fs->fs_flags &= ~FS_INDEXDIRS;	/* no support for directory indicies */
622 	fs->fs_flags &= ~FS_UNCLEAN;
623 	if (fs->fs_clean == 0) {
624 		fs->fs_flags |= FS_UNCLEAN;
625 		if (ronly || (mp->mnt_flag & MNT_FORCE) ||
626 		    ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
627 		     (fs->fs_flags & FS_DOSOFTDEP))) {
628 			printf(
629 "WARNING: %s was not properly dismounted\n",
630 			    fs->fs_fsmnt);
631 		} else {
632 			printf(
633 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
634 			    fs->fs_fsmnt);
635 			error = EPERM;
636 			goto out;
637 		}
638 		if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
639 		    (mp->mnt_flag & MNT_FORCE)) {
640 			printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt,
641 			    (intmax_t)fs->fs_pendingblocks,
642 			    fs->fs_pendinginodes);
643 			fs->fs_pendingblocks = 0;
644 			fs->fs_pendinginodes = 0;
645 		}
646 	}
647 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
648 		printf("%s: mount pending error: blocks %jd files %d\n",
649 		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
650 		    fs->fs_pendinginodes);
651 		fs->fs_pendingblocks = 0;
652 		fs->fs_pendinginodes = 0;
653 	}
654 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
655 	ump->um_cp = cp;
656 	ump->um_bo = &devvp->v_bufobj;
657 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
658 	if (fs->fs_magic == FS_UFS1_MAGIC) {
659 		ump->um_fstype = UFS1;
660 		ump->um_balloc = ffs_balloc_ufs1;
661 	} else {
662 		ump->um_fstype = UFS2;
663 		ump->um_balloc = ffs_balloc_ufs2;
664 	}
665 	ump->um_blkatoff = ffs_blkatoff;
666 	ump->um_truncate = ffs_truncate;
667 	ump->um_update = ffs_update;
668 	ump->um_valloc = ffs_valloc;
669 	ump->um_vfree = ffs_vfree;
670 	ump->um_ifree = ffs_ifree;
671 	mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
672 	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
673 	if (fs->fs_sbsize < SBLOCKSIZE)
674 		bp->b_flags |= B_INVAL | B_NOCACHE;
675 	brelse(bp);
676 	bp = NULL;
677 	fs = ump->um_fs;
678 	ffs_oldfscompat_read(fs, ump, sblockloc);
679 	fs->fs_ronly = ronly;
680 	size = fs->fs_cssize;
681 	blks = howmany(size, fs->fs_fsize);
682 	if (fs->fs_contigsumsize > 0)
683 		size += fs->fs_ncg * sizeof(int32_t);
684 	size += fs->fs_ncg * sizeof(u_int8_t);
685 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
686 	fs->fs_csp = space;
687 	for (i = 0; i < blks; i += fs->fs_frag) {
688 		size = fs->fs_bsize;
689 		if (i + fs->fs_frag > blks)
690 			size = (blks - i) * fs->fs_fsize;
691 		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
692 		    cred, &bp)) != 0) {
693 			free(fs->fs_csp, M_UFSMNT);
694 			goto out;
695 		}
696 		bcopy(bp->b_data, space, (u_int)size);
697 		space = (char *)space + size;
698 		brelse(bp);
699 		bp = NULL;
700 	}
701 	if (fs->fs_contigsumsize > 0) {
702 		fs->fs_maxcluster = lp = space;
703 		for (i = 0; i < fs->fs_ncg; i++)
704 			*lp++ = fs->fs_contigsumsize;
705 		space = lp;
706 	}
707 	size = fs->fs_ncg * sizeof(u_int8_t);
708 	fs->fs_contigdirs = (u_int8_t *)space;
709 	bzero(fs->fs_contigdirs, size);
710 	fs->fs_active = NULL;
711 	mp->mnt_data = (qaddr_t)ump;
712 	mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
713 	mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
714 	if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
715 	    vfs_getvfs(&mp->mnt_stat.f_fsid))
716 		vfs_getnewfsid(mp);
717 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
718 	mp->mnt_flag |= MNT_LOCAL;
719 	if ((fs->fs_flags & FS_MULTILABEL) != 0)
720 #ifdef MAC
721 		mp->mnt_flag |= MNT_MULTILABEL;
722 #else
723 		printf(
724 "WARNING: %s: multilabel flag on fs but no MAC support\n",
725 		    fs->fs_fsmnt);
726 #endif
727 	if ((fs->fs_flags & FS_ACLS) != 0)
728 #ifdef UFS_ACL
729 		mp->mnt_flag |= MNT_ACLS;
730 #else
731 		printf(
732 "WARNING: %s: ACLs flag on fs but no ACLs support\n",
733 		    fs->fs_fsmnt);
734 #endif
735 	ump->um_mountp = mp;
736 	ump->um_dev = dev;
737 	ump->um_devvp = devvp;
738 	ump->um_nindir = fs->fs_nindir;
739 	ump->um_bptrtodb = fs->fs_fsbtodb;
740 	ump->um_seqinc = fs->fs_frag;
741 	for (i = 0; i < MAXQUOTAS; i++)
742 		ump->um_quotas[i] = NULLVP;
743 #ifdef UFS_EXTATTR
744 	ufs_extattr_uepm_init(&ump->um_extattr);
745 #endif
746 	/*
747 	 * Set FS local "last mounted on" information (NULL pad)
748 	 */
749 	vfs_mountedfrom(mp, fs->fs_fsmnt);
750 
751 	if( mp->mnt_flag & MNT_ROOTFS) {
752 		/*
753 		 * Root mount; update timestamp in mount structure.
754 		 * this will be used by the common root mount code
755 		 * to update the system clock.
756 		 */
757 		mp->mnt_time = fs->fs_time;
758 	}
759 
760 	if (ronly == 0) {
761 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
762 		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
763 			free(fs->fs_csp, M_UFSMNT);
764 			goto out;
765 		}
766 		if (fs->fs_snapinum[0] != 0)
767 			ffs_snapshot_mount(mp);
768 		fs->fs_fmod = 1;
769 		fs->fs_clean = 0;
770 		(void) ffs_sbupdate(ump, MNT_WAIT);
771 	}
772 	/*
773 	 * Initialize filesystem stat information in mount struct.
774 	 */
775 #ifdef UFS_EXTATTR
776 #ifdef UFS_EXTATTR_AUTOSTART
777 	/*
778 	 *
779 	 * Auto-starting does the following:
780 	 *	- check for /.attribute in the fs, and extattr_start if so
781 	 *	- for each file in .attribute, enable that file with
782 	 * 	  an attribute of the same name.
783 	 * Not clear how to report errors -- probably eat them.
784 	 * This would all happen while the filesystem was busy/not
785 	 * available, so would effectively be "atomic".
786 	 */
787 	(void) ufs_extattr_autostart(mp, td);
788 #endif /* !UFS_EXTATTR_AUTOSTART */
789 #endif /* !UFS_EXTATTR */
790 #ifndef QUOTA
791 	mp->mnt_kern_flag |= MNTK_MPSAFE;
792 #endif
793 	return (0);
794 out:
795 	if (bp)
796 		brelse(bp);
797 	vinvalbuf(devvp, V_SAVE, td, 0, 0);
798 	if (cp != NULL) {
799 		DROP_GIANT();
800 		g_topology_lock();
801 		g_vfs_close(cp, td);
802 		g_topology_unlock();
803 		PICKUP_GIANT();
804 	}
805 	if (ump) {
806 		mtx_destroy(UFS_MTX(ump));
807 		free(ump->um_fs, M_UFSMNT);
808 		free(ump, M_UFSMNT);
809 		mp->mnt_data = (qaddr_t)0;
810 	}
811 	return (error);
812 }
813 
814 #include <sys/sysctl.h>
815 static int bigcgs = 0;
816 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
817 
818 /*
819  * Sanity checks for loading old filesystem superblocks.
820  * See ffs_oldfscompat_write below for unwound actions.
821  *
822  * XXX - Parts get retired eventually.
823  * Unfortunately new bits get added.
824  */
825 static void
826 ffs_oldfscompat_read(fs, ump, sblockloc)
827 	struct fs *fs;
828 	struct ufsmount *ump;
829 	ufs2_daddr_t sblockloc;
830 {
831 	off_t maxfilesize;
832 
833 	/*
834 	 * If not yet done, update fs_flags location and value of fs_sblockloc.
835 	 */
836 	if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
837 		fs->fs_flags = fs->fs_old_flags;
838 		fs->fs_old_flags |= FS_FLAGS_UPDATED;
839 		fs->fs_sblockloc = sblockloc;
840 	}
841 	/*
842 	 * If not yet done, update UFS1 superblock with new wider fields.
843 	 */
844 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
845 		fs->fs_maxbsize = fs->fs_bsize;
846 		fs->fs_time = fs->fs_old_time;
847 		fs->fs_size = fs->fs_old_size;
848 		fs->fs_dsize = fs->fs_old_dsize;
849 		fs->fs_csaddr = fs->fs_old_csaddr;
850 		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
851 		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
852 		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
853 		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
854 	}
855 	if (fs->fs_magic == FS_UFS1_MAGIC &&
856 	    fs->fs_old_inodefmt < FS_44INODEFMT) {
857 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
858 		fs->fs_qbmask = ~fs->fs_bmask;
859 		fs->fs_qfmask = ~fs->fs_fmask;
860 	}
861 	if (fs->fs_magic == FS_UFS1_MAGIC) {
862 		ump->um_savedmaxfilesize = fs->fs_maxfilesize;
863 		maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1;
864 		if (fs->fs_maxfilesize > maxfilesize)
865 			fs->fs_maxfilesize = maxfilesize;
866 	}
867 	/* Compatibility for old filesystems */
868 	if (fs->fs_avgfilesize <= 0)
869 		fs->fs_avgfilesize = AVFILESIZ;
870 	if (fs->fs_avgfpdir <= 0)
871 		fs->fs_avgfpdir = AFPDIR;
872 	if (bigcgs) {
873 		fs->fs_save_cgsize = fs->fs_cgsize;
874 		fs->fs_cgsize = fs->fs_bsize;
875 	}
876 }
877 
878 /*
879  * Unwinding superblock updates for old filesystems.
880  * See ffs_oldfscompat_read above for details.
881  *
882  * XXX - Parts get retired eventually.
883  * Unfortunately new bits get added.
884  */
885 static void
886 ffs_oldfscompat_write(fs, ump)
887 	struct fs *fs;
888 	struct ufsmount *ump;
889 {
890 
891 	/*
892 	 * Copy back UFS2 updated fields that UFS1 inspects.
893 	 */
894 	if (fs->fs_magic == FS_UFS1_MAGIC) {
895 		fs->fs_old_time = fs->fs_time;
896 		fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
897 		fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
898 		fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
899 		fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
900 		fs->fs_maxfilesize = ump->um_savedmaxfilesize;
901 	}
902 	if (bigcgs) {
903 		fs->fs_cgsize = fs->fs_save_cgsize;
904 		fs->fs_save_cgsize = 0;
905 	}
906 }
907 
908 /*
909  * unmount system call
910  */
911 static int
912 ffs_unmount(mp, mntflags, td)
913 	struct mount *mp;
914 	int mntflags;
915 	struct thread *td;
916 {
917 	struct ufsmount *ump = VFSTOUFS(mp);
918 	struct fs *fs;
919 	int error, flags;
920 
921 	flags = 0;
922 	if (mntflags & MNT_FORCE) {
923 		flags |= FORCECLOSE;
924 	}
925 #ifdef UFS_EXTATTR
926 	if ((error = ufs_extattr_stop(mp, td))) {
927 		if (error != EOPNOTSUPP)
928 			printf("ffs_unmount: ufs_extattr_stop returned %d\n",
929 			    error);
930 	} else {
931 		ufs_extattr_uepm_destroy(&ump->um_extattr);
932 	}
933 #endif
934 	if (mp->mnt_flag & MNT_SOFTDEP) {
935 		if ((error = softdep_flushfiles(mp, flags, td)) != 0)
936 			return (error);
937 	} else {
938 		if ((error = ffs_flushfiles(mp, flags, td)) != 0)
939 			return (error);
940 	}
941 	fs = ump->um_fs;
942 	UFS_LOCK(ump);
943 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
944 		printf("%s: unmount pending error: blocks %jd files %d\n",
945 		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
946 		    fs->fs_pendinginodes);
947 		fs->fs_pendingblocks = 0;
948 		fs->fs_pendinginodes = 0;
949 	}
950 	UFS_UNLOCK(ump);
951 	if (fs->fs_ronly == 0) {
952 		fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
953 		error = ffs_sbupdate(ump, MNT_WAIT);
954 		if (error) {
955 			fs->fs_clean = 0;
956 			return (error);
957 		}
958 	}
959 	vinvalbuf(ump->um_devvp, V_SAVE, td, 0, 0);
960 	DROP_GIANT();
961 	g_topology_lock();
962 	g_vfs_close(ump->um_cp, td);
963 	g_topology_unlock();
964 	PICKUP_GIANT();
965 	vrele(ump->um_devvp);
966 	mtx_destroy(UFS_MTX(ump));
967 	free(fs->fs_csp, M_UFSMNT);
968 	free(fs, M_UFSMNT);
969 	free(ump, M_UFSMNT);
970 	mp->mnt_data = (qaddr_t)0;
971 	mp->mnt_flag &= ~MNT_LOCAL;
972 	return (error);
973 }
974 
975 /*
976  * Flush out all the files in a filesystem.
977  */
978 int
979 ffs_flushfiles(mp, flags, td)
980 	struct mount *mp;
981 	int flags;
982 	struct thread *td;
983 {
984 	struct ufsmount *ump;
985 	int error;
986 
987 	ump = VFSTOUFS(mp);
988 #ifdef QUOTA
989 	if (mp->mnt_flag & MNT_QUOTA) {
990 		int i;
991 		error = vflush(mp, 0, SKIPSYSTEM|flags, td);
992 		if (error)
993 			return (error);
994 		for (i = 0; i < MAXQUOTAS; i++) {
995 			if (ump->um_quotas[i] == NULLVP)
996 				continue;
997 			quotaoff(td, mp, i);
998 		}
999 		/*
1000 		 * Here we fall through to vflush again to ensure
1001 		 * that we have gotten rid of all the system vnodes.
1002 		 */
1003 	}
1004 #endif
1005 	ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1006 	if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1007 		if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1008 			return (error);
1009 		ffs_snapshot_unmount(mp);
1010 		/*
1011 		 * Here we fall through to vflush again to ensure
1012 		 * that we have gotten rid of all the system vnodes.
1013 		 */
1014 	}
1015         /*
1016 	 * Flush all the files.
1017 	 */
1018 	if ((error = vflush(mp, 0, flags, td)) != 0)
1019 		return (error);
1020 	/*
1021 	 * Flush filesystem metadata.
1022 	 */
1023 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
1024 	error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1025 	VOP_UNLOCK(ump->um_devvp, 0, td);
1026 	return (error);
1027 }
1028 
1029 /*
1030  * Get filesystem statistics.
1031  */
1032 static int
1033 ffs_statfs(mp, sbp, td)
1034 	struct mount *mp;
1035 	struct statfs *sbp;
1036 	struct thread *td;
1037 {
1038 	struct ufsmount *ump;
1039 	struct fs *fs;
1040 
1041 	ump = VFSTOUFS(mp);
1042 	fs = ump->um_fs;
1043 	if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1044 		panic("ffs_statfs");
1045 	sbp->f_version = STATFS_VERSION;
1046 	sbp->f_bsize = fs->fs_fsize;
1047 	sbp->f_iosize = fs->fs_bsize;
1048 	sbp->f_blocks = fs->fs_dsize;
1049 	UFS_LOCK(ump);
1050 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1051 	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1052 	sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1053 	    dbtofsb(fs, fs->fs_pendingblocks);
1054 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
1055 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1056 	UFS_UNLOCK(ump);
1057 	sbp->f_namemax = NAME_MAX;
1058 	return (0);
1059 }
1060 
1061 /*
1062  * Go through the disk queues to initiate sandbagged IO;
1063  * go through the inodes to write those that have been modified;
1064  * initiate the writing of the super block if it has been modified.
1065  *
1066  * Note: we are always called with the filesystem marked `MPBUSY'.
1067  */
1068 static int
1069 ffs_sync(mp, waitfor, td)
1070 	struct mount *mp;
1071 	int waitfor;
1072 	struct thread *td;
1073 {
1074 	struct vnode *nvp, *vp, *devvp;
1075 	struct inode *ip;
1076 	struct ufsmount *ump = VFSTOUFS(mp);
1077 	struct fs *fs;
1078 	int error, count, wait, lockreq, allerror = 0;
1079 	struct bufobj *bo;
1080 
1081 	fs = ump->um_fs;
1082 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
1083 		printf("fs = %s\n", fs->fs_fsmnt);
1084 		panic("ffs_sync: rofs mod");
1085 	}
1086 	/*
1087 	 * Write back each (modified) inode.
1088 	 */
1089 	wait = 0;
1090 	lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1091 	if (waitfor == MNT_WAIT) {
1092 		wait = 1;
1093 		lockreq = LK_EXCLUSIVE;
1094 	}
1095 	lockreq |= LK_INTERLOCK;
1096 	MNT_ILOCK(mp);
1097 loop:
1098 	MNT_VNODE_FOREACH(vp, mp, nvp) {
1099 		/*
1100 		 * Depend on the mntvnode_slock to keep things stable enough
1101 		 * for a quick test.  Since there might be hundreds of
1102 		 * thousands of vnodes, we cannot afford even a subroutine
1103 		 * call unless there's a good chance that we have work to do.
1104 		 */
1105 		VI_LOCK(vp);
1106 		if (vp->v_iflag & VI_XLOCK) {
1107 			VI_UNLOCK(vp);
1108 			continue;
1109 		}
1110 		ip = VTOI(vp);
1111 		if (vp->v_type == VNON || ((ip->i_flag &
1112 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1113 		    vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1114 			VI_UNLOCK(vp);
1115 			continue;
1116 		}
1117 		MNT_IUNLOCK(mp);
1118 		if ((error = vget(vp, lockreq, td)) != 0) {
1119 			MNT_ILOCK(mp);
1120 			if (error == ENOENT)
1121 				goto loop;
1122 			continue;
1123 		}
1124 		if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1125 			allerror = error;
1126 		VOP_UNLOCK(vp, 0, td);
1127 		vrele(vp);
1128 		MNT_ILOCK(mp);
1129 	}
1130 	MNT_IUNLOCK(mp);
1131 	/*
1132 	 * Force stale filesystem control information to be flushed.
1133 	 */
1134 	if (waitfor == MNT_WAIT) {
1135 		if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1136 			allerror = error;
1137 		/* Flushed work items may create new vnodes to clean */
1138 		if (allerror == 0 && count) {
1139 			MNT_ILOCK(mp);
1140 			goto loop;
1141 		}
1142 	}
1143 #ifdef QUOTA
1144 	qsync(mp);
1145 #endif
1146 	devvp = ump->um_devvp;
1147 	VI_LOCK(devvp);
1148 	bo = &devvp->v_bufobj;
1149 	if (waitfor != MNT_LAZY &&
1150 	    (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1151 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td);
1152 		if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1153 			allerror = error;
1154 		VOP_UNLOCK(devvp, 0, td);
1155 		if (allerror == 0 && waitfor == MNT_WAIT) {
1156 			MNT_ILOCK(mp);
1157 			goto loop;
1158 		}
1159 	} else
1160 		VI_UNLOCK(devvp);
1161 	/*
1162 	 * Write back modified superblock.
1163 	 */
1164 	if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1165 		allerror = error;
1166 	return (allerror);
1167 }
1168 
1169 int
1170 ffs_vget(mp, ino, flags, vpp)
1171 	struct mount *mp;
1172 	ino_t ino;
1173 	int flags;
1174 	struct vnode **vpp;
1175 {
1176 	struct thread *td = curthread; 		/* XXX */
1177 	struct fs *fs;
1178 	struct inode *ip;
1179 	struct ufsmount *ump;
1180 	struct buf *bp;
1181 	struct vnode *vp;
1182 	struct cdev *dev;
1183 	int error;
1184 
1185 	ump = VFSTOUFS(mp);
1186 	dev = ump->um_dev;
1187 	fs = ump->um_fs;
1188 
1189 	/*
1190 	 * We do not lock vnode creation as it is believed to be too
1191 	 * expensive for such rare case as simultaneous creation of vnode
1192 	 * for same ino by different processes. We just allow them to race
1193 	 * and check later to decide who wins. Let the race begin!
1194 	 */
1195 	if ((error = ufs_ihashget(dev, ino, flags, vpp)) != 0)
1196 		return (error);
1197 	if (*vpp != NULL)
1198 		return (0);
1199 
1200 	/*
1201 	 * If this MALLOC() is performed after the getnewvnode()
1202 	 * it might block, leaving a vnode with a NULL v_data to be
1203 	 * found by ffs_sync() if a sync happens to fire right then,
1204 	 * which will cause a panic because ffs_sync() blindly
1205 	 * dereferences vp->v_data (as well it should).
1206 	 */
1207 	ip = uma_zalloc(uma_inode, M_WAITOK);
1208 
1209 	/* Allocate a new vnode/inode. */
1210 	if (fs->fs_magic == FS_UFS1_MAGIC)
1211 		error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1212 	else
1213 		error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1214 	if (error) {
1215 		*vpp = NULL;
1216 		uma_zfree(uma_inode, ip);
1217 		return (error);
1218 	}
1219 	bzero((caddr_t)ip, sizeof(struct inode));
1220 	/*
1221 	 * FFS supports recursive locking.
1222 	 */
1223 	vp->v_vnlock->lk_flags |= LK_CANRECURSE;
1224 	vp->v_data = ip;
1225 	vp->v_bufobj.bo_bsize = fs->fs_bsize;
1226 	ip->i_vnode = vp;
1227 	ip->i_ump = ump;
1228 	ip->i_fs = fs;
1229 	ip->i_dev = dev;
1230 	ip->i_number = ino;
1231 #ifdef QUOTA
1232 	{
1233 		int i;
1234 		for (i = 0; i < MAXQUOTAS; i++)
1235 			ip->i_dquot[i] = NODQUOT;
1236 	}
1237 #endif
1238 	/*
1239 	 * Exclusively lock the vnode before adding to hash. Note, that we
1240 	 * must not release nor downgrade the lock (despite flags argument
1241 	 * says) till it is fully initialized.
1242 	 */
1243 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, (struct mtx *)0, td);
1244 
1245 	/*
1246 	 * Atomicaly (in terms of ufs_hash operations) check the hash for
1247 	 * duplicate of vnode being created and add it to the hash. If a
1248 	 * duplicate vnode was found, it will be vget()ed from hash for us.
1249 	 */
1250 	if ((error = ufs_ihashins(ip, flags, vpp)) != 0) {
1251 		vput(vp);
1252 		*vpp = NULL;
1253 		return (error);
1254 	}
1255 
1256 	/* We lost the race, then throw away our vnode and return existing */
1257 	if (*vpp != NULL) {
1258 		vput(vp);
1259 		return (0);
1260 	}
1261 
1262 	/* Read in the disk contents for the inode, copy into the inode. */
1263 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1264 	    (int)fs->fs_bsize, NOCRED, &bp);
1265 	if (error) {
1266 		/*
1267 		 * The inode does not contain anything useful, so it would
1268 		 * be misleading to leave it on its hash chain. With mode
1269 		 * still zero, it will be unlinked and returned to the free
1270 		 * list by vput().
1271 		 */
1272 		brelse(bp);
1273 		vput(vp);
1274 		*vpp = NULL;
1275 		return (error);
1276 	}
1277 	if (ip->i_ump->um_fstype == UFS1)
1278 		ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1279 	else
1280 		ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1281 	ffs_load_inode(bp, ip, fs, ino);
1282 	if (DOINGSOFTDEP(vp))
1283 		softdep_load_inodeblock(ip);
1284 	else
1285 		ip->i_effnlink = ip->i_nlink;
1286 	bqrelse(bp);
1287 
1288 	/*
1289 	 * Initialize the vnode from the inode, check for aliases.
1290 	 * Note that the underlying vnode may have changed.
1291 	 */
1292 	if (ip->i_ump->um_fstype == UFS1)
1293 		error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1294 	else
1295 		error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1296 	if (error) {
1297 		vput(vp);
1298 		*vpp = NULL;
1299 		return (error);
1300 	}
1301 	/*
1302 	 * Finish inode initialization.
1303 	 */
1304 	VREF(ip->i_devvp);
1305 	/*
1306 	 * Set up a generation number for this inode if it does not
1307 	 * already have one. This should only happen on old filesystems.
1308 	 */
1309 	if (ip->i_gen == 0) {
1310 		ip->i_gen = arc4random() / 2 + 1;
1311 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1312 			ip->i_flag |= IN_MODIFIED;
1313 			DIP_SET(ip, i_gen, ip->i_gen);
1314 		}
1315 	}
1316 	/*
1317 	 * Ensure that uid and gid are correct. This is a temporary
1318 	 * fix until fsck has been changed to do the update.
1319 	 */
1320 	if (fs->fs_magic == FS_UFS1_MAGIC &&		/* XXX */
1321 	    fs->fs_old_inodefmt < FS_44INODEFMT) {	/* XXX */
1322 		ip->i_uid = ip->i_din1->di_ouid;	/* XXX */
1323 		ip->i_gid = ip->i_din1->di_ogid;	/* XXX */
1324 	}						/* XXX */
1325 
1326 #ifdef MAC
1327 	if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1328 		/*
1329 		 * If this vnode is already allocated, and we're running
1330 		 * multi-label, attempt to perform a label association
1331 		 * from the extended attributes on the inode.
1332 		 */
1333 		error = mac_associate_vnode_extattr(mp, vp);
1334 		if (error) {
1335 			/* ufs_inactive will release ip->i_devvp ref. */
1336 			vput(vp);
1337 			*vpp = NULL;
1338 			return (error);
1339 		}
1340 	}
1341 #endif
1342 
1343 	*vpp = vp;
1344 	return (0);
1345 }
1346 
1347 /*
1348  * File handle to vnode
1349  *
1350  * Have to be really careful about stale file handles:
1351  * - check that the inode number is valid
1352  * - call ffs_vget() to get the locked inode
1353  * - check for an unallocated inode (i_mode == 0)
1354  * - check that the given client host has export rights and return
1355  *   those rights via. exflagsp and credanonp
1356  */
1357 static int
1358 ffs_fhtovp(mp, fhp, vpp)
1359 	struct mount *mp;
1360 	struct fid *fhp;
1361 	struct vnode **vpp;
1362 {
1363 	struct ufid *ufhp;
1364 	struct fs *fs;
1365 
1366 	ufhp = (struct ufid *)fhp;
1367 	fs = VFSTOUFS(mp)->um_fs;
1368 	if (ufhp->ufid_ino < ROOTINO ||
1369 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1370 		return (ESTALE);
1371 	return (ufs_fhtovp(mp, ufhp, vpp));
1372 }
1373 
1374 /*
1375  * Vnode pointer to File handle
1376  */
1377 /* ARGSUSED */
1378 static int
1379 ffs_vptofh(vp, fhp)
1380 	struct vnode *vp;
1381 	struct fid *fhp;
1382 {
1383 	struct inode *ip;
1384 	struct ufid *ufhp;
1385 
1386 	ip = VTOI(vp);
1387 	ufhp = (struct ufid *)fhp;
1388 	ufhp->ufid_len = sizeof(struct ufid);
1389 	ufhp->ufid_ino = ip->i_number;
1390 	ufhp->ufid_gen = ip->i_gen;
1391 	return (0);
1392 }
1393 
1394 /*
1395  * Initialize the filesystem.
1396  */
1397 static int
1398 ffs_init(vfsp)
1399 	struct vfsconf *vfsp;
1400 {
1401 
1402 	softdep_initialize();
1403 	return (ufs_init(vfsp));
1404 }
1405 
1406 /*
1407  * Undo the work of ffs_init().
1408  */
1409 static int
1410 ffs_uninit(vfsp)
1411 	struct vfsconf *vfsp;
1412 {
1413 	int ret;
1414 
1415 	ret = ufs_uninit(vfsp);
1416 	softdep_uninitialize();
1417 	return (ret);
1418 }
1419 
1420 /*
1421  * Write a superblock and associated information back to disk.
1422  */
1423 static int
1424 ffs_sbupdate(mp, waitfor)
1425 	struct ufsmount *mp;
1426 	int waitfor;
1427 {
1428 	struct fs *fs = mp->um_fs;
1429 	struct buf *sbbp;
1430 	struct buf *bp;
1431 	int blks;
1432 	void *space;
1433 	int i, size, error, allerror = 0;
1434 
1435 	if (fs->fs_ronly == 1 &&
1436 	    (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1437 	    (MNT_RDONLY | MNT_UPDATE))
1438 		panic("ffs_sbupdate: write read-only filesystem");
1439 	/*
1440 	 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1441 	 */
1442 	sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1443 	    0, 0, 0);
1444 	/*
1445 	 * First write back the summary information.
1446 	 */
1447 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1448 	space = fs->fs_csp;
1449 	for (i = 0; i < blks; i += fs->fs_frag) {
1450 		size = fs->fs_bsize;
1451 		if (i + fs->fs_frag > blks)
1452 			size = (blks - i) * fs->fs_fsize;
1453 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1454 		    size, 0, 0, 0);
1455 		bcopy(space, bp->b_data, (u_int)size);
1456 		space = (char *)space + size;
1457 		if (waitfor != MNT_WAIT)
1458 			bawrite(bp);
1459 		else if ((error = bwrite(bp)) != 0)
1460 			allerror = error;
1461 	}
1462 	/*
1463 	 * Now write back the superblock itself. If any errors occurred
1464 	 * up to this point, then fail so that the superblock avoids
1465 	 * being written out as clean.
1466 	 */
1467 	if (allerror) {
1468 		brelse(sbbp);
1469 		return (allerror);
1470 	}
1471 	bp = sbbp;
1472 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1473 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1474 		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1475 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1476 		fs->fs_sblockloc = SBLOCK_UFS1;
1477 	}
1478 	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1479 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1480 		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1481 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1482 		fs->fs_sblockloc = SBLOCK_UFS2;
1483 	}
1484 	fs->fs_fmod = 0;
1485 	fs->fs_time = time_second;
1486 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1487 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1488 	if (waitfor != MNT_WAIT)
1489 		bawrite(bp);
1490 	else if ((error = bwrite(bp)) != 0)
1491 		allerror = error;
1492 	return (allerror);
1493 }
1494 
1495 static int
1496 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1497 	int attrnamespace, const char *attrname, struct thread *td)
1498 {
1499 
1500 #ifdef UFS_EXTATTR
1501 	return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1502 	    attrname, td));
1503 #else
1504 	return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1505 	    attrname, td));
1506 #endif
1507 }
1508 
1509 static void
1510 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1511 {
1512 
1513 	if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1514 		uma_zfree(uma_ufs1, ip->i_din1);
1515 	else if (ip->i_din2 != NULL)
1516 		uma_zfree(uma_ufs2, ip->i_din2);
1517 	uma_zfree(uma_inode, ip);
1518 }
1519 
1520 static int dobkgrdwrite = 1;
1521 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1522     "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1523 
1524 /*
1525  * Complete a background write started from bwrite.
1526  */
1527 static void
1528 ffs_backgroundwritedone(struct buf *bp)
1529 {
1530 	struct buf *origbp;
1531 
1532 	/*
1533 	 * Find the original buffer that we are writing.
1534 	 */
1535 	BO_LOCK(bp->b_bufobj);
1536 	if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1537 		panic("backgroundwritedone: lost buffer");
1538 	BO_UNLOCK(bp->b_bufobj);
1539 	/*
1540 	 * Process dependencies then return any unfinished ones.
1541 	 */
1542 	if (LIST_FIRST(&bp->b_dep) != NULL)
1543 		buf_complete(bp);
1544 #ifdef SOFTUPDATES
1545 	if (LIST_FIRST(&bp->b_dep) != NULL)
1546 		softdep_move_dependencies(bp, origbp);
1547 #endif
1548 
1549 	/*
1550 	 * This buffer is marked B_NOCACHE, so when it is released
1551 	 * by biodone, it will be tossed. We mark it with BIO_READ
1552 	 * to avoid biodone doing a second bufobj_wdrop.
1553 	 */
1554 	bp->b_flags |= B_NOCACHE;
1555 	bp->b_iocmd = BIO_READ;
1556 	bp->b_flags &= ~(B_CACHE | B_DONE);
1557 	bp->b_iodone = 0;
1558 	bufdone(bp);
1559 	BO_LOCK(origbp->b_bufobj);
1560 	/*
1561 	 * Clear the BV_BKGRDINPROG flag in the original buffer
1562 	 * and awaken it if it is waiting for the write to complete.
1563 	 * If BV_BKGRDINPROG is not set in the original buffer it must
1564 	 * have been released and re-instantiated - which is not legal.
1565 	 */
1566 	KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1567 	    ("backgroundwritedone: lost buffer2"));
1568 	origbp->b_vflags &= ~BV_BKGRDINPROG;
1569 	if (origbp->b_vflags & BV_BKGRDWAIT) {
1570 		origbp->b_vflags &= ~BV_BKGRDWAIT;
1571 		wakeup(&origbp->b_xflags);
1572 	}
1573 	BO_UNLOCK(origbp->b_bufobj);
1574 }
1575 
1576 
1577 /*
1578  * Write, release buffer on completion.  (Done by iodone
1579  * if async).  Do not bother writing anything if the buffer
1580  * is invalid.
1581  *
1582  * Note that we set B_CACHE here, indicating that buffer is
1583  * fully valid and thus cacheable.  This is true even of NFS
1584  * now so we set it generally.  This could be set either here
1585  * or in biodone() since the I/O is synchronous.  We put it
1586  * here.
1587  */
1588 static int
1589 ffs_bufwrite(struct buf *bp)
1590 {
1591 	int oldflags, s;
1592 	struct buf *newbp;
1593 
1594 	CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1595 	if (bp->b_flags & B_INVAL) {
1596 		brelse(bp);
1597 		return (0);
1598 	}
1599 
1600 	oldflags = bp->b_flags;
1601 
1602 	if (BUF_REFCNT(bp) == 0)
1603 		panic("bufwrite: buffer is not busy???");
1604 	s = splbio();
1605 	/*
1606 	 * If a background write is already in progress, delay
1607 	 * writing this block if it is asynchronous. Otherwise
1608 	 * wait for the background write to complete.
1609 	 */
1610 	BO_LOCK(bp->b_bufobj);
1611 	if (bp->b_vflags & BV_BKGRDINPROG) {
1612 		if (bp->b_flags & B_ASYNC) {
1613 			BO_UNLOCK(bp->b_bufobj);
1614 			splx(s);
1615 			bdwrite(bp);
1616 			return (0);
1617 		}
1618 		bp->b_vflags |= BV_BKGRDWAIT;
1619 		msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1620 		if (bp->b_vflags & BV_BKGRDINPROG)
1621 			panic("bufwrite: still writing");
1622 	}
1623 	BO_UNLOCK(bp->b_bufobj);
1624 
1625 	/* Mark the buffer clean */
1626 	bundirty(bp);
1627 
1628 	/*
1629 	 * If this buffer is marked for background writing and we
1630 	 * do not have to wait for it, make a copy and write the
1631 	 * copy so as to leave this buffer ready for further use.
1632 	 *
1633 	 * This optimization eats a lot of memory.  If we have a page
1634 	 * or buffer shortfall we can't do it.
1635 	 */
1636 	if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1637 	    (bp->b_flags & B_ASYNC) &&
1638 	    !vm_page_count_severe() &&
1639 	    !buf_dirty_count_severe()) {
1640 		KASSERT(bp->b_iodone == NULL,
1641 		    ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1642 
1643 		/* get a new block */
1644 		newbp = geteblk(bp->b_bufsize);
1645 
1646 		/*
1647 		 * set it to be identical to the old block.  We have to
1648 		 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1649 		 * to avoid confusing the splay tree and gbincore().
1650 		 */
1651 		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1652 		newbp->b_lblkno = bp->b_lblkno;
1653 		newbp->b_xflags |= BX_BKGRDMARKER;
1654 		BO_LOCK(bp->b_bufobj);
1655 		bp->b_vflags |= BV_BKGRDINPROG;
1656 		bgetvp(bp->b_vp, newbp);
1657 		BO_UNLOCK(bp->b_bufobj);
1658 		newbp->b_bufobj = &bp->b_vp->v_bufobj;
1659 		newbp->b_blkno = bp->b_blkno;
1660 		newbp->b_offset = bp->b_offset;
1661 		newbp->b_iodone = ffs_backgroundwritedone;
1662 		newbp->b_flags |= B_ASYNC;
1663 		newbp->b_flags &= ~B_INVAL;
1664 
1665 #ifdef SOFTUPDATES
1666 		/* move over the dependencies */
1667 		if (LIST_FIRST(&bp->b_dep) != NULL)
1668 			softdep_move_dependencies(bp, newbp);
1669 #endif
1670 
1671 		/*
1672 		 * Initiate write on the copy, release the original to
1673 		 * the B_LOCKED queue so that it cannot go away until
1674 		 * the background write completes. If not locked it could go
1675 		 * away and then be reconstituted while it was being written.
1676 		 * If the reconstituted buffer were written, we could end up
1677 		 * with two background copies being written at the same time.
1678 		 */
1679 		bqrelse(bp);
1680 		bp = newbp;
1681 	}
1682 
1683 	/* Let the normal bufwrite do the rest for us */
1684 	bufwrite(bp);
1685 
1686 	return (0);
1687 }
1688 
1689 
1690 static void
1691 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1692 {
1693 
1694 #ifdef SOFTUPDATES
1695 	if (bp->b_iocmd == BIO_WRITE && softdep_disk_prewrite(bp))
1696 		return;
1697 #endif
1698 	g_vfs_strategy(bo, bp);
1699 }
1700