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