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