2 * Copyright (c) 2011-2015 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>
6 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/nlookup.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/fcntl.h>
44 #include <sys/vfsops.h>
45 #include <sys/sysctl.h>
46 #include <sys/socket.h>
47 #include <sys/objcache.h>
50 #include <sys/namei.h>
51 #include <sys/mountctl.h>
52 #include <sys/dirent.h>
55 #include <sys/mutex.h>
56 #include <sys/mutex2.h>
59 #include "hammer2_disk.h"
60 #include "hammer2_mount.h"
61 #include "hammer2_lz4.h"
63 #include "zlib/hammer2_zlib.h"
65 #define REPORT_REFS_ERRORS 1 /* XXX remove me */
67 MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache");
69 struct hammer2_sync_info {
74 TAILQ_HEAD(hammer2_mntlist, hammer2_dev);
75 TAILQ_HEAD(hammer2_pfslist, hammer2_pfs);
76 static struct hammer2_mntlist hammer2_mntlist;
77 static struct hammer2_pfslist hammer2_pfslist;
78 static struct lock hammer2_mntlk;
81 int hammer2_cluster_enable = 1;
82 int hammer2_hardlink_enable = 1;
83 int hammer2_flush_pipe = 100;
84 int hammer2_synchronous_flush = 1;
85 int hammer2_dio_count;
86 long hammer2_limit_dirty_chains;
87 long hammer2_iod_file_read;
88 long hammer2_iod_meta_read;
89 long hammer2_iod_indr_read;
90 long hammer2_iod_fmap_read;
91 long hammer2_iod_volu_read;
92 long hammer2_iod_file_write;
93 long hammer2_iod_file_wembed;
94 long hammer2_iod_file_wzero;
95 long hammer2_iod_file_wdedup;
96 long hammer2_iod_meta_write;
97 long hammer2_iod_indr_write;
98 long hammer2_iod_fmap_write;
99 long hammer2_iod_volu_write;
100 long hammer2_ioa_file_read;
101 long hammer2_ioa_meta_read;
102 long hammer2_ioa_indr_read;
103 long hammer2_ioa_fmap_read;
104 long hammer2_ioa_volu_read;
105 long hammer2_ioa_fmap_write;
106 long hammer2_ioa_file_write;
107 long hammer2_ioa_meta_write;
108 long hammer2_ioa_indr_write;
109 long hammer2_ioa_volu_write;
111 MALLOC_DECLARE(M_HAMMER2_CBUFFER);
112 MALLOC_DEFINE(M_HAMMER2_CBUFFER, "HAMMER2-compbuffer",
113 "Buffer used for compression.");
115 MALLOC_DECLARE(M_HAMMER2_DEBUFFER);
116 MALLOC_DEFINE(M_HAMMER2_DEBUFFER, "HAMMER2-decompbuffer",
117 "Buffer used for decompression.");
119 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
121 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
122 &hammer2_debug, 0, "");
123 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
124 &hammer2_cluster_enable, 0, "");
125 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
126 &hammer2_hardlink_enable, 0, "");
127 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
128 &hammer2_flush_pipe, 0, "");
129 SYSCTL_INT(_vfs_hammer2, OID_AUTO, synchronous_flush, CTLFLAG_RW,
130 &hammer2_synchronous_flush, 0, "");
131 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW,
132 &hammer2_limit_dirty_chains, 0, "");
133 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD,
134 &hammer2_dio_count, 0, "");
136 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
137 &hammer2_iod_file_read, 0, "");
138 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
139 &hammer2_iod_meta_read, 0, "");
140 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
141 &hammer2_iod_indr_read, 0, "");
142 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
143 &hammer2_iod_fmap_read, 0, "");
144 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
145 &hammer2_iod_volu_read, 0, "");
147 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
148 &hammer2_iod_file_write, 0, "");
149 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wembed, CTLFLAG_RW,
150 &hammer2_iod_file_wembed, 0, "");
151 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wzero, CTLFLAG_RW,
152 &hammer2_iod_file_wzero, 0, "");
153 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wdedup, CTLFLAG_RW,
154 &hammer2_iod_file_wdedup, 0, "");
155 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
156 &hammer2_iod_meta_write, 0, "");
157 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
158 &hammer2_iod_indr_write, 0, "");
159 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
160 &hammer2_iod_fmap_write, 0, "");
161 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
162 &hammer2_iod_volu_write, 0, "");
164 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
165 &hammer2_ioa_file_read, 0, "");
166 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
167 &hammer2_ioa_meta_read, 0, "");
168 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
169 &hammer2_ioa_indr_read, 0, "");
170 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
171 &hammer2_ioa_fmap_read, 0, "");
172 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
173 &hammer2_ioa_volu_read, 0, "");
175 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
176 &hammer2_ioa_file_write, 0, "");
177 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
178 &hammer2_ioa_meta_write, 0, "");
179 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
180 &hammer2_ioa_indr_write, 0, "");
181 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
182 &hammer2_ioa_fmap_write, 0, "");
183 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
184 &hammer2_ioa_volu_write, 0, "");
186 static int hammer2_vfs_init(struct vfsconf *conf);
187 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
188 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
190 static int hammer2_remount(hammer2_dev_t *, struct mount *, char *,
191 struct vnode *, struct ucred *);
192 static int hammer2_recovery(hammer2_dev_t *hmp);
193 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
194 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
195 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
197 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
199 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
200 ino_t ino, struct vnode **vpp);
201 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
202 struct fid *fhp, struct vnode **vpp);
203 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
204 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
205 int *exflagsp, struct ucred **credanonp);
207 static int hammer2_install_volume_header(hammer2_dev_t *hmp);
208 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
210 static void hammer2_update_pmps(hammer2_dev_t *hmp);
212 static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp);
213 static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp,
217 * HAMMER2 vfs operations.
219 static struct vfsops hammer2_vfsops = {
220 .vfs_init = hammer2_vfs_init,
221 .vfs_uninit = hammer2_vfs_uninit,
222 .vfs_sync = hammer2_vfs_sync,
223 .vfs_mount = hammer2_vfs_mount,
224 .vfs_unmount = hammer2_vfs_unmount,
225 .vfs_root = hammer2_vfs_root,
226 .vfs_statfs = hammer2_vfs_statfs,
227 .vfs_statvfs = hammer2_vfs_statvfs,
228 .vfs_vget = hammer2_vfs_vget,
229 .vfs_vptofh = hammer2_vfs_vptofh,
230 .vfs_fhtovp = hammer2_vfs_fhtovp,
231 .vfs_checkexp = hammer2_vfs_checkexp
234 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
236 VFS_SET(hammer2_vfsops, hammer2, 0);
237 MODULE_VERSION(hammer2, 1);
241 hammer2_vfs_init(struct vfsconf *conf)
243 static struct objcache_malloc_args margs_read;
244 static struct objcache_malloc_args margs_write;
245 static struct objcache_malloc_args margs_vop;
251 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
253 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
255 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
259 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
261 margs_read.objsize = 65536;
262 margs_read.mtype = M_HAMMER2_DEBUFFER;
264 margs_write.objsize = 32768;
265 margs_write.mtype = M_HAMMER2_CBUFFER;
267 margs_vop.objsize = sizeof(hammer2_xop_t);
268 margs_vop.mtype = M_HAMMER2;
271 * Note thaht for the XOPS cache we want backing store allocations
272 * to use M_ZERO. This is not allowed in objcache_get() (to avoid
273 * confusion), so use the backing store function that does it. This
274 * means that initial XOPS objects are zerod but REUSED objects are
275 * not. So we are responsible for cleaning the object up sufficiently
276 * for our needs before objcache_put()ing it back (typically just the
279 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
280 0, 1, NULL, NULL, NULL,
281 objcache_malloc_alloc,
282 objcache_malloc_free,
284 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
285 0, 1, NULL, NULL, NULL,
286 objcache_malloc_alloc,
287 objcache_malloc_free,
289 cache_xops = objcache_create(margs_vop.mtype->ks_shortdesc,
290 0, 1, NULL, NULL, NULL,
291 objcache_malloc_alloc_zero,
292 objcache_malloc_free,
296 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
297 TAILQ_INIT(&hammer2_mntlist);
298 TAILQ_INIT(&hammer2_pfslist);
300 hammer2_limit_dirty_chains = desiredvnodes / 10;
307 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
309 objcache_destroy(cache_buffer_read);
310 objcache_destroy(cache_buffer_write);
311 objcache_destroy(cache_xops);
316 * Core PFS allocator. Used to allocate the pmp structure for PFS cluster
317 * mounts and the spmp structure for media (hmp) structures.
319 * pmp->modify_tid tracks new modify_tid transaction ids for front-end
320 * transactions. Note that synchronization does not use this field.
321 * (typically frontend operations and synchronization cannot run on the
322 * same PFS node at the same time).
327 hammer2_pfsalloc(hammer2_chain_t *chain, const hammer2_inode_data_t *ripdata,
328 hammer2_tid_t modify_tid)
330 hammer2_inode_t *iroot;
337 * Locate or create the PFS based on the cluster id. If ripdata
338 * is NULL this is a spmp which is unique and is always allocated.
341 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
342 if (bcmp(&pmp->pfs_clid, &ripdata->meta.pfs_clid,
343 sizeof(pmp->pfs_clid)) == 0) {
352 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
353 hammer2_trans_manage_init(pmp);
354 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
355 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
356 lockinit(&pmp->lock, "pfslk", 0, 0);
357 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum");
358 spin_init(&pmp->xop_spin, "h2xop");
359 RB_INIT(&pmp->inum_tree);
360 TAILQ_INIT(&pmp->unlinkq);
361 spin_init(&pmp->list_spin, "hm2pfsalloc_list");
364 * Distribute backend operations to threads
366 for (j = 0; j < HAMMER2_MAXCLUSTER; ++j)
367 TAILQ_INIT(&pmp->xopq[j]);
368 for (j = 0; j < HAMMER2_XOPGROUPS; ++j)
369 hammer2_xop_group_init(pmp, &pmp->xop_groups[j]);
372 * Save the last media transaction id for the flusher. Set
376 pmp->pfs_clid = ripdata->meta.pfs_clid;
377 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
380 * The synchronization thread may start too early, make
381 * sure it stays frozen until we are ready to let it go.
385 pmp->primary_thr.flags = HAMMER2_THREAD_FROZEN |
386 HAMMER2_THREAD_REMASTER;
391 * Create the PFS's root inode.
393 if ((iroot = pmp->iroot) == NULL) {
394 iroot = hammer2_inode_get(pmp, NULL, NULL, -1);
396 hammer2_inode_ref(iroot);
397 hammer2_inode_unlock(iroot);
401 * Stop here if no chain is passed in.
407 * When a chain is passed in we must add it to the PFS's root
408 * inode, update pmp->pfs_types[], and update the syncronization
411 * At the moment empty spots can develop due to removals or failures.
412 * Ultimately we want to re-fill these spots but doing so might
413 * confused running code. XXX
415 hammer2_inode_ref(iroot);
416 hammer2_mtx_ex(&iroot->lock);
417 j = iroot->cluster.nchains;
419 kprintf("add PFS to pmp %p[%d]\n", pmp, j);
421 if (j == HAMMER2_MAXCLUSTER) {
422 kprintf("hammer2_mount: cluster full!\n");
423 /* XXX fatal error? */
425 KKASSERT(chain->pmp == NULL);
427 hammer2_chain_ref(chain);
428 iroot->cluster.array[j].chain = chain;
429 pmp->pfs_types[j] = ripdata->meta.pfs_type;
430 pmp->pfs_names[j] = kstrdup(ripdata->filename, M_HAMMER2);
431 pmp->pfs_hmps[j] = chain->hmp;
434 * If the PFS is already mounted we must account
435 * for the mount_count here.
438 ++chain->hmp->mount_count;
441 * May have to fixup dirty chain tracking. Previous
442 * pmp was NULL so nothing to undo.
444 if (chain->flags & HAMMER2_CHAIN_MODIFIED)
445 hammer2_pfs_memory_inc(pmp);
448 iroot->cluster.nchains = j;
451 * Update nmasters from any PFS inode which is part of the cluster.
452 * It is possible that this will result in a value which is too
453 * high. MASTER PFSs are authoritative for pfs_nmasters and will
454 * override this value later on.
456 * (This informs us of masters that might not currently be
457 * discoverable by this mount).
459 if (ripdata && pmp->pfs_nmasters < ripdata->meta.pfs_nmasters) {
460 pmp->pfs_nmasters = ripdata->meta.pfs_nmasters;
464 * Count visible masters. Masters are usually added with
465 * ripdata->meta.pfs_nmasters set to 1. This detects when there
466 * are more (XXX and must update the master inodes).
469 for (i = 0; i < iroot->cluster.nchains; ++i) {
470 if (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER)
473 if (pmp->pfs_nmasters < count)
474 pmp->pfs_nmasters = count;
477 * Create missing synchronization and support threads.
479 * Single-node masters (including snapshots) have nothing to
480 * synchronize and do not require this thread.
482 * Multi-node masters or any number of soft masters, slaves, copy,
483 * or other PFS types need the thread.
485 * Each thread is responsible for its particular cluster index.
486 * We use independent threads so stalls or mismatches related to
487 * any given target do not affect other targets.
489 for (i = 0; i < iroot->cluster.nchains; ++i) {
491 * Single-node masters (including snapshots) have nothing
492 * to synchronize and will make direct xops support calls,
493 * thus they do not require this thread.
495 * Note that there can be thousands of snapshots. We do not
496 * want to create thousands of threads.
498 if (pmp->pfs_nmasters <= 1 &&
499 pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) {
504 * Sync support thread
506 if (pmp->sync_thrs[i].td == NULL) {
507 hammer2_thr_create(&pmp->sync_thrs[i], pmp,
509 hammer2_primary_sync_thread);
514 * Create missing Xop threads
517 hammer2_xop_helper_create(pmp);
519 hammer2_mtx_unlock(&iroot->lock);
520 hammer2_inode_drop(iroot);
526 * Destroy a PFS, typically only occurs after the last mount on a device
530 hammer2_pfsfree(hammer2_pfs_t *pmp)
532 hammer2_inode_t *iroot;
537 * Cleanup our reference on iroot. iroot is (should) not be needed
540 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
544 for (i = 0; i < iroot->cluster.nchains; ++i) {
545 hammer2_thr_delete(&pmp->sync_thrs[i]);
546 for (j = 0; j < HAMMER2_XOPGROUPS; ++j)
547 hammer2_thr_delete(&pmp->xop_groups[j].thrs[i]);
549 #if REPORT_REFS_ERRORS
550 if (pmp->iroot->refs != 1)
551 kprintf("PMP->IROOT %p REFS WRONG %d\n",
552 pmp->iroot, pmp->iroot->refs);
554 KKASSERT(pmp->iroot->refs == 1);
556 /* ref for pmp->iroot */
557 hammer2_inode_drop(pmp->iroot);
561 kmalloc_destroy(&pmp->mmsg);
562 kmalloc_destroy(&pmp->minode);
564 kfree(pmp, M_HAMMER2);
568 * Remove all references to hmp from the pfs list. Any PFS which becomes
569 * empty is terminated and freed.
574 hammer2_pfsfree_scan(hammer2_dev_t *hmp)
577 hammer2_inode_t *iroot;
578 hammer2_chain_t *rchain;
584 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
585 if ((iroot = pmp->iroot) == NULL)
587 if (hmp->spmp == pmp) {
588 kprintf("unmount hmp %p remove spmp %p\n",
594 * Determine if this PFS is affected. If it is we must
595 * freeze all management threads and lock its iroot.
597 * Freezing a management thread forces it idle, operations
598 * in-progress will be aborted and it will have to start
599 * over again when unfrozen, or exit if told to exit.
601 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
602 if (pmp->pfs_hmps[i] == hmp)
605 if (i != HAMMER2_MAXCLUSTER) {
607 * Make sure all synchronization threads are locked
610 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
611 if (pmp->pfs_hmps[i] == NULL)
613 hammer2_thr_freeze_async(&pmp->sync_thrs[i]);
614 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
615 hammer2_thr_freeze_async(
616 &pmp->xop_groups[j].thrs[i]);
619 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
620 if (pmp->pfs_hmps[i] == NULL)
622 hammer2_thr_freeze(&pmp->sync_thrs[i]);
623 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
625 &pmp->xop_groups[j].thrs[i]);
630 * Lock the inode and clean out matching chains.
631 * Note that we cannot use hammer2_inode_lock_*()
632 * here because that would attempt to validate the
633 * cluster that we are in the middle of ripping
636 * WARNING! We are working directly on the inodes
639 hammer2_mtx_ex(&iroot->lock);
642 * Remove the chain from matching elements of the PFS.
644 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
645 if (pmp->pfs_hmps[i] != hmp)
647 hammer2_thr_delete(&pmp->sync_thrs[i]);
648 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
650 &pmp->xop_groups[j].thrs[i]);
652 rchain = iroot->cluster.array[i].chain;
653 iroot->cluster.array[i].chain = NULL;
654 pmp->pfs_types[i] = 0;
655 if (pmp->pfs_names[i]) {
656 kfree(pmp->pfs_names[i], M_HAMMER2);
657 pmp->pfs_names[i] = NULL;
660 hammer2_chain_drop(rchain);
662 if (iroot->cluster.focus == rchain)
663 iroot->cluster.focus = NULL;
665 pmp->pfs_hmps[i] = NULL;
667 hammer2_mtx_unlock(&iroot->lock);
668 didfreeze = 1; /* remaster, unfreeze down below */
674 * Cleanup trailing chains. Gaps may remain.
676 for (i = HAMMER2_MAXCLUSTER - 1; i >= 0; --i) {
677 if (pmp->pfs_hmps[i])
680 iroot->cluster.nchains = i + 1;
683 * If the PMP has no elements remaining we can destroy it.
684 * (this will transition management threads from frozen->exit).
686 if (iroot->cluster.nchains == 0) {
687 kprintf("unmount hmp %p last ref to PMP=%p\n",
689 hammer2_pfsfree(pmp);
694 * If elements still remain we need to set the REMASTER
695 * flag and unfreeze it.
698 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
699 if (pmp->pfs_hmps[i] == NULL)
701 hammer2_thr_remaster(&pmp->sync_thrs[i]);
702 hammer2_thr_unfreeze(&pmp->sync_thrs[i]);
703 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
704 hammer2_thr_remaster(
705 &pmp->xop_groups[j].thrs[i]);
706 hammer2_thr_unfreeze(
707 &pmp->xop_groups[j].thrs[i]);
715 * Mount or remount HAMMER2 fileystem from physical media
718 * mp mount point structure
724 * mp mount point structure
725 * path path to mount point
726 * data pointer to argument structure in user space
727 * volume volume path (device@LABEL form)
728 * hflags user mount flags
729 * cred user credentials
736 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
739 struct hammer2_mount_info info;
743 hammer2_key_t key_next;
744 hammer2_key_t key_dummy;
747 struct nlookupdata nd;
748 hammer2_chain_t *parent;
749 hammer2_chain_t *chain;
750 hammer2_cluster_t *cluster;
751 const hammer2_inode_data_t *ripdata;
752 hammer2_blockref_t bref;
754 char devstr[MNAMELEN];
771 kprintf("hammer2_mount\n");
777 bzero(&info, sizeof(info));
778 info.cluster_fd = -1;
779 ksnprintf(devstr, sizeof(devstr), "%s",
780 mp->mnt_stat.f_mntfromname);
781 kprintf("hammer2_mount: root '%s'\n", devstr);
784 * Non-root mount or updating a mount
786 error = copyin(data, &info, sizeof(info));
790 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
795 /* Extract device and label */
797 label = strchr(devstr, '@');
799 ((label + 1) - dev) > done) {
807 if (mp->mnt_flag & MNT_UPDATE) {
809 * Update mount. Note that pmp->iroot->cluster is
810 * an inode-embedded cluster and thus cannot be
813 * XXX HAMMER2 needs to implement NFS export via
817 cluster = &pmp->iroot->cluster;
818 for (i = 0; i < cluster->nchains; ++i) {
819 if (cluster->array[i].chain == NULL)
821 hmp = cluster->array[i].chain->hmp;
823 error = hammer2_remount(hmp, mp, path,
835 * Lookup name and verify it refers to a block device.
838 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
840 error = nlookup(&nd);
842 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
846 cdev_t cdev = kgetdiskbyname(dev);
847 error = bdevvp(cdev, &devvp);
849 kprintf("hammer2: cannot find '%s'\n", dev);
853 if (vn_isdisk(devvp, &error))
854 error = vfs_mountedon(devvp);
858 * Determine if the device has already been mounted. After this
859 * check hmp will be non-NULL if we are doing the second or more
860 * hammer2 mounts from the same device.
862 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
863 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
864 if (hmp->devvp == devvp)
869 * Open the device if this isn't a secondary mount and construct
870 * the H2 device mount (hmp).
873 hammer2_chain_t *schain;
876 if (error == 0 && vcount(devvp) > 0)
880 * Now open the device
883 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
884 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
885 error = vinvalbuf(devvp, V_SAVE, 0, 0);
887 error = VOP_OPEN(devvp,
888 ronly ? FREAD : FREAD | FWRITE,
893 if (error && devvp) {
898 lockmgr(&hammer2_mntlk, LK_RELEASE);
901 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
902 ksnprintf(hmp->devrepname, sizeof(hmp->devrepname), "%s", dev);
905 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
906 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
907 RB_INIT(&hmp->iotree);
908 spin_init(&hmp->io_spin, "hm2mount_io");
909 spin_init(&hmp->list_spin, "hm2mount_list");
910 TAILQ_INIT(&hmp->flushq);
912 lockinit(&hmp->vollk, "h2vol", 0, 0);
913 lockinit(&hmp->bulklk, "h2bulk", 0, 0);
916 * vchain setup. vchain.data is embedded.
917 * vchain.refs is initialized and will never drop to 0.
919 * NOTE! voldata is not yet loaded.
921 hmp->vchain.hmp = hmp;
922 hmp->vchain.refs = 1;
923 hmp->vchain.data = (void *)&hmp->voldata;
924 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
925 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
926 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
928 hammer2_chain_core_init(&hmp->vchain);
929 /* hmp->vchain.u.xxx is left NULL */
932 * fchain setup. fchain.data is embedded.
933 * fchain.refs is initialized and will never drop to 0.
935 * The data is not used but needs to be initialized to
936 * pass assertion muster. We use this chain primarily
937 * as a placeholder for the freemap's top-level RBTREE
938 * so it does not interfere with the volume's topology
941 hmp->fchain.hmp = hmp;
942 hmp->fchain.refs = 1;
943 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
944 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
945 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
946 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
947 hmp->fchain.bref.methods =
948 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
949 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
951 hammer2_chain_core_init(&hmp->fchain);
952 /* hmp->fchain.u.xxx is left NULL */
955 * Install the volume header and initialize fields from
958 error = hammer2_install_volume_header(hmp);
960 hammer2_unmount_helper(mp, NULL, hmp);
961 lockmgr(&hammer2_mntlk, LK_RELEASE);
962 hammer2_vfs_unmount(mp, MNT_FORCE);
967 * Really important to get these right or flush will get
970 hmp->spmp = hammer2_pfsalloc(NULL, NULL, 0);
971 kprintf("alloc spmp %p tid %016jx\n",
972 hmp->spmp, hmp->voldata.mirror_tid);
976 * Dummy-up vchain and fchain's modify_tid. mirror_tid
977 * is inherited from the volume header.
980 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
981 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid;
982 hmp->vchain.pmp = spmp;
983 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
984 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid;
985 hmp->fchain.pmp = spmp;
988 * First locate the super-root inode, which is key 0
989 * relative to the volume header's blockset.
991 * Then locate the root inode by scanning the directory keyspace
992 * represented by the label.
994 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
995 schain = hammer2_chain_lookup(&parent, &key_dummy,
996 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
998 hammer2_chain_lookup_done(parent);
999 if (schain == NULL) {
1000 kprintf("hammer2_mount: invalid super-root\n");
1001 hammer2_unmount_helper(mp, NULL, hmp);
1002 lockmgr(&hammer2_mntlk, LK_RELEASE);
1003 hammer2_vfs_unmount(mp, MNT_FORCE);
1006 if (schain->error) {
1007 kprintf("hammer2_mount: error %s reading super-root\n",
1008 hammer2_error_str(schain->error));
1009 hammer2_chain_unlock(schain);
1010 hammer2_chain_drop(schain);
1012 hammer2_unmount_helper(mp, NULL, hmp);
1013 lockmgr(&hammer2_mntlk, LK_RELEASE);
1014 hammer2_vfs_unmount(mp, MNT_FORCE);
1019 * The super-root always uses an inode_tid of 1 when
1022 spmp->inode_tid = 1;
1023 spmp->modify_tid = schain->bref.modify_tid + 1;
1026 * Sanity-check schain's pmp and finish initialization.
1027 * Any chain belonging to the super-root topology should
1028 * have a NULL pmp (not even set to spmp).
1030 ripdata = &hammer2_chain_rdata(schain)->ipdata;
1031 KKASSERT(schain->pmp == NULL);
1032 spmp->pfs_clid = ripdata->meta.pfs_clid;
1035 * Replace the dummy spmp->iroot with a real one. It's
1036 * easier to just do a wholesale replacement than to try
1037 * to update the chain and fixup the iroot fields.
1039 * The returned inode is locked with the supplied cluster.
1041 cluster = hammer2_cluster_from_chain(schain);
1042 hammer2_inode_drop(spmp->iroot);
1044 spmp->iroot = hammer2_inode_get(spmp, NULL, cluster, -1);
1045 spmp->spmp_hmp = hmp;
1046 spmp->pfs_types[0] = ripdata->meta.pfs_type;
1047 spmp->pfs_hmps[0] = hmp;
1048 hammer2_inode_ref(spmp->iroot);
1049 hammer2_inode_unlock(spmp->iroot);
1050 hammer2_cluster_unlock(cluster);
1051 hammer2_cluster_drop(cluster);
1053 /* leave spmp->iroot with one ref */
1055 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1056 error = hammer2_recovery(hmp);
1057 /* XXX do something with error */
1059 hammer2_update_pmps(hmp);
1060 hammer2_iocom_init(hmp);
1063 * Ref the cluster management messaging descriptor. The mount
1064 * program deals with the other end of the communications pipe.
1066 * Root mounts typically do not supply one.
1068 if (info.cluster_fd >= 0) {
1069 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
1071 hammer2_cluster_reconnect(hmp, fp);
1073 kprintf("hammer2_mount: bad cluster_fd!\n");
1081 * Lookup the mount point under the media-localized super-root.
1082 * Scanning hammer2_pfslist doesn't help us because it represents
1083 * PFS cluster ids which can aggregate several named PFSs together.
1085 * cluster->pmp will incorrectly point to spmp and must be fixed
1088 hammer2_inode_lock(spmp->iroot, 0);
1089 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS);
1090 lhc = hammer2_dirhash(label, strlen(label));
1091 chain = hammer2_chain_lookup(&parent, &key_next,
1092 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1095 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1096 strcmp(label, chain->data->ipdata.filename) == 0) {
1099 chain = hammer2_chain_next(&parent, chain, &key_next,
1101 lhc + HAMMER2_DIRHASH_LOMASK,
1105 hammer2_chain_unlock(parent);
1106 hammer2_chain_drop(parent);
1108 hammer2_inode_unlock(spmp->iroot);
1111 * PFS could not be found?
1113 if (chain == NULL) {
1114 kprintf("hammer2_mount: PFS label not found\n");
1115 hammer2_unmount_helper(mp, NULL, hmp);
1116 lockmgr(&hammer2_mntlk, LK_RELEASE);
1117 hammer2_vfs_unmount(mp, MNT_FORCE);
1123 * Acquire the pmp structure (it should have already been allocated
1124 * via hammer2_update_pmps() so do not pass cluster in to add to
1125 * available chains).
1127 * Check if the cluster has already been mounted. A cluster can
1128 * only be mounted once, use null mounts to mount additional copies.
1130 ripdata = &chain->data->ipdata;
1132 pmp = hammer2_pfsalloc(NULL, ripdata, bref.modify_tid);
1133 hammer2_chain_unlock(chain);
1134 hammer2_chain_drop(chain);
1137 kprintf("hammer2_mount: PFS already mounted!\n");
1138 hammer2_unmount_helper(mp, NULL, hmp);
1139 lockmgr(&hammer2_mntlk, LK_RELEASE);
1140 hammer2_vfs_unmount(mp, MNT_FORCE);
1148 kprintf("hammer2_mount hmp=%p pmp=%p\n", hmp, pmp);
1150 mp->mnt_flag = MNT_LOCAL;
1151 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
1152 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
1155 * required mount structure initializations
1157 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
1158 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
1160 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
1161 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1166 mp->mnt_iosize_max = MAXPHYS;
1169 * Connect up mount pointers.
1171 hammer2_mount_helper(mp, pmp);
1173 lockmgr(&hammer2_mntlk, LK_RELEASE);
1179 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
1180 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
1181 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
1184 copyinstr(info.volume, mp->mnt_stat.f_mntfromname,
1185 MNAMELEN - 1, &size);
1186 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
1187 } /* else root mount, already in there */
1189 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
1191 copyinstr(path, mp->mnt_stat.f_mntonname,
1192 sizeof(mp->mnt_stat.f_mntonname) - 1,
1196 mp->mnt_stat.f_mntonname[0] = '/';
1200 * Initial statfs to prime mnt_stat.
1202 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
1208 * Scan PFSs under the super-root and create hammer2_pfs structures.
1212 hammer2_update_pmps(hammer2_dev_t *hmp)
1214 const hammer2_inode_data_t *ripdata;
1215 hammer2_chain_t *parent;
1216 hammer2_chain_t *chain;
1217 hammer2_blockref_t bref;
1218 hammer2_pfs_t *spmp;
1220 hammer2_key_t key_next;
1221 int cache_index = -1;
1224 * Lookup mount point under the media-localized super-root.
1226 * cluster->pmp will incorrectly point to spmp and must be fixed
1230 hammer2_inode_lock(spmp->iroot, 0);
1231 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS);
1232 chain = hammer2_chain_lookup(&parent, &key_next,
1233 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX,
1236 if (chain->bref.type != HAMMER2_BREF_TYPE_INODE)
1238 ripdata = &chain->data->ipdata;
1240 kprintf("ADD LOCAL PFS: %s\n", ripdata->filename);
1242 pmp = hammer2_pfsalloc(chain, ripdata, bref.modify_tid);
1243 chain = hammer2_chain_next(&parent, chain, &key_next,
1244 key_next, HAMMER2_KEY_MAX,
1248 hammer2_chain_unlock(parent);
1249 hammer2_chain_drop(parent);
1251 hammer2_inode_unlock(spmp->iroot);
1256 hammer2_remount(hammer2_dev_t *hmp, struct mount *mp, char *path __unused,
1257 struct vnode *devvp, struct ucred *cred)
1261 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
1262 error = hammer2_recovery(hmp);
1271 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1282 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1285 * If mount initialization proceeded far enough we must flush
1286 * its vnodes and sync the underlying mount points. Three syncs
1287 * are required to fully flush the filesystem (freemap updates lag
1288 * by one flush, and one extra for safety).
1290 if (mntflags & MNT_FORCE)
1295 error = vflush(mp, 0, flags);
1298 hammer2_vfs_sync(mp, MNT_WAIT);
1299 hammer2_vfs_sync(mp, MNT_WAIT);
1300 hammer2_vfs_sync(mp, MNT_WAIT);
1304 * Cleanup the frontend support XOPS threads
1306 hammer2_xop_helper_cleanup(pmp);
1309 * Cleanup our reference on ihidden.
1312 hammer2_inode_drop(pmp->ihidden);
1313 pmp->ihidden = NULL;
1316 hammer2_unmount_helper(mp, pmp, NULL);
1320 lockmgr(&hammer2_mntlk, LK_RELEASE);
1326 * Mount helper, hook the system mount into our PFS.
1327 * The mount lock is held.
1329 * We must bump the mount_count on related devices for any
1334 hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp)
1336 hammer2_cluster_t *cluster;
1337 hammer2_chain_t *rchain;
1340 mp->mnt_data = (qaddr_t)pmp;
1344 * After pmp->mp is set we have to adjust hmp->mount_count.
1346 cluster = &pmp->iroot->cluster;
1347 for (i = 0; i < cluster->nchains; ++i) {
1348 rchain = cluster->array[i].chain;
1351 ++rchain->hmp->mount_count;
1352 kprintf("hammer2_mount hmp=%p ++mount_count=%d\n",
1353 rchain->hmp, rchain->hmp->mount_count);
1357 * Create missing Xop threads
1359 hammer2_xop_helper_create(pmp);
1363 * Mount helper, unhook the system mount from our PFS.
1364 * The mount lock is held.
1366 * If hmp is supplied a mount responsible for being the first to open
1367 * the block device failed and the block device and all PFSs using the
1368 * block device must be cleaned up.
1370 * If pmp is supplied multiple devices might be backing the PFS and each
1371 * must be disconnect. This might not be the last PFS using some of the
1372 * underlying devices. Also, we have to adjust our hmp->mount_count
1373 * accounting for the devices backing the pmp which is now undergoing an
1378 hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp)
1380 hammer2_cluster_t *cluster;
1381 hammer2_chain_t *rchain;
1382 struct vnode *devvp;
1388 * If no device supplied this is a high-level unmount and we have to
1389 * to disconnect the mount, adjust mount_count, and locate devices
1390 * that might now have no mounts.
1393 KKASSERT(hmp == NULL);
1394 KKASSERT((void *)(intptr_t)mp->mnt_data == pmp);
1396 mp->mnt_data = NULL;
1399 * After pmp->mp is cleared we have to account for
1402 cluster = &pmp->iroot->cluster;
1403 for (i = 0; i < cluster->nchains; ++i) {
1404 rchain = cluster->array[i].chain;
1407 --rchain->hmp->mount_count;
1408 kprintf("hammer2_unmount hmp=%p --mount_count=%d\n",
1409 rchain->hmp, rchain->hmp->mount_count);
1410 /* scrapping hmp now may invalidate the pmp */
1413 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
1414 if (hmp->mount_count == 0) {
1415 hammer2_unmount_helper(NULL, NULL, hmp);
1423 * Try to terminate the block device. We can't terminate it if
1424 * there are still PFSs referencing it.
1426 kprintf("hammer2_unmount hmp=%p mount_count=%d\n",
1427 hmp, hmp->mount_count);
1428 if (hmp->mount_count)
1431 hammer2_pfsfree_scan(hmp);
1432 hammer2_dev_exlock(hmp); /* XXX order */
1435 * Cycle the volume data lock as a safety (probably not needed any
1436 * more). To ensure everything is out we need to flush at least
1437 * three times. (1) The running of the unlinkq can dirty the
1438 * filesystem, (2) A normal flush can dirty the freemap, and
1439 * (3) ensure that the freemap is fully synchronized.
1441 * The next mount's recovery scan can clean everything up but we want
1442 * to leave the filesystem in a 100% clean state on a normal unmount.
1445 hammer2_voldata_lock(hmp);
1446 hammer2_voldata_unlock(hmp);
1448 hammer2_iocom_uninit(hmp);
1450 if ((hmp->vchain.flags | hmp->fchain.flags) &
1451 HAMMER2_CHAIN_FLUSH_MASK) {
1452 kprintf("hammer2_unmount: chains left over "
1453 "after final sync\n");
1454 kprintf(" vchain %08x\n", hmp->vchain.flags);
1455 kprintf(" fchain %08x\n", hmp->fchain.flags);
1457 if (hammer2_debug & 0x0010)
1458 Debugger("entered debugger");
1461 KKASSERT(hmp->spmp == NULL);
1464 * Finish up with the device vnode
1466 if ((devvp = hmp->devvp) != NULL) {
1467 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1468 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1470 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL);
1477 * Clear vchain/fchain flags that might prevent final cleanup
1480 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) {
1481 atomic_clear_int(&hmp->vchain.flags,
1482 HAMMER2_CHAIN_MODIFIED);
1483 hammer2_pfs_memory_wakeup(hmp->vchain.pmp);
1484 hammer2_chain_drop(&hmp->vchain);
1486 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) {
1487 atomic_clear_int(&hmp->vchain.flags,
1488 HAMMER2_CHAIN_UPDATE);
1489 hammer2_chain_drop(&hmp->vchain);
1492 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) {
1493 atomic_clear_int(&hmp->fchain.flags,
1494 HAMMER2_CHAIN_MODIFIED);
1495 hammer2_pfs_memory_wakeup(hmp->fchain.pmp);
1496 hammer2_chain_drop(&hmp->fchain);
1498 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) {
1499 atomic_clear_int(&hmp->fchain.flags,
1500 HAMMER2_CHAIN_UPDATE);
1501 hammer2_chain_drop(&hmp->fchain);
1505 * Final drop of embedded freemap root chain to
1506 * clean up fchain.core (fchain structure is not
1507 * flagged ALLOCATED so it is cleaned out and then
1510 hammer2_chain_drop(&hmp->fchain);
1513 * Final drop of embedded volume root chain to clean
1514 * up vchain.core (vchain structure is not flagged
1515 * ALLOCATED so it is cleaned out and then left to
1519 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v');
1521 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f');
1522 hammer2_dev_unlock(hmp);
1523 hammer2_chain_drop(&hmp->vchain);
1525 hammer2_io_cleanup(hmp, &hmp->iotree);
1526 if (hmp->iofree_count) {
1527 kprintf("io_cleanup: %d I/O's left hanging\n",
1531 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1532 kmalloc_destroy(&hmp->mchain);
1533 kfree(hmp, M_HAMMER2);
1538 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1539 ino_t ino, struct vnode **vpp)
1541 kprintf("hammer2_vget\n");
1542 return (EOPNOTSUPP);
1547 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1554 if (pmp->iroot == NULL) {
1560 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED);
1562 while (pmp->inode_tid == 0) {
1563 hammer2_xop_ipcluster_t *xop;
1564 hammer2_inode_meta_t *meta;
1566 xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING);
1567 hammer2_xop_start(&xop->head, hammer2_xop_ipcluster);
1568 error = hammer2_xop_collect(&xop->head, 0);
1571 meta = &xop->head.cluster.focus->data->ipdata.meta;
1572 pmp->iroot->meta = *meta;
1573 pmp->inode_tid = meta->pfs_inum + 1;
1574 if (pmp->inode_tid < HAMMER2_INODE_START)
1575 pmp->inode_tid = HAMMER2_INODE_START;
1577 xop->head.cluster.focus->bref.modify_tid + 1;
1578 kprintf("PFS: Starting inode %jd\n",
1579 (intmax_t)pmp->inode_tid);
1580 kprintf("PMP focus good set nextino=%ld mod=%016jx\n",
1581 pmp->inode_tid, pmp->modify_tid);
1582 wakeup(&pmp->iroot);
1584 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1587 * Prime the mount info.
1589 hammer2_vfs_statfs(mp, &mp->mnt_stat, NULL);
1592 * With the cluster operational, check for and
1593 * install ihidden if needed. The install_hidden
1594 * code needs to get a transaction so we must unlock
1597 * This is only applicable PFS mounts, there is no
1598 * hidden directory in the spmp.
1600 hammer2_inode_unlock(pmp->iroot);
1601 hammer2_inode_install_hidden(pmp);
1602 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED);
1610 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1611 hammer2_inode_unlock(pmp->iroot);
1612 error = tsleep(&pmp->iroot, PCATCH, "h2root", hz);
1613 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED);
1619 hammer2_inode_unlock(pmp->iroot);
1622 vp = hammer2_igetv(pmp->iroot, &error);
1623 hammer2_inode_unlock(pmp->iroot);
1633 * XXX incorporate ipdata->meta.inode_quota and data_quota
1637 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1641 hammer2_blockref_t bref;
1645 * NOTE: iroot might not have validated the cluster yet.
1649 mp->mnt_stat.f_files = 0;
1650 mp->mnt_stat.f_ffree = 0;
1651 mp->mnt_stat.f_blocks = 0;
1652 mp->mnt_stat.f_bfree = 0;
1653 mp->mnt_stat.f_bavail = 0;
1655 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
1656 hmp = pmp->pfs_hmps[i];
1659 if (pmp->iroot->cluster.array[i].chain)
1660 bref = pmp->iroot->cluster.array[i].chain->bref;
1662 bzero(&bref, sizeof(bref));
1664 mp->mnt_stat.f_files = bref.inode_count;
1665 mp->mnt_stat.f_ffree = 0;
1666 mp->mnt_stat.f_blocks = (bref.data_count +
1667 hmp->voldata.allocator_free) /
1668 mp->mnt_vstat.f_bsize;
1669 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free /
1670 mp->mnt_vstat.f_bsize;
1671 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1673 *sbp = mp->mnt_stat;
1680 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1684 hammer2_blockref_t bref;
1688 * NOTE: iroot might not have validated the cluster yet.
1692 mp->mnt_vstat.f_bsize = 0;
1693 mp->mnt_vstat.f_files = 0;
1694 mp->mnt_vstat.f_ffree = 0;
1695 mp->mnt_vstat.f_blocks = 0;
1696 mp->mnt_vstat.f_bfree = 0;
1697 mp->mnt_vstat.f_bavail = 0;
1699 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
1700 hmp = pmp->pfs_hmps[i];
1703 if (pmp->iroot->cluster.array[i].chain)
1704 bref = pmp->iroot->cluster.array[i].chain->bref;
1706 bzero(&bref, sizeof(bref));
1708 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1709 mp->mnt_vstat.f_files = bref.inode_count;
1710 mp->mnt_vstat.f_ffree = 0;
1711 mp->mnt_vstat.f_blocks = (bref.data_count +
1712 hmp->voldata.allocator_free) /
1713 mp->mnt_vstat.f_bsize;
1714 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free /
1715 mp->mnt_vstat.f_bsize;
1716 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1718 *sbp = mp->mnt_vstat;
1724 * Mount-time recovery (RW mounts)
1726 * Updates to the free block table are allowed to lag flushes by one
1727 * transaction. In case of a crash, then on a fresh mount we must do an
1728 * incremental scan of the last committed transaction id and make sure that
1729 * all related blocks have been marked allocated.
1731 * The super-root topology and each PFS has its own transaction id domain,
1732 * so we must track PFS boundary transitions.
1734 struct hammer2_recovery_elm {
1735 TAILQ_ENTRY(hammer2_recovery_elm) entry;
1736 hammer2_chain_t *chain;
1737 hammer2_tid_t sync_tid;
1740 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
1742 struct hammer2_recovery_info {
1743 struct hammer2_recovery_list list;
1748 static int hammer2_recovery_scan(hammer2_dev_t *hmp,
1749 hammer2_chain_t *parent,
1750 struct hammer2_recovery_info *info,
1751 hammer2_tid_t sync_tid);
1753 #define HAMMER2_RECOVERY_MAXDEPTH 10
1757 hammer2_recovery(hammer2_dev_t *hmp)
1759 struct hammer2_recovery_info info;
1760 struct hammer2_recovery_elm *elm;
1761 hammer2_chain_t *parent;
1762 hammer2_tid_t sync_tid;
1763 hammer2_tid_t mirror_tid;
1765 int cumulative_error = 0;
1767 hammer2_trans_init(hmp->spmp, 0);
1769 sync_tid = hmp->voldata.freemap_tid;
1770 mirror_tid = hmp->voldata.mirror_tid;
1772 kprintf("hammer2 mount \"%s\": ", hmp->devrepname);
1773 if (sync_tid >= mirror_tid) {
1774 kprintf(" no recovery needed\n");
1776 kprintf(" freemap recovery %016jx-%016jx\n",
1777 sync_tid + 1, mirror_tid);
1780 TAILQ_INIT(&info.list);
1782 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
1783 cumulative_error = hammer2_recovery_scan(hmp, parent, &info, sync_tid);
1784 hammer2_chain_lookup_done(parent);
1786 while ((elm = TAILQ_FIRST(&info.list)) != NULL) {
1787 TAILQ_REMOVE(&info.list, elm, entry);
1788 parent = elm->chain;
1789 sync_tid = elm->sync_tid;
1790 kfree(elm, M_HAMMER2);
1792 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1793 error = hammer2_recovery_scan(hmp, parent, &info,
1794 hmp->voldata.freemap_tid);
1795 hammer2_chain_unlock(parent);
1796 hammer2_chain_drop(parent); /* drop elm->chain ref */
1798 cumulative_error = error;
1800 hammer2_trans_done(hmp->spmp);
1802 return cumulative_error;
1807 hammer2_recovery_scan(hammer2_dev_t *hmp, hammer2_chain_t *parent,
1808 struct hammer2_recovery_info *info,
1809 hammer2_tid_t sync_tid)
1811 const hammer2_inode_data_t *ripdata;
1812 hammer2_chain_t *chain;
1813 hammer2_blockref_t bref;
1815 int cumulative_error = 0;
1820 * Adjust freemap to ensure that the block(s) are marked allocated.
1822 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
1823 hammer2_freemap_adjust(hmp, &parent->bref,
1824 HAMMER2_FREEMAP_DORECOVER);
1828 * Check type for recursive scan
1830 switch(parent->bref.type) {
1831 case HAMMER2_BREF_TYPE_VOLUME:
1832 /* data already instantiated */
1834 case HAMMER2_BREF_TYPE_INODE:
1836 * Must instantiate data for DIRECTDATA test and also
1839 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1840 ripdata = &hammer2_chain_rdata(parent)->ipdata;
1841 if (ripdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1842 /* not applicable to recovery scan */
1843 hammer2_chain_unlock(parent);
1846 hammer2_chain_unlock(parent);
1848 case HAMMER2_BREF_TYPE_INDIRECT:
1850 * Must instantiate data for recursion
1852 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1853 hammer2_chain_unlock(parent);
1855 case HAMMER2_BREF_TYPE_DATA:
1856 case HAMMER2_BREF_TYPE_FREEMAP:
1857 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1858 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1859 /* not applicable to recovery scan */
1867 * Defer operation if depth limit reached or if we are crossing a
1870 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH) {
1871 struct hammer2_recovery_elm *elm;
1873 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
1874 elm->chain = parent;
1875 elm->sync_tid = sync_tid;
1876 hammer2_chain_ref(parent);
1877 TAILQ_INSERT_TAIL(&info->list, elm, entry);
1878 /* unlocked by caller */
1885 * Recursive scan of the last flushed transaction only. We are
1886 * doing this without pmp assignments so don't leave the chains
1887 * hanging around after we are done with them.
1893 while (hammer2_chain_scan(parent, &chain, &bref,
1894 &first, &cache_index,
1895 HAMMER2_LOOKUP_NODATA) != NULL) {
1899 if (chain == NULL) {
1900 if (bref.mirror_tid > sync_tid) {
1901 hammer2_freemap_adjust(hmp, &bref,
1902 HAMMER2_FREEMAP_DORECOVER);
1908 * This may or may not be a recursive node.
1910 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
1911 if (bref.mirror_tid > sync_tid) {
1913 error = hammer2_recovery_scan(hmp, chain,
1917 cumulative_error = error;
1921 * Flush the recovery at the PFS boundary to stage it for
1922 * the final flush of the super-root topology.
1924 if ((bref.flags & HAMMER2_BREF_FLAG_PFSROOT) &&
1925 (chain->flags & HAMMER2_CHAIN_ONFLUSH)) {
1926 hammer2_flush(chain, HAMMER2_FLUSH_TOP);
1930 return cumulative_error;
1934 * Sync a mount point; this is called on a per-mount basis from the
1935 * filesystem syncer process periodically and whenever a user issues
1939 hammer2_vfs_sync(struct mount *mp, int waitfor)
1941 hammer2_xop_flush_t *xop;
1942 struct hammer2_sync_info info;
1943 hammer2_inode_t *iroot;
1951 KKASSERT(iroot->pmp == pmp);
1954 * We can't acquire locks on existing vnodes while in a transaction
1955 * without risking a deadlock. This assumes that vfsync() can be
1956 * called without the vnode locked (which it can in DragonFly).
1957 * Otherwise we'd have to implement a multi-pass or flag the lock
1958 * failures and retry.
1960 * The reclamation code interlocks with the sync list's token
1961 * (by removing the vnode from the scan list) before unlocking
1962 * the inode, giving us time to ref the inode.
1964 /*flags = VMSC_GETVP;*/
1966 if (waitfor & MNT_LAZY)
1967 flags |= VMSC_ONEPASS;
1971 * Preflush the vnodes using a normal transaction before interlocking
1972 * with a flush transaction.
1974 hammer2_trans_init(pmp, 0);
1976 info.waitfor = MNT_NOWAIT;
1977 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
1978 hammer2_trans_done(pmp);
1982 * Start our flush transaction. This does not return until all
1983 * concurrent transactions have completed and will prevent any
1984 * new transactions from running concurrently, except for the
1985 * buffer cache transactions.
1987 * For efficiency do an async pass before making sure with a
1988 * synchronous pass on all related buffer cache buffers. It
1989 * should theoretically not be possible for any new file buffers
1990 * to be instantiated during this sequence.
1992 hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH |
1993 HAMMER2_TRANS_PREFLUSH);
1994 hammer2_inode_run_unlinkq(pmp);
1997 info.waitfor = MNT_NOWAIT;
1998 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
1999 info.waitfor = MNT_WAIT;
2000 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2003 * Clear PREFLUSH. This prevents (or asserts on) any new logical
2004 * buffer cache flushes which occur during the flush. Device buffers
2007 hammer2_bioq_sync(pmp);
2008 hammer2_trans_clear_preflush(pmp);
2011 * Use the XOP interface to concurrently flush all nodes to
2012 * synchronize the PFSROOT subtopology to the media. A standard
2013 * end-of-scan ENOENT error indicates cluster sufficiency.
2015 * Note that this flush will not be visible on crash recovery until
2016 * we flush the super-root topology in the next loop.
2018 * XXX For now wait for all flushes to complete.
2021 xop = hammer2_xop_alloc(iroot, HAMMER2_XOP_MODIFYING);
2022 hammer2_xop_start(&xop->head, hammer2_inode_xop_flush);
2023 error = hammer2_xop_collect(&xop->head,
2024 HAMMER2_XOP_COLLECT_WAITALL);
2025 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2026 if (error == ENOENT)
2031 hammer2_trans_done(pmp);
2039 * Note that we ignore the tranasction mtid we got above. Instead,
2040 * each vfsync below will ultimately get its own via TRANS_BUFCACHE
2044 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2046 struct hammer2_sync_info *info = data;
2047 hammer2_inode_t *ip;
2051 * Degenerate cases. Note that ip == NULL typically means the
2052 * syncer vnode itself and we don't want to vclrisdirty() in that
2059 if (vp->v_type == VNON || vp->v_type == VBAD) {
2065 * VOP_FSYNC will start a new transaction so replicate some code
2066 * here to do it inline (see hammer2_vop_fsync()).
2068 * WARNING: The vfsync interacts with the buffer cache and might
2069 * block, we can't hold the inode lock at that time.
2070 * However, we MUST ref ip before blocking to ensure that
2071 * it isn't ripped out from under us (since we do not
2072 * hold a lock on the vnode).
2074 hammer2_inode_ref(ip);
2075 if ((ip->flags & HAMMER2_INODE_MODIFIED) ||
2076 !RB_EMPTY(&vp->v_rbdirty_tree)) {
2077 vfsync(vp, info->waitfor, 1, NULL, NULL);
2078 if (ip->flags & (HAMMER2_INODE_RESIZED |
2079 HAMMER2_INODE_MODIFIED)) {
2080 hammer2_inode_lock(ip, 0);
2081 hammer2_inode_chain_sync(ip);
2082 hammer2_inode_unlock(ip);
2085 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
2086 RB_EMPTY(&vp->v_rbdirty_tree)) {
2090 hammer2_inode_drop(ip);
2094 info->error = error;
2101 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
2108 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
2109 struct fid *fhp, struct vnode **vpp)
2116 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
2117 int *exflagsp, struct ucred **credanonp)
2123 * Support code for hammer2_vfs_mount(). Read, verify, and install the volume
2124 * header into the HMP
2126 * XXX read four volhdrs and use the one with the highest TID whos CRC
2131 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2132 * nonexistant locations.
2134 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
2138 hammer2_install_volume_header(hammer2_dev_t *hmp)
2140 hammer2_volume_data_t *vd;
2142 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
2154 * There are up to 4 copies of the volume header (syncs iterate
2155 * between them so there is no single master). We don't trust the
2156 * volu_size field so we don't know precisely how large the filesystem
2157 * is, so depend on the OS to return an error if we go beyond the
2158 * block device's EOF.
2160 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
2161 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2162 HAMMER2_VOLUME_BYTES, &bp);
2169 vd = (struct hammer2_volume_data *) bp->b_data;
2170 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
2171 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
2177 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
2178 /* XXX: Reversed-endianness filesystem */
2179 kprintf("hammer2: reverse-endian filesystem detected");
2185 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
2186 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
2187 HAMMER2_VOLUME_ICRC0_SIZE);
2188 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
2189 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
2190 HAMMER2_VOLUME_ICRC1_SIZE);
2191 if ((crc0 != crc) || (bcrc0 != bcrc)) {
2192 kprintf("hammer2 volume header crc "
2193 "mismatch copy #%d %08x/%08x\n",
2200 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
2209 hmp->volsync = hmp->voldata;
2211 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
2212 kprintf("hammer2: using volume header #%d\n",
2217 kprintf("hammer2: no valid volume headers found!\n");
2223 * This handles hysteresis on regular file flushes. Because the BIOs are
2224 * routed to a thread it is possible for an excessive number to build up
2225 * and cause long front-end stalls long before the runningbuffspace limit
2226 * is hit, so we implement hammer2_flush_pipe to control the
2229 * This is a particular problem when compression is used.
2232 hammer2_lwinprog_ref(hammer2_pfs_t *pmp)
2234 atomic_add_int(&pmp->count_lwinprog, 1);
2238 hammer2_lwinprog_drop(hammer2_pfs_t *pmp)
2242 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2243 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2244 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2245 atomic_clear_int(&pmp->count_lwinprog,
2246 HAMMER2_LWINPROG_WAITING);
2247 wakeup(&pmp->count_lwinprog);
2249 if ((lwinprog & HAMMER2_LWINPROG_WAITING0) &&
2250 (lwinprog & HAMMER2_LWINPROG_MASK) <= 0) {
2251 atomic_clear_int(&pmp->count_lwinprog,
2252 HAMMER2_LWINPROG_WAITING0);
2253 wakeup(&pmp->count_lwinprog);
2258 hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int flush_pipe)
2261 int lwflag = (flush_pipe) ? HAMMER2_LWINPROG_WAITING :
2262 HAMMER2_LWINPROG_WAITING0;
2265 lwinprog = pmp->count_lwinprog;
2267 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe)
2269 tsleep_interlock(&pmp->count_lwinprog, 0);
2270 atomic_set_int(&pmp->count_lwinprog, lwflag);
2271 lwinprog = pmp->count_lwinprog;
2272 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe)
2274 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2279 * Manage excessive memory resource use for chain and related
2283 hammer2_pfs_memory_wait(hammer2_pfs_t *pmp)
2293 * Atomic check condition and wait. Also do an early speedup of
2294 * the syncer to try to avoid hitting the wait.
2297 waiting = pmp->inmem_dirty_chains;
2299 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
2301 limit = pmp->mp->mnt_nvnodelistsize / 10;
2302 if (limit < hammer2_limit_dirty_chains)
2303 limit = hammer2_limit_dirty_chains;
2308 if ((int)(ticks - zzticks) > hz) {
2310 kprintf("count %ld %ld\n", count, limit);
2315 * Block if there are too many dirty chains present, wait
2316 * for the flush to clean some out.
2318 if (count > limit) {
2319 tsleep_interlock(&pmp->inmem_dirty_chains, 0);
2320 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2322 waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
2323 speedup_syncer(pmp->mp);
2324 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
2327 continue; /* loop on success or fail */
2331 * Try to start an early flush before we are forced to block.
2333 if (count > limit * 7 / 10)
2334 speedup_syncer(pmp->mp);
2340 hammer2_pfs_memory_inc(hammer2_pfs_t *pmp)
2343 atomic_add_int(&pmp->inmem_dirty_chains, 1);
2348 hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp)
2356 waiting = pmp->inmem_dirty_chains;
2358 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2361 ~HAMMER2_DIRTYCHAIN_WAITING)) {
2366 if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
2367 wakeup(&pmp->inmem_dirty_chains);
2374 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx)
2376 hammer2_chain_t *scan;
2377 hammer2_chain_t *parent;
2381 kprintf("%*.*s...\n", tab, tab, "");
2386 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
2388 chain, chain->bref.type,
2389 chain->bref.key, chain->bref.keybits,
2390 chain->bref.mirror_tid);
2392 kprintf("%*.*s [%08x] (%s) refs=%d",
2395 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2396 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
2399 parent = chain->parent;
2401 kprintf("\n%*.*s p=%p [pflags %08x prefs %d",
2403 parent, parent->flags, parent->refs);
2404 if (RB_EMPTY(&chain->core.rbtree)) {
2408 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree)
2409 hammer2_dump_chain(scan, tab + 4, countp, 'a');
2410 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2411 kprintf("%*.*s}(%s)\n", tab, tab, "",
2412 chain->data->ipdata.filename);
2414 kprintf("%*.*s}\n", tab, tab, "");