xref: /freebsd/sys/ufs/ffs/ffs_vfsops.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
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 | LOCKLEAF, 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 		vput(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 		if ((error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td))!= 0){
328 			vput(devvp);
329 			return (error);
330 		}
331 	}
332 
333 	if (mp->mnt_flag & MNT_UPDATE) {
334 		/*
335 		 * Update only
336 		 *
337 		 * If it's not the same vnode, or at least the same device
338 		 * then it's not correct.
339 		 */
340 
341 		if (devvp->v_rdev != ump->um_devvp->v_rdev)
342 			error = EINVAL;	/* needs translation */
343 		vput(devvp);
344 		if (error)
345 			return (error);
346 	} else {
347 		/*
348 		 * New mount
349 		 *
350 		 * We need the name for the mount point (also used for
351 		 * "last mounted on") copied in. If an error occurs,
352 		 * the mount point is discarded by the upper level code.
353 		 * Note that vfs_mount() populates f_mntonname for us.
354 		 */
355 		if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
356 			vrele(devvp);
357 			return (error);
358 		}
359 	}
360 	vfs_mountedfrom(mp, fspec);
361 	return (0);
362 }
363 
364 /*
365  * Compatibility with old mount system call.
366  */
367 
368 static int
369 ffs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
370 {
371 	struct ufs_args args;
372 	int error;
373 
374 	if (data == NULL)
375 		return (EINVAL);
376 	error = copyin(data, &args, sizeof args);
377 	if (error)
378 		return (error);
379 
380 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
381 	ma = mount_arg(ma, "export", &args.export, sizeof args.export);
382 	error = kernel_mount(ma, flags);
383 
384 	return (error);
385 }
386 
387 /*
388  * Reload all incore data for a filesystem (used after running fsck on
389  * the root filesystem and finding things to fix). The filesystem must
390  * be mounted read-only.
391  *
392  * Things to do to update the mount:
393  *	1) invalidate all cached meta-data.
394  *	2) re-read superblock from disk.
395  *	3) re-read summary information from disk.
396  *	4) invalidate all inactive vnodes.
397  *	5) invalidate all cached file data.
398  *	6) re-read inode data for all active vnodes.
399  */
400 static int
401 ffs_reload(struct mount *mp, struct thread *td)
402 {
403 	struct vnode *vp, *nvp, *devvp;
404 	struct inode *ip;
405 	void *space;
406 	struct buf *bp;
407 	struct fs *fs, *newfs;
408 	struct ufsmount *ump;
409 	ufs2_daddr_t sblockloc;
410 	int i, blks, size, error;
411 	int32_t *lp;
412 
413 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
414 		return (EINVAL);
415 	ump = VFSTOUFS(mp);
416 	/*
417 	 * Step 1: invalidate all cached meta-data.
418 	 */
419 	devvp = VFSTOUFS(mp)->um_devvp;
420 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
421 	if (vinvalbuf(devvp, 0, td, 0, 0) != 0)
422 		panic("ffs_reload: dirty1");
423 	VOP_UNLOCK(devvp, 0, td);
424 
425 	/*
426 	 * Step 2: re-read superblock from disk.
427 	 */
428 	fs = VFSTOUFS(mp)->um_fs;
429 	if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
430 	    NOCRED, &bp)) != 0)
431 		return (error);
432 	newfs = (struct fs *)bp->b_data;
433 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
434 	     newfs->fs_magic != FS_UFS2_MAGIC) ||
435 	    newfs->fs_bsize > MAXBSIZE ||
436 	    newfs->fs_bsize < sizeof(struct fs)) {
437 			brelse(bp);
438 			return (EIO);		/* XXX needs translation */
439 	}
440 	/*
441 	 * Copy pointer fields back into superblock before copying in	XXX
442 	 * new superblock. These should really be in the ufsmount.	XXX
443 	 * Note that important parameters (eg fs_ncg) are unchanged.
444 	 */
445 	newfs->fs_csp = fs->fs_csp;
446 	newfs->fs_maxcluster = fs->fs_maxcluster;
447 	newfs->fs_contigdirs = fs->fs_contigdirs;
448 	newfs->fs_active = fs->fs_active;
449 	/* The file system is still read-only. */
450 	newfs->fs_ronly = 1;
451 	sblockloc = fs->fs_sblockloc;
452 	bcopy(newfs, fs, (u_int)fs->fs_sbsize);
453 	brelse(bp);
454 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
455 	ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
456 	UFS_LOCK(ump);
457 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
458 		printf("%s: reload pending error: blocks %jd files %d\n",
459 		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
460 		    fs->fs_pendinginodes);
461 		fs->fs_pendingblocks = 0;
462 		fs->fs_pendinginodes = 0;
463 	}
464 	UFS_UNLOCK(ump);
465 
466 	/*
467 	 * Step 3: re-read summary information from disk.
468 	 */
469 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
470 	space = fs->fs_csp;
471 	for (i = 0; i < blks; i += fs->fs_frag) {
472 		size = fs->fs_bsize;
473 		if (i + fs->fs_frag > blks)
474 			size = (blks - i) * fs->fs_fsize;
475 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
476 		    NOCRED, &bp);
477 		if (error)
478 			return (error);
479 		bcopy(bp->b_data, space, (u_int)size);
480 		space = (char *)space + size;
481 		brelse(bp);
482 	}
483 	/*
484 	 * We no longer know anything about clusters per cylinder group.
485 	 */
486 	if (fs->fs_contigsumsize > 0) {
487 		lp = fs->fs_maxcluster;
488 		for (i = 0; i < fs->fs_ncg; i++)
489 			*lp++ = fs->fs_contigsumsize;
490 	}
491 
492 loop:
493 	MNT_ILOCK(mp);
494 	MNT_VNODE_FOREACH(vp, mp, nvp) {
495 		VI_LOCK(vp);
496 		if (vp->v_iflag & VI_DOOMED) {
497 			VI_UNLOCK(vp);
498 			continue;
499 		}
500 		MNT_IUNLOCK(mp);
501 		/*
502 		 * Step 4: invalidate all cached file data.
503 		 */
504 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
505 			goto loop;
506 		}
507 		if (vinvalbuf(vp, 0, td, 0, 0))
508 			panic("ffs_reload: dirty2");
509 		/*
510 		 * Step 5: re-read inode data for all active vnodes.
511 		 */
512 		ip = VTOI(vp);
513 		error =
514 		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
515 		    (int)fs->fs_bsize, NOCRED, &bp);
516 		if (error) {
517 			VOP_UNLOCK(vp, 0, td);
518 			vrele(vp);
519 			return (error);
520 		}
521 		ffs_load_inode(bp, ip, fs, ip->i_number);
522 		ip->i_effnlink = ip->i_nlink;
523 		brelse(bp);
524 		VOP_UNLOCK(vp, 0, td);
525 		vrele(vp);
526 		MNT_ILOCK(mp);
527 	}
528 	MNT_IUNLOCK(mp);
529 	return (0);
530 }
531 
532 /*
533  * Possible superblock locations ordered from most to least likely.
534  */
535 static int sblock_try[] = SBLOCKSEARCH;
536 
537 /*
538  * Common code for mount and mountroot
539  */
540 static int
541 ffs_mountfs(devvp, mp, td)
542 	struct vnode *devvp;
543 	struct mount *mp;
544 	struct thread *td;
545 {
546 	struct ufsmount *ump;
547 	struct buf *bp;
548 	struct fs *fs;
549 	struct cdev *dev;
550 	void *space;
551 	ufs2_daddr_t sblockloc;
552 	int error, i, blks, size, ronly;
553 	int32_t *lp;
554 	struct ucred *cred;
555 	struct g_consumer *cp;
556 
557 	dev = devvp->v_rdev;
558 	cred = td ? td->td_ucred : NOCRED;
559 
560 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
561 	DROP_GIANT();
562 	g_topology_lock();
563 	error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
564 
565 	/*
566 	 * If we are a root mount, drop the E flag so fsck can do its magic.
567 	 * We will pick it up again when we remount R/W.
568 	 */
569 	if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
570 		error = g_access(cp, 0, 0, -1);
571 	g_topology_unlock();
572 	PICKUP_GIANT();
573 	VOP_UNLOCK(devvp, 0, td);
574 	if (error)
575 		return (error);
576 	if (devvp->v_rdev->si_iosize_max != 0)
577 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
578 	if (mp->mnt_iosize_max > MAXPHYS)
579 		mp->mnt_iosize_max = MAXPHYS;
580 
581 	devvp->v_bufobj.bo_private = cp;
582 	devvp->v_bufobj.bo_ops = &ffs_ops;
583 
584 	bp = NULL;
585 	ump = NULL;
586 	fs = NULL;
587 	sblockloc = 0;
588 	/*
589 	 * Try reading the superblock in each of its possible locations.
590 	 */
591 	for (i = 0; sblock_try[i] != -1; i++) {
592 		if ((error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
593 		    cred, &bp)) != 0)
594 			goto out;
595 		fs = (struct fs *)bp->b_data;
596 		sblockloc = sblock_try[i];
597 		if ((fs->fs_magic == FS_UFS1_MAGIC ||
598 		     (fs->fs_magic == FS_UFS2_MAGIC &&
599 		      (fs->fs_sblockloc == sblockloc ||
600 		       (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
601 		    fs->fs_bsize <= MAXBSIZE &&
602 		    fs->fs_bsize >= sizeof(struct fs))
603 			break;
604 		brelse(bp);
605 		bp = NULL;
606 	}
607 	if (sblock_try[i] == -1) {
608 		error = EINVAL;		/* XXX needs translation */
609 		goto out;
610 	}
611 	fs->fs_fmod = 0;
612 	fs->fs_flags &= ~FS_INDEXDIRS;	/* no support for directory indicies */
613 	fs->fs_flags &= ~FS_UNCLEAN;
614 	if (fs->fs_clean == 0) {
615 		fs->fs_flags |= FS_UNCLEAN;
616 		if (ronly || (mp->mnt_flag & MNT_FORCE) ||
617 		    ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
618 		     (fs->fs_flags & FS_DOSOFTDEP))) {
619 			printf(
620 "WARNING: %s was not properly dismounted\n",
621 			    fs->fs_fsmnt);
622 		} else {
623 			printf(
624 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
625 			    fs->fs_fsmnt);
626 			error = EPERM;
627 			goto out;
628 		}
629 		if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
630 		    (mp->mnt_flag & MNT_FORCE)) {
631 			printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt,
632 			    (intmax_t)fs->fs_pendingblocks,
633 			    fs->fs_pendinginodes);
634 			fs->fs_pendingblocks = 0;
635 			fs->fs_pendinginodes = 0;
636 		}
637 	}
638 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
639 		printf("%s: mount pending error: blocks %jd files %d\n",
640 		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
641 		    fs->fs_pendinginodes);
642 		fs->fs_pendingblocks = 0;
643 		fs->fs_pendinginodes = 0;
644 	}
645 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
646 	ump->um_cp = cp;
647 	ump->um_bo = &devvp->v_bufobj;
648 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
649 	if (fs->fs_magic == FS_UFS1_MAGIC) {
650 		ump->um_fstype = UFS1;
651 		ump->um_balloc = ffs_balloc_ufs1;
652 	} else {
653 		ump->um_fstype = UFS2;
654 		ump->um_balloc = ffs_balloc_ufs2;
655 	}
656 	ump->um_blkatoff = ffs_blkatoff;
657 	ump->um_truncate = ffs_truncate;
658 	ump->um_update = ffs_update;
659 	ump->um_valloc = ffs_valloc;
660 	ump->um_vfree = ffs_vfree;
661 	ump->um_ifree = ffs_ifree;
662 	mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
663 	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
664 	if (fs->fs_sbsize < SBLOCKSIZE)
665 		bp->b_flags |= B_INVAL | B_NOCACHE;
666 	brelse(bp);
667 	bp = NULL;
668 	fs = ump->um_fs;
669 	ffs_oldfscompat_read(fs, ump, sblockloc);
670 	fs->fs_ronly = ronly;
671 	size = fs->fs_cssize;
672 	blks = howmany(size, fs->fs_fsize);
673 	if (fs->fs_contigsumsize > 0)
674 		size += fs->fs_ncg * sizeof(int32_t);
675 	size += fs->fs_ncg * sizeof(u_int8_t);
676 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
677 	fs->fs_csp = space;
678 	for (i = 0; i < blks; i += fs->fs_frag) {
679 		size = fs->fs_bsize;
680 		if (i + fs->fs_frag > blks)
681 			size = (blks - i) * fs->fs_fsize;
682 		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
683 		    cred, &bp)) != 0) {
684 			free(fs->fs_csp, M_UFSMNT);
685 			goto out;
686 		}
687 		bcopy(bp->b_data, space, (u_int)size);
688 		space = (char *)space + size;
689 		brelse(bp);
690 		bp = NULL;
691 	}
692 	if (fs->fs_contigsumsize > 0) {
693 		fs->fs_maxcluster = lp = space;
694 		for (i = 0; i < fs->fs_ncg; i++)
695 			*lp++ = fs->fs_contigsumsize;
696 		space = lp;
697 	}
698 	size = fs->fs_ncg * sizeof(u_int8_t);
699 	fs->fs_contigdirs = (u_int8_t *)space;
700 	bzero(fs->fs_contigdirs, size);
701 	fs->fs_active = NULL;
702 	mp->mnt_data = (qaddr_t)ump;
703 	mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
704 	mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
705 	if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
706 	    vfs_getvfs(&mp->mnt_stat.f_fsid))
707 		vfs_getnewfsid(mp);
708 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
709 	mp->mnt_flag |= MNT_LOCAL;
710 	if ((fs->fs_flags & FS_MULTILABEL) != 0)
711 #ifdef MAC
712 		mp->mnt_flag |= MNT_MULTILABEL;
713 #else
714 		printf(
715 "WARNING: %s: multilabel flag on fs but no MAC support\n",
716 		    fs->fs_fsmnt);
717 #endif
718 	if ((fs->fs_flags & FS_ACLS) != 0)
719 #ifdef UFS_ACL
720 		mp->mnt_flag |= MNT_ACLS;
721 #else
722 		printf(
723 "WARNING: %s: ACLs flag on fs but no ACLs support\n",
724 		    fs->fs_fsmnt);
725 #endif
726 	ump->um_mountp = mp;
727 	ump->um_dev = dev;
728 	ump->um_devvp = devvp;
729 	ump->um_nindir = fs->fs_nindir;
730 	ump->um_bptrtodb = fs->fs_fsbtodb;
731 	ump->um_seqinc = fs->fs_frag;
732 	for (i = 0; i < MAXQUOTAS; i++)
733 		ump->um_quotas[i] = NULLVP;
734 #ifdef UFS_EXTATTR
735 	ufs_extattr_uepm_init(&ump->um_extattr);
736 #endif
737 	/*
738 	 * Set FS local "last mounted on" information (NULL pad)
739 	 */
740 	bzero(fs->fs_fsmnt, MAXMNTLEN);
741 	strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
742 
743 	if( mp->mnt_flag & MNT_ROOTFS) {
744 		/*
745 		 * Root mount; update timestamp in mount structure.
746 		 * this will be used by the common root mount code
747 		 * to update the system clock.
748 		 */
749 		mp->mnt_time = fs->fs_time;
750 	}
751 
752 	if (ronly == 0) {
753 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
754 		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
755 			free(fs->fs_csp, M_UFSMNT);
756 			goto out;
757 		}
758 		if (fs->fs_snapinum[0] != 0)
759 			ffs_snapshot_mount(mp);
760 		fs->fs_fmod = 1;
761 		fs->fs_clean = 0;
762 		(void) ffs_sbupdate(ump, MNT_WAIT);
763 	}
764 	/*
765 	 * Initialize filesystem stat information in mount struct.
766 	 */
767 #ifdef UFS_EXTATTR
768 #ifdef UFS_EXTATTR_AUTOSTART
769 	/*
770 	 *
771 	 * Auto-starting does the following:
772 	 *	- check for /.attribute in the fs, and extattr_start if so
773 	 *	- for each file in .attribute, enable that file with
774 	 * 	  an attribute of the same name.
775 	 * Not clear how to report errors -- probably eat them.
776 	 * This would all happen while the filesystem was busy/not
777 	 * available, so would effectively be "atomic".
778 	 */
779 	(void) ufs_extattr_autostart(mp, td);
780 #endif /* !UFS_EXTATTR_AUTOSTART */
781 #endif /* !UFS_EXTATTR */
782 #ifndef QUOTA
783 	mp->mnt_kern_flag |= MNTK_MPSAFE;
784 #endif
785 	return (0);
786 out:
787 	if (bp)
788 		brelse(bp);
789 	if (cp != NULL) {
790 		DROP_GIANT();
791 		g_topology_lock();
792 		g_vfs_close(cp, td);
793 		g_topology_unlock();
794 		PICKUP_GIANT();
795 	}
796 	if (ump) {
797 		mtx_destroy(UFS_MTX(ump));
798 		free(ump->um_fs, M_UFSMNT);
799 		free(ump, M_UFSMNT);
800 		mp->mnt_data = (qaddr_t)0;
801 	}
802 	return (error);
803 }
804 
805 #include <sys/sysctl.h>
806 static int bigcgs = 0;
807 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
808 
809 /*
810  * Sanity checks for loading old filesystem superblocks.
811  * See ffs_oldfscompat_write below for unwound actions.
812  *
813  * XXX - Parts get retired eventually.
814  * Unfortunately new bits get added.
815  */
816 static void
817 ffs_oldfscompat_read(fs, ump, sblockloc)
818 	struct fs *fs;
819 	struct ufsmount *ump;
820 	ufs2_daddr_t sblockloc;
821 {
822 	off_t maxfilesize;
823 
824 	/*
825 	 * If not yet done, update fs_flags location and value of fs_sblockloc.
826 	 */
827 	if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
828 		fs->fs_flags = fs->fs_old_flags;
829 		fs->fs_old_flags |= FS_FLAGS_UPDATED;
830 		fs->fs_sblockloc = sblockloc;
831 	}
832 	/*
833 	 * If not yet done, update UFS1 superblock with new wider fields.
834 	 */
835 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
836 		fs->fs_maxbsize = fs->fs_bsize;
837 		fs->fs_time = fs->fs_old_time;
838 		fs->fs_size = fs->fs_old_size;
839 		fs->fs_dsize = fs->fs_old_dsize;
840 		fs->fs_csaddr = fs->fs_old_csaddr;
841 		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
842 		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
843 		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
844 		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
845 	}
846 	if (fs->fs_magic == FS_UFS1_MAGIC &&
847 	    fs->fs_old_inodefmt < FS_44INODEFMT) {
848 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
849 		fs->fs_qbmask = ~fs->fs_bmask;
850 		fs->fs_qfmask = ~fs->fs_fmask;
851 	}
852 	if (fs->fs_magic == FS_UFS1_MAGIC) {
853 		ump->um_savedmaxfilesize = fs->fs_maxfilesize;
854 		maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1;
855 		if (fs->fs_maxfilesize > maxfilesize)
856 			fs->fs_maxfilesize = maxfilesize;
857 	}
858 	/* Compatibility for old filesystems */
859 	if (fs->fs_avgfilesize <= 0)
860 		fs->fs_avgfilesize = AVFILESIZ;
861 	if (fs->fs_avgfpdir <= 0)
862 		fs->fs_avgfpdir = AFPDIR;
863 	if (bigcgs) {
864 		fs->fs_save_cgsize = fs->fs_cgsize;
865 		fs->fs_cgsize = fs->fs_bsize;
866 	}
867 }
868 
869 /*
870  * Unwinding superblock updates for old filesystems.
871  * See ffs_oldfscompat_read above for details.
872  *
873  * XXX - Parts get retired eventually.
874  * Unfortunately new bits get added.
875  */
876 static void
877 ffs_oldfscompat_write(fs, ump)
878 	struct fs *fs;
879 	struct ufsmount *ump;
880 {
881 
882 	/*
883 	 * Copy back UFS2 updated fields that UFS1 inspects.
884 	 */
885 	if (fs->fs_magic == FS_UFS1_MAGIC) {
886 		fs->fs_old_time = fs->fs_time;
887 		fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
888 		fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
889 		fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
890 		fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
891 		fs->fs_maxfilesize = ump->um_savedmaxfilesize;
892 	}
893 	if (bigcgs) {
894 		fs->fs_cgsize = fs->fs_save_cgsize;
895 		fs->fs_save_cgsize = 0;
896 	}
897 }
898 
899 /*
900  * unmount system call
901  */
902 static int
903 ffs_unmount(mp, mntflags, td)
904 	struct mount *mp;
905 	int mntflags;
906 	struct thread *td;
907 {
908 	struct ufsmount *ump = VFSTOUFS(mp);
909 	struct fs *fs;
910 	int error, flags;
911 
912 	flags = 0;
913 	if (mntflags & MNT_FORCE) {
914 		flags |= FORCECLOSE;
915 	}
916 #ifdef UFS_EXTATTR
917 	if ((error = ufs_extattr_stop(mp, td))) {
918 		if (error != EOPNOTSUPP)
919 			printf("ffs_unmount: ufs_extattr_stop returned %d\n",
920 			    error);
921 	} else {
922 		ufs_extattr_uepm_destroy(&ump->um_extattr);
923 	}
924 #endif
925 	if (mp->mnt_flag & MNT_SOFTDEP) {
926 		if ((error = softdep_flushfiles(mp, flags, td)) != 0)
927 			return (error);
928 	} else {
929 		if ((error = ffs_flushfiles(mp, flags, td)) != 0)
930 			return (error);
931 	}
932 	fs = ump->um_fs;
933 	UFS_LOCK(ump);
934 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
935 		printf("%s: unmount pending error: blocks %jd files %d\n",
936 		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
937 		    fs->fs_pendinginodes);
938 		fs->fs_pendingblocks = 0;
939 		fs->fs_pendinginodes = 0;
940 	}
941 	UFS_UNLOCK(ump);
942 	if (fs->fs_ronly == 0) {
943 		fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
944 		error = ffs_sbupdate(ump, MNT_WAIT);
945 		if (error) {
946 			fs->fs_clean = 0;
947 			return (error);
948 		}
949 	}
950 	DROP_GIANT();
951 	g_topology_lock();
952 	g_vfs_close(ump->um_cp, td);
953 	g_topology_unlock();
954 	PICKUP_GIANT();
955 	vrele(ump->um_devvp);
956 	mtx_destroy(UFS_MTX(ump));
957 	free(fs->fs_csp, M_UFSMNT);
958 	free(fs, M_UFSMNT);
959 	free(ump, M_UFSMNT);
960 	mp->mnt_data = (qaddr_t)0;
961 	mp->mnt_flag &= ~MNT_LOCAL;
962 	return (error);
963 }
964 
965 /*
966  * Flush out all the files in a filesystem.
967  */
968 int
969 ffs_flushfiles(mp, flags, td)
970 	struct mount *mp;
971 	int flags;
972 	struct thread *td;
973 {
974 	struct ufsmount *ump;
975 	int error;
976 
977 	ump = VFSTOUFS(mp);
978 #ifdef QUOTA
979 	if (mp->mnt_flag & MNT_QUOTA) {
980 		int i;
981 		error = vflush(mp, 0, SKIPSYSTEM|flags, td);
982 		if (error)
983 			return (error);
984 		for (i = 0; i < MAXQUOTAS; i++) {
985 			if (ump->um_quotas[i] == NULLVP)
986 				continue;
987 			quotaoff(td, mp, i);
988 		}
989 		/*
990 		 * Here we fall through to vflush again to ensure
991 		 * that we have gotten rid of all the system vnodes.
992 		 */
993 	}
994 #endif
995 	ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
996 	if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
997 		if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
998 			return (error);
999 		ffs_snapshot_unmount(mp);
1000 		/*
1001 		 * Here we fall through to vflush again to ensure
1002 		 * that we have gotten rid of all the system vnodes.
1003 		 */
1004 	}
1005         /*
1006 	 * Flush all the files.
1007 	 */
1008 	if ((error = vflush(mp, 0, flags, td)) != 0)
1009 		return (error);
1010 	/*
1011 	 * Flush filesystem metadata.
1012 	 */
1013 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
1014 	error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1015 	VOP_UNLOCK(ump->um_devvp, 0, td);
1016 	return (error);
1017 }
1018 
1019 /*
1020  * Get filesystem statistics.
1021  */
1022 static int
1023 ffs_statfs(mp, sbp, td)
1024 	struct mount *mp;
1025 	struct statfs *sbp;
1026 	struct thread *td;
1027 {
1028 	struct ufsmount *ump;
1029 	struct fs *fs;
1030 
1031 	ump = VFSTOUFS(mp);
1032 	fs = ump->um_fs;
1033 	if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1034 		panic("ffs_statfs");
1035 	sbp->f_version = STATFS_VERSION;
1036 	sbp->f_bsize = fs->fs_fsize;
1037 	sbp->f_iosize = fs->fs_bsize;
1038 	sbp->f_blocks = fs->fs_dsize;
1039 	UFS_LOCK(ump);
1040 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1041 	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1042 	sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1043 	    dbtofsb(fs, fs->fs_pendingblocks);
1044 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
1045 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1046 	UFS_UNLOCK(ump);
1047 	sbp->f_namemax = NAME_MAX;
1048 	return (0);
1049 }
1050 
1051 /*
1052  * Go through the disk queues to initiate sandbagged IO;
1053  * go through the inodes to write those that have been modified;
1054  * initiate the writing of the super block if it has been modified.
1055  *
1056  * Note: we are always called with the filesystem marked `MPBUSY'.
1057  */
1058 static int
1059 ffs_sync(mp, waitfor, td)
1060 	struct mount *mp;
1061 	int waitfor;
1062 	struct thread *td;
1063 {
1064 	struct vnode *nvp, *vp, *devvp;
1065 	struct inode *ip;
1066 	struct ufsmount *ump = VFSTOUFS(mp);
1067 	struct fs *fs;
1068 	int error, count, wait, lockreq, allerror = 0;
1069 	struct bufobj *bo;
1070 
1071 	fs = ump->um_fs;
1072 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
1073 		printf("fs = %s\n", fs->fs_fsmnt);
1074 		panic("ffs_sync: rofs mod");
1075 	}
1076 	/*
1077 	 * Write back each (modified) inode.
1078 	 */
1079 	wait = 0;
1080 	lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1081 	if (waitfor == MNT_WAIT) {
1082 		wait = 1;
1083 		lockreq = LK_EXCLUSIVE;
1084 	}
1085 	lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1086 	MNT_ILOCK(mp);
1087 loop:
1088 	MNT_VNODE_FOREACH(vp, mp, nvp) {
1089 		/*
1090 		 * Depend on the mntvnode_slock to keep things stable enough
1091 		 * for a quick test.  Since there might be hundreds of
1092 		 * thousands of vnodes, we cannot afford even a subroutine
1093 		 * call unless there's a good chance that we have work to do.
1094 		 */
1095 		VI_LOCK(vp);
1096 		if (vp->v_iflag & VI_DOOMED) {
1097 			VI_UNLOCK(vp);
1098 			continue;
1099 		}
1100 		ip = VTOI(vp);
1101 		if (vp->v_type == VNON || ((ip->i_flag &
1102 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1103 		    vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1104 			VI_UNLOCK(vp);
1105 			continue;
1106 		}
1107 		MNT_IUNLOCK(mp);
1108 		if ((error = vget(vp, lockreq, td)) != 0) {
1109 			MNT_ILOCK(mp);
1110 			if (error == ENOENT || error == ENOLCK)
1111 				goto loop;
1112 			continue;
1113 		}
1114 		if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1115 			allerror = error;
1116 		vput(vp);
1117 		MNT_ILOCK(mp);
1118 	}
1119 	MNT_IUNLOCK(mp);
1120 	/*
1121 	 * Force stale filesystem control information to be flushed.
1122 	 */
1123 	if (waitfor == MNT_WAIT) {
1124 		if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1125 			allerror = error;
1126 		/* Flushed work items may create new vnodes to clean */
1127 		if (allerror == 0 && count) {
1128 			MNT_ILOCK(mp);
1129 			goto loop;
1130 		}
1131 	}
1132 #ifdef QUOTA
1133 	qsync(mp);
1134 #endif
1135 	devvp = ump->um_devvp;
1136 	VI_LOCK(devvp);
1137 	bo = &devvp->v_bufobj;
1138 	if (waitfor != MNT_LAZY &&
1139 	    (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1140 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td);
1141 		if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1142 			allerror = error;
1143 		VOP_UNLOCK(devvp, 0, td);
1144 		if (allerror == 0 && waitfor == MNT_WAIT) {
1145 			MNT_ILOCK(mp);
1146 			goto loop;
1147 		}
1148 	} else
1149 		VI_UNLOCK(devvp);
1150 	/*
1151 	 * Write back modified superblock.
1152 	 */
1153 	if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1154 		allerror = error;
1155 	return (allerror);
1156 }
1157 
1158 int
1159 ffs_vget(mp, ino, flags, vpp)
1160 	struct mount *mp;
1161 	ino_t ino;
1162 	int flags;
1163 	struct vnode **vpp;
1164 {
1165 	struct fs *fs;
1166 	struct inode *ip;
1167 	struct ufsmount *ump;
1168 	struct buf *bp;
1169 	struct vnode *vp;
1170 	struct cdev *dev;
1171 	int error;
1172 
1173 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1174 	if (error || *vpp != NULL)
1175 		return (error);
1176 
1177 	/*
1178 	 * We must promote to an exclusive lock for vnode creation.  This
1179 	 * can happen if lookup is passed LOCKSHARED.
1180  	 */
1181 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1182 		flags &= ~LK_TYPE_MASK;
1183 		flags |= LK_EXCLUSIVE;
1184 	}
1185 
1186 	/*
1187 	 * We do not lock vnode creation as it is believed to be too
1188 	 * expensive for such rare case as simultaneous creation of vnode
1189 	 * for same ino by different processes. We just allow them to race
1190 	 * and check later to decide who wins. Let the race begin!
1191 	 */
1192 
1193 	ump = VFSTOUFS(mp);
1194 	dev = ump->um_dev;
1195 	fs = ump->um_fs;
1196 
1197 	/*
1198 	 * If this MALLOC() is performed after the getnewvnode()
1199 	 * it might block, leaving a vnode with a NULL v_data to be
1200 	 * found by ffs_sync() if a sync happens to fire right then,
1201 	 * which will cause a panic because ffs_sync() blindly
1202 	 * dereferences vp->v_data (as well it should).
1203 	 */
1204 	ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1205 
1206 	/* Allocate a new vnode/inode. */
1207 	if (fs->fs_magic == FS_UFS1_MAGIC)
1208 		error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1209 	else
1210 		error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1211 	if (error) {
1212 		*vpp = NULL;
1213 		uma_zfree(uma_inode, ip);
1214 		return (error);
1215 	}
1216 	/*
1217 	 * FFS supports recursive and shared locking.
1218 	 */
1219 	vp->v_vnlock->lk_flags |= LK_CANRECURSE;
1220 	vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
1221 	vp->v_data = ip;
1222 	vp->v_bufobj.bo_bsize = fs->fs_bsize;
1223 	ip->i_vnode = vp;
1224 	ip->i_ump = ump;
1225 	ip->i_fs = fs;
1226 	ip->i_dev = dev;
1227 	ip->i_number = ino;
1228 #ifdef QUOTA
1229 	{
1230 		int i;
1231 		for (i = 0; i < MAXQUOTAS; i++)
1232 			ip->i_dquot[i] = NODQUOT;
1233 	}
1234 #endif
1235 
1236 	error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1237 	if (error || *vpp != NULL)
1238 		return (error);
1239 
1240 	/* Read in the disk contents for the inode, copy into the inode. */
1241 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1242 	    (int)fs->fs_bsize, NOCRED, &bp);
1243 	if (error) {
1244 		/*
1245 		 * The inode does not contain anything useful, so it would
1246 		 * be misleading to leave it on its hash chain. With mode
1247 		 * still zero, it will be unlinked and returned to the free
1248 		 * list by vput().
1249 		 */
1250 		brelse(bp);
1251 		vput(vp);
1252 		*vpp = NULL;
1253 		return (error);
1254 	}
1255 	if (ip->i_ump->um_fstype == UFS1)
1256 		ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1257 	else
1258 		ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1259 	ffs_load_inode(bp, ip, fs, ino);
1260 	if (DOINGSOFTDEP(vp))
1261 		softdep_load_inodeblock(ip);
1262 	else
1263 		ip->i_effnlink = ip->i_nlink;
1264 	bqrelse(bp);
1265 
1266 	/*
1267 	 * Initialize the vnode from the inode, check for aliases.
1268 	 * Note that the underlying vnode may have changed.
1269 	 */
1270 	if (ip->i_ump->um_fstype == UFS1)
1271 		error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1272 	else
1273 		error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1274 	if (error) {
1275 		vput(vp);
1276 		*vpp = NULL;
1277 		return (error);
1278 	}
1279 
1280 	/*
1281 	 * Finish inode initialization.
1282 	 */
1283 
1284 	/*
1285 	 * Set up a generation number for this inode if it does not
1286 	 * already have one. This should only happen on old filesystems.
1287 	 */
1288 	if (ip->i_gen == 0) {
1289 		ip->i_gen = arc4random() / 2 + 1;
1290 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1291 			ip->i_flag |= IN_MODIFIED;
1292 			DIP_SET(ip, i_gen, ip->i_gen);
1293 		}
1294 	}
1295 	/*
1296 	 * Ensure that uid and gid are correct. This is a temporary
1297 	 * fix until fsck has been changed to do the update.
1298 	 */
1299 	if (fs->fs_magic == FS_UFS1_MAGIC &&		/* XXX */
1300 	    fs->fs_old_inodefmt < FS_44INODEFMT) {	/* XXX */
1301 		ip->i_uid = ip->i_din1->di_ouid;	/* XXX */
1302 		ip->i_gid = ip->i_din1->di_ogid;	/* XXX */
1303 	}						/* XXX */
1304 
1305 #ifdef MAC
1306 	if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1307 		/*
1308 		 * If this vnode is already allocated, and we're running
1309 		 * multi-label, attempt to perform a label association
1310 		 * from the extended attributes on the inode.
1311 		 */
1312 		error = mac_associate_vnode_extattr(mp, vp);
1313 		if (error) {
1314 			/* ufs_inactive will release ip->i_devvp ref. */
1315 			vput(vp);
1316 			*vpp = NULL;
1317 			return (error);
1318 		}
1319 	}
1320 #endif
1321 
1322 	*vpp = vp;
1323 	return (0);
1324 }
1325 
1326 /*
1327  * File handle to vnode
1328  *
1329  * Have to be really careful about stale file handles:
1330  * - check that the inode number is valid
1331  * - call ffs_vget() to get the locked inode
1332  * - check for an unallocated inode (i_mode == 0)
1333  * - check that the given client host has export rights and return
1334  *   those rights via. exflagsp and credanonp
1335  */
1336 static int
1337 ffs_fhtovp(mp, fhp, vpp)
1338 	struct mount *mp;
1339 	struct fid *fhp;
1340 	struct vnode **vpp;
1341 {
1342 	struct ufid *ufhp;
1343 	struct fs *fs;
1344 
1345 	ufhp = (struct ufid *)fhp;
1346 	fs = VFSTOUFS(mp)->um_fs;
1347 	if (ufhp->ufid_ino < ROOTINO ||
1348 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1349 		return (ESTALE);
1350 	return (ufs_fhtovp(mp, ufhp, vpp));
1351 }
1352 
1353 /*
1354  * Vnode pointer to File handle
1355  */
1356 /* ARGSUSED */
1357 static int
1358 ffs_vptofh(vp, fhp)
1359 	struct vnode *vp;
1360 	struct fid *fhp;
1361 {
1362 	struct inode *ip;
1363 	struct ufid *ufhp;
1364 
1365 	ip = VTOI(vp);
1366 	ufhp = (struct ufid *)fhp;
1367 	ufhp->ufid_len = sizeof(struct ufid);
1368 	ufhp->ufid_ino = ip->i_number;
1369 	ufhp->ufid_gen = ip->i_gen;
1370 	return (0);
1371 }
1372 
1373 /*
1374  * Initialize the filesystem.
1375  */
1376 static int
1377 ffs_init(vfsp)
1378 	struct vfsconf *vfsp;
1379 {
1380 
1381 	softdep_initialize();
1382 	return (ufs_init(vfsp));
1383 }
1384 
1385 /*
1386  * Undo the work of ffs_init().
1387  */
1388 static int
1389 ffs_uninit(vfsp)
1390 	struct vfsconf *vfsp;
1391 {
1392 	int ret;
1393 
1394 	ret = ufs_uninit(vfsp);
1395 	softdep_uninitialize();
1396 	return (ret);
1397 }
1398 
1399 /*
1400  * Write a superblock and associated information back to disk.
1401  */
1402 static int
1403 ffs_sbupdate(mp, waitfor)
1404 	struct ufsmount *mp;
1405 	int waitfor;
1406 {
1407 	struct fs *fs = mp->um_fs;
1408 	struct buf *sbbp;
1409 	struct buf *bp;
1410 	int blks;
1411 	void *space;
1412 	int i, size, error, allerror = 0;
1413 
1414 	if (fs->fs_ronly == 1 &&
1415 	    (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1416 	    (MNT_RDONLY | MNT_UPDATE))
1417 		panic("ffs_sbupdate: write read-only filesystem");
1418 	/*
1419 	 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1420 	 */
1421 	sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1422 	    0, 0, 0);
1423 	/*
1424 	 * First write back the summary information.
1425 	 */
1426 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1427 	space = fs->fs_csp;
1428 	for (i = 0; i < blks; i += fs->fs_frag) {
1429 		size = fs->fs_bsize;
1430 		if (i + fs->fs_frag > blks)
1431 			size = (blks - i) * fs->fs_fsize;
1432 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1433 		    size, 0, 0, 0);
1434 		bcopy(space, bp->b_data, (u_int)size);
1435 		space = (char *)space + size;
1436 		if (waitfor != MNT_WAIT)
1437 			bawrite(bp);
1438 		else if ((error = bwrite(bp)) != 0)
1439 			allerror = error;
1440 	}
1441 	/*
1442 	 * Now write back the superblock itself. If any errors occurred
1443 	 * up to this point, then fail so that the superblock avoids
1444 	 * being written out as clean.
1445 	 */
1446 	if (allerror) {
1447 		brelse(sbbp);
1448 		return (allerror);
1449 	}
1450 	bp = sbbp;
1451 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1452 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1453 		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1454 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1455 		fs->fs_sblockloc = SBLOCK_UFS1;
1456 	}
1457 	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1458 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1459 		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1460 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1461 		fs->fs_sblockloc = SBLOCK_UFS2;
1462 	}
1463 	fs->fs_fmod = 0;
1464 	fs->fs_time = time_second;
1465 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1466 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1467 	if (waitfor != MNT_WAIT)
1468 		bawrite(bp);
1469 	else if ((error = bwrite(bp)) != 0)
1470 		allerror = error;
1471 	return (allerror);
1472 }
1473 
1474 static int
1475 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1476 	int attrnamespace, const char *attrname, struct thread *td)
1477 {
1478 
1479 #ifdef UFS_EXTATTR
1480 	return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1481 	    attrname, td));
1482 #else
1483 	return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1484 	    attrname, td));
1485 #endif
1486 }
1487 
1488 static void
1489 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1490 {
1491 
1492 	if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1493 		uma_zfree(uma_ufs1, ip->i_din1);
1494 	else if (ip->i_din2 != NULL)
1495 		uma_zfree(uma_ufs2, ip->i_din2);
1496 	uma_zfree(uma_inode, ip);
1497 }
1498 
1499 static int dobkgrdwrite = 1;
1500 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1501     "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1502 
1503 /*
1504  * Complete a background write started from bwrite.
1505  */
1506 static void
1507 ffs_backgroundwritedone(struct buf *bp)
1508 {
1509 	struct bufobj *bufobj;
1510 	struct buf *origbp;
1511 
1512 	/*
1513 	 * Find the original buffer that we are writing.
1514 	 */
1515 	bufobj = bp->b_bufobj;
1516 	BO_LOCK(bufobj);
1517 	if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1518 		panic("backgroundwritedone: lost buffer");
1519 	/* Grab an extra reference to be dropped by the bufdone() below. */
1520 	bufobj_wrefl(bufobj);
1521 	BO_UNLOCK(bufobj);
1522 	/*
1523 	 * Process dependencies then return any unfinished ones.
1524 	 */
1525 	if (LIST_FIRST(&bp->b_dep) != NULL)
1526 		buf_complete(bp);
1527 #ifdef SOFTUPDATES
1528 	if (LIST_FIRST(&bp->b_dep) != NULL)
1529 		softdep_move_dependencies(bp, origbp);
1530 #endif
1531 	/*
1532 	 * This buffer is marked B_NOCACHE so when it is released
1533 	 * by biodone it will be tossed.
1534 	 */
1535 	bp->b_flags |= B_NOCACHE;
1536 	bp->b_flags &= ~B_CACHE;
1537 	bufdone(bp);
1538 	BO_LOCK(bufobj);
1539 	/*
1540 	 * Clear the BV_BKGRDINPROG flag in the original buffer
1541 	 * and awaken it if it is waiting for the write to complete.
1542 	 * If BV_BKGRDINPROG is not set in the original buffer it must
1543 	 * have been released and re-instantiated - which is not legal.
1544 	 */
1545 	KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1546 	    ("backgroundwritedone: lost buffer2"));
1547 	origbp->b_vflags &= ~BV_BKGRDINPROG;
1548 	if (origbp->b_vflags & BV_BKGRDWAIT) {
1549 		origbp->b_vflags &= ~BV_BKGRDWAIT;
1550 		wakeup(&origbp->b_xflags);
1551 	}
1552 	BO_UNLOCK(bufobj);
1553 }
1554 
1555 
1556 /*
1557  * Write, release buffer on completion.  (Done by iodone
1558  * if async).  Do not bother writing anything if the buffer
1559  * is invalid.
1560  *
1561  * Note that we set B_CACHE here, indicating that buffer is
1562  * fully valid and thus cacheable.  This is true even of NFS
1563  * now so we set it generally.  This could be set either here
1564  * or in biodone() since the I/O is synchronous.  We put it
1565  * here.
1566  */
1567 static int
1568 ffs_bufwrite(struct buf *bp)
1569 {
1570 	int oldflags, s;
1571 	struct buf *newbp;
1572 
1573 	CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1574 	if (bp->b_flags & B_INVAL) {
1575 		brelse(bp);
1576 		return (0);
1577 	}
1578 
1579 	oldflags = bp->b_flags;
1580 
1581 	if (BUF_REFCNT(bp) == 0)
1582 		panic("bufwrite: buffer is not busy???");
1583 	s = splbio();
1584 	/*
1585 	 * If a background write is already in progress, delay
1586 	 * writing this block if it is asynchronous. Otherwise
1587 	 * wait for the background write to complete.
1588 	 */
1589 	BO_LOCK(bp->b_bufobj);
1590 	if (bp->b_vflags & BV_BKGRDINPROG) {
1591 		if (bp->b_flags & B_ASYNC) {
1592 			BO_UNLOCK(bp->b_bufobj);
1593 			splx(s);
1594 			bdwrite(bp);
1595 			return (0);
1596 		}
1597 		bp->b_vflags |= BV_BKGRDWAIT;
1598 		msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1599 		if (bp->b_vflags & BV_BKGRDINPROG)
1600 			panic("bufwrite: still writing");
1601 	}
1602 	BO_UNLOCK(bp->b_bufobj);
1603 
1604 	/* Mark the buffer clean */
1605 	bundirty(bp);
1606 
1607 	/*
1608 	 * If this buffer is marked for background writing and we
1609 	 * do not have to wait for it, make a copy and write the
1610 	 * copy so as to leave this buffer ready for further use.
1611 	 *
1612 	 * This optimization eats a lot of memory.  If we have a page
1613 	 * or buffer shortfall we can't do it.
1614 	 */
1615 	if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1616 	    (bp->b_flags & B_ASYNC) &&
1617 	    !vm_page_count_severe() &&
1618 	    !buf_dirty_count_severe()) {
1619 		KASSERT(bp->b_iodone == NULL,
1620 		    ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1621 
1622 		/* get a new block */
1623 		newbp = geteblk(bp->b_bufsize);
1624 
1625 		/*
1626 		 * set it to be identical to the old block.  We have to
1627 		 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1628 		 * to avoid confusing the splay tree and gbincore().
1629 		 */
1630 		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1631 		newbp->b_lblkno = bp->b_lblkno;
1632 		newbp->b_xflags |= BX_BKGRDMARKER;
1633 		BO_LOCK(bp->b_bufobj);
1634 		bp->b_vflags |= BV_BKGRDINPROG;
1635 		bgetvp(bp->b_vp, newbp);
1636 		BO_UNLOCK(bp->b_bufobj);
1637 		newbp->b_bufobj = &bp->b_vp->v_bufobj;
1638 		newbp->b_blkno = bp->b_blkno;
1639 		newbp->b_offset = bp->b_offset;
1640 		newbp->b_iodone = ffs_backgroundwritedone;
1641 		newbp->b_flags |= B_ASYNC;
1642 		newbp->b_flags &= ~B_INVAL;
1643 
1644 #ifdef SOFTUPDATES
1645 		/* move over the dependencies */
1646 		if (LIST_FIRST(&bp->b_dep) != NULL)
1647 			softdep_move_dependencies(bp, newbp);
1648 #endif
1649 
1650 		/*
1651 		 * Initiate write on the copy, release the original to
1652 		 * the B_LOCKED queue so that it cannot go away until
1653 		 * the background write completes. If not locked it could go
1654 		 * away and then be reconstituted while it was being written.
1655 		 * If the reconstituted buffer were written, we could end up
1656 		 * with two background copies being written at the same time.
1657 		 */
1658 		bqrelse(bp);
1659 		bp = newbp;
1660 	}
1661 
1662 	/* Let the normal bufwrite do the rest for us */
1663 	bufwrite(bp);
1664 
1665 	return (0);
1666 }
1667 
1668 
1669 static void
1670 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1671 {
1672 	struct vnode *vp;
1673 	int error;
1674 
1675 	vp = bo->__bo_vnode;
1676 	if (bp->b_iocmd == BIO_WRITE) {
1677 #ifdef SOFTUPDATES
1678 		if (LIST_FIRST(&bp->b_dep) != NULL)
1679 			buf_start(bp);
1680 #endif
1681 		if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1682 		    bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1683 		    (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1684 			panic("ffs_geom_strategy: bad I/O");
1685 		bp->b_flags &= ~B_VALIDSUSPWRT;
1686 		if ((vp->v_vflag & VV_COPYONWRITE) &&
1687 		    vp->v_rdev->si_snapdata != NULL &&
1688 		    (error = (ffs_copyonwrite)(vp, bp)) != 0 &&
1689 		    error != EOPNOTSUPP) {
1690 			bp->b_error = error;
1691 			bp->b_ioflags |= BIO_ERROR;
1692 			bufdone(bp);
1693 			return;
1694 		}
1695 	}
1696 	g_vfs_strategy(bo, bp);
1697 }
1698