2 * Copyright (c) 2011-2013 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>
45 #include <sys/socket.h>
48 #include "hammer2_disk.h"
49 #include "hammer2_mount.h"
51 #define REPORT_REFS_ERRORS 1 /* XXX remove me */
53 struct hammer2_sync_info {
54 hammer2_trans_t trans;
59 TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
60 static struct hammer2_mntlist hammer2_mntlist;
61 static struct lock hammer2_mntlk;
64 int hammer2_cluster_enable = 1;
65 int hammer2_hardlink_enable = 1;
66 long hammer2_iod_file_read;
67 long hammer2_iod_meta_read;
68 long hammer2_iod_indr_read;
69 long hammer2_iod_file_write;
70 long hammer2_iod_meta_write;
71 long hammer2_iod_indr_write;
72 long hammer2_iod_fmap_write;
73 long hammer2_iod_volu_write;
74 long hammer2_ioa_file_read;
75 long hammer2_ioa_meta_read;
76 long hammer2_ioa_indr_read;
77 long hammer2_ioa_fmap_write;
78 long hammer2_ioa_file_write;
79 long hammer2_ioa_meta_write;
80 long hammer2_ioa_indr_write;
81 long hammer2_ioa_volu_write;
83 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
85 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
86 &hammer2_debug, 0, "");
87 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
88 &hammer2_cluster_enable, 0, "");
89 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
90 &hammer2_hardlink_enable, 0, "");
91 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
92 &hammer2_iod_file_read, 0, "");
93 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
94 &hammer2_iod_meta_read, 0, "");
95 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
96 &hammer2_iod_indr_read, 0, "");
97 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
98 &hammer2_iod_file_write, 0, "");
99 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
100 &hammer2_iod_meta_write, 0, "");
101 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
102 &hammer2_iod_indr_write, 0, "");
103 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
104 &hammer2_iod_volu_write, 0, "");
105 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
106 &hammer2_ioa_file_read, 0, "");
107 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
108 &hammer2_ioa_meta_read, 0, "");
109 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
110 &hammer2_ioa_indr_read, 0, "");
111 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
112 &hammer2_ioa_file_write, 0, "");
113 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
114 &hammer2_ioa_meta_write, 0, "");
115 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
116 &hammer2_ioa_indr_write, 0, "");
117 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
118 &hammer2_ioa_volu_write, 0, "");
120 static int hammer2_vfs_init(struct vfsconf *conf);
121 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
123 static int hammer2_remount(struct mount *, char *, struct vnode *,
125 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
126 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
127 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
129 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
131 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
132 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
133 ino_t ino, struct vnode **vpp);
134 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
135 struct fid *fhp, struct vnode **vpp);
136 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
137 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
138 int *exflagsp, struct ucred **credanonp);
140 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
141 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
142 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
144 static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
145 static void hammer2_autodmsg(kdmsg_msg_t *msg);
148 * HAMMER2 vfs operations.
150 static struct vfsops hammer2_vfsops = {
151 .vfs_init = hammer2_vfs_init,
152 .vfs_sync = hammer2_vfs_sync,
153 .vfs_mount = hammer2_vfs_mount,
154 .vfs_unmount = hammer2_vfs_unmount,
155 .vfs_root = hammer2_vfs_root,
156 .vfs_statfs = hammer2_vfs_statfs,
157 .vfs_statvfs = hammer2_vfs_statvfs,
158 .vfs_vget = hammer2_vfs_vget,
159 .vfs_vptofh = hammer2_vfs_vptofh,
160 .vfs_fhtovp = hammer2_vfs_fhtovp,
161 .vfs_checkexp = hammer2_vfs_checkexp
164 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
166 VFS_SET(hammer2_vfsops, hammer2, 0);
167 MODULE_VERSION(hammer2, 1);
171 hammer2_vfs_init(struct vfsconf *conf)
177 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
179 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
181 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
185 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
187 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
188 TAILQ_INIT(&hammer2_mntlist);
194 * Mount or remount HAMMER2 fileystem from physical media
197 * mp mount point structure
203 * mp mount point structure
204 * path path to mount point
205 * data pointer to argument structure in user space
206 * volume volume path (device@LABEL form)
207 * hflags user mount flags
208 * cred user credentials
215 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
218 struct hammer2_mount_info info;
219 hammer2_pfsmount_t *pmp;
220 hammer2_mount_t *hmp;
223 struct nlookupdata nd;
224 hammer2_chain_t *parent;
225 hammer2_chain_t *schain;
226 hammer2_chain_t *rchain;
228 char devstr[MNAMELEN];
243 kprintf("hammer2_mount\n");
249 bzero(&info, sizeof(info));
250 info.cluster_fd = -1;
254 * Non-root mount or updating a mount
256 error = copyin(data, &info, sizeof(info));
260 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
264 /* Extract device and label */
266 label = strchr(devstr, '@');
268 ((label + 1) - dev) > done) {
276 if (mp->mnt_flag & MNT_UPDATE) {
278 /* HAMMER2 implements NFS export via mountctl */
281 error = hammer2_remount(mp, path, devvp, cred);
289 * Lookup name and verify it refers to a block device.
291 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
293 error = nlookup(&nd);
295 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
299 if (vn_isdisk(devvp, &error))
300 error = vfs_mountedon(devvp);
304 * Determine if the device has already been mounted. After this
305 * check hmp will be non-NULL if we are doing the second or more
306 * hammer2 mounts from the same device.
308 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
309 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
310 if (hmp->devvp == devvp)
315 * Open the device if this isn't a secondary mount
321 if (error == 0 && vcount(devvp) > 0)
325 * Now open the device
328 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
329 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
330 error = vinvalbuf(devvp, V_SAVE, 0, 0);
332 error = VOP_OPEN(devvp,
333 ronly ? FREAD : FREAD | FWRITE,
338 if (error && devvp) {
343 lockmgr(&hammer2_mntlk, LK_RELEASE);
349 * Block device opened successfully, finish initializing the
352 * From this point on we have to call hammer2_unmount() on failure.
354 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
355 mp->mnt_data = (qaddr_t)pmp;
358 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
359 kdmsg_iocom_init(&pmp->iocom, pmp,
360 KDMSG_IOCOMF_AUTOCONN |
361 KDMSG_IOCOMF_AUTOSPAN |
362 KDMSG_IOCOMF_AUTOCIRC,
363 pmp->mmsg, hammer2_rcvdmsg);
366 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
369 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
370 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
371 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
373 ccms_domain_init(&pmp->ccms_dom);
376 lockmgr(&hammer2_mntlk, LK_RELEASE);
377 kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
379 mp->mnt_flag = MNT_LOCAL;
380 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
386 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
387 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
390 * vchain setup. vchain.data is special cased to NULL.
391 * vchain.refs is initialized and will never drop to 0.
393 hmp->vchain.hmp = hmp;
394 hmp->vchain.refs = 1;
395 hmp->vchain.data = (void *)&hmp->voldata;
396 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
397 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
398 hammer2_chain_core_alloc(&hmp->vchain, NULL);
399 /* hmp->vchain.u.xxx is left NULL */
402 * Install the volume header
404 error = hammer2_install_volume_header(hmp);
406 hammer2_vfs_unmount(mp, MNT_FORCE);
412 * required mount structure initializations
414 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
415 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
417 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
418 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
423 mp->mnt_iosize_max = MAXPHYS;
426 * First locate the super-root inode, which is key 0 relative to the
427 * volume header's blockset.
429 * Then locate the root inode by scanning the directory keyspace
430 * represented by the label.
433 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
434 schain = hammer2_chain_lookup(&parent,
435 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
436 hammer2_chain_lookup_done(parent);
437 if (schain == NULL) {
438 kprintf("hammer2_mount: invalid super-root\n");
439 hammer2_vfs_unmount(mp, MNT_FORCE);
442 hammer2_chain_ref(schain); /* for hmp->schain */
443 hmp->schain = schain; /* left locked for inode_get */
444 hmp->sroot = hammer2_inode_get(hmp, NULL, NULL, schain);
445 hammer2_inode_ref(hmp->sroot); /* for hmp->sroot */
446 hammer2_inode_unlock_ex(hmp->sroot); /* eats schain lock */
448 schain = hmp->schain;
452 * schain only has 1 ref now for its hmp->schain assignment.
453 * Setup for lookup (which will lock it).
455 parent = hammer2_chain_lookup_init(schain, 0);
456 lhc = hammer2_dirhash(label, strlen(label));
457 rchain = hammer2_chain_lookup(&parent,
458 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
461 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
462 strcmp(label, rchain->data->ipdata.filename) == 0) {
465 rchain = hammer2_chain_next(&parent, rchain,
466 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
469 hammer2_chain_lookup_done(parent);
470 if (rchain == NULL) {
471 kprintf("hammer2_mount: PFS label not found\n");
472 hammer2_vfs_unmount(mp, MNT_FORCE);
475 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
476 hammer2_chain_unlock(rchain);
477 kprintf("hammer2_mount: PFS label already mounted!\n");
478 hammer2_vfs_unmount(mp, MNT_FORCE);
481 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
484 * NOTE: *_get() integrates chain's lock into the inode lock.
486 hammer2_chain_ref(rchain); /* for pmp->rchain */
487 pmp->rchain = rchain; /* left held & unlocked */
488 pmp->iroot = hammer2_inode_get(hmp, pmp, NULL, rchain);
489 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */
490 hammer2_inode_unlock_ex(pmp->iroot); /* iroot & its chain */
492 kprintf("iroot %p\n", pmp->iroot);
495 * Ref the cluster management messaging descriptor. The mount
496 * program deals with the other end of the communications pipe.
498 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
500 kprintf("hammer2_mount: bad cluster_fd!\n");
501 hammer2_vfs_unmount(mp, MNT_FORCE);
504 hammer2_cluster_reconnect(pmp, fp);
510 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
511 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
512 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
514 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
515 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
516 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
517 copyinstr(path, mp->mnt_stat.f_mntonname,
518 sizeof(mp->mnt_stat.f_mntonname) - 1,
522 * Initial statfs to prime mnt_stat.
524 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
531 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
539 hammer2_vfs_unmount(struct mount *mp, int mntflags)
541 hammer2_pfsmount_t *pmp;
542 hammer2_mount_t *hmp;
545 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
552 if (mntflags & MNT_FORCE)
555 hammer2_mount_exlock(hmp);
558 * If mount initialization proceeded far enough we must flush
562 error = vflush(mp, 0, flags);
567 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
569 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
572 * Flush any left over chains. The voldata lock is only used
573 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
575 hammer2_voldata_lock(hmp);
576 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
577 HAMMER2_CHAIN_SUBMODIFIED)) {
578 hammer2_voldata_unlock(hmp, 0);
579 hammer2_vfs_sync(mp, MNT_WAIT);
581 hammer2_voldata_unlock(hmp, 0);
583 if (hmp->pmp_count == 0) {
584 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
585 HAMMER2_CHAIN_SUBMODIFIED)) {
586 kprintf("hammer2_unmount: chains left over after "
588 if (hammer2_debug & 0x0010)
589 Debugger("entered debugger");
594 * Cleanup the root and super-root chain elements (which should be
598 hammer2_inode_lock_ex(pmp->iroot);
599 hammer2_inode_put(pmp->iroot);
600 /* lock destroyed by the put */
601 #if REPORT_REFS_ERRORS
602 if (pmp->iroot->refs != 1)
603 kprintf("PMP->IROOT %p REFS WRONG %d\n",
604 pmp->iroot, pmp->iroot->refs);
606 KKASSERT(pmp->iroot->refs == 1);
608 hammer2_inode_drop(pmp->iroot); /* ref for pmp->iroot */
612 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
613 #if REPORT_REFS_ERRORS
614 if (pmp->rchain->refs != 1)
615 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
616 pmp->rchain, pmp->rchain->refs);
618 KKASSERT(pmp->rchain->refs == 1);
620 hammer2_chain_drop(pmp->rchain);
623 ccms_domain_uninit(&pmp->ccms_dom);
626 * Kill cluster controller
628 kdmsg_iocom_uninit(&pmp->iocom);
631 * If no PFS's left drop the master hammer2_mount for the device.
633 if (hmp->pmp_count == 0) {
635 hammer2_inode_drop(hmp->sroot);
639 #if REPORT_REFS_ERRORS
640 if (hmp->schain->refs != 1)
641 kprintf("HMP->SCHAIN %p REFS WRONG %d\n",
642 hmp->schain, hmp->schain->refs);
644 KKASSERT(hmp->schain->refs == 1);
646 hammer2_chain_drop(hmp->schain);
651 * Finish up with the device vnode
653 if ((devvp = hmp->devvp) != NULL) {
654 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
656 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
661 hammer2_mount_unlock(hmp);
664 * Final drop of embedded volume root chain to clean up
665 * vchain.core (vchain structure is not flagged ALLOCATED
666 * so it is cleaned out and then left).
668 hammer2_chain_drop(&hmp->vchain);
674 kmalloc_destroy(&pmp->mmsg);
676 kfree(pmp, M_HAMMER2);
677 if (hmp->pmp_count == 0) {
678 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
679 kmalloc_destroy(&hmp->minode);
680 kmalloc_destroy(&hmp->mchain);
681 kfree(hmp, M_HAMMER2);
683 lockmgr(&hammer2_mntlk, LK_RELEASE);
689 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
690 ino_t ino, struct vnode **vpp)
692 kprintf("hammer2_vget\n");
698 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
700 hammer2_pfsmount_t *pmp;
701 hammer2_mount_t *hmp;
707 hammer2_mount_exlock(hmp);
708 if (pmp->iroot == NULL) {
712 hammer2_inode_lock_sh(pmp->iroot);
713 vp = hammer2_igetv(pmp->iroot, &error);
714 hammer2_inode_unlock_sh(pmp->iroot);
717 kprintf("vnodefail\n");
719 hammer2_mount_unlock(hmp);
727 * XXX incorporate ipdata->inode_quota and data_quota
731 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
733 hammer2_pfsmount_t *pmp;
734 hammer2_mount_t *hmp;
739 mp->mnt_stat.f_files = pmp->inode_count;
740 mp->mnt_stat.f_ffree = 0;
741 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
742 mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
743 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
744 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
752 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
754 hammer2_pfsmount_t *pmp;
755 hammer2_mount_t *hmp;
760 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
761 mp->mnt_vstat.f_files = pmp->inode_count;
762 mp->mnt_vstat.f_ffree = 0;
763 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
764 mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
765 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
766 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
768 *sbp = mp->mnt_vstat;
773 * Sync the entire filesystem; this is called from the filesystem syncer
774 * process periodically and whenever a user calls sync(1) on the hammer
777 * Currently is actually called from the syncer! \o/
779 * This task will have to snapshot the state of the dirty inode chain.
780 * From that, it will have to make sure all of the inodes on the dirty
781 * chain have IO initiated. We make sure that io is initiated for the root
784 * If waitfor is set, we wait for media to acknowledge the new rootblock.
786 * THINKS: side A vs side B, to have sync not stall all I/O?
790 hammer2_vfs_sync(struct mount *mp, int waitfor)
792 struct hammer2_sync_info info;
793 hammer2_mount_t *hmp;
801 if (waitfor & MNT_LAZY)
802 flags |= VMSC_ONEPASS;
804 hammer2_trans_init(&info.trans, hmp);
806 info.waitfor = MNT_NOWAIT;
807 vmntvnodescan(mp, flags | VMSC_NOWAIT,
809 hammer2_sync_scan2, &info);
810 if (info.error == 0 && (waitfor & MNT_WAIT)) {
811 info.waitfor = waitfor;
812 vmntvnodescan(mp, flags,
814 hammer2_sync_scan2, &info);
818 if (waitfor == MNT_WAIT) {
824 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
825 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
826 HAMMER2_CHAIN_SUBMODIFIED)) {
827 hammer2_chain_flush(&info.trans, &hmp->vchain);
829 hammer2_chain_unlock(&hmp->vchain);
830 hammer2_trans_done(&info.trans);
835 * We can't safely flush the volume header until we have
836 * flushed any device buffers which have built up.
839 if ((waitfor & MNT_LAZY) == 0) {
840 waitfor = MNT_NOWAIT;
841 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
842 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
843 vn_unlock(hmp->devvp);
846 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
847 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
848 vn_unlock(hmp->devvp);
851 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
852 * volume header needs synchronization via hmp->volsync.
854 * XXX synchronize the flag & data with only this flush XXX
856 if (error == 0 && (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
860 * Synchronize the disk before flushing the volume
864 bp->b_bio1.bio_offset = 0;
867 bp->b_cmd = BUF_CMD_FLUSH;
868 bp->b_bio1.bio_done = biodone_sync;
869 bp->b_bio1.bio_flags |= BIO_SYNC;
870 vn_strategy(hmp->devvp, &bp->b_bio1);
871 biowait(&bp->b_bio1, "h2vol");
875 * Then we can safely flush the version of the volume header
876 * synchronized by the flush code.
878 i = hmp->volhdrno + 1;
879 if (i >= HAMMER2_NUM_VOLHDRS)
881 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
882 hmp->volsync.volu_size) {
885 kprintf("sync volhdr %d %jd\n",
886 i, (intmax_t)hmp->volsync.volu_size);
887 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
888 HAMMER2_PBUFSIZE, 0, 0);
889 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_VOLUMESYNC);
890 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
900 * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
901 * won't flush on those flags. The syncer code above will do a
902 * general meta-data flush globally that will catch these flags.
905 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
910 if (vp->v_type == VNON || ip == NULL ||
911 ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
912 RB_EMPTY(&vp->v_rbdirty_tree))) {
919 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
921 struct hammer2_sync_info *info = data;
926 if (vp->v_type == VNON || vp->v_type == VBAD ||
927 ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
928 RB_EMPTY(&vp->v_rbdirty_tree))) {
931 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
939 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
946 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
947 struct fid *fhp, struct vnode **vpp)
954 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
955 int *exflagsp, struct ucred **credanonp)
961 * Support code for hammer2_mount(). Read, verify, and install the volume
962 * header into the HMP
964 * XXX read four volhdrs and use the one with the highest TID whos CRC
969 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
970 * nonexistant locations.
972 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
976 hammer2_install_volume_header(hammer2_mount_t *hmp)
978 hammer2_volume_data_t *vd;
980 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
992 * There are up to 4 copies of the volume header (syncs iterate
993 * between them so there is no single master). We don't trust the
994 * volu_size field so we don't know precisely how large the filesystem
995 * is, so depend on the OS to return an error if we go beyond the
996 * block device's EOF.
998 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
999 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1000 HAMMER2_VOLUME_BYTES, &bp);
1007 vd = (struct hammer2_volume_data *) bp->b_data;
1008 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
1009 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
1015 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
1016 /* XXX: Reversed-endianness filesystem */
1017 kprintf("hammer2: reverse-endian filesystem detected");
1023 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
1024 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
1025 HAMMER2_VOLUME_ICRC0_SIZE);
1026 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
1027 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
1028 HAMMER2_VOLUME_ICRC1_SIZE);
1029 if ((crc0 != crc) || (bcrc0 != bcrc)) {
1030 kprintf("hammer2 volume header crc "
1031 "mismatch copy #%d %08x/%08x\n",
1038 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
1047 hmp->volsync = hmp->voldata;
1049 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
1050 kprintf("hammer2: using volume header #%d\n",
1055 kprintf("hammer2: no valid volume headers found!\n");
1061 * Reconnect using the passed file pointer. The caller must ref the
1065 hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
1067 hammer2_inode_data_t *ipdata;
1071 * Closes old comm descriptor, kills threads, cleans up
1072 * states, then installs the new descriptor and creates
1075 kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
1078 * Setup LNK_CONN fields for autoinitiated state machine
1080 hammer2_inode_lock_ex(pmp->iroot);
1081 ipdata = &pmp->iroot->chain->data->ipdata;
1082 pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
1083 pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
1084 pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
1085 pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
1086 pmp->iocom.auto_lnk_conn.peer_type = pmp->hmp->voldata.peer_type;
1087 hammer2_inode_unlock_ex(pmp->iroot);
1090 * Filter adjustment. Clients do not need visibility into other
1091 * clients (otherwise millions of clients would present a serious
1092 * problem). The fs_label also serves to restrict the namespace.
1094 pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
1095 pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
1096 switch (ipdata->pfs_type) {
1097 case DMSG_PFSTYPE_CLIENT:
1098 pmp->iocom.auto_lnk_conn.peer_mask &=
1099 ~(1LLU << DMSG_PFSTYPE_CLIENT);
1105 name_len = ipdata->name_len;
1106 if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
1107 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
1108 bcopy(ipdata->filename,
1109 pmp->iocom.auto_lnk_conn.fs_label,
1111 pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
1114 * Setup LNK_SPAN fields for autoinitiated state machine
1116 pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
1117 pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
1118 pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
1119 pmp->iocom.auto_lnk_span.peer_type = pmp->hmp->voldata.peer_type;
1120 pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
1121 name_len = ipdata->name_len;
1122 if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
1123 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
1124 bcopy(ipdata->filename,
1125 pmp->iocom.auto_lnk_span.fs_label,
1127 pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
1129 kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
1133 hammer2_rcvdmsg(kdmsg_msg_t *msg)
1135 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
1136 case DMSG_DBG_SHELL:
1139 * Execute shell command (not supported atm)
1141 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1143 case DMSG_DBG_SHELL | DMSGF_REPLY:
1147 if (msg->aux_data) {
1148 msg->aux_data[msg->aux_size - 1] = 0;
1149 kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
1154 * Unsupported message received. We only need to
1155 * reply if it's a transaction in order to close our end.
1156 * Ignore any one-way messages are any further messages
1157 * associated with the transaction.
1159 * NOTE: This case also includes DMSG_LNK_ERROR messages
1160 * which might be one-way, replying to those would
1161 * cause an infinite ping-pong.
1163 if (msg->any.head.cmd & DMSGF_CREATE)
1164 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1171 * This function is called after KDMSG has automatically handled processing
1172 * of a LNK layer message (typically CONN, SPAN, or CIRC).
1174 * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
1175 * advertises all available hammer2 super-root volumes.
1178 hammer2_autodmsg(kdmsg_msg_t *msg)
1180 hammer2_pfsmount_t *pmp = msg->iocom->handle;
1181 hammer2_mount_t *hmp = pmp->hmp;
1185 * We only care about replies to our LNK_CONN auto-request. kdmsg
1186 * has already processed the reply, we use this calback as a shim
1187 * to know when we can advertise available super-root volumes.
1189 if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
1190 (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
1191 msg->state == NULL) {
1195 kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
1197 if (msg->any.head.cmd & DMSGF_CREATE) {
1198 kprintf("HAMMER2: VOLDATA DUMP\n");
1201 * Dump the configuration stored in the volume header
1203 hammer2_voldata_lock(hmp);
1204 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
1205 if (hmp->voldata.copyinfo[copyid].copyid == 0)
1207 hammer2_volconf_update(pmp, copyid);
1209 hammer2_voldata_unlock(hmp, 0);
1211 if ((msg->any.head.cmd & DMSGF_DELETE) &&
1212 msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
1213 kprintf("HAMMER2: CONN WAS TERMINATED\n");
1218 * Volume configuration updates are passed onto the userland service
1219 * daemon via the open LNK_CONN transaction.
1222 hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
1224 hammer2_mount_t *hmp = pmp->hmp;
1227 /* XXX interlock against connection state termination */
1228 kprintf("volconf update %p\n", pmp->iocom.conn_state);
1229 if (pmp->iocom.conn_state) {
1230 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
1231 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
1232 DMSG_LNK_VOLCONF, NULL, NULL);
1233 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
1234 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
1235 msg->any.lnk_volconf.index = index;
1236 kdmsg_msg_write(msg);