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 the hammer2 helper thread API, including
36 * the frontend/backend XOP API.
41 * Signal that the thread has work.
44 hammer2_thr_signal(hammer2_thread_t *thr, uint32_t flags)
51 if (oflags & HAMMER2_THREAD_WAITING) {
52 if (atomic_cmpset_int(&thr->flags, oflags,
53 (oflags | flags) & ~HAMMER2_THREAD_WAITING)) {
58 if (atomic_cmpset_int(&thr->flags, oflags,
67 * Return status to waiting client(s)
69 * WARNING! During teardown (thr) can disappear the instant our cmpset
73 hammer2_thr_return(hammer2_thread_t *thr, uint32_t flags)
81 nflags = (oflags | flags) & ~HAMMER2_THREAD_CLIENTWAIT;
83 if (oflags & HAMMER2_THREAD_CLIENTWAIT) {
84 if (atomic_cmpset_int(&thr->flags, oflags, nflags)) {
89 if (atomic_cmpset_int(&thr->flags, oflags, nflags))
96 * Wait until the bits in flags are set.
98 * WARNING! During teardown (thr) can disappear the instant our cmpset
102 hammer2_thr_wait(hammer2_thread_t *thr, uint32_t flags)
110 if ((oflags & flags) == flags)
112 nflags = oflags | HAMMER2_THREAD_CLIENTWAIT;
113 tsleep_interlock(&thr->flags, 0);
114 if (atomic_cmpset_int(&thr->flags, oflags, nflags)) {
115 tsleep(&thr->flags, PINTERLOCKED, "h2twait", hz*60);
121 * Wait until the bits in flags are clear.
123 * WARNING! During teardown (thr) can disappear the instant our cmpset
127 hammer2_thr_wait_neg(hammer2_thread_t *thr, uint32_t flags)
135 if ((oflags & flags) == 0)
137 nflags = oflags | HAMMER2_THREAD_CLIENTWAIT;
138 tsleep_interlock(&thr->flags, 0);
139 if (atomic_cmpset_int(&thr->flags, oflags, nflags)) {
140 tsleep(&thr->flags, PINTERLOCKED, "h2twait", hz*60);
146 * Initialize the supplied thread structure, starting the specified
149 * NOTE: thr structure can be retained across mounts and unmounts for this
150 * pmp, so make sure the flags are in a sane state.
153 hammer2_thr_create(hammer2_thread_t *thr, hammer2_pfs_t *pmp,
154 const char *id, int clindex, int repidx,
155 void (*func)(void *arg))
158 thr->clindex = clindex;
159 thr->repidx = repidx;
160 TAILQ_INIT(&thr->xopq);
161 atomic_clear_int(&thr->flags, HAMMER2_THREAD_STOP |
162 HAMMER2_THREAD_STOPPED |
163 HAMMER2_THREAD_FREEZE |
164 HAMMER2_THREAD_FROZEN);
165 if (thr->scratch == NULL)
166 thr->scratch = kmalloc(MAXPHYS, M_HAMMER2, M_WAITOK | M_ZERO);
168 lwkt_create(func, thr, &thr->td, NULL, 0, repidx % ncpus,
169 "%s-%s.%02d", id, pmp->pfs_names[clindex], repidx);
171 lwkt_create(func, thr, &thr->td, NULL, 0, -1,
172 "%s-%s", id, pmp->pfs_names[clindex]);
177 * Terminate a thread. This function will silently return if the thread
178 * was never initialized or has already been deleted.
180 * This is accomplished by setting the STOP flag and waiting for the td
181 * structure to become NULL.
184 hammer2_thr_delete(hammer2_thread_t *thr)
188 hammer2_thr_signal(thr, HAMMER2_THREAD_STOP);
189 hammer2_thr_wait(thr, HAMMER2_THREAD_STOPPED);
192 kfree(thr->scratch, M_HAMMER2);
195 KKASSERT(TAILQ_EMPTY(&thr->xopq));
199 * Asynchronous remaster request. Ask the synchronization thread to
200 * start over soon (as if it were frozen and unfrozen, but without waiting).
201 * The thread always recalculates mastership relationships when restarting.
204 hammer2_thr_remaster(hammer2_thread_t *thr)
208 hammer2_thr_signal(thr, HAMMER2_THREAD_REMASTER);
212 hammer2_thr_freeze_async(hammer2_thread_t *thr)
214 hammer2_thr_signal(thr, HAMMER2_THREAD_FREEZE);
218 hammer2_thr_freeze(hammer2_thread_t *thr)
222 hammer2_thr_signal(thr, HAMMER2_THREAD_FREEZE);
223 hammer2_thr_wait(thr, HAMMER2_THREAD_FROZEN);
227 hammer2_thr_unfreeze(hammer2_thread_t *thr)
231 hammer2_thr_signal(thr, HAMMER2_THREAD_UNFREEZE);
232 hammer2_thr_wait_neg(thr, HAMMER2_THREAD_FROZEN);
236 hammer2_thr_break(hammer2_thread_t *thr)
238 if (thr->flags & (HAMMER2_THREAD_STOP |
239 HAMMER2_THREAD_REMASTER |
240 HAMMER2_THREAD_FREEZE)) {
246 /****************************************************************************
248 ****************************************************************************/
251 hammer2_xop_group_init(hammer2_pfs_t *pmp, hammer2_xop_group_t *xgrp)
253 /* no extra fields in structure at the moment */
257 * Allocate a XOP request.
259 * Once allocated a XOP request can be started, collected, and retired,
260 * and can be retired early if desired.
262 * NOTE: Fifo indices might not be zero but ri == wi on objcache_get().
265 hammer2_xop_alloc(hammer2_inode_t *ip, int flags)
269 xop = objcache_get(cache_xops, M_WAITOK);
270 KKASSERT(xop->head.cluster.array[0].chain == NULL);
273 xop->head.func = NULL;
274 xop->head.flags = flags;
277 xop->head.collect_key = 0;
278 xop->head.check_counter = 0;
279 if (flags & HAMMER2_XOP_MODIFYING)
280 xop->head.mtid = hammer2_trans_sub(ip->pmp);
284 xop->head.cluster.nchains = ip->cluster.nchains;
285 xop->head.cluster.pmp = ip->pmp;
286 xop->head.cluster.flags = HAMMER2_CLUSTER_LOCKED;
289 * run_mask - Active thread (or frontend) associated with XOP
291 xop->head.run_mask = HAMMER2_XOPMASK_VOP;
293 hammer2_inode_ref(ip);
299 hammer2_xop_setname(hammer2_xop_head_t *xop, const char *name, size_t name_len)
301 xop->name1 = kmalloc(name_len + 1, M_HAMMER2, M_WAITOK | M_ZERO);
302 xop->name1_len = name_len;
303 bcopy(name, xop->name1, name_len);
307 hammer2_xop_setname2(hammer2_xop_head_t *xop, const char *name, size_t name_len)
309 xop->name2 = kmalloc(name_len + 1, M_HAMMER2, M_WAITOK | M_ZERO);
310 xop->name2_len = name_len;
311 bcopy(name, xop->name2, name_len);
315 hammer2_xop_setname_inum(hammer2_xop_head_t *xop, hammer2_key_t inum)
317 const size_t name_len = 18;
319 xop->name1 = kmalloc(name_len + 1, M_HAMMER2, M_WAITOK | M_ZERO);
320 xop->name1_len = name_len;
321 ksnprintf(xop->name1, name_len + 1, "0x%016jx", (intmax_t)inum);
328 hammer2_xop_setip2(hammer2_xop_head_t *xop, hammer2_inode_t *ip2)
331 hammer2_inode_ref(ip2);
335 hammer2_xop_setip3(hammer2_xop_head_t *xop, hammer2_inode_t *ip3)
338 hammer2_inode_ref(ip3);
342 hammer2_xop_reinit(hammer2_xop_head_t *xop)
346 xop->collect_key = 0;
347 xop->run_mask = HAMMER2_XOPMASK_VOP;
351 * A mounted PFS needs Xops threads to support frontend operations.
354 hammer2_xop_helper_create(hammer2_pfs_t *pmp)
359 lockmgr(&pmp->lock, LK_EXCLUSIVE);
360 pmp->has_xop_threads = 1;
362 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
363 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
364 if (pmp->xop_groups[j].thrs[i].td)
366 hammer2_thr_create(&pmp->xop_groups[j].thrs[i], pmp,
368 hammer2_primary_xops_thread);
371 lockmgr(&pmp->lock, LK_RELEASE);
375 hammer2_xop_helper_cleanup(hammer2_pfs_t *pmp)
380 for (i = 0; i < pmp->pfs_nmasters; ++i) {
381 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
382 if (pmp->xop_groups[j].thrs[i].td)
383 hammer2_thr_delete(&pmp->xop_groups[j].thrs[i]);
386 pmp->has_xop_threads = 0;
390 * Start a XOP request, queueing it to all nodes in the cluster to
391 * execute the cluster op.
393 * XXX optimize single-target case.
396 hammer2_xop_start_except(hammer2_xop_head_t *xop, hammer2_xop_func_t func,
399 hammer2_inode_t *ip1;
401 hammer2_thread_t *thr;
408 if (pmp->has_xop_threads == 0)
409 hammer2_xop_helper_create(pmp);
412 * The intent of the XOP sequencer is to ensure that ops on the same
413 * inode execute in the same order. This is necessary when issuing
414 * modifying operations to multiple targets because some targets might
415 * get behind and the frontend is allowed to complete the moment a
416 * quorum of targets succeed.
418 * Strategy operations must be segregated from non-strategy operations
419 * to avoid a deadlock. For example, if a vfsync and a bread/bwrite
420 * were queued to the same worker thread, the locked buffer in the
421 * strategy operation can deadlock the vfsync's buffer list scan.
423 * TODO - RENAME fails here because it is potentially modifying
424 * three different inodes.
426 if (xop->flags & HAMMER2_XOP_STRATEGY) {
427 hammer2_xop_strategy_t *xopst;
429 xopst = &((hammer2_xop_t *)xop)->xop_strategy;
430 ng = (int)(hammer2_icrc32(&xop->ip1, sizeof(xop->ip1)) ^
431 hammer2_icrc32(&xopst->lbase, sizeof(xopst->lbase)));
432 ng = ng & (HAMMER2_XOPGROUPS_MASK >> 1);
433 ng += HAMMER2_XOPGROUPS / 2;
435 ng = (int)(hammer2_icrc32(&xop->ip1, sizeof(xop->ip1)));
436 ng = ng & (HAMMER2_XOPGROUPS_MASK >> 1);
441 * The instant xop is queued another thread can pick it off. In the
442 * case of asynchronous ops, another thread might even finish and
445 hammer2_spin_ex(&pmp->xop_spin);
446 nchains = ip1->cluster.nchains;
447 for (i = 0; i < nchains; ++i) {
449 * XXX ip1->cluster.array* not stable here. This temporary
450 * hack fixes basic issues in target XOPs which need to
451 * obtain a starting chain from the inode but does not
452 * address possible races against inode updates which
453 * might NULL-out a chain.
455 if (i != notidx && ip1->cluster.array[i].chain) {
456 thr = &pmp->xop_groups[ng].thrs[i];
457 atomic_set_int(&xop->run_mask, 1U << i);
458 atomic_set_int(&xop->chk_mask, 1U << i);
459 xop->collect[i].thr = thr;
460 TAILQ_INSERT_TAIL(&thr->xopq, xop, collect[i].entry);
463 hammer2_spin_unex(&pmp->xop_spin);
464 /* xop can become invalid at this point */
467 * Each thread has its own xopq
469 for (i = 0; i < nchains; ++i) {
471 thr = &pmp->xop_groups[ng].thrs[i];
472 hammer2_thr_signal(thr, HAMMER2_THREAD_XOPQ);
478 hammer2_xop_start(hammer2_xop_head_t *xop, hammer2_xop_func_t func)
480 hammer2_xop_start_except(xop, func, -1);
484 * Retire a XOP. Used by both the VOP frontend and by the XOP backend.
487 hammer2_xop_retire(hammer2_xop_head_t *xop, uint32_t mask)
489 hammer2_chain_t *chain;
494 * Remove the frontend collector or remove a backend feeder.
495 * When removing the frontend we must wakeup any backend feeders
496 * who are waiting for FIFO space.
498 * XXX optimize wakeup.
500 KKASSERT(xop->run_mask & mask);
501 nmask = atomic_fetchadd_int(&xop->run_mask, -mask);
502 if ((nmask & ~HAMMER2_XOPMASK_FIFOW) != mask) {
503 if (mask == HAMMER2_XOPMASK_VOP) {
504 if (nmask & HAMMER2_XOPMASK_FIFOW)
509 /* else nobody else left, we can ignore FIFOW */
512 * All collectors are gone, we can cleanup and dispose of the XOP.
513 * Note that this can wind up being a frontend OR a backend.
514 * Pending chains are locked shared and not owned by any thread.
516 * Cleanup the collection cluster.
518 for (i = 0; i < xop->cluster.nchains; ++i) {
519 xop->cluster.array[i].flags = 0;
520 chain = xop->cluster.array[i].chain;
522 xop->cluster.array[i].chain = NULL;
523 hammer2_chain_drop_unhold(chain);
528 * Cleanup the fifos, use check_counter to optimize the loop.
529 * Since we are the only entity left on this xop we don't have
530 * to worry about fifo flow control, and one lfence() will do the
534 mask = xop->chk_mask;
535 for (i = 0; mask && i < HAMMER2_MAXCLUSTER; ++i) {
536 hammer2_xop_fifo_t *fifo = &xop->collect[i];
537 while (fifo->ri != fifo->wi) {
538 chain = fifo->array[fifo->ri & HAMMER2_XOPFIFO_MASK];
540 hammer2_chain_drop_unhold(chain);
547 * The inode is only held at this point, simply drop it.
550 hammer2_inode_drop(xop->ip1);
554 hammer2_inode_drop(xop->ip2);
558 hammer2_inode_drop(xop->ip3);
562 kfree(xop->name1, M_HAMMER2);
567 kfree(xop->name2, M_HAMMER2);
572 objcache_put(cache_xops, xop);
576 * (Backend) Returns non-zero if the frontend is still attached.
579 hammer2_xop_active(hammer2_xop_head_t *xop)
581 if (xop->run_mask & HAMMER2_XOPMASK_VOP)
588 * (Backend) Feed chain data through the cluster validator and back to
589 * the frontend. Chains are fed from multiple nodes concurrently
590 * and pipelined via per-node FIFOs in the XOP.
592 * The chain must be locked (either shared or exclusive). The caller may
593 * unlock and drop the chain on return. This function will add an extra
594 * ref and hold the chain's data for the pass-back.
596 * No xop lock is needed because we are only manipulating fields under
597 * our direct control.
599 * Returns 0 on success and a hammer error code if sync is permanently
600 * lost. The caller retains a ref on the chain but by convention
601 * the lock is typically inherited by the xop (caller loses lock).
603 * Returns non-zero on error. In this situation the caller retains a
604 * ref on the chain but loses the lock (we unlock here).
607 hammer2_xop_feed(hammer2_xop_head_t *xop, hammer2_chain_t *chain,
608 int clindex, int error)
610 hammer2_xop_fifo_t *fifo;
614 * Early termination (typicaly of xop_readir)
616 if (hammer2_xop_active(xop) == 0) {
622 * Multi-threaded entry into the XOP collector. We own the
623 * fifo->wi for our clindex.
625 fifo = &xop->collect[clindex];
627 if (fifo->ri == fifo->wi - HAMMER2_XOPFIFO)
629 while (fifo->ri == fifo->wi - HAMMER2_XOPFIFO) {
630 atomic_set_int(&fifo->flags, HAMMER2_XOP_FIFO_STALL);
631 mask = xop->run_mask;
632 if ((mask & HAMMER2_XOPMASK_VOP) == 0) {
636 tsleep_interlock(xop, 0);
637 if (atomic_cmpset_int(&xop->run_mask, mask,
638 mask | HAMMER2_XOPMASK_FIFOW)) {
639 if (fifo->ri == fifo->wi - HAMMER2_XOPFIFO) {
640 tsleep(xop, PINTERLOCKED, "h2feed", hz*60);
645 atomic_clear_int(&fifo->flags, HAMMER2_XOP_FIFO_STALL);
647 hammer2_chain_ref_hold(chain);
648 if (error == 0 && chain)
649 error = chain->error;
650 fifo->errors[fifo->wi & HAMMER2_XOPFIFO_MASK] = error;
651 fifo->array[fifo->wi & HAMMER2_XOPFIFO_MASK] = chain;
654 if (atomic_fetchadd_int(&xop->check_counter, HAMMER2_XOP_CHKINC) &
655 HAMMER2_XOP_CHKWAIT) {
656 atomic_clear_int(&xop->check_counter, HAMMER2_XOP_CHKWAIT);
657 wakeup(&xop->check_counter);
662 * Cleanup. If an error occurred we eat the lock. If no error
663 * occurred the fifo inherits the lock and gains an additional ref.
665 * The caller's ref remains in both cases.
672 * (Frontend) collect a response from a running cluster op.
674 * Responses are fed from all appropriate nodes concurrently
675 * and collected into a cohesive response >= collect_key.
677 * The collector will return the instant quorum or other requirements
678 * are met, even if some nodes get behind or become non-responsive.
680 * HAMMER2_XOP_COLLECT_NOWAIT - Used to 'poll' a completed collection,
681 * usually called synchronously from the
682 * node XOPs for the strategy code to
683 * fake the frontend collection and complete
684 * the BIO as soon as possible.
686 * HAMMER2_XOP_SYNCHRONIZER - Reqeuest synchronization with a particular
687 * cluster index, prevents looping when that
688 * index is out of sync so caller can act on
689 * the out of sync element. ESRCH and EDEADLK
690 * can be returned if this flag is specified.
692 * Returns 0 on success plus a filled out xop->cluster structure.
693 * Return ENOENT on normal termination.
694 * Otherwise return an error.
697 hammer2_xop_collect(hammer2_xop_head_t *xop, int flags)
699 hammer2_xop_fifo_t *fifo;
700 hammer2_chain_t *chain;
704 int adv; /* advance the element */
706 uint32_t check_counter;
710 * First loop tries to advance pieces of the cluster which
713 lokey = HAMMER2_KEY_MAX;
714 keynull = HAMMER2_CHECK_NULL;
715 check_counter = xop->check_counter;
718 for (i = 0; i < xop->cluster.nchains; ++i) {
719 chain = xop->cluster.array[i].chain;
722 } else if (chain->bref.key < xop->collect_key) {
725 keynull &= ~HAMMER2_CHECK_NULL;
726 if (lokey > chain->bref.key)
727 lokey = chain->bref.key;
734 * Advance element if possible, advanced element may be NULL.
737 hammer2_chain_drop_unhold(chain);
739 fifo = &xop->collect[i];
740 if (fifo->ri != fifo->wi) {
742 chain = fifo->array[fifo->ri & HAMMER2_XOPFIFO_MASK];
743 error = fifo->errors[fifo->ri & HAMMER2_XOPFIFO_MASK];
745 xop->cluster.array[i].chain = chain;
746 xop->cluster.array[i].error = error;
749 xop->cluster.array[i].flags |=
752 if (fifo->wi - fifo->ri <= HAMMER2_XOPFIFO / 2) {
753 if (fifo->flags & HAMMER2_XOP_FIFO_STALL) {
754 atomic_clear_int(&fifo->flags,
755 HAMMER2_XOP_FIFO_STALL);
760 --i; /* loop on same index */
763 * Retain CITEM_NULL flag. If set just repeat EOF.
764 * If not, the NULL,0 combination indicates an
765 * operation in-progress.
767 xop->cluster.array[i].chain = NULL;
768 /* retain any CITEM_NULL setting */
773 * Determine whether the lowest collected key meets clustering
774 * requirements. Returns:
776 * 0 - key valid, cluster can be returned.
778 * ENOENT - normal end of scan, return ENOENT.
780 * ESRCH - sufficient elements collected, quorum agreement
781 * that lokey is not a valid element and should be
784 * EDEADLK - sufficient elements collected, no quorum agreement
785 * (and no agreement possible). In this situation a
786 * repair is needed, for now we loop.
788 * EINPROGRESS - insufficient elements collected to resolve, wait
789 * for event and loop.
791 if ((flags & HAMMER2_XOP_COLLECT_WAITALL) &&
792 xop->run_mask != HAMMER2_XOPMASK_VOP) {
795 error = hammer2_cluster_check(&xop->cluster, lokey, keynull);
797 if (error == EINPROGRESS) {
798 if ((flags & HAMMER2_XOP_COLLECT_NOWAIT) == 0)
799 tsleep_interlock(&xop->check_counter, 0);
800 if (atomic_cmpset_int(&xop->check_counter,
802 check_counter | HAMMER2_XOP_CHKWAIT)) {
803 if (flags & HAMMER2_XOP_COLLECT_NOWAIT)
805 tsleep(&xop->check_counter, PINTERLOCKED, "h2coll", hz*60);
809 if (error == ESRCH) {
810 if (lokey != HAMMER2_KEY_MAX) {
811 xop->collect_key = lokey + 1;
816 if (error == EDEADLK) {
817 kprintf("hammer2: no quorum possible lokey %016jx\n",
819 if (lokey != HAMMER2_KEY_MAX) {
820 xop->collect_key = lokey + 1;
825 if (lokey == HAMMER2_KEY_MAX)
826 xop->collect_key = lokey;
828 xop->collect_key = lokey + 1;
834 * N x M processing threads are available to handle XOPs, N per cluster
835 * index x M cluster nodes.
837 * Locate and return the next runnable xop, or NULL if no xops are
838 * present or none of the xops are currently runnable (for various reasons).
839 * The xop is left on the queue and serves to block other dependent xops
842 * Dependent xops will not be returned.
844 * Sets HAMMER2_XOP_FIFO_RUN on the returned xop or returns NULL.
846 * NOTE! Xops run concurrently for each cluster index.
848 #define XOP_HASH_SIZE 16
849 #define XOP_HASH_MASK (XOP_HASH_SIZE - 1)
853 xop_testhash(hammer2_thread_t *thr, hammer2_inode_t *ip, uint32_t *hash)
858 hv = (int)((uintptr_t)ip + (uintptr_t)thr) / sizeof(hammer2_inode_t);
859 mask = 1U << (hv & 31);
862 return ((int)(hash[hv & XOP_HASH_MASK] & mask));
867 xop_sethash(hammer2_thread_t *thr, hammer2_inode_t *ip, uint32_t *hash)
872 hv = (int)((uintptr_t)ip + (uintptr_t)thr) / sizeof(hammer2_inode_t);
873 mask = 1U << (hv & 31);
876 hash[hv & XOP_HASH_MASK] |= mask;
881 hammer2_xop_next(hammer2_thread_t *thr)
883 hammer2_pfs_t *pmp = thr->pmp;
884 int clindex = thr->clindex;
885 uint32_t hash[XOP_HASH_SIZE] = { 0 };
886 hammer2_xop_head_t *xop;
888 hammer2_spin_ex(&pmp->xop_spin);
889 TAILQ_FOREACH(xop, &thr->xopq, collect[clindex].entry) {
893 if (xop_testhash(thr, xop->ip1, hash) ||
894 (xop->ip2 && xop_testhash(thr, xop->ip2, hash)) ||
895 (xop->ip3 && xop_testhash(thr, xop->ip3, hash))) {
898 xop_sethash(thr, xop->ip1, hash);
900 xop_sethash(thr, xop->ip2, hash);
902 xop_sethash(thr, xop->ip3, hash);
905 * Check already running
907 if (xop->collect[clindex].flags & HAMMER2_XOP_FIFO_RUN)
911 * Found a good one, return it.
913 atomic_set_int(&xop->collect[clindex].flags,
914 HAMMER2_XOP_FIFO_RUN);
917 hammer2_spin_unex(&pmp->xop_spin);
923 * Remove the completed XOP from the queue, clear HAMMER2_XOP_FIFO_RUN.
925 * NOTE! Xops run concurrently for each cluster index.
929 hammer2_xop_dequeue(hammer2_thread_t *thr, hammer2_xop_head_t *xop)
931 hammer2_pfs_t *pmp = thr->pmp;
932 int clindex = thr->clindex;
934 hammer2_spin_ex(&pmp->xop_spin);
935 TAILQ_REMOVE(&thr->xopq, xop, collect[clindex].entry);
936 atomic_clear_int(&xop->collect[clindex].flags,
937 HAMMER2_XOP_FIFO_RUN);
938 hammer2_spin_unex(&pmp->xop_spin);
939 if (TAILQ_FIRST(&thr->xopq))
940 hammer2_thr_signal(thr, HAMMER2_THREAD_XOPQ);
944 * Primary management thread for xops support. Each node has several such
945 * threads which replicate front-end operations on cluster nodes.
947 * XOPS thread node operations, allowing the function to focus on a single
948 * node in the cluster after validating the operation with the cluster.
949 * This is primarily what prevents dead or stalled nodes from stalling
953 hammer2_primary_xops_thread(void *arg)
955 hammer2_thread_t *thr = arg;
957 hammer2_xop_head_t *xop;
961 hammer2_xop_func_t last_func = NULL;
964 /*xgrp = &pmp->xop_groups[thr->repidx]; not needed */
965 mask = 1U << thr->clindex;
971 * Handle stop request
973 if (flags & HAMMER2_THREAD_STOP)
977 * Handle freeze request
979 if (flags & HAMMER2_THREAD_FREEZE) {
980 nflags = (flags & ~(HAMMER2_THREAD_FREEZE |
981 HAMMER2_THREAD_CLIENTWAIT)) |
982 HAMMER2_THREAD_FROZEN;
983 if (!atomic_cmpset_int(&thr->flags, flags, nflags))
985 if (flags & HAMMER2_THREAD_CLIENTWAIT)
991 if (flags & HAMMER2_THREAD_UNFREEZE) {
992 nflags = flags & ~(HAMMER2_THREAD_UNFREEZE |
993 HAMMER2_THREAD_FROZEN |
994 HAMMER2_THREAD_CLIENTWAIT);
995 if (!atomic_cmpset_int(&thr->flags, flags, nflags))
997 if (flags & HAMMER2_THREAD_CLIENTWAIT)
1004 * Force idle if frozen until unfrozen or stopped.
1006 if (flags & HAMMER2_THREAD_FROZEN) {
1007 nflags = flags | HAMMER2_THREAD_WAITING;
1008 tsleep_interlock(&thr->flags, 0);
1009 if (atomic_cmpset_int(&thr->flags, flags, nflags)) {
1010 tsleep(&thr->flags, PINTERLOCKED, "frozen", 0);
1011 atomic_clear_int(&thr->flags,
1012 HAMMER2_THREAD_WAITING);
1018 * Reset state on REMASTER request
1020 if (flags & HAMMER2_THREAD_REMASTER) {
1021 nflags = flags & ~HAMMER2_THREAD_REMASTER;
1022 if (atomic_cmpset_int(&thr->flags, flags, nflags)) {
1023 /* reset state here */
1029 * Process requests. Each request can be multi-queued.
1031 * If we get behind and the frontend VOP is no longer active,
1032 * we retire the request without processing it. The callback
1033 * may also abort processing if the frontend VOP becomes
1036 if (flags & HAMMER2_THREAD_XOPQ) {
1037 nflags = flags & ~HAMMER2_THREAD_XOPQ;
1038 if (!atomic_cmpset_int(&thr->flags, flags, nflags))
1043 while ((xop = hammer2_xop_next(thr)) != NULL) {
1044 if (hammer2_xop_active(xop)) {
1045 last_func = xop->func;
1046 xop->func(thr, (hammer2_xop_t *)xop);
1047 hammer2_xop_dequeue(thr, xop);
1048 hammer2_xop_retire(xop, mask);
1050 last_func = xop->func;
1051 hammer2_xop_feed(xop, NULL, thr->clindex,
1053 hammer2_xop_dequeue(thr, xop);
1054 hammer2_xop_retire(xop, mask);
1059 * Wait for event, interlock using THREAD_WAITING and
1062 * For robustness poll on a 30-second interval, but nominally
1063 * expect to be woken up.
1065 nflags = flags | HAMMER2_THREAD_WAITING;
1067 tsleep_interlock(&thr->flags, 0);
1068 if (atomic_cmpset_int(&thr->flags, flags, nflags)) {
1069 tsleep(&thr->flags, PINTERLOCKED, "h2idle", hz*30);
1070 atomic_clear_int(&thr->flags, HAMMER2_THREAD_WAITING);
1076 * Cleanup / termination
1078 while ((xop = TAILQ_FIRST(&thr->xopq)) != NULL) {
1079 kprintf("hammer2_thread: aborting xop %p\n", xop->func);
1080 TAILQ_REMOVE(&thr->xopq, xop,
1081 collect[thr->clindex].entry);
1082 hammer2_xop_retire(xop, mask);
1086 hammer2_thr_return(thr, HAMMER2_THREAD_STOPPED);
1087 /* thr structure can go invalid after this point */