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
401 error = vflush(mp, 0, flags);
407 * Flush any left over chains
409 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED1 |
410 HAMMER2_CHAIN_SUBMODIFIED)) {
411 hammer2_vfs_sync(mp, MNT_WAIT);
413 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED1 |
414 HAMMER2_CHAIN_SUBMODIFIED)) {
415 kprintf("hammer2_unmount: chains left over after final sync\n");
419 * Cleanup the root and super-root chain elements (which should be
424 KKASSERT(hmp->rchain->refs == 1);
425 hammer2_chain_drop(hmp, hmp->rchain);
429 KKASSERT(hmp->schain->refs == 1);
430 hammer2_chain_drop(hmp, hmp->schain);
435 * Finish up with the device vnode
437 if ((devvp = hmp->devvp) != NULL) {
438 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
440 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
445 kmalloc_destroy(&hmp->minode);
446 kmalloc_destroy(&hmp->mchain);
448 hammer2_mount_unlock(hmp);
452 kfree(hmp, M_HAMMER2);
459 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
460 ino_t ino, struct vnode **vpp)
462 kprintf("hammer2_vget\n");
468 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
470 hammer2_mount_t *hmp;
474 kprintf("hammer2_root\n");
477 hammer2_mount_exlock(hmp);
478 if (hmp->iroot == NULL) {
482 vp = hammer2_igetv(hmp->iroot, &error);
485 kprintf("vnodefail\n");
487 hammer2_mount_unlock(hmp);
494 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
496 hammer2_mount_t *hmp;
500 mp->mnt_stat.f_files = 10;
501 mp->mnt_stat.f_bfree = 10;
502 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
510 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
512 hammer2_mount_t *hmp;
516 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
517 mp->mnt_vstat.f_files = 0;
518 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
520 *sbp = mp->mnt_vstat;
525 * Sync the entire filesystem; this is called from the filesystem syncer
526 * process periodically and whenever a user calls sync(1) on the hammer
529 * Currently is actually called from the syncer! \o/
531 * This task will have to snapshot the state of the dirty inode chain.
532 * From that, it will have to make sure all of the inodes on the dirty
533 * chain have IO initiated. We make sure that io is initiated for the root
536 * If waitfor is set, we wait for media to acknowledge the new rootblock.
538 * THINKS: side A vs side B, to have sync not stall all I/O?
542 hammer2_vfs_sync(struct mount *mp, int waitfor)
544 struct hammer2_sync_info info;
545 hammer2_mount_t *hmp;
550 kprintf("hammer2_sync \n");
554 if (waitfor & MNT_LAZY)
555 flags |= VMSC_ONEPASS;
558 info.waitfor = MNT_NOWAIT;
559 vmntvnodescan(mp, flags | VMSC_NOWAIT,
561 hammer2_sync_scan2, &info);
562 if (info.error == 0 && (waitfor & MNT_WAIT)) {
563 info.waitfor = waitfor;
564 vmntvnodescan(mp, flags,
566 hammer2_sync_scan2, &info);
570 if (waitfor == MNT_WAIT) {
576 hammer2_chain_lock(hmp, &hmp->vchain);
577 if (hmp->vchain.flags &
578 (HAMMER2_CHAIN_MODIFIED1 | HAMMER2_CHAIN_SUBMODIFIED)) {
579 hammer2_chain_flush(hmp, &hmp->vchain);
584 hammer2_chain_unlock(hmp, &hmp->vchain);
585 error = vinvalbuf(hmp->devvp, V_SAVE, 0, 0);
586 if (error == 0 && haswork) {
589 kprintf("synchronize disk\n");
592 bp->b_bio1.bio_offset = 0;
595 bp->b_cmd = BUF_CMD_FLUSH;
596 bp->b_bio1.bio_done = biodone_sync;
597 bp->b_bio1.bio_flags |= BIO_SYNC;
598 vn_strategy(hmp->devvp, &bp->b_bio1);
599 biowait(&bp->b_bio1, "h2vol");
602 kprintf("flush volume header\n");
604 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
605 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
613 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
618 if (vp->v_type == VNON || ip == NULL ||
619 ((ip->chain.flags & HAMMER2_CHAIN_MODIFIED1) == 0 &&
620 RB_EMPTY(&vp->v_rbdirty_tree))) {
627 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
629 struct hammer2_sync_info *info = data;
634 if (vp->v_type == VNON || vp->v_type == VBAD ||
635 ((ip->chain.flags & HAMMER2_CHAIN_MODIFIED1) == 0 &&
636 RB_EMPTY(&vp->v_rbdirty_tree))) {
639 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
647 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
654 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
655 struct fid *fhp, struct vnode **vpp)
662 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
663 int *exflagsp, struct ucred **credanonp)
669 * Support code for hammer2_mount(). Read, verify, and install the volume
670 * header into the HMP
672 * XXX read four volhdrs and use the one with the highest TID whos CRC
677 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
678 * nonexistant locations.
680 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
684 hammer2_install_volume_header(hammer2_mount_t *hmp)
686 hammer2_volume_data_t *vd;
688 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
700 * There are up to 4 copies of the volume header (syncs iterate
701 * between them so there is no single master). We don't trust the
702 * volu_size field so we don't know precisely how large the filesystem
703 * is, so depend on the OS to return an error if we go beyond the
704 * block device's EOF.
706 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
707 error = bread(hmp->devvp, i * HAMMER2_RESERVE_BYTES64,
708 HAMMER2_VOLUME_BYTES, &bp);
715 vd = (struct hammer2_volume_data *) bp->b_data;
716 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
717 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
723 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
724 /* XXX: Reversed-endianness filesystem */
725 kprintf("hammer2: reverse-endian filesystem detected");
731 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
732 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
733 HAMMER2_VOLUME_ICRC0_SIZE);
734 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
735 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
736 HAMMER2_VOLUME_ICRC1_SIZE);
737 if ((crc0 != crc) || (bcrc0 != bcrc)) {
738 kprintf("hammer2 volume header crc "
739 "mismatch copy #%d\t%08x %08x",
746 if (valid == 0 || hmp->voldata.last_tid < vd->last_tid) {
756 kprintf("hammer2: a valid volume header was found\n");
759 kprintf("hammer2: no valid volume headers found!\n");