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