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