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