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