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