Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.96
      1 /*	$NetBSD: ffs_vfsops.c,v 1.96 2002/04/01 01:52:44 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.96 2002/04/01 01:52:44 christos Exp $");
     40 
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_ffs.h"
     43 #include "opt_quota.h"
     44 #include "opt_compat_netbsd.h"
     45 #include "opt_softdep.h"
     46 #endif
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/namei.h>
     51 #include <sys/proc.h>
     52 #include <sys/kernel.h>
     53 #include <sys/vnode.h>
     54 #include <sys/socket.h>
     55 #include <sys/mount.h>
     56 #include <sys/buf.h>
     57 #include <sys/device.h>
     58 #include <sys/mbuf.h>
     59 #include <sys/file.h>
     60 #include <sys/disklabel.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/errno.h>
     63 #include <sys/malloc.h>
     64 #include <sys/pool.h>
     65 #include <sys/lock.h>
     66 #include <sys/sysctl.h>
     67 
     68 #include <miscfs/specfs/specdev.h>
     69 
     70 #include <ufs/ufs/quota.h>
     71 #include <ufs/ufs/ufsmount.h>
     72 #include <ufs/ufs/inode.h>
     73 #include <ufs/ufs/dir.h>
     74 #include <ufs/ufs/ufs_extern.h>
     75 #include <ufs/ufs/ufs_bswap.h>
     76 
     77 #include <ufs/ffs/fs.h>
     78 #include <ufs/ffs/ffs_extern.h>
     79 
     80 /* how many times ffs_init() was called */
     81 int ffs_initcount = 0;
     82 
     83 extern struct lock ufs_hashlock;
     84 
     85 extern struct vnodeopv_desc ffs_vnodeop_opv_desc;
     86 extern struct vnodeopv_desc ffs_specop_opv_desc;
     87 extern struct vnodeopv_desc ffs_fifoop_opv_desc;
     88 
     89 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
     90 	&ffs_vnodeop_opv_desc,
     91 	&ffs_specop_opv_desc,
     92 	&ffs_fifoop_opv_desc,
     93 	NULL,
     94 };
     95 
     96 struct vfsops ffs_vfsops = {
     97 	MOUNT_FFS,
     98 	ffs_mount,
     99 	ufs_start,
    100 	ffs_unmount,
    101 	ufs_root,
    102 	ufs_quotactl,
    103 	ffs_statfs,
    104 	ffs_sync,
    105 	ffs_vget,
    106 	ffs_fhtovp,
    107 	ffs_vptofh,
    108 	ffs_init,
    109 	ffs_reinit,
    110 	ffs_done,
    111 	ffs_sysctl,
    112 	ffs_mountroot,
    113 	ufs_check_export,
    114 	ffs_vnodeopv_descs,
    115 };
    116 
    117 struct genfs_ops ffs_genfsops = {
    118 	ffs_gop_size,
    119 	ffs_gop_alloc,
    120 	genfs_gop_write,
    121 };
    122 
    123 struct pool ffs_inode_pool;
    124 
    125 /*
    126  * Called by main() when ffs is going to be mounted as root.
    127  */
    128 
    129 int
    130 ffs_mountroot()
    131 {
    132 	struct fs *fs;
    133 	struct mount *mp;
    134 	struct proc *p = curproc;	/* XXX */
    135 	struct ufsmount *ump;
    136 	int error;
    137 
    138 	if (root_device->dv_class != DV_DISK)
    139 		return (ENODEV);
    140 
    141 	/*
    142 	 * Get vnodes for rootdev.
    143 	 */
    144 	if (bdevvp(rootdev, &rootvp))
    145 		panic("ffs_mountroot: can't setup bdevvp's");
    146 
    147 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
    148 		vrele(rootvp);
    149 		return (error);
    150 	}
    151 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
    152 		mp->mnt_op->vfs_refcount--;
    153 		vfs_unbusy(mp);
    154 		free(mp, M_MOUNT);
    155 		vrele(rootvp);
    156 		return (error);
    157 	}
    158 	simple_lock(&mountlist_slock);
    159 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    160 	simple_unlock(&mountlist_slock);
    161 	ump = VFSTOUFS(mp);
    162 	fs = ump->um_fs;
    163 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    164 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    165 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
    166 	vfs_unbusy(mp);
    167 	inittodr(fs->fs_time);
    168 	return (0);
    169 }
    170 
    171 /*
    172  * VFS Operations.
    173  *
    174  * mount system call
    175  */
    176 int
    177 ffs_mount(mp, path, data, ndp, p)
    178 	struct mount *mp;
    179 	const char *path;
    180 	void *data;
    181 	struct nameidata *ndp;
    182 	struct proc *p;
    183 {
    184 	struct vnode *devvp;
    185 	struct ufs_args args;
    186 	struct ufsmount *ump = NULL;
    187 	struct fs *fs;
    188 	size_t size;
    189 	int error, flags, update;
    190 	mode_t accessmode;
    191 
    192 	error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
    193 	if (error)
    194 		return (error);
    195 
    196 #if !defined(SOFTDEP)
    197 	mp->mnt_flag &= ~MNT_SOFTDEP;
    198 #endif
    199 
    200 	update = mp->mnt_flag & MNT_UPDATE;
    201 
    202 	/* Check arguments */
    203 	if (update) {
    204 		/* Use the extant mount */
    205 		ump = VFSTOUFS(mp);
    206 		devvp = ump->um_devvp;
    207 	} else {
    208 		/* New mounts must have a filename for the device */
    209 		if (args.fspec == NULL)
    210 			return (EINVAL);
    211 	}
    212 
    213 	if (args.fspec) {
    214 		/*
    215 		 * Look up the name and verify that it's sane.
    216 		 */
    217 		NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    218 		if ((error = namei(ndp)) != 0)
    219 			return (error);
    220 		devvp = ndp->ni_vp;
    221 
    222 		if (!update) {
    223 			/*
    224 			 * Be sure this is a valid block device
    225 			 */
    226 			if (devvp->v_type != VBLK)
    227 				error = ENOTBLK;
    228 			else if (major(devvp->v_rdev) >= nblkdev)
    229 				error = ENXIO;
    230 		} else {
    231 			/*
    232 			 * Be sure we're still naming the same device
    233 			 * used for our initial mount
    234 			 */
    235 			if (devvp != ump->um_devvp)
    236 				error = EINVAL;
    237 		}
    238 	}
    239 
    240 	/*
    241 	 * If mount by non-root, then verify that user has necessary
    242 	 * permissions on the device.
    243 	 */
    244 	if (error == 0 && p->p_ucred->cr_uid != 0) {
    245 		accessmode = VREAD;
    246 		if (update ?
    247 		    (mp->mnt_flag & MNT_WANTRDWR) != 0 :
    248 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    249 			accessmode |= VWRITE;
    250 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    251 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    252 		VOP_UNLOCK(devvp, 0);
    253 	}
    254 
    255 	if (error) {
    256 		vrele(devvp);
    257 		return (error);
    258 	}
    259 
    260 	if (!update) {
    261 		error = ffs_mountfs(devvp, mp, p);
    262 		if (error) {
    263 			vrele(devvp);
    264 			return (error);
    265 		}
    266 
    267 		ump = VFSTOUFS(mp);
    268 		fs = ump->um_fs;
    269 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    270 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    271 			printf("%s fs uses soft updates, "
    272 			    "ignoring async mode\n",
    273 			    fs->fs_fsmnt);
    274 			mp->mnt_flag &= ~MNT_ASYNC;
    275 		}
    276 	} else {
    277 		/*
    278 		 * Update the mount.
    279 		 */
    280 
    281 		/*
    282 		 * The initial mount got a reference on this
    283 		 * device, so drop the one obtained via
    284 		 * namei(), above.
    285 		 */
    286 		vrele(devvp);
    287 
    288 		fs = ump->um_fs;
    289 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    290 			/*
    291 			 * Changing from r/w to r/o
    292 			 */
    293 			flags = WRITECLOSE;
    294 			if (mp->mnt_flag & MNT_FORCE)
    295 				flags |= FORCECLOSE;
    296 			if (mp->mnt_flag & MNT_SOFTDEP)
    297 				error = softdep_flushfiles(mp, flags, p);
    298 			else
    299 				error = ffs_flushfiles(mp, flags, p);
    300 			if (fs->fs_pendingblocks != 0 ||
    301 			    fs->fs_pendinginodes != 0) {
    302 				printf("%s: update error: blocks %d files %d\n",
    303 				    fs->fs_fsmnt, fs->fs_pendingblocks,
    304 				    fs->fs_pendinginodes);
    305 				fs->fs_pendingblocks = 0;
    306 				fs->fs_pendinginodes = 0;
    307 			}
    308 			if (error == 0 &&
    309 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    310 			    fs->fs_clean & FS_WASCLEAN) {
    311 				if (mp->mnt_flag & MNT_SOFTDEP)
    312 					fs->fs_flags &= ~FS_DOSOFTDEP;
    313 				fs->fs_clean = FS_ISCLEAN;
    314 				(void) ffs_sbupdate(ump, MNT_WAIT);
    315 			}
    316 			if (error)
    317 				return (error);
    318 			fs->fs_ronly = 1;
    319 			fs->fs_fmod = 0;
    320 		}
    321 
    322 		/*
    323 		 * Flush soft dependencies if disabling it via an update
    324 		 * mount. This may leave some items to be processed,
    325 		 * so don't do this yet XXX.
    326 		 */
    327 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
    328 		    !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    329 #ifdef notyet
    330 			flags = WRITECLOSE;
    331 			if (mp->mnt_flag & MNT_FORCE)
    332 				flags |= FORCECLOSE;
    333 			error = softdep_flushfiles(mp, flags, p);
    334 			if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
    335 				fs->fs_flags &= ~FS_DOSOFTDEP;
    336 				(void) ffs_sbupdate(ump, MNT_WAIT);
    337 #elif defined(SOFTDEP)
    338 			mp->mnt_flag |= MNT_SOFTDEP;
    339 #endif
    340 		}
    341 
    342 		/*
    343 		 * When upgrading to a softdep mount, we must first flush
    344 		 * all vnodes. (not done yet -- see above)
    345 		 */
    346 		if (!(fs->fs_flags & FS_DOSOFTDEP) &&
    347 		    (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    348 #ifdef notyet
    349 			flags = WRITECLOSE;
    350 			if (mp->mnt_flag & MNT_FORCE)
    351 				flags |= FORCECLOSE;
    352 			error = ffs_flushfiles(mp, flags, p);
    353 #else
    354 			mp->mnt_flag &= ~MNT_SOFTDEP;
    355 #endif
    356 		}
    357 
    358 		if (mp->mnt_flag & MNT_RELOAD) {
    359 			error = ffs_reload(mp, p->p_ucred, p);
    360 			if (error)
    361 				return (error);
    362 		}
    363 
    364 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
    365 			/*
    366 			 * Changing from read-only to read/write
    367 			 */
    368 			fs->fs_ronly = 0;
    369 			fs->fs_clean <<= 1;
    370 			fs->fs_fmod = 1;
    371 			if ((fs->fs_flags & FS_DOSOFTDEP)) {
    372 				error = softdep_mount(devvp, mp, fs,
    373 				    p->p_ucred);
    374 				if (error)
    375 					return (error);
    376 			}
    377 		}
    378 		if (args.fspec == 0) {
    379 			/*
    380 			 * Process export requests.
    381 			 */
    382 			return (vfs_export(mp, &ump->um_export, &args.export));
    383 		}
    384 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    385 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    386 			printf("%s fs uses soft updates, ignoring async mode\n",
    387 			    fs->fs_fsmnt);
    388 			mp->mnt_flag &= ~MNT_ASYNC;
    389 		}
    390 	}
    391 
    392 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
    393 	memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
    394 	memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
    395 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    396 	    &size);
    397 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
    398 	if (mp->mnt_flag & MNT_SOFTDEP)
    399 		fs->fs_flags |= FS_DOSOFTDEP;
    400 	else
    401 		fs->fs_flags &= ~FS_DOSOFTDEP;
    402 	if (fs->fs_fmod != 0) {	/* XXX */
    403 		fs->fs_fmod = 0;
    404 		if (fs->fs_clean & FS_WASCLEAN)
    405 			fs->fs_time = time.tv_sec;
    406 		else {
    407 			printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
    408 			    mp->mnt_stat.f_mntfromname, fs->fs_clean);
    409 			printf("%s: lost blocks %d files %d\n",
    410 			    mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
    411 			    fs->fs_pendinginodes);
    412 		}
    413 		(void) ffs_cgupdate(ump, MNT_WAIT);
    414 	}
    415 	return (0);
    416 }
    417 
    418 /*
    419  * Reload all incore data for a filesystem (used after running fsck on
    420  * the root filesystem and finding things to fix). The filesystem must
    421  * be mounted read-only.
    422  *
    423  * Things to do to update the mount:
    424  *	1) invalidate all cached meta-data.
    425  *	2) re-read superblock from disk.
    426  *	3) re-read summary information from disk.
    427  *	4) invalidate all inactive vnodes.
    428  *	5) invalidate all cached file data.
    429  *	6) re-read inode data for all active vnodes.
    430  */
    431 int
    432 ffs_reload(mountp, cred, p)
    433 	struct mount *mountp;
    434 	struct ucred *cred;
    435 	struct proc *p;
    436 {
    437 	struct vnode *vp, *nvp, *devvp;
    438 	struct inode *ip;
    439 	void *space;
    440 	struct buf *bp;
    441 	struct fs *fs, *newfs;
    442 	struct partinfo dpart;
    443 	int i, blks, size, error;
    444 	int32_t *lp;
    445 	caddr_t cp;
    446 
    447 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    448 		return (EINVAL);
    449 	/*
    450 	 * Step 1: invalidate all cached meta-data.
    451 	 */
    452 	devvp = VFSTOUFS(mountp)->um_devvp;
    453 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    454 	error = vinvalbuf(devvp, 0, cred, p, 0, 0);
    455 	VOP_UNLOCK(devvp, 0);
    456 	if (error)
    457 		panic("ffs_reload: dirty1");
    458 	/*
    459 	 * Step 2: re-read superblock from disk.
    460 	 */
    461 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
    462 		size = DEV_BSIZE;
    463 	else
    464 		size = dpart.disklab->d_secsize;
    465 	error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
    466 	if (error) {
    467 		brelse(bp);
    468 		return (error);
    469 	}
    470 	fs = VFSTOUFS(mountp)->um_fs;
    471 	newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
    472 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
    473 #ifdef FFS_EI
    474 	if (VFSTOUFS(mountp)->um_flags & UFS_NEEDSWAP) {
    475 		ffs_sb_swap((struct fs*)bp->b_data, newfs);
    476 		fs->fs_flags |= FS_SWAPPED;
    477 	}
    478 #endif
    479 	if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
    480 	    newfs->fs_bsize < sizeof(struct fs)) {
    481 		brelse(bp);
    482 		free(newfs, M_UFSMNT);
    483 		return (EIO);		/* XXX needs translation */
    484 	}
    485 	/*
    486 	 * Copy pointer fields back into superblock before copying in	XXX
    487 	 * new superblock. These should really be in the ufsmount.	XXX
    488 	 * Note that important parameters (eg fs_ncg) are unchanged.
    489 	 */
    490 	newfs->fs_csp = fs->fs_csp;
    491 	newfs->fs_maxcluster = fs->fs_maxcluster;
    492 	newfs->fs_contigdirs = fs->fs_contigdirs;
    493 	newfs->fs_ronly = fs->fs_ronly;
    494 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    495 	if (fs->fs_sbsize < SBSIZE)
    496 		bp->b_flags |= B_INVAL;
    497 	brelse(bp);
    498 	free(newfs, M_UFSMNT);
    499 	mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    500 	ffs_oldfscompat(fs);
    501 		/* An old fsck may have zeroed these fields, so recheck them. */
    502 	if (fs->fs_avgfilesize <= 0)
    503 		fs->fs_avgfilesize = AVFILESIZ;
    504 	if (fs->fs_avgfpdir <= 0)
    505 		fs->fs_avgfpdir = AFPDIR;
    506 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    507 		fs->fs_pendingblocks = 0;
    508 		fs->fs_pendinginodes = 0;
    509 	}
    510 
    511 	ffs_statfs(mountp, &mountp->mnt_stat, p);
    512 	/*
    513 	 * Step 3: re-read summary information from disk.
    514 	 */
    515 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    516 	space = fs->fs_csp;
    517 	for (i = 0; i < blks; i += fs->fs_frag) {
    518 		size = fs->fs_bsize;
    519 		if (i + fs->fs_frag > blks)
    520 			size = (blks - i) * fs->fs_fsize;
    521 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    522 			      NOCRED, &bp);
    523 		if (error) {
    524 			brelse(bp);
    525 			return (error);
    526 		}
    527 #ifdef FFS_EI
    528 		if (UFS_FSNEEDSWAP(fs))
    529 			ffs_csum_swap((struct csum *)bp->b_data,
    530 			    (struct csum *)space, size);
    531 		else
    532 #endif
    533 			memcpy(space, bp->b_data, (size_t)size);
    534 		space = (char *)space + size;
    535 		brelse(bp);
    536 	}
    537 	if ((fs->fs_flags & FS_DOSOFTDEP))
    538 		softdep_mount(devvp, mountp, fs, cred);
    539 	/*
    540 	 * We no longer know anything about clusters per cylinder group.
    541 	 */
    542 	if (fs->fs_contigsumsize > 0) {
    543 		lp = fs->fs_maxcluster;
    544 		for (i = 0; i < fs->fs_ncg; i++)
    545 			*lp++ = fs->fs_contigsumsize;
    546 	}
    547 
    548 loop:
    549 	simple_lock(&mntvnode_slock);
    550 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    551 		if (vp->v_mount != mountp) {
    552 			simple_unlock(&mntvnode_slock);
    553 			goto loop;
    554 		}
    555 		nvp = vp->v_mntvnodes.le_next;
    556 		/*
    557 		 * Step 4: invalidate all inactive vnodes.
    558 		 */
    559 		if (vrecycle(vp, &mntvnode_slock, p))
    560 			goto loop;
    561 		/*
    562 		 * Step 5: invalidate all cached file data.
    563 		 */
    564 		simple_lock(&vp->v_interlock);
    565 		simple_unlock(&mntvnode_slock);
    566 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    567 			goto loop;
    568 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    569 			panic("ffs_reload: dirty2");
    570 		/*
    571 		 * Step 6: re-read inode data for all active vnodes.
    572 		 */
    573 		ip = VTOI(vp);
    574 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    575 			      (int)fs->fs_bsize, NOCRED, &bp);
    576 		if (error) {
    577 			brelse(bp);
    578 			vput(vp);
    579 			return (error);
    580 		}
    581 		cp = (caddr_t)bp->b_data +
    582 		    (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
    583 #ifdef FFS_EI
    584 		if (UFS_FSNEEDSWAP(fs))
    585 			ffs_dinode_swap((struct dinode *)cp,
    586 			    &ip->i_din.ffs_din);
    587 		else
    588 #endif
    589 			memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
    590 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
    591 		brelse(bp);
    592 		vput(vp);
    593 		simple_lock(&mntvnode_slock);
    594 	}
    595 	simple_unlock(&mntvnode_slock);
    596 	return (0);
    597 }
    598 
    599 /*
    600  * Common code for mount and mountroot
    601  */
    602 int
    603 ffs_mountfs(devvp, mp, p)
    604 	struct vnode *devvp;
    605 	struct mount *mp;
    606 	struct proc *p;
    607 {
    608 	struct ufsmount *ump;
    609 	struct buf *bp;
    610 	struct fs *fs;
    611 	dev_t dev;
    612 	struct partinfo dpart;
    613 	void *space;
    614 	int blks;
    615 	int error, i, size, ronly;
    616 #ifdef FFS_EI
    617 	int needswap;
    618 #endif
    619 	int32_t *lp;
    620 	struct ucred *cred;
    621 	u_int64_t maxfilesize;					/* XXX */
    622 	u_int32_t sbsize;
    623 
    624 	dev = devvp->v_rdev;
    625 	cred = p ? p->p_ucred : NOCRED;
    626 	/*
    627 	 * Disallow multiple mounts of the same device.
    628 	 * Disallow mounting of a device that is currently in use
    629 	 * (except for root, which might share swap device for miniroot).
    630 	 * Flush out any old buffers remaining from a previous use.
    631 	 */
    632 	if ((error = vfs_mountedon(devvp)) != 0)
    633 		return (error);
    634 	if (vcount(devvp) > 1 && devvp != rootvp)
    635 		return (EBUSY);
    636 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    637 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
    638 	VOP_UNLOCK(devvp, 0);
    639 	if (error)
    640 		return (error);
    641 
    642 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    643 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    644 	if (error)
    645 		return (error);
    646 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
    647 		size = DEV_BSIZE;
    648 	else
    649 		size = dpart.disklab->d_secsize;
    650 
    651 	bp = NULL;
    652 	ump = NULL;
    653 	error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, cred, &bp);
    654 	if (error)
    655 		goto out;
    656 
    657 	fs = (struct fs*)bp->b_data;
    658 	if (fs->fs_magic == FS_MAGIC) {
    659 		sbsize = fs->fs_sbsize;
    660 #ifdef FFS_EI
    661 		needswap = 0;
    662 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
    663 		sbsize = bswap32(fs->fs_sbsize);
    664 		needswap = 1;
    665 #endif
    666 	} else {
    667 		error = EINVAL;
    668 		goto out;
    669 	}
    670 	if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs)) {
    671 		error = EINVAL;
    672 		goto out;
    673 	}
    674 
    675 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    676 	memcpy(fs, bp->b_data, sbsize);
    677 #ifdef FFS_EI
    678 	if (needswap) {
    679 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    680 		fs->fs_flags |= FS_SWAPPED;
    681 	}
    682 #endif
    683 	ffs_oldfscompat(fs);
    684 
    685 	if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
    686 		error = EINVAL;
    687 		goto out;
    688 	}
    689 	 /* make sure cylinder group summary area is a reasonable size. */
    690 	if (fs->fs_cgsize == 0 || fs->fs_cpg == 0 ||
    691 	    fs->fs_ncg > fs->fs_ncyl / fs->fs_cpg + 1 ||
    692 	    fs->fs_cssize >
    693 	    fragroundup(fs, fs->fs_ncg * sizeof(struct csum))) {
    694 		error = EINVAL;		/* XXX needs translation */
    695 		goto out2;
    696 	}
    697 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    698 		fs->fs_pendingblocks = 0;
    699 		fs->fs_pendinginodes = 0;
    700 	}
    701 	/* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
    702 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
    703 		error = EROFS;		/* XXX what should be returned? */
    704 		goto out2;
    705 	}
    706 
    707 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    708 	memset((caddr_t)ump, 0, sizeof *ump);
    709 	ump->um_fs = fs;
    710 	if (fs->fs_sbsize < SBSIZE)
    711 		bp->b_flags |= B_INVAL;
    712 	brelse(bp);
    713 	bp = NULL;
    714 
    715 	/*
    716 	 * verify that we can access the last block in the fs.
    717 	 */
    718 
    719 	error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize, cred,
    720 	    &bp);
    721 	if (bp->b_bcount != fs->fs_fsize)
    722 		error = EINVAL;
    723 	bp->b_flags |= B_INVAL;
    724 	if (error)
    725 		goto out;
    726 	brelse(bp);
    727 	bp = NULL;
    728 
    729 	fs->fs_ronly = ronly;
    730 	if (ronly == 0) {
    731 		fs->fs_clean <<= 1;
    732 		fs->fs_fmod = 1;
    733 	}
    734 	size = fs->fs_cssize;
    735 	blks = howmany(size, fs->fs_fsize);
    736 	if (fs->fs_contigsumsize > 0)
    737 		size += fs->fs_ncg * sizeof(int32_t);
    738 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    739 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    740 	fs->fs_csp = space;
    741 	for (i = 0; i < blks; i += fs->fs_frag) {
    742 		size = fs->fs_bsize;
    743 		if (i + fs->fs_frag > blks)
    744 			size = (blks - i) * fs->fs_fsize;
    745 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    746 			      cred, &bp);
    747 		if (error) {
    748 			free(fs->fs_csp, M_UFSMNT);
    749 			goto out2;
    750 		}
    751 #ifdef FFS_EI
    752 		if (needswap)
    753 			ffs_csum_swap((struct csum *)bp->b_data,
    754 				(struct csum *)space, size);
    755 		else
    756 #endif
    757 			memcpy(space, bp->b_data, (u_int)size);
    758 
    759 		space = (char *)space + size;
    760 		brelse(bp);
    761 		bp = NULL;
    762 	}
    763 	if (fs->fs_contigsumsize > 0) {
    764 		fs->fs_maxcluster = lp = space;
    765 		for (i = 0; i < fs->fs_ncg; i++)
    766 			*lp++ = fs->fs_contigsumsize;
    767 		space = lp;
    768 	}
    769 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    770 	fs->fs_contigdirs = space;
    771 	space = (char *)space + size;
    772 	memset(fs->fs_contigdirs, 0, size);
    773 		/* Compatibility for old filesystems - XXX */
    774 	if (fs->fs_avgfilesize <= 0)
    775 		fs->fs_avgfilesize = AVFILESIZ;
    776 	if (fs->fs_avgfpdir <= 0)
    777 		fs->fs_avgfpdir = AFPDIR;
    778 	mp->mnt_data = (qaddr_t)ump;
    779 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    780 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
    781 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    782 	mp->mnt_fs_bshift = fs->fs_bshift;
    783 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    784 	mp->mnt_flag |= MNT_LOCAL;
    785 #ifdef FFS_EI
    786 	if (needswap)
    787 		ump->um_flags |= UFS_NEEDSWAP;
    788 #endif
    789 	ump->um_mountp = mp;
    790 	ump->um_dev = dev;
    791 	ump->um_devvp = devvp;
    792 	ump->um_nindir = fs->fs_nindir;
    793 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
    794 	ump->um_bptrtodb = fs->fs_fsbtodb;
    795 	ump->um_seqinc = fs->fs_frag;
    796 	for (i = 0; i < MAXQUOTAS; i++)
    797 		ump->um_quotas[i] = NULLVP;
    798 	devvp->v_specmountpoint = mp;
    799 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
    800 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;	/* XXX */
    801 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
    802 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
    803 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
    804 		error = softdep_mount(devvp, mp, fs, cred);
    805 		if (error) {
    806 			free(fs->fs_csp, M_UFSMNT);
    807 			goto out;
    808 		}
    809 	}
    810 	return (0);
    811 out2:
    812 	free(fs, M_UFSMNT);
    813 out:
    814 	devvp->v_specmountpoint = NULL;
    815 	if (bp)
    816 		brelse(bp);
    817 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    818 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    819 	VOP_UNLOCK(devvp, 0);
    820 	if (ump) {
    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 int
    833 ffs_oldfscompat(fs)
    834 	struct fs *fs;
    835 {
    836 	int i;
    837 
    838 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    839 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    840 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    841 		fs->fs_nrpos = 8;				/* XXX */
    842 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    843 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
    844 								/* XXX */
    845 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    846 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    847 			sizepb *= NINDIR(fs);			/* XXX */
    848 			fs->fs_maxfilesize += sizepb;		/* XXX */
    849 		}						/* XXX */
    850 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    851 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    852 	}							/* XXX */
    853 	return (0);
    854 }
    855 
    856 /*
    857  * unmount system call
    858  */
    859 int
    860 ffs_unmount(mp, mntflags, p)
    861 	struct mount *mp;
    862 	int mntflags;
    863 	struct proc *p;
    864 {
    865 	struct ufsmount *ump;
    866 	struct fs *fs;
    867 	int error, flags, penderr;
    868 
    869 	penderr = 0;
    870 	flags = 0;
    871 	if (mntflags & MNT_FORCE)
    872 		flags |= FORCECLOSE;
    873 	if (mp->mnt_flag & MNT_SOFTDEP) {
    874 		if ((error = softdep_flushfiles(mp, flags, p)) != 0)
    875 			return (error);
    876 	} else {
    877 		if ((error = ffs_flushfiles(mp, flags, p)) != 0)
    878 			return (error);
    879 	}
    880 	ump = VFSTOUFS(mp);
    881 	fs = ump->um_fs;
    882 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    883 		printf("%s: unmount pending error: blocks %d files %d\n",
    884 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
    885 		fs->fs_pendingblocks = 0;
    886 		fs->fs_pendinginodes = 0;
    887 		penderr = 1;
    888 	}
    889 	if (fs->fs_ronly == 0 &&
    890 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    891 	    fs->fs_clean & FS_WASCLEAN) {
    892 		/*
    893 		 * XXXX don't mark fs clean in the case of softdep
    894 		 * pending block errors, until they are fixed.
    895 		 */
    896 		if (penderr == 0) {
    897 			if (mp->mnt_flag & MNT_SOFTDEP)
    898 				fs->fs_flags &= ~FS_DOSOFTDEP;
    899 			fs->fs_clean = FS_ISCLEAN;
    900 		}
    901 		(void) ffs_sbupdate(ump, MNT_WAIT);
    902 	}
    903 	if (ump->um_devvp->v_type != VBAD)
    904 		ump->um_devvp->v_specmountpoint = NULL;
    905 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    906 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
    907 		NOCRED, p);
    908 	vput(ump->um_devvp);
    909 	free(fs->fs_csp, M_UFSMNT);
    910 	free(fs, M_UFSMNT);
    911 	free(ump, M_UFSMNT);
    912 	mp->mnt_data = (qaddr_t)0;
    913 	mp->mnt_flag &= ~MNT_LOCAL;
    914 	return (error);
    915 }
    916 
    917 /*
    918  * Flush out all the files in a filesystem.
    919  */
    920 int
    921 ffs_flushfiles(mp, flags, p)
    922 	struct mount *mp;
    923 	int flags;
    924 	struct proc *p;
    925 {
    926 	extern int doforce;
    927 	struct ufsmount *ump;
    928 	int error;
    929 
    930 	if (!doforce)
    931 		flags &= ~FORCECLOSE;
    932 	ump = VFSTOUFS(mp);
    933 #ifdef QUOTA
    934 	if (mp->mnt_flag & MNT_QUOTA) {
    935 		int i;
    936 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
    937 			return (error);
    938 		for (i = 0; i < MAXQUOTAS; i++) {
    939 			if (ump->um_quotas[i] == NULLVP)
    940 				continue;
    941 			quotaoff(p, mp, i);
    942 		}
    943 		/*
    944 		 * Here we fall through to vflush again to ensure
    945 		 * that we have gotten rid of all the system vnodes.
    946 		 */
    947 	}
    948 #endif
    949 	/*
    950 	 * Flush all the files.
    951 	 */
    952 	error = vflush(mp, NULLVP, flags);
    953 	if (error)
    954 		return (error);
    955 	/*
    956 	 * Flush filesystem metadata.
    957 	 */
    958 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    959 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
    960 	VOP_UNLOCK(ump->um_devvp, 0);
    961 	return (error);
    962 }
    963 
    964 /*
    965  * Get file system statistics.
    966  */
    967 int
    968 ffs_statfs(mp, sbp, p)
    969 	struct mount *mp;
    970 	struct statfs *sbp;
    971 	struct proc *p;
    972 {
    973 	struct ufsmount *ump;
    974 	struct fs *fs;
    975 
    976 	ump = VFSTOUFS(mp);
    977 	fs = ump->um_fs;
    978 	if (fs->fs_magic != FS_MAGIC)
    979 		panic("ffs_statfs");
    980 #ifdef COMPAT_09
    981 	sbp->f_type = 1;
    982 #else
    983 	sbp->f_type = 0;
    984 #endif
    985 	sbp->f_bsize = fs->fs_fsize;
    986 	sbp->f_iosize = fs->fs_bsize;
    987 	sbp->f_blocks = fs->fs_dsize;
    988 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
    989 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
    990 	sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
    991 	    (100 - fs->fs_minfree) / (u_int64_t) 100) -
    992 	    (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
    993 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
    994 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
    995 	if (sbp != &mp->mnt_stat) {
    996 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
    997 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
    998 	}
    999 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
   1000 	return (0);
   1001 }
   1002 
   1003 /*
   1004  * Go through the disk queues to initiate sandbagged IO;
   1005  * go through the inodes to write those that have been modified;
   1006  * initiate the writing of the super block if it has been modified.
   1007  *
   1008  * Note: we are always called with the filesystem marked `MPBUSY'.
   1009  */
   1010 int
   1011 ffs_sync(mp, waitfor, cred, p)
   1012 	struct mount *mp;
   1013 	int waitfor;
   1014 	struct ucred *cred;
   1015 	struct proc *p;
   1016 {
   1017 	struct vnode *vp, *nvp;
   1018 	struct inode *ip;
   1019 	struct ufsmount *ump = VFSTOUFS(mp);
   1020 	struct fs *fs;
   1021 	int error, allerror = 0;
   1022 
   1023 	fs = ump->um_fs;
   1024 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1025 		printf("fs = %s\n", fs->fs_fsmnt);
   1026 		panic("update: rofs mod");
   1027 	}
   1028 	/*
   1029 	 * Write back each (modified) inode.
   1030 	 */
   1031 	simple_lock(&mntvnode_slock);
   1032 loop:
   1033 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
   1034 		/*
   1035 		 * If the vnode that we are about to sync is no longer
   1036 		 * associated with this mount point, start over.
   1037 		 */
   1038 		if (vp->v_mount != mp)
   1039 			goto loop;
   1040 		simple_lock(&vp->v_interlock);
   1041 		nvp = LIST_NEXT(vp, v_mntvnodes);
   1042 		ip = VTOI(vp);
   1043 		if (vp->v_type == VNON ||
   1044 		    ((ip->i_flag &
   1045 		      (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1046 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1047 		     vp->v_uobj.uo_npages == 0))
   1048 		{
   1049 			simple_unlock(&vp->v_interlock);
   1050 			continue;
   1051 		}
   1052 		simple_unlock(&mntvnode_slock);
   1053 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1054 		if (error) {
   1055 			simple_lock(&mntvnode_slock);
   1056 			if (error == ENOENT)
   1057 				goto loop;
   1058 			continue;
   1059 		}
   1060 		if ((error = VOP_FSYNC(vp, cred,
   1061 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1062 			allerror = error;
   1063 		vput(vp);
   1064 		simple_lock(&mntvnode_slock);
   1065 	}
   1066 	simple_unlock(&mntvnode_slock);
   1067 	/*
   1068 	 * Force stale file system control information to be flushed.
   1069 	 */
   1070 	if (waitfor != MNT_LAZY) {
   1071 		if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
   1072 			waitfor = MNT_NOWAIT;
   1073 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1074 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1075 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1076 			allerror = error;
   1077 		VOP_UNLOCK(ump->um_devvp, 0);
   1078 	}
   1079 #ifdef QUOTA
   1080 	qsync(mp);
   1081 #endif
   1082 	/*
   1083 	 * Write back modified superblock.
   1084 	 */
   1085 	if (fs->fs_fmod != 0) {
   1086 		fs->fs_fmod = 0;
   1087 		fs->fs_time = time.tv_sec;
   1088 		if ((error = ffs_cgupdate(ump, waitfor)))
   1089 			allerror = error;
   1090 	}
   1091 	return (allerror);
   1092 }
   1093 
   1094 /*
   1095  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1096  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1097  * return the inode locked.  Detection and handling of mount points must be
   1098  * done by the calling routine.
   1099  */
   1100 int
   1101 ffs_vget(mp, ino, vpp)
   1102 	struct mount *mp;
   1103 	ino_t ino;
   1104 	struct vnode **vpp;
   1105 {
   1106 	struct fs *fs;
   1107 	struct inode *ip;
   1108 	struct ufsmount *ump;
   1109 	struct buf *bp;
   1110 	struct vnode *vp;
   1111 	dev_t dev;
   1112 	int error;
   1113 	caddr_t cp;
   1114 
   1115 	ump = VFSTOUFS(mp);
   1116 	dev = ump->um_dev;
   1117 
   1118 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1119 		return (0);
   1120 
   1121 	/* Allocate a new vnode/inode. */
   1122 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1123 		*vpp = NULL;
   1124 		return (error);
   1125 	}
   1126 
   1127 	/*
   1128 	 * If someone beat us to it while sleeping in getnewvnode(),
   1129 	 * push back the freshly allocated vnode we don't need, and return.
   1130 	 */
   1131 
   1132 	do {
   1133 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1134 			ungetnewvnode(vp);
   1135 			return (0);
   1136 		}
   1137 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1138 
   1139 	/*
   1140 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1141 	 * XXX create another pool for MFS inodes?
   1142 	 */
   1143 
   1144 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1145 	memset(ip, 0, sizeof(struct inode));
   1146 	vp->v_data = ip;
   1147 	ip->i_vnode = vp;
   1148 	ip->i_fs = fs = ump->um_fs;
   1149 	ip->i_dev = dev;
   1150 	ip->i_number = ino;
   1151 	LIST_INIT(&ip->i_pcbufhd);
   1152 #ifdef QUOTA
   1153 	{
   1154 		int i;
   1155 
   1156 		for (i = 0; i < MAXQUOTAS; i++)
   1157 			ip->i_dquot[i] = NODQUOT;
   1158 	}
   1159 #endif
   1160 
   1161 	/*
   1162 	 * Put it onto its hash chain and lock it so that other requests for
   1163 	 * this inode will block if they arrive while we are sleeping waiting
   1164 	 * for old data structures to be purged or for the contents of the
   1165 	 * disk portion of this inode to be read.
   1166 	 */
   1167 
   1168 	ufs_ihashins(ip);
   1169 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1170 
   1171 	/* Read in the disk contents for the inode, copy into the inode. */
   1172 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1173 		      (int)fs->fs_bsize, NOCRED, &bp);
   1174 	if (error) {
   1175 
   1176 		/*
   1177 		 * The inode does not contain anything useful, so it would
   1178 		 * be misleading to leave it on its hash chain. With mode
   1179 		 * still zero, it will be unlinked and returned to the free
   1180 		 * list by vput().
   1181 		 */
   1182 
   1183 		vput(vp);
   1184 		brelse(bp);
   1185 		*vpp = NULL;
   1186 		return (error);
   1187 	}
   1188 	cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
   1189 #ifdef FFS_EI
   1190 	if (UFS_FSNEEDSWAP(fs))
   1191 		ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
   1192 	else
   1193 #endif
   1194 		memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
   1195 	if (DOINGSOFTDEP(vp))
   1196 		softdep_load_inodeblock(ip);
   1197 	else
   1198 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1199 	brelse(bp);
   1200 
   1201 	/*
   1202 	 * Initialize the vnode from the inode, check for aliases.
   1203 	 * Note that the underlying vnode may have changed.
   1204 	 */
   1205 
   1206 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1207 
   1208 	/*
   1209 	 * Finish inode initialization now that aliasing has been resolved.
   1210 	 */
   1211 
   1212 	genfs_node_init(vp, &ffs_genfsops);
   1213 	ip->i_devvp = ump->um_devvp;
   1214 	VREF(ip->i_devvp);
   1215 
   1216 	/*
   1217 	 * Ensure that uid and gid are correct. This is a temporary
   1218 	 * fix until fsck has been changed to do the update.
   1219 	 */
   1220 
   1221 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1222 		ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid;	/* XXX */
   1223 		ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid;	/* XXX */
   1224 	}							/* XXX */
   1225 	uvm_vnp_setsize(vp, ip->i_ffs_size);
   1226 	*vpp = vp;
   1227 	return (0);
   1228 }
   1229 
   1230 /*
   1231  * File handle to vnode
   1232  *
   1233  * Have to be really careful about stale file handles:
   1234  * - check that the inode number is valid
   1235  * - call ffs_vget() to get the locked inode
   1236  * - check for an unallocated inode (i_mode == 0)
   1237  * - check that the given client host has export rights and return
   1238  *   those rights via. exflagsp and credanonp
   1239  */
   1240 int
   1241 ffs_fhtovp(mp, fhp, vpp)
   1242 	struct mount *mp;
   1243 	struct fid *fhp;
   1244 	struct vnode **vpp;
   1245 {
   1246 	struct ufid *ufhp;
   1247 	struct fs *fs;
   1248 
   1249 	ufhp = (struct ufid *)fhp;
   1250 	fs = VFSTOUFS(mp)->um_fs;
   1251 	if (ufhp->ufid_ino < ROOTINO ||
   1252 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1253 		return (ESTALE);
   1254 	return (ufs_fhtovp(mp, ufhp, vpp));
   1255 }
   1256 
   1257 /*
   1258  * Vnode pointer to File handle
   1259  */
   1260 /* ARGSUSED */
   1261 int
   1262 ffs_vptofh(vp, fhp)
   1263 	struct vnode *vp;
   1264 	struct fid *fhp;
   1265 {
   1266 	struct inode *ip;
   1267 	struct ufid *ufhp;
   1268 
   1269 	ip = VTOI(vp);
   1270 	ufhp = (struct ufid *)fhp;
   1271 	ufhp->ufid_len = sizeof(struct ufid);
   1272 	ufhp->ufid_ino = ip->i_number;
   1273 	ufhp->ufid_gen = ip->i_ffs_gen;
   1274 	return (0);
   1275 }
   1276 
   1277 void
   1278 ffs_init()
   1279 {
   1280 	if (ffs_initcount++ > 0)
   1281 		return;
   1282 
   1283 	softdep_initialize();
   1284 	ufs_init();
   1285 
   1286 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
   1287 	    &pool_allocator_nointr);
   1288 }
   1289 
   1290 void
   1291 ffs_reinit()
   1292 {
   1293 	softdep_reinitialize();
   1294 	ufs_reinit();
   1295 }
   1296 
   1297 void
   1298 ffs_done()
   1299 {
   1300 	if (--ffs_initcount > 0)
   1301 		return;
   1302 
   1303 	/* XXX softdep cleanup ? */
   1304 	ufs_done();
   1305 	pool_destroy(&ffs_inode_pool);
   1306 }
   1307 
   1308 int
   1309 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1310 	int *name;
   1311 	u_int namelen;
   1312 	void *oldp;
   1313 	size_t *oldlenp;
   1314 	void *newp;
   1315 	size_t newlen;
   1316 	struct proc *p;
   1317 {
   1318 	extern int doasyncfree;
   1319 	extern int ffs_log_changeopt;
   1320 
   1321 	/* all sysctl names at this level are terminal */
   1322 	if (namelen != 1)
   1323 		return (ENOTDIR);		/* overloaded */
   1324 
   1325 	switch (name[0]) {
   1326 	case FFS_ASYNCFREE:
   1327 		return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
   1328 	case FFS_LOG_CHANGEOPT:
   1329 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1330 			&ffs_log_changeopt));
   1331 	default:
   1332 		return (EOPNOTSUPP);
   1333 	}
   1334 	/* NOTREACHED */
   1335 }
   1336 
   1337 /*
   1338  * Write a superblock and associated information back to disk.
   1339  */
   1340 int
   1341 ffs_sbupdate(mp, waitfor)
   1342 	struct ufsmount *mp;
   1343 	int waitfor;
   1344 {
   1345 	struct fs *fs = mp->um_fs;
   1346 	struct buf *bp;
   1347 	int i, error = 0;
   1348 	int32_t saved_nrpos = fs->fs_nrpos;
   1349 	int64_t saved_qbmask = fs->fs_qbmask;
   1350 	int64_t saved_qfmask = fs->fs_qfmask;
   1351 	u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
   1352 	u_int8_t saveflag;
   1353 
   1354 	/* Restore compatibility to old file systems.		   XXX */
   1355 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
   1356 		fs->fs_nrpos = -1;		/* XXX */
   1357 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1358 		int32_t *lp, tmp;				/* XXX */
   1359 								/* XXX */
   1360 		lp = (int32_t *)&fs->fs_qbmask;	/* XXX nuke qfmask too */
   1361 		tmp = lp[4];					/* XXX */
   1362 		for (i = 4; i > 0; i--)				/* XXX */
   1363 			lp[i] = lp[i-1];			/* XXX */
   1364 		lp[0] = tmp;					/* XXX */
   1365 	}							/* XXX */
   1366 	fs->fs_maxfilesize = mp->um_savedmaxfilesize;	/* XXX */
   1367 
   1368 	bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
   1369 	    (int)fs->fs_sbsize, 0, 0);
   1370 	saveflag = fs->fs_flags & FS_INTERNAL;
   1371 	fs->fs_flags &= ~FS_INTERNAL;
   1372 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1373 #ifdef FFS_EI
   1374 	if (mp->um_flags & UFS_NEEDSWAP)
   1375 		ffs_sb_swap(fs, (struct fs*)bp->b_data);
   1376 #endif
   1377 
   1378 	fs->fs_flags |= saveflag;
   1379 	fs->fs_nrpos = saved_nrpos; /* XXX */
   1380 	fs->fs_qbmask = saved_qbmask; /* XXX */
   1381 	fs->fs_qfmask = saved_qfmask; /* XXX */
   1382 	fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
   1383 
   1384 	if (waitfor == MNT_WAIT)
   1385 		error = bwrite(bp);
   1386 	else
   1387 		bawrite(bp);
   1388 	return (error);
   1389 }
   1390 
   1391 int
   1392 ffs_cgupdate(mp, waitfor)
   1393 	struct ufsmount *mp;
   1394 	int waitfor;
   1395 {
   1396 	struct fs *fs = mp->um_fs;
   1397 	struct buf *bp;
   1398 	int blks;
   1399 	void *space;
   1400 	int i, size, error = 0, allerror = 0;
   1401 
   1402 	allerror = ffs_sbupdate(mp, waitfor);
   1403 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1404 	space = fs->fs_csp;
   1405 	for (i = 0; i < blks; i += fs->fs_frag) {
   1406 		size = fs->fs_bsize;
   1407 		if (i + fs->fs_frag > blks)
   1408 			size = (blks - i) * fs->fs_fsize;
   1409 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1410 		    size, 0, 0);
   1411 #ifdef FFS_EI
   1412 		if (mp->um_flags & UFS_NEEDSWAP)
   1413 			ffs_csum_swap((struct csum*)space,
   1414 			    (struct csum*)bp->b_data, size);
   1415 		else
   1416 #endif
   1417 			memcpy(bp->b_data, space, (u_int)size);
   1418 		space = (char *)space + size;
   1419 		if (waitfor == MNT_WAIT)
   1420 			error = bwrite(bp);
   1421 		else
   1422 			bawrite(bp);
   1423 	}
   1424 	if (!allerror && error)
   1425 		allerror = error;
   1426 	return (allerror);
   1427 }
   1428