2 * Copyright (c) 2011-2020 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 * and 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 * Chains are no-longer delete-duplicated. Instead, the original in-memory
44 * chain will be moved along with its block reference (e.g. for things like
45 * renames, hardlink operations, modifications, etc), and will be indexed
46 * on a secondary list for flush handling instead of propagating a flag
49 * Concurrent front-end operations can still run against backend flushes
50 * as long as they do not cross the current flush boundary. An operation
51 * running above the current flush (in areas not yet flushed) can become
52 * part of the current flush while ano peration running below the current
53 * flush can become part of the next flush.
55 #include <sys/cdefs.h>
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/types.h>
60 #include <sys/kern_syscall.h>
63 #include <crypto/sha2/sha2.h>
67 static hammer2_chain_t *hammer2_chain_create_indirect(
68 hammer2_chain_t *parent,
69 hammer2_key_t key, int keybits,
70 hammer2_tid_t mtid, int for_type, int *errorp);
71 static int hammer2_chain_delete_obref(hammer2_chain_t *parent,
72 hammer2_chain_t *chain,
73 hammer2_tid_t mtid, int flags,
74 hammer2_blockref_t *obref);
75 static hammer2_chain_t *hammer2_combined_find(
76 hammer2_chain_t *parent,
77 hammer2_blockref_t *base, int count,
78 hammer2_key_t *key_nextp,
79 hammer2_key_t key_beg, hammer2_key_t key_end,
80 hammer2_blockref_t **bresp);
81 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain,
83 static void hammer2_chain_lru_flush(hammer2_pfs_t *pmp);
86 * There are many degenerate situations where an extreme rate of console
87 * output can occur from warnings and errors. Make sure this output does
88 * not impede operations.
90 static struct krate krate_h2chk = { .freq = 5 };
91 static struct krate krate_h2me = { .freq = 1 };
92 static struct krate krate_h2em = { .freq = 1 };
95 * Basic RBTree for chains (core.rbtree).
97 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
100 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
102 hammer2_key_t c1_beg;
103 hammer2_key_t c1_end;
104 hammer2_key_t c2_beg;
105 hammer2_key_t c2_end;
108 * Compare chains. Overlaps are not supposed to happen and catch
109 * any software issues early we count overlaps as a match.
111 c1_beg = chain1->bref.key;
112 c1_end = c1_beg + ((hammer2_key_t)1 << chain1->bref.keybits) - 1;
113 c2_beg = chain2->bref.key;
114 c2_end = c2_beg + ((hammer2_key_t)1 << chain2->bref.keybits) - 1;
116 if (c1_end < c2_beg) /* fully to the left */
118 if (c1_beg > c2_end) /* fully to the right */
120 return(0); /* overlap (must not cross edge boundary) */
124 * Assert that a chain has no media data associated with it.
127 hammer2_chain_assert_no_data(hammer2_chain_t *chain)
129 KKASSERT(chain->dio == NULL);
130 if (chain->bref.type != HAMMER2_BREF_TYPE_VOLUME &&
131 chain->bref.type != HAMMER2_BREF_TYPE_FREEMAP &&
133 panic("hammer2_chain_assert_no_data: chain %p still has data",
139 * Make a chain visible to the flusher. The flusher operates using a top-down
140 * recursion based on the ONFLUSH flag. It locates MODIFIED and UPDATE chains,
141 * flushes them, and updates blocks back to the volume root.
143 * This routine sets the ONFLUSH flag upward from the triggering chain until
144 * it hits an inode root or the volume root. Inode chains serve as inflection
145 * points, requiring the flusher to bridge across trees. Inodes include
146 * regular inodes, PFS roots (pmp->iroot), and the media super root
150 hammer2_chain_setflush(hammer2_chain_t *chain)
152 hammer2_chain_t *parent;
154 if ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
155 hammer2_spin_sh(&chain->core.spin);
156 while ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
157 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH);
158 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
160 if ((parent = chain->parent) == NULL)
162 hammer2_spin_sh(&parent->core.spin);
163 hammer2_spin_unsh(&chain->core.spin);
166 hammer2_spin_unsh(&chain->core.spin);
171 * Allocate a new disconnected chain element representing the specified
172 * bref. chain->refs is set to 1 and the passed bref is copied to
173 * chain->bref. chain->bytes is derived from the bref.
175 * chain->pmp inherits pmp unless the chain is an inode (other than the
178 * NOTE: Returns a referenced but unlocked (because there is no core) chain.
181 hammer2_chain_alloc(hammer2_dev_t *hmp, hammer2_pfs_t *pmp,
182 hammer2_blockref_t *bref)
184 hammer2_chain_t *chain;
187 if (bref->type == HAMMER2_BREF_TYPE_EMPTY)
188 panic("hammer2_chain_alloc: empty blockref type\n");
191 * Special case - radix of 0 indicates a chain that does not
192 * need a data reference (context is completely embedded in the
195 if ((int)(bref->data_off & HAMMER2_OFF_MASK_RADIX))
196 bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
200 atomic_add_long(&hammer2_chain_allocs, 1);
201 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
204 * Initialize the new chain structure. pmp must be set to NULL for
205 * chains belonging to the super-root topology of a device mount.
207 if (pmp == hmp->spmp)
214 chain->bytes = bytes;
216 chain->flags = HAMMER2_CHAIN_ALLOCATED;
217 lockinit(&chain->diolk, "chdio", 0, 0);
220 * Set the PFS boundary flag if this chain represents a PFS root.
222 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
223 atomic_set_int(&chain->flags, HAMMER2_CHAIN_PFSBOUNDARY);
224 hammer2_chain_core_init(chain);
230 * Initialize a chain's core structure. This structure used to be allocated
231 * but is now embedded.
233 * The core is not locked. No additional refs on the chain are made.
234 * (trans) must not be NULL if (core) is not NULL.
237 hammer2_chain_core_init(hammer2_chain_t *chain)
240 * Fresh core under nchain (no multi-homing of ochain's
243 RB_INIT(&chain->core.rbtree); /* live chains */
244 hammer2_mtx_init(&chain->lock, "h2chain");
248 * Add a reference to a chain element, preventing its destruction.
250 * (can be called with spinlock held)
253 hammer2_chain_ref(hammer2_chain_t *chain)
255 if (atomic_fetchadd_int(&chain->refs, 1) == 0) {
257 * Just flag that the chain was used and should be recycled
258 * on the LRU if it encounters it later.
260 if (chain->flags & HAMMER2_CHAIN_ONLRU)
261 atomic_set_int(&chain->flags, HAMMER2_CHAIN_LRUHINT);
265 * REMOVED - reduces contention, lru_list is more heuristical
268 * 0->non-zero transition must ensure that chain is removed
271 * NOTE: Already holding lru_spin here so we cannot call
272 * hammer2_chain_ref() to get it off lru_list, do
275 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
276 hammer2_pfs_t *pmp = chain->pmp;
277 hammer2_spin_ex(&pmp->lru_spin);
278 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
279 atomic_add_int(&pmp->lru_count, -1);
280 atomic_clear_int(&chain->flags,
281 HAMMER2_CHAIN_ONLRU);
282 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
284 hammer2_spin_unex(&pmp->lru_spin);
291 * Ref a locked chain and force the data to be held across an unlock.
292 * Chain must be currently locked. The user of the chain who desires
293 * to release the hold must call hammer2_chain_lock_unhold() to relock
294 * and unhold the chain, then unlock normally, or may simply call
295 * hammer2_chain_drop_unhold() (which is safer against deadlocks).
298 hammer2_chain_ref_hold(hammer2_chain_t *chain)
300 atomic_add_int(&chain->lockcnt, 1);
301 hammer2_chain_ref(chain);
305 * Insert the chain in the core rbtree.
307 * Normal insertions are placed in the live rbtree. Insertion of a deleted
308 * chain is a special case used by the flush code that is placed on the
309 * unstaged deleted list to avoid confusing the live view.
311 #define HAMMER2_CHAIN_INSERT_SPIN 0x0001
312 #define HAMMER2_CHAIN_INSERT_LIVE 0x0002
313 #define HAMMER2_CHAIN_INSERT_RACE 0x0004
317 hammer2_chain_insert(hammer2_chain_t *parent, hammer2_chain_t *chain,
318 int flags, int generation)
320 hammer2_chain_t *xchain;
323 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
324 hammer2_spin_ex(&parent->core.spin);
327 * Interlocked by spinlock, check for race
329 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
330 parent->core.generation != generation) {
331 error = HAMMER2_ERROR_EAGAIN;
338 xchain = RB_INSERT(hammer2_chain_tree, &parent->core.rbtree, chain);
339 KASSERT(xchain == NULL,
340 ("hammer2_chain_insert: collision %p %p (key=%016jx)",
341 chain, xchain, chain->bref.key));
342 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
343 chain->parent = parent;
344 ++parent->core.chain_count;
345 ++parent->core.generation; /* XXX incs for _get() too, XXX */
348 * We have to keep track of the effective live-view blockref count
349 * so the create code knows when to push an indirect block.
351 if (flags & HAMMER2_CHAIN_INSERT_LIVE)
352 atomic_add_int(&parent->core.live_count, 1);
354 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
355 hammer2_spin_unex(&parent->core.spin);
360 * Drop the caller's reference to the chain. When the ref count drops to
361 * zero this function will try to disassociate the chain from its parent and
362 * deallocate it, then recursely drop the parent using the implied ref
363 * from the chain's chain->parent.
365 * Nobody should own chain's mutex on the 1->0 transition, unless this drop
366 * races an acquisition by another cpu. Therefore we can loop if we are
367 * unable to acquire the mutex, and refs is unlikely to be 1 unless we again
368 * race against another drop.
371 hammer2_chain_drop(hammer2_chain_t *chain)
375 KKASSERT(chain->refs > 0);
383 if (hammer2_mtx_ex_try(&chain->lock) == 0)
384 chain = hammer2_chain_lastdrop(chain, 0);
385 /* retry the same chain, or chain from lastdrop */
387 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
389 /* retry the same chain */
396 * Unhold a held and probably not-locked chain, ensure that the data is
397 * dropped on the 1->0 transition of lockcnt by obtaining an exclusive
398 * lock and then simply unlocking the chain.
401 hammer2_chain_unhold(hammer2_chain_t *chain)
407 lockcnt = chain->lockcnt;
410 if (atomic_cmpset_int(&chain->lockcnt,
411 lockcnt, lockcnt - 1)) {
414 } else if (hammer2_mtx_ex_try(&chain->lock) == 0) {
415 hammer2_chain_unlock(chain);
419 * This situation can easily occur on SMP due to
420 * the gap inbetween the 1->0 transition and the
421 * final unlock. We cannot safely block on the
422 * mutex because lockcnt might go above 1.
424 * XXX Sleep for one tick if it takes too long.
427 if (iter > 1000 + hz) {
428 kprintf("hammer2: h2race1 %p\n", chain);
431 tsleep(&iter, 0, "h2race1", 1);
439 hammer2_chain_drop_unhold(hammer2_chain_t *chain)
441 hammer2_chain_unhold(chain);
442 hammer2_chain_drop(chain);
446 hammer2_chain_rehold(hammer2_chain_t *chain)
448 hammer2_chain_lock(chain, HAMMER2_RESOLVE_SHARED);
449 atomic_add_int(&chain->lockcnt, 1);
450 hammer2_chain_unlock(chain);
454 * Handles the (potential) last drop of chain->refs from 1->0. Called with
455 * the mutex exclusively locked, refs == 1, and lockcnt 0. SMP races are
456 * possible against refs and lockcnt. We must dispose of the mutex on chain.
458 * This function returns an unlocked chain for recursive drop or NULL. It
459 * can return the same chain if it determines it has raced another ref.
463 * When two chains need to be recursively dropped we use the chain we
464 * would otherwise free to placehold the additional chain. It's a bit
465 * convoluted but we can't just recurse without potentially blowing out
468 * The chain cannot be freed if it has any children.
469 * The chain cannot be freed if flagged MODIFIED unless we can dispose of it.
470 * The chain cannot be freed if flagged UPDATE unless we can dispose of it.
471 * Any dedup registration can remain intact.
473 * The core spinlock is allowed to nest child-to-parent (not parent-to-child).
477 hammer2_chain_lastdrop(hammer2_chain_t *chain, int depth)
481 hammer2_chain_t *parent;
482 hammer2_chain_t *rdrop;
485 * We need chain's spinlock to interlock the sub-tree test.
486 * We already have chain's mutex, protecting chain->parent.
488 * Remember that chain->refs can be in flux.
490 hammer2_spin_ex(&chain->core.spin);
492 if (chain->parent != NULL) {
494 * If the chain has a parent the UPDATE bit prevents scrapping
495 * as the chain is needed to properly flush the parent. Try
496 * to complete the 1->0 transition and return NULL. Retry
497 * (return chain) if we are unable to complete the 1->0
498 * transition, else return NULL (nothing more to do).
500 * If the chain has a parent the MODIFIED bit prevents
503 * Chains with UPDATE/MODIFIED are *not* put on the LRU list!
505 if (chain->flags & (HAMMER2_CHAIN_UPDATE |
506 HAMMER2_CHAIN_MODIFIED)) {
507 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
508 hammer2_spin_unex(&chain->core.spin);
509 hammer2_chain_assert_no_data(chain);
510 hammer2_mtx_unlock(&chain->lock);
513 hammer2_spin_unex(&chain->core.spin);
514 hammer2_mtx_unlock(&chain->lock);
518 /* spinlock still held */
519 } else if (chain->bref.type == HAMMER2_BREF_TYPE_VOLUME ||
520 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP) {
522 * Retain the static vchain and fchain. Clear bits that
523 * are not relevant. Do not clear the MODIFIED bit,
524 * and certainly do not put it on the delayed-flush queue.
526 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
529 * The chain has no parent and can be flagged for destruction.
530 * Since it has no parent, UPDATE can also be cleared.
532 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
533 if (chain->flags & HAMMER2_CHAIN_UPDATE)
534 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
537 * If the chain has children we must propagate the DESTROY
538 * flag downward and rip the disconnected topology apart.
539 * This is accomplished by calling hammer2_flush() on the
542 * Any dedup is already handled by the underlying DIO, so
543 * we do not have to specifically flush it here.
545 if (chain->core.chain_count) {
546 hammer2_spin_unex(&chain->core.spin);
547 hammer2_flush(chain, HAMMER2_FLUSH_TOP |
549 hammer2_mtx_unlock(&chain->lock);
551 return(chain); /* retry drop */
555 * Otherwise we can scrap the MODIFIED bit if it is set,
556 * and continue along the freeing path.
558 * Be sure to clean-out any dedup bits. Without a parent
559 * this chain will no longer be visible to the flush code.
560 * Easy check data_off to avoid the volume root.
562 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
563 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
564 atomic_add_long(&hammer2_count_modified_chains, -1);
566 hammer2_pfs_memory_wakeup(chain->pmp, -1);
568 /* spinlock still held */
571 /* spinlock still held */
574 * If any children exist we must leave the chain intact with refs == 0.
575 * They exist because chains are retained below us which have refs or
576 * may require flushing.
578 * Retry (return chain) if we fail to transition the refs to 0, else
579 * return NULL indication nothing more to do.
581 * Chains with children are NOT put on the LRU list.
583 if (chain->core.chain_count) {
584 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
585 hammer2_spin_unex(&chain->core.spin);
586 hammer2_chain_assert_no_data(chain);
587 hammer2_mtx_unlock(&chain->lock);
590 hammer2_spin_unex(&chain->core.spin);
591 hammer2_mtx_unlock(&chain->lock);
595 /* spinlock still held */
596 /* no chains left under us */
599 * chain->core has no children left so no accessors can get to our
600 * chain from there. Now we have to lock the parent core to interlock
601 * remaining possible accessors that might bump chain's refs before
602 * we can safely drop chain's refs with intent to free the chain.
605 pmp = chain->pmp; /* can be NULL */
608 parent = chain->parent;
611 * WARNING! chain's spin lock is still held here, and other spinlocks
612 * will be acquired and released in the code below. We
613 * cannot be making fancy procedure calls!
617 * We can cache the chain if it is associated with a pmp
618 * and not flagged as being destroyed or requesting a full
619 * release. In this situation the chain is not removed
620 * from its parent, i.e. it can still be looked up.
622 * We intentionally do not cache DATA chains because these
623 * were likely used to load data into the logical buffer cache
624 * and will not be accessed again for some time.
627 (HAMMER2_CHAIN_DESTROY | HAMMER2_CHAIN_RELEASE)) == 0 &&
629 chain->bref.type != HAMMER2_BREF_TYPE_DATA) {
631 hammer2_spin_ex(&parent->core.spin);
632 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
634 * 1->0 transition failed, retry. Do not drop
635 * the chain's data yet!
638 hammer2_spin_unex(&parent->core.spin);
639 hammer2_spin_unex(&chain->core.spin);
640 hammer2_mtx_unlock(&chain->lock);
648 hammer2_chain_assert_no_data(chain);
651 * Make sure we are on the LRU list, clean up excessive
652 * LRU entries. We can only really drop one but there might
653 * be other entries that we can remove from the lru_list
656 * NOTE: HAMMER2_CHAIN_ONLRU may only be safely set when
657 * chain->core.spin AND pmp->lru_spin are held, but
658 * can be safely cleared only holding pmp->lru_spin.
660 if ((chain->flags & HAMMER2_CHAIN_ONLRU) == 0) {
661 hammer2_spin_ex(&pmp->lru_spin);
662 if ((chain->flags & HAMMER2_CHAIN_ONLRU) == 0) {
663 atomic_set_int(&chain->flags,
664 HAMMER2_CHAIN_ONLRU);
665 TAILQ_INSERT_TAIL(&pmp->lru_list,
667 atomic_add_int(&pmp->lru_count, 1);
669 if (pmp->lru_count < HAMMER2_LRU_LIMIT)
670 depth = 1; /* disable lru_list flush */
671 hammer2_spin_unex(&pmp->lru_spin);
673 /* disable lru flush */
678 hammer2_spin_unex(&parent->core.spin);
679 parent = NULL; /* safety */
681 hammer2_spin_unex(&chain->core.spin);
682 hammer2_mtx_unlock(&chain->lock);
685 * lru_list hysteresis (see above for depth overrides).
686 * Note that depth also prevents excessive lastdrop recursion.
689 hammer2_chain_lru_flush(pmp);
696 * Make sure we are not on the LRU list.
698 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
699 hammer2_spin_ex(&pmp->lru_spin);
700 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
701 atomic_add_int(&pmp->lru_count, -1);
702 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
703 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
705 hammer2_spin_unex(&pmp->lru_spin);
709 * Spinlock the parent and try to drop the last ref on chain.
710 * On success determine if we should dispose of the chain
711 * (remove the chain from its parent, etc).
713 * (normal core locks are top-down recursive but we define
714 * core spinlocks as bottom-up recursive, so this is safe).
717 hammer2_spin_ex(&parent->core.spin);
718 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
720 * 1->0 transition failed, retry.
722 hammer2_spin_unex(&parent->core.spin);
723 hammer2_spin_unex(&chain->core.spin);
724 hammer2_mtx_unlock(&chain->lock);
730 * 1->0 transition successful, parent spin held to prevent
731 * new lookups, chain spinlock held to protect parent field.
732 * Remove chain from the parent.
734 * If the chain is being removed from the parent's btree but
735 * is not bmapped, we have to adjust live_count downward. If
736 * it is bmapped then the blockref is retained in the parent
737 * as is its associated live_count. This case can occur when
738 * a chain added to the topology is unable to flush and is
739 * then later deleted.
741 if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
742 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) &&
743 (chain->flags & HAMMER2_CHAIN_BMAPPED) == 0) {
744 atomic_add_int(&parent->core.live_count, -1);
746 RB_REMOVE(hammer2_chain_tree,
747 &parent->core.rbtree, chain);
748 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
749 --parent->core.chain_count;
750 chain->parent = NULL;
754 * If our chain was the last chain in the parent's core the
755 * core is now empty and its parent might have to be
756 * re-dropped if it has 0 refs.
758 if (parent->core.chain_count == 0) {
760 atomic_add_int(&rdrop->refs, 1);
762 if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0)
766 hammer2_spin_unex(&parent->core.spin);
767 parent = NULL; /* safety */
773 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
775 * 1->0 transition failed, retry.
777 hammer2_spin_unex(&parent->core.spin);
778 hammer2_spin_unex(&chain->core.spin);
779 hammer2_mtx_unlock(&chain->lock);
786 * Successful 1->0 transition, no parent, no children... no way for
787 * anyone to ref this chain any more. We can clean-up and free it.
789 * We still have the core spinlock, and core's chain_count is 0.
790 * Any parent spinlock is gone.
792 hammer2_spin_unex(&chain->core.spin);
793 hammer2_chain_assert_no_data(chain);
794 hammer2_mtx_unlock(&chain->lock);
795 KKASSERT(RB_EMPTY(&chain->core.rbtree) &&
796 chain->core.chain_count == 0);
799 * All locks are gone, no pointers remain to the chain, finish
802 KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE |
803 HAMMER2_CHAIN_MODIFIED)) == 0);
806 * Once chain resources are gone we can use the now dead chain
807 * structure to placehold what might otherwise require a recursive
808 * drop, because we have potentially two things to drop and can only
809 * return one directly.
811 if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
812 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ALLOCATED);
814 kfree(chain, hmp->mchain);
818 * Possible chaining loop when parent re-drop needed.
824 * Heuristical flush of the LRU, try to reduce the number of entries
825 * on the LRU to (HAMMER2_LRU_LIMIT * 2 / 3). This procedure is called
826 * only when lru_count exceeds HAMMER2_LRU_LIMIT.
830 hammer2_chain_lru_flush(hammer2_pfs_t *pmp)
832 hammer2_chain_t *chain;
836 hammer2_spin_ex(&pmp->lru_spin);
837 while (pmp->lru_count > HAMMER2_LRU_LIMIT * 2 / 3) {
839 * Pick a chain off the lru_list, just recycle it quickly
840 * if LRUHINT is set (the chain was ref'd but left on
841 * the lru_list, so cycle to the end).
843 chain = TAILQ_FIRST(&pmp->lru_list);
844 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
846 if (chain->flags & HAMMER2_CHAIN_LRUHINT) {
847 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_LRUHINT);
848 TAILQ_INSERT_TAIL(&pmp->lru_list, chain, lru_node);
854 * Ok, we are off the LRU. We must adjust refs before we
855 * can safely clear the ONLRU flag.
857 atomic_add_int(&pmp->lru_count, -1);
858 if (atomic_cmpset_int(&chain->refs, 0, 1)) {
859 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
860 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
863 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
866 hammer2_spin_unex(&pmp->lru_spin);
871 * If we picked a chain off the lru list we may be able to lastdrop
872 * it. Use a depth of 1 to prevent excessive lastdrop recursion.
882 if (hammer2_mtx_ex_try(&chain->lock) == 0)
883 chain = hammer2_chain_lastdrop(chain, 1);
884 /* retry the same chain, or chain from lastdrop */
886 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
888 /* retry the same chain */
896 * On last lock release.
898 static hammer2_io_t *
899 hammer2_chain_drop_data(hammer2_chain_t *chain)
903 if ((dio = chain->dio) != NULL) {
907 switch(chain->bref.type) {
908 case HAMMER2_BREF_TYPE_VOLUME:
909 case HAMMER2_BREF_TYPE_FREEMAP:
912 if (chain->data != NULL) {
913 hammer2_spin_unex(&chain->core.spin);
914 panic("chain data not null: "
915 "chain %p bref %016jx.%02x "
916 "refs %d parent %p dio %p data %p",
917 chain, chain->bref.data_off,
918 chain->bref.type, chain->refs,
920 chain->dio, chain->data);
922 KKASSERT(chain->data == NULL);
930 * Lock a referenced chain element, acquiring its data with I/O if necessary,
931 * and specify how you would like the data to be resolved.
933 * If an I/O or other fatal error occurs, chain->error will be set to non-zero.
935 * The lock is allowed to recurse, multiple locking ops will aggregate
936 * the requested resolve types. Once data is assigned it will not be
937 * removed until the last unlock.
939 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
940 * (typically used to avoid device/logical buffer
943 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
944 * the INITIAL-create state (indirect blocks only).
946 * Do not resolve data elements for DATA chains.
947 * (typically used to avoid device/logical buffer
950 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
952 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
953 * it will be locked exclusive.
955 * HAMMER2_RESOLVE_NONBLOCK- (flag) The chain is locked non-blocking. If
956 * the lock fails, EAGAIN is returned.
958 * NOTE: Embedded elements (volume header, inodes) are always resolved
961 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
962 * element will instantiate and zero its buffer, and flush it on
965 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
966 * so as not to instantiate a device buffer, which could alias against
967 * a logical file buffer. However, if ALWAYS is specified the
968 * device buffer will be instantiated anyway.
970 * NOTE: The return value is always 0 unless NONBLOCK is specified, in which
971 * case it can be either 0 or EAGAIN.
973 * WARNING! This function blocks on I/O if data needs to be fetched. This
974 * blocking can run concurrent with other compatible lock holders
975 * who do not need data returning. The lock is not upgraded to
976 * exclusive during a data fetch, a separate bit is used to
977 * interlock I/O. However, an exclusive lock holder can still count
978 * on being interlocked against an I/O fetch managed by a shared
982 hammer2_chain_lock(hammer2_chain_t *chain, int how)
984 KKASSERT(chain->refs > 0);
986 if (how & HAMMER2_RESOLVE_NONBLOCK) {
988 * We still have to bump lockcnt before acquiring the lock,
989 * even for non-blocking operation, because the unlock code
990 * live-loops on lockcnt == 1 when dropping the last lock.
992 * If the non-blocking operation fails we have to use an
993 * unhold sequence to undo the mess.
995 * NOTE: LOCKAGAIN must always succeed without blocking,
996 * even if NONBLOCK is specified.
998 atomic_add_int(&chain->lockcnt, 1);
999 if (how & HAMMER2_RESOLVE_SHARED) {
1000 if (how & HAMMER2_RESOLVE_LOCKAGAIN) {
1001 hammer2_mtx_sh_again(&chain->lock);
1003 if (hammer2_mtx_sh_try(&chain->lock) != 0) {
1004 hammer2_chain_unhold(chain);
1009 if (hammer2_mtx_ex_try(&chain->lock) != 0) {
1010 hammer2_chain_unhold(chain);
1016 * Get the appropriate lock. If LOCKAGAIN is flagged with
1017 * SHARED the caller expects a shared lock to already be
1018 * present and we are giving it another ref. This case must
1019 * importantly not block if there is a pending exclusive lock
1022 atomic_add_int(&chain->lockcnt, 1);
1023 if (how & HAMMER2_RESOLVE_SHARED) {
1024 if (how & HAMMER2_RESOLVE_LOCKAGAIN) {
1025 hammer2_mtx_sh_again(&chain->lock);
1027 hammer2_mtx_sh(&chain->lock);
1030 hammer2_mtx_ex(&chain->lock);
1035 * If we already have a valid data pointer make sure the data is
1036 * synchronized to the current cpu, and then no further action is
1041 hammer2_io_bkvasync(chain->dio);
1046 * Do we have to resolve the data? This is generally only
1047 * applicable to HAMMER2_BREF_TYPE_DATA which is special-cased.
1048 * Other BREF types expects the data to be there.
1050 switch(how & HAMMER2_RESOLVE_MASK) {
1051 case HAMMER2_RESOLVE_NEVER:
1053 case HAMMER2_RESOLVE_MAYBE:
1054 if (chain->flags & HAMMER2_CHAIN_INITIAL)
1056 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
1059 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
1061 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
1065 case HAMMER2_RESOLVE_ALWAYS:
1071 * Caller requires data
1073 hammer2_chain_load_data(chain);
1079 * Lock the chain, retain the hold, and drop the data persistence count.
1080 * The data should remain valid because we never transitioned lockcnt
1084 hammer2_chain_lock_unhold(hammer2_chain_t *chain, int how)
1086 hammer2_chain_lock(chain, how);
1087 atomic_add_int(&chain->lockcnt, -1);
1092 * Downgrade an exclusive chain lock to a shared chain lock.
1094 * NOTE: There is no upgrade equivalent due to the ease of
1095 * deadlocks in that direction.
1098 hammer2_chain_lock_downgrade(hammer2_chain_t *chain)
1100 hammer2_mtx_downgrade(&chain->lock);
1105 * Issue I/O and install chain->data. Caller must hold a chain lock, lock
1106 * may be of any type.
1108 * Once chain->data is set it cannot be disposed of until all locks are
1111 * Make sure the data is synchronized to the current cpu.
1114 hammer2_chain_load_data(hammer2_chain_t *chain)
1116 hammer2_blockref_t *bref;
1123 * Degenerate case, data already present, or chain has no media
1124 * reference to load.
1126 KKASSERT(chain->lock.mtx_lock & MTX_MASK);
1129 hammer2_io_bkvasync(chain->dio);
1132 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0)
1136 KKASSERT(hmp != NULL);
1139 * Gain the IOINPROG bit, interlocked block.
1145 oflags = chain->flags;
1147 if (oflags & HAMMER2_CHAIN_IOINPROG) {
1148 nflags = oflags | HAMMER2_CHAIN_IOSIGNAL;
1149 tsleep_interlock(&chain->flags, 0);
1150 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1151 tsleep(&chain->flags, PINTERLOCKED,
1156 nflags = oflags | HAMMER2_CHAIN_IOINPROG;
1157 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1165 * We own CHAIN_IOINPROG
1167 * Degenerate case if we raced another load.
1171 hammer2_io_bkvasync(chain->dio);
1176 * We must resolve to a device buffer, either by issuing I/O or
1177 * by creating a zero-fill element. We do not mark the buffer
1178 * dirty when creating a zero-fill element (the hammer2_chain_modify()
1179 * API must still be used to do that).
1181 * The device buffer is variable-sized in powers of 2 down
1182 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
1183 * chunk always contains buffers of the same size. (XXX)
1185 * The minimum physical IO size may be larger than the variable
1188 bref = &chain->bref;
1191 * The getblk() optimization can only be used on newly created
1192 * elements if the physical block size matches the request.
1194 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1195 error = hammer2_io_new(hmp, bref->type,
1196 bref->data_off, chain->bytes,
1199 error = hammer2_io_bread(hmp, bref->type,
1200 bref->data_off, chain->bytes,
1202 hammer2_adjreadcounter(chain->bref.type, chain->bytes);
1205 chain->error = HAMMER2_ERROR_EIO;
1206 kprintf("hammer2_chain_load_data: I/O error %016jx: %d\n",
1207 (intmax_t)bref->data_off, error);
1208 hammer2_io_bqrelse(&chain->dio);
1214 * This isn't perfect and can be ignored on OSs which do not have
1215 * an indication as to whether a buffer is coming from cache or
1216 * if I/O was actually issued for the read. TESTEDGOOD will work
1217 * pretty well without the B_IOISSUED logic because chains are
1218 * cached, but in that situation (without B_IOISSUED) it will not
1219 * detect whether a re-read via I/O is corrupted verses the original
1222 * We can't re-run the CRC on every fresh lock. That would be
1223 * insanely expensive.
1225 * If the underlying kernel buffer covers the entire chain we can
1226 * use the B_IOISSUED indication to determine if we have to re-run
1227 * the CRC on chain data for chains that managed to stay cached
1228 * across the kernel disposal of the original buffer.
1230 if ((dio = chain->dio) != NULL && dio->bp) {
1231 struct buf *bp = dio->bp;
1233 if (dio->psize == chain->bytes &&
1234 (bp->b_flags & B_IOISSUED)) {
1235 atomic_clear_int(&chain->flags,
1236 HAMMER2_CHAIN_TESTEDGOOD);
1237 bp->b_flags &= ~B_IOISSUED;
1242 * NOTE: A locked chain's data cannot be modified without first
1243 * calling hammer2_chain_modify().
1247 * NOTE: hammer2_io_data() call issues bkvasync()
1249 bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
1251 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1253 * Clear INITIAL. In this case we used io_new() and the
1254 * buffer has been zero'd and marked dirty.
1256 * CHAIN_MODIFIED has not been set yet, and we leave it
1257 * that way for now. Set a temporary CHAIN_NOTTESTED flag
1258 * to prevent hammer2_chain_testcheck() from trying to match
1259 * a check code that has not yet been generated. This bit
1260 * should NOT end up on the actual media.
1262 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1263 atomic_set_int(&chain->flags, HAMMER2_CHAIN_NOTTESTED);
1264 } else if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
1266 * check data not currently synchronized due to
1267 * modification. XXX assumes data stays in the buffer
1268 * cache, which might not be true (need biodep on flush
1269 * to calculate crc? or simple crc?).
1271 } else if ((chain->flags & HAMMER2_CHAIN_TESTEDGOOD) == 0) {
1272 if (hammer2_chain_testcheck(chain, bdata) == 0) {
1273 chain->error = HAMMER2_ERROR_CHECK;
1275 atomic_set_int(&chain->flags, HAMMER2_CHAIN_TESTEDGOOD);
1280 * Setup the data pointer, either pointing it to an embedded data
1281 * structure and copying the data from the buffer, or pointing it
1284 * The buffer is not retained when copying to an embedded data
1285 * structure in order to avoid potential deadlocks or recursions
1286 * on the same physical buffer.
1288 * WARNING! Other threads can start using the data the instant we
1289 * set chain->data non-NULL.
1291 switch (bref->type) {
1292 case HAMMER2_BREF_TYPE_VOLUME:
1293 case HAMMER2_BREF_TYPE_FREEMAP:
1295 * Copy data from bp to embedded buffer
1297 panic("hammer2_chain_load_data: unresolved volume header");
1299 case HAMMER2_BREF_TYPE_DIRENT:
1300 KKASSERT(chain->bytes != 0);
1302 case HAMMER2_BREF_TYPE_INODE:
1303 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1304 case HAMMER2_BREF_TYPE_INDIRECT:
1305 case HAMMER2_BREF_TYPE_DATA:
1306 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1309 * Point data at the device buffer and leave dio intact.
1311 chain->data = (void *)bdata;
1316 * Release HAMMER2_CHAIN_IOINPROG and signal waiters if requested.
1323 oflags = chain->flags;
1324 nflags = oflags & ~(HAMMER2_CHAIN_IOINPROG |
1325 HAMMER2_CHAIN_IOSIGNAL);
1326 KKASSERT(oflags & HAMMER2_CHAIN_IOINPROG);
1327 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1328 if (oflags & HAMMER2_CHAIN_IOSIGNAL)
1329 wakeup(&chain->flags);
1336 * Unlock and deref a chain element.
1338 * Remember that the presence of children under chain prevent the chain's
1339 * destruction but do not add additional references, so the dio will still
1343 hammer2_chain_unlock(hammer2_chain_t *chain)
1350 * If multiple locks are present (or being attempted) on this
1351 * particular chain we can just unlock, drop refs, and return.
1353 * Otherwise fall-through on the 1->0 transition.
1356 lockcnt = chain->lockcnt;
1357 KKASSERT(lockcnt > 0);
1360 if (atomic_cmpset_int(&chain->lockcnt,
1361 lockcnt, lockcnt - 1)) {
1362 hammer2_mtx_unlock(&chain->lock);
1365 } else if (hammer2_mtx_upgrade_try(&chain->lock) == 0) {
1366 /* while holding the mutex exclusively */
1367 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
1371 * This situation can easily occur on SMP due to
1372 * the gap inbetween the 1->0 transition and the
1373 * final unlock. We cannot safely block on the
1374 * mutex because lockcnt might go above 1.
1376 * XXX Sleep for one tick if it takes too long.
1378 if (++iter > 1000) {
1379 if (iter > 1000 + hz) {
1380 kprintf("hammer2: h2race2 %p\n", chain);
1383 tsleep(&iter, 0, "h2race2", 1);
1391 * Last unlock / mutex upgraded to exclusive. Drop the data
1394 dio = hammer2_chain_drop_data(chain);
1396 hammer2_io_bqrelse(&dio);
1397 hammer2_mtx_unlock(&chain->lock);
1401 * Unlock and hold chain data intact
1404 hammer2_chain_unlock_hold(hammer2_chain_t *chain)
1406 atomic_add_int(&chain->lockcnt, 1);
1407 hammer2_chain_unlock(chain);
1411 * Helper to obtain the blockref[] array base and count for a chain.
1413 * XXX Not widely used yet, various use cases need to be validated and
1414 * converted to use this function.
1417 hammer2_blockref_t *
1418 hammer2_chain_base_and_count(hammer2_chain_t *parent, int *countp)
1420 hammer2_blockref_t *base;
1423 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1426 switch(parent->bref.type) {
1427 case HAMMER2_BREF_TYPE_INODE:
1428 count = HAMMER2_SET_COUNT;
1430 case HAMMER2_BREF_TYPE_INDIRECT:
1431 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1432 count = parent->bytes / sizeof(hammer2_blockref_t);
1434 case HAMMER2_BREF_TYPE_VOLUME:
1435 count = HAMMER2_SET_COUNT;
1437 case HAMMER2_BREF_TYPE_FREEMAP:
1438 count = HAMMER2_SET_COUNT;
1441 panic("hammer2_chain_base_and_count: "
1442 "unrecognized blockref type: %d",
1448 switch(parent->bref.type) {
1449 case HAMMER2_BREF_TYPE_INODE:
1450 base = &parent->data->ipdata.u.blockset.blockref[0];
1451 count = HAMMER2_SET_COUNT;
1453 case HAMMER2_BREF_TYPE_INDIRECT:
1454 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1455 base = &parent->data->npdata[0];
1456 count = parent->bytes / sizeof(hammer2_blockref_t);
1458 case HAMMER2_BREF_TYPE_VOLUME:
1459 base = &parent->data->voldata.
1460 sroot_blockset.blockref[0];
1461 count = HAMMER2_SET_COUNT;
1463 case HAMMER2_BREF_TYPE_FREEMAP:
1464 base = &parent->data->blkset.blockref[0];
1465 count = HAMMER2_SET_COUNT;
1468 panic("hammer2_chain_base_and_count: "
1469 "unrecognized blockref type: %d",
1482 * This counts the number of live blockrefs in a block array and
1483 * also calculates the point at which all remaining blockrefs are empty.
1484 * This routine can only be called on a live chain.
1486 * Caller holds the chain locked, but possibly with a shared lock. We
1487 * must use an exclusive spinlock to prevent corruption.
1489 * NOTE: Flag is not set until after the count is complete, allowing
1490 * callers to test the flag without holding the spinlock.
1492 * NOTE: If base is NULL the related chain is still in the INITIAL
1493 * state and there are no blockrefs to count.
1495 * NOTE: live_count may already have some counts accumulated due to
1496 * creation and deletion and could even be initially negative.
1499 hammer2_chain_countbrefs(hammer2_chain_t *chain,
1500 hammer2_blockref_t *base, int count)
1502 hammer2_spin_ex(&chain->core.spin);
1503 if ((chain->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) {
1505 while (--count >= 0) {
1506 if (base[count].type != HAMMER2_BREF_TYPE_EMPTY)
1509 chain->core.live_zero = count + 1;
1510 while (count >= 0) {
1511 if (base[count].type != HAMMER2_BREF_TYPE_EMPTY)
1512 atomic_add_int(&chain->core.live_count,
1517 chain->core.live_zero = 0;
1519 /* else do not modify live_count */
1520 atomic_set_int(&chain->flags, HAMMER2_CHAIN_COUNTEDBREFS);
1522 hammer2_spin_unex(&chain->core.spin);
1526 * Resize the chain's physical storage allocation in-place. This function does
1527 * not usually adjust the data pointer and must be followed by (typically) a
1528 * hammer2_chain_modify() call to copy any old data over and adjust the
1531 * Chains can be resized smaller without reallocating the storage. Resizing
1532 * larger will reallocate the storage. Excess or prior storage is reclaimed
1533 * asynchronously at a later time.
1535 * An nradix value of 0 is special-cased to mean that the storage should
1536 * be disassociated, that is the chain is being resized to 0 bytes (not 1
1539 * Must be passed an exclusively locked parent and chain.
1541 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
1542 * to avoid instantiating a device buffer that conflicts with the vnode data
1543 * buffer. However, because H2 can compress or encrypt data, the chain may
1544 * have a dio assigned to it in those situations, and they do not conflict.
1546 * XXX return error if cannot resize.
1549 hammer2_chain_resize(hammer2_chain_t *chain,
1550 hammer2_tid_t mtid, hammer2_off_t dedup_off,
1551 int nradix, int flags)
1561 * Only data and indirect blocks can be resized for now.
1562 * (The volu root, inodes, and freemap elements use a fixed size).
1564 KKASSERT(chain != &hmp->vchain);
1565 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1566 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1567 chain->bref.type == HAMMER2_BREF_TYPE_DIRENT);
1570 * Nothing to do if the element is already the proper size
1572 obytes = chain->bytes;
1573 nbytes = (nradix) ? (1U << nradix) : 0;
1574 if (obytes == nbytes)
1575 return (chain->error);
1578 * Make sure the old data is instantiated so we can copy it. If this
1579 * is a data block, the device data may be superfluous since the data
1580 * might be in a logical block, but compressed or encrypted data is
1583 * NOTE: The modify will set BMAPUPD for us if BMAPPED is set.
1585 error = hammer2_chain_modify(chain, mtid, dedup_off, 0);
1590 * Reallocate the block, even if making it smaller (because different
1591 * block sizes may be in different regions).
1593 * NOTE: Operation does not copy the data and may only be used
1594 * to resize data blocks in-place, or directory entry blocks
1595 * which are about to be modified in some manner.
1597 error = hammer2_freemap_alloc(chain, nbytes);
1601 chain->bytes = nbytes;
1604 * We don't want the followup chain_modify() to try to copy data
1605 * from the old (wrong-sized) buffer. It won't know how much to
1606 * copy. This case should only occur during writes when the
1607 * originator already has the data to write in-hand.
1610 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1611 chain->bref.type == HAMMER2_BREF_TYPE_DIRENT);
1612 hammer2_io_brelse(&chain->dio);
1615 return (chain->error);
1619 * Set the chain modified so its data can be changed by the caller, or
1620 * install deduplicated data. The caller must call this routine for each
1621 * set of modifications it makes, even if the chain is already flagged
1624 * Sets bref.modify_tid to mtid only if mtid != 0. Note that bref.modify_tid
1625 * is a CLC (cluster level change) field and is not updated by parent
1626 * propagation during a flush.
1628 * Returns an appropriate HAMMER2_ERROR_* code, which will generally reflect
1629 * chain->error except for HAMMER2_ERROR_ENOSPC. If the allocation fails
1630 * due to no space available, HAMMER2_ERROR_ENOSPC is returned and the chain
1631 * remains unmodified with its old data ref intact and chain->error
1636 * If the DEDUPABLE flag is set in the chain the storage must be reallocated
1637 * even if the chain is still flagged MODIFIED. In this case the chain's
1638 * DEDUPABLE flag will be cleared once the new storage has been assigned.
1640 * If the caller passes a non-zero dedup_off we will use it to assign the
1641 * new storage. The MODIFIED flag will be *CLEARED* in this case, and
1642 * DEDUPABLE will be set (NOTE: the UPDATE flag is always set). The caller
1643 * must not modify the data content upon return.
1646 hammer2_chain_modify(hammer2_chain_t *chain, hammer2_tid_t mtid,
1647 hammer2_off_t dedup_off, int flags)
1649 hammer2_blockref_t obref;
1660 obref = chain->bref;
1661 KKASSERT(chain->lock.mtx_lock & MTX_EXCLUSIVE);
1664 * Data is not optional for freemap chains (we must always be sure
1665 * to copy the data on COW storage allocations).
1667 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1668 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1669 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1670 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1674 * Data must be resolved if already assigned, unless explicitly
1675 * flagged otherwise. If we cannot safety load the data the
1676 * modification fails and we return early.
1678 if (chain->data == NULL && chain->bytes != 0 &&
1679 (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1680 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1681 hammer2_chain_load_data(chain);
1683 return (chain->error);
1688 * Set MODIFIED to indicate that the chain has been modified. A new
1689 * allocation is required when modifying a chain.
1691 * Set UPDATE to ensure that the blockref is updated in the parent.
1693 * If MODIFIED is already set determine if we can reuse the assigned
1694 * data block or if we need a new data block.
1696 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1698 * Must set modified bit.
1700 atomic_add_long(&hammer2_count_modified_chains, 1);
1701 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1702 hammer2_pfs_memory_inc(chain->pmp); /* can be NULL */
1706 * We may be able to avoid a copy-on-write if the chain's
1707 * check mode is set to NONE and the chain's current
1708 * modify_tid is beyond the last explicit snapshot tid.
1710 * This implements HAMMER2's overwrite-in-place feature.
1712 * NOTE! This data-block cannot be used as a de-duplication
1713 * source when the check mode is set to NONE.
1715 if ((chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1716 chain->bref.type == HAMMER2_BREF_TYPE_DIRENT) &&
1717 (chain->flags & HAMMER2_CHAIN_INITIAL) == 0 &&
1718 (chain->flags & HAMMER2_CHAIN_DEDUPABLE) == 0 &&
1719 HAMMER2_DEC_CHECK(chain->bref.methods) ==
1720 HAMMER2_CHECK_NONE &&
1722 chain->bref.modify_tid >
1723 chain->pmp->iroot->meta.pfs_lsnap_tid) {
1725 * Sector overwrite allowed.
1728 } else if ((hmp->hflags & HMNT2_EMERG) &&
1730 chain->bref.modify_tid >
1731 chain->pmp->iroot->meta.pfs_lsnap_tid) {
1733 * If in emergency delete mode then do a modify-in-
1734 * place on any chain type belonging to the PFS as
1735 * long as it doesn't mess up a snapshot. We might
1736 * be forced to do this anyway a little further down
1737 * in the code if the allocation fails.
1739 * Also note that in emergency mode, these modify-in-
1740 * place operations are NOT SAFE. A storage failure,
1741 * power failure, or panic can corrupt the filesystem.
1746 * Sector overwrite not allowed, must copy-on-write.
1750 } else if (chain->flags & HAMMER2_CHAIN_DEDUPABLE) {
1752 * If the modified chain was registered for dedup we need
1753 * a new allocation. This only happens for delayed-flush
1754 * chains (i.e. which run through the front-end buffer
1761 * Already flagged modified, no new allocation is needed.
1768 * Flag parent update required.
1770 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
1771 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1778 * The XOP code returns held but unlocked focus chains. This
1779 * prevents the chain from being destroyed but does not prevent
1780 * it from being modified. diolk is used to interlock modifications
1781 * against XOP frontend accesses to the focus.
1783 * This allows us to theoretically avoid deadlocking the frontend
1784 * if one of the backends lock up by not formally locking the
1785 * focused chain in the frontend. In addition, the synchronization
1786 * code relies on this mechanism to avoid deadlocking concurrent
1787 * synchronization threads.
1789 lockmgr(&chain->diolk, LK_EXCLUSIVE);
1792 * The modification or re-modification requires an allocation and
1793 * possible COW. If an error occurs, the previous content and data
1794 * reference is retained and the modification fails.
1796 * If dedup_off is non-zero, the caller is requesting a deduplication
1797 * rather than a modification. The MODIFIED bit is not set and the
1798 * data offset is set to the deduplication offset. The data cannot
1801 * NOTE: The dedup offset is allowed to be in a partially free state
1802 * and we must be sure to reset it to a fully allocated state
1803 * to force two bulkfree passes to free it again.
1805 * NOTE: Only applicable when chain->bytes != 0.
1807 * XXX can a chain already be marked MODIFIED without a data
1808 * assignment? If not, assert here instead of testing the case.
1810 if (chain != &hmp->vchain && chain != &hmp->fchain &&
1812 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1816 * NOTE: We do not have to remove the dedup
1817 * registration because the area is still
1818 * allocated and the underlying DIO will
1822 chain->bref.data_off = dedup_off;
1823 chain->bytes = 1 << (dedup_off &
1824 HAMMER2_OFF_MASK_RADIX);
1826 atomic_clear_int(&chain->flags,
1827 HAMMER2_CHAIN_MODIFIED);
1828 atomic_add_long(&hammer2_count_modified_chains,
1831 hammer2_pfs_memory_wakeup(
1834 hammer2_freemap_adjust(hmp, &chain->bref,
1835 HAMMER2_FREEMAP_DORECOVER);
1836 atomic_set_int(&chain->flags,
1837 HAMMER2_CHAIN_DEDUPABLE);
1839 error = hammer2_freemap_alloc(chain,
1841 atomic_clear_int(&chain->flags,
1842 HAMMER2_CHAIN_DEDUPABLE);
1845 * If we are unable to allocate a new block
1846 * but we are in emergency mode, issue a
1847 * warning to the console and reuse the same
1850 * We behave as if the allocation were
1853 * THIS IS IMPORTANT: These modifications
1854 * are virtually guaranteed to corrupt any
1855 * snapshots related to this filesystem.
1857 if (error && (hmp->hflags & HMNT2_EMERG)) {
1859 chain->bref.flags |=
1860 HAMMER2_BREF_FLAG_EMERG_MIP;
1862 krateprintf(&krate_h2em,
1863 "hammer2: Emergency Mode WARNING: "
1864 "Operation will likely corrupt "
1865 "related snapshot: "
1866 "%016jx.%02x key=%016jx\n",
1867 chain->bref.data_off,
1870 } else if (error == 0) {
1871 chain->bref.flags &=
1872 ~HAMMER2_BREF_FLAG_EMERG_MIP;
1879 * Stop here if error. We have to undo any flag bits we might
1884 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1885 atomic_add_long(&hammer2_count_modified_chains, -1);
1887 hammer2_pfs_memory_wakeup(chain->pmp, -1);
1890 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1892 lockmgr(&chain->diolk, LK_RELEASE);
1898 * Update mirror_tid and modify_tid. modify_tid is only updated
1899 * if not passed as zero (during flushes, parent propagation passes
1902 * NOTE: chain->pmp could be the device spmp.
1904 chain->bref.mirror_tid = hmp->voldata.mirror_tid + 1;
1906 chain->bref.modify_tid = mtid;
1909 * Set BMAPUPD to tell the flush code that an existing blockmap entry
1910 * requires updating as well as to tell the delete code that the
1911 * chain's blockref might not exactly match (in terms of physical size
1912 * or block offset) the one in the parent's blocktable. The base key
1913 * of course will still match.
1915 if (chain->flags & HAMMER2_CHAIN_BMAPPED)
1916 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD);
1919 * Short-cut data block handling when the caller does not need an
1920 * actual data reference to (aka OPTDATA), as long as the chain does
1921 * not already have a data pointer to the data and no de-duplication
1924 * This generally means that the modifications are being done via the
1925 * logical buffer cache.
1927 * NOTE: If deduplication occurred we have to run through the data
1928 * stuff to clear INITIAL, and the caller will likely want to
1929 * assign the check code anyway. Leaving INITIAL set on a
1930 * dedup can be deadly (it can cause the block to be zero'd!).
1932 * This code also handles bytes == 0 (most dirents).
1934 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA &&
1935 (flags & HAMMER2_MODIFY_OPTDATA) &&
1936 chain->data == NULL) {
1937 if (dedup_off == 0) {
1938 KKASSERT(chain->dio == NULL);
1944 * Clearing the INITIAL flag (for indirect blocks) indicates that
1945 * we've processed the uninitialized storage allocation.
1947 * If this flag is already clear we are likely in a copy-on-write
1948 * situation but we have to be sure NOT to bzero the storage if
1949 * no data is present.
1951 * Clearing of NOTTESTED is allowed if the MODIFIED bit is set,
1953 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1954 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1961 * Instantiate data buffer and possibly execute COW operation
1963 switch(chain->bref.type) {
1964 case HAMMER2_BREF_TYPE_VOLUME:
1965 case HAMMER2_BREF_TYPE_FREEMAP:
1967 * The data is embedded, no copy-on-write operation is
1970 KKASSERT(chain->dio == NULL);
1972 case HAMMER2_BREF_TYPE_DIRENT:
1974 * The data might be fully embedded.
1976 if (chain->bytes == 0) {
1977 KKASSERT(chain->dio == NULL);
1981 case HAMMER2_BREF_TYPE_INODE:
1982 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1983 case HAMMER2_BREF_TYPE_DATA:
1984 case HAMMER2_BREF_TYPE_INDIRECT:
1985 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1987 * Perform the copy-on-write operation
1989 * zero-fill or copy-on-write depending on whether
1990 * chain->data exists or not and set the dirty state for
1991 * the new buffer. hammer2_io_new() will handle the
1994 * If a dedup_off was supplied this is an existing block
1995 * and no COW, copy, or further modification is required.
1997 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1999 if (wasinitial && dedup_off == 0) {
2000 error = hammer2_io_new(hmp, chain->bref.type,
2001 chain->bref.data_off,
2002 chain->bytes, &dio);
2004 error = hammer2_io_bread(hmp, chain->bref.type,
2005 chain->bref.data_off,
2006 chain->bytes, &dio);
2008 hammer2_adjreadcounter(chain->bref.type, chain->bytes);
2011 * If an I/O error occurs make sure callers cannot accidently
2012 * modify the old buffer's contents and corrupt the filesystem.
2014 * NOTE: hammer2_io_data() call issues bkvasync()
2017 kprintf("hammer2_chain_modify: hmp=%p I/O error\n",
2019 chain->error = HAMMER2_ERROR_EIO;
2020 hammer2_io_brelse(&dio);
2021 hammer2_io_brelse(&chain->dio);
2026 bdata = hammer2_io_data(dio, chain->bref.data_off);
2030 * COW (unless a dedup).
2032 KKASSERT(chain->dio != NULL);
2033 if (chain->data != (void *)bdata && dedup_off == 0) {
2034 bcopy(chain->data, bdata, chain->bytes);
2036 } else if (wasinitial == 0 && dedup_off == 0) {
2038 * We have a problem. We were asked to COW but
2039 * we don't have any data to COW with!
2041 panic("hammer2_chain_modify: having a COW %p\n",
2046 * Retire the old buffer, replace with the new. Dirty or
2047 * redirty the new buffer.
2049 * WARNING! The system buffer cache may have already flushed
2050 * the buffer, so we must be sure to [re]dirty it
2051 * for further modification.
2053 * If dedup_off was supplied, the caller is not
2054 * expected to make any further modification to the
2057 * WARNING! hammer2_get_gdata() assumes dio never transitions
2058 * through NULL in order to optimize away unnecessary
2064 if ((tio = chain->dio) != NULL)
2065 hammer2_io_bqrelse(&tio);
2066 chain->data = (void *)bdata;
2069 hammer2_io_setdirty(dio);
2073 panic("hammer2_chain_modify: illegal non-embedded type %d",
2080 * setflush on parent indicating that the parent must recurse down
2081 * to us. Do not call on chain itself which might already have it
2085 hammer2_chain_setflush(chain->parent);
2086 lockmgr(&chain->diolk, LK_RELEASE);
2088 return (chain->error);
2092 * Modify the chain associated with an inode.
2095 hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
2096 hammer2_tid_t mtid, int flags)
2100 hammer2_inode_modify(ip);
2101 error = hammer2_chain_modify(chain, mtid, 0, flags);
2107 * Volume header data locks
2110 hammer2_voldata_lock(hammer2_dev_t *hmp)
2112 lockmgr(&hmp->vollk, LK_EXCLUSIVE);
2116 hammer2_voldata_unlock(hammer2_dev_t *hmp)
2118 lockmgr(&hmp->vollk, LK_RELEASE);
2122 hammer2_voldata_modify(hammer2_dev_t *hmp)
2124 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
2125 atomic_add_long(&hammer2_count_modified_chains, 1);
2126 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
2127 hammer2_pfs_memory_inc(hmp->vchain.pmp);
2132 * This function returns the chain at the nearest key within the specified
2133 * range. The returned chain will be referenced but not locked.
2135 * This function will recurse through chain->rbtree as necessary and will
2136 * return a *key_nextp suitable for iteration. *key_nextp is only set if
2137 * the iteration value is less than the current value of *key_nextp.
2139 * The caller should use (*key_nextp) to calculate the actual range of
2140 * the returned element, which will be (key_beg to *key_nextp - 1), because
2141 * there might be another element which is superior to the returned element
2144 * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
2145 * chains continue to be returned. On EOF (*key_nextp) may overflow since
2146 * it will wind up being (key_end + 1).
2148 * WARNING! Must be called with child's spinlock held. Spinlock remains
2149 * held through the operation.
2151 struct hammer2_chain_find_info {
2152 hammer2_chain_t *best;
2153 hammer2_key_t key_beg;
2154 hammer2_key_t key_end;
2155 hammer2_key_t key_next;
2158 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
2159 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
2163 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
2164 hammer2_key_t key_beg, hammer2_key_t key_end)
2166 struct hammer2_chain_find_info info;
2169 info.key_beg = key_beg;
2170 info.key_end = key_end;
2171 info.key_next = *key_nextp;
2173 RB_SCAN(hammer2_chain_tree, &parent->core.rbtree,
2174 hammer2_chain_find_cmp, hammer2_chain_find_callback,
2176 *key_nextp = info.key_next;
2178 kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
2179 parent, key_beg, key_end, *key_nextp);
2187 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
2189 struct hammer2_chain_find_info *info = data;
2190 hammer2_key_t child_beg;
2191 hammer2_key_t child_end;
2193 child_beg = child->bref.key;
2194 child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
2196 if (child_end < info->key_beg)
2198 if (child_beg > info->key_end)
2205 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
2207 struct hammer2_chain_find_info *info = data;
2208 hammer2_chain_t *best;
2209 hammer2_key_t child_end;
2212 * WARNING! Layerq is scanned forwards, exact matches should keep
2213 * the existing info->best.
2215 if ((best = info->best) == NULL) {
2217 * No previous best. Assign best
2220 } else if (best->bref.key <= info->key_beg &&
2221 child->bref.key <= info->key_beg) {
2226 /*info->best = child;*/
2227 } else if (child->bref.key < best->bref.key) {
2229 * Child has a nearer key and best is not flush with key_beg.
2230 * Set best to child. Truncate key_next to the old best key.
2233 if (info->key_next > best->bref.key || info->key_next == 0)
2234 info->key_next = best->bref.key;
2235 } else if (child->bref.key == best->bref.key) {
2237 * If our current best is flush with the child then this
2238 * is an illegal overlap.
2240 * key_next will automatically be limited to the smaller of
2241 * the two end-points.
2247 * Keep the current best but truncate key_next to the child's
2250 * key_next will also automatically be limited to the smaller
2251 * of the two end-points (probably not necessary for this case
2252 * but we do it anyway).
2254 if (info->key_next > child->bref.key || info->key_next == 0)
2255 info->key_next = child->bref.key;
2259 * Always truncate key_next based on child's end-of-range.
2261 child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
2262 if (child_end && (info->key_next > child_end || info->key_next == 0))
2263 info->key_next = child_end;
2269 * Retrieve the specified chain from a media blockref, creating the
2270 * in-memory chain structure which reflects it. The returned chain is
2271 * held and locked according to (how) (HAMMER2_RESOLVE_*). The caller must
2272 * handle crc-checks and so forth, and should check chain->error before
2273 * assuming that the data is good.
2275 * To handle insertion races pass the INSERT_RACE flag along with the
2276 * generation number of the core. NULL will be returned if the generation
2277 * number changes before we have a chance to insert the chain. Insert
2278 * races can occur because the parent might be held shared.
2280 * Caller must hold the parent locked shared or exclusive since we may
2281 * need the parent's bref array to find our block.
2283 * WARNING! chain->pmp is always set to NULL for any chain representing
2284 * part of the super-root topology.
2287 hammer2_chain_get(hammer2_chain_t *parent, int generation,
2288 hammer2_blockref_t *bref, int how)
2290 hammer2_dev_t *hmp = parent->hmp;
2291 hammer2_chain_t *chain;
2295 * Allocate a chain structure representing the existing media
2296 * entry. Resulting chain has one ref and is not locked.
2298 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
2299 chain = hammer2_chain_alloc(hmp, NULL, bref);
2301 chain = hammer2_chain_alloc(hmp, parent->pmp, bref);
2302 /* ref'd chain returned */
2305 * Flag that the chain is in the parent's blockmap so delete/flush
2306 * knows what to do with it.
2308 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
2311 * chain must be locked to avoid unexpected ripouts
2313 hammer2_chain_lock(chain, how);
2316 * Link the chain into its parent. A spinlock is required to safely
2317 * access the RBTREE, and it is possible to collide with another
2318 * hammer2_chain_get() operation because the caller might only hold
2319 * a shared lock on the parent.
2321 * NOTE: Get races can occur quite often when we distribute
2322 * asynchronous read-aheads across multiple threads.
2324 KKASSERT(parent->refs > 0);
2325 error = hammer2_chain_insert(parent, chain,
2326 HAMMER2_CHAIN_INSERT_SPIN |
2327 HAMMER2_CHAIN_INSERT_RACE,
2330 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
2331 /*kprintf("chain %p get race\n", chain);*/
2332 hammer2_chain_unlock(chain);
2333 hammer2_chain_drop(chain);
2336 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
2340 * Return our new chain referenced but not locked, or NULL if
2347 * Lookup initialization/completion API
2350 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
2352 hammer2_chain_ref(parent);
2353 if (flags & HAMMER2_LOOKUP_SHARED) {
2354 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
2355 HAMMER2_RESOLVE_SHARED);
2357 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2363 hammer2_chain_lookup_done(hammer2_chain_t *parent)
2366 hammer2_chain_unlock(parent);
2367 hammer2_chain_drop(parent);
2372 * Take the locked chain and return a locked parent. The chain remains
2373 * locked on return, but may have to be temporarily unlocked to acquire
2374 * the parent. Because of this, (chain) must be stable and cannot be
2375 * deleted while it was temporarily unlocked (typically means that (chain)
2378 * Pass HAMMER2_RESOLVE_* flags in flags.
2380 * This will work even if the chain is errored, and the caller can check
2381 * parent->error on return if desired since the parent will be locked.
2383 * This function handles the lock order reversal.
2386 hammer2_chain_getparent(hammer2_chain_t *chain, int flags)
2388 hammer2_chain_t *parent;
2391 * Be careful of order, chain must be unlocked before parent
2392 * is locked below to avoid a deadlock. Try it trivially first.
2394 parent = chain->parent;
2396 panic("hammer2_chain_getparent: no parent");
2397 hammer2_chain_ref(parent);
2398 if (hammer2_chain_lock(parent, flags|HAMMER2_RESOLVE_NONBLOCK) == 0)
2402 hammer2_chain_unlock(chain);
2403 hammer2_chain_lock(parent, flags);
2404 hammer2_chain_lock(chain, flags);
2407 * Parent relinking races are quite common. We have to get
2408 * it right or we will blow up the block table.
2410 if (chain->parent == parent)
2412 hammer2_chain_unlock(parent);
2413 hammer2_chain_drop(parent);
2415 parent = chain->parent;
2417 panic("hammer2_chain_getparent: no parent");
2418 hammer2_chain_ref(parent);
2424 * Take the locked chain and return a locked parent. The chain is unlocked
2425 * and dropped. *chainp is set to the returned parent as a convenience.
2426 * Pass HAMMER2_RESOLVE_* flags in flags.
2428 * This will work even if the chain is errored, and the caller can check
2429 * parent->error on return if desired since the parent will be locked.
2431 * The chain does NOT need to be stable. We use a tracking structure
2432 * to track the expected parent if the chain is deleted out from under us.
2434 * This function handles the lock order reversal.
2437 hammer2_chain_repparent(hammer2_chain_t **chainp, int flags)
2439 hammer2_chain_t *chain;
2440 hammer2_chain_t *parent;
2441 struct hammer2_reptrack reptrack;
2442 struct hammer2_reptrack **repp;
2445 * Be careful of order, chain must be unlocked before parent
2446 * is locked below to avoid a deadlock. Try it trivially first.
2449 parent = chain->parent;
2450 if (parent == NULL) {
2451 hammer2_spin_unex(&chain->core.spin);
2452 panic("hammer2_chain_repparent: no parent");
2454 hammer2_chain_ref(parent);
2455 if (hammer2_chain_lock(parent, flags|HAMMER2_RESOLVE_NONBLOCK) == 0) {
2456 hammer2_chain_unlock(chain);
2457 hammer2_chain_drop(chain);
2464 * Ok, now it gets a bit nasty. There are multiple situations where
2465 * the parent might be in the middle of a deletion, or where the child
2466 * (chain) might be deleted the instant we let go of its lock.
2467 * We can potentially end up in a no-win situation!
2469 * In particular, the indirect_maintenance() case can cause these
2472 * To deal with this we install a reptrack structure in the parent
2473 * This reptrack structure 'owns' the parent ref and will automatically
2474 * migrate to the parent's parent if the parent is deleted permanently.
2476 hammer2_spin_init(&reptrack.spin, "h2reptrk");
2477 reptrack.chain = parent;
2478 hammer2_chain_ref(parent); /* for the reptrack */
2480 hammer2_spin_ex(&parent->core.spin);
2481 reptrack.next = parent->core.reptrack;
2482 parent->core.reptrack = &reptrack;
2483 hammer2_spin_unex(&parent->core.spin);
2485 hammer2_chain_unlock(chain);
2486 hammer2_chain_drop(chain);
2487 chain = NULL; /* gone */
2490 * At the top of this loop, chain is gone and parent is refd both
2491 * by us explicitly AND via our reptrack. We are attempting to
2495 hammer2_chain_lock(parent, flags);
2497 if (reptrack.chain == parent)
2499 hammer2_chain_unlock(parent);
2500 hammer2_chain_drop(parent);
2502 kprintf("hammer2: debug REPTRACK %p->%p\n",
2503 parent, reptrack.chain);
2504 hammer2_spin_ex(&reptrack.spin);
2505 parent = reptrack.chain;
2506 hammer2_chain_ref(parent);
2507 hammer2_spin_unex(&reptrack.spin);
2511 * Once parent is locked and matches our reptrack, our reptrack
2512 * will be stable and we have our parent. We can unlink our
2515 * WARNING! Remember that the chain lock might be shared. Chains
2516 * locked shared have stable parent linkages.
2518 hammer2_spin_ex(&parent->core.spin);
2519 repp = &parent->core.reptrack;
2520 while (*repp != &reptrack)
2521 repp = &(*repp)->next;
2522 *repp = reptrack.next;
2523 hammer2_spin_unex(&parent->core.spin);
2525 hammer2_chain_drop(parent); /* reptrack ref */
2526 *chainp = parent; /* return parent lock+ref */
2532 * Dispose of any linked reptrack structures in (chain) by shifting them to
2533 * (parent). Both (chain) and (parent) must be exclusively locked.
2535 * This is interlocked against any children of (chain) on the other side.
2536 * No children so remain as-of when this is called so we can test
2537 * core.reptrack without holding the spin-lock.
2539 * Used whenever the caller intends to permanently delete chains related
2540 * to topological recursions (BREF_TYPE_INDIRECT, BREF_TYPE_FREEMAP_NODE),
2541 * where the chains underneath the node being deleted are given a new parent
2542 * above the node being deleted.
2546 hammer2_chain_repchange(hammer2_chain_t *parent, hammer2_chain_t *chain)
2548 struct hammer2_reptrack *reptrack;
2550 KKASSERT(chain->core.live_count == 0 && RB_EMPTY(&chain->core.rbtree));
2551 while (chain->core.reptrack) {
2552 hammer2_spin_ex(&parent->core.spin);
2553 hammer2_spin_ex(&chain->core.spin);
2554 reptrack = chain->core.reptrack;
2555 if (reptrack == NULL) {
2556 hammer2_spin_unex(&chain->core.spin);
2557 hammer2_spin_unex(&parent->core.spin);
2560 hammer2_spin_ex(&reptrack->spin);
2561 chain->core.reptrack = reptrack->next;
2562 reptrack->chain = parent;
2563 reptrack->next = parent->core.reptrack;
2564 parent->core.reptrack = reptrack;
2565 hammer2_chain_ref(parent); /* reptrack */
2567 hammer2_spin_unex(&chain->core.spin);
2568 hammer2_spin_unex(&parent->core.spin);
2569 kprintf("hammer2: debug repchange %p %p->%p\n",
2570 reptrack, chain, parent);
2571 hammer2_chain_drop(chain); /* reptrack */
2576 * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
2577 * (*parentp) typically points to an inode but can also point to a related
2578 * indirect block and this function will recurse upwards and find the inode
2579 * or the nearest undeleted indirect block covering the key range.
2581 * This function unconditionally sets *errorp, replacing any previous value.
2583 * (*parentp) must be exclusive or shared locked (depending on flags) and
2584 * referenced and can be an inode or an existing indirect block within the
2587 * If (*parent) is errored out, this function will not attempt to recurse
2588 * the radix tree and will return NULL along with an appropriate *errorp.
2589 * If NULL is returned and *errorp is 0, the requested lookup could not be
2592 * On return (*parentp) will be modified to point at the deepest parent chain
2593 * element encountered during the search, as a helper for an insertion or
2596 * The new (*parentp) will be locked shared or exclusive (depending on flags),
2597 * and referenced, and the old will be unlocked and dereferenced (no change
2598 * if they are both the same). This is particularly important if the caller
2599 * wishes to insert a new chain, (*parentp) will be set properly even if NULL
2600 * is returned, as long as no error occurred.
2602 * The matching chain will be returned locked according to flags.
2606 * NULL is returned if no match was found, but (*parentp) will still
2607 * potentially be adjusted.
2609 * On return (*key_nextp) will point to an iterative value for key_beg.
2610 * (If NULL is returned (*key_nextp) is set to (key_end + 1)).
2612 * This function will also recurse up the chain if the key is not within the
2613 * current parent's range. (*parentp) can never be set to NULL. An iteration
2614 * can simply allow (*parentp) to float inside the loop.
2616 * NOTE! chain->data is not always resolved. By default it will not be
2617 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
2618 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
2619 * BREF_TYPE_DATA as the device buffer can alias the logical file
2624 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
2625 hammer2_key_t key_beg, hammer2_key_t key_end,
2626 int *errorp, int flags)
2629 hammer2_chain_t *parent;
2630 hammer2_chain_t *chain;
2631 hammer2_blockref_t *base;
2632 hammer2_blockref_t *bref;
2633 hammer2_blockref_t bsave;
2634 hammer2_key_t scan_beg;
2635 hammer2_key_t scan_end;
2637 int how_always = HAMMER2_RESOLVE_ALWAYS;
2638 int how_maybe = HAMMER2_RESOLVE_MAYBE;
2641 int maxloops = 300000;
2642 volatile hammer2_mtx_t save_mtx;
2644 if (flags & HAMMER2_LOOKUP_ALWAYS) {
2645 how_maybe = how_always;
2646 how = HAMMER2_RESOLVE_ALWAYS;
2647 } else if (flags & HAMMER2_LOOKUP_NODATA) {
2648 how = HAMMER2_RESOLVE_NEVER;
2650 how = HAMMER2_RESOLVE_MAYBE;
2652 if (flags & HAMMER2_LOOKUP_SHARED) {
2653 how_maybe |= HAMMER2_RESOLVE_SHARED;
2654 how_always |= HAMMER2_RESOLVE_SHARED;
2655 how |= HAMMER2_RESOLVE_SHARED;
2659 * Recurse (*parentp) upward if necessary until the parent completely
2660 * encloses the key range or we hit the inode.
2662 * Handle races against the flusher deleting indirect nodes on its
2663 * way back up by continuing to recurse upward past the deletion.
2669 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2670 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2671 scan_beg = parent->bref.key;
2672 scan_end = scan_beg +
2673 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2674 if ((parent->flags & HAMMER2_CHAIN_DELETED) == 0) {
2675 if (key_beg >= scan_beg && key_end <= scan_end)
2678 parent = hammer2_chain_repparent(parentp, how_maybe);
2681 if (--maxloops == 0)
2682 panic("hammer2_chain_lookup: maxloops");
2685 * MATCHIND case that does not require parent->data (do prior to
2686 * parent->error check).
2688 switch(parent->bref.type) {
2689 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2690 case HAMMER2_BREF_TYPE_INDIRECT:
2691 if (flags & HAMMER2_LOOKUP_MATCHIND) {
2692 scan_beg = parent->bref.key;
2693 scan_end = scan_beg +
2694 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2695 if (key_beg == scan_beg && key_end == scan_end) {
2697 hammer2_chain_ref(chain);
2698 hammer2_chain_lock(chain, how_maybe);
2699 *key_nextp = scan_end + 1;
2709 * No lookup is possible if the parent is errored. We delayed
2710 * this check as long as we could to ensure that the parent backup,
2711 * embedded data, and MATCHIND code could still execute.
2713 if (parent->error) {
2714 *errorp = parent->error;
2719 * Locate the blockref array. Currently we do a fully associative
2720 * search through the array.
2722 switch(parent->bref.type) {
2723 case HAMMER2_BREF_TYPE_INODE:
2725 * Special shortcut for embedded data returns the inode
2726 * itself. Callers must detect this condition and access
2727 * the embedded data (the strategy code does this for us).
2729 * This is only applicable to regular files and softlinks.
2731 * We need a second lock on parent. Since we already have
2732 * a lock we must pass LOCKAGAIN to prevent unexpected
2733 * blocking (we don't want to block on a second shared
2734 * ref if an exclusive lock is pending)
2736 if (parent->data->ipdata.meta.op_flags &
2737 HAMMER2_OPFLAG_DIRECTDATA) {
2738 if (flags & HAMMER2_LOOKUP_NODIRECT) {
2740 *key_nextp = key_end + 1;
2743 hammer2_chain_ref(parent);
2744 hammer2_chain_lock(parent, how_always |
2745 HAMMER2_RESOLVE_LOCKAGAIN);
2746 *key_nextp = key_end + 1;
2749 base = &parent->data->ipdata.u.blockset.blockref[0];
2750 count = HAMMER2_SET_COUNT;
2752 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2753 case HAMMER2_BREF_TYPE_INDIRECT:
2755 * Optimize indirect blocks in the INITIAL state to avoid
2758 * Debugging: Enter permanent wait state instead of
2759 * panicing on unexpectedly NULL data for the moment.
2761 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2764 if (parent->data == NULL) {
2765 kprintf("hammer2: unexpected NULL data "
2768 tsleep(parent, 0, "xxx", 0);
2770 base = &parent->data->npdata[0];
2772 count = parent->bytes / sizeof(hammer2_blockref_t);
2774 case HAMMER2_BREF_TYPE_VOLUME:
2775 base = &parent->data->voldata.sroot_blockset.blockref[0];
2776 count = HAMMER2_SET_COUNT;
2778 case HAMMER2_BREF_TYPE_FREEMAP:
2779 base = &parent->data->blkset.blockref[0];
2780 count = HAMMER2_SET_COUNT;
2783 panic("hammer2_chain_lookup: unrecognized "
2784 "blockref(B) type: %d",
2786 base = NULL; /* safety */
2787 count = 0; /* safety */
2792 * Merged scan to find next candidate.
2794 * hammer2_base_*() functions require the parent->core.live_* fields
2795 * to be synchronized.
2797 * We need to hold the spinlock to access the block array and RB tree
2798 * and to interlock chain creation.
2800 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2801 hammer2_chain_countbrefs(parent, base, count);
2806 hammer2_spin_ex(&parent->core.spin);
2807 chain = hammer2_combined_find(parent, base, count,
2811 generation = parent->core.generation;
2814 * Exhausted parent chain, iterate.
2817 KKASSERT(chain == NULL);
2818 hammer2_spin_unex(&parent->core.spin);
2819 if (key_beg == key_end) /* short cut single-key case */
2823 * Stop if we reached the end of the iteration.
2825 if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
2826 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2831 * Calculate next key, stop if we reached the end of the
2832 * iteration, otherwise go up one level and loop.
2834 key_beg = parent->bref.key +
2835 ((hammer2_key_t)1 << parent->bref.keybits);
2836 if (key_beg == 0 || key_beg > key_end)
2838 parent = hammer2_chain_repparent(parentp, how_maybe);
2843 * Selected from blockref or in-memory chain.
2846 if (chain == NULL) {
2847 hammer2_spin_unex(&parent->core.spin);
2848 if (bsave.type == HAMMER2_BREF_TYPE_INDIRECT ||
2849 bsave.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2850 chain = hammer2_chain_get(parent, generation,
2853 chain = hammer2_chain_get(parent, generation,
2859 hammer2_chain_ref(chain);
2860 hammer2_spin_unex(&parent->core.spin);
2863 * chain is referenced but not locked. We must lock the
2864 * chain to obtain definitive state.
2866 if (bsave.type == HAMMER2_BREF_TYPE_INDIRECT ||
2867 bsave.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2868 hammer2_chain_lock(chain, how_maybe);
2870 hammer2_chain_lock(chain, how);
2872 KKASSERT(chain->parent == parent);
2874 if (bcmp(&bsave, &chain->bref, sizeof(bsave)) ||
2875 chain->parent != parent) {
2876 hammer2_chain_unlock(chain);
2877 hammer2_chain_drop(chain);
2878 chain = NULL; /* SAFETY */
2884 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2886 * NOTE: Chain's key range is not relevant as there might be
2887 * one-offs within the range that are not deleted.
2889 * NOTE: Lookups can race delete-duplicate because
2890 * delete-duplicate does not lock the parent's core
2891 * (they just use the spinlock on the core).
2893 if (chain->flags & HAMMER2_CHAIN_DELETED) {
2894 kprintf("skip deleted chain %016jx.%02x key=%016jx\n",
2895 chain->bref.data_off, chain->bref.type,
2897 hammer2_chain_unlock(chain);
2898 hammer2_chain_drop(chain);
2899 chain = NULL; /* SAFETY */
2900 key_beg = *key_nextp;
2901 if (key_beg == 0 || key_beg > key_end)
2907 * If the chain element is an indirect block it becomes the new
2908 * parent and we loop on it. We must maintain our top-down locks
2909 * to prevent the flusher from interfering (i.e. doing a
2910 * delete-duplicate and leaving us recursing down a deleted chain).
2912 * The parent always has to be locked with at least RESOLVE_MAYBE
2913 * so we can access its data. It might need a fixup if the caller
2914 * passed incompatible flags. Be careful not to cause a deadlock
2915 * as a data-load requires an exclusive lock.
2917 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
2918 * range is within the requested key range we return the indirect
2919 * block and do NOT loop. This is usually only used to acquire
2922 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2923 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2924 save_mtx = parent->lock;
2925 hammer2_chain_unlock(parent);
2926 hammer2_chain_drop(parent);
2927 *parentp = parent = chain;
2928 chain = NULL; /* SAFETY */
2933 * All done, return the locked chain.
2935 * If the caller does not want a locked chain, replace the lock with
2936 * a ref. Perhaps this can eventually be optimized to not obtain the
2937 * lock in the first place for situations where the data does not
2938 * need to be resolved.
2940 * NOTE! A chain->error must be tested by the caller upon return.
2941 * *errorp is only set based on issues which occur while
2942 * trying to reach the chain.
2948 * After having issued a lookup we can iterate all matching keys.
2950 * If chain is non-NULL we continue the iteration from just after it's index.
2952 * If chain is NULL we assume the parent was exhausted and continue the
2953 * iteration at the next parent.
2955 * If a fatal error occurs (typically an I/O error), a dummy chain is
2956 * returned with chain->error and error-identifying information set. This
2957 * chain will assert if you try to do anything fancy with it.
2959 * XXX Depending on where the error occurs we should allow continued iteration.
2961 * parent must be locked on entry and remains locked throughout. chain's
2962 * lock status must match flags. Chain is always at least referenced.
2964 * WARNING! The MATCHIND flag does not apply to this function.
2967 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
2968 hammer2_key_t *key_nextp,
2969 hammer2_key_t key_beg, hammer2_key_t key_end,
2970 int *errorp, int flags)
2972 hammer2_chain_t *parent;
2976 * Calculate locking flags for upward recursion.
2978 how_maybe = HAMMER2_RESOLVE_MAYBE;
2979 if (flags & HAMMER2_LOOKUP_SHARED)
2980 how_maybe |= HAMMER2_RESOLVE_SHARED;
2986 * Calculate the next index and recalculate the parent if necessary.
2989 key_beg = chain->bref.key +
2990 ((hammer2_key_t)1 << chain->bref.keybits);
2991 hammer2_chain_unlock(chain);
2992 hammer2_chain_drop(chain);
2995 * chain invalid past this point, but we can still do a
2996 * pointer comparison w/parent.
2998 * Any scan where the lookup returned degenerate data embedded
2999 * in the inode has an invalid index and must terminate.
3001 if (chain == parent)
3003 if (key_beg == 0 || key_beg > key_end)
3006 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
3007 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
3009 * We reached the end of the iteration.
3014 * Continue iteration with next parent unless the current
3015 * parent covers the range.
3017 * (This also handles the case of a deleted, empty indirect
3020 key_beg = parent->bref.key +
3021 ((hammer2_key_t)1 << parent->bref.keybits);
3022 if (key_beg == 0 || key_beg > key_end)
3024 parent = hammer2_chain_repparent(parentp, how_maybe);
3030 return (hammer2_chain_lookup(parentp, key_nextp,
3036 * Caller wishes to iterate chains under parent, loading new chains into
3037 * chainp. Caller must initialize *chainp to NULL and *firstp to 1, and
3038 * then call hammer2_chain_scan() repeatedly until a non-zero return.
3039 * During the scan, *firstp will be set to 0 and (*chainp) will be replaced
3040 * with the returned chain for the scan. The returned *chainp will be
3041 * locked and referenced. Any prior contents will be unlocked and dropped.
3043 * Caller should check the return value. A normal scan EOF will return
3044 * exactly HAMMER2_ERROR_EOF. Any other non-zero value indicates an
3045 * error trying to access parent data. Any error in the returned chain
3046 * must be tested separately by the caller.
3048 * (*chainp) is dropped on each scan, but will only be set if the returned
3049 * element itself can recurse. Leaf elements are NOT resolved, loaded, or
3050 * returned via *chainp. The caller will get their bref only.
3052 * The raw scan function is similar to lookup/next but does not seek to a key.
3053 * Blockrefs are iterated via first_bref = (parent, NULL) and
3054 * next_chain = (parent, bref).
3056 * The passed-in parent must be locked and its data resolved. The function
3057 * nominally returns a locked and referenced *chainp != NULL for chains
3058 * the caller might need to recurse on (and will dipose of any *chainp passed
3059 * in). The caller must check the chain->bref.type either way.
3062 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t **chainp,
3063 hammer2_blockref_t *bref, int *firstp,
3067 hammer2_blockref_t *base;
3068 hammer2_blockref_t *bref_ptr;
3070 hammer2_key_t next_key;
3071 hammer2_chain_t *chain = NULL;
3073 int how_always = HAMMER2_RESOLVE_ALWAYS;
3074 int how_maybe = HAMMER2_RESOLVE_MAYBE;
3077 int maxloops = 300000;
3084 * Scan flags borrowed from lookup.
3086 if (flags & HAMMER2_LOOKUP_ALWAYS) {
3087 how_maybe = how_always;
3088 how = HAMMER2_RESOLVE_ALWAYS;
3089 } else if (flags & HAMMER2_LOOKUP_NODATA) {
3090 how = HAMMER2_RESOLVE_NEVER;
3092 how = HAMMER2_RESOLVE_MAYBE;
3094 if (flags & HAMMER2_LOOKUP_SHARED) {
3095 how_maybe |= HAMMER2_RESOLVE_SHARED;
3096 how_always |= HAMMER2_RESOLVE_SHARED;
3097 how |= HAMMER2_RESOLVE_SHARED;
3101 * Calculate key to locate first/next element, unlocking the previous
3102 * element as we go. Be careful, the key calculation can overflow.
3104 * (also reset bref to NULL)
3110 key = bref->key + ((hammer2_key_t)1 << bref->keybits);
3111 if ((chain = *chainp) != NULL) {
3113 hammer2_chain_unlock(chain);
3114 hammer2_chain_drop(chain);
3118 error |= HAMMER2_ERROR_EOF;
3124 if (parent->error) {
3125 error = parent->error;
3128 if (--maxloops == 0)
3129 panic("hammer2_chain_scan: maxloops");
3132 * Locate the blockref array. Currently we do a fully associative
3133 * search through the array.
3135 switch(parent->bref.type) {
3136 case HAMMER2_BREF_TYPE_INODE:
3138 * An inode with embedded data has no sub-chains.
3140 * WARNING! Bulk scan code may pass a static chain marked
3141 * as BREF_TYPE_INODE with a copy of the volume
3142 * root blockset to snapshot the volume.
3144 if (parent->data->ipdata.meta.op_flags &
3145 HAMMER2_OPFLAG_DIRECTDATA) {
3146 error |= HAMMER2_ERROR_EOF;
3149 base = &parent->data->ipdata.u.blockset.blockref[0];
3150 count = HAMMER2_SET_COUNT;
3152 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3153 case HAMMER2_BREF_TYPE_INDIRECT:
3155 * Optimize indirect blocks in the INITIAL state to avoid
3158 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
3161 if (parent->data == NULL)
3162 panic("parent->data is NULL");
3163 base = &parent->data->npdata[0];
3165 count = parent->bytes / sizeof(hammer2_blockref_t);
3167 case HAMMER2_BREF_TYPE_VOLUME:
3168 base = &parent->data->voldata.sroot_blockset.blockref[0];
3169 count = HAMMER2_SET_COUNT;
3171 case HAMMER2_BREF_TYPE_FREEMAP:
3172 base = &parent->data->blkset.blockref[0];
3173 count = HAMMER2_SET_COUNT;
3176 panic("hammer2_chain_scan: unrecognized blockref type: %d",
3178 base = NULL; /* safety */
3179 count = 0; /* safety */
3184 * Merged scan to find next candidate.
3186 * hammer2_base_*() functions require the parent->core.live_* fields
3187 * to be synchronized.
3189 * We need to hold the spinlock to access the block array and RB tree
3190 * and to interlock chain creation.
3192 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
3193 hammer2_chain_countbrefs(parent, base, count);
3197 hammer2_spin_ex(&parent->core.spin);
3198 chain = hammer2_combined_find(parent, base, count,
3200 key, HAMMER2_KEY_MAX,
3202 generation = parent->core.generation;
3205 * Exhausted parent chain, we're done.
3207 if (bref_ptr == NULL) {
3208 hammer2_spin_unex(&parent->core.spin);
3209 KKASSERT(chain == NULL);
3210 error |= HAMMER2_ERROR_EOF;
3215 * Copy into the supplied stack-based blockref.
3220 * Selected from blockref or in-memory chain.
3222 if (chain == NULL) {
3223 switch(bref->type) {
3224 case HAMMER2_BREF_TYPE_INODE:
3225 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3226 case HAMMER2_BREF_TYPE_INDIRECT:
3227 case HAMMER2_BREF_TYPE_VOLUME:
3228 case HAMMER2_BREF_TYPE_FREEMAP:
3230 * Recursion, always get the chain
3232 hammer2_spin_unex(&parent->core.spin);
3233 chain = hammer2_chain_get(parent, generation,
3240 * No recursion, do not waste time instantiating
3241 * a chain, just iterate using the bref.
3243 hammer2_spin_unex(&parent->core.spin);
3248 * Recursion or not we need the chain in order to supply
3251 hammer2_chain_ref(chain);
3252 hammer2_spin_unex(&parent->core.spin);
3253 hammer2_chain_lock(chain, how);
3256 (bcmp(bref, &chain->bref, sizeof(*bref)) ||
3257 chain->parent != parent)) {
3258 hammer2_chain_unlock(chain);
3259 hammer2_chain_drop(chain);
3265 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
3267 * NOTE: chain's key range is not relevant as there might be
3268 * one-offs within the range that are not deleted.
3270 * NOTE: XXX this could create problems with scans used in
3271 * situations other than mount-time recovery.
3273 * NOTE: Lookups can race delete-duplicate because
3274 * delete-duplicate does not lock the parent's core
3275 * (they just use the spinlock on the core).
3277 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3278 hammer2_chain_unlock(chain);
3279 hammer2_chain_drop(chain);
3284 error |= HAMMER2_ERROR_EOF;
3292 * All done, return the bref or NULL, supply chain if necessary.
3300 * Create and return a new hammer2 system memory structure of the specified
3301 * key, type and size and insert it under (*parentp). This is a full
3302 * insertion, based on the supplied key/keybits, and may involve creating
3303 * indirect blocks and moving other chains around via delete/duplicate.
3305 * This call can be made with parent == NULL as long as a non -1 methods
3306 * is supplied. hmp must also be supplied in this situation (otherwise
3307 * hmp is extracted from the supplied parent). The chain will be detached
3308 * from the topology. A later call with both parent and chain can be made
3311 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION
3312 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
3313 * FULL. This typically means that the caller is creating the chain after
3314 * doing a hammer2_chain_lookup().
3316 * (*parentp) must be exclusive locked and may be replaced on return
3317 * depending on how much work the function had to do.
3319 * (*parentp) must not be errored or this function will assert.
3321 * (*chainp) usually starts out NULL and returns the newly created chain,
3322 * but if the caller desires the caller may allocate a disconnected chain
3323 * and pass it in instead.
3325 * This function should NOT be used to insert INDIRECT blocks. It is
3326 * typically used to create/insert inodes and data blocks.
3328 * Caller must pass-in an exclusively locked parent the new chain is to
3329 * be inserted under, and optionally pass-in a disconnected, exclusively
3330 * locked chain to insert (else we create a new chain). The function will
3331 * adjust (*parentp) as necessary, create or connect the chain, and
3332 * return an exclusively locked chain in *chainp.
3334 * When creating a PFSROOT inode under the super-root, pmp is typically NULL
3335 * and will be reassigned.
3337 * NOTE: returns HAMMER_ERROR_* flags
3340 hammer2_chain_create(hammer2_chain_t **parentp, hammer2_chain_t **chainp,
3341 hammer2_dev_t *hmp, hammer2_pfs_t *pmp, int methods,
3342 hammer2_key_t key, int keybits, int type, size_t bytes,
3343 hammer2_tid_t mtid, hammer2_off_t dedup_off, int flags)
3345 hammer2_chain_t *chain;
3346 hammer2_chain_t *parent;
3347 hammer2_blockref_t *base;
3348 hammer2_blockref_t dummy;
3352 int maxloops = 300000;
3355 * Topology may be crossing a PFS boundary.
3359 KKASSERT(hammer2_mtx_owned(&parent->lock));
3360 KKASSERT(parent->error == 0);
3365 if (chain == NULL) {
3367 * First allocate media space and construct the dummy bref,
3368 * then allocate the in-memory chain structure. Set the
3369 * INITIAL flag for fresh chains which do not have embedded
3372 bzero(&dummy, sizeof(dummy));
3375 dummy.keybits = keybits;
3376 dummy.data_off = hammer2_getradix(bytes);
3379 * Inherit methods from parent by default. Primarily used
3380 * for BREF_TYPE_DATA. Non-data types *must* be set to
3381 * a non-NONE check algorithm.
3384 dummy.methods = parent->bref.methods;
3386 dummy.methods = (uint8_t)methods;
3388 if (type != HAMMER2_BREF_TYPE_DATA &&
3389 HAMMER2_DEC_CHECK(dummy.methods) == HAMMER2_CHECK_NONE) {
3391 HAMMER2_ENC_CHECK(HAMMER2_CHECK_DEFAULT);
3394 chain = hammer2_chain_alloc(hmp, pmp, &dummy);
3397 * Lock the chain manually, chain_lock will load the chain
3398 * which we do NOT want to do. (note: chain->refs is set
3399 * to 1 by chain_alloc() for us, but lockcnt is not).
3402 hammer2_mtx_ex(&chain->lock);
3406 * Set INITIAL to optimize I/O. The flag will generally be
3407 * processed when we call hammer2_chain_modify().
3410 case HAMMER2_BREF_TYPE_VOLUME:
3411 case HAMMER2_BREF_TYPE_FREEMAP:
3412 panic("hammer2_chain_create: called with volume type");
3414 case HAMMER2_BREF_TYPE_INDIRECT:
3415 panic("hammer2_chain_create: cannot be used to"
3416 "create indirect block");
3418 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3419 panic("hammer2_chain_create: cannot be used to"
3420 "create freemap root or node");
3422 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3423 KKASSERT(bytes == sizeof(chain->data->bmdata));
3425 case HAMMER2_BREF_TYPE_DIRENT:
3426 case HAMMER2_BREF_TYPE_INODE:
3427 case HAMMER2_BREF_TYPE_DATA:
3430 * leave chain->data NULL, set INITIAL
3432 KKASSERT(chain->data == NULL);
3433 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
3438 * We are reattaching a previously deleted chain, possibly
3439 * under a new parent and possibly with a new key/keybits.
3440 * The chain does not have to be in a modified state. The
3441 * UPDATE flag will be set later on in this routine.
3443 * Do NOT mess with the current state of the INITIAL flag.
3445 chain->bref.key = key;
3446 chain->bref.keybits = keybits;
3447 if (chain->flags & HAMMER2_CHAIN_DELETED)
3448 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3449 KKASSERT(chain->parent == NULL);
3453 * Set the appropriate bref flag if requested.
3455 * NOTE! Callers can call this function to move chains without
3456 * knowing about special flags, so don't clear bref flags
3459 if (flags & HAMMER2_INSERT_PFSROOT)
3460 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
3466 * Calculate how many entries we have in the blockref array and
3467 * determine if an indirect block is required when inserting into
3471 if (--maxloops == 0)
3472 panic("hammer2_chain_create: maxloops");
3474 switch(parent->bref.type) {
3475 case HAMMER2_BREF_TYPE_INODE:
3476 if ((parent->data->ipdata.meta.op_flags &
3477 HAMMER2_OPFLAG_DIRECTDATA) != 0) {
3478 kprintf("hammer2: parent set for direct-data! "
3479 "pkey=%016jx ckey=%016jx\n",
3483 KKASSERT((parent->data->ipdata.meta.op_flags &
3484 HAMMER2_OPFLAG_DIRECTDATA) == 0);
3485 KKASSERT(parent->data != NULL);
3486 base = &parent->data->ipdata.u.blockset.blockref[0];
3487 count = HAMMER2_SET_COUNT;
3489 case HAMMER2_BREF_TYPE_INDIRECT:
3490 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3491 if (parent->flags & HAMMER2_CHAIN_INITIAL)
3494 base = &parent->data->npdata[0];
3495 count = parent->bytes / sizeof(hammer2_blockref_t);
3497 case HAMMER2_BREF_TYPE_VOLUME:
3498 KKASSERT(parent->data != NULL);
3499 base = &parent->data->voldata.sroot_blockset.blockref[0];
3500 count = HAMMER2_SET_COUNT;
3502 case HAMMER2_BREF_TYPE_FREEMAP:
3503 KKASSERT(parent->data != NULL);
3504 base = &parent->data->blkset.blockref[0];
3505 count = HAMMER2_SET_COUNT;
3508 panic("hammer2_chain_create: unrecognized blockref type: %d",
3516 * Make sure we've counted the brefs
3518 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
3519 hammer2_chain_countbrefs(parent, base, count);
3521 KASSERT(parent->core.live_count >= 0 &&
3522 parent->core.live_count <= count,
3523 ("bad live_count %d/%d (%02x, %d)",
3524 parent->core.live_count, count,
3525 parent->bref.type, parent->bytes));
3528 * If no free blockref could be found we must create an indirect
3529 * block and move a number of blockrefs into it. With the parent
3530 * locked we can safely lock each child in order to delete+duplicate
3531 * it without causing a deadlock.
3533 * This may return the new indirect block or the old parent depending
3534 * on where the key falls. NULL is returned on error.
3536 if (parent->core.live_count == count) {
3537 hammer2_chain_t *nparent;
3539 KKASSERT((flags & HAMMER2_INSERT_SAMEPARENT) == 0);
3541 nparent = hammer2_chain_create_indirect(parent, key, keybits,
3542 mtid, type, &error);
3543 if (nparent == NULL) {
3545 hammer2_chain_drop(chain);
3549 if (parent != nparent) {
3550 hammer2_chain_unlock(parent);
3551 hammer2_chain_drop(parent);
3552 parent = *parentp = nparent;
3558 * fall through if parent, or skip to here if no parent.
3561 if (chain->flags & HAMMER2_CHAIN_DELETED)
3562 kprintf("Inserting deleted chain @%016jx\n",
3566 * Link the chain into its parent.
3568 if (chain->parent != NULL)
3569 panic("hammer2: hammer2_chain_create: chain already connected");
3570 KKASSERT(chain->parent == NULL);
3572 KKASSERT(parent->core.live_count < count);
3573 hammer2_chain_insert(parent, chain,
3574 HAMMER2_CHAIN_INSERT_SPIN |
3575 HAMMER2_CHAIN_INSERT_LIVE,
3581 * Mark the newly created chain modified. This will cause
3582 * UPDATE to be set and process the INITIAL flag.
3584 * Device buffers are not instantiated for DATA elements
3585 * as these are handled by logical buffers.
3587 * Indirect and freemap node indirect blocks are handled
3588 * by hammer2_chain_create_indirect() and not by this
3591 * Data for all other bref types is expected to be
3592 * instantiated (INODE, LEAF).
3594 switch(chain->bref.type) {
3595 case HAMMER2_BREF_TYPE_DATA:
3596 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3597 case HAMMER2_BREF_TYPE_DIRENT:
3598 case HAMMER2_BREF_TYPE_INODE:
3599 error = hammer2_chain_modify(chain, mtid, dedup_off,
3600 HAMMER2_MODIFY_OPTDATA);
3604 * Remaining types are not supported by this function.
3605 * In particular, INDIRECT and LEAF_NODE types are
3606 * handled by create_indirect().
3608 panic("hammer2_chain_create: bad type: %d",
3615 * When reconnecting a chain we must set UPDATE and
3616 * setflush so the flush recognizes that it must update
3617 * the bref in the parent.
3619 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0)
3620 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
3624 * We must setflush(parent) to ensure that it recurses through to
3625 * chain. setflush(chain) might not work because ONFLUSH is possibly
3626 * already set in the chain (so it won't recurse up to set it in the
3630 hammer2_chain_setflush(parent);
3639 * Move the chain from its old parent to a new parent. The chain must have
3640 * already been deleted or already disconnected (or never associated) with
3641 * a parent. The chain is reassociated with the new parent and the deleted
3642 * flag will be cleared (no longer deleted). The chain's modification state
3645 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION
3646 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
3647 * FULL. This typically means that the caller is creating the chain after
3648 * doing a hammer2_chain_lookup().
3650 * Neither (parent) or (chain) can be errored.
3652 * If (parent) is non-NULL then the chain is inserted under the parent.
3654 * If (parent) is NULL then the newly duplicated chain is not inserted
3655 * anywhere, similar to if it had just been chain_alloc()'d (suitable for
3656 * passing into hammer2_chain_create() after this function returns).
3658 * WARNING! This function calls create which means it can insert indirect
3659 * blocks. This can cause other unrelated chains in the parent to
3660 * be moved to a newly inserted indirect block in addition to the
3664 hammer2_chain_rename(hammer2_chain_t **parentp, hammer2_chain_t *chain,
3665 hammer2_tid_t mtid, int flags)
3667 hammer2_blockref_t *bref;
3669 hammer2_chain_t *parent;
3672 * WARNING! We should never resolve DATA to device buffers
3673 * (XXX allow it if the caller did?), and since
3674 * we currently do not have the logical buffer cache
3675 * buffer in-hand to fix its cached physical offset
3676 * we also force the modify code to not COW it. XXX
3678 * NOTE! We allow error'd chains to be renamed. The bref itself
3679 * is good and can be renamed. The content, however, may
3683 KKASSERT(chain->parent == NULL);
3684 /*KKASSERT(chain->error == 0); allow */
3685 bref = &chain->bref;
3688 * If parent is not NULL the duplicated chain will be entered under
3689 * the parent and the UPDATE bit set to tell flush to update
3692 * We must setflush(parent) to ensure that it recurses through to
3693 * chain. setflush(chain) might not work because ONFLUSH is possibly
3694 * already set in the chain (so it won't recurse up to set it in the
3697 * Having both chains locked is extremely important for atomicy.
3699 if (parentp && (parent = *parentp) != NULL) {
3700 KKASSERT(hammer2_mtx_owned(&parent->lock));
3701 KKASSERT(parent->refs > 0);
3702 KKASSERT(parent->error == 0);
3704 hammer2_chain_create(parentp, &chain, NULL, chain->pmp,
3705 HAMMER2_METH_DEFAULT,
3706 bref->key, bref->keybits, bref->type,
3707 chain->bytes, mtid, 0, flags);
3708 KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE);
3709 hammer2_chain_setflush(*parentp);
3714 * This works in tandem with delete_obref() to install a blockref in
3715 * (typically) an indirect block that is associated with the chain being
3716 * moved to *parentp.
3718 * The reason we need this function is that the caller needs to maintain
3719 * the blockref as it was, and not generate a new blockref for what might
3720 * be a modified chain. Otherwise stuff will leak into the flush that
3721 * the flush code's FLUSH_INODE_STOP flag is unable to catch.
3723 * It is EXTREMELY important that we properly set CHAIN_BMAPUPD and
3724 * CHAIN_UPDATE. We must set BMAPUPD if the bref does not match, and
3725 * we must clear CHAIN_UPDATE (that was likely set by the chain_rename) if
3726 * it does. Otherwise we can end up in a situation where H2 is unable to
3727 * clean up the in-memory chain topology.
3729 * The reason for this is that flushes do not generally flush through
3730 * BREF_TYPE_INODE chains and depend on a hammer2_inode_t queued to syncq
3731 * or sideq to properly flush and dispose of the related inode chain's flags.
3732 * Situations where the inode is not actually modified by the frontend,
3733 * but where we have to move the related chains around as we insert or cleanup
3734 * indirect blocks, can leave us with a 'dirty' (non-disposable) in-memory
3735 * inode chain that does not have a hammer2_inode_t associated with it.
3738 hammer2_chain_rename_obref(hammer2_chain_t **parentp, hammer2_chain_t *chain,
3739 hammer2_tid_t mtid, int flags,
3740 hammer2_blockref_t *obref)
3742 hammer2_chain_rename(parentp, chain, mtid, flags);
3744 if (obref->type != HAMMER2_BREF_TYPE_EMPTY) {
3745 hammer2_blockref_t *tbase;
3748 KKASSERT((chain->flags & HAMMER2_CHAIN_BMAPPED) == 0);
3749 hammer2_chain_modify(*parentp, mtid, 0, 0);
3750 tbase = hammer2_chain_base_and_count(*parentp, &tcount);
3751 hammer2_base_insert(*parentp, tbase, tcount, chain, obref);
3752 if (bcmp(obref, &chain->bref, sizeof(chain->bref))) {
3753 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD |
3754 HAMMER2_CHAIN_UPDATE);
3756 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
3762 * Helper function for deleting chains.
3764 * The chain is removed from the live view (the RBTREE) as well as the parent's
3765 * blockmap. Both chain and its parent must be locked.
3767 * parent may not be errored. chain can be errored.
3770 _hammer2_chain_delete_helper(hammer2_chain_t *parent, hammer2_chain_t *chain,
3771 hammer2_tid_t mtid, int flags,
3772 hammer2_blockref_t *obref)
3777 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
3778 KKASSERT(chain->parent == parent);
3781 if (chain->flags & HAMMER2_CHAIN_BMAPPED) {
3783 * Chain is blockmapped, so there must be a parent.
3784 * Atomically remove the chain from the parent and remove
3785 * the blockmap entry. The parent must be set modified
3786 * to remove the blockmap entry.
3788 hammer2_blockref_t *base;
3791 KKASSERT(parent != NULL);
3792 KKASSERT(parent->error == 0);
3793 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
3794 error = hammer2_chain_modify(parent, mtid, 0, 0);
3799 * Calculate blockmap pointer
3801 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
3802 hammer2_spin_ex(&chain->core.spin);
3803 hammer2_spin_ex(&parent->core.spin);
3805 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3806 atomic_add_int(&parent->core.live_count, -1);
3807 ++parent->core.generation;
3808 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
3809 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
3810 --parent->core.chain_count;
3811 chain->parent = NULL;
3813 switch(parent->bref.type) {
3814 case HAMMER2_BREF_TYPE_INODE:
3816 * Access the inode's block array. However, there
3817 * is no block array if the inode is flagged
3821 (parent->data->ipdata.meta.op_flags &
3822 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
3824 &parent->data->ipdata.u.blockset.blockref[0];
3828 count = HAMMER2_SET_COUNT;
3830 case HAMMER2_BREF_TYPE_INDIRECT:
3831 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3833 base = &parent->data->npdata[0];
3836 count = parent->bytes / sizeof(hammer2_blockref_t);
3838 case HAMMER2_BREF_TYPE_VOLUME:
3839 base = &parent->data->voldata.
3840 sroot_blockset.blockref[0];
3841 count = HAMMER2_SET_COUNT;
3843 case HAMMER2_BREF_TYPE_FREEMAP:
3844 base = &parent->data->blkset.blockref[0];
3845 count = HAMMER2_SET_COUNT;
3850 panic("_hammer2_chain_delete_helper: "
3851 "unrecognized blockref type: %d",
3857 * delete blockmapped chain from its parent.
3859 * The parent is not affected by any statistics in chain
3860 * which are pending synchronization. That is, there is
3861 * nothing to undo in the parent since they have not yet
3862 * been incorporated into the parent.
3864 * The parent is affected by statistics stored in inodes.
3865 * Those have already been synchronized, so they must be
3866 * undone. XXX split update possible w/delete in middle?
3869 hammer2_base_delete(parent, base, count, chain, obref);
3871 hammer2_spin_unex(&parent->core.spin);
3872 hammer2_spin_unex(&chain->core.spin);
3873 } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
3875 * Chain is not blockmapped but a parent is present.
3876 * Atomically remove the chain from the parent. There is
3877 * no blockmap entry to remove.
3879 * Because chain was associated with a parent but not
3880 * synchronized, the chain's *_count_up fields contain
3881 * inode adjustment statistics which must be undone.
3883 hammer2_spin_ex(&chain->core.spin);
3884 hammer2_spin_ex(&parent->core.spin);
3885 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3886 atomic_add_int(&parent->core.live_count, -1);
3887 ++parent->core.generation;
3888 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
3889 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
3890 --parent->core.chain_count;
3891 chain->parent = NULL;
3892 hammer2_spin_unex(&parent->core.spin);
3893 hammer2_spin_unex(&chain->core.spin);
3896 * Chain is not blockmapped and has no parent. This
3897 * is a degenerate case.
3899 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3906 * Create an indirect block that covers one or more of the elements in the
3907 * current parent. Either returns the existing parent with no locking or
3908 * ref changes or returns the new indirect block locked and referenced
3909 * and leaving the original parent lock/ref intact as well.
3911 * If an error occurs, NULL is returned and *errorp is set to the H2 error.
3913 * The returned chain depends on where the specified key falls.
3915 * The key/keybits for the indirect mode only needs to follow three rules:
3917 * (1) That all elements underneath it fit within its key space and
3919 * (2) That all elements outside it are outside its key space.
3921 * (3) When creating the new indirect block any elements in the current
3922 * parent that fit within the new indirect block's keyspace must be
3923 * moved into the new indirect block.
3925 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
3926 * keyspace the the current parent, but lookup/iteration rules will
3927 * ensure (and must ensure) that rule (2) for all parents leading up
3928 * to the nearest inode or the root volume header is adhered to. This
3929 * is accomplished by always recursing through matching keyspaces in
3930 * the hammer2_chain_lookup() and hammer2_chain_next() API.
3932 * The current implementation calculates the current worst-case keyspace by
3933 * iterating the current parent and then divides it into two halves, choosing
3934 * whichever half has the most elements (not necessarily the half containing
3935 * the requested key).
3937 * We can also opt to use the half with the least number of elements. This
3938 * causes lower-numbered keys (aka logical file offsets) to recurse through
3939 * fewer indirect blocks and higher-numbered keys to recurse through more.
3940 * This also has the risk of not moving enough elements to the new indirect
3941 * block and being forced to create several indirect blocks before the element
3944 * Must be called with an exclusively locked parent.
3946 * NOTE: *errorp set to HAMMER_ERROR_* flags
3948 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
3949 hammer2_key_t *keyp, int keybits,
3950 hammer2_blockref_t *base, int count);
3951 static int hammer2_chain_indkey_file(hammer2_chain_t *parent,
3952 hammer2_key_t *keyp, int keybits,
3953 hammer2_blockref_t *base, int count,
3955 static int hammer2_chain_indkey_dir(hammer2_chain_t *parent,
3956 hammer2_key_t *keyp, int keybits,
3957 hammer2_blockref_t *base, int count,
3961 hammer2_chain_create_indirect(hammer2_chain_t *parent,
3962 hammer2_key_t create_key, int create_bits,
3963 hammer2_tid_t mtid, int for_type, int *errorp)
3966 hammer2_blockref_t *base;
3967 hammer2_blockref_t *bref;
3968 hammer2_blockref_t bsave;
3969 hammer2_blockref_t dummy;
3970 hammer2_chain_t *chain;
3971 hammer2_chain_t *ichain;
3972 hammer2_key_t key = create_key;
3973 hammer2_key_t key_beg;
3974 hammer2_key_t key_end;
3975 hammer2_key_t key_next;
3976 int keybits = create_bits;
3984 int maxloops = 300000;
3987 * Calculate the base blockref pointer or NULL if the chain
3988 * is known to be empty. We need to calculate the array count
3989 * for RB lookups either way.
3992 KKASSERT(hammer2_mtx_owned(&parent->lock));
3995 * Pre-modify the parent now to avoid having to deal with error
3996 * processing if we tried to later (in the middle of our loop).
3998 * We are going to be moving bref's around, the indirect blocks
3999 * cannot be in an initial state. Do not pass MODIFY_OPTDATA.
4001 *errorp = hammer2_chain_modify(parent, mtid, 0, 0);
4003 kprintf("hammer2_chain_create_indirect: error %08x %s\n",
4004 *errorp, hammer2_error_str(*errorp));
4007 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
4009 /*hammer2_chain_modify(&parent, HAMMER2_MODIFY_OPTDATA);*/
4010 base = hammer2_chain_base_and_count(parent, &count);
4013 * How big should our new indirect block be? It has to be at least
4014 * as large as its parent for splits to work properly.
4016 * The freemap uses a specific indirect block size. The number of
4017 * levels are built dynamically and ultimately depend on the size
4018 * volume. Because freemap blocks are taken from the reserved areas
4019 * of the volume our goal is efficiency (fewer levels) and not so
4020 * much to save disk space.
4022 * The first indirect block level for a directory usually uses
4023 * HAMMER2_IND_BYTES_MIN (4KB = 32 directory entries). Due to
4024 * the hash mechanism, this typically gives us a nominal
4025 * 32 * 4 entries with one level of indirection.
4027 * We use HAMMER2_IND_BYTES_NOM (16KB = 128 blockrefs) for FILE
4028 * indirect blocks. The initial 4 entries in the inode gives us
4029 * 256KB. Up to 4 indirect blocks gives us 32MB. Three levels
4030 * of indirection gives us 137GB, and so forth. H2 can support
4031 * huge file sizes but they are not typical, so we try to stick
4032 * with compactness and do not use a larger indirect block size.
4034 * We could use 64KB (PBUFSIZE), giving us 512 blockrefs, but
4035 * due to the way indirect blocks are created this usually winds
4036 * up being extremely inefficient for small files. Even though
4037 * 16KB requires more levels of indirection for very large files,
4038 * the 16KB records can be ganged together into 64KB DIOs.
4040 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
4041 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
4042 nbytes = HAMMER2_FREEMAP_LEVELN_PSIZE;
4043 } else if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
4044 if (parent->data->ipdata.meta.type ==
4045 HAMMER2_OBJTYPE_DIRECTORY)
4046 nbytes = HAMMER2_IND_BYTES_MIN; /* 4KB = 32 entries */
4048 nbytes = HAMMER2_IND_BYTES_NOM; /* 16KB = ~8MB file */
4051 nbytes = HAMMER2_IND_BYTES_NOM;
4053 if (nbytes < count * sizeof(hammer2_blockref_t)) {
4054 KKASSERT(for_type != HAMMER2_BREF_TYPE_FREEMAP_NODE &&
4055 for_type != HAMMER2_BREF_TYPE_FREEMAP_LEAF);
4056 nbytes = count * sizeof(hammer2_blockref_t);
4058 ncount = nbytes / sizeof(hammer2_blockref_t);
4061 * When creating an indirect block for a freemap node or leaf
4062 * the key/keybits must be fitted to static radix levels because
4063 * particular radix levels use particular reserved blocks in the
4066 * This routine calculates the key/radix of the indirect block
4067 * we need to create, and whether it is on the high-side or the
4071 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
4072 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
4073 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
4076 case HAMMER2_BREF_TYPE_DATA:
4077 keybits = hammer2_chain_indkey_file(parent, &key, keybits,
4078 base, count, ncount);
4080 case HAMMER2_BREF_TYPE_DIRENT:
4081 case HAMMER2_BREF_TYPE_INODE:
4082 keybits = hammer2_chain_indkey_dir(parent, &key, keybits,
4083 base, count, ncount);
4086 panic("illegal indirect block for bref type %d", for_type);
4091 * Normalize the key for the radix being represented, keeping the
4092 * high bits and throwing away the low bits.
4094 key &= ~(((hammer2_key_t)1 << keybits) - 1);
4097 * Ok, create our new indirect block
4099 bzero(&dummy, sizeof(dummy));
4100 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
4101 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
4102 dummy.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
4104 dummy.type = HAMMER2_BREF_TYPE_INDIRECT;
4107 dummy.keybits = keybits;
4108 dummy.data_off = hammer2_getradix(nbytes);
4110 HAMMER2_ENC_CHECK(HAMMER2_DEC_CHECK(parent->bref.methods)) |
4111 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
4113 ichain = hammer2_chain_alloc(hmp, parent->pmp, &dummy);
4114 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
4115 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
4116 /* ichain has one ref at this point */
4119 * We have to mark it modified to allocate its block, but use
4120 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
4121 * it won't be acted upon by the flush code.
4123 * XXX remove OPTDATA, we need a fully initialized indirect block to
4124 * be able to move the original blockref.
4126 *errorp = hammer2_chain_modify(ichain, mtid, 0, 0);
4128 kprintf("hammer2_chain_create_indirect: error %08x %s\n",
4129 *errorp, hammer2_error_str(*errorp));
4130 hammer2_chain_unlock(ichain);
4131 hammer2_chain_drop(ichain);
4134 KKASSERT((ichain->flags & HAMMER2_CHAIN_INITIAL) == 0);
4137 * Iterate the original parent and move the matching brefs into
4138 * the new indirect block.
4140 * XXX handle flushes.
4143 key_end = HAMMER2_KEY_MAX;
4144 key_next = 0; /* avoid gcc warnings */
4145 hammer2_spin_ex(&parent->core.spin);
4151 * Parent may have been modified, relocating its block array.
4152 * Reload the base pointer.
4154 base = hammer2_chain_base_and_count(parent, &count);
4156 if (++loops > 100000) {
4157 hammer2_spin_unex(&parent->core.spin);
4158 panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
4159 reason, parent, base, count, key_next);
4163 * NOTE: spinlock stays intact, returned chain (if not NULL)
4164 * is not referenced or locked which means that we
4165 * cannot safely check its flagged / deletion status
4168 chain = hammer2_combined_find(parent, base, count,
4172 generation = parent->core.generation;
4175 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4178 * Skip keys that are not within the key/radix of the new
4179 * indirect block. They stay in the parent.
4181 if (rounddown2(key ^ bref->key, (hammer2_key_t)1 << keybits) != 0) {
4182 goto next_key_spinlocked;
4186 * Load the new indirect block by acquiring the related
4187 * chains (potentially from media as it might not be
4188 * in-memory). Then move it to the new parent (ichain).
4190 * chain is referenced but not locked. We must lock the
4191 * chain to obtain definitive state.
4196 * Use chain already present in the RBTREE
4198 hammer2_chain_ref(chain);
4199 hammer2_spin_unex(&parent->core.spin);
4200 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
4203 * Get chain for blockref element. _get returns NULL
4204 * on insertion race.
4206 hammer2_spin_unex(&parent->core.spin);
4207 chain = hammer2_chain_get(parent, generation, &bsave,
4208 HAMMER2_RESOLVE_NEVER);
4209 if (chain == NULL) {
4211 hammer2_spin_ex(&parent->core.spin);
4217 * This is always live so if the chain has been deleted
4218 * we raced someone and we have to retry.
4220 * NOTE: Lookups can race delete-duplicate because
4221 * delete-duplicate does not lock the parent's core
4222 * (they just use the spinlock on the core).
4224 * (note reversed logic for this one)
4226 if (bcmp(&bsave, &chain->bref, sizeof(bsave)) ||
4227 chain->parent != parent ||
4228 (chain->flags & HAMMER2_CHAIN_DELETED)) {
4229 hammer2_chain_unlock(chain);
4230 hammer2_chain_drop(chain);
4231 if (hammer2_debug & 0x0040) {
4232 kprintf("LOST PARENT RETRY "
4233 "RETRY (%p,%p)->%p %08x\n",
4234 parent, chain->parent, chain, chain->flags);
4236 hammer2_spin_ex(&parent->core.spin);
4241 * Shift the chain to the indirect block.
4243 * WARNING! No reason for us to load chain data, pass NOSTATS
4244 * to prevent delete/insert from trying to access
4245 * inode stats (and thus asserting if there is no
4246 * chain->data loaded).
4248 * WARNING! The (parent, chain) deletion may modify the parent
4249 * and invalidate the base pointer.
4251 * WARNING! Parent must already be marked modified, so we
4252 * can assume that chain_delete always suceeds.
4254 * WARNING! hammer2_chain_repchange() does not have to be
4255 * called (and doesn't work anyway because we are
4256 * only doing a partial shift). A recursion that is
4257 * in-progress can continue at the current parent
4258 * and will be able to properly find its next key.
4260 error = hammer2_chain_delete_obref(parent, chain, mtid, 0,
4262 KKASSERT(error == 0);
4263 hammer2_chain_rename_obref(&ichain, chain, mtid, 0, &bsave);
4264 hammer2_chain_unlock(chain);
4265 hammer2_chain_drop(chain);
4266 KKASSERT(parent->refs > 0);
4268 base = NULL; /* safety */
4269 hammer2_spin_ex(&parent->core.spin);
4270 next_key_spinlocked:
4271 if (--maxloops == 0)
4272 panic("hammer2_chain_create_indirect: maxloops");
4274 if (key_next == 0 || key_next > key_end)
4279 hammer2_spin_unex(&parent->core.spin);
4282 * Insert the new indirect block into the parent now that we've
4283 * cleared out some entries in the parent. We calculated a good
4284 * insertion index in the loop above (ichain->index).
4286 * We don't have to set UPDATE here because we mark ichain
4287 * modified down below (so the normal modified -> flush -> set-moved
4288 * sequence applies).
4290 * The insertion shouldn't race as this is a completely new block
4291 * and the parent is locked.
4293 base = NULL; /* safety, parent modify may change address */
4294 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
4295 KKASSERT(parent->core.live_count < count);
4296 hammer2_chain_insert(parent, ichain,
4297 HAMMER2_CHAIN_INSERT_SPIN |
4298 HAMMER2_CHAIN_INSERT_LIVE,
4302 * Make sure flushes propogate after our manual insertion.
4304 hammer2_chain_setflush(ichain);
4305 hammer2_chain_setflush(parent);
4308 * Figure out what to return.
4310 if (rounddown2(create_key ^ key, (hammer2_key_t)1 << keybits)) {
4312 * Key being created is outside the key range,
4313 * return the original parent.
4315 hammer2_chain_unlock(ichain);
4316 hammer2_chain_drop(ichain);
4319 * Otherwise its in the range, return the new parent.
4320 * (leave both the new and old parent locked).
4329 * Do maintenance on an indirect chain. Both parent and chain are locked.
4331 * Returns non-zero if (chain) is deleted, either due to being empty or
4332 * because its children were safely moved into the parent.
4335 hammer2_chain_indirect_maintenance(hammer2_chain_t *parent,
4336 hammer2_chain_t *chain)
4338 hammer2_blockref_t *chain_base;
4339 hammer2_blockref_t *base;
4340 hammer2_blockref_t *bref;
4341 hammer2_blockref_t bsave;
4342 hammer2_key_t key_next;
4343 hammer2_key_t key_beg;
4344 hammer2_key_t key_end;
4345 hammer2_chain_t *sub;
4352 * Make sure we have an accurate live_count
4354 if ((chain->flags & (HAMMER2_CHAIN_INITIAL |
4355 HAMMER2_CHAIN_COUNTEDBREFS)) == 0) {
4356 base = &chain->data->npdata[0];
4357 count = chain->bytes / sizeof(hammer2_blockref_t);
4358 hammer2_chain_countbrefs(chain, base, count);
4362 * If the indirect block is empty we can delete it.
4363 * (ignore deletion error)
4365 if (chain->core.live_count == 0 && RB_EMPTY(&chain->core.rbtree)) {
4366 hammer2_chain_delete(parent, chain,
4367 chain->bref.modify_tid,
4368 HAMMER2_DELETE_PERMANENT);
4369 hammer2_chain_repchange(parent, chain);
4373 base = hammer2_chain_base_and_count(parent, &count);
4375 if ((parent->flags & (HAMMER2_CHAIN_INITIAL |
4376 HAMMER2_CHAIN_COUNTEDBREFS)) == 0) {
4377 hammer2_chain_countbrefs(parent, base, count);
4381 * Determine if we can collapse chain into parent, calculate
4382 * hysteresis for chain emptiness.
4384 if (parent->core.live_count + chain->core.live_count - 1 > count)
4386 chain_count = chain->bytes / sizeof(hammer2_blockref_t);
4387 if (chain->core.live_count > chain_count * 3 / 4)
4391 * Ok, theoretically we can collapse chain's contents into
4392 * parent. chain is locked, but any in-memory children of chain
4393 * are not. For this to work, we must be able to dispose of any
4394 * in-memory children of chain.
4396 * For now require that there are no in-memory children of chain.
4398 * WARNING! Both chain and parent must remain locked across this
4403 * Parent must be marked modified. Don't try to collapse it if we
4404 * can't mark it modified. Once modified, destroy chain to make room
4405 * and to get rid of what will be a conflicting key (this is included
4406 * in the calculation above). Finally, move the children of chain
4407 * into chain's parent.
4409 * This order creates an accounting problem for bref.embed.stats
4410 * because we destroy chain before we remove its children. Any
4411 * elements whos blockref is already synchronized will be counted
4412 * twice. To deal with the problem we clean out chain's stats prior
4415 error = hammer2_chain_modify(parent, 0, 0, 0);
4417 krateprintf(&krate_h2me, "hammer2: indirect_maint: %s\n",
4418 hammer2_error_str(error));
4421 error = hammer2_chain_modify(chain, chain->bref.modify_tid, 0, 0);
4423 krateprintf(&krate_h2me, "hammer2: indirect_maint: %s\n",
4424 hammer2_error_str(error));
4428 chain->bref.embed.stats.inode_count = 0;
4429 chain->bref.embed.stats.data_count = 0;
4430 error = hammer2_chain_delete(parent, chain,
4431 chain->bref.modify_tid,
4432 HAMMER2_DELETE_PERMANENT);
4433 KKASSERT(error == 0);
4436 * The combined_find call requires core.spin to be held. One would
4437 * think there wouldn't be any conflicts since we hold chain
4438 * exclusively locked, but the caching mechanism for 0-ref children
4439 * does not require a chain lock.
4441 hammer2_spin_ex(&chain->core.spin);
4445 key_end = HAMMER2_KEY_MAX;
4447 chain_base = &chain->data->npdata[0];
4448 chain_count = chain->bytes / sizeof(hammer2_blockref_t);
4449 sub = hammer2_combined_find(chain, chain_base, chain_count,
4453 generation = chain->core.generation;
4456 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4460 hammer2_chain_ref(sub);
4461 hammer2_spin_unex(&chain->core.spin);
4462 hammer2_chain_lock(sub, HAMMER2_RESOLVE_NEVER);
4464 hammer2_spin_unex(&chain->core.spin);
4465 sub = hammer2_chain_get(chain, generation, &bsave,
4466 HAMMER2_RESOLVE_NEVER);
4468 hammer2_spin_ex(&chain->core.spin);
4472 if (bcmp(&bsave, &sub->bref, sizeof(bsave)) ||
4473 sub->parent != chain ||
4474 (sub->flags & HAMMER2_CHAIN_DELETED)) {
4475 hammer2_chain_unlock(sub);
4476 hammer2_chain_drop(sub);
4477 hammer2_spin_ex(&chain->core.spin);
4478 sub = NULL; /* safety */
4481 error = hammer2_chain_delete_obref(chain, sub,
4482 sub->bref.modify_tid, 0,
4484 KKASSERT(error == 0);
4485 hammer2_chain_rename_obref(&parent, sub,
4486 sub->bref.modify_tid,
4487 HAMMER2_INSERT_SAMEPARENT, &bsave);
4488 hammer2_chain_unlock(sub);
4489 hammer2_chain_drop(sub);
4490 hammer2_spin_ex(&chain->core.spin);
4496 hammer2_spin_unex(&chain->core.spin);
4498 hammer2_chain_repchange(parent, chain);
4504 * Freemap indirect blocks
4506 * Calculate the keybits and highside/lowside of the freemap node the
4507 * caller is creating.
4509 * This routine will specify the next higher-level freemap key/radix
4510 * representing the lowest-ordered set. By doing so, eventually all
4511 * low-ordered sets will be moved one level down.
4513 * We have to be careful here because the freemap reserves a limited
4514 * number of blocks for a limited number of levels. So we can't just
4515 * push indiscriminately.
4518 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
4519 int keybits, hammer2_blockref_t *base, int count)
4521 hammer2_chain_t *chain;
4522 hammer2_blockref_t *bref;
4524 hammer2_key_t key_beg;
4525 hammer2_key_t key_end;
4526 hammer2_key_t key_next;
4529 int maxloops = 300000;
4537 * Calculate the range of keys in the array being careful to skip
4538 * slots which are overridden with a deletion.
4541 key_end = HAMMER2_KEY_MAX;
4542 hammer2_spin_ex(&parent->core.spin);
4545 if (--maxloops == 0) {
4546 panic("indkey_freemap shit %p %p:%d\n",
4547 parent, base, count);
4549 chain = hammer2_combined_find(parent, base, count,
4561 * Skip deleted chains.
4563 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4564 if (key_next == 0 || key_next > key_end)
4571 * Use the full live (not deleted) element for the scan
4572 * iteration. HAMMER2 does not allow partial replacements.
4574 * XXX should be built into hammer2_combined_find().
4576 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4578 if (keybits > bref->keybits) {
4580 keybits = bref->keybits;
4581 } else if (keybits == bref->keybits && bref->key < key) {
4588 hammer2_spin_unex(&parent->core.spin);
4591 * Return the keybits for a higher-level FREEMAP_NODE covering
4595 case HAMMER2_FREEMAP_LEVEL0_RADIX:
4596 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
4598 case HAMMER2_FREEMAP_LEVEL1_RADIX:
4599 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
4601 case HAMMER2_FREEMAP_LEVEL2_RADIX:
4602 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
4604 case HAMMER2_FREEMAP_LEVEL3_RADIX:
4605 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
4607 case HAMMER2_FREEMAP_LEVEL4_RADIX:
4608 keybits = HAMMER2_FREEMAP_LEVEL5_RADIX;
4610 case HAMMER2_FREEMAP_LEVEL5_RADIX:
4611 panic("hammer2_chain_indkey_freemap: level too high");
4614 panic("hammer2_chain_indkey_freemap: bad radix");
4623 * File indirect blocks
4625 * Calculate the key/keybits for the indirect block to create by scanning
4626 * existing keys. The key being created is also passed in *keyp and can be
4627 * inside or outside the indirect block. Regardless, the indirect block
4628 * must hold at least two keys in order to guarantee sufficient space.
4630 * We use a modified version of the freemap's fixed radix tree, but taylored
4631 * for file data. Basically we configure an indirect block encompassing the
4635 hammer2_chain_indkey_file(hammer2_chain_t *parent, hammer2_key_t *keyp,
4636 int keybits, hammer2_blockref_t *base, int count,
4639 hammer2_chain_t *chain;
4640 hammer2_blockref_t *bref;
4642 hammer2_key_t key_beg;
4643 hammer2_key_t key_end;
4644 hammer2_key_t key_next;
4648 int maxloops = 300000;
4656 * Calculate the range of keys in the array being careful to skip
4657 * slots which are overridden with a deletion.
4659 * Locate the smallest key.
4662 key_end = HAMMER2_KEY_MAX;
4663 hammer2_spin_ex(&parent->core.spin);
4666 if (--maxloops == 0) {
4667 panic("indkey_freemap shit %p %p:%d\n",
4668 parent, base, count);
4670 chain = hammer2_combined_find(parent, base, count,
4682 * Skip deleted chains.
4684 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4685 if (key_next == 0 || key_next > key_end)
4692 * Use the full live (not deleted) element for the scan
4693 * iteration. HAMMER2 does not allow partial replacements.
4695 * XXX should be built into hammer2_combined_find().
4697 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4699 if (keybits > bref->keybits) {
4701 keybits = bref->keybits;
4702 } else if (keybits == bref->keybits && bref->key < key) {
4709 hammer2_spin_unex(&parent->core.spin);
4712 * Calculate the static keybits for a higher-level indirect block
4713 * that contains the key.
4718 case HAMMER2_IND_BYTES_MIN / sizeof(hammer2_blockref_t):
4719 nradix = HAMMER2_IND_RADIX_MIN - HAMMER2_BLOCKREF_RADIX;
4721 case HAMMER2_IND_BYTES_NOM / sizeof(hammer2_blockref_t):
4722 nradix = HAMMER2_IND_RADIX_NOM - HAMMER2_BLOCKREF_RADIX;
4724 case HAMMER2_IND_BYTES_MAX / sizeof(hammer2_blockref_t):
4725 nradix = HAMMER2_IND_RADIX_MAX - HAMMER2_BLOCKREF_RADIX;
4728 panic("bad ncount %d\n", ncount);
4734 * The largest radix that can be returned for an indirect block is
4735 * 63 bits. (The largest practical indirect block radix is actually
4736 * 62 bits because the top-level inode or volume root contains four
4737 * entries, but allow 63 to be returned).
4742 return keybits + nradix;
4748 * Directory indirect blocks.
4750 * Covers both the inode index (directory of inodes), and directory contents
4751 * (filenames hardlinked to inodes).
4753 * Because directory keys are hashed we generally try to cut the space in
4754 * half. We accomodate the inode index (which tends to have linearly
4755 * increasing inode numbers) by ensuring that the keyspace is at least large
4756 * enough to fill up the indirect block being created.
4759 hammer2_chain_indkey_dir(hammer2_chain_t *parent, hammer2_key_t *keyp,
4760 int keybits, hammer2_blockref_t *base, int count,
4763 hammer2_blockref_t *bref;
4764 hammer2_chain_t *chain;
4765 hammer2_key_t key_beg;
4766 hammer2_key_t key_end;
4767 hammer2_key_t key_next;
4772 int maxloops = 300000;
4775 * NOTE: We can't take a shortcut here anymore for inodes because
4776 * the root directory can contain a mix of inodes and directory
4777 * entries (we used to just return 63 if parent->bref.type was
4778 * HAMMER2_BREF_TYPE_INODE.
4785 * Calculate the range of keys in the array being careful to skip
4786 * slots which are overridden with a deletion.
4789 key_end = HAMMER2_KEY_MAX;
4790 hammer2_spin_ex(&parent->core.spin);
4793 if (--maxloops == 0) {
4794 panic("indkey_freemap shit %p %p:%d\n",
4795 parent, base, count);
4797 chain = hammer2_combined_find(parent, base, count,
4811 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4812 if (key_next == 0 || key_next > key_end)
4819 * Use the full live (not deleted) element for the scan
4820 * iteration. HAMMER2 does not allow partial replacements.
4822 * XXX should be built into hammer2_combined_find().
4824 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4827 * Expand our calculated key range (key, keybits) to fit
4828 * the scanned key. nkeybits represents the full range
4829 * that we will later cut in half (two halves @ nkeybits - 1).
4832 if (nkeybits < bref->keybits) {
4833 if (bref->keybits > 64) {
4834 kprintf("bad bref chain %p bref %p\n",
4838 nkeybits = bref->keybits;
4840 while (nkeybits < 64 &&
4841 rounddown2(key ^ bref->key, (hammer2_key_t)1 << nkeybits) != 0) {
4846 * If the new key range is larger we have to determine
4847 * which side of the new key range the existing keys fall
4848 * under by checking the high bit, then collapsing the
4849 * locount into the hicount or vise-versa.
4851 if (keybits != nkeybits) {
4852 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
4863 * The newly scanned key will be in the lower half or the
4864 * upper half of the (new) key range.
4866 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
4875 hammer2_spin_unex(&parent->core.spin);
4876 bref = NULL; /* now invalid (safety) */
4879 * Adjust keybits to represent half of the full range calculated
4880 * above (radix 63 max) for our new indirect block.
4885 * Expand keybits to hold at least ncount elements. ncount will be
4886 * a power of 2. This is to try to completely fill leaf nodes (at
4887 * least for keys which are not hashes).
4889 * We aren't counting 'in' or 'out', we are counting 'high side'
4890 * and 'low side' based on the bit at (1LL << keybits). We want
4891 * everything to be inside in these cases so shift it all to
4892 * the low or high side depending on the new high bit.
4894 while (((hammer2_key_t)1 << keybits) < ncount) {
4896 if (key & ((hammer2_key_t)1 << keybits)) {
4905 if (hicount > locount)
4906 key |= (hammer2_key_t)1 << keybits;
4908 key &= ~(hammer2_key_t)1 << keybits;
4918 * Directory indirect blocks.
4920 * Covers both the inode index (directory of inodes), and directory contents
4921 * (filenames hardlinked to inodes).
4923 * Because directory keys are hashed we generally try to cut the space in
4924 * half. We accomodate the inode index (which tends to have linearly
4925 * increasing inode numbers) by ensuring that the keyspace is at least large
4926 * enough to fill up the indirect block being created.
4929 hammer2_chain_indkey_dir(hammer2_chain_t *parent, hammer2_key_t *keyp,
4930 int keybits, hammer2_blockref_t *base, int count,
4933 hammer2_blockref_t *bref;
4934 hammer2_chain_t *chain;
4935 hammer2_key_t key_beg;
4936 hammer2_key_t key_end;
4937 hammer2_key_t key_next;
4942 int maxloops = 300000;
4945 * Shortcut if the parent is the inode. In this situation the
4946 * parent has 4+1 directory entries and we are creating an indirect
4947 * block capable of holding many more.
4949 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
4958 * Calculate the range of keys in the array being careful to skip
4959 * slots which are overridden with a deletion.
4962 key_end = HAMMER2_KEY_MAX;
4963 hammer2_spin_ex(&parent->core.spin);
4966 if (--maxloops == 0) {
4967 panic("indkey_freemap shit %p %p:%d\n",
4968 parent, base, count);
4970 chain = hammer2_combined_find(parent, base, count,
4984 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4985 if (key_next == 0 || key_next > key_end)
4992 * Use the full live (not deleted) element for the scan
4993 * iteration. HAMMER2 does not allow partial replacements.
4995 * XXX should be built into hammer2_combined_find().
4997 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
5000 * Expand our calculated key range (key, keybits) to fit
5001 * the scanned key. nkeybits represents the full range
5002 * that we will later cut in half (two halves @ nkeybits - 1).
5005 if (nkeybits < bref->keybits) {
5006 if (bref->keybits > 64) {
5007 kprintf("bad bref chain %p bref %p\n",
5011 nkeybits = bref->keybits;
5013 while (nkeybits < 64 &&
5014 (~(((hammer2_key_t)1 << nkeybits) - 1) &
5015 (key ^ bref->key)) != 0) {
5020 * If the new key range is larger we have to determine
5021 * which side of the new key range the existing keys fall
5022 * under by checking the high bit, then collapsing the
5023 * locount into the hicount or vise-versa.
5025 if (keybits != nkeybits) {
5026 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
5037 * The newly scanned key will be in the lower half or the
5038 * upper half of the (new) key range.
5040 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
5049 hammer2_spin_unex(&parent->core.spin);
5050 bref = NULL; /* now invalid (safety) */
5053 * Adjust keybits to represent half of the full range calculated
5054 * above (radix 63 max) for our new indirect block.
5059 * Expand keybits to hold at least ncount elements. ncount will be
5060 * a power of 2. This is to try to completely fill leaf nodes (at
5061 * least for keys which are not hashes).
5063 * We aren't counting 'in' or 'out', we are counting 'high side'
5064 * and 'low side' based on the bit at (1LL << keybits). We want
5065 * everything to be inside in these cases so shift it all to
5066 * the low or high side depending on the new high bit.
5068 while (((hammer2_key_t)1 << keybits) < ncount) {
5070 if (key & ((hammer2_key_t)1 << keybits)) {
5079 if (hicount > locount)
5080 key |= (hammer2_key_t)1 << keybits;
5082 key &= ~(hammer2_key_t)1 << keybits;
5092 * Sets CHAIN_DELETED and remove the chain's blockref from the parent if
5095 * Both parent and chain must be locked exclusively.
5097 * This function will modify the parent if the blockref requires removal
5098 * from the parent's block table.
5100 * This function is NOT recursive. Any entity already pushed into the
5101 * chain (such as an inode) may still need visibility into its contents,
5102 * as well as the ability to read and modify the contents. For example,
5103 * for an unlinked file which is still open.
5105 * Also note that the flusher is responsible for cleaning up empty
5109 hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
5110 hammer2_tid_t mtid, int flags)
5114 KKASSERT(hammer2_mtx_owned(&chain->lock));
5117 * Nothing to do if already marked.
5119 * We need the spinlock on the core whos RBTREE contains chain
5120 * to protect against races.
5122 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
5123 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
5124 chain->parent == parent);
5125 error = _hammer2_chain_delete_helper(parent, chain,
5130 * Permanent deletions mark the chain as destroyed.
5132 * NOTE: We do not setflush the chain unless the deletion is
5133 * permanent, since the deletion of a chain does not actually
5134 * require it to be flushed.
5137 if (flags & HAMMER2_DELETE_PERMANENT) {
5138 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
5139 hammer2_chain_setflush(chain);
5147 hammer2_chain_delete_obref(hammer2_chain_t *parent, hammer2_chain_t *chain,
5148 hammer2_tid_t mtid, int flags,
5149 hammer2_blockref_t *obref)
5153 KKASSERT(hammer2_mtx_owned(&chain->lock));
5156 * Nothing to do if already marked.
5158 * We need the spinlock on the core whos RBTREE contains chain
5159 * to protect against races.
5161 obref->type = HAMMER2_BREF_TYPE_EMPTY;
5162 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
5163 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
5164 chain->parent == parent);
5165 error = _hammer2_chain_delete_helper(parent, chain,
5166 mtid, flags, obref);
5170 * Permanent deletions mark the chain as destroyed.
5172 * NOTE: We do not setflush the chain unless the deletion is
5173 * permanent, since the deletion of a chain does not actually
5174 * require it to be flushed.
5177 if (flags & HAMMER2_DELETE_PERMANENT) {
5178 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
5179 hammer2_chain_setflush(chain);
5187 * Returns the index of the nearest element in the blockref array >= elm.
5188 * Returns (count) if no element could be found.
5190 * Sets *key_nextp to the next key for loop purposes but does not modify
5191 * it if the next key would be higher than the current value of *key_nextp.
5192 * Note that *key_nexp can overflow to 0, which should be tested by the
5195 * WARNING! Must be called with parent's spinlock held. Spinlock remains
5196 * held through the operation.
5199 hammer2_base_find(hammer2_chain_t *parent,
5200 hammer2_blockref_t *base, int count,
5201 hammer2_key_t *key_nextp,
5202 hammer2_key_t key_beg, hammer2_key_t key_end)
5204 hammer2_blockref_t *scan;
5205 hammer2_key_t scan_end;
5210 * Require the live chain's already have their core's counted
5211 * so we can optimize operations.
5213 KKASSERT(parent->flags & HAMMER2_CHAIN_COUNTEDBREFS);
5218 if (count == 0 || base == NULL)
5222 * Sequential optimization using parent->cache_index. This is
5223 * the most likely scenario.
5225 * We can avoid trailing empty entries on live chains, otherwise
5226 * we might have to check the whole block array.
5228 i = parent->cache_index; /* SMP RACE OK */
5230 limit = parent->core.live_zero;
5235 KKASSERT(i < count);
5241 while (i > 0 && (scan->type == HAMMER2_BREF_TYPE_EMPTY ||
5242 scan->key > key_beg)) {
5246 parent->cache_index = i;
5249 * Search forwards, stop when we find a scan element which
5250 * encloses the key or until we know that there are no further
5254 if (scan->type != HAMMER2_BREF_TYPE_EMPTY) {
5255 scan_end = scan->key +
5256 ((hammer2_key_t)1 << scan->keybits) - 1;
5257 if (scan->key > key_beg || scan_end >= key_beg)
5266 parent->cache_index = i;
5270 scan_end = scan->key +
5271 ((hammer2_key_t)1 << scan->keybits);
5272 if (scan_end && (*key_nextp > scan_end ||
5274 *key_nextp = scan_end;
5282 * Do a combined search and return the next match either from the blockref
5283 * array or from the in-memory chain. Sets *bresp to the returned bref in
5284 * both cases, or sets it to NULL if the search exhausted. Only returns
5285 * a non-NULL chain if the search matched from the in-memory chain.
5287 * When no in-memory chain has been found and a non-NULL bref is returned
5291 * The returned chain is not locked or referenced. Use the returned bref
5292 * to determine if the search exhausted or not. Iterate if the base find
5293 * is chosen but matches a deleted chain.
5295 * WARNING! Must be called with parent's spinlock held. Spinlock remains
5296 * held through the operation.
5299 hammer2_combined_find(hammer2_chain_t *parent,
5300 hammer2_blockref_t *base, int count,
5301 hammer2_key_t *key_nextp,
5302 hammer2_key_t key_beg, hammer2_key_t key_end,
5303 hammer2_blockref_t **bresp)
5305 hammer2_blockref_t *bref;
5306 hammer2_chain_t *chain;
5310 * Lookup in block array and in rbtree.
5312 *key_nextp = key_end + 1;
5313 i = hammer2_base_find(parent, base, count, key_nextp,
5315 chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
5320 if (i == count && chain == NULL) {
5326 * Only chain matched.
5329 bref = &chain->bref;
5334 * Only blockref matched.
5336 if (chain == NULL) {
5342 * Both in-memory and blockref matched, select the nearer element.
5344 * If both are flush with the left-hand side or both are the
5345 * same distance away, select the chain. In this situation the
5346 * chain must have been loaded from the matching blockmap.
5348 if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
5349 chain->bref.key == base[i].key) {
5350 KKASSERT(chain->bref.key == base[i].key);
5351 bref = &chain->bref;
5356 * Select the nearer key
5358 if (chain->bref.key < base[i].key) {
5359 bref = &chain->bref;
5366 * If the bref is out of bounds we've exhausted our search.
5369 if (bref->key > key_end) {
5379 * Locate the specified block array element and delete it. The element
5382 * The spin lock on the related chain must be held.
5384 * NOTE: live_count was adjusted when the chain was deleted, so it does not
5385 * need to be adjusted when we commit the media change.
5388 hammer2_base_delete(hammer2_chain_t *parent,
5389 hammer2_blockref_t *base, int count,
5390 hammer2_chain_t *chain,
5391 hammer2_blockref_t *obref)
5393 hammer2_blockref_t *elm = &chain->bref;
5394 hammer2_blockref_t *scan;
5395 hammer2_key_t key_next;
5399 * Delete element. Expect the element to exist.
5401 * XXX see caller, flush code not yet sophisticated enough to prevent
5402 * re-flushed in some cases.
5404 key_next = 0; /* max range */
5405 i = hammer2_base_find(parent, base, count, &key_next,
5406 elm->key, elm->key);
5408 if (i == count || scan->type == HAMMER2_BREF_TYPE_EMPTY ||
5409 scan->key != elm->key ||
5410 ((chain->flags & HAMMER2_CHAIN_BMAPUPD) == 0 &&
5411 scan->keybits != elm->keybits)) {
5412 hammer2_spin_unex(&parent->core.spin);
5413 panic("delete base %p element not found at %d/%d elm %p\n",
5414 base, i, count, elm);
5419 * Update stats and zero the entry.
5421 * NOTE: Handle radix == 0 (0 bytes) case.
5423 if ((int)(scan->data_off & HAMMER2_OFF_MASK_RADIX)) {
5424 parent->bref.embed.stats.data_count -= (hammer2_off_t)1 <<
5425 (int)(scan->data_off & HAMMER2_OFF_MASK_RADIX);
5427 switch(scan->type) {
5428 case HAMMER2_BREF_TYPE_INODE:
5429 --parent->bref.embed.stats.inode_count;
5431 case HAMMER2_BREF_TYPE_DATA:
5432 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5433 atomic_set_int(&chain->flags,
5434 HAMMER2_CHAIN_HINT_LEAF_COUNT);
5436 if (parent->bref.leaf_count)
5437 --parent->bref.leaf_count;
5440 case HAMMER2_BREF_TYPE_INDIRECT:
5441 if (scan->type != HAMMER2_BREF_TYPE_DATA) {
5442 parent->bref.embed.stats.data_count -=
5443 scan->embed.stats.data_count;
5444 parent->bref.embed.stats.inode_count -=
5445 scan->embed.stats.inode_count;
5447 if (scan->type == HAMMER2_BREF_TYPE_INODE)
5449 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5450 atomic_set_int(&chain->flags,
5451 HAMMER2_CHAIN_HINT_LEAF_COUNT);
5453 if (parent->bref.leaf_count <= scan->leaf_count)
5454 parent->bref.leaf_count = 0;
5456 parent->bref.leaf_count -= scan->leaf_count;
5459 case HAMMER2_BREF_TYPE_DIRENT:
5460 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5461 atomic_set_int(&chain->flags,
5462 HAMMER2_CHAIN_HINT_LEAF_COUNT);
5464 if (parent->bref.leaf_count)
5465 --parent->bref.leaf_count;
5473 bzero(scan, sizeof(*scan));
5476 * We can only optimize parent->core.live_zero for live chains.
5478 if (parent->core.live_zero == i + 1) {
5479 while (--i >= 0 && base[i].type == HAMMER2_BREF_TYPE_EMPTY)
5481 parent->core.live_zero = i + 1;
5485 * Clear appropriate blockmap flags in chain.
5487 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED |
5488 HAMMER2_CHAIN_BMAPUPD);
5492 * Insert the specified element. The block array must not already have the
5493 * element and must have space available for the insertion.
5495 * The spin lock on the related chain must be held.
5497 * NOTE: live_count was adjusted when the chain was deleted, so it does not
5498 * need to be adjusted when we commit the media change.
5501 hammer2_base_insert(hammer2_chain_t *parent,
5502 hammer2_blockref_t *base, int count,
5503 hammer2_chain_t *chain, hammer2_blockref_t *elm)
5505 hammer2_key_t key_next;
5514 * Insert new element. Expect the element to not already exist
5515 * unless we are replacing it.
5517 * XXX see caller, flush code not yet sophisticated enough to prevent
5518 * re-flushed in some cases.
5520 key_next = 0; /* max range */
5521 i = hammer2_base_find(parent, base, count, &key_next,
5522 elm->key, elm->key);
5525 * Shortcut fill optimization, typical ordered insertion(s) may not
5528 KKASSERT(i >= 0 && i <= count);
5531 * Set appropriate blockmap flags in chain (if not NULL)
5534 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
5537 * Update stats and zero the entry
5539 if ((int)(elm->data_off & HAMMER2_OFF_MASK_RADIX)) {
5540 parent->bref.embed.stats.data_count += (hammer2_off_t)1 <<
5541 (int)(elm->data_off & HAMMER2_OFF_MASK_RADIX);
5544 case HAMMER2_BREF_TYPE_INODE:
5545 ++parent->bref.embed.stats.inode_count;
5547 case HAMMER2_BREF_TYPE_DATA:
5548 if (parent->bref.leaf_count != HAMMER2_BLOCKREF_LEAF_MAX)
5549 ++parent->bref.leaf_count;
5551 case HAMMER2_BREF_TYPE_INDIRECT:
5552 if (elm->type != HAMMER2_BREF_TYPE_DATA) {
5553 parent->bref.embed.stats.data_count +=
5554 elm->embed.stats.data_count;
5555 parent->bref.embed.stats.inode_count +=
5556 elm->embed.stats.inode_count;
5558 if (elm->type == HAMMER2_BREF_TYPE_INODE)
5560 if (parent->bref.leaf_count + elm->leaf_count <
5561 HAMMER2_BLOCKREF_LEAF_MAX) {
5562 parent->bref.leaf_count += elm->leaf_count;
5564 parent->bref.leaf_count = HAMMER2_BLOCKREF_LEAF_MAX;
5567 case HAMMER2_BREF_TYPE_DIRENT:
5568 if (parent->bref.leaf_count != HAMMER2_BLOCKREF_LEAF_MAX)
5569 ++parent->bref.leaf_count;
5577 * We can only optimize parent->core.live_zero for live chains.
5579 if (i == count && parent->core.live_zero < count) {
5580 i = parent->core.live_zero++;
5585 xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
5586 if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
5587 hammer2_spin_unex(&parent->core.spin);
5588 panic("insert base %p overlapping elements at %d elm %p\n",
5593 * Try to find an empty slot before or after.
5597 while (j > 0 || k < count) {
5599 if (j >= 0 && base[j].type == HAMMER2_BREF_TYPE_EMPTY) {
5603 bcopy(&base[j+1], &base[j],
5604 (i - j - 1) * sizeof(*base));
5610 if (k < count && base[k].type == HAMMER2_BREF_TYPE_EMPTY) {
5611 bcopy(&base[i], &base[i+1],
5612 (k - i) * sizeof(hammer2_blockref_t));
5616 * We can only update parent->core.live_zero for live
5619 if (parent->core.live_zero <= k)
5620 parent->core.live_zero = k + 1;
5625 panic("hammer2_base_insert: no room!");
5632 for (l = 0; l < count; ++l) {
5633 if (base[l].type != HAMMER2_BREF_TYPE_EMPTY) {
5634 key_next = base[l].key +
5635 ((hammer2_key_t)1 << base[l].keybits) - 1;
5639 while (++l < count) {
5640 if (base[l].type != HAMMER2_BREF_TYPE_EMPTY) {
5641 if (base[l].key <= key_next)
5642 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
5643 key_next = base[l].key +
5644 ((hammer2_key_t)1 << base[l].keybits) - 1;
5654 * Sort the blockref array for the chain. Used by the flush code to
5655 * sort the blockref[] array.
5657 * The chain must be exclusively locked AND spin-locked.
5659 typedef hammer2_blockref_t *hammer2_blockref_p;
5663 hammer2_base_sort_callback(const void *v1, const void *v2)
5665 hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
5666 hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
5669 * Make sure empty elements are placed at the end of the array
5671 if (bref1->type == HAMMER2_BREF_TYPE_EMPTY) {
5672 if (bref2->type == HAMMER2_BREF_TYPE_EMPTY)
5675 } else if (bref2->type == HAMMER2_BREF_TYPE_EMPTY) {
5682 if (bref1->key < bref2->key)
5684 if (bref1->key > bref2->key)
5690 hammer2_base_sort(hammer2_chain_t *chain)
5692 hammer2_blockref_t *base;
5695 switch(chain->bref.type) {
5696 case HAMMER2_BREF_TYPE_INODE:
5698 * Special shortcut for embedded data returns the inode
5699 * itself. Callers must detect this condition and access
5700 * the embedded data (the strategy code does this for us).
5702 * This is only applicable to regular files and softlinks.
5704 if (chain->data->ipdata.meta.op_flags &
5705 HAMMER2_OPFLAG_DIRECTDATA) {
5708 base = &chain->data->ipdata.u.blockset.blockref[0];
5709 count = HAMMER2_SET_COUNT;
5711 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
5712 case HAMMER2_BREF_TYPE_INDIRECT:
5714 * Optimize indirect blocks in the INITIAL state to avoid
5717 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
5718 base = &chain->data->npdata[0];
5719 count = chain->bytes / sizeof(hammer2_blockref_t);
5721 case HAMMER2_BREF_TYPE_VOLUME:
5722 base = &chain->data->voldata.sroot_blockset.blockref[0];
5723 count = HAMMER2_SET_COUNT;
5725 case HAMMER2_BREF_TYPE_FREEMAP:
5726 base = &chain->data->blkset.blockref[0];
5727 count = HAMMER2_SET_COUNT;
5730 panic("hammer2_base_sort: unrecognized "
5731 "blockref(A) type: %d",
5733 base = NULL; /* safety */
5734 count = 0; /* safety */
5737 kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
5743 * Chain memory management
5746 hammer2_chain_wait(hammer2_chain_t *chain)
5748 tsleep(chain, 0, "chnflw", 1);
5751 const hammer2_media_data_t *
5752 hammer2_chain_rdata(hammer2_chain_t *chain)
5754 KKASSERT(chain->data != NULL);
5755 return (chain->data);
5758 hammer2_media_data_t *
5759 hammer2_chain_wdata(hammer2_chain_t *chain)
5761 KKASSERT(chain->data != NULL);
5762 return (chain->data);
5766 * Set the check data for a chain. This can be a heavy-weight operation
5767 * and typically only runs on-flush. For file data check data is calculated
5768 * when the logical buffers are flushed.
5771 hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata)
5773 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_NOTTESTED);
5775 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
5776 case HAMMER2_CHECK_NONE:
5778 case HAMMER2_CHECK_DISABLED:
5780 case HAMMER2_CHECK_ISCSI32:
5781 chain->bref.check.iscsi32.value =
5782 hammer2_icrc32(bdata, chain->bytes);
5784 case HAMMER2_CHECK_XXHASH64:
5785 chain->bref.check.xxhash64.value =
5786 XXH64(bdata, chain->bytes, XXH_HAMMER2_SEED);
5788 case HAMMER2_CHECK_SHA192:
5790 SHA256_CTX hash_ctx;
5792 uint8_t digest[SHA256_DIGEST_LENGTH];
5793 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
5796 SHA256_Init(&hash_ctx);
5797 SHA256_Update(&hash_ctx, bdata, chain->bytes);
5798 SHA256_Final(u.digest, &hash_ctx);
5799 u.digest64[2] ^= u.digest64[3];
5801 chain->bref.check.sha192.data,
5802 sizeof(chain->bref.check.sha192.data));
5805 case HAMMER2_CHECK_FREEMAP:
5806 chain->bref.check.freemap.icrc32 =
5807 hammer2_icrc32(bdata, chain->bytes);
5810 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
5811 chain->bref.methods);
5817 * Characterize a failed check code and try to trace back to the inode.
5820 hammer2_characterize_failed_chain(hammer2_chain_t *chain, uint64_t check,
5823 hammer2_chain_t *lchain;
5824 hammer2_chain_t *ochain;
5827 did = krateprintf(&krate_h2chk,
5828 "chain %016jx.%02x (%s) meth=%02x CHECK FAIL "
5829 "(flags=%08x, bref/data ",
5830 chain->bref.data_off,
5832 hammer2_bref_type_str(chain->bref.type),
5833 chain->bref.methods,
5839 kprintf("%08x/%08x)\n",
5840 chain->bref.check.iscsi32.value,
5843 kprintf("%016jx/%016jx)\n",
5844 chain->bref.check.xxhash64.value,
5849 * Run up the chains to try to find the governing inode so we
5852 * XXX This error reporting is not really MPSAFE
5856 while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
5858 chain = chain->parent;
5861 if (chain && chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
5862 ((chain->bref.flags & HAMMER2_BREF_FLAG_PFSROOT) == 0 ||
5863 (lchain->bref.key & HAMMER2_DIRHASH_VISIBLE))) {
5864 kprintf(" Resides at/in inode %ld\n",
5866 } else if (chain && chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
5867 kprintf(" Resides in inode index - CRITICAL!!!\n");
5869 kprintf(" Resides in root index - CRITICAL!!!\n");
5872 const char *pfsname = "UNKNOWN";
5876 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
5877 if (ochain->pmp->pfs_hmps[i] == ochain->hmp &&
5878 ochain->pmp->pfs_names[i]) {
5879 pfsname = ochain->pmp->pfs_names[i];
5884 kprintf(" In pfs %s on device %s\n",
5885 pfsname, ochain->hmp->devrepname);
5890 * Returns non-zero on success, 0 on failure.
5893 hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata)
5899 if (chain->flags & HAMMER2_CHAIN_NOTTESTED)
5902 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
5903 case HAMMER2_CHECK_NONE:
5906 case HAMMER2_CHECK_DISABLED:
5909 case HAMMER2_CHECK_ISCSI32:
5910 check32 = hammer2_icrc32(bdata, chain->bytes);
5911 r = (chain->bref.check.iscsi32.value == check32);
5913 hammer2_characterize_failed_chain(chain, check32, 32);
5915 hammer2_process_icrc32 += chain->bytes;
5917 case HAMMER2_CHECK_XXHASH64:
5918 check64 = XXH64(bdata, chain->bytes, XXH_HAMMER2_SEED);
5919 r = (chain->bref.check.xxhash64.value == check64);
5921 hammer2_characterize_failed_chain(chain, check64, 64);
5923 hammer2_process_xxhash64 += chain->bytes;
5925 case HAMMER2_CHECK_SHA192:
5927 SHA256_CTX hash_ctx;
5929 uint8_t digest[SHA256_DIGEST_LENGTH];
5930 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
5933 SHA256_Init(&hash_ctx);
5934 SHA256_Update(&hash_ctx, bdata, chain->bytes);
5935 SHA256_Final(u.digest, &hash_ctx);
5936 u.digest64[2] ^= u.digest64[3];
5938 chain->bref.check.sha192.data,
5939 sizeof(chain->bref.check.sha192.data)) == 0) {
5943 krateprintf(&krate_h2chk,
5944 "chain %016jx.%02x meth=%02x "
5946 chain->bref.data_off,
5948 chain->bref.methods);
5952 case HAMMER2_CHECK_FREEMAP:
5953 r = (chain->bref.check.freemap.icrc32 ==
5954 hammer2_icrc32(bdata, chain->bytes));
5958 did = krateprintf(&krate_h2chk,
5959 "chain %016jx.%02x meth=%02x "
5961 chain->bref.data_off,
5963 chain->bref.methods);
5965 kprintf("freemap.icrc %08x icrc32 %08x (%d)\n",
5966 chain->bref.check.freemap.icrc32,
5967 hammer2_icrc32(bdata, chain->bytes),
5970 kprintf("dio %p buf %016jx,%d "
5973 chain->dio->bp->b_loffset,
5974 chain->dio->bp->b_bufsize,
5976 chain->dio->bp->b_data);
5982 kprintf("hammer2_chain_testcheck: unknown check type %02x\n",
5983 chain->bref.methods);
5991 * Acquire the chain and parent representing the specified inode for the
5992 * device at the specified cluster index.
5994 * The flags passed in are LOOKUP flags, not RESOLVE flags.
5996 * If we are unable to locate the inode, HAMMER2_ERROR_EIO is returned and
5997 * *chainp will be NULL. *parentp may still be set error or not, or NULL
5998 * if the parent itself could not be resolved.
6000 * The caller may pass-in a locked *parentp and/or *chainp, or neither.
6001 * They will be unlocked and released by this function. The *parentp and
6002 * *chainp representing the located inode are returned locked.
6005 hammer2_chain_inode_find(hammer2_pfs_t *pmp, hammer2_key_t inum,
6006 int clindex, int flags,
6007 hammer2_chain_t **parentp, hammer2_chain_t **chainp)
6009 hammer2_chain_t *parent;
6010 hammer2_chain_t *rchain;
6011 hammer2_key_t key_dummy;
6012 hammer2_inode_t *ip;
6016 resolve_flags = (flags & HAMMER2_LOOKUP_SHARED) ?
6017 HAMMER2_RESOLVE_SHARED : 0;
6020 * Caller expects us to replace these.
6023 hammer2_chain_unlock(*chainp);
6024 hammer2_chain_drop(*chainp);
6028 hammer2_chain_unlock(*parentp);
6029 hammer2_chain_drop(*parentp);
6034 * Be very careful, this is a backend function and we CANNOT
6035 * lock any frontend inode structure we find. But we have to
6036 * look the inode up this way first in case it exists but is
6037 * detached from the radix tree.
6039 ip = hammer2_inode_lookup(pmp, inum);
6041 *chainp = hammer2_inode_chain_and_parent(ip, clindex,
6044 hammer2_inode_drop(ip);
6047 hammer2_chain_unlock(*chainp);
6048 hammer2_chain_drop(*chainp);
6051 hammer2_chain_unlock(*parentp);
6052 hammer2_chain_drop(*parentp);
6058 * Inodes hang off of the iroot (bit 63 is clear, differentiating
6059 * inodes from root directory entries in the key lookup).
6061 parent = hammer2_inode_chain(pmp->iroot, clindex, resolve_flags);
6064 rchain = hammer2_chain_lookup(&parent, &key_dummy,
6068 error = HAMMER2_ERROR_EIO;
6077 * Used by the bulkscan code to snapshot the synchronized storage for
6078 * a volume, allowing it to be scanned concurrently against normal
6082 hammer2_chain_bulksnap(hammer2_dev_t *hmp)
6084 hammer2_chain_t *copy;
6086 copy = hammer2_chain_alloc(hmp, hmp->spmp, &hmp->vchain.bref);
6087 copy->data = kmalloc(sizeof(copy->data->voldata),
6090 hammer2_voldata_lock(hmp);
6091 copy->data->voldata = hmp->volsync;
6092 hammer2_voldata_unlock(hmp);
6098 hammer2_chain_bulkdrop(hammer2_chain_t *copy)
6100 KKASSERT(copy->bref.type == HAMMER2_BREF_TYPE_VOLUME);
6101 KKASSERT(copy->data);
6102 kfree(copy->data, copy->hmp->mchain);
6104 atomic_add_long(&hammer2_chain_allocs, -1);
6105 hammer2_chain_drop(copy);
6109 * Returns non-zero if the chain (INODE or DIRENT) matches the
6113 hammer2_chain_dirent_test(hammer2_chain_t *chain, const char *name,
6116 const hammer2_inode_data_t *ripdata;
6117 const hammer2_dirent_head_t *den;
6119 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
6120 ripdata = &chain->data->ipdata;
6121 if (ripdata->meta.name_len == name_len &&
6122 bcmp(ripdata->filename, name, name_len) == 0) {
6126 if (chain->bref.type == HAMMER2_BREF_TYPE_DIRENT &&
6127 chain->bref.embed.dirent.namlen == name_len) {
6128 den = &chain->bref.embed.dirent;
6129 if (name_len > sizeof(chain->bref.check.buf) &&
6130 bcmp(chain->data->buf, name, name_len) == 0) {
6133 if (name_len <= sizeof(chain->bref.check.buf) &&
6134 bcmp(chain->bref.check.buf, name, name_len) == 0) {