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