Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.125
      1 /*	$NetBSD: ffs_vfsops.c,v 1.125 2003/10/15 11:29:01 hannken 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.125 2003/10/15 11:29:01 hannken 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 	daddr_t sblockloc;
    465 	int i, blks, size, error;
    466 	int32_t *lp;
    467 	struct ufsmount *ump;
    468 
    469 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    470 		return (EINVAL);
    471 
    472 	ump = VFSTOUFS(mountp);
    473 	/*
    474 	 * Step 1: invalidate all cached meta-data.
    475 	 */
    476 	devvp = ump->um_devvp;
    477 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    478 	error = vinvalbuf(devvp, 0, cred, p, 0, 0);
    479 	VOP_UNLOCK(devvp, 0);
    480 	if (error)
    481 		panic("ffs_reload: dirty1");
    482 	/*
    483 	 * Step 2: re-read superblock from disk.
    484 	 */
    485 	fs = ump->um_fs;
    486 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p) != 0)
    487 		size = DEV_BSIZE;
    488 	else
    489 		size = dpart.disklab->d_secsize;
    490 	error = bread(devvp, fs->fs_sblockloc / size, fs->fs_sbsize,
    491 		      NOCRED, &bp);
    492 	if (error) {
    493 		brelse(bp);
    494 		return (error);
    495 	}
    496 	newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
    497 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
    498 #ifdef SUPPORT_42POSTBLFMT_WRITE
    499 	if (fs->fs_old_postblformat == FS_42POSTBLFMT)
    500 		memcpy(ump->um_opostsave, &newfs->fs_old_postbl_start, 256);
    501 #endif
    502 #ifdef FFS_EI
    503 	if (ump->um_flags & UFS_NEEDSWAP) {
    504 		ffs_sb_swap((struct fs*)bp->b_data, newfs);
    505 		fs->fs_flags |= FS_SWAPPED;
    506 	} else
    507 #endif
    508 		fs->fs_flags &= ~FS_SWAPPED;
    509 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
    510 	     newfs->fs_magic != FS_UFS2_MAGIC)||
    511 	     newfs->fs_bsize > MAXBSIZE ||
    512 	     newfs->fs_bsize < sizeof(struct fs)) {
    513 		brelse(bp);
    514 		free(newfs, M_UFSMNT);
    515 		return (EIO);		/* XXX needs translation */
    516 	}
    517 	/*
    518 	 * Copy pointer fields back into superblock before copying in	XXX
    519 	 * new superblock. These should really be in the ufsmount.	XXX
    520 	 * Note that important parameters (eg fs_ncg) are unchanged.
    521 	 */
    522 	newfs->fs_csp = fs->fs_csp;
    523 	newfs->fs_maxcluster = fs->fs_maxcluster;
    524 	newfs->fs_contigdirs = fs->fs_contigdirs;
    525 	newfs->fs_ronly = fs->fs_ronly;
    526 	newfs->fs_active = fs->fs_active;
    527 	sblockloc = fs->fs_sblockloc;
    528 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    529 	brelse(bp);
    530 	free(newfs, M_UFSMNT);
    531 
    532 	/* Recheck for apple UFS filesystem */
    533 	VFSTOUFS(mountp)->um_flags &= ~UFS_ISAPPLEUFS;
    534 	/* First check to see if this is tagged as an Apple UFS filesystem
    535 	 * in the disklabel
    536 	 */
    537 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
    538 		(dpart.part->p_fstype == FS_APPLEUFS)) {
    539 		VFSTOUFS(mountp)->um_flags |= UFS_ISAPPLEUFS;
    540 	}
    541 #ifdef APPLE_UFS
    542 	else {
    543 		/* Manually look for an apple ufs label, and if a valid one
    544 		 * is found, then treat it like an Apple UFS filesystem anyway
    545 		 */
    546 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
    547 			APPLEUFS_LABEL_SIZE, cred, &bp);
    548 		if (error) {
    549 			brelse(bp);
    550 			return (error);
    551 		}
    552 		error = ffs_appleufs_validate(fs->fs_fsmnt,
    553 			(struct appleufslabel *)bp->b_data,NULL);
    554 		if (error == 0) {
    555 			VFSTOUFS(mountp)->um_flags |= UFS_ISAPPLEUFS;
    556 		}
    557 		brelse(bp);
    558 		bp = NULL;
    559 	}
    560 #else
    561 	if (VFSTOUFS(mountp)->um_flags & UFS_ISAPPLEUFS)
    562 		return (EIO);
    563 #endif
    564 
    565 	mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    566 	if (UFS_MPISAPPLEUFS(mountp)) {
    567 		/* see comment about NeXT below */
    568 		mountp->mnt_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    569 	}
    570 	ffs_oldfscompat_read(fs, VFSTOUFS(mountp), fs->fs_sblockloc);
    571 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    572 		fs->fs_pendingblocks = 0;
    573 		fs->fs_pendinginodes = 0;
    574 	}
    575 
    576 	ffs_statfs(mountp, &mountp->mnt_stat, p);
    577 	/*
    578 	 * Step 3: re-read summary information from disk.
    579 	 */
    580 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    581 	space = fs->fs_csp;
    582 	for (i = 0; i < blks; i += fs->fs_frag) {
    583 		size = fs->fs_bsize;
    584 		if (i + fs->fs_frag > blks)
    585 			size = (blks - i) * fs->fs_fsize;
    586 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    587 			      NOCRED, &bp);
    588 		if (error) {
    589 			brelse(bp);
    590 			return (error);
    591 		}
    592 #ifdef FFS_EI
    593 		if (UFS_FSNEEDSWAP(fs))
    594 			ffs_csum_swap((struct csum *)bp->b_data,
    595 			    (struct csum *)space, size);
    596 		else
    597 #endif
    598 			memcpy(space, bp->b_data, (size_t)size);
    599 		space = (char *)space + size;
    600 		brelse(bp);
    601 	}
    602 	if ((fs->fs_flags & FS_DOSOFTDEP))
    603 		softdep_mount(devvp, mountp, fs, cred);
    604 	/*
    605 	 * We no longer know anything about clusters per cylinder group.
    606 	 */
    607 	if (fs->fs_contigsumsize > 0) {
    608 		lp = fs->fs_maxcluster;
    609 		for (i = 0; i < fs->fs_ncg; i++)
    610 			*lp++ = fs->fs_contigsumsize;
    611 	}
    612 
    613 loop:
    614 	simple_lock(&mntvnode_slock);
    615 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    616 		if (vp->v_mount != mountp) {
    617 			simple_unlock(&mntvnode_slock);
    618 			goto loop;
    619 		}
    620 		nvp = vp->v_mntvnodes.le_next;
    621 		/*
    622 		 * Step 4: invalidate all inactive vnodes.
    623 		 */
    624 		if (vrecycle(vp, &mntvnode_slock, p))
    625 			goto loop;
    626 		/*
    627 		 * Step 5: invalidate all cached file data.
    628 		 */
    629 		simple_lock(&vp->v_interlock);
    630 		simple_unlock(&mntvnode_slock);
    631 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    632 			goto loop;
    633 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    634 			panic("ffs_reload: dirty2");
    635 		/*
    636 		 * Step 6: re-read inode data for all active vnodes.
    637 		 */
    638 		ip = VTOI(vp);
    639 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    640 			      (int)fs->fs_bsize, NOCRED, &bp);
    641 		if (error) {
    642 			brelse(bp);
    643 			vput(vp);
    644 			return (error);
    645 		}
    646 		ffs_load_inode(bp, ip, fs, ip->i_number);
    647 		ip->i_ffs_effnlink = ip->i_nlink;
    648 		brelse(bp);
    649 		vput(vp);
    650 		simple_lock(&mntvnode_slock);
    651 	}
    652 	simple_unlock(&mntvnode_slock);
    653 	return (0);
    654 }
    655 
    656 /*
    657  * Possible superblock locations ordered from most to least likely.
    658  */
    659 static int sblock_try[] = SBLOCKSEARCH;
    660 
    661 /*
    662  * Common code for mount and mountroot
    663  */
    664 int
    665 ffs_mountfs(devvp, mp, p)
    666 	struct vnode *devvp;
    667 	struct mount *mp;
    668 	struct proc *p;
    669 {
    670 	struct ufsmount *ump;
    671 	struct buf *bp;
    672 	struct fs *fs;
    673 	dev_t dev;
    674 	struct partinfo dpart;
    675 	void *space;
    676 	daddr_t sblockloc, fsblockloc;
    677 	int blks, fstype;
    678 	int error, i, size, ronly;
    679 #ifdef FFS_EI
    680 	int needswap = 0;		/* keep gcc happy */
    681 #endif
    682 	int32_t *lp;
    683 	struct ucred *cred;
    684 	u_int32_t sbsize = 8192;	/* keep gcc happy*/
    685 
    686 	dev = devvp->v_rdev;
    687 	cred = p ? p->p_ucred : NOCRED;
    688 	/*
    689 	 * Disallow multiple mounts of the same device.
    690 	 * Disallow mounting of a device that is currently in use
    691 	 * (except for root, which might share swap device for miniroot).
    692 	 * Flush out any old buffers remaining from a previous use.
    693 	 */
    694 	if ((error = vfs_mountedon(devvp)) != 0)
    695 		return (error);
    696 	if (vcount(devvp) > 1 && devvp != rootvp)
    697 		return (EBUSY);
    698 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    699 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
    700 	VOP_UNLOCK(devvp, 0);
    701 	if (error)
    702 		return (error);
    703 
    704 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    705 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    706 	if (error)
    707 		return (error);
    708 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) != 0)
    709 		size = DEV_BSIZE;
    710 	else
    711 		size = dpart.disklab->d_secsize;
    712 
    713 	bp = NULL;
    714 	ump = NULL;
    715 	fs = NULL;
    716 	fsblockloc = sblockloc = 0;
    717 	fstype = 0;
    718 
    719 	/*
    720 	 * Try reading the superblock in each of its possible locations.		 */
    721 	for (i = 0; sblock_try[i] != -1; i++) {
    722 		error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
    723 			      &bp);
    724 		if (error)
    725 			goto out;
    726 		fs = (struct fs*)bp->b_data;
    727 		fsblockloc = sblockloc = sblock_try[i];
    728 		if (fs->fs_magic == FS_UFS1_MAGIC) {
    729 			sbsize = fs->fs_sbsize;
    730 			fstype = UFS1;
    731 #ifdef FFS_EI
    732 			needswap = 0;
    733 		} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
    734 			sbsize = bswap32(fs->fs_sbsize);
    735 			fstype = UFS1;
    736 			needswap = 1;
    737 #endif
    738 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
    739 			sbsize = fs->fs_sbsize;
    740 			fstype = UFS2;
    741 			fsblockloc = fs->fs_sblockloc;
    742 #ifdef FFS_EI
    743 			needswap = 0;
    744 		} else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
    745 			sbsize = bswap32(fs->fs_sbsize);
    746 			fstype = UFS2;
    747 			fsblockloc = bswap64(fs->fs_sblockloc);
    748 			needswap = 1;
    749 #endif
    750 		} else
    751 			goto next_sblock;
    752 
    753 		if ((fsblockloc == sblockloc ||
    754 		     (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)
    755 		    && sbsize <= MAXBSIZE && sbsize >= sizeof(struct fs))
    756 			break;
    757 
    758 next_sblock:
    759 		bp->b_flags |= B_NOCACHE;
    760 		brelse(bp);
    761 		bp = NULL;
    762 	}
    763 
    764 	if (sblock_try[i] == -1) {
    765 		error = EINVAL;
    766 		goto out;
    767 	}
    768 
    769 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    770 	memcpy(fs, bp->b_data, sbsize);
    771 
    772 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    773 	memset(ump, 0, sizeof *ump);
    774 	ump->um_fs = fs;
    775 
    776 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
    777 #ifdef SUPPORT_FS_42POSTBLFMT_WRITE
    778 		ump->um_opostsave = malloc(256, M_UFSMNT, M_WAITOK);
    779 		memcpy(ump->um_opostsave, &fs->fs_old_postbl_start, 256);
    780 #else
    781 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    782 			error = EROFS;
    783 			goto out2;
    784 		}
    785 #endif
    786 	}
    787 
    788 #ifdef FFS_EI
    789 	if (needswap) {
    790 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    791 		fs->fs_flags |= FS_SWAPPED;
    792 	} else
    793 #endif
    794 		fs->fs_flags &= ~FS_SWAPPED;
    795 
    796 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    797 		fs->fs_pendingblocks = 0;
    798 		fs->fs_pendinginodes = 0;
    799 	}
    800 
    801 	ump->um_fstype = fstype;
    802 	if (fs->fs_sbsize < SBLOCKSIZE)
    803 		bp->b_flags |= B_INVAL;
    804 	brelse(bp);
    805 	bp = NULL;
    806 
    807 	ffs_oldfscompat_read(fs, ump, sblockloc);
    808 
    809 	/* First check to see if this is tagged as an Apple UFS filesystem
    810 	 * in the disklabel
    811 	 */
    812 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
    813 		(dpart.part->p_fstype == FS_APPLEUFS)) {
    814 		ump->um_flags |= UFS_ISAPPLEUFS;
    815 	}
    816 #ifdef APPLE_UFS
    817 	else {
    818 		/* Manually look for an apple ufs label, and if a valid one
    819 		 * is found, then treat it like an Apple UFS filesystem anyway
    820 		 */
    821 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
    822 			APPLEUFS_LABEL_SIZE, cred, &bp);
    823 		if (error)
    824 			goto out;
    825 		error = ffs_appleufs_validate(fs->fs_fsmnt,
    826 			(struct appleufslabel *)bp->b_data,NULL);
    827 		if (error == 0) {
    828 			ump->um_flags |= UFS_ISAPPLEUFS;
    829 		}
    830 		brelse(bp);
    831 		bp = NULL;
    832 	}
    833 #else
    834 	if (ump->um_flags & UFS_ISAPPLEUFS) {
    835 		error = EINVAL;
    836 		goto out;
    837 	}
    838 #endif
    839 
    840 	/*
    841 	 * verify that we can access the last block in the fs
    842 	 * if we're mounting read/write.
    843 	 */
    844 
    845 	if (!ronly) {
    846 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
    847 		    cred, &bp);
    848 		if (bp->b_bcount != fs->fs_fsize)
    849 			error = EINVAL;
    850 		bp->b_flags |= B_INVAL;
    851 		if (error)
    852 			goto out;
    853 		brelse(bp);
    854 		bp = NULL;
    855 	}
    856 
    857 	fs->fs_ronly = ronly;
    858 	if (ronly == 0) {
    859 		fs->fs_clean <<= 1;
    860 		fs->fs_fmod = 1;
    861 	}
    862 	size = fs->fs_cssize;
    863 	blks = howmany(size, fs->fs_fsize);
    864 	if (fs->fs_contigsumsize > 0)
    865 		size += fs->fs_ncg * sizeof(int32_t);
    866 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    867 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    868 	fs->fs_csp = space;
    869 	for (i = 0; i < blks; i += fs->fs_frag) {
    870 		size = fs->fs_bsize;
    871 		if (i + fs->fs_frag > blks)
    872 			size = (blks - i) * fs->fs_fsize;
    873 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    874 			      cred, &bp);
    875 		if (error) {
    876 			free(fs->fs_csp, M_UFSMNT);
    877 			goto out2;
    878 		}
    879 #ifdef FFS_EI
    880 		if (needswap)
    881 			ffs_csum_swap((struct csum *)bp->b_data,
    882 				(struct csum *)space, size);
    883 		else
    884 #endif
    885 			memcpy(space, bp->b_data, (u_int)size);
    886 
    887 		space = (char *)space + size;
    888 		brelse(bp);
    889 		bp = NULL;
    890 	}
    891 	if (fs->fs_contigsumsize > 0) {
    892 		fs->fs_maxcluster = lp = space;
    893 		for (i = 0; i < fs->fs_ncg; i++)
    894 			*lp++ = fs->fs_contigsumsize;
    895 		space = lp;
    896 	}
    897 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    898 	fs->fs_contigdirs = space;
    899 	space = (char *)space + size;
    900 	memset(fs->fs_contigdirs, 0, size);
    901 		/* Compatibility for old filesystems - XXX */
    902 	if (fs->fs_avgfilesize <= 0)
    903 		fs->fs_avgfilesize = AVFILESIZ;
    904 	if (fs->fs_avgfpdir <= 0)
    905 		fs->fs_avgfpdir = AFPDIR;
    906 	mp->mnt_data = ump;
    907 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    908 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
    909 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    910 	if (UFS_MPISAPPLEUFS(mp)) {
    911 		/* NeXT used to keep short symlinks in the inode even
    912 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
    913 		 * is probably -1, but we still need to be able to identify
    914 		 * short symlinks.
    915 		 */
    916 		mp->mnt_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    917 	}
    918 	mp->mnt_fs_bshift = fs->fs_bshift;
    919 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    920 	mp->mnt_flag |= MNT_LOCAL;
    921 #ifdef FFS_EI
    922 	if (needswap)
    923 		ump->um_flags |= UFS_NEEDSWAP;
    924 #endif
    925 	ump->um_mountp = mp;
    926 	ump->um_dev = dev;
    927 	ump->um_devvp = devvp;
    928 	ump->um_nindir = fs->fs_nindir;
    929 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
    930 	ump->um_bptrtodb = fs->fs_fsbtodb;
    931 	ump->um_seqinc = fs->fs_frag;
    932 	for (i = 0; i < MAXQUOTAS; i++)
    933 		ump->um_quotas[i] = NULLVP;
    934 	devvp->v_specmountpoint = mp;
    935 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
    936 		error = softdep_mount(devvp, mp, fs, cred);
    937 		if (error) {
    938 			free(fs->fs_csp, M_UFSMNT);
    939 			goto out;
    940 		}
    941 	}
    942 	return (0);
    943 out2:
    944 	free(fs, M_UFSMNT);
    945 out:
    946 	devvp->v_specmountpoint = NULL;
    947 	if (bp)
    948 		brelse(bp);
    949 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    950 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    951 	VOP_UNLOCK(devvp, 0);
    952 	if (ump) {
    953 		free(ump, M_UFSMNT);
    954 		mp->mnt_data = NULL;
    955 	}
    956 	return (error);
    957 }
    958 
    959 /*
    960  * Sanity checks for loading old filesystem superblocks.
    961  * See ffs_oldfscompat_write below for unwound actions.
    962  *
    963  * XXX - Parts get retired eventually.
    964  * Unfortunately new bits get added.
    965  */
    966 static void
    967 ffs_oldfscompat_read(fs, ump, sblockloc)
    968 	struct fs *fs;
    969 	struct ufsmount *ump;
    970 	daddr_t sblockloc;
    971 {
    972 	off_t maxfilesize;
    973 	int old_ufs1;
    974 
    975 	if (fs->fs_magic != FS_UFS1_MAGIC)
    976 		return;
    977 
    978 	/*
    979 	 * If not yet done, update UFS1 superblock with new wider fields,
    980 	 * and  update fs_flags location and value of fs_sblockloc.
    981 	 */
    982 	old_ufs1 = fs->fs_maxbsize != fs->fs_bsize;
    983 	if (old_ufs1) {
    984 		fs->fs_maxbsize = fs->fs_bsize;
    985 		fs->fs_time = fs->fs_old_time;
    986 		fs->fs_size = fs->fs_old_size;
    987 		fs->fs_dsize = fs->fs_old_dsize;
    988 		fs->fs_csaddr = fs->fs_old_csaddr;
    989 		fs->fs_sblockloc = sblockloc;
    990 		fs->fs_flags = fs->fs_old_flags;
    991 		fs->fs_old_flags |= FS_FLAGS_UPDATED;
    992 	}
    993 
    994 	/*
    995 	 * If the new fields haven't been set yet, or if the filesystem
    996 	 * was mounted and modified by an old kernel, use the old csum
    997 	 * totals, and update the flags
    998 	 */
    999 	if (old_ufs1 || fs->fs_time < fs->fs_old_time) {
   1000 		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
   1001 		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
   1002 		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
   1003 		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
   1004 		fs->fs_flags |= (fs->fs_old_flags & ~FS_FLAGS_UPDATED);
   1005 	}
   1006 
   1007 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
   1008 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
   1009 		fs->fs_qbmask = ~fs->fs_bmask;
   1010 		fs->fs_qfmask = ~fs->fs_fmask;
   1011 	}
   1012 
   1013 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;
   1014 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1015 	if (fs->fs_maxfilesize > maxfilesize)
   1016 		fs->fs_maxfilesize = maxfilesize;
   1017 
   1018 	/* Compatibility for old filesystems */
   1019 	if (fs->fs_avgfilesize <= 0)
   1020 		fs->fs_avgfilesize = AVFILESIZ;
   1021 	if (fs->fs_avgfpdir <= 0)
   1022 		fs->fs_avgfpdir = AFPDIR;
   1023 #if 0
   1024 	if (bigcgs) {
   1025 		fs->fs_save_cgsize = fs->fs_cgsize;
   1026 		fs->fs_cgsize = fs->fs_bsize;
   1027 	}
   1028 #endif
   1029 }
   1030 
   1031 /*
   1032  * Unwinding superblock updates for old filesystems.
   1033  * See ffs_oldfscompat_read above for details.
   1034  *
   1035  * XXX - Parts get retired eventually.
   1036  * Unfortunately new bits get added.
   1037  */
   1038 static void
   1039 ffs_oldfscompat_write(fs, ump)
   1040 	struct fs *fs;
   1041 	struct ufsmount *ump;
   1042 {
   1043 	if (fs->fs_magic != FS_UFS1_MAGIC)
   1044 		return;
   1045 
   1046 	/*
   1047 	 * if none of the newer flags are used, copy back fs_flags
   1048 	 * to fs_old_flags
   1049 	 */
   1050 	if ((fs->fs_flags & ~(FS_UNCLEAN|FS_DOSOFTDEP)) == 0)
   1051 		fs->fs_old_flags = fs->fs_flags & (FS_UNCLEAN|FS_DOSOFTDEP);
   1052 	/*
   1053 	 * OS X somehow still seems to use this field and panic.
   1054 	 * Just set it to zero.
   1055 	 */
   1056 	if (ump->um_flags & UFS_ISAPPLEUFS)
   1057 		fs->fs_old_nrpos = 0;
   1058 	/*
   1059 	 * Copy back UFS2 updated fields that UFS1 inspects.
   1060 	 */
   1061 
   1062 	fs->fs_old_time = fs->fs_time;
   1063 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1064 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1065 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1066 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1067 	fs->fs_maxfilesize = ump->um_savedmaxfilesize;
   1068 
   1069 #if 0
   1070 	if (bigcgs) {
   1071 		fs->fs_cgsize = fs->fs_save_cgsize;
   1072 		fs->fs_save_cgsize = 0;
   1073 	}
   1074 #endif
   1075 }
   1076 
   1077 /*
   1078  * unmount system call
   1079  */
   1080 int
   1081 ffs_unmount(mp, mntflags, p)
   1082 	struct mount *mp;
   1083 	int mntflags;
   1084 	struct proc *p;
   1085 {
   1086 	struct ufsmount *ump;
   1087 	struct fs *fs;
   1088 	int error, flags, penderr;
   1089 
   1090 	penderr = 0;
   1091 	flags = 0;
   1092 	if (mntflags & MNT_FORCE)
   1093 		flags |= FORCECLOSE;
   1094 	if (mp->mnt_flag & MNT_SOFTDEP) {
   1095 		if ((error = softdep_flushfiles(mp, flags, p)) != 0)
   1096 			return (error);
   1097 	} else {
   1098 		if ((error = ffs_flushfiles(mp, flags, p)) != 0)
   1099 			return (error);
   1100 	}
   1101 	ump = VFSTOUFS(mp);
   1102 	fs = ump->um_fs;
   1103 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1104 		printf("%s: unmount pending error: blocks %" PRId64
   1105 		       " files %d\n",
   1106 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
   1107 		fs->fs_pendingblocks = 0;
   1108 		fs->fs_pendinginodes = 0;
   1109 		penderr = 1;
   1110 	}
   1111 	if (fs->fs_ronly == 0 &&
   1112 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1113 	    fs->fs_clean & FS_WASCLEAN) {
   1114 		/*
   1115 		 * XXXX don't mark fs clean in the case of softdep
   1116 		 * pending block errors, until they are fixed.
   1117 		 */
   1118 		if (penderr == 0) {
   1119 			if (mp->mnt_flag & MNT_SOFTDEP)
   1120 				fs->fs_flags &= ~FS_DOSOFTDEP;
   1121 			fs->fs_clean = FS_ISCLEAN;
   1122 		}
   1123 		fs->fs_fmod = 0;
   1124 		(void) ffs_sbupdate(ump, MNT_WAIT);
   1125 	}
   1126 	if (ump->um_devvp->v_type != VBAD)
   1127 		ump->um_devvp->v_specmountpoint = NULL;
   1128 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1129 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
   1130 		NOCRED, p);
   1131 	vput(ump->um_devvp);
   1132 	free(fs->fs_csp, M_UFSMNT);
   1133 	free(fs, M_UFSMNT);
   1134 #ifdef SUPPORT_FS_42POSTBLFMT_WRITE
   1135 	if (ump->um_opostsave != NULL)
   1136 		free(ump->um_opostsave, M_UFSMNT);
   1137 #endif
   1138 	free(ump, M_UFSMNT);
   1139 	mp->mnt_data = NULL;
   1140 	mp->mnt_flag &= ~MNT_LOCAL;
   1141 	return (error);
   1142 }
   1143 
   1144 /*
   1145  * Flush out all the files in a filesystem.
   1146  */
   1147 int
   1148 ffs_flushfiles(mp, flags, p)
   1149 	struct mount *mp;
   1150 	int flags;
   1151 	struct proc *p;
   1152 {
   1153 	extern int doforce;
   1154 	struct ufsmount *ump;
   1155 	int error;
   1156 
   1157 	if (!doforce)
   1158 		flags &= ~FORCECLOSE;
   1159 	ump = VFSTOUFS(mp);
   1160 #ifdef QUOTA
   1161 	if (mp->mnt_flag & MNT_QUOTA) {
   1162 		int i;
   1163 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
   1164 			return (error);
   1165 		for (i = 0; i < MAXQUOTAS; i++) {
   1166 			if (ump->um_quotas[i] == NULLVP)
   1167 				continue;
   1168 			quotaoff(p, mp, i);
   1169 		}
   1170 		/*
   1171 		 * Here we fall through to vflush again to ensure
   1172 		 * that we have gotten rid of all the system vnodes.
   1173 		 */
   1174 	}
   1175 #endif
   1176 	/*
   1177 	 * Flush all the files.
   1178 	 */
   1179 	error = vflush(mp, NULLVP, flags);
   1180 	if (error)
   1181 		return (error);
   1182 	/*
   1183 	 * Flush filesystem metadata.
   1184 	 */
   1185 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1186 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
   1187 	VOP_UNLOCK(ump->um_devvp, 0);
   1188 	return (error);
   1189 }
   1190 
   1191 /*
   1192  * Get file system statistics.
   1193  */
   1194 int
   1195 ffs_statfs(mp, sbp, p)
   1196 	struct mount *mp;
   1197 	struct statfs *sbp;
   1198 	struct proc *p;
   1199 {
   1200 	struct ufsmount *ump;
   1201 	struct fs *fs;
   1202 
   1203 	ump = VFSTOUFS(mp);
   1204 	fs = ump->um_fs;
   1205 #ifdef COMPAT_09
   1206 	sbp->f_type = 1;
   1207 #else
   1208 	sbp->f_type = 0;
   1209 #endif
   1210 	sbp->f_bsize = fs->fs_fsize;
   1211 	sbp->f_iosize = fs->fs_bsize;
   1212 	sbp->f_blocks = fs->fs_dsize;
   1213 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1214 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
   1215 	sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
   1216 	    (100 - fs->fs_minfree) / (u_int64_t) 100) -
   1217 	    (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
   1218 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1219 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1220 	copy_statfs_info(sbp, mp);
   1221 	return (0);
   1222 }
   1223 
   1224 /*
   1225  * Go through the disk queues to initiate sandbagged IO;
   1226  * go through the inodes to write those that have been modified;
   1227  * initiate the writing of the super block if it has been modified.
   1228  *
   1229  * Note: we are always called with the filesystem marked `MPBUSY'.
   1230  */
   1231 int
   1232 ffs_sync(mp, waitfor, cred, p)
   1233 	struct mount *mp;
   1234 	int waitfor;
   1235 	struct ucred *cred;
   1236 	struct proc *p;
   1237 {
   1238 	struct vnode *vp, *nvp;
   1239 	struct inode *ip;
   1240 	struct ufsmount *ump = VFSTOUFS(mp);
   1241 	struct fs *fs;
   1242 	int error, allerror = 0;
   1243 
   1244 	fs = ump->um_fs;
   1245 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1246 		printf("fs = %s\n", fs->fs_fsmnt);
   1247 		panic("update: rofs mod");
   1248 	}
   1249 	/*
   1250 	 * Write back each (modified) inode.
   1251 	 */
   1252 	simple_lock(&mntvnode_slock);
   1253 loop:
   1254 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
   1255 		/*
   1256 		 * If the vnode that we are about to sync is no longer
   1257 		 * associated with this mount point, start over.
   1258 		 */
   1259 		if (vp->v_mount != mp)
   1260 			goto loop;
   1261 		simple_lock(&vp->v_interlock);
   1262 		nvp = LIST_NEXT(vp, v_mntvnodes);
   1263 		ip = VTOI(vp);
   1264 		if (vp->v_type == VNON ||
   1265 		    ((ip->i_flag &
   1266 		      (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1267 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1268 		     vp->v_uobj.uo_npages == 0))
   1269 		{
   1270 			simple_unlock(&vp->v_interlock);
   1271 			continue;
   1272 		}
   1273 		simple_unlock(&mntvnode_slock);
   1274 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1275 		if (error) {
   1276 			simple_lock(&mntvnode_slock);
   1277 			if (error == ENOENT)
   1278 				goto loop;
   1279 			continue;
   1280 		}
   1281 		if ((error = VOP_FSYNC(vp, cred,
   1282 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1283 			allerror = error;
   1284 		vput(vp);
   1285 		simple_lock(&mntvnode_slock);
   1286 	}
   1287 	simple_unlock(&mntvnode_slock);
   1288 	/*
   1289 	 * Force stale file system control information to be flushed.
   1290 	 */
   1291 	if (waitfor != MNT_LAZY) {
   1292 		if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
   1293 			waitfor = MNT_NOWAIT;
   1294 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1295 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1296 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1297 			allerror = error;
   1298 		VOP_UNLOCK(ump->um_devvp, 0);
   1299 	}
   1300 #ifdef QUOTA
   1301 	qsync(mp);
   1302 #endif
   1303 	/*
   1304 	 * Write back modified superblock.
   1305 	 */
   1306 	if (fs->fs_fmod != 0) {
   1307 		fs->fs_fmod = 0;
   1308 		fs->fs_time = time.tv_sec;
   1309 		if ((error = ffs_cgupdate(ump, waitfor)))
   1310 			allerror = error;
   1311 	}
   1312 	return (allerror);
   1313 }
   1314 
   1315 /*
   1316  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1317  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1318  * return the inode locked.  Detection and handling of mount points must be
   1319  * done by the calling routine.
   1320  */
   1321 int
   1322 ffs_vget(mp, ino, vpp)
   1323 	struct mount *mp;
   1324 	ino_t ino;
   1325 	struct vnode **vpp;
   1326 {
   1327 	struct fs *fs;
   1328 	struct inode *ip;
   1329 	struct ufsmount *ump;
   1330 	struct buf *bp;
   1331 	struct vnode *vp;
   1332 	dev_t dev;
   1333 	int error;
   1334 
   1335 	ump = VFSTOUFS(mp);
   1336 	dev = ump->um_dev;
   1337 
   1338 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1339 		return (0);
   1340 
   1341 	/* Allocate a new vnode/inode. */
   1342 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1343 		*vpp = NULL;
   1344 		return (error);
   1345 	}
   1346 
   1347 	/*
   1348 	 * If someone beat us to it while sleeping in getnewvnode(),
   1349 	 * push back the freshly allocated vnode we don't need, and return.
   1350 	 */
   1351 
   1352 	do {
   1353 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1354 			ungetnewvnode(vp);
   1355 			return (0);
   1356 		}
   1357 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1358 
   1359 	/*
   1360 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1361 	 * XXX create another pool for MFS inodes?
   1362 	 */
   1363 
   1364 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1365 	memset(ip, 0, sizeof(struct inode));
   1366 	vp->v_data = ip;
   1367 	ip->i_vnode = vp;
   1368 	ip->i_ump = ump;
   1369 	ip->i_fs = fs = ump->um_fs;
   1370 	ip->i_dev = dev;
   1371 	ip->i_number = ino;
   1372 	LIST_INIT(&ip->i_pcbufhd);
   1373 #ifdef QUOTA
   1374 	{
   1375 		int i;
   1376 
   1377 		for (i = 0; i < MAXQUOTAS; i++)
   1378 			ip->i_dquot[i] = NODQUOT;
   1379 	}
   1380 #endif
   1381 
   1382 	/*
   1383 	 * Put it onto its hash chain and lock it so that other requests for
   1384 	 * this inode will block if they arrive while we are sleeping waiting
   1385 	 * for old data structures to be purged or for the contents of the
   1386 	 * disk portion of this inode to be read.
   1387 	 */
   1388 
   1389 	ufs_ihashins(ip);
   1390 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1391 
   1392 	/* Read in the disk contents for the inode, copy into the inode. */
   1393 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1394 		      (int)fs->fs_bsize, NOCRED, &bp);
   1395 	if (error) {
   1396 
   1397 		/*
   1398 		 * The inode does not contain anything useful, so it would
   1399 		 * be misleading to leave it on its hash chain. With mode
   1400 		 * still zero, it will be unlinked and returned to the free
   1401 		 * list by vput().
   1402 		 */
   1403 
   1404 		vput(vp);
   1405 		brelse(bp);
   1406 		*vpp = NULL;
   1407 		return (error);
   1408 	}
   1409 	if (ip->i_ump->um_fstype == UFS1)
   1410 		ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
   1411 	else
   1412 		ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
   1413 	ffs_load_inode(bp, ip, fs, ino);
   1414 	if (DOINGSOFTDEP(vp))
   1415 		softdep_load_inodeblock(ip);
   1416 	else
   1417 		ip->i_ffs_effnlink = ip->i_nlink;
   1418 	brelse(bp);
   1419 
   1420 	/*
   1421 	 * Initialize the vnode from the inode, check for aliases.
   1422 	 * Note that the underlying vnode may have changed.
   1423 	 */
   1424 
   1425 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1426 
   1427 	/*
   1428 	 * Finish inode initialization now that aliasing has been resolved.
   1429 	 */
   1430 
   1431 	genfs_node_init(vp, &ffs_genfsops);
   1432 	ip->i_devvp = ump->um_devvp;
   1433 	VREF(ip->i_devvp);
   1434 
   1435 	/*
   1436 	 * Ensure that uid and gid are correct. This is a temporary
   1437 	 * fix until fsck has been changed to do the update.
   1438 	 */
   1439 
   1440 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   1441 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   1442 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   1443 	}							/* XXX */
   1444 	uvm_vnp_setsize(vp, ip->i_size);
   1445 	*vpp = vp;
   1446 	return (0);
   1447 }
   1448 
   1449 /*
   1450  * File handle to vnode
   1451  *
   1452  * Have to be really careful about stale file handles:
   1453  * - check that the inode number is valid
   1454  * - call ffs_vget() to get the locked inode
   1455  * - check for an unallocated inode (i_mode == 0)
   1456  * - check that the given client host has export rights and return
   1457  *   those rights via. exflagsp and credanonp
   1458  */
   1459 int
   1460 ffs_fhtovp(mp, fhp, vpp)
   1461 	struct mount *mp;
   1462 	struct fid *fhp;
   1463 	struct vnode **vpp;
   1464 {
   1465 	struct ufid *ufhp;
   1466 	struct fs *fs;
   1467 
   1468 	ufhp = (struct ufid *)fhp;
   1469 	fs = VFSTOUFS(mp)->um_fs;
   1470 	if (ufhp->ufid_ino < ROOTINO ||
   1471 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1472 		return (ESTALE);
   1473 	return (ufs_fhtovp(mp, ufhp, vpp));
   1474 }
   1475 
   1476 /*
   1477  * Vnode pointer to File handle
   1478  */
   1479 /* ARGSUSED */
   1480 int
   1481 ffs_vptofh(vp, fhp)
   1482 	struct vnode *vp;
   1483 	struct fid *fhp;
   1484 {
   1485 	struct inode *ip;
   1486 	struct ufid *ufhp;
   1487 
   1488 	ip = VTOI(vp);
   1489 	ufhp = (struct ufid *)fhp;
   1490 	ufhp->ufid_len = sizeof(struct ufid);
   1491 	ufhp->ufid_ino = ip->i_number;
   1492 	ufhp->ufid_gen = ip->i_gen;
   1493 	return (0);
   1494 }
   1495 
   1496 void
   1497 ffs_init()
   1498 {
   1499 	if (ffs_initcount++ > 0)
   1500 		return;
   1501 
   1502 	softdep_initialize();
   1503 	ufs_init();
   1504 
   1505 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
   1506 	    &pool_allocator_nointr);
   1507 	pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
   1508 	    "dino1pl", &pool_allocator_nointr);
   1509 	pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
   1510 	    "dino2pl", &pool_allocator_nointr);
   1511 }
   1512 
   1513 void
   1514 ffs_reinit()
   1515 {
   1516 	softdep_reinitialize();
   1517 	ufs_reinit();
   1518 }
   1519 
   1520 void
   1521 ffs_done()
   1522 {
   1523 	if (--ffs_initcount > 0)
   1524 		return;
   1525 
   1526 	/* XXX softdep cleanup ? */
   1527 	ufs_done();
   1528 	pool_destroy(&ffs_inode_pool);
   1529 }
   1530 
   1531 int
   1532 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1533 	int *name;
   1534 	u_int namelen;
   1535 	void *oldp;
   1536 	size_t *oldlenp;
   1537 	void *newp;
   1538 	size_t newlen;
   1539 	struct proc *p;
   1540 {
   1541 	extern int doasyncfree;
   1542 	extern int ffs_log_changeopt;
   1543 
   1544 	/* all sysctl names at this level are terminal */
   1545 	if (namelen != 1)
   1546 		return (ENOTDIR);		/* overloaded */
   1547 
   1548 	switch (name[0]) {
   1549 	case FFS_ASYNCFREE:
   1550 		return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
   1551 	case FFS_LOG_CHANGEOPT:
   1552 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1553 			&ffs_log_changeopt));
   1554 	default:
   1555 		return (EOPNOTSUPP);
   1556 	}
   1557 	/* NOTREACHED */
   1558 }
   1559 
   1560 /*
   1561  * Write a superblock and associated information back to disk.
   1562  */
   1563 int
   1564 ffs_sbupdate(mp, waitfor)
   1565 	struct ufsmount *mp;
   1566 	int waitfor;
   1567 {
   1568 	struct fs *fs = mp->um_fs;
   1569 	struct buf *bp;
   1570 	int error = 0;
   1571 	u_int32_t saveflag;
   1572 
   1573 	bp = getblk(mp->um_devvp,
   1574 	    fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
   1575 	    (int)fs->fs_sbsize, 0, 0);
   1576 	saveflag = fs->fs_flags & FS_INTERNAL;
   1577 	fs->fs_flags &= ~FS_INTERNAL;
   1578 
   1579 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1) {
   1580 		printf("%s: correcting fs_sblockloc from %" PRId64 " to %d\n",
   1581 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
   1582 		fs->fs_sblockloc = SBLOCK_UFS1;
   1583 	}
   1584 
   1585 	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2) {
   1586 		printf("%s: correcting fs_sblockloc from %" PRId64 " to %d\n",
   1587 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
   1588 		fs->fs_sblockloc = SBLOCK_UFS2;
   1589 	}
   1590 
   1591 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1592 
   1593 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   1594 #ifdef FFS_EI
   1595 	if (mp->um_flags & UFS_NEEDSWAP)
   1596 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
   1597 #endif
   1598 #ifdef SUPPORT_FS_42POSTBLFMT_WRITE
   1599 	if (fs->fs_old_postblformat == FS_42POSTBLFMT)
   1600 		memcpy(&((struct fs *)bp->b_data)->fs_old_postbl_start,
   1601 		    mp->um_opostsave, 256);
   1602 #endif
   1603 	fs->fs_flags |= saveflag;
   1604 
   1605 	if (waitfor == MNT_WAIT)
   1606 		error = bwrite(bp);
   1607 	else
   1608 		bawrite(bp);
   1609 	return (error);
   1610 }
   1611 
   1612 int
   1613 ffs_cgupdate(mp, waitfor)
   1614 	struct ufsmount *mp;
   1615 	int waitfor;
   1616 {
   1617 	struct fs *fs = mp->um_fs;
   1618 	struct buf *bp;
   1619 	int blks;
   1620 	void *space;
   1621 	int i, size, error = 0, allerror = 0;
   1622 
   1623 	allerror = ffs_sbupdate(mp, waitfor);
   1624 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1625 	space = fs->fs_csp;
   1626 	for (i = 0; i < blks; i += fs->fs_frag) {
   1627 		size = fs->fs_bsize;
   1628 		if (i + fs->fs_frag > blks)
   1629 			size = (blks - i) * fs->fs_fsize;
   1630 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1631 		    size, 0, 0);
   1632 #ifdef FFS_EI
   1633 		if (mp->um_flags & UFS_NEEDSWAP)
   1634 			ffs_csum_swap((struct csum*)space,
   1635 			    (struct csum*)bp->b_data, size);
   1636 		else
   1637 #endif
   1638 			memcpy(bp->b_data, space, (u_int)size);
   1639 		space = (char *)space + size;
   1640 		if (waitfor == MNT_WAIT)
   1641 			error = bwrite(bp);
   1642 		else
   1643 			bawrite(bp);
   1644 	}
   1645 	if (!allerror && error)
   1646 		allerror = error;
   1647 	return (allerror);
   1648 }
   1649