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