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>
46 #include "hammer2_disk.h"
47 #include "hammer2_mount.h"
49 struct hammer2_sync_info {
54 static int hammer2_vfs_init(struct vfsconf *conf);
55 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
57 static int hammer2_remount(struct mount *, char *, struct vnode *,
59 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
60 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
61 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
63 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
65 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
66 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
67 ino_t ino, struct vnode **vpp);
68 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
69 struct fid *fhp, struct vnode **vpp);
70 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
71 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
72 int *exflagsp, struct ucred **credanonp);
74 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
75 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
76 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
79 * HAMMER2 vfs operations.
81 static struct vfsops hammer2_vfsops = {
82 .vfs_init = hammer2_vfs_init,
83 .vfs_sync = hammer2_vfs_sync,
84 .vfs_mount = hammer2_vfs_mount,
85 .vfs_unmount = hammer2_vfs_unmount,
86 .vfs_root = hammer2_vfs_root,
87 .vfs_statfs = hammer2_vfs_statfs,
88 .vfs_statvfs = hammer2_vfs_statvfs,
89 .vfs_vget = hammer2_vfs_vget,
90 .vfs_vptofh = hammer2_vfs_vptofh,
91 .vfs_fhtovp = hammer2_vfs_fhtovp,
92 .vfs_checkexp = hammer2_vfs_checkexp
95 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
97 VFS_SET(hammer2_vfsops, hammer2, 0);
98 MODULE_VERSION(hammer2, 1);
102 hammer2_vfs_init(struct vfsconf *conf)
108 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
110 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
112 if (HAMMER2_ALLOCREF_BYTES != sizeof(struct hammer2_allocref))
114 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
118 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
124 * Mount or remount HAMMER2 fileystem from physical media
127 * mp mount point structure
133 * mp mount point structure
134 * path path to mount point
135 * data pointer to argument structure in user space
136 * volume volume path (device@LABEL form)
137 * hflags user mount flags
138 * cred user credentials
145 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
148 struct hammer2_mount_info info;
149 hammer2_mount_t *hmp;
152 struct nlookupdata nd;
153 hammer2_chain_t *parent;
154 hammer2_chain_t *schain;
155 hammer2_chain_t *rchain;
156 char devstr[MNAMELEN];
169 kprintf("hammer2_mount\n");
178 * Non-root mount or updating a mount
180 error = copyin(data, &info, sizeof(info));
184 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
188 /* Extract device and label */
190 label = strchr(devstr, '@');
192 ((label + 1) - dev) > done) {
200 if (mp->mnt_flag & MNT_UPDATE) {
202 /* HAMMER2 implements NFS export via mountctl */
205 error = hammer2_remount(mp, path, devvp, cred);
213 /* Lookup name and verify it refers to a block device */
214 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
216 error = nlookup(&nd);
218 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
222 if (vn_isdisk(devvp, &error))
223 error = vfs_mountedon(devvp);
225 if (error == 0 && vcount(devvp) > 0)
229 * Now open the device
232 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
233 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
234 error = vinvalbuf(devvp, V_SAVE, 0, 0);
236 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD | FWRITE,
241 if (error && devvp) {
249 * Block device opened successfully, finish initializing the
252 * From this point on we have to call hammer2_unmount() on failure.
254 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
255 mp->mnt_data = (qaddr_t)hmp;
259 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
260 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
262 mp->mnt_flag = MNT_LOCAL;
263 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
266 * vchain setup. vchain.data is special cased to NULL. vchain.refs
267 * is initialized and will never drop to 0.
269 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
270 hmp->vchain.refs = 1;
271 hmp->vchain.data = (void *)&hmp->voldata;
272 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
273 /* hmp->vchain.u.xxx is left NULL */
274 lockinit(&hmp->vchain.lk, "volume", 0, LK_CANRECURSE);
277 * Install the volume header
279 error = hammer2_install_volume_header(hmp);
281 hammer2_vfs_unmount(mp, MNT_FORCE);
286 * required mount structure initializations
288 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
289 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
291 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
292 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
295 * First locate the super-root inode, which is key 0 relative to the
296 * volume header's blockset.
298 * Then locate the root inode by scanning the directory keyspace
299 * represented by the label.
301 lhc = hammer2_dirhash(label, strlen(label));
302 parent = &hmp->vchain;
303 hammer2_chain_ref(hmp, parent);
304 hammer2_chain_lock(hmp, parent);
305 schain = hammer2_chain_lookup(hmp, &parent,
306 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
307 hammer2_chain_put(hmp, parent);
308 if (schain == NULL) {
309 kprintf("hammer2_mount: invalid super-root\n");
310 hammer2_vfs_unmount(mp, MNT_FORCE);
315 hammer2_chain_ref(hmp, parent); /* parent: lock+ref, schain: ref */
316 rchain = hammer2_chain_lookup(hmp, &parent,
317 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
320 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
322 strcmp(label, rchain->data->ipdata.filename) == 0) {
325 rchain = hammer2_chain_next(hmp, &parent, rchain,
326 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
329 hammer2_chain_put(hmp, parent);
330 if (rchain == NULL) {
331 kprintf("hammer2_mount: root label not found\n");
332 hammer2_chain_drop(hmp, schain);
333 hammer2_vfs_unmount(mp, MNT_FORCE);
336 hammer2_chain_unlock(hmp, rchain); /* rchain: ref */
338 hmp->schain = schain; /* left held & unlocked */
339 hmp->rchain = rchain; /* left held & unlocked */
340 hmp->iroot = rchain->u.ip; /* implied hold from rchain */
341 kprintf("iroot %p\n", rchain->u.ip);
344 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
345 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
346 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
348 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
349 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
350 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
351 copyinstr(path, mp->mnt_stat.f_mntonname,
352 sizeof(mp->mnt_stat.f_mntonname) - 1,
355 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
362 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
370 hammer2_vfs_unmount(struct mount *mp, int mntflags)
372 hammer2_mount_t *hmp;
375 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
378 kprintf("hammer2_unmount\n");
383 if (mntflags & MNT_FORCE)
386 hammer2_mount_exlock(hmp);
389 * If mount initialization proceeded far enough we must flush
392 kprintf("iroot %p\n", hmp->iroot);
394 error = vflush(mp, 0, flags);
401 * 1) Wait on the flusher having no work; heat up if needed
402 * 2) Scan inode RB tree till all the inodes are free
403 * 3) Destroy the kmalloc inode zone
404 * 4) Free the mount point
408 KKASSERT(hmp->rchain->refs == 1);
409 hammer2_chain_drop(hmp, hmp->rchain);
413 KKASSERT(hmp->schain->refs == 1);
414 hammer2_chain_drop(hmp, hmp->schain);
417 if ((devvp = hmp->devvp) != NULL) {
418 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
420 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
425 kmalloc_destroy(&hmp->minode);
426 kmalloc_destroy(&hmp->mchain);
428 hammer2_mount_unlock(hmp);
432 kfree(hmp, M_HAMMER2);
439 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
440 ino_t ino, struct vnode **vpp)
442 kprintf("hammer2_vget\n");
448 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
450 hammer2_mount_t *hmp;
454 kprintf("hammer2_root\n");
457 hammer2_mount_exlock(hmp);
458 if (hmp->iroot == NULL) {
462 vp = hammer2_igetv(hmp->iroot, &error);
465 kprintf("vnodefail\n");
467 hammer2_mount_unlock(hmp);
474 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
476 hammer2_mount_t *hmp;
480 mp->mnt_stat.f_files = 10;
481 mp->mnt_stat.f_bfree = 10;
482 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
490 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
492 hammer2_mount_t *hmp;
496 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
497 mp->mnt_vstat.f_files = 0;
498 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
500 *sbp = mp->mnt_vstat;
505 * Sync the entire filesystem; this is called from the filesystem syncer
506 * process periodically and whenever a user calls sync(1) on the hammer
509 * Currently is actually called from the syncer! \o/
511 * This task will have to snapshot the state of the dirty inode chain.
512 * From that, it will have to make sure all of the inodes on the dirty
513 * chain have IO initiated. We make sure that io is initiated for the root
516 * If waitfor is set, we wait for media to acknowledge the new rootblock.
518 * THINKS: side A vs side B, to have sync not stall all I/O?
522 hammer2_vfs_sync(struct mount *mp, int waitfor)
524 struct hammer2_sync_info info;
525 hammer2_mount_t *hmp;
529 kprintf("hammer2_sync \n");
533 if (waitfor & MNT_LAZY)
534 flags |= VMSC_ONEPASS;
537 info.waitfor = MNT_NOWAIT;
538 vmntvnodescan(mp, flags | VMSC_NOWAIT,
540 hammer2_sync_scan2, &info);
541 if (info.error == 0 && (waitfor & MNT_WAIT)) {
542 info.waitfor = waitfor;
543 vmntvnodescan(mp, flags,
545 hammer2_sync_scan2, &info);
549 if (waitfor == MNT_WAIT) {
555 hammer2_chain_lock(hmp, &hmp->vchain);
556 hammer2_chain_flush(hmp, &hmp->vchain, NULL);
557 hammer2_chain_unlock(hmp, &hmp->vchain);
558 error = vinvalbuf(hmp->devvp, V_SAVE, 0, 0);
563 bp->b_bio1.bio_offset = 0;
566 bp->b_cmd = BUF_CMD_FLUSH;
567 bp->b_bio1.bio_done = biodone_sync;
568 bp->b_bio1.bio_flags |= BIO_SYNC;
569 vn_strategy(hmp->devvp, &bp->b_bio1);
570 biowait(&bp->b_bio1, "h2vol");
573 kprintf("flush volume header\n");
575 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
576 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
584 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
589 if (vp->v_type == VNON || ip == NULL ||
590 ((ip->chain.flags & HAMMER2_CHAIN_MODIFIED) == 0 &&
591 RB_EMPTY(&vp->v_rbdirty_tree))) {
598 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
600 struct hammer2_sync_info *info = data;
605 if (vp->v_type == VNON || vp->v_type == VBAD ||
606 ((ip->chain.flags & HAMMER2_CHAIN_MODIFIED) == 0 &&
607 RB_EMPTY(&vp->v_rbdirty_tree))) {
610 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
618 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
625 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
626 struct fid *fhp, struct vnode **vpp)
633 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
634 int *exflagsp, struct ucred **credanonp)
640 * Support code for hammer2_mount(). Read, verify, and install the volume
641 * header into the HMP
643 * XXX read four volhdrs and use the one with the highest TID whos CRC
648 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
649 * nonexistant locations.
651 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
655 hammer2_install_volume_header(hammer2_mount_t *hmp)
657 hammer2_volume_data_t *vd;
659 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
671 * There are up to 4 copies of the volume header (syncs iterate
672 * between them so there is no single master). We don't trust the
673 * volu_size field so we don't know precisely how large the filesystem
674 * is, so depend on the OS to return an error if we go beyond the
675 * block device's EOF.
677 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
678 error = bread(hmp->devvp, i * HAMMER2_RESERVE_BYTES64,
679 HAMMER2_VOLUME_BYTES, &bp);
686 vd = (struct hammer2_volume_data *) bp->b_data;
687 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
688 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
694 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
695 /* XXX: Reversed-endianness filesystem */
696 kprintf("hammer2: reverse-endian filesystem detected");
702 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
703 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
704 HAMMER2_VOLUME_ICRC0_SIZE);
705 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
706 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
707 HAMMER2_VOLUME_ICRC1_SIZE);
708 if ((crc0 != crc) || (bcrc0 != bcrc)) {
709 kprintf("hammer2 volume header crc "
710 "mismatch copy #%d\t%08x %08x",
717 if (valid == 0 || hmp->voldata.last_tid < vd->last_tid) {
727 kprintf("hammer2: a valid volume header was found\n");
730 kprintf("hammer2: no valid volume headers found!\n");