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