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