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