Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.93
      1 /*	$NetBSD: ffs_vfsops.c,v 1.93 2002/03/08 20:48:46 thorpej 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.93 2002/03/08 20:48:46 thorpej 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 		fs->fs_pendingblocks = 0;
    488 		fs->fs_pendinginodes = 0;
    489 	}
    490 
    491 	ffs_statfs(mountp, &mountp->mnt_stat, p);
    492 	/*
    493 	 * Step 3: re-read summary information from disk.
    494 	 */
    495 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    496 	space = fs->fs_csp;
    497 	for (i = 0; i < blks; i += fs->fs_frag) {
    498 		size = fs->fs_bsize;
    499 		if (i + fs->fs_frag > blks)
    500 			size = (blks - i) * fs->fs_fsize;
    501 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    502 			      NOCRED, &bp);
    503 		if (error) {
    504 			brelse(bp);
    505 			return (error);
    506 		}
    507 #ifdef FFS_EI
    508 		if (UFS_FSNEEDSWAP(fs))
    509 			ffs_csum_swap((struct csum *)bp->b_data,
    510 			    (struct csum *)space, size);
    511 		else
    512 #endif
    513 			memcpy(space, bp->b_data, (size_t)size);
    514 		space = (char *)space + size;
    515 		brelse(bp);
    516 	}
    517 	if ((fs->fs_flags & FS_DOSOFTDEP))
    518 		softdep_mount(devvp, mountp, fs, cred);
    519 	/*
    520 	 * We no longer know anything about clusters per cylinder group.
    521 	 */
    522 	if (fs->fs_contigsumsize > 0) {
    523 		lp = fs->fs_maxcluster;
    524 		for (i = 0; i < fs->fs_ncg; i++)
    525 			*lp++ = fs->fs_contigsumsize;
    526 	}
    527 
    528 loop:
    529 	simple_lock(&mntvnode_slock);
    530 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    531 		if (vp->v_mount != mountp) {
    532 			simple_unlock(&mntvnode_slock);
    533 			goto loop;
    534 		}
    535 		nvp = vp->v_mntvnodes.le_next;
    536 		/*
    537 		 * Step 4: invalidate all inactive vnodes.
    538 		 */
    539 		if (vrecycle(vp, &mntvnode_slock, p))
    540 			goto loop;
    541 		/*
    542 		 * Step 5: invalidate all cached file data.
    543 		 */
    544 		simple_lock(&vp->v_interlock);
    545 		simple_unlock(&mntvnode_slock);
    546 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    547 			goto loop;
    548 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    549 			panic("ffs_reload: dirty2");
    550 		/*
    551 		 * Step 6: re-read inode data for all active vnodes.
    552 		 */
    553 		ip = VTOI(vp);
    554 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    555 			      (int)fs->fs_bsize, NOCRED, &bp);
    556 		if (error) {
    557 			brelse(bp);
    558 			vput(vp);
    559 			return (error);
    560 		}
    561 		cp = (caddr_t)bp->b_data +
    562 		    (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
    563 #ifdef FFS_EI
    564 		if (UFS_FSNEEDSWAP(fs))
    565 			ffs_dinode_swap((struct dinode *)cp,
    566 			    &ip->i_din.ffs_din);
    567 		else
    568 #endif
    569 			memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
    570 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
    571 		brelse(bp);
    572 		vput(vp);
    573 		simple_lock(&mntvnode_slock);
    574 	}
    575 	simple_unlock(&mntvnode_slock);
    576 	return (0);
    577 }
    578 
    579 /*
    580  * Common code for mount and mountroot
    581  */
    582 int
    583 ffs_mountfs(devvp, mp, p)
    584 	struct vnode *devvp;
    585 	struct mount *mp;
    586 	struct proc *p;
    587 {
    588 	struct ufsmount *ump;
    589 	struct buf *bp;
    590 	struct fs *fs;
    591 	dev_t dev;
    592 	struct partinfo dpart;
    593 	void *space;
    594 	int blks;
    595 	int error, i, size, ronly;
    596 #ifdef FFS_EI
    597 	int needswap;
    598 #endif
    599 	int32_t *lp;
    600 	struct ucred *cred;
    601 	u_int64_t maxfilesize;					/* XXX */
    602 	u_int32_t sbsize;
    603 
    604 	dev = devvp->v_rdev;
    605 	cred = p ? p->p_ucred : NOCRED;
    606 	/*
    607 	 * Disallow multiple mounts of the same device.
    608 	 * Disallow mounting of a device that is currently in use
    609 	 * (except for root, which might share swap device for miniroot).
    610 	 * Flush out any old buffers remaining from a previous use.
    611 	 */
    612 	if ((error = vfs_mountedon(devvp)) != 0)
    613 		return (error);
    614 	if (vcount(devvp) > 1 && devvp != rootvp)
    615 		return (EBUSY);
    616 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    617 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
    618 	VOP_UNLOCK(devvp, 0);
    619 	if (error)
    620 		return (error);
    621 
    622 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    623 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    624 	if (error)
    625 		return (error);
    626 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
    627 		size = DEV_BSIZE;
    628 	else
    629 		size = dpart.disklab->d_secsize;
    630 
    631 	bp = NULL;
    632 	ump = NULL;
    633 	error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, cred, &bp);
    634 	if (error)
    635 		goto out;
    636 
    637 	fs = (struct fs*)bp->b_data;
    638 	if (fs->fs_magic == FS_MAGIC) {
    639 		sbsize = fs->fs_sbsize;
    640 #ifdef FFS_EI
    641 		needswap = 0;
    642 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
    643 		sbsize = bswap32(fs->fs_sbsize);
    644 		needswap = 1;
    645 #endif
    646 	} else {
    647 		error = EINVAL;
    648 		goto out;
    649 	}
    650 	if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs)) {
    651 		error = EINVAL;
    652 		goto out;
    653 	}
    654 
    655 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    656 	memcpy(fs, bp->b_data, sbsize);
    657 #ifdef FFS_EI
    658 	if (needswap) {
    659 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    660 		fs->fs_flags |= FS_SWAPPED;
    661 	}
    662 #endif
    663 	ffs_oldfscompat(fs);
    664 
    665 	if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
    666 		error = EINVAL;
    667 		goto out;
    668 	}
    669 	 /* make sure cylinder group summary area is a reasonable size. */
    670 	if (fs->fs_cgsize == 0 || fs->fs_cpg == 0 ||
    671 	    fs->fs_ncg > fs->fs_ncyl / fs->fs_cpg + 1 ||
    672 	    fs->fs_cssize >
    673 	    fragroundup(fs, fs->fs_ncg * sizeof(struct csum))) {
    674 		error = EINVAL;		/* XXX needs translation */
    675 		goto out2;
    676 	}
    677 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    678 		fs->fs_pendingblocks = 0;
    679 		fs->fs_pendinginodes = 0;
    680 	}
    681 	/* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
    682 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
    683 		error = EROFS;		/* XXX what should be returned? */
    684 		goto out2;
    685 	}
    686 
    687 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    688 	memset((caddr_t)ump, 0, sizeof *ump);
    689 	ump->um_fs = fs;
    690 	if (fs->fs_sbsize < SBSIZE)
    691 		bp->b_flags |= B_INVAL;
    692 	brelse(bp);
    693 	bp = NULL;
    694 	fs->fs_ronly = ronly;
    695 	if (ronly == 0) {
    696 		fs->fs_clean <<= 1;
    697 		fs->fs_fmod = 1;
    698 	}
    699 	size = fs->fs_cssize;
    700 	blks = howmany(size, fs->fs_fsize);
    701 	if (fs->fs_contigsumsize > 0)
    702 		size += fs->fs_ncg * sizeof(int32_t);
    703 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    704 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    705 	fs->fs_csp = space;
    706 	for (i = 0; i < blks; i += fs->fs_frag) {
    707 		size = fs->fs_bsize;
    708 		if (i + fs->fs_frag > blks)
    709 			size = (blks - i) * fs->fs_fsize;
    710 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    711 			      cred, &bp);
    712 		if (error) {
    713 			free(fs->fs_csp, M_UFSMNT);
    714 			goto out2;
    715 		}
    716 #ifdef FFS_EI
    717 		if (needswap)
    718 			ffs_csum_swap((struct csum *)bp->b_data,
    719 				(struct csum *)space, size);
    720 		else
    721 #endif
    722 			memcpy(space, bp->b_data, (u_int)size);
    723 
    724 		space = (char *)space + size;
    725 		brelse(bp);
    726 		bp = NULL;
    727 	}
    728 	if (fs->fs_contigsumsize > 0) {
    729 		fs->fs_maxcluster = lp = space;
    730 		for (i = 0; i < fs->fs_ncg; i++)
    731 			*lp++ = fs->fs_contigsumsize;
    732 		space = lp;
    733 	}
    734 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    735 	fs->fs_contigdirs = space;
    736 	space = (char *)space + size;
    737 	memset(fs->fs_contigdirs, 0, size);
    738 		/* Compatibility for old filesystems - XXX */
    739 	if (fs->fs_avgfilesize <= 0)
    740 		fs->fs_avgfilesize = AVFILESIZ;
    741 	if (fs->fs_avgfpdir <= 0)
    742 		fs->fs_avgfpdir = AFPDIR;
    743 	mp->mnt_data = (qaddr_t)ump;
    744 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    745 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
    746 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    747 	mp->mnt_fs_bshift = fs->fs_bshift;
    748 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    749 	mp->mnt_flag |= MNT_LOCAL;
    750 #ifdef FFS_EI
    751 	if (needswap)
    752 		ump->um_flags |= UFS_NEEDSWAP;
    753 #endif
    754 	ump->um_mountp = mp;
    755 	ump->um_dev = dev;
    756 	ump->um_devvp = devvp;
    757 	ump->um_nindir = fs->fs_nindir;
    758 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
    759 	ump->um_bptrtodb = fs->fs_fsbtodb;
    760 	ump->um_seqinc = fs->fs_frag;
    761 	for (i = 0; i < MAXQUOTAS; i++)
    762 		ump->um_quotas[i] = NULLVP;
    763 	devvp->v_specmountpoint = mp;
    764 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
    765 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;	/* XXX */
    766 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
    767 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
    768 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
    769 		error = softdep_mount(devvp, mp, fs, cred);
    770 		if (error) {
    771 			free(fs->fs_csp, M_UFSMNT);
    772 			goto out;
    773 		}
    774 	}
    775 	return (0);
    776 out2:
    777 	free(fs, M_UFSMNT);
    778 out:
    779 	devvp->v_specmountpoint = NULL;
    780 	if (bp)
    781 		brelse(bp);
    782 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    783 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    784 	VOP_UNLOCK(devvp, 0);
    785 	if (ump) {
    786 		free(ump, M_UFSMNT);
    787 		mp->mnt_data = (qaddr_t)0;
    788 	}
    789 	return (error);
    790 }
    791 
    792 /*
    793  * Sanity checks for old file systems.
    794  *
    795  * XXX - goes away some day.
    796  */
    797 int
    798 ffs_oldfscompat(fs)
    799 	struct fs *fs;
    800 {
    801 	int i;
    802 
    803 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    804 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    805 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    806 		fs->fs_nrpos = 8;				/* XXX */
    807 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    808 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
    809 								/* XXX */
    810 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    811 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    812 			sizepb *= NINDIR(fs);			/* XXX */
    813 			fs->fs_maxfilesize += sizepb;		/* XXX */
    814 		}						/* XXX */
    815 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    816 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    817 	}							/* XXX */
    818 	return (0);
    819 }
    820 
    821 /*
    822  * unmount system call
    823  */
    824 int
    825 ffs_unmount(mp, mntflags, p)
    826 	struct mount *mp;
    827 	int mntflags;
    828 	struct proc *p;
    829 {
    830 	struct ufsmount *ump;
    831 	struct fs *fs;
    832 	int error, flags, penderr;
    833 
    834 	penderr = 0;
    835 	flags = 0;
    836 	if (mntflags & MNT_FORCE)
    837 		flags |= FORCECLOSE;
    838 	if (mp->mnt_flag & MNT_SOFTDEP) {
    839 		if ((error = softdep_flushfiles(mp, flags, p)) != 0)
    840 			return (error);
    841 	} else {
    842 		if ((error = ffs_flushfiles(mp, flags, p)) != 0)
    843 			return (error);
    844 	}
    845 	ump = VFSTOUFS(mp);
    846 	fs = ump->um_fs;
    847 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    848 		printf("%s: unmount pending error: blocks %d files %d\n",
    849 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
    850 		fs->fs_pendingblocks = 0;
    851 		fs->fs_pendinginodes = 0;
    852 		penderr = 1;
    853 	}
    854 	if (fs->fs_ronly == 0 &&
    855 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    856 	    fs->fs_clean & FS_WASCLEAN) {
    857 		/*
    858 		 * XXXX don't mark fs clean in the case of softdep
    859 		 * pending block errors, until they are fixed.
    860 		 */
    861 		if (penderr == 0) {
    862 			if (mp->mnt_flag & MNT_SOFTDEP)
    863 				fs->fs_flags &= ~FS_DOSOFTDEP;
    864 			fs->fs_clean = FS_ISCLEAN;
    865 		}
    866 		(void) ffs_sbupdate(ump, MNT_WAIT);
    867 	}
    868 	if (ump->um_devvp->v_type != VBAD)
    869 		ump->um_devvp->v_specmountpoint = NULL;
    870 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    871 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
    872 		NOCRED, p);
    873 	vput(ump->um_devvp);
    874 	free(fs->fs_csp, M_UFSMNT);
    875 	free(fs, M_UFSMNT);
    876 	free(ump, M_UFSMNT);
    877 	mp->mnt_data = (qaddr_t)0;
    878 	mp->mnt_flag &= ~MNT_LOCAL;
    879 	return (error);
    880 }
    881 
    882 /*
    883  * Flush out all the files in a filesystem.
    884  */
    885 int
    886 ffs_flushfiles(mp, flags, p)
    887 	struct mount *mp;
    888 	int flags;
    889 	struct proc *p;
    890 {
    891 	extern int doforce;
    892 	struct ufsmount *ump;
    893 	int error;
    894 
    895 	if (!doforce)
    896 		flags &= ~FORCECLOSE;
    897 	ump = VFSTOUFS(mp);
    898 #ifdef QUOTA
    899 	if (mp->mnt_flag & MNT_QUOTA) {
    900 		int i;
    901 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
    902 			return (error);
    903 		for (i = 0; i < MAXQUOTAS; i++) {
    904 			if (ump->um_quotas[i] == NULLVP)
    905 				continue;
    906 			quotaoff(p, mp, i);
    907 		}
    908 		/*
    909 		 * Here we fall through to vflush again to ensure
    910 		 * that we have gotten rid of all the system vnodes.
    911 		 */
    912 	}
    913 #endif
    914 	/*
    915 	 * Flush all the files.
    916 	 */
    917 	error = vflush(mp, NULLVP, flags);
    918 	if (error)
    919 		return (error);
    920 	/*
    921 	 * Flush filesystem metadata.
    922 	 */
    923 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    924 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
    925 	VOP_UNLOCK(ump->um_devvp, 0);
    926 	return (error);
    927 }
    928 
    929 /*
    930  * Get file system statistics.
    931  */
    932 int
    933 ffs_statfs(mp, sbp, p)
    934 	struct mount *mp;
    935 	struct statfs *sbp;
    936 	struct proc *p;
    937 {
    938 	struct ufsmount *ump;
    939 	struct fs *fs;
    940 
    941 	ump = VFSTOUFS(mp);
    942 	fs = ump->um_fs;
    943 	if (fs->fs_magic != FS_MAGIC)
    944 		panic("ffs_statfs");
    945 #ifdef COMPAT_09
    946 	sbp->f_type = 1;
    947 #else
    948 	sbp->f_type = 0;
    949 #endif
    950 	sbp->f_bsize = fs->fs_fsize;
    951 	sbp->f_iosize = fs->fs_bsize;
    952 	sbp->f_blocks = fs->fs_dsize;
    953 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
    954 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
    955 	sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
    956 	    (100 - fs->fs_minfree) / (u_int64_t) 100) -
    957 	    (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
    958 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
    959 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
    960 	if (sbp != &mp->mnt_stat) {
    961 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
    962 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
    963 	}
    964 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
    965 	return (0);
    966 }
    967 
    968 /*
    969  * Go through the disk queues to initiate sandbagged IO;
    970  * go through the inodes to write those that have been modified;
    971  * initiate the writing of the super block if it has been modified.
    972  *
    973  * Note: we are always called with the filesystem marked `MPBUSY'.
    974  */
    975 int
    976 ffs_sync(mp, waitfor, cred, p)
    977 	struct mount *mp;
    978 	int waitfor;
    979 	struct ucred *cred;
    980 	struct proc *p;
    981 {
    982 	struct vnode *vp, *nvp;
    983 	struct inode *ip;
    984 	struct ufsmount *ump = VFSTOUFS(mp);
    985 	struct fs *fs;
    986 	int error, allerror = 0;
    987 
    988 	fs = ump->um_fs;
    989 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
    990 		printf("fs = %s\n", fs->fs_fsmnt);
    991 		panic("update: rofs mod");
    992 	}
    993 	/*
    994 	 * Write back each (modified) inode.
    995 	 */
    996 	simple_lock(&mntvnode_slock);
    997 loop:
    998 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
    999 		/*
   1000 		 * If the vnode that we are about to sync is no longer
   1001 		 * associated with this mount point, start over.
   1002 		 */
   1003 		if (vp->v_mount != mp)
   1004 			goto loop;
   1005 		simple_lock(&vp->v_interlock);
   1006 		nvp = LIST_NEXT(vp, v_mntvnodes);
   1007 		ip = VTOI(vp);
   1008 		if (vp->v_type == VNON ||
   1009 		    ((ip->i_flag &
   1010 		      (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1011 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1012 		     vp->v_uobj.uo_npages == 0))
   1013 		{
   1014 			simple_unlock(&vp->v_interlock);
   1015 			continue;
   1016 		}
   1017 		simple_unlock(&mntvnode_slock);
   1018 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1019 		if (error) {
   1020 			simple_lock(&mntvnode_slock);
   1021 			if (error == ENOENT)
   1022 				goto loop;
   1023 			continue;
   1024 		}
   1025 		if ((error = VOP_FSYNC(vp, cred,
   1026 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1027 			allerror = error;
   1028 		vput(vp);
   1029 		simple_lock(&mntvnode_slock);
   1030 	}
   1031 	simple_unlock(&mntvnode_slock);
   1032 	/*
   1033 	 * Force stale file system control information to be flushed.
   1034 	 */
   1035 	if (waitfor != MNT_LAZY) {
   1036 		if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
   1037 			waitfor = MNT_NOWAIT;
   1038 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1039 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1040 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1041 			allerror = error;
   1042 		VOP_UNLOCK(ump->um_devvp, 0);
   1043 	}
   1044 #ifdef QUOTA
   1045 	qsync(mp);
   1046 #endif
   1047 	/*
   1048 	 * Write back modified superblock.
   1049 	 */
   1050 	if (fs->fs_fmod != 0) {
   1051 		fs->fs_fmod = 0;
   1052 		fs->fs_time = time.tv_sec;
   1053 		if ((error = ffs_cgupdate(ump, waitfor)))
   1054 			allerror = error;
   1055 	}
   1056 	return (allerror);
   1057 }
   1058 
   1059 /*
   1060  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1061  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1062  * return the inode locked.  Detection and handling of mount points must be
   1063  * done by the calling routine.
   1064  */
   1065 int
   1066 ffs_vget(mp, ino, vpp)
   1067 	struct mount *mp;
   1068 	ino_t ino;
   1069 	struct vnode **vpp;
   1070 {
   1071 	struct fs *fs;
   1072 	struct inode *ip;
   1073 	struct ufsmount *ump;
   1074 	struct buf *bp;
   1075 	struct vnode *vp;
   1076 	dev_t dev;
   1077 	int error;
   1078 	caddr_t cp;
   1079 
   1080 	ump = VFSTOUFS(mp);
   1081 	dev = ump->um_dev;
   1082 
   1083 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1084 		return (0);
   1085 
   1086 	/* Allocate a new vnode/inode. */
   1087 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1088 		*vpp = NULL;
   1089 		return (error);
   1090 	}
   1091 
   1092 	/*
   1093 	 * If someone beat us to it while sleeping in getnewvnode(),
   1094 	 * push back the freshly allocated vnode we don't need, and return.
   1095 	 */
   1096 
   1097 	do {
   1098 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1099 			ungetnewvnode(vp);
   1100 			return (0);
   1101 		}
   1102 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1103 
   1104 	/*
   1105 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1106 	 * XXX create another pool for MFS inodes?
   1107 	 */
   1108 
   1109 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1110 	memset(ip, 0, sizeof(struct inode));
   1111 	vp->v_data = ip;
   1112 	ip->i_vnode = vp;
   1113 	ip->i_fs = fs = ump->um_fs;
   1114 	ip->i_dev = dev;
   1115 	ip->i_number = ino;
   1116 	LIST_INIT(&ip->i_pcbufhd);
   1117 #ifdef QUOTA
   1118 	{
   1119 		int i;
   1120 
   1121 		for (i = 0; i < MAXQUOTAS; i++)
   1122 			ip->i_dquot[i] = NODQUOT;
   1123 	}
   1124 #endif
   1125 
   1126 	/*
   1127 	 * Put it onto its hash chain and lock it so that other requests for
   1128 	 * this inode will block if they arrive while we are sleeping waiting
   1129 	 * for old data structures to be purged or for the contents of the
   1130 	 * disk portion of this inode to be read.
   1131 	 */
   1132 
   1133 	ufs_ihashins(ip);
   1134 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1135 
   1136 	/* Read in the disk contents for the inode, copy into the inode. */
   1137 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1138 		      (int)fs->fs_bsize, NOCRED, &bp);
   1139 	if (error) {
   1140 
   1141 		/*
   1142 		 * The inode does not contain anything useful, so it would
   1143 		 * be misleading to leave it on its hash chain. With mode
   1144 		 * still zero, it will be unlinked and returned to the free
   1145 		 * list by vput().
   1146 		 */
   1147 
   1148 		vput(vp);
   1149 		brelse(bp);
   1150 		*vpp = NULL;
   1151 		return (error);
   1152 	}
   1153 	cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
   1154 #ifdef FFS_EI
   1155 	if (UFS_FSNEEDSWAP(fs))
   1156 		ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
   1157 	else
   1158 #endif
   1159 		memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
   1160 	if (DOINGSOFTDEP(vp))
   1161 		softdep_load_inodeblock(ip);
   1162 	else
   1163 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1164 	brelse(bp);
   1165 
   1166 	/*
   1167 	 * Initialize the vnode from the inode, check for aliases.
   1168 	 * Note that the underlying vnode may have changed.
   1169 	 */
   1170 
   1171 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1172 
   1173 	/*
   1174 	 * Finish inode initialization now that aliasing has been resolved.
   1175 	 */
   1176 
   1177 	genfs_node_init(vp, &ffs_genfsops);
   1178 	ip->i_devvp = ump->um_devvp;
   1179 	VREF(ip->i_devvp);
   1180 
   1181 	/*
   1182 	 * Ensure that uid and gid are correct. This is a temporary
   1183 	 * fix until fsck has been changed to do the update.
   1184 	 */
   1185 
   1186 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1187 		ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid;	/* XXX */
   1188 		ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid;	/* XXX */
   1189 	}							/* XXX */
   1190 	uvm_vnp_setsize(vp, ip->i_ffs_size);
   1191 	*vpp = vp;
   1192 	return (0);
   1193 }
   1194 
   1195 /*
   1196  * File handle to vnode
   1197  *
   1198  * Have to be really careful about stale file handles:
   1199  * - check that the inode number is valid
   1200  * - call ffs_vget() to get the locked inode
   1201  * - check for an unallocated inode (i_mode == 0)
   1202  * - check that the given client host has export rights and return
   1203  *   those rights via. exflagsp and credanonp
   1204  */
   1205 int
   1206 ffs_fhtovp(mp, fhp, vpp)
   1207 	struct mount *mp;
   1208 	struct fid *fhp;
   1209 	struct vnode **vpp;
   1210 {
   1211 	struct ufid *ufhp;
   1212 	struct fs *fs;
   1213 
   1214 	ufhp = (struct ufid *)fhp;
   1215 	fs = VFSTOUFS(mp)->um_fs;
   1216 	if (ufhp->ufid_ino < ROOTINO ||
   1217 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1218 		return (ESTALE);
   1219 	return (ufs_fhtovp(mp, ufhp, vpp));
   1220 }
   1221 
   1222 /*
   1223  * Vnode pointer to File handle
   1224  */
   1225 /* ARGSUSED */
   1226 int
   1227 ffs_vptofh(vp, fhp)
   1228 	struct vnode *vp;
   1229 	struct fid *fhp;
   1230 {
   1231 	struct inode *ip;
   1232 	struct ufid *ufhp;
   1233 
   1234 	ip = VTOI(vp);
   1235 	ufhp = (struct ufid *)fhp;
   1236 	ufhp->ufid_len = sizeof(struct ufid);
   1237 	ufhp->ufid_ino = ip->i_number;
   1238 	ufhp->ufid_gen = ip->i_ffs_gen;
   1239 	return (0);
   1240 }
   1241 
   1242 void
   1243 ffs_init()
   1244 {
   1245 	if (ffs_initcount++ > 0)
   1246 		return;
   1247 
   1248 	softdep_initialize();
   1249 	ufs_init();
   1250 
   1251 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
   1252 	    &pool_allocator_nointr);
   1253 }
   1254 
   1255 void
   1256 ffs_reinit()
   1257 {
   1258 	softdep_reinitialize();
   1259 	ufs_reinit();
   1260 }
   1261 
   1262 void
   1263 ffs_done()
   1264 {
   1265 	if (--ffs_initcount > 0)
   1266 		return;
   1267 
   1268 	/* XXX softdep cleanup ? */
   1269 	ufs_done();
   1270 	pool_destroy(&ffs_inode_pool);
   1271 }
   1272 
   1273 int
   1274 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1275 	int *name;
   1276 	u_int namelen;
   1277 	void *oldp;
   1278 	size_t *oldlenp;
   1279 	void *newp;
   1280 	size_t newlen;
   1281 	struct proc *p;
   1282 {
   1283 	extern int doasyncfree;
   1284 	extern int ffs_log_changeopt;
   1285 
   1286 	/* all sysctl names at this level are terminal */
   1287 	if (namelen != 1)
   1288 		return (ENOTDIR);		/* overloaded */
   1289 
   1290 	switch (name[0]) {
   1291 	case FFS_ASYNCFREE:
   1292 		return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
   1293 	case FFS_LOG_CHANGEOPT:
   1294 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1295 			&ffs_log_changeopt));
   1296 	default:
   1297 		return (EOPNOTSUPP);
   1298 	}
   1299 	/* NOTREACHED */
   1300 }
   1301 
   1302 /*
   1303  * Write a superblock and associated information back to disk.
   1304  */
   1305 int
   1306 ffs_sbupdate(mp, waitfor)
   1307 	struct ufsmount *mp;
   1308 	int waitfor;
   1309 {
   1310 	struct fs *fs = mp->um_fs;
   1311 	struct buf *bp;
   1312 	int i, error = 0;
   1313 	int32_t saved_nrpos = fs->fs_nrpos;
   1314 	int64_t saved_qbmask = fs->fs_qbmask;
   1315 	int64_t saved_qfmask = fs->fs_qfmask;
   1316 	u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
   1317 	u_int8_t saveflag;
   1318 
   1319 	/* Restore compatibility to old file systems.		   XXX */
   1320 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
   1321 		fs->fs_nrpos = -1;		/* XXX */
   1322 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1323 		int32_t *lp, tmp;				/* XXX */
   1324 								/* XXX */
   1325 		lp = (int32_t *)&fs->fs_qbmask;	/* XXX nuke qfmask too */
   1326 		tmp = lp[4];					/* XXX */
   1327 		for (i = 4; i > 0; i--)				/* XXX */
   1328 			lp[i] = lp[i-1];			/* XXX */
   1329 		lp[0] = tmp;					/* XXX */
   1330 	}							/* XXX */
   1331 	fs->fs_maxfilesize = mp->um_savedmaxfilesize;	/* XXX */
   1332 
   1333 	bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
   1334 	    (int)fs->fs_sbsize, 0, 0);
   1335 	saveflag = fs->fs_flags & FS_INTERNAL;
   1336 	fs->fs_flags &= ~FS_INTERNAL;
   1337 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1338 #ifdef FFS_EI
   1339 	if (mp->um_flags & UFS_NEEDSWAP)
   1340 		ffs_sb_swap(fs, (struct fs*)bp->b_data);
   1341 #endif
   1342 
   1343 	fs->fs_flags |= saveflag;
   1344 	fs->fs_nrpos = saved_nrpos; /* XXX */
   1345 	fs->fs_qbmask = saved_qbmask; /* XXX */
   1346 	fs->fs_qfmask = saved_qfmask; /* XXX */
   1347 	fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
   1348 
   1349 	if (waitfor == MNT_WAIT)
   1350 		error = bwrite(bp);
   1351 	else
   1352 		bawrite(bp);
   1353 	return (error);
   1354 }
   1355 
   1356 int
   1357 ffs_cgupdate(mp, waitfor)
   1358 	struct ufsmount *mp;
   1359 	int waitfor;
   1360 {
   1361 	struct fs *fs = mp->um_fs;
   1362 	struct buf *bp;
   1363 	int blks;
   1364 	void *space;
   1365 	int i, size, error = 0, allerror = 0;
   1366 
   1367 	allerror = ffs_sbupdate(mp, waitfor);
   1368 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1369 	space = fs->fs_csp;
   1370 	for (i = 0; i < blks; i += fs->fs_frag) {
   1371 		size = fs->fs_bsize;
   1372 		if (i + fs->fs_frag > blks)
   1373 			size = (blks - i) * fs->fs_fsize;
   1374 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1375 		    size, 0, 0);
   1376 #ifdef FFS_EI
   1377 		if (mp->um_flags & UFS_NEEDSWAP)
   1378 			ffs_csum_swap((struct csum*)space,
   1379 			    (struct csum*)bp->b_data, size);
   1380 		else
   1381 #endif
   1382 			memcpy(bp->b_data, space, (u_int)size);
   1383 		space = (char *)space + size;
   1384 		if (waitfor == MNT_WAIT)
   1385 			error = bwrite(bp);
   1386 		else
   1387 			bawrite(bp);
   1388 	}
   1389 	if (!allerror && error)
   1390 		allerror = error;
   1391 	return (allerror);
   1392 }
   1393