Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.275.2.1.2.1
      1 /*	$NetBSD: ffs_vfsops.c,v 1.275.2.1.2.1 2012/11/01 16:45:04 matt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Wasabi Systems, Inc, and by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1989, 1991, 1993, 1994
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.275.2.1.2.1 2012/11/01 16:45:04 matt Exp $");
     65 
     66 #if defined(_KERNEL_OPT)
     67 #include "opt_ffs.h"
     68 #include "opt_quota.h"
     69 #include "opt_wapbl.h"
     70 #endif
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/namei.h>
     75 #include <sys/proc.h>
     76 #include <sys/kernel.h>
     77 #include <sys/vnode.h>
     78 #include <sys/socket.h>
     79 #include <sys/mount.h>
     80 #include <sys/buf.h>
     81 #include <sys/device.h>
     82 #include <sys/disk.h>
     83 #include <sys/mbuf.h>
     84 #include <sys/file.h>
     85 #include <sys/disklabel.h>
     86 #include <sys/ioctl.h>
     87 #include <sys/errno.h>
     88 #include <sys/kmem.h>
     89 #include <sys/pool.h>
     90 #include <sys/lock.h>
     91 #include <sys/sysctl.h>
     92 #include <sys/conf.h>
     93 #include <sys/kauth.h>
     94 #include <sys/wapbl.h>
     95 #include <sys/fstrans.h>
     96 #include <sys/module.h>
     97 
     98 #include <miscfs/genfs/genfs.h>
     99 #include <miscfs/specfs/specdev.h>
    100 
    101 #include <ufs/ufs/quota.h>
    102 #include <ufs/ufs/ufsmount.h>
    103 #include <ufs/ufs/inode.h>
    104 #include <ufs/ufs/dir.h>
    105 #include <ufs/ufs/ufs_extern.h>
    106 #include <ufs/ufs/ufs_bswap.h>
    107 #include <ufs/ufs/ufs_wapbl.h>
    108 
    109 #include <ufs/ffs/fs.h>
    110 #include <ufs/ffs/ffs_extern.h>
    111 
    112 MODULE(MODULE_CLASS_VFS, ffs, NULL);
    113 
    114 static int	ffs_vfs_fsync(vnode_t *, int);
    115 
    116 static struct sysctllog *ffs_sysctl_log;
    117 
    118 /* how many times ffs_init() was called */
    119 int ffs_initcount = 0;
    120 
    121 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
    122 extern const struct vnodeopv_desc ffs_specop_opv_desc;
    123 extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
    124 
    125 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
    126 	&ffs_vnodeop_opv_desc,
    127 	&ffs_specop_opv_desc,
    128 	&ffs_fifoop_opv_desc,
    129 	NULL,
    130 };
    131 
    132 struct vfsops ffs_vfsops = {
    133 	MOUNT_FFS,
    134 	sizeof (struct ufs_args),
    135 	ffs_mount,
    136 	ufs_start,
    137 	ffs_unmount,
    138 	ufs_root,
    139 	ufs_quotactl,
    140 	ffs_statvfs,
    141 	ffs_sync,
    142 	ffs_vget,
    143 	ffs_fhtovp,
    144 	ffs_vptofh,
    145 	ffs_init,
    146 	ffs_reinit,
    147 	ffs_done,
    148 	ffs_mountroot,
    149 	ffs_snapshot,
    150 	ffs_extattrctl,
    151 	ffs_suspendctl,
    152 	genfs_renamelock_enter,
    153 	genfs_renamelock_exit,
    154 	ffs_vfs_fsync,
    155 	ffs_vnodeopv_descs,
    156 	0,
    157 	{ NULL, NULL },
    158 };
    159 
    160 static const struct genfs_ops ffs_genfsops = {
    161 	.gop_size = ffs_gop_size,
    162 	.gop_alloc = ufs_gop_alloc,
    163 	.gop_write = genfs_gop_write,
    164 	.gop_markupdate = ufs_gop_markupdate,
    165 };
    166 
    167 static const struct ufs_ops ffs_ufsops = {
    168 	.uo_itimes = ffs_itimes,
    169 	.uo_update = ffs_update,
    170 	.uo_truncate = ffs_truncate,
    171 	.uo_valloc = ffs_valloc,
    172 	.uo_vfree = ffs_vfree,
    173 	.uo_balloc = ffs_balloc,
    174 	.uo_unmark_vnode = (void (*)(vnode_t *))nullop,
    175 };
    176 
    177 static int
    178 ffs_modcmd(modcmd_t cmd, void *arg)
    179 {
    180 	int error;
    181 
    182 #if 0
    183 	extern int doasyncfree;
    184 #endif
    185 #ifdef UFS_EXTATTR
    186 	extern int ufs_extattr_autocreate;
    187 #endif
    188 	extern int ffs_log_changeopt;
    189 
    190 	switch (cmd) {
    191 	case MODULE_CMD_INIT:
    192 		error = vfs_attach(&ffs_vfsops);
    193 		if (error != 0)
    194 			break;
    195 
    196 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    197 			       CTLFLAG_PERMANENT,
    198 			       CTLTYPE_NODE, "vfs", NULL,
    199 			       NULL, 0, NULL, 0,
    200 			       CTL_VFS, CTL_EOL);
    201 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    202 			       CTLFLAG_PERMANENT,
    203 			       CTLTYPE_NODE, "ffs",
    204 			       SYSCTL_DESCR("Berkeley Fast File System"),
    205 			       NULL, 0, NULL, 0,
    206 			       CTL_VFS, 1, CTL_EOL);
    207 		/*
    208 		 * @@@ should we even bother with these first three?
    209 		 */
    210 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    211 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    212 			       CTLTYPE_INT, "doclusterread", NULL,
    213 			       sysctl_notavail, 0, NULL, 0,
    214 			       CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
    215 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    216 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    217 			       CTLTYPE_INT, "doclusterwrite", NULL,
    218 			       sysctl_notavail, 0, NULL, 0,
    219 			       CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
    220 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    221 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    222 			       CTLTYPE_INT, "doreallocblks", NULL,
    223 			       sysctl_notavail, 0, NULL, 0,
    224 			       CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
    225 #if 0
    226 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    227 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    228 			       CTLTYPE_INT, "doasyncfree",
    229 			       SYSCTL_DESCR("Release dirty blocks asynchronously"),
    230 			       NULL, 0, &doasyncfree, 0,
    231 			       CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
    232 #endif
    233 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    234 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    235 			       CTLTYPE_INT, "log_changeopt",
    236 			       SYSCTL_DESCR("Log changes in optimization strategy"),
    237 			       NULL, 0, &ffs_log_changeopt, 0,
    238 			       CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
    239 #ifdef UFS_EXTATTR
    240 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    241 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    242 			       CTLTYPE_INT, "extattr_autocreate",
    243 			       SYSCTL_DESCR("Size of attribute for "
    244 					    "backing file autocreation"),
    245 			       NULL, 0, &ufs_extattr_autocreate, 0,
    246 			       CTL_VFS, 1, FFS_EXTATTR_AUTOCREATE, CTL_EOL);
    247 
    248 #endif /* UFS_EXTATTR */
    249 
    250 		break;
    251 	case MODULE_CMD_FINI:
    252 		error = vfs_detach(&ffs_vfsops);
    253 		if (error != 0)
    254 			break;
    255 		sysctl_teardown(&ffs_sysctl_log);
    256 		break;
    257 	default:
    258 		error = ENOTTY;
    259 		break;
    260 	}
    261 
    262 	return (error);
    263 }
    264 
    265 pool_cache_t ffs_inode_cache;
    266 pool_cache_t ffs_dinode1_cache;
    267 pool_cache_t ffs_dinode2_cache;
    268 
    269 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
    270 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
    271 
    272 /*
    273  * Called by main() when ffs is going to be mounted as root.
    274  */
    275 
    276 int
    277 ffs_mountroot(void)
    278 {
    279 	struct fs *fs;
    280 	struct mount *mp;
    281 	struct lwp *l = curlwp;			/* XXX */
    282 	struct ufsmount *ump;
    283 	int error;
    284 
    285 	if (device_class(root_device) != DV_DISK)
    286 		return (ENODEV);
    287 
    288 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
    289 		vrele(rootvp);
    290 		return (error);
    291 	}
    292 
    293 	/*
    294 	 * We always need to be able to mount the root file system.
    295 	 */
    296 	mp->mnt_flag |= MNT_FORCE;
    297 	if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
    298 		vfs_unbusy(mp, false, NULL);
    299 		vfs_destroy(mp);
    300 		return (error);
    301 	}
    302 	mp->mnt_flag &= ~MNT_FORCE;
    303 	mutex_enter(&mountlist_lock);
    304 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    305 	mutex_exit(&mountlist_lock);
    306 	ump = VFSTOUFS(mp);
    307 	fs = ump->um_fs;
    308 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    309 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    310 	(void)ffs_statvfs(mp, &mp->mnt_stat);
    311 	vfs_unbusy(mp, false, NULL);
    312 	setrootfstime((time_t)fs->fs_time);
    313 	return (0);
    314 }
    315 
    316 /*
    317  * VFS Operations.
    318  *
    319  * mount system call
    320  */
    321 int
    322 ffs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    323 {
    324 	struct lwp *l = curlwp;
    325 	struct vnode *devvp = NULL;
    326 	struct ufs_args *args = data;
    327 	struct ufsmount *ump = NULL;
    328 	struct fs *fs;
    329 	int error = 0, flags, update;
    330 	mode_t accessmode;
    331 
    332 	if (*data_len < sizeof *args)
    333 		return EINVAL;
    334 
    335 	if (mp->mnt_flag & MNT_GETARGS) {
    336 		ump = VFSTOUFS(mp);
    337 		if (ump == NULL)
    338 			return EIO;
    339 		args->fspec = NULL;
    340 		*data_len = sizeof *args;
    341 		return 0;
    342 	}
    343 
    344 	update = mp->mnt_flag & MNT_UPDATE;
    345 
    346 	/* Check arguments */
    347 	if (args->fspec != NULL) {
    348 		/*
    349 		 * Look up the name and verify that it's sane.
    350 		 */
    351 		error = namei_simple_user(args->fspec,
    352 					NSM_FOLLOW_NOEMULROOT, &devvp);
    353 		if (error != 0)
    354 			return (error);
    355 
    356 		if (!update) {
    357 			/*
    358 			 * Be sure this is a valid block device
    359 			 */
    360 			if (devvp->v_type != VBLK)
    361 				error = ENOTBLK;
    362 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    363 				error = ENXIO;
    364 		} else {
    365 			/*
    366 			 * Be sure we're still naming the same device
    367 			 * used for our initial mount
    368 			 */
    369 			ump = VFSTOUFS(mp);
    370 			if (devvp != ump->um_devvp) {
    371 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
    372 					error = EINVAL;
    373 				else {
    374 					vrele(devvp);
    375 					devvp = ump->um_devvp;
    376 					vref(devvp);
    377 				}
    378 			}
    379 		}
    380 	} else {
    381 		if (!update) {
    382 			/* New mounts must have a filename for the device */
    383 			return (EINVAL);
    384 		} else {
    385 			/* Use the extant mount */
    386 			ump = VFSTOUFS(mp);
    387 			devvp = ump->um_devvp;
    388 			vref(devvp);
    389 		}
    390 	}
    391 
    392 	/*
    393 	 * If mount by non-root, then verify that user has necessary
    394 	 * permissions on the device.
    395 	 *
    396 	 * Permission to update a mount is checked higher, so here we presume
    397 	 * updating the mount is okay (for example, as far as securelevel goes)
    398 	 * which leaves us with the normal check.
    399 	 */
    400 	if (error == 0) {
    401 		accessmode = VREAD;
    402 		if (update ?
    403 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    404 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    405 			accessmode |= VWRITE;
    406 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    407 		error = genfs_can_mount(devvp, accessmode, l->l_cred);
    408 		VOP_UNLOCK(devvp);
    409 	}
    410 
    411 	if (error) {
    412 		vrele(devvp);
    413 		return (error);
    414 	}
    415 
    416 #ifdef WAPBL
    417 	/* WAPBL can only be enabled on a r/w mount. */
    418 	if ((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) {
    419 		mp->mnt_flag &= ~MNT_LOG;
    420 	}
    421 #else /* !WAPBL */
    422 	mp->mnt_flag &= ~MNT_LOG;
    423 #endif /* !WAPBL */
    424 
    425 	if (!update) {
    426 		int xflags;
    427 
    428 		if (mp->mnt_flag & MNT_RDONLY)
    429 			xflags = FREAD;
    430 		else
    431 			xflags = FREAD | FWRITE;
    432 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    433 		error = VOP_OPEN(devvp, xflags, FSCRED);
    434 		VOP_UNLOCK(devvp);
    435 		if (error)
    436 			goto fail;
    437 		error = ffs_mountfs(devvp, mp, l);
    438 		if (error) {
    439 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    440 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
    441 			VOP_UNLOCK(devvp);
    442 			goto fail;
    443 		}
    444 
    445 		ump = VFSTOUFS(mp);
    446 		fs = ump->um_fs;
    447 	} else {
    448 		/*
    449 		 * Update the mount.
    450 		 */
    451 
    452 		/*
    453 		 * The initial mount got a reference on this
    454 		 * device, so drop the one obtained via
    455 		 * namei(), above.
    456 		 */
    457 		vrele(devvp);
    458 
    459 		ump = VFSTOUFS(mp);
    460 		fs = ump->um_fs;
    461 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    462 			/*
    463 			 * Changing from r/w to r/o
    464 			 */
    465 			flags = WRITECLOSE;
    466 			if (mp->mnt_flag & MNT_FORCE)
    467 				flags |= FORCECLOSE;
    468 			error = ffs_flushfiles(mp, flags, l);
    469 			if (error == 0)
    470 				error = UFS_WAPBL_BEGIN(mp);
    471 			if (error == 0 &&
    472 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    473 			    fs->fs_clean & FS_WASCLEAN) {
    474 				if (mp->mnt_flag & MNT_SOFTDEP)
    475 					fs->fs_flags &= ~FS_DOSOFTDEP;
    476 				fs->fs_clean = FS_ISCLEAN;
    477 				(void) ffs_sbupdate(ump, MNT_WAIT);
    478 			}
    479 			if (error == 0)
    480 				UFS_WAPBL_END(mp);
    481 			if (error)
    482 				return (error);
    483 		}
    484 
    485 #ifdef WAPBL
    486 		if ((mp->mnt_flag & MNT_LOG) == 0) {
    487 			error = ffs_wapbl_stop(mp, mp->mnt_flag & MNT_FORCE);
    488 			if (error)
    489 				return error;
    490 		}
    491 #endif /* WAPBL */
    492 
    493 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    494 			/*
    495 			 * Finish change from r/w to r/o
    496 			 */
    497 			fs->fs_ronly = 1;
    498 			fs->fs_fmod = 0;
    499 		}
    500 
    501 		if (mp->mnt_flag & MNT_RELOAD) {
    502 			error = ffs_reload(mp, l->l_cred, l);
    503 			if (error)
    504 				return (error);
    505 		}
    506 
    507 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    508 			/*
    509 			 * Changing from read-only to read/write
    510 			 */
    511 #ifndef QUOTA2
    512 			if (fs->fs_flags & FS_DOQUOTA2) {
    513 				ump->um_flags |= UFS_QUOTA2;
    514 				uprintf("%s: options QUOTA2 not enabled%s\n",
    515 				    mp->mnt_stat.f_mntonname,
    516 				    (mp->mnt_flag & MNT_FORCE) ? "" :
    517 				    ", not mounting");
    518 				return EINVAL;
    519 			}
    520 #endif
    521 			fs->fs_ronly = 0;
    522 			fs->fs_clean <<= 1;
    523 			fs->fs_fmod = 1;
    524 #ifdef WAPBL
    525 			if (fs->fs_flags & FS_DOWAPBL) {
    526 				printf("%s: replaying log to disk\n",
    527 				    mp->mnt_stat.f_mntonname);
    528 				KDASSERT(mp->mnt_wapbl_replay);
    529 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
    530 							   devvp);
    531 				if (error) {
    532 					return error;
    533 				}
    534 				wapbl_replay_stop(mp->mnt_wapbl_replay);
    535 				fs->fs_clean = FS_WASCLEAN;
    536 			}
    537 #endif /* WAPBL */
    538 			if (fs->fs_snapinum[0] != 0)
    539 				ffs_snapshot_mount(mp);
    540 		}
    541 
    542 #ifdef WAPBL
    543 		error = ffs_wapbl_start(mp);
    544 		if (error)
    545 			return error;
    546 #endif /* WAPBL */
    547 
    548 #ifdef QUOTA2
    549 		if (!fs->fs_ronly) {
    550 			error = ffs_quota2_mount(mp);
    551 			if (error) {
    552 				return error;
    553 			}
    554 		}
    555 #endif
    556 		if (args->fspec == NULL)
    557 			return 0;
    558 	}
    559 
    560 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    561 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    562 	if (error == 0)
    563 		(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    564 		    sizeof(fs->fs_fsmnt));
    565 	fs->fs_flags &= ~FS_DOSOFTDEP;
    566 	if (fs->fs_fmod != 0) {	/* XXX */
    567 		int err;
    568 
    569 		fs->fs_fmod = 0;
    570 		if (fs->fs_clean & FS_WASCLEAN)
    571 			fs->fs_time = time_second;
    572 		else {
    573 			printf("%s: file system not clean (fs_clean=%#x); "
    574 			    "please fsck(8)\n", mp->mnt_stat.f_mntfromname,
    575 			    fs->fs_clean);
    576 			printf("%s: lost blocks %" PRId64 " files %d\n",
    577 			    mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
    578 			    fs->fs_pendinginodes);
    579 		}
    580 		err = UFS_WAPBL_BEGIN(mp);
    581 		if (err == 0) {
    582 			(void) ffs_cgupdate(ump, MNT_WAIT);
    583 			UFS_WAPBL_END(mp);
    584 		}
    585 	}
    586 	if ((mp->mnt_flag & MNT_SOFTDEP) != 0) {
    587 		printf("%s: `-o softdep' is no longer supported, "
    588 		    "consider `-o log'\n", mp->mnt_stat.f_mntfromname);
    589 		mp->mnt_flag &= ~MNT_SOFTDEP;
    590 	}
    591 
    592 	return (error);
    593 
    594 fail:
    595 	vrele(devvp);
    596 	return (error);
    597 }
    598 
    599 /*
    600  * Reload all incore data for a filesystem (used after running fsck on
    601  * the root filesystem and finding things to fix). The filesystem must
    602  * be mounted read-only.
    603  *
    604  * Things to do to update the mount:
    605  *	1) invalidate all cached meta-data.
    606  *	2) re-read superblock from disk.
    607  *	3) re-read summary information from disk.
    608  *	4) invalidate all inactive vnodes.
    609  *	5) invalidate all cached file data.
    610  *	6) re-read inode data for all active vnodes.
    611  */
    612 int
    613 ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
    614 {
    615 	struct vnode *vp, *mvp, *devvp;
    616 	struct inode *ip;
    617 	void *space;
    618 	struct buf *bp;
    619 	struct fs *fs, *newfs;
    620 	struct dkwedge_info dkw;
    621 	int i, bsize, blks, error;
    622 	int32_t *lp;
    623 	struct ufsmount *ump;
    624 	daddr_t sblockloc;
    625 
    626 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    627 		return (EINVAL);
    628 
    629 	ump = VFSTOUFS(mp);
    630 	/*
    631 	 * Step 1: invalidate all cached meta-data.
    632 	 */
    633 	devvp = ump->um_devvp;
    634 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    635 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
    636 	VOP_UNLOCK(devvp);
    637 	if (error)
    638 		panic("ffs_reload: dirty1");
    639 	/*
    640 	 * Step 2: re-read superblock from disk.
    641 	 */
    642 	fs = ump->um_fs;
    643 
    644 	/* XXX we don't handle possibility that superblock moved. */
    645 	error = bread(devvp, fs->fs_sblockloc / DEV_BSIZE, fs->fs_sbsize,
    646 		      NOCRED, 0, &bp);
    647 	if (error) {
    648 		brelse(bp, 0);
    649 		return (error);
    650 	}
    651 	newfs = kmem_alloc(fs->fs_sbsize, KM_SLEEP);
    652 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
    653 #ifdef FFS_EI
    654 	if (ump->um_flags & UFS_NEEDSWAP) {
    655 		ffs_sb_swap((struct fs*)bp->b_data, newfs);
    656 		fs->fs_flags |= FS_SWAPPED;
    657 	} else
    658 #endif
    659 		fs->fs_flags &= ~FS_SWAPPED;
    660 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
    661 	     newfs->fs_magic != FS_UFS2_MAGIC)||
    662 	     newfs->fs_bsize > MAXBSIZE ||
    663 	     newfs->fs_bsize < sizeof(struct fs)) {
    664 		brelse(bp, 0);
    665 		kmem_free(newfs, fs->fs_sbsize);
    666 		return (EIO);		/* XXX needs translation */
    667 	}
    668 	/* Store off old fs_sblockloc for fs_oldfscompat_read. */
    669 	sblockloc = fs->fs_sblockloc;
    670 	/*
    671 	 * Copy pointer fields back into superblock before copying in	XXX
    672 	 * new superblock. These should really be in the ufsmount.	XXX
    673 	 * Note that important parameters (eg fs_ncg) are unchanged.
    674 	 */
    675 	newfs->fs_csp = fs->fs_csp;
    676 	newfs->fs_maxcluster = fs->fs_maxcluster;
    677 	newfs->fs_contigdirs = fs->fs_contigdirs;
    678 	newfs->fs_ronly = fs->fs_ronly;
    679 	newfs->fs_active = fs->fs_active;
    680 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    681 	brelse(bp, 0);
    682 	kmem_free(newfs, fs->fs_sbsize);
    683 
    684 	/* Recheck for apple UFS filesystem */
    685 	ump->um_flags &= ~UFS_ISAPPLEUFS;
    686 	/* First check to see if this is tagged as an Apple UFS filesystem
    687 	 * in the disklabel
    688 	 */
    689 	if (getdiskinfo(devvp, &dkw) == 0 &&
    690 	    strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
    691 		ump->um_flags |= UFS_ISAPPLEUFS;
    692 #ifdef APPLE_UFS
    693 	else {
    694 		/* Manually look for an apple ufs label, and if a valid one
    695 		 * is found, then treat it like an Apple UFS filesystem anyway
    696 		 *
    697 		 * EINVAL is most probably a blocksize or alignment problem,
    698 		 * it is unlikely that this is an Apple UFS filesystem then.
    699 		 */
    700 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
    701 			APPLEUFS_LABEL_SIZE, cred, 0, &bp);
    702 		if (error && error != EINVAL) {
    703 			brelse(bp, 0);
    704 			return (error);
    705 		}
    706 		if (error == 0) {
    707 			error = ffs_appleufs_validate(fs->fs_fsmnt,
    708 				(struct appleufslabel *)bp->b_data, NULL);
    709 			if (error == 0)
    710 				ump->um_flags |= UFS_ISAPPLEUFS;
    711 		}
    712 		brelse(bp, 0);
    713 		bp = NULL;
    714 	}
    715 #else
    716 	if (ump->um_flags & UFS_ISAPPLEUFS)
    717 		return (EIO);
    718 #endif
    719 
    720 	if (UFS_MPISAPPLEUFS(ump)) {
    721 		/* see comment about NeXT below */
    722 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    723 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
    724 		mp->mnt_iflag |= IMNT_DTYPE;
    725 	} else {
    726 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
    727 		ump->um_dirblksiz = DIRBLKSIZ;
    728 		if (ump->um_maxsymlinklen > 0)
    729 			mp->mnt_iflag |= IMNT_DTYPE;
    730 		else
    731 			mp->mnt_iflag &= ~IMNT_DTYPE;
    732 	}
    733 	ffs_oldfscompat_read(fs, ump, sblockloc);
    734 
    735 	mutex_enter(&ump->um_lock);
    736 	ump->um_maxfilesize = fs->fs_maxfilesize;
    737 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
    738 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
    739 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
    740 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
    741 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
    742 			mutex_exit(&ump->um_lock);
    743 			return (EINVAL);
    744 		}
    745 	}
    746 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    747 		fs->fs_pendingblocks = 0;
    748 		fs->fs_pendinginodes = 0;
    749 	}
    750 	mutex_exit(&ump->um_lock);
    751 
    752 	ffs_statvfs(mp, &mp->mnt_stat);
    753 	/*
    754 	 * Step 3: re-read summary information from disk.
    755 	 */
    756 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    757 	space = fs->fs_csp;
    758 	for (i = 0; i < blks; i += fs->fs_frag) {
    759 		bsize = fs->fs_bsize;
    760 		if (i + fs->fs_frag > blks)
    761 			bsize = (blks - i) * fs->fs_fsize;
    762 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), bsize,
    763 			      NOCRED, 0, &bp);
    764 		if (error) {
    765 			brelse(bp, 0);
    766 			return (error);
    767 		}
    768 #ifdef FFS_EI
    769 		if (UFS_FSNEEDSWAP(fs))
    770 			ffs_csum_swap((struct csum *)bp->b_data,
    771 			    (struct csum *)space, bsize);
    772 		else
    773 #endif
    774 			memcpy(space, bp->b_data, (size_t)bsize);
    775 		space = (char *)space + bsize;
    776 		brelse(bp, 0);
    777 	}
    778 	if (fs->fs_snapinum[0] != 0)
    779 		ffs_snapshot_mount(mp);
    780 	/*
    781 	 * We no longer know anything about clusters per cylinder group.
    782 	 */
    783 	if (fs->fs_contigsumsize > 0) {
    784 		lp = fs->fs_maxcluster;
    785 		for (i = 0; i < fs->fs_ncg; i++)
    786 			*lp++ = fs->fs_contigsumsize;
    787 	}
    788 
    789 	/* Allocate a marker vnode. */
    790 	mvp = vnalloc(mp);
    791 	/*
    792 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
    793 	 * and vclean() can be called indirectly
    794 	 */
    795 	mutex_enter(&mntvnode_lock);
    796  loop:
    797 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    798 		vmark(mvp, vp);
    799 		if (vp->v_mount != mp || vismarker(vp))
    800 			continue;
    801 		/*
    802 		 * Step 4: invalidate all inactive vnodes.
    803 		 */
    804 		if (vrecycle(vp, &mntvnode_lock, l)) {
    805 			mutex_enter(&mntvnode_lock);
    806 			(void)vunmark(mvp);
    807 			goto loop;
    808 		}
    809 		/*
    810 		 * Step 5: invalidate all cached file data.
    811 		 */
    812 		mutex_enter(vp->v_interlock);
    813 		mutex_exit(&mntvnode_lock);
    814 		if (vget(vp, LK_EXCLUSIVE)) {
    815 			(void)vunmark(mvp);
    816 			goto loop;
    817 		}
    818 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
    819 			panic("ffs_reload: dirty2");
    820 		/*
    821 		 * Step 6: re-read inode data for all active vnodes.
    822 		 */
    823 		ip = VTOI(vp);
    824 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    825 			      (int)fs->fs_bsize, NOCRED, 0, &bp);
    826 		if (error) {
    827 			brelse(bp, 0);
    828 			vput(vp);
    829 			(void)vunmark(mvp);
    830 			break;
    831 		}
    832 		ffs_load_inode(bp, ip, fs, ip->i_number);
    833 		brelse(bp, 0);
    834 		vput(vp);
    835 		mutex_enter(&mntvnode_lock);
    836 	}
    837 	mutex_exit(&mntvnode_lock);
    838 	vnfree(mvp);
    839 	return (error);
    840 }
    841 
    842 /*
    843  * Possible superblock locations ordered from most to least likely.
    844  */
    845 static const int sblock_try[] = SBLOCKSEARCH;
    846 
    847 /*
    848  * Common code for mount and mountroot
    849  */
    850 int
    851 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
    852 {
    853 	struct ufsmount *ump;
    854 	struct buf *bp;
    855 	struct fs *fs;
    856 	dev_t dev;
    857 	struct dkwedge_info dkw;
    858 	void *space;
    859 	daddr_t sblockloc, fsblockloc;
    860 	int blks, fstype;
    861 	int error, i, bsize, ronly, bset = 0;
    862 #ifdef FFS_EI
    863 	int needswap = 0;		/* keep gcc happy */
    864 #endif
    865 	int32_t *lp;
    866 	kauth_cred_t cred;
    867 	u_int32_t sbsize = 8192;	/* keep gcc happy*/
    868 	u_int32_t allocsbsize;
    869 	int32_t fsbsize;
    870 
    871 	dev = devvp->v_rdev;
    872 	cred = l ? l->l_cred : NOCRED;
    873 
    874 	/* Flush out any old buffers remaining from a previous use. */
    875 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    876 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    877 	VOP_UNLOCK(devvp);
    878 	if (error)
    879 		return (error);
    880 
    881 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    882 
    883 	bp = NULL;
    884 	ump = NULL;
    885 	fs = NULL;
    886 	sblockloc = 0;
    887 	fstype = 0;
    888 
    889 	error = fstrans_mount(mp);
    890 	if (error)
    891 		return error;
    892 
    893 	ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
    894 	mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
    895 	error = ffs_snapshot_init(ump);
    896 	if (error)
    897 		goto out;
    898 	ump->um_ops = &ffs_ufsops;
    899 
    900 #ifdef WAPBL
    901  sbagain:
    902 #endif
    903 	/*
    904 	 * Try reading the superblock in each of its possible locations.
    905 	 */
    906 	for (i = 0; ; i++) {
    907 		if (bp != NULL) {
    908 			brelse(bp, BC_NOCACHE);
    909 			bp = NULL;
    910 		}
    911 		if (sblock_try[i] == -1) {
    912 			error = EINVAL;
    913 			fs = NULL;
    914 			goto out;
    915 		}
    916 		error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, cred,
    917 			      0, &bp);
    918 		if (error) {
    919 			fs = NULL;
    920 			goto out;
    921 		}
    922 		fs = (struct fs*)bp->b_data;
    923 		fsblockloc = sblockloc = sblock_try[i];
    924 		if (fs->fs_magic == FS_UFS1_MAGIC) {
    925 			sbsize = fs->fs_sbsize;
    926 			fstype = UFS1;
    927 			fsbsize = fs->fs_bsize;
    928 #ifdef FFS_EI
    929 			needswap = 0;
    930 		} else if (fs->fs_magic == FS_UFS1_MAGIC_SWAPPED) {
    931 			sbsize = bswap32(fs->fs_sbsize);
    932 			fstype = UFS1;
    933 			fsbsize = bswap32(fs->fs_bsize);
    934 			needswap = 1;
    935 #endif
    936 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
    937 			sbsize = fs->fs_sbsize;
    938 			fstype = UFS2;
    939 			fsbsize = fs->fs_bsize;
    940 #ifdef FFS_EI
    941 			needswap = 0;
    942 		} else if (fs->fs_magic == FS_UFS2_MAGIC_SWAPPED) {
    943 			sbsize = bswap32(fs->fs_sbsize);
    944 			fstype = UFS2;
    945 			fsbsize = bswap32(fs->fs_bsize);
    946 			needswap = 1;
    947 #endif
    948 		} else
    949 			continue;
    950 
    951 
    952 		/* fs->fs_sblockloc isn't defined for old filesystems */
    953 		if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
    954 			if (sblockloc == SBLOCK_UFS2)
    955 				/*
    956 				 * This is likely to be the first alternate
    957 				 * in a filesystem with 64k blocks.
    958 				 * Don't use it.
    959 				 */
    960 				continue;
    961 			fsblockloc = sblockloc;
    962 		} else {
    963 			fsblockloc = fs->fs_sblockloc;
    964 #ifdef FFS_EI
    965 			if (needswap)
    966 				fsblockloc = bswap64(fsblockloc);
    967 #endif
    968 		}
    969 
    970 		/* Check we haven't found an alternate superblock */
    971 		if (fsblockloc != sblockloc)
    972 			continue;
    973 
    974 		/* Validate size of superblock */
    975 		if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
    976 			continue;
    977 
    978 		/* Check that we can handle the file system blocksize */
    979 		if (fsbsize > MAXBSIZE) {
    980 			printf("ffs_mountfs: block size (%d) > MAXBSIZE (%d)\n",
    981 			    fsbsize, MAXBSIZE);
    982 			continue;
    983 		}
    984 
    985 		/* Ok seems to be a good superblock */
    986 		break;
    987 	}
    988 
    989 	fs = kmem_alloc((u_long)sbsize, KM_SLEEP);
    990 	memcpy(fs, bp->b_data, sbsize);
    991 	ump->um_fs = fs;
    992 
    993 #ifdef FFS_EI
    994 	if (needswap) {
    995 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    996 		fs->fs_flags |= FS_SWAPPED;
    997 	} else
    998 #endif
    999 		fs->fs_flags &= ~FS_SWAPPED;
   1000 
   1001 #ifdef WAPBL
   1002 	if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
   1003 		error = ffs_wapbl_replay_start(mp, fs, devvp);
   1004 		if (error && (mp->mnt_flag & MNT_FORCE) == 0)
   1005 			goto out;
   1006 		if (!error) {
   1007 			if (!ronly) {
   1008 				/* XXX fsmnt may be stale. */
   1009 				printf("%s: replaying log to disk\n",
   1010 				    fs->fs_fsmnt);
   1011 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
   1012 				    devvp);
   1013 				if (error)
   1014 					goto out;
   1015 				wapbl_replay_stop(mp->mnt_wapbl_replay);
   1016 				fs->fs_clean = FS_WASCLEAN;
   1017 			} else {
   1018 				/* XXX fsmnt may be stale */
   1019 				printf("%s: replaying log to memory\n",
   1020 				    fs->fs_fsmnt);
   1021 			}
   1022 
   1023 			/* Force a re-read of the superblock */
   1024 			brelse(bp, BC_INVAL);
   1025 			bp = NULL;
   1026 			kmem_free(fs, sbsize);
   1027 			fs = NULL;
   1028 			goto sbagain;
   1029 		}
   1030 	}
   1031 #else /* !WAPBL */
   1032 	if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
   1033 		error = EPERM;
   1034 		goto out;
   1035 	}
   1036 #endif /* !WAPBL */
   1037 
   1038 	ffs_oldfscompat_read(fs, ump, sblockloc);
   1039 	ump->um_maxfilesize = fs->fs_maxfilesize;
   1040 
   1041 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
   1042 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
   1043 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
   1044 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
   1045 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
   1046 			error = EINVAL;
   1047 			goto out;
   1048 		}
   1049 	}
   1050 
   1051 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1052 		fs->fs_pendingblocks = 0;
   1053 		fs->fs_pendinginodes = 0;
   1054 	}
   1055 
   1056 	ump->um_fstype = fstype;
   1057 	if (fs->fs_sbsize < SBLOCKSIZE)
   1058 		brelse(bp, BC_INVAL);
   1059 	else
   1060 		brelse(bp, 0);
   1061 	bp = NULL;
   1062 
   1063 	/* First check to see if this is tagged as an Apple UFS filesystem
   1064 	 * in the disklabel
   1065 	 */
   1066 	if (getdiskinfo(devvp, &dkw) == 0 &&
   1067 	    strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
   1068 		ump->um_flags |= UFS_ISAPPLEUFS;
   1069 #ifdef APPLE_UFS
   1070 	else {
   1071 		/* Manually look for an apple ufs label, and if a valid one
   1072 		 * is found, then treat it like an Apple UFS filesystem anyway
   1073 		 */
   1074 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
   1075 			APPLEUFS_LABEL_SIZE, cred, 0, &bp);
   1076 		if (error)
   1077 			goto out;
   1078 		error = ffs_appleufs_validate(fs->fs_fsmnt,
   1079 			(struct appleufslabel *)bp->b_data, NULL);
   1080 		if (error == 0) {
   1081 			ump->um_flags |= UFS_ISAPPLEUFS;
   1082 		}
   1083 		brelse(bp, 0);
   1084 		bp = NULL;
   1085 	}
   1086 #else
   1087 	if (ump->um_flags & UFS_ISAPPLEUFS) {
   1088 		error = EINVAL;
   1089 		goto out;
   1090 	}
   1091 #endif
   1092 
   1093 #if 0
   1094 /*
   1095  * XXX This code changes the behaviour of mounting dirty filesystems, to
   1096  * XXX require "mount -f ..." to mount them.  This doesn't match what
   1097  * XXX mount(8) describes and is disabled for now.
   1098  */
   1099 	/*
   1100 	 * If the file system is not clean, don't allow it to be mounted
   1101 	 * unless MNT_FORCE is specified.  (Note: MNT_FORCE is always set
   1102 	 * for the root file system.)
   1103 	 */
   1104 	if (fs->fs_flags & FS_DOWAPBL) {
   1105 		/*
   1106 		 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
   1107 		 * bit is set, although there's a window in unmount where it
   1108 		 * could be FS_ISCLEAN
   1109 		 */
   1110 		if ((mp->mnt_flag & MNT_FORCE) == 0 &&
   1111 		    (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
   1112 			error = EPERM;
   1113 			goto out;
   1114 		}
   1115 	} else
   1116 		if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
   1117 		    (mp->mnt_flag & MNT_FORCE) == 0) {
   1118 			error = EPERM;
   1119 			goto out;
   1120 		}
   1121 #endif
   1122 
   1123 	/*
   1124 	 * verify that we can access the last block in the fs
   1125 	 * if we're mounting read/write.
   1126 	 */
   1127 
   1128 	if (!ronly) {
   1129 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
   1130 		    cred, 0, &bp);
   1131 		if (bp->b_bcount != fs->fs_fsize)
   1132 			error = EINVAL;
   1133 		if (error) {
   1134 			bset = BC_INVAL;
   1135 			goto out;
   1136 		}
   1137 		brelse(bp, BC_INVAL);
   1138 		bp = NULL;
   1139 	}
   1140 
   1141 	fs->fs_ronly = ronly;
   1142 	/* Don't bump fs_clean if we're replaying journal */
   1143 	if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN)))
   1144 		if (ronly == 0) {
   1145 			fs->fs_clean <<= 1;
   1146 			fs->fs_fmod = 1;
   1147 		}
   1148 	bsize = fs->fs_cssize;
   1149 	blks = howmany(bsize, fs->fs_fsize);
   1150 	if (fs->fs_contigsumsize > 0)
   1151 		bsize += fs->fs_ncg * sizeof(int32_t);
   1152 	bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1153 	allocsbsize = bsize;
   1154 	space = kmem_alloc((u_long)allocsbsize, KM_SLEEP);
   1155 	fs->fs_csp = space;
   1156 	for (i = 0; i < blks; i += fs->fs_frag) {
   1157 		bsize = fs->fs_bsize;
   1158 		if (i + fs->fs_frag > blks)
   1159 			bsize = (blks - i) * fs->fs_fsize;
   1160 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), bsize,
   1161 			      cred, 0, &bp);
   1162 		if (error) {
   1163 			kmem_free(fs->fs_csp, allocsbsize);
   1164 			goto out;
   1165 		}
   1166 #ifdef FFS_EI
   1167 		if (needswap)
   1168 			ffs_csum_swap((struct csum *)bp->b_data,
   1169 				(struct csum *)space, bsize);
   1170 		else
   1171 #endif
   1172 			memcpy(space, bp->b_data, (u_int)bsize);
   1173 
   1174 		space = (char *)space + bsize;
   1175 		brelse(bp, 0);
   1176 		bp = NULL;
   1177 	}
   1178 	if (fs->fs_contigsumsize > 0) {
   1179 		fs->fs_maxcluster = lp = space;
   1180 		for (i = 0; i < fs->fs_ncg; i++)
   1181 			*lp++ = fs->fs_contigsumsize;
   1182 		space = lp;
   1183 	}
   1184 	bsize = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1185 	fs->fs_contigdirs = space;
   1186 	space = (char *)space + bsize;
   1187 	memset(fs->fs_contigdirs, 0, bsize);
   1188 		/* Compatibility for old filesystems - XXX */
   1189 	if (fs->fs_avgfilesize <= 0)
   1190 		fs->fs_avgfilesize = AVFILESIZ;
   1191 	if (fs->fs_avgfpdir <= 0)
   1192 		fs->fs_avgfpdir = AFPDIR;
   1193 	fs->fs_active = NULL;
   1194 	mp->mnt_data = ump;
   1195 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
   1196 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
   1197 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
   1198 	mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
   1199 	if (UFS_MPISAPPLEUFS(ump)) {
   1200 		/* NeXT used to keep short symlinks in the inode even
   1201 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
   1202 		 * is probably -1, but we still need to be able to identify
   1203 		 * short symlinks.
   1204 		 */
   1205 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
   1206 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
   1207 		mp->mnt_iflag |= IMNT_DTYPE;
   1208 	} else {
   1209 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
   1210 		ump->um_dirblksiz = DIRBLKSIZ;
   1211 		if (ump->um_maxsymlinklen > 0)
   1212 			mp->mnt_iflag |= IMNT_DTYPE;
   1213 		else
   1214 			mp->mnt_iflag &= ~IMNT_DTYPE;
   1215 	}
   1216 	mp->mnt_fs_bshift = fs->fs_bshift;
   1217 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
   1218 	mp->mnt_flag |= MNT_LOCAL;
   1219 	mp->mnt_iflag |= IMNT_MPSAFE;
   1220 #ifdef FFS_EI
   1221 	if (needswap)
   1222 		ump->um_flags |= UFS_NEEDSWAP;
   1223 #endif
   1224 	ump->um_mountp = mp;
   1225 	ump->um_dev = dev;
   1226 	ump->um_devvp = devvp;
   1227 	ump->um_nindir = fs->fs_nindir;
   1228 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
   1229 	ump->um_bptrtodb = fs->fs_fshift - DEV_BSHIFT;
   1230 	ump->um_seqinc = fs->fs_frag;
   1231 	for (i = 0; i < MAXQUOTAS; i++)
   1232 		ump->um_quotas[i] = NULLVP;
   1233 	devvp->v_specmountpoint = mp;
   1234 	if (ronly == 0 && fs->fs_snapinum[0] != 0)
   1235 		ffs_snapshot_mount(mp);
   1236 #ifdef WAPBL
   1237 	if (!ronly) {
   1238 		KDASSERT(fs->fs_ronly == 0);
   1239 		/*
   1240 		 * ffs_wapbl_start() needs mp->mnt_stat initialised if it
   1241 		 * needs to create a new log file in-filesystem.
   1242 		 */
   1243 		ffs_statvfs(mp, &mp->mnt_stat);
   1244 
   1245 		error = ffs_wapbl_start(mp);
   1246 		if (error) {
   1247 			kmem_free(fs->fs_csp, allocsbsize);
   1248 			goto out;
   1249 		}
   1250 	}
   1251 #endif /* WAPBL */
   1252 	if (ronly == 0) {
   1253 #ifdef QUOTA2
   1254 		error = ffs_quota2_mount(mp);
   1255 		if (error) {
   1256 			kmem_free(fs->fs_csp, allocsbsize);
   1257 			goto out;
   1258 		}
   1259 #else
   1260 		if (fs->fs_flags & FS_DOQUOTA2) {
   1261 			ump->um_flags |= UFS_QUOTA2;
   1262 			uprintf("%s: options QUOTA2 not enabled%s\n",
   1263 			    mp->mnt_stat.f_mntonname,
   1264 			    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
   1265 			if ((mp->mnt_flag & MNT_FORCE) == 0) {
   1266 				error = EINVAL;
   1267 				kmem_free(fs->fs_csp, allocsbsize);
   1268 				goto out;
   1269 			}
   1270 		}
   1271 #endif
   1272 	 }
   1273 #ifdef UFS_EXTATTR
   1274 	/*
   1275 	 * Initialize file-backed extended attributes on UFS1 file
   1276 	 * systems.
   1277 	 */
   1278 	if (ump->um_fstype == UFS1)
   1279 		ufs_extattr_uepm_init(&ump->um_extattr);
   1280 #endif /* UFS_EXTATTR */
   1281 
   1282 	return (0);
   1283 out:
   1284 #ifdef WAPBL
   1285 	if (mp->mnt_wapbl_replay) {
   1286 		wapbl_replay_stop(mp->mnt_wapbl_replay);
   1287 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1288 		mp->mnt_wapbl_replay = 0;
   1289 	}
   1290 #endif
   1291 
   1292 	fstrans_unmount(mp);
   1293 	if (fs)
   1294 		kmem_free(fs, fs->fs_sbsize);
   1295 	devvp->v_specmountpoint = NULL;
   1296 	if (bp)
   1297 		brelse(bp, bset);
   1298 	if (ump) {
   1299 		if (ump->um_oldfscompat)
   1300 			kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t));
   1301 		mutex_destroy(&ump->um_lock);
   1302 		kmem_free(ump, sizeof(*ump));
   1303 		mp->mnt_data = NULL;
   1304 	}
   1305 	return (error);
   1306 }
   1307 
   1308 /*
   1309  * Sanity checks for loading old filesystem superblocks.
   1310  * See ffs_oldfscompat_write below for unwound actions.
   1311  *
   1312  * XXX - Parts get retired eventually.
   1313  * Unfortunately new bits get added.
   1314  */
   1315 static void
   1316 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
   1317 {
   1318 	off_t maxfilesize;
   1319 	int32_t *extrasave;
   1320 
   1321 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1322 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1323 		return;
   1324 
   1325 	if (!ump->um_oldfscompat)
   1326 		ump->um_oldfscompat = kmem_alloc(512 + 3*sizeof(int32_t),
   1327 		    KM_SLEEP);
   1328 
   1329 	memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
   1330 	extrasave = ump->um_oldfscompat;
   1331 	extrasave += 512/sizeof(int32_t);
   1332 	extrasave[0] = fs->fs_old_npsect;
   1333 	extrasave[1] = fs->fs_old_interleave;
   1334 	extrasave[2] = fs->fs_old_trackskew;
   1335 
   1336 	/* These fields will be overwritten by their
   1337 	 * original values in fs_oldfscompat_write, so it is harmless
   1338 	 * to modify them here.
   1339 	 */
   1340 	fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
   1341 	fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
   1342 	fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
   1343 	fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
   1344 
   1345 	fs->fs_maxbsize = fs->fs_bsize;
   1346 	fs->fs_time = fs->fs_old_time;
   1347 	fs->fs_size = fs->fs_old_size;
   1348 	fs->fs_dsize = fs->fs_old_dsize;
   1349 	fs->fs_csaddr = fs->fs_old_csaddr;
   1350 	fs->fs_sblockloc = sblockloc;
   1351 
   1352 	fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
   1353 
   1354 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
   1355 		fs->fs_old_nrpos = 8;
   1356 		fs->fs_old_npsect = fs->fs_old_nsect;
   1357 		fs->fs_old_interleave = 1;
   1358 		fs->fs_old_trackskew = 0;
   1359 	}
   1360 
   1361 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
   1362 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
   1363 		fs->fs_qbmask = ~fs->fs_bmask;
   1364 		fs->fs_qfmask = ~fs->fs_fmask;
   1365 	}
   1366 
   1367 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1368 	if (fs->fs_maxfilesize > maxfilesize)
   1369 		fs->fs_maxfilesize = maxfilesize;
   1370 
   1371 	/* Compatibility for old filesystems */
   1372 	if (fs->fs_avgfilesize <= 0)
   1373 		fs->fs_avgfilesize = AVFILESIZ;
   1374 	if (fs->fs_avgfpdir <= 0)
   1375 		fs->fs_avgfpdir = AFPDIR;
   1376 
   1377 #if 0
   1378 	if (bigcgs) {
   1379 		fs->fs_save_cgsize = fs->fs_cgsize;
   1380 		fs->fs_cgsize = fs->fs_bsize;
   1381 	}
   1382 #endif
   1383 }
   1384 
   1385 /*
   1386  * Unwinding superblock updates for old filesystems.
   1387  * See ffs_oldfscompat_read above for details.
   1388  *
   1389  * XXX - Parts get retired eventually.
   1390  * Unfortunately new bits get added.
   1391  */
   1392 static void
   1393 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
   1394 {
   1395 	int32_t *extrasave;
   1396 
   1397 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1398 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1399 		return;
   1400 
   1401 	fs->fs_old_time = fs->fs_time;
   1402 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1403 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1404 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1405 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1406 	fs->fs_old_flags = fs->fs_flags;
   1407 
   1408 #if 0
   1409 	if (bigcgs) {
   1410 		fs->fs_cgsize = fs->fs_save_cgsize;
   1411 	}
   1412 #endif
   1413 
   1414 	memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
   1415 	extrasave = ump->um_oldfscompat;
   1416 	extrasave += 512/sizeof(int32_t);
   1417 	fs->fs_old_npsect = extrasave[0];
   1418 	fs->fs_old_interleave = extrasave[1];
   1419 	fs->fs_old_trackskew = extrasave[2];
   1420 
   1421 }
   1422 
   1423 /*
   1424  * unmount vfs operation
   1425  */
   1426 int
   1427 ffs_unmount(struct mount *mp, int mntflags)
   1428 {
   1429 	struct lwp *l = curlwp;
   1430 	struct ufsmount *ump = VFSTOUFS(mp);
   1431 	struct fs *fs = ump->um_fs;
   1432 	int error, flags;
   1433 	u_int32_t bsize;
   1434 #ifdef WAPBL
   1435 	extern int doforce;
   1436 #endif
   1437 
   1438 	flags = 0;
   1439 	if (mntflags & MNT_FORCE)
   1440 		flags |= FORCECLOSE;
   1441 	if ((error = ffs_flushfiles(mp, flags, l)) != 0)
   1442 		return (error);
   1443 	error = UFS_WAPBL_BEGIN(mp);
   1444 	if (error == 0)
   1445 		if (fs->fs_ronly == 0 &&
   1446 		    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1447 		    fs->fs_clean & FS_WASCLEAN) {
   1448 			fs->fs_clean = FS_ISCLEAN;
   1449 			fs->fs_fmod = 0;
   1450 			(void) ffs_sbupdate(ump, MNT_WAIT);
   1451 		}
   1452 	if (error == 0)
   1453 		UFS_WAPBL_END(mp);
   1454 #ifdef WAPBL
   1455 	KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
   1456 	if (mp->mnt_wapbl_replay) {
   1457 		KDASSERT(fs->fs_ronly);
   1458 		wapbl_replay_stop(mp->mnt_wapbl_replay);
   1459 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1460 		mp->mnt_wapbl_replay = 0;
   1461 	}
   1462 	error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
   1463 	if (error) {
   1464 		return error;
   1465 	}
   1466 #endif /* WAPBL */
   1467 
   1468 	if (ump->um_devvp->v_type != VBAD)
   1469 		ump->um_devvp->v_specmountpoint = NULL;
   1470 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1471 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
   1472 		NOCRED);
   1473 	vput(ump->um_devvp);
   1474 
   1475 	bsize = fs->fs_cssize;
   1476 	if (fs->fs_contigsumsize > 0)
   1477 		bsize += fs->fs_ncg * sizeof(int32_t);
   1478 	bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1479 	kmem_free(fs->fs_csp, bsize);
   1480 
   1481 	kmem_free(fs, fs->fs_sbsize);
   1482 	if (ump->um_oldfscompat != NULL)
   1483 		kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t));
   1484 	mutex_destroy(&ump->um_lock);
   1485 	ffs_snapshot_fini(ump);
   1486 	kmem_free(ump, sizeof(*ump));
   1487 	mp->mnt_data = NULL;
   1488 	mp->mnt_flag &= ~MNT_LOCAL;
   1489 	fstrans_unmount(mp);
   1490 	return (0);
   1491 }
   1492 
   1493 /*
   1494  * Flush out all the files in a filesystem.
   1495  */
   1496 int
   1497 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
   1498 {
   1499 	extern int doforce;
   1500 	struct ufsmount *ump;
   1501 	int error;
   1502 
   1503 	if (!doforce)
   1504 		flags &= ~FORCECLOSE;
   1505 	ump = VFSTOUFS(mp);
   1506 #ifdef QUOTA
   1507 	if ((error = quota1_umount(mp, flags)) != 0)
   1508 		return (error);
   1509 #endif
   1510 #ifdef QUOTA2
   1511 	if ((error = quota2_umount(mp, flags)) != 0)
   1512 		return (error);
   1513 #endif
   1514 #ifdef UFS_EXTATTR
   1515 	if (ump->um_fstype == UFS1) {
   1516 		if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)
   1517 			ufs_extattr_stop(mp, l);
   1518 		if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED)
   1519 			ufs_extattr_uepm_destroy(&ump->um_extattr);
   1520 	}
   1521 #endif
   1522 	if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
   1523 		return (error);
   1524 	ffs_snapshot_unmount(mp);
   1525 	/*
   1526 	 * Flush all the files.
   1527 	 */
   1528 	error = vflush(mp, NULLVP, flags);
   1529 	if (error)
   1530 		return (error);
   1531 	/*
   1532 	 * Flush filesystem metadata.
   1533 	 */
   1534 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1535 	error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
   1536 	VOP_UNLOCK(ump->um_devvp);
   1537 	if (flags & FORCECLOSE) /* XXXDBJ */
   1538 		error = 0;
   1539 
   1540 #ifdef WAPBL
   1541 	if (error)
   1542 		return error;
   1543 	if (mp->mnt_wapbl) {
   1544 		error = wapbl_flush(mp->mnt_wapbl, 1);
   1545 		if (flags & FORCECLOSE)
   1546 			error = 0;
   1547 	}
   1548 #endif
   1549 
   1550 	return (error);
   1551 }
   1552 
   1553 /*
   1554  * Get file system statistics.
   1555  */
   1556 int
   1557 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
   1558 {
   1559 	struct ufsmount *ump;
   1560 	struct fs *fs;
   1561 
   1562 	ump = VFSTOUFS(mp);
   1563 	fs = ump->um_fs;
   1564 	mutex_enter(&ump->um_lock);
   1565 	sbp->f_bsize = fs->fs_bsize;
   1566 	sbp->f_frsize = fs->fs_fsize;
   1567 	sbp->f_iosize = fs->fs_bsize;
   1568 	sbp->f_blocks = fs->fs_dsize;
   1569 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1570 	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
   1571 	sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
   1572 	    fs->fs_minfree) / (u_int64_t) 100;
   1573 	if (sbp->f_bfree > sbp->f_bresvd)
   1574 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1575 	else
   1576 		sbp->f_bavail = 0;
   1577 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1578 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1579 	sbp->f_favail = sbp->f_ffree;
   1580 	sbp->f_fresvd = 0;
   1581 	mutex_exit(&ump->um_lock);
   1582 	copy_statvfs_info(sbp, mp);
   1583 
   1584 	return (0);
   1585 }
   1586 
   1587 /*
   1588  * Go through the disk queues to initiate sandbagged IO;
   1589  * go through the inodes to write those that have been modified;
   1590  * initiate the writing of the super block if it has been modified.
   1591  *
   1592  * Note: we are always called with the filesystem marked `MPBUSY'.
   1593  */
   1594 int
   1595 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
   1596 {
   1597 	struct vnode *vp, *mvp, *nvp;
   1598 	struct inode *ip;
   1599 	struct ufsmount *ump = VFSTOUFS(mp);
   1600 	struct fs *fs;
   1601 	int error, allerror = 0;
   1602 	bool is_suspending;
   1603 
   1604 	fs = ump->um_fs;
   1605 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1606 		printf("fs = %s\n", fs->fs_fsmnt);
   1607 		panic("update: rofs mod");
   1608 	}
   1609 
   1610 	/* Allocate a marker vnode. */
   1611 	mvp = vnalloc(mp);
   1612 
   1613 	fstrans_start(mp, FSTRANS_SHARED);
   1614 	is_suspending = (fstrans_getstate(mp) == FSTRANS_SUSPENDING);
   1615 	/*
   1616 	 * Write back each (modified) inode.
   1617 	 */
   1618 	mutex_enter(&mntvnode_lock);
   1619 loop:
   1620 	/*
   1621 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
   1622 	 * and vclean() can be called indirectly
   1623 	 */
   1624 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
   1625 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
   1626 		/*
   1627 		 * If the vnode that we are about to sync is no longer
   1628 		 * associated with this mount point, start over.
   1629 		 */
   1630 		if (vp->v_mount != mp)
   1631 			goto loop;
   1632 		/*
   1633 		 * Don't interfere with concurrent scans of this FS.
   1634 		 */
   1635 		if (vismarker(vp))
   1636 			continue;
   1637 		mutex_enter(vp->v_interlock);
   1638 		ip = VTOI(vp);
   1639 
   1640 		/*
   1641 		 * Skip the vnode/inode if inaccessible.
   1642 		 */
   1643 		if (ip == NULL || (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0 ||
   1644 		    vp->v_type == VNON) {
   1645 			mutex_exit(vp->v_interlock);
   1646 			continue;
   1647 		}
   1648 
   1649 		/*
   1650 		 * We deliberately update inode times here.  This will
   1651 		 * prevent a massive queue of updates accumulating, only
   1652 		 * to be handled by a call to unmount.
   1653 		 *
   1654 		 * XXX It would be better to have the syncer trickle these
   1655 		 * out.  Adjustment needed to allow registering vnodes for
   1656 		 * sync when the vnode is clean, but the inode dirty.  Or
   1657 		 * have ufs itself trickle out inode updates.
   1658 		 *
   1659 		 * If doing a lazy sync, we don't care about metadata or
   1660 		 * data updates, because they are handled by each vnode's
   1661 		 * synclist entry.  In this case we are only interested in
   1662 		 * writing back modified inodes.
   1663 		 */
   1664 		if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
   1665 		    IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1666 		    (waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1667 		    UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
   1668 			mutex_exit(vp->v_interlock);
   1669 			continue;
   1670 		}
   1671 		if (vp->v_type == VBLK && is_suspending) {
   1672 			mutex_exit(vp->v_interlock);
   1673 			continue;
   1674 		}
   1675 		vmark(mvp, vp);
   1676 		mutex_exit(&mntvnode_lock);
   1677 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT);
   1678 		if (error) {
   1679 			mutex_enter(&mntvnode_lock);
   1680 			nvp = vunmark(mvp);
   1681 			if (error == ENOENT) {
   1682 				goto loop;
   1683 			}
   1684 			continue;
   1685 		}
   1686 		if (waitfor == MNT_LAZY) {
   1687 			error = UFS_WAPBL_BEGIN(vp->v_mount);
   1688 			if (!error) {
   1689 				error = ffs_update(vp, NULL, NULL,
   1690 				    UPDATE_CLOSE);
   1691 				UFS_WAPBL_END(vp->v_mount);
   1692 			}
   1693 		} else {
   1694 			error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
   1695 			    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
   1696 		}
   1697 		if (error)
   1698 			allerror = error;
   1699 		vput(vp);
   1700 		mutex_enter(&mntvnode_lock);
   1701 		nvp = vunmark(mvp);
   1702 	}
   1703 	mutex_exit(&mntvnode_lock);
   1704 	/*
   1705 	 * Force stale file system control information to be flushed.
   1706 	 */
   1707 	if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
   1708 	    !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
   1709 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1710 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1711 		    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
   1712 		    0, 0)) != 0)
   1713 			allerror = error;
   1714 		VOP_UNLOCK(ump->um_devvp);
   1715 		if (allerror == 0 && waitfor == MNT_WAIT && !mp->mnt_wapbl) {
   1716 			mutex_enter(&mntvnode_lock);
   1717 			goto loop;
   1718 		}
   1719 	}
   1720 #if defined(QUOTA) || defined(QUOTA2)
   1721 	qsync(mp);
   1722 #endif
   1723 	/*
   1724 	 * Write back modified superblock.
   1725 	 */
   1726 	if (fs->fs_fmod != 0) {
   1727 		fs->fs_fmod = 0;
   1728 		fs->fs_time = time_second;
   1729 		error = UFS_WAPBL_BEGIN(mp);
   1730 		if (error)
   1731 			allerror = error;
   1732 		else {
   1733 			if ((error = ffs_cgupdate(ump, waitfor)))
   1734 				allerror = error;
   1735 			UFS_WAPBL_END(mp);
   1736 		}
   1737 	}
   1738 
   1739 #ifdef WAPBL
   1740 	if (mp->mnt_wapbl) {
   1741 		error = wapbl_flush(mp->mnt_wapbl, 0);
   1742 		if (error)
   1743 			allerror = error;
   1744 	}
   1745 #endif
   1746 
   1747 	fstrans_done(mp);
   1748 	vnfree(mvp);
   1749 	return (allerror);
   1750 }
   1751 
   1752 /*
   1753  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1754  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1755  * return the inode locked.  Detection and handling of mount points must be
   1756  * done by the calling routine.
   1757  */
   1758 int
   1759 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1760 {
   1761 	struct fs *fs;
   1762 	struct inode *ip;
   1763 	struct ufsmount *ump;
   1764 	struct buf *bp;
   1765 	struct vnode *vp;
   1766 	dev_t dev;
   1767 	int error;
   1768 
   1769 	ump = VFSTOUFS(mp);
   1770 	dev = ump->um_dev;
   1771 
   1772  retry:
   1773 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1774 		return (0);
   1775 
   1776 	/* Allocate a new vnode/inode. */
   1777 	error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, NULL, &vp);
   1778 	if (error) {
   1779 		*vpp = NULL;
   1780 		return (error);
   1781 	}
   1782 	ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
   1783 
   1784 	/*
   1785 	 * If someone beat us to it, put back the freshly allocated
   1786 	 * vnode/inode pair and retry.
   1787 	 */
   1788 	mutex_enter(&ufs_hashlock);
   1789 	if (ufs_ihashget(dev, ino, 0) != NULL) {
   1790 		mutex_exit(&ufs_hashlock);
   1791 		ungetnewvnode(vp);
   1792 		pool_cache_put(ffs_inode_cache, ip);
   1793 		goto retry;
   1794 	}
   1795 
   1796 	vp->v_vflag |= VV_LOCKSWORK;
   1797 
   1798 	/*
   1799 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1800 	 * XXX create another pool for MFS inodes?
   1801 	 */
   1802 
   1803 	memset(ip, 0, sizeof(struct inode));
   1804 	vp->v_data = ip;
   1805 	ip->i_vnode = vp;
   1806 	ip->i_ump = ump;
   1807 	ip->i_fs = fs = ump->um_fs;
   1808 	ip->i_dev = dev;
   1809 	ip->i_number = ino;
   1810 #if defined(QUOTA) || defined(QUOTA2)
   1811 	ufsquota_init(ip);
   1812 #endif
   1813 
   1814 	/*
   1815 	 * Initialize genfs node, we might proceed to destroy it in
   1816 	 * error branches.
   1817 	 */
   1818 	genfs_node_init(vp, &ffs_genfsops);
   1819 
   1820 	/*
   1821 	 * Put it onto its hash chain and lock it so that other requests for
   1822 	 * this inode will block if they arrive while we are sleeping waiting
   1823 	 * for old data structures to be purged or for the contents of the
   1824 	 * disk portion of this inode to be read.
   1825 	 */
   1826 
   1827 	ufs_ihashins(ip);
   1828 	mutex_exit(&ufs_hashlock);
   1829 
   1830 	/* Read in the disk contents for the inode, copy into the inode. */
   1831 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1832 		      (int)fs->fs_bsize, NOCRED, 0, &bp);
   1833 	if (error) {
   1834 
   1835 		/*
   1836 		 * The inode does not contain anything useful, so it would
   1837 		 * be misleading to leave it on its hash chain. With mode
   1838 		 * still zero, it will be unlinked and returned to the free
   1839 		 * list by vput().
   1840 		 */
   1841 
   1842 		vput(vp);
   1843 		brelse(bp, 0);
   1844 		*vpp = NULL;
   1845 		return (error);
   1846 	}
   1847 	if (ip->i_ump->um_fstype == UFS1)
   1848 		ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
   1849 		    PR_WAITOK);
   1850 	else
   1851 		ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
   1852 		    PR_WAITOK);
   1853 	ffs_load_inode(bp, ip, fs, ino);
   1854 	brelse(bp, 0);
   1855 
   1856 	/*
   1857 	 * Initialize the vnode from the inode, check for aliases.
   1858 	 * Note that the underlying vnode may have changed.
   1859 	 */
   1860 
   1861 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1862 
   1863 	/*
   1864 	 * Finish inode initialization now that aliasing has been resolved.
   1865 	 */
   1866 
   1867 	ip->i_devvp = ump->um_devvp;
   1868 	vref(ip->i_devvp);
   1869 
   1870 	/*
   1871 	 * Ensure that uid and gid are correct. This is a temporary
   1872 	 * fix until fsck has been changed to do the update.
   1873 	 */
   1874 
   1875 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   1876 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   1877 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   1878 	}							/* XXX */
   1879 	uvm_vnp_setsize(vp, ip->i_size);
   1880 	*vpp = vp;
   1881 	return (0);
   1882 }
   1883 
   1884 /*
   1885  * File handle to vnode
   1886  *
   1887  * Have to be really careful about stale file handles:
   1888  * - check that the inode number is valid
   1889  * - call ffs_vget() to get the locked inode
   1890  * - check for an unallocated inode (i_mode == 0)
   1891  * - check that the given client host has export rights and return
   1892  *   those rights via. exflagsp and credanonp
   1893  */
   1894 int
   1895 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1896 {
   1897 	struct ufid ufh;
   1898 	struct fs *fs;
   1899 
   1900 	if (fhp->fid_len != sizeof(struct ufid))
   1901 		return EINVAL;
   1902 
   1903 	memcpy(&ufh, fhp, sizeof(ufh));
   1904 	fs = VFSTOUFS(mp)->um_fs;
   1905 	if (ufh.ufid_ino < ROOTINO ||
   1906 	    ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1907 		return (ESTALE);
   1908 	return (ufs_fhtovp(mp, &ufh, vpp));
   1909 }
   1910 
   1911 /*
   1912  * Vnode pointer to File handle
   1913  */
   1914 /* ARGSUSED */
   1915 int
   1916 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1917 {
   1918 	struct inode *ip;
   1919 	struct ufid ufh;
   1920 
   1921 	if (*fh_size < sizeof(struct ufid)) {
   1922 		*fh_size = sizeof(struct ufid);
   1923 		return E2BIG;
   1924 	}
   1925 	ip = VTOI(vp);
   1926 	*fh_size = sizeof(struct ufid);
   1927 	memset(&ufh, 0, sizeof(ufh));
   1928 	ufh.ufid_len = sizeof(struct ufid);
   1929 	ufh.ufid_ino = ip->i_number;
   1930 	ufh.ufid_gen = ip->i_gen;
   1931 	memcpy(fhp, &ufh, sizeof(ufh));
   1932 	return (0);
   1933 }
   1934 
   1935 void
   1936 ffs_init(void)
   1937 {
   1938 	if (ffs_initcount++ > 0)
   1939 		return;
   1940 
   1941 	ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
   1942 	    "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
   1943 	ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
   1944 	    "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
   1945 	ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
   1946 	    "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
   1947 	ufs_init();
   1948 }
   1949 
   1950 void
   1951 ffs_reinit(void)
   1952 {
   1953 
   1954 	ufs_reinit();
   1955 }
   1956 
   1957 void
   1958 ffs_done(void)
   1959 {
   1960 	if (--ffs_initcount > 0)
   1961 		return;
   1962 
   1963 	ufs_done();
   1964 	pool_cache_destroy(ffs_dinode2_cache);
   1965 	pool_cache_destroy(ffs_dinode1_cache);
   1966 	pool_cache_destroy(ffs_inode_cache);
   1967 }
   1968 
   1969 /*
   1970  * Write a superblock and associated information back to disk.
   1971  */
   1972 int
   1973 ffs_sbupdate(struct ufsmount *mp, int waitfor)
   1974 {
   1975 	struct fs *fs = mp->um_fs;
   1976 	struct buf *bp;
   1977 	int error = 0;
   1978 	u_int32_t saveflag;
   1979 
   1980 	error = ffs_getblk(mp->um_devvp,
   1981 	    fs->fs_sblockloc / DEV_BSIZE, FFS_NOBLK,
   1982 	    fs->fs_sbsize, false, &bp);
   1983 	if (error)
   1984 		return error;
   1985 	saveflag = fs->fs_flags & FS_INTERNAL;
   1986 	fs->fs_flags &= ~FS_INTERNAL;
   1987 
   1988 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1989 
   1990 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   1991 #ifdef FFS_EI
   1992 	if (mp->um_flags & UFS_NEEDSWAP)
   1993 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
   1994 #endif
   1995 	fs->fs_flags |= saveflag;
   1996 
   1997 	if (waitfor == MNT_WAIT)
   1998 		error = bwrite(bp);
   1999 	else
   2000 		bawrite(bp);
   2001 	return (error);
   2002 }
   2003 
   2004 int
   2005 ffs_cgupdate(struct ufsmount *mp, int waitfor)
   2006 {
   2007 	struct fs *fs = mp->um_fs;
   2008 	struct buf *bp;
   2009 	int blks;
   2010 	void *space;
   2011 	int i, size, error = 0, allerror = 0;
   2012 
   2013 	allerror = ffs_sbupdate(mp, waitfor);
   2014 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   2015 	space = fs->fs_csp;
   2016 	for (i = 0; i < blks; i += fs->fs_frag) {
   2017 		size = fs->fs_bsize;
   2018 		if (i + fs->fs_frag > blks)
   2019 			size = (blks - i) * fs->fs_fsize;
   2020 		error = ffs_getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   2021 		    FFS_NOBLK, size, false, &bp);
   2022 		if (error)
   2023 			break;
   2024 #ifdef FFS_EI
   2025 		if (mp->um_flags & UFS_NEEDSWAP)
   2026 			ffs_csum_swap((struct csum*)space,
   2027 			    (struct csum*)bp->b_data, size);
   2028 		else
   2029 #endif
   2030 			memcpy(bp->b_data, space, (u_int)size);
   2031 		space = (char *)space + size;
   2032 		if (waitfor == MNT_WAIT)
   2033 			error = bwrite(bp);
   2034 		else
   2035 			bawrite(bp);
   2036 	}
   2037 	if (!allerror && error)
   2038 		allerror = error;
   2039 	return (allerror);
   2040 }
   2041 
   2042 int
   2043 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
   2044     int attrnamespace, const char *attrname)
   2045 {
   2046 #ifdef UFS_EXTATTR
   2047 	/*
   2048 	 * File-backed extended attributes are only supported on UFS1.
   2049 	 * UFS2 has native extended attributes.
   2050 	 */
   2051 	if (VFSTOUFS(mp)->um_fstype == UFS1)
   2052 		return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
   2053 #endif
   2054 	return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
   2055 }
   2056 
   2057 int
   2058 ffs_suspendctl(struct mount *mp, int cmd)
   2059 {
   2060 	int error;
   2061 	struct lwp *l = curlwp;
   2062 
   2063 	switch (cmd) {
   2064 	case SUSPEND_SUSPEND:
   2065 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
   2066 			return error;
   2067 		error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
   2068 		if (error == 0)
   2069 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
   2070 #ifdef WAPBL
   2071 		if (error == 0 && mp->mnt_wapbl)
   2072 			error = wapbl_flush(mp->mnt_wapbl, 1);
   2073 #endif
   2074 		if (error != 0) {
   2075 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
   2076 			return error;
   2077 		}
   2078 		return 0;
   2079 
   2080 	case SUSPEND_RESUME:
   2081 		return fstrans_setstate(mp, FSTRANS_NORMAL);
   2082 
   2083 	default:
   2084 		return EINVAL;
   2085 	}
   2086 }
   2087 
   2088 /*
   2089  * Synch vnode for a mounted file system.
   2090  */
   2091 static int
   2092 ffs_vfs_fsync(vnode_t *vp, int flags)
   2093 {
   2094 	int error, i, pflags;
   2095 #ifdef WAPBL
   2096 	struct mount *mp;
   2097 #endif
   2098 
   2099 	KASSERT(vp->v_type == VBLK);
   2100 	KASSERT(vp->v_specmountpoint != NULL);
   2101 
   2102 	/*
   2103 	 * Flush all dirty data associated with the vnode.
   2104 	 */
   2105 	pflags = PGO_ALLPAGES | PGO_CLEANIT;
   2106 	if ((flags & FSYNC_WAIT) != 0)
   2107 		pflags |= PGO_SYNCIO;
   2108 	mutex_enter(vp->v_interlock);
   2109 	error = VOP_PUTPAGES(vp, 0, 0, pflags);
   2110 	if (error)
   2111 		return error;
   2112 
   2113 #ifdef WAPBL
   2114 	mp = vp->v_specmountpoint;
   2115 	if (mp && mp->mnt_wapbl) {
   2116 		/*
   2117 		 * Don't bother writing out metadata if the syncer is
   2118 		 * making the request.  We will let the sync vnode
   2119 		 * write it out in a single burst through a call to
   2120 		 * VFS_SYNC().
   2121 		 */
   2122 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
   2123 			return 0;
   2124 
   2125 		/*
   2126 		 * Don't flush the log if the vnode being flushed
   2127 		 * contains no dirty buffers that could be in the log.
   2128 		 */
   2129 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
   2130 			error = wapbl_flush(mp->mnt_wapbl, 0);
   2131 			if (error)
   2132 				return error;
   2133 		}
   2134 
   2135 		if ((flags & FSYNC_WAIT) != 0) {
   2136 			mutex_enter(vp->v_interlock);
   2137 			while (vp->v_numoutput)
   2138 				cv_wait(&vp->v_cv, vp->v_interlock);
   2139 			mutex_exit(vp->v_interlock);
   2140 		}
   2141 
   2142 		return 0;
   2143 	}
   2144 #endif /* WAPBL */
   2145 
   2146 	error = vflushbuf(vp, flags);
   2147 	if (error == 0 && (flags & FSYNC_CACHE) != 0) {
   2148 		i = 1;
   2149 		(void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE,
   2150 		    kauth_cred_get());
   2151 	}
   2152 
   2153 	return error;
   2154 }
   2155