2 * Copyright (c) 2011, 2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS 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 OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/nlookup.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
40 #include <sys/fcntl.h>
43 #include <sys/vfsops.h>
44 #include <sys/sysctl.h>
47 #include "hammer2_disk.h"
48 #include "hammer2_mount.h"
50 struct hammer2_sync_info {
57 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
59 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
60 &hammer2_debug, 0, "");
62 static int hammer2_vfs_init(struct vfsconf *conf);
63 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
65 static int hammer2_remount(struct mount *, char *, struct vnode *,
67 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
68 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
69 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
71 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
73 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
74 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
75 ino_t ino, struct vnode **vpp);
76 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
77 struct fid *fhp, struct vnode **vpp);
78 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
79 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
80 int *exflagsp, struct ucred **credanonp);
82 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
83 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
84 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
87 * HAMMER2 vfs operations.
89 static struct vfsops hammer2_vfsops = {
90 .vfs_init = hammer2_vfs_init,
91 .vfs_sync = hammer2_vfs_sync,
92 .vfs_mount = hammer2_vfs_mount,
93 .vfs_unmount = hammer2_vfs_unmount,
94 .vfs_root = hammer2_vfs_root,
95 .vfs_statfs = hammer2_vfs_statfs,
96 .vfs_statvfs = hammer2_vfs_statvfs,
97 .vfs_vget = hammer2_vfs_vget,
98 .vfs_vptofh = hammer2_vfs_vptofh,
99 .vfs_fhtovp = hammer2_vfs_fhtovp,
100 .vfs_checkexp = hammer2_vfs_checkexp
103 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
105 VFS_SET(hammer2_vfsops, hammer2, 0);
106 MODULE_VERSION(hammer2, 1);
110 hammer2_vfs_init(struct vfsconf *conf)
116 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
118 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
120 if (HAMMER2_ALLOCREF_BYTES != sizeof(struct hammer2_allocref))
122 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
126 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
132 * Mount or remount HAMMER2 fileystem from physical media
135 * mp mount point structure
141 * mp mount point structure
142 * path path to mount point
143 * data pointer to argument structure in user space
144 * volume volume path (device@LABEL form)
145 * hflags user mount flags
146 * cred user credentials
153 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
156 struct hammer2_mount_info info;
157 hammer2_mount_t *hmp;
160 struct nlookupdata nd;
161 hammer2_chain_t *parent;
162 hammer2_chain_t *schain;
163 hammer2_chain_t *rchain;
164 char devstr[MNAMELEN];
177 kprintf("hammer2_mount\n");
186 * Non-root mount or updating a mount
188 error = copyin(data, &info, sizeof(info));
192 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
196 /* Extract device and label */
198 label = strchr(devstr, '@');
200 ((label + 1) - dev) > done) {
208 if (mp->mnt_flag & MNT_UPDATE) {
210 /* HAMMER2 implements NFS export via mountctl */
213 error = hammer2_remount(mp, path, devvp, cred);
221 /* Lookup name and verify it refers to a block device */
222 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
224 error = nlookup(&nd);
226 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
230 if (vn_isdisk(devvp, &error))
231 error = vfs_mountedon(devvp);
233 if (error == 0 && vcount(devvp) > 0)
237 * Now open the device
240 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
241 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
242 error = vinvalbuf(devvp, V_SAVE, 0, 0);
244 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD | FWRITE,
249 if (error && devvp) {
257 * Block device opened successfully, finish initializing the
260 * From this point on we have to call hammer2_unmount() on failure.
262 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
263 mp->mnt_data = (qaddr_t)hmp;
267 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
268 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
270 mp->mnt_flag = MNT_LOCAL;
271 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
274 * vchain setup. vchain.data is special cased to NULL. vchain.refs
275 * is initialized and will never drop to 0.
277 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
278 hmp->vchain.refs = 1;
279 hmp->vchain.data = (void *)&hmp->voldata;
280 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
281 /* hmp->vchain.u.xxx is left NULL */
282 lockinit(&hmp->vchain.lk, "volume", 0, LK_CANRECURSE);
285 * Install the volume header
287 error = hammer2_install_volume_header(hmp);
289 hammer2_vfs_unmount(mp, MNT_FORCE);
294 * required mount structure initializations
296 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
297 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
299 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
300 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
303 * First locate the super-root inode, which is key 0 relative to the
304 * volume header's blockset.
306 * Then locate the root inode by scanning the directory keyspace
307 * represented by the label.
309 lhc = hammer2_dirhash(label, strlen(label));
310 parent = &hmp->vchain;
311 hammer2_chain_ref(hmp, parent);
312 hammer2_chain_lock(hmp, parent);
313 schain = hammer2_chain_lookup(hmp, &parent,
314 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
315 hammer2_chain_put(hmp, parent);
316 if (schain == NULL) {
317 kprintf("hammer2_mount: invalid super-root\n");
318 hammer2_vfs_unmount(mp, MNT_FORCE);
323 hammer2_chain_ref(hmp, parent); /* parent: lock+ref, schain: ref */
324 rchain = hammer2_chain_lookup(hmp, &parent,
325 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
328 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
330 strcmp(label, rchain->data->ipdata.filename) == 0) {
333 rchain = hammer2_chain_next(hmp, &parent, rchain,
334 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
337 hammer2_chain_put(hmp, parent);
338 if (rchain == NULL) {
339 kprintf("hammer2_mount: root label not found\n");
340 hammer2_chain_drop(hmp, schain);
341 hammer2_vfs_unmount(mp, MNT_FORCE);
344 hammer2_chain_unlock(hmp, rchain); /* rchain: ref */
346 hmp->schain = schain; /* left held & unlocked */
347 hmp->rchain = rchain; /* left held & unlocked */
348 hmp->iroot = rchain->u.ip; /* implied hold from rchain */
349 kprintf("iroot %p\n", rchain->u.ip);
352 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
353 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
354 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
356 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
357 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
358 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
359 copyinstr(path, mp->mnt_stat.f_mntonname,
360 sizeof(mp->mnt_stat.f_mntonname) - 1,
363 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
370 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
378 hammer2_vfs_unmount(struct mount *mp, int mntflags)
380 hammer2_mount_t *hmp;
383 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
386 kprintf("hammer2_unmount\n");
391 if (mntflags & MNT_FORCE)
394 hammer2_mount_exlock(hmp);
397 * If mount initialization proceeded far enough we must flush
400 kprintf("iroot %p\n", hmp->iroot);
402 error = vflush(mp, 0, flags);
409 * 1) Wait on the flusher having no work; heat up if needed
410 * 2) Scan inode RB tree till all the inodes are free
411 * 3) Destroy the kmalloc inode zone
412 * 4) Free the mount point
416 KKASSERT(hmp->rchain->refs == 1);
417 hammer2_chain_drop(hmp, hmp->rchain);
421 KKASSERT(hmp->schain->refs == 1);
422 hammer2_chain_drop(hmp, hmp->schain);
425 if ((devvp = hmp->devvp) != NULL) {
426 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
428 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
433 kmalloc_destroy(&hmp->minode);
434 kmalloc_destroy(&hmp->mchain);
436 hammer2_mount_unlock(hmp);
440 kfree(hmp, M_HAMMER2);
447 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
448 ino_t ino, struct vnode **vpp)
450 kprintf("hammer2_vget\n");
456 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
458 hammer2_mount_t *hmp;
462 kprintf("hammer2_root\n");
465 hammer2_mount_exlock(hmp);
466 if (hmp->iroot == NULL) {
470 vp = hammer2_igetv(hmp->iroot, &error);
473 kprintf("vnodefail\n");
475 hammer2_mount_unlock(hmp);
482 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
484 hammer2_mount_t *hmp;
488 mp->mnt_stat.f_files = 10;
489 mp->mnt_stat.f_bfree = 10;
490 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
498 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
500 hammer2_mount_t *hmp;
504 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
505 mp->mnt_vstat.f_files = 0;
506 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
508 *sbp = mp->mnt_vstat;
513 * Sync the entire filesystem; this is called from the filesystem syncer
514 * process periodically and whenever a user calls sync(1) on the hammer
517 * Currently is actually called from the syncer! \o/
519 * This task will have to snapshot the state of the dirty inode chain.
520 * From that, it will have to make sure all of the inodes on the dirty
521 * chain have IO initiated. We make sure that io is initiated for the root
524 * If waitfor is set, we wait for media to acknowledge the new rootblock.
526 * THINKS: side A vs side B, to have sync not stall all I/O?
530 hammer2_vfs_sync(struct mount *mp, int waitfor)
532 struct hammer2_sync_info info;
533 hammer2_mount_t *hmp;
537 kprintf("hammer2_sync \n");
541 if (waitfor & MNT_LAZY)
542 flags |= VMSC_ONEPASS;
545 info.waitfor = MNT_NOWAIT;
546 vmntvnodescan(mp, flags | VMSC_NOWAIT,
548 hammer2_sync_scan2, &info);
549 if (info.error == 0 && (waitfor & MNT_WAIT)) {
550 info.waitfor = waitfor;
551 vmntvnodescan(mp, flags,
553 hammer2_sync_scan2, &info);
557 if (waitfor == MNT_WAIT) {
563 hammer2_chain_lock(hmp, &hmp->vchain);
564 hammer2_chain_flush(hmp, &hmp->vchain, NULL);
565 hammer2_chain_unlock(hmp, &hmp->vchain);
566 error = vinvalbuf(hmp->devvp, V_SAVE, 0, 0);
571 bp->b_bio1.bio_offset = 0;
574 bp->b_cmd = BUF_CMD_FLUSH;
575 bp->b_bio1.bio_done = biodone_sync;
576 bp->b_bio1.bio_flags |= BIO_SYNC;
577 vn_strategy(hmp->devvp, &bp->b_bio1);
578 biowait(&bp->b_bio1, "h2vol");
581 kprintf("flush volume header\n");
583 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
584 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
592 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
597 if (vp->v_type == VNON || ip == NULL ||
598 ((ip->chain.flags & HAMMER2_CHAIN_MODIFIED) == 0 &&
599 RB_EMPTY(&vp->v_rbdirty_tree))) {
606 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
608 struct hammer2_sync_info *info = data;
613 if (vp->v_type == VNON || vp->v_type == VBAD ||
614 ((ip->chain.flags & HAMMER2_CHAIN_MODIFIED) == 0 &&
615 RB_EMPTY(&vp->v_rbdirty_tree))) {
618 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
626 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
633 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
634 struct fid *fhp, struct vnode **vpp)
641 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
642 int *exflagsp, struct ucred **credanonp)
648 * Support code for hammer2_mount(). Read, verify, and install the volume
649 * header into the HMP
651 * XXX read four volhdrs and use the one with the highest TID whos CRC
656 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
657 * nonexistant locations.
659 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
663 hammer2_install_volume_header(hammer2_mount_t *hmp)
665 hammer2_volume_data_t *vd;
667 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
679 * There are up to 4 copies of the volume header (syncs iterate
680 * between them so there is no single master). We don't trust the
681 * volu_size field so we don't know precisely how large the filesystem
682 * is, so depend on the OS to return an error if we go beyond the
683 * block device's EOF.
685 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
686 error = bread(hmp->devvp, i * HAMMER2_RESERVE_BYTES64,
687 HAMMER2_VOLUME_BYTES, &bp);
694 vd = (struct hammer2_volume_data *) bp->b_data;
695 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
696 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
702 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
703 /* XXX: Reversed-endianness filesystem */
704 kprintf("hammer2: reverse-endian filesystem detected");
710 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
711 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
712 HAMMER2_VOLUME_ICRC0_SIZE);
713 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
714 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
715 HAMMER2_VOLUME_ICRC1_SIZE);
716 if ((crc0 != crc) || (bcrc0 != bcrc)) {
717 kprintf("hammer2 volume header crc "
718 "mismatch copy #%d\t%08x %08x",
725 if (valid == 0 || hmp->voldata.last_tid < vd->last_tid) {
735 kprintf("hammer2: a valid volume header was found\n");
738 kprintf("hammer2: no valid volume headers found!\n");