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