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