2 * Copyright (c) 2013-2015 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * The cluster module collects multiple chains representing the same
36 * information from different nodes into a single entity. It allows direct
37 * access to media data as long as it is not blockref array data (which
38 * will obviously have to be different at each node).
40 * This module also handles I/O dispatch, status rollup, and various
41 * mastership arrangements including quorum operations. It effectively
42 * presents one topology to the vnops layer.
44 * Many of the API calls mimic chain API calls but operate on clusters
45 * instead of chains. Please see hammer2_chain.c for more complete code
46 * documentation of the API functions.
48 * WARNING! This module is *extremely* complex. It must issue asynchronous
49 * locks and I/O, do quorum and/or master-slave processing, and
50 * it must operate properly even if some nodes are broken (which
51 * can also mean indefinite locks).
55 * Cluster operations can be broken down into three pieces:
57 * (1) Chain locking and data retrieval.
58 * hammer2_cluster_lock()
59 * hammer2_cluster_parent()
61 * - Most complex functions, quorum management on transaction ids.
63 * - Locking and data accesses must be internally asynchronous.
65 * - Validate and manage cache coherency primitives (cache state
66 * is stored in chain topologies but must be validated by these
69 * (2) Lookups and Scans
70 * hammer2_cluster_lookup()
71 * hammer2_cluster_next()
73 * - Depend on locking & data retrieval functions, but still complex.
75 * - Must do quorum management on transaction ids.
77 * - Lookup and Iteration ops Must be internally asynchronous.
79 * (3) Modifying Operations
80 * hammer2_cluster_create()
81 * hammer2_cluster_rename()
82 * hammer2_cluster_delete()
83 * hammer2_cluster_modify()
84 * hammer2_cluster_modsync()
86 * - Can usually punt on failures, operation continues unless quorum
87 * is lost. If quorum is lost, must wait for resynchronization
88 * (depending on the management mode).
90 * - Must disconnect node on failures (also not flush), remount, and
93 * - Network links (via kdmsg) are relatively easy to issue as the
94 * complex underworkings of hammer2_chain.c don't have to messed
95 * with (the protocol is at a higher level than block-level).
97 * - Multiple local disk nodes (i.e. block devices) are another matter.
98 * Chain operations have to be dispatched to per-node threads (xN)
99 * because we can't asynchronize potentially very complex chain
100 * operations in hammer2_chain.c (it would be a huge mess).
102 * (these threads are also used to terminate incoming kdmsg ops from
105 * - Single-node filesystems do not use threads and will simply call
106 * hammer2_chain.c functions directly. This short-cut is handled
107 * at the base of each cluster function.
109 #include <sys/cdefs.h>
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/types.h>
113 #include <sys/lock.h>
114 #include <sys/uuid.h>
119 * Returns TRUE if any chain in the cluster needs to be resized.
122 hammer2_cluster_need_resize(hammer2_cluster_t *cluster, int bytes)
124 hammer2_chain_t *chain;
127 for (i = 0; i < cluster->nchains; ++i) {
128 chain = cluster->array[i].chain;
129 if (chain && chain->bytes != bytes)
136 hammer2_cluster_type(hammer2_cluster_t *cluster)
138 return(cluster->focus->bref.type);
142 hammer2_cluster_modified(hammer2_cluster_t *cluster)
144 return((cluster->focus->flags & HAMMER2_CHAIN_MODIFIED) != 0);
148 * Return a bref representative of the cluster. Any data offset is removed
149 * (since it would only be applicable to a particular chain in the cluster).
151 * However, the radix portion of data_off is used for many purposes and will
155 hammer2_cluster_bref(hammer2_cluster_t *cluster, hammer2_blockref_t *bref)
157 *bref = cluster->focus->bref;
158 bref->data_off &= HAMMER2_OFF_MASK_RADIX;
162 * Return non-zero if the chain representing an inode has been flagged
163 * as having been unlinked. Allows the vnode reclaim to avoid loading
164 * the inode data from disk e.g. when unmount or recycling old, clean
168 hammer2_cluster_isunlinked(hammer2_cluster_t *cluster)
170 hammer2_chain_t *chain;
175 for (i = 0; i < cluster->nchains; ++i) {
176 chain = cluster->array[i].chain;
178 flags |= chain->flags;
180 return (flags & HAMMER2_CHAIN_UNLINKED);
184 hammer2_cluster_set_chainflags(hammer2_cluster_t *cluster, uint32_t flags)
186 hammer2_chain_t *chain;
189 for (i = 0; i < cluster->nchains; ++i) {
190 chain = cluster->array[i].chain;
192 atomic_set_int(&chain->flags, flags);
197 hammer2_cluster_clr_chainflags(hammer2_cluster_t *cluster, uint32_t flags)
199 hammer2_chain_t *chain;
202 for (i = 0; i < cluster->nchains; ++i) {
203 chain = cluster->array[i].chain;
205 atomic_clear_int(&chain->flags, flags);
210 hammer2_cluster_setflush(hammer2_trans_t *trans, hammer2_cluster_t *cluster)
212 hammer2_chain_t *chain;
215 for (i = 0; i < cluster->nchains; ++i) {
216 chain = cluster->array[i].chain;
218 hammer2_chain_setflush(trans, chain);
223 hammer2_cluster_setmethod_check(hammer2_trans_t *trans,
224 hammer2_cluster_t *cluster,
227 hammer2_chain_t *chain;
230 for (i = 0; i < cluster->nchains; ++i) {
231 chain = cluster->array[i].chain;
233 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
234 chain->bref.methods &= ~HAMMER2_ENC_CHECK(-1);
235 chain->bref.methods |= HAMMER2_ENC_CHECK(check_algo);
241 * Create a cluster with one ref from the specified chain. The chain
242 * is not further referenced. The caller typically supplies a locked
243 * chain and transfers ownership to the cluster.
245 * The returned cluster will be focused on the chain (strictly speaking,
246 * the focus should be NULL if the chain is not locked but we do not check
247 * for this condition).
252 hammer2_cluster_from_chain(hammer2_chain_t *chain)
254 hammer2_cluster_t *cluster;
256 cluster = kmalloc(sizeof(*cluster), M_HAMMER2, M_WAITOK | M_ZERO);
257 cluster->array[0].chain = chain;
258 cluster->nchains = 1;
259 cluster->focus = chain;
260 cluster->pmp = chain->pmp;
262 cluster->flags = HAMMER2_CLUSTER_LOCKED |
263 HAMMER2_CLUSTER_WRHARD |
264 HAMMER2_CLUSTER_RDHARD |
265 HAMMER2_CLUSTER_MSYNCED |
266 HAMMER2_CLUSTER_SSYNCED;
272 * Add a reference to a cluster.
274 * We must also ref the underlying chains in order to allow ref/unlock
275 * sequences to later re-lock.
278 hammer2_cluster_ref(hammer2_cluster_t *cluster)
280 hammer2_chain_t *chain;
283 atomic_add_int(&cluster->refs, 1);
284 for (i = 0; i < cluster->nchains; ++i) {
285 chain = cluster->array[i].chain;
287 hammer2_chain_ref(chain);
292 * Drop the caller's reference to the cluster. When the ref count drops to
293 * zero this function frees the cluster and drops all underlying chains.
295 * In-progress read I/Os are typically detached from the cluster once the
296 * first one returns (the remaining stay attached to the DIOs but are then
297 * ignored and drop naturally).
300 hammer2_cluster_drop(hammer2_cluster_t *cluster)
302 hammer2_chain_t *chain;
305 KKASSERT(cluster->refs > 0);
306 for (i = 0; i < cluster->nchains; ++i) {
307 chain = cluster->array[i].chain;
309 hammer2_chain_drop(chain);
310 if (cluster->refs == 1)
311 cluster->array[i].chain = NULL;
314 if (atomic_fetchadd_int(&cluster->refs, -1) == 1) {
315 cluster->focus = NULL; /* safety XXX chg to assert */
316 kfree(cluster, M_HAMMER2);
317 /* cluster is invalid */
322 hammer2_cluster_wait(hammer2_cluster_t *cluster)
324 tsleep(cluster->focus, 0, "h2clcw", 1);
328 * Lock and ref a cluster. This adds a ref to the cluster and its chains
329 * and then locks them, modified by various RESOLVE flags.
331 * The act of locking a cluster sets its focus.
333 * The chains making up the cluster may be narrowed down based on quorum
334 * acceptability, and if RESOLVE_RDONLY is specified the chains can be
335 * narrowed down to a single chain as long as the entire subtopology is known
336 * to be intact. So, for example, we can narrow a read-only op to a single
337 * fast SLAVE but if we focus a CACHE chain we must still retain at least
338 * a SLAVE to ensure that the subtopology can be accessed.
340 * RESOLVE_RDONLY operations are effectively as-of so the quorum does not need
341 * to be maintained once the topology is validated as-of the top level of
344 * If a failure occurs the operation must be aborted by higher-level code and
348 hammer2_cluster_lock(hammer2_cluster_t *cluster, int how)
350 hammer2_chain_t *chain;
353 /* cannot be on inode-embedded cluster template, must be on copy */
354 KKASSERT((cluster->flags & HAMMER2_CLUSTER_INODE) == 0);
355 if (cluster->flags & HAMMER2_CLUSTER_LOCKED) {
356 kprintf("hammer2_cluster_lock: cluster %p already locked!\n",
359 KKASSERT(cluster->focus == NULL);
361 atomic_set_int(&cluster->flags, HAMMER2_CLUSTER_LOCKED);
363 if ((how & HAMMER2_RESOLVE_NOREF) == 0)
364 atomic_add_int(&cluster->refs, 1);
367 * Lock chains and resolve state.
369 for (i = 0; i < cluster->nchains; ++i) {
370 chain = cluster->array[i].chain;
373 hammer2_chain_lock(chain, how);
376 hammer2_cluster_resolve(cluster);
380 hammer2_cluster_resolve(hammer2_cluster_t *cluster)
382 hammer2_chain_t *chain;
384 hammer2_tid_t quorum_tid;
408 KKASSERT(pmp != NULL || cluster->nchains == 0);
409 nquorum = pmp ? pmp->pfs_nmasters / 2 + 1 : 0;
414 for (i = 0; i < cluster->nchains; ++i) {
415 chain = cluster->array[i].chain;
419 switch (cluster->pmp->pfs_types[i]) {
420 case HAMMER2_PFSTYPE_MASTER:
422 if (quorum_tid < chain->bref.mirror_tid ||
425 quorum_tid = chain->bref.mirror_tid;
426 } else if (quorum_tid == chain->bref.mirror_tid) {
430 case HAMMER2_PFSTYPE_SLAVE:
433 case HAMMER2_PFSTYPE_SOFT_MASTER:
434 nflags |= HAMMER2_CLUSTER_WRSOFT;
435 nflags |= HAMMER2_CLUSTER_RDSOFT;
437 case HAMMER2_PFSTYPE_SOFT_SLAVE:
438 nflags |= HAMMER2_CLUSTER_RDSOFT;
440 case HAMMER2_PFSTYPE_SUPROOT:
442 * Degenerate cluster representing the super-root
443 * topology on a single device.
445 nflags |= HAMMER2_CLUSTER_WRHARD;
446 nflags |= HAMMER2_CLUSTER_RDHARD;
447 cluster->focus = chain;
448 cluster->error = chain->error;
458 for (i = 0; i < cluster->nchains; ++i) {
459 chain = cluster->array[i].chain;
463 switch (cluster->pmp->pfs_types[i]) {
464 case HAMMER2_PFSTYPE_MASTER:
466 * We must have enough up-to-date masters to reach
467 * a quorum and the master mirror_tid must match
468 * the quorum's mirror_tid.
470 * Do not select an errored master.
472 if (nmasters >= nquorum &&
474 quorum_tid == chain->bref.mirror_tid) {
475 nflags |= HAMMER2_CLUSTER_WRHARD;
476 nflags |= HAMMER2_CLUSTER_RDHARD;
477 if (cluster->focus == NULL ||
478 focus_pfs_type == HAMMER2_PFSTYPE_SLAVE) {
479 focus_pfs_type = HAMMER2_PFSTYPE_MASTER;
480 cluster->focus = chain;
481 cluster->error = chain->error;
485 case HAMMER2_PFSTYPE_SLAVE:
487 * We must have enough up-to-date masters to reach
488 * a quorum and the slave mirror_tid must match the
489 * quorum's mirror_tid.
491 * Do not select an errored slave.
493 if (nmasters >= nquorum &&
495 quorum_tid == chain->bref.mirror_tid) {
497 nflags |= HAMMER2_CLUSTER_RDHARD;
498 if (cluster->focus == NULL) {
499 focus_pfs_type = HAMMER2_PFSTYPE_SLAVE;
500 cluster->focus = chain;
501 cluster->error = chain->error;
505 case HAMMER2_PFSTYPE_SOFT_MASTER:
507 * Directly mounted soft master always wins. There
508 * should be only one.
510 KKASSERT(focus_pfs_type != HAMMER2_PFSTYPE_SOFT_MASTER);
511 cluster->focus = chain;
512 cluster->error = chain->error;
513 focus_pfs_type = HAMMER2_PFSTYPE_SOFT_MASTER;
515 case HAMMER2_PFSTYPE_SOFT_SLAVE:
517 * Directly mounted soft slave always wins. There
518 * should be only one.
520 KKASSERT(focus_pfs_type != HAMMER2_PFSTYPE_SOFT_SLAVE);
521 if (focus_pfs_type != HAMMER2_PFSTYPE_SOFT_MASTER) {
522 cluster->focus = chain;
523 cluster->error = chain->error;
524 focus_pfs_type = HAMMER2_PFSTYPE_SOFT_SLAVE;
533 * Set SSYNCED or MSYNCED for slaves and masters respectively if
534 * all available nodes (even if 0 are available) are fully
535 * synchronized. This is used by the synchronization thread to
536 * determine if there is work it could potentially accomplish.
538 if (nslaves == ttlslaves)
539 nflags |= HAMMER2_CLUSTER_SSYNCED;
540 if (nmasters == ttlmasters)
541 nflags |= HAMMER2_CLUSTER_MSYNCED;
544 * Determine if the cluster was successfully locked for the
545 * requested operation and generate an error code. The cluster
546 * will not be locked (or ref'd) if an error is returned.
548 * Caller can use hammer2_cluster_rdok() and hammer2_cluster_wrok()
549 * to determine if reading or writing is possible. If writing, the
550 * cluster still requires a call to hammer2_cluster_modify() first.
552 atomic_set_int(&cluster->flags, nflags);
553 atomic_clear_int(&cluster->flags, HAMMER2_CLUSTER_ZFLAGS & ~nflags);
557 * Copy a cluster, returned a ref'd cluster. All underlying chains
558 * are also ref'd, but not locked.
560 * The cluster focus is not set because the cluster is not yet locked
561 * (and the originating cluster does not have to be locked either).
564 hammer2_cluster_copy(hammer2_cluster_t *ocluster)
566 hammer2_pfs_t *pmp = ocluster->pmp;
567 hammer2_cluster_t *ncluster;
568 hammer2_chain_t *chain;
571 ncluster = kmalloc(sizeof(*ncluster), M_HAMMER2, M_WAITOK | M_ZERO);
573 ncluster->nchains = ocluster->nchains;
575 ncluster->flags = 0; /* cluster not locked */
577 for (i = 0; i < ocluster->nchains; ++i) {
578 chain = ocluster->array[i].chain;
579 ncluster->array[i].chain = chain;
581 hammer2_chain_ref(chain);
587 * Unlock and deref a cluster. The cluster is destroyed if this is the
591 hammer2_cluster_unlock(hammer2_cluster_t *cluster)
593 hammer2_chain_t *chain;
596 if ((cluster->flags & HAMMER2_CLUSTER_LOCKED) == 0) {
597 kprintf("hammer2_cluster_unlock: cluster %p not locked\n",
600 /* KKASSERT(cluster->flags & HAMMER2_CLUSTER_LOCKED); */
601 KKASSERT(cluster->refs > 0);
602 atomic_clear_int(&cluster->flags, HAMMER2_CLUSTER_LOCKED);
604 for (i = 0; i < cluster->nchains; ++i) {
605 chain = cluster->array[i].chain;
607 hammer2_chain_unlock(chain);
608 if (cluster->refs == 1)
609 cluster->array[i].chain = NULL; /* safety */
612 cluster->focus = NULL;
614 if (atomic_fetchadd_int(&cluster->refs, -1) == 1) {
615 kfree(cluster, M_HAMMER2);
616 /* cluster = NULL; safety */
621 * Resize the cluster's physical storage allocation in-place. This may
622 * replace the cluster's chains.
625 hammer2_cluster_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
626 hammer2_cluster_t *cparent, hammer2_cluster_t *cluster,
627 int nradix, int flags)
629 hammer2_chain_t *chain;
632 KKASSERT(cparent->pmp == cluster->pmp); /* can be NULL */
633 KKASSERT(cparent->nchains == cluster->nchains);
635 for (i = 0; i < cluster->nchains; ++i) {
636 chain = cluster->array[i].chain;
638 KKASSERT(cparent->array[i].chain);
639 hammer2_chain_resize(trans, ip,
640 cparent->array[i].chain, chain,
647 * Set an inode's cluster modified, marking the related chains RW and
648 * duplicating them if necessary.
650 * The passed-in chain is a localized copy of the chain previously acquired
651 * when the inode was locked (and possilby replaced in the mean time), and
652 * must also be updated. In fact, we update it first and then synchronize
653 * the inode's cluster cache.
655 hammer2_inode_data_t *
656 hammer2_cluster_modify_ip(hammer2_trans_t *trans, hammer2_inode_t *ip,
657 hammer2_cluster_t *cluster, int flags)
659 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
660 hammer2_cluster_modify(trans, cluster, flags);
662 hammer2_inode_repoint(ip, NULL, cluster);
665 return (&hammer2_cluster_wdata(cluster)->ipdata);
669 * Adjust the cluster's chains to allow modification and adjust the
670 * focus. Data will be accessible on return.
672 * If our focused master errors on modify, re-resolve the cluster to
673 * try to select a different master.
676 hammer2_cluster_modify(hammer2_trans_t *trans, hammer2_cluster_t *cluster,
679 hammer2_chain_t *chain;
684 for (i = 0; i < cluster->nchains; ++i) {
685 chain = cluster->array[i].chain;
687 hammer2_chain_modify(trans, chain, flags);
688 if (cluster->focus == chain &&
690 cluster->error = chain->error;
696 hammer2_cluster_resolve(cluster);
700 * Synchronize modifications from the focus to other chains in a cluster.
701 * Convenient because nominal API users can just modify the contents of the
702 * focus (at least for non-blockref data).
704 * Nominal front-end operations only edit non-block-table data in a single
705 * chain. This code copies such modifications to the other chains in the
706 * cluster. Blocktable modifications are handled on a chain-by-chain basis
707 * by both the frontend and the backend and will explode in fireworks if
711 hammer2_cluster_modsync(hammer2_cluster_t *cluster)
713 hammer2_chain_t *focus;
714 hammer2_chain_t *scan;
715 const hammer2_inode_data_t *ripdata;
716 hammer2_inode_data_t *wipdata;
719 focus = cluster->focus;
720 KKASSERT(focus->flags & HAMMER2_CHAIN_MODIFIED);
722 for (i = 0; i < cluster->nchains; ++i) {
723 scan = cluster->array[i].chain;
724 if (scan == NULL || scan == focus)
726 KKASSERT(scan->flags & HAMMER2_CHAIN_MODIFIED);
727 KKASSERT(focus->bytes == scan->bytes &&
728 focus->bref.type == scan->bref.type);
729 switch(focus->bref.type) {
730 case HAMMER2_BREF_TYPE_INODE:
731 ripdata = &focus->data->ipdata;
732 wipdata = &scan->data->ipdata;
733 if ((ripdata->op_flags &
734 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
735 bcopy(ripdata, wipdata,
736 offsetof(hammer2_inode_data_t, u));
739 /* fall through to full copy */
740 case HAMMER2_BREF_TYPE_DATA:
741 bcopy(focus->data, scan->data, focus->bytes);
743 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
744 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
745 case HAMMER2_BREF_TYPE_FREEMAP:
746 case HAMMER2_BREF_TYPE_VOLUME:
747 panic("hammer2_cluster_modsync: illegal node type");
751 panic("hammer2_cluster_modsync: unknown node type");
758 * Lookup initialization/completion API
761 hammer2_cluster_lookup_init(hammer2_cluster_t *cparent, int flags)
763 hammer2_cluster_t *cluster;
766 cluster = kmalloc(sizeof(*cluster), M_HAMMER2, M_WAITOK | M_ZERO);
767 cluster->pmp = cparent->pmp; /* can be NULL */
768 cluster->flags = 0; /* cluster not locked (yet) */
769 /* cluster->focus = NULL; already null */
771 for (i = 0; i < cparent->nchains; ++i)
772 cluster->array[i].chain = cparent->array[i].chain;
773 cluster->nchains = cparent->nchains;
776 * Independently lock (this will also give cluster 1 ref)
778 if (flags & HAMMER2_LOOKUP_SHARED) {
779 hammer2_cluster_lock(cluster, HAMMER2_RESOLVE_ALWAYS |
780 HAMMER2_RESOLVE_SHARED);
782 hammer2_cluster_lock(cluster, HAMMER2_RESOLVE_ALWAYS);
788 hammer2_cluster_lookup_done(hammer2_cluster_t *cparent)
791 hammer2_cluster_unlock(cparent);
795 * Locate first match or overlap under parent, return a new cluster
798 hammer2_cluster_lookup(hammer2_cluster_t *cparent, hammer2_key_t *key_nextp,
799 hammer2_key_t key_beg, hammer2_key_t key_end, int flags)
802 hammer2_cluster_t *cluster;
803 hammer2_chain_t *chain;
804 hammer2_key_t key_accum;
805 hammer2_key_t key_next;
806 hammer2_key_t bref_key;
813 pmp = cparent->pmp; /* can be NULL */
814 key_accum = *key_nextp;
821 cluster = kmalloc(sizeof(*cluster), M_HAMMER2, M_WAITOK | M_ZERO);
822 cluster->pmp = pmp; /* can be NULL */
824 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0)
825 cluster->flags |= HAMMER2_CLUSTER_LOCKED;
827 for (i = 0; i < cparent->nchains; ++i) {
828 key_next = *key_nextp;
829 if (cparent->array[i].chain == NULL) {
833 chain = hammer2_chain_lookup(&cparent->array[i].chain,
836 &cparent->array[i].cache_index,
838 cluster->array[i].chain = chain;
842 int ddflag = (chain->bref.type ==
843 HAMMER2_BREF_TYPE_INODE);
848 if (cluster->focus == NULL) {
849 bref_type = chain->bref.type;
850 bref_key = chain->bref.key;
851 bref_keybits = chain->bref.keybits;
852 bytes = chain->bytes;
853 cluster->ddflag = ddflag;
854 cluster->focus = chain;
858 * Override default focus to follow the parent.
860 if (cparent->focus == cparent->array[i].chain)
861 cluster->focus = chain;
863 KKASSERT(bref_type == chain->bref.type);
864 KKASSERT(bref_key == chain->bref.key);
865 KKASSERT(bref_keybits == chain->bref.keybits);
866 KKASSERT(bytes == chain->bytes);
867 KKASSERT(cluster->ddflag == ddflag);
869 if (key_accum > key_next)
870 key_accum = key_next;
872 *key_nextp = key_accum;
873 cluster->nchains = i;
874 hammer2_cluster_resolve(cluster);
876 if (null_count == i) {
877 hammer2_cluster_drop(cluster);
885 * Locate next match or overlap under parent, replace cluster
888 hammer2_cluster_next(hammer2_cluster_t *cparent, hammer2_cluster_t *cluster,
889 hammer2_key_t *key_nextp,
890 hammer2_key_t key_beg, hammer2_key_t key_end, int flags)
892 hammer2_chain_t *chain;
893 hammer2_key_t key_accum;
894 hammer2_key_t key_next;
895 hammer2_key_t bref_key;
902 key_accum = *key_nextp;
904 cluster->focus = NULL;
905 cparent->focus = NULL;
913 for (i = 0; i < cparent->nchains; ++i) {
914 key_next = *key_nextp;
915 chain = cluster->array[i].chain;
920 if (cparent->array[i].chain == NULL) {
921 if (flags & HAMMER2_LOOKUP_NOLOCK)
922 hammer2_chain_drop(chain);
924 hammer2_chain_unlock(chain);
928 chain = hammer2_chain_next(&cparent->array[i].chain, chain,
929 &key_next, key_beg, key_end,
930 &cparent->array[i].cache_index,
932 cluster->array[i].chain = chain;
936 int ddflag = (chain->bref.type ==
937 HAMMER2_BREF_TYPE_INODE);
938 if (cluster->focus == NULL) {
939 bref_type = chain->bref.type;
940 bref_key = chain->bref.key;
941 bref_keybits = chain->bref.keybits;
942 bytes = chain->bytes;
943 cluster->ddflag = ddflag;
944 cluster->focus = chain;
948 * Override default focus to follow the parent.
950 if (cparent->focus == cparent->array[i].chain)
951 cluster->focus = chain;
953 KKASSERT(bref_type == chain->bref.type);
954 KKASSERT(bref_key == chain->bref.key);
955 KKASSERT(bref_keybits == chain->bref.keybits);
956 KKASSERT(bytes == chain->bytes);
957 KKASSERT(cluster->ddflag == ddflag);
959 if (key_accum > key_next)
960 key_accum = key_next;
962 cluster->nchains = i;
963 hammer2_cluster_resolve(cluster);
965 if (null_count == i) {
966 hammer2_cluster_drop(cluster);
973 * Create a new cluster using the specified key
976 hammer2_cluster_create(hammer2_trans_t *trans, hammer2_cluster_t *cparent,
977 hammer2_cluster_t **clusterp,
978 hammer2_key_t key, int keybits,
979 int type, size_t bytes, int flags)
981 hammer2_cluster_t *cluster;
986 pmp = trans->pmp; /* can be NULL */
988 if ((cluster = *clusterp) == NULL) {
989 cluster = kmalloc(sizeof(*cluster), M_HAMMER2,
991 cluster->pmp = pmp; /* can be NULL */
993 cluster->flags = HAMMER2_CLUSTER_LOCKED;
995 cluster->focus = NULL;
998 * NOTE: cluster->array[] entries can initially be NULL. If
999 * *clusterp is supplied, skip NULL entries, otherwise
1000 * create new chains.
1002 for (i = 0; i < cparent->nchains; ++i) {
1003 if (*clusterp && cluster->array[i].chain == NULL) {
1006 error = hammer2_chain_create(trans, &cparent->array[i].chain,
1007 &cluster->array[i].chain, pmp,
1009 type, bytes, flags);
1010 KKASSERT(error == 0);
1011 if (cluster->focus == NULL)
1012 cluster->focus = cluster->array[i].chain;
1013 if (cparent->focus == cparent->array[i].chain)
1014 cluster->focus = cluster->array[i].chain;
1016 cluster->nchains = i;
1017 *clusterp = cluster;
1018 hammer2_cluster_resolve(cluster);
1024 * Rename a cluster to a new parent.
1026 * WARNING! Unlike hammer2_chain_rename(), only the key and keybits fields
1027 * are used from a passed-in non-NULL bref pointer. All other fields
1028 * are extracted from the original chain for each chain in the
1032 hammer2_cluster_rename(hammer2_trans_t *trans, hammer2_blockref_t *bref,
1033 hammer2_cluster_t *cparent, hammer2_cluster_t *cluster,
1036 hammer2_chain_t *chain;
1037 hammer2_blockref_t xbref;
1040 cluster->focus = NULL;
1041 cparent->focus = NULL;
1043 for (i = 0; i < cluster->nchains; ++i) {
1044 chain = cluster->array[i].chain;
1047 xbref = chain->bref;
1048 xbref.key = bref->key;
1049 xbref.keybits = bref->keybits;
1050 hammer2_chain_rename(trans, &xbref,
1051 &cparent->array[i].chain,
1054 hammer2_chain_rename(trans, NULL,
1055 &cparent->array[i].chain,
1058 KKASSERT(cluster->array[i].chain == chain); /*remove*/
1064 * Mark a cluster deleted
1067 hammer2_cluster_delete(hammer2_trans_t *trans, hammer2_cluster_t *cparent,
1068 hammer2_cluster_t *cluster, int flags)
1070 hammer2_chain_t *chain;
1071 hammer2_chain_t *parent;
1074 if (cparent == NULL) {
1075 kprintf("cparent is NULL\n");
1079 for (i = 0; i < cluster->nchains; ++i) {
1080 parent = (i < cparent->nchains) ?
1081 cparent->array[i].chain : NULL;
1082 chain = cluster->array[i].chain;
1085 if (chain->parent != parent) {
1086 kprintf("hammer2_cluster_delete: parent "
1087 "mismatch chain=%p parent=%p against=%p\n",
1088 chain, chain->parent, parent);
1090 hammer2_chain_delete(trans, parent, chain, flags);
1096 * Create a snapshot of the specified {parent, ochain} with the specified
1097 * label. The originating hammer2_inode must be exclusively locked for
1100 * The ioctl code has already synced the filesystem.
1103 hammer2_cluster_snapshot(hammer2_trans_t *trans, hammer2_cluster_t *ocluster,
1104 hammer2_ioc_pfs_t *pfs)
1107 hammer2_cluster_t *ncluster;
1108 const hammer2_inode_data_t *ripdata;
1109 hammer2_inode_data_t *wipdata;
1110 hammer2_chain_t *nchain;
1111 hammer2_inode_t *nip;
1121 kprintf("snapshot %s\n", pfs->name);
1123 name_len = strlen(pfs->name);
1124 lhc = hammer2_dirhash(pfs->name, name_len);
1129 ripdata = &hammer2_cluster_rdata(ocluster)->ipdata;
1131 opfs_clid = ripdata->pfs_clid;
1133 hmp = ocluster->focus->hmp; /* XXX find synchronized local disk */
1136 * Create the snapshot directory under the super-root
1138 * Set PFS type, generate a unique filesystem id, and generate
1139 * a cluster id. Use the same clid when snapshotting a PFS root,
1140 * which theoretically allows the snapshot to be used as part of
1141 * the same cluster (perhaps as a cache).
1143 * Copy the (flushed) blockref array. Theoretically we could use
1144 * chain_duplicate() but it becomes difficult to disentangle
1145 * the shared core so for now just brute-force it.
1151 nip = hammer2_inode_create(trans, hmp->spmp->iroot, &vat,
1152 proc0.p_ucred, pfs->name, name_len,
1154 HAMMER2_INSERT_PFSROOT, &error);
1157 wipdata = hammer2_cluster_modify_ip(trans, nip, ncluster, 0);
1158 wipdata->pfs_type = HAMMER2_PFSTYPE_MASTER;
1159 wipdata->pfs_subtype = HAMMER2_PFSSUBTYPE_SNAPSHOT;
1160 wipdata->op_flags |= HAMMER2_OPFLAG_PFSROOT;
1161 kern_uuidgen(&wipdata->pfs_fsid, 1);
1164 * Give the snapshot its own private cluster. As a snapshot
1165 * no further synchronization with the original cluster will
1169 if (ocluster->focus->flags & HAMMER2_CHAIN_PFSBOUNDARY)
1170 wipdata->pfs_clid = opfs_clid;
1172 kern_uuidgen(&wipdata->pfs_clid, 1);
1174 kern_uuidgen(&wipdata->pfs_clid, 1);
1176 for (i = 0; i < ncluster->nchains; ++i) {
1177 nchain = ncluster->array[i].chain;
1179 nchain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
1182 /* XXX can't set this unless we do an explicit flush, which
1183 we also need a pmp assigned to do, else the flush code
1184 won't flush ncluster because it thinks it is crossing a
1186 hammer2_cluster_set_chainflags(ncluster,
1187 HAMMER2_CHAIN_PFSBOUNDARY);
1190 /* XXX hack blockset copy */
1191 /* XXX doesn't work with real cluster */
1192 KKASSERT(ocluster->nchains == 1);
1193 wipdata->u.blockset = ripdata->u.blockset;
1194 hammer2_cluster_modsync(ncluster);
1195 for (i = 0; i < ncluster->nchains; ++i) {
1196 nchain = ncluster->array[i].chain;
1198 hammer2_flush(trans, nchain);
1200 hammer2_inode_unlock_ex(nip, ncluster);
1206 * Return locked parent cluster given a locked child. The child remains
1207 * locked on return. The new parent's focus follows the child's focus
1208 * and the parent is always resolved.
1211 hammer2_cluster_parent(hammer2_cluster_t *cluster)
1213 hammer2_cluster_t *cparent;
1216 cparent = hammer2_cluster_copy(cluster);
1218 for (i = 0; i < cparent->nchains; ++i) {
1219 hammer2_chain_t *chain;
1220 hammer2_chain_t *rchain;
1223 * Calculate parent for each element. Old chain has an extra
1224 * ref for cparent but the lock remains with cluster.
1226 chain = cparent->array[i].chain;
1229 while ((rchain = chain->parent) != NULL) {
1230 hammer2_chain_ref(rchain);
1231 hammer2_chain_unlock(chain);
1232 hammer2_chain_lock(rchain, HAMMER2_RESOLVE_ALWAYS);
1233 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1234 hammer2_chain_drop(rchain);
1235 if (chain->parent == rchain)
1237 hammer2_chain_unlock(rchain);
1239 if (cluster->focus == chain)
1240 cparent->focus = rchain;
1241 cparent->array[i].chain = rchain;
1242 hammer2_chain_drop(chain);
1244 cparent->flags |= HAMMER2_CLUSTER_LOCKED;
1245 hammer2_cluster_resolve(cparent);
1250 /************************************************************************
1252 ************************************************************************
1255 * WARNING! blockref[] array data is not universal. These functions should
1256 * only be used to access universal data.
1258 * NOTE! The rdata call will wait for at least one of the chain I/Os to
1259 * complete if necessary. The I/O's should have already been
1260 * initiated by the cluster_lock/chain_lock operation.
1262 * The cluster must already be in a modified state before wdata
1263 * is called. The data will already be available for this case.
1265 const hammer2_media_data_t *
1266 hammer2_cluster_rdata(hammer2_cluster_t *cluster)
1268 return(cluster->focus->data);
1271 hammer2_media_data_t *
1272 hammer2_cluster_wdata(hammer2_cluster_t *cluster)
1274 KKASSERT(hammer2_cluster_modified(cluster));
1275 return(cluster->focus->data);
1279 * Load cluster data asynchronously with callback.
1281 * The callback is made for the first validated data found, or NULL
1282 * if no valid data is available.
1284 * NOTE! The cluster structure is either unique or serialized (e.g. embedded
1285 * in the inode with an exclusive lock held), the chain structure may be
1289 hammer2_cluster_load_async(hammer2_cluster_t *cluster,
1290 void (*callback)(hammer2_iocb_t *iocb), void *ptr)
1292 hammer2_chain_t *chain;
1293 hammer2_iocb_t *iocb;
1295 hammer2_blockref_t *bref;
1299 * Try to find a chain whos data is already resolved. If none can
1300 * be found, start with the first chain.
1303 for (i = 0; i < cluster->nchains; ++i) {
1304 chain = cluster->array[i].chain;
1305 if (chain && chain->data)
1308 if (i == cluster->nchains) {
1309 chain = cluster->array[0].chain;
1313 iocb = &cluster->iocb;
1314 iocb->callback = callback;
1315 iocb->dio = NULL; /* for already-validated case */
1316 iocb->cluster = cluster;
1317 iocb->chain = chain;
1319 iocb->lbase = (off_t)i;
1324 * Data already validated
1332 * We must resolve to a device buffer, either by issuing I/O or
1333 * by creating a zero-fill element. We do not mark the buffer
1334 * dirty when creating a zero-fill element (the hammer2_chain_modify()
1335 * API must still be used to do that).
1337 * The device buffer is variable-sized in powers of 2 down
1338 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
1339 * chunk always contains buffers of the same size. (XXX)
1341 * The minimum physical IO size may be larger than the variable
1344 * XXX TODO - handle HAMMER2_CHAIN_INITIAL for case where chain->bytes
1345 * matches hammer2_devblksize()? Or does the freemap's
1346 * pre-zeroing handle the case for us?
1348 bref = &chain->bref;
1352 /* handled by callback? <- TODO XXX even needed for loads? */
1354 * The getblk() optimization for a 100% overwrite can only be used
1355 * if the physical block size matches the request.
1357 if ((chain->flags & HAMMER2_CHAIN_INITIAL) &&
1358 chain->bytes == hammer2_devblksize(chain->bytes)) {
1359 error = hammer2_io_new(hmp, bref->data_off, chain->bytes, &dio);
1360 KKASSERT(error == 0);
1368 * Otherwise issue a read
1370 hammer2_adjreadcounter(&chain->bref, chain->bytes);
1371 hammer2_io_getblk(hmp, bref->data_off, chain->bytes, iocb);
1374 /************************************************************************
1376 ************************************************************************
1378 * A node failure can occur for numerous reasons.
1380 * - A read I/O may fail
1381 * - A write I/O may fail
1382 * - An unexpected chain might be found (or be missing)
1383 * - A node might disconnect temporarily and reconnect later
1384 * (for example, a USB stick could get pulled, or a node might
1385 * be programmatically disconnected).
1386 * - A node might run out of space during a modifying operation.
1388 * When a read failure or an unexpected chain state is found, the chain and
1389 * parent chain at the failure point for the nodes involved (the nodes
1390 * which we determine to be in error) are flagged as failed and removed
1391 * from the cluster. The node itself is allowed to remain active. The
1392 * highest common point (usually a parent chain) is queued to the
1393 * resynchronization thread for action.
1395 * When a write I/O fails or a node runs out of space, we first adjust
1396 * as if a read failure occurs but we further disable flushes on the
1397 * ENTIRE node. Concurrent modifying transactions are allowed to complete
1398 * but any new modifying transactions will automatically remove the node
1399 * from consideration in all related cluster structures and not generate
1400 * any new modified chains. The ROOT chain for the failed node(s) is queued
1401 * to the resynchronization thread for action.
1403 * A temporary disconnect is handled as if a write failure occurred.
1405 * Any of these failures might or might not stall related high level VNOPS,
1406 * depending on what has failed, what nodes remain, the type of cluster,
1407 * and the operating state of the cluster.
1409 * FLUSH ON WRITE-DISABLED NODES
1411 * A flush on a write-disabled node is not allowed to write anything because
1412 * we cannot safely update the mirror_tid anywhere on the failed node. The
1413 * synchronization thread uses mirror_tid to calculate incremental resyncs.
1414 * Dirty meta-data related to the failed node is thrown away.
1416 * Dirty buffer cache buffers and inodes are only thrown away if they can be
1417 * retired... that is, if the filesystem still has enough nodes to complete
1421 /************************************************************************
1422 * SYNCHRONIZATION THREAD *
1423 ************************************************************************
1425 * This thread is responsible for [re]synchronizing the cluster representing
1426 * a PFS. Any out-of-sync or failed node starts this thread on a
1427 * node-by-node basis when the failure is detected.
1429 * Clusters needing resynchronization are queued at the highest point
1430 * where the parent on the failed node is still valid, or a special
1431 * incremental scan from the ROOT is queued if no parent exists. This
1432 * thread is also responsible for waiting for reconnections of the failed
1433 * node if the cause was due to a disconnect, and waiting for space to be
1434 * freed up if the cause was due to running out of space.
1436 * If the cause is due to a node running out of space, this thread will also
1437 * remove older (unlocked) snapshots to make new space, recover space, and
1438 * then start resynchronization.
1440 * Each resynchronization pass virtually snapshots the PFS on the good nodes
1441 * and synchronizes using that snapshot against the target node. This
1442 * ensures a consistent chain topology and also avoids interference between
1443 * the resynchronization thread and frontend operations.
1445 * Since these are per-node threads it is possible to resynchronize several