Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.113
      1 /*	$NetBSD: ffs_vfsops.c,v 1.113 2003/04/16 21:44:27 christos 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.113 2003/04/16 21:44:27 christos 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 #include <sys/conf.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 const struct vnodeopv_desc ffs_vnodeop_opv_desc;
     87 extern const struct vnodeopv_desc ffs_specop_opv_desc;
     88 extern const 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 	ufs_gop_alloc,
    121 	genfs_gop_write,
    122 };
    123 
    124 struct pool ffs_inode_pool;
    125 struct pool ffs_dinode1_pool;
    126 struct pool ffs_dinode2_pool;
    127 
    128 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
    129 				   daddr_t);
    130 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
    131 
    132 /*
    133  * Called by main() when ffs is going to be mounted as root.
    134  */
    135 
    136 int
    137 ffs_mountroot()
    138 {
    139 	struct fs *fs;
    140 	struct mount *mp;
    141 	struct proc *p = curproc;	/* XXX */
    142 	struct ufsmount *ump;
    143 	int error;
    144 
    145 	if (root_device->dv_class != DV_DISK)
    146 		return (ENODEV);
    147 
    148 	/*
    149 	 * Get vnodes for rootdev.
    150 	 */
    151 	if (bdevvp(rootdev, &rootvp))
    152 		panic("ffs_mountroot: can't setup bdevvp's");
    153 
    154 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
    155 		vrele(rootvp);
    156 		return (error);
    157 	}
    158 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
    159 		mp->mnt_op->vfs_refcount--;
    160 		vfs_unbusy(mp);
    161 		free(mp, M_MOUNT);
    162 		vrele(rootvp);
    163 		return (error);
    164 	}
    165 	simple_lock(&mountlist_slock);
    166 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    167 	simple_unlock(&mountlist_slock);
    168 	ump = VFSTOUFS(mp);
    169 	fs = ump->um_fs;
    170 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    171 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    172 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
    173 	vfs_unbusy(mp);
    174 	inittodr(fs->fs_time);
    175 	return (0);
    176 }
    177 
    178 /*
    179  * VFS Operations.
    180  *
    181  * mount system call
    182  */
    183 int
    184 ffs_mount(mp, path, data, ndp, p)
    185 	struct mount *mp;
    186 	const char *path;
    187 	void *data;
    188 	struct nameidata *ndp;
    189 	struct proc *p;
    190 {
    191 	struct vnode *devvp = NULL;
    192 	struct ufs_args args;
    193 	struct ufsmount *ump = NULL;
    194 	struct fs *fs;
    195 	int error, flags, update;
    196 	mode_t accessmode;
    197 
    198 	if (mp->mnt_flag & MNT_GETARGS) {
    199 		ump = VFSTOUFS(mp);
    200 		if (ump == NULL)
    201 			return EIO;
    202 		args.fspec = NULL;
    203 		vfs_showexport(mp, &args.export, &ump->um_export);
    204 		return copyout(&args, data, sizeof(args));
    205 	}
    206 	error = copyin(data, &args, sizeof (struct ufs_args));
    207 	if (error)
    208 		return (error);
    209 
    210 #if !defined(SOFTDEP)
    211 	mp->mnt_flag &= ~MNT_SOFTDEP;
    212 #endif
    213 
    214 	update = mp->mnt_flag & MNT_UPDATE;
    215 
    216 	/* Check arguments */
    217 	if (update) {
    218 		/* Use the extant mount */
    219 		ump = VFSTOUFS(mp);
    220 		devvp = ump->um_devvp;
    221 		if (args.fspec == NULL)
    222 			vref(devvp);
    223 	} else {
    224 		/* New mounts must have a filename for the device */
    225 		if (args.fspec == NULL)
    226 			return (EINVAL);
    227 	}
    228 
    229 	if (args.fspec != NULL) {
    230 		/*
    231 		 * Look up the name and verify that it's sane.
    232 		 */
    233 		NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    234 		if ((error = namei(ndp)) != 0)
    235 			return (error);
    236 		devvp = ndp->ni_vp;
    237 
    238 		if (!update) {
    239 			/*
    240 			 * Be sure this is a valid block device
    241 			 */
    242 			if (devvp->v_type != VBLK)
    243 				error = ENOTBLK;
    244 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    245 				error = ENXIO;
    246 		} else {
    247 			/*
    248 			 * Be sure we're still naming the same device
    249 			 * used for our initial mount
    250 			 */
    251 			if (devvp != ump->um_devvp)
    252 				error = EINVAL;
    253 		}
    254 	}
    255 
    256 	/*
    257 	 * If mount by non-root, then verify that user has necessary
    258 	 * permissions on the device.
    259 	 */
    260 	if (error == 0 && p->p_ucred->cr_uid != 0) {
    261 		accessmode = VREAD;
    262 		if (update ?
    263 		    (mp->mnt_flag & MNT_WANTRDWR) != 0 :
    264 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    265 			accessmode |= VWRITE;
    266 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    267 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    268 		VOP_UNLOCK(devvp, 0);
    269 	}
    270 
    271 	if (error) {
    272 		vrele(devvp);
    273 		return (error);
    274 	}
    275 
    276 	if (!update) {
    277 		error = ffs_mountfs(devvp, mp, p);
    278 		if (error) {
    279 			vrele(devvp);
    280 			return (error);
    281 		}
    282 
    283 		ump = VFSTOUFS(mp);
    284 		fs = ump->um_fs;
    285 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    286 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    287 			printf("%s fs uses soft updates, "
    288 			    "ignoring async mode\n",
    289 			    fs->fs_fsmnt);
    290 			mp->mnt_flag &= ~MNT_ASYNC;
    291 		}
    292 	} else {
    293 		/*
    294 		 * Update the mount.
    295 		 */
    296 
    297 		/*
    298 		 * The initial mount got a reference on this
    299 		 * device, so drop the one obtained via
    300 		 * namei(), above.
    301 		 */
    302 		vrele(devvp);
    303 
    304 		fs = ump->um_fs;
    305 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    306 			/*
    307 			 * Changing from r/w to r/o
    308 			 */
    309 			flags = WRITECLOSE;
    310 			if (mp->mnt_flag & MNT_FORCE)
    311 				flags |= FORCECLOSE;
    312 			if (mp->mnt_flag & MNT_SOFTDEP)
    313 				error = softdep_flushfiles(mp, flags, p);
    314 			else
    315 				error = ffs_flushfiles(mp, flags, p);
    316 			if (fs->fs_pendingblocks != 0 ||
    317 			    fs->fs_pendinginodes != 0) {
    318 				printf("%s: update error: blocks %" PRId64
    319 				       " files %d\n",
    320 				    fs->fs_fsmnt, fs->fs_pendingblocks,
    321 				    fs->fs_pendinginodes);
    322 				fs->fs_pendingblocks = 0;
    323 				fs->fs_pendinginodes = 0;
    324 			}
    325 			if (error == 0 &&
    326 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    327 			    fs->fs_clean & FS_WASCLEAN) {
    328 				if (mp->mnt_flag & MNT_SOFTDEP)
    329 					fs->fs_flags &= ~FS_DOSOFTDEP;
    330 				fs->fs_clean = FS_ISCLEAN;
    331 				(void) ffs_sbupdate(ump, MNT_WAIT);
    332 			}
    333 			if (error)
    334 				return (error);
    335 			fs->fs_ronly = 1;
    336 			fs->fs_fmod = 0;
    337 		}
    338 
    339 		/*
    340 		 * Flush soft dependencies if disabling it via an update
    341 		 * mount. This may leave some items to be processed,
    342 		 * so don't do this yet XXX.
    343 		 */
    344 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
    345 		    !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    346 #ifdef notyet
    347 			flags = WRITECLOSE;
    348 			if (mp->mnt_flag & MNT_FORCE)
    349 				flags |= FORCECLOSE;
    350 			error = softdep_flushfiles(mp, flags, p);
    351 			if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
    352 				fs->fs_flags &= ~FS_DOSOFTDEP;
    353 				(void) ffs_sbupdate(ump, MNT_WAIT);
    354 #elif defined(SOFTDEP)
    355 			mp->mnt_flag |= MNT_SOFTDEP;
    356 #endif
    357 		}
    358 
    359 		/*
    360 		 * When upgrading to a softdep mount, we must first flush
    361 		 * all vnodes. (not done yet -- see above)
    362 		 */
    363 		if (!(fs->fs_flags & FS_DOSOFTDEP) &&
    364 		    (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    365 #ifdef notyet
    366 			flags = WRITECLOSE;
    367 			if (mp->mnt_flag & MNT_FORCE)
    368 				flags |= FORCECLOSE;
    369 			error = ffs_flushfiles(mp, flags, p);
    370 #else
    371 			mp->mnt_flag &= ~MNT_SOFTDEP;
    372 #endif
    373 		}
    374 
    375 		if (mp->mnt_flag & MNT_RELOAD) {
    376 			error = ffs_reload(mp, p->p_ucred, p);
    377 			if (error)
    378 				return (error);
    379 		}
    380 
    381 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
    382 			/*
    383 			 * Changing from read-only to read/write
    384 			 */
    385 			fs->fs_ronly = 0;
    386 			fs->fs_clean <<= 1;
    387 			fs->fs_fmod = 1;
    388 			if ((fs->fs_flags & FS_DOSOFTDEP)) {
    389 				error = softdep_mount(devvp, mp, fs,
    390 				    p->p_ucred);
    391 				if (error)
    392 					return (error);
    393 			}
    394 		}
    395 		if (args.fspec == 0) {
    396 			/*
    397 			 * Process export requests.
    398 			 */
    399 			return (vfs_export(mp, &ump->um_export, &args.export));
    400 		}
    401 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    402 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    403 			printf("%s fs uses soft updates, ignoring async mode\n",
    404 			    fs->fs_fsmnt);
    405 			mp->mnt_flag &= ~MNT_ASYNC;
    406 		}
    407 	}
    408 
    409 	error = set_statfs_info(path, UIO_USERSPACE, args.fspec,
    410 	    UIO_USERSPACE, mp, p);
    411 	if (mp->mnt_flag & MNT_SOFTDEP)
    412 		fs->fs_flags |= FS_DOSOFTDEP;
    413 	else
    414 		fs->fs_flags &= ~FS_DOSOFTDEP;
    415 	if (fs->fs_fmod != 0) {	/* XXX */
    416 		fs->fs_fmod = 0;
    417 		if (fs->fs_clean & FS_WASCLEAN)
    418 			fs->fs_time = time.tv_sec;
    419 		else {
    420 			printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
    421 			    mp->mnt_stat.f_mntfromname, fs->fs_clean);
    422 			printf("%s: lost blocks %" PRId64 " files %d\n",
    423 			    mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
    424 			    fs->fs_pendinginodes);
    425 		}
    426 		(void) ffs_cgupdate(ump, MNT_WAIT);
    427 	}
    428 	return error;
    429 }
    430 
    431 /*
    432  * Reload all incore data for a filesystem (used after running fsck on
    433  * the root filesystem and finding things to fix). The filesystem must
    434  * be mounted read-only.
    435  *
    436  * Things to do to update the mount:
    437  *	1) invalidate all cached meta-data.
    438  *	2) re-read superblock from disk.
    439  *	3) re-read summary information from disk.
    440  *	4) invalidate all inactive vnodes.
    441  *	5) invalidate all cached file data.
    442  *	6) re-read inode data for all active vnodes.
    443  */
    444 int
    445 ffs_reload(mountp, cred, p)
    446 	struct mount *mountp;
    447 	struct ucred *cred;
    448 	struct proc *p;
    449 {
    450 	struct vnode *vp, *nvp, *devvp;
    451 	struct inode *ip;
    452 	void *space;
    453 	struct buf *bp;
    454 	struct fs *fs, *newfs;
    455 	struct partinfo dpart;
    456 	daddr_t sblockloc;
    457 	int i, blks, size, error;
    458 	int32_t *lp;
    459 	struct ufsmount *ump;
    460 
    461 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    462 		return (EINVAL);
    463 
    464 	ump = VFSTOUFS(mountp);
    465 	/*
    466 	 * Step 1: invalidate all cached meta-data.
    467 	 */
    468 	devvp = ump->um_devvp;
    469 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    470 	error = vinvalbuf(devvp, 0, cred, p, 0, 0);
    471 	VOP_UNLOCK(devvp, 0);
    472 	if (error)
    473 		panic("ffs_reload: dirty1");
    474 	/*
    475 	 * Step 2: re-read superblock from disk.
    476 	 */
    477 	fs = ump->um_fs;
    478 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p) != 0)
    479 		size = DEV_BSIZE;
    480 	else
    481 		size = dpart.disklab->d_secsize;
    482 	error = bread(devvp, fs->fs_sblockloc / size, fs->fs_sbsize,
    483 		      NOCRED, &bp);
    484 	if (error) {
    485 		brelse(bp);
    486 		return (error);
    487 	}
    488 	newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
    489 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
    490 #ifdef SUPPORT_42POSTBLFMT_WRITE
    491 	if (fs->fs_old_postblformat == FS_42POSTBLFMT)
    492 		memcpy(ump->um_opostsave, &newfs->fs_old_postbl_start, 256);
    493 #endif
    494 #ifdef FFS_EI
    495 	if (ump->um_flags & UFS_NEEDSWAP) {
    496 		ffs_sb_swap((struct fs*)bp->b_data, newfs);
    497 		fs->fs_flags |= FS_SWAPPED;
    498 	}
    499 #endif
    500 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
    501 	     newfs->fs_magic != FS_UFS2_MAGIC)||
    502 	     newfs->fs_bsize > MAXBSIZE ||
    503 	     newfs->fs_bsize < sizeof(struct fs)) {
    504 		brelse(bp);
    505 		free(newfs, M_UFSMNT);
    506 		return (EIO);		/* XXX needs translation */
    507 	}
    508 	/*
    509 	 * Copy pointer fields back into superblock before copying in	XXX
    510 	 * new superblock. These should really be in the ufsmount.	XXX
    511 	 * Note that important parameters (eg fs_ncg) are unchanged.
    512 	 */
    513 	newfs->fs_csp = fs->fs_csp;
    514 	newfs->fs_maxcluster = fs->fs_maxcluster;
    515 	newfs->fs_contigdirs = fs->fs_contigdirs;
    516 	newfs->fs_ronly = fs->fs_ronly;
    517 	newfs->fs_active = fs->fs_active;
    518 	sblockloc = fs->fs_sblockloc;
    519 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    520 	brelse(bp);
    521 	free(newfs, M_UFSMNT);
    522 
    523 	/* Recheck for apple UFS filesystem */
    524 	VFSTOUFS(mountp)->um_flags &= ~UFS_ISAPPLEUFS;
    525 	/* First check to see if this is tagged as an Apple UFS filesystem
    526 	 * in the disklabel
    527 	 */
    528 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
    529 		(dpart.part->p_fstype == FS_APPLEUFS)) {
    530 		VFSTOUFS(mountp)->um_flags |= UFS_ISAPPLEUFS;
    531 	}
    532 #ifdef APPLE_UFS
    533 	else {
    534 		/* Manually look for an apple ufs label, and if a valid one
    535 		 * is found, then treat it like an Apple UFS filesystem anyway
    536 		 */
    537 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
    538 			APPLEUFS_LABEL_SIZE, cred, &bp);
    539 		if (error) {
    540 			brelse(bp);
    541 			return (error);
    542 		}
    543 		error = ffs_appleufs_validate(fs->fs_fsmnt,
    544 			(struct appleufslabel *)bp->b_data,NULL);
    545 		if (error == 0) {
    546 			VFSTOUFS(mountp)->um_flags |= UFS_ISAPPLEUFS;
    547 		}
    548 		brelse(bp);
    549 		bp = NULL;
    550 	}
    551 #else
    552 	if (VFSTOUFS(mountp)->um_flags & UFS_ISAPPLEUFS)
    553 		return (EIO);
    554 #endif
    555 
    556 	mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    557 	if (UFS_MPISAPPLEUFS(mountp)) {
    558 		/* see comment about NeXT below */
    559 		mountp->mnt_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    560 	}
    561 	ffs_oldfscompat_read(fs, VFSTOUFS(mountp), fs->fs_sblockloc);
    562 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    563 		fs->fs_pendingblocks = 0;
    564 		fs->fs_pendinginodes = 0;
    565 	}
    566 
    567 	ffs_statfs(mountp, &mountp->mnt_stat, p);
    568 	/*
    569 	 * Step 3: re-read summary information from disk.
    570 	 */
    571 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    572 	space = fs->fs_csp;
    573 	for (i = 0; i < blks; i += fs->fs_frag) {
    574 		size = fs->fs_bsize;
    575 		if (i + fs->fs_frag > blks)
    576 			size = (blks - i) * fs->fs_fsize;
    577 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    578 			      NOCRED, &bp);
    579 		if (error) {
    580 			brelse(bp);
    581 			return (error);
    582 		}
    583 #ifdef FFS_EI
    584 		if (UFS_FSNEEDSWAP(fs))
    585 			ffs_csum_swap((struct csum *)bp->b_data,
    586 			    (struct csum *)space, size);
    587 		else
    588 #endif
    589 			memcpy(space, bp->b_data, (size_t)size);
    590 		space = (char *)space + size;
    591 		brelse(bp);
    592 	}
    593 	if ((fs->fs_flags & FS_DOSOFTDEP))
    594 		softdep_mount(devvp, mountp, fs, cred);
    595 	/*
    596 	 * We no longer know anything about clusters per cylinder group.
    597 	 */
    598 	if (fs->fs_contigsumsize > 0) {
    599 		lp = fs->fs_maxcluster;
    600 		for (i = 0; i < fs->fs_ncg; i++)
    601 			*lp++ = fs->fs_contigsumsize;
    602 	}
    603 
    604 loop:
    605 	simple_lock(&mntvnode_slock);
    606 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    607 		if (vp->v_mount != mountp) {
    608 			simple_unlock(&mntvnode_slock);
    609 			goto loop;
    610 		}
    611 		nvp = vp->v_mntvnodes.le_next;
    612 		/*
    613 		 * Step 4: invalidate all inactive vnodes.
    614 		 */
    615 		if (vrecycle(vp, &mntvnode_slock, p))
    616 			goto loop;
    617 		/*
    618 		 * Step 5: invalidate all cached file data.
    619 		 */
    620 		simple_lock(&vp->v_interlock);
    621 		simple_unlock(&mntvnode_slock);
    622 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    623 			goto loop;
    624 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    625 			panic("ffs_reload: dirty2");
    626 		/*
    627 		 * Step 6: re-read inode data for all active vnodes.
    628 		 */
    629 		ip = VTOI(vp);
    630 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    631 			      (int)fs->fs_bsize, NOCRED, &bp);
    632 		if (error) {
    633 			brelse(bp);
    634 			vput(vp);
    635 			return (error);
    636 		}
    637 		ffs_load_inode(bp, ip, fs, ip->i_number);
    638 		ip->i_ffs_effnlink = ip->i_nlink;
    639 		brelse(bp);
    640 		vput(vp);
    641 		simple_lock(&mntvnode_slock);
    642 	}
    643 	simple_unlock(&mntvnode_slock);
    644 	return (0);
    645 }
    646 
    647 /*
    648  * Possible superblock locations ordered from most to least likely.
    649  */
    650 static int sblock_try[] = SBLOCKSEARCH;
    651 
    652 /*
    653  * Common code for mount and mountroot
    654  */
    655 int
    656 ffs_mountfs(devvp, mp, p)
    657 	struct vnode *devvp;
    658 	struct mount *mp;
    659 	struct proc *p;
    660 {
    661 	struct ufsmount *ump;
    662 	struct buf *bp;
    663 	struct fs *fs;
    664 	dev_t dev;
    665 	struct partinfo dpart;
    666 	void *space;
    667 	daddr_t sblockloc, fsblockloc;
    668 	int blks, fstype;
    669 	int error, i, size, ronly;
    670 #ifdef FFS_EI
    671 	int needswap = 0;		/* keep gcc happy */
    672 #endif
    673 	int32_t *lp;
    674 	struct ucred *cred;
    675 	u_int32_t sbsize = 8192;	/* keep gcc happy*/
    676 
    677 	dev = devvp->v_rdev;
    678 	cred = p ? p->p_ucred : NOCRED;
    679 	/*
    680 	 * Disallow multiple mounts of the same device.
    681 	 * Disallow mounting of a device that is currently in use
    682 	 * (except for root, which might share swap device for miniroot).
    683 	 * Flush out any old buffers remaining from a previous use.
    684 	 */
    685 	if ((error = vfs_mountedon(devvp)) != 0)
    686 		return (error);
    687 	if (vcount(devvp) > 1 && devvp != rootvp)
    688 		return (EBUSY);
    689 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    690 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
    691 	VOP_UNLOCK(devvp, 0);
    692 	if (error)
    693 		return (error);
    694 
    695 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    696 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    697 	if (error)
    698 		return (error);
    699 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) != 0)
    700 		size = DEV_BSIZE;
    701 	else
    702 		size = dpart.disklab->d_secsize;
    703 
    704 	bp = NULL;
    705 	ump = NULL;
    706 	fs = NULL;
    707 	fsblockloc = sblockloc = 0;
    708 	fstype = 0;
    709 
    710 	/*
    711 	 * Try reading the superblock in each of its possible locations.		 */
    712 	for (i = 0; sblock_try[i] != -1; i++) {
    713 		error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
    714 			      &bp);
    715 		if (error)
    716 			goto out;
    717 		fs = (struct fs*)bp->b_data;
    718 		fsblockloc = sblockloc = sblock_try[i];
    719 		if (fs->fs_magic == FS_UFS1_MAGIC) {
    720 			sbsize = fs->fs_sbsize;
    721 			fstype = UFS1;
    722 #ifdef FFS_EI
    723 			needswap = 0;
    724 		} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
    725 			sbsize = bswap32(fs->fs_sbsize);
    726 			fstype = UFS1;
    727 			needswap = 1;
    728 #endif
    729 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
    730 			sbsize = fs->fs_sbsize;
    731 			fstype = UFS2;
    732 			fsblockloc = fs->fs_sblockloc;
    733 #ifdef FFS_EI
    734 			needswap = 0;
    735 		} else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
    736 			sbsize = bswap32(fs->fs_sbsize);
    737 			fstype = UFS2;
    738 			fsblockloc = bswap64(fs->fs_sblockloc);
    739 			needswap = 1;
    740 #endif
    741 		} else
    742 			goto next_sblock;
    743 
    744 		if ((fsblockloc == sblockloc ||
    745 		     (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)
    746 		    && sbsize <= MAXBSIZE && sbsize >= sizeof(struct fs))
    747 			break;
    748 
    749 next_sblock:
    750 		bp->b_flags |= B_NOCACHE;
    751 		brelse(bp);
    752 		bp = NULL;
    753 	}
    754 
    755 	if (sblock_try[i] == -1) {
    756 		error = EINVAL;
    757 		goto out;
    758 	}
    759 
    760 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    761 	memcpy(fs, bp->b_data, sbsize);
    762 
    763 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    764 	memset(ump, 0, sizeof *ump);
    765 	ump->um_fs = fs;
    766 
    767 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
    768 #ifdef SUPPORT_FS_42POSTBLFMT_WRITE
    769 		ump->um_opostsave = malloc(256, M_UFSMNT, M_WAITOK);
    770 		memcpy(ump->um_opostsave, &fs->fs_old_postbl_start, 256);
    771 #else
    772 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    773 			error = EROFS;
    774 			goto out2;
    775 		}
    776 #endif
    777 	}
    778 
    779 #ifdef FFS_EI
    780 	if (needswap) {
    781 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    782 		fs->fs_flags |= FS_SWAPPED;
    783 	}
    784 #endif
    785 
    786 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    787 		fs->fs_pendingblocks = 0;
    788 		fs->fs_pendinginodes = 0;
    789 	}
    790 
    791 	ump->um_fstype = fstype;
    792 	if (fs->fs_sbsize < SBLOCKSIZE)
    793 		bp->b_flags |= B_INVAL;
    794 	brelse(bp);
    795 	bp = NULL;
    796 
    797 	ffs_oldfscompat_read(fs, ump, sblockloc);
    798 
    799 	/* First check to see if this is tagged as an Apple UFS filesystem
    800 	 * in the disklabel
    801 	 */
    802 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
    803 		(dpart.part->p_fstype == FS_APPLEUFS)) {
    804 		ump->um_flags |= UFS_ISAPPLEUFS;
    805 	}
    806 #ifdef APPLE_UFS
    807 	else {
    808 		/* Manually look for an apple ufs label, and if a valid one
    809 		 * is found, then treat it like an Apple UFS filesystem anyway
    810 		 */
    811 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
    812 			APPLEUFS_LABEL_SIZE, cred, &bp);
    813 		if (error)
    814 			goto out;
    815 		error = ffs_appleufs_validate(fs->fs_fsmnt,
    816 			(struct appleufslabel *)bp->b_data,NULL);
    817 		if (error == 0) {
    818 			ump->um_flags |= UFS_ISAPPLEUFS;
    819 		}
    820 		brelse(bp);
    821 		bp = NULL;
    822 	}
    823 #else
    824 	if (ump->um_flags & UFS_ISAPPLEUFS) {
    825 		error = EINVAL;
    826 		goto out;
    827 	}
    828 #endif
    829 
    830 	/*
    831 	 * verify that we can access the last block in the fs
    832 	 * if we're mounting read/write.
    833 	 */
    834 
    835 	if (!ronly) {
    836 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
    837 		    cred, &bp);
    838 		if (bp->b_bcount != fs->fs_fsize)
    839 			error = EINVAL;
    840 		bp->b_flags |= B_INVAL;
    841 		if (error)
    842 			goto out;
    843 		brelse(bp);
    844 		bp = NULL;
    845 	}
    846 
    847 	fs->fs_ronly = ronly;
    848 	if (ronly == 0) {
    849 		fs->fs_clean <<= 1;
    850 		fs->fs_fmod = 1;
    851 	}
    852 	size = fs->fs_cssize;
    853 	blks = howmany(size, fs->fs_fsize);
    854 	if (fs->fs_contigsumsize > 0)
    855 		size += fs->fs_ncg * sizeof(int32_t);
    856 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    857 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    858 	fs->fs_csp = space;
    859 	for (i = 0; i < blks; i += fs->fs_frag) {
    860 		size = fs->fs_bsize;
    861 		if (i + fs->fs_frag > blks)
    862 			size = (blks - i) * fs->fs_fsize;
    863 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    864 			      cred, &bp);
    865 		if (error) {
    866 			free(fs->fs_csp, M_UFSMNT);
    867 			goto out2;
    868 		}
    869 #ifdef FFS_EI
    870 		if (needswap)
    871 			ffs_csum_swap((struct csum *)bp->b_data,
    872 				(struct csum *)space, size);
    873 		else
    874 #endif
    875 			memcpy(space, bp->b_data, (u_int)size);
    876 
    877 		space = (char *)space + size;
    878 		brelse(bp);
    879 		bp = NULL;
    880 	}
    881 	if (fs->fs_contigsumsize > 0) {
    882 		fs->fs_maxcluster = lp = space;
    883 		for (i = 0; i < fs->fs_ncg; i++)
    884 			*lp++ = fs->fs_contigsumsize;
    885 		space = lp;
    886 	}
    887 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    888 	fs->fs_contigdirs = space;
    889 	space = (char *)space + size;
    890 	memset(fs->fs_contigdirs, 0, size);
    891 		/* Compatibility for old filesystems - XXX */
    892 	if (fs->fs_avgfilesize <= 0)
    893 		fs->fs_avgfilesize = AVFILESIZ;
    894 	if (fs->fs_avgfpdir <= 0)
    895 		fs->fs_avgfpdir = AFPDIR;
    896 	mp->mnt_data = ump;
    897 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    898 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
    899 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    900 	if (UFS_MPISAPPLEUFS(mp)) {
    901 		/* NeXT used to keep short symlinks in the inode even
    902 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
    903 		 * is probably -1, but we still need to be able to identify
    904 		 * short symlinks.
    905 		 */
    906 		mp->mnt_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    907 	}
    908 	mp->mnt_fs_bshift = fs->fs_bshift;
    909 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    910 	mp->mnt_flag |= MNT_LOCAL;
    911 #ifdef FFS_EI
    912 	if (needswap)
    913 		ump->um_flags |= UFS_NEEDSWAP;
    914 #endif
    915 	ump->um_mountp = mp;
    916 	ump->um_dev = dev;
    917 	ump->um_devvp = devvp;
    918 	ump->um_nindir = fs->fs_nindir;
    919 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
    920 	ump->um_bptrtodb = fs->fs_fsbtodb;
    921 	ump->um_seqinc = fs->fs_frag;
    922 	for (i = 0; i < MAXQUOTAS; i++)
    923 		ump->um_quotas[i] = NULLVP;
    924 	devvp->v_specmountpoint = mp;
    925 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
    926 		error = softdep_mount(devvp, mp, fs, cred);
    927 		if (error) {
    928 			free(fs->fs_csp, M_UFSMNT);
    929 			goto out;
    930 		}
    931 	}
    932 	return (0);
    933 out2:
    934 	free(fs, M_UFSMNT);
    935 out:
    936 	devvp->v_specmountpoint = NULL;
    937 	if (bp)
    938 		brelse(bp);
    939 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    940 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    941 	VOP_UNLOCK(devvp, 0);
    942 	if (ump) {
    943 		free(ump, M_UFSMNT);
    944 		mp->mnt_data = NULL;
    945 	}
    946 	return (error);
    947 }
    948 
    949 /*
    950  * Sanity checks for loading old filesystem superblocks.
    951  * See ffs_oldfscompat_write below for unwound actions.
    952  *
    953  * XXX - Parts get retired eventually.
    954  * Unfortunately new bits get added.
    955  */
    956 static void
    957 ffs_oldfscompat_read(fs, ump, sblockloc)
    958 	struct fs *fs;
    959 	struct ufsmount *ump;
    960 	daddr_t sblockloc;
    961 {
    962 	off_t maxfilesize;
    963 
    964 	if (fs->fs_magic != FS_UFS1_MAGIC)
    965 		return;
    966 
    967 	/*
    968 	 * If not yet done, update fs_flags location and value of fs_sblockloc.
    969 	 */
    970 	if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
    971 		fs->fs_flags = fs->fs_old_flags;
    972 		fs->fs_old_flags |= FS_FLAGS_UPDATED;
    973 		fs->fs_sblockloc = sblockloc;
    974 	}
    975 
    976 	/*
    977 	 * If the new fields haven't been set yet, or if the filesystem
    978 	 * was mounted and modified by an old kernel, use the old csum
    979 	 * totals.
    980 	 */
    981 	if (fs->fs_maxbsize != fs->fs_bsize || fs->fs_time < fs->fs_old_time) {
    982 		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
    983 		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
    984 		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
    985 		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
    986 	}
    987 
    988 	/*
    989 	 * If not yet done, update UFS1 superblock with new wider fields.
    990 	 */
    991 	if (fs->fs_maxbsize != fs->fs_bsize) {
    992 		fs->fs_maxbsize = fs->fs_bsize;
    993 		fs->fs_time = fs->fs_old_time;
    994 		fs->fs_size = fs->fs_old_size;
    995 		fs->fs_dsize = fs->fs_old_dsize;
    996 		fs->fs_csaddr = fs->fs_old_csaddr;
    997 	}
    998 
    999 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
   1000 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
   1001 		fs->fs_qbmask = ~fs->fs_bmask;
   1002 		fs->fs_qfmask = ~fs->fs_fmask;
   1003 	}
   1004 
   1005 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;
   1006 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1007 	if (fs->fs_maxfilesize > maxfilesize)
   1008 		fs->fs_maxfilesize = maxfilesize;
   1009 
   1010 	/* Compatibility for old filesystems */
   1011 	if (fs->fs_avgfilesize <= 0)
   1012 		fs->fs_avgfilesize = AVFILESIZ;
   1013 	if (fs->fs_avgfpdir <= 0)
   1014 		fs->fs_avgfpdir = AFPDIR;
   1015 #if 0
   1016 	if (bigcgs) {
   1017 		fs->fs_save_cgsize = fs->fs_cgsize;
   1018 		fs->fs_cgsize = fs->fs_bsize;
   1019 	}
   1020 #endif
   1021 }
   1022 
   1023 /*
   1024  * Unwinding superblock updates for old filesystems.
   1025  * See ffs_oldfscompat_read above for details.
   1026  *
   1027  * XXX - Parts get retired eventually.
   1028  * Unfortunately new bits get added.
   1029  */
   1030 static void
   1031 ffs_oldfscompat_write(fs, ump)
   1032 	struct fs *fs;
   1033 	struct ufsmount *ump;
   1034 {
   1035 	if (fs->fs_magic != FS_UFS1_MAGIC)
   1036 		return;
   1037 	/*
   1038 	 * Copy back UFS2 updated fields that UFS1 inspects.
   1039 	 */
   1040 
   1041 	fs->fs_old_time = fs->fs_time;
   1042 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1043 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1044 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1045 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1046 	fs->fs_maxfilesize = ump->um_savedmaxfilesize;
   1047 
   1048 #if 0
   1049 	if (bigcgs) {
   1050 		fs->fs_cgsize = fs->fs_save_cgsize;
   1051 		fs->fs_save_cgsize = 0;
   1052 	}
   1053 #endif
   1054 }
   1055 
   1056 /*
   1057  * unmount system call
   1058  */
   1059 int
   1060 ffs_unmount(mp, mntflags, p)
   1061 	struct mount *mp;
   1062 	int mntflags;
   1063 	struct proc *p;
   1064 {
   1065 	struct ufsmount *ump;
   1066 	struct fs *fs;
   1067 	int error, flags, penderr;
   1068 
   1069 	penderr = 0;
   1070 	flags = 0;
   1071 	if (mntflags & MNT_FORCE)
   1072 		flags |= FORCECLOSE;
   1073 	if (mp->mnt_flag & MNT_SOFTDEP) {
   1074 		if ((error = softdep_flushfiles(mp, flags, p)) != 0)
   1075 			return (error);
   1076 	} else {
   1077 		if ((error = ffs_flushfiles(mp, flags, p)) != 0)
   1078 			return (error);
   1079 	}
   1080 	ump = VFSTOUFS(mp);
   1081 	fs = ump->um_fs;
   1082 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1083 		printf("%s: unmount pending error: blocks %" PRId64
   1084 		       " files %d\n",
   1085 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
   1086 		fs->fs_pendingblocks = 0;
   1087 		fs->fs_pendinginodes = 0;
   1088 		penderr = 1;
   1089 	}
   1090 	if (fs->fs_ronly == 0 &&
   1091 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1092 	    fs->fs_clean & FS_WASCLEAN) {
   1093 		/*
   1094 		 * XXXX don't mark fs clean in the case of softdep
   1095 		 * pending block errors, until they are fixed.
   1096 		 */
   1097 		if (penderr == 0) {
   1098 			if (mp->mnt_flag & MNT_SOFTDEP)
   1099 				fs->fs_flags &= ~FS_DOSOFTDEP;
   1100 			fs->fs_clean = FS_ISCLEAN;
   1101 		}
   1102 		fs->fs_fmod = 0;
   1103 		(void) ffs_sbupdate(ump, MNT_WAIT);
   1104 	}
   1105 	if (ump->um_devvp->v_type != VBAD)
   1106 		ump->um_devvp->v_specmountpoint = NULL;
   1107 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1108 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
   1109 		NOCRED, p);
   1110 	vput(ump->um_devvp);
   1111 	free(fs->fs_csp, M_UFSMNT);
   1112 	free(fs, M_UFSMNT);
   1113 #ifdef SUPPORT_FS_42POSTBLFMT_WRITE
   1114 	if (ump->um_opostsave != NULL)
   1115 		free(ump->um_opostsave, M_UFSMNT);
   1116 #endif
   1117 	free(ump, M_UFSMNT);
   1118 	mp->mnt_data = NULL;
   1119 	mp->mnt_flag &= ~MNT_LOCAL;
   1120 	return (error);
   1121 }
   1122 
   1123 /*
   1124  * Flush out all the files in a filesystem.
   1125  */
   1126 int
   1127 ffs_flushfiles(mp, flags, p)
   1128 	struct mount *mp;
   1129 	int flags;
   1130 	struct proc *p;
   1131 {
   1132 	extern int doforce;
   1133 	struct ufsmount *ump;
   1134 	int error;
   1135 
   1136 	if (!doforce)
   1137 		flags &= ~FORCECLOSE;
   1138 	ump = VFSTOUFS(mp);
   1139 #ifdef QUOTA
   1140 	if (mp->mnt_flag & MNT_QUOTA) {
   1141 		int i;
   1142 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
   1143 			return (error);
   1144 		for (i = 0; i < MAXQUOTAS; i++) {
   1145 			if (ump->um_quotas[i] == NULLVP)
   1146 				continue;
   1147 			quotaoff(p, mp, i);
   1148 		}
   1149 		/*
   1150 		 * Here we fall through to vflush again to ensure
   1151 		 * that we have gotten rid of all the system vnodes.
   1152 		 */
   1153 	}
   1154 #endif
   1155 	/*
   1156 	 * Flush all the files.
   1157 	 */
   1158 	error = vflush(mp, NULLVP, flags);
   1159 	if (error)
   1160 		return (error);
   1161 	/*
   1162 	 * Flush filesystem metadata.
   1163 	 */
   1164 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1165 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
   1166 	VOP_UNLOCK(ump->um_devvp, 0);
   1167 	return (error);
   1168 }
   1169 
   1170 /*
   1171  * Get file system statistics.
   1172  */
   1173 int
   1174 ffs_statfs(mp, sbp, p)
   1175 	struct mount *mp;
   1176 	struct statfs *sbp;
   1177 	struct proc *p;
   1178 {
   1179 	struct ufsmount *ump;
   1180 	struct fs *fs;
   1181 
   1182 	ump = VFSTOUFS(mp);
   1183 	fs = ump->um_fs;
   1184 #ifdef COMPAT_09
   1185 	sbp->f_type = 1;
   1186 #else
   1187 	sbp->f_type = 0;
   1188 #endif
   1189 	sbp->f_bsize = fs->fs_fsize;
   1190 	sbp->f_iosize = fs->fs_bsize;
   1191 	sbp->f_blocks = fs->fs_dsize;
   1192 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1193 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
   1194 	sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
   1195 	    (100 - fs->fs_minfree) / (u_int64_t) 100) -
   1196 	    (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
   1197 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1198 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1199 	copy_statfs_info(sbp, mp);
   1200 	return (0);
   1201 }
   1202 
   1203 /*
   1204  * Go through the disk queues to initiate sandbagged IO;
   1205  * go through the inodes to write those that have been modified;
   1206  * initiate the writing of the super block if it has been modified.
   1207  *
   1208  * Note: we are always called with the filesystem marked `MPBUSY'.
   1209  */
   1210 int
   1211 ffs_sync(mp, waitfor, cred, p)
   1212 	struct mount *mp;
   1213 	int waitfor;
   1214 	struct ucred *cred;
   1215 	struct proc *p;
   1216 {
   1217 	struct vnode *vp, *nvp;
   1218 	struct inode *ip;
   1219 	struct ufsmount *ump = VFSTOUFS(mp);
   1220 	struct fs *fs;
   1221 	int error, allerror = 0;
   1222 
   1223 	fs = ump->um_fs;
   1224 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1225 		printf("fs = %s\n", fs->fs_fsmnt);
   1226 		panic("update: rofs mod");
   1227 	}
   1228 	/*
   1229 	 * Write back each (modified) inode.
   1230 	 */
   1231 	simple_lock(&mntvnode_slock);
   1232 loop:
   1233 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
   1234 		/*
   1235 		 * If the vnode that we are about to sync is no longer
   1236 		 * associated with this mount point, start over.
   1237 		 */
   1238 		if (vp->v_mount != mp)
   1239 			goto loop;
   1240 		simple_lock(&vp->v_interlock);
   1241 		nvp = LIST_NEXT(vp, v_mntvnodes);
   1242 		ip = VTOI(vp);
   1243 		if (vp->v_type == VNON ||
   1244 		    ((ip->i_flag &
   1245 		      (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1246 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1247 		     vp->v_uobj.uo_npages == 0))
   1248 		{
   1249 			simple_unlock(&vp->v_interlock);
   1250 			continue;
   1251 		}
   1252 		simple_unlock(&mntvnode_slock);
   1253 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1254 		if (error) {
   1255 			simple_lock(&mntvnode_slock);
   1256 			if (error == ENOENT)
   1257 				goto loop;
   1258 			continue;
   1259 		}
   1260 		if ((error = VOP_FSYNC(vp, cred,
   1261 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1262 			allerror = error;
   1263 		vput(vp);
   1264 		simple_lock(&mntvnode_slock);
   1265 	}
   1266 	simple_unlock(&mntvnode_slock);
   1267 	/*
   1268 	 * Force stale file system control information to be flushed.
   1269 	 */
   1270 	if (waitfor != MNT_LAZY) {
   1271 		if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
   1272 			waitfor = MNT_NOWAIT;
   1273 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1274 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1275 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1276 			allerror = error;
   1277 		VOP_UNLOCK(ump->um_devvp, 0);
   1278 	}
   1279 #ifdef QUOTA
   1280 	qsync(mp);
   1281 #endif
   1282 	/*
   1283 	 * Write back modified superblock.
   1284 	 */
   1285 	if (fs->fs_fmod != 0) {
   1286 		fs->fs_fmod = 0;
   1287 		fs->fs_time = time.tv_sec;
   1288 		if ((error = ffs_cgupdate(ump, waitfor)))
   1289 			allerror = error;
   1290 	}
   1291 	return (allerror);
   1292 }
   1293 
   1294 /*
   1295  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1296  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1297  * return the inode locked.  Detection and handling of mount points must be
   1298  * done by the calling routine.
   1299  */
   1300 int
   1301 ffs_vget(mp, ino, vpp)
   1302 	struct mount *mp;
   1303 	ino_t ino;
   1304 	struct vnode **vpp;
   1305 {
   1306 	struct fs *fs;
   1307 	struct inode *ip;
   1308 	struct ufsmount *ump;
   1309 	struct buf *bp;
   1310 	struct vnode *vp;
   1311 	dev_t dev;
   1312 	int error;
   1313 
   1314 	ump = VFSTOUFS(mp);
   1315 	dev = ump->um_dev;
   1316 
   1317 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1318 		return (0);
   1319 
   1320 	/* Allocate a new vnode/inode. */
   1321 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1322 		*vpp = NULL;
   1323 		return (error);
   1324 	}
   1325 
   1326 	/*
   1327 	 * If someone beat us to it while sleeping in getnewvnode(),
   1328 	 * push back the freshly allocated vnode we don't need, and return.
   1329 	 */
   1330 
   1331 	do {
   1332 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1333 			ungetnewvnode(vp);
   1334 			return (0);
   1335 		}
   1336 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1337 
   1338 	/*
   1339 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1340 	 * XXX create another pool for MFS inodes?
   1341 	 */
   1342 
   1343 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1344 	memset(ip, 0, sizeof(struct inode));
   1345 	vp->v_data = ip;
   1346 	ip->i_vnode = vp;
   1347 	ip->i_ump = ump;
   1348 	ip->i_fs = fs = ump->um_fs;
   1349 	ip->i_dev = dev;
   1350 	ip->i_number = ino;
   1351 	LIST_INIT(&ip->i_pcbufhd);
   1352 #ifdef QUOTA
   1353 	{
   1354 		int i;
   1355 
   1356 		for (i = 0; i < MAXQUOTAS; i++)
   1357 			ip->i_dquot[i] = NODQUOT;
   1358 	}
   1359 #endif
   1360 
   1361 	/*
   1362 	 * Put it onto its hash chain and lock it so that other requests for
   1363 	 * this inode will block if they arrive while we are sleeping waiting
   1364 	 * for old data structures to be purged or for the contents of the
   1365 	 * disk portion of this inode to be read.
   1366 	 */
   1367 
   1368 	ufs_ihashins(ip);
   1369 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1370 
   1371 	/* Read in the disk contents for the inode, copy into the inode. */
   1372 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1373 		      (int)fs->fs_bsize, NOCRED, &bp);
   1374 	if (error) {
   1375 
   1376 		/*
   1377 		 * The inode does not contain anything useful, so it would
   1378 		 * be misleading to leave it on its hash chain. With mode
   1379 		 * still zero, it will be unlinked and returned to the free
   1380 		 * list by vput().
   1381 		 */
   1382 
   1383 		vput(vp);
   1384 		brelse(bp);
   1385 		*vpp = NULL;
   1386 		return (error);
   1387 	}
   1388 	if (ip->i_ump->um_fstype == UFS1)
   1389 		ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
   1390 	else
   1391 		ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
   1392 	ffs_load_inode(bp, ip, fs, ino);
   1393 	if (DOINGSOFTDEP(vp))
   1394 		softdep_load_inodeblock(ip);
   1395 	else
   1396 		ip->i_ffs_effnlink = ip->i_nlink;
   1397 	brelse(bp);
   1398 
   1399 	/*
   1400 	 * Initialize the vnode from the inode, check for aliases.
   1401 	 * Note that the underlying vnode may have changed.
   1402 	 */
   1403 
   1404 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1405 
   1406 	/*
   1407 	 * Finish inode initialization now that aliasing has been resolved.
   1408 	 */
   1409 
   1410 	genfs_node_init(vp, &ffs_genfsops);
   1411 	ip->i_devvp = ump->um_devvp;
   1412 	VREF(ip->i_devvp);
   1413 
   1414 	/*
   1415 	 * Ensure that uid and gid are correct. This is a temporary
   1416 	 * fix until fsck has been changed to do the update.
   1417 	 */
   1418 
   1419 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   1420 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   1421 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   1422 	}							/* XXX */
   1423 	uvm_vnp_setsize(vp, ip->i_size);
   1424 	*vpp = vp;
   1425 	return (0);
   1426 }
   1427 
   1428 /*
   1429  * File handle to vnode
   1430  *
   1431  * Have to be really careful about stale file handles:
   1432  * - check that the inode number is valid
   1433  * - call ffs_vget() to get the locked inode
   1434  * - check for an unallocated inode (i_mode == 0)
   1435  * - check that the given client host has export rights and return
   1436  *   those rights via. exflagsp and credanonp
   1437  */
   1438 int
   1439 ffs_fhtovp(mp, fhp, vpp)
   1440 	struct mount *mp;
   1441 	struct fid *fhp;
   1442 	struct vnode **vpp;
   1443 {
   1444 	struct ufid *ufhp;
   1445 	struct fs *fs;
   1446 
   1447 	ufhp = (struct ufid *)fhp;
   1448 	fs = VFSTOUFS(mp)->um_fs;
   1449 	if (ufhp->ufid_ino < ROOTINO ||
   1450 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1451 		return (ESTALE);
   1452 	return (ufs_fhtovp(mp, ufhp, vpp));
   1453 }
   1454 
   1455 /*
   1456  * Vnode pointer to File handle
   1457  */
   1458 /* ARGSUSED */
   1459 int
   1460 ffs_vptofh(vp, fhp)
   1461 	struct vnode *vp;
   1462 	struct fid *fhp;
   1463 {
   1464 	struct inode *ip;
   1465 	struct ufid *ufhp;
   1466 
   1467 	ip = VTOI(vp);
   1468 	ufhp = (struct ufid *)fhp;
   1469 	ufhp->ufid_len = sizeof(struct ufid);
   1470 	ufhp->ufid_ino = ip->i_number;
   1471 	ufhp->ufid_gen = ip->i_gen;
   1472 	return (0);
   1473 }
   1474 
   1475 void
   1476 ffs_init()
   1477 {
   1478 	if (ffs_initcount++ > 0)
   1479 		return;
   1480 
   1481 	softdep_initialize();
   1482 	ufs_init();
   1483 
   1484 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
   1485 	    &pool_allocator_nointr);
   1486 	pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
   1487 	    "dino1pl", &pool_allocator_nointr);
   1488 	pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
   1489 	    "dino2pl", &pool_allocator_nointr);
   1490 }
   1491 
   1492 void
   1493 ffs_reinit()
   1494 {
   1495 	softdep_reinitialize();
   1496 	ufs_reinit();
   1497 }
   1498 
   1499 void
   1500 ffs_done()
   1501 {
   1502 	if (--ffs_initcount > 0)
   1503 		return;
   1504 
   1505 	/* XXX softdep cleanup ? */
   1506 	ufs_done();
   1507 	pool_destroy(&ffs_inode_pool);
   1508 }
   1509 
   1510 int
   1511 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1512 	int *name;
   1513 	u_int namelen;
   1514 	void *oldp;
   1515 	size_t *oldlenp;
   1516 	void *newp;
   1517 	size_t newlen;
   1518 	struct proc *p;
   1519 {
   1520 	extern int doasyncfree;
   1521 	extern int ffs_log_changeopt;
   1522 
   1523 	/* all sysctl names at this level are terminal */
   1524 	if (namelen != 1)
   1525 		return (ENOTDIR);		/* overloaded */
   1526 
   1527 	switch (name[0]) {
   1528 	case FFS_ASYNCFREE:
   1529 		return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
   1530 	case FFS_LOG_CHANGEOPT:
   1531 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1532 			&ffs_log_changeopt));
   1533 	default:
   1534 		return (EOPNOTSUPP);
   1535 	}
   1536 	/* NOTREACHED */
   1537 }
   1538 
   1539 /*
   1540  * Write a superblock and associated information back to disk.
   1541  */
   1542 int
   1543 ffs_sbupdate(mp, waitfor)
   1544 	struct ufsmount *mp;
   1545 	int waitfor;
   1546 {
   1547 	struct fs *fs = mp->um_fs;
   1548 	struct buf *bp;
   1549 	int error = 0;
   1550 	u_int32_t saveflag;
   1551 
   1552 	bp = getblk(mp->um_devvp,
   1553 	    fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
   1554 	    (int)fs->fs_sbsize, 0, 0);
   1555 	saveflag = fs->fs_flags & FS_INTERNAL;
   1556 	fs->fs_flags &= ~FS_INTERNAL;
   1557 
   1558 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
   1559 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
   1560 		printf("%s: correcting fs_sblockloc from %" PRId64 " to %d\n",
   1561 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
   1562 		fs->fs_sblockloc = SBLOCK_UFS1;
   1563 	}
   1564 
   1565 	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
   1566 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
   1567 		printf("%s: correcting fs_sblockloc from %" PRId64 " to %d\n",
   1568 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
   1569 		fs->fs_sblockloc = SBLOCK_UFS2;
   1570 	}
   1571 
   1572 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1573 
   1574 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   1575 #ifdef FFS_EI
   1576 	if (mp->um_flags & UFS_NEEDSWAP)
   1577 		ffs_sb_swap(fs, (struct fs *)bp->b_data);
   1578 #endif
   1579 #ifdef SUPPORT_FS_42POSTBLFMT_WRITE
   1580 	if (fs->fs_old_postblformat == FS_42POSTBLFMT)
   1581 		memcpy(&((struct fs *)bp->b_data)->fs_old_postbl_start,
   1582 		    mp->um_opostsave, 256);
   1583 #endif
   1584 	fs->fs_flags |= saveflag;
   1585 
   1586 	if (waitfor == MNT_WAIT)
   1587 		error = bwrite(bp);
   1588 	else
   1589 		bawrite(bp);
   1590 	return (error);
   1591 }
   1592 
   1593 int
   1594 ffs_cgupdate(mp, waitfor)
   1595 	struct ufsmount *mp;
   1596 	int waitfor;
   1597 {
   1598 	struct fs *fs = mp->um_fs;
   1599 	struct buf *bp;
   1600 	int blks;
   1601 	void *space;
   1602 	int i, size, error = 0, allerror = 0;
   1603 
   1604 	allerror = ffs_sbupdate(mp, waitfor);
   1605 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1606 	space = fs->fs_csp;
   1607 	for (i = 0; i < blks; i += fs->fs_frag) {
   1608 		size = fs->fs_bsize;
   1609 		if (i + fs->fs_frag > blks)
   1610 			size = (blks - i) * fs->fs_fsize;
   1611 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1612 		    size, 0, 0);
   1613 #ifdef FFS_EI
   1614 		if (mp->um_flags & UFS_NEEDSWAP)
   1615 			ffs_csum_swap((struct csum*)space,
   1616 			    (struct csum*)bp->b_data, size);
   1617 		else
   1618 #endif
   1619 			memcpy(bp->b_data, space, (u_int)size);
   1620 		space = (char *)space + size;
   1621 		if (waitfor == MNT_WAIT)
   1622 			error = bwrite(bp);
   1623 		else
   1624 			bawrite(bp);
   1625 	}
   1626 	if (!allerror && error)
   1627 		allerror = error;
   1628 	return (allerror);
   1629 }
   1630