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