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);
144 * HAMMER2 vfs operations.
146 static struct vfsops hammer2_vfsops = {
147 .vfs_init = hammer2_vfs_init,
148 .vfs_sync = hammer2_vfs_sync,
149 .vfs_mount = hammer2_vfs_mount,
150 .vfs_unmount = hammer2_vfs_unmount,
151 .vfs_root = hammer2_vfs_root,
152 .vfs_statfs = hammer2_vfs_statfs,
153 .vfs_statvfs = hammer2_vfs_statvfs,
154 .vfs_vget = hammer2_vfs_vget,
155 .vfs_vptofh = hammer2_vfs_vptofh,
156 .vfs_fhtovp = hammer2_vfs_fhtovp,
157 .vfs_checkexp = hammer2_vfs_checkexp
160 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
162 VFS_SET(hammer2_vfsops, hammer2, 0);
163 MODULE_VERSION(hammer2, 1);
167 hammer2_vfs_init(struct vfsconf *conf)
173 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
175 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
177 if (HAMMER2_ALLOCREF_BYTES != sizeof(struct hammer2_allocref))
179 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
183 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
185 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
186 TAILQ_INIT(&hammer2_mntlist);
192 * Mount or remount HAMMER2 fileystem from physical media
195 * mp mount point structure
201 * mp mount point structure
202 * path path to mount point
203 * data pointer to argument structure in user space
204 * volume volume path (device@LABEL form)
205 * hflags user mount flags
206 * cred user credentials
213 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
216 struct hammer2_mount_info info;
217 hammer2_pfsmount_t *pmp;
218 hammer2_mount_t *hmp;
221 struct nlookupdata nd;
222 hammer2_chain_t *parent;
223 hammer2_chain_t *schain;
224 hammer2_chain_t *rchain;
225 char devstr[MNAMELEN];
240 kprintf("hammer2_mount\n");
246 bzero(&info, sizeof(info));
247 info.cluster_fd = -1;
251 * Non-root mount or updating a mount
253 error = copyin(data, &info, sizeof(info));
257 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
261 /* Extract device and label */
263 label = strchr(devstr, '@');
265 ((label + 1) - dev) > done) {
273 if (mp->mnt_flag & MNT_UPDATE) {
275 /* HAMMER2 implements NFS export via mountctl */
278 error = hammer2_remount(mp, path, devvp, cred);
286 * Lookup name and verify it refers to a block device.
288 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
290 error = nlookup(&nd);
292 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
296 if (vn_isdisk(devvp, &error))
297 error = vfs_mountedon(devvp);
301 * Determine if the device has already been mounted. After this
302 * check hmp will be non-NULL if we are doing the second or more
303 * hammer2 mounts from the same device.
305 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
306 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
307 if (hmp->devvp == devvp)
312 * Open the device if this isn't a secondary mount
318 if (error == 0 && vcount(devvp) > 0)
322 * Now open the device
325 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
326 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
327 error = vinvalbuf(devvp, V_SAVE, 0, 0);
329 error = VOP_OPEN(devvp,
330 ronly ? FREAD : FREAD | FWRITE,
335 if (error && devvp) {
340 lockmgr(&hammer2_mntlk, LK_RELEASE);
346 * Block device opened successfully, finish initializing the
349 * From this point on we have to call hammer2_unmount() on failure.
351 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
352 mp->mnt_data = (qaddr_t)pmp;
354 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
355 lockinit(&pmp->msglk, "h2msg", 0, 0);
356 TAILQ_INIT(&pmp->msgq);
357 RB_INIT(&pmp->staterd_tree);
358 RB_INIT(&pmp->statewr_tree);
361 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
364 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
365 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
366 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
368 ccms_domain_init(&pmp->ccms_dom);
371 lockmgr(&hammer2_mntlk, LK_RELEASE);
372 kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
374 mp->mnt_flag = MNT_LOCAL;
375 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
379 * vchain setup. vchain.data is special cased to NULL.
380 * vchain.refs is initialized and will never drop to 0.
382 hmp->vchain.refs = 1;
383 hmp->vchain.data = (void *)&hmp->voldata;
384 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
385 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
386 hmp->vchain.bref_flush = hmp->vchain.bref;
387 ccms_cst_init(&hmp->vchain.cst, NULL);
388 /* hmp->vchain.u.xxx is left NULL */
389 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
390 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
393 * Install the volume header
395 error = hammer2_install_volume_header(hmp);
397 hammer2_vfs_unmount(mp, MNT_FORCE);
403 * required mount structure initializations
405 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
406 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
408 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
409 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
414 mp->mnt_iosize_max = MAXPHYS;
417 * First locate the super-root inode, which is key 0 relative to the
418 * volume header's blockset.
420 * Then locate the root inode by scanning the directory keyspace
421 * represented by the label.
424 parent = &hmp->vchain;
425 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
426 schain = hammer2_chain_lookup(hmp, &parent,
427 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
428 hammer2_chain_unlock(hmp, parent);
429 if (schain == NULL) {
430 kprintf("hammer2_mount: invalid super-root\n");
431 hammer2_vfs_unmount(mp, MNT_FORCE);
434 hammer2_chain_ref(hmp, schain); /* for hmp->schain */
435 hmp->schain = schain; /* left locked */
437 schain = hmp->schain;
438 hammer2_chain_lock(hmp, schain, HAMMER2_RESOLVE_ALWAYS);
442 lhc = hammer2_dirhash(label, strlen(label));
443 rchain = hammer2_chain_lookup(hmp, &parent,
444 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
447 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
449 strcmp(label, rchain->data->ipdata.filename) == 0) {
452 rchain = hammer2_chain_next(hmp, &parent, rchain,
453 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
456 hammer2_chain_unlock(hmp, parent);
457 if (rchain == NULL) {
458 kprintf("hammer2_mount: PFS label not found\n");
459 hammer2_vfs_unmount(mp, MNT_FORCE);
462 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
463 hammer2_chain_unlock(hmp, rchain);
464 kprintf("hammer2_mount: PFS label already mounted!\n");
465 hammer2_vfs_unmount(mp, MNT_FORCE);
468 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
470 hammer2_chain_ref(hmp, rchain); /* for pmp->rchain */
471 hammer2_chain_unlock(hmp, rchain);
472 pmp->rchain = rchain; /* left held & unlocked */
473 pmp->iroot = rchain->u.ip; /* implied hold from rchain */
474 pmp->iroot->pmp = pmp;
476 kprintf("iroot %p\n", pmp->iroot);
479 * Ref the cluster management messaging descriptor. The mount
480 * program deals with the other end of the communications pipe.
482 pmp->msg_fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
483 if (pmp->msg_fp == NULL) {
484 kprintf("hammer2_mount: bad cluster_fd!\n");
485 hammer2_vfs_unmount(mp, MNT_FORCE);
488 lwkt_create(hammer2_cluster_thread_rd, pmp, &pmp->msgrd_td,
489 NULL, 0, -1, "hammer2-msgrd");
490 lwkt_create(hammer2_cluster_thread_wr, pmp, &pmp->msgwr_td,
491 NULL, 0, -1, "hammer2-msgwr");
497 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
498 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
499 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
501 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
502 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
503 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
504 copyinstr(path, mp->mnt_stat.f_mntonname,
505 sizeof(mp->mnt_stat.f_mntonname) - 1,
509 * Initial statfs to prime mnt_stat.
511 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
518 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
526 hammer2_vfs_unmount(struct mount *mp, int mntflags)
528 hammer2_pfsmount_t *pmp;
529 hammer2_mount_t *hmp;
532 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
539 if (mntflags & MNT_FORCE)
542 hammer2_mount_exlock(hmp);
545 * If mount initialization proceeded far enough we must flush
549 error = vflush(mp, 0, flags);
554 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
556 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
559 * Flush any left over chains. The voldata lock is only used
560 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
562 hammer2_voldata_lock(hmp);
563 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
564 HAMMER2_CHAIN_MODIFIED_AUX |
565 HAMMER2_CHAIN_SUBMODIFIED)) {
566 hammer2_voldata_unlock(hmp);
567 hammer2_vfs_sync(mp, MNT_WAIT);
569 hammer2_voldata_unlock(hmp);
571 if (hmp->pmp_count == 0) {
572 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
573 HAMMER2_CHAIN_MODIFIED_AUX |
574 HAMMER2_CHAIN_SUBMODIFIED)) {
575 kprintf("hammer2_unmount: chains left over after "
577 if (hammer2_debug & 0x0010)
578 Debugger("entered debugger");
583 * Cleanup the root and super-root chain elements (which should be
588 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
589 KKASSERT(pmp->rchain->refs == 1);
590 hammer2_chain_drop(hmp, pmp->rchain);
593 ccms_domain_uninit(&pmp->ccms_dom);
596 * Ask the cluster controller to go away
598 atomic_set_int(&pmp->msg_ctl, HAMMER2_CLUSTERCTL_KILL);
599 while (pmp->msgrd_td || pmp->msgwr_td) {
600 wakeup(&pmp->msg_ctl);
601 tsleep(pmp, 0, "clstrkl", hz);
605 * Drop communications descriptor
613 * If no PFS's left drop the master hammer2_mount for the device.
615 if (hmp->pmp_count == 0) {
617 KKASSERT(hmp->schain->refs == 1);
618 hammer2_chain_drop(hmp, hmp->schain);
623 * Finish up with the device vnode
625 if ((devvp = hmp->devvp) != NULL) {
626 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
628 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
633 hammer2_mount_unlock(hmp);
639 kmalloc_destroy(&pmp->mmsg);
641 kfree(pmp, M_HAMMER2);
642 if (hmp->pmp_count == 0) {
643 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
644 kmalloc_destroy(&hmp->minode);
645 kmalloc_destroy(&hmp->mchain);
646 kfree(hmp, M_HAMMER2);
648 lockmgr(&hammer2_mntlk, LK_RELEASE);
654 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
655 ino_t ino, struct vnode **vpp)
657 kprintf("hammer2_vget\n");
663 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
665 hammer2_pfsmount_t *pmp;
666 hammer2_mount_t *hmp;
672 hammer2_mount_exlock(hmp);
673 if (pmp->iroot == NULL) {
677 hammer2_chain_lock(hmp, &pmp->iroot->chain,
678 HAMMER2_RESOLVE_ALWAYS |
679 HAMMER2_RESOLVE_SHARED);
680 vp = hammer2_igetv(pmp->iroot, &error);
681 hammer2_chain_unlock(hmp, &pmp->iroot->chain);
684 kprintf("vnodefail\n");
686 hammer2_mount_unlock(hmp);
694 * XXX incorporate pmp->iroot->ip_data.inode_quota and data_quota
698 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
700 hammer2_pfsmount_t *pmp;
701 hammer2_mount_t *hmp;
706 mp->mnt_stat.f_files = pmp->iroot->ip_data.inode_count +
707 pmp->iroot->delta_icount;
708 mp->mnt_stat.f_ffree = 0;
709 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
710 mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
711 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
712 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
720 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
722 hammer2_pfsmount_t *pmp;
723 hammer2_mount_t *hmp;
728 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
729 mp->mnt_vstat.f_files = pmp->iroot->ip_data.inode_count +
730 pmp->iroot->delta_icount;
731 mp->mnt_vstat.f_ffree = 0;
732 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
733 mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
734 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
735 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
737 *sbp = mp->mnt_vstat;
742 * Sync the entire filesystem; this is called from the filesystem syncer
743 * process periodically and whenever a user calls sync(1) on the hammer
746 * Currently is actually called from the syncer! \o/
748 * This task will have to snapshot the state of the dirty inode chain.
749 * From that, it will have to make sure all of the inodes on the dirty
750 * chain have IO initiated. We make sure that io is initiated for the root
753 * If waitfor is set, we wait for media to acknowledge the new rootblock.
755 * THINKS: side A vs side B, to have sync not stall all I/O?
759 hammer2_vfs_sync(struct mount *mp, int waitfor)
761 struct hammer2_sync_info info;
762 hammer2_mount_t *hmp;
770 if (waitfor & MNT_LAZY)
771 flags |= VMSC_ONEPASS;
774 info.waitfor = MNT_NOWAIT;
775 vmntvnodescan(mp, flags | VMSC_NOWAIT,
777 hammer2_sync_scan2, &info);
778 if (info.error == 0 && (waitfor & MNT_WAIT)) {
779 info.waitfor = waitfor;
780 vmntvnodescan(mp, flags,
782 hammer2_sync_scan2, &info);
786 if (waitfor == MNT_WAIT) {
792 hammer2_chain_lock(hmp, &hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
793 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
794 HAMMER2_CHAIN_MODIFIED_AUX |
795 HAMMER2_CHAIN_SUBMODIFIED)) {
796 hammer2_chain_flush(hmp, &hmp->vchain, 0);
801 hammer2_chain_unlock(hmp, &hmp->vchain);
805 if ((waitfor & MNT_LAZY) == 0) {
806 waitfor = MNT_NOWAIT;
807 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
808 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
809 vn_unlock(hmp->devvp);
812 if (error == 0 && haswork) {
816 * Synchronize the disk before flushing the volume
820 bp->b_bio1.bio_offset = 0;
823 bp->b_cmd = BUF_CMD_FLUSH;
824 bp->b_bio1.bio_done = biodone_sync;
825 bp->b_bio1.bio_flags |= BIO_SYNC;
826 vn_strategy(hmp->devvp, &bp->b_bio1);
827 biowait(&bp->b_bio1, "h2vol");
831 * Then we can safely flush the volume header. Volume
832 * data is locked separately to prevent ioctl functions
833 * from deadlocking due to a configuration issue.
835 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
836 hammer2_voldata_lock(hmp);
837 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
838 hammer2_voldata_unlock(hmp);
847 * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
848 * won't flush on those flags. The syncer code above will do a
849 * general meta-data flush globally that will catch these flags.
852 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
857 if (vp->v_type == VNON || ip == NULL ||
858 ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
859 HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
860 RB_EMPTY(&vp->v_rbdirty_tree))) {
867 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
869 struct hammer2_sync_info *info = data;
874 if (vp->v_type == VNON || vp->v_type == VBAD ||
875 ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
876 HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
877 RB_EMPTY(&vp->v_rbdirty_tree))) {
880 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
888 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
895 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
896 struct fid *fhp, struct vnode **vpp)
903 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
904 int *exflagsp, struct ucred **credanonp)
910 * Support code for hammer2_mount(). Read, verify, and install the volume
911 * header into the HMP
913 * XXX read four volhdrs and use the one with the highest TID whos CRC
918 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
919 * nonexistant locations.
921 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
925 hammer2_install_volume_header(hammer2_mount_t *hmp)
927 hammer2_volume_data_t *vd;
929 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
941 * There are up to 4 copies of the volume header (syncs iterate
942 * between them so there is no single master). We don't trust the
943 * volu_size field so we don't know precisely how large the filesystem
944 * is, so depend on the OS to return an error if we go beyond the
945 * block device's EOF.
947 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
948 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
949 HAMMER2_VOLUME_BYTES, &bp);
956 vd = (struct hammer2_volume_data *) bp->b_data;
957 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
958 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
964 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
965 /* XXX: Reversed-endianness filesystem */
966 kprintf("hammer2: reverse-endian filesystem detected");
972 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
973 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
974 HAMMER2_VOLUME_ICRC0_SIZE);
975 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
976 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
977 HAMMER2_VOLUME_ICRC1_SIZE);
978 if ((crc0 != crc) || (bcrc0 != bcrc)) {
979 kprintf("hammer2 volume header crc "
980 "mismatch copy #%d\t%08x %08x",
987 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
997 kprintf("hammer2: a valid volume header was found\n");
1000 kprintf("hammer2: no valid volume headers found!\n");
1006 * Cluster controller thread. Perform messaging functions. We have one
1007 * thread for the reader and one for the writer. The writer handles
1008 * shutdown requests (which should break the reader thread).
1012 hammer2_cluster_thread_rd(void *arg)
1014 hammer2_pfsmount_t *pmp = arg;
1015 hammer2_msg_hdr_t hdr;
1017 hammer2_state_t *state;
1021 while ((pmp->msg_ctl & HAMMER2_CLUSTERCTL_KILL) == 0) {
1023 * Retrieve the message from the pipe or socket.
1025 error = fp_read(pmp->msg_fp, &hdr, sizeof(hdr),
1026 NULL, 1, UIO_SYSSPACE);
1029 if (hdr.magic != HAMMER2_MSGHDR_MAGIC) {
1030 kprintf("hammer2: msgrd: bad magic: %04x\n",
1035 hbytes = (hdr.cmd & HAMMER2_MSGF_SIZE) * HAMMER2_MSG_ALIGN;
1036 if (hbytes < sizeof(hdr) || hbytes > HAMMER2_MSGAUX_MAX) {
1037 kprintf("hammer2: msgrd: bad header size %zd\n",
1042 msg = kmalloc(offsetof(struct hammer2_msg, any) + hbytes,
1043 pmp->mmsg, M_WAITOK | M_ZERO);
1044 msg->any.head = hdr;
1045 msg->hdr_size = hbytes;
1046 if (hbytes > sizeof(hdr)) {
1047 error = fp_read(pmp->msg_fp, &msg->any.head + 1,
1048 hbytes - sizeof(hdr),
1049 NULL, 1, UIO_SYSSPACE);
1051 kprintf("hammer2: short msg received\n");
1056 msg->aux_size = hdr.aux_bytes * HAMMER2_MSG_ALIGN;
1057 if (msg->aux_size > HAMMER2_MSGAUX_MAX) {
1058 kprintf("hammer2: illegal msg payload size %zd\n",
1063 if (msg->aux_size) {
1064 msg->aux_data = kmalloc(msg->aux_size, pmp->mmsg,
1066 error = fp_read(pmp->msg_fp, msg->aux_data,
1068 NULL, 1, UIO_SYSSPACE);
1070 kprintf("hammer2: short msg "
1071 "payload received\n");
1077 * State machine tracking, state assignment for msg,
1078 * returns error and discard status. Errors are fatal
1079 * to the connection except for EALREADY which forces
1080 * a discard without execution.
1082 error = hammer2_state_msgrx(pmp, msg);
1084 hammer2_msg_free(pmp, msg);
1085 if (error == EALREADY)
1088 error = hammer2_msg_execute(pmp, msg);
1089 hammer2_state_cleanuprx(pmp, msg);
1095 kprintf("hammer2: msg read failed error %d\n", error);
1097 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1099 if (msg->state && msg->state->msg == msg)
1100 msg->state->msg = NULL;
1101 hammer2_msg_free(pmp, msg);
1104 if ((state = pmp->freerd_state) != NULL) {
1105 pmp->freerd_state = NULL;
1106 hammer2_state_free(state);
1109 while ((state = RB_ROOT(&pmp->staterd_tree)) != NULL) {
1110 RB_REMOVE(hammer2_state_tree, &pmp->staterd_tree, state);
1111 hammer2_state_free(state);
1113 lockmgr(&pmp->msglk, LK_RELEASE);
1115 fp_shutdown(pmp->msg_fp, SHUT_RDWR);
1116 pmp->msgrd_td = NULL;
1117 /* pmp can be ripped out from under us at this point */
1124 hammer2_cluster_thread_wr(void *arg)
1126 hammer2_pfsmount_t *pmp = arg;
1127 hammer2_msg_t *msg = NULL;
1128 hammer2_state_t *state;
1132 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1133 while ((pmp->msg_ctl & HAMMER2_CLUSTERCTL_KILL) == 0 && error == 0) {
1134 lksleep(&pmp->msg_ctl, &pmp->msglk, 0, "msgwr", hz);
1135 while ((msg = TAILQ_FIRST(&pmp->msgq)) != NULL) {
1137 * Remove msg from the transmit queue and do
1138 * persist and half-closed state handling.
1140 TAILQ_REMOVE(&pmp->msgq, msg, qentry);
1141 lockmgr(&pmp->msglk, LK_RELEASE);
1143 error = hammer2_state_msgtx(pmp, msg);
1144 if (error == EALREADY) {
1146 hammer2_msg_free(pmp, msg);
1147 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1154 * Dump the message to the pipe or socket.
1156 error = fp_write(pmp->msg_fp, &msg->any, msg->hdr_size,
1157 &res, UIO_SYSSPACE);
1158 if (error || res != msg->hdr_size) {
1161 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1164 if (msg->aux_size) {
1165 error = fp_write(pmp->msg_fp,
1166 msg->aux_data, msg->aux_size,
1167 &res, UIO_SYSSPACE);
1168 if (error || res != msg->aux_size) {
1171 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1175 hammer2_state_cleanuptx(pmp, msg);
1176 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1181 * Cleanup messages pending transmission and release msgq lock.
1184 kprintf("hammer2: msg write failed error %d\n", error);
1187 if (msg->state && msg->state->msg == msg)
1188 msg->state->msg = NULL;
1189 hammer2_msg_free(pmp, msg);
1192 while ((msg = TAILQ_FIRST(&pmp->msgq)) != NULL) {
1193 TAILQ_REMOVE(&pmp->msgq, msg, qentry);
1194 if (msg->state && msg->state->msg == msg)
1195 msg->state->msg = NULL;
1196 hammer2_msg_free(pmp, msg);
1199 if ((state = pmp->freewr_state) != NULL) {
1200 pmp->freewr_state = NULL;
1201 hammer2_state_free(state);
1204 while ((state = RB_ROOT(&pmp->statewr_tree)) != NULL) {
1205 RB_REMOVE(hammer2_state_tree, &pmp->statewr_tree, state);
1206 hammer2_state_free(state);
1208 lockmgr(&pmp->msglk, LK_RELEASE);
1211 * Cleanup descriptor, be sure the read size is shutdown so the
1212 * (probably blocked) read operations returns an error.
1214 * pmp can be ripped out from under us once msgwr_td is set to NULL.
1216 fp_shutdown(pmp->msg_fp, SHUT_RDWR);
1217 pmp->msgwr_td = NULL;