2 * Copyright (c) 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 * This module implements various PFS-based helper threads.
39 typedef struct hammer2_deferred_ip {
40 struct hammer2_deferred_ip *next;
42 } hammer2_deferred_ip_t;
44 typedef struct hammer2_deferred_list {
45 hammer2_deferred_ip_t *base;
47 } hammer2_deferred_list_t;
50 #define HAMMER2_THREAD_DEBUG 1
52 static int hammer2_sync_slaves(hammer2_thread_t *thr, hammer2_inode_t *ip,
53 hammer2_deferred_list_t *list);
55 static void hammer2_update_pfs_status(hammer2_thread_t *thr, uint32_t flags);
56 nerror = hammer2_sync_insert(
58 focus->bref.modify_tid,
61 static int hammer2_sync_insert(hammer2_thread_t *thr,
62 hammer2_chain_t **parentp, hammer2_chain_t **chainp,
63 hammer2_tid_t modify_tid, int idx,
64 hammer2_chain_t *focus);
65 static int hammer2_sync_destroy(hammer2_thread_t *thr,
66 hammer2_chain_t **parentp, hammer2_chain_t **chainp,
68 static int hammer2_sync_replace(hammer2_thread_t *thr,
69 hammer2_chain_t *parent, hammer2_chain_t *chain,
70 hammer2_tid_t modify_tid, int idx,
71 hammer2_chain_t *focus);
73 /****************************************************************************
74 * HAMMER2 THREAD API *
75 ****************************************************************************/
77 * Initialize the suspplied thread structure, starting the specified
81 hammer2_thr_create(hammer2_thread_t *thr, hammer2_pfs_t *pmp,
82 const char *id, int clindex, int repidx,
83 void (*func)(void *arg))
85 lockinit(&thr->lk, "h2thr", 0, 0);
87 thr->clindex = clindex;
89 TAILQ_INIT(&thr->xopq);
91 lwkt_create(func, thr, &thr->td, NULL, 0, -1,
92 "%s-%s.%02d", id, pmp->pfs_names[clindex], repidx);
94 lwkt_create(func, thr, &thr->td, NULL, 0, -1,
95 "%s-%s", id, pmp->pfs_names[clindex]);
100 * Terminate a thread. This function will silently return if the thread
101 * was never initialized or has already been deleted.
103 * This is accomplished by setting the STOP flag and waiting for the td
104 * structure to become NULL.
107 hammer2_thr_delete(hammer2_thread_t *thr)
111 lockmgr(&thr->lk, LK_EXCLUSIVE);
112 atomic_set_int(&thr->flags, HAMMER2_THREAD_STOP);
115 lksleep(thr, &thr->lk, 0, "h2thr", hz);
117 lockmgr(&thr->lk, LK_RELEASE);
119 lockuninit(&thr->lk);
123 * Asynchronous remaster request. Ask the synchronization thread to
124 * start over soon (as if it were frozen and unfrozen, but without waiting).
125 * The thread always recalculates mastership relationships when restarting.
128 hammer2_thr_remaster(hammer2_thread_t *thr)
132 lockmgr(&thr->lk, LK_EXCLUSIVE);
133 atomic_set_int(&thr->flags, HAMMER2_THREAD_REMASTER);
135 lockmgr(&thr->lk, LK_RELEASE);
139 hammer2_thr_freeze_async(hammer2_thread_t *thr)
141 atomic_set_int(&thr->flags, HAMMER2_THREAD_FREEZE);
146 hammer2_thr_freeze(hammer2_thread_t *thr)
150 lockmgr(&thr->lk, LK_EXCLUSIVE);
151 atomic_set_int(&thr->flags, HAMMER2_THREAD_FREEZE);
153 while ((thr->flags & HAMMER2_THREAD_FROZEN) == 0) {
154 lksleep(thr, &thr->lk, 0, "h2frz", hz);
156 lockmgr(&thr->lk, LK_RELEASE);
160 hammer2_thr_unfreeze(hammer2_thread_t *thr)
164 lockmgr(&thr->lk, LK_EXCLUSIVE);
165 atomic_clear_int(&thr->flags, HAMMER2_THREAD_FROZEN);
167 lockmgr(&thr->lk, LK_RELEASE);
172 hammer2_thr_break(hammer2_thread_t *thr)
174 if (thr->flags & (HAMMER2_THREAD_STOP |
175 HAMMER2_THREAD_REMASTER |
176 HAMMER2_THREAD_FREEZE)) {
182 /****************************************************************************
183 * HAMMER2 SYNC THREADS *
184 ****************************************************************************/
186 * Primary management thread for an element of a node. A thread will exist
187 * for each element requiring management.
189 * No management threads are needed for the SPMP or for any PMP with only
192 * On the SPMP - handles bulkfree and dedup operations
193 * On a PFS - handles remastering and synchronization
196 hammer2_primary_sync_thread(void *arg)
198 hammer2_thread_t *thr = arg;
200 hammer2_deferred_list_t list;
201 hammer2_deferred_ip_t *defer;
205 bzero(&list, sizeof(list));
207 lockmgr(&thr->lk, LK_EXCLUSIVE);
208 while ((thr->flags & HAMMER2_THREAD_STOP) == 0) {
210 * Handle freeze request
212 if (thr->flags & HAMMER2_THREAD_FREEZE) {
213 atomic_set_int(&thr->flags, HAMMER2_THREAD_FROZEN);
214 atomic_clear_int(&thr->flags, HAMMER2_THREAD_FREEZE);
218 * Force idle if frozen until unfrozen or stopped.
220 if (thr->flags & HAMMER2_THREAD_FROZEN) {
221 lksleep(&thr->flags, &thr->lk, 0, "frozen", 0);
226 * Reset state on REMASTER request
228 if (thr->flags & HAMMER2_THREAD_REMASTER) {
229 atomic_clear_int(&thr->flags, HAMMER2_THREAD_REMASTER);
234 * Synchronization scan.
236 kprintf("sync_slaves clindex %d\n", thr->clindex);
237 hammer2_trans_init(pmp, 0);
239 hammer2_inode_ref(pmp->iroot);
242 error = hammer2_sync_slaves(thr, pmp->iroot, &list);
245 while ((defer = list.base) != NULL) {
246 hammer2_inode_t *nip;
249 error = hammer2_sync_slaves(thr, nip, &list);
250 if (error && error != EAGAIN)
252 if (hammer2_thr_break(thr)) {
258 * If no additional defers occurred we can
259 * remove this one, otherwrise keep it on
260 * the list and retry once the additional
261 * defers have completed.
263 if (defer == list.base) {
264 list.base = defer->next;
265 kfree(defer, M_HAMMER2);
266 defer = NULL; /* safety */
267 hammer2_inode_drop(nip);
272 * If the thread is being remastered, frozen, or
273 * stopped, clean up any left-over deferals.
276 kprintf("didbreak\n");
277 while ((defer = list.base) != NULL) {
278 hammer2_inode_drop(defer->ip);
279 list.base = defer->next;
280 kfree(defer, M_HAMMER2);
287 hammer2_inode_drop(pmp->iroot);
288 hammer2_trans_done(pmp);
291 kprintf("hammer2_sync_slaves: error %d\n", error);
294 * Wait for event, or 5-second poll.
296 lksleep(&thr->flags, &thr->lk, 0, "h2idle", hz * 5);
300 lockmgr(&thr->lk, LK_RELEASE);
301 /* thr structure can go invalid after this point */
306 * Given a locked cluster created from pmp->iroot, update the PFS's
311 hammer2_update_pfs_status(hammer2_thread_t *thr, uint32_t flags)
313 hammer2_pfs_t *pmp = thr->pmp;
315 flags &= HAMMER2_CLUSTER_ZFLAGS;
316 if (pmp->cluster_flags == flags)
318 pmp->cluster_flags = flags;
320 kprintf("pfs %p", pmp);
321 if (flags & HAMMER2_CLUSTER_MSYNCED)
322 kprintf(" masters-all-good");
323 if (flags & HAMMER2_CLUSTER_SSYNCED)
324 kprintf(" slaves-all-good");
326 if (flags & HAMMER2_CLUSTER_WRHARD)
327 kprintf(" quorum/rw");
328 else if (flags & HAMMER2_CLUSTER_RDHARD)
329 kprintf(" quorum/ro");
331 if (flags & HAMMER2_CLUSTER_UNHARD)
332 kprintf(" out-of-sync-masters");
333 else if (flags & HAMMER2_CLUSTER_NOHARD)
334 kprintf(" no-masters-visible");
336 if (flags & HAMMER2_CLUSTER_WRSOFT)
338 else if (flags & HAMMER2_CLUSTER_RDSOFT)
341 if (flags & HAMMER2_CLUSTER_UNSOFT)
342 kprintf(" out-of-sync-slaves");
343 else if (flags & HAMMER2_CLUSTER_NOSOFT)
344 kprintf(" no-slaves-visible");
352 dumpcluster(const char *label,
353 hammer2_cluster_t *cparent, hammer2_cluster_t *cluster)
355 hammer2_chain_t *chain;
358 if ((hammer2_debug & 1) == 0)
361 kprintf("%s\t", label);
362 KKASSERT(cparent->nchains == cluster->nchains);
363 for (i = 0; i < cparent->nchains; ++i) {
367 if ((chain = cparent->array[i].chain) != NULL) {
370 ((cparent->array[i].flags &
371 HAMMER2_CITEM_INVALID) ? "(I)" : " ")
374 kprintf(" NULL %s ", " ");
376 if ((chain = cluster->array[i].chain) != NULL) {
379 ((cluster->array[i].flags &
380 HAMMER2_CITEM_INVALID) ? "(I)" : " ")
383 kprintf(" NULL %s ", " ");
391 * Each out of sync node sync-thread must issue an all-nodes XOP scan of
392 * the inode. This creates a multiplication effect since the XOP scan itself
393 * issues to all nodes. However, this is the only way we can safely
394 * synchronize nodes which might have disparate I/O bandwidths and the only
395 * way we can safely deal with stalled nodes.
399 hammer2_sync_slaves(hammer2_thread_t *thr, hammer2_inode_t *ip,
400 hammer2_deferred_list_t *list)
402 hammer2_xop_scanall_t *xop;
403 hammer2_chain_t *parent;
404 hammer2_chain_t *chain;
406 hammer2_key_t key_next;
407 int cache_index = -1;
416 idx = thr->clindex; /* cluster node we are responsible for */
422 * Nothing to do if all slaves are synchronized.
423 * Nothing to do if cluster not authoritatively readable.
425 if (pmp->cluster_flags & HAMMER2_CLUSTER_SSYNCED)
427 if ((pmp->cluster_flags & HAMMER2_CLUSTER_RDHARD) == 0)
428 return(HAMMER2_ERROR_INCOMPLETE);
434 * The inode is left unlocked during the scan. Issue a XOP
435 * that does *not* include our cluster index to iterate
436 * properly synchronized elements and resolve our cluster index
439 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
440 xop = &hammer2_xop_alloc(ip)->xop_scanall;
441 xop->key_beg = HAMMER2_KEY_MIN;
442 xop->key_end = HAMMER2_KEY_MAX;
443 hammer2_xop_start_except(&xop->head, hammer2_xop_scanall, idx);
444 parent = hammer2_inode_chain(ip, idx,
445 HAMMER2_RESOLVE_ALWAYS |
446 HAMMER2_RESOLVE_SHARED);
448 hammer2_inode_unlock(ip);
450 chain = hammer2_chain_lookup(&parent, &key_next,
451 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX,
453 HAMMER2_LOOKUP_SHARED |
454 HAMMER2_LOOKUP_NODIRECT |
455 HAMMER2_LOOKUP_NODATA);
456 error = hammer2_xop_collect(&xop->head, 0);
460 * We are done if our scan is done and the XOP scan is done.
461 * We are done if the XOP scan failed (that is, we don't
462 * have authoritative data to synchronize with).
464 int advance_local = 0;
467 hammer2_chain_t *focus;
469 if (chain == NULL && error == ENOENT)
471 if (error && error != ENOENT)
477 if (chain && error == ENOENT) {
479 * If we have local chains but the XOP scan is done,
480 * the chains need to be deleted.
484 } else if (chain == NULL) {
486 * If our local scan is done but the XOP scan is not,
487 * we need to create the missing chain(s).
490 focus = xop->head.cluster.focus;
493 * Otherwise compare to determine the action
496 focus = xop->head.cluster.focus;
497 n = hammer2_chain_cmp(chain, focus);
501 * Take action based on comparison results.
505 * Delete extranious local data. This will
506 * automatically advance the chain.
508 nerror = hammer2_sync_destroy(thr, &parent, &chain,
511 } else if (n == 0 && chain->bref.modify_tid !=
512 focus->bref.modify_tid) {
514 * Matching key but local data or meta-data requires
515 * updating. If we will recurse, we still need to
516 * update to compatible content first but we do not
517 * synchronize modify_tid until the entire recursion
518 * has completed successfully.
520 if (focus->bref.type == HAMMER2_BREF_TYPE_INODE) {
521 nerror = hammer2_sync_replace(
527 nerror = hammer2_sync_replace(
529 focus->bref.modify_tid,
535 * 100% match, advance both
542 * Insert missing local data.
544 * If we will recurse, we still need to update to
545 * compatible content first but we do not synchronize
546 * modify_tid until the entire recursion has
547 * completed successfully.
549 if (focus->bref.type == HAMMER2_BREF_TYPE_INODE) {
550 nerror = hammer2_sync_insert(
551 thr, &parent, &chain,
556 nerror = hammer2_sync_insert(
557 thr, &parent, &chain,
558 focus->bref.modify_tid,
567 * We cannot recurse depth-first because the XOP is still
568 * running in node threads for this scan. Create a placemarker
569 * by obtaining and record the hammer2_inode.
571 * We excluded our node from the XOP so we must temporarily
572 * add it to xop->head.cluster so it is properly incorporated
575 * The deferral is pushed onto a LIFO list for bottom-up
578 if (error == 0 && dodefer) {
579 hammer2_inode_t *nip;
580 hammer2_deferred_ip_t *defer;
582 KKASSERT(focus->bref.type == HAMMER2_BREF_TYPE_INODE);
584 defer = kmalloc(sizeof(*defer), M_HAMMER2,
586 KKASSERT(xop->head.cluster.array[idx].chain == NULL);
587 xop->head.cluster.array[idx].flags =
588 HAMMER2_CITEM_INVALID;
589 xop->head.cluster.array[idx].chain = chain;
590 nip = hammer2_inode_get(pmp, ip, &xop->head.cluster);
591 xop->head.cluster.array[idx].chain = NULL;
593 kprintf("DEFER INODE %p->%p\n", ip, nip);
594 hammer2_inode_ref(nip);
595 hammer2_inode_unlock(nip);
597 defer->next = list->base;
605 * If at least one deferral was added and the deferral
606 * list has grown too large, stop adding more. This
607 * will trigger an EAGAIN return.
609 if (needrescan && list->count > 1000)
613 * Advancements for iteration.
616 error = hammer2_xop_collect(&xop->head, 0);
619 chain = hammer2_chain_next(&parent, chain, &key_next,
620 key_next, HAMMER2_KEY_MAX,
622 HAMMER2_LOOKUP_SHARED |
623 HAMMER2_LOOKUP_NODIRECT |
624 HAMMER2_LOOKUP_NODATA);
627 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
629 hammer2_chain_unlock(chain);
630 hammer2_chain_drop(chain);
633 hammer2_chain_unlock(parent);
634 hammer2_chain_drop(parent);
638 * If we added deferrals we want the caller to synchronize them
639 * and then call us again.
641 * NOTE: In this situation we do not yet want to synchronize our
642 * inode, setting the error code also has that effect.
644 if (error == 0 && needrescan)
648 * If no error occurred and work was performed, synchronize the
649 * inode meta-data itself.
651 * XXX inode lock was lost
653 if (error == 0 && didwork) {
654 hammer2_xop_ipcluster_t *xop2;
655 hammer2_chain_t *focus;
657 xop2 = &hammer2_xop_alloc(ip)->xop_ipcluster;
658 hammer2_xop_start_except(&xop2->head, hammer2_xop_ipcluster,
660 error = hammer2_xop_collect(&xop2->head, 0);
662 focus = xop2->head.cluster.focus;
663 kprintf("syncthr: update inode\n");
664 chain = hammer2_inode_chain_and_parent(ip, idx,
666 HAMMER2_RESOLVE_ALWAYS |
667 HAMMER2_RESOLVE_SHARED);
669 KKASSERT(parent != NULL);
670 nerror = hammer2_sync_replace(
672 focus->bref.modify_tid,
673 idx, xop2->head.cluster.focus);
674 hammer2_chain_unlock(chain);
675 hammer2_chain_drop(chain);
676 hammer2_chain_unlock(parent);
677 hammer2_chain_drop(parent);
680 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
687 * Create a missing chain by copying the focus from another device.
689 * On entry *parentp and focus are both locked shared. The chain will be
690 * created and returned in *chainp also locked shared.
694 hammer2_sync_insert(hammer2_thread_t *thr,
695 hammer2_chain_t **parentp, hammer2_chain_t **chainp,
696 hammer2_tid_t modify_tid, int idx,
697 hammer2_chain_t *focus)
699 hammer2_chain_t *chain;
701 #if HAMMER2_THREAD_DEBUG
702 if (hammer2_debug & 1)
703 kprintf("insert rec par=%p/%d.%016jx slave %d %d.%016jx mod=%016jx\n",
705 (*parentp)->bref.type,
706 (*parentp)->bref.key,
708 focus->bref.type, focus->bref.key, modify_tid);
711 hammer2_chain_unlock(*parentp);
712 hammer2_chain_lock(*parentp, HAMMER2_RESOLVE_ALWAYS);
713 /* reissue lookup? */
716 * Create the missing chain.
718 * Have to be careful to avoid deadlocks.
721 hammer2_chain_create(parentp, &chain, thr->pmp,
722 focus->bref.key, focus->bref.keybits,
723 focus->bref.type, focus->bytes,
725 hammer2_chain_modify(chain, HAMMER2_MODIFY_KEEPMODIFY);
728 * Copy focus to new chain
731 /* type already set */
732 chain->bref.methods = focus->bref.methods;
733 /* keybits already set */
734 chain->bref.vradix = focus->bref.vradix;
735 /* mirror_tid set by flush */
736 chain->bref.modify_tid = modify_tid;
737 chain->bref.flags = focus->bref.flags;
738 /* key already present */
739 /* check code will be recalculated */
744 switch(chain->bref.type) {
745 case HAMMER2_BREF_TYPE_INODE:
746 if ((focus->data->ipdata.meta.op_flags &
747 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
748 bcopy(focus->data, chain->data,
749 offsetof(hammer2_inode_data_t, u));
753 case HAMMER2_BREF_TYPE_DATA:
754 bcopy(focus->data, chain->data, chain->bytes);
755 hammer2_chain_setcheck(chain, chain->data);
762 hammer2_chain_unlock(focus);
763 hammer2_chain_unlock(chain); /* unlock, leave ref */
766 * Avoid ordering deadlock when relocking cparent.
768 hammer2_chain_unlock(*parentp);
769 hammer2_chain_lock(*parentp, HAMMER2_RESOLVE_SHARED |
770 HAMMER2_RESOLVE_ALWAYS);
771 hammer2_chain_lock(chain, HAMMER2_RESOLVE_SHARED);
777 * Destroy an extranious chain.
779 * Both *parentp and *chainp are locked shared.
781 * On return, *chainp will be adjusted to point to the next element in the
782 * iteration and locked shared.
786 hammer2_sync_destroy(hammer2_thread_t *thr,
787 hammer2_chain_t **parentp, hammer2_chain_t **chainp,
790 hammer2_chain_t *chain;
791 hammer2_chain_t *parent;
792 hammer2_key_t key_next;
793 hammer2_key_t save_key;
794 int cache_index = -1;
798 #if HAMMER2_THREAD_DEBUG
799 if (hammer2_debug & 1)
800 kprintf("destroy rec %p/%p slave %d %d.%016jx\n",
802 idx, chain->bref.type, chain->bref.key);
805 save_key = chain->bref.key;
806 if (save_key != HAMMER2_KEY_MAX)
810 * Try to avoid unnecessary I/O.
812 * XXX accounting not propagated up properly. We might have to do
813 * a RESOLVE_MAYBE here and pass 0 for the flags.
815 hammer2_chain_unlock(chain); /* relock exclusive */
816 hammer2_chain_unlock(*parentp);
817 hammer2_chain_lock(*parentp, HAMMER2_RESOLVE_ALWAYS);
818 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
820 hammer2_chain_delete(*parentp, chain, HAMMER2_DELETE_PERMANENT);
821 hammer2_chain_unlock(chain);
822 hammer2_chain_drop(chain);
823 chain = NULL; /* safety */
825 hammer2_chain_unlock(*parentp); /* relock shared */
826 hammer2_chain_lock(*parentp, HAMMER2_RESOLVE_SHARED |
827 HAMMER2_RESOLVE_ALWAYS);
828 *chainp = hammer2_chain_lookup(&parent, &key_next,
829 save_key, HAMMER2_KEY_MAX,
831 HAMMER2_LOOKUP_SHARED |
832 HAMMER2_LOOKUP_NODIRECT |
833 HAMMER2_LOOKUP_NODATA);
838 * cparent is locked exclusively, with an extra ref, cluster is not locked.
839 * Replace element [i] in the cluster.
843 hammer2_sync_replace(hammer2_thread_t *thr,
844 hammer2_chain_t *parent, hammer2_chain_t *chain,
845 hammer2_tid_t modify_tid, int idx,
846 hammer2_chain_t *focus)
851 #if HAMMER2_THREAD_DEBUG
852 if (hammer2_debug & 1)
853 kprintf("replace rec %p slave %d %d.%016jx mod=%016jx\n",
856 focus->bref.type, focus->bref.key, modify_tid);
858 hammer2_chain_unlock(chain);
859 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
860 if (chain->bytes != focus->bytes) {
861 /* XXX what if compressed? */
862 nradix = hammer2_getradix(chain->bytes);
863 hammer2_chain_resize(NULL, parent, chain, nradix, 0);
865 hammer2_chain_modify(chain, HAMMER2_MODIFY_KEEPMODIFY);
866 otype = chain->bref.type;
867 chain->bref.type = focus->bref.type;
868 chain->bref.methods = focus->bref.methods;
869 chain->bref.keybits = focus->bref.keybits;
870 chain->bref.vradix = focus->bref.vradix;
871 /* mirror_tid updated by flush */
872 chain->bref.modify_tid = modify_tid;
873 chain->bref.flags = focus->bref.flags;
874 /* key already present */
875 /* check code will be recalculated */
881 switch(chain->bref.type) {
882 case HAMMER2_BREF_TYPE_INODE:
883 if ((focus->data->ipdata.meta.op_flags &
884 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
886 * If DIRECTDATA is transitioning to 0 or the old
887 * chain is not an inode we have to initialize
890 if (otype != HAMMER2_BREF_TYPE_INODE ||
891 (chain->data->ipdata.meta.op_flags &
892 HAMMER2_OPFLAG_DIRECTDATA)) {
893 kprintf("chain inode trans away from dd\n");
894 bzero(&chain->data->ipdata.u,
895 sizeof(chain->data->ipdata.u));
897 bcopy(focus->data, chain->data,
898 offsetof(hammer2_inode_data_t, u));
899 /* XXX setcheck on inode should not be needed */
900 hammer2_chain_setcheck(chain, chain->data);
904 case HAMMER2_BREF_TYPE_DATA:
905 bcopy(focus->data, chain->data, chain->bytes);
906 hammer2_chain_setcheck(chain, chain->data);
913 hammer2_chain_unlock(chain);
914 hammer2_chain_lock(chain, HAMMER2_RESOLVE_SHARED |
915 HAMMER2_RESOLVE_MAYBE);
920 /****************************************************************************
921 * HAMMER2 XOPS THREADS *
922 ****************************************************************************/
925 hammer2_xop_group_init(hammer2_pfs_t *pmp, hammer2_xop_group_t *xgrp)
927 hammer2_mtx_init(&xgrp->mtx, "h2xopq");
928 hammer2_mtx_init(&xgrp->mtx2, "h2xopio");
932 * Allocate a XOP request.
934 * Once allocated a XOP request can be started, collected, and retired,
935 * and can be retired early if desired.
937 * NOTE: Fifo indices might not be zero but ri == wi on objcache_get().
940 hammer2_xop_alloc(hammer2_inode_t *ip)
944 xop = objcache_get(cache_xops, M_WAITOK);
945 KKASSERT(xop->head.cluster.array[0].chain == NULL);
947 xop->head.func = NULL;
950 xop->head.collect_key = 0;
952 xop->head.cluster.nchains = ip->cluster.nchains;
953 xop->head.cluster.pmp = ip->pmp;
954 xop->head.cluster.flags = HAMMER2_CLUSTER_LOCKED;
957 * run_mask - Active thread (or frontend) associated with XOP
959 xop->head.run_mask = HAMMER2_XOPMASK_VOP;
961 hammer2_inode_ref(ip);
967 hammer2_xop_setname(hammer2_xop_head_t *xop, const char *name, size_t name_len)
969 xop->name = kmalloc(name_len + 1, M_HAMMER2, M_WAITOK | M_ZERO);
970 xop->name_len = name_len;
971 bcopy(name, xop->name, name_len);
975 hammer2_xop_setname2(hammer2_xop_head_t *xop, const char *name, size_t name_len)
977 xop->name2 = kmalloc(name_len + 1, M_HAMMER2, M_WAITOK | M_ZERO);
978 xop->name2_len = name_len;
979 bcopy(name, xop->name2, name_len);
984 hammer2_xop_setip2(hammer2_xop_head_t *xop, hammer2_inode_t *ip2)
987 hammer2_inode_ref(ip2);
991 hammer2_xop_setip3(hammer2_xop_head_t *xop, hammer2_inode_t *ip3)
994 hammer2_inode_ref(ip3);
998 hammer2_xop_reinit(hammer2_xop_head_t *xop)
1002 xop->collect_key = 0;
1003 xop->run_mask = HAMMER2_XOPMASK_VOP;
1007 * A mounted PFS needs Xops threads to support frontend operations.
1010 hammer2_xop_helper_create(hammer2_pfs_t *pmp)
1015 for (i = 0; i < pmp->pfs_nmasters; ++i) {
1016 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
1017 if (pmp->xop_groups[j].thrs[i].td)
1019 hammer2_thr_create(&pmp->xop_groups[j].thrs[i], pmp,
1021 hammer2_primary_xops_thread);
1027 hammer2_xop_helper_cleanup(hammer2_pfs_t *pmp)
1032 for (i = 0; i < pmp->pfs_nmasters; ++i) {
1033 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
1034 if (pmp->xop_groups[j].thrs[i].td)
1035 hammer2_thr_delete(&pmp->xop_groups[j].thrs[i]);
1044 * Start a XOP request, queueing it to all nodes in the cluster to
1045 * execute the cluster op.
1047 * XXX optimize single-target case.
1050 hammer2_xop_start_except(hammer2_xop_head_t *xop, hammer2_xop_func_t func,
1053 hammer2_xop_group_t *xgrp;
1054 hammer2_thread_t *thr;
1061 g = pmp->xop_iterator++;
1062 g = g & HAMMER2_XOPGROUPS_MASK;
1063 xgrp = &pmp->xop_groups[g];
1067 for (i = 0; i < xop->ip->cluster.nchains; ++i) {
1068 thr = &xgrp->thrs[i];
1069 if (thr->td && i != notidx) {
1070 lockmgr(&thr->lk, LK_EXCLUSIVE);
1072 (thr->flags & HAMMER2_THREAD_STOP) == 0) {
1073 atomic_set_int(&xop->run_mask, 1U << i);
1074 TAILQ_INSERT_TAIL(&thr->xopq, xop,
1077 lockmgr(&thr->lk, LK_RELEASE);
1078 wakeup(&thr->flags);
1084 hammer2_xop_start(hammer2_xop_head_t *xop, hammer2_xop_func_t func)
1086 hammer2_xop_start_except(xop, func, -1);
1090 * Retire a XOP. Used by both the VOP frontend and by the XOP backend.
1093 hammer2_xop_retire(hammer2_xop_head_t *xop, uint32_t mask)
1095 hammer2_xop_group_t *xgrp;
1096 hammer2_chain_t *chain;
1102 * Remove the frontend or remove a backend feeder. When removing
1103 * the frontend we must wakeup any backend feeders who are waiting
1106 * XXX optimize wakeup.
1108 KKASSERT(xop->run_mask & mask);
1109 if (atomic_fetchadd_int(&xop->run_mask, -mask) != mask) {
1110 if (mask == HAMMER2_XOPMASK_VOP)
1116 * Cleanup the collection cluster.
1118 for (i = 0; i < xop->cluster.nchains; ++i) {
1119 xop->cluster.array[i].flags = 0;
1120 chain = xop->cluster.array[i].chain;
1122 xop->cluster.array[i].chain = NULL;
1123 hammer2_chain_unlock(chain);
1124 hammer2_chain_drop(chain);
1129 * Cleanup the fifos, use check_counter to optimize the loop.
1131 mask = xop->chk_mask;
1132 for (i = 0; mask && i < HAMMER2_MAXCLUSTER; ++i) {
1133 hammer2_xop_fifo_t *fifo = &xop->collect[i];
1134 while (fifo->ri != fifo->wi) {
1135 chain = fifo->array[fifo->ri & HAMMER2_XOPFIFO_MASK];
1137 hammer2_chain_unlock(chain);
1138 hammer2_chain_drop(chain);
1141 if (fifo->wi - fifo->ri < HAMMER2_XOPFIFO / 2)
1142 wakeup(xop); /* XXX optimize */
1148 * The inode is only held at this point, simply drop it.
1151 hammer2_inode_drop(xop->ip);
1155 hammer2_inode_drop(xop->ip2);
1159 hammer2_inode_drop(xop->ip3);
1163 kfree(xop->name, M_HAMMER2);
1168 kfree(xop->name2, M_HAMMER2);
1173 objcache_put(cache_xops, xop);
1177 * (Backend) Returns non-zero if the frontend is still attached.
1180 hammer2_xop_active(hammer2_xop_head_t *xop)
1182 if (xop->run_mask & HAMMER2_XOPMASK_VOP)
1189 * (Backend) Feed chain data through the cluster validator and back to
1190 * the frontend. Chains are fed from multiple nodes concurrently
1191 * and pipelined via per-node FIFOs in the XOP.
1193 * No xop lock is needed because we are only manipulating fields under
1194 * our direct control.
1196 * Returns 0 on success and a hammer error code if sync is permanently
1197 * lost. The caller retains a ref on the chain but by convention
1198 * the lock is typically inherited by the xop (caller loses lock).
1200 * Returns non-zero on error. In this situation the caller retains a
1201 * ref on the chain but loses the lock (we unlock here).
1203 * WARNING! The chain is moving between two different threads, it must
1204 * be locked SHARED to retain its data mapping, not exclusive.
1205 * When multiple operations are in progress at once, chains fed
1206 * back to the frontend for collection can wind up being locked
1207 * in different orders, only a shared lock can prevent a deadlock.
1209 * Exclusive locks may only be used by a XOP backend node thread
1210 * temporarily, with no direct or indirect dependencies (aka
1211 * blocking/waiting) on other nodes.
1214 hammer2_xop_feed(hammer2_xop_head_t *xop, hammer2_chain_t *chain,
1215 int clindex, int error)
1217 hammer2_xop_fifo_t *fifo;
1220 * Multi-threaded entry into the XOP collector. We own the
1221 * fifo->wi for our clindex.
1223 fifo = &xop->collect[clindex];
1225 while (fifo->ri == fifo->wi - HAMMER2_XOPFIFO) {
1226 tsleep_interlock(xop, 0);
1227 if (hammer2_xop_active(xop) == 0) {
1231 if (fifo->ri == fifo->wi - HAMMER2_XOPFIFO) {
1232 tsleep(xop, PINTERLOCKED, "h2feed", hz*60);
1236 hammer2_chain_ref(chain);
1237 fifo->errors[fifo->wi & HAMMER2_XOPFIFO_MASK] = error;
1238 fifo->array[fifo->wi & HAMMER2_XOPFIFO_MASK] = chain;
1241 atomic_set_int(&xop->chk_mask, 1U << clindex);
1242 atomic_add_int(&xop->check_counter, 1);
1243 wakeup(&xop->check_counter); /* XXX optimize */
1247 * Cleanup. If an error occurred we eat the lock. If no error
1248 * occurred the fifo inherits the lock and gains an additional ref.
1250 * The caller's ref remains in both cases.
1254 hammer2_chain_unlock(chain);
1259 * (Frontend) collect a response from a running cluster op.
1261 * Responses are fed from all appropriate nodes concurrently
1262 * and collected into a cohesive response >= collect_key.
1264 * The collector will return the instant quorum or other requirements
1265 * are met, even if some nodes get behind or become non-responsive.
1267 * HAMMER2_XOP_COLLECT_NOWAIT - Used to 'poll' a completed collection,
1268 * usually called synchronously from the
1269 * node XOPs for the strategy code to
1270 * fake the frontend collection and complete
1271 * the BIO as soon as possible.
1273 * HAMMER2_XOP_SYNCHRONIZER - Reqeuest synchronization with a particular
1274 * cluster index, prevents looping when that
1275 * index is out of sync so caller can act on
1276 * the out of sync element. ESRCH and EDEADLK
1277 * can be returned if this flag is specified.
1279 * Returns 0 on success plus a filled out xop->cluster structure.
1280 * Return ENOENT on normal termination.
1281 * Otherwise return an error.
1284 hammer2_xop_collect(hammer2_xop_head_t *xop, int flags)
1286 hammer2_xop_fifo_t *fifo;
1287 hammer2_chain_t *chain;
1288 hammer2_key_t lokey;
1291 int adv; /* advance the element */
1293 uint32_t check_counter;
1297 * First loop tries to advance pieces of the cluster which
1300 lokey = HAMMER2_KEY_MAX;
1301 keynull = HAMMER2_CHECK_NULL;
1302 check_counter = xop->check_counter;
1305 for (i = 0; i < xop->cluster.nchains; ++i) {
1306 chain = xop->cluster.array[i].chain;
1307 if (chain == NULL) {
1309 } else if (chain->bref.key < xop->collect_key) {
1312 keynull &= ~HAMMER2_CHECK_NULL;
1313 if (lokey > chain->bref.key)
1314 lokey = chain->bref.key;
1321 * Advance element if possible, advanced element may be NULL.
1324 hammer2_chain_unlock(chain);
1325 hammer2_chain_drop(chain);
1327 fifo = &xop->collect[i];
1328 if (fifo->ri != fifo->wi) {
1330 chain = fifo->array[fifo->ri & HAMMER2_XOPFIFO_MASK];
1332 xop->cluster.array[i].chain = chain;
1333 if (chain == NULL) {
1334 xop->cluster.array[i].flags |=
1337 if (fifo->wi - fifo->ri < HAMMER2_XOPFIFO / 2)
1338 wakeup(xop); /* XXX optimize */
1339 --i; /* loop on same index */
1342 * Retain CITEM_NULL flag. If set just repeat EOF.
1343 * If not, the NULL,0 combination indicates an
1344 * operation in-progress.
1346 xop->cluster.array[i].chain = NULL;
1347 /* retain any CITEM_NULL setting */
1352 * Determine whether the lowest collected key meets clustering
1353 * requirements. Returns:
1355 * 0 - key valid, cluster can be returned.
1357 * ENOENT - normal end of scan, return ENOENT.
1359 * ESRCH - sufficient elements collected, quorum agreement
1360 * that lokey is not a valid element and should be
1363 * EDEADLK - sufficient elements collected, no quorum agreement
1364 * (and no agreement possible). In this situation a
1365 * repair is needed, for now we loop.
1367 * EINPROGRESS - insufficient elements collected to resolve, wait
1368 * for event and loop.
1370 error = hammer2_cluster_check(&xop->cluster, lokey, keynull);
1371 if (error == EINPROGRESS) {
1372 if (xop->check_counter == check_counter) {
1373 if (flags & HAMMER2_XOP_COLLECT_NOWAIT)
1375 tsleep_interlock(&xop->check_counter, 0);
1377 if (xop->check_counter == check_counter) {
1378 tsleep(&xop->check_counter, PINTERLOCKED,
1384 if (error == ESRCH) {
1385 if (lokey != HAMMER2_KEY_MAX) {
1386 xop->collect_key = lokey + 1;
1391 if (error == EDEADLK) {
1392 kprintf("hammer2: no quorum possible lokey %016jx\n",
1394 if (lokey != HAMMER2_KEY_MAX) {
1395 xop->collect_key = lokey + 1;
1400 if (lokey == HAMMER2_KEY_MAX)
1401 xop->collect_key = lokey;
1403 xop->collect_key = lokey + 1;
1409 * Primary management thread for xops support. Each node has several such
1410 * threads which replicate front-end operations on cluster nodes.
1412 * XOPS thread node operations, allowing the function to focus on a single
1413 * node in the cluster after validating the operation with the cluster.
1414 * This is primarily what prevents dead or stalled nodes from stalling
1418 hammer2_primary_xops_thread(void *arg)
1420 hammer2_thread_t *thr = arg;
1422 hammer2_xop_head_t *xop;
1423 hammer2_xop_group_t *xgrp;
1427 xgrp = &pmp->xop_groups[thr->repidx];
1428 mask = 1U << thr->clindex;
1430 lockmgr(&thr->lk, LK_EXCLUSIVE);
1431 while ((thr->flags & HAMMER2_THREAD_STOP) == 0) {
1433 * Handle freeze request
1435 if (thr->flags & HAMMER2_THREAD_FREEZE) {
1436 atomic_set_int(&thr->flags, HAMMER2_THREAD_FROZEN);
1437 atomic_clear_int(&thr->flags, HAMMER2_THREAD_FREEZE);
1441 * Force idle if frozen until unfrozen or stopped.
1443 if (thr->flags & HAMMER2_THREAD_FROZEN) {
1444 lksleep(&thr->flags, &thr->lk, 0, "frozen", 0);
1449 * Reset state on REMASTER request
1451 if (thr->flags & HAMMER2_THREAD_REMASTER) {
1452 atomic_clear_int(&thr->flags, HAMMER2_THREAD_REMASTER);
1457 * Process requests. Each request can be multi-queued.
1459 * If we get behind and the frontend VOP is no longer active,
1460 * we retire the request without processing it. The callback
1461 * may also abort processing if the frontend VOP becomes
1464 while ((xop = TAILQ_FIRST(&thr->xopq)) != NULL) {
1465 TAILQ_REMOVE(&thr->xopq, xop,
1466 collect[thr->clindex].entry);
1467 if (hammer2_xop_active(xop)) {
1468 lockmgr(&thr->lk, LK_RELEASE);
1469 xop->func((hammer2_xop_t *)xop, thr->clindex);
1470 hammer2_xop_retire(xop, mask);
1471 lockmgr(&thr->lk, LK_EXCLUSIVE);
1473 hammer2_xop_feed(xop, NULL, thr->clindex,
1475 hammer2_xop_retire(xop, mask);
1482 lksleep(&thr->flags, &thr->lk, 0, "h2idle", 0);
1486 * Cleanup / termination
1488 while ((xop = TAILQ_FIRST(&thr->xopq)) != NULL) {
1489 kprintf("hammer2_thread: aborting xop %p\n", xop->func);
1490 TAILQ_REMOVE(&thr->xopq, xop,
1491 collect[thr->clindex].entry);
1492 hammer2_xop_retire(xop, mask);
1497 lockmgr(&thr->lk, LK_RELEASE);
1498 /* thr structure can go invalid after this point */