xref: /netbsd-src/sys/ufs/chfs/chfs_vfsops.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: chfs_vfsops.c,v 1.11 2014/04/16 18:55:19 maxv 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/malloc.h>
47 #include <sys/fcntl.h>
48 #include <sys/conf.h>
49 #include <sys/buf.h>
50 //XXX needed just for debugging
51 #include <sys/fstrans.h>
52 #include <sys/sleepq.h>
53 #include <sys/lockdebug.h>
54 #include <sys/ktrace.h>
55 
56 #include <uvm/uvm.h>
57 #include <uvm/uvm_pager.h>
58 #include <ufs/ufs/dir.h>
59 #include <ufs/ufs/ufs_extern.h>
60 #include <miscfs/genfs/genfs.h>
61 #include <miscfs/genfs/genfs_node.h>
62 #include <miscfs/specfs/specdev.h>
63 #include "chfs.h"
64 #include "chfs_args.h"
65 
66 MODULE(MODULE_CLASS_VFS, chfs, "flash");
67 
68 /* --------------------------------------------------------------------- */
69 /* functions */
70 
71 static int chfs_mount(struct mount *, const char *, void *, size_t *);
72 static int chfs_unmount(struct mount *, int);
73 static int chfs_root(struct mount *, struct vnode **);
74 static int chfs_vget(struct mount *, ino_t, struct vnode **);
75 static int chfs_fhtovp(struct mount *, struct fid *, struct vnode **);
76 static int chfs_vptofh(struct vnode *, struct fid *, size_t *);
77 static int chfs_start(struct mount *, int);
78 static int chfs_statvfs(struct mount *, struct statvfs *);
79 static int chfs_sync(struct mount *, int, kauth_cred_t);
80 static void chfs_init(void);
81 static void chfs_reinit(void);
82 static void chfs_done(void);
83 static int chfs_snapshot(struct mount *, struct vnode *,
84     struct timespec *);
85 
86 /* --------------------------------------------------------------------- */
87 /* structures */
88 
89 int
90 chfs_gop_alloc(struct vnode *vp, off_t off, off_t len,  int flags,
91     kauth_cred_t cred)
92 {
93 	return (0);
94 }
95 
96 const struct genfs_ops chfs_genfsops = {
97 	.gop_size = genfs_size,
98 	.gop_alloc = chfs_gop_alloc,
99 	.gop_write = genfs_gop_write,
100 	.gop_markupdate = ufs_gop_markupdate,
101 };
102 
103 struct pool chfs_inode_pool;
104 
105 /* for looking up the major for flash */
106 extern const struct cdevsw flash_cdevsw;
107 
108 /* --------------------------------------------------------------------- */
109 
110 static int
111 chfs_mount(struct mount *mp,
112     const char *path, void *data, size_t *data_len)
113 {
114 	struct lwp *l = curlwp;
115 	struct nameidata nd;
116 	struct pathbuf *pb;
117 	struct vnode *devvp = NULL;
118 	struct ufs_args *args = data;
119 	struct ufsmount *ump = NULL;
120 	struct chfs_mount *chmp;
121 	int err = 0;
122 	int xflags;
123 
124 	dbg("mount()\n");
125 
126 	if (args == NULL)
127 		return EINVAL;
128 	if (*data_len < sizeof *args)
129 		return EINVAL;
130 
131 	if (mp->mnt_flag & MNT_GETARGS) {
132 		ump = VFSTOUFS(mp);
133 		if (ump == NULL)
134 			return EIO;
135 		memset(args, 0, sizeof *args);
136 		args->fspec = NULL;
137 		*data_len = sizeof *args;
138 		return 0;
139 	}
140 
141 	if (mp->mnt_flag & MNT_UPDATE) {
142 		/* XXX: There is no support yet to update file system
143 		 * settings.  Should be added. */
144 
145 		return ENODEV;
146 	}
147 
148 	if (args->fspec != NULL) {
149 		err = pathbuf_copyin(args->fspec, &pb);
150 		if (err) {
151 			return err;
152 		}
153 		/* Look up the name and verify that it's sane. */
154 		NDINIT(&nd, LOOKUP, FOLLOW, pb);
155 		if ((err = namei(&nd)) != 0 )
156 			return (err);
157 		devvp = nd.ni_vp;
158 
159 		/* Be sure this is a valid block device */
160 		if (devvp->v_type != VBLK)
161 			err = ENOTBLK;
162 		else if (bdevsw_lookup(devvp->v_rdev) == NULL)
163 			err = ENXIO;
164 	}
165 
166 	if (err) {
167 		vrele(devvp);
168 		return (err);
169 	}
170 
171 	if (mp->mnt_flag & MNT_RDONLY)
172 		xflags = FREAD;
173 	else
174 		xflags = FREAD|FWRITE;
175 
176 	err = VOP_OPEN(devvp, xflags, FSCRED);
177 	if (err)
178 		goto fail;
179 
180 	/* call CHFS mount function */
181 	err = chfs_mountfs(devvp, mp);
182 	if (err) {
183 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
184 		(void)VOP_CLOSE(devvp, xflags, NOCRED);
185 		VOP_UNLOCK(devvp);
186 		goto fail;
187 	}
188 
189 	ump = VFSTOUFS(mp);
190 	chmp = ump->um_chfs;
191 
192 	vfs_getnewfsid(mp);
193 	chmp->chm_fsmp = mp;
194 
195 	return set_statvfs_info(path,
196 	    UIO_USERSPACE, args->fspec,
197 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
198 
199 fail:
200 	vrele(devvp);
201 	return (err);
202 }
203 
204 /* chfs_mountfs - init CHFS */
205 int
206 chfs_mountfs(struct vnode *devvp, struct mount *mp)
207 {
208 	struct lwp *l = curlwp;
209 	kauth_cred_t cred;
210 	devmajor_t flash_major;
211 	dev_t dev;
212 	struct ufsmount* ump = NULL;
213 	struct chfs_mount* chmp;
214 	struct vnode *vp;
215 	int err = 0;
216 
217 	dbg("mountfs()\n");
218 
219 	dev = devvp->v_rdev;
220 	cred = l ? l->l_cred : NOCRED;
221 
222 	/* Flush out any old buffers remaining from a previous use. */
223 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
224 	err = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
225 	VOP_UNLOCK(devvp);
226 	if (err)
227 		return (err);
228 
229 	/* Setup device. */
230 	flash_major = cdevsw_lookup_major(&flash_cdevsw);
231 
232 	if (devvp->v_type != VBLK)
233 		err = ENOTBLK;
234 	else if (bdevsw_lookup(dev) == NULL)
235 		err = ENXIO;
236 	else if (major(dev) != flash_major) {
237 		dbg("major(dev): %d, flash_major: %d\n",
238 		    major(dev), flash_major);
239 		err = ENODEV;
240 	}
241 	if (err) {
242 		vrele(devvp);
243 		return (err);
244 	}
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 fail;
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 contants. (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 		chfs_vnocache_hash_destroy(chmp->chm_vnocache_hash);
321 		ebh_close(chmp->chm_ebh);
322 		err = EIO;
323 		goto fail;
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 fail:
360 	kmem_free(chmp->chm_ebh, sizeof(struct chfs_ebh));
361 	kmem_free(chmp, sizeof(struct chfs_mount));
362 	kmem_free(ump, sizeof(struct ufsmount));
363 	return err;
364 }
365 
366 /* --------------------------------------------------------------------- */
367 
368 static int
369 chfs_unmount(struct mount *mp, int mntflags)
370 {
371 	int flags = 0, i = 0;
372 	struct ufsmount *ump;
373 	struct chfs_mount *chmp;
374 
375 	if (mntflags & MNT_FORCE)
376 		flags |= FORCECLOSE;
377 
378 	dbg("[START]\n");
379 
380 	ump = VFSTOUFS(mp);
381 	chmp = ump->um_chfs;
382 
383 	/* Stop GC. */
384 	chfs_gc_thread_stop(chmp);
385 
386 	/* Flush everyt buffer. */
387 	(void)vflush(mp, NULLVP, flags);
388 
389 	if (chmp->chm_wbuf_len) {
390 		mutex_enter(&chmp->chm_lock_mountfields);
391 		chfs_flush_pending_wbuf(chmp);
392 		mutex_exit(&chmp->chm_lock_mountfields);
393 	}
394 
395 	/* Free node references. */
396 	for (i = 0; i < chmp->chm_ebh->peb_nr; i++) {
397 		chfs_free_node_refs(&chmp->chm_blocks[i]);
398 	}
399 
400 	/* Destroy vnode cache hashtable. */
401 	chfs_vnocache_hash_destroy(chmp->chm_vnocache_hash);
402 
403 	/* Close eraseblock handler. */
404 	ebh_close(chmp->chm_ebh);
405 
406 	/* Destroy mutexes. */
407 	rw_destroy(&chmp->chm_lock_wbuf);
408 	mutex_destroy(&chmp->chm_lock_vnocache);
409 	mutex_destroy(&chmp->chm_lock_sizes);
410 	mutex_destroy(&chmp->chm_lock_mountfields);
411 
412 	/* Unmount UFS. */
413 	if (ump->um_devvp->v_type != VBAD) {
414 		spec_node_setmountedfs(ump->um_devvp, NULL);
415 	}
416 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
417 	(void)VOP_CLOSE(ump->um_devvp, FREAD|FWRITE, NOCRED);
418 	vput(ump->um_devvp);
419 
420 	mutex_destroy(&ump->um_lock);
421 
422 	/* Everything done. */
423 	kmem_free(ump, sizeof(struct ufsmount));
424 	mp->mnt_data = NULL;
425 	mp->mnt_flag &= ~MNT_LOCAL;
426 	dbg("[END]\n");
427 	return (0);
428 }
429 
430 /* --------------------------------------------------------------------- */
431 
432 static int
433 chfs_root(struct mount *mp, struct vnode **vpp)
434 {
435 	struct vnode *vp;
436 	int error;
437 
438 	if ((error = VFS_VGET(mp, (ino_t)UFS_ROOTINO, &vp)) != 0)
439 		return error;
440 	*vpp = vp;
441 	return 0;
442 }
443 
444 /* --------------------------------------------------------------------- */
445 
446 extern rb_tree_ops_t frag_rbtree_ops;
447 
448 static int
449 chfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
450 {
451 	struct chfs_mount *chmp;
452 	struct chfs_inode *ip;
453 	struct ufsmount *ump;
454 	struct vnode *vp;
455 	dev_t dev;
456 	int error;
457 	struct chfs_vnode_cache* chvc = NULL;
458 	struct chfs_node_ref* nref = NULL;
459 	struct buf *bp;
460 
461 	dbg("vget() | ino: %llu\n", (unsigned long long)ino);
462 
463 	ump = VFSTOUFS(mp);
464 	dev = ump->um_dev;
465 retry:
466 	if (!vpp) {
467 		vpp = kmem_alloc(sizeof(struct vnode*), KM_SLEEP);
468 	}
469 
470 	/* Get node from inode hash. */
471 	if ((*vpp = chfs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
472 		return 0;
473 	}
474 
475 	/* Allocate a new vnode/inode. */
476 	if ((error = getnewvnode(VT_CHFS,
477 		    mp, chfs_vnodeop_p, NULL, &vp)) != 0) {
478 		*vpp = NULL;
479 		return (error);
480 	}
481 	ip = pool_get(&chfs_inode_pool, PR_WAITOK);
482 
483 	mutex_enter(&chfs_hashlock);
484 	if ((*vpp = chfs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
485 		mutex_exit(&chfs_hashlock);
486 		ungetnewvnode(vp);
487 		pool_put(&chfs_inode_pool, ip);
488 		goto retry;
489 	}
490 
491 	vp->v_vflag |= VV_LOCKSWORK;
492 
493 	/* Initialize vnode/inode. */
494 	memset(ip, 0, sizeof(*ip));
495 	vp->v_data = ip;
496 	ip->vp = vp;
497 	ip->ch_type = VTTOCHT(vp->v_type);
498 	ip->ump = ump;
499 	ip->chmp = chmp = ump->um_chfs;
500 	ip->dev = dev;
501 	ip->ino = ino;
502 	vp->v_mount = mp;
503 	genfs_node_init(vp, &chfs_genfsops);
504 
505 	rb_tree_init(&ip->fragtree, &frag_rbtree_ops);
506 
507 	chfs_ihashins(ip);
508 	mutex_exit(&chfs_hashlock);
509 
510 	/* Set root inode. */
511 	if (ino == CHFS_ROOTINO) {
512 		dbg("SETROOT\n");
513 		vp->v_vflag |= VV_ROOT;
514 		vp->v_type = VDIR;
515 		ip->ch_type = CHT_DIR;
516 		ip->mode = IFMT | IEXEC | IWRITE | IREAD;
517 		ip->iflag |= (IN_ACCESS | IN_CHANGE | IN_UPDATE);
518 		chfs_update(vp, NULL, NULL, UPDATE_WAIT);
519 		TAILQ_INIT(&ip->dents);
520 		chfs_set_vnode_size(vp, 512);
521 	}
522 
523 	mutex_enter(&chmp->chm_lock_vnocache);
524 	chvc = chfs_vnode_cache_get(chmp, ino);
525 	mutex_exit(&chmp->chm_lock_vnocache);
526 	if (!chvc) {
527 		dbg("!chvc\n");
528 		/* Initialize the corresponding vnode cache. */
529 		/* XXX, we cant alloc under a lock, refactor this! */
530 		chvc = chfs_vnode_cache_alloc(ino);
531 		mutex_enter(&chmp->chm_lock_vnocache);
532 		if (ino == CHFS_ROOTINO) {
533 			chvc->nlink = 2;
534 			chvc->pvno = CHFS_ROOTINO;
535 			chvc->state = VNO_STATE_CHECKEDABSENT;
536 		}
537 		chfs_vnode_cache_add(chmp, chvc);
538 		mutex_exit(&chmp->chm_lock_vnocache);
539 
540 		ip->chvc = chvc;
541 		TAILQ_INIT(&ip->dents);
542 	} else {
543 		dbg("chvc\n");
544 		ip->chvc = chvc;
545 		/* We had a vnode cache, the node is already on flash, so read it */
546 		if (ino == CHFS_ROOTINO) {
547 			chvc->pvno = CHFS_ROOTINO;
548 			TAILQ_INIT(&chvc->scan_dirents);
549 		} else {
550 			chfs_readvnode(mp, ino, &vp);
551 		}
552 
553 		mutex_enter(&chmp->chm_lock_mountfields);
554 		/* Initialize type specific things. */
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 			if (error) {
574 				vput(vp);
575 				*vpp = NULL;
576 				mutex_exit(&chmp->chm_lock_mountfields);
577 				return (error);
578 			}
579 			break;
580 		case CHT_LNK:
581 			/* Collect data. */
582 			dbg("read_inode_internal | ino: %llu\n",
583 				(unsigned long long)ip->ino);
584 			error = chfs_read_inode_internal(chmp, ip);
585 			if (error) {
586 				vput(vp);
587 				*vpp = NULL;
588 				mutex_exit(&chmp->chm_lock_mountfields);
589 				return (error);
590 			}
591 
592 			/* Set link. */
593 			dbg("size: %llu\n", (unsigned long long)ip->size);
594 			bp = getiobuf(vp, true);
595 			bp->b_blkno = 0;
596 			bp->b_bufsize = bp->b_resid =
597 			    bp->b_bcount = ip->size;
598 			bp->b_data = kmem_alloc(ip->size, KM_SLEEP);
599 			chfs_read_data(chmp, vp, bp);
600 			if (!ip->target)
601 				ip->target = kmem_alloc(ip->size,
602 				    KM_SLEEP);
603 			memcpy(ip->target, bp->b_data, ip->size);
604 			kmem_free(bp->b_data, ip->size);
605 			putiobuf(bp);
606 
607 			break;
608 		case CHT_CHR:
609 			/* FALLTHROUGH */
610 		case CHT_BLK:
611 			/* FALLTHROUGH */
612 		case CHT_FIFO:
613 			/* Collect data. */
614 			dbg("read_inode_internal | ino: %llu\n",
615 				(unsigned long long)ip->ino);
616 			error = chfs_read_inode_internal(chmp, ip);
617 			if (error) {
618 				vput(vp);
619 				*vpp = NULL;
620 				mutex_exit(&chmp->chm_lock_mountfields);
621 				return (error);
622 			}
623 
624 			/* Set device. */
625 			bp = getiobuf(vp, true);
626 			bp->b_blkno = 0;
627 			bp->b_bufsize = bp->b_resid =
628 			    bp->b_bcount = sizeof(dev_t);
629 			bp->b_data = kmem_alloc(sizeof(dev_t), KM_SLEEP);
630 			chfs_read_data(chmp, vp, bp);
631 			memcpy(&ip->rdev,
632 			    bp->b_data, sizeof(dev_t));
633 			kmem_free(bp->b_data, sizeof(dev_t));
634 			putiobuf(bp);
635 			/* Set specific operations. */
636 			if (ip->ch_type == CHT_FIFO) {
637 				vp->v_op = chfs_fifoop_p;
638 			} else {
639 				vp->v_op = chfs_specop_p;
640 				spec_node_init(vp, ip->rdev);
641 			}
642 
643 		    break;
644 		case CHT_BLANK:
645 			/* FALLTHROUGH */
646 		case CHT_BAD:
647 			break;
648 		}
649 		mutex_exit(&chmp->chm_lock_mountfields);
650 
651 	}
652 
653 	/* Finish inode initalization. */
654 	ip->devvp = ump->um_devvp;
655 	vref(ip->devvp);
656 
657 	uvm_vnp_setsize(vp, ip->size);
658 	*vpp = vp;
659 
660 	return 0;
661 }
662 
663 /* --------------------------------------------------------------------- */
664 
665 static int
666 chfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
667 {
668 	return ENODEV;
669 }
670 
671 /* --------------------------------------------------------------------- */
672 
673 static int
674 chfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
675 {
676 	return ENODEV;
677 }
678 
679 /* --------------------------------------------------------------------- */
680 
681 static int
682 chfs_start(struct mount *mp, int flags)
683 {
684 	return 0;
685 }
686 
687 /* --------------------------------------------------------------------- */
688 
689 static int
690 chfs_statvfs(struct mount *mp, struct statvfs *sbp)
691 {
692  	struct chfs_mount *chmp;
693 	struct ufsmount *ump;
694 	dbg("statvfs\n");
695 
696 	ump = VFSTOUFS(mp);
697 	chmp = ump->um_chfs;
698 
699 	sbp->f_flag   = mp->mnt_flag;
700 	sbp->f_bsize  = chmp->chm_ebh->eb_size;
701 	sbp->f_frsize = chmp->chm_ebh->eb_size;
702 	sbp->f_iosize = chmp->chm_ebh->eb_size;
703 
704 	sbp->f_blocks = chmp->chm_ebh->peb_nr;
705 	sbp->f_files  = 0;
706 	sbp->f_bavail = chmp->chm_nr_free_blocks - chmp->chm_resv_blocks_write;
707 
708 	sbp->f_bfree = chmp->chm_nr_free_blocks;
709 	sbp->f_bresvd = chmp->chm_resv_blocks_write;
710 
711 	/* FFS specific */
712 	sbp->f_ffree  = 0;
713 	sbp->f_favail = 0;
714 	sbp->f_fresvd = 0;
715 
716 	copy_statvfs_info(sbp, mp);
717 
718 	return 0;
719 }
720 
721 /* --------------------------------------------------------------------- */
722 
723 static int
724 chfs_sync(struct mount *mp, int waitfor,
725     kauth_cred_t uc)
726 {
727 	return 0;
728 }
729 
730 /* --------------------------------------------------------------------- */
731 
732 static void
733 chfs_init(void)
734 {
735 	/* Initialize pools and inode hash. */
736 	chfs_alloc_pool_caches();
737 	chfs_ihashinit();
738 	pool_init(&chfs_inode_pool, sizeof(struct chfs_inode), 0, 0, 0,
739 	    "chfsinopl", &pool_allocator_nointr, IPL_NONE);
740 	ufs_init();
741 }
742 
743 /* --------------------------------------------------------------------- */
744 
745 static void
746 chfs_reinit(void)
747 {
748 	chfs_ihashreinit();
749 	ufs_reinit();
750 }
751 
752 /* --------------------------------------------------------------------- */
753 
754 static void
755 chfs_done(void)
756 {
757 	ufs_done();
758 	chfs_ihashdone();
759 	pool_destroy(&chfs_inode_pool);
760 	chfs_destroy_pool_caches();
761 }
762 
763 /* --------------------------------------------------------------------- */
764 
765 static int
766 chfs_snapshot(struct mount *mp, struct vnode *vp,
767     struct timespec *ctime)
768 {
769 	return ENODEV;
770 }
771 
772 /* --------------------------------------------------------------------- */
773 
774 /*
775  * chfs vfs operations.
776  */
777 
778 extern const struct vnodeopv_desc chfs_fifoop_opv_desc;
779 extern const struct vnodeopv_desc chfs_specop_opv_desc;
780 extern const struct vnodeopv_desc chfs_vnodeop_opv_desc;
781 
782 const struct vnodeopv_desc * const chfs_vnodeopv_descs[] = {
783 	&chfs_fifoop_opv_desc,
784 	&chfs_specop_opv_desc,
785 	&chfs_vnodeop_opv_desc,
786 	NULL,
787 };
788 
789 struct vfsops chfs_vfsops = {
790 	.vfs_name = MOUNT_CHFS,
791 	.vfs_min_mount_data = sizeof (struct chfs_args),
792 	.vfs_mount = chfs_mount,
793 	.vfs_start = chfs_start,
794 	.vfs_unmount = chfs_unmount,
795 	.vfs_root = chfs_root,
796 	.vfs_quotactl = ufs_quotactl,
797 	.vfs_statvfs = chfs_statvfs,
798 	.vfs_sync = chfs_sync,
799 	.vfs_vget = chfs_vget,
800 	.vfs_fhtovp = chfs_fhtovp,
801 	.vfs_vptofh = chfs_vptofh,
802 	.vfs_init = chfs_init,
803 	.vfs_reinit = chfs_reinit,
804 	.vfs_done = chfs_done,
805 	.vfs_snapshot = chfs_snapshot,
806 	.vfs_extattrctl = vfs_stdextattrctl,
807 	.vfs_suspendctl = (void *)eopnotsupp,
808 	.vfs_renamelock_enter = genfs_renamelock_enter,
809 	.vfs_renamelock_exit = genfs_renamelock_exit,
810 	.vfs_fsync = (void *)eopnotsupp,
811 	.vfs_opv_descs = chfs_vnodeopv_descs
812 };
813 
814 /* For using CHFS as a module. */
815 static int
816 chfs_modcmd(modcmd_t cmd, void *arg)
817 {
818 	switch (cmd) {
819 	case MODULE_CMD_INIT:
820 		return vfs_attach(&chfs_vfsops);
821 	case MODULE_CMD_FINI:
822 		return vfs_detach(&chfs_vfsops);
823 	default:
824 		return ENOTTY;
825 	}
826 }
827