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);
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);
319 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
321 strcmp(label, rchain->data->ipdata.filename) == 0) {
324 rchain = hammer2_chain_next(hmp, &parent, rchain,
325 lhc, lhc + HAMMER2_DIRHASH_LOMASK);
327 hammer2_chain_put(hmp, parent);
328 if (rchain == NULL) {
329 kprintf("hammer2_mount: root label not found\n");
330 hammer2_chain_drop(hmp, schain);
331 hammer2_vfs_unmount(mp, MNT_FORCE);
334 hammer2_chain_unlock(hmp, rchain); /* rchain: ref */
336 hmp->schain = schain; /* left held & unlocked */
337 hmp->rchain = rchain; /* left held & unlocked */
338 hmp->iroot = rchain->u.ip; /* implied hold from rchain */
339 kprintf("iroot %p\n", rchain->u.ip);
342 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
343 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
344 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
346 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
347 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
348 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
349 copyinstr(path, mp->mnt_stat.f_mntonname,
350 sizeof(mp->mnt_stat.f_mntonname) - 1,
353 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
360 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
368 hammer2_vfs_unmount(struct mount *mp, int mntflags)
370 hammer2_mount_t *hmp;
373 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
376 kprintf("hammer2_unmount\n");
381 if (mntflags & MNT_FORCE)
384 hammer2_mount_exlock(hmp);
387 * If mount initialization proceeded far enough we must flush
390 kprintf("iroot %p\n", hmp->iroot);
392 error = vflush(mp, 0, flags);
399 * 1) Wait on the flusher having no work; heat up if needed
400 * 2) Scan inode RB tree till all the inodes are free
401 * 3) Destroy the kmalloc inode zone
402 * 4) Free the mount point
406 KKASSERT(hmp->rchain->refs == 1);
407 hammer2_chain_drop(hmp, hmp->rchain);
411 KKASSERT(hmp->schain->refs == 1);
412 hammer2_chain_drop(hmp, hmp->schain);
415 if ((devvp = hmp->devvp) != NULL) {
416 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
418 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
423 kmalloc_destroy(&hmp->minode);
424 kmalloc_destroy(&hmp->mchain);
426 hammer2_mount_unlock(hmp);
430 kfree(hmp, M_HAMMER2);
437 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
438 ino_t ino, struct vnode **vpp)
440 kprintf("hammer2_vget\n");
446 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
448 hammer2_mount_t *hmp;
452 kprintf("hammer2_root\n");
455 hammer2_mount_exlock(hmp);
456 if (hmp->iroot == NULL) {
460 vp = hammer2_igetv(hmp->iroot, &error);
463 kprintf("vnodefail\n");
465 hammer2_mount_unlock(hmp);
472 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
474 hammer2_mount_t *hmp;
478 mp->mnt_stat.f_files = 10;
479 mp->mnt_stat.f_bfree = 10;
480 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
488 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
490 hammer2_mount_t *hmp;
494 mp->mnt_vstat.f_files = 10;
495 mp->mnt_vstat.f_bfree = 10;
496 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
498 *sbp = mp->mnt_vstat;
503 * Sync the entire filesystem; this is called from the filesystem syncer
504 * process periodically and whenever a user calls sync(1) on the hammer
507 * Currently is actually called from the syncer! \o/
509 * This task will have to snapshot the state of the dirty inode chain.
510 * From that, it will have to make sure all of the inodes on the dirty
511 * chain have IO initiated. We make sure that io is initiated for the root
514 * If waitfor is set, we wait for media to acknowledge the new rootblock.
516 * THINKS: side A vs side B, to have sync not stall all I/O?
520 hammer2_vfs_sync(struct mount *mp, int waitfor)
522 struct hammer2_sync_info info;
523 hammer2_mount_t *hmp;
527 kprintf("hammer2_sync \n");
531 if (waitfor & MNT_LAZY)
532 flags |= VMSC_ONEPASS;
535 info.waitfor = MNT_NOWAIT;
536 vmntvnodescan(mp, flags | VMSC_NOWAIT,
538 hammer2_sync_scan2, &info);
539 if (info.error == 0 && (waitfor & MNT_WAIT)) {
540 info.waitfor = waitfor;
541 vmntvnodescan(mp, flags,
543 hammer2_sync_scan2, &info);
547 if (waitfor == MNT_WAIT) {
553 hammer2_chain_lock(hmp, &hmp->vchain);
554 hammer2_chain_flush(hmp, &hmp->vchain, NULL);
555 hammer2_chain_unlock(hmp, &hmp->vchain);
556 error = vinvalbuf(hmp->devvp, V_SAVE, 0, 0);
561 bp->b_bio1.bio_offset = 0;
564 bp->b_cmd = BUF_CMD_FLUSH;
565 bp->b_bio1.bio_done = biodone_sync;
566 bp->b_bio1.bio_flags |= BIO_SYNC;
567 vn_strategy(hmp->devvp, &bp->b_bio1);
568 biowait(&bp->b_bio1, "h2vol");
571 kprintf("flush volume header\n");
573 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
574 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
582 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
587 if (vp->v_type == VNON || ip == NULL ||
588 ((ip->chain.flags & HAMMER2_CHAIN_MODIFIED) == 0 &&
589 RB_EMPTY(&vp->v_rbdirty_tree))) {
596 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
598 struct hammer2_sync_info *info = data;
603 if (vp->v_type == VNON || vp->v_type == VBAD ||
604 ((ip->chain.flags & HAMMER2_CHAIN_MODIFIED) == 0 &&
605 RB_EMPTY(&vp->v_rbdirty_tree))) {
608 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
616 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
623 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
624 struct fid *fhp, struct vnode **vpp)
631 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
632 int *exflagsp, struct ucred **credanonp)
638 * Support code for hammer2_mount(). Read, verify, and install the volume
639 * header into the HMP
641 * XXX read four volhdrs and use the one with the highest TID whos CRC
646 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
647 * nonexistant locations.
649 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
653 hammer2_install_volume_header(hammer2_mount_t *hmp)
655 hammer2_volume_data_t *vd;
657 hammer2_crc32_t ccrc, crc;
669 * There are up to 4 copies of the volume header (syncs iterate
670 * between them so there is no single master). We don't trust the
671 * volu_size field so we don't know precisely how large the filesystem
672 * is, so depend on the OS to return an error if we go beyond the
673 * block device's EOF.
675 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
676 error = bread(hmp->devvp, i * HAMMER2_RESERVE_BYTES64,
677 HAMMER2_VOLUME_BYTES, &bp);
684 vd = (struct hammer2_volume_data *)bp->b_data;
685 if (vd->magic != HAMMER2_VOLUME_ID_HBO)
688 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
689 ccrc = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
690 HAMMER2_VOLUME_ICRC0_SIZE);
692 kprintf("hammer2 volume header crc "
693 "mismatch copy #%d\t%08x %08x",
700 if (valid == 0 || hmp->voldata.last_tid < vd->last_tid) {
710 kprintf("hammer2: a valid volume header was found\n");
713 kprintf("hammer2: no valid volume headers found!\n");