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