2013-12-06 12:04:52 +01:00
|
|
|
/* $NetBSD: chfs_vfsops.c,v 1.9 2013/10/20 17:18:38 christos Exp $ */
|
2012-03-13 02:37:22 +01:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2010 Department of Software Engineering,
|
|
|
|
* University of Szeged, Hungary
|
|
|
|
* Copyright (C) 2010 Tamas Toth <ttoth@inf.u-szeged.hu>
|
|
|
|
* Copyright (C) 2010 Adam Hoka <ahoka@NetBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by the Department of Software Engineering, University of Szeged, Hungary
|
|
|
|
*
|
|
|
|
* 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 AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
//XXX needed just for debugging
|
|
|
|
#include <sys/fstrans.h>
|
|
|
|
#include <sys/sleepq.h>
|
|
|
|
#include <sys/lockdebug.h>
|
|
|
|
#include <sys/ktrace.h>
|
|
|
|
|
|
|
|
#include <uvm/uvm.h>
|
|
|
|
#include <uvm/uvm_pager.h>
|
|
|
|
#include <ufs/ufs/dir.h>
|
|
|
|
#include <ufs/ufs/ufs_extern.h>
|
|
|
|
#include <miscfs/genfs/genfs.h>
|
|
|
|
#include <miscfs/genfs/genfs_node.h>
|
|
|
|
#include <miscfs/specfs/specdev.h>
|
|
|
|
#include "chfs.h"
|
|
|
|
#include "chfs_args.h"
|
|
|
|
|
|
|
|
MODULE(MODULE_CLASS_VFS, chfs, "flash");
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
/* functions */
|
|
|
|
|
|
|
|
static int chfs_mount(struct mount *, const char *, void *, size_t *);
|
|
|
|
static int chfs_unmount(struct mount *, int);
|
|
|
|
static int chfs_root(struct mount *, struct vnode **);
|
|
|
|
static int chfs_vget(struct mount *, ino_t, struct vnode **);
|
|
|
|
static int chfs_fhtovp(struct mount *, struct fid *, struct vnode **);
|
|
|
|
static int chfs_vptofh(struct vnode *, struct fid *, size_t *);
|
|
|
|
static int chfs_start(struct mount *, int);
|
|
|
|
static int chfs_statvfs(struct mount *, struct statvfs *);
|
|
|
|
static int chfs_sync(struct mount *, int, kauth_cred_t);
|
|
|
|
static void chfs_init(void);
|
|
|
|
static void chfs_reinit(void);
|
|
|
|
static void chfs_done(void);
|
|
|
|
static int chfs_snapshot(struct mount *, struct vnode *,
|
|
|
|
struct timespec *);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
/* structures */
|
|
|
|
|
|
|
|
int
|
|
|
|
chfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
|
|
|
|
kauth_cred_t cred)
|
|
|
|
{
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct genfs_ops chfs_genfsops = {
|
|
|
|
.gop_size = genfs_size,
|
|
|
|
.gop_alloc = chfs_gop_alloc,
|
|
|
|
.gop_write = genfs_gop_write,
|
|
|
|
.gop_markupdate = ufs_gop_markupdate,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pool chfs_inode_pool;
|
|
|
|
|
|
|
|
/* for looking up the major for flash */
|
|
|
|
extern const struct cdevsw flash_cdevsw;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
chfs_mount(struct mount *mp,
|
|
|
|
const char *path, void *data, size_t *data_len)
|
|
|
|
{
|
|
|
|
struct lwp *l = curlwp;
|
|
|
|
struct nameidata nd;
|
|
|
|
struct pathbuf *pb;
|
|
|
|
struct vnode *devvp = NULL;
|
|
|
|
struct ufs_args *args = data;
|
|
|
|
struct ufsmount *ump = NULL;
|
|
|
|
struct chfs_mount *chmp;
|
|
|
|
int err = 0;
|
|
|
|
int xflags;
|
|
|
|
|
|
|
|
dbg("mount()\n");
|
|
|
|
|
|
|
|
if (*data_len < sizeof *args)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (mp->mnt_flag & MNT_GETARGS) {
|
|
|
|
ump = VFSTOUFS(mp);
|
|
|
|
if (ump == NULL)
|
|
|
|
return EIO;
|
|
|
|
memset(args, 0, sizeof *args);
|
|
|
|
args->fspec = NULL;
|
|
|
|
*data_len = sizeof *args;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mp->mnt_flag & MNT_UPDATE) {
|
|
|
|
/* XXX: There is no support yet to update file system
|
|
|
|
* settings. Should be added. */
|
|
|
|
|
|
|
|
return ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args->fspec != NULL) {
|
|
|
|
err = pathbuf_copyin(args->fspec, &pb);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Look up the name and verify that it's sane. */
|
2012-03-13 02:37:22 +01:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, pb);
|
|
|
|
if ((err = namei(&nd)) != 0 )
|
|
|
|
return (err);
|
|
|
|
devvp = nd.ni_vp;
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Be sure this is a valid block device */
|
2012-03-13 02:37:22 +01:00
|
|
|
if (devvp->v_type != VBLK)
|
|
|
|
err = ENOTBLK;
|
|
|
|
else if (bdevsw_lookup(devvp->v_rdev) == NULL)
|
|
|
|
err = ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
vrele(devvp);
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mp->mnt_flag & MNT_RDONLY)
|
|
|
|
xflags = FREAD;
|
|
|
|
else
|
|
|
|
xflags = FREAD|FWRITE;
|
|
|
|
|
|
|
|
err = VOP_OPEN(devvp, xflags, FSCRED);
|
|
|
|
if (err)
|
|
|
|
goto fail;
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* call CHFS mount function */
|
2012-03-13 02:37:22 +01:00
|
|
|
err = chfs_mountfs(devvp, mp);
|
|
|
|
if (err) {
|
|
|
|
vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
|
|
|
|
(void)VOP_CLOSE(devvp, xflags, NOCRED);
|
|
|
|
VOP_UNLOCK(devvp);
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
|
2012-03-13 02:37:22 +01:00
|
|
|
ump = VFSTOUFS(mp);
|
|
|
|
chmp = ump->um_chfs;
|
|
|
|
|
|
|
|
vfs_getnewfsid(mp);
|
|
|
|
chmp->chm_fsmp = mp;
|
|
|
|
|
|
|
|
return set_statvfs_info(path,
|
|
|
|
UIO_USERSPACE, args->fspec,
|
|
|
|
UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
vrele(devvp);
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* chfs_mountfs - init CHFS */
|
2012-03-13 02:37:22 +01:00
|
|
|
int
|
|
|
|
chfs_mountfs(struct vnode *devvp, struct mount *mp)
|
|
|
|
{
|
|
|
|
struct lwp *l = curlwp;
|
|
|
|
kauth_cred_t cred;
|
|
|
|
devmajor_t flash_major;
|
|
|
|
dev_t dev;
|
|
|
|
struct ufsmount* ump = NULL;
|
|
|
|
struct chfs_mount* chmp;
|
|
|
|
struct vnode *vp;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
dbg("mountfs()\n");
|
|
|
|
|
|
|
|
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);
|
|
|
|
err = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
|
|
|
|
VOP_UNLOCK(devvp);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Setup device. */
|
2012-03-13 02:37:22 +01:00
|
|
|
flash_major = cdevsw_lookup_major(&flash_cdevsw);
|
|
|
|
|
|
|
|
if (devvp->v_type != VBLK)
|
|
|
|
err = ENOTBLK;
|
|
|
|
else if (bdevsw_lookup(dev) == NULL)
|
|
|
|
err = ENXIO;
|
|
|
|
else if (major(dev) != flash_major) {
|
|
|
|
dbg("major(dev): %d, flash_major: %d\n",
|
|
|
|
major(dev), flash_major);
|
|
|
|
err = ENODEV;
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
vrele(devvp);
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Connect CHFS to UFS. */
|
|
|
|
ump = kmem_zalloc(sizeof(struct ufsmount), KM_SLEEP);
|
2012-03-13 02:37:22 +01:00
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
ump->um_fstype = UFS1;
|
|
|
|
ump->um_chfs = kmem_zalloc(sizeof(struct chfs_mount), KM_SLEEP);
|
2012-03-13 02:37:22 +01:00
|
|
|
mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
|
|
|
|
chmp = ump->um_chfs;
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize erase block handler. */
|
2012-03-13 02:37:22 +01:00
|
|
|
chmp->chm_ebh = kmem_alloc(sizeof(struct chfs_ebh), KM_SLEEP);
|
|
|
|
|
|
|
|
dbg("[]opening flash: %u\n", (unsigned int)devvp->v_rdev);
|
|
|
|
err = ebh_open(chmp->chm_ebh, devvp->v_rdev);
|
|
|
|
if (err) {
|
|
|
|
dbg("error while opening flash\n");
|
2013-12-06 12:04:52 +01:00
|
|
|
goto fail;
|
2012-03-13 02:37:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO check flash sizes
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize vnode cache's hashtable and eraseblock array. */
|
2012-03-13 02:37:22 +01:00
|
|
|
chmp->chm_gbl_version = 0;
|
|
|
|
chmp->chm_vnocache_hash = chfs_vnocache_hash_init();
|
|
|
|
|
|
|
|
chmp->chm_blocks = kmem_zalloc(chmp->chm_ebh->peb_nr *
|
|
|
|
sizeof(struct chfs_eraseblock), KM_SLEEP);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize mutexes. */
|
2012-03-13 02:37:22 +01:00
|
|
|
mutex_init(&chmp->chm_lock_mountfields, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
mutex_init(&chmp->chm_lock_sizes, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
mutex_init(&chmp->chm_lock_vnocache, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize read/write contants. (from UFS) */
|
2012-03-13 02:37:22 +01:00
|
|
|
chmp->chm_fs_bmask = -4096;
|
|
|
|
chmp->chm_fs_bsize = 4096;
|
|
|
|
chmp->chm_fs_qbmask = 4095;
|
|
|
|
chmp->chm_fs_bshift = 12;
|
|
|
|
chmp->chm_fs_fmask = -2048;
|
|
|
|
chmp->chm_fs_qfmask = 2047;
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize writebuffer. */
|
2012-03-13 02:37:22 +01:00
|
|
|
chmp->chm_wbuf_pagesize = chmp->chm_ebh->flash_if->page_size;
|
|
|
|
dbg("wbuf size: %zu\n", chmp->chm_wbuf_pagesize);
|
|
|
|
chmp->chm_wbuf = kmem_alloc(chmp->chm_wbuf_pagesize, KM_SLEEP);
|
|
|
|
rw_init(&chmp->chm_lock_wbuf);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize queues. */
|
2012-03-13 02:37:22 +01:00
|
|
|
TAILQ_INIT(&chmp->chm_free_queue);
|
|
|
|
TAILQ_INIT(&chmp->chm_clean_queue);
|
|
|
|
TAILQ_INIT(&chmp->chm_dirty_queue);
|
|
|
|
TAILQ_INIT(&chmp->chm_very_dirty_queue);
|
|
|
|
TAILQ_INIT(&chmp->chm_erasable_pending_wbuf_queue);
|
|
|
|
TAILQ_INIT(&chmp->chm_erase_pending_queue);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize flash-specific constants. */
|
2012-03-13 02:37:22 +01:00
|
|
|
chfs_calc_trigger_levels(chmp);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize sizes. */
|
2012-03-13 02:37:22 +01:00
|
|
|
chmp->chm_nr_free_blocks = 0;
|
|
|
|
chmp->chm_nr_erasable_blocks = 0;
|
|
|
|
chmp->chm_max_vno = 2;
|
|
|
|
chmp->chm_checked_vno = 2;
|
|
|
|
chmp->chm_unchecked_size = 0;
|
|
|
|
chmp->chm_used_size = 0;
|
|
|
|
chmp->chm_dirty_size = 0;
|
|
|
|
chmp->chm_wasted_size = 0;
|
|
|
|
chmp->chm_free_size = chmp->chm_ebh->eb_size * chmp->chm_ebh->peb_nr;
|
2013-12-06 12:04:52 +01:00
|
|
|
|
|
|
|
/* Build filesystem. */
|
2012-03-13 02:37:22 +01:00
|
|
|
err = chfs_build_filesystem(chmp);
|
|
|
|
|
|
|
|
if (err) {
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Armageddon and return. */
|
2012-03-13 02:37:22 +01:00
|
|
|
chfs_vnocache_hash_destroy(chmp->chm_vnocache_hash);
|
|
|
|
ebh_close(chmp->chm_ebh);
|
2013-12-06 12:04:52 +01:00
|
|
|
err = EIO;
|
|
|
|
goto fail;
|
2012-03-13 02:37:22 +01:00
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize UFS. */
|
2012-03-13 02:37:22 +01:00
|
|
|
mp->mnt_data = ump;
|
|
|
|
mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
|
|
|
|
mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CHFS);
|
|
|
|
mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
|
|
|
|
mp->mnt_stat.f_namemax = MAXNAMLEN;
|
|
|
|
mp->mnt_flag |= MNT_LOCAL;
|
|
|
|
mp->mnt_fs_bshift = PAGE_SHIFT;
|
|
|
|
mp->mnt_dev_bshift = DEV_BSHIFT;
|
|
|
|
mp->mnt_iflag |= IMNT_MPSAFE;
|
|
|
|
ump->um_flags = 0;
|
|
|
|
ump->um_mountp = mp;
|
|
|
|
ump->um_dev = dev;
|
|
|
|
ump->um_devvp = devvp;
|
|
|
|
ump->um_maxfilesize = 1048512 * 1024;
|
2013-12-06 12:04:52 +01:00
|
|
|
|
|
|
|
/* Allocate the root vnode. */
|
2012-03-13 02:37:22 +01:00
|
|
|
err = VFS_VGET(mp, CHFS_ROOTINO, &vp);
|
|
|
|
if (err) {
|
|
|
|
dbg("error: %d while allocating root node\n", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
vput(vp);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Start GC. */
|
2012-03-13 02:37:22 +01:00
|
|
|
chfs_gc_thread_start(chmp);
|
|
|
|
mutex_enter(&chmp->chm_lock_mountfields);
|
|
|
|
chfs_gc_trigger(chmp);
|
|
|
|
mutex_exit(&chmp->chm_lock_mountfields);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
spec_node_setmountedfs(devvp, mp);
|
2012-03-13 02:37:22 +01:00
|
|
|
return 0;
|
2013-12-06 12:04:52 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
kmem_free(chmp->chm_ebh, sizeof(struct chfs_ebh));
|
|
|
|
kmem_free(chmp, sizeof(struct chfs_mount));
|
|
|
|
kmem_free(ump, sizeof(struct ufsmount));
|
|
|
|
return err;
|
2012-03-13 02:37:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
chfs_unmount(struct mount *mp, int mntflags)
|
|
|
|
{
|
|
|
|
int flags = 0, i = 0;
|
|
|
|
struct ufsmount *ump;
|
|
|
|
struct chfs_mount *chmp;
|
|
|
|
|
|
|
|
if (mntflags & MNT_FORCE)
|
|
|
|
flags |= FORCECLOSE;
|
|
|
|
|
|
|
|
dbg("[START]\n");
|
|
|
|
|
|
|
|
ump = VFSTOUFS(mp);
|
|
|
|
chmp = ump->um_chfs;
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Stop GC. */
|
2012-03-13 02:37:22 +01:00
|
|
|
chfs_gc_thread_stop(chmp);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Flush everyt buffer. */
|
2012-03-13 02:37:22 +01:00
|
|
|
(void)vflush(mp, NULLVP, flags);
|
|
|
|
|
|
|
|
if (chmp->chm_wbuf_len) {
|
|
|
|
mutex_enter(&chmp->chm_lock_mountfields);
|
|
|
|
chfs_flush_pending_wbuf(chmp);
|
|
|
|
mutex_exit(&chmp->chm_lock_mountfields);
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Free node references. */
|
2012-03-13 02:37:22 +01:00
|
|
|
for (i = 0; i < chmp->chm_ebh->peb_nr; i++) {
|
|
|
|
chfs_free_node_refs(&chmp->chm_blocks[i]);
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Destroy vnode cache hashtable. */
|
2012-03-13 02:37:22 +01:00
|
|
|
chfs_vnocache_hash_destroy(chmp->chm_vnocache_hash);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Close eraseblock handler. */
|
2012-03-13 02:37:22 +01:00
|
|
|
ebh_close(chmp->chm_ebh);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Destroy mutexes. */
|
2012-03-13 02:37:22 +01:00
|
|
|
rw_destroy(&chmp->chm_lock_wbuf);
|
|
|
|
mutex_destroy(&chmp->chm_lock_vnocache);
|
|
|
|
mutex_destroy(&chmp->chm_lock_sizes);
|
|
|
|
mutex_destroy(&chmp->chm_lock_mountfields);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Unmount UFS. */
|
2012-03-13 02:37:22 +01:00
|
|
|
if (ump->um_devvp->v_type != VBAD) {
|
2013-12-06 12:04:52 +01:00
|
|
|
spec_node_setmountedfs(ump->um_devvp, NULL);
|
2012-03-13 02:37:22 +01:00
|
|
|
}
|
|
|
|
vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
|
|
|
|
(void)VOP_CLOSE(ump->um_devvp, FREAD|FWRITE, NOCRED);
|
|
|
|
vput(ump->um_devvp);
|
|
|
|
|
|
|
|
mutex_destroy(&ump->um_lock);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Everything done. */
|
|
|
|
kmem_free(ump, sizeof(struct ufsmount));
|
2012-03-13 02:37:22 +01:00
|
|
|
mp->mnt_data = NULL;
|
|
|
|
mp->mnt_flag &= ~MNT_LOCAL;
|
|
|
|
dbg("[END]\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
chfs_root(struct mount *mp, struct vnode **vpp)
|
|
|
|
{
|
|
|
|
struct vnode *vp;
|
|
|
|
int error;
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
if ((error = VFS_VGET(mp, (ino_t)UFS_ROOTINO, &vp)) != 0)
|
2012-03-13 02:37:22 +01:00
|
|
|
return error;
|
|
|
|
*vpp = vp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
extern rb_tree_ops_t frag_rbtree_ops;
|
|
|
|
|
|
|
|
static int
|
|
|
|
chfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
|
|
|
|
{
|
|
|
|
struct chfs_mount *chmp;
|
|
|
|
struct chfs_inode *ip;
|
|
|
|
struct ufsmount *ump;
|
|
|
|
struct vnode *vp;
|
|
|
|
dev_t dev;
|
|
|
|
int error;
|
|
|
|
struct chfs_vnode_cache* chvc = NULL;
|
|
|
|
struct chfs_node_ref* nref = NULL;
|
|
|
|
struct buf *bp;
|
|
|
|
|
|
|
|
dbg("vget() | ino: %llu\n", (unsigned long long)ino);
|
|
|
|
|
|
|
|
ump = VFSTOUFS(mp);
|
|
|
|
dev = ump->um_dev;
|
|
|
|
retry:
|
|
|
|
if (!vpp) {
|
|
|
|
vpp = kmem_alloc(sizeof(struct vnode*), KM_SLEEP);
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Get node from inode hash. */
|
2012-03-13 02:37:22 +01:00
|
|
|
if ((*vpp = chfs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a new vnode/inode. */
|
|
|
|
if ((error = getnewvnode(VT_CHFS,
|
|
|
|
mp, chfs_vnodeop_p, NULL, &vp)) != 0) {
|
|
|
|
*vpp = NULL;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
ip = pool_get(&chfs_inode_pool, PR_WAITOK);
|
|
|
|
|
|
|
|
mutex_enter(&chfs_hashlock);
|
|
|
|
if ((*vpp = chfs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
|
|
|
|
mutex_exit(&chfs_hashlock);
|
|
|
|
ungetnewvnode(vp);
|
|
|
|
pool_put(&chfs_inode_pool, ip);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
vp->v_vflag |= VV_LOCKSWORK;
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize vnode/inode. */
|
2012-03-13 02:37:22 +01:00
|
|
|
memset(ip, 0, sizeof(*ip));
|
|
|
|
vp->v_data = ip;
|
|
|
|
ip->vp = vp;
|
2013-12-06 12:04:52 +01:00
|
|
|
ip->ch_type = VTTOCHT(vp->v_type);
|
2012-03-13 02:37:22 +01:00
|
|
|
ip->ump = ump;
|
|
|
|
ip->chmp = chmp = ump->um_chfs;
|
|
|
|
ip->dev = dev;
|
|
|
|
ip->ino = ino;
|
|
|
|
vp->v_mount = mp;
|
|
|
|
genfs_node_init(vp, &chfs_genfsops);
|
|
|
|
|
|
|
|
rb_tree_init(&ip->fragtree, &frag_rbtree_ops);
|
|
|
|
|
|
|
|
chfs_ihashins(ip);
|
|
|
|
mutex_exit(&chfs_hashlock);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Set root inode. */
|
2012-03-13 02:37:22 +01:00
|
|
|
if (ino == CHFS_ROOTINO) {
|
|
|
|
dbg("SETROOT\n");
|
|
|
|
vp->v_vflag |= VV_ROOT;
|
|
|
|
vp->v_type = VDIR;
|
2013-12-06 12:04:52 +01:00
|
|
|
ip->ch_type = CHT_DIR;
|
2012-03-13 02:37:22 +01:00
|
|
|
ip->mode = IFMT | IEXEC | IWRITE | IREAD;
|
|
|
|
ip->iflag |= (IN_ACCESS | IN_CHANGE | IN_UPDATE);
|
|
|
|
chfs_update(vp, NULL, NULL, UPDATE_WAIT);
|
|
|
|
TAILQ_INIT(&ip->dents);
|
|
|
|
chfs_set_vnode_size(vp, 512);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_enter(&chmp->chm_lock_vnocache);
|
|
|
|
chvc = chfs_vnode_cache_get(chmp, ino);
|
|
|
|
mutex_exit(&chmp->chm_lock_vnocache);
|
|
|
|
if (!chvc) {
|
|
|
|
dbg("!chvc\n");
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize the corresponding vnode cache. */
|
2012-03-13 02:37:22 +01:00
|
|
|
/* XXX, we cant alloc under a lock, refactor this! */
|
|
|
|
chvc = chfs_vnode_cache_alloc(ino);
|
|
|
|
mutex_enter(&chmp->chm_lock_vnocache);
|
|
|
|
if (ino == CHFS_ROOTINO) {
|
|
|
|
chvc->nlink = 2;
|
|
|
|
chvc->pvno = CHFS_ROOTINO;
|
2013-12-06 12:04:52 +01:00
|
|
|
chvc->state = VNO_STATE_CHECKEDABSENT;
|
2012-03-13 02:37:22 +01:00
|
|
|
}
|
|
|
|
chfs_vnode_cache_add(chmp, chvc);
|
|
|
|
mutex_exit(&chmp->chm_lock_vnocache);
|
|
|
|
|
|
|
|
ip->chvc = chvc;
|
|
|
|
TAILQ_INIT(&ip->dents);
|
|
|
|
} else {
|
|
|
|
dbg("chvc\n");
|
|
|
|
ip->chvc = chvc;
|
2013-12-06 12:04:52 +01:00
|
|
|
/* We had a vnode cache, the node is already on flash, so read it */
|
2012-03-13 02:37:22 +01:00
|
|
|
if (ino == CHFS_ROOTINO) {
|
|
|
|
chvc->pvno = CHFS_ROOTINO;
|
|
|
|
TAILQ_INIT(&chvc->scan_dirents);
|
|
|
|
} else {
|
|
|
|
chfs_readvnode(mp, ino, &vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_enter(&chmp->chm_lock_mountfields);
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize type specific things. */
|
|
|
|
switch (ip->ch_type) {
|
|
|
|
case CHT_DIR:
|
|
|
|
/* Read every dirent. */
|
2012-03-13 02:37:22 +01:00
|
|
|
nref = chvc->dirents;
|
|
|
|
while (nref &&
|
|
|
|
(struct chfs_vnode_cache *)nref != chvc) {
|
|
|
|
chfs_readdirent(mp, nref, ip);
|
|
|
|
nref = nref->nref_next;
|
|
|
|
}
|
|
|
|
chfs_set_vnode_size(vp, 512);
|
|
|
|
break;
|
2013-12-06 12:04:52 +01:00
|
|
|
case CHT_REG:
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CHT_SOCK:
|
|
|
|
/* Collect data. */
|
2012-03-13 02:37:22 +01:00
|
|
|
dbg("read_inode_internal | ino: %llu\n",
|
|
|
|
(unsigned long long)ip->ino);
|
|
|
|
error = chfs_read_inode(chmp, ip);
|
|
|
|
if (error) {
|
|
|
|
vput(vp);
|
|
|
|
*vpp = NULL;
|
|
|
|
mutex_exit(&chmp->chm_lock_mountfields);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
break;
|
2013-12-06 12:04:52 +01:00
|
|
|
case CHT_LNK:
|
|
|
|
/* Collect data. */
|
2012-03-13 02:37:22 +01:00
|
|
|
dbg("read_inode_internal | ino: %llu\n",
|
|
|
|
(unsigned long long)ip->ino);
|
|
|
|
error = chfs_read_inode_internal(chmp, ip);
|
|
|
|
if (error) {
|
|
|
|
vput(vp);
|
|
|
|
*vpp = NULL;
|
|
|
|
mutex_exit(&chmp->chm_lock_mountfields);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Set link. */
|
2012-03-13 02:37:22 +01:00
|
|
|
dbg("size: %llu\n", (unsigned long long)ip->size);
|
|
|
|
bp = getiobuf(vp, true);
|
|
|
|
bp->b_blkno = 0;
|
|
|
|
bp->b_bufsize = bp->b_resid =
|
|
|
|
bp->b_bcount = ip->size;
|
|
|
|
bp->b_data = kmem_alloc(ip->size, KM_SLEEP);
|
|
|
|
chfs_read_data(chmp, vp, bp);
|
|
|
|
if (!ip->target)
|
|
|
|
ip->target = kmem_alloc(ip->size,
|
|
|
|
KM_SLEEP);
|
|
|
|
memcpy(ip->target, bp->b_data, ip->size);
|
|
|
|
kmem_free(bp->b_data, ip->size);
|
|
|
|
putiobuf(bp);
|
|
|
|
|
|
|
|
break;
|
2013-12-06 12:04:52 +01:00
|
|
|
case CHT_CHR:
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CHT_BLK:
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CHT_FIFO:
|
|
|
|
/* Collect data. */
|
2012-03-13 02:37:22 +01:00
|
|
|
dbg("read_inode_internal | ino: %llu\n",
|
|
|
|
(unsigned long long)ip->ino);
|
|
|
|
error = chfs_read_inode_internal(chmp, ip);
|
|
|
|
if (error) {
|
|
|
|
vput(vp);
|
|
|
|
*vpp = NULL;
|
|
|
|
mutex_exit(&chmp->chm_lock_mountfields);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Set device. */
|
2012-03-13 02:37:22 +01:00
|
|
|
bp = getiobuf(vp, true);
|
|
|
|
bp->b_blkno = 0;
|
|
|
|
bp->b_bufsize = bp->b_resid =
|
|
|
|
bp->b_bcount = sizeof(dev_t);
|
|
|
|
bp->b_data = kmem_alloc(sizeof(dev_t), KM_SLEEP);
|
|
|
|
chfs_read_data(chmp, vp, bp);
|
|
|
|
memcpy(&ip->rdev,
|
|
|
|
bp->b_data, sizeof(dev_t));
|
|
|
|
kmem_free(bp->b_data, sizeof(dev_t));
|
|
|
|
putiobuf(bp);
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Set specific operations. */
|
|
|
|
if (ip->ch_type == CHT_FIFO) {
|
2012-03-13 02:37:22 +01:00
|
|
|
vp->v_op = chfs_fifoop_p;
|
2013-12-06 12:04:52 +01:00
|
|
|
} else {
|
2012-03-13 02:37:22 +01:00
|
|
|
vp->v_op = chfs_specop_p;
|
|
|
|
spec_node_init(vp, ip->rdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2013-12-06 12:04:52 +01:00
|
|
|
case CHT_BLANK:
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CHT_BAD:
|
2012-03-13 02:37:22 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_exit(&chmp->chm_lock_mountfields);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Finish inode initalization. */
|
2012-03-13 02:37:22 +01:00
|
|
|
ip->devvp = ump->um_devvp;
|
|
|
|
vref(ip->devvp);
|
|
|
|
|
|
|
|
uvm_vnp_setsize(vp, ip->size);
|
|
|
|
*vpp = vp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
chfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
|
|
|
|
{
|
|
|
|
return ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
chfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
|
|
|
|
{
|
|
|
|
return ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
chfs_start(struct mount *mp, int flags)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
chfs_statvfs(struct mount *mp, struct statvfs *sbp)
|
|
|
|
{
|
|
|
|
struct chfs_mount *chmp;
|
|
|
|
struct ufsmount *ump;
|
|
|
|
dbg("statvfs\n");
|
|
|
|
|
|
|
|
ump = VFSTOUFS(mp);
|
|
|
|
chmp = ump->um_chfs;
|
|
|
|
|
|
|
|
sbp->f_flag = mp->mnt_flag;
|
|
|
|
sbp->f_bsize = chmp->chm_ebh->eb_size;
|
|
|
|
sbp->f_frsize = chmp->chm_ebh->eb_size;
|
|
|
|
sbp->f_iosize = chmp->chm_ebh->eb_size;
|
|
|
|
|
|
|
|
sbp->f_blocks = chmp->chm_ebh->peb_nr;
|
|
|
|
sbp->f_files = 0;
|
|
|
|
sbp->f_bavail = chmp->chm_nr_free_blocks - chmp->chm_resv_blocks_write;
|
|
|
|
|
|
|
|
sbp->f_bfree = chmp->chm_nr_free_blocks;
|
|
|
|
sbp->f_bresvd = chmp->chm_resv_blocks_write;
|
|
|
|
|
|
|
|
/* FFS specific */
|
|
|
|
sbp->f_ffree = 0;
|
|
|
|
sbp->f_favail = 0;
|
|
|
|
sbp->f_fresvd = 0;
|
|
|
|
|
|
|
|
copy_statvfs_info(sbp, mp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
chfs_sync(struct mount *mp, int waitfor,
|
|
|
|
kauth_cred_t uc)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
|
|
|
chfs_init(void)
|
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
/* Initialize pools and inode hash. */
|
2012-03-13 02:37:22 +01:00
|
|
|
chfs_alloc_pool_caches();
|
|
|
|
chfs_ihashinit();
|
|
|
|
pool_init(&chfs_inode_pool, sizeof(struct chfs_inode), 0, 0, 0,
|
|
|
|
"chfsinopl", &pool_allocator_nointr, IPL_NONE);
|
|
|
|
ufs_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
|
|
|
chfs_reinit(void)
|
|
|
|
{
|
|
|
|
chfs_ihashreinit();
|
|
|
|
ufs_reinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
|
|
|
chfs_done(void)
|
|
|
|
{
|
|
|
|
ufs_done();
|
|
|
|
chfs_ihashdone();
|
|
|
|
pool_destroy(&chfs_inode_pool);
|
|
|
|
chfs_destroy_pool_caches();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
chfs_snapshot(struct mount *mp, struct vnode *vp,
|
|
|
|
struct timespec *ctime)
|
|
|
|
{
|
|
|
|
return ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* chfs vfs operations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern const struct vnodeopv_desc chfs_fifoop_opv_desc;
|
|
|
|
extern const struct vnodeopv_desc chfs_specop_opv_desc;
|
|
|
|
extern const struct vnodeopv_desc chfs_vnodeop_opv_desc;
|
|
|
|
|
|
|
|
const struct vnodeopv_desc * const chfs_vnodeopv_descs[] = {
|
|
|
|
&chfs_fifoop_opv_desc,
|
|
|
|
&chfs_specop_opv_desc,
|
|
|
|
&chfs_vnodeop_opv_desc,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct vfsops chfs_vfsops = {
|
|
|
|
MOUNT_CHFS, /* vfs_name */
|
|
|
|
sizeof (struct chfs_args),
|
|
|
|
chfs_mount, /* vfs_mount */
|
|
|
|
chfs_start, /* vfs_start */
|
|
|
|
chfs_unmount, /* vfs_unmount */
|
|
|
|
chfs_root, /* vfs_root */
|
2013-12-06 12:04:52 +01:00
|
|
|
ufs_quotactl, /* vfs_quotactl */
|
2012-03-13 02:37:22 +01:00
|
|
|
chfs_statvfs, /* vfs_statvfs */
|
|
|
|
chfs_sync, /* vfs_sync */
|
|
|
|
chfs_vget, /* vfs_vget */
|
|
|
|
chfs_fhtovp, /* vfs_fhtovp */
|
|
|
|
chfs_vptofh, /* vfs_vptofh */
|
|
|
|
chfs_init, /* vfs_init */
|
|
|
|
chfs_reinit, /* vfs_reinit */
|
|
|
|
chfs_done, /* vfs_done */
|
|
|
|
NULL, /* vfs_mountroot */
|
|
|
|
chfs_snapshot, /* vfs_snapshot */
|
2013-12-06 12:04:52 +01:00
|
|
|
vfs_stdextattrctl, /* vfs_extattrctl */
|
|
|
|
(void *)eopnotsupp, /* vfs_suspendctl */
|
2012-03-13 02:37:22 +01:00
|
|
|
genfs_renamelock_enter,
|
|
|
|
genfs_renamelock_exit,
|
|
|
|
(void *)eopnotsupp,
|
|
|
|
chfs_vnodeopv_descs,
|
2013-12-06 12:04:52 +01:00
|
|
|
0, /* vfs_refcount */
|
2012-03-13 02:37:22 +01:00
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* For using CHFS as a module. */
|
2012-03-13 02:37:22 +01:00
|
|
|
static int
|
|
|
|
chfs_modcmd(modcmd_t cmd, void *arg)
|
|
|
|
{
|
|
|
|
switch (cmd) {
|
|
|
|
case MODULE_CMD_INIT:
|
|
|
|
return vfs_attach(&chfs_vfsops);
|
|
|
|
case MODULE_CMD_FINI:
|
|
|
|
return vfs_detach(&chfs_vfsops);
|
|
|
|
default:
|
|
|
|
return ENOTTY;
|
|
|
|
}
|
|
|
|
}
|