xref: /openbsd/sys/ufs/ffs/ffs_vfsops.c (revision 73471bf0)
1 /*	$OpenBSD: ffs_vfsops.c,v 1.192 2021/10/20 06:35:39 semarie Exp $	*/
2 /*	$NetBSD: ffs_vfsops.c,v 1.19 1996/02/09 22:22:26 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1991, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/namei.h>
38 #include <sys/proc.h>
39 #include <sys/kernel.h>
40 #include <sys/vnode.h>
41 #include <sys/socket.h>
42 #include <sys/mount.h>
43 #include <sys/buf.h>
44 #include <sys/mbuf.h>
45 #include <sys/fcntl.h>
46 #include <sys/ioctl.h>
47 #include <sys/errno.h>
48 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
50 #include <sys/pool.h>
51 #include <sys/dkio.h>
52 #include <sys/disk.h>
53 #include <sys/specdev.h>
54 
55 #include <ufs/ufs/quota.h>
56 #include <ufs/ufs/ufsmount.h>
57 #include <ufs/ufs/inode.h>
58 #include <ufs/ufs/dir.h>
59 #include <ufs/ufs/ufs_extern.h>
60 #include <ufs/ufs/dirhash.h>
61 
62 #include <ufs/ffs/fs.h>
63 #include <ufs/ffs/ffs_extern.h>
64 
65 #include <uvm/uvm_extern.h>
66 
67 int ffs_sbupdate(struct ufsmount *, int);
68 int ffs_reload_vnode(struct vnode *, void *);
69 int ffs_sync_vnode(struct vnode *, void *);
70 int ffs_validate(struct fs *);
71 
72 void ffs1_compat_read(struct fs *, struct ufsmount *, daddr_t);
73 void ffs1_compat_write(struct fs *, struct ufsmount *);
74 
75 const struct vfsops ffs_vfsops = {
76 	.vfs_mount	= ffs_mount,
77 	.vfs_start	= ufs_start,
78 	.vfs_unmount	= ffs_unmount,
79 	.vfs_root	= ufs_root,
80 	.vfs_quotactl	= ufs_quotactl,
81 	.vfs_statfs	= ffs_statfs,
82 	.vfs_sync	= ffs_sync,
83 	.vfs_vget	= ffs_vget,
84 	.vfs_fhtovp	= ffs_fhtovp,
85 	.vfs_vptofh	= ffs_vptofh,
86 	.vfs_init	= ffs_init,
87 	.vfs_sysctl	= ffs_sysctl,
88 	.vfs_checkexp	= ufs_check_export,
89 };
90 
91 struct inode_vtbl ffs_vtbl = {
92 	ffs_truncate,
93 	ffs_update,
94 	ffs_inode_alloc,
95 	ffs_inode_free,
96 	ffs_balloc,
97 	ffs_bufatoff
98 };
99 
100 int
101 ffs_checkrange(struct mount *mp, uint32_t ino)
102 {
103 	struct buf *bp;
104 	struct cg *cgp;
105 	struct fs *fs;
106 	struct ufsmount *ump;
107 	int cg, error;
108 
109 	fs = VFSTOUFS(mp)->um_fs;
110 	if (ino < ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
111 		return ESTALE;
112 
113 	/*
114 	 * Need to check if inode is initialized because ffsv2 does
115 	 * lazy initialization and we can get here from nfs_fhtovp
116 	 */
117 	if (fs->fs_magic != FS_UFS2_MAGIC)
118 		return 0;
119 
120 	cg = ino_to_cg(fs, ino);
121 	ump = VFSTOUFS(mp);
122 
123 	error = bread(ump->um_devvp, fsbtodb(fs, cgtod(fs, cg)),
124 	    (int)fs->fs_cgsize, &bp);
125 	if (error)
126 		return error;
127 
128 	cgp = (struct cg *)bp->b_data;
129 	if (!cg_chkmagic(cgp)) {
130 		brelse(bp);
131 		return ESTALE;
132 	}
133 
134 	brelse(bp);
135 
136 	if (cg * fs->fs_ipg + cgp->cg_initediblk < ino)
137 		return ESTALE;
138 
139 	return 0;
140 }
141 
142 /*
143  * Called by main() when ufs is going to be mounted as root.
144  */
145 
146 struct pool ffs_ino_pool;
147 struct pool ffs_dinode1_pool;
148 #ifdef FFS2
149 struct pool ffs_dinode2_pool;
150 #endif
151 
152 int
153 ffs_mountroot(void)
154 {
155 	struct fs *fs;
156 	struct mount *mp;
157 	struct proc *p = curproc;	/* XXX */
158 	struct ufsmount *ump;
159 	int error;
160 
161 	/*
162 	 * Get vnodes for swapdev and rootdev.
163 	 */
164 	swapdev_vp = NULL;
165 	if ((error = bdevvp(swapdev, &swapdev_vp)) ||
166 	    (error = bdevvp(rootdev, &rootvp))) {
167 		printf("ffs_mountroot: can't setup bdevvp's\n");
168 		if (swapdev_vp)
169 			vrele(swapdev_vp);
170 		return (error);
171 	}
172 
173 	if ((error = vfs_rootmountalloc("ffs", "root_device", &mp)) != 0) {
174 		vrele(swapdev_vp);
175 		vrele(rootvp);
176 		return (error);
177 	}
178 
179 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
180 		vfs_unbusy(mp);
181 		vfs_mount_free(mp);
182 		vrele(swapdev_vp);
183 		vrele(rootvp);
184 		return (error);
185 	}
186 
187 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
188 	ump = VFSTOUFS(mp);
189 	fs = ump->um_fs;
190 	strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, sizeof(fs->fs_fsmnt));
191 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
192 	vfs_unbusy(mp);
193 	inittodr(fs->fs_time);
194 
195 	return (0);
196 }
197 
198 /*
199  * VFS Operations.
200  *
201  * mount system call
202  */
203 int
204 ffs_mount(struct mount *mp, const char *path, void *data,
205     struct nameidata *ndp, struct proc *p)
206 {
207 	struct vnode *devvp;
208 	struct ufs_args *args = data;
209 	struct ufsmount *ump = NULL;
210 	struct fs *fs;
211 	char fname[MNAMELEN];
212 	char fspec[MNAMELEN];
213 	int error = 0, flags;
214 	int ronly;
215 
216 #ifndef FFS_SOFTUPDATES
217 	if (mp->mnt_flag & MNT_SOFTDEP) {
218 		printf("WARNING: soft updates isn't compiled in\n");
219 		mp->mnt_flag &= ~MNT_SOFTDEP;
220 	}
221 #endif
222 
223 	/*
224 	 * Soft updates is incompatible with "async",
225 	 * so if we are doing softupdates stop the user
226 	 * from setting the async flag.
227 	 */
228 	if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
229 	    (MNT_SOFTDEP | MNT_ASYNC)) {
230 		return (EINVAL);
231 	}
232 	/*
233 	 * If updating, check whether changing from read-only to
234 	 * read/write; if there is no device name, that's all we do.
235 	 */
236 	if (mp->mnt_flag & MNT_UPDATE) {
237 		ump = VFSTOUFS(mp);
238 		fs = ump->um_fs;
239 		devvp = ump->um_devvp;
240 		error = 0;
241 		ronly = fs->fs_ronly;
242 
243 		/*
244 		 * Soft updates won't be set if read/write,
245 		 * so "async" will be illegal.
246 		 */
247 		if (ronly == 0 && (mp->mnt_flag & MNT_ASYNC) &&
248 		    (fs->fs_flags & FS_DOSOFTDEP)) {
249 			error = EINVAL;
250 			goto error_1;
251 		}
252 
253 		if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
254 			/* Flush any dirty data */
255 			VFS_SYNC(mp, MNT_WAIT, 0, p->p_ucred, p);
256 
257 			/*
258 			 * Get rid of files open for writing.
259 			 */
260 			flags = WRITECLOSE;
261 			if (args == NULL)
262 				flags |= IGNORECLEAN;
263 			if (mp->mnt_flag & MNT_FORCE)
264 				flags |= FORCECLOSE;
265 			if (fs->fs_flags & FS_DOSOFTDEP) {
266 				error = softdep_flushfiles(mp, flags, p);
267 				mp->mnt_flag &= ~MNT_SOFTDEP;
268 			} else
269 				error = ffs_flushfiles(mp, flags, p);
270 			mp->mnt_flag |= MNT_RDONLY;
271 			ronly = 1;
272 		}
273 
274 		/*
275 		 * Flush soft dependencies if disabling it via an update
276 		 * mount. This may leave some items to be processed,
277 		 * so don't do this yet XXX.
278 		 */
279 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
280 		    !(mp->mnt_flag & MNT_SOFTDEP) &&
281 		    !(mp->mnt_flag & MNT_RDONLY) && fs->fs_ronly == 0) {
282 #if 0
283 			flags = WRITECLOSE;
284 			if (mp->mnt_flag & MNT_FORCE)
285 				flags |= FORCECLOSE;
286 			error = softdep_flushfiles(mp, flags, p);
287 #elif FFS_SOFTUPDATES
288 			mp->mnt_flag |= MNT_SOFTDEP;
289 #endif
290 		}
291 		/*
292 		 * When upgrading to a softdep mount, we must first flush
293 		 * all vnodes. (not done yet -- see above)
294 		 */
295 		if (!(fs->fs_flags & FS_DOSOFTDEP) &&
296 		    (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
297 #if 0
298 			flags = WRITECLOSE;
299 			if (mp->mnt_flag & MNT_FORCE)
300 				flags |= FORCECLOSE;
301 			error = ffs_flushfiles(mp, flags, p);
302 #else
303 			mp->mnt_flag &= ~MNT_SOFTDEP;
304 #endif
305 		}
306 
307 		if (!error && (mp->mnt_flag & MNT_RELOAD))
308 			error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p);
309 		if (error)
310 			goto error_1;
311 
312 		if (ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
313 			if (fs->fs_clean == 0) {
314 #if 0
315 				/*
316 				 * It is safe to mount an unclean file system
317 				 * if it was previously mounted with softdep
318 				 * but we may lose space and must
319 				 * sometimes run fsck manually.
320 				 */
321 				if (fs->fs_flags & FS_DOSOFTDEP)
322 					printf(
323 "WARNING: %s was not properly unmounted\n",
324 					    fs->fs_fsmnt);
325 				else
326 #endif
327 				if (mp->mnt_flag & MNT_FORCE) {
328 					printf(
329 "WARNING: %s was not properly unmounted\n",
330 					    fs->fs_fsmnt);
331 				} else {
332 					printf(
333 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
334 					    fs->fs_fsmnt);
335 					error = EROFS;
336 					goto error_1;
337 				}
338 			}
339 
340 			if ((fs->fs_flags & FS_DOSOFTDEP)) {
341 				error = softdep_mount(devvp, mp, fs,
342 						      p->p_ucred);
343 				if (error)
344 					goto error_1;
345 			}
346 			fs->fs_contigdirs = malloc((u_long)fs->fs_ncg,
347 			     M_UFSMNT, M_WAITOK|M_ZERO);
348 
349 			ronly = 0;
350 		}
351 		if (args == NULL)
352 			goto success;
353 		if (args->fspec == NULL) {
354 			/*
355 			 * Process export requests.
356 			 */
357 			error = vfs_export(mp, &ump->um_export,
358 			    &args->export_info);
359 			if (error)
360 				goto error_1;
361 			else
362 				goto success;
363 		}
364 	}
365 
366 	/*
367 	 * Not an update, or updating the name: look up the name
368 	 * and verify that it refers to a sensible block device.
369 	 */
370 	error = copyinstr(args->fspec, fspec, sizeof(fspec), NULL);
371 	if (error)
372 		goto error_1;
373 
374 	if (disk_map(fspec, fname, MNAMELEN, DM_OPENBLCK) == -1)
375 		memcpy(fname, fspec, sizeof(fname));
376 
377 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fname, p);
378 	if ((error = namei(ndp)) != 0)
379 		goto error_1;
380 
381 	devvp = ndp->ni_vp;
382 
383 	if (devvp->v_type != VBLK) {
384 		error = ENOTBLK;
385 		goto error_2;
386 	}
387 
388 	if (major(devvp->v_rdev) >= nblkdev) {
389 		error = ENXIO;
390 		goto error_2;
391 	}
392 
393 	if (mp->mnt_flag & MNT_UPDATE) {
394 		/*
395 		 * UPDATE
396 		 * If it's not the same vnode, or at least the same device
397 		 * then it's not correct.
398 		 */
399 
400 		if (devvp != ump->um_devvp) {
401 			if (devvp->v_rdev == ump->um_devvp->v_rdev) {
402 				vrele(devvp);
403 			} else {
404 				error = EINVAL;	/* needs translation */
405 			}
406 		} else
407 			vrele(devvp);
408 		/*
409 		 * Update device name only on success
410 		 */
411 		if (!error) {
412 			/*
413 			 * Save "mounted from" info for mount point (NULL pad)
414 			 */
415 			memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
416 			strlcpy(mp->mnt_stat.f_mntfromname, fname, MNAMELEN);
417 			memset(mp->mnt_stat.f_mntfromspec, 0, MNAMELEN);
418 			strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
419 		}
420 	} else {
421 		/*
422 		 * Since this is a new mount, we want the names for
423 		 * the device and the mount point copied in.  If an
424 		 * error occurs,  the mountpoint is discarded by the
425 		 * upper level code.
426 		 */
427 		memset(mp->mnt_stat.f_mntonname, 0, MNAMELEN);
428 		strlcpy(mp->mnt_stat.f_mntonname, path, MNAMELEN);
429 		memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
430 		strlcpy(mp->mnt_stat.f_mntfromname, fname, MNAMELEN);
431 		memset(mp->mnt_stat.f_mntfromspec, 0, MNAMELEN);
432 		strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
433 
434 		error = ffs_mountfs(devvp, mp, p);
435 	}
436 
437 	if (error)
438 		goto error_2;
439 
440 	/*
441 	 * Initialize FS stat information in mount struct; uses both
442 	 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
443 	 *
444 	 * This code is common to root and non-root mounts
445 	 */
446 	if (args)
447 		memcpy(&mp->mnt_stat.mount_info.ufs_args, args, sizeof(*args));
448 	VFS_STATFS(mp, &mp->mnt_stat, p);
449 
450 success:
451 	if (path && (mp->mnt_flag & MNT_UPDATE)) {
452 		/* Update clean flag after changing read-onlyness. */
453 		fs = ump->um_fs;
454 		if (ronly != fs->fs_ronly) {
455 			fs->fs_ronly = ronly;
456 			fs->fs_clean = ronly &&
457 			    (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
458 			if (ronly)
459 				free(fs->fs_contigdirs, M_UFSMNT, fs->fs_ncg);
460 		}
461 		if (!ronly) {
462 			if (mp->mnt_flag & MNT_SOFTDEP)
463 				fs->fs_flags |= FS_DOSOFTDEP;
464 			else
465 				fs->fs_flags &= ~FS_DOSOFTDEP;
466 		}
467 		ffs_sbupdate(ump, MNT_WAIT);
468 #if 0
469 		if (ronly) {
470 			int force = 0;
471 
472 			/*
473 			 * Updating mount to readonly. Try a cache flush.
474 			 * Ignore error because the ioctl may not be supported.
475 			 */
476 			VOP_IOCTL(ump->um_devvp, DIOCCACHESYNC, &force,
477 			    FWRITE, FSCRED, p);
478                }
479 #endif
480 	}
481 	return (0);
482 
483 error_2:	/* error with devvp held */
484 	vrele (devvp);
485 
486 error_1:	/* no state to back out */
487 	return (error);
488 }
489 
490 struct ffs_reload_args {
491 	struct fs *fs;
492 	struct proc *p;
493 	struct ucred *cred;
494 	struct vnode *devvp;
495 };
496 
497 int
498 ffs_reload_vnode(struct vnode *vp, void *args)
499 {
500 	struct ffs_reload_args *fra = args;
501 	struct inode *ip;
502 	struct buf *bp;
503 	int error;
504 
505 	/*
506 	 * Step 4: invalidate all inactive vnodes.
507 	 */
508 	if (vp->v_usecount == 0) {
509 		vgonel(vp, fra->p);
510 		return (0);
511 	}
512 
513 	/*
514 	 * Step 5: invalidate all cached file data.
515 	 */
516 	if (vget(vp, LK_EXCLUSIVE))
517 		return (0);
518 
519 	if (vinvalbuf(vp, 0, fra->cred, fra->p, 0, INFSLP))
520 		panic("ffs_reload: dirty2");
521 
522 	/*
523 	 * Step 6: re-read inode data for all active vnodes.
524 	 */
525 	ip = VTOI(vp);
526 
527 	error = bread(fra->devvp,
528 	    fsbtodb(fra->fs, ino_to_fsba(fra->fs, ip->i_number)),
529 	    (int)fra->fs->fs_bsize, &bp);
530 	if (error) {
531 		brelse(bp);
532 		vput(vp);
533 		return (error);
534 	}
535 
536 	if (fra->fs->fs_magic == FS_UFS1_MAGIC)
537 		*ip->i_din1 = *((struct ufs1_dinode *)bp->b_data +
538 		    ino_to_fsbo(fra->fs, ip->i_number));
539 #ifdef FFS2
540 	else
541 		*ip->i_din2 = *((struct ufs2_dinode *)bp->b_data +
542 		    ino_to_fsbo(fra->fs, ip->i_number));
543 #endif /* FFS2 */
544 	ip->i_effnlink = DIP(ip, nlink);
545 	brelse(bp);
546 	vput(vp);
547 	return (0);
548 }
549 
550 /*
551  * Reload all incore data for a filesystem (used after running fsck on
552  * the root filesystem and finding things to fix). The filesystem must
553  * be mounted read-only.
554  *
555  * Things to do to update the mount:
556  *	1) invalidate all cached meta-data.
557  *	2) re-read superblock from disk.
558  *	3) re-read summary information from disk.
559  *	4) invalidate all inactive vnodes.
560  *	5) invalidate all cached file data.
561  *	6) re-read inode data for all active vnodes.
562  */
563 int
564 ffs_reload(struct mount *mountp, struct ucred *cred, struct proc *p)
565 {
566 	struct vnode *devvp;
567 	caddr_t space;
568 	struct fs *fs, *newfs;
569 	int i, blks, size, error;
570 	int32_t *lp;
571 	struct buf *bp = NULL;
572 	struct ffs_reload_args fra;
573 
574 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
575 		return (EINVAL);
576 	/*
577 	 * Step 1: invalidate all cached meta-data.
578 	 */
579 	devvp = VFSTOUFS(mountp)->um_devvp;
580 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
581 	error = vinvalbuf(devvp, 0, cred, p, 0, INFSLP);
582 	VOP_UNLOCK(devvp);
583 	if (error)
584 		panic("ffs_reload: dirty1");
585 
586 	/*
587 	 * Step 2: re-read superblock from disk.
588 	 */
589 	fs = VFSTOUFS(mountp)->um_fs;
590 
591 	error = bread(devvp, fs->fs_sblockloc / DEV_BSIZE, SBSIZE, &bp);
592 	if (error) {
593 		brelse(bp);
594 		return (error);
595 	}
596 
597 	newfs = (struct fs *)bp->b_data;
598 	if (ffs_validate(newfs) == 0) {
599 		brelse(bp);
600 		return (EINVAL);
601 	}
602 
603 	/*
604 	 * Copy pointer fields back into superblock before copying in	XXX
605 	 * new superblock. These should really be in the ufsmount.	XXX
606 	 * Note that important parameters (eg fs_ncg) are unchanged.
607 	 */
608 	newfs->fs_csp = fs->fs_csp;
609 	newfs->fs_maxcluster = fs->fs_maxcluster;
610 	newfs->fs_ronly = fs->fs_ronly;
611 	memcpy(fs, newfs, fs->fs_sbsize);
612 	if (fs->fs_sbsize < SBSIZE)
613 		bp->b_flags |= B_INVAL;
614 	brelse(bp);
615 	VFSTOUFS(mountp)->um_maxsymlinklen = fs->fs_maxsymlinklen;
616 	ffs1_compat_read(fs, VFSTOUFS(mountp), fs->fs_sblockloc);
617 	ffs_oldfscompat(fs);
618 	(void)ffs_statfs(mountp, &mountp->mnt_stat, p);
619 	/*
620 	 * Step 3: re-read summary information from disk.
621 	 */
622 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
623 	space = (caddr_t)fs->fs_csp;
624 	for (i = 0; i < blks; i += fs->fs_frag) {
625 		size = fs->fs_bsize;
626 		if (i + fs->fs_frag > blks)
627 			size = (blks - i) * fs->fs_fsize;
628 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, &bp);
629 		if (error) {
630 			brelse(bp);
631 			return (error);
632 		}
633 		memcpy(space, bp->b_data, size);
634 		space += size;
635 		brelse(bp);
636 	}
637 	if ((fs->fs_flags & FS_DOSOFTDEP))
638 		(void) softdep_mount(devvp, mountp, fs, cred);
639 	/*
640 	 * We no longer know anything about clusters per cylinder group.
641 	 */
642 	if (fs->fs_contigsumsize > 0) {
643 		lp = fs->fs_maxcluster;
644 		for (i = 0; i < fs->fs_ncg; i++)
645 			*lp++ = fs->fs_contigsumsize;
646 	}
647 
648 	fra.p = p;
649 	fra.cred = cred;
650 	fra.fs = fs;
651 	fra.devvp = devvp;
652 
653 	error = vfs_mount_foreach_vnode(mountp, ffs_reload_vnode, &fra);
654 
655 	return (error);
656 }
657 
658 /*
659  * Checks if a super block is sane enough to be mounted.
660  */
661 int
662 ffs_validate(struct fs *fsp)
663 {
664 #ifdef FFS2
665 	if (fsp->fs_magic != FS_UFS2_MAGIC && fsp->fs_magic != FS_UFS1_MAGIC)
666 		return (0); /* Invalid magic */
667 #else
668 	if (fsp->fs_magic != FS_UFS1_MAGIC)
669 		return (0); /* Invalid magic */
670 #endif /* FFS2 */
671 
672 	if ((u_int)fsp->fs_bsize > MAXBSIZE)
673 		return (0); /* Invalid block size */
674 
675 	if ((u_int)fsp->fs_bsize < sizeof(struct fs))
676 		return (0); /* Invalid block size */
677 
678 	if ((u_int)fsp->fs_sbsize > SBSIZE)
679 		return (0); /* Invalid super block size */
680 
681 	if ((u_int)fsp->fs_frag > MAXFRAG || fragtbl[fsp->fs_frag] == NULL)
682 		return (0); /* Invalid number of fragments */
683 
684 	if (fsp->fs_inodefmt == FS_42INODEFMT)
685 		fsp->fs_maxsymlinklen = 0;
686 	else if (fsp->fs_maxsymlinklen < 0)
687 		return (0); /* Invalid max size of short symlink */
688 
689 	return (1); /* Super block is okay */
690 }
691 
692 /*
693  * Possible locations for the super-block.
694  */
695 const int sbtry[] = SBLOCKSEARCH;
696 
697 /*
698  * Common code for mount and mountroot
699  */
700 int
701 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p)
702 {
703 	struct ufsmount *ump;
704 	struct buf *bp;
705 	struct fs *fs;
706 	dev_t dev;
707 	caddr_t space;
708 	daddr_t sbloc;
709 	int error, i, blks, size, ronly;
710 	int32_t *lp;
711 	struct ucred *cred;
712 	u_int64_t maxfilesize;					/* XXX */
713 
714 	dev = devvp->v_rdev;
715 	cred = p ? p->p_ucred : NOCRED;
716 	/*
717 	 * Disallow multiple mounts of the same device.
718 	 * Disallow mounting of a device that is currently in use
719 	 * (except for root, which might share swap device for miniroot).
720 	 * Flush out any old buffers remaining from a previous use.
721 	 */
722 	if ((error = vfs_mountedon(devvp)) != 0)
723 		return (error);
724 	if (vcount(devvp) > 1 && devvp != rootvp)
725 		return (EBUSY);
726 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
727 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, INFSLP);
728 	VOP_UNLOCK(devvp);
729 	if (error)
730 		return (error);
731 
732 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
733 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
734 	if (error)
735 		return (error);
736 
737 	bp = NULL;
738 	ump = NULL;
739 
740 	/*
741 	 * Try reading the super-block in each of its possible locations.
742 	 */
743 	for (i = 0; sbtry[i] != -1; i++) {
744 		if (bp != NULL) {
745 			bp->b_flags |= B_NOCACHE;
746 			brelse(bp);
747 			bp = NULL;
748 		}
749 
750 		error = bread(devvp, sbtry[i] / DEV_BSIZE, SBSIZE, &bp);
751 		if (error)
752 			goto out;
753 
754 		fs = (struct fs *) bp->b_data;
755 		sbloc = sbtry[i];
756 
757 		/*
758 		 * Do not look for an FFS1 file system at SBLOCK_UFS2. Doing so
759 		 * will find the wrong super-block for file systems with 64k
760 		 * block size.
761 		 */
762 		if (fs->fs_magic == FS_UFS1_MAGIC && sbloc == SBLOCK_UFS2)
763 			continue;
764 
765 		if (ffs_validate(fs))
766 			break; /* Super block validated */
767 	}
768 
769 	if (sbtry[i] == -1) {
770 		error = EINVAL;
771 		goto out;
772 	}
773 
774 	fs->fs_fmod = 0;
775 	fs->fs_flags &= ~FS_UNCLEAN;
776 	if (fs->fs_clean == 0) {
777 #if 0
778 		/*
779 		 * It is safe to mount an unclean file system
780 		 * if it was previously mounted with softdep
781 		 * but we may lose space and must
782 		 * sometimes run fsck manually.
783 		 */
784 		if (fs->fs_flags & FS_DOSOFTDEP)
785 			printf(
786 "WARNING: %s was not properly unmounted\n",
787 			    fs->fs_fsmnt);
788 		else
789 #endif
790 		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
791 			printf(
792 "WARNING: %s was not properly unmounted\n",
793 			    fs->fs_fsmnt);
794 		} else {
795 			printf(
796 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
797 			    fs->fs_fsmnt);
798 			error = EROFS;
799 			goto out;
800 		}
801 	}
802 
803 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
804 #ifndef SMALL_KERNEL
805 		printf("ffs_mountfs(): obsolete rotational table format, "
806 		    "please use fsck_ffs(8) -c 1\n");
807 #endif
808 		error = EROFS;
809 		goto out;
810 	}
811 
812 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK|M_ZERO);
813 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
814 	    M_WAITOK);
815 
816 	if (fs->fs_magic == FS_UFS1_MAGIC)
817 		ump->um_fstype = UM_UFS1;
818 #ifdef FFS2
819 	else
820 		ump->um_fstype = UM_UFS2;
821 #endif
822 
823 	memcpy(ump->um_fs, bp->b_data, fs->fs_sbsize);
824 	if (fs->fs_sbsize < SBSIZE)
825 		bp->b_flags |= B_INVAL;
826 	brelse(bp);
827 	bp = NULL;
828 	fs = ump->um_fs;
829 
830 	ffs1_compat_read(fs, ump, sbloc);
831 
832 	if (fs->fs_clean == 0)
833 		fs->fs_flags |= FS_UNCLEAN;
834 	fs->fs_ronly = ronly;
835 	size = fs->fs_cssize;
836 	blks = howmany(size, fs->fs_fsize);
837 	if (fs->fs_contigsumsize > 0)
838 		size += fs->fs_ncg * sizeof(int32_t);
839 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
840 	fs->fs_csp = (struct csum *)space;
841 	for (i = 0; i < blks; i += fs->fs_frag) {
842 		size = fs->fs_bsize;
843 		if (i + fs->fs_frag > blks)
844 			size = (blks - i) * fs->fs_fsize;
845 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, &bp);
846 		if (error) {
847 			free(fs->fs_csp, M_UFSMNT, 0);
848 			goto out;
849 		}
850 		memcpy(space, bp->b_data, size);
851 		space += size;
852 		brelse(bp);
853 		bp = NULL;
854 	}
855 	if (fs->fs_contigsumsize > 0) {
856 		fs->fs_maxcluster = lp = (int32_t *)space;
857 		for (i = 0; i < fs->fs_ncg; i++)
858 			*lp++ = fs->fs_contigsumsize;
859 	}
860 	mp->mnt_data = ump;
861 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
862 	/* Use on-disk fsid if it exists, else fake it */
863 	if (fs->fs_id[0] != 0 && fs->fs_id[1] != 0)
864 		mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
865 	else
866 		mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
867 	mp->mnt_stat.f_namemax = MAXNAMLEN;
868 	mp->mnt_flag |= MNT_LOCAL;
869 	ump->um_mountp = mp;
870 	ump->um_dev = dev;
871 	ump->um_devvp = devvp;
872 	ump->um_nindir = fs->fs_nindir;
873 	ump->um_bptrtodb = fs->fs_fsbtodb;
874 	ump->um_seqinc = fs->fs_frag;
875 	ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
876 	for (i = 0; i < MAXQUOTAS; i++)
877 		ump->um_quotas[i] = NULLVP;
878 
879 	devvp->v_specmountpoint = mp;
880 	ffs_oldfscompat(fs);
881 
882 	if (ronly)
883 		fs->fs_contigdirs = NULL;
884 	else {
885 		fs->fs_contigdirs = malloc((u_long)fs->fs_ncg,
886 		    M_UFSMNT, M_WAITOK|M_ZERO);
887 	}
888 
889 	/*
890 	 * Set FS local "last mounted on" information (NULL pad)
891 	 */
892 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
893 	strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, sizeof(fs->fs_fsmnt));
894 
895 #if 0
896 	if( mp->mnt_flag & MNT_ROOTFS) {
897 		/*
898 		 * Root mount; update timestamp in mount structure.
899 		 * this will be used by the common root mount code
900 		 * to update the system clock.
901 		 */
902 		mp->mnt_time = fs->fs_time;
903 	}
904 #endif
905 
906 	/*
907 	 * XXX
908 	 * Limit max file size.  Even though ffs can handle files up to 16TB,
909 	 * we do limit the max file to 2^31 pages to prevent overflow of
910 	 * a 32-bit unsigned int.  The buffer cache has its own checks but
911 	 * a little added paranoia never hurts.
912 	 */
913 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
914 	maxfilesize = FS_KERNMAXFILESIZE(PAGE_SIZE, fs);
915 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
916 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
917 	if (ronly == 0) {
918 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
919 		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
920 			free(fs->fs_csp, M_UFSMNT, 0);
921 			free(fs->fs_contigdirs, M_UFSMNT, fs->fs_ncg);
922 			goto out;
923 		}
924 		fs->fs_fmod = 1;
925 		fs->fs_clean = 0;
926 		if (mp->mnt_flag & MNT_SOFTDEP)
927 			fs->fs_flags |= FS_DOSOFTDEP;
928 		else
929 			fs->fs_flags &= ~FS_DOSOFTDEP;
930 		error = ffs_sbupdate(ump, MNT_WAIT);
931 		if (error == EROFS)
932 			goto out;
933 	}
934 	return (0);
935 out:
936 	if (devvp->v_specinfo)
937 		devvp->v_specmountpoint = NULL;
938 	if (bp)
939 		brelse(bp);
940 
941 	vn_lock(devvp, LK_EXCLUSIVE|LK_RETRY);
942 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
943 	VOP_UNLOCK(devvp);
944 
945 	if (ump) {
946 		free(ump->um_fs, M_UFSMNT, ump->um_fs->fs_sbsize);
947 		free(ump, M_UFSMNT, sizeof(*ump));
948 		mp->mnt_data = NULL;
949 	}
950 	return (error);
951 }
952 
953 /*
954  * Sanity checks for old file systems.
955  */
956 int
957 ffs_oldfscompat(struct fs *fs)
958 {
959 	int i;
960 
961 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
962 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
963 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
964 		fs->fs_nrpos = 8;				/* XXX */
965 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
966 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
967 								/* XXX */
968 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
969 		for (i = 0; i < NIADDR; i++) {			/* XXX */
970 			sizepb *= NINDIR(fs);			/* XXX */
971 			fs->fs_maxfilesize += sizepb;		/* XXX */
972 		}						/* XXX */
973 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
974 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
975 	}							/* XXX */
976 	if (fs->fs_avgfilesize <= 0)				/* XXX */
977 		fs->fs_avgfilesize = AVFILESIZ;			/* XXX */
978 	if (fs->fs_avgfpdir <= 0)				/* XXX */
979 		fs->fs_avgfpdir = AFPDIR;			/* XXX */
980 	return (0);
981 }
982 
983 /*
984  * Auxiliary function for reading FFS1 super blocks.
985  */
986 void
987 ffs1_compat_read(struct fs *fs, struct ufsmount *ump, daddr_t sbloc)
988 {
989 	if (fs->fs_magic == FS_UFS2_MAGIC)
990 		return; /* UFS2 */
991 #if 0
992 	if (fs->fs_ffs1_flags & FS_FLAGS_UPDATED)
993 		return; /* Already updated */
994 #endif
995 	fs->fs_flags = fs->fs_ffs1_flags;
996 	fs->fs_sblockloc = sbloc;
997 	fs->fs_maxbsize = fs->fs_bsize;
998 	fs->fs_time = fs->fs_ffs1_time;
999 	fs->fs_size = fs->fs_ffs1_size;
1000 	fs->fs_dsize = fs->fs_ffs1_dsize;
1001 	fs->fs_csaddr = fs->fs_ffs1_csaddr;
1002 	fs->fs_cstotal.cs_ndir = fs->fs_ffs1_cstotal.cs_ndir;
1003 	fs->fs_cstotal.cs_nbfree = fs->fs_ffs1_cstotal.cs_nbfree;
1004 	fs->fs_cstotal.cs_nifree = fs->fs_ffs1_cstotal.cs_nifree;
1005 	fs->fs_cstotal.cs_nffree = fs->fs_ffs1_cstotal.cs_nffree;
1006 	fs->fs_ffs1_flags |= FS_FLAGS_UPDATED;
1007 }
1008 
1009 /*
1010  * Auxiliary function for writing FFS1 super blocks.
1011  */
1012 void
1013 ffs1_compat_write(struct fs *fs, struct ufsmount *ump)
1014 {
1015 	if (fs->fs_magic != FS_UFS1_MAGIC)
1016 		return; /* UFS2 */
1017 
1018 	fs->fs_ffs1_time = fs->fs_time;
1019 	fs->fs_ffs1_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1020 	fs->fs_ffs1_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1021 	fs->fs_ffs1_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1022 	fs->fs_ffs1_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1023 }
1024 
1025 /*
1026  * unmount system call
1027  */
1028 int
1029 ffs_unmount(struct mount *mp, int mntflags, struct proc *p)
1030 {
1031 	struct ufsmount *ump;
1032 	struct fs *fs;
1033 	int error, flags;
1034 
1035 	flags = 0;
1036 	if (mntflags & MNT_FORCE)
1037 		flags |= FORCECLOSE;
1038 
1039 	ump = VFSTOUFS(mp);
1040 	fs = ump->um_fs;
1041 	if (mp->mnt_flag & MNT_SOFTDEP)
1042 		error = softdep_flushfiles(mp, flags, p);
1043 	else
1044 		error = ffs_flushfiles(mp, flags, p);
1045 	if (error != 0)
1046 		return (error);
1047 
1048 	if (fs->fs_ronly == 0) {
1049 		fs->fs_clean = (fs->fs_flags & FS_UNCLEAN) ? 0 : 1;
1050 		error = ffs_sbupdate(ump, MNT_WAIT);
1051 		/* ignore write errors if mounted RW on read-only device */
1052 		if (error && error != EROFS) {
1053 			fs->fs_clean = 0;
1054 			return (error);
1055 		}
1056 		free(fs->fs_contigdirs, M_UFSMNT, fs->fs_ncg);
1057 	}
1058 	ump->um_devvp->v_specmountpoint = NULL;
1059 
1060 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1061 	vinvalbuf(ump->um_devvp, V_SAVE, NOCRED, p, 0, INFSLP);
1062 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
1063 	    NOCRED, p);
1064 	vput(ump->um_devvp);
1065 	free(fs->fs_csp, M_UFSMNT, 0);
1066 	free(fs, M_UFSMNT, fs->fs_sbsize);
1067 	free(ump, M_UFSMNT, sizeof(*ump));
1068 	mp->mnt_data = NULL;
1069 	mp->mnt_flag &= ~MNT_LOCAL;
1070 	return (0);
1071 }
1072 
1073 /*
1074  * Flush out all the files in a filesystem.
1075  */
1076 int
1077 ffs_flushfiles(struct mount *mp, int flags, struct proc *p)
1078 {
1079 	struct ufsmount *ump;
1080 	int error;
1081 
1082 	ump = VFSTOUFS(mp);
1083 	if (mp->mnt_flag & MNT_QUOTA) {
1084 		int i;
1085 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
1086 			return (error);
1087 		for (i = 0; i < MAXQUOTAS; i++) {
1088 			if (ump->um_quotas[i] == NULLVP)
1089 				continue;
1090 			quotaoff(p, mp, i);
1091 		}
1092 		/*
1093 		 * Here we fall through to vflush again to ensure
1094 		 * that we have gotten rid of all the system vnodes.
1095 		 */
1096 	}
1097 
1098 	/*
1099 	 * Flush all the files.
1100 	 */
1101 	if ((error = vflush(mp, NULL, flags)) != 0)
1102 		return (error);
1103 	/*
1104 	 * Flush filesystem metadata.
1105 	 */
1106 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1107 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, MNT_WAIT, p);
1108 	VOP_UNLOCK(ump->um_devvp);
1109 	return (error);
1110 }
1111 
1112 /*
1113  * Get file system statistics.
1114  */
1115 int
1116 ffs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
1117 {
1118 	struct ufsmount *ump;
1119 	struct fs *fs;
1120 
1121 	ump = VFSTOUFS(mp);
1122 	fs = ump->um_fs;
1123 
1124 #ifdef FFS2
1125 	if (fs->fs_magic != FS_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1126 		panic("ffs_statfs");
1127 #else
1128 	if (fs->fs_magic != FS_MAGIC)
1129 		panic("ffs_statfs");
1130 #endif /* FFS2 */
1131 
1132 	sbp->f_bsize = fs->fs_fsize;
1133 	sbp->f_iosize = fs->fs_bsize;
1134 	sbp->f_blocks = fs->fs_dsize;
1135 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1136 	    fs->fs_cstotal.cs_nffree;
1137 	sbp->f_bavail = sbp->f_bfree -
1138 	    ((int64_t)fs->fs_dsize * fs->fs_minfree / 100);
1139 	sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1140 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
1141 	sbp->f_favail = sbp->f_ffree;
1142 	copy_statfs_info(sbp, mp);
1143 
1144 	return (0);
1145 }
1146 
1147 struct ffs_sync_args {
1148 	int allerror;
1149 	struct proc *p;
1150 	int waitfor;
1151 	int nlink0;
1152 	int inflight;
1153 	struct ucred *cred;
1154 };
1155 
1156 int
1157 ffs_sync_vnode(struct vnode *vp, void *arg)
1158 {
1159 	struct ffs_sync_args *fsa = arg;
1160 	struct inode *ip;
1161 	int error, nlink0 = 0;
1162 
1163 	if (vp->v_type == VNON)
1164 		return (0);
1165 
1166 	ip = VTOI(vp);
1167 
1168 	/*
1169 	 * If unmounting or converting rw to ro, then stop deferring
1170 	 * timestamp writes.
1171 	 */
1172 	if (fsa->waitfor == MNT_WAIT && (ip->i_flag & IN_LAZYMOD)) {
1173 		ip->i_flag |= IN_MODIFIED;
1174 		UFS_UPDATE(ip, 1);
1175 	}
1176 
1177 	if (ip->i_effnlink == 0)
1178 		nlink0 = 1;
1179 
1180 	if ((ip->i_flag &
1181 	    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1182 	    LIST_EMPTY(&vp->v_dirtyblkhd)) {
1183 		goto end;
1184 	}
1185 
1186 	if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT)) {
1187 		fsa->inflight = MIN(fsa->inflight+1, 65536);
1188 		goto end;
1189 	}
1190 
1191 	if ((error = VOP_FSYNC(vp, fsa->cred, fsa->waitfor, fsa->p)))
1192 		fsa->allerror = error;
1193 	VOP_UNLOCK(vp);
1194 	vrele(vp);
1195 
1196 end:
1197 	fsa->nlink0 = MIN(fsa->nlink0 + nlink0, 65536);
1198 	return (0);
1199 }
1200 
1201 /*
1202  * Go through the disk queues to initiate sandbagged IO;
1203  * go through the inodes to write those that have been modified;
1204  * initiate the writing of the super block if it has been modified.
1205  *
1206  * Should always be called with the mount point locked.
1207  */
1208 int
1209 ffs_sync(struct mount *mp, int waitfor, int stall, struct ucred *cred, struct proc *p)
1210 {
1211 	struct ufsmount *ump = VFSTOUFS(mp);
1212 	struct fs *fs;
1213 	int error, allerror = 0, count, clean, fmod;
1214 	struct ffs_sync_args fsa;
1215 
1216 	fs = ump->um_fs;
1217 	/*
1218 	 * Write back modified superblock.
1219 	 * Consistency check that the superblock
1220 	 * is still in the buffer cache.
1221 	 */
1222 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {
1223 		printf("fs = %s\n", fs->fs_fsmnt);
1224 		panic("update: rofs mod");
1225 	}
1226  loop:
1227 	/*
1228 	 * Write back each (modified) inode.
1229 	 */
1230 	fsa.allerror = 0;
1231 	fsa.p = p;
1232 	fsa.cred = cred;
1233 	fsa.waitfor = waitfor;
1234 	fsa.nlink0 = 0;
1235 	fsa.inflight = 0;
1236 
1237 	/*
1238 	 * Don't traverse the vnode list if we want to skip all of them.
1239 	 */
1240 	if (waitfor != MNT_LAZY) {
1241 		vfs_mount_foreach_vnode(mp, ffs_sync_vnode, &fsa);
1242 		allerror = fsa.allerror;
1243 	}
1244 
1245 	/*
1246 	 * Force stale file system control information to be flushed.
1247 	 */
1248 	if ((ump->um_mountp->mnt_flag & MNT_SOFTDEP) && waitfor == MNT_WAIT) {
1249 		if ((error = softdep_flushworklist(ump->um_mountp, &count, p)))
1250 			allerror = error;
1251 		/* Flushed work items may create new vnodes to clean */
1252 		if (count)
1253 			goto loop;
1254 	}
1255 	if (waitfor != MNT_LAZY) {
1256 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1257 		if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
1258 			allerror = error;
1259 		VOP_UNLOCK(ump->um_devvp);
1260 	}
1261 	qsync(mp);
1262 	/*
1263 	 * Write back modified superblock.
1264 	 */
1265 	clean = fs->fs_clean;
1266 	fmod = fs->fs_fmod;
1267 	if (stall && fs->fs_ronly == 0) {
1268 		fs->fs_fmod = 1;
1269 		if (allerror == 0 && fsa.nlink0 == 0 && fsa.inflight == 0) {
1270 			fs->fs_clean = (fs->fs_flags & FS_UNCLEAN) ? 0 : 1;
1271 #if 0
1272 			printf("%s force clean (dangling %d inflight %d)\n",
1273 			    mp->mnt_stat.f_mntonname, fsa.nlink0, fsa.inflight);
1274 #endif
1275 		} else {
1276 			fs->fs_clean = 0;
1277 #if 0
1278 			printf("%s force dirty (dangling %d inflight %d)\n",
1279 			    mp->mnt_stat.f_mntonname, fsa.nlink0, fsa.inflight);
1280 #endif
1281 		}
1282 	}
1283 	if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1284 		allerror = error;
1285 	fs->fs_clean = clean;
1286 	fs->fs_fmod = fmod;
1287 
1288 	return (allerror);
1289 }
1290 
1291 /*
1292  * Look up a FFS dinode number to find its incore vnode, otherwise read it
1293  * in from disk.  If it is in core, wait for the lock bit to clear, then
1294  * return the inode locked.  Detection and handling of mount points must be
1295  * done by the calling routine.
1296  */
1297 int
1298 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1299 {
1300 	struct fs *fs;
1301 	struct inode *ip;
1302 	struct ufs1_dinode *dp1;
1303 #ifdef FFS2
1304 	struct ufs2_dinode *dp2;
1305 #endif
1306 	struct ufsmount *ump;
1307 	struct buf *bp;
1308 	struct vnode *vp;
1309 	dev_t dev;
1310 	int error;
1311 
1312 	if (ino > (ufsino_t)-1)
1313 		panic("ffs_vget: alien ino_t %llu", (unsigned long long)ino);
1314 
1315 	ump = VFSTOUFS(mp);
1316 	dev = ump->um_dev;
1317 retry:
1318 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
1319 		return (0);
1320 
1321 	/* Allocate a new vnode/inode. */
1322 	if ((error = getnewvnode(VT_UFS, mp, &ffs_vops, &vp)) != 0) {
1323 		*vpp = NULL;
1324 		return (error);
1325 	}
1326 
1327 #ifdef VFSLCKDEBUG
1328 	vp->v_flag |= VLOCKSWORK;
1329 #endif
1330 	ip = pool_get(&ffs_ino_pool, PR_WAITOK|PR_ZERO);
1331 	rrw_init_flags(&ip->i_lock, "inode", RWL_DUPOK | RWL_IS_VNODE);
1332 	ip->i_ump = ump;
1333 	vref(ip->i_devvp);
1334 	vp->v_data = ip;
1335 	ip->i_vnode = vp;
1336 	ip->i_fs = fs = ump->um_fs;
1337 	ip->i_dev = dev;
1338 	ip->i_number = ino;
1339 	ip->i_vtbl = &ffs_vtbl;
1340 
1341 	/*
1342 	 * Put it onto its hash chain and lock it so that other requests for
1343 	 * this inode will block if they arrive while we are sleeping waiting
1344 	 * for old data structures to be purged or for the contents of the
1345 	 * disk portion of this inode to be read.
1346 	 */
1347 	error = ufs_ihashins(ip);
1348 
1349 	if (error) {
1350 		/*
1351 		 * VOP_INACTIVE will treat this as a stale file
1352 		 * and recycle it quickly
1353 		 */
1354 		vrele(vp);
1355 
1356 		if (error == EEXIST)
1357 			goto retry;
1358 
1359 		return (error);
1360 	}
1361 
1362 
1363 	/* Read in the disk contents for the inode, copy into the inode. */
1364 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1365 	    (int)fs->fs_bsize, &bp);
1366 	if (error) {
1367 		/*
1368 		 * The inode does not contain anything useful, so it would
1369 		 * be misleading to leave it on its hash chain. With mode
1370 		 * still zero, it will be unlinked and returned to the free
1371 		 * list by vput().
1372 		 */
1373 		vput(vp);
1374 		brelse(bp);
1375 		*vpp = NULL;
1376 		return (error);
1377 	}
1378 
1379 #ifdef FFS2
1380 	if (ip->i_ump->um_fstype == UM_UFS2) {
1381 		ip->i_din2 = pool_get(&ffs_dinode2_pool, PR_WAITOK);
1382 		dp2 = (struct ufs2_dinode *) bp->b_data + ino_to_fsbo(fs, ino);
1383 		*ip->i_din2 = *dp2;
1384 	} else
1385 #endif
1386 	{
1387 		ip->i_din1 = pool_get(&ffs_dinode1_pool, PR_WAITOK);
1388 		dp1 = (struct ufs1_dinode *) bp->b_data + ino_to_fsbo(fs, ino);
1389 		*ip->i_din1 = *dp1;
1390 	}
1391 
1392 	brelse(bp);
1393 
1394 	if (DOINGSOFTDEP(vp))
1395 		softdep_load_inodeblock(ip);
1396 	else
1397 		ip->i_effnlink = DIP(ip, nlink);
1398 
1399 	/*
1400 	 * Initialize the vnode from the inode, check for aliases.
1401 	 * Note that the underlying vnode may have changed.
1402 	 */
1403 	if ((error = ffs_vinit(mp, &vp)) != 0) {
1404 		vput(vp);
1405 		*vpp = NULL;
1406 		return (error);
1407 	}
1408 
1409 	/*
1410 	 * Set up a generation number for this inode if it does not
1411 	 * already have one. This should only happen on old filesystems.
1412 	 */
1413 	if (DIP(ip, gen) == 0) {
1414 		while (DIP(ip, gen) == 0)
1415 			DIP_ASSIGN(ip, gen, arc4random());
1416 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1417 			ip->i_flag |= IN_MODIFIED;
1418 	}
1419 
1420 	/*
1421 	 * Ensure that uid and gid are correct. This is a temporary
1422 	 * fix until fsck has been changed to do the update.
1423 	 */
1424 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_inodefmt < FS_44INODEFMT) {
1425 		ip->i_ffs1_uid = ip->i_din1->di_ouid;
1426 		ip->i_ffs1_gid = ip->i_din1->di_ogid;
1427 	}
1428 
1429 	*vpp = vp;
1430 
1431 	return (0);
1432 }
1433 
1434 /*
1435  * File handle to vnode
1436  *
1437  * Have to be really careful about stale file handles.
1438  */
1439 int
1440 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1441 {
1442 	struct ufid *ufhp;
1443 	int error;
1444 
1445 	ufhp = (struct ufid *)fhp;
1446 	if (ufhp->ufid_len != sizeof(*ufhp))
1447 		return EINVAL;
1448 
1449 	if ((error = ffs_checkrange(mp, ufhp->ufid_ino)) != 0)
1450 		return error;
1451 
1452 	return (ufs_fhtovp(mp, ufhp, vpp));
1453 }
1454 
1455 /*
1456  * Vnode pointer to File handle
1457  */
1458 int
1459 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1460 {
1461 	struct inode *ip;
1462 	struct ufid *ufhp;
1463 
1464 	ip = VTOI(vp);
1465 	ufhp = (struct ufid *)fhp;
1466 	ufhp->ufid_len = sizeof(struct ufid);
1467 	ufhp->ufid_ino = ip->i_number;
1468 	ufhp->ufid_gen = DIP(ip, gen);
1469 
1470 	return (0);
1471 }
1472 
1473 /*
1474  * Write a superblock and associated information back to disk.
1475  */
1476 int
1477 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1478 {
1479 	struct fs *dfs, *fs = mp->um_fs;
1480 	struct buf *bp;
1481 	int blks;
1482 	caddr_t space;
1483 	int i, size, error, allerror = 0;
1484 
1485 	/*
1486 	 * First write back the summary information.
1487 	 */
1488 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1489 	space = (caddr_t)fs->fs_csp;
1490 	for (i = 0; i < blks; i += fs->fs_frag) {
1491 		size = fs->fs_bsize;
1492 		if (i + fs->fs_frag > blks)
1493 			size = (blks - i) * fs->fs_fsize;
1494 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1495 		    size, 0, INFSLP);
1496 		memcpy(bp->b_data, space, size);
1497 		space += size;
1498 		if (waitfor != MNT_WAIT)
1499 			bawrite(bp);
1500 		else if ((error = bwrite(bp)))
1501 			allerror = error;
1502 	}
1503 
1504 	/*
1505 	 * Now write back the superblock itself. If any errors occurred
1506 	 * up to this point, then fail so that the superblock avoids
1507 	 * being written out as clean.
1508 	 */
1509 	if (allerror) {
1510 		return (allerror);
1511 	}
1512 
1513 	bp = getblk(mp->um_devvp,
1514 	    fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
1515 	    (int)fs->fs_sbsize, 0, INFSLP);
1516 	fs->fs_fmod = 0;
1517 	fs->fs_time = gettime();
1518 	memcpy(bp->b_data, fs, fs->fs_sbsize);
1519 	/* Restore compatibility to old file systems.		   XXX */
1520 	dfs = (struct fs *)bp->b_data;				/* XXX */
1521 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
1522 		dfs->fs_nrpos = -1;				/* XXX */
1523 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
1524 		int32_t *lp, tmp;				/* XXX */
1525 								/* XXX */
1526 		lp = (int32_t *)&dfs->fs_qbmask;		/* XXX */
1527 		tmp = lp[4];					/* XXX */
1528 		for (i = 4; i > 0; i--)				/* XXX */
1529 			lp[i] = lp[i-1];			/* XXX */
1530 		lp[0] = tmp;					/* XXX */
1531 	}							/* XXX */
1532 	dfs->fs_maxfilesize = mp->um_savedmaxfilesize;		/* XXX */
1533 
1534 	ffs1_compat_write(dfs, mp);
1535 
1536 	if (waitfor != MNT_WAIT)
1537 		bawrite(bp);
1538 	else if ((error = bwrite(bp)))
1539 		allerror = error;
1540 
1541 	return (allerror);
1542 }
1543 
1544 int
1545 ffs_init(struct vfsconf *vfsp)
1546 {
1547 	static int done;
1548 
1549 	if (done)
1550 		return (0);
1551 
1552 	done = 1;
1553 
1554 	pool_init(&ffs_ino_pool, sizeof(struct inode), 0, IPL_NONE,
1555 	    PR_WAITOK, "ffsino", NULL);
1556 	pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, IPL_NONE,
1557 	    PR_WAITOK, "dino1pl", NULL);
1558 #ifdef FFS2
1559 	pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, IPL_NONE,
1560 	    PR_WAITOK, "dino2pl", NULL);
1561 #endif
1562 
1563 	softdep_initialize();
1564 
1565 	return (ufs_init(vfsp));
1566 }
1567 
1568 #ifdef FFS_SOFTUPDATES
1569 extern int max_softdeps, tickdelay, stat_worklist_push;
1570 extern int stat_blk_limit_push, stat_ino_limit_push, stat_blk_limit_hit;
1571 extern int stat_ino_limit_hit, stat_sync_limit_hit, stat_indir_blk_ptrs;
1572 extern int stat_inode_bitmap, stat_direct_blk_ptrs, stat_dir_entry;
1573 #endif
1574 const struct sysctl_bounded_args ffs_vars[] = {
1575 #ifdef FFS_SOFTUPDATES
1576 	{ FFS_MAX_SOFTDEPS, &max_softdeps, 0, INT_MAX },
1577 	{ FFS_SD_TICKDELAY, &tickdelay, 2, INT_MAX },
1578 	{ FFS_SD_WORKLIST_PUSH, &stat_worklist_push, SYSCTL_INT_READONLY },
1579 	{ FFS_SD_BLK_LIMIT_PUSH, &stat_blk_limit_push, SYSCTL_INT_READONLY },
1580 	{ FFS_SD_INO_LIMIT_PUSH, &stat_ino_limit_push, SYSCTL_INT_READONLY },
1581 	{ FFS_SD_BLK_LIMIT_HIT, &stat_blk_limit_hit, SYSCTL_INT_READONLY },
1582 	{ FFS_SD_INO_LIMIT_HIT, &stat_ino_limit_hit, SYSCTL_INT_READONLY },
1583 	{ FFS_SD_SYNC_LIMIT_HIT, &stat_sync_limit_hit, SYSCTL_INT_READONLY },
1584 	{ FFS_SD_INDIR_BLK_PTRS, &stat_indir_blk_ptrs, SYSCTL_INT_READONLY },
1585 	{ FFS_SD_INODE_BITMAP, &stat_inode_bitmap, SYSCTL_INT_READONLY },
1586 	{ FFS_SD_DIRECT_BLK_PTRS, &stat_direct_blk_ptrs, SYSCTL_INT_READONLY },
1587 	{ FFS_SD_DIR_ENTRY, &stat_dir_entry, SYSCTL_INT_READONLY },
1588 #endif
1589 #ifdef UFS_DIRHASH
1590 	{ FFS_DIRHASH_DIRSIZE, &ufs_mindirhashsize, 0, INT_MAX },
1591 	{ FFS_DIRHASH_MAXMEM, &ufs_dirhashmaxmem, 0, INT_MAX },
1592 	{ FFS_DIRHASH_MEM, &ufs_dirhashmem, SYSCTL_INT_READONLY },
1593 #endif
1594 };
1595 
1596 /*
1597  * fast filesystem related variables.
1598  */
1599 int
1600 ffs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1601     size_t newlen, struct proc *p)
1602 {
1603 	return sysctl_bounded_arr(ffs_vars, nitems(ffs_vars), name,
1604 	    namelen, oldp, oldlenp, newp, newlen);
1605 }
1606