/*-
* Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Wasabi Systems, Inc, and by Andrew Doran.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1989, 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
*/
static int
ffs_checkrange(struct mount *mp, ino_t ino)
{
struct fs *fs = VFSTOUFS(mp)->um_fs;
if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg) {
DPRINTF("out of range %" PRIu64 "\n", ino);
return ESTALE;
}
/*
* Need to check if inode is initialized because ffsv2 does
* lazy initialization and we can get here from nfs_fhtovp
*/
if (fs->fs_magic != FS_UFS2_MAGIC)
return 0;
static int
ffs_modcmd(modcmd_t cmd, void *arg)
{
int error;
#if 0
extern int doasyncfree;
#endif
switch (cmd) {
case MODULE_CMD_INIT:
error = vfs_attach(&ffs_vfsops);
if (error != 0)
break;
ffs_snapshot_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
ffs_snapshot_cb, NULL);
if (ffs_snapshot_listener == NULL)
printf("ffs_modcmd: can't listen on system scope.\n");
break;
case MODULE_CMD_FINI:
error = vfs_detach(&ffs_vfsops);
if (error != 0)
break;
if (ffs_snapshot_listener != NULL)
kauth_unlisten_scope(ffs_snapshot_listener);
break;
default:
error = ENOTTY;
break;
}
/* Check arguments */
if (args->fspec == NULL) {
if (!update) {
/* New mounts must have a filename for the device */
DPRINTF("no filename for mount");
return EINVAL;
}
} else {
/*
* Look up the name and verify that it's sane.
*/
error = namei_simple_user(args->fspec,
NSM_FOLLOW_NOEMULROOT, &devvp);
if (error != 0) {
DPRINTF("namei_simple_user returned %d", error);
return error;
}
/*
* Be sure this is a valid block device
*/
if (devvp->v_type != VBLK) {
DPRINTF("non block device %d", devvp->v_type);
error = ENOTBLK;
goto fail;
}
if (update) {
/*
* Be sure we're still naming the same device
* used for our initial mount
*/
if (devvp != ump->um_devvp &&
devvp->v_rdev != ump->um_devvp->v_rdev) {
DPRINTF("wrong device 0x%jx != 0x%jx",
(uintmax_t)devvp->v_rdev,
(uintmax_t)ump->um_devvp->v_rdev);
error = EINVAL;
goto fail;
}
vrele(devvp);
devvp = NULL;
}
}
if (devvp == NULL) {
devvp = ump->um_devvp;
vref(devvp);
}
/*
* If mount by non-root, then verify that user has necessary
* permissions on the device.
*
* Permission to update a mount is checked higher, so here we presume
* updating the mount is okay (for example, as far as securelevel goes)
* which leaves us with the normal check.
*/
accessmode = VREAD;
if (update ? (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
(mp->mnt_flag & MNT_RDONLY) == 0)
accessmode |= VWRITE;
vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(accessmode));
VOP_UNLOCK(devvp);
if (error) {
DPRINTF("kauth returned %d", error);
goto fail;
}
#ifdef WAPBL
/* WAPBL can only be enabled on a r/w mount. */
if (((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) ||
(mp->mnt_iflag & IMNT_WANTRDONLY)) {
mp->mnt_flag &= ~MNT_LOG;
}
#else /* !WAPBL */
mp->mnt_flag &= ~MNT_LOG;
#endif /* !WAPBL */
/*
* Reload all incore data for a filesystem (used after running fsck on
* the root filesystem and finding things to fix). The filesystem must
* be mounted read-only.
*
* Things to do to update the mount:
* 1) invalidate all cached meta-data.
* 2) re-read superblock from disk.
* 3) re-read summary information from disk.
* 4) invalidate all inactive vnodes.
* 5) invalidate all cached file data.
* 6) re-read inode data for all active vnodes.
*/
int
ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
{
struct vnode *vp, *devvp;
struct inode *ip;
void *space;
struct buf *bp;
struct fs *fs, *newfs;
int i, bsize, blks, error;
int32_t *lp, fs_sbsize;
struct ufsmount *ump;
daddr_t sblockloc;
struct vnode_iterator *marker;
if ((mp->mnt_flag & MNT_RDONLY) == 0)
return (EINVAL);
/*
* The current implementation doesn't handle the possibility that
* these values may have changed.
*/
if ((newfs->fs_sbsize != fs_sbsize) ||
(newfs->fs_cssize != fs->fs_cssize) ||
(newfs->fs_contigsumsize != fs->fs_contigsumsize) ||
(newfs->fs_ncg != fs->fs_ncg)) {
kmem_free(newfs, fs_sbsize);
return (EINVAL);
}
/* Store off old fs_sblockloc for fs_oldfscompat_read. */
sblockloc = fs->fs_sblockloc;
/*
* Copy pointer fields back into superblock before copying in XXX
* new superblock. These should really be in the ufsmount. XXX
* Note that important parameters (eg fs_ncg) are unchanged.
*/
newfs->fs_csp = fs->fs_csp;
newfs->fs_maxcluster = fs->fs_maxcluster;
newfs->fs_contigdirs = fs->fs_contigdirs;
newfs->fs_ronly = fs->fs_ronly;
newfs->fs_active = fs->fs_active;
memcpy(fs, newfs, (u_int)fs_sbsize);
kmem_free(newfs, fs_sbsize);
/*
* Recheck for Apple UFS filesystem.
*/
ump->um_flags &= ~UFS_ISAPPLEUFS;
if (ffs_is_appleufs(devvp, fs)) {
#ifdef APPLE_UFS
ump->um_flags |= UFS_ISAPPLEUFS;
#else
DPRINTF("AppleUFS not supported");
return (EIO); /* XXX: really? */
#endif
}
if (UFS_MPISAPPLEUFS(ump)) {
/* see comment about NeXT below */
ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
mp->mnt_iflag |= IMNT_DTYPE;
} else {
ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
ump->um_dirblksiz = UFS_DIRBLKSIZ;
if (ump->um_maxsymlinklen > 0)
mp->mnt_iflag |= IMNT_DTYPE;
else
mp->mnt_iflag &= ~IMNT_DTYPE;
}
ffs_oldfscompat_read(fs, ump, sblockloc);
ffs_statvfs(mp, &mp->mnt_stat);
/*
* Step 3: re-read summary information from disk.
*/
blks = howmany(fs->fs_cssize, fs->fs_fsize);
space = fs->fs_csp;
for (i = 0; i < blks; i += fs->fs_frag) {
bsize = fs->fs_bsize;
if (i + fs->fs_frag > blks)
bsize = (blks - i) * fs->fs_fsize;
error = bread(devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), bsize,
0, &bp);
if (error) {
return (error);
}
#ifdef FFS_EI
if (UFS_FSNEEDSWAP(fs))
ffs_csum_swap((struct csum *)bp->b_data,
(struct csum *)space, bsize);
else
#endif
memcpy(space, bp->b_data, (size_t)bsize);
space = (char *)space + bsize;
brelse(bp, 0);
}
/*
* We no longer know anything about clusters per cylinder group.
*/
if (fs->fs_contigsumsize > 0) {
lp = fs->fs_maxcluster;
for (i = 0; i < fs->fs_ncg; i++)
*lp++ = fs->fs_contigsumsize;
}
vfs_vnode_iterator_init(mp, &marker);
while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
/*
* Step 4: invalidate all inactive vnodes.
*/
if (vrecycle(vp))
continue;
/*
* Step 5: invalidate all cached file data.
*/
if (vn_lock(vp, LK_EXCLUSIVE)) {
vrele(vp);
continue;
}
if (vinvalbuf(vp, 0, cred, l, 0, 0))
panic("%s: dirty2", __func__);
/*
* Step 6: re-read inode data for all active vnodes.
*/
ip = VTOI(vp);
error = bread(devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
(int)fs->fs_bsize, 0, &bp);
if (error) {
vput(vp);
break;
}
ffs_load_inode(bp, ip, fs, ip->i_number);
brelse(bp, 0);
vput(vp);
}
vfs_vnode_iterator_destroy(marker);
return (error);
}
/*
* Possible superblock locations ordered from most to least likely.
*/
static const int sblock_try[] = SBLOCKSEARCH;
static int
ffs_superblock_validate(struct fs *fs)
{
int32_t i, fs_bshift = 0, fs_fshift = 0, fs_fragshift = 0, fs_frag;
int32_t fs_inopb;
/* Check the superblock size */
if (fs->fs_sbsize > SBLOCKSIZE || fs->fs_sbsize < sizeof(struct fs))
return 0;
/* Check the file system blocksize */
if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < MINBSIZE)
return 0;
if (!powerof2(fs->fs_bsize))
return 0;
/* Check the size of frag blocks */
if (!powerof2(fs->fs_fsize))
return 0;
if (fs->fs_fsize == 0)
return 0;
/*
* XXX: these values are just zero-checked to prevent obvious
* bugs. We need more strict checks.
*/
if (fs->fs_size == 0 && fs->fs_old_size == 0)
return 0;
if (fs->fs_cssize == 0)
return 0;
if (fs->fs_ipg == 0)
return 0;
if (fs->fs_fpg == 0)
return 0;
if (fs->fs_ncg == 0)
return 0;
if (fs->fs_maxbpg == 0)
return 0;
/* Check the number of inodes per block */
if (fs->fs_magic == FS_UFS1_MAGIC)
fs_inopb = fs->fs_bsize / sizeof(struct ufs1_dinode);
else /* fs->fs_magic == FS_UFS2_MAGIC */
fs_inopb = fs->fs_bsize / sizeof(struct ufs2_dinode);
if (fs->fs_inopb != fs_inopb)
return 0;
/* Block size cannot be smaller than fragment size */
if (fs->fs_bsize < fs->fs_fsize)
return 0;
/* Compute fs_bshift and ensure it is consistent */
for (i = fs->fs_bsize; i > 1; i >>= 1)
fs_bshift++;
if (fs->fs_bshift != fs_bshift)
return 0;
/* Compute fs_fshift and ensure it is consistent */
for (i = fs->fs_fsize; i > 1; i >>= 1)
fs_fshift++;
if (fs->fs_fshift != fs_fshift)
return 0;
/* Compute fs_fragshift and ensure it is consistent */
for (i = fs->fs_frag; i > 1; i >>= 1)
fs_fragshift++;
if (fs->fs_fragshift != fs_fragshift)
return 0;
/* Check the masks */
if (fs->fs_bmask != ~(fs->fs_bsize - 1))
return 0;
if (fs->fs_fmask != ~(fs->fs_fsize - 1))
return 0;
/*
* Now that the shifts and masks are sanitized, we can use the ffs_ API.
*/
/* Check the number of frag blocks */
if ((fs_frag = ffs_numfrags(fs, fs->fs_bsize)) > MAXFRAG)
return 0;
if (fs->fs_frag != fs_frag)
return 0;
/* Check the size of cylinder groups */
if ((fs->fs_cgsize < sizeof(struct cg)) ||
(fs->fs_cgsize > fs->fs_bsize))
return 0;
return 1;
}
static int
ffs_is_appleufs(struct vnode *devvp, struct fs *fs)
{
struct dkwedge_info dkw;
int ret = 0;
/*
* First check to see if this is tagged as an Apple UFS filesystem
* in the disklabel.
*/
if (getdiskinfo(devvp, &dkw) == 0 &&
strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
ret = 1;
#ifdef APPLE_UFS
else {
struct appleufslabel *applefs;
struct buf *bp;
daddr_t blkno = APPLEUFS_LABEL_OFFSET / DEV_BSIZE;
int error;
/*
* Manually look for an Apple UFS label, and if a valid one
* is found, then treat it like an Apple UFS filesystem anyway.
*/
error = bread(devvp, blkno, APPLEUFS_LABEL_SIZE, 0, &bp);
if (error) {
DPRINTF("bread@0x%jx returned %d", (intmax_t)blkno, error);
return 0;
}
applefs = (struct appleufslabel *)bp->b_data;
error = ffs_appleufs_validate(fs->fs_fsmnt, applefs, NULL);
if (error == 0)
ret = 1;
brelse(bp, 0);
}
#endif
return ret;
}
/*
* Common code for mount and mountroot
*/
int
ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
{
struct ufsmount *ump = NULL;
struct buf *bp = NULL;
struct fs *fs = NULL;
dev_t dev;
void *space;
daddr_t sblockloc = 0;
int blks, fstype = 0;
int error, i, bsize, ronly, bset = 0;
#ifdef FFS_EI
int needswap = 0; /* keep gcc happy */
#endif
int32_t *lp;
kauth_cred_t cred;
u_int32_t allocsbsize, fs_sbsize = 0;
dev = devvp->v_rdev;
cred = l ? l->l_cred : NOCRED;
/* Flush out any old buffers remaining from a previous use. */
vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
VOP_UNLOCK(devvp);
if (error) {
DPRINTF("vinvalbuf returned %d", error);
return error;
}
/*
* Swap: here, we swap fs->fs_sbsize in order to get the correct
* size to read the superblock. Once read, we swap the whole
* superblock structure.
*/
if (fs->fs_magic == FS_UFS2EA_MAGIC) {
ump->um_flags |= UFS_EA;
fs->fs_magic = FS_UFS2_MAGIC;
} else if (fs->fs_magic == FS_UFS2EA_MAGIC_SWAPPED) {
ump->um_flags |= UFS_EA;
fs->fs_magic = FS_UFS2_MAGIC_SWAPPED;
}
if (fs->fs_magic == FS_UFS1_MAGIC) {
fs_sbsize = fs->fs_sbsize;
fstype = UFS1;
#ifdef FFS_EI
needswap = 0;
} else if (fs->fs_magic == FS_UFS1_MAGIC_SWAPPED) {
fs_sbsize = bswap32(fs->fs_sbsize);
fstype = UFS1;
needswap = 1;
#endif
} else if (fs->fs_magic == FS_UFS2_MAGIC) {
fs_sbsize = fs->fs_sbsize;
fstype = UFS2;
#ifdef FFS_EI
needswap = 0;
} else if (fs->fs_magic == FS_UFS2_MAGIC_SWAPPED) {
fs_sbsize = bswap32(fs->fs_sbsize);
fstype = UFS2;
needswap = 1;
#endif
} else
continue;
/* fs->fs_sblockloc isn't defined for old filesystems */
if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
if (sblockloc == SBLOCK_UFS2)
/*
* This is likely to be the first alternate
* in a filesystem with 64k blocks.
* Don't use it.
*/
continue;
fs_sblockloc = sblockloc;
} else {
fs_sblockloc = fs->fs_sblockloc;
#ifdef FFS_EI
if (needswap)
fs_sblockloc = bswap64(fs_sblockloc);
#endif
}
/* Check we haven't found an alternate superblock */
if (fs_sblockloc != sblockloc)
continue;
/* Swap the whole superblock structure, if necessary. */
#ifdef FFS_EI
if (needswap) {
ffs_sb_swap((struct fs*)bp->b_data, fs);
fs->fs_flags |= FS_SWAPPED;
} else
#endif
fs->fs_flags &= ~FS_SWAPPED;
/*
* Now that everything is swapped, the superblock is ready to
* be sanitized.
*/
if (!ffs_superblock_validate(fs)) {
kmem_free(fs, fs_sbsize);
continue;
}
/* Ok seems to be a good superblock */
break;
}
ump->um_fs = fs;
#ifdef WAPBL
if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
error = ffs_wapbl_replay_start(mp, fs, devvp);
if (error && (mp->mnt_flag & MNT_FORCE) == 0) {
DPRINTF("ffs_wapbl_replay_start returned %d", error);
goto out;
}
if (!error) {
if (!ronly) {
/* XXX fsmnt may be stale. */
printf("%s: replaying log to disk\n",
fs->fs_fsmnt);
error = wapbl_replay_write(mp->mnt_wapbl_replay,
devvp);
if (error) {
DPRINTF("wapbl_replay_write returned %d",
error);
goto out;
}
wapbl_replay_stop(mp->mnt_wapbl_replay);
fs->fs_clean = FS_WASCLEAN;
} else {
/* XXX fsmnt may be stale */
printf("%s: replaying log to memory\n",
fs->fs_fsmnt);
}
/* Force a re-read of the superblock */
brelse(bp, BC_INVAL);
bp = NULL;
kmem_free(fs, fs_sbsize);
fs = NULL;
goto sbagain;
}
}
#else /* !WAPBL */
if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
error = EPERM;
DPRINTF("no force %d", error);
goto out;
}
#endif /* !WAPBL */
ump->um_fstype = fstype;
if (fs->fs_sbsize < SBLOCKSIZE)
brelse(bp, BC_INVAL);
else
brelse(bp, 0);
bp = NULL;
if (ffs_is_appleufs(devvp, fs)) {
#ifdef APPLE_UFS
ump->um_flags |= UFS_ISAPPLEUFS;
#else
DPRINTF("AppleUFS not supported");
error = EINVAL;
goto out;
#endif
}
#if 0
/*
* XXX This code changes the behaviour of mounting dirty filesystems, to
* XXX require "mount -f ..." to mount them. This doesn't match what
* XXX mount(8) describes and is disabled for now.
*/
/*
* If the file system is not clean, don't allow it to be mounted
* unless MNT_FORCE is specified. (Note: MNT_FORCE is always set
* for the root file system.)
*/
if (fs->fs_flags & FS_DOWAPBL) {
/*
* wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
* bit is set, although there's a window in unmount where it
* could be FS_ISCLEAN
*/
if ((mp->mnt_flag & MNT_FORCE) == 0 &&
(fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
error = EPERM;
goto out;
}
} else
if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
(mp->mnt_flag & MNT_FORCE) == 0) {
error = EPERM;
goto out;
}
#endif
/*
* Verify that we can access the last block in the fs
* if we're mounting read/write.
*/
if (!ronly) {
error = bread(devvp, FFS_FSBTODB(fs, fs->fs_size - 1),
fs->fs_fsize, 0, &bp);
if (error) {
DPRINTF("bread@0x%jx returned %d",
(intmax_t)FFS_FSBTODB(fs, fs->fs_size - 1),
error);
bset = BC_INVAL;
goto out;
}
if (bp->b_bcount != fs->fs_fsize) {
DPRINTF("bcount %x != fsize %x", bp->b_bcount,
fs->fs_fsize);
error = EINVAL;
bset = BC_INVAL;
goto out;
}
brelse(bp, BC_INVAL);
bp = NULL;
}
/* These fields will be overwritten by their
* original values in fs_oldfscompat_write, so it is harmless
* to modify them here.
*/
fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
/* Compatibility for old filesystems */
if (fs->fs_avgfilesize <= 0)
fs->fs_avgfilesize = AVFILESIZ;
if (fs->fs_avgfpdir <= 0)
fs->fs_avgfpdir = AFPDIR;
/*
* Unwinding superblock updates for old filesystems.
* See ffs_oldfscompat_read above for details.
*
* XXX - Parts get retired eventually.
* Unfortunately new bits get added.
*/
static void
ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
{
int32_t *extrasave;
if ((fs->fs_magic != FS_UFS1_MAGIC) ||
(fs->fs_old_flags & FS_FLAGS_UPDATED))
return;
/*
* Flush out all the files in a filesystem.
*/
int
ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
{
extern int doforce;
struct ufsmount *ump;
int error;
ip = VTOI(vp);
/*
* Skip the vnode/inode if inaccessible.
*/
if (ip == NULL || vp->v_type == VNON)
return false;
/*
* We deliberately update inode times here. This will
* prevent a massive queue of updates accumulating, only
* to be handled by a call to unmount.
*
* XXX It would be better to have the syncer trickle these
* out. Adjustment needed to allow registering vnodes for
* sync when the vnode is clean, but the inode dirty. Or
* have ufs itself trickle out inode updates.
*
* If doing a lazy sync, we don't care about metadata or
* data updates, because they are handled by each vnode's
* synclist entry. In this case we are only interested in
* writing back modified inodes.
*/
if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
(c->waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
(vp->v_iflag & VI_ONWORKLST) == 0)))
return false;
return true;
}
/*
* Go through the disk queues to initiate sandbagged IO;
* go through the inodes to write those that have been modified;
* initiate the writing of the super block if it has been modified.
*/
int
ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
{
struct vnode *vp;
struct ufsmount *ump = VFSTOUFS(mp);
struct fs *fs;
struct vnode_iterator *marker;
int error, allerror = 0;
struct ffs_sync_ctx ctx;
#ifdef WAPBL
if (mp->mnt_wapbl) {
error = wapbl_flush(mp->mnt_wapbl, (waitfor == MNT_WAIT));
if (error)
allerror = error;
}
#endif
return (allerror);
}
/*
* Load inode from disk and initialize vnode.
*/
static int
ffs_init_vnode(struct ufsmount *ump, struct vnode *vp, ino_t ino)
{
struct fs *fs;
struct inode *ip;
struct buf *bp;
int error;
fs = ump->um_fs;
/* Read in the disk contents for the inode. */
error = bread(ump->um_devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ino)),
(int)fs->fs_bsize, 0, &bp);
if (error)
return error;
/*
* Read an inode from disk and initialize this vnode / inode pair.
* Caller assures no other thread will try to load this inode.
*/
int
ffs_loadvnode(struct mount *mp, struct vnode *vp,
const void *key, size_t key_len, const void **new_key)
{
ino_t ino;
struct fs *fs;
struct inode *ip;
struct ufsmount *ump;
int error;
/* Set type and finalize. */
ip->i_flags = 0;
DIP_ASSIGN(ip, flags, 0);
ip->i_mode = mode;
DIP_ASSIGN(ip, mode, mode);
if (vap->va_rdev != VNOVAL) {
/*
* Want to be able to use this to make badblock
* inodes, so don't truncate the dev number.
*/
if (ump->um_fstype == UFS1)
ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev,
UFS_MPNEEDSWAP(ump));
else
ip->i_ffs2_rdev = ufs_rw64(vap->va_rdev,
UFS_MPNEEDSWAP(ump));
}
ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
ip->i_devvp = ump->um_devvp;
vref(ip->i_devvp);
/* Set up a new generation number for this inode. */
ip->i_gen++;
DIP_ASSIGN(ip, gen, ip->i_gen);
if (fs->fs_magic == FS_UFS2_MAGIC) {
vfs_timestamp(&ts);
ip->i_ffs2_birthtime = ts.tv_sec;
ip->i_ffs2_birthnsec = ts.tv_nsec;
}
/*
* File handle to vnode
*
* Have to be really careful about stale file handles:
* - check that the inode number is valid
* - call ffs_vget() to get the locked inode
* - check for an unallocated inode (i_mode == 0)
* - check that the given client host has export rights and return
* those rights via. exflagsp and credanonp
*/
int
ffs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
{
struct ufid ufh;
int error;
if (fhp->fid_len != sizeof(struct ufid))
return EINVAL;
/*
* Write a superblock and associated information back to disk.
*/
int
ffs_sbupdate(struct ufsmount *mp, int waitfor)
{
struct fs *fs = mp->um_fs;
struct fs *bfs;
struct buf *bp;
int error;
int
ffs_cgupdate(struct ufsmount *mp, int waitfor)
{
struct fs *fs = mp->um_fs;
struct buf *bp;
int blks;
void *space;
int i, size, error = 0, allerror = 0;
UFS_WAPBL_JLOCK_ASSERT(mp->um_mountp);
allerror = ffs_sbupdate(mp, waitfor);
blks = howmany(fs->fs_cssize, fs->fs_fsize);
space = fs->fs_csp;
for (i = 0; i < blks; i += fs->fs_frag) {
size = fs->fs_bsize;
if (i + fs->fs_frag > blks)
size = (blks - i) * fs->fs_fsize;
error = ffs_getblk(mp->um_devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i),
FFS_NOBLK, size, false, &bp);
if (error)
break;
#ifdef FFS_EI
if (mp->um_flags & UFS_NEEDSWAP)
ffs_csum_swap((struct csum*)space,
(struct csum*)bp->b_data, size);
else
#endif
memcpy(bp->b_data, space, (u_int)size);
space = (char *)space + size;
if (waitfor == MNT_WAIT)
error = bwrite(bp);
else
bawrite(bp);
}
if (!allerror && error)
allerror = error;
return (allerror);
}
int
ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
int attrnamespace, const char *attrname)
{
#ifdef UFS_EXTATTR
/*
* File-backed extended attributes are only supported on UFS1.
* UFS2 has native extended attributes.
*/
if (VFSTOUFS(mp)->um_fstype == UFS1)
return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
#endif
return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
}
/*
* Synch vnode for a mounted file system.
*/
static int
ffs_vfs_fsync(vnode_t *vp, int flags)
{
int error, i, pflags;
#ifdef WAPBL
struct mount *mp;
#endif
/*
* Flush all dirty data associated with the vnode.
*/
pflags = PGO_ALLPAGES | PGO_CLEANIT;
if ((flags & FSYNC_WAIT) != 0)
pflags |= PGO_SYNCIO;
rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
error = VOP_PUTPAGES(vp, 0, 0, pflags);
if (error)
return error;
#ifdef WAPBL
mp = spec_node_getmountedfs(vp);
if (mp && mp->mnt_wapbl) {
/*
* Don't bother writing out metadata if the syncer is
* making the request. We will let the sync vnode
* write it out in a single burst through a call to
* VFS_SYNC().
*/
if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
return 0;
/*
* Don't flush the log if the vnode being flushed
* contains no dirty buffers that could be in the log.
*/
if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
error = wapbl_flush(mp->mnt_wapbl, 0);
if (error)
return error;
}
if ((flags & FSYNC_WAIT) != 0) {
mutex_enter(vp->v_interlock);
while (vp->v_numoutput)
cv_wait(&vp->v_cv, vp->v_interlock);
mutex_exit(vp->v_interlock);
}