Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.380
      1  1.380       rin /*	$NetBSD: ffs_vfsops.c,v 1.380 2023/06/05 04:59:46 rin Exp $	*/
      2  1.231    simonb 
      3  1.231    simonb /*-
      4  1.245        ad  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  1.231    simonb  * All rights reserved.
      6  1.231    simonb  *
      7  1.231    simonb  * This code is derived from software contributed to The NetBSD Foundation
      8  1.245        ad  * by Wasabi Systems, Inc, and by Andrew Doran.
      9  1.231    simonb  *
     10  1.231    simonb  * Redistribution and use in source and binary forms, with or without
     11  1.231    simonb  * modification, are permitted provided that the following conditions
     12  1.231    simonb  * are met:
     13  1.231    simonb  * 1. Redistributions of source code must retain the above copyright
     14  1.231    simonb  *    notice, this list of conditions and the following disclaimer.
     15  1.231    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.231    simonb  *    notice, this list of conditions and the following disclaimer in the
     17  1.231    simonb  *    documentation and/or other materials provided with the distribution.
     18  1.231    simonb  *
     19  1.231    simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.231    simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.231    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.231    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.231    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.231    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.231    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.231    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.231    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.231    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.231    simonb  * POSSIBILITY OF SUCH DAMAGE.
     30  1.231    simonb  */
     31    1.4       cgd 
     32    1.1   mycroft /*
     33    1.1   mycroft  * Copyright (c) 1989, 1991, 1993, 1994
     34    1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     35    1.1   mycroft  *
     36    1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     37    1.1   mycroft  * modification, are permitted provided that the following conditions
     38    1.1   mycroft  * are met:
     39    1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     40    1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     41    1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     42    1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     43    1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     44  1.119       agc  * 3. Neither the name of the University nor the names of its contributors
     45    1.1   mycroft  *    may be used to endorse or promote products derived from this software
     46    1.1   mycroft  *    without specific prior written permission.
     47    1.1   mycroft  *
     48    1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49    1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50    1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51    1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52    1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53    1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54    1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55    1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56    1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57    1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58    1.1   mycroft  * SUCH DAMAGE.
     59    1.1   mycroft  *
     60   1.33      fvdl  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
     61    1.1   mycroft  */
     62   1.88     lukem 
     63   1.88     lukem #include <sys/cdefs.h>
     64  1.380       rin __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.380 2023/06/05 04:59:46 rin Exp $");
     65   1.36    scottr 
     66   1.81       mrg #if defined(_KERNEL_OPT)
     67   1.45   thorpej #include "opt_ffs.h"
     68   1.36    scottr #include "opt_quota.h"
     69  1.231    simonb #include "opt_wapbl.h"
     70   1.37    scottr #endif
     71    1.1   mycroft 
     72    1.1   mycroft #include <sys/param.h>
     73    1.1   mycroft #include <sys/systm.h>
     74    1.1   mycroft #include <sys/namei.h>
     75    1.1   mycroft #include <sys/proc.h>
     76    1.1   mycroft #include <sys/kernel.h>
     77    1.1   mycroft #include <sys/vnode.h>
     78  1.370   hannken #include <sys/fstrans.h>
     79    1.1   mycroft #include <sys/socket.h>
     80    1.1   mycroft #include <sys/mount.h>
     81    1.1   mycroft #include <sys/buf.h>
     82   1.23   thorpej #include <sys/device.h>
     83  1.270  christos #include <sys/disk.h>
     84    1.1   mycroft #include <sys/file.h>
     85    1.1   mycroft #include <sys/disklabel.h>
     86    1.1   mycroft #include <sys/ioctl.h>
     87    1.1   mycroft #include <sys/errno.h>
     88  1.273      para #include <sys/kmem.h>
     89   1.43   thorpej #include <sys/pool.h>
     90   1.29      fvdl #include <sys/lock.h>
     91   1.33      fvdl #include <sys/sysctl.h>
     92  1.101   gehenna #include <sys/conf.h>
     93  1.181      elad #include <sys/kauth.h>
     94  1.231    simonb #include <sys/wapbl.h>
     95  1.227    rumble #include <sys/module.h>
     96    1.1   mycroft 
     97  1.221  dholland #include <miscfs/genfs/genfs.h>
     98    1.1   mycroft #include <miscfs/specfs/specdev.h>
     99    1.1   mycroft 
    100    1.1   mycroft #include <ufs/ufs/quota.h>
    101    1.1   mycroft #include <ufs/ufs/ufsmount.h>
    102    1.1   mycroft #include <ufs/ufs/inode.h>
    103   1.25    bouyer #include <ufs/ufs/dir.h>
    104    1.1   mycroft #include <ufs/ufs/ufs_extern.h>
    105   1.34    bouyer #include <ufs/ufs/ufs_bswap.h>
    106  1.231    simonb #include <ufs/ufs/ufs_wapbl.h>
    107    1.1   mycroft 
    108    1.1   mycroft #include <ufs/ffs/fs.h>
    109    1.1   mycroft #include <ufs/ffs/ffs_extern.h>
    110    1.1   mycroft 
    111  1.337  pgoyette #ifdef WAPBL
    112  1.362  pgoyette MODULE(MODULE_CLASS_VFS, ffs, "ufs,wapbl");
    113  1.337  pgoyette #else
    114  1.362  pgoyette MODULE(MODULE_CLASS_VFS, ffs, "ufs");
    115  1.337  pgoyette #endif
    116  1.227    rumble 
    117  1.317      maxv static int ffs_vfs_fsync(vnode_t *, int);
    118  1.317      maxv static int ffs_superblock_validate(struct fs *);
    119  1.329      maxv static int ffs_is_appleufs(struct vnode *, struct fs *);
    120  1.242        ad 
    121  1.325   hannken static int ffs_init_vnode(struct ufsmount *, struct vnode *, ino_t);
    122  1.325   hannken static void ffs_deinit_vnode(struct ufsmount *, struct vnode *);
    123  1.325   hannken 
    124  1.276      elad static kauth_listener_t ffs_snapshot_listener;
    125  1.276      elad 
    126   1.59  jdolecek /* how many times ffs_init() was called */
    127   1.59  jdolecek int ffs_initcount = 0;
    128   1.59  jdolecek 
    129  1.303  christos #ifdef DEBUG_FFS_MOUNT
    130  1.319      maxv #define DPRINTF(_fmt, args...)	printf("%s: " _fmt "\n", __func__, ##args)
    131  1.303  christos #else
    132  1.319      maxv #define DPRINTF(_fmt, args...)	do {} while (/*CONSTCOND*/0)
    133  1.303  christos #endif
    134  1.303  christos 
    135  1.105      matt extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
    136  1.105      matt extern const struct vnodeopv_desc ffs_specop_opv_desc;
    137  1.105      matt extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
    138   1.32   thorpej 
    139   1.79  jdolecek const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
    140   1.32   thorpej 	&ffs_vnodeop_opv_desc,
    141   1.32   thorpej 	&ffs_specop_opv_desc,
    142   1.32   thorpej 	&ffs_fifoop_opv_desc,
    143   1.32   thorpej 	NULL,
    144   1.32   thorpej };
    145   1.32   thorpej 
    146   1.17   mycroft struct vfsops ffs_vfsops = {
    147  1.295   hannken 	.vfs_name = MOUNT_FFS,
    148  1.295   hannken 	.vfs_min_mount_data = sizeof (struct ufs_args),
    149  1.295   hannken 	.vfs_mount = ffs_mount,
    150  1.295   hannken 	.vfs_start = ufs_start,
    151  1.295   hannken 	.vfs_unmount = ffs_unmount,
    152  1.295   hannken 	.vfs_root = ufs_root,
    153  1.295   hannken 	.vfs_quotactl = ufs_quotactl,
    154  1.295   hannken 	.vfs_statvfs = ffs_statvfs,
    155  1.295   hannken 	.vfs_sync = ffs_sync,
    156  1.298   hannken 	.vfs_vget = ufs_vget,
    157  1.298   hannken 	.vfs_loadvnode = ffs_loadvnode,
    158  1.325   hannken 	.vfs_newvnode = ffs_newvnode,
    159  1.295   hannken 	.vfs_fhtovp = ffs_fhtovp,
    160  1.295   hannken 	.vfs_vptofh = ffs_vptofh,
    161  1.295   hannken 	.vfs_init = ffs_init,
    162  1.295   hannken 	.vfs_reinit = ffs_reinit,
    163  1.295   hannken 	.vfs_done = ffs_done,
    164  1.295   hannken 	.vfs_mountroot = ffs_mountroot,
    165  1.295   hannken 	.vfs_snapshot = ffs_snapshot,
    166  1.295   hannken 	.vfs_extattrctl = ffs_extattrctl,
    167  1.345   hannken 	.vfs_suspendctl = genfs_suspendctl,
    168  1.295   hannken 	.vfs_renamelock_enter = genfs_renamelock_enter,
    169  1.295   hannken 	.vfs_renamelock_exit = genfs_renamelock_exit,
    170  1.295   hannken 	.vfs_fsync = ffs_vfs_fsync,
    171  1.295   hannken 	.vfs_opv_descs = ffs_vnodeopv_descs
    172    1.1   mycroft };
    173    1.1   mycroft 
    174  1.165      yamt static const struct genfs_ops ffs_genfsops = {
    175  1.165      yamt 	.gop_size = ffs_gop_size,
    176  1.165      yamt 	.gop_alloc = ufs_gop_alloc,
    177  1.238   hannken 	.gop_write = genfs_gop_write,
    178  1.167      yamt 	.gop_markupdate = ufs_gop_markupdate,
    179  1.357       chs 	.gop_putrange = genfs_gop_putrange,
    180   1.87       chs };
    181   1.87       chs 
    182  1.175      yamt static const struct ufs_ops ffs_ufsops = {
    183  1.175      yamt 	.uo_itimes = ffs_itimes,
    184  1.176      yamt 	.uo_update = ffs_update,
    185  1.176      yamt 	.uo_truncate = ffs_truncate,
    186  1.176      yamt 	.uo_balloc = ffs_balloc,
    187  1.284   hannken 	.uo_snapgone = ffs_snapgone,
    188  1.326  riastrad 	.uo_bufrd = ffs_bufrd,
    189  1.326  riastrad 	.uo_bufwr = ffs_bufwr,
    190  1.175      yamt };
    191  1.175      yamt 
    192  1.227    rumble static int
    193  1.374  riastrad ffs_checkrange(struct mount *mp, ino_t ino)
    194  1.338  christos {
    195  1.338  christos 	struct fs *fs = VFSTOUFS(mp)->um_fs;
    196  1.338  christos 
    197  1.338  christos 	if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg) {
    198  1.380       rin 		DPRINTF("out of range %" PRIu64 "\n", ino);
    199  1.338  christos 		return ESTALE;
    200  1.338  christos 	}
    201  1.338  christos 
    202  1.338  christos 	/*
    203  1.338  christos 	 * Need to check if inode is initialized because ffsv2 does
    204  1.338  christos 	 * lazy initialization and we can get here from nfs_fhtovp
    205  1.338  christos 	 */
    206  1.338  christos 	if (fs->fs_magic != FS_UFS2_MAGIC)
    207  1.338  christos 		return 0;
    208  1.338  christos 
    209  1.338  christos 	struct buf *bp;
    210  1.338  christos 	int cg = ino_to_cg(fs, ino);
    211  1.338  christos 	struct ufsmount *ump = VFSTOUFS(mp);
    212  1.338  christos 
    213  1.338  christos 	int error = bread(ump->um_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
    214  1.338  christos 	    (int)fs->fs_cgsize, B_MODIFY, &bp);
    215  1.338  christos 	if (error) {
    216  1.380       rin 		DPRINTF("error %d reading cg %d ino %" PRIu64 "\n",
    217  1.380       rin 		    error, cg, ino);
    218  1.338  christos 		return error;
    219  1.338  christos 	}
    220  1.338  christos 
    221  1.338  christos 	const int needswap = UFS_FSNEEDSWAP(fs);
    222  1.338  christos 
    223  1.338  christos 	struct cg *cgp = (struct cg *)bp->b_data;
    224  1.338  christos 	if (!cg_chkmagic(cgp, needswap)) {
    225  1.338  christos 		brelse(bp, 0);
    226  1.380       rin 		DPRINTF("bad cylinder group magic cg %d ino %" PRIu64 "\n",
    227  1.380       rin 		    cg, ino);
    228  1.338  christos 		return ESTALE;
    229  1.338  christos 	}
    230  1.338  christos 
    231  1.338  christos 	int32_t initediblk = ufs_rw32(cgp->cg_initediblk, needswap);
    232  1.338  christos 	brelse(bp, 0);
    233  1.338  christos 
    234  1.338  christos 	if (cg * fs->fs_ipg + initediblk < ino) {
    235  1.380       rin 		DPRINTF("cg=%d fs->fs_ipg=%d initediblk=%d ino=%" PRIu64 "\n",
    236  1.338  christos 		    cg, fs->fs_ipg, initediblk, ino);
    237  1.338  christos 		return ESTALE;
    238  1.338  christos 	}
    239  1.338  christos 	return 0;
    240  1.338  christos }
    241  1.338  christos 
    242  1.338  christos static int
    243  1.276      elad ffs_snapshot_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    244  1.276      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    245  1.276      elad {
    246  1.276      elad 	vnode_t *vp = arg2;
    247  1.300     njoly 	int result = KAUTH_RESULT_DEFER;
    248  1.276      elad 
    249  1.276      elad 	if (action != KAUTH_SYSTEM_FS_SNAPSHOT)
    250  1.276      elad 		return result;
    251  1.276      elad 
    252  1.276      elad 	if (VTOI(vp)->i_uid == kauth_cred_geteuid(cred))
    253  1.276      elad 		result = KAUTH_RESULT_ALLOW;
    254  1.276      elad 
    255  1.276      elad 	return result;
    256  1.276      elad }
    257  1.276      elad 
    258  1.366  pgoyette SYSCTL_SETUP(ffs_sysctl_setup, "ffs sysctls")
    259  1.366  pgoyette {
    260  1.366  pgoyette #ifdef UFS_EXTATTR
    261  1.366  pgoyette 	extern int ufs_extattr_autocreate;
    262  1.366  pgoyette #endif
    263  1.366  pgoyette 	extern int ffs_log_changeopt;
    264  1.366  pgoyette 
    265  1.366  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    266  1.366  pgoyette 		       CTLFLAG_PERMANENT,
    267  1.366  pgoyette 		       CTLTYPE_NODE, "ffs",
    268  1.366  pgoyette 		       SYSCTL_DESCR("Berkeley Fast File System"),
    269  1.366  pgoyette 		       NULL, 0, NULL, 0,
    270  1.366  pgoyette 		       CTL_VFS, 1, CTL_EOL);
    271  1.366  pgoyette 	/*
    272  1.366  pgoyette 	 * @@@ should we even bother with these first three?
    273  1.366  pgoyette 	 */
    274  1.366  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    275  1.366  pgoyette 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    276  1.366  pgoyette 		       CTLTYPE_INT, "doclusterread", NULL,
    277  1.366  pgoyette 		       sysctl_notavail, 0, NULL, 0,
    278  1.366  pgoyette 		       CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
    279  1.366  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    280  1.366  pgoyette 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    281  1.366  pgoyette 		       CTLTYPE_INT, "doclusterwrite", NULL,
    282  1.366  pgoyette 		       sysctl_notavail, 0, NULL, 0,
    283  1.366  pgoyette 		       CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
    284  1.366  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    285  1.366  pgoyette 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    286  1.366  pgoyette 		       CTLTYPE_INT, "doreallocblks", NULL,
    287  1.366  pgoyette 		       sysctl_notavail, 0, NULL, 0,
    288  1.366  pgoyette 		       CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
    289  1.366  pgoyette #if 0
    290  1.366  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    291  1.366  pgoyette 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    292  1.366  pgoyette 		       CTLTYPE_INT, "doasyncfree",
    293  1.366  pgoyette 		       SYSCTL_DESCR("Release dirty blocks asynchronously"),
    294  1.366  pgoyette 		       NULL, 0, &doasyncfree, 0,
    295  1.366  pgoyette 		       CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
    296  1.366  pgoyette #endif
    297  1.366  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    298  1.366  pgoyette 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    299  1.366  pgoyette 		       CTLTYPE_INT, "log_changeopt",
    300  1.366  pgoyette 		       SYSCTL_DESCR("Log changes in optimization strategy"),
    301  1.366  pgoyette 		       NULL, 0, &ffs_log_changeopt, 0,
    302  1.366  pgoyette 		       CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
    303  1.366  pgoyette #ifdef UFS_EXTATTR
    304  1.366  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    305  1.366  pgoyette 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    306  1.366  pgoyette 		       CTLTYPE_INT, "extattr_autocreate",
    307  1.366  pgoyette 		       SYSCTL_DESCR("Size of attribute for "
    308  1.366  pgoyette 				    "backing file autocreation"),
    309  1.366  pgoyette 		       NULL, 0, &ufs_extattr_autocreate, 0,
    310  1.366  pgoyette 		       CTL_VFS, 1, FFS_EXTATTR_AUTOCREATE, CTL_EOL);
    311  1.366  pgoyette 
    312  1.366  pgoyette #endif /* UFS_EXTATTR */
    313  1.366  pgoyette }
    314  1.366  pgoyette 
    315  1.276      elad static int
    316  1.227    rumble ffs_modcmd(modcmd_t cmd, void *arg)
    317  1.227    rumble {
    318  1.230    rumble 	int error;
    319  1.230    rumble 
    320  1.230    rumble #if 0
    321  1.230    rumble 	extern int doasyncfree;
    322  1.230    rumble #endif
    323  1.227    rumble 
    324  1.227    rumble 	switch (cmd) {
    325  1.227    rumble 	case MODULE_CMD_INIT:
    326  1.230    rumble 		error = vfs_attach(&ffs_vfsops);
    327  1.230    rumble 		if (error != 0)
    328  1.230    rumble 			break;
    329  1.230    rumble 
    330  1.276      elad 		ffs_snapshot_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    331  1.276      elad 		    ffs_snapshot_cb, NULL);
    332  1.276      elad 		if (ffs_snapshot_listener == NULL)
    333  1.276      elad 			printf("ffs_modcmd: can't listen on system scope.\n");
    334  1.276      elad 
    335  1.230    rumble 		break;
    336  1.227    rumble 	case MODULE_CMD_FINI:
    337  1.230    rumble 		error = vfs_detach(&ffs_vfsops);
    338  1.230    rumble 		if (error != 0)
    339  1.230    rumble 			break;
    340  1.276      elad 		if (ffs_snapshot_listener != NULL)
    341  1.276      elad 			kauth_unlisten_scope(ffs_snapshot_listener);
    342  1.230    rumble 		break;
    343  1.227    rumble 	default:
    344  1.230    rumble 		error = ENOTTY;
    345  1.230    rumble 		break;
    346  1.227    rumble 	}
    347  1.230    rumble 
    348  1.230    rumble 	return (error);
    349  1.227    rumble }
    350  1.227    rumble 
    351  1.216        ad pool_cache_t ffs_inode_cache;
    352  1.216        ad pool_cache_t ffs_dinode1_cache;
    353  1.216        ad pool_cache_t ffs_dinode2_cache;
    354  1.110      fvdl 
    355  1.146    simonb static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
    356  1.110      fvdl static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
    357   1.43   thorpej 
    358    1.1   mycroft /*
    359   1.33      fvdl  * Called by main() when ffs is going to be mounted as root.
    360    1.1   mycroft  */
    361    1.1   mycroft 
    362   1.19  christos int
    363  1.166   thorpej ffs_mountroot(void)
    364    1.1   mycroft {
    365   1.33      fvdl 	struct fs *fs;
    366   1.33      fvdl 	struct mount *mp;
    367  1.177  christos 	struct lwp *l = curlwp;			/* XXX */
    368    1.1   mycroft 	struct ufsmount *ump;
    369    1.1   mycroft 	int error;
    370   1.23   thorpej 
    371  1.180   thorpej 	if (device_class(root_device) != DV_DISK)
    372   1.23   thorpej 		return (ENODEV);
    373   1.23   thorpej 
    374   1.51  wrstuden 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
    375   1.51  wrstuden 		vrele(rootvp);
    376   1.33      fvdl 		return (error);
    377   1.51  wrstuden 	}
    378  1.231    simonb 
    379  1.231    simonb 	/*
    380  1.231    simonb 	 * We always need to be able to mount the root file system.
    381  1.231    simonb 	 */
    382  1.231    simonb 	mp->mnt_flag |= MNT_FORCE;
    383  1.177  christos 	if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
    384  1.353   hannken 		vfs_unbusy(mp);
    385  1.352   hannken 		vfs_rele(mp);
    386    1.1   mycroft 		return (error);
    387    1.1   mycroft 	}
    388  1.231    simonb 	mp->mnt_flag &= ~MNT_FORCE;
    389  1.291  christos 	mountlist_append(mp);
    390    1.1   mycroft 	ump = VFSTOUFS(mp);
    391    1.1   mycroft 	fs = ump->um_fs;
    392   1.42     perry 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    393   1.33      fvdl 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    394  1.211     pooka 	(void)ffs_statvfs(mp, &mp->mnt_stat);
    395  1.353   hannken 	vfs_unbusy(mp);
    396  1.151        pk 	setrootfstime((time_t)fs->fs_time);
    397    1.1   mycroft 	return (0);
    398    1.1   mycroft }
    399    1.1   mycroft 
    400  1.379       chs static int
    401  1.369  christos ffs_acls(struct mount *mp, int fs_flags)
    402  1.369  christos {
    403  1.379       chs 	struct ufsmount *ump;
    404  1.369  christos 
    405  1.379       chs 	ump = VFSTOUFS(mp);
    406  1.379       chs 	if (ump->um_fstype == UFS2 && (ump->um_flags & UFS_EA) == 0 &&
    407  1.379       chs 	    ((mp->mnt_flag & (MNT_POSIX1EACLS | MNT_NFS4ACLS)) != 0 ||
    408  1.379       chs 	     (fs_flags & (FS_POSIX1EACLS | FS_NFS4ACLS)) != 0)) {
    409  1.379       chs 		printf("%s: ACLs requested but not supported by this fs\n",
    410  1.379       chs 		       mp->mnt_stat.f_mntonname);
    411  1.379       chs 		return EINVAL;
    412  1.369  christos 	}
    413  1.379       chs 
    414  1.369  christos 	if ((fs_flags & FS_POSIX1EACLS) != 0) {
    415  1.369  christos #ifdef UFS_ACL
    416  1.373  christos 		if (mp->mnt_flag & MNT_NFS4ACLS)
    417  1.379       chs 			printf("WARNING: %s: POSIX.1e ACLs flag on fs conflicts "
    418  1.379       chs 			    "with \"nfsv4acls\" mount option; option ignored\n",
    419  1.369  christos 			    mp->mnt_stat.f_mntonname);
    420  1.373  christos 		mp->mnt_flag &= ~MNT_NFS4ACLS;
    421  1.371  christos 		mp->mnt_flag |= MNT_POSIX1EACLS;
    422  1.369  christos #else
    423  1.369  christos 		printf("WARNING: %s: POSIX.1e ACLs flag on fs but no "
    424  1.369  christos 		    "ACLs support\n", mp->mnt_stat.f_mntonname);
    425  1.369  christos #endif
    426  1.369  christos 	}
    427  1.379       chs 	if ((fs_flags & FS_NFS4ACLS) != 0) {
    428  1.379       chs #ifdef UFS_ACL
    429  1.379       chs 		if (mp->mnt_flag & MNT_POSIX1EACLS)
    430  1.379       chs 			printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
    431  1.379       chs 			    "with \"posix1eacls\" mount option; option ignored\n",
    432  1.379       chs 			    mp->mnt_stat.f_mntonname);
    433  1.379       chs 		mp->mnt_flag &= ~MNT_POSIX1EACLS;
    434  1.379       chs 		mp->mnt_flag |= MNT_NFS4ACLS;
    435  1.369  christos 
    436  1.379       chs #else
    437  1.379       chs 		printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
    438  1.379       chs 		    "ACLs support\n", mp->mnt_stat.f_mntonname);
    439  1.379       chs #endif
    440  1.379       chs 	}
    441  1.373  christos 	if ((mp->mnt_flag & (MNT_NFS4ACLS | MNT_POSIX1EACLS))
    442  1.373  christos 	    == (MNT_NFS4ACLS | MNT_POSIX1EACLS))
    443  1.369  christos 	{
    444  1.379       chs 		printf("%s: \"posix1eacls\" and \"nfsv4acls\" options "
    445  1.379       chs 		       "are mutually exclusive\n",
    446  1.369  christos 		    mp->mnt_stat.f_mntonname);
    447  1.379       chs 		return EINVAL;
    448  1.369  christos 	}
    449  1.369  christos 
    450  1.373  christos 	if (mp->mnt_flag & (MNT_NFS4ACLS | MNT_POSIX1EACLS))
    451  1.369  christos 		mp->mnt_iflag &= ~(IMNT_SHRLOOKUP|IMNT_NCLOOKUP);
    452  1.369  christos 	else
    453  1.369  christos 		mp->mnt_iflag |= IMNT_SHRLOOKUP|IMNT_NCLOOKUP;
    454  1.379       chs 	return 0;
    455  1.369  christos }
    456  1.369  christos 
    457    1.1   mycroft /*
    458    1.1   mycroft  * VFS Operations.
    459    1.1   mycroft  *
    460    1.1   mycroft  * mount system call
    461    1.1   mycroft  */
    462    1.1   mycroft int
    463  1.211     pooka ffs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    464    1.1   mycroft {
    465  1.211     pooka 	struct lwp *l = curlwp;
    466  1.243        ad 	struct vnode *devvp = NULL;
    467  1.204       dsl 	struct ufs_args *args = data;
    468   1.19  christos 	struct ufsmount *ump = NULL;
    469   1.61  augustss 	struct fs *fs;
    470  1.204       dsl 	int error = 0, flags, update;
    471    1.9   mycroft 	mode_t accessmode;
    472    1.1   mycroft 
    473  1.303  christos 	if (args == NULL) {
    474  1.319      maxv 		DPRINTF("NULL args");
    475  1.297      maxv 		return EINVAL;
    476  1.303  christos 	}
    477  1.303  christos 	if (*data_len < sizeof(*args)) {
    478  1.319      maxv 		DPRINTF("bad size args %zu != %zu", *data_len, sizeof(*args));
    479  1.204       dsl 		return EINVAL;
    480  1.303  christos 	}
    481  1.204       dsl 
    482  1.355  christos 	ump = VFSTOUFS(mp);
    483  1.355  christos 	if ((mp->mnt_flag & (MNT_GETARGS|MNT_UPDATE)) && ump == NULL) {
    484  1.355  christos 		DPRINTF("no ump");
    485  1.355  christos 		return EIO;
    486  1.355  christos 	}
    487  1.355  christos 
    488  1.102  christos 	if (mp->mnt_flag & MNT_GETARGS) {
    489  1.204       dsl 		args->fspec = NULL;
    490  1.204       dsl 		*data_len = sizeof *args;
    491  1.204       dsl 		return 0;
    492  1.102  christos 	}
    493   1.66      matt 
    494   1.95  christos 	update = mp->mnt_flag & MNT_UPDATE;
    495   1.95  christos 
    496   1.95  christos 	/* Check arguments */
    497  1.355  christos 	if (args->fspec == NULL) {
    498  1.355  christos 		if (!update) {
    499  1.355  christos 			/* New mounts must have a filename for the device */
    500  1.355  christos 			DPRINTF("no filename for mount");
    501  1.355  christos 			return EINVAL;
    502  1.355  christos 		}
    503  1.355  christos 	} else {
    504   1.95  christos 		/*
    505   1.95  christos 		 * Look up the name and verify that it's sane.
    506   1.95  christos 		 */
    507  1.247  dholland 		error = namei_simple_user(args->fspec,
    508  1.303  christos 		    NSM_FOLLOW_NOEMULROOT, &devvp);
    509  1.303  christos 		if (error != 0) {
    510  1.319      maxv 			DPRINTF("namei_simple_user returned %d", error);
    511  1.303  christos 			return error;
    512  1.303  christos 		}
    513   1.95  christos 
    514  1.355  christos 		/*
    515  1.355  christos 		 * Be sure this is a valid block device
    516  1.355  christos 		 */
    517  1.355  christos 		if (devvp->v_type != VBLK) {
    518  1.355  christos 			DPRINTF("non block device %d", devvp->v_type);
    519  1.355  christos 			error = ENOTBLK;
    520  1.355  christos 			goto fail;
    521  1.355  christos 		}
    522  1.355  christos 
    523  1.355  christos 		if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    524  1.355  christos 			DPRINTF("can't find block device 0x%jx",
    525  1.355  christos 			    devvp->v_rdev);
    526  1.355  christos 			error = ENXIO;
    527  1.355  christos 			goto fail;
    528  1.355  christos 		}
    529  1.355  christos 
    530  1.355  christos 		if (update) {
    531   1.95  christos 			/*
    532   1.95  christos 			 * Be sure we're still naming the same device
    533   1.95  christos 			 * used for our initial mount
    534   1.95  christos 			 */
    535  1.355  christos 			if (devvp != ump->um_devvp &&
    536  1.355  christos 			    devvp->v_rdev != ump->um_devvp->v_rdev) {
    537  1.355  christos 				DPRINTF("wrong device 0x%jx != 0x%jx",
    538  1.355  christos 				    (uintmax_t)devvp->v_rdev,
    539  1.355  christos 				    (uintmax_t)ump->um_devvp->v_rdev);
    540  1.355  christos 				error = EINVAL;
    541  1.355  christos 				goto fail;
    542  1.186       jld 			}
    543  1.355  christos 			vrele(devvp);
    544  1.355  christos 			devvp = NULL;
    545   1.95  christos 		}
    546  1.355  christos 	}
    547  1.355  christos 
    548  1.355  christos 	if (devvp == NULL) {
    549  1.355  christos 		devvp = ump->um_devvp;
    550  1.355  christos 		vref(devvp);
    551   1.95  christos 	}
    552  1.218        ad 
    553  1.218        ad 	/*
    554   1.95  christos 	 * If mount by non-root, then verify that user has necessary
    555   1.95  christos 	 * permissions on the device.
    556  1.246      elad 	 *
    557  1.246      elad 	 * Permission to update a mount is checked higher, so here we presume
    558  1.246      elad 	 * updating the mount is okay (for example, as far as securelevel goes)
    559  1.246      elad 	 * which leaves us with the normal check.
    560  1.246      elad 	 */
    561  1.355  christos 	accessmode = VREAD;
    562  1.355  christos 	if (update ? (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    563  1.355  christos 	    (mp->mnt_flag & MNT_RDONLY) == 0)
    564  1.355  christos 		accessmode |= VWRITE;
    565  1.355  christos 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    566  1.355  christos 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    567  1.355  christos 	    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(accessmode));
    568  1.355  christos 	VOP_UNLOCK(devvp);
    569   1.95  christos 	if (error) {
    570  1.355  christos 		DPRINTF("kauth returned %d", error);
    571  1.355  christos 		goto fail;
    572   1.95  christos 	}
    573   1.95  christos 
    574  1.231    simonb #ifdef WAPBL
    575  1.243        ad 	/* WAPBL can only be enabled on a r/w mount. */
    576  1.349   hannken 	if (((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) ||
    577  1.349   hannken 	    (mp->mnt_iflag & IMNT_WANTRDONLY)) {
    578  1.231    simonb 		mp->mnt_flag &= ~MNT_LOG;
    579  1.231    simonb 	}
    580  1.231    simonb #else /* !WAPBL */
    581  1.231    simonb 	mp->mnt_flag &= ~MNT_LOG;
    582  1.231    simonb #endif /* !WAPBL */
    583  1.231    simonb 
    584  1.379       chs 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    585  1.379       chs 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    586  1.379       chs 	if (error)
    587  1.379       chs 		goto fail;
    588  1.379       chs 
    589   1.95  christos 	if (!update) {
    590  1.164  christos 		int xflags;
    591  1.159   mycroft 
    592  1.159   mycroft 		if (mp->mnt_flag & MNT_RDONLY)
    593  1.164  christos 			xflags = FREAD;
    594  1.159   mycroft 		else
    595  1.231    simonb 			xflags = FREAD | FWRITE;
    596  1.271   hannken 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    597  1.211     pooka 		error = VOP_OPEN(devvp, xflags, FSCRED);
    598  1.271   hannken 		VOP_UNLOCK(devvp);
    599  1.303  christos 		if (error) {
    600  1.319      maxv 			DPRINTF("VOP_OPEN returned %d", error);
    601  1.159   mycroft 			goto fail;
    602  1.303  christos 		}
    603  1.370   hannken 		/* Need fstrans_start() for assertion in ufs_strategy(). */
    604  1.370   hannken 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    605  1.370   hannken 			fstrans_start(mp);
    606  1.177  christos 		error = ffs_mountfs(devvp, mp, l);
    607  1.370   hannken 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    608  1.370   hannken 			fstrans_done(mp);
    609   1.95  christos 		if (error) {
    610  1.319      maxv 			DPRINTF("ffs_mountfs returned %d", error);
    611  1.159   mycroft 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    612  1.211     pooka 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
    613  1.259   hannken 			VOP_UNLOCK(devvp);
    614  1.159   mycroft 			goto fail;
    615   1.95  christos 		}
    616   1.95  christos 
    617    1.1   mycroft 		ump = VFSTOUFS(mp);
    618    1.1   mycroft 		fs = ump->um_fs;
    619   1.95  christos 	} else {
    620   1.95  christos 		/*
    621   1.96  christos 		 * Update the mount.
    622   1.96  christos 		 */
    623   1.96  christos 
    624   1.96  christos 		/*
    625   1.96  christos 		 * The initial mount got a reference on this
    626   1.96  christos 		 * device, so drop the one obtained via
    627   1.96  christos 		 * namei(), above.
    628   1.95  christos 		 */
    629   1.96  christos 		vrele(devvp);
    630   1.96  christos 
    631  1.160   mycroft 		ump = VFSTOUFS(mp);
    632   1.95  christos 		fs = ump->um_fs;
    633  1.348   hannken 		if (fs->fs_ronly == 0 && (mp->mnt_iflag & IMNT_WANTRDONLY)) {
    634   1.95  christos 			/*
    635   1.95  christos 			 * Changing from r/w to r/o
    636   1.95  christos 			 */
    637    1.1   mycroft 			flags = WRITECLOSE;
    638    1.1   mycroft 			if (mp->mnt_flag & MNT_FORCE)
    639    1.1   mycroft 				flags |= FORCECLOSE;
    640  1.243        ad 			error = ffs_flushfiles(mp, flags, l);
    641  1.350  jdolecek 			if (error)
    642  1.350  jdolecek 				return error;
    643  1.350  jdolecek 
    644  1.350  jdolecek 			error = UFS_WAPBL_BEGIN(mp);
    645  1.350  jdolecek 			if (error) {
    646  1.350  jdolecek 				DPRINTF("wapbl %d", error);
    647  1.350  jdolecek 				return error;
    648  1.350  jdolecek 			}
    649  1.350  jdolecek 
    650  1.350  jdolecek 			if (ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    651   1.15   mycroft 			    fs->fs_clean & FS_WASCLEAN) {
    652   1.65      fvdl 				if (mp->mnt_flag & MNT_SOFTDEP)
    653   1.65      fvdl 					fs->fs_flags &= ~FS_DOSOFTDEP;
    654   1.15   mycroft 				fs->fs_clean = FS_ISCLEAN;
    655   1.15   mycroft 				(void) ffs_sbupdate(ump, MNT_WAIT);
    656   1.15   mycroft 			}
    657  1.350  jdolecek 
    658  1.303  christos 			UFS_WAPBL_END(mp);
    659  1.231    simonb 		}
    660  1.231    simonb 
    661  1.231    simonb #ifdef WAPBL
    662  1.231    simonb 		if ((mp->mnt_flag & MNT_LOG) == 0) {
    663  1.231    simonb 			error = ffs_wapbl_stop(mp, mp->mnt_flag & MNT_FORCE);
    664  1.303  christos 			if (error) {
    665  1.319      maxv 				DPRINTF("ffs_wapbl_stop returned %d", error);
    666  1.231    simonb 				return error;
    667  1.303  christos 			}
    668  1.231    simonb 		}
    669  1.231    simonb #endif /* WAPBL */
    670  1.231    simonb 
    671  1.348   hannken 		if (fs->fs_ronly == 0 && (mp->mnt_iflag & IMNT_WANTRDONLY)) {
    672  1.231    simonb 			/*
    673  1.231    simonb 			 * Finish change from r/w to r/o
    674  1.231    simonb 			 */
    675   1.15   mycroft 			fs->fs_ronly = 1;
    676   1.78   mycroft 			fs->fs_fmod = 0;
    677    1.1   mycroft 		}
    678   1.65      fvdl 
    679  1.379       chs 		error = ffs_acls(mp, fs->fs_flags);
    680  1.379       chs 		if (error)
    681  1.379       chs 			return error;
    682   1.15   mycroft 		if (mp->mnt_flag & MNT_RELOAD) {
    683  1.184        ad 			error = ffs_reload(mp, l->l_cred, l);
    684  1.303  christos 			if (error) {
    685  1.319      maxv 				DPRINTF("ffs_reload returned %d", error);
    686  1.303  christos 				return error;
    687  1.303  christos 			}
    688   1.15   mycroft 		}
    689   1.95  christos 
    690  1.124       dbj 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    691    1.9   mycroft 			/*
    692   1.95  christos 			 * Changing from read-only to read/write
    693    1.9   mycroft 			 */
    694  1.264    bouyer #ifndef QUOTA2
    695  1.264    bouyer 			if (fs->fs_flags & FS_DOQUOTA2) {
    696  1.264    bouyer 				ump->um_flags |= UFS_QUOTA2;
    697  1.264    bouyer 				uprintf("%s: options QUOTA2 not enabled%s\n",
    698  1.264    bouyer 				    mp->mnt_stat.f_mntonname,
    699  1.264    bouyer 				    (mp->mnt_flag & MNT_FORCE) ? "" :
    700  1.264    bouyer 				    ", not mounting");
    701  1.319      maxv 				DPRINTF("ffs_quota2 %d", EINVAL);
    702  1.264    bouyer 				return EINVAL;
    703  1.264    bouyer 			}
    704  1.264    bouyer #endif
    705    1.1   mycroft 			fs->fs_ronly = 0;
    706  1.377   hannken 			fs->fs_clean =
    707  1.377   hannken 			    fs->fs_clean == FS_ISCLEAN ? FS_WASCLEAN : 0;
    708   1.15   mycroft 			fs->fs_fmod = 1;
    709  1.231    simonb #ifdef WAPBL
    710  1.231    simonb 			if (fs->fs_flags & FS_DOWAPBL) {
    711  1.305  christos 				const char *nm = mp->mnt_stat.f_mntonname;
    712  1.305  christos 				if (!mp->mnt_wapbl_replay) {
    713  1.305  christos 					printf("%s: log corrupted;"
    714  1.305  christos 					    " replay cancelled\n", nm);
    715  1.305  christos 					return EFTYPE;
    716  1.305  christos 				}
    717  1.305  christos 				printf("%s: replaying log to disk\n", nm);
    718  1.231    simonb 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
    719  1.305  christos 				    devvp);
    720  1.231    simonb 				if (error) {
    721  1.319      maxv 					DPRINTF("%s: wapbl_replay_write %d",
    722  1.319      maxv 					    nm, error);
    723  1.231    simonb 					return error;
    724  1.231    simonb 				}
    725  1.231    simonb 				wapbl_replay_stop(mp->mnt_wapbl_replay);
    726  1.231    simonb 				fs->fs_clean = FS_WASCLEAN;
    727  1.231    simonb 			}
    728  1.231    simonb #endif /* WAPBL */
    729  1.149   hannken 			if (fs->fs_snapinum[0] != 0)
    730  1.149   hannken 				ffs_snapshot_mount(mp);
    731    1.9   mycroft 		}
    732  1.231    simonb 
    733  1.231    simonb #ifdef WAPBL
    734  1.231    simonb 		error = ffs_wapbl_start(mp);
    735  1.303  christos 		if (error) {
    736  1.319      maxv 			DPRINTF("ffs_wapbl_start returned %d", error);
    737  1.231    simonb 			return error;
    738  1.303  christos 		}
    739  1.231    simonb #endif /* WAPBL */
    740  1.231    simonb 
    741  1.264    bouyer #ifdef QUOTA2
    742  1.264    bouyer 		if (!fs->fs_ronly) {
    743  1.264    bouyer 			error = ffs_quota2_mount(mp);
    744  1.264    bouyer 			if (error) {
    745  1.319      maxv 				DPRINTF("ffs_quota2_mount returned %d", error);
    746  1.264    bouyer 				return error;
    747  1.264    bouyer 			}
    748  1.264    bouyer 		}
    749  1.264    bouyer #endif
    750  1.280  drochner 
    751  1.280  drochner 		if ((mp->mnt_flag & MNT_DISCARD) && !(ump->um_discarddata))
    752  1.280  drochner 			ump->um_discarddata = ffs_discard_init(devvp, fs);
    753  1.280  drochner 
    754  1.204       dsl 		if (args->fspec == NULL)
    755  1.248  christos 			return 0;
    756    1.1   mycroft 	}
    757    1.1   mycroft 
    758  1.379       chs 	(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    759  1.379       chs 	    sizeof(fs->fs_fsmnt));
    760  1.379       chs 
    761  1.243        ad 	fs->fs_flags &= ~FS_DOSOFTDEP;
    762  1.377   hannken 
    763  1.377   hannken 	if ((fs->fs_ronly && (fs->fs_clean & FS_ISCLEAN) == 0) ||
    764  1.377   hannken 	    (!fs->fs_ronly && (fs->fs_clean & FS_WASCLEAN) == 0)) {
    765  1.377   hannken 		printf("%s: file system not clean (fs_clean=%#x); "
    766  1.377   hannken 		    "please fsck(8)\n", mp->mnt_stat.f_mntfromname,
    767  1.377   hannken 		    fs->fs_clean);
    768  1.377   hannken 	}
    769  1.377   hannken 
    770  1.377   hannken 	if (fs->fs_fmod != 0) {
    771  1.231    simonb 		int err;
    772  1.231    simonb 
    773  1.377   hannken 		KASSERT(!fs->fs_ronly);
    774  1.377   hannken 
    775   1.15   mycroft 		if (fs->fs_clean & FS_WASCLEAN)
    776  1.182    kardel 			fs->fs_time = time_second;
    777  1.377   hannken 		fs->fs_fmod = 0;
    778  1.231    simonb 		err = UFS_WAPBL_BEGIN(mp);
    779  1.231    simonb 		if (err == 0) {
    780  1.231    simonb 			(void) ffs_cgupdate(ump, MNT_WAIT);
    781  1.231    simonb 			UFS_WAPBL_END(mp);
    782  1.231    simonb 		}
    783   1.15   mycroft 	}
    784  1.243        ad 	if ((mp->mnt_flag & MNT_SOFTDEP) != 0) {
    785  1.243        ad 		printf("%s: `-o softdep' is no longer supported, "
    786  1.243        ad 		    "consider `-o log'\n", mp->mnt_stat.f_mntfromname);
    787  1.243        ad 		mp->mnt_flag &= ~MNT_SOFTDEP;
    788  1.243        ad 	}
    789  1.243        ad 
    790  1.159   mycroft 	return (error);
    791  1.159   mycroft 
    792  1.159   mycroft fail:
    793  1.159   mycroft 	vrele(devvp);
    794  1.159   mycroft 	return (error);
    795    1.1   mycroft }
    796    1.1   mycroft 
    797    1.1   mycroft /*
    798    1.1   mycroft  * Reload all incore data for a filesystem (used after running fsck on
    799    1.1   mycroft  * the root filesystem and finding things to fix). The filesystem must
    800    1.1   mycroft  * be mounted read-only.
    801    1.1   mycroft  *
    802    1.1   mycroft  * Things to do to update the mount:
    803    1.1   mycroft  *	1) invalidate all cached meta-data.
    804    1.1   mycroft  *	2) re-read superblock from disk.
    805    1.1   mycroft  *	3) re-read summary information from disk.
    806    1.1   mycroft  *	4) invalidate all inactive vnodes.
    807    1.1   mycroft  *	5) invalidate all cached file data.
    808    1.1   mycroft  *	6) re-read inode data for all active vnodes.
    809    1.1   mycroft  */
    810   1.19  christos int
    811  1.181      elad ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
    812    1.1   mycroft {
    813  1.293   hannken 	struct vnode *vp, *devvp;
    814    1.1   mycroft 	struct inode *ip;
    815   1.84     lukem 	void *space;
    816    1.1   mycroft 	struct buf *bp;
    817   1.18       cgd 	struct fs *fs, *newfs;
    818  1.255   mlelstv 	int i, bsize, blks, error;
    819  1.312      maxv 	int32_t *lp, fs_sbsize;
    820  1.111      fvdl 	struct ufsmount *ump;
    821  1.141       dbj 	daddr_t sblockloc;
    822  1.293   hannken 	struct vnode_iterator *marker;
    823    1.1   mycroft 
    824  1.153   mycroft 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    825    1.1   mycroft 		return (EINVAL);
    826  1.111      fvdl 
    827  1.153   mycroft 	ump = VFSTOUFS(mp);
    828  1.311      maxv 
    829    1.1   mycroft 	/*
    830    1.1   mycroft 	 * Step 1: invalidate all cached meta-data.
    831    1.1   mycroft 	 */
    832  1.111      fvdl 	devvp = ump->um_devvp;
    833   1.55      fvdl 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    834  1.177  christos 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
    835  1.259   hannken 	VOP_UNLOCK(devvp);
    836   1.55      fvdl 	if (error)
    837  1.339  christos 		panic("%s: dirty1", __func__);
    838  1.311      maxv 
    839    1.1   mycroft 	/*
    840  1.311      maxv 	 * Step 2: re-read superblock from disk. XXX: We don't handle
    841  1.312      maxv 	 * possibility that superblock moved. Which implies that we don't
    842  1.312      maxv 	 * want its size to change either.
    843    1.1   mycroft 	 */
    844  1.111      fvdl 	fs = ump->um_fs;
    845  1.312      maxv 	fs_sbsize = fs->fs_sbsize;
    846  1.312      maxv 	error = bread(devvp, fs->fs_sblockloc / DEV_BSIZE, fs_sbsize,
    847  1.327      maxv 		      0, &bp);
    848  1.311      maxv 	if (error)
    849    1.1   mycroft 		return (error);
    850  1.312      maxv 	newfs = kmem_alloc(fs_sbsize, KM_SLEEP);
    851  1.312      maxv 	memcpy(newfs, bp->b_data, fs_sbsize);
    852  1.311      maxv 
    853   1.34    bouyer #ifdef FFS_EI
    854  1.111      fvdl 	if (ump->um_flags & UFS_NEEDSWAP) {
    855  1.324      maxv 		ffs_sb_swap((struct fs *)bp->b_data, newfs);
    856  1.324      maxv 		newfs->fs_flags |= FS_SWAPPED;
    857  1.121    bouyer 	} else
    858   1.34    bouyer #endif
    859  1.324      maxv 		newfs->fs_flags &= ~FS_SWAPPED;
    860  1.310      maxv 
    861  1.321      maxv 	brelse(bp, 0);
    862  1.321      maxv 
    863  1.378       chs 	/* Allow converting from UFS2 to UFS2EA but not vice versa. */
    864  1.378       chs 	if (newfs->fs_magic == FS_UFS2EA_MAGIC) {
    865  1.378       chs 		ump->um_flags |= UFS_EA;
    866  1.378       chs 		newfs->fs_magic = FS_UFS2_MAGIC;
    867  1.378       chs 	} else {
    868  1.378       chs 		if ((ump->um_flags & UFS_EA) != 0)
    869  1.378       chs 			return EINVAL;
    870  1.378       chs 	}
    871  1.378       chs 
    872  1.324      maxv 	if ((newfs->fs_magic != FS_UFS1_MAGIC) &&
    873  1.324      maxv 	    (newfs->fs_magic != FS_UFS2_MAGIC)) {
    874  1.312      maxv 		kmem_free(newfs, fs_sbsize);
    875    1.1   mycroft 		return (EIO);		/* XXX needs translation */
    876    1.1   mycroft 	}
    877  1.314      maxv 	if (!ffs_superblock_validate(newfs)) {
    878  1.312      maxv 		kmem_free(newfs, fs_sbsize);
    879  1.310      maxv 		return (EINVAL);
    880  1.310      maxv 	}
    881  1.310      maxv 
    882  1.320      maxv 	/*
    883  1.320      maxv 	 * The current implementation doesn't handle the possibility that
    884  1.320      maxv 	 * these values may have changed.
    885  1.320      maxv 	 */
    886  1.320      maxv 	if ((newfs->fs_sbsize != fs_sbsize) ||
    887  1.320      maxv 	    (newfs->fs_cssize != fs->fs_cssize) ||
    888  1.320      maxv 	    (newfs->fs_contigsumsize != fs->fs_contigsumsize) ||
    889  1.320      maxv 	    (newfs->fs_ncg != fs->fs_ncg)) {
    890  1.320      maxv 		kmem_free(newfs, fs_sbsize);
    891  1.320      maxv 		return (EINVAL);
    892  1.320      maxv 	}
    893  1.320      maxv 
    894  1.141       dbj 	/* Store off old fs_sblockloc for fs_oldfscompat_read. */
    895  1.141       dbj 	sblockloc = fs->fs_sblockloc;
    896  1.161     perry 	/*
    897   1.18       cgd 	 * Copy pointer fields back into superblock before copying in	XXX
    898   1.18       cgd 	 * new superblock. These should really be in the ufsmount.	XXX
    899   1.18       cgd 	 * Note that important parameters (eg fs_ncg) are unchanged.
    900   1.18       cgd 	 */
    901   1.84     lukem 	newfs->fs_csp = fs->fs_csp;
    902   1.18       cgd 	newfs->fs_maxcluster = fs->fs_maxcluster;
    903   1.85     lukem 	newfs->fs_contigdirs = fs->fs_contigdirs;
    904   1.76   mycroft 	newfs->fs_ronly = fs->fs_ronly;
    905  1.110      fvdl 	newfs->fs_active = fs->fs_active;
    906  1.312      maxv 	memcpy(fs, newfs, (u_int)fs_sbsize);
    907  1.312      maxv 	kmem_free(newfs, fs_sbsize);
    908  1.103       dbj 
    909  1.329      maxv 	/*
    910  1.329      maxv 	 * Recheck for Apple UFS filesystem.
    911  1.329      maxv 	 */
    912  1.153   mycroft 	ump->um_flags &= ~UFS_ISAPPLEUFS;
    913  1.329      maxv 	if (ffs_is_appleufs(devvp, fs)) {
    914  1.329      maxv #ifdef APPLE_UFS
    915  1.153   mycroft 		ump->um_flags |= UFS_ISAPPLEUFS;
    916  1.103       dbj #else
    917  1.329      maxv 		DPRINTF("AppleUFS not supported");
    918  1.329      maxv 		return (EIO); /* XXX: really? */
    919  1.103       dbj #endif
    920  1.329      maxv 	}
    921  1.103       dbj 
    922  1.153   mycroft 	if (UFS_MPISAPPLEUFS(ump)) {
    923  1.103       dbj 		/* see comment about NeXT below */
    924  1.153   mycroft 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    925  1.153   mycroft 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
    926  1.153   mycroft 		mp->mnt_iflag |= IMNT_DTYPE;
    927  1.153   mycroft 	} else {
    928  1.153   mycroft 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
    929  1.283  dholland 		ump->um_dirblksiz = UFS_DIRBLKSIZ;
    930  1.153   mycroft 		if (ump->um_maxsymlinklen > 0)
    931  1.153   mycroft 			mp->mnt_iflag |= IMNT_DTYPE;
    932  1.153   mycroft 		else
    933  1.153   mycroft 			mp->mnt_iflag &= ~IMNT_DTYPE;
    934  1.103       dbj 	}
    935  1.154      yamt 	ffs_oldfscompat_read(fs, ump, sblockloc);
    936  1.243        ad 
    937  1.209        ad 	mutex_enter(&ump->um_lock);
    938  1.153   mycroft 	ump->um_maxfilesize = fs->fs_maxfilesize;
    939  1.231    simonb 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
    940  1.231    simonb 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
    941  1.231    simonb 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
    942  1.231    simonb 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
    943  1.231    simonb 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
    944  1.231    simonb 			mutex_exit(&ump->um_lock);
    945  1.231    simonb 			return (EINVAL);
    946  1.231    simonb 		}
    947  1.231    simonb 	}
    948  1.369  christos 
    949   1.89      fvdl 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    950   1.89      fvdl 		fs->fs_pendingblocks = 0;
    951   1.89      fvdl 		fs->fs_pendinginodes = 0;
    952   1.89      fvdl 	}
    953  1.209        ad 	mutex_exit(&ump->um_lock);
    954   1.85     lukem 
    955  1.211     pooka 	ffs_statvfs(mp, &mp->mnt_stat);
    956    1.1   mycroft 	/*
    957    1.1   mycroft 	 * Step 3: re-read summary information from disk.
    958    1.1   mycroft 	 */
    959    1.1   mycroft 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    960   1.84     lukem 	space = fs->fs_csp;
    961    1.1   mycroft 	for (i = 0; i < blks; i += fs->fs_frag) {
    962  1.255   mlelstv 		bsize = fs->fs_bsize;
    963    1.1   mycroft 		if (i + fs->fs_frag > blks)
    964  1.255   mlelstv 			bsize = (blks - i) * fs->fs_fsize;
    965  1.285  dholland 		error = bread(devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), bsize,
    966  1.327      maxv 			      0, &bp);
    967   1.47    bouyer 		if (error) {
    968    1.1   mycroft 			return (error);
    969   1.47    bouyer 		}
    970   1.34    bouyer #ifdef FFS_EI
    971   1.55      fvdl 		if (UFS_FSNEEDSWAP(fs))
    972   1.84     lukem 			ffs_csum_swap((struct csum *)bp->b_data,
    973  1.255   mlelstv 			    (struct csum *)space, bsize);
    974   1.34    bouyer 		else
    975   1.34    bouyer #endif
    976  1.255   mlelstv 			memcpy(space, bp->b_data, (size_t)bsize);
    977  1.255   mlelstv 		space = (char *)space + bsize;
    978  1.209        ad 		brelse(bp, 0);
    979    1.1   mycroft 	}
    980   1.18       cgd 	/*
    981   1.18       cgd 	 * We no longer know anything about clusters per cylinder group.
    982   1.18       cgd 	 */
    983   1.18       cgd 	if (fs->fs_contigsumsize > 0) {
    984   1.18       cgd 		lp = fs->fs_maxcluster;
    985   1.18       cgd 		for (i = 0; i < fs->fs_ncg; i++)
    986   1.18       cgd 			*lp++ = fs->fs_contigsumsize;
    987   1.18       cgd 	}
    988   1.18       cgd 
    989  1.293   hannken 	vfs_vnode_iterator_init(mp, &marker);
    990  1.299  christos 	while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
    991    1.1   mycroft 		/*
    992    1.1   mycroft 		 * Step 4: invalidate all inactive vnodes.
    993    1.1   mycroft 		 */
    994  1.293   hannken 		if (vrecycle(vp))
    995  1.293   hannken 			continue;
    996    1.1   mycroft 		/*
    997    1.1   mycroft 		 * Step 5: invalidate all cached file data.
    998    1.1   mycroft 		 */
    999  1.293   hannken 		if (vn_lock(vp, LK_EXCLUSIVE)) {
   1000  1.293   hannken 			vrele(vp);
   1001  1.293   hannken 			continue;
   1002  1.214        ad 		}
   1003  1.177  christos 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
   1004  1.339  christos 			panic("%s: dirty2", __func__);
   1005    1.1   mycroft 		/*
   1006    1.1   mycroft 		 * Step 6: re-read inode data for all active vnodes.
   1007    1.1   mycroft 		 */
   1008    1.1   mycroft 		ip = VTOI(vp);
   1009  1.285  dholland 		error = bread(devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
   1010  1.327      maxv 			      (int)fs->fs_bsize, 0, &bp);
   1011   1.19  christos 		if (error) {
   1012    1.1   mycroft 			vput(vp);
   1013  1.214        ad 			break;
   1014    1.1   mycroft 		}
   1015  1.110      fvdl 		ffs_load_inode(bp, ip, fs, ip->i_number);
   1016  1.209        ad 		brelse(bp, 0);
   1017    1.1   mycroft 		vput(vp);
   1018    1.1   mycroft 	}
   1019  1.293   hannken 	vfs_vnode_iterator_destroy(marker);
   1020  1.214        ad 	return (error);
   1021    1.1   mycroft }
   1022    1.1   mycroft 
   1023    1.1   mycroft /*
   1024  1.110      fvdl  * Possible superblock locations ordered from most to least likely.
   1025  1.110      fvdl  */
   1026  1.135  jdolecek static const int sblock_try[] = SBLOCKSEARCH;
   1027  1.110      fvdl 
   1028  1.307      maxv 
   1029  1.307      maxv static int
   1030  1.314      maxv ffs_superblock_validate(struct fs *fs)
   1031  1.307      maxv {
   1032  1.318      maxv 	int32_t i, fs_bshift = 0, fs_fshift = 0, fs_fragshift = 0, fs_frag;
   1033  1.336      maxv 	int32_t fs_inopb;
   1034  1.315      maxv 
   1035  1.307      maxv 	/* Check the superblock size */
   1036  1.314      maxv 	if (fs->fs_sbsize > SBLOCKSIZE || fs->fs_sbsize < sizeof(struct fs))
   1037  1.307      maxv 		return 0;
   1038  1.307      maxv 
   1039  1.307      maxv 	/* Check the file system blocksize */
   1040  1.314      maxv 	if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < MINBSIZE)
   1041  1.308      maxv 		return 0;
   1042  1.314      maxv 	if (!powerof2(fs->fs_bsize))
   1043  1.308      maxv 		return 0;
   1044  1.308      maxv 
   1045  1.308      maxv 	/* Check the size of frag blocks */
   1046  1.308      maxv 	if (!powerof2(fs->fs_fsize))
   1047  1.307      maxv 		return 0;
   1048  1.318      maxv 	if (fs->fs_fsize == 0)
   1049  1.318      maxv 		return 0;
   1050  1.307      maxv 
   1051  1.323      maxv 	/*
   1052  1.323      maxv 	 * XXX: these values are just zero-checked to prevent obvious
   1053  1.323      maxv 	 * bugs. We need more strict checks.
   1054  1.323      maxv 	 */
   1055  1.358       uwe 	if (fs->fs_size == 0 && fs->fs_old_size == 0)
   1056  1.307      maxv 		return 0;
   1057  1.309      maxv 	if (fs->fs_cssize == 0)
   1058  1.309      maxv 		return 0;
   1059  1.323      maxv 	if (fs->fs_ipg == 0)
   1060  1.323      maxv 		return 0;
   1061  1.323      maxv 	if (fs->fs_fpg == 0)
   1062  1.323      maxv 		return 0;
   1063  1.328      maxv 	if (fs->fs_ncg == 0)
   1064  1.328      maxv 		return 0;
   1065  1.328      maxv 	if (fs->fs_maxbpg == 0)
   1066  1.328      maxv 		return 0;
   1067  1.307      maxv 
   1068  1.322      maxv 	/* Check the number of inodes per block */
   1069  1.322      maxv 	if (fs->fs_magic == FS_UFS1_MAGIC)
   1070  1.322      maxv 		fs_inopb = fs->fs_bsize / sizeof(struct ufs1_dinode);
   1071  1.322      maxv 	else /* fs->fs_magic == FS_UFS2_MAGIC */
   1072  1.322      maxv 		fs_inopb = fs->fs_bsize / sizeof(struct ufs2_dinode);
   1073  1.322      maxv 	if (fs->fs_inopb != fs_inopb)
   1074  1.322      maxv 		return 0;
   1075  1.322      maxv 
   1076  1.308      maxv 	/* Block size cannot be smaller than fragment size */
   1077  1.314      maxv 	if (fs->fs_bsize < fs->fs_fsize)
   1078  1.308      maxv 		return 0;
   1079  1.308      maxv 
   1080  1.315      maxv 	/* Compute fs_bshift and ensure it is consistent */
   1081  1.315      maxv 	for (i = fs->fs_bsize; i > 1; i >>= 1)
   1082  1.315      maxv 		fs_bshift++;
   1083  1.315      maxv 	if (fs->fs_bshift != fs_bshift)
   1084  1.315      maxv 		return 0;
   1085  1.315      maxv 
   1086  1.315      maxv 	/* Compute fs_fshift and ensure it is consistent */
   1087  1.315      maxv 	for (i = fs->fs_fsize; i > 1; i >>= 1)
   1088  1.315      maxv 		fs_fshift++;
   1089  1.315      maxv 	if (fs->fs_fshift != fs_fshift)
   1090  1.315      maxv 		return 0;
   1091  1.315      maxv 
   1092  1.318      maxv 	/* Compute fs_fragshift and ensure it is consistent */
   1093  1.318      maxv 	for (i = fs->fs_frag; i > 1; i >>= 1)
   1094  1.318      maxv 		fs_fragshift++;
   1095  1.318      maxv 	if (fs->fs_fragshift != fs_fragshift)
   1096  1.318      maxv 		return 0;
   1097  1.318      maxv 
   1098  1.318      maxv 	/* Check the masks */
   1099  1.318      maxv 	if (fs->fs_bmask != ~(fs->fs_bsize - 1))
   1100  1.318      maxv 		return 0;
   1101  1.318      maxv 	if (fs->fs_fmask != ~(fs->fs_fsize - 1))
   1102  1.318      maxv 		return 0;
   1103  1.318      maxv 
   1104  1.318      maxv 	/*
   1105  1.318      maxv 	 * Now that the shifts and masks are sanitized, we can use the ffs_ API.
   1106  1.318      maxv 	 */
   1107  1.315      maxv 
   1108  1.313      maxv 	/* Check the number of frag blocks */
   1109  1.316      maxv 	if ((fs_frag = ffs_numfrags(fs, fs->fs_bsize)) > MAXFRAG)
   1110  1.316      maxv 		return 0;
   1111  1.316      maxv 	if (fs->fs_frag != fs_frag)
   1112  1.313      maxv 		return 0;
   1113  1.313      maxv 
   1114  1.330      maxv 	/* Check the size of cylinder groups */
   1115  1.336      maxv 	if ((fs->fs_cgsize < sizeof(struct cg)) ||
   1116  1.336      maxv 	    (fs->fs_cgsize > fs->fs_bsize))
   1117  1.336      maxv 		return 0;
   1118  1.330      maxv 
   1119  1.307      maxv 	return 1;
   1120  1.307      maxv }
   1121  1.307      maxv 
   1122  1.329      maxv static int
   1123  1.329      maxv ffs_is_appleufs(struct vnode *devvp, struct fs *fs)
   1124  1.329      maxv {
   1125  1.329      maxv 	struct dkwedge_info dkw;
   1126  1.329      maxv 	int ret = 0;
   1127  1.329      maxv 
   1128  1.329      maxv 	/*
   1129  1.329      maxv 	 * First check to see if this is tagged as an Apple UFS filesystem
   1130  1.329      maxv 	 * in the disklabel.
   1131  1.329      maxv 	 */
   1132  1.329      maxv 	if (getdiskinfo(devvp, &dkw) == 0 &&
   1133  1.329      maxv 	    strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
   1134  1.329      maxv 		ret = 1;
   1135  1.329      maxv #ifdef APPLE_UFS
   1136  1.329      maxv 	else {
   1137  1.329      maxv 		struct appleufslabel *applefs;
   1138  1.329      maxv 		struct buf *bp;
   1139  1.329      maxv 		daddr_t blkno = APPLEUFS_LABEL_OFFSET / DEV_BSIZE;
   1140  1.329      maxv 		int error;
   1141  1.329      maxv 
   1142  1.329      maxv 		/*
   1143  1.329      maxv 		 * Manually look for an Apple UFS label, and if a valid one
   1144  1.329      maxv 		 * is found, then treat it like an Apple UFS filesystem anyway.
   1145  1.329      maxv 		 */
   1146  1.329      maxv 		error = bread(devvp, blkno, APPLEUFS_LABEL_SIZE, 0, &bp);
   1147  1.329      maxv 		if (error) {
   1148  1.329      maxv 			DPRINTF("bread@0x%jx returned %d", (intmax_t)blkno, error);
   1149  1.329      maxv 			return 0;
   1150  1.329      maxv 		}
   1151  1.329      maxv 		applefs = (struct appleufslabel *)bp->b_data;
   1152  1.329      maxv 		error = ffs_appleufs_validate(fs->fs_fsmnt, applefs, NULL);
   1153  1.329      maxv 		if (error == 0)
   1154  1.329      maxv 			ret = 1;
   1155  1.329      maxv 		brelse(bp, 0);
   1156  1.329      maxv 	}
   1157  1.329      maxv #endif
   1158  1.329      maxv 
   1159  1.329      maxv 	return ret;
   1160  1.329      maxv }
   1161  1.329      maxv 
   1162  1.110      fvdl /*
   1163    1.1   mycroft  * Common code for mount and mountroot
   1164    1.1   mycroft  */
   1165    1.1   mycroft int
   1166  1.177  christos ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
   1167    1.1   mycroft {
   1168  1.306      maxv 	struct ufsmount *ump = NULL;
   1169  1.306      maxv 	struct buf *bp = NULL;
   1170  1.306      maxv 	struct fs *fs = NULL;
   1171    1.9   mycroft 	dev_t dev;
   1172   1.84     lukem 	void *space;
   1173  1.306      maxv 	daddr_t sblockloc = 0;
   1174  1.306      maxv 	int blks, fstype = 0;
   1175  1.255   mlelstv 	int error, i, bsize, ronly, bset = 0;
   1176   1.52  drochner #ifdef FFS_EI
   1177  1.110      fvdl 	int needswap = 0;		/* keep gcc happy */
   1178   1.52  drochner #endif
   1179    1.9   mycroft 	int32_t *lp;
   1180  1.181      elad 	kauth_cred_t cred;
   1181  1.319      maxv 	u_int32_t allocsbsize, fs_sbsize = 0;
   1182    1.1   mycroft 
   1183    1.9   mycroft 	dev = devvp->v_rdev;
   1184  1.184        ad 	cred = l ? l->l_cred : NOCRED;
   1185  1.159   mycroft 
   1186  1.159   mycroft 	/* Flush out any old buffers remaining from a previous use. */
   1187   1.55      fvdl 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
   1188  1.177  christos 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
   1189  1.259   hannken 	VOP_UNLOCK(devvp);
   1190  1.303  christos 	if (error) {
   1191  1.319      maxv 		DPRINTF("vinvalbuf returned %d", error);
   1192  1.303  christos 		return error;
   1193  1.303  christos 	}
   1194    1.1   mycroft 
   1195    1.1   mycroft 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
   1196    1.1   mycroft 
   1197  1.274     rmind 	ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
   1198  1.231    simonb 	mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
   1199  1.231    simonb 	error = ffs_snapshot_init(ump);
   1200  1.303  christos 	if (error) {
   1201  1.319      maxv 		DPRINTF("ffs_snapshot_init returned %d", error);
   1202  1.231    simonb 		goto out;
   1203  1.303  christos 	}
   1204  1.231    simonb 	ump->um_ops = &ffs_ufsops;
   1205  1.231    simonb 
   1206  1.231    simonb #ifdef WAPBL
   1207  1.231    simonb  sbagain:
   1208  1.231    simonb #endif
   1209  1.110      fvdl 	/*
   1210  1.192     isaki 	 * Try reading the superblock in each of its possible locations.
   1211  1.192     isaki 	 */
   1212  1.138       dsl 	for (i = 0; ; i++) {
   1213  1.319      maxv 		daddr_t fs_sblockloc;
   1214  1.306      maxv 
   1215  1.138       dsl 		if (bp != NULL) {
   1216  1.209        ad 			brelse(bp, BC_NOCACHE);
   1217  1.138       dsl 			bp = NULL;
   1218  1.138       dsl 		}
   1219  1.138       dsl 		if (sblock_try[i] == -1) {
   1220  1.319      maxv 			DPRINTF("no superblock found");
   1221  1.138       dsl 			error = EINVAL;
   1222  1.138       dsl 			fs = NULL;
   1223  1.138       dsl 			goto out;
   1224  1.138       dsl 		}
   1225  1.306      maxv 
   1226  1.303  christos 		error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
   1227  1.327      maxv 		    0, &bp);
   1228  1.168  drochner 		if (error) {
   1229  1.319      maxv 			DPRINTF("bread@0x%x returned %d",
   1230  1.319      maxv 			    sblock_try[i] / DEV_BSIZE, error);
   1231  1.168  drochner 			fs = NULL;
   1232  1.110      fvdl 			goto out;
   1233  1.168  drochner 		}
   1234  1.319      maxv 		fs = (struct fs *)bp->b_data;
   1235  1.306      maxv 
   1236  1.319      maxv 		sblockloc = sblock_try[i];
   1237  1.319      maxv 		DPRINTF("fs_magic 0x%x", fs->fs_magic);
   1238  1.314      maxv 
   1239  1.314      maxv 		/*
   1240  1.314      maxv 		 * Swap: here, we swap fs->fs_sbsize in order to get the correct
   1241  1.314      maxv 		 * size to read the superblock. Once read, we swap the whole
   1242  1.314      maxv 		 * superblock structure.
   1243  1.314      maxv 		 */
   1244  1.378       chs 		if (fs->fs_magic == FS_UFS2EA_MAGIC) {
   1245  1.378       chs 			ump->um_flags |= UFS_EA;
   1246  1.378       chs 			fs->fs_magic = FS_UFS2_MAGIC;
   1247  1.378       chs 		} else if (fs->fs_magic == FS_UFS2EA_MAGIC_SWAPPED) {
   1248  1.378       chs 			ump->um_flags |= UFS_EA;
   1249  1.378       chs 			fs->fs_magic = FS_UFS2_MAGIC_SWAPPED;
   1250  1.378       chs 		}
   1251  1.110      fvdl 		if (fs->fs_magic == FS_UFS1_MAGIC) {
   1252  1.306      maxv 			fs_sbsize = fs->fs_sbsize;
   1253  1.110      fvdl 			fstype = UFS1;
   1254   1.34    bouyer #ifdef FFS_EI
   1255  1.110      fvdl 			needswap = 0;
   1256  1.275    nonaka 		} else if (fs->fs_magic == FS_UFS1_MAGIC_SWAPPED) {
   1257  1.306      maxv 			fs_sbsize = bswap32(fs->fs_sbsize);
   1258  1.110      fvdl 			fstype = UFS1;
   1259  1.110      fvdl 			needswap = 1;
   1260   1.34    bouyer #endif
   1261  1.110      fvdl 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
   1262  1.306      maxv 			fs_sbsize = fs->fs_sbsize;
   1263  1.110      fvdl 			fstype = UFS2;
   1264  1.110      fvdl #ifdef FFS_EI
   1265  1.110      fvdl 			needswap = 0;
   1266  1.275    nonaka 		} else if (fs->fs_magic == FS_UFS2_MAGIC_SWAPPED) {
   1267  1.306      maxv 			fs_sbsize = bswap32(fs->fs_sbsize);
   1268  1.110      fvdl 			fstype = UFS2;
   1269  1.110      fvdl 			needswap = 1;
   1270  1.110      fvdl #endif
   1271  1.112      fvdl 		} else
   1272  1.138       dsl 			continue;
   1273  1.138       dsl 
   1274  1.138       dsl 		/* fs->fs_sblockloc isn't defined for old filesystems */
   1275  1.140       dsl 		if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
   1276  1.140       dsl 			if (sblockloc == SBLOCK_UFS2)
   1277  1.138       dsl 				/*
   1278  1.138       dsl 				 * This is likely to be the first alternate
   1279  1.138       dsl 				 * in a filesystem with 64k blocks.
   1280  1.138       dsl 				 * Don't use it.
   1281  1.138       dsl 				 */
   1282  1.138       dsl 				continue;
   1283  1.319      maxv 			fs_sblockloc = sblockloc;
   1284  1.138       dsl 		} else {
   1285  1.319      maxv 			fs_sblockloc = fs->fs_sblockloc;
   1286  1.138       dsl #ifdef FFS_EI
   1287  1.138       dsl 			if (needswap)
   1288  1.319      maxv 				fs_sblockloc = bswap64(fs_sblockloc);
   1289  1.138       dsl #endif
   1290  1.138       dsl 		}
   1291  1.110      fvdl 
   1292  1.138       dsl 		/* Check we haven't found an alternate superblock */
   1293  1.319      maxv 		if (fs_sblockloc != sblockloc)
   1294  1.138       dsl 			continue;
   1295  1.112      fvdl 
   1296  1.314      maxv 		/* Check the superblock size */
   1297  1.314      maxv 		if (fs_sbsize > SBLOCKSIZE || fs_sbsize < sizeof(struct fs))
   1298  1.138       dsl 			continue;
   1299  1.314      maxv 		fs = kmem_alloc((u_long)fs_sbsize, KM_SLEEP);
   1300  1.314      maxv 		memcpy(fs, bp->b_data, fs_sbsize);
   1301  1.314      maxv 
   1302  1.314      maxv 		/* Swap the whole superblock structure, if necessary. */
   1303  1.314      maxv #ifdef FFS_EI
   1304  1.314      maxv 		if (needswap) {
   1305  1.314      maxv 			ffs_sb_swap((struct fs*)bp->b_data, fs);
   1306  1.314      maxv 			fs->fs_flags |= FS_SWAPPED;
   1307  1.314      maxv 		} else
   1308  1.314      maxv #endif
   1309  1.314      maxv 			fs->fs_flags &= ~FS_SWAPPED;
   1310  1.314      maxv 
   1311  1.314      maxv 		/*
   1312  1.314      maxv 		 * Now that everything is swapped, the superblock is ready to
   1313  1.314      maxv 		 * be sanitized.
   1314  1.314      maxv 		 */
   1315  1.314      maxv 		if (!ffs_superblock_validate(fs)) {
   1316  1.314      maxv 			kmem_free(fs, fs_sbsize);
   1317  1.314      maxv 			continue;
   1318  1.314      maxv 		}
   1319  1.110      fvdl 
   1320  1.138       dsl 		/* Ok seems to be a good superblock */
   1321  1.138       dsl 		break;
   1322   1.34    bouyer 	}
   1323   1.34    bouyer 
   1324  1.111      fvdl 	ump->um_fs = fs;
   1325  1.111      fvdl 
   1326  1.231    simonb #ifdef WAPBL
   1327  1.231    simonb 	if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
   1328  1.231    simonb 		error = ffs_wapbl_replay_start(mp, fs, devvp);
   1329  1.303  christos 		if (error && (mp->mnt_flag & MNT_FORCE) == 0) {
   1330  1.319      maxv 			DPRINTF("ffs_wapbl_replay_start returned %d", error);
   1331  1.231    simonb 			goto out;
   1332  1.303  christos 		}
   1333  1.252    bouyer 		if (!error) {
   1334  1.252    bouyer 			if (!ronly) {
   1335  1.252    bouyer 				/* XXX fsmnt may be stale. */
   1336  1.252    bouyer 				printf("%s: replaying log to disk\n",
   1337  1.252    bouyer 				    fs->fs_fsmnt);
   1338  1.252    bouyer 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
   1339  1.252    bouyer 				    devvp);
   1340  1.303  christos 				if (error) {
   1341  1.319      maxv 					DPRINTF("wapbl_replay_write returned %d",
   1342  1.319      maxv 					    error);
   1343  1.252    bouyer 					goto out;
   1344  1.303  christos 				}
   1345  1.252    bouyer 				wapbl_replay_stop(mp->mnt_wapbl_replay);
   1346  1.252    bouyer 				fs->fs_clean = FS_WASCLEAN;
   1347  1.252    bouyer 			} else {
   1348  1.252    bouyer 				/* XXX fsmnt may be stale */
   1349  1.252    bouyer 				printf("%s: replaying log to memory\n",
   1350  1.252    bouyer 				    fs->fs_fsmnt);
   1351  1.252    bouyer 			}
   1352  1.231    simonb 
   1353  1.252    bouyer 			/* Force a re-read of the superblock */
   1354  1.252    bouyer 			brelse(bp, BC_INVAL);
   1355  1.252    bouyer 			bp = NULL;
   1356  1.306      maxv 			kmem_free(fs, fs_sbsize);
   1357  1.252    bouyer 			fs = NULL;
   1358  1.252    bouyer 			goto sbagain;
   1359  1.231    simonb 		}
   1360  1.231    simonb 	}
   1361  1.231    simonb #else /* !WAPBL */
   1362  1.231    simonb 	if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
   1363  1.231    simonb 		error = EPERM;
   1364  1.319      maxv 		DPRINTF("no force %d", error);
   1365  1.231    simonb 		goto out;
   1366  1.231    simonb 	}
   1367  1.231    simonb #endif /* !WAPBL */
   1368  1.231    simonb 
   1369  1.154      yamt 	ffs_oldfscompat_read(fs, ump, sblockloc);
   1370  1.153   mycroft 	ump->um_maxfilesize = fs->fs_maxfilesize;
   1371  1.131       dbj 
   1372  1.231    simonb 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
   1373  1.231    simonb 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
   1374  1.231    simonb 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
   1375  1.231    simonb 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
   1376  1.231    simonb 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
   1377  1.231    simonb 			error = EINVAL;
   1378  1.319      maxv 			DPRINTF("no force %d", error);
   1379  1.231    simonb 			goto out;
   1380  1.231    simonb 		}
   1381  1.231    simonb 	}
   1382  1.231    simonb 
   1383  1.377   hannken 	fs->fs_fmod = 0;
   1384   1.89      fvdl 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1385   1.89      fvdl 		fs->fs_pendingblocks = 0;
   1386   1.89      fvdl 		fs->fs_pendinginodes = 0;
   1387   1.89      fvdl 	}
   1388   1.56  drochner 
   1389  1.110      fvdl 	ump->um_fstype = fstype;
   1390  1.110      fvdl 	if (fs->fs_sbsize < SBLOCKSIZE)
   1391  1.209        ad 		brelse(bp, BC_INVAL);
   1392  1.209        ad 	else
   1393  1.209        ad 		brelse(bp, 0);
   1394    1.1   mycroft 	bp = NULL;
   1395   1.94       chs 
   1396  1.329      maxv 	if (ffs_is_appleufs(devvp, fs)) {
   1397  1.329      maxv #ifdef APPLE_UFS
   1398  1.103       dbj 		ump->um_flags |= UFS_ISAPPLEUFS;
   1399  1.103       dbj #else
   1400  1.319      maxv 		DPRINTF("AppleUFS not supported");
   1401  1.103       dbj 		error = EINVAL;
   1402  1.103       dbj 		goto out;
   1403  1.329      maxv #endif
   1404  1.103       dbj 	}
   1405  1.103       dbj 
   1406  1.231    simonb #if 0
   1407  1.231    simonb /*
   1408  1.231    simonb  * XXX This code changes the behaviour of mounting dirty filesystems, to
   1409  1.231    simonb  * XXX require "mount -f ..." to mount them.  This doesn't match what
   1410  1.231    simonb  * XXX mount(8) describes and is disabled for now.
   1411  1.231    simonb  */
   1412  1.231    simonb 	/*
   1413  1.231    simonb 	 * If the file system is not clean, don't allow it to be mounted
   1414  1.231    simonb 	 * unless MNT_FORCE is specified.  (Note: MNT_FORCE is always set
   1415  1.231    simonb 	 * for the root file system.)
   1416  1.231    simonb 	 */
   1417  1.231    simonb 	if (fs->fs_flags & FS_DOWAPBL) {
   1418  1.231    simonb 		/*
   1419  1.231    simonb 		 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
   1420  1.231    simonb 		 * bit is set, although there's a window in unmount where it
   1421  1.231    simonb 		 * could be FS_ISCLEAN
   1422  1.231    simonb 		 */
   1423  1.231    simonb 		if ((mp->mnt_flag & MNT_FORCE) == 0 &&
   1424  1.231    simonb 		    (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
   1425  1.231    simonb 			error = EPERM;
   1426  1.231    simonb 			goto out;
   1427  1.231    simonb 		}
   1428  1.231    simonb 	} else
   1429  1.231    simonb 		if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
   1430  1.231    simonb 		    (mp->mnt_flag & MNT_FORCE) == 0) {
   1431  1.231    simonb 			error = EPERM;
   1432  1.231    simonb 			goto out;
   1433  1.231    simonb 		}
   1434  1.231    simonb #endif
   1435  1.231    simonb 
   1436   1.94       chs 	/*
   1437  1.306      maxv 	 * Verify that we can access the last block in the fs
   1438   1.99       chs 	 * if we're mounting read/write.
   1439   1.94       chs 	 */
   1440   1.99       chs 	if (!ronly) {
   1441  1.296  christos 		error = bread(devvp, FFS_FSBTODB(fs, fs->fs_size - 1),
   1442  1.327      maxv 		    fs->fs_fsize, 0, &bp);
   1443  1.209        ad 		if (error) {
   1444  1.319      maxv 			DPRINTF("bread@0x%jx returned %d",
   1445  1.303  christos 			    (intmax_t)FFS_FSBTODB(fs, fs->fs_size - 1),
   1446  1.319      maxv 			    error);
   1447  1.209        ad 			bset = BC_INVAL;
   1448   1.99       chs 			goto out;
   1449  1.209        ad 		}
   1450  1.303  christos 		if (bp->b_bcount != fs->fs_fsize) {
   1451  1.319      maxv 			DPRINTF("bcount %x != fsize %x", bp->b_bcount,
   1452  1.319      maxv 			    fs->fs_fsize);
   1453  1.296  christos 			error = EINVAL;
   1454  1.334      maxv 			bset = BC_INVAL;
   1455  1.334      maxv 			goto out;
   1456  1.303  christos 		}
   1457  1.209        ad 		brelse(bp, BC_INVAL);
   1458   1.99       chs 		bp = NULL;
   1459   1.99       chs 	}
   1460   1.94       chs 
   1461    1.1   mycroft 	fs->fs_ronly = ronly;
   1462  1.231    simonb 	/* Don't bump fs_clean if we're replaying journal */
   1463  1.306      maxv 	if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN))) {
   1464  1.231    simonb 		if (ronly == 0) {
   1465  1.377   hannken 			fs->fs_clean =
   1466  1.377   hannken 			    fs->fs_clean == FS_ISCLEAN ? FS_WASCLEAN : 0;
   1467  1.231    simonb 			fs->fs_fmod = 1;
   1468  1.231    simonb 		}
   1469  1.306      maxv 	}
   1470  1.306      maxv 
   1471  1.255   mlelstv 	bsize = fs->fs_cssize;
   1472  1.255   mlelstv 	blks = howmany(bsize, fs->fs_fsize);
   1473    1.9   mycroft 	if (fs->fs_contigsumsize > 0)
   1474  1.255   mlelstv 		bsize += fs->fs_ncg * sizeof(int32_t);
   1475  1.255   mlelstv 	bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1476  1.273      para 	allocsbsize = bsize;
   1477  1.273      para 	space = kmem_alloc((u_long)allocsbsize, KM_SLEEP);
   1478   1.84     lukem 	fs->fs_csp = space;
   1479  1.306      maxv 
   1480    1.1   mycroft 	for (i = 0; i < blks; i += fs->fs_frag) {
   1481  1.255   mlelstv 		bsize = fs->fs_bsize;
   1482    1.1   mycroft 		if (i + fs->fs_frag > blks)
   1483  1.255   mlelstv 			bsize = (blks - i) * fs->fs_fsize;
   1484  1.285  dholland 		error = bread(devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), bsize,
   1485  1.327      maxv 			      0, &bp);
   1486   1.19  christos 		if (error) {
   1487  1.319      maxv 			DPRINTF("bread@0x%jx %d",
   1488  1.303  christos 			    (intmax_t)FFS_FSBTODB(fs, fs->fs_csaddr + i),
   1489  1.319      maxv 			    error);
   1490  1.303  christos 			goto out1;
   1491    1.1   mycroft 		}
   1492   1.34    bouyer #ifdef FFS_EI
   1493   1.34    bouyer 		if (needswap)
   1494   1.84     lukem 			ffs_csum_swap((struct csum *)bp->b_data,
   1495  1.255   mlelstv 				(struct csum *)space, bsize);
   1496   1.34    bouyer 		else
   1497   1.34    bouyer #endif
   1498  1.255   mlelstv 			memcpy(space, bp->b_data, (u_int)bsize);
   1499  1.161     perry 
   1500  1.255   mlelstv 		space = (char *)space + bsize;
   1501  1.209        ad 		brelse(bp, 0);
   1502    1.1   mycroft 		bp = NULL;
   1503    1.1   mycroft 	}
   1504    1.9   mycroft 	if (fs->fs_contigsumsize > 0) {
   1505   1.85     lukem 		fs->fs_maxcluster = lp = space;
   1506    1.9   mycroft 		for (i = 0; i < fs->fs_ncg; i++)
   1507    1.9   mycroft 			*lp++ = fs->fs_contigsumsize;
   1508   1.85     lukem 		space = lp;
   1509    1.9   mycroft 	}
   1510  1.255   mlelstv 	bsize = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1511   1.85     lukem 	fs->fs_contigdirs = space;
   1512  1.255   mlelstv 	space = (char *)space + bsize;
   1513  1.255   mlelstv 	memset(fs->fs_contigdirs, 0, bsize);
   1514  1.318      maxv 
   1515  1.318      maxv 	/* Compatibility for old filesystems - XXX */
   1516   1.85     lukem 	if (fs->fs_avgfilesize <= 0)
   1517   1.85     lukem 		fs->fs_avgfilesize = AVFILESIZ;
   1518   1.85     lukem 	if (fs->fs_avgfpdir <= 0)
   1519   1.85     lukem 		fs->fs_avgfpdir = AFPDIR;
   1520  1.150   hannken 	fs->fs_active = NULL;
   1521  1.318      maxv 
   1522  1.100     soren 	mp->mnt_data = ump;
   1523  1.143  christos 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
   1524  1.143  christos 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
   1525  1.143  christos 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
   1526  1.169  christos 	mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
   1527  1.153   mycroft 	if (UFS_MPISAPPLEUFS(ump)) {
   1528  1.103       dbj 		/* NeXT used to keep short symlinks in the inode even
   1529  1.103       dbj 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
   1530  1.103       dbj 		 * is probably -1, but we still need to be able to identify
   1531  1.103       dbj 		 * short symlinks.
   1532  1.103       dbj 		 */
   1533  1.153   mycroft 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
   1534  1.153   mycroft 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
   1535  1.153   mycroft 		mp->mnt_iflag |= IMNT_DTYPE;
   1536  1.153   mycroft 	} else {
   1537  1.153   mycroft 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
   1538  1.283  dholland 		ump->um_dirblksiz = UFS_DIRBLKSIZ;
   1539  1.153   mycroft 		if (ump->um_maxsymlinklen > 0)
   1540  1.153   mycroft 			mp->mnt_iflag |= IMNT_DTYPE;
   1541  1.153   mycroft 		else
   1542  1.153   mycroft 			mp->mnt_iflag &= ~IMNT_DTYPE;
   1543  1.103       dbj 	}
   1544   1.73       chs 	mp->mnt_fs_bshift = fs->fs_bshift;
   1545   1.73       chs 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
   1546    1.1   mycroft 	mp->mnt_flag |= MNT_LOCAL;
   1547  1.367        ad 	mp->mnt_iflag |= IMNT_MPSAFE | IMNT_CAN_RWTORO | IMNT_SHRLOOKUP |
   1548  1.367        ad 	    IMNT_NCLOOKUP;
   1549   1.34    bouyer #ifdef FFS_EI
   1550   1.34    bouyer 	if (needswap)
   1551   1.34    bouyer 		ump->um_flags |= UFS_NEEDSWAP;
   1552   1.34    bouyer #endif
   1553  1.379       chs 	error = ffs_acls(mp, fs->fs_flags);
   1554  1.379       chs 	if (error)
   1555  1.379       chs 		goto out1;
   1556    1.1   mycroft 	ump->um_mountp = mp;
   1557    1.1   mycroft 	ump->um_dev = dev;
   1558    1.1   mycroft 	ump->um_devvp = devvp;
   1559    1.1   mycroft 	ump->um_nindir = fs->fs_nindir;
   1560   1.73       chs 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
   1561  1.256   mlelstv 	ump->um_bptrtodb = fs->fs_fshift - DEV_BSHIFT;
   1562    1.1   mycroft 	ump->um_seqinc = fs->fs_frag;
   1563    1.1   mycroft 	for (i = 0; i < MAXQUOTAS; i++)
   1564    1.1   mycroft 		ump->um_quotas[i] = NULLVP;
   1565  1.289   hannken 	spec_node_setmountedfs(devvp, mp);
   1566  1.232   hannken 	if (ronly == 0 && fs->fs_snapinum[0] != 0)
   1567  1.232   hannken 		ffs_snapshot_mount(mp);
   1568  1.231    simonb #ifdef WAPBL
   1569  1.231    simonb 	if (!ronly) {
   1570  1.231    simonb 		KDASSERT(fs->fs_ronly == 0);
   1571  1.231    simonb 		/*
   1572  1.231    simonb 		 * ffs_wapbl_start() needs mp->mnt_stat initialised if it
   1573  1.231    simonb 		 * needs to create a new log file in-filesystem.
   1574  1.231    simonb 		 */
   1575  1.303  christos 		error = ffs_statvfs(mp, &mp->mnt_stat);
   1576  1.303  christos 		if (error) {
   1577  1.319      maxv 			DPRINTF("ffs_statvfs returned %d", error);
   1578  1.303  christos 			goto out1;
   1579  1.303  christos 		}
   1580  1.231    simonb 
   1581  1.231    simonb 		error = ffs_wapbl_start(mp);
   1582  1.231    simonb 		if (error) {
   1583  1.319      maxv 			DPRINTF("ffs_wapbl_start returned %d", error);
   1584  1.303  christos 			goto out1;
   1585  1.231    simonb 		}
   1586  1.231    simonb 	}
   1587  1.231    simonb #endif /* WAPBL */
   1588  1.264    bouyer 	if (ronly == 0) {
   1589  1.264    bouyer #ifdef QUOTA2
   1590  1.264    bouyer 		error = ffs_quota2_mount(mp);
   1591  1.264    bouyer 		if (error) {
   1592  1.319      maxv 			DPRINTF("ffs_quota2_mount returned %d", error);
   1593  1.303  christos 			goto out1;
   1594  1.264    bouyer 		}
   1595  1.264    bouyer #else
   1596  1.264    bouyer 		if (fs->fs_flags & FS_DOQUOTA2) {
   1597  1.264    bouyer 			ump->um_flags |= UFS_QUOTA2;
   1598  1.264    bouyer 			uprintf("%s: options QUOTA2 not enabled%s\n",
   1599  1.264    bouyer 			    mp->mnt_stat.f_mntonname,
   1600  1.264    bouyer 			    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
   1601  1.264    bouyer 			if ((mp->mnt_flag & MNT_FORCE) == 0) {
   1602  1.264    bouyer 				error = EINVAL;
   1603  1.319      maxv 				DPRINTF("quota disabled %d", error);
   1604  1.303  christos 				goto out1;
   1605  1.264    bouyer 			}
   1606  1.264    bouyer 		}
   1607  1.264    bouyer #endif
   1608  1.268      manu 	 }
   1609  1.268      manu 
   1610  1.279  drochner 	if (mp->mnt_flag & MNT_DISCARD)
   1611  1.279  drochner 		ump->um_discarddata = ffs_discard_init(devvp, fs);
   1612  1.279  drochner 
   1613    1.1   mycroft 	return (0);
   1614  1.303  christos out1:
   1615  1.303  christos 	kmem_free(fs->fs_csp, allocsbsize);
   1616    1.1   mycroft out:
   1617  1.231    simonb #ifdef WAPBL
   1618  1.231    simonb 	if (mp->mnt_wapbl_replay) {
   1619  1.240     joerg 		wapbl_replay_stop(mp->mnt_wapbl_replay);
   1620  1.231    simonb 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1621  1.231    simonb 		mp->mnt_wapbl_replay = 0;
   1622  1.231    simonb 	}
   1623  1.231    simonb #endif
   1624  1.231    simonb 
   1625  1.128       dbj 	if (fs)
   1626  1.273      para 		kmem_free(fs, fs->fs_sbsize);
   1627  1.289   hannken 	spec_node_setmountedfs(devvp, NULL);
   1628    1.1   mycroft 	if (bp)
   1629  1.209        ad 		brelse(bp, bset);
   1630    1.1   mycroft 	if (ump) {
   1631  1.131       dbj 		if (ump->um_oldfscompat)
   1632  1.273      para 			kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t));
   1633  1.209        ad 		mutex_destroy(&ump->um_lock);
   1634  1.273      para 		kmem_free(ump, sizeof(*ump));
   1635  1.100     soren 		mp->mnt_data = NULL;
   1636    1.1   mycroft 	}
   1637    1.1   mycroft 	return (error);
   1638    1.1   mycroft }
   1639    1.1   mycroft 
   1640    1.1   mycroft /*
   1641  1.110      fvdl  * Sanity checks for loading old filesystem superblocks.
   1642  1.110      fvdl  * See ffs_oldfscompat_write below for unwound actions.
   1643    1.1   mycroft  *
   1644  1.110      fvdl  * XXX - Parts get retired eventually.
   1645  1.110      fvdl  * Unfortunately new bits get added.
   1646    1.1   mycroft  */
   1647  1.110      fvdl static void
   1648  1.166   thorpej ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
   1649  1.110      fvdl {
   1650  1.110      fvdl 	off_t maxfilesize;
   1651  1.131       dbj 	int32_t *extrasave;
   1652  1.110      fvdl 
   1653  1.131       dbj 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1654  1.131       dbj 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1655  1.111      fvdl 		return;
   1656  1.111      fvdl 
   1657  1.131       dbj 	if (!ump->um_oldfscompat)
   1658  1.273      para 		ump->um_oldfscompat = kmem_alloc(512 + 3*sizeof(int32_t),
   1659  1.273      para 		    KM_SLEEP);
   1660  1.131       dbj 
   1661  1.131       dbj 	memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
   1662  1.131       dbj 	extrasave = ump->um_oldfscompat;
   1663  1.131       dbj 	extrasave += 512/sizeof(int32_t);
   1664  1.131       dbj 	extrasave[0] = fs->fs_old_npsect;
   1665  1.131       dbj 	extrasave[1] = fs->fs_old_interleave;
   1666  1.131       dbj 	extrasave[2] = fs->fs_old_trackskew;
   1667  1.131       dbj 
   1668  1.131       dbj 	/* These fields will be overwritten by their
   1669  1.131       dbj 	 * original values in fs_oldfscompat_write, so it is harmless
   1670  1.131       dbj 	 * to modify them here.
   1671  1.131       dbj 	 */
   1672  1.131       dbj 	fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
   1673  1.131       dbj 	fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
   1674  1.131       dbj 	fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
   1675  1.131       dbj 	fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
   1676  1.131       dbj 
   1677  1.131       dbj 	fs->fs_maxbsize = fs->fs_bsize;
   1678  1.131       dbj 	fs->fs_time = fs->fs_old_time;
   1679  1.131       dbj 	fs->fs_size = fs->fs_old_size;
   1680  1.131       dbj 	fs->fs_dsize = fs->fs_old_dsize;
   1681  1.131       dbj 	fs->fs_csaddr = fs->fs_old_csaddr;
   1682  1.131       dbj 	fs->fs_sblockloc = sblockloc;
   1683  1.131       dbj 
   1684  1.231    simonb 	fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
   1685  1.122     enami 
   1686  1.131       dbj 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
   1687  1.131       dbj 		fs->fs_old_nrpos = 8;
   1688  1.131       dbj 		fs->fs_old_npsect = fs->fs_old_nsect;
   1689  1.131       dbj 		fs->fs_old_interleave = 1;
   1690  1.131       dbj 		fs->fs_old_trackskew = 0;
   1691  1.111      fvdl 	}
   1692  1.111      fvdl 
   1693  1.340    martin 	if (fs->fs_magic == FS_UFS1_MAGIC &&
   1694  1.340    martin 	    fs->fs_old_inodefmt < FS_44INODEFMT) {
   1695  1.201   tsutsui 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
   1696  1.110      fvdl 		fs->fs_qbmask = ~fs->fs_bmask;
   1697  1.110      fvdl 		fs->fs_qfmask = ~fs->fs_fmask;
   1698  1.110      fvdl 	}
   1699  1.111      fvdl 
   1700  1.111      fvdl 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1701  1.201   tsutsui 	if (fs->fs_maxfilesize > maxfilesize)
   1702  1.201   tsutsui 		fs->fs_maxfilesize = maxfilesize;
   1703  1.111      fvdl 
   1704  1.110      fvdl 	/* Compatibility for old filesystems */
   1705  1.110      fvdl 	if (fs->fs_avgfilesize <= 0)
   1706  1.110      fvdl 		fs->fs_avgfilesize = AVFILESIZ;
   1707  1.110      fvdl 	if (fs->fs_avgfpdir <= 0)
   1708  1.110      fvdl 		fs->fs_avgfpdir = AFPDIR;
   1709  1.131       dbj 
   1710  1.110      fvdl #if 0
   1711  1.110      fvdl 	if (bigcgs) {
   1712  1.110      fvdl 		fs->fs_save_cgsize = fs->fs_cgsize;
   1713  1.110      fvdl 		fs->fs_cgsize = fs->fs_bsize;
   1714  1.110      fvdl 	}
   1715  1.110      fvdl #endif
   1716  1.110      fvdl }
   1717  1.110      fvdl 
   1718  1.110      fvdl /*
   1719  1.110      fvdl  * Unwinding superblock updates for old filesystems.
   1720  1.110      fvdl  * See ffs_oldfscompat_read above for details.
   1721  1.110      fvdl  *
   1722  1.110      fvdl  * XXX - Parts get retired eventually.
   1723  1.110      fvdl  * Unfortunately new bits get added.
   1724  1.110      fvdl  */
   1725  1.110      fvdl static void
   1726  1.166   thorpej ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
   1727    1.1   mycroft {
   1728  1.131       dbj 	int32_t *extrasave;
   1729  1.131       dbj 
   1730  1.131       dbj 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1731  1.131       dbj 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1732  1.111      fvdl 		return;
   1733  1.115      fvdl 
   1734  1.111      fvdl 	fs->fs_old_time = fs->fs_time;
   1735  1.111      fvdl 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1736  1.111      fvdl 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1737  1.111      fvdl 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1738  1.111      fvdl 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1739  1.131       dbj 	fs->fs_old_flags = fs->fs_flags;
   1740  1.111      fvdl 
   1741  1.110      fvdl #if 0
   1742  1.110      fvdl 	if (bigcgs) {
   1743  1.110      fvdl 		fs->fs_cgsize = fs->fs_save_cgsize;
   1744  1.110      fvdl 	}
   1745  1.110      fvdl #endif
   1746  1.131       dbj 
   1747  1.131       dbj 	memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
   1748  1.131       dbj 	extrasave = ump->um_oldfscompat;
   1749  1.131       dbj 	extrasave += 512/sizeof(int32_t);
   1750  1.131       dbj 	fs->fs_old_npsect = extrasave[0];
   1751  1.131       dbj 	fs->fs_old_interleave = extrasave[1];
   1752  1.131       dbj 	fs->fs_old_trackskew = extrasave[2];
   1753  1.131       dbj 
   1754    1.1   mycroft }
   1755    1.1   mycroft 
   1756    1.1   mycroft /*
   1757  1.250     pooka  * unmount vfs operation
   1758    1.1   mycroft  */
   1759    1.1   mycroft int
   1760  1.211     pooka ffs_unmount(struct mount *mp, int mntflags)
   1761    1.1   mycroft {
   1762  1.211     pooka 	struct lwp *l = curlwp;
   1763  1.170   thorpej 	struct ufsmount *ump = VFSTOUFS(mp);
   1764  1.170   thorpej 	struct fs *fs = ump->um_fs;
   1765  1.243        ad 	int error, flags;
   1766  1.273      para 	u_int32_t bsize;
   1767  1.231    simonb #ifdef WAPBL
   1768  1.231    simonb 	extern int doforce;
   1769  1.231    simonb #endif
   1770    1.1   mycroft 
   1771  1.279  drochner 	if (ump->um_discarddata) {
   1772  1.279  drochner 		ffs_discard_finish(ump->um_discarddata, mntflags);
   1773  1.279  drochner 		ump->um_discarddata = NULL;
   1774  1.279  drochner 	}
   1775  1.279  drochner 
   1776    1.1   mycroft 	flags = 0;
   1777   1.11   mycroft 	if (mntflags & MNT_FORCE)
   1778    1.1   mycroft 		flags |= FORCECLOSE;
   1779  1.243        ad 	if ((error = ffs_flushfiles(mp, flags, l)) != 0)
   1780  1.243        ad 		return (error);
   1781  1.231    simonb 	error = UFS_WAPBL_BEGIN(mp);
   1782  1.231    simonb 	if (error == 0)
   1783  1.231    simonb 		if (fs->fs_ronly == 0 &&
   1784  1.231    simonb 		    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1785  1.231    simonb 		    fs->fs_clean & FS_WASCLEAN) {
   1786  1.243        ad 			fs->fs_clean = FS_ISCLEAN;
   1787  1.231    simonb 			fs->fs_fmod = 0;
   1788  1.231    simonb 			(void) ffs_sbupdate(ump, MNT_WAIT);
   1789   1.91      fvdl 		}
   1790  1.231    simonb 	if (error == 0)
   1791  1.231    simonb 		UFS_WAPBL_END(mp);
   1792  1.231    simonb #ifdef WAPBL
   1793  1.231    simonb 	KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
   1794  1.231    simonb 	if (mp->mnt_wapbl_replay) {
   1795  1.231    simonb 		KDASSERT(fs->fs_ronly);
   1796  1.231    simonb 		wapbl_replay_stop(mp->mnt_wapbl_replay);
   1797  1.231    simonb 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1798  1.231    simonb 		mp->mnt_wapbl_replay = 0;
   1799  1.231    simonb 	}
   1800  1.231    simonb 	error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
   1801  1.231    simonb 	if (error) {
   1802  1.231    simonb 		return error;
   1803   1.15   mycroft 	}
   1804  1.231    simonb #endif /* WAPBL */
   1805  1.250     pooka 
   1806   1.54     enami 	if (ump->um_devvp->v_type != VBAD)
   1807  1.289   hannken 		spec_node_setmountedfs(ump->um_devvp, NULL);
   1808   1.53  wrstuden 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1809  1.231    simonb 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
   1810  1.211     pooka 		NOCRED);
   1811   1.53  wrstuden 	vput(ump->um_devvp);
   1812  1.273      para 
   1813  1.273      para 	bsize = fs->fs_cssize;
   1814  1.273      para 	if (fs->fs_contigsumsize > 0)
   1815  1.273      para 		bsize += fs->fs_ncg * sizeof(int32_t);
   1816  1.273      para 	bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1817  1.273      para 	kmem_free(fs->fs_csp, bsize);
   1818  1.273      para 
   1819  1.273      para 	kmem_free(fs, fs->fs_sbsize);
   1820  1.131       dbj 	if (ump->um_oldfscompat != NULL)
   1821  1.273      para 		kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t));
   1822  1.209        ad 	mutex_destroy(&ump->um_lock);
   1823  1.223   hannken 	ffs_snapshot_fini(ump);
   1824  1.273      para 	kmem_free(ump, sizeof(*ump));
   1825  1.100     soren 	mp->mnt_data = NULL;
   1826    1.1   mycroft 	mp->mnt_flag &= ~MNT_LOCAL;
   1827  1.129       dbj 	return (0);
   1828    1.1   mycroft }
   1829    1.1   mycroft 
   1830    1.1   mycroft /*
   1831    1.1   mycroft  * Flush out all the files in a filesystem.
   1832    1.1   mycroft  */
   1833   1.19  christos int
   1834  1.177  christos ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
   1835    1.1   mycroft {
   1836    1.1   mycroft 	extern int doforce;
   1837   1.61  augustss 	struct ufsmount *ump;
   1838   1.19  christos 	int error;
   1839    1.1   mycroft 
   1840    1.1   mycroft 	if (!doforce)
   1841    1.1   mycroft 		flags &= ~FORCECLOSE;
   1842    1.1   mycroft 	ump = VFSTOUFS(mp);
   1843    1.1   mycroft #ifdef QUOTA
   1844  1.264    bouyer 	if ((error = quota1_umount(mp, flags)) != 0)
   1845  1.264    bouyer 		return (error);
   1846  1.264    bouyer #endif
   1847  1.264    bouyer #ifdef QUOTA2
   1848  1.264    bouyer 	if ((error = quota2_umount(mp, flags)) != 0)
   1849  1.264    bouyer 		return (error);
   1850    1.1   mycroft #endif
   1851  1.278      manu #ifdef UFS_EXTATTR
   1852  1.278      manu 	if (ump->um_fstype == UFS1) {
   1853  1.278      manu 		if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)
   1854  1.278      manu 			ufs_extattr_stop(mp, l);
   1855  1.278      manu 		if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED)
   1856  1.278      manu 			ufs_extattr_uepm_destroy(&ump->um_extattr);
   1857  1.302      manu 		mp->mnt_flag &= ~MNT_EXTATTR;
   1858  1.278      manu 	}
   1859  1.278      manu #endif
   1860  1.149   hannken 	if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
   1861  1.149   hannken 		return (error);
   1862  1.149   hannken 	ffs_snapshot_unmount(mp);
   1863   1.55      fvdl 	/*
   1864   1.55      fvdl 	 * Flush all the files.
   1865   1.55      fvdl 	 */
   1866    1.1   mycroft 	error = vflush(mp, NULLVP, flags);
   1867   1.55      fvdl 	if (error)
   1868   1.55      fvdl 		return (error);
   1869   1.55      fvdl 	/*
   1870   1.55      fvdl 	 * Flush filesystem metadata.
   1871   1.55      fvdl 	 */
   1872   1.55      fvdl 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1873  1.211     pooka 	error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
   1874  1.259   hannken 	VOP_UNLOCK(ump->um_devvp);
   1875  1.231    simonb 	if (flags & FORCECLOSE) /* XXXDBJ */
   1876  1.231    simonb 		error = 0;
   1877  1.231    simonb 
   1878  1.231    simonb #ifdef WAPBL
   1879  1.231    simonb 	if (error)
   1880  1.231    simonb 		return error;
   1881  1.231    simonb 	if (mp->mnt_wapbl) {
   1882  1.231    simonb 		error = wapbl_flush(mp->mnt_wapbl, 1);
   1883  1.231    simonb 		if (flags & FORCECLOSE)
   1884  1.231    simonb 			error = 0;
   1885  1.231    simonb 	}
   1886  1.231    simonb #endif
   1887  1.231    simonb 
   1888    1.1   mycroft 	return (error);
   1889    1.1   mycroft }
   1890    1.1   mycroft 
   1891    1.1   mycroft /*
   1892    1.1   mycroft  * Get file system statistics.
   1893    1.1   mycroft  */
   1894    1.1   mycroft int
   1895  1.211     pooka ffs_statvfs(struct mount *mp, struct statvfs *sbp)
   1896    1.1   mycroft {
   1897   1.61  augustss 	struct ufsmount *ump;
   1898   1.61  augustss 	struct fs *fs;
   1899    1.1   mycroft 
   1900    1.1   mycroft 	ump = VFSTOUFS(mp);
   1901    1.1   mycroft 	fs = ump->um_fs;
   1902  1.209        ad 	mutex_enter(&ump->um_lock);
   1903  1.143  christos 	sbp->f_bsize = fs->fs_bsize;
   1904  1.143  christos 	sbp->f_frsize = fs->fs_fsize;
   1905    1.1   mycroft 	sbp->f_iosize = fs->fs_bsize;
   1906    1.1   mycroft 	sbp->f_blocks = fs->fs_dsize;
   1907  1.286  dholland 	sbp->f_bfree = ffs_blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1908  1.285  dholland 	    fs->fs_cstotal.cs_nffree + FFS_DBTOFSB(fs, fs->fs_pendingblocks);
   1909  1.143  christos 	sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
   1910  1.143  christos 	    fs->fs_minfree) / (u_int64_t) 100;
   1911  1.143  christos 	if (sbp->f_bfree > sbp->f_bresvd)
   1912  1.143  christos 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1913  1.143  christos 	else
   1914  1.143  christos 		sbp->f_bavail = 0;
   1915  1.282  dholland 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
   1916   1.89      fvdl 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1917  1.143  christos 	sbp->f_favail = sbp->f_ffree;
   1918  1.143  christos 	sbp->f_fresvd = 0;
   1919  1.209        ad 	mutex_exit(&ump->um_lock);
   1920  1.143  christos 	copy_statvfs_info(sbp, mp);
   1921  1.209        ad 
   1922    1.1   mycroft 	return (0);
   1923    1.1   mycroft }
   1924    1.1   mycroft 
   1925  1.299  christos struct ffs_sync_ctx {
   1926  1.299  christos 	int waitfor;
   1927  1.299  christos };
   1928  1.299  christos 
   1929  1.299  christos static bool
   1930  1.299  christos ffs_sync_selector(void *cl, struct vnode *vp)
   1931  1.299  christos {
   1932  1.299  christos 	struct ffs_sync_ctx *c = cl;
   1933  1.299  christos 	struct inode *ip;
   1934  1.299  christos 
   1935  1.351  riastrad 	KASSERT(mutex_owned(vp->v_interlock));
   1936  1.351  riastrad 
   1937  1.299  christos 	ip = VTOI(vp);
   1938  1.299  christos 	/*
   1939  1.299  christos 	 * Skip the vnode/inode if inaccessible.
   1940  1.299  christos 	 */
   1941  1.299  christos 	if (ip == NULL || vp->v_type == VNON)
   1942  1.299  christos 		return false;
   1943  1.299  christos 
   1944  1.299  christos 	/*
   1945  1.299  christos 	 * We deliberately update inode times here.  This will
   1946  1.299  christos 	 * prevent a massive queue of updates accumulating, only
   1947  1.299  christos 	 * to be handled by a call to unmount.
   1948  1.299  christos 	 *
   1949  1.299  christos 	 * XXX It would be better to have the syncer trickle these
   1950  1.299  christos 	 * out.  Adjustment needed to allow registering vnodes for
   1951  1.299  christos 	 * sync when the vnode is clean, but the inode dirty.  Or
   1952  1.299  christos 	 * have ufs itself trickle out inode updates.
   1953  1.299  christos 	 *
   1954  1.299  christos 	 * If doing a lazy sync, we don't care about metadata or
   1955  1.299  christos 	 * data updates, because they are handled by each vnode's
   1956  1.299  christos 	 * synclist entry.  In this case we are only interested in
   1957  1.299  christos 	 * writing back modified inodes.
   1958  1.299  christos 	 */
   1959  1.299  christos 	if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
   1960  1.299  christos 	    IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1961  1.299  christos 	    (c->waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1962  1.365        ad 	    (vp->v_iflag & VI_ONWORKLST) == 0)))
   1963  1.299  christos 		return false;
   1964  1.299  christos 
   1965  1.299  christos 	return true;
   1966  1.299  christos }
   1967  1.299  christos 
   1968    1.1   mycroft /*
   1969    1.1   mycroft  * Go through the disk queues to initiate sandbagged IO;
   1970    1.1   mycroft  * go through the inodes to write those that have been modified;
   1971    1.1   mycroft  * initiate the writing of the super block if it has been modified.
   1972    1.1   mycroft  *
   1973    1.1   mycroft  * Note: we are always called with the filesystem marked `MPBUSY'.
   1974    1.1   mycroft  */
   1975    1.1   mycroft int
   1976  1.211     pooka ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
   1977    1.1   mycroft {
   1978  1.294   hannken 	struct vnode *vp;
   1979   1.33      fvdl 	struct ufsmount *ump = VFSTOUFS(mp);
   1980   1.33      fvdl 	struct fs *fs;
   1981  1.294   hannken 	struct vnode_iterator *marker;
   1982  1.263   hannken 	int error, allerror = 0;
   1983  1.299  christos 	struct ffs_sync_ctx ctx;
   1984    1.1   mycroft 
   1985    1.1   mycroft 	fs = ump->um_fs;
   1986   1.33      fvdl 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1987  1.339  christos 		panic("%s: rofs mod, fs=%s", __func__, fs->fs_fsmnt);
   1988    1.1   mycroft 	}
   1989  1.214        ad 
   1990  1.253   hannken 	/*
   1991    1.1   mycroft 	 * Write back each (modified) inode.
   1992    1.1   mycroft 	 */
   1993  1.294   hannken 	vfs_vnode_iterator_init(mp, &marker);
   1994  1.299  christos 
   1995  1.299  christos 	ctx.waitfor = waitfor;
   1996  1.299  christos 	while ((vp = vfs_vnode_iterator_next(marker, ffs_sync_selector, &ctx)))
   1997  1.299  christos 	{
   1998  1.342   hannken 		error = vn_lock(vp,
   1999  1.342   hannken 		    LK_EXCLUSIVE | (waitfor == MNT_LAZY ? LK_NOWAIT : 0));
   2000  1.294   hannken 		if (error) {
   2001  1.294   hannken 			vrele(vp);
   2002  1.214        ad 			continue;
   2003  1.294   hannken 		}
   2004  1.245        ad 		if (waitfor == MNT_LAZY) {
   2005  1.231    simonb 			error = UFS_WAPBL_BEGIN(vp->v_mount);
   2006  1.231    simonb 			if (!error) {
   2007  1.244        ad 				error = ffs_update(vp, NULL, NULL,
   2008  1.244        ad 				    UPDATE_CLOSE);
   2009  1.231    simonb 				UFS_WAPBL_END(vp->v_mount);
   2010  1.231    simonb 			}
   2011  1.231    simonb 		} else {
   2012  1.231    simonb 			error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
   2013  1.231    simonb 			    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
   2014  1.231    simonb 		}
   2015  1.152   mycroft 		if (error)
   2016    1.1   mycroft 			allerror = error;
   2017  1.263   hannken 		vput(vp);
   2018    1.1   mycroft 	}
   2019  1.294   hannken 	vfs_vnode_iterator_destroy(marker);
   2020  1.294   hannken 
   2021    1.1   mycroft 	/*
   2022    1.1   mycroft 	 * Force stale file system control information to be flushed.
   2023    1.1   mycroft 	 */
   2024  1.132   hannken 	if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
   2025  1.132   hannken 	    !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
   2026   1.55      fvdl 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   2027   1.55      fvdl 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   2028  1.231    simonb 		    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
   2029  1.231    simonb 		    0, 0)) != 0)
   2030   1.55      fvdl 			allerror = error;
   2031  1.259   hannken 		VOP_UNLOCK(ump->um_devvp);
   2032   1.55      fvdl 	}
   2033  1.264    bouyer #if defined(QUOTA) || defined(QUOTA2)
   2034  1.118      fvdl 	qsync(mp);
   2035    1.1   mycroft #endif
   2036   1.33      fvdl 	/*
   2037   1.33      fvdl 	 * Write back modified superblock.
   2038   1.33      fvdl 	 */
   2039   1.33      fvdl 	if (fs->fs_fmod != 0) {
   2040   1.33      fvdl 		fs->fs_fmod = 0;
   2041  1.182    kardel 		fs->fs_time = time_second;
   2042  1.231    simonb 		error = UFS_WAPBL_BEGIN(mp);
   2043  1.231    simonb 		if (error)
   2044   1.64   mycroft 			allerror = error;
   2045  1.231    simonb 		else {
   2046  1.231    simonb 			if ((error = ffs_cgupdate(ump, waitfor)))
   2047  1.231    simonb 				allerror = error;
   2048  1.239     joerg 			UFS_WAPBL_END(mp);
   2049  1.231    simonb 		}
   2050   1.33      fvdl 	}
   2051  1.231    simonb 
   2052  1.231    simonb #ifdef WAPBL
   2053  1.231    simonb 	if (mp->mnt_wapbl) {
   2054  1.343   hannken 		error = wapbl_flush(mp->mnt_wapbl, (waitfor == MNT_WAIT));
   2055  1.231    simonb 		if (error)
   2056  1.231    simonb 			allerror = error;
   2057  1.231    simonb 	}
   2058  1.231    simonb #endif
   2059  1.231    simonb 
   2060    1.1   mycroft 	return (allerror);
   2061    1.1   mycroft }
   2062    1.1   mycroft 
   2063    1.1   mycroft /*
   2064  1.325   hannken  * Load inode from disk and initialize vnode.
   2065    1.1   mycroft  */
   2066  1.325   hannken static int
   2067  1.325   hannken ffs_init_vnode(struct ufsmount *ump, struct vnode *vp, ino_t ino)
   2068    1.1   mycroft {
   2069   1.33      fvdl 	struct fs *fs;
   2070   1.33      fvdl 	struct inode *ip;
   2071    1.1   mycroft 	struct buf *bp;
   2072   1.43   thorpej 	int error;
   2073    1.1   mycroft 
   2074  1.298   hannken 	fs = ump->um_fs;
   2075   1.68      fvdl 
   2076  1.298   hannken 	/* Read in the disk contents for the inode. */
   2077  1.298   hannken 	error = bread(ump->um_devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ino)),
   2078  1.327      maxv 		      (int)fs->fs_bsize, 0, &bp);
   2079  1.298   hannken 	if (error)
   2080  1.298   hannken 		return error;
   2081    1.1   mycroft 
   2082  1.298   hannken 	/* Allocate and initialize inode. */
   2083  1.216        ad 	ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
   2084  1.298   hannken 	memset(ip, 0, sizeof(struct inode));
   2085  1.110      fvdl 	ip->i_ump = ump;
   2086  1.298   hannken 	ip->i_fs = fs;
   2087  1.325   hannken 	ip->i_dev = ump->um_dev;
   2088    1.1   mycroft 	ip->i_number = ino;
   2089  1.325   hannken 	if (ump->um_fstype == UFS1)
   2090  1.325   hannken 		ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
   2091  1.325   hannken 		    PR_WAITOK);
   2092  1.325   hannken 	else
   2093  1.325   hannken 		ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
   2094  1.325   hannken 		    PR_WAITOK);
   2095  1.325   hannken 	ffs_load_inode(bp, ip, fs, ino);
   2096  1.325   hannken 	brelse(bp, 0);
   2097  1.325   hannken 	ip->i_vnode = vp;
   2098  1.264    bouyer #if defined(QUOTA) || defined(QUOTA2)
   2099  1.203   hannken 	ufsquota_init(ip);
   2100    1.1   mycroft #endif
   2101   1.86       chs 
   2102  1.325   hannken 	/* Initialise vnode with this inode. */
   2103  1.325   hannken 	vp->v_tag = VT_UFS;
   2104  1.325   hannken 	vp->v_op = ffs_vnodeop_p;
   2105  1.325   hannken 	vp->v_data = ip;
   2106  1.325   hannken 
   2107  1.298   hannken 	/* Initialize genfs node. */
   2108  1.213    dyoung 	genfs_node_init(vp, &ffs_genfsops);
   2109  1.213    dyoung 
   2110  1.325   hannken 	return 0;
   2111  1.325   hannken }
   2112  1.325   hannken 
   2113  1.325   hannken /*
   2114  1.325   hannken  * Undo ffs_init_vnode().
   2115  1.325   hannken  */
   2116  1.325   hannken static void
   2117  1.325   hannken ffs_deinit_vnode(struct ufsmount *ump, struct vnode *vp)
   2118  1.325   hannken {
   2119  1.325   hannken 	struct inode *ip = VTOI(vp);
   2120  1.325   hannken 
   2121  1.356   hannken 	genfs_node_destroy(vp);
   2122  1.356   hannken 	vp->v_data = NULL;
   2123  1.356   hannken 
   2124  1.325   hannken 	if (ump->um_fstype == UFS1)
   2125  1.325   hannken 		pool_cache_put(ffs_dinode1_cache, ip->i_din.ffs1_din);
   2126  1.110      fvdl 	else
   2127  1.325   hannken 		pool_cache_put(ffs_dinode2_cache, ip->i_din.ffs2_din);
   2128  1.325   hannken 	pool_cache_put(ffs_inode_cache, ip);
   2129  1.325   hannken }
   2130  1.325   hannken 
   2131  1.325   hannken /*
   2132  1.325   hannken  * Read an inode from disk and initialize this vnode / inode pair.
   2133  1.325   hannken  * Caller assures no other thread will try to load this inode.
   2134  1.325   hannken  */
   2135  1.325   hannken int
   2136  1.325   hannken ffs_loadvnode(struct mount *mp, struct vnode *vp,
   2137  1.325   hannken     const void *key, size_t key_len, const void **new_key)
   2138  1.325   hannken {
   2139  1.325   hannken 	ino_t ino;
   2140  1.325   hannken 	struct fs *fs;
   2141  1.325   hannken 	struct inode *ip;
   2142  1.325   hannken 	struct ufsmount *ump;
   2143  1.325   hannken 	int error;
   2144  1.325   hannken 
   2145  1.325   hannken 	KASSERT(key_len == sizeof(ino));
   2146  1.325   hannken 	memcpy(&ino, key, key_len);
   2147  1.325   hannken 	ump = VFSTOUFS(mp);
   2148  1.325   hannken 	fs = ump->um_fs;
   2149  1.325   hannken 
   2150  1.325   hannken 	error = ffs_init_vnode(ump, vp, ino);
   2151  1.325   hannken 	if (error)
   2152  1.325   hannken 		return error;
   2153  1.325   hannken 
   2154  1.325   hannken 	ip = VTOI(vp);
   2155  1.325   hannken 	if (ip->i_mode == 0) {
   2156  1.325   hannken 		ffs_deinit_vnode(ump, vp);
   2157  1.325   hannken 
   2158  1.325   hannken 		return ENOENT;
   2159  1.325   hannken 	}
   2160    1.1   mycroft 
   2161  1.298   hannken 	/* Initialize the vnode from the inode. */
   2162   1.87       chs 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   2163   1.87       chs 
   2164  1.298   hannken 	/* Finish inode initialization.  */
   2165    1.1   mycroft 	ip->i_devvp = ump->um_devvp;
   2166  1.254     pooka 	vref(ip->i_devvp);
   2167   1.87       chs 
   2168    1.1   mycroft 	/*
   2169    1.1   mycroft 	 * Ensure that uid and gid are correct. This is a temporary
   2170    1.1   mycroft 	 * fix until fsck has been changed to do the update.
   2171    1.1   mycroft 	 */
   2172   1.87       chs 
   2173  1.340    martin 	if (fs->fs_magic == FS_UFS1_MAGIC &&			/* XXX */
   2174  1.340    martin 	    fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   2175  1.110      fvdl 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   2176  1.110      fvdl 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   2177   1.38    kleink 	}							/* XXX */
   2178  1.110      fvdl 	uvm_vnp_setsize(vp, ip->i_size);
   2179  1.372  christos 	cache_enter_id(vp, ip->i_mode, ip->i_uid, ip->i_gid, !HAS_ACLS(ip));
   2180  1.298   hannken 	*new_key = &ip->i_number;
   2181  1.298   hannken 	return 0;
   2182    1.1   mycroft }
   2183    1.1   mycroft 
   2184    1.1   mycroft /*
   2185  1.325   hannken  * Create a new inode on disk and initialize this vnode / inode pair.
   2186  1.325   hannken  */
   2187  1.325   hannken int
   2188  1.325   hannken ffs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp,
   2189  1.361   hannken     struct vattr *vap, kauth_cred_t cred, void *extra,
   2190  1.325   hannken     size_t *key_len, const void **new_key)
   2191  1.325   hannken {
   2192  1.325   hannken 	ino_t ino;
   2193  1.325   hannken 	struct fs *fs;
   2194  1.325   hannken 	struct inode *ip;
   2195  1.325   hannken 	struct timespec ts;
   2196  1.325   hannken 	struct ufsmount *ump;
   2197  1.325   hannken 	int error, mode;
   2198  1.325   hannken 
   2199  1.325   hannken 	KASSERT(dvp->v_mount == mp);
   2200  1.325   hannken 	KASSERT(vap->va_type != VNON);
   2201  1.325   hannken 
   2202  1.325   hannken 	*key_len = sizeof(ino);
   2203  1.325   hannken 	ump = VFSTOUFS(mp);
   2204  1.325   hannken 	fs = ump->um_fs;
   2205  1.325   hannken 	mode = MAKEIMODE(vap->va_type, vap->va_mode);
   2206  1.325   hannken 
   2207  1.325   hannken 	/* Allocate fresh inode. */
   2208  1.325   hannken 	error = ffs_valloc(dvp, mode, cred, &ino);
   2209  1.325   hannken 	if (error)
   2210  1.325   hannken 		return error;
   2211  1.325   hannken 
   2212  1.325   hannken 	/* Attach inode to vnode. */
   2213  1.325   hannken 	error = ffs_init_vnode(ump, vp, ino);
   2214  1.325   hannken 	if (error) {
   2215  1.325   hannken 		if (UFS_WAPBL_BEGIN(mp) == 0) {
   2216  1.325   hannken 			ffs_vfree(dvp, ino, mode);
   2217  1.325   hannken 			UFS_WAPBL_END(mp);
   2218  1.325   hannken 		}
   2219  1.325   hannken 		return error;
   2220  1.325   hannken 	}
   2221  1.325   hannken 
   2222  1.325   hannken 	ip = VTOI(vp);
   2223  1.339  christos 	if (ip->i_mode) {
   2224  1.354      maya 		panic("%s: dup alloc ino=%" PRId64 " on %s: mode %o/%o "
   2225  1.339  christos 		    "gen %x/%x size %" PRIx64 " blocks %" PRIx64,
   2226  1.339  christos 		    __func__, ino, fs->fs_fsmnt, DIP(ip, mode), ip->i_mode,
   2227  1.339  christos 		    DIP(ip, gen), ip->i_gen, DIP(ip, size), DIP(ip, blocks));
   2228  1.339  christos 	}
   2229  1.339  christos 	if (DIP(ip, size) || DIP(ip, blocks)) {
   2230  1.339  christos 		printf("%s: ino=%" PRId64 " on %s: "
   2231  1.339  christos 		    "gen %x/%x has non zero blocks %" PRIx64 " or size %"
   2232  1.339  christos 		    PRIx64 "\n",
   2233  1.339  christos 		    __func__, ino, fs->fs_fsmnt, DIP(ip, gen), ip->i_gen,
   2234  1.339  christos 		    DIP(ip, blocks), DIP(ip, size));
   2235  1.339  christos 		if ((ip)->i_ump->um_fstype == UFS1)
   2236  1.339  christos 			panic("%s: dirty filesystem?", __func__);
   2237  1.339  christos 		DIP_ASSIGN(ip, blocks, 0);
   2238  1.339  christos 		DIP_ASSIGN(ip, size, 0);
   2239  1.325   hannken 	}
   2240  1.325   hannken 
   2241  1.325   hannken 	/* Set uid / gid. */
   2242  1.325   hannken 	if (cred == NOCRED || cred == FSCRED) {
   2243  1.325   hannken 		ip->i_gid = 0;
   2244  1.325   hannken 		ip->i_uid = 0;
   2245  1.325   hannken 	} else {
   2246  1.325   hannken 		ip->i_gid = VTOI(dvp)->i_gid;
   2247  1.325   hannken 		ip->i_uid = kauth_cred_geteuid(cred);
   2248  1.325   hannken 	}
   2249  1.325   hannken 	DIP_ASSIGN(ip, gid, ip->i_gid);
   2250  1.325   hannken 	DIP_ASSIGN(ip, uid, ip->i_uid);
   2251  1.325   hannken 
   2252  1.325   hannken #if defined(QUOTA) || defined(QUOTA2)
   2253  1.325   hannken 	error = UFS_WAPBL_BEGIN(mp);
   2254  1.325   hannken 	if (error) {
   2255  1.325   hannken 		ffs_deinit_vnode(ump, vp);
   2256  1.325   hannken 
   2257  1.325   hannken 		return error;
   2258  1.325   hannken 	}
   2259  1.325   hannken 	error = chkiq(ip, 1, cred, 0);
   2260  1.325   hannken 	if (error) {
   2261  1.325   hannken 		ffs_vfree(dvp, ino, mode);
   2262  1.325   hannken 		UFS_WAPBL_END(mp);
   2263  1.325   hannken 		ffs_deinit_vnode(ump, vp);
   2264  1.325   hannken 
   2265  1.325   hannken 		return error;
   2266  1.325   hannken 	}
   2267  1.325   hannken 	UFS_WAPBL_END(mp);
   2268  1.325   hannken #endif
   2269  1.325   hannken 
   2270  1.325   hannken 	/* Set type and finalize. */
   2271  1.325   hannken 	ip->i_flags = 0;
   2272  1.325   hannken 	DIP_ASSIGN(ip, flags, 0);
   2273  1.325   hannken 	ip->i_mode = mode;
   2274  1.325   hannken 	DIP_ASSIGN(ip, mode, mode);
   2275  1.325   hannken 	if (vap->va_rdev != VNOVAL) {
   2276  1.325   hannken 		/*
   2277  1.325   hannken 		 * Want to be able to use this to make badblock
   2278  1.325   hannken 		 * inodes, so don't truncate the dev number.
   2279  1.325   hannken 		 */
   2280  1.325   hannken 		if (ump->um_fstype == UFS1)
   2281  1.325   hannken 			ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev,
   2282  1.325   hannken 			    UFS_MPNEEDSWAP(ump));
   2283  1.325   hannken 		else
   2284  1.325   hannken 			ip->i_ffs2_rdev = ufs_rw64(vap->va_rdev,
   2285  1.325   hannken 			    UFS_MPNEEDSWAP(ump));
   2286  1.325   hannken 	}
   2287  1.325   hannken 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   2288  1.325   hannken 	ip->i_devvp = ump->um_devvp;
   2289  1.325   hannken 	vref(ip->i_devvp);
   2290  1.325   hannken 
   2291  1.325   hannken 	/* Set up a new generation number for this inode.  */
   2292  1.325   hannken 	ip->i_gen++;
   2293  1.325   hannken 	DIP_ASSIGN(ip, gen, ip->i_gen);
   2294  1.325   hannken 	if (fs->fs_magic == FS_UFS2_MAGIC) {
   2295  1.325   hannken 		vfs_timestamp(&ts);
   2296  1.325   hannken 		ip->i_ffs2_birthtime = ts.tv_sec;
   2297  1.325   hannken 		ip->i_ffs2_birthnsec = ts.tv_nsec;
   2298  1.325   hannken 	}
   2299  1.325   hannken 
   2300  1.325   hannken 	uvm_vnp_setsize(vp, ip->i_size);
   2301  1.372  christos 	cache_enter_id(vp, ip->i_mode, ip->i_uid, ip->i_gid, !HAS_ACLS(ip));
   2302  1.325   hannken 	*new_key = &ip->i_number;
   2303  1.325   hannken 	return 0;
   2304  1.325   hannken }
   2305  1.325   hannken 
   2306  1.325   hannken /*
   2307    1.1   mycroft  * File handle to vnode
   2308    1.1   mycroft  *
   2309    1.1   mycroft  * Have to be really careful about stale file handles:
   2310    1.1   mycroft  * - check that the inode number is valid
   2311    1.1   mycroft  * - call ffs_vget() to get the locked inode
   2312    1.1   mycroft  * - check for an unallocated inode (i_mode == 0)
   2313    1.1   mycroft  * - check that the given client host has export rights and return
   2314    1.1   mycroft  *   those rights via. exflagsp and credanonp
   2315    1.1   mycroft  */
   2316    1.1   mycroft int
   2317  1.363        ad ffs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
   2318    1.1   mycroft {
   2319  1.183    martin 	struct ufid ufh;
   2320  1.338  christos 	int error;
   2321    1.1   mycroft 
   2322  1.183    martin 	if (fhp->fid_len != sizeof(struct ufid))
   2323  1.183    martin 		return EINVAL;
   2324  1.183    martin 
   2325  1.183    martin 	memcpy(&ufh, fhp, sizeof(ufh));
   2326  1.338  christos 	if ((error = ffs_checkrange(mp, ufh.ufid_ino)) != 0)
   2327  1.338  christos 		return error;
   2328  1.338  christos 
   2329  1.363        ad 	return (ufs_fhtovp(mp, &ufh, lktype, vpp));
   2330    1.1   mycroft }
   2331    1.1   mycroft 
   2332    1.1   mycroft /*
   2333    1.1   mycroft  * Vnode pointer to File handle
   2334    1.1   mycroft  */
   2335    1.1   mycroft /* ARGSUSED */
   2336   1.19  christos int
   2337  1.183    martin ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   2338    1.1   mycroft {
   2339   1.61  augustss 	struct inode *ip;
   2340  1.183    martin 	struct ufid ufh;
   2341    1.1   mycroft 
   2342  1.183    martin 	if (*fh_size < sizeof(struct ufid)) {
   2343  1.183    martin 		*fh_size = sizeof(struct ufid);
   2344  1.183    martin 		return E2BIG;
   2345  1.183    martin 	}
   2346    1.1   mycroft 	ip = VTOI(vp);
   2347  1.183    martin 	*fh_size = sizeof(struct ufid);
   2348  1.183    martin 	memset(&ufh, 0, sizeof(ufh));
   2349  1.183    martin 	ufh.ufid_len = sizeof(struct ufid);
   2350  1.183    martin 	ufh.ufid_ino = ip->i_number;
   2351  1.183    martin 	ufh.ufid_gen = ip->i_gen;
   2352  1.183    martin 	memcpy(fhp, &ufh, sizeof(ufh));
   2353    1.1   mycroft 	return (0);
   2354   1.33      fvdl }
   2355   1.33      fvdl 
   2356   1.33      fvdl void
   2357  1.166   thorpej ffs_init(void)
   2358   1.33      fvdl {
   2359   1.59  jdolecek 	if (ffs_initcount++ > 0)
   2360   1.59  jdolecek 		return;
   2361   1.59  jdolecek 
   2362  1.216        ad 	ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
   2363  1.216        ad 	    "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
   2364  1.216        ad 	ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
   2365  1.216        ad 	    "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
   2366  1.216        ad 	ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
   2367  1.216        ad 	    "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
   2368   1.33      fvdl 	ufs_init();
   2369   1.86       chs }
   2370   1.86       chs 
   2371   1.86       chs void
   2372  1.166   thorpej ffs_reinit(void)
   2373   1.86       chs {
   2374   1.86       chs 	ufs_reinit();
   2375   1.59  jdolecek }
   2376   1.59  jdolecek 
   2377   1.59  jdolecek void
   2378  1.166   thorpej ffs_done(void)
   2379   1.59  jdolecek {
   2380   1.59  jdolecek 	if (--ffs_initcount > 0)
   2381   1.59  jdolecek 		return;
   2382   1.59  jdolecek 
   2383   1.59  jdolecek 	ufs_done();
   2384  1.216        ad 	pool_cache_destroy(ffs_dinode2_cache);
   2385  1.216        ad 	pool_cache_destroy(ffs_dinode1_cache);
   2386  1.216        ad 	pool_cache_destroy(ffs_inode_cache);
   2387   1.33      fvdl }
   2388   1.33      fvdl 
   2389    1.1   mycroft /*
   2390    1.1   mycroft  * Write a superblock and associated information back to disk.
   2391    1.1   mycroft  */
   2392    1.1   mycroft int
   2393  1.166   thorpej ffs_sbupdate(struct ufsmount *mp, int waitfor)
   2394    1.1   mycroft {
   2395   1.61  augustss 	struct fs *fs = mp->um_fs;
   2396   1.61  augustss 	struct buf *bp;
   2397  1.335      maxv 	int error;
   2398  1.110      fvdl 	u_int32_t saveflag;
   2399   1.34    bouyer 
   2400  1.229   hannken 	error = ffs_getblk(mp->um_devvp,
   2401  1.257   mlelstv 	    fs->fs_sblockloc / DEV_BSIZE, FFS_NOBLK,
   2402  1.229   hannken 	    fs->fs_sbsize, false, &bp);
   2403  1.229   hannken 	if (error)
   2404  1.229   hannken 		return error;
   2405   1.55      fvdl 	saveflag = fs->fs_flags & FS_INTERNAL;
   2406   1.55      fvdl 	fs->fs_flags &= ~FS_INTERNAL;
   2407  1.161     perry 
   2408   1.42     perry 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   2409  1.110      fvdl 
   2410  1.110      fvdl 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   2411  1.378       chs 	if (mp->um_flags & UFS_EA) {
   2412  1.378       chs 		struct fs *bfs = (struct fs *)bp->b_data;
   2413  1.378       chs 		KASSERT(bfs->fs_magic == FS_UFS2_MAGIC);
   2414  1.378       chs 		bfs->fs_magic = FS_UFS2EA_MAGIC;
   2415  1.378       chs 	}
   2416   1.34    bouyer #ifdef FFS_EI
   2417   1.34    bouyer 	if (mp->um_flags & UFS_NEEDSWAP)
   2418  1.123     enami 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
   2419  1.111      fvdl #endif
   2420   1.55      fvdl 	fs->fs_flags |= saveflag;
   2421   1.34    bouyer 
   2422    1.1   mycroft 	if (waitfor == MNT_WAIT)
   2423    1.1   mycroft 		error = bwrite(bp);
   2424    1.1   mycroft 	else
   2425    1.1   mycroft 		bawrite(bp);
   2426   1.15   mycroft 	return (error);
   2427   1.15   mycroft }
   2428   1.15   mycroft 
   2429   1.15   mycroft int
   2430  1.166   thorpej ffs_cgupdate(struct ufsmount *mp, int waitfor)
   2431   1.15   mycroft {
   2432   1.61  augustss 	struct fs *fs = mp->um_fs;
   2433   1.61  augustss 	struct buf *bp;
   2434   1.15   mycroft 	int blks;
   2435   1.84     lukem 	void *space;
   2436   1.15   mycroft 	int i, size, error = 0, allerror = 0;
   2437   1.15   mycroft 
   2438  1.360  jdolecek 	UFS_WAPBL_JLOCK_ASSERT(mp->um_mountp);
   2439  1.341  jdolecek 
   2440   1.15   mycroft 	allerror = ffs_sbupdate(mp, waitfor);
   2441    1.1   mycroft 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   2442   1.84     lukem 	space = fs->fs_csp;
   2443    1.1   mycroft 	for (i = 0; i < blks; i += fs->fs_frag) {
   2444    1.1   mycroft 		size = fs->fs_bsize;
   2445    1.1   mycroft 		if (i + fs->fs_frag > blks)
   2446    1.1   mycroft 			size = (blks - i) * fs->fs_fsize;
   2447  1.285  dholland 		error = ffs_getblk(mp->um_devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i),
   2448  1.229   hannken 		    FFS_NOBLK, size, false, &bp);
   2449  1.229   hannken 		if (error)
   2450  1.229   hannken 			break;
   2451   1.34    bouyer #ifdef FFS_EI
   2452   1.34    bouyer 		if (mp->um_flags & UFS_NEEDSWAP)
   2453   1.34    bouyer 			ffs_csum_swap((struct csum*)space,
   2454   1.38    kleink 			    (struct csum*)bp->b_data, size);
   2455   1.34    bouyer 		else
   2456   1.34    bouyer #endif
   2457   1.42     perry 			memcpy(bp->b_data, space, (u_int)size);
   2458   1.84     lukem 		space = (char *)space + size;
   2459    1.1   mycroft 		if (waitfor == MNT_WAIT)
   2460    1.1   mycroft 			error = bwrite(bp);
   2461    1.1   mycroft 		else
   2462    1.1   mycroft 			bawrite(bp);
   2463    1.1   mycroft 	}
   2464   1.15   mycroft 	if (!allerror && error)
   2465   1.15   mycroft 		allerror = error;
   2466   1.15   mycroft 	return (allerror);
   2467    1.1   mycroft }
   2468  1.170   thorpej 
   2469  1.170   thorpej int
   2470  1.170   thorpej ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
   2471  1.211     pooka     int attrnamespace, const char *attrname)
   2472  1.170   thorpej {
   2473  1.170   thorpej #ifdef UFS_EXTATTR
   2474  1.170   thorpej 	/*
   2475  1.170   thorpej 	 * File-backed extended attributes are only supported on UFS1.
   2476  1.170   thorpej 	 * UFS2 has native extended attributes.
   2477  1.170   thorpej 	 */
   2478  1.170   thorpej 	if (VFSTOUFS(mp)->um_fstype == UFS1)
   2479  1.211     pooka 		return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
   2480  1.170   thorpej #endif
   2481  1.211     pooka 	return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
   2482  1.170   thorpej }
   2483  1.193   hannken 
   2484  1.242        ad /*
   2485  1.266   hannken  * Synch vnode for a mounted file system.
   2486  1.242        ad  */
   2487  1.242        ad static int
   2488  1.242        ad ffs_vfs_fsync(vnode_t *vp, int flags)
   2489  1.242        ad {
   2490  1.266   hannken 	int error, i, pflags;
   2491  1.243        ad #ifdef WAPBL
   2492  1.242        ad 	struct mount *mp;
   2493  1.243        ad #endif
   2494  1.242        ad 
   2495  1.242        ad 	KASSERT(vp->v_type == VBLK);
   2496  1.289   hannken 	KASSERT(spec_node_getmountedfs(vp) != NULL);
   2497  1.242        ad 
   2498  1.242        ad 	/*
   2499  1.242        ad 	 * Flush all dirty data associated with the vnode.
   2500  1.242        ad 	 */
   2501  1.242        ad 	pflags = PGO_ALLPAGES | PGO_CLEANIT;
   2502  1.242        ad 	if ((flags & FSYNC_WAIT) != 0)
   2503  1.242        ad 		pflags |= PGO_SYNCIO;
   2504  1.364        ad 	rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
   2505  1.242        ad 	error = VOP_PUTPAGES(vp, 0, 0, pflags);
   2506  1.242        ad 	if (error)
   2507  1.242        ad 		return error;
   2508  1.242        ad 
   2509  1.242        ad #ifdef WAPBL
   2510  1.289   hannken 	mp = spec_node_getmountedfs(vp);
   2511  1.242        ad 	if (mp && mp->mnt_wapbl) {
   2512  1.242        ad 		/*
   2513  1.242        ad 		 * Don't bother writing out metadata if the syncer is
   2514  1.242        ad 		 * making the request.  We will let the sync vnode
   2515  1.242        ad 		 * write it out in a single burst through a call to
   2516  1.242        ad 		 * VFS_SYNC().
   2517  1.242        ad 		 */
   2518  1.242        ad 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
   2519  1.242        ad 			return 0;
   2520  1.242        ad 
   2521  1.242        ad 		/*
   2522  1.242        ad 		 * Don't flush the log if the vnode being flushed
   2523  1.242        ad 		 * contains no dirty buffers that could be in the log.
   2524  1.242        ad 		 */
   2525  1.242        ad 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
   2526  1.376   hannken 			VOP_UNLOCK(vp);
   2527  1.242        ad 			error = wapbl_flush(mp->mnt_wapbl, 0);
   2528  1.376   hannken 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2529  1.242        ad 			if (error)
   2530  1.242        ad 				return error;
   2531  1.242        ad 		}
   2532  1.242        ad 
   2533  1.242        ad 		if ((flags & FSYNC_WAIT) != 0) {
   2534  1.267     rmind 			mutex_enter(vp->v_interlock);
   2535  1.242        ad 			while (vp->v_numoutput)
   2536  1.267     rmind 				cv_wait(&vp->v_cv, vp->v_interlock);
   2537  1.267     rmind 			mutex_exit(vp->v_interlock);
   2538  1.242        ad 		}
   2539  1.242        ad 
   2540  1.242        ad 		return 0;
   2541  1.242        ad 	}
   2542  1.242        ad #endif /* WAPBL */
   2543  1.242        ad 
   2544  1.277       chs 	error = vflushbuf(vp, flags);
   2545  1.242        ad 	if (error == 0 && (flags & FSYNC_CACHE) != 0) {
   2546  1.266   hannken 		i = 1;
   2547  1.376   hannken 		VOP_UNLOCK(vp);
   2548  1.242        ad 		(void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE,
   2549  1.242        ad 		    kauth_cred_get());
   2550  1.376   hannken 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2551  1.242        ad 	}
   2552  1.242        ad 
   2553  1.242        ad 	return error;
   2554  1.242        ad }
   2555