Home | History | Annotate | Line # | Download | only in chfs
      1 /*	$NetBSD: chfs_vfsops.c,v 1.24 2025/05/28 06:06:54 andvar 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_extern.h>
     56 
     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 /* --------------------------------------------------------------------- */
     66 /* functions */
     67 
     68 static int chfs_mount(struct mount *, const char *, void *, size_t *);
     69 static int chfs_unmount(struct mount *, int);
     70 static int chfs_root(struct mount *, int, struct vnode **);
     71 static int chfs_loadvnode(struct mount *, struct vnode *,
     72     const void *, size_t, const void **);
     73 static int chfs_vget(struct mount *, ino_t, int, struct vnode **);
     74 static int chfs_fhtovp(struct mount *, struct fid *, int, 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 	.gop_putrange = genfs_gop_putrange,
    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 		err = namei(&nd);
    156 		pathbuf_destroy(pb);
    157 		if (err)
    158 			return err;
    159 		devvp = nd.ni_vp;
    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 		goto fail0;
    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 		goto fail0;
    245 
    246 	/* Connect CHFS to UFS. */
    247 	ump = kmem_zalloc(sizeof(struct ufsmount), KM_SLEEP);
    248 
    249 	ump->um_fstype = UFS1;
    250 	ump->um_chfs = kmem_zalloc(sizeof(struct chfs_mount), KM_SLEEP);
    251 	mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
    252 
    253 	chmp = ump->um_chfs;
    254 
    255 	/* Initialize erase block handler. */
    256 	chmp->chm_ebh = kmem_alloc(sizeof(struct chfs_ebh), KM_SLEEP);
    257 
    258 	dbg("[]opening flash: %u\n", (unsigned int)devvp->v_rdev);
    259 	err = ebh_open(chmp->chm_ebh, devvp->v_rdev);
    260 	if (err) {
    261 		dbg("error while opening flash\n");
    262 		goto fail1;
    263 	}
    264 
    265 	//TODO check flash sizes
    266 
    267 	/* Initialize vnode cache's hashtable and eraseblock array. */
    268 	chmp->chm_gbl_version = 0;
    269 	chmp->chm_vnocache_hash = chfs_vnocache_hash_init();
    270 
    271 	chmp->chm_blocks = kmem_zalloc(chmp->chm_ebh->peb_nr *
    272 	    sizeof(struct chfs_eraseblock), KM_SLEEP);
    273 
    274 	/* Initialize mutexes. */
    275 	mutex_init(&chmp->chm_lock_mountfields, MUTEX_DEFAULT, IPL_NONE);
    276 	mutex_init(&chmp->chm_lock_sizes, MUTEX_DEFAULT, IPL_NONE);
    277 	mutex_init(&chmp->chm_lock_vnocache, MUTEX_DEFAULT, IPL_NONE);
    278 
    279 	/* Initialize read/write constants. (from UFS) */
    280 	chmp->chm_fs_bmask = -4096;
    281 	chmp->chm_fs_bsize = 4096;
    282 	chmp->chm_fs_qbmask = 4095;
    283 	chmp->chm_fs_bshift = 12;
    284 	chmp->chm_fs_fmask = -2048;
    285 	chmp->chm_fs_qfmask = 2047;
    286 
    287 	/* Initialize writebuffer. */
    288 	chmp->chm_wbuf_pagesize = chmp->chm_ebh->flash_if->page_size;
    289 	dbg("wbuf size: %zu\n", chmp->chm_wbuf_pagesize);
    290 	chmp->chm_wbuf = kmem_alloc(chmp->chm_wbuf_pagesize, KM_SLEEP);
    291 	rw_init(&chmp->chm_lock_wbuf);
    292 
    293 	/* Initialize queues. */
    294 	TAILQ_INIT(&chmp->chm_free_queue);
    295 	TAILQ_INIT(&chmp->chm_clean_queue);
    296 	TAILQ_INIT(&chmp->chm_dirty_queue);
    297 	TAILQ_INIT(&chmp->chm_very_dirty_queue);
    298 	TAILQ_INIT(&chmp->chm_erasable_pending_wbuf_queue);
    299 	TAILQ_INIT(&chmp->chm_erase_pending_queue);
    300 
    301 	/* Initialize flash-specific constants. */
    302 	chfs_calc_trigger_levels(chmp);
    303 
    304 	/* Initialize sizes. */
    305 	chmp->chm_nr_free_blocks = 0;
    306 	chmp->chm_nr_erasable_blocks = 0;
    307 	chmp->chm_max_vno = 2;
    308 	chmp->chm_checked_vno = 2;
    309 	chmp->chm_unchecked_size = 0;
    310 	chmp->chm_used_size = 0;
    311 	chmp->chm_dirty_size = 0;
    312 	chmp->chm_wasted_size = 0;
    313 	chmp->chm_free_size = chmp->chm_ebh->eb_size * chmp->chm_ebh->peb_nr;
    314 
    315 	/* Build filesystem. */
    316 	err = chfs_build_filesystem(chmp);
    317 
    318 	if (err) {
    319 		/* Armageddon and return. */
    320 		err = EIO;
    321 		goto fail2;
    322 	}
    323 
    324 	/* Initialize UFS. */
    325 	mp->mnt_data = ump;
    326 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    327 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CHFS);
    328 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    329 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    330 	mp->mnt_flag |= MNT_LOCAL;
    331 	mp->mnt_fs_bshift = PAGE_SHIFT;
    332 	mp->mnt_dev_bshift = DEV_BSHIFT;
    333 	mp->mnt_iflag |= IMNT_MPSAFE;
    334 	ump->um_flags = 0;
    335 	ump->um_mountp = mp;
    336 	ump->um_dev = dev;
    337 	ump->um_devvp = devvp;
    338 	ump->um_maxfilesize = 1048512 * 1024;
    339 
    340 	/* Allocate the root vnode. */
    341 	err = VFS_VGET(mp, CHFS_ROOTINO, LK_EXCLUSIVE, &vp);
    342 	if (err) {
    343 		dbg("error: %d while allocating root node\n", err);
    344 		return err;
    345 	}
    346 	vput(vp);
    347 
    348 	/* Start GC. */
    349 	chfs_gc_thread_start(chmp);
    350 	mutex_enter(&chmp->chm_lock_mountfields);
    351 	chfs_gc_trigger(chmp);
    352 	mutex_exit(&chmp->chm_lock_mountfields);
    353 
    354 	spec_node_setmountedfs(devvp, mp);
    355 	return 0;
    356 
    357 fail2:
    358 	KASSERT(TAILQ_EMPTY(&chmp->chm_erase_pending_queue));
    359 	KASSERT(TAILQ_EMPTY(&chmp->chm_erasable_pending_wbuf_queue));
    360 	KASSERT(TAILQ_EMPTY(&chmp->chm_very_dirty_queue));
    361 	KASSERT(TAILQ_EMPTY(&chmp->chm_dirty_queue));
    362 	KASSERT(TAILQ_EMPTY(&chmp->chm_clean_queue));
    363 	KASSERT(TAILQ_EMPTY(&chmp->chm_free_queue));
    364 	rw_destroy(&chmp->chm_lock_wbuf);
    365 	kmem_free(chmp->chm_wbuf, chmp->chm_wbuf_pagesize);
    366 	mutex_destroy(&chmp->chm_lock_vnocache);
    367 	mutex_destroy(&chmp->chm_lock_sizes);
    368 	mutex_destroy(&chmp->chm_lock_mountfields);
    369 	kmem_free(chmp->chm_blocks, chmp->chm_ebh->peb_nr *
    370 	    sizeof(struct chfs_eraseblock));
    371 	chfs_vnocache_hash_destroy(chmp->chm_vnocache_hash);
    372 	ebh_close(chmp->chm_ebh);
    373 
    374 fail1:
    375 	kmem_free(chmp->chm_ebh, sizeof(struct chfs_ebh));
    376 	mutex_destroy(&ump->um_lock);
    377 	kmem_free(chmp, sizeof(struct chfs_mount));
    378 	kmem_free(ump, sizeof(struct ufsmount));
    379 
    380 fail0:
    381 	KASSERT(err);
    382 	return err;
    383 }
    384 
    385 /* --------------------------------------------------------------------- */
    386 
    387 static int
    388 chfs_unmount(struct mount *mp, int mntflags)
    389 {
    390 	int flags = 0, i = 0;
    391 	struct ufsmount *ump;
    392 	struct chfs_mount *chmp;
    393 
    394 	if (mntflags & MNT_FORCE)
    395 		flags |= FORCECLOSE;
    396 
    397 	dbg("[START]\n");
    398 
    399 	ump = VFSTOUFS(mp);
    400 	chmp = ump->um_chfs;
    401 
    402 	/* Stop GC. */
    403 	chfs_gc_thread_stop(chmp);
    404 
    405 	/* Flush everyt buffer. */
    406 	(void)vflush(mp, NULLVP, flags);
    407 
    408 	if (chmp->chm_wbuf_len) {
    409 		mutex_enter(&chmp->chm_lock_mountfields);
    410 		chfs_flush_pending_wbuf(chmp);
    411 		mutex_exit(&chmp->chm_lock_mountfields);
    412 	}
    413 
    414 	/* Free node references. */
    415 	for (i = 0; i < chmp->chm_ebh->peb_nr; i++) {
    416 		chfs_free_node_refs(&chmp->chm_blocks[i]);
    417 	}
    418 
    419 	/* Destroy vnode cache hashtable. */
    420 	chfs_vnocache_hash_destroy(chmp->chm_vnocache_hash);
    421 
    422 	/* Close eraseblock handler. */
    423 	ebh_close(chmp->chm_ebh);
    424 
    425 	/* Destroy mutexes. */
    426 	rw_destroy(&chmp->chm_lock_wbuf);
    427 	mutex_destroy(&chmp->chm_lock_vnocache);
    428 	mutex_destroy(&chmp->chm_lock_sizes);
    429 	mutex_destroy(&chmp->chm_lock_mountfields);
    430 
    431 	/* Unmount UFS. */
    432 	if (ump->um_devvp->v_type != VBAD) {
    433 		spec_node_setmountedfs(ump->um_devvp, 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 	/* Everything done. */
    442 	kmem_free(ump, sizeof(struct ufsmount));
    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, int lktype, struct vnode **vpp)
    453 {
    454 	struct vnode *vp;
    455 	int error;
    456 
    457 	if ((error = VFS_VGET(mp, (ino_t)UFS_ROOTINO, lktype, &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_loadvnode(struct mount *mp, struct vnode *vp,
    469     const void *key, size_t key_len, const void **new_key)
    470 {
    471 	struct chfs_mount *chmp;
    472 	struct chfs_inode *ip;
    473 	struct ufsmount *ump;
    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 	ino_t ino;
    480 
    481 	KASSERT(key_len == sizeof(ino));
    482 	memcpy(&ino, key, key_len);
    483 
    484 	dbg("vget() | ino: %llu\n", (unsigned long long)ino);
    485 
    486 	ump = VFSTOUFS(mp);
    487 	dev = ump->um_dev;
    488 
    489 	ip = pool_get(&chfs_inode_pool, PR_WAITOK);
    490 
    491 	/* Initialize vnode/inode. */
    492 	memset(ip, 0, sizeof(*ip));
    493 	ip->vp = vp;
    494 	ip->ump = ump;
    495 	ip->chmp = chmp = ump->um_chfs;
    496 	ip->dev = dev;
    497 	ip->ino = ino;
    498 
    499 	rb_tree_init(&ip->fragtree, &frag_rbtree_ops);
    500 
    501 	vp->v_tag = VT_CHFS;
    502 	vp->v_op = chfs_vnodeop_p;
    503 	if (ino == CHFS_ROOTINO)
    504 		vp->v_vflag |= VV_ROOT;
    505 	vp->v_data = ip;
    506 
    507 	/* Set root inode. */
    508 	if (ino == CHFS_ROOTINO) {
    509 		dbg("SETROOT\n");
    510 		vp->v_type = VDIR;
    511 		ip->ch_type = CHT_DIR;
    512 		ip->mode = IFMT | IEXEC | IWRITE | IREAD;
    513 		ip->iflag |= (IN_ACCESS | IN_CHANGE | IN_UPDATE);
    514 		chfs_update(vp, NULL, NULL, UPDATE_WAIT);
    515 		TAILQ_INIT(&ip->dents);
    516 		chfs_set_vnode_size(vp, 512);
    517 	}
    518 
    519 	mutex_enter(&chmp->chm_lock_vnocache);
    520 	chvc = chfs_vnode_cache_get(chmp, ino);
    521 	mutex_exit(&chmp->chm_lock_vnocache);
    522 	if (!chvc) {
    523 		dbg("!chvc\n");
    524 		/* Initialize the corresponding vnode cache. */
    525 		/* XXX, we cant alloc under a lock, refactor this! */
    526 		chvc = chfs_vnode_cache_alloc(ino);
    527 		mutex_enter(&chmp->chm_lock_vnocache);
    528 		if (ino == CHFS_ROOTINO) {
    529 			chvc->nlink = 2;
    530 			chvc->pvno = CHFS_ROOTINO;
    531 			chvc->state = VNO_STATE_CHECKEDABSENT;
    532 		}
    533 		chfs_vnode_cache_add(chmp, chvc);
    534 		mutex_exit(&chmp->chm_lock_vnocache);
    535 
    536 		ip->chvc = chvc;
    537 		TAILQ_INIT(&ip->dents);
    538 	} else {
    539 		dbg("chvc\n");
    540 		ip->chvc = chvc;
    541 		/* We had a vnode cache, the node is already on flash, so read it */
    542 		if (ino == CHFS_ROOTINO) {
    543 			chvc->pvno = CHFS_ROOTINO;
    544 			TAILQ_INIT(&chvc->scan_dirents);
    545 		} else {
    546 			chfs_readvnode(mp, ino, &vp);
    547 		}
    548 
    549 		mutex_enter(&chmp->chm_lock_mountfields);
    550 		/* Initialize type specific things. */
    551 		error = 0;
    552 		switch (ip->ch_type) {
    553 		case CHT_DIR:
    554 			/* Read every dirent. */
    555 			nref = chvc->dirents;
    556 			while (nref &&
    557 			    (struct chfs_vnode_cache *)nref != chvc) {
    558 				chfs_readdirent(mp, nref, ip);
    559 				nref = nref->nref_next;
    560 			}
    561 			chfs_set_vnode_size(vp, 512);
    562 			break;
    563 		case CHT_REG:
    564 			/* FALLTHROUGH */
    565 		case CHT_SOCK:
    566 			/* Collect data. */
    567 			dbg("read_inode_internal | ino: %llu\n",
    568 				(unsigned long long)ip->ino);
    569 			error = chfs_read_inode(chmp, ip);
    570 			break;
    571 		case CHT_LNK:
    572 			/* Collect data. */
    573 			dbg("read_inode_internal | ino: %llu\n",
    574 				(unsigned long long)ip->ino);
    575 			error = chfs_read_inode_internal(chmp, ip);
    576 			if (error)
    577 				break;
    578 
    579 			/* Set link. */
    580 			dbg("size: %llu\n", (unsigned long long)ip->size);
    581 			bp = getiobuf(vp, true);
    582 			bp->b_blkno = 0;
    583 			bp->b_bufsize = bp->b_resid =
    584 			    bp->b_bcount = ip->size;
    585 			bp->b_data = kmem_alloc(ip->size, KM_SLEEP);
    586 			chfs_read_data(chmp, vp, bp);
    587 			if (!ip->target)
    588 				ip->target = kmem_alloc(ip->size,
    589 				    KM_SLEEP);
    590 			memcpy(ip->target, bp->b_data, ip->size);
    591 			kmem_free(bp->b_data, ip->size);
    592 			putiobuf(bp);
    593 
    594 			break;
    595 		case CHT_CHR:
    596 			/* FALLTHROUGH */
    597 		case CHT_BLK:
    598 			/* FALLTHROUGH */
    599 		case CHT_FIFO:
    600 			/* Collect data. */
    601 			dbg("read_inode_internal | ino: %llu\n",
    602 				(unsigned long long)ip->ino);
    603 			error = chfs_read_inode_internal(chmp, ip);
    604 			if (error)
    605 				break;
    606 
    607 			/* Set device. */
    608 			bp = getiobuf(vp, true);
    609 			bp->b_blkno = 0;
    610 			bp->b_bufsize = bp->b_resid =
    611 			    bp->b_bcount = sizeof(dev_t);
    612 			bp->b_data = kmem_alloc(sizeof(dev_t), KM_SLEEP);
    613 			chfs_read_data(chmp, vp, bp);
    614 			memcpy(&ip->rdev,
    615 			    bp->b_data, sizeof(dev_t));
    616 			kmem_free(bp->b_data, sizeof(dev_t));
    617 			putiobuf(bp);
    618 			/* Set specific operations. */
    619 			if (ip->ch_type == CHT_FIFO) {
    620 				vp->v_op = chfs_fifoop_p;
    621 			} else {
    622 				vp->v_op = chfs_specop_p;
    623 				spec_node_init(vp, ip->rdev);
    624 			}
    625 
    626 		    break;
    627 		case CHT_BLANK:
    628 			/* FALLTHROUGH */
    629 		case CHT_BAD:
    630 			break;
    631 		}
    632 		mutex_exit(&chmp->chm_lock_mountfields);
    633 		if (error) {
    634 			vp->v_data = NULL;
    635 			KASSERT(TAILQ_FIRST(&ip->dents) == NULL);
    636 			pool_put(&chfs_inode_pool, ip);
    637 			return error;
    638 		}
    639 
    640 	}
    641 
    642 	/* Finish inode initialization. */
    643 	ip->ch_type = VTTOCHT(vp->v_type);
    644 	ip->devvp = ump->um_devvp;
    645 	vref(ip->devvp);
    646 
    647 	genfs_node_init(vp, &chfs_genfsops);
    648 	uvm_vnp_setsize(vp, ip->size);
    649 
    650 	*new_key = &ip->ino;
    651 
    652 	return 0;
    653 }
    654 
    655 /* --------------------------------------------------------------------- */
    656 
    657 static int
    658 chfs_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
    659 {
    660 	int error;
    661 
    662 	error = vcache_get(mp, &ino, sizeof(ino), vpp);
    663 	if (error)
    664 		return error;
    665 
    666 	error = vn_lock(*vpp, lktype);
    667 	if (error) {
    668 		vrele(*vpp);
    669 		*vpp = NULL;
    670 		return error;
    671 	}
    672 
    673 	return 0;
    674 }
    675 
    676 /* --------------------------------------------------------------------- */
    677 
    678 
    679 static int
    680 chfs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
    681 {
    682 	return ENODEV;
    683 }
    684 
    685 /* --------------------------------------------------------------------- */
    686 
    687 static int
    688 chfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    689 {
    690 	return ENODEV;
    691 }
    692 
    693 /* --------------------------------------------------------------------- */
    694 
    695 static int
    696 chfs_start(struct mount *mp, int flags)
    697 {
    698 	return 0;
    699 }
    700 
    701 /* --------------------------------------------------------------------- */
    702 
    703 static int
    704 chfs_statvfs(struct mount *mp, struct statvfs *sbp)
    705 {
    706  	struct chfs_mount *chmp;
    707 	struct ufsmount *ump;
    708 	dbg("statvfs\n");
    709 
    710 	ump = VFSTOUFS(mp);
    711 	chmp = ump->um_chfs;
    712 
    713 	sbp->f_flag   = mp->mnt_flag;
    714 	sbp->f_bsize  = chmp->chm_ebh->eb_size;
    715 	sbp->f_frsize = chmp->chm_ebh->eb_size;
    716 	sbp->f_iosize = chmp->chm_ebh->eb_size;
    717 
    718 	sbp->f_blocks = chmp->chm_ebh->peb_nr;
    719 	sbp->f_files  = 0;
    720 	sbp->f_bavail = chmp->chm_nr_free_blocks - chmp->chm_resv_blocks_write;
    721 
    722 	sbp->f_bfree = chmp->chm_nr_free_blocks;
    723 	sbp->f_bresvd = chmp->chm_resv_blocks_write;
    724 
    725 	/* FFS specific */
    726 	sbp->f_ffree  = 0;
    727 	sbp->f_favail = 0;
    728 	sbp->f_fresvd = 0;
    729 
    730 	copy_statvfs_info(sbp, mp);
    731 
    732 	return 0;
    733 }
    734 
    735 /* --------------------------------------------------------------------- */
    736 
    737 static int
    738 chfs_sync(struct mount *mp, int waitfor,
    739     kauth_cred_t uc)
    740 {
    741 	return 0;
    742 }
    743 
    744 /* --------------------------------------------------------------------- */
    745 
    746 static void
    747 chfs_init(void)
    748 {
    749 	/* Initialize pools and inode hash. */
    750 	chfs_alloc_pool_caches();
    751 	pool_init(&chfs_inode_pool, sizeof(struct chfs_inode), 0, 0, 0,
    752 	    "chfsinopl", &pool_allocator_nointr, IPL_NONE);
    753 	ufs_init();
    754 }
    755 
    756 /* --------------------------------------------------------------------- */
    757 
    758 static void
    759 chfs_reinit(void)
    760 {
    761 	ufs_reinit();
    762 }
    763 
    764 /* --------------------------------------------------------------------- */
    765 
    766 static void
    767 chfs_done(void)
    768 {
    769 	ufs_done();
    770 	pool_destroy(&chfs_inode_pool);
    771 	chfs_destroy_pool_caches();
    772 }
    773 
    774 /* --------------------------------------------------------------------- */
    775 
    776 static int
    777 chfs_snapshot(struct mount *mp, struct vnode *vp,
    778     struct timespec *ctime)
    779 {
    780 	return ENODEV;
    781 }
    782 
    783 /* --------------------------------------------------------------------- */
    784 
    785 /*
    786  * chfs vfs operations.
    787  */
    788 
    789 extern const struct vnodeopv_desc chfs_fifoop_opv_desc;
    790 extern const struct vnodeopv_desc chfs_specop_opv_desc;
    791 extern const struct vnodeopv_desc chfs_vnodeop_opv_desc;
    792 
    793 const struct vnodeopv_desc * const chfs_vnodeopv_descs[] = {
    794 	&chfs_fifoop_opv_desc,
    795 	&chfs_specop_opv_desc,
    796 	&chfs_vnodeop_opv_desc,
    797 	NULL,
    798 };
    799 
    800 struct vfsops chfs_vfsops = {
    801 	.vfs_name = MOUNT_CHFS,
    802 	.vfs_min_mount_data = sizeof (struct chfs_args),
    803 	.vfs_mount = chfs_mount,
    804 	.vfs_start = chfs_start,
    805 	.vfs_unmount = chfs_unmount,
    806 	.vfs_root = chfs_root,
    807 	.vfs_quotactl = ufs_quotactl,
    808 	.vfs_statvfs = chfs_statvfs,
    809 	.vfs_sync = chfs_sync,
    810 	.vfs_vget = chfs_vget,
    811 	.vfs_loadvnode = chfs_loadvnode,
    812 	.vfs_fhtovp = chfs_fhtovp,
    813 	.vfs_vptofh = chfs_vptofh,
    814 	.vfs_init = chfs_init,
    815 	.vfs_reinit = chfs_reinit,
    816 	.vfs_done = chfs_done,
    817 	.vfs_snapshot = chfs_snapshot,
    818 	.vfs_extattrctl = vfs_stdextattrctl,
    819 	.vfs_suspendctl = genfs_suspendctl,
    820 	.vfs_renamelock_enter = genfs_renamelock_enter,
    821 	.vfs_renamelock_exit = genfs_renamelock_exit,
    822 	.vfs_fsync = (void *)eopnotsupp,
    823 	.vfs_opv_descs = chfs_vnodeopv_descs
    824 };
    825 
    826 /* For using CHFS as a module. */
    827 
    828 MODULE(MODULE_CLASS_VFS, chfs, "ufs,flash");
    829 
    830 static int
    831 chfs_modcmd(modcmd_t cmd, void *arg)
    832 {
    833 	switch (cmd) {
    834 	case MODULE_CMD_INIT:
    835 		return vfs_attach(&chfs_vfsops);
    836 	case MODULE_CMD_FINI:
    837 		return vfs_detach(&chfs_vfsops);
    838 	default:
    839 		return ENOTTY;
    840 	}
    841 }
    842