xref: /openbsd/sys/ufs/ffs/ffs_vfsops.c (revision 274d7c50)
1 /*	$OpenBSD: ffs_vfsops.c,v 1.181 2019/07/25 01:43:21 cheloha 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 	ffs_mount,
77 	ufs_start,
78 	ffs_unmount,
79 	ufs_root,
80 	ufs_quotactl,
81 	ffs_statfs,
82 	ffs_sync,
83 	ffs_vget,
84 	ffs_fhtovp,
85 	ffs_vptofh,
86 	ffs_init,
87 	ffs_sysctl,
88 	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 	*ip->i_din1 = *((struct ufs1_dinode *)bp->b_data +
537 	    ino_to_fsbo(fra->fs, ip->i_number));
538 	ip->i_effnlink = DIP(ip, nlink);
539 	brelse(bp);
540 	vput(vp);
541 	return (0);
542 }
543 
544 /*
545  * Reload all incore data for a filesystem (used after running fsck on
546  * the root filesystem and finding things to fix). The filesystem must
547  * be mounted read-only.
548  *
549  * Things to do to update the mount:
550  *	1) invalidate all cached meta-data.
551  *	2) re-read superblock from disk.
552  *	3) re-read summary information from disk.
553  *	4) invalidate all inactive vnodes.
554  *	5) invalidate all cached file data.
555  *	6) re-read inode data for all active vnodes.
556  */
557 int
558 ffs_reload(struct mount *mountp, struct ucred *cred, struct proc *p)
559 {
560 	struct vnode *devvp;
561 	caddr_t space;
562 	struct fs *fs, *newfs;
563 	int i, blks, size, error;
564 	int32_t *lp;
565 	struct buf *bp = NULL;
566 	struct ffs_reload_args fra;
567 
568 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
569 		return (EINVAL);
570 	/*
571 	 * Step 1: invalidate all cached meta-data.
572 	 */
573 	devvp = VFSTOUFS(mountp)->um_devvp;
574 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
575 	error = vinvalbuf(devvp, 0, cred, p, 0, INFSLP);
576 	VOP_UNLOCK(devvp);
577 	if (error)
578 		panic("ffs_reload: dirty1");
579 
580 	/*
581 	 * Step 2: re-read superblock from disk.
582 	 */
583 	fs = VFSTOUFS(mountp)->um_fs;
584 
585 	error = bread(devvp, fs->fs_sblockloc / DEV_BSIZE, SBSIZE, &bp);
586 	if (error) {
587 		brelse(bp);
588 		return (error);
589 	}
590 
591 	newfs = (struct fs *)bp->b_data;
592 	if (ffs_validate(newfs) == 0) {
593 		brelse(bp);
594 		return (EINVAL);
595 	}
596 
597 	/*
598 	 * Copy pointer fields back into superblock before copying in	XXX
599 	 * new superblock. These should really be in the ufsmount.	XXX
600 	 * Note that important parameters (eg fs_ncg) are unchanged.
601 	 */
602 	newfs->fs_csp = fs->fs_csp;
603 	newfs->fs_maxcluster = fs->fs_maxcluster;
604 	newfs->fs_ronly = fs->fs_ronly;
605 	memcpy(fs, newfs, fs->fs_sbsize);
606 	if (fs->fs_sbsize < SBSIZE)
607 		bp->b_flags |= B_INVAL;
608 	brelse(bp);
609 	VFSTOUFS(mountp)->um_maxsymlinklen = fs->fs_maxsymlinklen;
610 	ffs1_compat_read(fs, VFSTOUFS(mountp), fs->fs_sblockloc);
611 	ffs_oldfscompat(fs);
612 	(void)ffs_statfs(mountp, &mountp->mnt_stat, p);
613 	/*
614 	 * Step 3: re-read summary information from disk.
615 	 */
616 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
617 	space = (caddr_t)fs->fs_csp;
618 	for (i = 0; i < blks; i += fs->fs_frag) {
619 		size = fs->fs_bsize;
620 		if (i + fs->fs_frag > blks)
621 			size = (blks - i) * fs->fs_fsize;
622 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, &bp);
623 		if (error) {
624 			brelse(bp);
625 			return (error);
626 		}
627 		memcpy(space, bp->b_data, size);
628 		space += size;
629 		brelse(bp);
630 	}
631 	if ((fs->fs_flags & FS_DOSOFTDEP))
632 		(void) softdep_mount(devvp, mountp, fs, cred);
633 	/*
634 	 * We no longer know anything about clusters per cylinder group.
635 	 */
636 	if (fs->fs_contigsumsize > 0) {
637 		lp = fs->fs_maxcluster;
638 		for (i = 0; i < fs->fs_ncg; i++)
639 			*lp++ = fs->fs_contigsumsize;
640 	}
641 
642 	fra.p = p;
643 	fra.cred = cred;
644 	fra.fs = fs;
645 	fra.devvp = devvp;
646 
647 	error = vfs_mount_foreach_vnode(mountp, ffs_reload_vnode, &fra);
648 
649 	return (error);
650 }
651 
652 /*
653  * Checks if a super block is sane enough to be mounted.
654  */
655 int
656 ffs_validate(struct fs *fsp)
657 {
658 #ifdef FFS2
659 	if (fsp->fs_magic != FS_UFS2_MAGIC && fsp->fs_magic != FS_UFS1_MAGIC)
660 		return (0); /* Invalid magic */
661 #else
662 	if (fsp->fs_magic != FS_UFS1_MAGIC)
663 		return (0); /* Invalid magic */
664 #endif /* FFS2 */
665 
666 	if ((u_int)fsp->fs_bsize > MAXBSIZE)
667 		return (0); /* Invalid block size */
668 
669 	if ((u_int)fsp->fs_bsize < sizeof(struct fs))
670 		return (0); /* Invalid block size */
671 
672 	if ((u_int)fsp->fs_sbsize > SBSIZE)
673 		return (0); /* Invalid super block size */
674 
675 	if ((u_int)fsp->fs_frag > MAXFRAG || fragtbl[fsp->fs_frag] == NULL)
676 		return (0); /* Invalid number of fragments */
677 
678 	if (fsp->fs_inodefmt == FS_42INODEFMT)
679 		fsp->fs_maxsymlinklen = 0;
680 	else if (fsp->fs_maxsymlinklen < 0)
681 		return (0); /* Invalid max size of short symlink */
682 
683 	return (1); /* Super block is okay */
684 }
685 
686 /*
687  * Possible locations for the super-block.
688  */
689 const int sbtry[] = SBLOCKSEARCH;
690 
691 /*
692  * Common code for mount and mountroot
693  */
694 int
695 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p)
696 {
697 	struct ufsmount *ump;
698 	struct buf *bp;
699 	struct fs *fs;
700 	dev_t dev;
701 	caddr_t space;
702 	daddr_t sbloc;
703 	int error, i, blks, size, ronly;
704 	int32_t *lp;
705 	struct ucred *cred;
706 	u_int64_t maxfilesize;					/* XXX */
707 
708 	dev = devvp->v_rdev;
709 	cred = p ? p->p_ucred : NOCRED;
710 	/*
711 	 * Disallow multiple mounts of the same device.
712 	 * Disallow mounting of a device that is currently in use
713 	 * (except for root, which might share swap device for miniroot).
714 	 * Flush out any old buffers remaining from a previous use.
715 	 */
716 	if ((error = vfs_mountedon(devvp)) != 0)
717 		return (error);
718 	if (vcount(devvp) > 1 && devvp != rootvp)
719 		return (EBUSY);
720 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
721 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, INFSLP);
722 	VOP_UNLOCK(devvp);
723 	if (error)
724 		return (error);
725 
726 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
727 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
728 	if (error)
729 		return (error);
730 
731 	bp = NULL;
732 	ump = NULL;
733 
734 	/*
735 	 * Try reading the super-block in each of its possible locations.
736 	 */
737 	for (i = 0; sbtry[i] != -1; i++) {
738 		if (bp != NULL) {
739 			bp->b_flags |= B_NOCACHE;
740 			brelse(bp);
741 			bp = NULL;
742 		}
743 
744 		error = bread(devvp, sbtry[i] / DEV_BSIZE, SBSIZE, &bp);
745 		if (error)
746 			goto out;
747 
748 		fs = (struct fs *) bp->b_data;
749 		sbloc = sbtry[i];
750 
751 #if 0
752 		if (fs->fs_magic == FS_UFS2_MAGIC) {
753 			printf("ffs_mountfs(): Sorry, no UFS2 support (yet)\n");
754 			error = EFTYPE;
755 			goto out;
756 		}
757 #endif
758 
759 		/*
760 		 * Do not look for an FFS1 file system at SBLOCK_UFS2. Doing so
761 		 * will find the wrong super-block for file systems with 64k
762 		 * block size.
763 		 */
764 		if (fs->fs_magic == FS_UFS1_MAGIC && sbloc == SBLOCK_UFS2)
765 			continue;
766 
767 		if (ffs_validate(fs))
768 			break; /* Super block validated */
769 	}
770 
771 	if (sbtry[i] == -1) {
772 		error = EINVAL;
773 		goto out;
774 	}
775 
776 	fs->fs_fmod = 0;
777 	fs->fs_flags &= ~FS_UNCLEAN;
778 	if (fs->fs_clean == 0) {
779 #if 0
780 		/*
781 		 * It is safe to mount an unclean file system
782 		 * if it was previously mounted with softdep
783 		 * but we may lose space and must
784 		 * sometimes run fsck manually.
785 		 */
786 		if (fs->fs_flags & FS_DOSOFTDEP)
787 			printf(
788 "WARNING: %s was not properly unmounted\n",
789 			    fs->fs_fsmnt);
790 		else
791 #endif
792 		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
793 			printf(
794 "WARNING: %s was not properly unmounted\n",
795 			    fs->fs_fsmnt);
796 		} else {
797 			printf(
798 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
799 			    fs->fs_fsmnt);
800 			error = EROFS;
801 			goto out;
802 		}
803 	}
804 
805 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
806 #ifndef SMALL_KERNEL
807 		printf("ffs_mountfs(): obsolete rotational table format, "
808 		    "please use fsck_ffs(8) -c 1\n");
809 #endif
810 		error = EFTYPE;
811 		goto out;
812 	}
813 
814 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK|M_ZERO);
815 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
816 	    M_WAITOK);
817 
818 	if (fs->fs_magic == FS_UFS1_MAGIC)
819 		ump->um_fstype = UM_UFS1;
820 #ifdef FFS2
821 	else
822 		ump->um_fstype = UM_UFS2;
823 #endif
824 
825 	memcpy(ump->um_fs, bp->b_data, fs->fs_sbsize);
826 	if (fs->fs_sbsize < SBSIZE)
827 		bp->b_flags |= B_INVAL;
828 	brelse(bp);
829 	bp = NULL;
830 	fs = ump->um_fs;
831 
832 	ffs1_compat_read(fs, ump, sbloc);
833 
834 	if (fs->fs_clean == 0)
835 		fs->fs_flags |= FS_UNCLEAN;
836 	fs->fs_ronly = ronly;
837 	size = fs->fs_cssize;
838 	blks = howmany(size, fs->fs_fsize);
839 	if (fs->fs_contigsumsize > 0)
840 		size += fs->fs_ncg * sizeof(int32_t);
841 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
842 	fs->fs_csp = (struct csum *)space;
843 	for (i = 0; i < blks; i += fs->fs_frag) {
844 		size = fs->fs_bsize;
845 		if (i + fs->fs_frag > blks)
846 			size = (blks - i) * fs->fs_fsize;
847 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, &bp);
848 		if (error) {
849 			free(fs->fs_csp, M_UFSMNT, 0);
850 			goto out;
851 		}
852 		memcpy(space, bp->b_data, size);
853 		space += size;
854 		brelse(bp);
855 		bp = NULL;
856 	}
857 	if (fs->fs_contigsumsize > 0) {
858 		fs->fs_maxcluster = lp = (int32_t *)space;
859 		for (i = 0; i < fs->fs_ncg; i++)
860 			*lp++ = fs->fs_contigsumsize;
861 	}
862 	mp->mnt_data = ump;
863 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
864 	/* Use on-disk fsid if it exists, else fake it */
865 	if (fs->fs_id[0] != 0 && fs->fs_id[1] != 0)
866 		mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
867 	else
868 		mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
869 	mp->mnt_stat.f_namemax = MAXNAMLEN;
870 	mp->mnt_flag |= MNT_LOCAL;
871 	ump->um_mountp = mp;
872 	ump->um_dev = dev;
873 	ump->um_devvp = devvp;
874 	ump->um_nindir = fs->fs_nindir;
875 	ump->um_bptrtodb = fs->fs_fsbtodb;
876 	ump->um_seqinc = fs->fs_frag;
877 	ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
878 	for (i = 0; i < MAXQUOTAS; i++)
879 		ump->um_quotas[i] = NULLVP;
880 
881 	devvp->v_specmountpoint = mp;
882 	ffs_oldfscompat(fs);
883 
884 	if (ronly)
885 		fs->fs_contigdirs = NULL;
886 	else {
887 		fs->fs_contigdirs = malloc((u_long)fs->fs_ncg,
888 		    M_UFSMNT, M_WAITOK|M_ZERO);
889 	}
890 
891 	/*
892 	 * Set FS local "last mounted on" information (NULL pad)
893 	 */
894 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
895 	strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, sizeof(fs->fs_fsmnt));
896 
897 #if 0
898 	if( mp->mnt_flag & MNT_ROOTFS) {
899 		/*
900 		 * Root mount; update timestamp in mount structure.
901 		 * this will be used by the common root mount code
902 		 * to update the system clock.
903 		 */
904 		mp->mnt_time = fs->fs_time;
905 	}
906 #endif
907 
908 	/*
909 	 * XXX
910 	 * Limit max file size.  Even though ffs can handle files up to 16TB,
911 	 * we do limit the max file to 2^31 pages to prevent overflow of
912 	 * a 32-bit unsigned int.  The buffer cache has its own checks but
913 	 * a little added paranoia never hurts.
914 	 */
915 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
916 	maxfilesize = FS_KERNMAXFILESIZE(PAGE_SIZE, fs);
917 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
918 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
919 	if (ronly == 0) {
920 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
921 		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
922 			free(fs->fs_csp, M_UFSMNT, 0);
923 			free(fs->fs_contigdirs, M_UFSMNT, fs->fs_ncg);
924 			goto out;
925 		}
926 		fs->fs_fmod = 1;
927 		fs->fs_clean = 0;
928 		if (mp->mnt_flag & MNT_SOFTDEP)
929 			fs->fs_flags |= FS_DOSOFTDEP;
930 		else
931 			fs->fs_flags &= ~FS_DOSOFTDEP;
932 		error = ffs_sbupdate(ump, MNT_WAIT);
933 		if (error == EROFS)
934 			goto out;
935 	}
936 	return (0);
937 out:
938 	if (devvp->v_specinfo)
939 		devvp->v_specmountpoint = NULL;
940 	if (bp)
941 		brelse(bp);
942 
943 	vn_lock(devvp, LK_EXCLUSIVE|LK_RETRY);
944 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
945 	VOP_UNLOCK(devvp);
946 
947 	if (ump) {
948 		free(ump->um_fs, M_UFSMNT, ump->um_fs->fs_sbsize);
949 		free(ump, M_UFSMNT, sizeof(*ump));
950 		mp->mnt_data = NULL;
951 	}
952 	return (error);
953 }
954 
955 /*
956  * Sanity checks for old file systems.
957  */
958 int
959 ffs_oldfscompat(struct fs *fs)
960 {
961 	int i;
962 
963 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
964 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
965 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
966 		fs->fs_nrpos = 8;				/* XXX */
967 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
968 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
969 								/* XXX */
970 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
971 		for (i = 0; i < NIADDR; i++) {			/* XXX */
972 			sizepb *= NINDIR(fs);			/* XXX */
973 			fs->fs_maxfilesize += sizepb;		/* XXX */
974 		}						/* XXX */
975 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
976 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
977 	}							/* XXX */
978 	if (fs->fs_avgfilesize <= 0)				/* XXX */
979 		fs->fs_avgfilesize = AVFILESIZ;			/* XXX */
980 	if (fs->fs_avgfpdir <= 0)				/* XXX */
981 		fs->fs_avgfpdir = AFPDIR;			/* XXX */
982 	return (0);
983 }
984 
985 /*
986  * Auxiliary function for reading FFS1 super blocks.
987  */
988 void
989 ffs1_compat_read(struct fs *fs, struct ufsmount *ump, daddr_t sbloc)
990 {
991 	if (fs->fs_magic == FS_UFS2_MAGIC)
992 		return; /* UFS2 */
993 #if 0
994 	if (fs->fs_ffs1_flags & FS_FLAGS_UPDATED)
995 		return; /* Already updated */
996 #endif
997 	fs->fs_flags = fs->fs_ffs1_flags;
998 	fs->fs_sblockloc = sbloc;
999 	fs->fs_maxbsize = fs->fs_bsize;
1000 	fs->fs_time = fs->fs_ffs1_time;
1001 	fs->fs_size = fs->fs_ffs1_size;
1002 	fs->fs_dsize = fs->fs_ffs1_dsize;
1003 	fs->fs_csaddr = fs->fs_ffs1_csaddr;
1004 	fs->fs_cstotal.cs_ndir = fs->fs_ffs1_cstotal.cs_ndir;
1005 	fs->fs_cstotal.cs_nbfree = fs->fs_ffs1_cstotal.cs_nbfree;
1006 	fs->fs_cstotal.cs_nifree = fs->fs_ffs1_cstotal.cs_nifree;
1007 	fs->fs_cstotal.cs_nffree = fs->fs_ffs1_cstotal.cs_nffree;
1008 	fs->fs_ffs1_flags |= FS_FLAGS_UPDATED;
1009 }
1010 
1011 /*
1012  * Auxiliary function for writing FFS1 super blocks.
1013  */
1014 void
1015 ffs1_compat_write(struct fs *fs, struct ufsmount *ump)
1016 {
1017 	if (fs->fs_magic != FS_UFS1_MAGIC)
1018 		return; /* UFS2 */
1019 
1020 	fs->fs_ffs1_time = fs->fs_time;
1021 	fs->fs_ffs1_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1022 	fs->fs_ffs1_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1023 	fs->fs_ffs1_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1024 	fs->fs_ffs1_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1025 }
1026 
1027 /*
1028  * unmount system call
1029  */
1030 int
1031 ffs_unmount(struct mount *mp, int mntflags, struct proc *p)
1032 {
1033 	struct ufsmount *ump;
1034 	struct fs *fs;
1035 	int error, flags;
1036 
1037 	flags = 0;
1038 	if (mntflags & MNT_FORCE)
1039 		flags |= FORCECLOSE;
1040 
1041 	ump = VFSTOUFS(mp);
1042 	fs = ump->um_fs;
1043 	if (mp->mnt_flag & MNT_SOFTDEP)
1044 		error = softdep_flushfiles(mp, flags, p);
1045 	else
1046 		error = ffs_flushfiles(mp, flags, p);
1047 	if (error != 0)
1048 		return (error);
1049 
1050 	if (fs->fs_ronly == 0) {
1051 		fs->fs_clean = (fs->fs_flags & FS_UNCLEAN) ? 0 : 1;
1052 		error = ffs_sbupdate(ump, MNT_WAIT);
1053 		/* ignore write errors if mounted RW on read-only device */
1054 		if (error && error != EROFS) {
1055 			fs->fs_clean = 0;
1056 			return (error);
1057 		}
1058 		free(fs->fs_contigdirs, M_UFSMNT, fs->fs_ncg);
1059 	}
1060 	ump->um_devvp->v_specmountpoint = NULL;
1061 
1062 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1063 	vinvalbuf(ump->um_devvp, V_SAVE, NOCRED, p, 0, INFSLP);
1064 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
1065 	    NOCRED, p);
1066 	vput(ump->um_devvp);
1067 	free(fs->fs_csp, M_UFSMNT, 0);
1068 	free(fs, M_UFSMNT, fs->fs_sbsize);
1069 	free(ump, M_UFSMNT, sizeof(*ump));
1070 	mp->mnt_data = NULL;
1071 	mp->mnt_flag &= ~MNT_LOCAL;
1072 	return (0);
1073 }
1074 
1075 /*
1076  * Flush out all the files in a filesystem.
1077  */
1078 int
1079 ffs_flushfiles(struct mount *mp, int flags, struct proc *p)
1080 {
1081 	struct ufsmount *ump;
1082 	int error;
1083 
1084 	ump = VFSTOUFS(mp);
1085 	if (mp->mnt_flag & MNT_QUOTA) {
1086 		int i;
1087 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
1088 			return (error);
1089 		for (i = 0; i < MAXQUOTAS; i++) {
1090 			if (ump->um_quotas[i] == NULLVP)
1091 				continue;
1092 			quotaoff(p, mp, i);
1093 		}
1094 		/*
1095 		 * Here we fall through to vflush again to ensure
1096 		 * that we have gotten rid of all the system vnodes.
1097 		 */
1098 	}
1099 
1100 	/*
1101 	 * Flush all the files.
1102 	 */
1103 	if ((error = vflush(mp, NULL, flags)) != 0)
1104 		return (error);
1105 	/*
1106 	 * Flush filesystem metadata.
1107 	 */
1108 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1109 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, MNT_WAIT, p);
1110 	VOP_UNLOCK(ump->um_devvp);
1111 	return (error);
1112 }
1113 
1114 /*
1115  * Get file system statistics.
1116  */
1117 int
1118 ffs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
1119 {
1120 	struct ufsmount *ump;
1121 	struct fs *fs;
1122 
1123 	ump = VFSTOUFS(mp);
1124 	fs = ump->um_fs;
1125 
1126 #ifdef FFS2
1127 	if (fs->fs_magic != FS_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1128 		panic("ffs_statfs");
1129 #else
1130 	if (fs->fs_magic != FS_MAGIC)
1131 		panic("ffs_statfs");
1132 #endif /* FFS2 */
1133 
1134 	sbp->f_bsize = fs->fs_fsize;
1135 	sbp->f_iosize = fs->fs_bsize;
1136 	sbp->f_blocks = fs->fs_dsize;
1137 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1138 	    fs->fs_cstotal.cs_nffree;
1139 	sbp->f_bavail = sbp->f_bfree -
1140 	    ((int64_t)fs->fs_dsize * fs->fs_minfree / 100);
1141 	sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1142 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
1143 	sbp->f_favail = sbp->f_ffree;
1144 	copy_statfs_info(sbp, mp);
1145 
1146 	return (0);
1147 }
1148 
1149 struct ffs_sync_args {
1150 	int allerror;
1151 	struct proc *p;
1152 	int waitfor;
1153 	int nlink0;
1154 	int inflight;
1155 	struct ucred *cred;
1156 };
1157 
1158 int
1159 ffs_sync_vnode(struct vnode *vp, void *arg)
1160 {
1161 	struct ffs_sync_args *fsa = arg;
1162 	struct inode *ip;
1163 	int error, nlink0 = 0;
1164 
1165 	if (vp->v_type == VNON)
1166 		return (0);
1167 
1168 	ip = VTOI(vp);
1169 
1170 	if (vp->v_inflight && !(vp->v_type == VCHR || vp->v_type == VBLK))
1171 		fsa->inflight = MIN(fsa->inflight+1, 65536);
1172 
1173 	/*
1174 	 * If unmounting or converting rw to ro, then stop deferring
1175 	 * timestamp writes.
1176 	 */
1177 	if (fsa->waitfor == MNT_WAIT && (ip->i_flag & IN_LAZYMOD)) {
1178 		ip->i_flag |= IN_MODIFIED;
1179 		UFS_UPDATE(ip, 1);
1180 	}
1181 
1182 	if (ip->i_effnlink == 0)
1183 		nlink0 = 1;
1184 
1185 	if ((ip->i_flag &
1186 	    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1187 	    LIST_EMPTY(&vp->v_dirtyblkhd)) {
1188 		goto end;
1189 	}
1190 
1191 	if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT)) {
1192 		nlink0 = 1;		/* potentially.. */
1193 		goto end;
1194 	}
1195 
1196 	if ((error = VOP_FSYNC(vp, fsa->cred, fsa->waitfor, fsa->p)))
1197 		fsa->allerror = error;
1198 	VOP_UNLOCK(vp);
1199 	vrele(vp);
1200 
1201 end:
1202 	fsa->nlink0 = MIN(fsa->nlink0 + nlink0, 65536);
1203 	return (0);
1204 }
1205 
1206 /*
1207  * Go through the disk queues to initiate sandbagged IO;
1208  * go through the inodes to write those that have been modified;
1209  * initiate the writing of the super block if it has been modified.
1210  *
1211  * Should always be called with the mount point locked.
1212  */
1213 int
1214 ffs_sync(struct mount *mp, int waitfor, int stall, struct ucred *cred, struct proc *p)
1215 {
1216 	struct ufsmount *ump = VFSTOUFS(mp);
1217 	struct fs *fs;
1218 	int error, allerror = 0, count, clean, fmod;
1219 	struct ffs_sync_args fsa;
1220 
1221 	fs = ump->um_fs;
1222 	/*
1223 	 * Write back modified superblock.
1224 	 * Consistency check that the superblock
1225 	 * is still in the buffer cache.
1226 	 */
1227 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {
1228 		printf("fs = %s\n", fs->fs_fsmnt);
1229 		panic("update: rofs mod");
1230 	}
1231  loop:
1232 	/*
1233 	 * Write back each (modified) inode.
1234 	 */
1235 	fsa.allerror = 0;
1236 	fsa.p = p;
1237 	fsa.cred = cred;
1238 	fsa.waitfor = waitfor;
1239 	fsa.nlink0 = 0;
1240 	fsa.inflight = 0;
1241 
1242 	/*
1243 	 * Don't traverse the vnode list if we want to skip all of them.
1244 	 */
1245 	if (waitfor != MNT_LAZY) {
1246 		vfs_mount_foreach_vnode(mp, ffs_sync_vnode, &fsa);
1247 		allerror = fsa.allerror;
1248 	}
1249 
1250 	/*
1251 	 * Force stale file system control information to be flushed.
1252 	 */
1253 	if ((ump->um_mountp->mnt_flag & MNT_SOFTDEP) && waitfor == MNT_WAIT) {
1254 		if ((error = softdep_flushworklist(ump->um_mountp, &count, p)))
1255 			allerror = error;
1256 		/* Flushed work items may create new vnodes to clean */
1257 		if (count)
1258 			goto loop;
1259 	}
1260 	if (waitfor != MNT_LAZY) {
1261 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1262 		if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
1263 			allerror = error;
1264 		VOP_UNLOCK(ump->um_devvp);
1265 	}
1266 	qsync(mp);
1267 	/*
1268 	 * Write back modified superblock.
1269 	 */
1270 	clean = fs->fs_clean;
1271 	fmod = fs->fs_fmod;
1272 	if (stall && fs->fs_ronly == 0) {
1273 		fs->fs_fmod = 1;
1274 		if (allerror == 0 && fsa.nlink0 == 0 && fsa.inflight == 0) {
1275 			fs->fs_clean = (fs->fs_flags & FS_UNCLEAN) ? 0 : 1;
1276 #if 0
1277 			printf("%s force clean (dangling %d inflight %d)\n",
1278 			    mp->mnt_stat.f_mntonname, fsa.nlink0, fsa.inflight);
1279 #endif
1280 		} else {
1281 			fs->fs_clean = 0;
1282 #if 0
1283 			printf("%s force dirty (dangling %d inflight %d)\n",
1284 			    mp->mnt_stat.f_mntonname, fsa.nlink0, fsa.inflight);
1285 #endif
1286 		}
1287 	}
1288 	if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1289 		allerror = error;
1290 	fs->fs_clean = clean;
1291 	fs->fs_fmod = fmod;
1292 
1293 	return (allerror);
1294 }
1295 
1296 /*
1297  * Look up a FFS dinode number to find its incore vnode, otherwise read it
1298  * in from disk.  If it is in core, wait for the lock bit to clear, then
1299  * return the inode locked.  Detection and handling of mount points must be
1300  * done by the calling routine.
1301  */
1302 int
1303 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1304 {
1305 	struct fs *fs;
1306 	struct inode *ip;
1307 	struct ufs1_dinode *dp1;
1308 #ifdef FFS2
1309 	struct ufs2_dinode *dp2;
1310 #endif
1311 	struct ufsmount *ump;
1312 	struct buf *bp;
1313 	struct vnode *vp;
1314 	dev_t dev;
1315 	int error;
1316 
1317 	if (ino > (ufsino_t)-1)
1318 		panic("ffs_vget: alien ino_t %llu", (unsigned long long)ino);
1319 
1320 	ump = VFSTOUFS(mp);
1321 	dev = ump->um_dev;
1322 retry:
1323 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
1324 		return (0);
1325 
1326 	/* Allocate a new vnode/inode. */
1327 	if ((error = getnewvnode(VT_UFS, mp, &ffs_vops, &vp)) != 0) {
1328 		*vpp = NULL;
1329 		return (error);
1330 	}
1331 
1332 #ifdef VFSLCKDEBUG
1333 	vp->v_flag |= VLOCKSWORK;
1334 #endif
1335 	ip = pool_get(&ffs_ino_pool, PR_WAITOK|PR_ZERO);
1336 	rrw_init_flags(&ip->i_lock, "inode", RWL_DUPOK | RWL_IS_VNODE);
1337 	ip->i_ump = ump;
1338 	vref(ip->i_devvp);
1339 	vp->v_data = ip;
1340 	ip->i_vnode = vp;
1341 	ip->i_fs = fs = ump->um_fs;
1342 	ip->i_dev = dev;
1343 	ip->i_number = ino;
1344 	ip->i_vtbl = &ffs_vtbl;
1345 
1346 	/*
1347 	 * Put it onto its hash chain and lock it so that other requests for
1348 	 * this inode will block if they arrive while we are sleeping waiting
1349 	 * for old data structures to be purged or for the contents of the
1350 	 * disk portion of this inode to be read.
1351 	 */
1352 	error = ufs_ihashins(ip);
1353 
1354 	if (error) {
1355 		/*
1356 		 * VOP_INACTIVE will treat this as a stale file
1357 		 * and recycle it quickly
1358 		 */
1359 		vrele(vp);
1360 
1361 		if (error == EEXIST)
1362 			goto retry;
1363 
1364 		return (error);
1365 	}
1366 
1367 
1368 	/* Read in the disk contents for the inode, copy into the inode. */
1369 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1370 	    (int)fs->fs_bsize, &bp);
1371 	if (error) {
1372 		/*
1373 		 * The inode does not contain anything useful, so it would
1374 		 * be misleading to leave it on its hash chain. With mode
1375 		 * still zero, it will be unlinked and returned to the free
1376 		 * list by vput().
1377 		 */
1378 		vput(vp);
1379 		brelse(bp);
1380 		*vpp = NULL;
1381 		return (error);
1382 	}
1383 
1384 #ifdef FFS2
1385 	if (ip->i_ump->um_fstype == UM_UFS2) {
1386 		ip->i_din2 = pool_get(&ffs_dinode2_pool, PR_WAITOK);
1387 		dp2 = (struct ufs2_dinode *) bp->b_data + ino_to_fsbo(fs, ino);
1388 		*ip->i_din2 = *dp2;
1389 	} else
1390 #endif
1391 	{
1392 		ip->i_din1 = pool_get(&ffs_dinode1_pool, PR_WAITOK);
1393 		dp1 = (struct ufs1_dinode *) bp->b_data + ino_to_fsbo(fs, ino);
1394 		*ip->i_din1 = *dp1;
1395 	}
1396 
1397 	brelse(bp);
1398 
1399 	if (DOINGSOFTDEP(vp))
1400 		softdep_load_inodeblock(ip);
1401 	else
1402 		ip->i_effnlink = DIP(ip, nlink);
1403 
1404 	/*
1405 	 * Initialize the vnode from the inode, check for aliases.
1406 	 * Note that the underlying vnode may have changed.
1407 	 */
1408 	if ((error = ffs_vinit(mp, &vp)) != 0) {
1409 		vput(vp);
1410 		*vpp = NULL;
1411 		return (error);
1412 	}
1413 
1414 	/*
1415 	 * Set up a generation number for this inode if it does not
1416 	 * already have one. This should only happen on old filesystems.
1417 	 */
1418 	if (DIP(ip, gen) == 0) {
1419 		DIP_ASSIGN(ip, gen, arc4random() & INT_MAX);
1420 		if (DIP(ip, gen) == 0 || DIP(ip, gen) == -1)
1421 			DIP_ASSIGN(ip, gen, 1);	/* Shouldn't happen */
1422 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1423 			ip->i_flag |= IN_MODIFIED;
1424 	}
1425 
1426 	/*
1427 	 * Ensure that uid and gid are correct. This is a temporary
1428 	 * fix until fsck has been changed to do the update.
1429 	 */
1430 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_inodefmt < FS_44INODEFMT) {
1431 		ip->i_ffs1_uid = ip->i_din1->di_ouid;
1432 		ip->i_ffs1_gid = ip->i_din1->di_ogid;
1433 	}
1434 
1435 	*vpp = vp;
1436 
1437 	return (0);
1438 }
1439 
1440 /*
1441  * File handle to vnode
1442  *
1443  * Have to be really careful about stale file handles.
1444  */
1445 int
1446 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1447 {
1448 	struct ufid *ufhp;
1449 	int error;
1450 
1451 	ufhp = (struct ufid *)fhp;
1452 	if (ufhp->ufid_len != sizeof(*ufhp))
1453 		return EINVAL;
1454 
1455 	if ((error = ffs_checkrange(mp, ufhp->ufid_ino)) != 0)
1456 		return error;
1457 
1458 	return (ufs_fhtovp(mp, ufhp, vpp));
1459 }
1460 
1461 /*
1462  * Vnode pointer to File handle
1463  */
1464 int
1465 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1466 {
1467 	struct inode *ip;
1468 	struct ufid *ufhp;
1469 
1470 	ip = VTOI(vp);
1471 	ufhp = (struct ufid *)fhp;
1472 	ufhp->ufid_len = sizeof(struct ufid);
1473 	ufhp->ufid_ino = ip->i_number;
1474 	ufhp->ufid_gen = DIP(ip, gen);
1475 
1476 	return (0);
1477 }
1478 
1479 /*
1480  * Write a superblock and associated information back to disk.
1481  */
1482 int
1483 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1484 {
1485 	struct fs *dfs, *fs = mp->um_fs;
1486 	struct buf *bp;
1487 	int blks;
1488 	caddr_t space;
1489 	int i, size, error, allerror = 0;
1490 
1491 	/*
1492 	 * First write back the summary information.
1493 	 */
1494 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1495 	space = (caddr_t)fs->fs_csp;
1496 	for (i = 0; i < blks; i += fs->fs_frag) {
1497 		size = fs->fs_bsize;
1498 		if (i + fs->fs_frag > blks)
1499 			size = (blks - i) * fs->fs_fsize;
1500 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1501 		    size, 0, INFSLP);
1502 		memcpy(bp->b_data, space, size);
1503 		space += size;
1504 		if (waitfor != MNT_WAIT)
1505 			bawrite(bp);
1506 		else if ((error = bwrite(bp)))
1507 			allerror = error;
1508 	}
1509 
1510 	/*
1511 	 * Now write back the superblock itself. If any errors occurred
1512 	 * up to this point, then fail so that the superblock avoids
1513 	 * being written out as clean.
1514 	 */
1515 	if (allerror) {
1516 		return (allerror);
1517 	}
1518 
1519 	bp = getblk(mp->um_devvp,
1520 	    fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
1521 	    (int)fs->fs_sbsize, 0, INFSLP);
1522 	fs->fs_fmod = 0;
1523 	fs->fs_time = time_second;
1524 	memcpy(bp->b_data, fs, fs->fs_sbsize);
1525 	/* Restore compatibility to old file systems.		   XXX */
1526 	dfs = (struct fs *)bp->b_data;				/* XXX */
1527 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
1528 		dfs->fs_nrpos = -1;				/* XXX */
1529 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
1530 		int32_t *lp, tmp;				/* XXX */
1531 								/* XXX */
1532 		lp = (int32_t *)&dfs->fs_qbmask;		/* XXX */
1533 		tmp = lp[4];					/* XXX */
1534 		for (i = 4; i > 0; i--)				/* XXX */
1535 			lp[i] = lp[i-1];			/* XXX */
1536 		lp[0] = tmp;					/* XXX */
1537 	}							/* XXX */
1538 	dfs->fs_maxfilesize = mp->um_savedmaxfilesize;		/* XXX */
1539 
1540 	ffs1_compat_write(dfs, mp);
1541 
1542 	if (waitfor != MNT_WAIT)
1543 		bawrite(bp);
1544 	else if ((error = bwrite(bp)))
1545 		allerror = error;
1546 
1547 	return (allerror);
1548 }
1549 
1550 int
1551 ffs_init(struct vfsconf *vfsp)
1552 {
1553 	static int done;
1554 
1555 	if (done)
1556 		return (0);
1557 
1558 	done = 1;
1559 
1560 	pool_init(&ffs_ino_pool, sizeof(struct inode), 0, IPL_NONE,
1561 	    PR_WAITOK, "ffsino", NULL);
1562 	pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, IPL_NONE,
1563 	    PR_WAITOK, "dino1pl", NULL);
1564 #ifdef FFS2
1565 	pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, IPL_NONE,
1566 	    PR_WAITOK, "dino2pl", NULL);
1567 #endif
1568 
1569 	softdep_initialize();
1570 
1571 	return (ufs_init(vfsp));
1572 }
1573 
1574 /*
1575  * fast filesystem related variables.
1576  */
1577 int
1578 ffs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1579     size_t newlen, struct proc *p)
1580 {
1581 #ifdef FFS_SOFTUPDATES
1582 	extern int max_softdeps, tickdelay, stat_worklist_push;
1583 	extern int stat_blk_limit_push, stat_ino_limit_push, stat_blk_limit_hit;
1584 	extern int stat_ino_limit_hit, stat_sync_limit_hit, stat_indir_blk_ptrs;
1585 	extern int stat_inode_bitmap, stat_direct_blk_ptrs, stat_dir_entry;
1586 #endif
1587 
1588 	/* all sysctl names at this level are terminal */
1589 	if (namelen != 1)
1590 		return (ENOTDIR);		/* overloaded */
1591 
1592 	switch (name[0]) {
1593 	case FFS_CLUSTERREAD:
1594 	case FFS_CLUSTERWRITE:
1595 	case FFS_REALLOCBLKS:
1596 	case FFS_ASYNCFREE:
1597 		return (EOPNOTSUPP);
1598 #ifdef FFS_SOFTUPDATES
1599 	case FFS_MAX_SOFTDEPS:
1600 		return (sysctl_int(oldp, oldlenp, newp, newlen, &max_softdeps));
1601 	case FFS_SD_TICKDELAY:
1602 		return (sysctl_int(oldp, oldlenp, newp, newlen, &tickdelay));
1603 	case FFS_SD_WORKLIST_PUSH:
1604 		return (sysctl_rdint(oldp, oldlenp, newp, stat_worklist_push));
1605 	case FFS_SD_BLK_LIMIT_PUSH:
1606 		return (sysctl_rdint(oldp, oldlenp, newp, stat_blk_limit_push));
1607 	case FFS_SD_INO_LIMIT_PUSH:
1608 		return (sysctl_rdint(oldp, oldlenp, newp, stat_ino_limit_push));
1609 	case FFS_SD_BLK_LIMIT_HIT:
1610 		return (sysctl_rdint(oldp, oldlenp, newp, stat_blk_limit_hit));
1611 	case FFS_SD_INO_LIMIT_HIT:
1612 		return (sysctl_rdint(oldp, oldlenp, newp, stat_ino_limit_hit));
1613 	case FFS_SD_SYNC_LIMIT_HIT:
1614 		return (sysctl_rdint(oldp, oldlenp, newp, stat_sync_limit_hit));
1615 	case FFS_SD_INDIR_BLK_PTRS:
1616 		return (sysctl_rdint(oldp, oldlenp, newp, stat_indir_blk_ptrs));
1617 	case FFS_SD_INODE_BITMAP:
1618 		return (sysctl_rdint(oldp, oldlenp, newp, stat_inode_bitmap));
1619 	case FFS_SD_DIRECT_BLK_PTRS:
1620 		return (sysctl_rdint(oldp, oldlenp, newp, stat_direct_blk_ptrs));
1621 	case FFS_SD_DIR_ENTRY:
1622 		return (sysctl_rdint(oldp, oldlenp, newp, stat_dir_entry));
1623 #endif
1624 #ifdef UFS_DIRHASH
1625 	case FFS_DIRHASH_DIRSIZE:
1626 		return (sysctl_int(oldp, oldlenp, newp, newlen,
1627 		    &ufs_mindirhashsize));
1628 	case FFS_DIRHASH_MAXMEM:
1629 		return (sysctl_int(oldp, oldlenp, newp, newlen,
1630 		    &ufs_dirhashmaxmem));
1631 	case FFS_DIRHASH_MEM:
1632 		return (sysctl_rdint(oldp, oldlenp, newp, ufs_dirhashmem));
1633 #endif
1634 
1635 	default:
1636 		return (EOPNOTSUPP);
1637 	}
1638 	/* NOTREACHED */
1639 }
1640