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