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_fmap_read;
70 long hammer2_iod_volu_read;
71 long hammer2_iod_file_write;
72 long hammer2_iod_meta_write;
73 long hammer2_iod_indr_write;
74 long hammer2_iod_fmap_write;
75 long hammer2_iod_volu_write;
76 long hammer2_ioa_file_read;
77 long hammer2_ioa_meta_read;
78 long hammer2_ioa_indr_read;
79 long hammer2_ioa_fmap_read;
80 long hammer2_ioa_volu_read;
81 long hammer2_ioa_fmap_write;
82 long hammer2_ioa_file_write;
83 long hammer2_ioa_meta_write;
84 long hammer2_ioa_indr_write;
85 long hammer2_ioa_volu_write;
87 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
89 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
90 &hammer2_debug, 0, "");
91 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
92 &hammer2_cluster_enable, 0, "");
93 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
94 &hammer2_hardlink_enable, 0, "");
96 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
97 &hammer2_iod_file_read, 0, "");
98 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
99 &hammer2_iod_meta_read, 0, "");
100 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
101 &hammer2_iod_indr_read, 0, "");
102 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
103 &hammer2_iod_fmap_read, 0, "");
104 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
105 &hammer2_iod_volu_read, 0, "");
107 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
108 &hammer2_iod_file_write, 0, "");
109 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
110 &hammer2_iod_meta_write, 0, "");
111 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
112 &hammer2_iod_indr_write, 0, "");
113 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
114 &hammer2_iod_fmap_write, 0, "");
115 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
116 &hammer2_iod_volu_write, 0, "");
118 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
119 &hammer2_ioa_file_read, 0, "");
120 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
121 &hammer2_ioa_meta_read, 0, "");
122 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
123 &hammer2_ioa_indr_read, 0, "");
124 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
125 &hammer2_ioa_fmap_read, 0, "");
126 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
127 &hammer2_ioa_volu_read, 0, "");
129 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
130 &hammer2_ioa_file_write, 0, "");
131 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
132 &hammer2_ioa_meta_write, 0, "");
133 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
134 &hammer2_ioa_indr_write, 0, "");
135 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
136 &hammer2_ioa_fmap_write, 0, "");
137 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
138 &hammer2_ioa_volu_write, 0, "");
140 static int hammer2_vfs_init(struct vfsconf *conf);
141 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
143 static int hammer2_remount(struct mount *, char *, struct vnode *,
145 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
146 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
147 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
149 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
151 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
152 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
153 ino_t ino, struct vnode **vpp);
154 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
155 struct fid *fhp, struct vnode **vpp);
156 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
157 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
158 int *exflagsp, struct ucred **credanonp);
160 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
161 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
162 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
164 static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
165 static void hammer2_autodmsg(kdmsg_msg_t *msg);
168 * HAMMER2 vfs operations.
170 static struct vfsops hammer2_vfsops = {
171 .vfs_init = hammer2_vfs_init,
172 .vfs_sync = hammer2_vfs_sync,
173 .vfs_mount = hammer2_vfs_mount,
174 .vfs_unmount = hammer2_vfs_unmount,
175 .vfs_root = hammer2_vfs_root,
176 .vfs_statfs = hammer2_vfs_statfs,
177 .vfs_statvfs = hammer2_vfs_statvfs,
178 .vfs_vget = hammer2_vfs_vget,
179 .vfs_vptofh = hammer2_vfs_vptofh,
180 .vfs_fhtovp = hammer2_vfs_fhtovp,
181 .vfs_checkexp = hammer2_vfs_checkexp
184 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
186 VFS_SET(hammer2_vfsops, hammer2, 0);
187 MODULE_VERSION(hammer2, 1);
191 hammer2_vfs_init(struct vfsconf *conf)
197 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
199 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
201 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
205 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
207 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
208 TAILQ_INIT(&hammer2_mntlist);
214 * Mount or remount HAMMER2 fileystem from physical media
217 * mp mount point structure
223 * mp mount point structure
224 * path path to mount point
225 * data pointer to argument structure in user space
226 * volume volume path (device@LABEL form)
227 * hflags user mount flags
228 * cred user credentials
235 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
238 struct hammer2_mount_info info;
239 hammer2_pfsmount_t *pmp;
240 hammer2_mount_t *hmp;
243 struct nlookupdata nd;
244 hammer2_chain_t *parent;
245 hammer2_chain_t *schain;
246 hammer2_chain_t *rchain;
248 char devstr[MNAMELEN];
262 kprintf("hammer2_mount\n");
268 bzero(&info, sizeof(info));
269 info.cluster_fd = -1;
273 * Non-root mount or updating a mount
275 error = copyin(data, &info, sizeof(info));
279 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
283 /* Extract device and label */
285 label = strchr(devstr, '@');
287 ((label + 1) - dev) > done) {
295 if (mp->mnt_flag & MNT_UPDATE) {
297 /* HAMMER2 implements NFS export via mountctl */
300 error = hammer2_remount(mp, path, devvp, cred);
308 * Lookup name and verify it refers to a block device.
310 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
312 error = nlookup(&nd);
314 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
318 if (vn_isdisk(devvp, &error))
319 error = vfs_mountedon(devvp);
323 * Determine if the device has already been mounted. After this
324 * check hmp will be non-NULL if we are doing the second or more
325 * hammer2 mounts from the same device.
327 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
328 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
329 if (hmp->devvp == devvp)
334 * Open the device if this isn't a secondary mount and construct
335 * the H2 device mount (hmp).
338 if (error == 0 && vcount(devvp) > 0)
342 * Now open the device
345 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
346 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
347 error = vinvalbuf(devvp, V_SAVE, 0, 0);
349 error = VOP_OPEN(devvp,
350 ronly ? FREAD : FREAD | FWRITE,
355 if (error && devvp) {
360 lockmgr(&hammer2_mntlk, LK_RELEASE);
363 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
366 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
367 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
369 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
370 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
371 TAILQ_INIT(&hmp->transq);
374 * vchain setup. vchain.data is embedded.
375 * vchain.refs is initialized and will never drop to 0.
377 hmp->vchain.hmp = hmp;
378 hmp->vchain.refs = 1;
379 hmp->vchain.data = (void *)&hmp->voldata;
380 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
381 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
382 hmp->vchain.delete_tid = HAMMER2_MAX_TID;
383 hammer2_chain_core_alloc(&hmp->vchain, NULL);
384 /* hmp->vchain.u.xxx is left NULL */
387 * fchain setup. fchain.data is embedded.
388 * fchain.refs is initialized and will never drop to 0.
390 * The data is not used but needs to be initialized to
391 * pass assertion muster. We use this chain primarily
392 * as a placeholder for the freemap's top-level RBTREE
393 * so it does not interfere with the volume's topology
396 hmp->fchain.hmp = hmp;
397 hmp->fchain.refs = 1;
398 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
399 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
400 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
401 hmp->fchain.bref.methods =
402 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
403 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
404 hmp->fchain.delete_tid = HAMMER2_MAX_TID;
406 hammer2_chain_core_alloc(&hmp->fchain, NULL);
407 /* hmp->fchain.u.xxx is left NULL */
410 * Install the volume header
412 error = hammer2_install_volume_header(hmp);
414 hammer2_vfs_unmount(mp, MNT_FORCE);
419 * First locate the super-root inode, which is key 0
420 * relative to the volume header's blockset.
422 * Then locate the root inode by scanning the directory keyspace
423 * represented by the label.
425 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
426 schain = hammer2_chain_lookup(&parent,
427 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
428 hammer2_chain_lookup_done(parent);
429 if (schain == NULL) {
430 kprintf("hammer2_mount: invalid super-root\n");
431 hammer2_vfs_unmount(mp, MNT_FORCE);
434 hammer2_chain_ref(schain); /* for hmp->schain */
435 hmp->schain = schain; /* left locked for inode_get */
436 hmp->sroot = hammer2_inode_get(NULL, NULL, schain);
437 hammer2_inode_ref(hmp->sroot); /* for hmp->sroot */
438 hammer2_inode_unlock_ex(hmp->sroot, schain);
443 * Block device opened successfully, finish initializing the
446 * From this point on we have to call hammer2_unmount() on failure.
448 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
449 pmp->mount_cluster = kmalloc(sizeof(hammer2_cluster_t), M_HAMMER2,
451 pmp->cluster = pmp->mount_cluster;
453 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
454 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
456 pmp->mount_cluster->hmp = hmp;
457 spin_init(&pmp->inum_spin);
458 RB_INIT(&pmp->inum_tree);
460 kdmsg_iocom_init(&pmp->iocom, pmp,
461 KDMSG_IOCOMF_AUTOCONN |
462 KDMSG_IOCOMF_AUTOSPAN |
463 KDMSG_IOCOMF_AUTOCIRC,
464 pmp->mmsg, hammer2_rcvdmsg);
466 ccms_domain_init(&pmp->ccms_dom);
468 lockmgr(&hammer2_mntlk, LK_RELEASE);
469 kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
471 mp->mnt_flag = MNT_LOCAL;
472 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
475 * required mount structure initializations
477 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
478 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
480 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
481 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
486 mp->mnt_iosize_max = MAXPHYS;
487 mp->mnt_data = (qaddr_t)pmp;
491 * schain only has 1 ref now for its hmp->schain assignment.
492 * Setup for lookup (which will lock it).
494 parent = hammer2_chain_lookup_init(hmp->schain, 0);
495 lhc = hammer2_dirhash(label, strlen(label));
496 rchain = hammer2_chain_lookup(&parent,
497 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
500 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
501 strcmp(label, rchain->data->ipdata.filename) == 0) {
504 rchain = hammer2_chain_next(&parent, rchain,
505 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
508 hammer2_chain_lookup_done(parent);
509 if (rchain == NULL) {
510 kprintf("hammer2_mount: PFS label not found\n");
511 hammer2_vfs_unmount(mp, MNT_FORCE);
514 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
515 hammer2_chain_unlock(rchain);
516 kprintf("hammer2_mount: PFS label already mounted!\n");
517 hammer2_vfs_unmount(mp, MNT_FORCE);
520 if (rchain->flags & HAMMER2_CHAIN_RECYCLE) {
521 kprintf("hammer2_mount: PFS label currently recycling\n");
522 hammer2_vfs_unmount(mp, MNT_FORCE);
526 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
529 * NOTE: *_get() integrates chain's lock into the inode lock.
531 hammer2_chain_ref(rchain); /* for pmp->rchain */
532 pmp->mount_cluster->rchain = rchain; /* left held & unlocked */
533 pmp->iroot = hammer2_inode_get(pmp, NULL, rchain);
534 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */
535 hammer2_inode_unlock_ex(pmp->iroot, rchain);
537 kprintf("iroot %p\n", pmp->iroot);
540 * Ref the cluster management messaging descriptor. The mount
541 * program deals with the other end of the communications pipe.
543 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
545 kprintf("hammer2_mount: bad cluster_fd!\n");
546 hammer2_vfs_unmount(mp, MNT_FORCE);
549 hammer2_cluster_reconnect(pmp, fp);
555 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
556 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
557 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
559 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
560 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
561 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
562 copyinstr(path, mp->mnt_stat.f_mntonname,
563 sizeof(mp->mnt_stat.f_mntonname) - 1,
567 * Initial statfs to prime mnt_stat.
569 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
576 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
584 hammer2_vfs_unmount(struct mount *mp, int mntflags)
586 hammer2_pfsmount_t *pmp;
587 hammer2_mount_t *hmp;
588 hammer2_cluster_t *cluster;
591 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
596 cluster = pmp->mount_cluster;
600 if (mntflags & MNT_FORCE)
603 hammer2_mount_exlock(hmp);
606 * If mount initialization proceeded far enough we must flush
610 error = vflush(mp, 0, flags);
613 hammer2_mount_unlock(hmp);
617 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
619 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
622 * Flush any left over chains. The voldata lock is only used
623 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
625 hammer2_voldata_lock(hmp);
626 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
627 HAMMER2_CHAIN_SUBMODIFIED)) {
628 hammer2_voldata_unlock(hmp, 0);
629 hammer2_vfs_sync(mp, MNT_WAIT);
631 hammer2_voldata_unlock(hmp, 0);
633 if (hmp->pmp_count == 0) {
634 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
635 HAMMER2_CHAIN_SUBMODIFIED)) {
636 kprintf("hammer2_unmount: chains left over after "
638 if (hammer2_debug & 0x0010)
639 Debugger("entered debugger");
644 * Cleanup the root and super-root chain elements (which should be
648 #if REPORT_REFS_ERRORS
649 if (pmp->iroot->refs != 1)
650 kprintf("PMP->IROOT %p REFS WRONG %d\n",
651 pmp->iroot, pmp->iroot->refs);
653 KKASSERT(pmp->iroot->refs == 1);
655 hammer2_inode_drop(pmp->iroot); /* ref for pmp->iroot */
658 if (cluster->rchain) {
659 atomic_clear_int(&cluster->rchain->flags,
660 HAMMER2_CHAIN_MOUNTED);
661 #if REPORT_REFS_ERRORS
662 if (cluster->rchain->refs != 1)
663 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
664 cluster->rchain, cluster->rchain->refs);
666 KKASSERT(cluster->rchain->refs == 1);
668 hammer2_chain_drop(cluster->rchain);
669 cluster->rchain = NULL;
671 ccms_domain_uninit(&pmp->ccms_dom);
674 * Kill cluster controller
676 kdmsg_iocom_uninit(&pmp->iocom);
679 * If no PFS's left drop the master hammer2_mount for the device.
681 if (hmp->pmp_count == 0) {
683 hammer2_inode_drop(hmp->sroot);
687 #if REPORT_REFS_ERRORS
688 if (hmp->schain->refs != 1)
689 kprintf("HMP->SCHAIN %p REFS WRONG %d\n",
690 hmp->schain, hmp->schain->refs);
692 KKASSERT(hmp->schain->refs == 1);
694 hammer2_chain_drop(hmp->schain);
699 * Finish up with the device vnode
701 if ((devvp = hmp->devvp) != NULL) {
702 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
704 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
710 * Final drop of embedded freemap root chain to clean up
711 * fchain.core (fchain structure is not flagged ALLOCATED
712 * so it is cleaned out and then left to rot).
714 hammer2_chain_drop(&hmp->fchain);
717 * Final drop of embedded volume root chain to clean up
718 * vchain.core (vchain structure is not flagged ALLOCATED
719 * so it is cleaned out and then left to rot).
722 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt);
723 hammer2_mount_unlock(hmp);
724 hammer2_chain_drop(&hmp->vchain);
726 hammer2_mount_unlock(hmp);
732 pmp->mount_cluster = NULL;
733 pmp->cluster = NULL; /* XXX */
735 kmalloc_destroy(&pmp->mmsg);
736 kmalloc_destroy(&pmp->minode);
740 kfree(cluster, M_HAMMER2);
741 kfree(pmp, M_HAMMER2);
742 if (hmp->pmp_count == 0) {
743 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
744 kmalloc_destroy(&hmp->mchain);
745 kfree(hmp, M_HAMMER2);
747 lockmgr(&hammer2_mntlk, LK_RELEASE);
754 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
755 ino_t ino, struct vnode **vpp)
757 kprintf("hammer2_vget\n");
763 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
765 hammer2_pfsmount_t *pmp;
766 hammer2_chain_t *parent;
771 if (pmp->iroot == NULL) {
775 parent = hammer2_inode_lock_sh(pmp->iroot);
776 vp = hammer2_igetv(pmp->iroot, &error);
777 hammer2_inode_unlock_sh(pmp->iroot, parent);
780 kprintf("vnodefail\n");
789 * XXX incorporate ipdata->inode_quota and data_quota
793 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
795 hammer2_pfsmount_t *pmp;
796 hammer2_mount_t *hmp;
801 mp->mnt_stat.f_files = pmp->inode_count;
802 mp->mnt_stat.f_ffree = 0;
803 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
804 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
805 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
813 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
815 hammer2_pfsmount_t *pmp;
816 hammer2_mount_t *hmp;
821 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
822 mp->mnt_vstat.f_files = pmp->inode_count;
823 mp->mnt_vstat.f_ffree = 0;
824 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
825 mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
826 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
827 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
829 *sbp = mp->mnt_vstat;
834 * Sync the entire filesystem; this is called from the filesystem syncer
835 * process periodically and whenever a user calls sync(1) on the hammer
838 * Currently is actually called from the syncer! \o/
840 * This task will have to snapshot the state of the dirty inode chain.
841 * From that, it will have to make sure all of the inodes on the dirty
842 * chain have IO initiated. We make sure that io is initiated for the root
845 * If waitfor is set, we wait for media to acknowledge the new rootblock.
847 * THINKS: side A vs side B, to have sync not stall all I/O?
851 hammer2_vfs_sync(struct mount *mp, int waitfor)
853 struct hammer2_sync_info info;
854 hammer2_pfsmount_t *pmp;
855 hammer2_cluster_t *cluster;
856 hammer2_mount_t *hmp;
864 if (waitfor & MNT_LAZY)
865 flags |= VMSC_ONEPASS;
867 hammer2_trans_init(&info.trans, pmp, HAMMER2_TRANS_ISFLUSH);
870 info.waitfor = MNT_NOWAIT;
871 vmntvnodescan(mp, flags | VMSC_NOWAIT,
873 hammer2_sync_scan2, &info);
874 if (info.error == 0 && (waitfor & MNT_WAIT)) {
875 info.waitfor = waitfor;
876 vmntvnodescan(mp, flags,
878 hammer2_sync_scan2, &info);
882 if (waitfor == MNT_WAIT) {
889 cluster = pmp->cluster;
892 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
893 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
894 HAMMER2_CHAIN_SUBMODIFIED)) {
895 hammer2_chain_flush(&info.trans, &hmp->vchain);
897 hammer2_chain_unlock(&hmp->vchain);
901 * Rollup flush. The fsyncs above basically just flushed
902 * data blocks. The flush below gets all the meta-data.
904 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
905 if (hmp->fchain.flags & (HAMMER2_CHAIN_MODIFIED |
906 HAMMER2_CHAIN_SUBMODIFIED)) {
907 /* this will modify vchain as a side effect */
908 hammer2_chain_flush(&info.trans, &hmp->fchain);
910 hammer2_chain_unlock(&hmp->fchain);
917 * We can't safely flush the volume header until we have
918 * flushed any device buffers which have built up.
920 * XXX this isn't being incremental
922 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
923 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
924 vn_unlock(hmp->devvp);
927 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
928 * volume header needs synchronization via hmp->volsync.
930 * XXX synchronize the flag & data with only this flush XXX
932 if (error == 0 && (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
936 * Synchronize the disk before flushing the volume
940 bp->b_bio1.bio_offset = 0;
943 bp->b_cmd = BUF_CMD_FLUSH;
944 bp->b_bio1.bio_done = biodone_sync;
945 bp->b_bio1.bio_flags |= BIO_SYNC;
946 vn_strategy(hmp->devvp, &bp->b_bio1);
947 biowait(&bp->b_bio1, "h2vol");
951 * Then we can safely flush the version of the volume header
952 * synchronized by the flush code.
954 i = hmp->volhdrno + 1;
955 if (i >= HAMMER2_NUM_VOLHDRS)
957 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
958 hmp->volsync.volu_size) {
961 kprintf("sync volhdr %d %jd\n",
962 i, (intmax_t)hmp->volsync.volu_size);
963 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
964 HAMMER2_PBUFSIZE, 0, 0);
965 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_VOLUMESYNC);
966 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
970 hammer2_trans_done(&info.trans);
977 * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
978 * won't flush on those flags. The syncer code above will do a
979 * general meta-data flush globally that will catch these flags.
982 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
987 if (vp->v_type == VNON || ip == NULL ||
988 ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
989 RB_EMPTY(&vp->v_rbdirty_tree))) {
996 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
998 struct hammer2_sync_info *info = data;
1000 hammer2_chain_t *parent;
1004 if (vp->v_type == VNON || vp->v_type == VBAD ||
1005 ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
1006 RB_EMPTY(&vp->v_rbdirty_tree))) {
1011 * VOP_FSYNC will start a new transaction so replicate some code
1012 * here to do it inline (see hammer2_vop_fsync()).
1014 parent = hammer2_inode_lock_ex(ip);
1015 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1017 vfsync(ip->vp, MNT_NOWAIT, 1, NULL, NULL);
1018 hammer2_chain_flush(&info->trans, parent);
1019 hammer2_inode_unlock_ex(ip, parent);
1022 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
1025 info->error = error;
1031 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
1038 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
1039 struct fid *fhp, struct vnode **vpp)
1046 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1047 int *exflagsp, struct ucred **credanonp)
1053 * Support code for hammer2_mount(). Read, verify, and install the volume
1054 * header into the HMP
1056 * XXX read four volhdrs and use the one with the highest TID whos CRC
1061 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
1062 * nonexistant locations.
1064 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
1068 hammer2_install_volume_header(hammer2_mount_t *hmp)
1070 hammer2_volume_data_t *vd;
1072 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
1084 * There are up to 4 copies of the volume header (syncs iterate
1085 * between them so there is no single master). We don't trust the
1086 * volu_size field so we don't know precisely how large the filesystem
1087 * is, so depend on the OS to return an error if we go beyond the
1088 * block device's EOF.
1090 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
1091 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1092 HAMMER2_VOLUME_BYTES, &bp);
1099 vd = (struct hammer2_volume_data *) bp->b_data;
1100 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
1101 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
1107 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
1108 /* XXX: Reversed-endianness filesystem */
1109 kprintf("hammer2: reverse-endian filesystem detected");
1115 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
1116 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
1117 HAMMER2_VOLUME_ICRC0_SIZE);
1118 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
1119 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
1120 HAMMER2_VOLUME_ICRC1_SIZE);
1121 if ((crc0 != crc) || (bcrc0 != bcrc)) {
1122 kprintf("hammer2 volume header crc "
1123 "mismatch copy #%d %08x/%08x\n",
1130 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
1139 hmp->volsync = hmp->voldata;
1141 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
1142 kprintf("hammer2: using volume header #%d\n",
1147 kprintf("hammer2: no valid volume headers found!\n");
1153 * Reconnect using the passed file pointer. The caller must ref the
1157 hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
1159 hammer2_inode_data_t *ipdata;
1160 hammer2_chain_t *parent;
1161 hammer2_mount_t *hmp;
1164 hmp = pmp->mount_cluster->hmp;
1167 * Closes old comm descriptor, kills threads, cleans up
1168 * states, then installs the new descriptor and creates
1171 kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
1174 * Setup LNK_CONN fields for autoinitiated state machine
1176 parent = hammer2_inode_lock_ex(pmp->iroot);
1177 ipdata = &parent->data->ipdata;
1178 pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
1179 pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
1180 pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
1181 pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
1182 pmp->iocom.auto_lnk_conn.peer_type = hmp->voldata.peer_type;
1185 * Filter adjustment. Clients do not need visibility into other
1186 * clients (otherwise millions of clients would present a serious
1187 * problem). The fs_label also serves to restrict the namespace.
1189 pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
1190 pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
1191 switch (ipdata->pfs_type) {
1192 case DMSG_PFSTYPE_CLIENT:
1193 pmp->iocom.auto_lnk_conn.peer_mask &=
1194 ~(1LLU << DMSG_PFSTYPE_CLIENT);
1200 name_len = ipdata->name_len;
1201 if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
1202 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
1203 bcopy(ipdata->filename,
1204 pmp->iocom.auto_lnk_conn.fs_label,
1206 pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
1209 * Setup LNK_SPAN fields for autoinitiated state machine
1211 pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
1212 pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
1213 pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
1214 pmp->iocom.auto_lnk_span.peer_type = hmp->voldata.peer_type;
1215 pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
1216 name_len = ipdata->name_len;
1217 if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
1218 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
1219 bcopy(ipdata->filename,
1220 pmp->iocom.auto_lnk_span.fs_label,
1222 pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
1223 hammer2_inode_unlock_ex(pmp->iroot, parent);
1225 kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
1229 hammer2_rcvdmsg(kdmsg_msg_t *msg)
1231 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
1232 case DMSG_DBG_SHELL:
1235 * Execute shell command (not supported atm)
1237 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1239 case DMSG_DBG_SHELL | DMSGF_REPLY:
1243 if (msg->aux_data) {
1244 msg->aux_data[msg->aux_size - 1] = 0;
1245 kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
1250 * Unsupported message received. We only need to
1251 * reply if it's a transaction in order to close our end.
1252 * Ignore any one-way messages are any further messages
1253 * associated with the transaction.
1255 * NOTE: This case also includes DMSG_LNK_ERROR messages
1256 * which might be one-way, replying to those would
1257 * cause an infinite ping-pong.
1259 if (msg->any.head.cmd & DMSGF_CREATE)
1260 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1267 * This function is called after KDMSG has automatically handled processing
1268 * of a LNK layer message (typically CONN, SPAN, or CIRC).
1270 * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
1271 * advertises all available hammer2 super-root volumes.
1274 hammer2_autodmsg(kdmsg_msg_t *msg)
1276 hammer2_pfsmount_t *pmp = msg->iocom->handle;
1277 hammer2_mount_t *hmp = pmp->mount_cluster->hmp;
1281 * We only care about replies to our LNK_CONN auto-request. kdmsg
1282 * has already processed the reply, we use this calback as a shim
1283 * to know when we can advertise available super-root volumes.
1285 if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
1286 (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
1287 msg->state == NULL) {
1291 kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
1293 if (msg->any.head.cmd & DMSGF_CREATE) {
1294 kprintf("HAMMER2: VOLDATA DUMP\n");
1297 * Dump the configuration stored in the volume header
1299 hammer2_voldata_lock(hmp);
1300 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
1301 if (hmp->voldata.copyinfo[copyid].copyid == 0)
1303 hammer2_volconf_update(pmp, copyid);
1305 hammer2_voldata_unlock(hmp, 0);
1307 if ((msg->any.head.cmd & DMSGF_DELETE) &&
1308 msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
1309 kprintf("HAMMER2: CONN WAS TERMINATED\n");
1314 * Volume configuration updates are passed onto the userland service
1315 * daemon via the open LNK_CONN transaction.
1318 hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
1320 hammer2_mount_t *hmp = pmp->mount_cluster->hmp;
1323 /* XXX interlock against connection state termination */
1324 kprintf("volconf update %p\n", pmp->iocom.conn_state);
1325 if (pmp->iocom.conn_state) {
1326 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
1327 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
1328 DMSG_LNK_VOLCONF, NULL, NULL);
1329 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
1330 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
1331 msg->any.lnk_volconf.index = index;
1332 kdmsg_msg_write(msg);
1337 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp)
1339 hammer2_chain_t *scan;
1343 kprintf("%*.*s...\n", tab, tab, "");
1348 kprintf("%*.*schain[%d] %p.%d [%08x][core=%p] (%s) dl=%p dt=%s refs=%d",
1350 chain->index, chain, chain->bref.type, chain->flags,
1352 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1353 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
1355 (chain->delete_tid == HAMMER2_MAX_TID ? "max" : "fls"),
1357 if (chain->core == NULL || RB_EMPTY(&chain->core->rbtree))
1361 RB_FOREACH(scan, hammer2_chain_tree, &chain->core->rbtree) {
1362 hammer2_dump_chain(scan, tab + 4, countp);
1364 if (chain->core && !RB_EMPTY(&chain->core->rbtree)) {
1365 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
1366 kprintf("%*.*s}(%s)\n", tab, tab, "",
1367 chain->data->ipdata.filename);
1369 kprintf("%*.*s}\n", tab, tab, "");