xref: /dflybsd-src/sys/vfs/ufs/ffs_vfsops.c (revision bc76a771df54af7e361532b257cecc26227736b4)
1 /*
2  * Copyright (c) 1989, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
34  * $FreeBSD: src/sys/ufs/ffs/ffs_vfsops.c,v 1.117.2.10 2002/06/23 22:34:52 iedowse Exp $
35  * $DragonFly: src/sys/vfs/ufs/ffs_vfsops.c,v 1.15 2004/03/11 20:17:36 hmp Exp $
36  */
37 
38 #include "opt_quota.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/namei.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/buf.h>
48 #include <sys/conf.h>
49 #include <sys/fcntl.h>
50 #include <sys/disklabel.h>
51 #include <sys/malloc.h>
52 
53 #include "quota.h"
54 #include "ufsmount.h"
55 #include "inode.h"
56 #include "ufs_extern.h"
57 
58 #include "fs.h"
59 #include "ffs_extern.h"
60 
61 #include <vm/vm.h>
62 #include <vm/vm_page.h>
63 #include <vm/vm_zone.h>
64 
65 static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part");
66 
67 static int	ffs_sbupdate (struct ufsmount *, int);
68 static int	ffs_reload (struct mount *,struct ucred *,struct thread *);
69 static int	ffs_oldfscompat (struct fs *);
70 static int	ffs_mount (struct mount *, char *, caddr_t,
71 				struct nameidata *, struct thread *);
72 static int	ffs_init (struct vfsconf *);
73 
74 static struct vfsops ufs_vfsops = {
75 	ffs_mount,
76 	ufs_start,
77 	ffs_unmount,
78 	ufs_root,
79 	ufs_quotactl,
80 	ffs_statfs,
81 	ffs_sync,
82 	ffs_vget,
83 	ffs_fhtovp,
84 	ufs_check_export,
85 	ffs_vptofh,
86 	ffs_init,
87 	vfs_stduninit,
88 	vfs_stdextattrctl,
89 };
90 
91 VFS_SET(ufs_vfsops, ufs, 0);
92 
93 /*
94  * ffs_mount
95  *
96  * Called when mounting local physical media
97  *
98  * PARAMETERS:
99  *		mountroot
100  *			mp	mount point structure
101  *			path	NULL (flag for root mount!!!)
102  *			data	<unused>
103  *			ndp	<unused>
104  *			p	process (user credentials check [statfs])
105  *
106  *		mount
107  *			mp	mount point structure
108  *			path	path to mount point
109  *			data	pointer to argument struct in user space
110  *			ndp	mount point namei() return (used for
111  *				credentials on reload), reused to look
112  *				up block device.
113  *			p	process (user credentials check)
114  *
115  * RETURNS:	0	Success
116  *		!0	error number (errno.h)
117  *
118  * LOCK STATE:
119  *
120  *		ENTRY
121  *			mount point is locked
122  *		EXIT
123  *			mount point is locked
124  *
125  * NOTES:
126  *		A NULL path can be used for a flag since the mount
127  *		system call will fail with EFAULT in copyinstr in
128  *		namei() if it is a genuine NULL from the user.
129  */
130 static int
131 ffs_mount( mp, path, data, ndp, td)
132         struct mount		*mp;	/* mount struct pointer*/
133         char			*path;	/* path to mount point*/
134         caddr_t			data;	/* arguments to FS specific mount*/
135         struct nameidata	*ndp;	/* mount point credentials*/
136         struct thread		*td;	/* process requesting mount*/
137 {
138 	size_t		size;
139 	int		err = 0;
140 	struct vnode	*devvp;
141 
142 	struct ufs_args args;
143 	struct ufsmount *ump = 0;
144 	struct fs *fs;
145 	int error, flags, ronly = 0;
146 	mode_t accessmode;
147 	struct ucred *cred;
148 
149 	KKASSERT(td->td_proc);
150 	cred = td->td_proc->p_ucred;
151 
152 	/*
153 	 * Use NULL path to flag a root mount
154 	 */
155 	if( path == NULL) {
156 		/*
157 		 ***
158 		 * Mounting root file system
159 		 ***
160 		 */
161 
162 		if ((err = bdevvp(rootdev, &rootvp))) {
163 			printf("ffs_mountroot: can't find rootvp\n");
164 			return (err);
165 		}
166 
167 		if( ( err = ffs_mountfs(rootvp, mp, td, M_FFSNODE)) != 0) {
168 			/* fs specific cleanup (if any)*/
169 			goto error_1;
170 		}
171 
172 		goto dostatfs;		/* success*/
173 
174 	}
175 
176 	/*
177 	 ***
178 	 * Mounting non-root file system or updating a file system
179 	 ***
180 	 */
181 
182 	/* copy in user arguments*/
183 	err = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
184 	if (err)
185 		goto error_1;		/* can't get arguments*/
186 
187 	/*
188 	 * If updating, check whether changing from read-only to
189 	 * read/write; if there is no device name, that's all we do.
190 	 */
191 	if (mp->mnt_flag & MNT_UPDATE) {
192 		ump = VFSTOUFS(mp);
193 		fs = ump->um_fs;
194 		devvp = ump->um_devvp;
195 		err = 0;
196 		ronly = fs->fs_ronly;	/* MNT_RELOAD might change this */
197 		if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
198 			/*
199 			 * Flush any dirty data.
200 			 */
201 			VFS_SYNC(mp, MNT_WAIT, td);
202 			/*
203 			 * Check for and optionally get rid of files open
204 			 * for writing.
205 			 */
206 			flags = WRITECLOSE;
207 			if (mp->mnt_flag & MNT_FORCE)
208 				flags |= FORCECLOSE;
209 			if (mp->mnt_flag & MNT_SOFTDEP) {
210 				err = softdep_flushfiles(mp, flags, td);
211 			} else {
212 				err = ffs_flushfiles(mp, flags, td);
213 			}
214 			ronly = 1;
215 		}
216 		if (!err && (mp->mnt_flag & MNT_RELOAD))
217 			err = ffs_reload(mp, ndp->ni_cnd.cn_cred, td);
218 		if (err) {
219 			goto error_1;
220 		}
221 		if (ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
222 			/*
223 			 * If upgrade to read-write by non-root, then verify
224 			 * that user has necessary permissions on the device.
225 			 */
226 			if (cred->cr_uid != 0) {
227 				vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
228 				if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
229 				    cred, td)) != 0) {
230 					VOP_UNLOCK(devvp, NULL, 0, td);
231 					return (error);
232 				}
233 				VOP_UNLOCK(devvp, NULL, 0, td);
234 			}
235 
236 			fs->fs_flags &= ~FS_UNCLEAN;
237 			if (fs->fs_clean == 0) {
238 				fs->fs_flags |= FS_UNCLEAN;
239 				if (mp->mnt_flag & MNT_FORCE) {
240 					printf(
241 "WARNING: %s was not properly dismounted\n",
242 					    fs->fs_fsmnt);
243 				} else {
244 					printf(
245 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
246 					    fs->fs_fsmnt);
247 					err = EPERM;
248 					goto error_1;
249 				}
250 			}
251 
252 			/* check to see if we need to start softdep */
253 			if (fs->fs_flags & FS_DOSOFTDEP) {
254 				err = softdep_mount(devvp, mp, fs);
255 				if (err)
256 					goto error_1;
257 			}
258 
259 			ronly = 0;
260 		}
261 		/*
262 		 * Soft updates is incompatible with "async",
263 		 * so if we are doing softupdates stop the user
264 		 * from setting the async flag in an update.
265 		 * Softdep_mount() clears it in an initial mount
266 		 * or ro->rw remount.
267 		 */
268 		if (mp->mnt_flag & MNT_SOFTDEP) {
269 			mp->mnt_flag &= ~MNT_ASYNC;
270 		}
271 		/* if not updating name...*/
272 		if (args.fspec == 0) {
273 			/*
274 			 * Process export requests.  Jumping to "success"
275 			 * will return the vfs_export() error code.
276 			 */
277 			err = vfs_export(mp, &ump->um_export, &args.export);
278 			goto success;
279 		}
280 	}
281 
282 	/*
283 	 * Not an update, or updating the name: look up the name
284 	 * and verify that it refers to a sensible block device.
285 	 */
286 	NDINIT(ndp, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, args.fspec, td);
287 	err = namei(ndp);
288 	if (err) {
289 		/* can't get devvp!*/
290 		goto error_1;
291 	}
292 
293 	NDFREE(ndp, NDF_ONLY_PNBUF);
294 	devvp = ndp->ni_vp;
295 
296 	if (!vn_isdisk(devvp, &err))
297 		goto error_2;
298 
299 	/*
300 	 * If mount by non-root, then verify that user has necessary
301 	 * permissions on the device.
302 	 */
303 	if (cred->cr_uid != 0) {
304 		accessmode = VREAD;
305 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
306 			accessmode |= VWRITE;
307 		vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
308 		if ((error = VOP_ACCESS(devvp, accessmode, cred, td)) != 0) {
309 			vput(devvp);
310 			return (error);
311 		}
312 		VOP_UNLOCK(devvp, NULL, 0, td);
313 	}
314 
315 	if (mp->mnt_flag & MNT_UPDATE) {
316 		/*
317 		 ********************
318 		 * UPDATE
319 		 * If it's not the same vnode, or at least the same device
320 		 * then it's not correct.
321 		 ********************
322 		 */
323 
324 		if (devvp != ump->um_devvp) {
325 			if ( devvp->v_rdev == ump->um_devvp->v_rdev) {
326 				vrele(devvp);
327 			} else {
328 				err = EINVAL;	/* needs translation */
329 			}
330 		} else
331 			vrele(devvp);
332 		/*
333 		 * Update device name only on success
334 		 */
335 		if( !err) {
336 			/* Save "mounted from" info for mount point (NULL pad)*/
337 			copyinstr(	args.fspec,
338 					mp->mnt_stat.f_mntfromname,
339 					MNAMELEN - 1,
340 					&size);
341 			bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
342 		}
343 	} else {
344 		/*
345 		 ********************
346 		 * NEW MOUNT
347 		 ********************
348 		 */
349 
350 		/*
351 		 * Since this is a new mount, we want the names for
352 		 * the device and the mount point copied in.  If an
353 		 * error occurs,  the mountpoint is discarded by the
354 		 * upper level code.
355 		 */
356 		/* Save "last mounted on" info for mount point (NULL pad)*/
357 		copyinstr(	path,				/* mount point*/
358 				mp->mnt_stat.f_mntonname,	/* save area*/
359 				MNAMELEN - 1,			/* max size*/
360 				&size);				/* real size*/
361 		bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
362 
363 		/* Save "mounted from" info for mount point (NULL pad)*/
364 		copyinstr(	args.fspec,			/* device name*/
365 				mp->mnt_stat.f_mntfromname,	/* save area*/
366 				MNAMELEN - 1,			/* max size*/
367 				&size);				/* real size*/
368 		bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
369 
370 		err = ffs_mountfs(devvp, mp, td, M_FFSNODE);
371 	}
372 	if (err) {
373 		goto error_2;
374 	}
375 
376 dostatfs:
377 	/*
378 	 * Initialize FS stat information in mount struct; uses both
379 	 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
380 	 *
381 	 * This code is common to root and non-root mounts
382 	 */
383 	(void)VFS_STATFS(mp, &mp->mnt_stat, td);
384 
385 	goto success;
386 
387 
388 error_2:	/* error with devvp held*/
389 
390 	/* release devvp before failing*/
391 	vrele(devvp);
392 
393 error_1:	/* no state to back out*/
394 
395 success:
396 	if (!err && path && (mp->mnt_flag & MNT_UPDATE)) {
397 		/* Update clean flag after changing read-onlyness. */
398 		fs = ump->um_fs;
399 		if (ronly != fs->fs_ronly) {
400 			fs->fs_ronly = ronly;
401 			fs->fs_clean = ronly &&
402 			    (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
403 			ffs_sbupdate(ump, MNT_WAIT);
404 		}
405 	}
406 	return (err);
407 }
408 
409 /*
410  * Reload all incore data for a filesystem (used after running fsck on
411  * the root filesystem and finding things to fix). The filesystem must
412  * be mounted read-only.
413  *
414  * Things to do to update the mount:
415  *	1) invalidate all cached meta-data.
416  *	2) re-read superblock from disk.
417  *	3) re-read summary information from disk.
418  *	4) invalidate all inactive vnodes.
419  *	5) invalidate all cached file data.
420  *	6) re-read inode data for all active vnodes.
421  */
422 
423 static int ffs_reload_scan1(struct mount *mp, struct vnode *vp, void *data);
424 static int ffs_reload_scan2(struct mount *mp, struct vnode *vp,
425 				lwkt_tokref_t vlock, void *data);
426 
427 struct scaninfo {
428 	int rescan;
429 	struct fs *fs;
430 	struct vnode *devvp;
431 	thread_t td;
432 	int waitfor;
433 	int allerror;
434 };
435 
436 static int
437 ffs_reload(struct mount *mp, struct ucred *cred, struct thread *td)
438 {
439 	struct vnode *devvp;
440 	void *space;
441 	struct buf *bp;
442 	struct fs *fs, *newfs;
443 	struct partinfo dpart;
444 	dev_t dev;
445 	int i, blks, size, error;
446 	lwkt_tokref vlock;
447 	struct scaninfo scaninfo;
448 	int32_t *lp;
449 
450 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
451 		return (EINVAL);
452 	/*
453 	 * Step 1: invalidate all cached meta-data.
454 	 */
455 	devvp = VFSTOUFS(mp)->um_devvp;
456 	vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
457 	error = vinvalbuf(devvp, 0, td, 0, 0);
458 	VOP_UNLOCK(devvp, NULL, 0, td);
459 	if (error)
460 		panic("ffs_reload: dirty1");
461 
462 	dev = devvp->v_rdev;
463 
464 	/*
465 	 * Only VMIO the backing device if the backing device is a real
466 	 * block device.  See ffs_mountmfs() for more details.
467 	 */
468 	if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) {
469 		vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
470 		vfs_object_create(devvp, td);
471 		lwkt_gettoken(&vlock, devvp->v_interlock);
472 		VOP_UNLOCK(devvp, &vlock, LK_INTERLOCK, td);
473 	}
474 
475 	/*
476 	 * Step 2: re-read superblock from disk.
477 	 */
478 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, td) != 0)
479 		size = DEV_BSIZE;
480 	else
481 		size = dpart.disklab->d_secsize;
482 	if ((error = bread(devvp, (ufs_daddr_t)(SBOFF/size), SBSIZE, &bp)) != 0)
483 		return (error);
484 	newfs = (struct fs *)bp->b_data;
485 	if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
486 		newfs->fs_bsize < sizeof(struct fs)) {
487 			brelse(bp);
488 			return (EIO);		/* XXX needs translation */
489 	}
490 	fs = VFSTOUFS(mp)->um_fs;
491 	/*
492 	 * Copy pointer fields back into superblock before copying in	XXX
493 	 * new superblock. These should really be in the ufsmount.	XXX
494 	 * Note that important parameters (eg fs_ncg) are unchanged.
495 	 */
496 	newfs->fs_csp = fs->fs_csp;
497 	newfs->fs_maxcluster = fs->fs_maxcluster;
498 	newfs->fs_contigdirs = fs->fs_contigdirs;
499 	/* The file system is still read-only. */
500 	newfs->fs_ronly = 1;
501 	bcopy(newfs, fs, (u_int)fs->fs_sbsize);
502 	if (fs->fs_sbsize < SBSIZE)
503 		bp->b_flags |= B_INVAL;
504 	brelse(bp);
505 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
506 	ffs_oldfscompat(fs);
507 	/* An old fsck may have zeroed these fields, so recheck them. */
508 	if (fs->fs_avgfilesize <= 0)		/* XXX */
509 		fs->fs_avgfilesize = AVFILESIZ;	/* XXX */
510 	if (fs->fs_avgfpdir <= 0)		/* XXX */
511 		fs->fs_avgfpdir = AFPDIR;	/* XXX */
512 
513 	/*
514 	 * Step 3: re-read summary information from disk.
515 	 */
516 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
517 	space = fs->fs_csp;
518 	for (i = 0; i < blks; i += fs->fs_frag) {
519 		size = fs->fs_bsize;
520 		if (i + fs->fs_frag > blks)
521 			size = (blks - i) * fs->fs_fsize;
522 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, &bp);
523 		if (error)
524 			return (error);
525 		bcopy(bp->b_data, space, (u_int)size);
526 		space = (char *)space + size;
527 		brelse(bp);
528 	}
529 	/*
530 	 * We no longer know anything about clusters per cylinder group.
531 	 */
532 	if (fs->fs_contigsumsize > 0) {
533 		lp = fs->fs_maxcluster;
534 		for (i = 0; i < fs->fs_ncg; i++)
535 			*lp++ = fs->fs_contigsumsize;
536 	}
537 
538 	scaninfo.rescan = 0;
539 	scaninfo.fs = fs;
540 	scaninfo.devvp = devvp;
541 	scaninfo.td = td;
542 	while (error == 0 && scaninfo.rescan) {
543 		scaninfo.rescan = 0;
544 		error = vmntvnodescan(mp, ffs_reload_scan1,
545 				    ffs_reload_scan2, &scaninfo);
546 	}
547 	return(error);
548 }
549 
550 static
551 int
552 ffs_reload_scan1(struct mount *mp, struct vnode *vp, void *data)
553 {
554 	struct scaninfo *info = data;
555 
556 	/*
557 	 * Step 4: invalidate all inactive vnodes.
558 	 */
559 	if (vrecycle(vp, NULL, info->td)) {
560 		info->rescan = 1;
561 		return(-1);	/* continue loop, do not call scan2 */
562 	}
563 	return(0);
564 }
565 
566 static
567 int
568 ffs_reload_scan2(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data)
569 {
570 	struct scaninfo *info = data;
571 	struct inode *ip;
572 	struct buf *bp;
573 	int error;
574 
575 	/*
576 	 * Step 5: invalidate all cached file data.
577 	 */
578 	if (vget(vp, vlock, LK_EXCLUSIVE | LK_INTERLOCK, info->td)) {
579 		info->rescan = 1;
580 		return(0);
581 	}
582 	if (vinvalbuf(vp, 0, info->td, 0, 0))
583 		panic("ffs_reload: dirty2");
584 	/*
585 	 * Step 6: re-read inode data for all active vnodes.
586 	 */
587 	ip = VTOI(vp);
588 	error = bread(info->devvp,
589 			fsbtodb(info->fs, ino_to_fsba(info->fs, ip->i_number)),
590 			(int)info->fs->fs_bsize, &bp);
591 	if (error) {
592 		vput(vp);
593 		return (error);
594 	}
595 	ip->i_din = *((struct dinode *)bp->b_data +
596 	    ino_to_fsbo(info->fs, ip->i_number));
597 	ip->i_effnlink = ip->i_nlink;
598 	brelse(bp);
599 	vput(vp);
600 	return(0);
601 }
602 
603 /*
604  * Common code for mount and mountroot
605  */
606 int
607 ffs_mountfs(devvp, mp, td, malloctype)
608 	struct vnode *devvp;
609 	struct mount *mp;
610 	struct thread *td;
611 	struct malloc_type *malloctype;
612 {
613 	struct ufsmount *ump;
614 	struct buf *bp;
615 	struct fs *fs;
616 	dev_t dev;
617 	struct partinfo dpart;
618 	void *space;
619 	int error, i, blks, size, ronly;
620 	lwkt_tokref vlock;
621 	int32_t *lp;
622 	u_int64_t maxfilesize;					/* XXX */
623 	size_t strsize;
624 	int ncount;
625 
626 	dev = devvp->v_rdev;
627 	/*
628 	 * Disallow multiple mounts of the same device.
629 	 * Disallow mounting of a device that is currently in use
630 	 * (except for root, which might share swap device for miniroot).
631 	 * Flush out any old buffers remaining from a previous use.
632 	 */
633 	error = vfs_mountedon(devvp);
634 	if (error)
635 		return (error);
636 	ncount = vcount(devvp);
637 
638 	if (ncount > 1 && devvp != rootvp)
639 		return (EBUSY);
640 	vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
641 	error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
642 	VOP_UNLOCK(devvp, NULL, 0, td);
643 	if (error)
644 		return (error);
645 
646 	/*
647 	 * Only VMIO the backing device if the backing device is a real
648 	 * block device.  This excludes the original MFS implementation.
649 	 * Note that it is optional that the backing device be VMIOed.  This
650 	 * increases the opportunity for metadata caching.
651 	 */
652 	if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) {
653 		vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
654 		vfs_object_create(devvp, td);
655 		lwkt_gettoken(&vlock, devvp->v_interlock);
656 		VOP_UNLOCK(devvp, &vlock, LK_INTERLOCK, td);
657 	}
658 
659 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
660 	vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
661 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td);
662 	VOP_UNLOCK(devvp, NULL, 0, td);
663 	if (error)
664 		return (error);
665 	if (devvp->v_rdev->si_iosize_max != 0)
666 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
667 	if (mp->mnt_iosize_max > MAXPHYS)
668 		mp->mnt_iosize_max = MAXPHYS;
669 
670 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, proc0.p_ucred, td) != 0)
671 		size = DEV_BSIZE;
672 	else
673 		size = dpart.disklab->d_secsize;
674 
675 	bp = NULL;
676 	ump = NULL;
677 	if ((error = bread(devvp, SBLOCK, SBSIZE, &bp)) != 0)
678 		goto out;
679 	fs = (struct fs *)bp->b_data;
680 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
681 	    fs->fs_bsize < sizeof(struct fs)) {
682 		error = EINVAL;		/* XXX needs translation */
683 		goto out;
684 	}
685 	fs->fs_fmod = 0;
686 	fs->fs_flags &= ~FS_UNCLEAN;
687 	if (fs->fs_clean == 0) {
688 		fs->fs_flags |= FS_UNCLEAN;
689 		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
690 			printf(
691 "WARNING: %s was not properly dismounted\n",
692 			    fs->fs_fsmnt);
693 		} else {
694 			printf(
695 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
696 			    fs->fs_fsmnt);
697 			error = EPERM;
698 			goto out;
699 		}
700 	}
701 	/* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
702 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
703 		error = EROFS;          /* needs translation */
704 		goto out;
705 	}
706 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
707 	bzero((caddr_t)ump, sizeof *ump);
708 	ump->um_malloctype = malloctype;
709 	ump->um_i_effnlink_valid = 1;
710 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
711 	    M_WAITOK);
712 	ump->um_blkatoff = ffs_blkatoff;
713 	ump->um_truncate = ffs_truncate;
714 	ump->um_update = ffs_update;
715 	ump->um_valloc = ffs_valloc;
716 	ump->um_vfree = ffs_vfree;
717 	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
718 	if (fs->fs_sbsize < SBSIZE)
719 		bp->b_flags |= B_INVAL;
720 	brelse(bp);
721 	bp = NULL;
722 	fs = ump->um_fs;
723 	fs->fs_ronly = ronly;
724 	size = fs->fs_cssize;
725 	blks = howmany(size, fs->fs_fsize);
726 	if (fs->fs_contigsumsize > 0)
727 		size += fs->fs_ncg * sizeof(int32_t);
728 	size += fs->fs_ncg * sizeof(u_int8_t);
729 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
730 	fs->fs_csp = space;
731 	for (i = 0; i < blks; i += fs->fs_frag) {
732 		size = fs->fs_bsize;
733 		if (i + fs->fs_frag > blks)
734 			size = (blks - i) * fs->fs_fsize;
735 		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
736 		    &bp)) != 0) {
737 			free(fs->fs_csp, M_UFSMNT);
738 			goto out;
739 		}
740 		bcopy(bp->b_data, space, (u_int)size);
741 		space = (char *)space + size;
742 		brelse(bp);
743 		bp = NULL;
744 	}
745 	if (fs->fs_contigsumsize > 0) {
746 		fs->fs_maxcluster = lp = space;
747 		for (i = 0; i < fs->fs_ncg; i++)
748 			*lp++ = fs->fs_contigsumsize;
749 		space = lp;
750 	}
751 	size = fs->fs_ncg * sizeof(u_int8_t);
752 	fs->fs_contigdirs = (u_int8_t *)space;
753 	bzero(fs->fs_contigdirs, size);
754 	/* Compatibility for old filesystems 	   XXX */
755 	if (fs->fs_avgfilesize <= 0)		/* XXX */
756 		fs->fs_avgfilesize = AVFILESIZ;	/* XXX */
757 	if (fs->fs_avgfpdir <= 0)		/* XXX */
758 		fs->fs_avgfpdir = AFPDIR;	/* XXX */
759 	mp->mnt_data = (qaddr_t)ump;
760 	mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
761 	mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
762 	if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
763 	    vfs_getvfs(&mp->mnt_stat.f_fsid))
764 		vfs_getnewfsid(mp);
765 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
766 	mp->mnt_flag |= MNT_LOCAL;
767 	ump->um_mountp = mp;
768 	ump->um_dev = dev;
769 	ump->um_devvp = devvp;
770 	ump->um_nindir = fs->fs_nindir;
771 	ump->um_bptrtodb = fs->fs_fsbtodb;
772 	ump->um_seqinc = fs->fs_frag;
773 	for (i = 0; i < MAXQUOTAS; i++)
774 		ump->um_quotas[i] = NULLVP;
775 	devvp->v_specmountpoint = mp;
776 	ffs_oldfscompat(fs);
777 
778 	/*
779 	 * Set FS local "last mounted on" information (NULL pad)
780 	 */
781 	copystr(	mp->mnt_stat.f_mntonname,	/* mount point*/
782 			fs->fs_fsmnt,			/* copy area*/
783 			sizeof(fs->fs_fsmnt) - 1,	/* max size*/
784 			&strsize);			/* real size*/
785 	bzero( fs->fs_fsmnt + strsize, sizeof(fs->fs_fsmnt) - strsize);
786 
787 	if( mp->mnt_flag & MNT_ROOTFS) {
788 		/*
789 		 * Root mount; update timestamp in mount structure.
790 		 * this will be used by the common root mount code
791 		 * to update the system clock.
792 		 */
793 		mp->mnt_time = fs->fs_time;
794 	}
795 
796 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
797 	maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1;	/* XXX */
798 	/* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
799 	if (maxfilesize > (u_int64_t)0x80000000u * PAGE_SIZE - 1)
800 		maxfilesize = (u_int64_t)0x80000000u * PAGE_SIZE - 1;
801 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
802 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
803 	if (ronly == 0) {
804 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
805 		    (error = softdep_mount(devvp, mp, fs)) != 0) {
806 			free(fs->fs_csp, M_UFSMNT);
807 			goto out;
808 		}
809 		fs->fs_fmod = 1;
810 		fs->fs_clean = 0;
811 		(void) ffs_sbupdate(ump, MNT_WAIT);
812 	}
813 	return (0);
814 out:
815 	devvp->v_specmountpoint = NULL;
816 	if (bp)
817 		brelse(bp);
818 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
819 	if (ump) {
820 		free(ump->um_fs, M_UFSMNT);
821 		free(ump, M_UFSMNT);
822 		mp->mnt_data = (qaddr_t)0;
823 	}
824 	return (error);
825 }
826 
827 /*
828  * Sanity checks for old file systems.
829  *
830  * XXX - goes away some day.
831  */
832 static int
833 ffs_oldfscompat(fs)
834 	struct fs *fs;
835 {
836 
837 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
838 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
839 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
840 		fs->fs_nrpos = 8;				/* XXX */
841 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
842 #if 0
843 		int i;						/* XXX */
844 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
845 								/* XXX */
846 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
847 		for (i = 0; i < NIADDR; i++) {			/* XXX */
848 			sizepb *= NINDIR(fs);			/* XXX */
849 			fs->fs_maxfilesize += sizepb;		/* XXX */
850 		}						/* XXX */
851 #endif
852 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
853 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
854 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
855 	}							/* XXX */
856 	return (0);
857 }
858 
859 /*
860  * unmount system call
861  */
862 int
863 ffs_unmount(struct mount *mp, int mntflags, struct thread *td)
864 {
865 	struct ufsmount *ump;
866 	struct fs *fs;
867 	int error, flags;
868 
869 	flags = 0;
870 	if (mntflags & MNT_FORCE) {
871 		flags |= FORCECLOSE;
872 	}
873 	if (mp->mnt_flag & MNT_SOFTDEP) {
874 		if ((error = softdep_flushfiles(mp, flags, td)) != 0)
875 			return (error);
876 	} else {
877 		if ((error = ffs_flushfiles(mp, flags, td)) != 0)
878 			return (error);
879 	}
880 	ump = VFSTOUFS(mp);
881 	fs = ump->um_fs;
882 	if (fs->fs_ronly == 0) {
883 		fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
884 		error = ffs_sbupdate(ump, MNT_WAIT);
885 		if (error) {
886 			fs->fs_clean = 0;
887 			return (error);
888 		}
889 	}
890 	ump->um_devvp->v_specmountpoint = NULL;
891 
892 	vinvalbuf(ump->um_devvp, V_SAVE, td, 0, 0);
893 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE, td);
894 
895 	vrele(ump->um_devvp);
896 
897 	free(fs->fs_csp, M_UFSMNT);
898 	free(fs, M_UFSMNT);
899 	free(ump, M_UFSMNT);
900 	mp->mnt_data = (qaddr_t)0;
901 	mp->mnt_flag &= ~MNT_LOCAL;
902 	return (error);
903 }
904 
905 /*
906  * Flush out all the files in a filesystem.
907  */
908 int
909 ffs_flushfiles(struct mount *mp, int flags, struct thread *td)
910 {
911 	struct ufsmount *ump;
912 	int error;
913 
914 	ump = VFSTOUFS(mp);
915 #ifdef QUOTA
916 	if (mp->mnt_flag & MNT_QUOTA) {
917 		int i;
918 		error = vflush(mp, 0, SKIPSYSTEM|flags);
919 		if (error)
920 			return (error);
921 		for (i = 0; i < MAXQUOTAS; i++) {
922 			if (ump->um_quotas[i] == NULLVP)
923 				continue;
924 			quotaoff(td, mp, i);
925 		}
926 		/*
927 		 * Here we fall through to vflush again to ensure
928 		 * that we have gotten rid of all the system vnodes.
929 		 */
930 	}
931 #endif
932         /*
933 	 * Flush all the files.
934 	 */
935 	if ((error = vflush(mp, 0, flags)) != 0)
936 		return (error);
937 	/*
938 	 * Flush filesystem metadata.
939 	 */
940 	vn_lock(ump->um_devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
941 	error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
942 	VOP_UNLOCK(ump->um_devvp, NULL, 0, td);
943 	return (error);
944 }
945 
946 /*
947  * Get file system statistics.
948  */
949 int
950 ffs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
951 {
952 	struct ufsmount *ump;
953 	struct fs *fs;
954 
955 	ump = VFSTOUFS(mp);
956 	fs = ump->um_fs;
957 	if (fs->fs_magic != FS_MAGIC)
958 		panic("ffs_statfs");
959 	sbp->f_bsize = fs->fs_fsize;
960 	sbp->f_iosize = fs->fs_bsize;
961 	sbp->f_blocks = fs->fs_dsize;
962 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
963 		fs->fs_cstotal.cs_nffree;
964 	sbp->f_bavail = freespace(fs, fs->fs_minfree);
965 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
966 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
967 	if (sbp != &mp->mnt_stat) {
968 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
969 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
970 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
971 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
972 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
973 	}
974 	return (0);
975 }
976 
977 /*
978  * Go through the disk queues to initiate sandbagged IO;
979  * go through the inodes to write those that have been modified;
980  * initiate the writing of the super block if it has been modified.
981  *
982  * Note: we are always called with the filesystem marked `MPBUSY'.
983  */
984 
985 
986 static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
987 static int ffs_sync_scan2(struct mount *mp, struct vnode *vp,
988                 lwkt_tokref_t vlock, void *data);
989 
990 int
991 ffs_sync(struct mount *mp, int waitfor, struct thread *td)
992 {
993 	struct ufsmount *ump = VFSTOUFS(mp);
994 	struct fs *fs;
995 	int error;
996 	struct scaninfo scaninfo;
997 
998 	fs = ump->um_fs;
999 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
1000 		printf("fs = %s\n", fs->fs_fsmnt);
1001 		panic("ffs_sync: rofs mod");
1002 	}
1003 
1004 	/*
1005 	 * Write back each (modified) inode.
1006 	 */
1007 	scaninfo.allerror = 0;
1008 	scaninfo.rescan = 1;
1009 	scaninfo.waitfor = waitfor;
1010 	while (scaninfo.rescan) {
1011 		scaninfo.rescan = 0;
1012 		vmntvnodescan(mp, ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
1013 	}
1014 
1015 	/*
1016 	 * Force stale file system control information to be flushed.
1017 	 */
1018 	if (waitfor != MNT_LAZY) {
1019 		if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
1020 			waitfor = MNT_NOWAIT;
1021 		vn_lock(ump->um_devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1022 		if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
1023 			scaninfo.allerror = error;
1024 		VOP_UNLOCK(ump->um_devvp, NULL, 0, td);
1025 	}
1026 #ifdef QUOTA
1027 	qsync(mp);
1028 #endif
1029 	/*
1030 	 * Write back modified superblock.
1031 	 */
1032 	if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1033 		scaninfo.allerror = error;
1034 	return (scaninfo.allerror);
1035 }
1036 
1037 static
1038 int
1039 ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1040 {
1041 	struct inode *ip;
1042 
1043 	/*
1044 	 * Depend on the mount list's vnode lock to keep things stable
1045 	 * enough for a quick test.  Since there might be hundreds of
1046 	 * thousands of vnodes, we cannot afford even a subroutine
1047 	 * call unless there's a good chance that we have work to do.
1048 	 */
1049 	ip = VTOI(vp);
1050 	if (vp->v_type == VNON || ((ip->i_flag &
1051 	     (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1052 	     TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1053 		return(-1);
1054 	}
1055 	return(0);
1056 }
1057 
1058 static
1059 int
1060 ffs_sync_scan2(struct mount *mp, struct vnode *vp,
1061                 lwkt_tokref_t vlock, void *data)
1062 {
1063 	struct scaninfo *info = data;
1064 	thread_t td = curthread;	/* XXX */
1065 	struct inode *ip;
1066 	int error;
1067 
1068 	/*
1069 	 * We have to recheck after having obtained the vnode interlock.
1070 	 */
1071 	ip = VTOI(vp);
1072 	if (vp->v_type == VNON || ((ip->i_flag &
1073 	     (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1074 	     TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1075 		lwkt_reltoken(vlock);
1076 		return(0);
1077 	}
1078 	if (vp->v_type != VCHR) {
1079 		error = vget(vp, vlock, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT, td);
1080 		if (error) {
1081 			if (error == ENOENT)
1082 				info->rescan = 1;
1083 		} else {
1084 			if ((error = VOP_FSYNC(vp, info->waitfor, td)) != 0)
1085 				info->allerror = error;
1086 			VOP_UNLOCK(vp, NULL, 0, td);
1087 			vrele(vp);
1088 		}
1089 	} else {
1090 		/*
1091 		 * We must reference the vp to prevent it from
1092 		 * getting ripped out from under UFS_UPDATE, since
1093 		 * we are not holding a vnode lock.
1094 		 */
1095 		VREF(vp);
1096 		lwkt_reltoken(vlock);
1097 		/* UFS_UPDATE(vp, waitfor == MNT_WAIT); */
1098 		UFS_UPDATE(vp, 0);
1099 		vrele(vp);
1100 	}
1101 	return(0);
1102 }
1103 
1104 /*
1105  * Look up a FFS dinode number to find its incore vnode, otherwise read it
1106  * in from disk.  If it is in core, wait for the lock bit to clear, then
1107  * return the inode locked.  Detection and handling of mount points must be
1108  * done by the calling routine.
1109  */
1110 static int ffs_inode_hash_lock;
1111 
1112 int
1113 ffs_vget(mp, ino, vpp)
1114 	struct mount *mp;
1115 	ino_t ino;
1116 	struct vnode **vpp;
1117 {
1118 	struct fs *fs;
1119 	struct inode *ip;
1120 	struct ufsmount *ump;
1121 	struct buf *bp;
1122 	struct vnode *vp;
1123 	dev_t dev;
1124 	int error;
1125 
1126 	ump = VFSTOUFS(mp);
1127 	dev = ump->um_dev;
1128 restart:
1129 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL) {
1130 		return (0);
1131 	}
1132 
1133 	/*
1134 	 * Lock out the creation of new entries in the FFS hash table in
1135 	 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1136 	 * may occur!
1137 	 */
1138 	if (ffs_inode_hash_lock) {
1139 		while (ffs_inode_hash_lock) {
1140 			ffs_inode_hash_lock = -1;
1141 			tsleep(&ffs_inode_hash_lock, 0, "ffsvgt", 0);
1142 		}
1143 		goto restart;
1144 	}
1145 	ffs_inode_hash_lock = 1;
1146 
1147 	/*
1148 	 * If this MALLOC() is performed after the getnewvnode()
1149 	 * it might block, leaving a vnode with a NULL v_data to be
1150 	 * found by ffs_sync() if a sync happens to fire right then,
1151 	 * which will cause a panic because ffs_sync() blindly
1152 	 * dereferences vp->v_data (as well it should).
1153 	 */
1154 	MALLOC(ip, struct inode *, sizeof(struct inode),
1155 	    ump->um_malloctype, M_WAITOK);
1156 
1157 	/* Allocate a new vnode/inode. */
1158 	error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp);
1159 	if (error) {
1160 		if (ffs_inode_hash_lock < 0)
1161 			wakeup(&ffs_inode_hash_lock);
1162 		ffs_inode_hash_lock = 0;
1163 		*vpp = NULL;
1164 		FREE(ip, ump->um_malloctype);
1165 		return (error);
1166 	}
1167 	bzero((caddr_t)ip, sizeof(struct inode));
1168 	lockinit(&ip->i_lock, 0, "inode", VLKTIMEOUT, LK_CANRECURSE);
1169 	vp->v_data = ip;
1170 	/*
1171 	 * FFS supports lock sharing in the stack of vnodes
1172 	 */
1173 	vp->v_vnlock = &ip->i_lock;
1174 	ip->i_vnode = vp;
1175 	ip->i_fs = fs = ump->um_fs;
1176 	ip->i_dev = dev;
1177 	ip->i_number = ino;
1178 #ifdef QUOTA
1179 	{
1180 		int i;
1181 		for (i = 0; i < MAXQUOTAS; i++)
1182 			ip->i_dquot[i] = NODQUOT;
1183 	}
1184 #endif
1185 	/*
1186 	 * Put it onto its hash chain and lock it so that other requests for
1187 	 * this inode will block if they arrive while we are sleeping waiting
1188 	 * for old data structures to be purged or for the contents of the
1189 	 * disk portion of this inode to be read.
1190 	 */
1191 	ufs_ihashins(ip);
1192 
1193 	if (ffs_inode_hash_lock < 0)
1194 		wakeup(&ffs_inode_hash_lock);
1195 	ffs_inode_hash_lock = 0;
1196 
1197 	/* Read in the disk contents for the inode, copy into the inode. */
1198 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1199 	    (int)fs->fs_bsize, &bp);
1200 	if (error) {
1201 		/*
1202 		 * The inode does not contain anything useful, so it would
1203 		 * be misleading to leave it on its hash chain. With mode
1204 		 * still zero, it will be unlinked and returned to the free
1205 		 * list by vput().
1206 		 */
1207 		brelse(bp);
1208 		vput(vp);
1209 		*vpp = NULL;
1210 		return (error);
1211 	}
1212 	ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1213 	if (DOINGSOFTDEP(vp))
1214 		softdep_load_inodeblock(ip);
1215 	else
1216 		ip->i_effnlink = ip->i_nlink;
1217 	bqrelse(bp);
1218 
1219 	/*
1220 	 * Initialize the vnode from the inode, check for aliases.
1221 	 * Note that the underlying vnode may have changed.
1222 	 */
1223 	error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1224 	if (error) {
1225 		vput(vp);
1226 		*vpp = NULL;
1227 		return (error);
1228 	}
1229 	/*
1230 	 * Finish inode initialization now that aliasing has been resolved.
1231 	 */
1232 	ip->i_devvp = ump->um_devvp;
1233 	VREF(ip->i_devvp);
1234 	/*
1235 	 * Set up a generation number for this inode if it does not
1236 	 * already have one. This should only happen on old filesystems.
1237 	 */
1238 	if (ip->i_gen == 0) {
1239 		ip->i_gen = random() / 2 + 1;
1240 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1241 			ip->i_flag |= IN_MODIFIED;
1242 	}
1243 	/*
1244 	 * Ensure that uid and gid are correct. This is a temporary
1245 	 * fix until fsck has been changed to do the update.
1246 	 */
1247 	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
1248 		ip->i_uid = ip->i_din.di_ouid;		/* XXX */
1249 		ip->i_gid = ip->i_din.di_ogid;		/* XXX */
1250 	}						/* XXX */
1251 
1252 	*vpp = vp;
1253 	return (0);
1254 }
1255 
1256 /*
1257  * File handle to vnode
1258  *
1259  * Have to be really careful about stale file handles:
1260  * - check that the inode number is valid
1261  * - call ffs_vget() to get the locked inode
1262  * - check for an unallocated inode (i_mode == 0)
1263  * - check that the given client host has export rights and return
1264  *   those rights via. exflagsp and credanonp
1265  */
1266 int
1267 ffs_fhtovp(mp, fhp, vpp)
1268 	struct mount *mp;
1269 	struct fid *fhp;
1270 	struct vnode **vpp;
1271 {
1272 	struct ufid *ufhp;
1273 	struct fs *fs;
1274 
1275 	ufhp = (struct ufid *)fhp;
1276 	fs = VFSTOUFS(mp)->um_fs;
1277 	if (ufhp->ufid_ino < ROOTINO ||
1278 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1279 		return (ESTALE);
1280 	return (ufs_fhtovp(mp, ufhp, vpp));
1281 }
1282 
1283 /*
1284  * Vnode pointer to File handle
1285  */
1286 /* ARGSUSED */
1287 int
1288 ffs_vptofh(vp, fhp)
1289 	struct vnode *vp;
1290 	struct fid *fhp;
1291 {
1292 	struct inode *ip;
1293 	struct ufid *ufhp;
1294 
1295 	ip = VTOI(vp);
1296 	ufhp = (struct ufid *)fhp;
1297 	ufhp->ufid_len = sizeof(struct ufid);
1298 	ufhp->ufid_ino = ip->i_number;
1299 	ufhp->ufid_gen = ip->i_gen;
1300 	return (0);
1301 }
1302 
1303 /*
1304  * Initialize the filesystem; just use ufs_init.
1305  */
1306 static int
1307 ffs_init(vfsp)
1308 	struct vfsconf *vfsp;
1309 {
1310 
1311 	softdep_initialize();
1312 	return (ufs_init(vfsp));
1313 }
1314 
1315 /*
1316  * Write a superblock and associated information back to disk.
1317  */
1318 static int
1319 ffs_sbupdate(mp, waitfor)
1320 	struct ufsmount *mp;
1321 	int waitfor;
1322 {
1323 	struct fs *dfs, *fs = mp->um_fs;
1324 	struct buf *bp;
1325 	int blks;
1326 	void *space;
1327 	int i, size, error, allerror = 0;
1328 
1329 	/*
1330 	 * First write back the summary information.
1331 	 */
1332 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1333 	space = fs->fs_csp;
1334 	for (i = 0; i < blks; i += fs->fs_frag) {
1335 		size = fs->fs_bsize;
1336 		if (i + fs->fs_frag > blks)
1337 			size = (blks - i) * fs->fs_fsize;
1338 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1339 		    size, 0, 0);
1340 		bcopy(space, bp->b_data, (u_int)size);
1341 		space = (char *)space + size;
1342 		if (waitfor != MNT_WAIT)
1343 			bawrite(bp);
1344 		else if ((error = bwrite(bp)) != 0)
1345 			allerror = error;
1346 	}
1347 	/*
1348 	 * Now write back the superblock itself. If any errors occurred
1349 	 * up to this point, then fail so that the superblock avoids
1350 	 * being written out as clean.
1351 	 */
1352 	if (allerror)
1353 		return (allerror);
1354 	bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0);
1355 	fs->fs_fmod = 0;
1356 	fs->fs_time = time_second;
1357 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1358 	/* Restore compatibility to old file systems.		   XXX */
1359 	dfs = (struct fs *)bp->b_data;				/* XXX */
1360 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
1361 		dfs->fs_nrpos = -1;				/* XXX */
1362 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
1363 		int32_t *lp, tmp;				/* XXX */
1364 								/* XXX */
1365 		lp = (int32_t *)&dfs->fs_qbmask;		/* XXX */
1366 		tmp = lp[4];					/* XXX */
1367 		for (i = 4; i > 0; i--)				/* XXX */
1368 			lp[i] = lp[i-1];			/* XXX */
1369 		lp[0] = tmp;					/* XXX */
1370 	}							/* XXX */
1371 	dfs->fs_maxfilesize = mp->um_savedmaxfilesize;		/* XXX */
1372 	if (waitfor != MNT_WAIT)
1373 		bawrite(bp);
1374 	else if ((error = bwrite(bp)) != 0)
1375 		allerror = error;
1376 	return (allerror);
1377 }
1378