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