Home | History | Annotate | Line # | Download | only in chfs
chfs_vfsops.c revision 1.17
      1 /*	$NetBSD: chfs_vfsops.c,v 1.17 2017/11/14 22:06:40 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 Department of Software Engineering,
      5  *		      University of Szeged, Hungary
      6  * Copyright (C) 2010 Tamas Toth <ttoth (at) inf.u-szeged.hu>
      7  * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
      8  * All rights reserved.
      9  *
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by the Department of Software Engineering, University of Szeged, Hungary
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 
     37 #include <sys/param.h>
     38 #include <sys/types.h>
     39 #include <sys/kmem.h>
     40 #include <sys/mount.h>
     41 #include <sys/stat.h>
     42 #include <sys/systm.h>
     43 #include <sys/proc.h>
     44 #include <sys/module.h>
     45 #include <sys/namei.h>
     46 #include <sys/fcntl.h>
     47 #include <sys/conf.h>
     48 #include <sys/buf.h>
     49 //XXX needed just for debugging
     50 #include <sys/fstrans.h>
     51 #include <sys/sleepq.h>
     52 #include <sys/lockdebug.h>
     53 #include <sys/ktrace.h>
     54 
     55 #include <uvm/uvm.h>
     56 #include <uvm/uvm_pager.h>
     57 #include <ufs/ufs/dir.h>
     58 #include <ufs/ufs/ufs_extern.h>
     59 #include <miscfs/genfs/genfs.h>
     60 #include <miscfs/genfs/genfs_node.h>
     61 #include <miscfs/specfs/specdev.h>
     62 #include "chfs.h"
     63 #include "chfs_args.h"
     64 
     65 MODULE(MODULE_CLASS_VFS, chfs, "flash");
     66 
     67 /* --------------------------------------------------------------------- */
     68 /* functions */
     69 
     70 static int chfs_mount(struct mount *, const char *, void *, size_t *);
     71 static int chfs_unmount(struct mount *, int);
     72 static int chfs_root(struct mount *, struct vnode **);
     73 static int chfs_loadvnode(struct mount *, struct vnode *,
     74     const void *, size_t, const void **);
     75 static int chfs_vget(struct mount *, ino_t, struct vnode **);
     76 static int chfs_fhtovp(struct mount *, struct fid *, struct vnode **);
     77 static int chfs_vptofh(struct vnode *, struct fid *, size_t *);
     78 static int chfs_start(struct mount *, int);
     79 static int chfs_statvfs(struct mount *, struct statvfs *);
     80 static int chfs_sync(struct mount *, int, kauth_cred_t);
     81 static void chfs_init(void);
     82 static void chfs_reinit(void);
     83 static void chfs_done(void);
     84 static int chfs_snapshot(struct mount *, struct vnode *,
     85     struct timespec *);
     86 
     87 /* --------------------------------------------------------------------- */
     88 /* structures */
     89 
     90 int
     91 chfs_gop_alloc(struct vnode *vp, off_t off, off_t len,  int flags,
     92     kauth_cred_t cred)
     93 {
     94 	return (0);
     95 }
     96 
     97 const struct genfs_ops chfs_genfsops = {
     98 	.gop_size = genfs_size,
     99 	.gop_alloc = chfs_gop_alloc,
    100 	.gop_write = genfs_gop_write,
    101 	.gop_markupdate = ufs_gop_markupdate,
    102 };
    103 
    104 struct pool chfs_inode_pool;
    105 
    106 /* for looking up the major for flash */
    107 extern const struct cdevsw flash_cdevsw;
    108 
    109 /* --------------------------------------------------------------------- */
    110 
    111 static int
    112 chfs_mount(struct mount *mp,
    113     const char *path, void *data, size_t *data_len)
    114 {
    115 	struct lwp *l = curlwp;
    116 	struct nameidata nd;
    117 	struct pathbuf *pb;
    118 	struct vnode *devvp = NULL;
    119 	struct ufs_args *args = data;
    120 	struct ufsmount *ump = NULL;
    121 	struct chfs_mount *chmp;
    122 	int err = 0;
    123 	int xflags;
    124 
    125 	dbg("mount()\n");
    126 
    127 	if (args == NULL)
    128 		return EINVAL;
    129 	if (*data_len < sizeof *args)
    130 		return EINVAL;
    131 
    132 	if (mp->mnt_flag & MNT_GETARGS) {
    133 		ump = VFSTOUFS(mp);
    134 		if (ump == NULL)
    135 			return EIO;
    136 		memset(args, 0, sizeof *args);
    137 		args->fspec = NULL;
    138 		*data_len = sizeof *args;
    139 		return 0;
    140 	}
    141 
    142 	if (mp->mnt_flag & MNT_UPDATE) {
    143 		/* XXX: There is no support yet to update file system
    144 		 * settings.  Should be added. */
    145 
    146 		return ENODEV;
    147 	}
    148 
    149 	if (args->fspec != NULL) {
    150 		err = pathbuf_copyin(args->fspec, &pb);
    151 		if (err) {
    152 			return err;
    153 		}
    154 		/* Look up the name and verify that it's sane. */
    155 		NDINIT(&nd, LOOKUP, FOLLOW, pb);
    156 		err = namei(&nd);
    157 		pathbuf_destroy(pb);
    158 		if (err)
    159 			return err;
    160 		devvp = nd.ni_vp;
    161 
    162 		/* Be sure this is a valid block device */
    163 		if (devvp->v_type != VBLK)
    164 			err = ENOTBLK;
    165 		else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    166 			err = ENXIO;
    167 	}
    168 
    169 	if (err) {
    170 		vrele(devvp);
    171 		return (err);
    172 	}
    173 
    174 	if (mp->mnt_flag & MNT_RDONLY)
    175 		xflags = FREAD;
    176 	else
    177 		xflags = FREAD|FWRITE;
    178 
    179 	err = VOP_OPEN(devvp, xflags, FSCRED);
    180 	if (err)
    181 		goto fail;
    182 
    183 	/* call CHFS mount function */
    184 	err = chfs_mountfs(devvp, mp);
    185 	if (err) {
    186 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    187 		(void)VOP_CLOSE(devvp, xflags, NOCRED);
    188 		VOP_UNLOCK(devvp);
    189 		goto fail;
    190 	}
    191 
    192 	ump = VFSTOUFS(mp);
    193 	chmp = ump->um_chfs;
    194 
    195 	vfs_getnewfsid(mp);
    196 	chmp->chm_fsmp = mp;
    197 
    198 	return set_statvfs_info(path,
    199 	    UIO_USERSPACE, args->fspec,
    200 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    201 
    202 fail:
    203 	vrele(devvp);
    204 	return (err);
    205 }
    206 
    207 /* chfs_mountfs - init CHFS */
    208 int
    209 chfs_mountfs(struct vnode *devvp, struct mount *mp)
    210 {
    211 	struct lwp *l = curlwp;
    212 	kauth_cred_t cred;
    213 	devmajor_t flash_major;
    214 	dev_t dev;
    215 	struct ufsmount* ump = NULL;
    216 	struct chfs_mount* chmp;
    217 	struct vnode *vp;
    218 	int err = 0;
    219 
    220 	dbg("mountfs()\n");
    221 
    222 	dev = devvp->v_rdev;
    223 	cred = l ? l->l_cred : NOCRED;
    224 
    225 	/* Flush out any old buffers remaining from a previous use. */
    226 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    227 	err = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    228 	VOP_UNLOCK(devvp);
    229 	if (err)
    230 		goto fail0;
    231 
    232 	/* Setup device. */
    233 	flash_major = cdevsw_lookup_major(&flash_cdevsw);
    234 
    235 	if (devvp->v_type != VBLK)
    236 		err = ENOTBLK;
    237 	else if (bdevsw_lookup(dev) == NULL)
    238 		err = ENXIO;
    239 	else if (major(dev) != flash_major) {
    240 		dbg("major(dev): %d, flash_major: %d\n",
    241 		    major(dev), flash_major);
    242 		err = ENODEV;
    243 	}
    244 	if (err)
    245 		goto fail0;
    246 
    247 	/* Connect CHFS to UFS. */
    248 	ump = kmem_zalloc(sizeof(struct ufsmount), KM_SLEEP);
    249 
    250 	ump->um_fstype = UFS1;
    251 	ump->um_chfs = kmem_zalloc(sizeof(struct chfs_mount), KM_SLEEP);
    252 	mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
    253 
    254 	chmp = ump->um_chfs;
    255 
    256 	/* Initialize erase block handler. */
    257 	chmp->chm_ebh = kmem_alloc(sizeof(struct chfs_ebh), KM_SLEEP);
    258 
    259 	dbg("[]opening flash: %u\n", (unsigned int)devvp->v_rdev);
    260 	err = ebh_open(chmp->chm_ebh, devvp->v_rdev);
    261 	if (err) {
    262 		dbg("error while opening flash\n");
    263 		goto fail1;
    264 	}
    265 
    266 	//TODO check flash sizes
    267 
    268 	/* Initialize vnode cache's hashtable and eraseblock array. */
    269 	chmp->chm_gbl_version = 0;
    270 	chmp->chm_vnocache_hash = chfs_vnocache_hash_init();
    271 
    272 	chmp->chm_blocks = kmem_zalloc(chmp->chm_ebh->peb_nr *
    273 	    sizeof(struct chfs_eraseblock), KM_SLEEP);
    274 
    275 	/* Initialize mutexes. */
    276 	mutex_init(&chmp->chm_lock_mountfields, MUTEX_DEFAULT, IPL_NONE);
    277 	mutex_init(&chmp->chm_lock_sizes, MUTEX_DEFAULT, IPL_NONE);
    278 	mutex_init(&chmp->chm_lock_vnocache, MUTEX_DEFAULT, IPL_NONE);
    279 
    280 	/* Initialize read/write contants. (from UFS) */
    281 	chmp->chm_fs_bmask = -4096;
    282 	chmp->chm_fs_bsize = 4096;
    283 	chmp->chm_fs_qbmask = 4095;
    284 	chmp->chm_fs_bshift = 12;
    285 	chmp->chm_fs_fmask = -2048;
    286 	chmp->chm_fs_qfmask = 2047;
    287 
    288 	/* Initialize writebuffer. */
    289 	chmp->chm_wbuf_pagesize = chmp->chm_ebh->flash_if->page_size;
    290 	dbg("wbuf size: %zu\n", chmp->chm_wbuf_pagesize);
    291 	chmp->chm_wbuf = kmem_alloc(chmp->chm_wbuf_pagesize, KM_SLEEP);
    292 	rw_init(&chmp->chm_lock_wbuf);
    293 
    294 	/* Initialize queues. */
    295 	TAILQ_INIT(&chmp->chm_free_queue);
    296 	TAILQ_INIT(&chmp->chm_clean_queue);
    297 	TAILQ_INIT(&chmp->chm_dirty_queue);
    298 	TAILQ_INIT(&chmp->chm_very_dirty_queue);
    299 	TAILQ_INIT(&chmp->chm_erasable_pending_wbuf_queue);
    300 	TAILQ_INIT(&chmp->chm_erase_pending_queue);
    301 
    302 	/* Initialize flash-specific constants. */
    303 	chfs_calc_trigger_levels(chmp);
    304 
    305 	/* Initialize sizes. */
    306 	chmp->chm_nr_free_blocks = 0;
    307 	chmp->chm_nr_erasable_blocks = 0;
    308 	chmp->chm_max_vno = 2;
    309 	chmp->chm_checked_vno = 2;
    310 	chmp->chm_unchecked_size = 0;
    311 	chmp->chm_used_size = 0;
    312 	chmp->chm_dirty_size = 0;
    313 	chmp->chm_wasted_size = 0;
    314 	chmp->chm_free_size = chmp->chm_ebh->eb_size * chmp->chm_ebh->peb_nr;
    315 
    316 	/* Build filesystem. */
    317 	err = chfs_build_filesystem(chmp);
    318 
    319 	if (err) {
    320 		/* Armageddon and return. */
    321 		err = EIO;
    322 		goto fail2;
    323 	}
    324 
    325 	/* Initialize UFS. */
    326 	mp->mnt_data = ump;
    327 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    328 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CHFS);
    329 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    330 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    331 	mp->mnt_flag |= MNT_LOCAL;
    332 	mp->mnt_fs_bshift = PAGE_SHIFT;
    333 	mp->mnt_dev_bshift = DEV_BSHIFT;
    334 	mp->mnt_iflag |= IMNT_MPSAFE;
    335 	ump->um_flags = 0;
    336 	ump->um_mountp = mp;
    337 	ump->um_dev = dev;
    338 	ump->um_devvp = devvp;
    339 	ump->um_maxfilesize = 1048512 * 1024;
    340 
    341 	/* Allocate the root vnode. */
    342 	err = VFS_VGET(mp, CHFS_ROOTINO, &vp);
    343 	if (err) {
    344 		dbg("error: %d while allocating root node\n", err);
    345 		return err;
    346 	}
    347 	vput(vp);
    348 
    349 	/* Start GC. */
    350 	chfs_gc_thread_start(chmp);
    351 	mutex_enter(&chmp->chm_lock_mountfields);
    352 	chfs_gc_trigger(chmp);
    353 	mutex_exit(&chmp->chm_lock_mountfields);
    354 
    355 	spec_node_setmountedfs(devvp, mp);
    356 	return 0;
    357 
    358 fail2:
    359 	KASSERT(TAILQ_EMPTY(&chmp->chm_erase_pending_queue));
    360 	KASSERT(TAILQ_EMPTY(&chmp->chm_erasable_pending_wbuf_queue));
    361 	KASSERT(TAILQ_EMPTY(&chmp->chm_very_dirty_queue));
    362 	KASSERT(TAILQ_EMPTY(&chmp->chm_dirty_queue));
    363 	KASSERT(TAILQ_EMPTY(&chmp->chm_clean_queue));
    364 	KASSERT(TAILQ_EMPTY(&chmp->chm_free_queue));
    365 	rw_destroy(&chmp->chm_lock_wbuf);
    366 	kmem_free(chmp->chm_wbuf, chmp->chm_wbuf_pagesize);
    367 	mutex_destroy(&chmp->chm_lock_vnocache);
    368 	mutex_destroy(&chmp->chm_lock_sizes);
    369 	mutex_destroy(&chmp->chm_lock_mountfields);
    370 	kmem_free(chmp->chm_blocks, chmp->chm_ebh->peb_nr *
    371 	    sizeof(struct chfs_eraseblock));
    372 	chfs_vnocache_hash_destroy(chmp->chm_vnocache_hash);
    373 	ebh_close(chmp->chm_ebh);
    374 
    375 fail1:
    376 	kmem_free(chmp->chm_ebh, sizeof(struct chfs_ebh));
    377 	mutex_destroy(&ump->um_lock);
    378 	kmem_free(chmp, sizeof(struct chfs_mount));
    379 	kmem_free(ump, sizeof(struct ufsmount));
    380 
    381 fail0:
    382 	KASSERT(err);
    383 	return err;
    384 }
    385 
    386 /* --------------------------------------------------------------------- */
    387 
    388 static int
    389 chfs_unmount(struct mount *mp, int mntflags)
    390 {
    391 	int flags = 0, i = 0;
    392 	struct ufsmount *ump;
    393 	struct chfs_mount *chmp;
    394 
    395 	if (mntflags & MNT_FORCE)
    396 		flags |= FORCECLOSE;
    397 
    398 	dbg("[START]\n");
    399 
    400 	ump = VFSTOUFS(mp);
    401 	chmp = ump->um_chfs;
    402 
    403 	/* Stop GC. */
    404 	chfs_gc_thread_stop(chmp);
    405 
    406 	/* Flush everyt buffer. */
    407 	(void)vflush(mp, NULLVP, flags);
    408 
    409 	if (chmp->chm_wbuf_len) {
    410 		mutex_enter(&chmp->chm_lock_mountfields);
    411 		chfs_flush_pending_wbuf(chmp);
    412 		mutex_exit(&chmp->chm_lock_mountfields);
    413 	}
    414 
    415 	/* Free node references. */
    416 	for (i = 0; i < chmp->chm_ebh->peb_nr; i++) {
    417 		chfs_free_node_refs(&chmp->chm_blocks[i]);
    418 	}
    419 
    420 	/* Destroy vnode cache hashtable. */
    421 	chfs_vnocache_hash_destroy(chmp->chm_vnocache_hash);
    422 
    423 	/* Close eraseblock handler. */
    424 	ebh_close(chmp->chm_ebh);
    425 
    426 	/* Destroy mutexes. */
    427 	rw_destroy(&chmp->chm_lock_wbuf);
    428 	mutex_destroy(&chmp->chm_lock_vnocache);
    429 	mutex_destroy(&chmp->chm_lock_sizes);
    430 	mutex_destroy(&chmp->chm_lock_mountfields);
    431 
    432 	/* Unmount UFS. */
    433 	if (ump->um_devvp->v_type != VBAD) {
    434 		spec_node_setmountedfs(ump->um_devvp, NULL);
    435 	}
    436 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    437 	(void)VOP_CLOSE(ump->um_devvp, FREAD|FWRITE, NOCRED);
    438 	vput(ump->um_devvp);
    439 
    440 	mutex_destroy(&ump->um_lock);
    441 
    442 	/* Everything done. */
    443 	kmem_free(ump, sizeof(struct ufsmount));
    444 	mp->mnt_data = NULL;
    445 	mp->mnt_flag &= ~MNT_LOCAL;
    446 	dbg("[END]\n");
    447 	return (0);
    448 }
    449 
    450 /* --------------------------------------------------------------------- */
    451 
    452 static int
    453 chfs_root(struct mount *mp, struct vnode **vpp)
    454 {
    455 	struct vnode *vp;
    456 	int error;
    457 
    458 	if ((error = VFS_VGET(mp, (ino_t)UFS_ROOTINO, &vp)) != 0)
    459 		return error;
    460 	*vpp = vp;
    461 	return 0;
    462 }
    463 
    464 /* --------------------------------------------------------------------- */
    465 
    466 extern rb_tree_ops_t frag_rbtree_ops;
    467 
    468 static int
    469 chfs_loadvnode(struct mount *mp, struct vnode *vp,
    470     const void *key, size_t key_len, const void **new_key)
    471 {
    472 	struct chfs_mount *chmp;
    473 	struct chfs_inode *ip;
    474 	struct ufsmount *ump;
    475 	dev_t dev;
    476 	int error;
    477 	struct chfs_vnode_cache* chvc = NULL;
    478 	struct chfs_node_ref* nref = NULL;
    479 	struct buf *bp;
    480 	ino_t ino;
    481 
    482 	KASSERT(key_len == sizeof(ino));
    483 	memcpy(&ino, key, key_len);
    484 
    485 	dbg("vget() | ino: %llu\n", (unsigned long long)ino);
    486 
    487 	ump = VFSTOUFS(mp);
    488 	dev = ump->um_dev;
    489 
    490 	ip = pool_get(&chfs_inode_pool, PR_WAITOK);
    491 
    492 	/* Initialize vnode/inode. */
    493 	memset(ip, 0, sizeof(*ip));
    494 	ip->vp = vp;
    495 	ip->ump = ump;
    496 	ip->chmp = chmp = ump->um_chfs;
    497 	ip->dev = dev;
    498 	ip->ino = ino;
    499 
    500 	rb_tree_init(&ip->fragtree, &frag_rbtree_ops);
    501 
    502 	vp->v_tag = VT_CHFS;
    503 	vp->v_op = chfs_vnodeop_p;
    504 	vp->v_vflag |= VV_LOCKSWORK;
    505 	if (ino == CHFS_ROOTINO)
    506 		vp->v_vflag |= VV_ROOT;
    507 	vp->v_data = ip;
    508 
    509 	/* Set root inode. */
    510 	if (ino == CHFS_ROOTINO) {
    511 		dbg("SETROOT\n");
    512 		vp->v_type = VDIR;
    513 		ip->ch_type = CHT_DIR;
    514 		ip->mode = IFMT | IEXEC | IWRITE | IREAD;
    515 		ip->iflag |= (IN_ACCESS | IN_CHANGE | IN_UPDATE);
    516 		chfs_update(vp, NULL, NULL, UPDATE_WAIT);
    517 		TAILQ_INIT(&ip->dents);
    518 		chfs_set_vnode_size(vp, 512);
    519 	}
    520 
    521 	mutex_enter(&chmp->chm_lock_vnocache);
    522 	chvc = chfs_vnode_cache_get(chmp, ino);
    523 	mutex_exit(&chmp->chm_lock_vnocache);
    524 	if (!chvc) {
    525 		dbg("!chvc\n");
    526 		/* Initialize the corresponding vnode cache. */
    527 		/* XXX, we cant alloc under a lock, refactor this! */
    528 		chvc = chfs_vnode_cache_alloc(ino);
    529 		mutex_enter(&chmp->chm_lock_vnocache);
    530 		if (ino == CHFS_ROOTINO) {
    531 			chvc->nlink = 2;
    532 			chvc->pvno = CHFS_ROOTINO;
    533 			chvc->state = VNO_STATE_CHECKEDABSENT;
    534 		}
    535 		chfs_vnode_cache_add(chmp, chvc);
    536 		mutex_exit(&chmp->chm_lock_vnocache);
    537 
    538 		ip->chvc = chvc;
    539 		TAILQ_INIT(&ip->dents);
    540 	} else {
    541 		dbg("chvc\n");
    542 		ip->chvc = chvc;
    543 		/* We had a vnode cache, the node is already on flash, so read it */
    544 		if (ino == CHFS_ROOTINO) {
    545 			chvc->pvno = CHFS_ROOTINO;
    546 			TAILQ_INIT(&chvc->scan_dirents);
    547 		} else {
    548 			chfs_readvnode(mp, ino, &vp);
    549 		}
    550 
    551 		mutex_enter(&chmp->chm_lock_mountfields);
    552 		/* Initialize type specific things. */
    553 		error = 0;
    554 		switch (ip->ch_type) {
    555 		case CHT_DIR:
    556 			/* Read every dirent. */
    557 			nref = chvc->dirents;
    558 			while (nref &&
    559 			    (struct chfs_vnode_cache *)nref != chvc) {
    560 				chfs_readdirent(mp, nref, ip);
    561 				nref = nref->nref_next;
    562 			}
    563 			chfs_set_vnode_size(vp, 512);
    564 			break;
    565 		case CHT_REG:
    566 			/* FALLTHROUGH */
    567 		case CHT_SOCK:
    568 			/* Collect data. */
    569 			dbg("read_inode_internal | ino: %llu\n",
    570 				(unsigned long long)ip->ino);
    571 			error = chfs_read_inode(chmp, ip);
    572 			break;
    573 		case CHT_LNK:
    574 			/* Collect data. */
    575 			dbg("read_inode_internal | ino: %llu\n",
    576 				(unsigned long long)ip->ino);
    577 			error = chfs_read_inode_internal(chmp, ip);
    578 			if (error)
    579 				break;
    580 
    581 			/* Set link. */
    582 			dbg("size: %llu\n", (unsigned long long)ip->size);
    583 			bp = getiobuf(vp, true);
    584 			bp->b_blkno = 0;
    585 			bp->b_bufsize = bp->b_resid =
    586 			    bp->b_bcount = ip->size;
    587 			bp->b_data = kmem_alloc(ip->size, KM_SLEEP);
    588 			chfs_read_data(chmp, vp, bp);
    589 			if (!ip->target)
    590 				ip->target = kmem_alloc(ip->size,
    591 				    KM_SLEEP);
    592 			memcpy(ip->target, bp->b_data, ip->size);
    593 			kmem_free(bp->b_data, ip->size);
    594 			putiobuf(bp);
    595 
    596 			break;
    597 		case CHT_CHR:
    598 			/* FALLTHROUGH */
    599 		case CHT_BLK:
    600 			/* FALLTHROUGH */
    601 		case CHT_FIFO:
    602 			/* Collect data. */
    603 			dbg("read_inode_internal | ino: %llu\n",
    604 				(unsigned long long)ip->ino);
    605 			error = chfs_read_inode_internal(chmp, ip);
    606 			if (error)
    607 				break;
    608 
    609 			/* Set device. */
    610 			bp = getiobuf(vp, true);
    611 			bp->b_blkno = 0;
    612 			bp->b_bufsize = bp->b_resid =
    613 			    bp->b_bcount = sizeof(dev_t);
    614 			bp->b_data = kmem_alloc(sizeof(dev_t), KM_SLEEP);
    615 			chfs_read_data(chmp, vp, bp);
    616 			memcpy(&ip->rdev,
    617 			    bp->b_data, sizeof(dev_t));
    618 			kmem_free(bp->b_data, sizeof(dev_t));
    619 			putiobuf(bp);
    620 			/* Set specific operations. */
    621 			if (ip->ch_type == CHT_FIFO) {
    622 				vp->v_op = chfs_fifoop_p;
    623 			} else {
    624 				vp->v_op = chfs_specop_p;
    625 				spec_node_init(vp, ip->rdev);
    626 			}
    627 
    628 		    break;
    629 		case CHT_BLANK:
    630 			/* FALLTHROUGH */
    631 		case CHT_BAD:
    632 			break;
    633 		}
    634 		mutex_exit(&chmp->chm_lock_mountfields);
    635 		if (error) {
    636 			vp->v_data = NULL;
    637 			KASSERT(TAILQ_FIRST(&ip->dents) == NULL);
    638 			pool_put(&chfs_inode_pool, ip);
    639 			return error;
    640 		}
    641 
    642 	}
    643 
    644 	/* Finish inode initalization. */
    645 	ip->ch_type = VTTOCHT(vp->v_type);
    646 	ip->devvp = ump->um_devvp;
    647 	vref(ip->devvp);
    648 
    649 	genfs_node_init(vp, &chfs_genfsops);
    650 	uvm_vnp_setsize(vp, ip->size);
    651 
    652 	*new_key = &ip->ino;
    653 
    654 	return 0;
    655 }
    656 
    657 /* --------------------------------------------------------------------- */
    658 
    659 static int
    660 chfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    661 {
    662 	int error;
    663 
    664 	error = vcache_get(mp, &ino, sizeof(ino), vpp);
    665 	if (error)
    666 		return error;
    667 
    668 	error = vn_lock(*vpp, LK_EXCLUSIVE);
    669 	if (error) {
    670 		vrele(*vpp);
    671 		*vpp = NULL;
    672 		return error;
    673 	}
    674 
    675 	return 0;
    676 }
    677 
    678 /* --------------------------------------------------------------------- */
    679 
    680 
    681 static int
    682 chfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    683 {
    684 	return ENODEV;
    685 }
    686 
    687 /* --------------------------------------------------------------------- */
    688 
    689 static int
    690 chfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    691 {
    692 	return ENODEV;
    693 }
    694 
    695 /* --------------------------------------------------------------------- */
    696 
    697 static int
    698 chfs_start(struct mount *mp, int flags)
    699 {
    700 	return 0;
    701 }
    702 
    703 /* --------------------------------------------------------------------- */
    704 
    705 static int
    706 chfs_statvfs(struct mount *mp, struct statvfs *sbp)
    707 {
    708  	struct chfs_mount *chmp;
    709 	struct ufsmount *ump;
    710 	dbg("statvfs\n");
    711 
    712 	ump = VFSTOUFS(mp);
    713 	chmp = ump->um_chfs;
    714 
    715 	sbp->f_flag   = mp->mnt_flag;
    716 	sbp->f_bsize  = chmp->chm_ebh->eb_size;
    717 	sbp->f_frsize = chmp->chm_ebh->eb_size;
    718 	sbp->f_iosize = chmp->chm_ebh->eb_size;
    719 
    720 	sbp->f_blocks = chmp->chm_ebh->peb_nr;
    721 	sbp->f_files  = 0;
    722 	sbp->f_bavail = chmp->chm_nr_free_blocks - chmp->chm_resv_blocks_write;
    723 
    724 	sbp->f_bfree = chmp->chm_nr_free_blocks;
    725 	sbp->f_bresvd = chmp->chm_resv_blocks_write;
    726 
    727 	/* FFS specific */
    728 	sbp->f_ffree  = 0;
    729 	sbp->f_favail = 0;
    730 	sbp->f_fresvd = 0;
    731 
    732 	copy_statvfs_info(sbp, mp);
    733 
    734 	return 0;
    735 }
    736 
    737 /* --------------------------------------------------------------------- */
    738 
    739 static int
    740 chfs_sync(struct mount *mp, int waitfor,
    741     kauth_cred_t uc)
    742 {
    743 	return 0;
    744 }
    745 
    746 /* --------------------------------------------------------------------- */
    747 
    748 static void
    749 chfs_init(void)
    750 {
    751 	/* Initialize pools and inode hash. */
    752 	chfs_alloc_pool_caches();
    753 	pool_init(&chfs_inode_pool, sizeof(struct chfs_inode), 0, 0, 0,
    754 	    "chfsinopl", &pool_allocator_nointr, IPL_NONE);
    755 	ufs_init();
    756 }
    757 
    758 /* --------------------------------------------------------------------- */
    759 
    760 static void
    761 chfs_reinit(void)
    762 {
    763 	ufs_reinit();
    764 }
    765 
    766 /* --------------------------------------------------------------------- */
    767 
    768 static void
    769 chfs_done(void)
    770 {
    771 	ufs_done();
    772 	pool_destroy(&chfs_inode_pool);
    773 	chfs_destroy_pool_caches();
    774 }
    775 
    776 /* --------------------------------------------------------------------- */
    777 
    778 static int
    779 chfs_snapshot(struct mount *mp, struct vnode *vp,
    780     struct timespec *ctime)
    781 {
    782 	return ENODEV;
    783 }
    784 
    785 /* --------------------------------------------------------------------- */
    786 
    787 /*
    788  * chfs vfs operations.
    789  */
    790 
    791 extern const struct vnodeopv_desc chfs_fifoop_opv_desc;
    792 extern const struct vnodeopv_desc chfs_specop_opv_desc;
    793 extern const struct vnodeopv_desc chfs_vnodeop_opv_desc;
    794 
    795 const struct vnodeopv_desc * const chfs_vnodeopv_descs[] = {
    796 	&chfs_fifoop_opv_desc,
    797 	&chfs_specop_opv_desc,
    798 	&chfs_vnodeop_opv_desc,
    799 	NULL,
    800 };
    801 
    802 struct vfsops chfs_vfsops = {
    803 	.vfs_name = MOUNT_CHFS,
    804 	.vfs_min_mount_data = sizeof (struct chfs_args),
    805 	.vfs_mount = chfs_mount,
    806 	.vfs_start = chfs_start,
    807 	.vfs_unmount = chfs_unmount,
    808 	.vfs_root = chfs_root,
    809 	.vfs_quotactl = ufs_quotactl,
    810 	.vfs_statvfs = chfs_statvfs,
    811 	.vfs_sync = chfs_sync,
    812 	.vfs_vget = chfs_vget,
    813 	.vfs_loadvnode = chfs_loadvnode,
    814 	.vfs_fhtovp = chfs_fhtovp,
    815 	.vfs_vptofh = chfs_vptofh,
    816 	.vfs_init = chfs_init,
    817 	.vfs_reinit = chfs_reinit,
    818 	.vfs_done = chfs_done,
    819 	.vfs_snapshot = chfs_snapshot,
    820 	.vfs_extattrctl = vfs_stdextattrctl,
    821 	.vfs_suspendctl = genfs_suspendctl,
    822 	.vfs_renamelock_enter = genfs_renamelock_enter,
    823 	.vfs_renamelock_exit = genfs_renamelock_exit,
    824 	.vfs_fsync = (void *)eopnotsupp,
    825 	.vfs_opv_descs = chfs_vnodeopv_descs
    826 };
    827 
    828 /* For using CHFS as a module. */
    829 static int
    830 chfs_modcmd(modcmd_t cmd, void *arg)
    831 {
    832 	switch (cmd) {
    833 	case MODULE_CMD_INIT:
    834 		return vfs_attach(&chfs_vfsops);
    835 	case MODULE_CMD_FINI:
    836 		return vfs_detach(&chfs_vfsops);
    837 	default:
    838 		return ENOTTY;
    839 	}
    840 }
    841