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_meta_write;
94 long hammer2_iod_indr_write;
95 long hammer2_iod_fmap_write;
96 long hammer2_iod_volu_write;
97 long hammer2_ioa_file_read;
98 long hammer2_ioa_meta_read;
99 long hammer2_ioa_indr_read;
100 long hammer2_ioa_fmap_read;
101 long hammer2_ioa_volu_read;
102 long hammer2_ioa_fmap_write;
103 long hammer2_ioa_file_write;
104 long hammer2_ioa_meta_write;
105 long hammer2_ioa_indr_write;
106 long hammer2_ioa_volu_write;
108 MALLOC_DECLARE(M_HAMMER2_CBUFFER);
109 MALLOC_DEFINE(M_HAMMER2_CBUFFER, "HAMMER2-compbuffer",
110 "Buffer used for compression.");
112 MALLOC_DECLARE(M_HAMMER2_DEBUFFER);
113 MALLOC_DEFINE(M_HAMMER2_DEBUFFER, "HAMMER2-decompbuffer",
114 "Buffer used for decompression.");
116 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
118 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
119 &hammer2_debug, 0, "");
120 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
121 &hammer2_cluster_enable, 0, "");
122 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
123 &hammer2_hardlink_enable, 0, "");
124 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
125 &hammer2_flush_pipe, 0, "");
126 SYSCTL_INT(_vfs_hammer2, OID_AUTO, synchronous_flush, CTLFLAG_RW,
127 &hammer2_synchronous_flush, 0, "");
128 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW,
129 &hammer2_limit_dirty_chains, 0, "");
130 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD,
131 &hammer2_dio_count, 0, "");
133 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
134 &hammer2_iod_file_read, 0, "");
135 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
136 &hammer2_iod_meta_read, 0, "");
137 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
138 &hammer2_iod_indr_read, 0, "");
139 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
140 &hammer2_iod_fmap_read, 0, "");
141 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
142 &hammer2_iod_volu_read, 0, "");
144 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
145 &hammer2_iod_file_write, 0, "");
146 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
147 &hammer2_iod_meta_write, 0, "");
148 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
149 &hammer2_iod_indr_write, 0, "");
150 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
151 &hammer2_iod_fmap_write, 0, "");
152 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
153 &hammer2_iod_volu_write, 0, "");
155 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
156 &hammer2_ioa_file_read, 0, "");
157 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
158 &hammer2_ioa_meta_read, 0, "");
159 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
160 &hammer2_ioa_indr_read, 0, "");
161 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
162 &hammer2_ioa_fmap_read, 0, "");
163 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
164 &hammer2_ioa_volu_read, 0, "");
166 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
167 &hammer2_ioa_file_write, 0, "");
168 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
169 &hammer2_ioa_meta_write, 0, "");
170 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
171 &hammer2_ioa_indr_write, 0, "");
172 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
173 &hammer2_ioa_fmap_write, 0, "");
174 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
175 &hammer2_ioa_volu_write, 0, "");
177 static int hammer2_vfs_init(struct vfsconf *conf);
178 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
179 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
181 static int hammer2_remount(hammer2_dev_t *, struct mount *, char *,
182 struct vnode *, struct ucred *);
183 static int hammer2_recovery(hammer2_dev_t *hmp);
184 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
185 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
186 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
188 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
190 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
191 ino_t ino, struct vnode **vpp);
192 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
193 struct fid *fhp, struct vnode **vpp);
194 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
195 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
196 int *exflagsp, struct ucred **credanonp);
198 static int hammer2_install_volume_header(hammer2_dev_t *hmp);
199 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
201 static void hammer2_update_pmps(hammer2_dev_t *hmp);
203 static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp);
204 static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp,
208 * HAMMER2 vfs operations.
210 static struct vfsops hammer2_vfsops = {
211 .vfs_init = hammer2_vfs_init,
212 .vfs_uninit = hammer2_vfs_uninit,
213 .vfs_sync = hammer2_vfs_sync,
214 .vfs_mount = hammer2_vfs_mount,
215 .vfs_unmount = hammer2_vfs_unmount,
216 .vfs_root = hammer2_vfs_root,
217 .vfs_statfs = hammer2_vfs_statfs,
218 .vfs_statvfs = hammer2_vfs_statvfs,
219 .vfs_vget = hammer2_vfs_vget,
220 .vfs_vptofh = hammer2_vfs_vptofh,
221 .vfs_fhtovp = hammer2_vfs_fhtovp,
222 .vfs_checkexp = hammer2_vfs_checkexp
225 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
227 VFS_SET(hammer2_vfsops, hammer2, 0);
228 MODULE_VERSION(hammer2, 1);
232 hammer2_vfs_init(struct vfsconf *conf)
234 static struct objcache_malloc_args margs_read;
235 static struct objcache_malloc_args margs_write;
236 static struct objcache_malloc_args margs_vop;
242 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
244 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
246 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
250 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
252 margs_read.objsize = 65536;
253 margs_read.mtype = M_HAMMER2_DEBUFFER;
255 margs_write.objsize = 32768;
256 margs_write.mtype = M_HAMMER2_CBUFFER;
258 margs_vop.objsize = sizeof(hammer2_xop_t);
259 margs_vop.mtype = M_HAMMER2;
262 * Note thaht for the XOPS cache we want backing store allocations
263 * to use M_ZERO. This is not allowed in objcache_get() (to avoid
264 * confusion), so use the backing store function that does it. This
265 * means that initial XOPS objects are zerod but REUSED objects are
266 * not. So we are responsible for cleaning the object up sufficiently
267 * for our needs before objcache_put()ing it back (typically just the
270 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
271 0, 1, NULL, NULL, NULL,
272 objcache_malloc_alloc,
273 objcache_malloc_free,
275 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
276 0, 1, NULL, NULL, NULL,
277 objcache_malloc_alloc,
278 objcache_malloc_free,
280 cache_xops = objcache_create(margs_vop.mtype->ks_shortdesc,
281 0, 1, NULL, NULL, NULL,
282 objcache_malloc_alloc_zero,
283 objcache_malloc_free,
287 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
288 TAILQ_INIT(&hammer2_mntlist);
289 TAILQ_INIT(&hammer2_pfslist);
291 hammer2_limit_dirty_chains = desiredvnodes / 10;
298 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
300 objcache_destroy(cache_buffer_read);
301 objcache_destroy(cache_buffer_write);
302 objcache_destroy(cache_xops);
307 * Core PFS allocator. Used to allocate the pmp structure for PFS cluster
308 * mounts and the spmp structure for media (hmp) structures.
310 * pmp->modify_tid tracks new modify_tid transaction ids for front-end
311 * transactions. Note that synchronization does not use this field.
312 * (typically frontend operations and synchronization cannot run on the
313 * same PFS node at the same time).
318 hammer2_pfsalloc(hammer2_cluster_t *cluster,
319 const hammer2_inode_data_t *ripdata,
320 hammer2_tid_t modify_tid)
322 hammer2_chain_t *rchain;
323 hammer2_inode_t *iroot;
330 * Locate or create the PFS based on the cluster id. If ripdata
331 * is NULL this is a spmp which is unique and is always allocated.
334 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
335 if (bcmp(&pmp->pfs_clid, &ripdata->meta.pfs_clid,
336 sizeof(pmp->pfs_clid)) == 0) {
345 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
346 hammer2_trans_manage_init(pmp);
347 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
348 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
349 lockinit(&pmp->lock, "pfslk", 0, 0);
350 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum");
351 RB_INIT(&pmp->inum_tree);
352 TAILQ_INIT(&pmp->unlinkq);
353 spin_init(&pmp->list_spin, "hm2pfsalloc_list");
355 for (j = 0; j < HAMMER2_XOPGROUPS; ++j)
356 hammer2_xop_group_init(pmp, &pmp->xop_groups[j]);
359 * Save the last media transaction id for the flusher. Set
363 pmp->pfs_clid = ripdata->meta.pfs_clid;
364 hammer2_mtx_init(&pmp->wthread_mtx, "h2wthr");
365 bioq_init(&pmp->wthread_bioq);
366 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
369 * The synchronization thread may start too early, make
370 * sure it stays frozen until we are ready to let it go.
374 pmp->primary_thr.flags = HAMMER2_THREAD_FROZEN |
375 HAMMER2_THREAD_REMASTER;
380 * Create the PFS's root inode.
382 if ((iroot = pmp->iroot) == NULL) {
383 iroot = hammer2_inode_get(pmp, NULL, NULL);
385 hammer2_inode_ref(iroot);
386 hammer2_inode_unlock(iroot, NULL);
390 * Stop here if no cluster is passed in.
396 * When a cluster is passed in we must add the cluster's chains
397 * to the PFS's root inode, update pmp->pfs_types[], and update
398 * the syncronization threads.
400 * At the moment empty spots can develop due to removals or failures.
401 * Ultimately we want to re-fill these spots but doing so might
402 * confused running code. XXX
404 hammer2_inode_ref(iroot);
405 hammer2_mtx_ex(&iroot->lock);
406 j = iroot->cluster.nchains;
408 kprintf("add PFS to pmp %p[%d]\n", pmp, j);
410 for (i = 0; i < cluster->nchains; ++i) {
411 if (j == HAMMER2_MAXCLUSTER)
413 rchain = cluster->array[i].chain;
414 KKASSERT(rchain->pmp == NULL);
416 hammer2_chain_ref(rchain);
417 iroot->cluster.array[j].chain = rchain;
418 pmp->pfs_types[j] = ripdata->meta.pfs_type;
419 pmp->pfs_names[j] = kstrdup(ripdata->filename, M_HAMMER2);
422 * If the PFS is already mounted we must account
423 * for the mount_count here.
426 ++rchain->hmp->mount_count;
429 * May have to fixup dirty chain tracking. Previous
430 * pmp was NULL so nothing to undo.
432 if (rchain->flags & HAMMER2_CHAIN_MODIFIED)
433 hammer2_pfs_memory_inc(pmp);
436 iroot->cluster.nchains = j;
438 if (i != cluster->nchains) {
439 kprintf("hammer2_mount: cluster full!\n");
440 /* XXX fatal error? */
444 * Update nmasters from any PFS inode which is part of the cluster.
445 * It is possible that this will result in a value which is too
446 * high. MASTER PFSs are authoritative for pfs_nmasters and will
447 * override this value later on.
449 * (This informs us of masters that might not currently be
450 * discoverable by this mount).
452 if (ripdata && pmp->pfs_nmasters < ripdata->meta.pfs_nmasters) {
453 pmp->pfs_nmasters = ripdata->meta.pfs_nmasters;
457 * Count visible masters. Masters are usually added with
458 * ripdata->meta.pfs_nmasters set to 1. This detects when there
459 * are more (XXX and must update the master inodes).
462 for (i = 0; i < iroot->cluster.nchains; ++i) {
463 if (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER)
466 if (pmp->pfs_nmasters < count)
467 pmp->pfs_nmasters = count;
470 * Create missing synchronization and support threads.
472 * Single-node masters (including snapshots) have nothing to
473 * synchronize and do not require this thread.
475 * Multi-node masters or any number of soft masters, slaves, copy,
476 * or other PFS types need the thread.
478 * Each thread is responsible for its particular cluster index.
479 * We use independent threads so stalls or mismatches related to
480 * any given target do not affect other targets.
482 for (i = 0; i < iroot->cluster.nchains; ++i) {
484 * Single-node masters (including snapshots) have nothing
485 * to synchronize and will make direct xops support calls,
486 * thus they do not require this thread.
488 * Note that there can be thousands of snapshots. We do not
489 * want to create thousands of threads.
491 if (pmp->pfs_nmasters <= 1 &&
492 pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) {
497 * Sync support thread
499 if (pmp->sync_thrs[i].td == NULL) {
500 hammer2_thr_create(&pmp->sync_thrs[i], pmp,
502 hammer2_primary_sync_thread);
507 * Create missing Xop threads
510 hammer2_xop_helper_create(pmp);
512 hammer2_mtx_unlock(&iroot->lock);
513 hammer2_inode_drop(iroot);
519 * Destroy a PFS, typically only occurs after the last mount on a device
523 hammer2_pfsfree(hammer2_pfs_t *pmp)
525 hammer2_inode_t *iroot;
530 * Cleanup our reference on iroot. iroot is (should) not be needed
533 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
537 for (i = 0; i < iroot->cluster.nchains; ++i) {
538 hammer2_thr_delete(&pmp->sync_thrs[i]);
539 for (j = 0; j < HAMMER2_XOPGROUPS; ++j)
540 hammer2_thr_delete(&pmp->xop_groups[j].thrs[i]);
542 #if REPORT_REFS_ERRORS
543 if (pmp->iroot->refs != 1)
544 kprintf("PMP->IROOT %p REFS WRONG %d\n",
545 pmp->iroot, pmp->iroot->refs);
547 KKASSERT(pmp->iroot->refs == 1);
549 /* ref for pmp->iroot */
550 hammer2_inode_drop(pmp->iroot);
554 kmalloc_destroy(&pmp->mmsg);
555 kmalloc_destroy(&pmp->minode);
557 kfree(pmp, M_HAMMER2);
561 * Remove all references to hmp from the pfs list. Any PFS which becomes
562 * empty is terminated and freed.
567 hammer2_pfsfree_scan(hammer2_dev_t *hmp)
570 hammer2_inode_t *iroot;
571 hammer2_cluster_t *cluster;
572 hammer2_chain_t *rchain;
578 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
579 if ((iroot = pmp->iroot) == NULL)
581 if (hmp->spmp == pmp) {
582 kprintf("unmount hmp %p remove spmp %p\n",
588 * Determine if this PFS is affected. If it is we must
589 * freeze all management threads and lock its iroot.
591 * Freezing a management thread forces it idle, operations
592 * in-progress will be aborted and it will have to start
593 * over again when unfrozen, or exit if told to exit.
595 cluster = &iroot->cluster;
596 for (i = 0; i < cluster->nchains; ++i) {
597 rchain = cluster->array[i].chain;
598 if (rchain == NULL || rchain->hmp != hmp)
602 if (i != cluster->nchains) {
604 * Make sure all synchronization threads are locked
607 for (i = 0; i < iroot->cluster.nchains; ++i) {
608 hammer2_thr_freeze_async(&pmp->sync_thrs[i]);
609 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
610 hammer2_thr_freeze_async(
611 &pmp->xop_groups[j].thrs[i]);
614 for (i = 0; i < iroot->cluster.nchains; ++i) {
615 hammer2_thr_freeze(&pmp->sync_thrs[i]);
616 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
618 &pmp->xop_groups[j].thrs[i]);
623 * Lock the inode and clean out matching chains.
624 * Note that we cannot use hammer2_inode_lock_*()
625 * here because that would attempt to validate the
626 * cluster that we are in the middle of ripping
629 * WARNING! We are working directly on the inodes
632 hammer2_mtx_ex(&iroot->lock);
635 * Remove the chain from matching elements of the PFS.
637 for (i = 0; i < cluster->nchains; ++i) {
638 rchain = cluster->array[i].chain;
639 if (rchain == NULL || rchain->hmp != hmp)
641 hammer2_thr_delete(&pmp->sync_thrs[i]);
642 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
644 &pmp->xop_groups[j].thrs[i]);
646 rchain = cluster->array[i].chain;
647 cluster->array[i].chain = NULL;
648 pmp->pfs_types[i] = 0;
649 if (pmp->pfs_names[i]) {
650 kfree(pmp->pfs_names[i], M_HAMMER2);
651 pmp->pfs_names[i] = NULL;
653 hammer2_chain_drop(rchain);
656 if (cluster->focus == rchain)
657 cluster->focus = NULL;
659 hammer2_mtx_unlock(&iroot->lock);
660 didfreeze = 1; /* remaster, unfreeze down below */
666 * Cleanup trailing chains. Do not reorder chains (for now).
667 * XXX might remove more than we intended.
670 if (cluster->array[i - 1].chain)
674 cluster->nchains = i;
677 * If the PMP has no elements remaining we can destroy it.
678 * (this will transition management threads from frozen->exit).
680 if (cluster->nchains == 0) {
681 kprintf("unmount hmp %p last ref to PMP=%p\n",
683 hammer2_pfsfree(pmp);
688 * If elements still remain we need to set the REMASTER
689 * flag and unfreeze it.
692 for (i = 0; i < iroot->cluster.nchains; ++i) {
693 hammer2_thr_remaster(&pmp->sync_thrs[i]);
694 hammer2_thr_unfreeze(&pmp->sync_thrs[i]);
695 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
696 hammer2_thr_remaster(
697 &pmp->xop_groups[j].thrs[i]);
698 hammer2_thr_unfreeze(
699 &pmp->xop_groups[j].thrs[i]);
707 * Mount or remount HAMMER2 fileystem from physical media
710 * mp mount point structure
716 * mp mount point structure
717 * path path to mount point
718 * data pointer to argument structure in user space
719 * volume volume path (device@LABEL form)
720 * hflags user mount flags
721 * cred user credentials
728 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
731 struct hammer2_mount_info info;
735 hammer2_key_t key_next;
736 hammer2_key_t key_dummy;
739 struct nlookupdata nd;
740 hammer2_chain_t *parent;
741 hammer2_cluster_t *cluster;
742 hammer2_cluster_t *cparent;
743 const hammer2_inode_data_t *ripdata;
744 hammer2_blockref_t bref;
746 char devstr[MNAMELEN];
763 kprintf("hammer2_mount\n");
769 bzero(&info, sizeof(info));
770 info.cluster_fd = -1;
774 * Non-root mount or updating a mount
776 error = copyin(data, &info, sizeof(info));
780 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
784 /* Extract device and label */
786 label = strchr(devstr, '@');
788 ((label + 1) - dev) > done) {
796 if (mp->mnt_flag & MNT_UPDATE) {
798 * Update mount. Note that pmp->iroot->cluster is
799 * an inode-embedded cluster and thus cannot be
802 * XXX HAMMER2 needs to implement NFS export via
806 cluster = &pmp->iroot->cluster;
807 for (i = 0; i < cluster->nchains; ++i) {
808 if (cluster->array[i].chain == NULL)
810 hmp = cluster->array[i].chain->hmp;
812 error = hammer2_remount(hmp, mp, path,
817 /*hammer2_inode_install_hidden(pmp);*/
826 * Lookup name and verify it refers to a block device.
828 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
830 error = nlookup(&nd);
832 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
836 if (vn_isdisk(devvp, &error))
837 error = vfs_mountedon(devvp);
841 * Determine if the device has already been mounted. After this
842 * check hmp will be non-NULL if we are doing the second or more
843 * hammer2 mounts from the same device.
845 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
846 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
847 if (hmp->devvp == devvp)
852 * Open the device if this isn't a secondary mount and construct
853 * the H2 device mount (hmp).
856 hammer2_chain_t *schain;
859 if (error == 0 && vcount(devvp) > 0)
863 * Now open the device
866 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
867 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
868 error = vinvalbuf(devvp, V_SAVE, 0, 0);
870 error = VOP_OPEN(devvp,
871 ronly ? FREAD : FREAD | FWRITE,
876 if (error && devvp) {
881 lockmgr(&hammer2_mntlk, LK_RELEASE);
884 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
885 ksnprintf(hmp->devrepname, sizeof(hmp->devrepname), "%s", dev);
888 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
889 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
890 RB_INIT(&hmp->iotree);
891 spin_init(&hmp->io_spin, "hm2mount_io");
892 spin_init(&hmp->list_spin, "hm2mount_list");
893 TAILQ_INIT(&hmp->flushq);
895 lockinit(&hmp->vollk, "h2vol", 0, 0);
898 * vchain setup. vchain.data is embedded.
899 * vchain.refs is initialized and will never drop to 0.
901 * NOTE! voldata is not yet loaded.
903 hmp->vchain.hmp = hmp;
904 hmp->vchain.refs = 1;
905 hmp->vchain.data = (void *)&hmp->voldata;
906 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
907 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
908 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
910 hammer2_chain_core_init(&hmp->vchain);
911 /* hmp->vchain.u.xxx is left NULL */
914 * fchain setup. fchain.data is embedded.
915 * fchain.refs is initialized and will never drop to 0.
917 * The data is not used but needs to be initialized to
918 * pass assertion muster. We use this chain primarily
919 * as a placeholder for the freemap's top-level RBTREE
920 * so it does not interfere with the volume's topology
923 hmp->fchain.hmp = hmp;
924 hmp->fchain.refs = 1;
925 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
926 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
927 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
928 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
929 hmp->fchain.bref.methods =
930 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
931 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
933 hammer2_chain_core_init(&hmp->fchain);
934 /* hmp->fchain.u.xxx is left NULL */
937 * Install the volume header and initialize fields from
940 error = hammer2_install_volume_header(hmp);
942 hammer2_unmount_helper(mp, NULL, hmp);
943 lockmgr(&hammer2_mntlk, LK_RELEASE);
944 hammer2_vfs_unmount(mp, MNT_FORCE);
949 * Really important to get these right or flush will get
952 hmp->spmp = hammer2_pfsalloc(NULL, NULL, 0);
953 kprintf("alloc spmp %p tid %016jx\n",
954 hmp->spmp, hmp->voldata.mirror_tid);
958 * Dummy-up vchain and fchain's modify_tid. mirror_tid
959 * is inherited from the volume header.
962 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
963 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid;
964 hmp->vchain.pmp = spmp;
965 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
966 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid;
967 hmp->fchain.pmp = spmp;
970 * First locate the super-root inode, which is key 0
971 * relative to the volume header's blockset.
973 * Then locate the root inode by scanning the directory keyspace
974 * represented by the label.
976 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
977 schain = hammer2_chain_lookup(&parent, &key_dummy,
978 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
980 hammer2_chain_lookup_done(parent);
981 if (schain == NULL) {
982 kprintf("hammer2_mount: invalid super-root\n");
983 hammer2_unmount_helper(mp, NULL, hmp);
984 lockmgr(&hammer2_mntlk, LK_RELEASE);
985 hammer2_vfs_unmount(mp, MNT_FORCE);
989 kprintf("hammer2_mount: error %s reading super-root\n",
990 hammer2_error_str(schain->error));
991 hammer2_chain_unlock(schain);
992 hammer2_chain_drop(schain);
994 hammer2_unmount_helper(mp, NULL, hmp);
995 lockmgr(&hammer2_mntlk, LK_RELEASE);
996 hammer2_vfs_unmount(mp, MNT_FORCE);
1001 * The super-root always uses an inode_tid of 1 when
1004 spmp->inode_tid = 1;
1005 spmp->modify_tid = schain->bref.modify_tid + 1;
1008 * Sanity-check schain's pmp and finish initialization.
1009 * Any chain belonging to the super-root topology should
1010 * have a NULL pmp (not even set to spmp).
1012 ripdata = &hammer2_chain_rdata(schain)->ipdata;
1013 KKASSERT(schain->pmp == NULL);
1014 spmp->pfs_clid = ripdata->meta.pfs_clid;
1017 * Replace the dummy spmp->iroot with a real one. It's
1018 * easier to just do a wholesale replacement than to try
1019 * to update the chain and fixup the iroot fields.
1021 * The returned inode is locked with the supplied cluster.
1023 cluster = hammer2_cluster_from_chain(schain);
1024 hammer2_inode_drop(spmp->iroot);
1026 spmp->iroot = hammer2_inode_get(spmp, NULL, cluster);
1027 spmp->spmp_hmp = hmp;
1028 spmp->pfs_types[0] = ripdata->meta.pfs_type;
1029 hammer2_inode_ref(spmp->iroot);
1030 hammer2_inode_unlock(spmp->iroot, cluster);
1032 /* leave spmp->iroot with one ref */
1034 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1035 error = hammer2_recovery(hmp);
1036 /* XXX do something with error */
1038 hammer2_update_pmps(hmp);
1039 hammer2_iocom_init(hmp);
1042 * Ref the cluster management messaging descriptor. The mount
1043 * program deals with the other end of the communications pipe.
1045 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
1047 hammer2_cluster_reconnect(hmp, fp);
1049 kprintf("hammer2_mount: bad cluster_fd!\n");
1056 * Lookup the mount point under the media-localized super-root.
1057 * Scanning hammer2_pfslist doesn't help us because it represents
1058 * PFS cluster ids which can aggregate several named PFSs together.
1060 * cluster->pmp will incorrectly point to spmp and must be fixed
1063 hammer2_inode_lock(spmp->iroot, HAMMER2_RESOLVE_ALWAYS);
1064 cparent = hammer2_inode_cluster(spmp->iroot, HAMMER2_RESOLVE_ALWAYS);
1065 lhc = hammer2_dirhash(label, strlen(label));
1066 cluster = hammer2_cluster_lookup(cparent, &key_next,
1067 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1070 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE &&
1072 hammer2_cluster_rdata(cluster)->ipdata.filename) == 0) {
1075 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1077 lhc + HAMMER2_DIRHASH_LOMASK, 0);
1079 hammer2_inode_unlock(spmp->iroot, cparent);
1082 * PFS could not be found?
1084 if (cluster == NULL) {
1085 kprintf("hammer2_mount: PFS label not found\n");
1086 hammer2_unmount_helper(mp, NULL, hmp);
1087 lockmgr(&hammer2_mntlk, LK_RELEASE);
1088 hammer2_vfs_unmount(mp, MNT_FORCE);
1094 * Acquire the pmp structure (it should have already been allocated
1095 * via hammer2_update_pmps() so do not pass cluster in to add to
1096 * available chains).
1098 * Check if the cluster has already been mounted. A cluster can
1099 * only be mounted once, use null mounts to mount additional copies.
1101 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1102 hammer2_cluster_bref(cluster, &bref);
1103 pmp = hammer2_pfsalloc(NULL, ripdata, bref.modify_tid);
1104 hammer2_cluster_unlock(cluster);
1105 hammer2_cluster_drop(cluster);
1108 kprintf("hammer2_mount: PFS already mounted!\n");
1109 hammer2_unmount_helper(mp, NULL, hmp);
1110 lockmgr(&hammer2_mntlk, LK_RELEASE);
1111 hammer2_vfs_unmount(mp, MNT_FORCE);
1119 kprintf("hammer2_mount hmp=%p pmp=%p\n", hmp, pmp);
1121 mp->mnt_flag = MNT_LOCAL;
1122 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
1123 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
1126 * required mount structure initializations
1128 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
1129 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
1131 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
1132 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1137 mp->mnt_iosize_max = MAXPHYS;
1140 * Connect up mount pointers.
1142 hammer2_mount_helper(mp, pmp);
1144 lockmgr(&hammer2_mntlk, LK_RELEASE);
1147 * A mounted PFS needs a write thread for logical buffers and
1148 * a hidden directory for deletions of open files. These features
1149 * are not used by unmounted PFSs.
1151 * The logical file buffer bio write thread handles things like
1152 * physical block assignment and compression.
1154 pmp->wthread_destroy = 0;
1155 lwkt_create(hammer2_write_thread, pmp,
1156 &pmp->wthread_td, NULL, 0, -1, "h2pfs-%s", label);
1159 * With the cluster operational install ihidden.
1160 * (only applicable to pfs mounts, not applicable to spmp)
1162 hammer2_inode_install_hidden(pmp);
1168 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
1169 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
1170 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
1172 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
1173 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
1174 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
1175 copyinstr(path, mp->mnt_stat.f_mntonname,
1176 sizeof(mp->mnt_stat.f_mntonname) - 1,
1180 * Initial statfs to prime mnt_stat.
1182 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
1188 * Scan PFSs under the super-root and create hammer2_pfs structures.
1192 hammer2_update_pmps(hammer2_dev_t *hmp)
1194 const hammer2_inode_data_t *ripdata;
1195 hammer2_cluster_t *cparent;
1196 hammer2_cluster_t *cluster;
1197 hammer2_blockref_t bref;
1198 hammer2_pfs_t *spmp;
1200 hammer2_key_t key_next;
1203 * Lookup mount point under the media-localized super-root.
1205 * cluster->pmp will incorrectly point to spmp and must be fixed
1209 hammer2_inode_lock(spmp->iroot, HAMMER2_RESOLVE_ALWAYS);
1210 cparent = hammer2_inode_cluster(spmp->iroot, HAMMER2_RESOLVE_ALWAYS);
1211 cluster = hammer2_cluster_lookup(cparent, &key_next,
1216 if (hammer2_cluster_type(cluster) != HAMMER2_BREF_TYPE_INODE)
1218 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1219 hammer2_cluster_bref(cluster, &bref);
1220 kprintf("ADD LOCAL PFS: %s\n", ripdata->filename);
1222 pmp = hammer2_pfsalloc(cluster, ripdata, bref.modify_tid);
1223 cluster = hammer2_cluster_next(cparent, cluster,
1229 hammer2_inode_unlock(spmp->iroot, cparent);
1234 hammer2_remount(hammer2_dev_t *hmp, struct mount *mp, char *path,
1235 struct vnode *devvp, struct ucred *cred)
1239 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
1240 error = hammer2_recovery(hmp);
1249 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1260 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1263 * If mount initialization proceeded far enough we must flush
1264 * its vnodes and sync the underlying mount points. Three syncs
1265 * are required to fully flush the filesystem (freemap updates lag
1266 * by one flush, and one extra for safety).
1268 if (mntflags & MNT_FORCE)
1273 error = vflush(mp, 0, flags);
1276 hammer2_vfs_sync(mp, MNT_WAIT);
1277 hammer2_vfs_sync(mp, MNT_WAIT);
1278 hammer2_vfs_sync(mp, MNT_WAIT);
1281 if (pmp->wthread_td) {
1282 hammer2_mtx_ex(&pmp->wthread_mtx);
1283 pmp->wthread_destroy = 1;
1284 wakeup(&pmp->wthread_bioq);
1285 while (pmp->wthread_destroy != -1) {
1286 mtxsleep(&pmp->wthread_destroy,
1287 &pmp->wthread_mtx, 0,
1290 hammer2_mtx_unlock(&pmp->wthread_mtx);
1291 pmp->wthread_td = NULL;
1295 * Cleanup the frontend support XOPS threads
1297 hammer2_xop_helper_cleanup(pmp);
1300 * Cleanup our reference on ihidden.
1303 hammer2_inode_drop(pmp->ihidden);
1304 pmp->ihidden = NULL;
1307 hammer2_unmount_helper(mp, pmp, NULL);
1311 lockmgr(&hammer2_mntlk, LK_RELEASE);
1317 * Mount helper, hook the system mount into our PFS.
1318 * The mount lock is held.
1320 * We must bump the mount_count on related devices for any
1325 hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp)
1327 hammer2_cluster_t *cluster;
1328 hammer2_chain_t *rchain;
1331 mp->mnt_data = (qaddr_t)pmp;
1335 * After pmp->mp is set we have to adjust hmp->mount_count.
1337 cluster = &pmp->iroot->cluster;
1338 for (i = 0; i < cluster->nchains; ++i) {
1339 rchain = cluster->array[i].chain;
1342 ++rchain->hmp->mount_count;
1343 kprintf("hammer2_mount hmp=%p ++mount_count=%d\n",
1344 rchain->hmp, rchain->hmp->mount_count);
1348 * Create missing Xop threads
1350 hammer2_xop_helper_create(pmp);
1354 * Mount helper, unhook the system mount from our PFS.
1355 * The mount lock is held.
1357 * If hmp is supplied a mount responsible for being the first to open
1358 * the block device failed and the block device and all PFSs using the
1359 * block device must be cleaned up.
1361 * If pmp is supplied multiple devices might be backing the PFS and each
1362 * must be disconnect. This might not be the last PFS using some of the
1363 * underlying devices. Also, we have to adjust our hmp->mount_count
1364 * accounting for the devices backing the pmp which is now undergoing an
1369 hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp)
1371 hammer2_cluster_t *cluster;
1372 hammer2_chain_t *rchain;
1373 struct vnode *devvp;
1379 * If no device supplied this is a high-level unmount and we have to
1380 * to disconnect the mount, adjust mount_count, and locate devices
1381 * that might now have no mounts.
1384 KKASSERT(hmp == NULL);
1385 KKASSERT((void *)(intptr_t)mp->mnt_data == pmp);
1387 mp->mnt_data = NULL;
1390 * After pmp->mp is cleared we have to account for
1393 cluster = &pmp->iroot->cluster;
1394 for (i = 0; i < cluster->nchains; ++i) {
1395 rchain = cluster->array[i].chain;
1398 --rchain->hmp->mount_count;
1399 kprintf("hammer2_unmount hmp=%p --mount_count=%d\n",
1400 rchain->hmp, rchain->hmp->mount_count);
1401 /* scrapping hmp now may invalidate the pmp */
1404 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
1405 if (hmp->mount_count == 0) {
1406 hammer2_unmount_helper(NULL, NULL, hmp);
1414 * Try to terminate the block device. We can't terminate it if
1415 * there are still PFSs referencing it.
1417 kprintf("hammer2_unmount hmp=%p mount_count=%d\n",
1418 hmp, hmp->mount_count);
1419 if (hmp->mount_count)
1422 hammer2_pfsfree_scan(hmp);
1423 hammer2_dev_exlock(hmp); /* XXX order */
1426 * Cycle the volume data lock as a safety (probably not needed any
1427 * more). To ensure everything is out we need to flush at least
1428 * three times. (1) The running of the unlinkq can dirty the
1429 * filesystem, (2) A normal flush can dirty the freemap, and
1430 * (3) ensure that the freemap is fully synchronized.
1432 * The next mount's recovery scan can clean everything up but we want
1433 * to leave the filesystem in a 100% clean state on a normal unmount.
1436 hammer2_voldata_lock(hmp);
1437 hammer2_voldata_unlock(hmp);
1439 hammer2_iocom_uninit(hmp);
1441 if ((hmp->vchain.flags | hmp->fchain.flags) &
1442 HAMMER2_CHAIN_FLUSH_MASK) {
1443 kprintf("hammer2_unmount: chains left over "
1444 "after final sync\n");
1445 kprintf(" vchain %08x\n", hmp->vchain.flags);
1446 kprintf(" fchain %08x\n", hmp->fchain.flags);
1448 if (hammer2_debug & 0x0010)
1449 Debugger("entered debugger");
1452 KKASSERT(hmp->spmp == NULL);
1455 * Finish up with the device vnode
1457 if ((devvp = hmp->devvp) != NULL) {
1458 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1459 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1461 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL);
1468 * Clear vchain/fchain flags that might prevent final cleanup
1471 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) {
1472 atomic_clear_int(&hmp->vchain.flags,
1473 HAMMER2_CHAIN_MODIFIED);
1474 hammer2_pfs_memory_wakeup(hmp->vchain.pmp);
1475 hammer2_chain_drop(&hmp->vchain);
1477 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) {
1478 atomic_clear_int(&hmp->vchain.flags,
1479 HAMMER2_CHAIN_UPDATE);
1480 hammer2_chain_drop(&hmp->vchain);
1483 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) {
1484 atomic_clear_int(&hmp->fchain.flags,
1485 HAMMER2_CHAIN_MODIFIED);
1486 hammer2_pfs_memory_wakeup(hmp->fchain.pmp);
1487 hammer2_chain_drop(&hmp->fchain);
1489 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) {
1490 atomic_clear_int(&hmp->fchain.flags,
1491 HAMMER2_CHAIN_UPDATE);
1492 hammer2_chain_drop(&hmp->fchain);
1496 * Final drop of embedded freemap root chain to
1497 * clean up fchain.core (fchain structure is not
1498 * flagged ALLOCATED so it is cleaned out and then
1501 hammer2_chain_drop(&hmp->fchain);
1504 * Final drop of embedded volume root chain to clean
1505 * up vchain.core (vchain structure is not flagged
1506 * ALLOCATED so it is cleaned out and then left to
1510 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v');
1512 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f');
1513 hammer2_dev_unlock(hmp);
1514 hammer2_chain_drop(&hmp->vchain);
1516 hammer2_io_cleanup(hmp, &hmp->iotree);
1517 if (hmp->iofree_count) {
1518 kprintf("io_cleanup: %d I/O's left hanging\n",
1522 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1523 kmalloc_destroy(&hmp->mchain);
1524 kfree(hmp, M_HAMMER2);
1529 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1530 ino_t ino, struct vnode **vpp)
1532 kprintf("hammer2_vget\n");
1533 return (EOPNOTSUPP);
1538 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1541 hammer2_cluster_t *cparent;
1546 if (pmp->iroot == NULL) {
1550 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_ALWAYS |
1551 HAMMER2_RESOLVE_SHARED);
1552 cparent = hammer2_inode_cluster(pmp->iroot,
1553 HAMMER2_RESOLVE_ALWAYS |
1554 HAMMER2_RESOLVE_SHARED);
1557 * Initialize pmp->inode_tid and pmp->modify_tid on first access
1558 * to the root of mount that resolves good.
1559 * XXX probably not the best place for this.
1561 if (pmp->inode_tid == 0 &&
1562 cparent->error == 0 && cparent->focus) {
1563 const hammer2_inode_data_t *ripdata;
1564 hammer2_blockref_t bref;
1566 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
1567 hammer2_cluster_bref(cparent, &bref);
1568 pmp->inode_tid = ripdata->meta.pfs_inum + 1;
1569 if (pmp->inode_tid < HAMMER2_INODE_START)
1570 pmp->inode_tid = HAMMER2_INODE_START;
1571 pmp->modify_tid = bref.modify_tid + 1;
1572 pmp->iroot->meta = ripdata->meta;
1573 hammer2_cluster_bref(cparent, &pmp->iroot->bref);
1574 kprintf("PMP focus good set nextino=%ld mod=%016jx\n",
1575 pmp->inode_tid, pmp->modify_tid);
1578 vp = hammer2_igetv(pmp->iroot, &error);
1579 hammer2_inode_unlock(pmp->iroot, cparent);
1582 kprintf("vnodefail\n");
1591 * XXX incorporate ipdata->meta.inode_quota and data_quota
1595 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1599 hammer2_blockref_t bref;
1602 KKASSERT(pmp->iroot->cluster.nchains >= 1);
1603 hmp = pmp->iroot->cluster.focus->hmp; /* iroot retains focus */
1604 bref = pmp->iroot->cluster.focus->bref; /* no lock */
1606 mp->mnt_stat.f_files = bref.inode_count;
1607 mp->mnt_stat.f_ffree = 0;
1608 mp->mnt_stat.f_blocks = (bref.data_count +
1609 hmp->voldata.allocator_free) /
1610 mp->mnt_vstat.f_bsize;
1611 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free /
1612 mp->mnt_vstat.f_bsize;
1613 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1615 *sbp = mp->mnt_stat;
1621 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1625 hammer2_blockref_t bref;
1628 KKASSERT(pmp->iroot->cluster.nchains >= 1);
1629 hmp = pmp->iroot->cluster.focus->hmp; /* iroot retains focus */
1630 bref = pmp->iroot->cluster.focus->bref; /* no lock */
1632 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1633 mp->mnt_vstat.f_files = bref.inode_count;
1634 mp->mnt_vstat.f_ffree = 0;
1635 mp->mnt_vstat.f_blocks = (bref.data_count +
1636 hmp->voldata.allocator_free) /
1637 mp->mnt_vstat.f_bsize;
1638 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free /
1639 mp->mnt_vstat.f_bsize;
1640 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1642 *sbp = mp->mnt_vstat;
1647 * Mount-time recovery (RW mounts)
1649 * Updates to the free block table are allowed to lag flushes by one
1650 * transaction. In case of a crash, then on a fresh mount we must do an
1651 * incremental scan of the last committed transaction id and make sure that
1652 * all related blocks have been marked allocated.
1654 * The super-root topology and each PFS has its own transaction id domain,
1655 * so we must track PFS boundary transitions.
1657 struct hammer2_recovery_elm {
1658 TAILQ_ENTRY(hammer2_recovery_elm) entry;
1659 hammer2_chain_t *chain;
1660 hammer2_tid_t sync_tid;
1663 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
1665 struct hammer2_recovery_info {
1666 struct hammer2_recovery_list list;
1670 static int hammer2_recovery_scan(hammer2_dev_t *hmp,
1671 hammer2_chain_t *parent,
1672 struct hammer2_recovery_info *info,
1673 hammer2_tid_t sync_tid);
1675 #define HAMMER2_RECOVERY_MAXDEPTH 10
1679 hammer2_recovery(hammer2_dev_t *hmp)
1681 struct hammer2_recovery_info info;
1682 struct hammer2_recovery_elm *elm;
1683 hammer2_chain_t *parent;
1684 hammer2_tid_t sync_tid;
1685 hammer2_tid_t mirror_tid;
1687 int cumulative_error = 0;
1689 hammer2_trans_init(hmp->spmp, 0);
1691 sync_tid = hmp->voldata.freemap_tid;
1692 mirror_tid = hmp->voldata.mirror_tid;
1694 kprintf("hammer2 mount \"%s\": ", hmp->devrepname);
1695 if (sync_tid >= mirror_tid) {
1696 kprintf(" no recovery needed\n");
1698 kprintf(" freemap recovery %016jx-%016jx\n",
1699 sync_tid + 1, mirror_tid);
1702 TAILQ_INIT(&info.list);
1704 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
1705 cumulative_error = hammer2_recovery_scan(hmp, parent,
1707 hammer2_chain_lookup_done(parent);
1709 while ((elm = TAILQ_FIRST(&info.list)) != NULL) {
1710 TAILQ_REMOVE(&info.list, elm, entry);
1711 parent = elm->chain;
1712 sync_tid = elm->sync_tid;
1713 kfree(elm, M_HAMMER2);
1715 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1716 error = hammer2_recovery_scan(hmp, parent,
1717 &info, hmp->voldata.freemap_tid);
1718 hammer2_chain_unlock(parent);
1719 hammer2_chain_drop(parent); /* drop elm->chain ref */
1721 cumulative_error = error;
1723 hammer2_trans_done(hmp->spmp);
1725 return cumulative_error;
1730 hammer2_recovery_scan(hammer2_dev_t *hmp, hammer2_chain_t *parent,
1731 struct hammer2_recovery_info *info,
1732 hammer2_tid_t sync_tid)
1734 const hammer2_inode_data_t *ripdata;
1735 hammer2_chain_t *chain;
1737 int cumulative_error = 0;
1741 * Adjust freemap to ensure that the block(s) are marked allocated.
1743 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
1744 hammer2_freemap_adjust(hmp, &parent->bref,
1745 HAMMER2_FREEMAP_DORECOVER);
1749 * Check type for recursive scan
1751 switch(parent->bref.type) {
1752 case HAMMER2_BREF_TYPE_VOLUME:
1753 /* data already instantiated */
1755 case HAMMER2_BREF_TYPE_INODE:
1757 * Must instantiate data for DIRECTDATA test and also
1760 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1761 ripdata = &hammer2_chain_rdata(parent)->ipdata;
1762 if (ripdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1763 /* not applicable to recovery scan */
1764 hammer2_chain_unlock(parent);
1767 hammer2_chain_unlock(parent);
1769 case HAMMER2_BREF_TYPE_INDIRECT:
1771 * Must instantiate data for recursion
1773 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1774 hammer2_chain_unlock(parent);
1776 case HAMMER2_BREF_TYPE_DATA:
1777 case HAMMER2_BREF_TYPE_FREEMAP:
1778 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1779 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1780 /* not applicable to recovery scan */
1788 * Defer operation if depth limit reached or if we are crossing a
1791 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH) {
1792 struct hammer2_recovery_elm *elm;
1794 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
1795 elm->chain = parent;
1796 elm->sync_tid = sync_tid;
1797 hammer2_chain_ref(parent);
1798 TAILQ_INSERT_TAIL(&info->list, elm, entry);
1799 /* unlocked by caller */
1806 * Recursive scan of the last flushed transaction only. We are
1807 * doing this without pmp assignments so don't leave the chains
1808 * hanging around after we are done with them.
1811 chain = hammer2_chain_scan(parent, NULL, &cache_index,
1812 HAMMER2_LOOKUP_NODATA);
1814 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
1815 if (chain->bref.mirror_tid > sync_tid) {
1817 error = hammer2_recovery_scan(hmp, chain,
1821 cumulative_error = error;
1825 * Flush the recovery at the PFS boundary to stage it for
1826 * the final flush of the super-root topology.
1828 if ((chain->bref.flags & HAMMER2_BREF_FLAG_PFSROOT) &&
1829 (chain->flags & HAMMER2_CHAIN_ONFLUSH)) {
1830 hammer2_flush(chain, 1);
1832 chain = hammer2_chain_scan(parent, chain, &cache_index,
1833 HAMMER2_LOOKUP_NODATA);
1836 return cumulative_error;
1840 * Sync a mount point; this is called on a per-mount basis from the
1841 * filesystem syncer process periodically and whenever a user issues
1845 hammer2_vfs_sync(struct mount *mp, int waitfor)
1847 struct hammer2_sync_info info;
1848 hammer2_inode_t *iroot;
1849 hammer2_chain_t *chain;
1850 hammer2_chain_t *parent;
1862 KKASSERT(iroot->pmp == pmp);
1865 * We can't acquire locks on existing vnodes while in a transaction
1866 * without risking a deadlock. This assumes that vfsync() can be
1867 * called without the vnode locked (which it can in DragonFly).
1868 * Otherwise we'd have to implement a multi-pass or flag the lock
1869 * failures and retry.
1871 * The reclamation code interlocks with the sync list's token
1872 * (by removing the vnode from the scan list) before unlocking
1873 * the inode, giving us time to ref the inode.
1875 /*flags = VMSC_GETVP;*/
1877 if (waitfor & MNT_LAZY)
1878 flags |= VMSC_ONEPASS;
1882 * Preflush the vnodes using a normal transaction before interlocking
1883 * with a flush transaction.
1885 hammer2_trans_init(pmp, 0);
1887 info.waitfor = MNT_NOWAIT;
1888 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
1889 hammer2_trans_done(pmp);
1893 * Start our flush transaction. This does not return until all
1894 * concurrent transactions have completed and will prevent any
1895 * new transactions from running concurrently, except for the
1896 * buffer cache transactions.
1898 * For efficiency do an async pass before making sure with a
1899 * synchronous pass on all related buffer cache buffers. It
1900 * should theoretically not be possible for any new file buffers
1901 * to be instantiated during this sequence.
1903 hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH |
1904 HAMMER2_TRANS_PREFLUSH);
1905 hammer2_run_unlinkq(pmp);
1908 info.waitfor = MNT_NOWAIT;
1909 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
1910 info.waitfor = MNT_WAIT;
1911 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
1914 * Clear PREFLUSH. This prevents (or asserts on) any new logical
1915 * buffer cache flushes which occur during the flush. Device buffers
1918 hammer2_bioq_sync(pmp);
1919 atomic_clear_int(&pmp->trans.flags, HAMMER2_TRANS_PREFLUSH);
1924 * Flush all nodes to synchronize the PFSROOT subtopology to the media.
1926 * Note that this flush will not be visible on crash recovery until
1927 * we flush the super-root topology in the next loop.
1929 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
1930 chain = iroot->cluster.array[i].chain;
1934 hammer2_chain_ref(chain);
1935 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1936 if (chain->flags & HAMMER2_CHAIN_FLUSH_MASK) {
1937 hammer2_flush(chain, 1);
1938 parent = chain->parent;
1939 KKASSERT(chain->pmp != parent->pmp);
1940 hammer2_chain_setflush(parent);
1942 hammer2_chain_unlock(chain);
1943 hammer2_chain_drop(chain);
1945 hammer2_trans_done(pmp);
1948 * Flush all volume roots to synchronize PFS flushes with the
1949 * storage media volume header. This will flush the freemap and
1950 * the superroot topology but stops when it reaches a PFSROOT
1951 * (which we already flushed above).
1953 * This is the last step which connects the volume root to the
1954 * PFSROOT dirs flushed above.
1956 * Each spmp (representing the hmp's super-root) requires its own
1959 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
1960 hammer2_chain_t *tmp;
1962 chain = iroot->cluster.array[i].chain;
1969 * We only have to flush each hmp once
1971 for (j = i - 1; j >= 0; --j) {
1972 if ((tmp = iroot->cluster.array[j].chain) != NULL) {
1973 if (tmp->hmp == hmp)
1981 * spmp transaction. The super-root is never directly
1982 * mounted so there shouldn't be any vnodes, let alone any
1983 * dirty vnodes associated with it.
1985 hammer2_trans_init(hmp->spmp, HAMMER2_TRANS_ISFLUSH);
1988 * Media mounts have two 'roots', vchain for the topology
1989 * and fchain for the free block table. Flush both.
1991 * Note that the topology and free block table are handled
1992 * independently, so the free block table can wind up being
1993 * ahead of the topology. We depend on the bulk free scan
1994 * code to deal with any loose ends.
1996 hammer2_chain_ref(&hmp->vchain);
1997 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
1998 hammer2_chain_ref(&hmp->fchain);
1999 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
2000 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2002 * This will also modify vchain as a side effect,
2003 * mark vchain as modified now.
2005 hammer2_voldata_modify(hmp);
2006 chain = &hmp->fchain;
2007 hammer2_flush(chain, 1);
2008 KKASSERT(chain == &hmp->fchain);
2010 hammer2_chain_unlock(&hmp->fchain);
2011 hammer2_chain_unlock(&hmp->vchain);
2012 hammer2_chain_drop(&hmp->fchain);
2013 /* vchain dropped down below */
2015 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2016 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2017 chain = &hmp->vchain;
2018 hammer2_flush(chain, 1);
2019 KKASSERT(chain == &hmp->vchain);
2021 hammer2_chain_unlock(&hmp->vchain);
2022 hammer2_chain_drop(&hmp->vchain);
2027 * We can't safely flush the volume header until we have
2028 * flushed any device buffers which have built up.
2030 * XXX this isn't being incremental
2032 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
2033 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
2034 vn_unlock(hmp->devvp);
2037 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
2038 * volume header needs synchronization via hmp->volsync.
2040 * XXX synchronize the flag & data with only this flush XXX
2043 (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
2047 * Synchronize the disk before flushing the volume
2051 bp->b_bio1.bio_offset = 0;
2054 bp->b_cmd = BUF_CMD_FLUSH;
2055 bp->b_bio1.bio_done = biodone_sync;
2056 bp->b_bio1.bio_flags |= BIO_SYNC;
2057 vn_strategy(hmp->devvp, &bp->b_bio1);
2058 biowait(&bp->b_bio1, "h2vol");
2062 * Then we can safely flush the version of the
2063 * volume header synchronized by the flush code.
2065 i = hmp->volhdrno + 1;
2066 if (i >= HAMMER2_NUM_VOLHDRS)
2068 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
2069 hmp->volsync.volu_size) {
2072 kprintf("sync volhdr %d %jd\n",
2073 i, (intmax_t)hmp->volsync.volu_size);
2074 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2075 HAMMER2_PBUFSIZE, 0, 0);
2076 atomic_clear_int(&hmp->vchain.flags,
2077 HAMMER2_CHAIN_VOLUMESYNC);
2078 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
2083 total_error = error;
2085 hammer2_trans_done(hmp->spmp); /* spmp trans */
2087 return (total_error);
2094 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2096 struct hammer2_sync_info *info = data;
2097 hammer2_inode_t *ip;
2101 * Degenerate cases. Note that ip == NULL typically means the
2102 * syncer vnode itself and we don't want to vclrisdirty() in that
2109 if (vp->v_type == VNON || vp->v_type == VBAD) {
2115 * VOP_FSYNC will start a new transaction so replicate some code
2116 * here to do it inline (see hammer2_vop_fsync()).
2118 * WARNING: The vfsync interacts with the buffer cache and might
2119 * block, we can't hold the inode lock at that time.
2120 * However, we MUST ref ip before blocking to ensure that
2121 * it isn't ripped out from under us (since we do not
2122 * hold a lock on the vnode).
2124 hammer2_inode_ref(ip);
2125 if ((ip->flags & HAMMER2_INODE_MODIFIED) ||
2126 !RB_EMPTY(&vp->v_rbdirty_tree)) {
2127 vfsync(vp, info->waitfor, 1, NULL, NULL);
2128 hammer2_inode_fsync(ip, NULL);
2130 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
2131 RB_EMPTY(&vp->v_rbdirty_tree)) {
2135 hammer2_inode_drop(ip);
2139 info->error = error;
2146 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
2153 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
2154 struct fid *fhp, struct vnode **vpp)
2161 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
2162 int *exflagsp, struct ucred **credanonp)
2168 * Support code for hammer2_vfs_mount(). Read, verify, and install the volume
2169 * header into the HMP
2171 * XXX read four volhdrs and use the one with the highest TID whos CRC
2176 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2177 * nonexistant locations.
2179 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
2183 hammer2_install_volume_header(hammer2_dev_t *hmp)
2185 hammer2_volume_data_t *vd;
2187 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
2199 * There are up to 4 copies of the volume header (syncs iterate
2200 * between them so there is no single master). We don't trust the
2201 * volu_size field so we don't know precisely how large the filesystem
2202 * is, so depend on the OS to return an error if we go beyond the
2203 * block device's EOF.
2205 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
2206 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2207 HAMMER2_VOLUME_BYTES, &bp);
2214 vd = (struct hammer2_volume_data *) bp->b_data;
2215 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
2216 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
2222 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
2223 /* XXX: Reversed-endianness filesystem */
2224 kprintf("hammer2: reverse-endian filesystem detected");
2230 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
2231 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
2232 HAMMER2_VOLUME_ICRC0_SIZE);
2233 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
2234 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
2235 HAMMER2_VOLUME_ICRC1_SIZE);
2236 if ((crc0 != crc) || (bcrc0 != bcrc)) {
2237 kprintf("hammer2 volume header crc "
2238 "mismatch copy #%d %08x/%08x\n",
2245 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
2254 hmp->volsync = hmp->voldata;
2256 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
2257 kprintf("hammer2: using volume header #%d\n",
2262 kprintf("hammer2: no valid volume headers found!\n");
2268 * This handles hysteresis on regular file flushes. Because the BIOs are
2269 * routed to a thread it is possible for an excessive number to build up
2270 * and cause long front-end stalls long before the runningbuffspace limit
2271 * is hit, so we implement hammer2_flush_pipe to control the
2274 * This is a particular problem when compression is used.
2277 hammer2_lwinprog_ref(hammer2_pfs_t *pmp)
2279 atomic_add_int(&pmp->count_lwinprog, 1);
2283 hammer2_lwinprog_drop(hammer2_pfs_t *pmp)
2287 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2288 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2289 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2290 atomic_clear_int(&pmp->count_lwinprog,
2291 HAMMER2_LWINPROG_WAITING);
2292 wakeup(&pmp->count_lwinprog);
2297 hammer2_lwinprog_wait(hammer2_pfs_t *pmp)
2302 lwinprog = pmp->count_lwinprog;
2304 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2306 tsleep_interlock(&pmp->count_lwinprog, 0);
2307 atomic_set_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING);
2308 lwinprog = pmp->count_lwinprog;
2309 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2311 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2316 * Manage excessive memory resource use for chain and related
2320 hammer2_pfs_memory_wait(hammer2_pfs_t *pmp)
2330 * Atomic check condition and wait. Also do an early speedup of
2331 * the syncer to try to avoid hitting the wait.
2334 waiting = pmp->inmem_dirty_chains;
2336 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
2338 limit = pmp->mp->mnt_nvnodelistsize / 10;
2339 if (limit < hammer2_limit_dirty_chains)
2340 limit = hammer2_limit_dirty_chains;
2345 if ((int)(ticks - zzticks) > hz) {
2347 kprintf("count %ld %ld\n", count, limit);
2352 * Block if there are too many dirty chains present, wait
2353 * for the flush to clean some out.
2355 if (count > limit) {
2356 tsleep_interlock(&pmp->inmem_dirty_chains, 0);
2357 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2359 waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
2360 speedup_syncer(pmp->mp);
2361 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
2364 continue; /* loop on success or fail */
2368 * Try to start an early flush before we are forced to block.
2370 if (count > limit * 7 / 10)
2371 speedup_syncer(pmp->mp);
2377 hammer2_pfs_memory_inc(hammer2_pfs_t *pmp)
2380 atomic_add_int(&pmp->inmem_dirty_chains, 1);
2385 hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp)
2393 waiting = pmp->inmem_dirty_chains;
2395 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2398 ~HAMMER2_DIRTYCHAIN_WAITING)) {
2403 if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
2404 wakeup(&pmp->inmem_dirty_chains);
2411 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx)
2413 hammer2_chain_t *scan;
2414 hammer2_chain_t *parent;
2418 kprintf("%*.*s...\n", tab, tab, "");
2423 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
2425 chain, chain->bref.type,
2426 chain->bref.key, chain->bref.keybits,
2427 chain->bref.mirror_tid);
2429 kprintf("%*.*s [%08x] (%s) refs=%d",
2432 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2433 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
2436 parent = chain->parent;
2438 kprintf("\n%*.*s p=%p [pflags %08x prefs %d",
2440 parent, parent->flags, parent->refs);
2441 if (RB_EMPTY(&chain->core.rbtree)) {
2445 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree)
2446 hammer2_dump_chain(scan, tab + 4, countp, 'a');
2447 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2448 kprintf("%*.*s}(%s)\n", tab, tab, "",
2449 chain->data->ipdata.filename);
2451 kprintf("%*.*s}\n", tab, tab, "");