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>
45 #include <sys/socket.h>
48 #include "hammer2_disk.h"
49 #include "hammer2_mount.h"
50 #include "hammer2_network.h"
52 struct hammer2_sync_info {
57 TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
58 static struct hammer2_mntlist hammer2_mntlist;
59 static struct lock hammer2_mntlk;
62 int hammer2_cluster_enable = 1;
63 int hammer2_hardlink_enable = 1;
64 long hammer2_iod_file_read;
65 long hammer2_iod_meta_read;
66 long hammer2_iod_indr_read;
67 long hammer2_iod_file_write;
68 long hammer2_iod_meta_write;
69 long hammer2_iod_indr_write;
70 long hammer2_iod_volu_write;
71 long hammer2_ioa_file_read;
72 long hammer2_ioa_meta_read;
73 long hammer2_ioa_indr_read;
74 long hammer2_ioa_file_write;
75 long hammer2_ioa_meta_write;
76 long hammer2_ioa_indr_write;
77 long hammer2_ioa_volu_write;
79 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
81 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
82 &hammer2_debug, 0, "");
83 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
84 &hammer2_cluster_enable, 0, "");
85 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
86 &hammer2_hardlink_enable, 0, "");
87 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
88 &hammer2_iod_file_read, 0, "");
89 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
90 &hammer2_iod_meta_read, 0, "");
91 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
92 &hammer2_iod_indr_read, 0, "");
93 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
94 &hammer2_iod_file_write, 0, "");
95 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
96 &hammer2_iod_meta_write, 0, "");
97 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
98 &hammer2_iod_indr_write, 0, "");
99 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
100 &hammer2_iod_volu_write, 0, "");
101 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
102 &hammer2_ioa_file_read, 0, "");
103 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
104 &hammer2_ioa_meta_read, 0, "");
105 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
106 &hammer2_ioa_indr_read, 0, "");
107 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
108 &hammer2_ioa_file_write, 0, "");
109 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
110 &hammer2_ioa_meta_write, 0, "");
111 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
112 &hammer2_ioa_indr_write, 0, "");
113 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
114 &hammer2_ioa_volu_write, 0, "");
116 static int hammer2_vfs_init(struct vfsconf *conf);
117 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
119 static int hammer2_remount(struct mount *, char *, struct vnode *,
121 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
122 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
123 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
125 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
127 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
128 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
129 ino_t ino, struct vnode **vpp);
130 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
131 struct fid *fhp, struct vnode **vpp);
132 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
133 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
134 int *exflagsp, struct ucred **credanonp);
136 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
137 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
138 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
140 static void hammer2_cluster_thread_rd(void *arg);
141 static void hammer2_cluster_thread_wr(void *arg);
142 static int hammer2_msg_conn_reply(hammer2_state_t *state, hammer2_msg_t *msg);
143 static int hammer2_msg_span_reply(hammer2_state_t *state, hammer2_msg_t *msg);
144 static int hammer2_msg_lnk_rcvmsg(hammer2_msg_t *msg);
147 * HAMMER2 vfs operations.
149 static struct vfsops hammer2_vfsops = {
150 .vfs_init = hammer2_vfs_init,
151 .vfs_sync = hammer2_vfs_sync,
152 .vfs_mount = hammer2_vfs_mount,
153 .vfs_unmount = hammer2_vfs_unmount,
154 .vfs_root = hammer2_vfs_root,
155 .vfs_statfs = hammer2_vfs_statfs,
156 .vfs_statvfs = hammer2_vfs_statvfs,
157 .vfs_vget = hammer2_vfs_vget,
158 .vfs_vptofh = hammer2_vfs_vptofh,
159 .vfs_fhtovp = hammer2_vfs_fhtovp,
160 .vfs_checkexp = hammer2_vfs_checkexp
163 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
165 VFS_SET(hammer2_vfsops, hammer2, 0);
166 MODULE_VERSION(hammer2, 1);
170 hammer2_vfs_init(struct vfsconf *conf)
176 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
178 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
180 if (HAMMER2_ALLOCREF_BYTES != sizeof(struct hammer2_allocref))
182 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
186 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
188 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
189 TAILQ_INIT(&hammer2_mntlist);
195 * Mount or remount HAMMER2 fileystem from physical media
198 * mp mount point structure
204 * mp mount point structure
205 * path path to mount point
206 * data pointer to argument structure in user space
207 * volume volume path (device@LABEL form)
208 * hflags user mount flags
209 * cred user credentials
216 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
219 struct hammer2_mount_info info;
220 hammer2_pfsmount_t *pmp;
221 hammer2_mount_t *hmp;
224 struct nlookupdata nd;
225 hammer2_chain_t *parent;
226 hammer2_chain_t *schain;
227 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;
357 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
358 lockinit(&pmp->msglk, "h2msg", 0, 0);
359 TAILQ_INIT(&pmp->msgq);
360 RB_INIT(&pmp->staterd_tree);
361 RB_INIT(&pmp->statewr_tree);
364 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
367 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
368 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
369 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
371 ccms_domain_init(&pmp->ccms_dom);
373 pmp->router.pmp = pmp;
375 lockmgr(&hammer2_mntlk, LK_RELEASE);
376 kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
378 mp->mnt_flag = MNT_LOCAL;
379 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
383 * vchain setup. vchain.data is special cased to NULL.
384 * vchain.refs is initialized and will never drop to 0.
386 hmp->vchain.refs = 1;
387 hmp->vchain.data = (void *)&hmp->voldata;
388 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
389 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
390 hmp->vchain.bref_flush = hmp->vchain.bref;
391 ccms_cst_init(&hmp->vchain.cst, NULL);
392 /* hmp->vchain.u.xxx is left NULL */
393 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
394 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
397 * Install the volume header
399 error = hammer2_install_volume_header(hmp);
401 hammer2_vfs_unmount(mp, MNT_FORCE);
407 * required mount structure initializations
409 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
410 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
412 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
413 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
418 mp->mnt_iosize_max = MAXPHYS;
421 * First locate the super-root inode, which is key 0 relative to the
422 * volume header's blockset.
424 * Then locate the root inode by scanning the directory keyspace
425 * represented by the label.
428 parent = &hmp->vchain;
429 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
430 schain = hammer2_chain_lookup(hmp, &parent,
431 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
432 hammer2_chain_unlock(hmp, parent);
433 if (schain == NULL) {
434 kprintf("hammer2_mount: invalid super-root\n");
435 hammer2_vfs_unmount(mp, MNT_FORCE);
438 hammer2_chain_ref(hmp, schain); /* for hmp->schain */
439 hmp->schain = schain; /* left locked */
441 schain = hmp->schain;
442 hammer2_chain_lock(hmp, schain, HAMMER2_RESOLVE_ALWAYS);
446 lhc = hammer2_dirhash(label, strlen(label));
447 rchain = hammer2_chain_lookup(hmp, &parent,
448 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
451 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
453 strcmp(label, rchain->data->ipdata.filename) == 0) {
456 rchain = hammer2_chain_next(hmp, &parent, rchain,
457 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
460 hammer2_chain_unlock(hmp, parent);
461 if (rchain == NULL) {
462 kprintf("hammer2_mount: PFS label not found\n");
463 hammer2_vfs_unmount(mp, MNT_FORCE);
466 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
467 hammer2_chain_unlock(hmp, rchain);
468 kprintf("hammer2_mount: PFS label already mounted!\n");
469 hammer2_vfs_unmount(mp, MNT_FORCE);
472 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
474 hammer2_chain_ref(hmp, rchain); /* for pmp->rchain */
475 hammer2_chain_unlock(hmp, rchain);
476 pmp->rchain = rchain; /* left held & unlocked */
477 pmp->iroot = rchain->u.ip; /* implied hold from rchain */
478 pmp->iroot->pmp = pmp;
480 kprintf("iroot %p\n", pmp->iroot);
483 * Ref the cluster management messaging descriptor. The mount
484 * program deals with the other end of the communications pipe.
486 pmp->msg_fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
487 if (pmp->msg_fp == NULL) {
488 kprintf("hammer2_mount: bad cluster_fd!\n");
489 hammer2_vfs_unmount(mp, MNT_FORCE);
492 lwkt_create(hammer2_cluster_thread_rd, pmp, &pmp->msgrd_td,
493 NULL, 0, -1, "hammer2-msgrd");
494 lwkt_create(hammer2_cluster_thread_wr, pmp, &pmp->msgwr_td,
495 NULL, 0, -1, "hammer2-msgwr");
501 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
502 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
503 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
505 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
506 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
507 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
508 copyinstr(path, mp->mnt_stat.f_mntonname,
509 sizeof(mp->mnt_stat.f_mntonname) - 1,
513 * Initial statfs to prime mnt_stat.
515 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
522 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
530 hammer2_vfs_unmount(struct mount *mp, int mntflags)
532 hammer2_pfsmount_t *pmp;
533 hammer2_mount_t *hmp;
536 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
543 if (mntflags & MNT_FORCE)
546 hammer2_mount_exlock(hmp);
549 * If mount initialization proceeded far enough we must flush
553 error = vflush(mp, 0, flags);
558 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
560 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
563 * Flush any left over chains. The voldata lock is only used
564 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
566 hammer2_voldata_lock(hmp);
567 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
568 HAMMER2_CHAIN_MODIFIED_AUX |
569 HAMMER2_CHAIN_SUBMODIFIED)) {
570 hammer2_voldata_unlock(hmp);
571 hammer2_vfs_sync(mp, MNT_WAIT);
573 hammer2_voldata_unlock(hmp);
575 if (hmp->pmp_count == 0) {
576 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
577 HAMMER2_CHAIN_MODIFIED_AUX |
578 HAMMER2_CHAIN_SUBMODIFIED)) {
579 kprintf("hammer2_unmount: chains left over after "
581 if (hammer2_debug & 0x0010)
582 Debugger("entered debugger");
587 * Cleanup the root and super-root chain elements (which should be
592 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
593 KKASSERT(pmp->rchain->refs == 1);
594 hammer2_chain_drop(hmp, pmp->rchain);
597 ccms_domain_uninit(&pmp->ccms_dom);
600 * Ask the cluster controller to go away
602 atomic_set_int(&pmp->msg_ctl, HAMMER2_CLUSTERCTL_KILL);
603 while (pmp->msgrd_td || pmp->msgwr_td) {
604 wakeup(&pmp->msg_ctl);
605 tsleep(pmp, 0, "clstrkl", hz);
609 * Drop communications descriptor
617 * If no PFS's left drop the master hammer2_mount for the device.
619 if (hmp->pmp_count == 0) {
621 KKASSERT(hmp->schain->refs == 1);
622 hammer2_chain_drop(hmp, hmp->schain);
627 * Finish up with the device vnode
629 if ((devvp = hmp->devvp) != NULL) {
630 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
632 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
637 hammer2_mount_unlock(hmp);
643 kmalloc_destroy(&pmp->mmsg);
645 kfree(pmp, M_HAMMER2);
646 if (hmp->pmp_count == 0) {
647 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
648 kmalloc_destroy(&hmp->minode);
649 kmalloc_destroy(&hmp->mchain);
650 kfree(hmp, M_HAMMER2);
652 lockmgr(&hammer2_mntlk, LK_RELEASE);
658 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
659 ino_t ino, struct vnode **vpp)
661 kprintf("hammer2_vget\n");
667 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
669 hammer2_pfsmount_t *pmp;
670 hammer2_mount_t *hmp;
676 hammer2_mount_exlock(hmp);
677 if (pmp->iroot == NULL) {
681 hammer2_chain_lock(hmp, &pmp->iroot->chain,
682 HAMMER2_RESOLVE_ALWAYS |
683 HAMMER2_RESOLVE_SHARED);
684 vp = hammer2_igetv(pmp->iroot, &error);
685 hammer2_chain_unlock(hmp, &pmp->iroot->chain);
688 kprintf("vnodefail\n");
690 hammer2_mount_unlock(hmp);
698 * XXX incorporate pmp->iroot->ip_data.inode_quota and data_quota
702 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
704 hammer2_pfsmount_t *pmp;
705 hammer2_mount_t *hmp;
710 mp->mnt_stat.f_files = pmp->iroot->ip_data.inode_count +
711 pmp->iroot->delta_icount;
712 mp->mnt_stat.f_ffree = 0;
713 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
714 mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
715 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
716 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
724 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
726 hammer2_pfsmount_t *pmp;
727 hammer2_mount_t *hmp;
732 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
733 mp->mnt_vstat.f_files = pmp->iroot->ip_data.inode_count +
734 pmp->iroot->delta_icount;
735 mp->mnt_vstat.f_ffree = 0;
736 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
737 mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
738 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
739 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
741 *sbp = mp->mnt_vstat;
746 * Sync the entire filesystem; this is called from the filesystem syncer
747 * process periodically and whenever a user calls sync(1) on the hammer
750 * Currently is actually called from the syncer! \o/
752 * This task will have to snapshot the state of the dirty inode chain.
753 * From that, it will have to make sure all of the inodes on the dirty
754 * chain have IO initiated. We make sure that io is initiated for the root
757 * If waitfor is set, we wait for media to acknowledge the new rootblock.
759 * THINKS: side A vs side B, to have sync not stall all I/O?
763 hammer2_vfs_sync(struct mount *mp, int waitfor)
765 struct hammer2_sync_info info;
766 hammer2_mount_t *hmp;
774 if (waitfor & MNT_LAZY)
775 flags |= VMSC_ONEPASS;
778 info.waitfor = MNT_NOWAIT;
779 vmntvnodescan(mp, flags | VMSC_NOWAIT,
781 hammer2_sync_scan2, &info);
782 if (info.error == 0 && (waitfor & MNT_WAIT)) {
783 info.waitfor = waitfor;
784 vmntvnodescan(mp, flags,
786 hammer2_sync_scan2, &info);
790 if (waitfor == MNT_WAIT) {
796 hammer2_chain_lock(hmp, &hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
797 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
798 HAMMER2_CHAIN_MODIFIED_AUX |
799 HAMMER2_CHAIN_SUBMODIFIED)) {
800 hammer2_chain_flush(hmp, &hmp->vchain, 0);
805 hammer2_chain_unlock(hmp, &hmp->vchain);
809 if ((waitfor & MNT_LAZY) == 0) {
810 waitfor = MNT_NOWAIT;
811 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
812 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
813 vn_unlock(hmp->devvp);
816 if (error == 0 && haswork) {
820 * Synchronize the disk before flushing the volume
824 bp->b_bio1.bio_offset = 0;
827 bp->b_cmd = BUF_CMD_FLUSH;
828 bp->b_bio1.bio_done = biodone_sync;
829 bp->b_bio1.bio_flags |= BIO_SYNC;
830 vn_strategy(hmp->devvp, &bp->b_bio1);
831 biowait(&bp->b_bio1, "h2vol");
835 * Then we can safely flush the volume header. Volume
836 * data is locked separately to prevent ioctl functions
837 * from deadlocking due to a configuration issue.
839 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
840 hammer2_voldata_lock(hmp);
841 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
842 hammer2_voldata_unlock(hmp);
851 * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
852 * won't flush on those flags. The syncer code above will do a
853 * general meta-data flush globally that will catch these flags.
856 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
861 if (vp->v_type == VNON || ip == NULL ||
862 ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
863 HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
864 RB_EMPTY(&vp->v_rbdirty_tree))) {
871 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
873 struct hammer2_sync_info *info = data;
878 if (vp->v_type == VNON || vp->v_type == VBAD ||
879 ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
880 HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
881 RB_EMPTY(&vp->v_rbdirty_tree))) {
884 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
892 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
899 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
900 struct fid *fhp, struct vnode **vpp)
907 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
908 int *exflagsp, struct ucred **credanonp)
914 * Support code for hammer2_mount(). Read, verify, and install the volume
915 * header into the HMP
917 * XXX read four volhdrs and use the one with the highest TID whos CRC
922 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
923 * nonexistant locations.
925 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
929 hammer2_install_volume_header(hammer2_mount_t *hmp)
931 hammer2_volume_data_t *vd;
933 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
945 * There are up to 4 copies of the volume header (syncs iterate
946 * between them so there is no single master). We don't trust the
947 * volu_size field so we don't know precisely how large the filesystem
948 * is, so depend on the OS to return an error if we go beyond the
949 * block device's EOF.
951 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
952 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
953 HAMMER2_VOLUME_BYTES, &bp);
960 vd = (struct hammer2_volume_data *) bp->b_data;
961 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
962 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
968 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
969 /* XXX: Reversed-endianness filesystem */
970 kprintf("hammer2: reverse-endian filesystem detected");
976 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
977 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
978 HAMMER2_VOLUME_ICRC0_SIZE);
979 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
980 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
981 HAMMER2_VOLUME_ICRC1_SIZE);
982 if ((crc0 != crc) || (bcrc0 != bcrc)) {
983 kprintf("hammer2 volume header crc "
984 "mismatch copy #%d\t%08x %08x",
991 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
1001 kprintf("hammer2: a valid volume header was found\n");
1004 kprintf("hammer2: no valid volume headers found!\n");
1010 * Cluster controller thread. Perform messaging functions. We have one
1011 * thread for the reader and one for the writer. The writer handles
1012 * shutdown requests (which should break the reader thread).
1016 hammer2_cluster_thread_rd(void *arg)
1018 hammer2_pfsmount_t *pmp = arg;
1019 hammer2_msg_hdr_t hdr;
1021 hammer2_state_t *state;
1025 while ((pmp->msg_ctl & HAMMER2_CLUSTERCTL_KILL) == 0) {
1027 * Retrieve the message from the pipe or socket.
1029 error = fp_read(pmp->msg_fp, &hdr, sizeof(hdr),
1030 NULL, 1, UIO_SYSSPACE);
1033 if (hdr.magic != HAMMER2_MSGHDR_MAGIC) {
1034 kprintf("hammer2: msgrd: bad magic: %04x\n",
1039 hbytes = (hdr.cmd & HAMMER2_MSGF_SIZE) * HAMMER2_MSG_ALIGN;
1040 if (hbytes < sizeof(hdr) || hbytes > HAMMER2_MSGAUX_MAX) {
1041 kprintf("hammer2: msgrd: bad header size %zd\n",
1046 msg = hammer2_msg_alloc(&pmp->router, hdr.cmd);
1047 msg->any.head = hdr;
1048 msg->hdr_size = hbytes;
1049 if (hbytes > sizeof(hdr)) {
1050 error = fp_read(pmp->msg_fp, &msg->any.head + 1,
1051 hbytes - sizeof(hdr),
1052 NULL, 1, UIO_SYSSPACE);
1054 kprintf("hammer2: short msg received\n");
1059 msg->aux_size = hdr.aux_bytes * HAMMER2_MSG_ALIGN;
1060 if (msg->aux_size > HAMMER2_MSGAUX_MAX) {
1061 kprintf("hammer2: illegal msg payload size %zd\n",
1066 if (msg->aux_size) {
1067 msg->aux_data = kmalloc(msg->aux_size, pmp->mmsg,
1069 error = fp_read(pmp->msg_fp, msg->aux_data,
1071 NULL, 1, UIO_SYSSPACE);
1073 kprintf("hammer2: short msg "
1074 "payload received\n");
1080 * State machine tracking, state assignment for msg,
1081 * returns error and discard status. Errors are fatal
1082 * to the connection except for EALREADY which forces
1083 * a discard without execution.
1085 error = hammer2_state_msgrx(msg);
1088 * Raw protocol or connection error
1090 hammer2_msg_free(msg);
1091 if (error == EALREADY)
1093 } else if (msg->state && msg->state->func) {
1095 * Message related to state which already has a
1096 * handling function installed for it.
1098 error = msg->state->func(msg->state, msg);
1099 hammer2_state_cleanuprx(msg);
1100 } else if ((msg->any.head.cmd & HAMMER2_MSGF_PROTOS) ==
1101 HAMMER2_MSG_PROTO_LNK) {
1103 * Message related to the LNK protocol set
1105 error = hammer2_msg_lnk_rcvmsg(msg);
1106 hammer2_state_cleanuprx(msg);
1107 } else if ((msg->any.head.cmd & HAMMER2_MSGF_PROTOS) ==
1108 HAMMER2_MSG_PROTO_DBG) {
1110 * Message related to the DBG protocol set
1112 error = hammer2_msg_dbg_rcvmsg(msg);
1113 hammer2_state_cleanuprx(msg);
1116 * Other higher-level messages (e.g. vnops)
1118 error = hammer2_msg_adhoc_input(msg);
1119 hammer2_state_cleanuprx(msg);
1125 kprintf("hammer2: msg read failed error %d\n", error);
1127 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1129 if (msg->state && msg->state->msg == msg)
1130 msg->state->msg = NULL;
1131 hammer2_msg_free(msg);
1134 if ((state = pmp->freerd_state) != NULL) {
1135 pmp->freerd_state = NULL;
1136 hammer2_state_free(state);
1139 while ((state = RB_ROOT(&pmp->staterd_tree)) != NULL) {
1140 RB_REMOVE(hammer2_state_tree, &pmp->staterd_tree, state);
1141 hammer2_state_free(state);
1143 lockmgr(&pmp->msglk, LK_RELEASE);
1145 fp_shutdown(pmp->msg_fp, SHUT_RDWR);
1146 pmp->msgrd_td = NULL;
1147 /* pmp can be ripped out from under us at this point */
1154 hammer2_cluster_thread_wr(void *arg)
1156 hammer2_pfsmount_t *pmp = arg;
1157 hammer2_msg_t *msg = NULL;
1158 hammer2_state_t *state;
1164 * Open a LNK_CONN transaction indicating that we want to take part
1165 * in the spanning tree algorithm. Filter explicitly on the PFS
1166 * info in the iroot.
1168 * We do not transmit our (only) LNK_SPAN until the other end has
1169 * acknowledged our link connection request.
1171 * The transaction remains fully open for the duration of the
1174 msg = hammer2_msg_alloc(&pmp->router, HAMMER2_LNK_CONN |
1175 HAMMER2_MSGF_CREATE);
1176 msg->any.lnk_conn.pfs_clid = pmp->iroot->ip_data.pfs_clid;
1177 msg->any.lnk_conn.pfs_fsid = pmp->iroot->ip_data.pfs_fsid;
1178 msg->any.lnk_conn.pfs_type = pmp->iroot->ip_data.pfs_type;
1179 msg->any.lnk_conn.proto_version = HAMMER2_SPAN_PROTO_1;
1180 name_len = pmp->iroot->ip_data.name_len;
1181 if (name_len >= sizeof(msg->any.lnk_conn.label))
1182 name_len = sizeof(msg->any.lnk_conn.label) - 1;
1183 bcopy(pmp->iroot->ip_data.filename, msg->any.lnk_conn.label, name_len);
1184 msg->any.lnk_conn.label[name_len] = 0;
1185 hammer2_msg_write(msg, hammer2_msg_conn_reply, pmp);
1191 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1193 while ((pmp->msg_ctl & HAMMER2_CLUSTERCTL_KILL) == 0 && error == 0) {
1194 lksleep(&pmp->msg_ctl, &pmp->msglk, 0, "msgwr", hz);
1195 while ((msg = TAILQ_FIRST(&pmp->msgq)) != NULL) {
1197 * Remove msg from the transmit queue and do
1198 * persist and half-closed state handling.
1200 TAILQ_REMOVE(&pmp->msgq, msg, qentry);
1201 lockmgr(&pmp->msglk, LK_RELEASE);
1203 error = hammer2_state_msgtx(msg);
1204 if (error == EALREADY) {
1206 hammer2_msg_free(msg);
1207 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1211 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1216 * Dump the message to the pipe or socket.
1218 error = fp_write(pmp->msg_fp, &msg->any, msg->hdr_size,
1219 &res, UIO_SYSSPACE);
1220 if (error || res != msg->hdr_size) {
1223 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1226 if (msg->aux_size) {
1227 error = fp_write(pmp->msg_fp,
1228 msg->aux_data, msg->aux_size,
1229 &res, UIO_SYSSPACE);
1230 if (error || res != msg->aux_size) {
1233 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1237 hammer2_state_cleanuptx(msg);
1238 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1243 * Cleanup messages pending transmission and release msgq lock.
1246 kprintf("hammer2: msg write failed error %d\n", error);
1249 if (msg->state && msg->state->msg == msg)
1250 msg->state->msg = NULL;
1251 hammer2_msg_free(msg);
1254 while ((msg = TAILQ_FIRST(&pmp->msgq)) != NULL) {
1255 TAILQ_REMOVE(&pmp->msgq, msg, qentry);
1256 if (msg->state && msg->state->msg == msg)
1257 msg->state->msg = NULL;
1258 hammer2_msg_free(msg);
1261 if ((state = pmp->freewr_state) != NULL) {
1262 pmp->freewr_state = NULL;
1263 hammer2_state_free(state);
1266 while ((state = RB_ROOT(&pmp->statewr_tree)) != NULL) {
1267 RB_REMOVE(hammer2_state_tree, &pmp->statewr_tree, state);
1268 hammer2_state_free(state);
1270 lockmgr(&pmp->msglk, LK_RELEASE);
1273 * Cleanup descriptor, be sure the read size is shutdown so the
1274 * (probably blocked) read operations returns an error.
1276 * pmp can be ripped out from under us once msgwr_td is set to NULL.
1278 fp_shutdown(pmp->msg_fp, SHUT_RDWR);
1279 pmp->msgwr_td = NULL;
1285 hammer2_msg_lnk_rcvmsg(hammer2_msg_t *msg)
1287 switch(msg->any.head.cmd & HAMMER2_MSGF_TRANSMASK) {
1288 case HAMMER2_LNK_CONN | HAMMER2_MSGF_CREATE:
1290 * reply & leave trans open
1292 kprintf("CONN RECEIVE - (just ignore it)\n");
1293 hammer2_msg_result(msg, 0);
1295 case HAMMER2_LNK_SPAN | HAMMER2_MSGF_CREATE:
1296 kprintf("SPAN RECEIVE - ADDED FROM CLUSTER\n");
1298 case HAMMER2_LNK_SPAN | HAMMER2_MSGF_DELETE:
1299 kprintf("SPAN RECEIVE - DELETED FROM CLUSTER\n");
1308 * This function is called when the other end replies to our LNK_CONN
1311 * We transmit our (single) SPAN on the initial reply, leaving that
1312 * transaction open too.
1315 hammer2_msg_conn_reply(hammer2_state_t *state, hammer2_msg_t *msg)
1317 hammer2_pfsmount_t *pmp = state->any.pmp;
1320 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE) {
1321 kprintf("LNK_CONN transaction replied to, initiate SPAN\n");
1322 msg = hammer2_msg_alloc(&pmp->router, HAMMER2_LNK_SPAN |
1323 HAMMER2_MSGF_CREATE);
1324 msg->any.lnk_span.pfs_clid = pmp->iroot->ip_data.pfs_clid;
1325 msg->any.lnk_span.pfs_fsid = pmp->iroot->ip_data.pfs_fsid;
1326 msg->any.lnk_span.pfs_type = pmp->iroot->ip_data.pfs_type;
1327 msg->any.lnk_span.proto_version = HAMMER2_SPAN_PROTO_1;
1328 name_len = pmp->iroot->ip_data.name_len;
1329 if (name_len >= sizeof(msg->any.lnk_span.label))
1330 name_len = sizeof(msg->any.lnk_span.label) - 1;
1331 bcopy(pmp->iroot->ip_data.filename,
1332 msg->any.lnk_span.label,
1334 msg->any.lnk_span.label[name_len] = 0;
1335 hammer2_msg_write(msg, hammer2_msg_span_reply, pmp);
1337 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
1338 kprintf("LNK_CONN transaction terminated by remote\n");
1339 hammer2_msg_reply(msg, 0);
1345 hammer2_msg_span_reply(hammer2_state_t *state, hammer2_msg_t *msg)
1347 hammer2_pfsmount_t *pmp = state->any.pmp;
1349 kprintf("SPAN REPLY - Our span was terminated? %p\n", pmp);