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"
51 struct hammer2_sync_info {
56 TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
57 static struct hammer2_mntlist hammer2_mntlist;
58 static struct lock hammer2_mntlk;
61 int hammer2_cluster_enable = 1;
62 int hammer2_hardlink_enable = 1;
63 long hammer2_iod_file_read;
64 long hammer2_iod_meta_read;
65 long hammer2_iod_indr_read;
66 long hammer2_iod_file_write;
67 long hammer2_iod_meta_write;
68 long hammer2_iod_indr_write;
69 long hammer2_iod_fmap_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_fmap_write;
75 long hammer2_ioa_file_write;
76 long hammer2_ioa_meta_write;
77 long hammer2_ioa_indr_write;
78 long hammer2_ioa_volu_write;
80 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
82 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
83 &hammer2_debug, 0, "");
84 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
85 &hammer2_cluster_enable, 0, "");
86 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
87 &hammer2_hardlink_enable, 0, "");
88 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
89 &hammer2_iod_file_read, 0, "");
90 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
91 &hammer2_iod_meta_read, 0, "");
92 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
93 &hammer2_iod_indr_read, 0, "");
94 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
95 &hammer2_iod_file_write, 0, "");
96 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
97 &hammer2_iod_meta_write, 0, "");
98 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
99 &hammer2_iod_indr_write, 0, "");
100 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
101 &hammer2_iod_volu_write, 0, "");
102 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
103 &hammer2_ioa_file_read, 0, "");
104 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
105 &hammer2_ioa_meta_read, 0, "");
106 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
107 &hammer2_ioa_indr_read, 0, "");
108 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
109 &hammer2_ioa_file_write, 0, "");
110 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
111 &hammer2_ioa_meta_write, 0, "");
112 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
113 &hammer2_ioa_indr_write, 0, "");
114 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
115 &hammer2_ioa_volu_write, 0, "");
117 static int hammer2_vfs_init(struct vfsconf *conf);
118 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
120 static int hammer2_remount(struct mount *, char *, struct vnode *,
122 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
123 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
124 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
126 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
128 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
129 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
130 ino_t ino, struct vnode **vpp);
131 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
132 struct fid *fhp, struct vnode **vpp);
133 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
134 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
135 int *exflagsp, struct ucred **credanonp);
137 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
138 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
139 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
141 static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
142 static void hammer2_autodmsg(kdmsg_msg_t *msg);
145 * HAMMER2 vfs operations.
147 static struct vfsops hammer2_vfsops = {
148 .vfs_init = hammer2_vfs_init,
149 .vfs_sync = hammer2_vfs_sync,
150 .vfs_mount = hammer2_vfs_mount,
151 .vfs_unmount = hammer2_vfs_unmount,
152 .vfs_root = hammer2_vfs_root,
153 .vfs_statfs = hammer2_vfs_statfs,
154 .vfs_statvfs = hammer2_vfs_statvfs,
155 .vfs_vget = hammer2_vfs_vget,
156 .vfs_vptofh = hammer2_vfs_vptofh,
157 .vfs_fhtovp = hammer2_vfs_fhtovp,
158 .vfs_checkexp = hammer2_vfs_checkexp
161 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
163 VFS_SET(hammer2_vfsops, hammer2, 0);
164 MODULE_VERSION(hammer2, 1);
168 hammer2_vfs_init(struct vfsconf *conf)
174 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
176 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
178 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
182 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
184 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
185 TAILQ_INIT(&hammer2_mntlist);
191 * Mount or remount HAMMER2 fileystem from physical media
194 * mp mount point structure
200 * mp mount point structure
201 * path path to mount point
202 * data pointer to argument structure in user space
203 * volume volume path (device@LABEL form)
204 * hflags user mount flags
205 * cred user credentials
212 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
215 struct hammer2_mount_info info;
216 hammer2_pfsmount_t *pmp;
217 hammer2_mount_t *hmp;
220 struct nlookupdata nd;
221 hammer2_chain_t *parent;
222 hammer2_chain_t *schain;
223 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;
355 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
356 kdmsg_iocom_init(&pmp->iocom, pmp,
357 KDMSG_IOCOMF_AUTOCONN |
358 KDMSG_IOCOMF_AUTOSPAN |
359 KDMSG_IOCOMF_AUTOCIRC,
360 pmp->mmsg, hammer2_rcvdmsg);
363 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
366 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
367 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
368 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
370 ccms_domain_init(&pmp->ccms_dom);
373 lockmgr(&hammer2_mntlk, LK_RELEASE);
374 kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
376 mp->mnt_flag = MNT_LOCAL;
377 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
381 * vchain setup. vchain.data is special cased to NULL.
382 * vchain.refs is initialized and will never drop to 0.
384 hmp->vchain.refs = 1;
385 hmp->vchain.data = (void *)&hmp->voldata;
386 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
387 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
388 hmp->vchain.bref_flush = hmp->vchain.bref;
389 ccms_cst_init(&hmp->vchain.cst, NULL);
390 /* hmp->vchain.u.xxx is left NULL */
391 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
392 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
395 * Install the volume header
397 error = hammer2_install_volume_header(hmp);
399 hammer2_vfs_unmount(mp, MNT_FORCE);
405 * required mount structure initializations
407 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
408 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
410 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
411 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
416 mp->mnt_iosize_max = MAXPHYS;
419 * First locate the super-root inode, which is key 0 relative to the
420 * volume header's blockset.
422 * Then locate the root inode by scanning the directory keyspace
423 * represented by the label.
426 parent = &hmp->vchain;
427 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
428 schain = hammer2_chain_lookup(hmp, &parent,
429 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
430 hammer2_chain_unlock(hmp, parent);
431 if (schain == NULL) {
432 kprintf("hammer2_mount: invalid super-root\n");
433 hammer2_vfs_unmount(mp, MNT_FORCE);
436 hammer2_chain_ref(hmp, schain); /* for hmp->schain */
437 hmp->schain = schain; /* left locked */
439 schain = hmp->schain;
440 hammer2_chain_lock(hmp, schain, HAMMER2_RESOLVE_ALWAYS);
444 lhc = hammer2_dirhash(label, strlen(label));
445 rchain = hammer2_chain_lookup(hmp, &parent,
446 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
449 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
450 strcmp(label, rchain->data->ipdata.filename) == 0) {
453 rchain = hammer2_chain_next(hmp, &parent, rchain,
454 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
457 hammer2_chain_unlock(hmp, parent);
458 if (rchain == NULL) {
459 kprintf("hammer2_mount: PFS label not found\n");
460 hammer2_vfs_unmount(mp, MNT_FORCE);
463 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
464 hammer2_chain_unlock(hmp, rchain);
465 kprintf("hammer2_mount: PFS label already mounted!\n");
466 hammer2_vfs_unmount(mp, MNT_FORCE);
469 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
472 * NOTE: *_get() integrates chain's lock into the inode lock.
474 hammer2_chain_ref(hmp, rchain); /* for pmp->rchain */
475 pmp->rchain = rchain; /* left held & unlocked */
476 pmp->iroot = hammer2_inode_get(pmp, NULL, rchain);
477 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */
478 hammer2_inode_unlock_ex(pmp->iroot); /* iroot & its chain */
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 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
488 kprintf("hammer2_mount: bad cluster_fd!\n");
489 hammer2_vfs_unmount(mp, MNT_FORCE);
492 hammer2_cluster_reconnect(pmp, fp);
498 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
499 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
500 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
502 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
503 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
504 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
505 copyinstr(path, mp->mnt_stat.f_mntonname,
506 sizeof(mp->mnt_stat.f_mntonname) - 1,
510 * Initial statfs to prime mnt_stat.
512 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
519 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
527 hammer2_vfs_unmount(struct mount *mp, int mntflags)
529 hammer2_pfsmount_t *pmp;
530 hammer2_mount_t *hmp;
533 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
540 if (mntflags & MNT_FORCE)
543 hammer2_mount_exlock(hmp);
546 * If mount initialization proceeded far enough we must flush
550 error = vflush(mp, 0, flags);
555 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
557 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
560 * Flush any left over chains. The voldata lock is only used
561 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
563 hammer2_voldata_lock(hmp);
564 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
565 HAMMER2_CHAIN_MODIFIED_AUX |
566 HAMMER2_CHAIN_SUBMODIFIED)) {
567 hammer2_voldata_unlock(hmp);
568 hammer2_vfs_sync(mp, MNT_WAIT);
570 hammer2_voldata_unlock(hmp);
572 if (hmp->pmp_count == 0) {
573 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
574 HAMMER2_CHAIN_MODIFIED_AUX |
575 HAMMER2_CHAIN_SUBMODIFIED)) {
576 kprintf("hammer2_unmount: chains left over after "
578 if (hammer2_debug & 0x0010)
579 Debugger("entered debugger");
584 * Cleanup the root and super-root chain elements (which should be
588 hammer2_inode_lock_ex(pmp->iroot);
589 hammer2_inode_put(pmp->iroot);
590 /* lock destroyed by the put */
591 KKASSERT(pmp->iroot->refs == 1);
592 hammer2_inode_drop(pmp->iroot);
596 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
597 KKASSERT(pmp->rchain->refs == 1);
598 hammer2_chain_drop(hmp, pmp->rchain);
601 ccms_domain_uninit(&pmp->ccms_dom);
604 * Kill cluster controller
606 kdmsg_iocom_uninit(&pmp->iocom);
609 * If no PFS's left drop the master hammer2_mount for the device.
611 if (hmp->pmp_count == 0) {
613 KKASSERT(hmp->schain->refs == 1);
614 hammer2_chain_drop(hmp, hmp->schain);
619 * Finish up with the device vnode
621 if ((devvp = hmp->devvp) != NULL) {
622 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
624 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
629 hammer2_mount_unlock(hmp);
635 kmalloc_destroy(&pmp->mmsg);
637 kfree(pmp, M_HAMMER2);
638 if (hmp->pmp_count == 0) {
639 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
640 kmalloc_destroy(&hmp->minode);
641 kmalloc_destroy(&hmp->mchain);
642 kfree(hmp, M_HAMMER2);
644 lockmgr(&hammer2_mntlk, LK_RELEASE);
650 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
651 ino_t ino, struct vnode **vpp)
653 kprintf("hammer2_vget\n");
659 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
661 hammer2_pfsmount_t *pmp;
662 hammer2_mount_t *hmp;
668 hammer2_mount_exlock(hmp);
669 if (pmp->iroot == NULL) {
673 hammer2_inode_lock_sh(pmp->iroot);
674 vp = hammer2_igetv(pmp->iroot, &error);
675 hammer2_inode_unlock_sh(pmp->iroot);
678 kprintf("vnodefail\n");
680 hammer2_mount_unlock(hmp);
688 * XXX incorporate ipdata->inode_quota and data_quota
692 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
694 hammer2_pfsmount_t *pmp;
695 hammer2_mount_t *hmp;
700 mp->mnt_stat.f_files = pmp->inode_count;
701 mp->mnt_stat.f_ffree = 0;
702 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
703 mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
704 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
705 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
713 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
715 hammer2_pfsmount_t *pmp;
716 hammer2_mount_t *hmp;
721 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
722 mp->mnt_vstat.f_files = pmp->inode_count;
723 mp->mnt_vstat.f_ffree = 0;
724 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
725 mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
726 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
727 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
729 *sbp = mp->mnt_vstat;
734 * Sync the entire filesystem; this is called from the filesystem syncer
735 * process periodically and whenever a user calls sync(1) on the hammer
738 * Currently is actually called from the syncer! \o/
740 * This task will have to snapshot the state of the dirty inode chain.
741 * From that, it will have to make sure all of the inodes on the dirty
742 * chain have IO initiated. We make sure that io is initiated for the root
745 * If waitfor is set, we wait for media to acknowledge the new rootblock.
747 * THINKS: side A vs side B, to have sync not stall all I/O?
751 hammer2_vfs_sync(struct mount *mp, int waitfor)
753 struct hammer2_sync_info info;
754 hammer2_mount_t *hmp;
763 if (waitfor & MNT_LAZY)
764 flags |= VMSC_ONEPASS;
767 info.waitfor = MNT_NOWAIT;
768 vmntvnodescan(mp, flags | VMSC_NOWAIT,
770 hammer2_sync_scan2, &info);
771 if (info.error == 0 && (waitfor & MNT_WAIT)) {
772 info.waitfor = waitfor;
773 vmntvnodescan(mp, flags,
775 hammer2_sync_scan2, &info);
779 if (waitfor == MNT_WAIT) {
785 hammer2_chain_lock(hmp, &hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
786 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
787 HAMMER2_CHAIN_MODIFIED_AUX |
788 HAMMER2_CHAIN_SUBMODIFIED)) {
789 hammer2_chain_flush(hmp, &hmp->vchain, 0);
794 hammer2_chain_unlock(hmp, &hmp->vchain);
799 * We can't safely flush the volume header until we have
800 * flushed any device buffers which have built up.
803 if ((waitfor & MNT_LAZY) == 0) {
804 waitfor = MNT_NOWAIT;
805 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
806 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
807 vn_unlock(hmp->devvp);
810 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
811 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
812 vn_unlock(hmp->devvp);
814 if (error == 0 && haswork) {
818 * Synchronize the disk before flushing the volume
822 bp->b_bio1.bio_offset = 0;
825 bp->b_cmd = BUF_CMD_FLUSH;
826 bp->b_bio1.bio_done = biodone_sync;
827 bp->b_bio1.bio_flags |= BIO_SYNC;
828 vn_strategy(hmp->devvp, &bp->b_bio1);
829 biowait(&bp->b_bio1, "h2vol");
833 * Then we can safely flush the version of the volume header
834 * synchronized by the flush code.
836 i = hmp->volhdrno + 1;
837 if (i >= HAMMER2_NUM_VOLHDRS)
839 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
840 hmp->volsync.volu_size) {
843 kprintf("sync volhdr %d %jd\n",
844 i, (intmax_t)hmp->volsync.volu_size);
845 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
846 HAMMER2_PBUFSIZE, 0, 0);
847 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
857 * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
858 * won't flush on those flags. The syncer code above will do a
859 * general meta-data flush globally that will catch these flags.
862 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
867 if (vp->v_type == VNON || ip == NULL ||
868 ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
869 RB_EMPTY(&vp->v_rbdirty_tree))) {
876 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
878 struct hammer2_sync_info *info = data;
883 if (vp->v_type == VNON || vp->v_type == VBAD ||
884 ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
885 RB_EMPTY(&vp->v_rbdirty_tree))) {
888 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
896 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
903 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
904 struct fid *fhp, struct vnode **vpp)
911 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
912 int *exflagsp, struct ucred **credanonp)
918 * Support code for hammer2_mount(). Read, verify, and install the volume
919 * header into the HMP
921 * XXX read four volhdrs and use the one with the highest TID whos CRC
926 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
927 * nonexistant locations.
929 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
933 hammer2_install_volume_header(hammer2_mount_t *hmp)
935 hammer2_volume_data_t *vd;
937 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
949 * There are up to 4 copies of the volume header (syncs iterate
950 * between them so there is no single master). We don't trust the
951 * volu_size field so we don't know precisely how large the filesystem
952 * is, so depend on the OS to return an error if we go beyond the
953 * block device's EOF.
955 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
956 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
957 HAMMER2_VOLUME_BYTES, &bp);
964 vd = (struct hammer2_volume_data *) bp->b_data;
965 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
966 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
972 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
973 /* XXX: Reversed-endianness filesystem */
974 kprintf("hammer2: reverse-endian filesystem detected");
980 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
981 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
982 HAMMER2_VOLUME_ICRC0_SIZE);
983 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
984 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
985 HAMMER2_VOLUME_ICRC1_SIZE);
986 if ((crc0 != crc) || (bcrc0 != bcrc)) {
987 kprintf("hammer2 volume header crc "
988 "mismatch copy #%d %08x/%08x\n",
995 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
1004 hmp->volsync = hmp->voldata;
1006 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
1007 kprintf("hammer2: using volume header #%d\n",
1012 kprintf("hammer2: no valid volume headers found!\n");
1018 * Reconnect using the passed file pointer. The caller must ref the
1022 hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
1024 hammer2_inode_data_t *ipdata;
1028 * Closes old comm descriptor, kills threads, cleans up
1029 * states, then installs the new descriptor and creates
1032 kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
1035 * Setup LNK_CONN fields for autoinitiated state machine
1037 hammer2_inode_lock_ex(pmp->iroot);
1038 ipdata = &pmp->iroot->chain->data->ipdata;
1039 pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
1040 pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
1041 pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
1042 pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
1043 pmp->iocom.auto_lnk_conn.peer_type = pmp->hmp->voldata.peer_type;
1044 hammer2_inode_unlock_ex(pmp->iroot);
1047 * Filter adjustment. Clients do not need visibility into other
1048 * clients (otherwise millions of clients would present a serious
1049 * problem). The fs_label also serves to restrict the namespace.
1051 pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
1052 pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
1053 switch (ipdata->pfs_type) {
1054 case DMSG_PFSTYPE_CLIENT:
1055 pmp->iocom.auto_lnk_conn.peer_mask &=
1056 ~(1LLU << DMSG_PFSTYPE_CLIENT);
1062 name_len = ipdata->name_len;
1063 if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
1064 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
1065 bcopy(ipdata->filename,
1066 pmp->iocom.auto_lnk_conn.fs_label,
1068 pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
1071 * Setup LNK_SPAN fields for autoinitiated state machine
1073 pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
1074 pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
1075 pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
1076 pmp->iocom.auto_lnk_span.peer_type = pmp->hmp->voldata.peer_type;
1077 pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
1078 name_len = ipdata->name_len;
1079 if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
1080 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
1081 bcopy(ipdata->filename,
1082 pmp->iocom.auto_lnk_span.fs_label,
1084 pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
1086 kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
1090 hammer2_rcvdmsg(kdmsg_msg_t *msg)
1092 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
1093 case DMSG_DBG_SHELL:
1096 * Execute shell command (not supported atm)
1098 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1100 case DMSG_DBG_SHELL | DMSGF_REPLY:
1104 if (msg->aux_data) {
1105 msg->aux_data[msg->aux_size - 1] = 0;
1106 kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
1111 * Unsupported message received. We only need to
1112 * reply if it's a transaction in order to close our end.
1113 * Ignore any one-way messages are any further messages
1114 * associated with the transaction.
1116 * NOTE: This case also includes DMSG_LNK_ERROR messages
1117 * which might be one-way, replying to those would
1118 * cause an infinite ping-pong.
1120 if (msg->any.head.cmd & DMSGF_CREATE)
1121 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1128 * This function is called after KDMSG has automatically handled processing
1129 * of a LNK layer message (typically CONN, SPAN, or CIRC).
1131 * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
1132 * advertises all available hammer2 super-root volumes.
1135 hammer2_autodmsg(kdmsg_msg_t *msg)
1137 hammer2_pfsmount_t *pmp = msg->iocom->handle;
1138 hammer2_mount_t *hmp = pmp->hmp;
1142 * We only care about replies to our LNK_CONN auto-request. kdmsg
1143 * has already processed the reply, we use this calback as a shim
1144 * to know when we can advertise available super-root volumes.
1146 if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
1147 (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
1148 msg->state == NULL) {
1152 kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
1154 if (msg->any.head.cmd & DMSGF_CREATE) {
1155 kprintf("HAMMER2: VOLDATA DUMP\n");
1158 * Dump the configuration stored in the volume header
1160 hammer2_voldata_lock(hmp);
1161 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
1162 if (hmp->voldata.copyinfo[copyid].copyid == 0)
1164 hammer2_volconf_update(pmp, copyid);
1166 hammer2_voldata_unlock(hmp);
1168 if ((msg->any.head.cmd & DMSGF_DELETE) &&
1169 msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
1170 kprintf("HAMMER2: CONN WAS TERMINATED\n");
1175 * Volume configuration updates are passed onto the userland service
1176 * daemon via the open LNK_CONN transaction.
1179 hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
1181 hammer2_mount_t *hmp = pmp->hmp;
1184 /* XXX interlock against connection state termination */
1185 kprintf("volconf update %p\n", pmp->iocom.conn_state);
1186 if (pmp->iocom.conn_state) {
1187 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
1188 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
1189 DMSG_LNK_VOLCONF, NULL, NULL);
1190 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
1191 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
1192 msg->any.lnk_volconf.index = index;
1193 kdmsg_msg_write(msg);