2 * Copyright (c) 2011-2013 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>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This subsystem implements most of the core support functions for
37 * the hammer2_chain structure.
39 * Chains are the in-memory version on media objects (volume header, inodes,
40 * indirect blocks, data blocks, etc). Chains represent a portion of the
43 * A chain is topologically stable once it has been inserted into the
44 * in-memory topology. Modifications which copy, move, or resize the chain
45 * are handled via the DELETE-DUPLICATE mechanic where the original chain
46 * stays intact but is marked deleted and a new chain is allocated which
47 * shares the old chain's children.
49 * This sharing is handled via the hammer2_chain_core structure.
51 * The DELETE-DUPLICATE mechanism allows the same topological level to contain
52 * many overloadings. However, our RBTREE mechanics require that there be
53 * no overlaps so we accomplish the overloading by moving conflicting chains
54 * with smaller or equal radii into a sub-RBTREE under the chain being
57 * DELETE-DUPLICATE is also used when a modification to a chain crosses a
58 * flush synchronization boundary, allowing the flush code to continue flushing
59 * the older version of the topology and not be disrupted by new frontend
64 * All lookup and iterate operations and most modifications are done on the
65 * live view. During flushes lookups are not normally done and modifications
66 * may be run on the flush view. However, flushes often needs to allocate
67 * blocks and the freemap_alloc/free code issues lookups. This code is
68 * special cased to use the live view when called from a flush.
70 * General chain lookup/iteration functions are NOT aware of the flush view,
71 * they only know about live views.
73 #include <sys/cdefs.h>
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/types.h>
78 #include <sys/kern_syscall.h>
83 static int hammer2_indirect_optimize; /* XXX SYSCTL */
85 static hammer2_chain_t *hammer2_chain_create_indirect(
86 hammer2_trans_t *trans, hammer2_chain_t *parent,
87 hammer2_key_t key, int keybits, int for_type, int *errorp);
88 static void hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop);
89 static void adjreadcounter(hammer2_blockref_t *bref, size_t bytes);
90 static hammer2_chain_t *hammer2_combined_find(
91 hammer2_chain_t *parent,
92 hammer2_blockref_t *base, int count,
93 int *cache_indexp, hammer2_key_t *key_nextp,
94 hammer2_key_t key_beg, hammer2_key_t key_end,
95 hammer2_blockref_t **bresp);
96 static void hammer2_chain_assert_not_present(hammer2_chain_core_t *above,
97 hammer2_chain_t *chain);
99 hammer2_chain_t *XXChain;
102 * Basic RBTree for chains. Chains cannot overlap within any given
103 * core->rbtree without recursing through chain->rbtree. We effectively
104 * guarantee this by checking the full range rather than just the first
105 * key element. By matching on the full range callers can detect when
106 * recursrion through chain->rbtree is needed.
108 * NOTE: This also means the a delete-duplicate on the same key will
109 * overload by placing the deleted element in the new element's
110 * chain->rbtree (when doing a direct replacement).
112 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
115 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
117 hammer2_key_t c1_beg;
118 hammer2_key_t c1_end;
119 hammer2_key_t c2_beg;
120 hammer2_key_t c2_end;
122 c1_beg = chain1->bref.key;
123 c1_end = c1_beg + ((hammer2_key_t)1 << chain1->bref.keybits) - 1;
124 c2_beg = chain2->bref.key;
125 c2_end = c2_beg + ((hammer2_key_t)1 << chain2->bref.keybits) - 1;
127 if (c1_end < c2_beg) /* fully to the left */
129 if (c1_beg > c2_end) /* fully to the right */
131 return(0); /* overlap (must not cross edge boundary) */
136 hammer2_isclusterable(hammer2_chain_t *chain)
138 if (hammer2_cluster_enable) {
139 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
140 chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
141 chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
149 * Recursively set the update_hi flag up to the root starting at chain's
150 * parent->core. update_hi is not set in chain's core.
152 * This controls top-down visibility for flushes. The child has just one
153 * 'above' core, but the core itself can be multi-homed with parents iterated
156 * This function is not used during a flush (except when the flush is
157 * allocating which requires the live tree). The flush keeps track of its
160 * XXX needs to be optimized to use roll-up TIDs. update_hi is only really
161 * compared against bref.mirror_tid which itself is only updated by a flush.
164 hammer2_chain_setsubmod(hammer2_trans_t *trans, hammer2_chain_t *chain)
166 hammer2_chain_core_t *above;
168 while ((above = chain->above) != NULL) {
169 spin_lock(&above->cst.spin);
171 if (above->update_hi < trans->sync_tid)
172 above->update_hi = trans->sync_tid;
173 chain = TAILQ_LAST(&above->ownerq, h2_core_list);
175 TAILQ_FOREACH_REVERSE(chain, &above->ownerq,
176 h2_core_list, core_entry) {
177 if (trans->sync_tid >= chain->modify_tid &&
178 trans->sync_tid <= chain->delete_tid) {
183 spin_unlock(&above->cst.spin);
188 * Allocate a new disconnected chain element representing the specified
189 * bref. chain->refs is set to 1 and the passed bref is copied to
190 * chain->bref. chain->bytes is derived from the bref.
192 * chain->core is NOT allocated and the media data and bp pointers are left
193 * NULL. The caller must call chain_core_alloc() to allocate or associate
194 * a core with the chain.
196 * NOTE: Returns a referenced but unlocked (because there is no core) chain.
199 hammer2_chain_alloc(hammer2_mount_t *hmp, hammer2_pfsmount_t *pmp,
200 hammer2_trans_t *trans, hammer2_blockref_t *bref)
202 hammer2_chain_t *chain;
203 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
206 * Construct the appropriate system structure.
209 case HAMMER2_BREF_TYPE_INODE:
210 case HAMMER2_BREF_TYPE_INDIRECT:
211 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
212 case HAMMER2_BREF_TYPE_DATA:
213 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
215 * Chain's are really only associated with the hmp but we
216 * maintain a pmp association for per-mount memory tracking
217 * purposes. The pmp can be NULL.
219 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
223 case HAMMER2_BREF_TYPE_VOLUME:
224 case HAMMER2_BREF_TYPE_FREEMAP:
226 panic("hammer2_chain_alloc volume type illegal for op");
229 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
235 chain->bytes = bytes;
237 chain->flags = HAMMER2_CHAIN_ALLOCATED;
238 chain->delete_tid = HAMMER2_MAX_TID;
241 * Set modify_tid if a transaction is creating the chain. When
242 * loading a chain from backing store trans is passed as NULL and
243 * modify_tid is left set to 0.
246 chain->modify_tid = trans->sync_tid;
252 * Associate an existing core with the chain or allocate a new core.
254 * The core is not locked. No additional refs on the chain are made.
255 * (trans) must not be NULL if (core) is not NULL.
257 * When chains are delete-duplicated during flushes we insert nchain on
258 * the ownerq after ochain instead of at the end in order to give the
259 * drop code visibility in the correct order, otherwise drops can be missed.
262 hammer2_chain_core_alloc(hammer2_trans_t *trans,
263 hammer2_chain_t *nchain, hammer2_chain_t *ochain)
265 hammer2_chain_core_t *core;
267 KKASSERT(nchain->core == NULL);
269 if (ochain == NULL) {
271 * Fresh core under nchain (no multi-homing of ochain's
274 core = kmalloc(sizeof(*core), nchain->hmp->mchain,
276 TAILQ_INIT(&core->layerq);
277 TAILQ_INIT(&core->ownerq);
281 core->update_hi = trans->sync_tid;
283 core->update_hi = nchain->bref.mirror_tid;
285 ccms_cst_init(&core->cst, nchain);
286 TAILQ_INSERT_TAIL(&core->ownerq, nchain, core_entry);
289 * Propagate the PFSROOT flag which we set on all subdirs
290 * under the super-root.
292 atomic_set_int(&nchain->flags,
293 ochain->flags & HAMMER2_CHAIN_PFSROOT);
296 * Duplicating ochain -> nchain. Set the DUPLICATED flag on
297 * ochain if nchain is not a snapshot.
299 * It is possible for the DUPLICATED flag to already be
300 * set when called via a flush operation because flush
301 * operations may have to work on elements with delete_tid's
302 * beyond the flush sync_tid. In this situation we must
303 * ensure that nchain is placed just after ochain in the
304 * ownerq and that the DUPLICATED flag is set on nchain so
305 * 'live' operations skip past it to the correct chain.
307 * The flusher understands the blockref synchronization state
308 * for any stale chains by observing bref.mirror_tid, which
309 * delete-duplicate replicates.
311 * WARNING! However, the case is disallowed when the flusher
312 * is allocating freemap space because this entails
313 * more than just adjusting a block table.
315 if (ochain->flags & HAMMER2_CHAIN_DUPLICATED) {
316 KKASSERT((trans->flags &
317 (HAMMER2_TRANS_ISFLUSH |
318 HAMMER2_TRANS_ISALLOCATING)) ==
319 HAMMER2_TRANS_ISFLUSH);
320 atomic_set_int(&nchain->flags,
321 HAMMER2_CHAIN_DUPLICATED);
323 if ((nchain->flags & HAMMER2_CHAIN_SNAPSHOT) == 0) {
324 atomic_set_int(&ochain->flags,
325 HAMMER2_CHAIN_DUPLICATED);
328 atomic_add_int(&core->sharecnt, 1);
330 spin_lock(&core->cst.spin);
334 if (core->update_hi < trans->sync_tid)
335 core->update_hi = trans->sync_tid;
339 * Maintain ordering for refactor test so we don't skip over
340 * a snapshot. Also, during flushes, delete-duplications
341 * for block-table updates can occur on blocks already
342 * deleted (delete-duplicated by a later transaction). We
343 * must insert nchain after ochain but before the later
344 * transaction's copy.
346 TAILQ_INSERT_AFTER(&core->ownerq, ochain, nchain, core_entry);
348 spin_unlock(&core->cst.spin);
353 * Add a reference to a chain element, preventing its destruction.
356 hammer2_chain_ref(hammer2_chain_t *chain)
358 atomic_add_int(&chain->refs, 1);
362 * Insert the chain in the core rbtree at the first layer
363 * which accepts it (for now we don't sort layers by the transaction tid)
365 #define HAMMER2_CHAIN_INSERT_SPIN 0x0001
366 #define HAMMER2_CHAIN_INSERT_LIVE 0x0002
367 #define HAMMER2_CHAIN_INSERT_RACE 0x0004
371 hammer2_chain_insert(hammer2_chain_core_t *above, hammer2_chain_layer_t *layer,
372 hammer2_chain_t *chain, int flags, int generation)
374 hammer2_chain_t *xchain;
375 hammer2_chain_layer_t *nlayer;
378 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
379 spin_lock(&above->cst.spin);
382 * Special case, place the chain in the next most-recent layer as the
383 * specified layer, inserting a layer inbetween if necessary.
386 KKASSERT((flags & HAMMER2_CHAIN_INSERT_RACE) == 0);
387 nlayer = TAILQ_PREV(layer, h2_layer_list, entry);
388 if (nlayer && RB_INSERT(hammer2_chain_tree,
389 &nlayer->rbtree, chain) == NULL) {
394 spin_unlock(&above->cst.spin);
395 KKASSERT((flags & HAMMER2_CHAIN_INSERT_LIVE) == 0);
396 nlayer = kmalloc(sizeof(*nlayer), chain->hmp->mchain,
398 RB_INIT(&nlayer->rbtree);
399 nlayer->good = 0xABCD;
400 spin_lock(&above->cst.spin);
402 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0)
403 hammer2_chain_assert_not_present(above, chain);
405 TAILQ_INSERT_BEFORE(layer, nlayer, entry);
406 RB_INSERT(hammer2_chain_tree, &nlayer->rbtree, chain);
412 * Interlocked by spinlock, check for race
414 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
415 above->generation != generation) {
421 * Try to insert, allocate a new layer if a nominal collision
422 * occurs (a collision is different from a SMP race).
424 layer = TAILQ_FIRST(&above->layerq);
428 (xchain = RB_INSERT(hammer2_chain_tree,
429 &layer->rbtree, chain)) != NULL) {
432 * Allocate a new layer to resolve the issue.
434 spin_unlock(&above->cst.spin);
435 layer = kmalloc(sizeof(*layer), chain->hmp->mchain,
437 RB_INIT(&layer->rbtree);
438 layer->good = 0xABCD;
439 spin_lock(&above->cst.spin);
441 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
442 above->generation != generation) {
443 spin_unlock(&above->cst.spin);
444 kfree(layer, chain->hmp->mchain);
445 spin_lock(&above->cst.spin);
449 hammer2_chain_assert_not_present(above, chain);
451 TAILQ_INSERT_HEAD(&above->layerq, layer, entry);
452 RB_INSERT(hammer2_chain_tree, &layer->rbtree, chain);
455 chain->above = above;
456 chain->inlayer = layer;
457 ++above->chain_count;
459 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
462 * We have to keep track of the effective live-view blockref count
463 * so the create code knows when to push an indirect block.
465 if ((flags & HAMMER2_CHAIN_INSERT_LIVE) &&
466 (chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
467 atomic_add_int(&above->live_count, 1);
470 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
471 spin_unlock(&above->cst.spin);
476 * Drop the caller's reference to the chain. When the ref count drops to
477 * zero this function will try to disassociate the chain from its parent and
478 * deallocate it, then recursely drop the parent using the implied ref
479 * from the chain's chain->parent.
481 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain,
482 struct h2_core_list *delayq);
485 hammer2_chain_drop(hammer2_chain_t *chain)
487 struct h2_core_list delayq;
488 hammer2_chain_t *scan;
492 if (hammer2_debug & 0x200000)
495 if (chain->flags & HAMMER2_CHAIN_MOVED)
497 if (chain->flags & HAMMER2_CHAIN_MODIFIED)
499 KKASSERT(chain->refs > need);
509 chain = hammer2_chain_lastdrop(chain, &delayq);
511 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
513 /* retry the same chain */
517 * When we've exhausted lastdrop chaining pull off of delayq.
518 * chains on delayq are dead but are used to placehold other
519 * chains which we added a ref to for the purpose of dropping.
522 hammer2_mount_t *hmp;
524 if ((scan = TAILQ_FIRST(&delayq)) != NULL) {
525 chain = (void *)scan->data;
526 TAILQ_REMOVE(&delayq, scan, core_entry);
527 scan->flags &= ~HAMMER2_CHAIN_ALLOCATED;
530 kfree(scan, hmp->mchain);
537 * Safe handling of the 1->0 transition on chain. Returns a chain for
538 * recursive drop or NULL, possibly returning the same chain if the atomic
541 * Whem two chains need to be recursively dropped we use the chain
542 * we would otherwise free to placehold the additional chain. It's a bit
543 * convoluted but we can't just recurse without potentially blowing out
546 * The chain cannot be freed if it has a non-empty core (children) or
547 * it is not at the head of ownerq.
549 * The cst spinlock is allowed nest child-to-parent (not parent-to-child).
553 hammer2_chain_lastdrop(hammer2_chain_t *chain, struct h2_core_list *delayq)
555 hammer2_pfsmount_t *pmp;
556 hammer2_mount_t *hmp;
557 hammer2_chain_core_t *above;
558 hammer2_chain_core_t *core;
559 hammer2_chain_layer_t *layer;
560 hammer2_chain_t *rdrop1;
561 hammer2_chain_t *rdrop2;
564 * Spinlock the core and check to see if it is empty. If it is
565 * not empty we leave chain intact with refs == 0. The elements
566 * in core->rbtree are associated with other chains contemporary
567 * with ours but not with our chain directly.
569 if ((core = chain->core) != NULL) {
570 spin_lock(&core->cst.spin);
573 * We can't free non-stale chains with children until we are
574 * able to free the children because there might be a flush
575 * dependency. Flushes of stale children (which should also
576 * have their deleted flag set) short-cut recursive flush
577 * dependencies and can be freed here. Any flushes which run
578 * through stale children due to the flush synchronization
579 * point should have the MOVED bit set in the chain and not
580 * reach lastdrop at this time.
582 * NOTE: We return (chain) on failure to retry.
584 if (core->chain_count &&
585 (chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0) {
586 if (atomic_cmpset_int(&chain->refs, 1, 0))
587 chain = NULL; /* success */
588 spin_unlock(&core->cst.spin);
591 /* no chains left under us */
594 * Various parts of the code might be holding a ref on a
595 * stale chain as a placemarker which must be iterated to
596 * locate a later non-stale (live) chain. We must be sure
597 * NOT to free the later non-stale chain (which might have
598 * no refs). Otherwise mass confusion may result.
600 * The DUPLICATED flag tells us whether the chain is stale
601 * or not, so the rule is that any chain whos DUPLICATED flag
602 * is NOT set must also be at the head of the ownerq.
604 * Note that the DELETED flag is not involved. That is, a
605 * live chain can represent a deletion that has not yet been
606 * flushed (or still has refs).
609 if (TAILQ_NEXT(chain, core_entry) == NULL &&
610 TAILQ_FIRST(&core->ownerq) != chain) {
612 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0 &&
613 TAILQ_FIRST(&core->ownerq) != chain) {
614 if (atomic_cmpset_int(&chain->refs, 1, 0))
615 chain = NULL; /* success */
616 spin_unlock(&core->cst.spin);
622 * chain->core has no children left so no accessors can get to our
623 * chain from there. Now we have to lock the above core to interlock
624 * remaining possible accessors that might bump chain's refs before
625 * we can safely drop chain's refs with intent to free the chain.
628 pmp = chain->pmp; /* can be NULL */
634 * Spinlock the parent and try to drop the last ref on chain.
635 * On success remove chain from its parent, otherwise return NULL.
637 * (normal core locks are top-down recursive but we define core
638 * spinlocks as bottom-up recursive, so this is safe).
640 if ((above = chain->above) != NULL) {
641 spin_lock(&above->cst.spin);
642 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
643 /* 1->0 transition failed */
644 spin_unlock(&above->cst.spin);
646 spin_unlock(&core->cst.spin);
647 return(chain); /* retry */
651 * 1->0 transition successful, remove chain from its
652 * above core. Track layer for removal/freeing.
654 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
655 layer = chain->inlayer;
656 RB_REMOVE(hammer2_chain_tree, &layer->rbtree, chain);
657 --above->chain_count;
658 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
660 chain->inlayer = NULL;
662 if (RB_EMPTY(&layer->rbtree) && layer->refs == 0) {
663 TAILQ_REMOVE(&above->layerq, layer, entry);
669 * If our chain was the last chain in the parent's core the
670 * core is now empty and its parents might now be droppable.
671 * Try to drop the first multi-homed parent by gaining a
672 * ref on it here and then dropping it below.
674 if (above->chain_count == 0) {
675 rdrop1 = TAILQ_FIRST(&above->ownerq);
677 atomic_cmpset_int(&rdrop1->refs, 0, 1) == 0) {
681 spin_unlock(&above->cst.spin);
682 above = NULL; /* safety */
686 * Successful 1->0 transition and the chain can be destroyed now.
688 * We still have the core spinlock (if core is non-NULL), and core's
689 * chain_count is 0. The above spinlock is gone.
691 * Remove chain from ownerq. Once core has no more owners (and no
692 * children which is already the case) we can destroy core.
694 * If core has more owners we may be able to continue a bottom-up
695 * drop with our next sibling.
700 TAILQ_REMOVE(&core->ownerq, chain, core_entry);
701 rdrop2 = TAILQ_FIRST(&core->ownerq);
702 if (rdrop2 && atomic_cmpset_int(&rdrop2->refs, 0, 1) == 0)
704 spin_unlock(&core->cst.spin);
707 * We can do the final 1->0 transition with an atomic op
708 * after releasing core's spinlock.
710 if (atomic_fetchadd_int(&core->sharecnt, -1) == 1) {
712 * On the 1->0 transition of core we can destroy
713 * it. Any remaining layers should no longer be
714 * referenced or visibile to other threads.
716 KKASSERT(TAILQ_EMPTY(&core->ownerq));
718 layer->good = 0xEF00;
719 kfree(layer, hmp->mchain);
721 while ((layer = TAILQ_FIRST(&core->layerq)) != NULL) {
722 KKASSERT(layer->refs == 0 &&
723 RB_EMPTY(&layer->rbtree));
724 TAILQ_REMOVE(&core->layerq, layer, entry);
725 layer->good = 0xEF01;
726 kfree(layer, hmp->mchain);
729 KKASSERT(core->cst.count == 0);
730 KKASSERT(core->cst.upgrade == 0);
732 kfree(core, hmp->mchain);
734 core = NULL; /* safety */
738 * All spin locks are gone, finish freeing stuff.
740 KKASSERT((chain->flags & (HAMMER2_CHAIN_MOVED |
741 HAMMER2_CHAIN_MODIFIED)) == 0);
742 hammer2_chain_drop_data(chain, 1);
744 KKASSERT(chain->dio == NULL);
747 * Free saved empty layer and return chained drop.
750 layer->good = 0xEF02;
751 kfree(layer, hmp->mchain);
755 * Once chain resources are gone we can use the now dead chain
756 * structure to placehold what might otherwise require a recursive
757 * drop, because we have potentially two things to drop and can only
758 * return one directly.
760 if (rdrop1 && rdrop2) {
761 KKASSERT(chain->flags & HAMMER2_CHAIN_ALLOCATED);
762 chain->data = (void *)rdrop1;
763 TAILQ_INSERT_TAIL(delayq, chain, core_entry);
765 } else if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
766 chain->flags &= ~HAMMER2_CHAIN_ALLOCATED;
768 kfree(chain, hmp->mchain);
772 * Either or both can be NULL. We already handled the case where
773 * both might not have been NULL.
782 * On either last lock release or last drop
785 hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop)
787 hammer2_mount_t *hmp = chain->hmp;
789 switch(chain->bref.type) {
790 case HAMMER2_BREF_TYPE_VOLUME:
791 case HAMMER2_BREF_TYPE_FREEMAP:
795 case HAMMER2_BREF_TYPE_INODE:
797 kfree(chain->data, hmp->mchain);
801 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
803 kfree(chain->data, hmp->mchain);
808 KKASSERT(chain->data == NULL);
814 * Ref and lock a chain element, acquiring its data with I/O if necessary,
815 * and specify how you would like the data to be resolved.
817 * Returns 0 on success or an error code if the data could not be acquired.
818 * The chain element is locked on return regardless of whether an error
821 * The lock is allowed to recurse, multiple locking ops will aggregate
822 * the requested resolve types. Once data is assigned it will not be
823 * removed until the last unlock.
825 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
826 * (typically used to avoid device/logical buffer
829 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
830 * the INITIAL-create state (indirect blocks only).
832 * Do not resolve data elements for DATA chains.
833 * (typically used to avoid device/logical buffer
836 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
838 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
839 * it will be locked exclusive.
841 * NOTE: Embedded elements (volume header, inodes) are always resolved
844 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
845 * element will instantiate and zero its buffer, and flush it on
848 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
849 * so as not to instantiate a device buffer, which could alias against
850 * a logical file buffer. However, if ALWAYS is specified the
851 * device buffer will be instantiated anyway.
853 * WARNING! If data must be fetched a shared lock will temporarily be
854 * upgraded to exclusive. However, a deadlock can occur if
855 * the caller owns more than one shared lock.
858 hammer2_chain_lock(hammer2_chain_t *chain, int how)
860 hammer2_mount_t *hmp;
861 hammer2_chain_core_t *core;
862 hammer2_blockref_t *bref;
868 * Ref and lock the element. Recursive locks are allowed.
870 if ((how & HAMMER2_RESOLVE_NOREF) == 0)
871 hammer2_chain_ref(chain);
872 atomic_add_int(&chain->lockcnt, 1);
875 KKASSERT(hmp != NULL);
878 * Get the appropriate lock.
881 if (how & HAMMER2_RESOLVE_SHARED)
882 ccms_thread_lock(&core->cst, CCMS_STATE_SHARED);
884 ccms_thread_lock(&core->cst, CCMS_STATE_EXCLUSIVE);
887 * If we already have a valid data pointer no further action is
894 * Do we have to resolve the data?
896 switch(how & HAMMER2_RESOLVE_MASK) {
897 case HAMMER2_RESOLVE_NEVER:
899 case HAMMER2_RESOLVE_MAYBE:
900 if (chain->flags & HAMMER2_CHAIN_INITIAL)
902 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
905 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
908 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
911 case HAMMER2_RESOLVE_ALWAYS:
916 * Upgrade to an exclusive lock so we can safely manipulate the
917 * buffer cache. If another thread got to it before us we
920 ostate = ccms_thread_lock_upgrade(&core->cst);
922 ccms_thread_lock_downgrade(&core->cst, ostate);
927 * We must resolve to a device buffer, either by issuing I/O or
928 * by creating a zero-fill element. We do not mark the buffer
929 * dirty when creating a zero-fill element (the hammer2_chain_modify()
930 * API must still be used to do that).
932 * The device buffer is variable-sized in powers of 2 down
933 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
934 * chunk always contains buffers of the same size. (XXX)
936 * The minimum physical IO size may be larger than the variable
942 * The getblk() optimization can only be used on newly created
943 * elements if the physical block size matches the request.
945 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
946 error = hammer2_io_new(hmp, bref->data_off, chain->bytes,
949 error = hammer2_io_bread(hmp, bref->data_off, chain->bytes,
951 adjreadcounter(&chain->bref, chain->bytes);
955 kprintf("hammer2_chain_lock: I/O error %016jx: %d\n",
956 (intmax_t)bref->data_off, error);
957 hammer2_io_bqrelse(&chain->dio);
958 ccms_thread_lock_downgrade(&core->cst, ostate);
963 * We can clear the INITIAL state now, we've resolved the buffer
964 * to zeros and marked it dirty with hammer2_io_new().
966 bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
967 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
968 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
972 * Setup the data pointer, either pointing it to an embedded data
973 * structure and copying the data from the buffer, or pointing it
976 * The buffer is not retained when copying to an embedded data
977 * structure in order to avoid potential deadlocks or recursions
978 * on the same physical buffer.
980 switch (bref->type) {
981 case HAMMER2_BREF_TYPE_VOLUME:
982 case HAMMER2_BREF_TYPE_FREEMAP:
984 * Copy data from bp to embedded buffer
986 panic("hammer2_chain_lock: called on unresolved volume header");
988 case HAMMER2_BREF_TYPE_INODE:
990 * Copy data from dio to embedded buffer, do not retain the
993 KKASSERT(chain->bytes == sizeof(chain->data->ipdata));
994 atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
995 chain->data = kmalloc(sizeof(chain->data->ipdata),
996 hmp->mchain, M_WAITOK | M_ZERO);
997 bcopy(bdata, &chain->data->ipdata, chain->bytes);
998 hammer2_io_bqrelse(&chain->dio);
1000 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1001 KKASSERT(chain->bytes == sizeof(chain->data->bmdata));
1002 atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
1003 chain->data = kmalloc(sizeof(chain->data->bmdata),
1004 hmp->mchain, M_WAITOK | M_ZERO);
1005 bcopy(bdata, &chain->data->bmdata, chain->bytes);
1006 hammer2_io_bqrelse(&chain->dio);
1008 case HAMMER2_BREF_TYPE_INDIRECT:
1009 case HAMMER2_BREF_TYPE_DATA:
1010 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1013 * Point data at the device buffer and leave bp intact.
1015 chain->data = (void *)bdata;
1018 ccms_thread_lock_downgrade(&core->cst, ostate);
1023 * This basically calls hammer2_io_breadcb() but does some pre-processing
1024 * of the chain first to handle certain cases.
1027 hammer2_chain_load_async(hammer2_chain_t *chain,
1028 void (*callback)(hammer2_io_t *dio,
1029 hammer2_chain_t *chain,
1030 void *arg_p, off_t arg_o),
1031 void *arg_p, off_t arg_o)
1033 hammer2_mount_t *hmp;
1034 struct hammer2_io *dio;
1035 hammer2_blockref_t *bref;
1039 callback(NULL, chain, arg_p, arg_o);
1044 * We must resolve to a device buffer, either by issuing I/O or
1045 * by creating a zero-fill element. We do not mark the buffer
1046 * dirty when creating a zero-fill element (the hammer2_chain_modify()
1047 * API must still be used to do that).
1049 * The device buffer is variable-sized in powers of 2 down
1050 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
1051 * chunk always contains buffers of the same size. (XXX)
1053 * The minimum physical IO size may be larger than the variable
1056 bref = &chain->bref;
1060 * The getblk() optimization can only be used on newly created
1061 * elements if the physical block size matches the request.
1063 if ((chain->flags & HAMMER2_CHAIN_INITIAL) &&
1064 chain->bytes == hammer2_devblksize(chain->bytes)) {
1065 error = hammer2_io_new(hmp, bref->data_off, chain->bytes, &dio);
1066 KKASSERT(error == 0);
1067 callback(dio, chain, arg_p, arg_o);
1072 * Otherwise issue a read
1074 adjreadcounter(&chain->bref, chain->bytes);
1075 hammer2_io_breadcb(hmp, bref->data_off, chain->bytes,
1076 callback, chain, arg_p, arg_o);
1080 * Unlock and deref a chain element.
1082 * On the last lock release any non-embedded data (chain->dio) will be
1086 hammer2_chain_unlock(hammer2_chain_t *chain)
1088 hammer2_chain_core_t *core = chain->core;
1089 ccms_state_t ostate;
1094 * The core->cst lock can be shared across several chains so we
1095 * need to track the per-chain lockcnt separately.
1097 * If multiple locks are present (or being attempted) on this
1098 * particular chain we can just unlock, drop refs, and return.
1100 * Otherwise fall-through on the 1->0 transition.
1103 lockcnt = chain->lockcnt;
1104 KKASSERT(lockcnt > 0);
1107 if (atomic_cmpset_int(&chain->lockcnt,
1108 lockcnt, lockcnt - 1)) {
1109 ccms_thread_unlock(&core->cst);
1110 hammer2_chain_drop(chain);
1114 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
1121 * On the 1->0 transition we upgrade the core lock (if necessary)
1122 * to exclusive for terminal processing. If after upgrading we find
1123 * that lockcnt is non-zero, another thread is racing us and will
1124 * handle the unload for us later on, so just cleanup and return
1125 * leaving the data/io intact
1127 * Otherwise if lockcnt is still 0 it is possible for it to become
1128 * non-zero and race, but since we hold the core->cst lock
1129 * exclusively all that will happen is that the chain will be
1130 * reloaded after we unload it.
1132 ostate = ccms_thread_lock_upgrade(&core->cst);
1133 if (chain->lockcnt) {
1134 ccms_thread_unlock_upgraded(&core->cst, ostate);
1135 hammer2_chain_drop(chain);
1140 * Shortcut the case if the data is embedded or not resolved.
1142 * Do NOT NULL out chain->data (e.g. inode data), it might be
1145 if (chain->dio == NULL) {
1146 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0)
1147 hammer2_chain_drop_data(chain, 0);
1148 ccms_thread_unlock_upgraded(&core->cst, ostate);
1149 hammer2_chain_drop(chain);
1156 if (hammer2_io_isdirty(chain->dio) == 0) {
1158 } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
1159 switch(chain->bref.type) {
1160 case HAMMER2_BREF_TYPE_DATA:
1161 counterp = &hammer2_ioa_file_write;
1163 case HAMMER2_BREF_TYPE_INODE:
1164 counterp = &hammer2_ioa_meta_write;
1166 case HAMMER2_BREF_TYPE_INDIRECT:
1167 counterp = &hammer2_ioa_indr_write;
1169 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1170 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1171 counterp = &hammer2_ioa_fmap_write;
1174 counterp = &hammer2_ioa_volu_write;
1177 *counterp += chain->bytes;
1179 switch(chain->bref.type) {
1180 case HAMMER2_BREF_TYPE_DATA:
1181 counterp = &hammer2_iod_file_write;
1183 case HAMMER2_BREF_TYPE_INODE:
1184 counterp = &hammer2_iod_meta_write;
1186 case HAMMER2_BREF_TYPE_INDIRECT:
1187 counterp = &hammer2_iod_indr_write;
1189 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1190 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1191 counterp = &hammer2_iod_fmap_write;
1194 counterp = &hammer2_iod_volu_write;
1197 *counterp += chain->bytes;
1201 * Clean out the dio.
1203 * If a device buffer was used for data be sure to destroy the
1204 * buffer when we are done to avoid aliases (XXX what about the
1205 * underlying VM pages?).
1207 * NOTE: Freemap leaf's use reserved blocks and thus no aliasing
1210 * NOTE: The isdirty check tracks whether we have to bdwrite() the
1211 * buffer or not. The buffer might already be dirty. The
1212 * flag is re-set when chain_modify() is called, even if
1213 * MODIFIED is already set, allowing the OS to retire the
1214 * buffer independent of a hammer2 flush.
1217 if ((chain->flags & HAMMER2_CHAIN_IOFLUSH) &&
1218 hammer2_io_isdirty(chain->dio)) {
1219 hammer2_io_bawrite(&chain->dio);
1221 hammer2_io_bqrelse(&chain->dio);
1223 ccms_thread_unlock_upgraded(&core->cst, ostate);
1224 hammer2_chain_drop(chain);
1228 * This counts the number of live blockrefs in a block array and
1229 * also calculates the point at which all remaining blockrefs are empty.
1230 * This routine can only be called on a live chain (DUPLICATED flag not set).
1232 * NOTE: Flag is not set until after the count is complete, allowing
1233 * callers to test the flag without holding the spinlock.
1235 * NOTE: If base is NULL the related chain is still in the INITIAL
1236 * state and there are no blockrefs to count.
1238 * NOTE: live_count may already have some counts accumulated due to
1239 * creation and deletion and could even be initially negative.
1242 hammer2_chain_countbrefs(hammer2_chain_t *chain,
1243 hammer2_blockref_t *base, int count)
1245 hammer2_chain_core_t *core = chain->core;
1247 KKASSERT((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0);
1249 spin_lock(&core->cst.spin);
1250 if ((core->flags & HAMMER2_CORE_COUNTEDBREFS) == 0) {
1252 while (--count >= 0) {
1253 if (base[count].type)
1256 core->live_zero = count + 1;
1257 while (count >= 0) {
1258 if (base[count].type)
1259 atomic_add_int(&core->live_count, 1);
1263 core->live_zero = 0;
1265 /* else do not modify live_count */
1266 atomic_set_int(&core->flags, HAMMER2_CORE_COUNTEDBREFS);
1268 spin_unlock(&core->cst.spin);
1272 * Resize the chain's physical storage allocation in-place. This may
1273 * replace the passed-in chain with a new chain.
1275 * Chains can be resized smaller without reallocating the storage.
1276 * Resizing larger will reallocate the storage.
1278 * Must be passed an exclusively locked parent and chain, returns a new
1279 * exclusively locked chain at the same index and unlocks the old chain.
1280 * Flushes the buffer if necessary.
1282 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
1283 * to avoid instantiating a device buffer that conflicts with the vnode
1284 * data buffer. That is, the passed-in bp is a logical buffer, whereas
1285 * any chain-oriented bp would be a device buffer.
1287 * XXX return error if cannot resize.
1290 hammer2_chain_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
1291 hammer2_chain_t *parent, hammer2_chain_t **chainp,
1292 int nradix, int flags)
1294 hammer2_mount_t *hmp;
1295 hammer2_chain_t *chain;
1303 * Only data and indirect blocks can be resized for now.
1304 * (The volu root, inodes, and freemap elements use a fixed size).
1306 KKASSERT(chain != &hmp->vchain);
1307 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1308 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
1311 * Nothing to do if the element is already the proper size
1313 obytes = chain->bytes;
1314 nbytes = 1U << nradix;
1315 if (obytes == nbytes)
1319 * Delete the old chain and duplicate it at the same (parent, index),
1320 * returning a new chain. This allows the old chain to still be
1321 * used by the flush code. The new chain will be returned in a
1324 * The parent does not have to be locked for the delete/duplicate call,
1325 * but is in this particular code path.
1327 * NOTE: If we are not crossing a synchronization point the
1328 * duplication code will simply reuse the existing chain
1331 hammer2_chain_delete_duplicate(trans, &chain, 0);
1334 * Relocate the block, even if making it smaller (because different
1335 * block sizes may be in different regions).
1337 hammer2_freemap_alloc(trans, chain, nbytes);
1338 chain->bytes = nbytes;
1339 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_FORCECOW);
1340 /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */
1343 * For now just support it on DATA chains (and not on indirect
1346 KKASSERT(chain->dio == NULL);
1350 * Make sure the chain is marked MOVED and propagate the update
1351 * to the root for flush.
1353 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
1354 hammer2_chain_ref(chain);
1355 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
1357 hammer2_chain_setsubmod(trans, chain);
1363 * Set a chain modified, making it read-write and duplicating it if necessary.
1364 * This function will assign a new physical block to the chain if necessary
1366 * Duplication of already-modified chains is possible when the modification
1367 * crosses a flush synchronization boundary.
1369 * Non-data blocks - The chain should be locked to at least the RESOLVE_MAYBE
1370 * level or the COW operation will not work.
1372 * Data blocks - The chain is usually locked RESOLVE_NEVER so as not to
1373 * run the data through the device buffers.
1375 * This function may return a different chain than was passed, in which case
1376 * the old chain will be unlocked and the new chain will be locked.
1378 * ip->chain may be adjusted by hammer2_chain_modify_ip().
1380 hammer2_inode_data_t *
1381 hammer2_chain_modify_ip(hammer2_trans_t *trans, hammer2_inode_t *ip,
1382 hammer2_chain_t **chainp, int flags)
1384 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1385 hammer2_chain_modify(trans, chainp, flags);
1386 if (ip->chain != *chainp)
1387 hammer2_inode_repoint(ip, NULL, *chainp);
1389 vsetisdirty(ip->vp);
1390 return(&ip->chain->data->ipdata);
1394 hammer2_chain_modify(hammer2_trans_t *trans, hammer2_chain_t **chainp,
1397 hammer2_mount_t *hmp;
1398 hammer2_chain_t *chain;
1407 KKASSERT(chain->bref.mirror_tid != trans->sync_tid ||
1408 (chain->flags & HAMMER2_CHAIN_MODIFIED));
1410 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP ||
1411 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1412 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1413 kprintf("trans %04jx/%08x MODIFY1 %p.%d [%08x] %016jx/%d %016jx C/D %016jx/%016jx\n",
1414 trans->sync_tid, trans->flags,
1415 chain, chain->bref.type, chain->flags,
1416 chain->bref.key, chain->bref.keybits,
1417 chain->bref.data_off,
1418 chain->modify_tid, chain->delete_tid);
1422 kprintf("MODIFY %p.%d flags %08x mod=%016jx del=%016jx\n", chain, chain->bref.type, chain->flags, chain->modify_tid, chain->delete_tid);
1425 * Data must be resolved if already assigned unless explicitly
1426 * flagged otherwise.
1428 if (chain->data == NULL && (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1429 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1430 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1431 hammer2_chain_unlock(chain);
1435 * data is not optional for freemap chains (we must always be sure
1436 * to copy the data on COW storage allocations).
1438 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1439 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1440 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1441 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1445 * Determine if a delete-duplicate is needed.
1447 * (a) Modify_tid is part of a prior flush
1448 * (b) Transaction is concurrent with a flush (has higher tid)
1449 * (c) and chain is not in the initial state (freshly created)
1450 * (d) and caller didn't request an in-place modification.
1452 * The freemap and volume header special chains are never D-Dd.
1454 if (chain->modify_tid != trans->sync_tid && /* cross boundary */
1455 (flags & HAMMER2_MODIFY_INPLACE) == 0) { /* from d-d */
1456 if (chain != &hmp->fchain && chain != &hmp->vchain) {
1457 KKASSERT((flags & HAMMER2_MODIFY_ASSERTNOCOPY) == 0);
1458 hammer2_chain_delete_duplicate(trans, chainp, 0);
1461 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP ||
1462 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1463 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1464 kprintf("trans %04jx/%08x MODIFY2 %p.%d [%08x] %016jx/%d %016jx\n",
1465 trans->sync_tid, trans->flags,
1466 chain, chain->bref.type, chain->flags,
1467 chain->bref.key, chain->bref.keybits,
1468 chain->bref.data_off);
1475 * Fall through if fchain or vchain, clearing the CHAIN_FLUSHED
1476 * flag. Basically other chains are delete-duplicated and so
1477 * the duplicated chains of course will not have the FLUSHED
1478 * flag set, but fchain and vchain are special-cased and the
1479 * flag must be cleared when changing modify_tid.
1481 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_FLUSHED);
1485 * Otherwise do initial-chain handling
1487 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1488 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1489 hammer2_chain_ref(chain);
1490 hammer2_chain_memory_inc(chain->pmp);
1493 /* shouldn't be needed */
1494 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
1495 hammer2_chain_ref(chain);
1496 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
1501 * The modification or re-modification requires an allocation and
1504 * We normally always allocate new storage here. If storage exists
1505 * and MODIFY_NOREALLOC is passed in, we do not allocate new storage.
1507 if (chain != &hmp->vchain && chain != &hmp->fchain) {
1508 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1509 ((flags & HAMMER2_MODIFY_NOREALLOC) == 0 &&
1510 chain->modify_tid != trans->sync_tid)
1512 hammer2_freemap_alloc(trans, chain, chain->bytes);
1513 /* XXX failed allocation */
1514 } else if (chain->flags & HAMMER2_CHAIN_FORCECOW) {
1515 hammer2_freemap_alloc(trans, chain, chain->bytes);
1516 /* XXX failed allocation */
1518 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_FORCECOW);
1522 * Update modify_tid. XXX special-case vchain/fchain because they
1523 * are always modified in-place. Otherwise the chain being modified
1524 * must not be part of a future transaction.
1526 if (chain == &hmp->vchain || chain == &hmp->fchain) {
1527 if (chain->modify_tid <= trans->sync_tid)
1528 chain->modify_tid = trans->sync_tid;
1530 KKASSERT(chain->modify_tid <= trans->sync_tid);
1531 chain->modify_tid = trans->sync_tid;
1534 if ((flags & HAMMER2_MODIFY_NO_MODIFY_TID) == 0)
1535 chain->bref.modify_tid = trans->sync_tid;
1538 * Do not COW if OPTDATA is set. INITIAL flag remains unchanged.
1539 * (OPTDATA does not prevent [re]allocation of storage, only the
1540 * related copy-on-write op).
1542 if (flags & HAMMER2_MODIFY_OPTDATA)
1546 * Clearing the INITIAL flag (for indirect blocks) indicates that
1547 * we've processed the uninitialized storage allocation.
1549 * If this flag is already clear we are likely in a copy-on-write
1550 * situation but we have to be sure NOT to bzero the storage if
1551 * no data is present.
1553 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1554 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1561 * Instantiate data buffer and possibly execute COW operation
1563 switch(chain->bref.type) {
1564 case HAMMER2_BREF_TYPE_VOLUME:
1565 case HAMMER2_BREF_TYPE_FREEMAP:
1566 case HAMMER2_BREF_TYPE_INODE:
1567 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1569 * The data is embedded, no copy-on-write operation is
1572 KKASSERT(chain->dio == NULL);
1574 case HAMMER2_BREF_TYPE_DATA:
1575 case HAMMER2_BREF_TYPE_INDIRECT:
1576 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1578 * Perform the copy-on-write operation
1580 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1583 error = hammer2_io_new(hmp, chain->bref.data_off,
1584 chain->bytes, &dio);
1586 error = hammer2_io_bread(hmp, chain->bref.data_off,
1587 chain->bytes, &dio);
1589 adjreadcounter(&chain->bref, chain->bytes);
1590 KKASSERT(error == 0);
1592 bdata = hammer2_io_data(dio, chain->bref.data_off);
1595 * Copy or zero-fill on write depending on whether
1596 * chain->data exists or not and set the dirty state for
1597 * the new buffer. Retire the existing buffer.
1600 KKASSERT(chain->dio != NULL);
1601 if (chain->data != (void *)bdata) {
1602 bcopy(chain->data, bdata, chain->bytes);
1604 } else if (wasinitial == 0) {
1606 * We have a problem. We were asked to COW but
1607 * we don't have any data to COW with!
1609 panic("hammer2_chain_modify: having a COW %p\n",
1612 hammer2_io_brelse(&chain->dio);
1613 chain->data = (void *)bdata;
1615 hammer2_io_setdirty(dio); /* modified by bcopy above */
1618 panic("hammer2_chain_modify: illegal non-embedded type %d",
1625 kprintf("RET2 %p.%d flags %08x mod=%016jx del=%016jx\n", chain, chain->bref.type, chain->flags, chain->modify_tid, chain->delete_tid);
1627 hammer2_chain_setsubmod(trans, chain);
1631 * Mark the volume as having been modified. This short-cut version
1632 * does not have to lock the volume's chain, which allows the ioctl
1633 * code to make adjustments to connections without deadlocking. XXX
1635 * No ref is made on vchain when flagging it MODIFIED.
1638 hammer2_modify_volume(hammer2_mount_t *hmp)
1640 hammer2_voldata_lock(hmp);
1641 hammer2_voldata_unlock(hmp, 1);
1645 * This function returns the chain at the nearest key within the specified
1646 * range with the highest delete_tid. The core spinlock must be held on
1647 * call and the returned chain will be referenced but not locked.
1649 * The returned chain may or may not be in a deleted state. Note that
1650 * live chains have a delete_tid = MAX_TID.
1652 * This function will recurse through chain->rbtree as necessary and will
1653 * return a *key_nextp suitable for iteration. *key_nextp is only set if
1654 * the iteration value is less than the current value of *key_nextp.
1656 * The caller should use (*key_nextp) to calculate the actual range of
1657 * the returned element, which will be (key_beg to *key_nextp - 1), because
1658 * there might be another element which is superior to the returned element
1661 * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
1662 * chains continue to be returned. On EOF (*key_nextp) may overflow since
1663 * it will wind up being (key_end + 1).
1665 struct hammer2_chain_find_info {
1666 hammer2_chain_t *best;
1667 hammer2_key_t key_beg;
1668 hammer2_key_t key_end;
1669 hammer2_key_t key_next;
1672 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
1673 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
1676 * DEBUGGING - Assert that the chain will not collide.
1680 hammer2_chain_assert_not_present(hammer2_chain_core_t *core,
1681 hammer2_chain_t *chain)
1683 struct hammer2_chain_find_info info;
1684 hammer2_chain_layer_t *layer;
1686 if (chain->flags & HAMMER2_CHAIN_DELETED)
1690 info.key_beg = chain->bref.key;
1691 info.key_end = chain->bref.key +
1692 ((hammer2_key_t)1 << chain->bref.keybits) - 1;
1693 info.key_next = HAMMER2_MAX_KEY;
1695 TAILQ_FOREACH(layer, &core->layerq, entry) {
1696 KKASSERT(layer->good == 0xABCD);
1697 RB_SCAN(hammer2_chain_tree, &layer->rbtree,
1698 hammer2_chain_find_cmp, hammer2_chain_find_callback,
1701 if (info.best && (info.best->flags & HAMMER2_CHAIN_DELETED) == 0)
1702 panic("hammer2_chain_assert_not_present: %p/%p\n",
1708 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
1709 hammer2_key_t key_beg, hammer2_key_t key_end)
1711 struct hammer2_chain_find_info info;
1712 hammer2_chain_layer_t *layer;
1715 info.key_beg = key_beg;
1716 info.key_end = key_end;
1717 info.key_next = *key_nextp;
1719 KKASSERT(parent->core->good == 0x1234);
1720 TAILQ_FOREACH(layer, &parent->core->layerq, entry) {
1721 KKASSERT(layer->good == 0xABCD);
1722 RB_SCAN(hammer2_chain_tree, &layer->rbtree,
1723 hammer2_chain_find_cmp, hammer2_chain_find_callback,
1726 *key_nextp = info.key_next;
1728 kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
1729 parent, key_beg, key_end, *key_nextp);
1737 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
1739 struct hammer2_chain_find_info *info = data;
1740 hammer2_key_t child_beg;
1741 hammer2_key_t child_end;
1743 child_beg = child->bref.key;
1744 child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
1746 if (child_end < info->key_beg)
1748 if (child_beg > info->key_end)
1755 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
1757 struct hammer2_chain_find_info *info = data;
1758 hammer2_chain_t *best;
1759 hammer2_key_t child_end;
1762 * WARNING! Do not discard DUPLICATED chains, it is possible that
1763 * we are catching an insertion half-way done. If a
1764 * duplicated chain turns out to be the best choice the
1765 * caller will re-check its flags after locking it.
1767 * WARNING! Layerq is scanned forwards, exact matches should keep
1768 * the existing info->best.
1770 if ((best = info->best) == NULL) {
1772 * No previous best. Assign best
1775 } else if (best->bref.key <= info->key_beg &&
1776 child->bref.key <= info->key_beg) {
1778 * If our current best is flush with key_beg and child is
1779 * also flush with key_beg choose based on delete_tid.
1781 * key_next will automatically be limited to the smaller of
1782 * the two end-points.
1784 if (child->delete_tid > best->delete_tid)
1786 } else if (child->bref.key < best->bref.key) {
1788 * Child has a nearer key and best is not flush with key_beg.
1789 * Truncate key_next to the old best key iff it had a better
1793 if (best->delete_tid >= child->delete_tid &&
1794 (info->key_next > best->bref.key || info->key_next == 0))
1795 info->key_next = best->bref.key;
1796 } else if (child->bref.key == best->bref.key) {
1798 * If our current best is flush with the child then choose
1799 * based on delete_tid.
1801 * key_next will automatically be limited to the smaller of
1802 * the two end-points.
1804 if (child->delete_tid > best->delete_tid)
1808 * Keep the current best but truncate key_next to the child's
1809 * base iff the child has a higher delete_tid.
1811 * key_next will also automatically be limited to the smaller
1812 * of the two end-points (probably not necessary for this case
1813 * but we do it anyway).
1815 if (child->delete_tid >= best->delete_tid &&
1816 (info->key_next > child->bref.key || info->key_next == 0))
1817 info->key_next = child->bref.key;
1821 * Always truncate key_next based on child's end-of-range.
1823 child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
1824 if (child_end && (info->key_next > child_end || info->key_next == 0))
1825 info->key_next = child_end;
1831 * Retrieve the specified chain from a media blockref, creating the
1832 * in-memory chain structure which reflects it. modify_tid will be
1833 * left 0 which forces any modifications to issue a delete-duplicate.
1835 * To handle insertion races pass the INSERT_RACE flag along with the
1836 * generation number of the core. NULL will be returned if the generation
1837 * number changes before we have a chance to insert the chain. Insert
1838 * races can occur because the parent might be held shared.
1840 * Caller must hold the parent locked shared or exclusive since we may
1841 * need the parent's bref array to find our block.
1844 hammer2_chain_get(hammer2_chain_t *parent, hammer2_blockref_t *bref,
1847 hammer2_mount_t *hmp = parent->hmp;
1848 hammer2_chain_core_t *above = parent->core;
1849 hammer2_chain_t *chain;
1853 * Allocate a chain structure representing the existing media
1854 * entry. Resulting chain has one ref and is not locked.
1856 chain = hammer2_chain_alloc(hmp, parent->pmp, NULL, bref);
1857 hammer2_chain_core_alloc(NULL, chain, NULL);
1858 /* ref'd chain returned */
1859 chain->modify_tid = chain->bref.mirror_tid;
1862 * Link the chain into its parent. A spinlock is required to safely
1863 * access the RBTREE, and it is possible to collide with another
1864 * hammer2_chain_get() operation because the caller might only hold
1865 * a shared lock on the parent.
1867 KKASSERT(parent->refs > 0);
1868 error = hammer2_chain_insert(above, NULL, chain,
1869 HAMMER2_CHAIN_INSERT_SPIN |
1870 HAMMER2_CHAIN_INSERT_RACE,
1873 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
1874 kprintf("chain %p get race\n", chain);
1875 hammer2_chain_drop(chain);
1878 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
1882 * Return our new chain referenced but not locked, or NULL if
1889 * Lookup initialization/completion API
1892 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
1894 if (flags & HAMMER2_LOOKUP_SHARED) {
1895 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1896 HAMMER2_RESOLVE_SHARED);
1898 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1904 hammer2_chain_lookup_done(hammer2_chain_t *parent)
1907 hammer2_chain_unlock(parent);
1912 hammer2_chain_getparent(hammer2_chain_t **parentp, int how)
1914 hammer2_chain_t *oparent;
1915 hammer2_chain_t *bparent;
1916 hammer2_chain_t *nparent;
1917 hammer2_chain_core_t *above;
1920 above = oparent->above;
1922 spin_lock(&above->cst.spin);
1923 bparent = TAILQ_FIRST(&above->ownerq);
1924 hammer2_chain_ref(bparent);
1927 * Be careful of order, oparent must be unlocked before nparent
1928 * is locked below to avoid a deadlock. We might as well delay its
1929 * unlocking until we conveniently no longer have the spinlock (instead
1930 * of cycling the spinlock).
1932 * Theoretically our ref on bparent should prevent elements of the
1933 * following chain from going away and prevent above from going away,
1934 * but we still need the spinlock to safely scan the list.
1938 while (nparent->flags & HAMMER2_CHAIN_DUPLICATED)
1939 nparent = TAILQ_NEXT(nparent, core_entry);
1940 hammer2_chain_ref(nparent);
1941 spin_unlock(&above->cst.spin);
1944 hammer2_chain_unlock(oparent);
1947 hammer2_chain_lock(nparent, how | HAMMER2_RESOLVE_NOREF);
1948 hammer2_chain_drop(bparent);
1951 * We might have raced a delete-duplicate.
1953 if ((nparent->flags & HAMMER2_CHAIN_DUPLICATED) == 0)
1956 hammer2_chain_ref(bparent);
1957 hammer2_chain_unlock(nparent);
1958 spin_lock(&above->cst.spin);
1967 * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
1968 * (*parentp) typically points to an inode but can also point to a related
1969 * indirect block and this function will recurse upwards and find the inode
1972 * (*parentp) must be exclusively locked and referenced and can be an inode
1973 * or an existing indirect block within the inode.
1975 * On return (*parentp) will be modified to point at the deepest parent chain
1976 * element encountered during the search, as a helper for an insertion or
1977 * deletion. The new (*parentp) will be locked and referenced and the old
1978 * will be unlocked and dereferenced (no change if they are both the same).
1980 * The matching chain will be returned exclusively locked. If NOLOCK is
1981 * requested the chain will be returned only referenced.
1983 * NULL is returned if no match was found, but (*parentp) will still
1984 * potentially be adjusted.
1986 * On return (*key_nextp) will point to an iterative value for key_beg.
1987 * (If NULL is returned (*key_nextp) is set to key_end).
1989 * This function will also recurse up the chain if the key is not within the
1990 * current parent's range. (*parentp) can never be set to NULL. An iteration
1991 * can simply allow (*parentp) to float inside the loop.
1993 * NOTE! chain->data is not always resolved. By default it will not be
1994 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
1995 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
1996 * BREF_TYPE_DATA as the device buffer can alias the logical file
2000 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
2001 hammer2_key_t key_beg, hammer2_key_t key_end,
2002 int *cache_indexp, int flags)
2004 hammer2_mount_t *hmp;
2005 hammer2_chain_t *parent;
2006 hammer2_chain_t *chain;
2007 hammer2_blockref_t *base;
2008 hammer2_blockref_t *bref;
2009 hammer2_blockref_t bcopy;
2010 hammer2_key_t scan_beg;
2011 hammer2_key_t scan_end;
2012 hammer2_chain_core_t *above;
2014 int how_always = HAMMER2_RESOLVE_ALWAYS;
2015 int how_maybe = HAMMER2_RESOLVE_MAYBE;
2018 int maxloops = 300000;
2020 hammer2_chain_t * volatile xxchain = NULL;
2021 volatile int xxchainwhy;
2023 if (flags & HAMMER2_LOOKUP_ALWAYS) {
2024 how_maybe = how_always;
2025 how = HAMMER2_RESOLVE_ALWAYS;
2026 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2027 how = HAMMER2_RESOLVE_NEVER;
2029 how = HAMMER2_RESOLVE_MAYBE;
2031 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK)) {
2032 how_maybe |= HAMMER2_RESOLVE_SHARED;
2033 how_always |= HAMMER2_RESOLVE_SHARED;
2034 how |= HAMMER2_RESOLVE_SHARED;
2038 * Recurse (*parentp) upward if necessary until the parent completely
2039 * encloses the key range or we hit the inode.
2041 * This function handles races against the flusher doing a delete-
2042 * duplicate above us and re-homes the parent to the duplicate in
2043 * that case, otherwise we'd wind up recursing down a stale chain.
2048 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2049 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2050 scan_beg = parent->bref.key;
2051 scan_end = scan_beg +
2052 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2053 if (key_beg >= scan_beg && key_end <= scan_end)
2055 parent = hammer2_chain_getparent(parentp, how_maybe);
2059 if (--maxloops == 0)
2060 panic("hammer2_chain_lookup: maxloops");
2062 * Locate the blockref array. Currently we do a fully associative
2063 * search through the array.
2065 switch(parent->bref.type) {
2066 case HAMMER2_BREF_TYPE_INODE:
2068 * Special shortcut for embedded data returns the inode
2069 * itself. Callers must detect this condition and access
2070 * the embedded data (the strategy code does this for us).
2072 * This is only applicable to regular files and softlinks.
2074 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
2075 if (flags & HAMMER2_LOOKUP_NOLOCK)
2076 hammer2_chain_ref(parent);
2078 hammer2_chain_lock(parent, how_always);
2079 *key_nextp = key_end + 1;
2082 base = &parent->data->ipdata.u.blockset.blockref[0];
2083 count = HAMMER2_SET_COUNT;
2085 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2086 case HAMMER2_BREF_TYPE_INDIRECT:
2088 * Handle MATCHIND on the parent
2090 if (flags & HAMMER2_LOOKUP_MATCHIND) {
2091 scan_beg = parent->bref.key;
2092 scan_end = scan_beg +
2093 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2094 if (key_beg == scan_beg && key_end == scan_end) {
2096 hammer2_chain_lock(chain, how_maybe);
2097 *key_nextp = scan_end + 1;
2102 * Optimize indirect blocks in the INITIAL state to avoid
2105 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2108 if (parent->data == NULL)
2109 panic("parent->data is NULL");
2110 base = &parent->data->npdata[0];
2112 count = parent->bytes / sizeof(hammer2_blockref_t);
2114 case HAMMER2_BREF_TYPE_VOLUME:
2115 base = &hmp->voldata.sroot_blockset.blockref[0];
2116 count = HAMMER2_SET_COUNT;
2118 case HAMMER2_BREF_TYPE_FREEMAP:
2119 base = &hmp->voldata.freemap_blockset.blockref[0];
2120 count = HAMMER2_SET_COUNT;
2123 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2125 base = NULL; /* safety */
2126 count = 0; /* safety */
2130 * Merged scan to find next candidate.
2132 * hammer2_base_*() functions require the above->live_* fields
2133 * to be synchronized.
2135 * We need to hold the spinlock to access the block array and RB tree
2136 * and to interlock chain creation.
2138 above = parent->core;
2139 if ((parent->core->flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
2140 hammer2_chain_countbrefs(parent, base, count);
2145 spin_lock(&above->cst.spin);
2146 chain = hammer2_combined_find(parent, base, count,
2147 cache_indexp, key_nextp,
2148 key_beg, key_end, &bref);
2149 generation = above->generation;
2152 * Exhausted parent chain, iterate.
2155 spin_unlock(&above->cst.spin);
2156 if (key_beg == key_end) /* short cut single-key case */
2158 return (hammer2_chain_next(parentp, NULL, key_nextp,
2160 cache_indexp, flags));
2164 * Selected from blockref or in-memory chain.
2166 if (chain == NULL) {
2168 spin_unlock(&above->cst.spin);
2169 chain = hammer2_chain_get(parent, &bcopy, generation);
2170 if (chain == NULL) {
2171 kprintf("retry lookup parent %p keys %016jx:%016jx\n",
2172 parent, key_beg, key_end);
2175 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2178 hammer2_chain_drop(chain);
2183 hammer2_chain_ref(chain);
2184 wasdup = ((chain->flags & HAMMER2_CHAIN_DUPLICATED) != 0);
2185 spin_unlock(&above->cst.spin);
2189 * chain is referenced but not locked. We must lock the chain
2190 * to obtain definitive DUPLICATED/DELETED state
2192 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2193 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2194 hammer2_chain_lock(chain, how_maybe | HAMMER2_RESOLVE_NOREF);
2196 hammer2_chain_lock(chain, how | HAMMER2_RESOLVE_NOREF);
2200 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2202 * NOTE: Chain's key range is not relevant as there might be
2203 * one-offs within the range that are not deleted.
2205 * NOTE: Lookups can race delete-duplicate because
2206 * delete-duplicate does not lock the parent's core
2207 * (they just use the spinlock on the core). We must
2208 * check for races by comparing the DUPLICATED flag before
2209 * releasing the spinlock with the flag after locking the
2212 if (chain->flags & HAMMER2_CHAIN_DELETED) {
2213 hammer2_chain_unlock(chain);
2214 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0 || wasdup) {
2215 key_beg = *key_nextp;
2216 if (key_beg == 0 || key_beg > key_end)
2225 * If the chain element is an indirect block it becomes the new
2226 * parent and we loop on it. We must maintain our top-down locks
2227 * to prevent the flusher from interfering (i.e. doing a
2228 * delete-duplicate and leaving us recursing down a deleted chain).
2230 * The parent always has to be locked with at least RESOLVE_MAYBE
2231 * so we can access its data. It might need a fixup if the caller
2232 * passed incompatible flags. Be careful not to cause a deadlock
2233 * as a data-load requires an exclusive lock.
2235 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
2236 * range is within the requested key range we return the indirect
2237 * block and do NOT loop. This is usually only used to acquire
2240 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2241 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2242 hammer2_chain_unlock(parent);
2243 *parentp = parent = chain;
2250 * All done, return the chain
2253 XXChainWhy = xxchainwhy;
2258 * After having issued a lookup we can iterate all matching keys.
2260 * If chain is non-NULL we continue the iteration from just after it's index.
2262 * If chain is NULL we assume the parent was exhausted and continue the
2263 * iteration at the next parent.
2265 * parent must be locked on entry and remains locked throughout. chain's
2266 * lock status must match flags. Chain is always at least referenced.
2268 * WARNING! The MATCHIND flag does not apply to this function.
2271 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
2272 hammer2_key_t *key_nextp,
2273 hammer2_key_t key_beg, hammer2_key_t key_end,
2274 int *cache_indexp, int flags)
2276 hammer2_chain_t *parent;
2280 * Calculate locking flags for upward recursion.
2282 how_maybe = HAMMER2_RESOLVE_MAYBE;
2283 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK))
2284 how_maybe |= HAMMER2_RESOLVE_SHARED;
2289 * Calculate the next index and recalculate the parent if necessary.
2292 key_beg = chain->bref.key +
2293 ((hammer2_key_t)1 << chain->bref.keybits);
2294 if (flags & HAMMER2_LOOKUP_NOLOCK)
2295 hammer2_chain_drop(chain);
2297 hammer2_chain_unlock(chain);
2300 * Any scan where the lookup returned degenerate data embedded
2301 * in the inode has an invalid index and must terminate.
2303 if (chain == parent)
2305 if (key_beg == 0 || key_beg > key_end)
2308 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
2309 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2311 * We reached the end of the iteration.
2316 * Continue iteration with next parent unless the current
2317 * parent covers the range.
2319 key_beg = parent->bref.key +
2320 ((hammer2_key_t)1 << parent->bref.keybits);
2321 if (key_beg == 0 || key_beg > key_end)
2323 parent = hammer2_chain_getparent(parentp, how_maybe);
2329 return (hammer2_chain_lookup(parentp, key_nextp,
2331 cache_indexp, flags));
2335 * Raw scan functions are similar to lookup/next but do not seek the parent
2336 * chain and do not skip stale chains. These functions are primarily used
2337 * by the recovery code.
2339 * Parent and chain are locked, parent's data must be resolved. To acquire
2340 * the first sub-chain under parent pass chain == NULL.
2343 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t *chain,
2344 int *cache_indexp, int flags)
2346 hammer2_mount_t *hmp;
2347 hammer2_blockref_t *base;
2348 hammer2_blockref_t *bref;
2349 hammer2_blockref_t bcopy;
2350 hammer2_chain_core_t *above;
2352 hammer2_key_t next_key;
2354 int how_always = HAMMER2_RESOLVE_ALWAYS;
2355 int how_maybe = HAMMER2_RESOLVE_MAYBE;
2358 int maxloops = 300000;
2364 * Scan flags borrowed from lookup
2366 if (flags & HAMMER2_LOOKUP_ALWAYS) {
2367 how_maybe = how_always;
2368 how = HAMMER2_RESOLVE_ALWAYS;
2369 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2370 how = HAMMER2_RESOLVE_NEVER;
2372 how = HAMMER2_RESOLVE_MAYBE;
2374 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK)) {
2375 how_maybe |= HAMMER2_RESOLVE_SHARED;
2376 how_always |= HAMMER2_RESOLVE_SHARED;
2377 how |= HAMMER2_RESOLVE_SHARED;
2381 * Calculate key to locate first/next element, unlocking the previous
2382 * element as we go. Be careful, the key calculation can overflow.
2385 key = chain->bref.key +
2386 ((hammer2_key_t)1 << chain->bref.keybits);
2387 hammer2_chain_unlock(chain);
2396 if (--maxloops == 0)
2397 panic("hammer2_chain_scan: maxloops");
2399 * Locate the blockref array. Currently we do a fully associative
2400 * search through the array.
2402 switch(parent->bref.type) {
2403 case HAMMER2_BREF_TYPE_INODE:
2405 * An inode with embedded data has no sub-chains.
2407 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA)
2409 base = &parent->data->ipdata.u.blockset.blockref[0];
2410 count = HAMMER2_SET_COUNT;
2412 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2413 case HAMMER2_BREF_TYPE_INDIRECT:
2415 * Optimize indirect blocks in the INITIAL state to avoid
2418 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2421 if (parent->data == NULL)
2422 panic("parent->data is NULL");
2423 base = &parent->data->npdata[0];
2425 count = parent->bytes / sizeof(hammer2_blockref_t);
2427 case HAMMER2_BREF_TYPE_VOLUME:
2428 base = &hmp->voldata.sroot_blockset.blockref[0];
2429 count = HAMMER2_SET_COUNT;
2431 case HAMMER2_BREF_TYPE_FREEMAP:
2432 base = &hmp->voldata.freemap_blockset.blockref[0];
2433 count = HAMMER2_SET_COUNT;
2436 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2438 base = NULL; /* safety */
2439 count = 0; /* safety */
2443 * Merged scan to find next candidate.
2445 * hammer2_base_*() functions require the above->live_* fields
2446 * to be synchronized.
2448 * We need to hold the spinlock to access the block array and RB tree
2449 * and to interlock chain creation.
2451 if ((parent->core->flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
2452 hammer2_chain_countbrefs(parent, base, count);
2454 above = parent->core;
2456 spin_lock(&above->cst.spin);
2457 chain = hammer2_combined_find(parent, base, count,
2458 cache_indexp, &next_key,
2459 key, HAMMER2_MAX_KEY, &bref);
2460 generation = above->generation;
2463 * Exhausted parent chain, we're done.
2466 spin_unlock(&above->cst.spin);
2467 KKASSERT(chain == NULL);
2472 * Selected from blockref or in-memory chain.
2474 if (chain == NULL) {
2476 spin_unlock(&above->cst.spin);
2477 chain = hammer2_chain_get(parent, &bcopy, generation);
2478 if (chain == NULL) {
2479 kprintf("retry scan parent %p keys %016jx\n",
2483 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2484 hammer2_chain_drop(chain);
2490 hammer2_chain_ref(chain);
2491 wasdup = ((chain->flags & HAMMER2_CHAIN_DUPLICATED) != 0);
2492 spin_unlock(&above->cst.spin);
2496 * chain is referenced but not locked. We must lock the chain
2497 * to obtain definitive DUPLICATED/DELETED state
2499 hammer2_chain_lock(chain, how | HAMMER2_RESOLVE_NOREF);
2502 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2504 * NOTE: chain's key range is not relevant as there might be
2505 * one-offs within the range that are not deleted.
2507 * NOTE: XXX this could create problems with scans used in
2508 * situations other than mount-time recovery.
2510 * NOTE: Lookups can race delete-duplicate because
2511 * delete-duplicate does not lock the parent's core
2512 * (they just use the spinlock on the core). We must
2513 * check for races by comparing the DUPLICATED flag before
2514 * releasing the spinlock with the flag after locking the
2517 if (chain->flags & HAMMER2_CHAIN_DELETED) {
2518 hammer2_chain_unlock(chain);
2521 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0 || wasdup) {
2531 * All done, return the chain or NULL
2537 * Create and return a new hammer2 system memory structure of the specified
2538 * key, type and size and insert it under (*parentp). This is a full
2539 * insertion, based on the supplied key/keybits, and may involve creating
2540 * indirect blocks and moving other chains around via delete/duplicate.
2542 * (*parentp) must be exclusive locked and may be replaced on return
2543 * depending on how much work the function had to do.
2545 * (*chainp) usually starts out NULL and returns the newly created chain,
2546 * but if the caller desires the caller may allocate a disconnected chain
2547 * and pass it in instead. (It is also possible for the caller to use
2548 * chain_duplicate() to create a disconnected chain, manipulate it, then
2549 * pass it into this function to insert it).
2551 * This function should NOT be used to insert INDIRECT blocks. It is
2552 * typically used to create/insert inodes and data blocks.
2554 * Caller must pass-in an exclusively locked parent the new chain is to
2555 * be inserted under, and optionally pass-in a disconnected, exclusively
2556 * locked chain to insert (else we create a new chain). The function will
2557 * adjust (*parentp) as necessary, create or connect the chain, and
2558 * return an exclusively locked chain in *chainp.
2561 hammer2_chain_create(hammer2_trans_t *trans, hammer2_chain_t **parentp,
2562 hammer2_chain_t **chainp,
2563 hammer2_key_t key, int keybits, int type, size_t bytes)
2565 hammer2_mount_t *hmp;
2566 hammer2_chain_t *chain;
2567 hammer2_chain_t *parent = *parentp;
2568 hammer2_chain_core_t *above;
2569 hammer2_blockref_t *base;
2570 hammer2_blockref_t dummy;
2574 int maxloops = 300000;
2576 above = parent->core;
2577 KKASSERT(ccms_thread_lock_owned(&above->cst));
2581 if (chain == NULL) {
2583 * First allocate media space and construct the dummy bref,
2584 * then allocate the in-memory chain structure. Set the
2585 * INITIAL flag for fresh chains which do not have embedded
2588 bzero(&dummy, sizeof(dummy));
2591 dummy.keybits = keybits;
2592 dummy.data_off = hammer2_getradix(bytes);
2593 dummy.methods = parent->bref.methods;
2594 chain = hammer2_chain_alloc(hmp, parent->pmp, trans, &dummy);
2595 hammer2_chain_core_alloc(trans, chain, NULL);
2598 * Lock the chain manually, chain_lock will load the chain
2599 * which we do NOT want to do. (note: chain->refs is set
2600 * to 1 by chain_alloc() for us, but lockcnt is not).
2603 ccms_thread_lock(&chain->core->cst, CCMS_STATE_EXCLUSIVE);
2607 * We do NOT set INITIAL here (yet). INITIAL is only
2608 * used for indirect blocks.
2610 * Recalculate bytes to reflect the actual media block
2613 bytes = (hammer2_off_t)1 <<
2614 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
2615 chain->bytes = bytes;
2618 case HAMMER2_BREF_TYPE_VOLUME:
2619 case HAMMER2_BREF_TYPE_FREEMAP:
2620 panic("hammer2_chain_create: called with volume type");
2622 case HAMMER2_BREF_TYPE_INODE:
2623 KKASSERT(bytes == HAMMER2_INODE_BYTES);
2624 atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
2625 chain->data = kmalloc(sizeof(chain->data->ipdata),
2626 hmp->mchain, M_WAITOK | M_ZERO);
2628 case HAMMER2_BREF_TYPE_INDIRECT:
2629 panic("hammer2_chain_create: cannot be used to"
2630 "create indirect block");
2632 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2633 panic("hammer2_chain_create: cannot be used to"
2634 "create freemap root or node");
2636 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2637 KKASSERT(bytes == sizeof(chain->data->bmdata));
2638 atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
2639 chain->data = kmalloc(sizeof(chain->data->bmdata),
2640 hmp->mchain, M_WAITOK | M_ZERO);
2642 case HAMMER2_BREF_TYPE_DATA:
2645 * leave chain->data NULL, set INITIAL
2647 KKASSERT(chain->data == NULL);
2648 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
2653 * We are reattaching a chain that has been duplicated and
2654 * left disconnected under a DIFFERENT parent with potentially
2655 * different key/keybits.
2657 * The chain must be modified in the current transaction
2658 * (the duplication code should have done that for us),
2659 * and it's modify_tid should be greater than the parent's
2660 * bref.mirror_tid. This should cause it to be created under
2663 * If deleted in the same transaction, the create/delete TIDs
2664 * will be the same and effective the chain will not have
2665 * existed at all from the point of view of the parent.
2667 * Do NOT mess with the current state of the INITIAL flag.
2669 KKASSERT(chain->modify_tid > parent->bref.mirror_tid);
2670 KKASSERT(chain->modify_tid == trans->sync_tid);
2671 chain->bref.key = key;
2672 chain->bref.keybits = keybits;
2673 /* chain->modify_tid = chain->bref.mirror_tid; */
2674 KKASSERT(chain->above == NULL);
2678 * Calculate how many entries we have in the blockref array and
2679 * determine if an indirect block is required.
2682 if (--maxloops == 0)
2683 panic("hammer2_chain_create: maxloops");
2684 above = parent->core;
2686 switch(parent->bref.type) {
2687 case HAMMER2_BREF_TYPE_INODE:
2688 KKASSERT((parent->data->ipdata.op_flags &
2689 HAMMER2_OPFLAG_DIRECTDATA) == 0);
2690 KKASSERT(parent->data != NULL);
2691 base = &parent->data->ipdata.u.blockset.blockref[0];
2692 count = HAMMER2_SET_COUNT;
2694 case HAMMER2_BREF_TYPE_INDIRECT:
2695 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2696 if (parent->flags & HAMMER2_CHAIN_INITIAL)
2699 base = &parent->data->npdata[0];
2700 count = parent->bytes / sizeof(hammer2_blockref_t);
2702 case HAMMER2_BREF_TYPE_VOLUME:
2703 KKASSERT(parent->data != NULL);
2704 base = &hmp->voldata.sroot_blockset.blockref[0];
2705 count = HAMMER2_SET_COUNT;
2707 case HAMMER2_BREF_TYPE_FREEMAP:
2708 KKASSERT(parent->data != NULL);
2709 base = &hmp->voldata.freemap_blockset.blockref[0];
2710 count = HAMMER2_SET_COUNT;
2713 panic("hammer2_chain_create: unrecognized blockref type: %d",
2721 * Make sure we've counted the brefs
2723 if ((parent->core->flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
2724 hammer2_chain_countbrefs(parent, base, count);
2726 KKASSERT(above->live_count >= 0 && above->live_count <= count);
2729 * If no free blockref could be found we must create an indirect
2730 * block and move a number of blockrefs into it. With the parent
2731 * locked we can safely lock each child in order to delete+duplicate
2732 * it without causing a deadlock.
2734 * This may return the new indirect block or the old parent depending
2735 * on where the key falls. NULL is returned on error.
2737 if (above->live_count == count) {
2738 hammer2_chain_t *nparent;
2740 nparent = hammer2_chain_create_indirect(trans, parent,
2743 if (nparent == NULL) {
2745 hammer2_chain_drop(chain);
2749 if (parent != nparent) {
2750 hammer2_chain_unlock(parent);
2751 parent = *parentp = nparent;
2757 * Link the chain into its parent. Later on we will have to set
2758 * the MOVED bit in situations where we don't mark the new chain
2759 * as being modified.
2761 if (chain->above != NULL)
2762 panic("hammer2: hammer2_chain_create: chain already connected");
2763 KKASSERT(chain->above == NULL);
2764 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
2765 hammer2_chain_insert(above, NULL, chain,
2766 HAMMER2_CHAIN_INSERT_SPIN |
2767 HAMMER2_CHAIN_INSERT_LIVE,
2772 * Mark the newly created chain modified.
2774 * Device buffers are not instantiated for DATA elements
2775 * as these are handled by logical buffers.
2777 * Indirect and freemap node indirect blocks are handled
2778 * by hammer2_chain_create_indirect() and not by this
2781 * Data for all other bref types is expected to be
2782 * instantiated (INODE, LEAF).
2784 switch(chain->bref.type) {
2785 case HAMMER2_BREF_TYPE_DATA:
2786 hammer2_chain_modify(trans, &chain,
2787 HAMMER2_MODIFY_OPTDATA |
2788 HAMMER2_MODIFY_ASSERTNOCOPY);
2790 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2791 case HAMMER2_BREF_TYPE_INODE:
2792 hammer2_chain_modify(trans, &chain,
2793 HAMMER2_MODIFY_ASSERTNOCOPY);
2797 * Remaining types are not supported by this function.
2798 * In particular, INDIRECT and LEAF_NODE types are
2799 * handled by create_indirect().
2801 panic("hammer2_chain_create: bad type: %d",
2808 * When reconnecting a chain we must set MOVED and setsubmod
2809 * so the flush recognizes that it must update the bref in
2812 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
2813 hammer2_chain_ref(chain);
2814 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
2817 hammer2_chain_setsubmod(trans, chain);
2826 * Replace (*chainp) with a duplicate in-memory chain structure which shares
2827 * the same core and media state as the orignal. The original *chainp is
2828 * unlocked and the replacement will be returned locked.
2830 * The old chain must be in a DELETED state unless snapshot is non-zero.
2832 * The new chain will be live (i.e. not deleted), and modified.
2834 * If (parent) is non-NULL then the new duplicated chain is inserted under
2837 * If (parent) is NULL then the newly duplicated chain is not inserted
2838 * anywhere, similar to if it had just been chain_alloc()'d (suitable for
2839 * passing into hammer2_chain_create() after this function returns).
2841 * WARNING! This function cannot take snapshots all by itself. The caller
2842 * needs to do other massaging for snapshots.
2844 * WARNING! This function calls create which means it can insert indirect
2845 * blocks. Callers may have to refactor locked chains held across
2846 * the call (other than the ones passed into the call).
2848 static void hammer2_chain_dup_fixup(hammer2_chain_t *ochain,
2849 hammer2_chain_t *nchain);
2852 hammer2_chain_duplicate(hammer2_trans_t *trans, hammer2_chain_t **parentp,
2853 hammer2_chain_t **chainp, hammer2_blockref_t *bref,
2854 int snapshot, int duplicate_reason)
2856 hammer2_mount_t *hmp;
2857 hammer2_chain_t *parent;
2858 hammer2_chain_t *ochain;
2859 hammer2_chain_t *nchain;
2860 hammer2_chain_core_t *above;
2864 * We want nchain to be our go-to live chain, but ochain may be in
2865 * a MODIFIED state within the current flush synchronization segment.
2866 * Force any further modifications of ochain to do another COW
2867 * operation even if modify_tid indicates that one is not needed.
2869 * We don't want to set FORCECOW on nchain simply as an optimization,
2870 * as many duplication calls simply move chains into ichains and
2871 * then delete the original.
2873 * WARNING! We should never resolve DATA to device buffers
2874 * (XXX allow it if the caller did?), and since
2875 * we currently do not have the logical buffer cache
2876 * buffer in-hand to fix its cached physical offset
2877 * we also force the modify code to not COW it. XXX
2881 KKASSERT(snapshot == 1 || (ochain->flags & HAMMER2_CHAIN_DELETED));
2883 atomic_set_int(&ochain->flags, HAMMER2_CHAIN_FORCECOW);
2886 * Now create a duplicate of the chain structure, associating
2887 * it with the same core, making it the same size, pointing it
2888 * to the same bref (the same media block).
2890 * Give the duplicate the same modify_tid that we previously
2891 * ensured was sufficiently advanced to trigger a block table
2892 * insertion on flush.
2894 * NOTE: bref.mirror_tid duplicated by virtue of bref copy in
2895 * hammer2_chain_alloc()
2898 bref = &ochain->bref;
2900 nchain = hammer2_chain_alloc(hmp, NULL, trans, bref);
2901 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_SNAPSHOT);
2903 nchain = hammer2_chain_alloc(hmp, ochain->pmp, trans, bref);
2905 hammer2_chain_core_alloc(trans, nchain, ochain);
2906 bytes = (hammer2_off_t)1 <<
2907 (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
2908 nchain->bytes = bytes;
2909 nchain->modify_tid = ochain->modify_tid;
2910 nchain->inode_reason = ochain->inode_reason + 0x100000;
2911 if (ochain->flags & HAMMER2_CHAIN_INITIAL)
2912 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_INITIAL);
2913 if (ochain->flags & HAMMER2_CHAIN_UNLINKED)
2914 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_UNLINKED);
2917 * Fixup (copy) any embedded data. Non-embedded data relies on the
2918 * media block. We must unlock ochain before we can access nchain's
2919 * media block because they might share the same bp and deadlock if
2922 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_NEVER |
2923 HAMMER2_RESOLVE_NOREF);
2924 hammer2_chain_dup_fixup(ochain, nchain);
2925 /* nchain has 1 ref */
2926 hammer2_chain_unlock(ochain);
2927 KKASSERT((ochain->flags & HAMMER2_CHAIN_EMBEDDED) ||
2928 ochain->data == NULL);
2931 * Place nchain in the modified state, instantiate media data
2932 * if necessary. Because modify_tid is already completely
2933 * synchronized this should not result in a delete-duplicate.
2935 * We want nchain at the target to look like a new insertion.
2936 * Forcing the modification to be INPLACE accomplishes this
2937 * because we get the same nchain with an updated modify_tid.
2939 if (nchain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2940 hammer2_chain_modify(trans, &nchain,
2941 HAMMER2_MODIFY_OPTDATA |
2942 HAMMER2_MODIFY_NOREALLOC |
2943 HAMMER2_MODIFY_INPLACE);
2944 } else if (nchain->flags & HAMMER2_CHAIN_INITIAL) {
2945 hammer2_chain_modify(trans, &nchain,
2946 HAMMER2_MODIFY_OPTDATA |
2947 HAMMER2_MODIFY_INPLACE);
2949 hammer2_chain_modify(trans, &nchain,
2950 HAMMER2_MODIFY_INPLACE);
2954 * If parent is not NULL the duplicated chain will be entered under
2955 * the parent and the MOVED bit set.
2957 * Having both chains locked is extremely important for atomicy.
2959 if (parentp && (parent = *parentp) != NULL) {
2960 above = parent->core;
2961 KKASSERT(ccms_thread_lock_owned(&above->cst));
2962 KKASSERT((nchain->flags & HAMMER2_CHAIN_DELETED) == 0);
2963 KKASSERT(parent->refs > 0);
2965 hammer2_chain_create(trans, parentp, &nchain,
2966 nchain->bref.key, nchain->bref.keybits,
2967 nchain->bref.type, nchain->bytes);
2970 if ((nchain->flags & HAMMER2_CHAIN_MOVED) == 0) {
2971 hammer2_chain_ref(nchain);
2972 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_MOVED);
2974 hammer2_chain_setsubmod(trans, nchain);
2979 * Unconditionally set MOVED to force the parent blockrefs to
2980 * update, and adjust update_hi below nchain so nchain's
2981 * blockrefs are updated with the new attachment.
2983 if (nchain->core->update_hi < trans->sync_tid) {
2984 spin_lock(&nchain->core->cst.spin);
2985 if (nchain->core->update_hi < trans->sync_tid)
2986 nchain->core->update_hi = trans->sync_tid;
2987 spin_unlock(&nchain->core->cst.spin);
2995 * Special in-place delete-duplicate sequence which does not require a
2996 * locked parent. (*chainp) is marked DELETED and atomically replaced
2997 * with a duplicate. Atomicy is at the very-fine spin-lock level in
2998 * order to ensure that lookups do not race us.
3000 * If the old chain is already marked deleted the new chain will also be
3001 * marked deleted. This case can occur when an inode is removed from the
3002 * filesystem but programs still have an open descriptor to it, and during
3003 * flushes when the flush needs to operate on a chain that is deleted in
3004 * the live view but still alive in the flush view.
3006 * The new chain will be marked modified for the current transaction.
3009 hammer2_chain_delete_duplicate(hammer2_trans_t *trans, hammer2_chain_t **chainp,
3012 hammer2_mount_t *hmp;
3013 hammer2_chain_t *ochain;
3014 hammer2_chain_t *nchain;
3015 hammer2_chain_core_t *above;
3018 if (hammer2_debug & 0x20000)
3022 * Note that we do not have to call setsubmod on ochain, calling it
3023 * on nchain is sufficient.
3028 if (ochain->bref.type == HAMMER2_BREF_TYPE_INODE) {
3029 KKASSERT(ochain->data);
3033 * First create a duplicate of the chain structure.
3034 * (nchain is allocated with one ref).
3036 * In the case where nchain inherits ochains core, nchain is
3037 * effectively locked due to ochain being locked (and sharing the
3038 * core), until we can give nchain its own official ock.
3040 nchain = hammer2_chain_alloc(hmp, ochain->pmp, trans, &ochain->bref);
3041 if (flags & HAMMER2_DELDUP_RECORE)
3042 hammer2_chain_core_alloc(trans, nchain, NULL);
3044 hammer2_chain_core_alloc(trans, nchain, ochain);
3045 above = ochain->above;
3047 bytes = (hammer2_off_t)1 <<
3048 (int)(ochain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
3049 nchain->bytes = bytes;
3052 * Duplicate inherits ochain's live state including its modification
3053 * state. This function disposes of the original. Because we are
3054 * doing this in-place under the same parent the block array
3055 * inserted/deleted state does not change.
3057 * The caller isn't expected to make further modifications of ochain
3058 * but set the FORCECOW bit anyway, just in case it does. If ochain
3059 * was previously marked FORCECOW we also flag nchain FORCECOW
3060 * (used during hardlink splits). FORCECOW forces a reallocation
3061 * of the block when we modify the chain a little later, it does
3062 * not force another delete-duplicate.
3064 * NOTE: bref.mirror_tid duplicated by virtue of bref copy in
3065 * hammer2_chain_alloc()
3067 nchain->data_count += ochain->data_count;
3068 nchain->inode_count += ochain->inode_count;
3069 atomic_set_int(&nchain->flags,
3070 ochain->flags & (HAMMER2_CHAIN_INITIAL |
3071 HAMMER2_CHAIN_FORCECOW |
3072 HAMMER2_CHAIN_UNLINKED));
3073 atomic_set_int(&ochain->flags, HAMMER2_CHAIN_FORCECOW);
3074 nchain->inode_reason = ochain->inode_reason + 0x1000;
3077 * Lock nchain so both chains are now locked (extremely important
3078 * for atomicy). Mark ochain deleted and reinsert into the topology
3079 * and insert nchain all in one go.
3081 * If the ochain is already deleted it is left alone and nchain
3082 * is inserted into the topology as a deleted chain. This is
3083 * important because it allows ongoing operations to be executed
3084 * on a deleted inode which still has open descriptors.
3086 * The deleted case can also occur when a flush delete-duplicates
3087 * a node which is being concurrently modified by ongoing operations
3088 * in a later transaction. This creates a problem because the flush
3089 * is intended to update blockrefs which then propagate, allowing
3090 * the original covering in-memory chains to be freed up. In this
3091 * situation the flush code does NOT free the original covering
3092 * chains and will re-apply them to successive copies.
3094 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_NEVER);
3095 hammer2_chain_dup_fixup(ochain, nchain);
3096 /* extra ref still present from original allocation */
3098 KKASSERT(ochain->flags & HAMMER2_CHAIN_ONRBTREE);
3099 spin_lock(&above->cst.spin);
3100 KKASSERT(ochain->flags & HAMMER2_CHAIN_ONRBTREE);
3103 * Ultimately nchain->modify_tid will be set to trans->sync_tid,
3104 * but we can't do that here because we want to call
3105 * hammer2_chain_modify() to reallocate the block (if necessary).
3107 nchain->modify_tid = ochain->modify_tid;
3109 if (ochain->flags & HAMMER2_CHAIN_DELETED) {
3111 * ochain was deleted
3113 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_DELETED);
3114 if (ochain->delete_tid > trans->sync_tid) {
3116 * delete-duplicate a chain deleted in a later
3117 * transaction. Only allowed on chains created
3118 * before or during the current transaction (flush
3119 * code should filter out chains created after the
3120 * current transaction).
3122 * To make this work is a bit of a hack. We convert
3123 * ochain's delete_tid to the current sync_tid and
3124 * create a nchain which sets up ochains original
3127 * This effectively forces ochain to flush as a
3128 * deletion and nchain as a creation. Thus MOVED
3129 * must be set in ochain (it should already be
3130 * set since it's original delete_tid could not
3131 * have been flushed yet). Since ochain's delete_tid
3132 * has been moved down to sync_tid, a re-flush at
3133 * sync_tid won't try to delete-duplicate ochain
3136 KKASSERT(ochain->modify_tid <= trans->sync_tid);
3137 nchain->delete_tid = ochain->delete_tid;
3138 ochain->delete_tid = trans->sync_tid;
3139 KKASSERT(ochain->flags & HAMMER2_CHAIN_MOVED);
3140 } else if (ochain->delete_tid == trans->sync_tid) {
3142 * ochain was deleted in the current transaction
3144 nchain->delete_tid = trans->sync_tid;
3147 * ochain was deleted in a prior transaction.
3148 * create and delete nchain in the current
3151 * (delete_tid might represent a deleted inode
3152 * which still has an open descriptor).
3154 nchain->delete_tid = trans->sync_tid;
3156 hammer2_chain_insert(above, ochain->inlayer, nchain, 0, 0);
3159 * ochain was not deleted, delete it in the current
3162 KKASSERT(trans->sync_tid >= ochain->modify_tid);
3163 ochain->delete_tid = trans->sync_tid;
3164 atomic_set_int(&ochain->flags, HAMMER2_CHAIN_DELETED);
3165 atomic_add_int(&above->live_count, -1);
3166 hammer2_chain_insert(above, NULL, nchain,
3167 HAMMER2_CHAIN_INSERT_LIVE, 0);
3170 if ((ochain->flags & HAMMER2_CHAIN_MOVED) == 0) {
3171 hammer2_chain_ref(ochain);
3172 atomic_set_int(&ochain->flags, HAMMER2_CHAIN_MOVED);
3174 spin_unlock(&above->cst.spin);
3177 * ochain must be unlocked because ochain and nchain might share
3178 * a buffer cache buffer, so we need to release it so nchain can
3179 * potentially obtain it.
3181 hammer2_chain_unlock(ochain);
3184 * Finishing fixing up nchain. A new block will be allocated if
3185 * crossing a synchronization point (meta-data only).
3187 * Calling hammer2_chain_modify() will update modify_tid to
3188 * (typically) trans->sync_tid.
3190 if (nchain->bref.type == HAMMER2_BREF_TYPE_DATA) {
3191 hammer2_chain_modify(trans, &nchain,
3192 HAMMER2_MODIFY_OPTDATA |
3193 HAMMER2_MODIFY_NOREALLOC |
3194 HAMMER2_MODIFY_INPLACE);
3195 } else if (nchain->flags & HAMMER2_CHAIN_INITIAL) {
3196 hammer2_chain_modify(trans, &nchain,
3197 HAMMER2_MODIFY_OPTDATA |
3198 HAMMER2_MODIFY_INPLACE);
3200 hammer2_chain_modify(trans, &nchain,
3201 HAMMER2_MODIFY_INPLACE);
3203 hammer2_chain_drop(nchain);
3206 * Unconditionally set MOVED to force the parent blockrefs to
3207 * update as the chain_modify() above won't necessarily do it.
3209 * Adjust update_hi below nchain so nchain's blockrefs are updated
3210 * with the new attachment.
3212 if ((nchain->flags & HAMMER2_CHAIN_MOVED) == 0) {
3213 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_MOVED);
3214 hammer2_chain_ref(nchain);
3217 if (nchain->core->update_hi < trans->sync_tid) {
3218 spin_lock(&nchain->core->cst.spin);
3219 if (nchain->core->update_hi < trans->sync_tid)
3220 nchain->core->update_hi = trans->sync_tid;
3221 spin_unlock(&nchain->core->cst.spin);
3224 hammer2_chain_setsubmod(trans, nchain);
3229 * Helper function to fixup inodes. The caller procedure stack may hold
3230 * multiple locks on ochain if it represents an inode, preventing our
3231 * unlock from retiring its state to the buffer cache.
3233 * In this situation any attempt to access the buffer cache could result
3234 * either in stale data or a deadlock. Work around the problem by copying
3235 * the embedded data directly.
3239 hammer2_chain_dup_fixup(hammer2_chain_t *ochain, hammer2_chain_t *nchain)
3241 if (ochain->data == NULL)
3243 switch(ochain->bref.type) {
3244 case HAMMER2_BREF_TYPE_INODE:
3245 KKASSERT(nchain->data == NULL);
3246 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_EMBEDDED);
3247 nchain->data = kmalloc(sizeof(nchain->data->ipdata),
3248 ochain->hmp->mchain, M_WAITOK | M_ZERO);
3249 nchain->data->ipdata = ochain->data->ipdata;
3251 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3252 KKASSERT(nchain->data == NULL);
3253 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_EMBEDDED);
3254 nchain->data = kmalloc(sizeof(nchain->data->bmdata),
3255 ochain->hmp->mchain, M_WAITOK | M_ZERO);
3256 bcopy(ochain->data->bmdata,
3257 nchain->data->bmdata,
3258 sizeof(nchain->data->bmdata));
3266 * Create a snapshot of the specified {parent, ochain} with the specified
3267 * label. The originating hammer2_inode must be exclusively locked for
3270 * The ioctl code has already synced the filesystem.
3273 hammer2_chain_snapshot(hammer2_trans_t *trans, hammer2_chain_t **ochainp,
3274 hammer2_ioc_pfs_t *pfs)
3276 hammer2_mount_t *hmp;
3277 hammer2_chain_t *ochain = *ochainp;
3278 hammer2_chain_t *nchain;
3279 hammer2_inode_data_t *ipdata;
3280 hammer2_inode_t *nip;
3287 kprintf("snapshot %s ochain->refs %d ochain->flags %08x\n",
3288 pfs->name, ochain->refs, ochain->flags);
3290 name_len = strlen(pfs->name);
3291 lhc = hammer2_dirhash(pfs->name, name_len);
3294 opfs_clid = ochain->data->ipdata.pfs_clid;
3299 * Create the snapshot directory under the super-root
3301 * Set PFS type, generate a unique filesystem id, and generate
3302 * a cluster id. Use the same clid when snapshotting a PFS root,
3303 * which theoretically allows the snapshot to be used as part of
3304 * the same cluster (perhaps as a cache).
3306 * Copy the (flushed) ochain's blockref array. Theoretically we
3307 * could use chain_duplicate() but it becomes difficult to disentangle
3308 * the shared core so for now just brute-force it.
3314 nip = hammer2_inode_create(trans, hmp->sroot, &vat, proc0.p_ucred,
3315 pfs->name, name_len, &nchain, &error);
3318 ipdata = hammer2_chain_modify_ip(trans, nip, &nchain, 0);
3319 ipdata->pfs_type = HAMMER2_PFSTYPE_SNAPSHOT;
3320 kern_uuidgen(&ipdata->pfs_fsid, 1);
3321 if (ochain->flags & HAMMER2_CHAIN_PFSROOT)
3322 ipdata->pfs_clid = opfs_clid;
3324 kern_uuidgen(&ipdata->pfs_clid, 1);
3325 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_PFSROOT);
3326 ipdata->u.blockset = ochain->data->ipdata.u.blockset;
3328 hammer2_inode_unlock_ex(nip, nchain);
3334 * Create an indirect block that covers one or more of the elements in the
3335 * current parent. Either returns the existing parent with no locking or
3336 * ref changes or returns the new indirect block locked and referenced
3337 * and leaving the original parent lock/ref intact as well.
3339 * If an error occurs, NULL is returned and *errorp is set to the error.
3341 * The returned chain depends on where the specified key falls.
3343 * The key/keybits for the indirect mode only needs to follow three rules:
3345 * (1) That all elements underneath it fit within its key space and
3347 * (2) That all elements outside it are outside its key space.
3349 * (3) When creating the new indirect block any elements in the current
3350 * parent that fit within the new indirect block's keyspace must be
3351 * moved into the new indirect block.
3353 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
3354 * keyspace the the current parent, but lookup/iteration rules will
3355 * ensure (and must ensure) that rule (2) for all parents leading up
3356 * to the nearest inode or the root volume header is adhered to. This
3357 * is accomplished by always recursing through matching keyspaces in
3358 * the hammer2_chain_lookup() and hammer2_chain_next() API.
3360 * The current implementation calculates the current worst-case keyspace by
3361 * iterating the current parent and then divides it into two halves, choosing
3362 * whichever half has the most elements (not necessarily the half containing
3363 * the requested key).
3365 * We can also opt to use the half with the least number of elements. This
3366 * causes lower-numbered keys (aka logical file offsets) to recurse through
3367 * fewer indirect blocks and higher-numbered keys to recurse through more.
3368 * This also has the risk of not moving enough elements to the new indirect
3369 * block and being forced to create several indirect blocks before the element
3372 * Must be called with an exclusively locked parent.
3374 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
3375 hammer2_key_t *keyp, int keybits,
3376 hammer2_blockref_t *base, int count);
3377 static int hammer2_chain_indkey_normal(hammer2_chain_t *parent,
3378 hammer2_key_t *keyp, int keybits,
3379 hammer2_blockref_t *base, int count);
3382 hammer2_chain_create_indirect(hammer2_trans_t *trans, hammer2_chain_t *parent,
3383 hammer2_key_t create_key, int create_bits,
3384 int for_type, int *errorp)
3386 hammer2_mount_t *hmp;
3387 hammer2_chain_core_t *above;
3388 hammer2_chain_core_t *icore;
3389 hammer2_blockref_t *base;
3390 hammer2_blockref_t *bref;
3391 hammer2_blockref_t bcopy;
3392 hammer2_chain_t *chain;
3393 hammer2_chain_t *ichain;
3394 hammer2_chain_t dummy;
3395 hammer2_key_t key = create_key;
3396 hammer2_key_t key_beg;
3397 hammer2_key_t key_end;
3398 hammer2_key_t key_next;
3399 int keybits = create_bits;
3406 int maxloops = 300000;
3409 hammer2_chain_t * volatile xxchain = NULL;
3412 * Calculate the base blockref pointer or NULL if the chain
3413 * is known to be empty. We need to calculate the array count
3414 * for RB lookups either way.
3418 KKASSERT(ccms_thread_lock_owned(&parent->core->cst));
3419 above = parent->core;
3421 /*hammer2_chain_modify(trans, &parent, HAMMER2_MODIFY_OPTDATA);*/
3422 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
3425 switch(parent->bref.type) {
3426 case HAMMER2_BREF_TYPE_INODE:
3427 count = HAMMER2_SET_COUNT;
3429 case HAMMER2_BREF_TYPE_INDIRECT:
3430 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3431 count = parent->bytes / sizeof(hammer2_blockref_t);
3433 case HAMMER2_BREF_TYPE_VOLUME:
3434 count = HAMMER2_SET_COUNT;
3436 case HAMMER2_BREF_TYPE_FREEMAP:
3437 count = HAMMER2_SET_COUNT;
3440 panic("hammer2_chain_create_indirect: "
3441 "unrecognized blockref type: %d",
3447 switch(parent->bref.type) {
3448 case HAMMER2_BREF_TYPE_INODE:
3449 base = &parent->data->ipdata.u.blockset.blockref[0];
3450 count = HAMMER2_SET_COUNT;
3452 case HAMMER2_BREF_TYPE_INDIRECT:
3453 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3454 base = &parent->data->npdata[0];
3455 count = parent->bytes / sizeof(hammer2_blockref_t);
3457 case HAMMER2_BREF_TYPE_VOLUME:
3458 base = &hmp->voldata.sroot_blockset.blockref[0];
3459 count = HAMMER2_SET_COUNT;
3461 case HAMMER2_BREF_TYPE_FREEMAP:
3462 base = &hmp->voldata.freemap_blockset.blockref[0];
3463 count = HAMMER2_SET_COUNT;
3466 panic("hammer2_chain_create_indirect: "
3467 "unrecognized blockref type: %d",
3475 * dummy used in later chain allocation (no longer used for lookups).
3477 bzero(&dummy, sizeof(dummy));
3478 dummy.delete_tid = HAMMER2_MAX_TID;
3481 * When creating an indirect block for a freemap node or leaf
3482 * the key/keybits must be fitted to static radix levels because
3483 * particular radix levels use particular reserved blocks in the
3486 * This routine calculates the key/radix of the indirect block
3487 * we need to create, and whether it is on the high-side or the
3490 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
3491 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
3492 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
3495 keybits = hammer2_chain_indkey_normal(parent, &key, keybits,
3500 * Normalize the key for the radix being represented, keeping the
3501 * high bits and throwing away the low bits.
3503 key &= ~(((hammer2_key_t)1 << keybits) - 1);
3506 * How big should our new indirect block be? It has to be at least
3507 * as large as its parent.
3509 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE)
3510 nbytes = HAMMER2_IND_BYTES_MIN;
3512 nbytes = HAMMER2_IND_BYTES_MAX;
3513 if (nbytes < count * sizeof(hammer2_blockref_t))
3514 nbytes = count * sizeof(hammer2_blockref_t);
3517 * Ok, create our new indirect block
3519 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
3520 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
3521 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
3523 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
3525 dummy.bref.key = key;
3526 dummy.bref.keybits = keybits;
3527 dummy.bref.data_off = hammer2_getradix(nbytes);
3528 dummy.bref.methods = parent->bref.methods;
3530 ichain = hammer2_chain_alloc(hmp, parent->pmp, trans, &dummy.bref);
3531 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
3532 hammer2_chain_core_alloc(trans, ichain, NULL);
3533 icore = ichain->core;
3534 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
3535 hammer2_chain_drop(ichain); /* excess ref from alloc */
3538 * We have to mark it modified to allocate its block, but use
3539 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
3540 * it won't be acted upon by the flush code.
3542 * XXX leave the node unmodified, depend on the update_hi
3543 * flush to assign and modify parent blocks.
3545 hammer2_chain_modify(trans, &ichain, HAMMER2_MODIFY_OPTDATA);
3548 * Iterate the original parent and move the matching brefs into
3549 * the new indirect block.
3551 * XXX handle flushes.
3554 key_end = HAMMER2_MAX_KEY;
3556 spin_lock(&above->cst.spin);
3562 if (++loops > 100000) {
3563 spin_unlock(&above->cst.spin);
3564 panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
3565 reason, parent, base, count, key_next);
3569 * NOTE: spinlock stays intact, returned chain (if not NULL)
3570 * is not referenced or locked which means that we
3571 * cannot safely check its flagged / deletion status
3574 chain = hammer2_combined_find(parent, base, count,
3575 &cache_index, &key_next,
3578 generation = above->generation;
3581 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3584 * Skip keys that are not within the key/radix of the new
3585 * indirect block. They stay in the parent.
3587 if ((~(((hammer2_key_t)1 << keybits) - 1) &
3588 (key ^ bref->key)) != 0) {
3589 goto next_key_spinlocked;
3593 * Load the new indirect block by acquiring the related
3594 * chains (potentially from media as it might not be
3595 * in-memory). Then move it to the new parent (ichain)
3596 * via DELETE-DUPLICATE.
3598 * chain is referenced but not locked. We must lock the
3599 * chain to obtain definitive DUPLICATED/DELETED state
3603 * Use chain already present in the RBTREE
3605 hammer2_chain_ref(chain);
3606 wasdup = ((chain->flags &
3607 HAMMER2_CHAIN_DUPLICATED) != 0);
3608 spin_unlock(&above->cst.spin);
3609 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER |
3610 HAMMER2_RESOLVE_NOREF);
3613 * Get chain for blockref element. _get returns NULL
3614 * on insertion race.
3617 spin_unlock(&above->cst.spin);
3618 chain = hammer2_chain_get(parent, &bcopy, generation);
3619 if (chain == NULL) {
3621 spin_lock(&above->cst.spin);
3624 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
3626 hammer2_chain_drop(chain);
3627 spin_lock(&above->cst.spin);
3630 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER |
3631 HAMMER2_RESOLVE_NOREF);
3636 * This is always live so if the chain has been delete-
3637 * duplicated we raced someone and we have to retry.
3639 * NOTE: Lookups can race delete-duplicate because
3640 * delete-duplicate does not lock the parent's core
3641 * (they just use the spinlock on the core). We must
3642 * check for races by comparing the DUPLICATED flag before
3643 * releasing the spinlock with the flag after locking the
3646 * (note reversed logic for this one)
3648 if (chain->flags & HAMMER2_CHAIN_DELETED) {
3649 hammer2_chain_unlock(chain);
3650 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) &&
3659 * Shift the chain to the indirect block.
3661 * WARNING! Can cause held-over chains to require a refactor.
3662 * Fortunately we have none (our locked chains are
3663 * passed into and modified by the call).
3665 hammer2_chain_delete(trans, chain, 0);
3666 hammer2_chain_duplicate(trans, &ichain, &chain, NULL, 0, 1);
3667 hammer2_chain_unlock(chain);
3668 KKASSERT(parent->refs > 0);
3671 spin_lock(&above->cst.spin);
3672 next_key_spinlocked:
3673 if (--maxloops == 0)
3674 panic("hammer2_chain_create_indirect: maxloops");
3676 if (retry_same == 0) {
3677 if (key_next == 0 || key_next > key_end)
3683 spin_unlock(&above->cst.spin);
3686 * Insert the new indirect block into the parent now that we've
3687 * cleared out some entries in the parent. We calculated a good
3688 * insertion index in the loop above (ichain->index).
3690 * We don't have to set MOVED here because we mark ichain modified
3691 * down below (so the normal modified -> flush -> set-moved sequence
3694 * The insertion shouldn't race as this is a completely new block
3695 * and the parent is locked.
3697 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
3698 hammer2_chain_insert(above, NULL, ichain,
3699 HAMMER2_CHAIN_INSERT_SPIN |
3700 HAMMER2_CHAIN_INSERT_LIVE,
3704 * Mark the new indirect block modified after insertion, which
3705 * will propagate up through parent all the way to the root and
3706 * also allocate the physical block in ichain for our caller,
3707 * and assign ichain->data to a pre-zero'd space (because there
3708 * is not prior data to copy into it).
3710 * We have to set update_hi in ichain's flags manually so the
3711 * flusher knows it has to recurse through it to get to all of
3712 * our moved blocks, then call setsubmod() to set the bit
3715 /*hammer2_chain_modify(trans, &ichain, HAMMER2_MODIFY_OPTDATA);*/
3716 if (ichain->core->update_hi < trans->sync_tid) {
3717 spin_lock(&ichain->core->cst.spin);
3718 if (ichain->core->update_hi < trans->sync_tid)
3719 ichain->core->update_hi = trans->sync_tid;
3720 spin_unlock(&ichain->core->cst.spin);
3722 hammer2_chain_setsubmod(trans, ichain);
3725 * Figure out what to return.
3727 if (~(((hammer2_key_t)1 << keybits) - 1) &
3728 (create_key ^ key)) {
3730 * Key being created is outside the key range,
3731 * return the original parent.
3733 hammer2_chain_unlock(ichain);
3736 * Otherwise its in the range, return the new parent.
3737 * (leave both the new and old parent locked).
3747 * Calculate the keybits and highside/lowside of the freemap node the
3748 * caller is creating.
3750 * This routine will specify the next higher-level freemap key/radix
3751 * representing the lowest-ordered set. By doing so, eventually all
3752 * low-ordered sets will be moved one level down.
3754 * We have to be careful here because the freemap reserves a limited
3755 * number of blocks for a limited number of levels. So we can't just
3756 * push indiscriminately.
3759 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
3760 int keybits, hammer2_blockref_t *base, int count)
3762 hammer2_chain_core_t *above;
3763 hammer2_chain_t *chain;
3764 hammer2_blockref_t *bref;
3766 hammer2_key_t key_beg;
3767 hammer2_key_t key_end;
3768 hammer2_key_t key_next;
3772 int maxloops = 300000;
3775 above = parent->core;
3781 * Calculate the range of keys in the array being careful to skip
3782 * slots which are overridden with a deletion.
3785 key_end = HAMMER2_MAX_KEY;
3787 spin_lock(&above->cst.spin);
3790 if (--maxloops == 0) {
3791 panic("indkey_freemap shit %p %p:%d\n",
3792 parent, base, count);
3794 chain = hammer2_combined_find(parent, base, count,
3795 &cache_index, &key_next,
3796 key_beg, key_end, &bref);
3805 * NOTE: No need to check DUPLICATED here because we do
3806 * not release the spinlock.
3808 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3809 if (key_next == 0 || key_next > key_end)
3816 * Use the full live (not deleted) element for the scan
3817 * iteration. HAMMER2 does not allow partial replacements.
3819 * XXX should be built into hammer2_combined_find().
3821 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3823 if (keybits > bref->keybits) {
3825 keybits = bref->keybits;
3826 } else if (keybits == bref->keybits && bref->key < key) {
3833 spin_unlock(&above->cst.spin);
3836 * Return the keybits for a higher-level FREEMAP_NODE covering
3840 case HAMMER2_FREEMAP_LEVEL0_RADIX:
3841 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
3843 case HAMMER2_FREEMAP_LEVEL1_RADIX:
3844 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
3846 case HAMMER2_FREEMAP_LEVEL2_RADIX:
3847 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
3849 case HAMMER2_FREEMAP_LEVEL3_RADIX:
3850 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
3852 case HAMMER2_FREEMAP_LEVEL4_RADIX:
3853 panic("hammer2_chain_indkey_freemap: level too high");
3856 panic("hammer2_chain_indkey_freemap: bad radix");
3865 * Calculate the keybits and highside/lowside of the indirect block the
3866 * caller is creating.
3869 hammer2_chain_indkey_normal(hammer2_chain_t *parent, hammer2_key_t *keyp,
3870 int keybits, hammer2_blockref_t *base, int count)
3872 hammer2_chain_core_t *above;
3873 hammer2_blockref_t *bref;
3874 hammer2_chain_t *chain;
3875 hammer2_key_t key_beg;
3876 hammer2_key_t key_end;
3877 hammer2_key_t key_next;
3883 int maxloops = 300000;
3886 above = parent->core;
3891 * Calculate the range of keys in the array being careful to skip
3892 * slots which are overridden with a deletion. Once the scan
3893 * completes we will cut the key range in half and shift half the
3894 * range into the new indirect block.
3897 key_end = HAMMER2_MAX_KEY;
3899 spin_lock(&above->cst.spin);
3902 if (--maxloops == 0) {
3903 panic("indkey_freemap shit %p %p:%d\n",
3904 parent, base, count);
3906 chain = hammer2_combined_find(parent, base, count,
3907 &cache_index, &key_next,
3908 key_beg, key_end, &bref);
3917 * NOTE: No need to check DUPLICATED here because we do
3918 * not release the spinlock.
3920 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3921 if (key_next == 0 || key_next > key_end)
3928 * Use the full live (not deleted) element for the scan
3929 * iteration. HAMMER2 does not allow partial replacements.
3931 * XXX should be built into hammer2_combined_find().
3933 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3936 * Expand our calculated key range (key, keybits) to fit
3937 * the scanned key. nkeybits represents the full range
3938 * that we will later cut in half (two halves @ nkeybits - 1).
3941 if (nkeybits < bref->keybits) {
3942 if (bref->keybits > 64) {
3943 kprintf("bad bref chain %p bref %p\n",
3947 nkeybits = bref->keybits;
3949 while (nkeybits < 64 &&
3950 (~(((hammer2_key_t)1 << nkeybits) - 1) &
3951 (key ^ bref->key)) != 0) {
3956 * If the new key range is larger we have to determine
3957 * which side of the new key range the existing keys fall
3958 * under by checking the high bit, then collapsing the
3959 * locount into the hicount or vise-versa.
3961 if (keybits != nkeybits) {
3962 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
3973 * The newly scanned key will be in the lower half or the
3974 * upper half of the (new) key range.
3976 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
3985 spin_unlock(&above->cst.spin);
3986 bref = NULL; /* now invalid (safety) */
3989 * Adjust keybits to represent half of the full range calculated
3990 * above (radix 63 max)
3995 * Select whichever half contains the most elements. Theoretically
3996 * we can select either side as long as it contains at least one
3997 * element (in order to ensure that a free slot is present to hold
3998 * the indirect block).
4000 if (hammer2_indirect_optimize) {
4002 * Insert node for least number of keys, this will arrange
4003 * the first few blocks of a large file or the first few
4004 * inodes in a directory with fewer indirect blocks when
4007 if (hicount < locount && hicount != 0)
4008 key |= (hammer2_key_t)1 << keybits;
4010 key &= ~(hammer2_key_t)1 << keybits;
4013 * Insert node for most number of keys, best for heavily
4016 if (hicount > locount)
4017 key |= (hammer2_key_t)1 << keybits;
4019 key &= ~(hammer2_key_t)1 << keybits;
4027 * Sets CHAIN_DELETED and CHAIN_MOVED in the chain being deleted and
4028 * set chain->delete_tid. The chain is not actually marked possibly-free
4029 * in the freemap until the deletion is completely flushed out (because
4030 * a flush which doesn't cover the entire deletion is flushing the deleted
4031 * chain as if it were live).
4033 * This function does NOT generate a modification to the parent. It
4034 * would be nearly impossible to figure out which parent to modify anyway.
4035 * Such modifications are handled top-down by the flush code and are
4036 * properly merged using the flush synchronization point.
4038 * The find/get code will properly overload the RBTREE check on top of
4039 * the bref check to detect deleted entries.
4041 * This function is NOT recursive. Any entity already pushed into the
4042 * chain (such as an inode) may still need visibility into its contents,
4043 * as well as the ability to read and modify the contents. For example,
4044 * for an unlinked file which is still open.
4046 * NOTE: This function does NOT set chain->modify_tid, allowing future
4047 * code to distinguish between live and deleted chains by testing
4048 * trans->sync_tid vs chain->modify_tid and chain->delete_tid.
4050 * NOTE: Deletions normally do not occur in the middle of a duplication
4051 * chain but we use a trick for hardlink migration that refactors
4052 * the originating inode without deleting it, so we make no assumptions
4056 hammer2_chain_delete(hammer2_trans_t *trans, hammer2_chain_t *chain, int flags)
4058 KKASSERT(ccms_thread_lock_owned(&chain->core->cst));
4061 * Nothing to do if already marked.
4063 if (chain->flags & HAMMER2_CHAIN_DELETED)
4067 * The setting of DELETED causes finds, lookups, and _next iterations
4068 * to no longer recognize the chain. RB_SCAN()s will still have
4069 * visibility (needed for flush serialization points).
4071 * We need the spinlock on the core whos RBTREE contains chain
4072 * to protect against races.
4074 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
4075 spin_lock(&chain->above->cst.spin);
4077 KKASSERT(trans->sync_tid >= chain->modify_tid);
4078 chain->delete_tid = trans->sync_tid;
4079 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
4080 atomic_add_int(&chain->above->live_count, -1);
4081 ++chain->above->generation;
4084 * We must set MOVED along with DELETED for the flush code to
4085 * recognize the operation and properly disconnect the chain
4088 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
4089 hammer2_chain_ref(chain);
4090 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
4092 spin_unlock(&chain->above->cst.spin);
4094 hammer2_chain_setsubmod(trans, chain);
4098 * Called with the core spinlock held to check for freeable layers.
4099 * Used by the flush code. Layers can wind up not being freed due
4100 * to the temporary layer->refs count. This function frees up any
4101 * layers that were missed.
4104 hammer2_chain_layer_check_locked(hammer2_mount_t *hmp,
4105 hammer2_chain_core_t *core)
4107 hammer2_chain_layer_t *layer;
4108 hammer2_chain_layer_t *tmp;
4110 tmp = TAILQ_FIRST(&core->layerq);
4111 while ((layer = tmp) != NULL) {
4112 tmp = TAILQ_NEXT(tmp, entry);
4113 if (layer->refs == 0 && RB_EMPTY(&layer->rbtree)) {
4114 TAILQ_REMOVE(&core->layerq, layer, entry);
4117 spin_unlock(&core->cst.spin);
4118 kfree(layer, hmp->mchain);
4119 spin_lock(&core->cst.spin);
4127 * Returns the index of the nearest element in the blockref array >= elm.
4128 * Returns (count) if no element could be found.
4130 * Sets *key_nextp to the next key for loop purposes but does not modify
4131 * it if the next key would be higher than the current value of *key_nextp.
4132 * Note that *key_nexp can overflow to 0, which should be tested by the
4135 * (*cache_indexp) is a heuristic and can be any value without effecting
4138 * The spin lock on the related chain must be held.
4141 hammer2_base_find(hammer2_chain_t *chain,
4142 hammer2_blockref_t *base, int count,
4143 int *cache_indexp, hammer2_key_t *key_nextp,
4144 hammer2_key_t key_beg, hammer2_key_t key_end)
4146 hammer2_chain_core_t *core = chain->core;
4147 hammer2_blockref_t *scan;
4148 hammer2_key_t scan_end;
4153 * Require the live chain's already have their core's counted
4154 * so we can optimize operations.
4156 KKASSERT((chain->flags & HAMMER2_CHAIN_DUPLICATED) ||
4157 core->flags & HAMMER2_CORE_COUNTEDBREFS);
4162 if (count == 0 || base == NULL)
4166 * Sequential optimization using *cache_indexp. This is the most
4169 * We can avoid trailing empty entries on live chains, otherwise
4170 * we might have to check the whole block array.
4174 if (chain->flags & HAMMER2_CHAIN_DUPLICATED)
4177 limit = core->live_zero;
4182 KKASSERT(i < count);
4188 while (i > 0 && (scan->type == 0 || scan->key > key_beg)) {
4195 * Search forwards, stop when we find a scan element which
4196 * encloses the key or until we know that there are no further
4200 if (scan->type != 0) {
4201 if (scan->key > key_beg)
4203 scan_end = scan->key +
4204 ((hammer2_key_t)1 << scan->keybits) - 1;
4205 if (scan_end >= key_beg)
4218 scan_end = scan->key +
4219 ((hammer2_key_t)1 << scan->keybits);
4220 if (scan_end && (*key_nextp > scan_end ||
4222 *key_nextp = scan_end;
4230 * Do a combined search and return the next match either from the blockref
4231 * array or from the in-memory chain. Sets *bresp to the returned bref in
4232 * both cases, or sets it to NULL if the search exhausted. Only returns
4233 * a non-NULL chain if the search matched from the in-memory chain.
4235 * Must be called with above's spinlock held. Spinlock remains held
4236 * through the operation.
4238 * The returned chain is not locked or referenced. Use the returned bref
4239 * to determine if the search exhausted or not.
4241 static hammer2_chain_t *
4242 hammer2_combined_find(hammer2_chain_t *parent,
4243 hammer2_blockref_t *base, int count,
4244 int *cache_indexp, hammer2_key_t *key_nextp,
4245 hammer2_key_t key_beg, hammer2_key_t key_end,
4246 hammer2_blockref_t **bresp)
4248 hammer2_blockref_t *bref;
4249 hammer2_chain_t *chain;
4252 *key_nextp = key_end + 1;
4253 i = hammer2_base_find(parent, base, count, cache_indexp,
4254 key_nextp, key_beg, key_end);
4255 chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
4260 if (i == count && chain == NULL) {
4262 return(chain); /* NULL */
4266 * Only chain matched
4269 bref = &chain->bref;
4274 * Only blockref matched.
4276 if (chain == NULL) {
4282 * Both in-memory and blockref match. Select the nearer element.
4283 * If both are flush with the left-hand side they are considered
4284 * to be the same distance.
4286 * When both are the same distance away select the chain if it is
4287 * live or if it's delete_tid is greater than the parent's
4288 * synchronized bref.mirror_tid (a single test suffices for both
4289 * conditions), otherwise select the element.
4291 * (It is possible for an old deletion to linger after a rename-over
4292 * and flush, which would make the media copy the correct choice).
4296 * Either both are flush with the left-hand side or they are the
4297 * same distance away. Select the chain if it is not deleted
4298 * or it has a higher delete_tid, else select the media.
4300 if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
4301 chain->bref.key == base[i].key) {
4302 if (chain->delete_tid > base[i].mirror_tid) {
4303 bref = &chain->bref;
4305 KKASSERT(chain->flags & HAMMER2_CHAIN_DELETED);
4313 * Select the nearer key.
4315 if (chain->bref.key < base[i].key) {
4316 bref = &chain->bref;
4323 * If the bref is out of bounds we've exhausted our search.
4326 if (bref->key > key_end) {
4336 * Locate the specified block array element and delete it. The element
4339 * The spin lock on the related chain must be held.
4341 * NOTE: live_count was adjusted when the chain was deleted, so it does not
4342 * need to be adjusted when we commit the media change.
4345 hammer2_base_delete(hammer2_trans_t *trans, hammer2_chain_t *parent,
4346 hammer2_blockref_t *base, int count,
4347 int *cache_indexp, hammer2_chain_t *child)
4349 hammer2_blockref_t *elm = &child->bref;
4350 hammer2_chain_core_t *core = parent->core;
4351 hammer2_key_t key_next;
4355 * Delete element. Expect the element to exist.
4357 * XXX see caller, flush code not yet sophisticated enough to prevent
4358 * re-flushed in some cases.
4360 key_next = 0; /* max range */
4361 i = hammer2_base_find(parent, base, count, cache_indexp,
4362 &key_next, elm->key, elm->key);
4363 if (i == count || base[i].type == 0 ||
4364 base[i].key != elm->key || base[i].keybits != elm->keybits) {
4365 panic("delete base %p element not found at %d/%d elm %p\n",
4366 base, i, count, elm);
4369 bzero(&base[i], sizeof(*base));
4370 base[i].mirror_tid = (intptr_t)parent; /* debug */
4371 base[i].modify_tid = (intptr_t)child; /* debug */
4372 base[i].check.debug.sync_tid = trans->sync_tid; /* debug */
4375 * We can only optimize core->live_zero for live chains.
4377 if ((parent->flags & HAMMER2_CHAIN_DUPLICATED) == 0) {
4378 if (core->live_zero == i + 1) {
4379 while (--i >= 0 && base[i].type == 0)
4381 core->live_zero = i + 1;
4387 * Insert the specified element. The block array must not already have the
4388 * element and must have space available for the insertion.
4390 * The spin lock on the related chain must be held.
4392 * NOTE: live_count was adjusted when the chain was deleted, so it does not
4393 * need to be adjusted when we commit the media change.
4396 hammer2_base_insert(hammer2_trans_t *trans __unused, hammer2_chain_t *parent,
4397 hammer2_blockref_t *base, int count,
4398 int *cache_indexp, hammer2_chain_t *child)
4400 hammer2_blockref_t *elm = &child->bref;
4401 hammer2_chain_core_t *core = parent->core;
4402 hammer2_key_t key_next;
4411 * Insert new element. Expect the element to not already exist
4412 * unless we are replacing it.
4414 * XXX see caller, flush code not yet sophisticated enough to prevent
4415 * re-flushed in some cases.
4417 key_next = 0; /* max range */
4418 i = hammer2_base_find(parent, base, count, cache_indexp,
4419 &key_next, elm->key, elm->key);
4422 * Shortcut fill optimization, typical ordered insertion(s) may not
4425 KKASSERT(i >= 0 && i <= count);
4428 * We can only optimize core->live_zero for live chains.
4430 if (i == count && core->live_zero < count) {
4431 if ((parent->flags & HAMMER2_CHAIN_DUPLICATED) == 0) {
4432 i = core->live_zero++;
4438 xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
4439 if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
4440 panic("insert base %p overlapping elements at %d elm %p\n",
4445 * Try to find an empty slot before or after.
4449 while (j > 0 || k < count) {
4451 if (j >= 0 && base[j].type == 0) {
4455 bcopy(&base[j+1], &base[j],
4456 (i - j - 1) * sizeof(*base));
4462 if (k < count && base[k].type == 0) {
4463 bcopy(&base[i], &base[i+1],
4464 (k - i) * sizeof(hammer2_blockref_t));
4468 * We can only update core->live_zero for live
4471 if ((parent->flags & HAMMER2_CHAIN_DUPLICATED) == 0) {
4472 if (core->live_zero <= k)
4473 core->live_zero = k + 1;
4479 panic("hammer2_base_insert: no room!");
4486 for (l = 0; l < count; ++l) {
4488 key_next = base[l].key +
4489 ((hammer2_key_t)1 << base[l].keybits) - 1;
4493 while (++l < count) {
4495 if (base[l].key <= key_next)
4496 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
4497 key_next = base[l].key +
4498 ((hammer2_key_t)1 << base[l].keybits) - 1;
4508 * Sort the blockref array for the chain. Used by the flush code to
4509 * sort the blockref[] array.
4511 * The chain must be exclusively locked AND spin-locked.
4513 typedef hammer2_blockref_t *hammer2_blockref_p;
4517 hammer2_base_sort_callback(const void *v1, const void *v2)
4519 hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
4520 hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
4523 * Make sure empty elements are placed at the end of the array
4525 if (bref1->type == 0) {
4526 if (bref2->type == 0)
4529 } else if (bref2->type == 0) {
4536 if (bref1->key < bref2->key)
4538 if (bref1->key > bref2->key)
4544 hammer2_base_sort(hammer2_chain_t *chain)
4546 hammer2_blockref_t *base;
4549 switch(chain->bref.type) {
4550 case HAMMER2_BREF_TYPE_INODE:
4552 * Special shortcut for embedded data returns the inode
4553 * itself. Callers must detect this condition and access
4554 * the embedded data (the strategy code does this for us).
4556 * This is only applicable to regular files and softlinks.
4558 if (chain->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA)
4560 base = &chain->data->ipdata.u.blockset.blockref[0];
4561 count = HAMMER2_SET_COUNT;
4563 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
4564 case HAMMER2_BREF_TYPE_INDIRECT:
4566 * Optimize indirect blocks in the INITIAL state to avoid
4569 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
4570 base = &chain->data->npdata[0];
4571 count = chain->bytes / sizeof(hammer2_blockref_t);
4573 case HAMMER2_BREF_TYPE_VOLUME:
4574 base = &chain->hmp->voldata.sroot_blockset.blockref[0];
4575 count = HAMMER2_SET_COUNT;
4577 case HAMMER2_BREF_TYPE_FREEMAP:
4578 base = &chain->hmp->voldata.freemap_blockset.blockref[0];
4579 count = HAMMER2_SET_COUNT;
4582 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
4584 base = NULL; /* safety */
4585 count = 0; /* safety */
4587 kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
4593 * Chain memory management
4596 hammer2_chain_wait(hammer2_chain_t *chain)
4598 tsleep(chain, 0, "chnflw", 1);
4602 * Manage excessive memory resource use for chain and related
4606 hammer2_chain_memory_wait(hammer2_pfsmount_t *pmp)
4616 * Atomic check condition and wait. Also do an early speedup of
4617 * the syncer to try to avoid hitting the wait.
4620 waiting = pmp->inmem_dirty_chains;
4622 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
4624 limit = pmp->mp->mnt_nvnodelistsize / 10;
4625 if (limit < hammer2_limit_dirty_chains)
4626 limit = hammer2_limit_dirty_chains;
4631 if ((int)(ticks - zzticks) > hz) {
4633 kprintf("count %ld %ld\n", count, limit);
4638 * Block if there are too many dirty chains present, wait
4639 * for the flush to clean some out.
4641 if (count > limit) {
4642 tsleep_interlock(&pmp->inmem_dirty_chains, 0);
4643 if (atomic_cmpset_long(&pmp->inmem_dirty_chains,
4645 waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
4646 speedup_syncer(pmp->mp);
4647 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
4650 continue; /* loop on success or fail */
4654 * Try to start an early flush before we are forced to block.
4656 if (count > limit * 7 / 10)
4657 speedup_syncer(pmp->mp);
4663 hammer2_chain_memory_inc(hammer2_pfsmount_t *pmp)
4666 atomic_add_long(&pmp->inmem_dirty_chains, 1);
4670 hammer2_chain_memory_wakeup(hammer2_pfsmount_t *pmp)
4678 waiting = pmp->inmem_dirty_chains;
4680 if (atomic_cmpset_long(&pmp->inmem_dirty_chains,
4683 ~HAMMER2_DIRTYCHAIN_WAITING)) {
4687 if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
4688 wakeup(&pmp->inmem_dirty_chains);
4693 adjreadcounter(hammer2_blockref_t *bref, size_t bytes)
4697 switch(bref->type) {
4698 case HAMMER2_BREF_TYPE_DATA:
4699 counterp = &hammer2_iod_file_read;
4701 case HAMMER2_BREF_TYPE_INODE:
4702 counterp = &hammer2_iod_meta_read;
4704 case HAMMER2_BREF_TYPE_INDIRECT:
4705 counterp = &hammer2_iod_indr_read;
4707 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
4708 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
4709 counterp = &hammer2_iod_fmap_read;
4712 counterp = &hammer2_iod_volu_read;