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 void hammer2_chain_rename_obref(hammer2_chain_t **parentp,
72 hammer2_chain_t *chain, hammer2_tid_t mtid,
73 int flags, hammer2_blockref_t *obref);
74 static int hammer2_chain_delete_obref(hammer2_chain_t *parent,
75 hammer2_chain_t *chain,
76 hammer2_tid_t mtid, int flags,
77 hammer2_blockref_t *obref);
78 static hammer2_io_t *hammer2_chain_drop_data(hammer2_chain_t *chain);
79 static hammer2_chain_t *hammer2_combined_find(
80 hammer2_chain_t *parent,
81 hammer2_blockref_t *base, int count,
82 hammer2_key_t *key_nextp,
83 hammer2_key_t key_beg, hammer2_key_t key_end,
84 hammer2_blockref_t **bresp);
87 * There are many degenerate situations where an extreme rate of console
88 * output can occur from warnings and errors. Make sure this output does
89 * not impede operations.
91 static struct krate krate_h2chk = { .freq = 5 };
92 static struct krate krate_h2me = { .freq = 1 };
93 static struct krate krate_h2em = { .freq = 1 };
96 * Basic RBTree for chains (core.rbtree).
98 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
101 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
103 hammer2_key_t c1_beg;
104 hammer2_key_t c1_end;
105 hammer2_key_t c2_beg;
106 hammer2_key_t c2_end;
109 * Compare chains. Overlaps are not supposed to happen and catch
110 * any software issues early we count overlaps as a match.
112 c1_beg = chain1->bref.key;
113 c1_end = c1_beg + ((hammer2_key_t)1 << chain1->bref.keybits) - 1;
114 c2_beg = chain2->bref.key;
115 c2_end = c2_beg + ((hammer2_key_t)1 << chain2->bref.keybits) - 1;
117 if (c1_end < c2_beg) /* fully to the left */
119 if (c1_beg > c2_end) /* fully to the right */
121 return(0); /* overlap (must not cross edge boundary) */
125 * Assert that a chain has no media data associated with it.
128 hammer2_chain_assert_no_data(hammer2_chain_t *chain)
130 KKASSERT(chain->dio == NULL);
131 if (chain->bref.type != HAMMER2_BREF_TYPE_VOLUME &&
132 chain->bref.type != HAMMER2_BREF_TYPE_FREEMAP &&
134 panic("hammer2_chain_assert_no_data: chain %p still has data",
140 * Make a chain visible to the flusher. The flusher operates using a top-down
141 * recursion based on the ONFLUSH flag. It locates MODIFIED and UPDATE chains,
142 * flushes them, and updates blocks back to the volume root.
144 * This routine sets the ONFLUSH flag upward from the triggering chain until
145 * it hits an inode root or the volume root. Inode chains serve as inflection
146 * points, requiring the flusher to bridge across trees. Inodes include
147 * regular inodes, PFS roots (pmp->iroot), and the media super root
151 hammer2_chain_setflush(hammer2_chain_t *chain)
153 hammer2_chain_t *parent;
155 if ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
156 hammer2_spin_sh(&chain->core.spin);
157 while ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
158 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH);
159 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
161 if ((parent = chain->parent) == NULL)
163 hammer2_spin_sh(&parent->core.spin);
164 hammer2_spin_unsh(&chain->core.spin);
167 hammer2_spin_unsh(&chain->core.spin);
172 * Allocate a new disconnected chain element representing the specified
173 * bref. chain->refs is set to 1 and the passed bref is copied to
174 * chain->bref. chain->bytes is derived from the bref.
176 * chain->pmp inherits pmp unless the chain is an inode (other than the
179 * NOTE: Returns a referenced but unlocked (because there is no core) chain.
182 hammer2_chain_alloc(hammer2_dev_t *hmp, hammer2_pfs_t *pmp,
183 hammer2_blockref_t *bref)
185 hammer2_chain_t *chain;
189 * Special case - radix of 0 indicates a chain that does not
190 * need a data reference (context is completely embedded in the
193 if ((int)(bref->data_off & HAMMER2_OFF_MASK_RADIX))
194 bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
198 atomic_add_long(&hammer2_chain_allocs, 1);
201 * Construct the appropriate system structure.
204 case HAMMER2_BREF_TYPE_DIRENT:
205 case HAMMER2_BREF_TYPE_INODE:
206 case HAMMER2_BREF_TYPE_INDIRECT:
207 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
208 case HAMMER2_BREF_TYPE_DATA:
209 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
210 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
212 case HAMMER2_BREF_TYPE_VOLUME:
213 case HAMMER2_BREF_TYPE_FREEMAP:
215 * Only hammer2_chain_bulksnap() calls this function with these
218 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
222 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
227 * Initialize the new chain structure. pmp must be set to NULL for
228 * chains belonging to the super-root topology of a device mount.
230 if (pmp == hmp->spmp)
237 chain->bytes = bytes;
239 chain->flags = HAMMER2_CHAIN_ALLOCATED;
240 lockinit(&chain->diolk, "chdio", 0, 0);
243 * Set the PFS boundary flag if this chain represents a PFS root.
245 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
246 atomic_set_int(&chain->flags, HAMMER2_CHAIN_PFSBOUNDARY);
247 hammer2_chain_core_init(chain);
253 * Initialize a chain's core structure. This structure used to be allocated
254 * but is now embedded.
256 * The core is not locked. No additional refs on the chain are made.
257 * (trans) must not be NULL if (core) is not NULL.
260 hammer2_chain_core_init(hammer2_chain_t *chain)
263 * Fresh core under nchain (no multi-homing of ochain's
266 RB_INIT(&chain->core.rbtree); /* live chains */
267 hammer2_mtx_init(&chain->lock, "h2chain");
271 * Add a reference to a chain element, preventing its destruction.
273 * (can be called with spinlock held)
276 hammer2_chain_ref(hammer2_chain_t *chain)
278 if (atomic_fetchadd_int(&chain->refs, 1) == 0) {
280 * Just flag that the chain was used and should be recycled
281 * on the LRU if it encounters it later.
283 if (chain->flags & HAMMER2_CHAIN_ONLRU)
284 atomic_set_int(&chain->flags, HAMMER2_CHAIN_LRUHINT);
288 * REMOVED - reduces contention, lru_list is more heuristical
291 * 0->non-zero transition must ensure that chain is removed
294 * NOTE: Already holding lru_spin here so we cannot call
295 * hammer2_chain_ref() to get it off lru_list, do
298 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
299 hammer2_pfs_t *pmp = chain->pmp;
300 hammer2_spin_ex(&pmp->lru_spin);
301 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
302 atomic_add_int(&pmp->lru_count, -1);
303 atomic_clear_int(&chain->flags,
304 HAMMER2_CHAIN_ONLRU);
305 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
307 hammer2_spin_unex(&pmp->lru_spin);
314 * Ref a locked chain and force the data to be held across an unlock.
315 * Chain must be currently locked. The user of the chain who desires
316 * to release the hold must call hammer2_chain_lock_unhold() to relock
317 * and unhold the chain, then unlock normally, or may simply call
318 * hammer2_chain_drop_unhold() (which is safer against deadlocks).
321 hammer2_chain_ref_hold(hammer2_chain_t *chain)
323 atomic_add_int(&chain->lockcnt, 1);
324 hammer2_chain_ref(chain);
328 * Insert the chain in the core rbtree.
330 * Normal insertions are placed in the live rbtree. Insertion of a deleted
331 * chain is a special case used by the flush code that is placed on the
332 * unstaged deleted list to avoid confusing the live view.
334 #define HAMMER2_CHAIN_INSERT_SPIN 0x0001
335 #define HAMMER2_CHAIN_INSERT_LIVE 0x0002
336 #define HAMMER2_CHAIN_INSERT_RACE 0x0004
340 hammer2_chain_insert(hammer2_chain_t *parent, hammer2_chain_t *chain,
341 int flags, int generation)
343 hammer2_chain_t *xchain;
346 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
347 hammer2_spin_ex(&parent->core.spin);
350 * Interlocked by spinlock, check for race
352 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
353 parent->core.generation != generation) {
354 error = HAMMER2_ERROR_EAGAIN;
361 xchain = RB_INSERT(hammer2_chain_tree, &parent->core.rbtree, chain);
362 KASSERT(xchain == NULL,
363 ("hammer2_chain_insert: collision %p %p (key=%016jx)",
364 chain, xchain, chain->bref.key));
365 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
366 chain->parent = parent;
367 ++parent->core.chain_count;
368 ++parent->core.generation; /* XXX incs for _get() too, XXX */
371 * We have to keep track of the effective live-view blockref count
372 * so the create code knows when to push an indirect block.
374 if (flags & HAMMER2_CHAIN_INSERT_LIVE)
375 atomic_add_int(&parent->core.live_count, 1);
377 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
378 hammer2_spin_unex(&parent->core.spin);
383 * Drop the caller's reference to the chain. When the ref count drops to
384 * zero this function will try to disassociate the chain from its parent and
385 * deallocate it, then recursely drop the parent using the implied ref
386 * from the chain's chain->parent.
388 * Nobody should own chain's mutex on the 1->0 transition, unless this drop
389 * races an acquisition by another cpu. Therefore we can loop if we are
390 * unable to acquire the mutex, and refs is unlikely to be 1 unless we again
391 * race against another drop.
393 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain,
395 static void hammer2_chain_lru_flush(hammer2_pfs_t *pmp);
398 hammer2_chain_drop(hammer2_chain_t *chain)
402 if (hammer2_debug & 0x200000)
405 KKASSERT(chain->refs > 0);
413 if (hammer2_mtx_ex_try(&chain->lock) == 0)
414 chain = hammer2_chain_lastdrop(chain, 0);
415 /* retry the same chain, or chain from lastdrop */
417 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
419 /* retry the same chain */
426 * Unhold a held and probably not-locked chain, ensure that the data is
427 * dropped on the 1->0 transition of lockcnt by obtaining an exclusive
428 * lock and then simply unlocking the chain.
431 hammer2_chain_unhold(hammer2_chain_t *chain)
437 lockcnt = chain->lockcnt;
440 if (atomic_cmpset_int(&chain->lockcnt,
441 lockcnt, lockcnt - 1)) {
444 } else if (hammer2_mtx_ex_try(&chain->lock) == 0) {
445 hammer2_chain_unlock(chain);
449 * This situation can easily occur on SMP due to
450 * the gap inbetween the 1->0 transition and the
451 * final unlock. We cannot safely block on the
452 * mutex because lockcnt might go above 1.
454 * XXX Sleep for one tick if it takes too long.
457 if (iter > 1000 + hz) {
458 kprintf("hammer2: h2race1 %p\n", chain);
461 tsleep(&iter, 0, "h2race1", 1);
469 hammer2_chain_drop_unhold(hammer2_chain_t *chain)
471 hammer2_chain_unhold(chain);
472 hammer2_chain_drop(chain);
476 hammer2_chain_rehold(hammer2_chain_t *chain)
478 hammer2_chain_lock(chain, HAMMER2_RESOLVE_SHARED);
479 atomic_add_int(&chain->lockcnt, 1);
480 hammer2_chain_unlock(chain);
484 * Handles the (potential) last drop of chain->refs from 1->0. Called with
485 * the mutex exclusively locked, refs == 1, and lockcnt 0. SMP races are
486 * possible against refs and lockcnt. We must dispose of the mutex on chain.
488 * This function returns an unlocked chain for recursive drop or NULL. It
489 * can return the same chain if it determines it has raced another ref.
493 * When two chains need to be recursively dropped we use the chain we
494 * would otherwise free to placehold the additional chain. It's a bit
495 * convoluted but we can't just recurse without potentially blowing out
498 * The chain cannot be freed if it has any children.
499 * The chain cannot be freed if flagged MODIFIED unless we can dispose of it.
500 * The chain cannot be freed if flagged UPDATE unless we can dispose of it.
501 * Any dedup registration can remain intact.
503 * The core spinlock is allowed to nest child-to-parent (not parent-to-child).
507 hammer2_chain_lastdrop(hammer2_chain_t *chain, int depth)
511 hammer2_chain_t *parent;
512 hammer2_chain_t *rdrop;
515 * We need chain's spinlock to interlock the sub-tree test.
516 * We already have chain's mutex, protecting chain->parent.
518 * Remember that chain->refs can be in flux.
520 hammer2_spin_ex(&chain->core.spin);
522 if (chain->parent != NULL) {
524 * If the chain has a parent the UPDATE bit prevents scrapping
525 * as the chain is needed to properly flush the parent. Try
526 * to complete the 1->0 transition and return NULL. Retry
527 * (return chain) if we are unable to complete the 1->0
528 * transition, else return NULL (nothing more to do).
530 * If the chain has a parent the MODIFIED bit prevents
533 * Chains with UPDATE/MODIFIED are *not* put on the LRU list!
535 if (chain->flags & (HAMMER2_CHAIN_UPDATE |
536 HAMMER2_CHAIN_MODIFIED)) {
537 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
538 hammer2_spin_unex(&chain->core.spin);
539 hammer2_chain_assert_no_data(chain);
540 hammer2_mtx_unlock(&chain->lock);
543 hammer2_spin_unex(&chain->core.spin);
544 hammer2_mtx_unlock(&chain->lock);
548 /* spinlock still held */
549 } else if (chain->bref.type == HAMMER2_BREF_TYPE_VOLUME ||
550 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP) {
552 * Retain the static vchain and fchain. Clear bits that
553 * are not relevant. Do not clear the MODIFIED bit,
554 * and certainly do not put it on the delayed-flush queue.
556 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
559 * The chain has no parent and can be flagged for destruction.
560 * Since it has no parent, UPDATE can also be cleared.
562 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
563 if (chain->flags & HAMMER2_CHAIN_UPDATE)
564 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
567 * If the chain has children we must propagate the DESTROY
568 * flag downward and rip the disconnected topology apart.
569 * This is accomplished by calling hammer2_flush() on the
572 * Any dedup is already handled by the underlying DIO, so
573 * we do not have to specifically flush it here.
575 if (chain->core.chain_count) {
576 hammer2_spin_unex(&chain->core.spin);
577 hammer2_flush(chain, HAMMER2_FLUSH_TOP |
579 hammer2_mtx_unlock(&chain->lock);
581 return(chain); /* retry drop */
585 * Otherwise we can scrap the MODIFIED bit if it is set,
586 * and continue along the freeing path.
588 * Be sure to clean-out any dedup bits. Without a parent
589 * this chain will no longer be visible to the flush code.
590 * Easy check data_off to avoid the volume root.
592 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
593 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
594 atomic_add_long(&hammer2_count_modified_chains, -1);
596 hammer2_pfs_memory_wakeup(chain->pmp, -1);
598 /* spinlock still held */
601 /* spinlock still held */
604 * If any children exist we must leave the chain intact with refs == 0.
605 * They exist because chains are retained below us which have refs or
606 * may require flushing.
608 * Retry (return chain) if we fail to transition the refs to 0, else
609 * return NULL indication nothing more to do.
611 * Chains with children are NOT put on the LRU list.
613 if (chain->core.chain_count) {
614 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
615 hammer2_spin_unex(&chain->core.spin);
616 hammer2_chain_assert_no_data(chain);
617 hammer2_mtx_unlock(&chain->lock);
620 hammer2_spin_unex(&chain->core.spin);
621 hammer2_mtx_unlock(&chain->lock);
625 /* spinlock still held */
626 /* no chains left under us */
629 * chain->core has no children left so no accessors can get to our
630 * chain from there. Now we have to lock the parent core to interlock
631 * remaining possible accessors that might bump chain's refs before
632 * we can safely drop chain's refs with intent to free the chain.
635 pmp = chain->pmp; /* can be NULL */
638 parent = chain->parent;
641 * WARNING! chain's spin lock is still held here, and other spinlocks
642 * will be acquired and released in the code below. We
643 * cannot be making fancy procedure calls!
647 * We can cache the chain if it is associated with a pmp
648 * and not flagged as being destroyed or requesting a full
649 * release. In this situation the chain is not removed
650 * from its parent, i.e. it can still be looked up.
652 * We intentionally do not cache DATA chains because these
653 * were likely used to load data into the logical buffer cache
654 * and will not be accessed again for some time.
657 (HAMMER2_CHAIN_DESTROY | HAMMER2_CHAIN_RELEASE)) == 0 &&
659 chain->bref.type != HAMMER2_BREF_TYPE_DATA) {
661 hammer2_spin_ex(&parent->core.spin);
662 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
664 * 1->0 transition failed, retry. Do not drop
665 * the chain's data yet!
668 hammer2_spin_unex(&parent->core.spin);
669 hammer2_spin_unex(&chain->core.spin);
670 hammer2_mtx_unlock(&chain->lock);
678 hammer2_chain_assert_no_data(chain);
681 * Make sure we are on the LRU list, clean up excessive
682 * LRU entries. We can only really drop one but there might
683 * be other entries that we can remove from the lru_list
686 * NOTE: HAMMER2_CHAIN_ONLRU may only be safely set when
687 * chain->core.spin AND pmp->lru_spin are held, but
688 * can be safely cleared only holding pmp->lru_spin.
690 if ((chain->flags & HAMMER2_CHAIN_ONLRU) == 0) {
691 hammer2_spin_ex(&pmp->lru_spin);
692 if ((chain->flags & HAMMER2_CHAIN_ONLRU) == 0) {
693 atomic_set_int(&chain->flags,
694 HAMMER2_CHAIN_ONLRU);
695 TAILQ_INSERT_TAIL(&pmp->lru_list,
697 atomic_add_int(&pmp->lru_count, 1);
699 if (pmp->lru_count < HAMMER2_LRU_LIMIT)
700 depth = 1; /* disable lru_list flush */
701 hammer2_spin_unex(&pmp->lru_spin);
703 /* disable lru flush */
708 hammer2_spin_unex(&parent->core.spin);
709 parent = NULL; /* safety */
711 hammer2_spin_unex(&chain->core.spin);
712 hammer2_mtx_unlock(&chain->lock);
715 * lru_list hysteresis (see above for depth overrides).
716 * Note that depth also prevents excessive lastdrop recursion.
719 hammer2_chain_lru_flush(pmp);
726 * Make sure we are not on the LRU list.
728 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
729 hammer2_spin_ex(&pmp->lru_spin);
730 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
731 atomic_add_int(&pmp->lru_count, -1);
732 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
733 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
735 hammer2_spin_unex(&pmp->lru_spin);
739 * Spinlock the parent and try to drop the last ref on chain.
740 * On success determine if we should dispose of the chain
741 * (remove the chain from its parent, etc).
743 * (normal core locks are top-down recursive but we define
744 * core spinlocks as bottom-up recursive, so this is safe).
747 hammer2_spin_ex(&parent->core.spin);
748 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
750 * 1->0 transition failed, retry.
752 hammer2_spin_unex(&parent->core.spin);
753 hammer2_spin_unex(&chain->core.spin);
754 hammer2_mtx_unlock(&chain->lock);
760 * 1->0 transition successful, parent spin held to prevent
761 * new lookups, chain spinlock held to protect parent field.
762 * Remove chain from the parent.
764 * If the chain is being removed from the parent's btree but
765 * is not bmapped, we have to adjust live_count downward. If
766 * it is bmapped then the blockref is retained in the parent
767 * as is its associated live_count. This case can occur when
768 * a chain added to the topology is unable to flush and is
769 * then later deleted.
771 if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
772 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) &&
773 (chain->flags & HAMMER2_CHAIN_BMAPPED) == 0) {
774 atomic_add_int(&parent->core.live_count, -1);
776 RB_REMOVE(hammer2_chain_tree,
777 &parent->core.rbtree, chain);
778 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
779 --parent->core.chain_count;
780 chain->parent = NULL;
784 * If our chain was the last chain in the parent's core the
785 * core is now empty and its parent might have to be
786 * re-dropped if it has 0 refs.
788 if (parent->core.chain_count == 0) {
790 atomic_add_int(&rdrop->refs, 1);
792 if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0)
796 hammer2_spin_unex(&parent->core.spin);
797 parent = NULL; /* safety */
803 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
805 * 1->0 transition failed, retry.
807 hammer2_spin_unex(&parent->core.spin);
808 hammer2_spin_unex(&chain->core.spin);
809 hammer2_mtx_unlock(&chain->lock);
816 * Successful 1->0 transition, no parent, no children... no way for
817 * anyone to ref this chain any more. We can clean-up and free it.
819 * We still have the core spinlock, and core's chain_count is 0.
820 * Any parent spinlock is gone.
822 hammer2_spin_unex(&chain->core.spin);
823 hammer2_chain_assert_no_data(chain);
824 hammer2_mtx_unlock(&chain->lock);
825 KKASSERT(RB_EMPTY(&chain->core.rbtree) &&
826 chain->core.chain_count == 0);
829 * All locks are gone, no pointers remain to the chain, finish
832 KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE |
833 HAMMER2_CHAIN_MODIFIED)) == 0);
836 * Once chain resources are gone we can use the now dead chain
837 * structure to placehold what might otherwise require a recursive
838 * drop, because we have potentially two things to drop and can only
839 * return one directly.
841 if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
842 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ALLOCATED);
844 kfree(chain, hmp->mchain);
848 * Possible chaining loop when parent re-drop needed.
854 * Heuristical flush of the LRU, try to reduce the number of entries
855 * on the LRU to (HAMMER2_LRU_LIMIT * 2 / 3). This procedure is called
856 * only when lru_count exceeds HAMMER2_LRU_LIMIT.
860 hammer2_chain_lru_flush(hammer2_pfs_t *pmp)
862 hammer2_chain_t *chain;
866 hammer2_spin_ex(&pmp->lru_spin);
867 while (pmp->lru_count > HAMMER2_LRU_LIMIT * 2 / 3) {
869 * Pick a chain off the lru_list, just recycle it quickly
870 * if LRUHINT is set (the chain was ref'd but left on
871 * the lru_list, so cycle to the end).
873 chain = TAILQ_FIRST(&pmp->lru_list);
874 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
876 if (chain->flags & HAMMER2_CHAIN_LRUHINT) {
877 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_LRUHINT);
878 TAILQ_INSERT_TAIL(&pmp->lru_list, chain, lru_node);
884 * Ok, we are off the LRU. We must adjust refs before we
885 * can safely clear the ONLRU flag.
887 atomic_add_int(&pmp->lru_count, -1);
888 if (atomic_cmpset_int(&chain->refs, 0, 1)) {
889 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
890 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
893 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
896 hammer2_spin_unex(&pmp->lru_spin);
901 * If we picked a chain off the lru list we may be able to lastdrop
902 * it. Use a depth of 1 to prevent excessive lastdrop recursion.
912 if (hammer2_mtx_ex_try(&chain->lock) == 0)
913 chain = hammer2_chain_lastdrop(chain, 1);
914 /* retry the same chain, or chain from lastdrop */
916 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
918 /* retry the same chain */
926 * On last lock release.
928 static hammer2_io_t *
929 hammer2_chain_drop_data(hammer2_chain_t *chain)
933 if ((dio = chain->dio) != NULL) {
937 switch(chain->bref.type) {
938 case HAMMER2_BREF_TYPE_VOLUME:
939 case HAMMER2_BREF_TYPE_FREEMAP:
942 if (chain->data != NULL) {
943 hammer2_spin_unex(&chain->core.spin);
944 panic("chain data not null: "
945 "chain %p bref %016jx.%02x "
946 "refs %d parent %p dio %p data %p",
947 chain, chain->bref.data_off,
948 chain->bref.type, chain->refs,
950 chain->dio, chain->data);
952 KKASSERT(chain->data == NULL);
960 * Lock a referenced chain element, acquiring its data with I/O if necessary,
961 * and specify how you would like the data to be resolved.
963 * If an I/O or other fatal error occurs, chain->error will be set to non-zero.
965 * The lock is allowed to recurse, multiple locking ops will aggregate
966 * the requested resolve types. Once data is assigned it will not be
967 * removed until the last unlock.
969 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
970 * (typically used to avoid device/logical buffer
973 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
974 * the INITIAL-create state (indirect blocks only).
976 * Do not resolve data elements for DATA chains.
977 * (typically used to avoid device/logical buffer
980 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
982 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
983 * it will be locked exclusive.
985 * HAMMER2_RESOLVE_NONBLOCK- (flag) The chain is locked non-blocking. If
986 * the lock fails, EAGAIN is returned.
988 * NOTE: Embedded elements (volume header, inodes) are always resolved
991 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
992 * element will instantiate and zero its buffer, and flush it on
995 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
996 * so as not to instantiate a device buffer, which could alias against
997 * a logical file buffer. However, if ALWAYS is specified the
998 * device buffer will be instantiated anyway.
1000 * NOTE: The return value is always 0 unless NONBLOCK is specified, in which
1001 * case it can be either 0 or EAGAIN.
1003 * WARNING! This function blocks on I/O if data needs to be fetched. This
1004 * blocking can run concurrent with other compatible lock holders
1005 * who do not need data returning. The lock is not upgraded to
1006 * exclusive during a data fetch, a separate bit is used to
1007 * interlock I/O. However, an exclusive lock holder can still count
1008 * on being interlocked against an I/O fetch managed by a shared
1012 hammer2_chain_lock(hammer2_chain_t *chain, int how)
1014 KKASSERT(chain->refs > 0);
1016 if (how & HAMMER2_RESOLVE_NONBLOCK) {
1018 * We still have to bump lockcnt before acquiring the lock,
1019 * even for non-blocking operation, because the unlock code
1020 * live-loops on lockcnt == 1 when dropping the last lock.
1022 * If the non-blocking operation fails we have to use an
1023 * unhold sequence to undo the mess.
1025 * NOTE: LOCKAGAIN must always succeed without blocking,
1026 * even if NONBLOCK is specified.
1028 atomic_add_int(&chain->lockcnt, 1);
1029 if (how & HAMMER2_RESOLVE_SHARED) {
1030 if (how & HAMMER2_RESOLVE_LOCKAGAIN) {
1031 hammer2_mtx_sh_again(&chain->lock);
1033 if (hammer2_mtx_sh_try(&chain->lock) != 0) {
1034 hammer2_chain_unhold(chain);
1039 if (hammer2_mtx_ex_try(&chain->lock) != 0) {
1040 hammer2_chain_unhold(chain);
1046 * Get the appropriate lock. If LOCKAGAIN is flagged with
1047 * SHARED the caller expects a shared lock to already be
1048 * present and we are giving it another ref. This case must
1049 * importantly not block if there is a pending exclusive lock
1052 atomic_add_int(&chain->lockcnt, 1);
1053 if (how & HAMMER2_RESOLVE_SHARED) {
1054 if (how & HAMMER2_RESOLVE_LOCKAGAIN) {
1055 hammer2_mtx_sh_again(&chain->lock);
1057 hammer2_mtx_sh(&chain->lock);
1060 hammer2_mtx_ex(&chain->lock);
1065 * If we already have a valid data pointer make sure the data is
1066 * synchronized to the current cpu, and then no further action is
1071 hammer2_io_bkvasync(chain->dio);
1076 * Do we have to resolve the data? This is generally only
1077 * applicable to HAMMER2_BREF_TYPE_DATA which is special-cased.
1078 * Other BREF types expects the data to be there.
1080 switch(how & HAMMER2_RESOLVE_MASK) {
1081 case HAMMER2_RESOLVE_NEVER:
1083 case HAMMER2_RESOLVE_MAYBE:
1084 if (chain->flags & HAMMER2_CHAIN_INITIAL)
1086 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
1089 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
1091 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
1095 case HAMMER2_RESOLVE_ALWAYS:
1101 * Caller requires data
1103 hammer2_chain_load_data(chain);
1109 * Lock the chain, retain the hold, and drop the data persistence count.
1110 * The data should remain valid because we never transitioned lockcnt
1114 hammer2_chain_lock_unhold(hammer2_chain_t *chain, int how)
1116 hammer2_chain_lock(chain, how);
1117 atomic_add_int(&chain->lockcnt, -1);
1122 * Downgrade an exclusive chain lock to a shared chain lock.
1124 * NOTE: There is no upgrade equivalent due to the ease of
1125 * deadlocks in that direction.
1128 hammer2_chain_lock_downgrade(hammer2_chain_t *chain)
1130 hammer2_mtx_downgrade(&chain->lock);
1135 * Issue I/O and install chain->data. Caller must hold a chain lock, lock
1136 * may be of any type.
1138 * Once chain->data is set it cannot be disposed of until all locks are
1141 * Make sure the data is synchronized to the current cpu.
1144 hammer2_chain_load_data(hammer2_chain_t *chain)
1146 hammer2_blockref_t *bref;
1153 * Degenerate case, data already present, or chain has no media
1154 * reference to load.
1156 KKASSERT(chain->lock.mtx_lock & MTX_MASK);
1159 hammer2_io_bkvasync(chain->dio);
1162 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0)
1166 KKASSERT(hmp != NULL);
1169 * Gain the IOINPROG bit, interlocked block.
1175 oflags = chain->flags;
1177 if (oflags & HAMMER2_CHAIN_IOINPROG) {
1178 nflags = oflags | HAMMER2_CHAIN_IOSIGNAL;
1179 tsleep_interlock(&chain->flags, 0);
1180 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1181 tsleep(&chain->flags, PINTERLOCKED,
1186 nflags = oflags | HAMMER2_CHAIN_IOINPROG;
1187 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1195 * We own CHAIN_IOINPROG
1197 * Degenerate case if we raced another load.
1201 hammer2_io_bkvasync(chain->dio);
1206 * We must resolve to a device buffer, either by issuing I/O or
1207 * by creating a zero-fill element. We do not mark the buffer
1208 * dirty when creating a zero-fill element (the hammer2_chain_modify()
1209 * API must still be used to do that).
1211 * The device buffer is variable-sized in powers of 2 down
1212 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
1213 * chunk always contains buffers of the same size. (XXX)
1215 * The minimum physical IO size may be larger than the variable
1218 bref = &chain->bref;
1221 * The getblk() optimization can only be used on newly created
1222 * elements if the physical block size matches the request.
1224 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1225 error = hammer2_io_new(hmp, bref->type,
1226 bref->data_off, chain->bytes,
1229 error = hammer2_io_bread(hmp, bref->type,
1230 bref->data_off, chain->bytes,
1232 hammer2_adjreadcounter(&chain->bref, chain->bytes);
1235 chain->error = HAMMER2_ERROR_EIO;
1236 kprintf("hammer2_chain_lock: I/O error %016jx: %d\n",
1237 (intmax_t)bref->data_off, error);
1238 hammer2_io_bqrelse(&chain->dio);
1244 * This isn't perfect and can be ignored on OSs which do not have
1245 * an indication as to whether a buffer is coming from cache or
1246 * if I/O was actually issued for the read. TESTEDGOOD will work
1247 * pretty well without the B_IOISSUED logic because chains are
1248 * cached, but in that situation (without B_IOISSUED) it will not
1249 * detect whether a re-read via I/O is corrupted verses the original
1252 * We can't re-run the CRC on every fresh lock. That would be
1253 * insanely expensive.
1255 * If the underlying kernel buffer covers the entire chain we can
1256 * use the B_IOISSUED indication to determine if we have to re-run
1257 * the CRC on chain data for chains that managed to stay cached
1258 * across the kernel disposal of the original buffer.
1260 if ((dio = chain->dio) != NULL && dio->bp) {
1261 struct buf *bp = dio->bp;
1263 if (dio->psize == chain->bytes &&
1264 (bp->b_flags & B_IOISSUED)) {
1265 atomic_clear_int(&chain->flags,
1266 HAMMER2_CHAIN_TESTEDGOOD);
1267 bp->b_flags &= ~B_IOISSUED;
1272 * NOTE: A locked chain's data cannot be modified without first
1273 * calling hammer2_chain_modify().
1277 * NOTE: hammer2_io_data() call issues bkvasync()
1279 bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
1281 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1283 * Clear INITIAL. In this case we used io_new() and the
1284 * buffer has been zero'd and marked dirty.
1286 * CHAIN_MODIFIED has not been set yet, and we leave it
1287 * that way for now. Set a temporary CHAIN_NOTTESTED flag
1288 * to prevent hammer2_chain_testcheck() from trying to match
1289 * a check code that has not yet been generated. This bit
1290 * should NOT end up on the actual media.
1292 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1293 atomic_set_int(&chain->flags, HAMMER2_CHAIN_NOTTESTED);
1294 } else if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
1296 * check data not currently synchronized due to
1297 * modification. XXX assumes data stays in the buffer
1298 * cache, which might not be true (need biodep on flush
1299 * to calculate crc? or simple crc?).
1301 } else if ((chain->flags & HAMMER2_CHAIN_TESTEDGOOD) == 0) {
1302 if (hammer2_chain_testcheck(chain, bdata) == 0) {
1303 chain->error = HAMMER2_ERROR_CHECK;
1305 atomic_set_int(&chain->flags, HAMMER2_CHAIN_TESTEDGOOD);
1310 * Setup the data pointer, either pointing it to an embedded data
1311 * structure and copying the data from the buffer, or pointing it
1314 * The buffer is not retained when copying to an embedded data
1315 * structure in order to avoid potential deadlocks or recursions
1316 * on the same physical buffer.
1318 * WARNING! Other threads can start using the data the instant we
1319 * set chain->data non-NULL.
1321 switch (bref->type) {
1322 case HAMMER2_BREF_TYPE_VOLUME:
1323 case HAMMER2_BREF_TYPE_FREEMAP:
1325 * Copy data from bp to embedded buffer
1327 panic("hammer2_chain_load_data: unresolved volume header");
1329 case HAMMER2_BREF_TYPE_DIRENT:
1330 KKASSERT(chain->bytes != 0);
1332 case HAMMER2_BREF_TYPE_INODE:
1333 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1334 case HAMMER2_BREF_TYPE_INDIRECT:
1335 case HAMMER2_BREF_TYPE_DATA:
1336 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1339 * Point data at the device buffer and leave dio intact.
1341 chain->data = (void *)bdata;
1346 * Release HAMMER2_CHAIN_IOINPROG and signal waiters if requested.
1353 oflags = chain->flags;
1354 nflags = oflags & ~(HAMMER2_CHAIN_IOINPROG |
1355 HAMMER2_CHAIN_IOSIGNAL);
1356 KKASSERT(oflags & HAMMER2_CHAIN_IOINPROG);
1357 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1358 if (oflags & HAMMER2_CHAIN_IOSIGNAL)
1359 wakeup(&chain->flags);
1366 * Unlock and deref a chain element.
1368 * Remember that the presence of children under chain prevent the chain's
1369 * destruction but do not add additional references, so the dio will still
1373 hammer2_chain_unlock(hammer2_chain_t *chain)
1380 * If multiple locks are present (or being attempted) on this
1381 * particular chain we can just unlock, drop refs, and return.
1383 * Otherwise fall-through on the 1->0 transition.
1386 lockcnt = chain->lockcnt;
1387 KKASSERT(lockcnt > 0);
1390 if (atomic_cmpset_int(&chain->lockcnt,
1391 lockcnt, lockcnt - 1)) {
1392 hammer2_mtx_unlock(&chain->lock);
1395 } else if (hammer2_mtx_upgrade_try(&chain->lock) == 0) {
1396 /* while holding the mutex exclusively */
1397 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
1401 * This situation can easily occur on SMP due to
1402 * the gap inbetween the 1->0 transition and the
1403 * final unlock. We cannot safely block on the
1404 * mutex because lockcnt might go above 1.
1406 * XXX Sleep for one tick if it takes too long.
1408 if (++iter > 1000) {
1409 if (iter > 1000 + hz) {
1410 kprintf("hammer2: h2race2 %p\n", chain);
1413 tsleep(&iter, 0, "h2race2", 1);
1421 * Last unlock / mutex upgraded to exclusive. Drop the data
1424 dio = hammer2_chain_drop_data(chain);
1426 hammer2_io_bqrelse(&dio);
1427 hammer2_mtx_unlock(&chain->lock);
1431 * Unlock and hold chain data intact
1434 hammer2_chain_unlock_hold(hammer2_chain_t *chain)
1436 atomic_add_int(&chain->lockcnt, 1);
1437 hammer2_chain_unlock(chain);
1441 * Helper to obtain the blockref[] array base and count for a chain.
1443 * XXX Not widely used yet, various use cases need to be validated and
1444 * converted to use this function.
1447 hammer2_blockref_t *
1448 hammer2_chain_base_and_count(hammer2_chain_t *parent, int *countp)
1450 hammer2_blockref_t *base;
1453 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1456 switch(parent->bref.type) {
1457 case HAMMER2_BREF_TYPE_INODE:
1458 count = HAMMER2_SET_COUNT;
1460 case HAMMER2_BREF_TYPE_INDIRECT:
1461 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1462 count = parent->bytes / sizeof(hammer2_blockref_t);
1464 case HAMMER2_BREF_TYPE_VOLUME:
1465 count = HAMMER2_SET_COUNT;
1467 case HAMMER2_BREF_TYPE_FREEMAP:
1468 count = HAMMER2_SET_COUNT;
1471 panic("hammer2_chain_base_and_count: "
1472 "unrecognized blockref type: %d",
1478 switch(parent->bref.type) {
1479 case HAMMER2_BREF_TYPE_INODE:
1480 base = &parent->data->ipdata.u.blockset.blockref[0];
1481 count = HAMMER2_SET_COUNT;
1483 case HAMMER2_BREF_TYPE_INDIRECT:
1484 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1485 base = &parent->data->npdata[0];
1486 count = parent->bytes / sizeof(hammer2_blockref_t);
1488 case HAMMER2_BREF_TYPE_VOLUME:
1489 base = &parent->data->voldata.
1490 sroot_blockset.blockref[0];
1491 count = HAMMER2_SET_COUNT;
1493 case HAMMER2_BREF_TYPE_FREEMAP:
1494 base = &parent->data->blkset.blockref[0];
1495 count = HAMMER2_SET_COUNT;
1498 panic("hammer2_chain_base_and_count: "
1499 "unrecognized blockref type: %d",
1511 * This counts the number of live blockrefs in a block array and
1512 * also calculates the point at which all remaining blockrefs are empty.
1513 * This routine can only be called on a live chain.
1515 * Caller holds the chain locked, but possibly with a shared lock. We
1516 * must use an exclusive spinlock to prevent corruption.
1518 * NOTE: Flag is not set until after the count is complete, allowing
1519 * callers to test the flag without holding the spinlock.
1521 * NOTE: If base is NULL the related chain is still in the INITIAL
1522 * state and there are no blockrefs to count.
1524 * NOTE: live_count may already have some counts accumulated due to
1525 * creation and deletion and could even be initially negative.
1528 hammer2_chain_countbrefs(hammer2_chain_t *chain,
1529 hammer2_blockref_t *base, int count)
1531 hammer2_spin_ex(&chain->core.spin);
1532 if ((chain->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) {
1534 while (--count >= 0) {
1535 if (base[count].type != HAMMER2_BREF_TYPE_EMPTY)
1538 chain->core.live_zero = count + 1;
1539 while (count >= 0) {
1540 if (base[count].type != HAMMER2_BREF_TYPE_EMPTY)
1541 atomic_add_int(&chain->core.live_count,
1546 chain->core.live_zero = 0;
1548 /* else do not modify live_count */
1549 atomic_set_int(&chain->flags, HAMMER2_CHAIN_COUNTEDBREFS);
1551 hammer2_spin_unex(&chain->core.spin);
1555 * Resize the chain's physical storage allocation in-place. This function does
1556 * not usually adjust the data pointer and must be followed by (typically) a
1557 * hammer2_chain_modify() call to copy any old data over and adjust the
1560 * Chains can be resized smaller without reallocating the storage. Resizing
1561 * larger will reallocate the storage. Excess or prior storage is reclaimed
1562 * asynchronously at a later time.
1564 * An nradix value of 0 is special-cased to mean that the storage should
1565 * be disassociated, that is the chain is being resized to 0 bytes (not 1
1568 * Must be passed an exclusively locked parent and chain.
1570 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
1571 * to avoid instantiating a device buffer that conflicts with the vnode data
1572 * buffer. However, because H2 can compress or encrypt data, the chain may
1573 * have a dio assigned to it in those situations, and they do not conflict.
1575 * XXX return error if cannot resize.
1578 hammer2_chain_resize(hammer2_chain_t *chain,
1579 hammer2_tid_t mtid, hammer2_off_t dedup_off,
1580 int nradix, int flags)
1590 * Only data and indirect blocks can be resized for now.
1591 * (The volu root, inodes, and freemap elements use a fixed size).
1593 KKASSERT(chain != &hmp->vchain);
1594 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1595 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1596 chain->bref.type == HAMMER2_BREF_TYPE_DIRENT);
1599 * Nothing to do if the element is already the proper size
1601 obytes = chain->bytes;
1602 nbytes = (nradix) ? (1U << nradix) : 0;
1603 if (obytes == nbytes)
1604 return (chain->error);
1607 * Make sure the old data is instantiated so we can copy it. If this
1608 * is a data block, the device data may be superfluous since the data
1609 * might be in a logical block, but compressed or encrypted data is
1612 * NOTE: The modify will set BMAPUPD for us if BMAPPED is set.
1614 error = hammer2_chain_modify(chain, mtid, dedup_off, 0);
1619 * Reallocate the block, even if making it smaller (because different
1620 * block sizes may be in different regions).
1622 * NOTE: Operation does not copy the data and may only be used
1623 * to resize data blocks in-place, or directory entry blocks
1624 * which are about to be modified in some manner.
1626 error = hammer2_freemap_alloc(chain, nbytes);
1630 chain->bytes = nbytes;
1633 * We don't want the followup chain_modify() to try to copy data
1634 * from the old (wrong-sized) buffer. It won't know how much to
1635 * copy. This case should only occur during writes when the
1636 * originator already has the data to write in-hand.
1639 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1640 chain->bref.type == HAMMER2_BREF_TYPE_DIRENT);
1641 hammer2_io_brelse(&chain->dio);
1644 return (chain->error);
1648 * Set the chain modified so its data can be changed by the caller, or
1649 * install deduplicated data. The caller must call this routine for each
1650 * set of modifications it makes, even if the chain is already flagged
1653 * Sets bref.modify_tid to mtid only if mtid != 0. Note that bref.modify_tid
1654 * is a CLC (cluster level change) field and is not updated by parent
1655 * propagation during a flush.
1657 * Returns an appropriate HAMMER2_ERROR_* code, which will generally reflect
1658 * chain->error except for HAMMER2_ERROR_ENOSPC. If the allocation fails
1659 * due to no space available, HAMMER2_ERROR_ENOSPC is returned and the chain
1660 * remains unmodified with its old data ref intact and chain->error
1665 * If the DEDUPABLE flag is set in the chain the storage must be reallocated
1666 * even if the chain is still flagged MODIFIED. In this case the chain's
1667 * DEDUPABLE flag will be cleared once the new storage has been assigned.
1669 * If the caller passes a non-zero dedup_off we will use it to assign the
1670 * new storage. The MODIFIED flag will be *CLEARED* in this case, and
1671 * DEDUPABLE will be set (NOTE: the UPDATE flag is always set). The caller
1672 * must not modify the data content upon return.
1675 hammer2_chain_modify(hammer2_chain_t *chain, hammer2_tid_t mtid,
1676 hammer2_off_t dedup_off, int flags)
1678 hammer2_blockref_t obref;
1689 obref = chain->bref;
1690 KKASSERT((chain->flags & HAMMER2_CHAIN_FICTITIOUS) == 0);
1691 KKASSERT(chain->lock.mtx_lock & MTX_EXCLUSIVE);
1694 * Data is not optional for freemap chains (we must always be sure
1695 * to copy the data on COW storage allocations).
1697 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1698 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1699 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1700 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1704 * Data must be resolved if already assigned, unless explicitly
1705 * flagged otherwise. If we cannot safety load the data the
1706 * modification fails and we return early.
1708 if (chain->data == NULL && chain->bytes != 0 &&
1709 (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1710 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1711 hammer2_chain_load_data(chain);
1713 return (chain->error);
1718 * Set MODIFIED to indicate that the chain has been modified. A new
1719 * allocation is required when modifying a chain.
1721 * Set UPDATE to ensure that the blockref is updated in the parent.
1723 * If MODIFIED is already set determine if we can reuse the assigned
1724 * data block or if we need a new data block.
1726 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1728 * Must set modified bit.
1730 atomic_add_long(&hammer2_count_modified_chains, 1);
1731 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1732 hammer2_pfs_memory_inc(chain->pmp); /* can be NULL */
1736 * We may be able to avoid a copy-on-write if the chain's
1737 * check mode is set to NONE and the chain's current
1738 * modify_tid is beyond the last explicit snapshot tid.
1740 * This implements HAMMER2's overwrite-in-place feature.
1742 * NOTE! This data-block cannot be used as a de-duplication
1743 * source when the check mode is set to NONE.
1745 if ((chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1746 chain->bref.type == HAMMER2_BREF_TYPE_DIRENT) &&
1747 (chain->flags & HAMMER2_CHAIN_INITIAL) == 0 &&
1748 (chain->flags & HAMMER2_CHAIN_DEDUPABLE) == 0 &&
1749 HAMMER2_DEC_CHECK(chain->bref.methods) ==
1750 HAMMER2_CHECK_NONE &&
1752 chain->bref.modify_tid >
1753 chain->pmp->iroot->meta.pfs_lsnap_tid) {
1755 * Sector overwrite allowed.
1758 } else if ((hmp->hflags & HMNT2_EMERG) &&
1760 chain->bref.modify_tid >
1761 chain->pmp->iroot->meta.pfs_lsnap_tid) {
1763 * If in emergency delete mode then do a modify-in-
1764 * place on any chain type belonging to the PFS as
1765 * long as it doesn't mess up a snapshot. We might
1766 * be forced to do this anyway a little further down
1767 * in the code if the allocation fails.
1769 * Also note that in emergency mode, these modify-in-
1770 * place operations are NOT SAFE. A storage failure,
1771 * power failure, or panic can corrupt the filesystem.
1776 * Sector overwrite not allowed, must copy-on-write.
1780 } else if (chain->flags & HAMMER2_CHAIN_DEDUPABLE) {
1782 * If the modified chain was registered for dedup we need
1783 * a new allocation. This only happens for delayed-flush
1784 * chains (i.e. which run through the front-end buffer
1791 * Already flagged modified, no new allocation is needed.
1798 * Flag parent update required.
1800 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
1801 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1808 * The XOP code returns held but unlocked focus chains. This
1809 * prevents the chain from being destroyed but does not prevent
1810 * it from being modified. diolk is used to interlock modifications
1811 * against XOP frontend accesses to the focus.
1813 * This allows us to theoretically avoid deadlocking the frontend
1814 * if one of the backends lock up by not formally locking the
1815 * focused chain in the frontend. In addition, the synchronization
1816 * code relies on this mechanism to avoid deadlocking concurrent
1817 * synchronization threads.
1819 lockmgr(&chain->diolk, LK_EXCLUSIVE);
1822 * The modification or re-modification requires an allocation and
1823 * possible COW. If an error occurs, the previous content and data
1824 * reference is retained and the modification fails.
1826 * If dedup_off is non-zero, the caller is requesting a deduplication
1827 * rather than a modification. The MODIFIED bit is not set and the
1828 * data offset is set to the deduplication offset. The data cannot
1831 * NOTE: The dedup offset is allowed to be in a partially free state
1832 * and we must be sure to reset it to a fully allocated state
1833 * to force two bulkfree passes to free it again.
1835 * NOTE: Only applicable when chain->bytes != 0.
1837 * XXX can a chain already be marked MODIFIED without a data
1838 * assignment? If not, assert here instead of testing the case.
1840 if (chain != &hmp->vchain && chain != &hmp->fchain &&
1842 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1846 * NOTE: We do not have to remove the dedup
1847 * registration because the area is still
1848 * allocated and the underlying DIO will
1852 chain->bref.data_off = dedup_off;
1853 chain->bytes = 1 << (dedup_off &
1854 HAMMER2_OFF_MASK_RADIX);
1856 atomic_clear_int(&chain->flags,
1857 HAMMER2_CHAIN_MODIFIED);
1858 atomic_add_long(&hammer2_count_modified_chains,
1861 hammer2_pfs_memory_wakeup(
1864 hammer2_freemap_adjust(hmp, &chain->bref,
1865 HAMMER2_FREEMAP_DORECOVER);
1866 atomic_set_int(&chain->flags,
1867 HAMMER2_CHAIN_DEDUPABLE);
1869 error = hammer2_freemap_alloc(chain,
1871 atomic_clear_int(&chain->flags,
1872 HAMMER2_CHAIN_DEDUPABLE);
1875 * If we are unable to allocate a new block
1876 * but we are in emergency mode, issue a
1877 * warning to the console and reuse the same
1880 * We behave as if the allocation were
1883 * THIS IS IMPORTANT: These modifications
1884 * are virtually guaranteed to corrupt any
1885 * snapshots related to this filesystem.
1887 if (error && (hmp->hflags & HMNT2_EMERG)) {
1889 chain->bref.flags |=
1890 HAMMER2_BREF_FLAG_EMERG_MIP;
1892 krateprintf(&krate_h2em,
1893 "hammer2: Emergency Mode WARNING: "
1894 "Operation will likely corrupt "
1895 "related snapshot: "
1896 "%016jx.%02x key=%016jx\n",
1897 chain->bref.data_off,
1900 } else if (error == 0) {
1901 chain->bref.flags &=
1902 ~HAMMER2_BREF_FLAG_EMERG_MIP;
1909 * Stop here if error. We have to undo any flag bits we might
1914 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1915 atomic_add_long(&hammer2_count_modified_chains, -1);
1917 hammer2_pfs_memory_wakeup(chain->pmp, -1);
1920 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1922 lockmgr(&chain->diolk, LK_RELEASE);
1928 * Update mirror_tid and modify_tid. modify_tid is only updated
1929 * if not passed as zero (during flushes, parent propagation passes
1932 * NOTE: chain->pmp could be the device spmp.
1934 chain->bref.mirror_tid = hmp->voldata.mirror_tid + 1;
1936 chain->bref.modify_tid = mtid;
1939 * Set BMAPUPD to tell the flush code that an existing blockmap entry
1940 * requires updating as well as to tell the delete code that the
1941 * chain's blockref might not exactly match (in terms of physical size
1942 * or block offset) the one in the parent's blocktable. The base key
1943 * of course will still match.
1945 if (chain->flags & HAMMER2_CHAIN_BMAPPED)
1946 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD);
1949 * Short-cut data block handling when the caller does not need an
1950 * actual data reference to (aka OPTDATA), as long as the chain does
1951 * not already have a data pointer to the data and no de-duplication
1954 * This generally means that the modifications are being done via the
1955 * logical buffer cache.
1957 * NOTE: If deduplication occurred we have to run through the data
1958 * stuff to clear INITIAL, and the caller will likely want to
1959 * assign the check code anyway. Leaving INITIAL set on a
1960 * dedup can be deadly (it can cause the block to be zero'd!).
1962 * This code also handles bytes == 0 (most dirents).
1964 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA &&
1965 (flags & HAMMER2_MODIFY_OPTDATA) &&
1966 chain->data == NULL) {
1967 if (dedup_off == 0) {
1968 KKASSERT(chain->dio == NULL);
1974 * Clearing the INITIAL flag (for indirect blocks) indicates that
1975 * we've processed the uninitialized storage allocation.
1977 * If this flag is already clear we are likely in a copy-on-write
1978 * situation but we have to be sure NOT to bzero the storage if
1979 * no data is present.
1981 * Clearing of NOTTESTED is allowed if the MODIFIED bit is set,
1983 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1984 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1991 * Instantiate data buffer and possibly execute COW operation
1993 switch(chain->bref.type) {
1994 case HAMMER2_BREF_TYPE_VOLUME:
1995 case HAMMER2_BREF_TYPE_FREEMAP:
1997 * The data is embedded, no copy-on-write operation is
2000 KKASSERT(chain->dio == NULL);
2002 case HAMMER2_BREF_TYPE_DIRENT:
2004 * The data might be fully embedded.
2006 if (chain->bytes == 0) {
2007 KKASSERT(chain->dio == NULL);
2011 case HAMMER2_BREF_TYPE_INODE:
2012 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2013 case HAMMER2_BREF_TYPE_DATA:
2014 case HAMMER2_BREF_TYPE_INDIRECT:
2015 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2017 * Perform the copy-on-write operation
2019 * zero-fill or copy-on-write depending on whether
2020 * chain->data exists or not and set the dirty state for
2021 * the new buffer. hammer2_io_new() will handle the
2024 * If a dedup_off was supplied this is an existing block
2025 * and no COW, copy, or further modification is required.
2027 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
2029 if (wasinitial && dedup_off == 0) {
2030 error = hammer2_io_new(hmp, chain->bref.type,
2031 chain->bref.data_off,
2032 chain->bytes, &dio);
2034 error = hammer2_io_bread(hmp, chain->bref.type,
2035 chain->bref.data_off,
2036 chain->bytes, &dio);
2038 hammer2_adjreadcounter(&chain->bref, chain->bytes);
2041 * If an I/O error occurs make sure callers cannot accidently
2042 * modify the old buffer's contents and corrupt the filesystem.
2044 * NOTE: hammer2_io_data() call issues bkvasync()
2047 kprintf("hammer2_chain_modify: hmp=%p I/O error\n",
2049 chain->error = HAMMER2_ERROR_EIO;
2050 hammer2_io_brelse(&dio);
2051 hammer2_io_brelse(&chain->dio);
2056 bdata = hammer2_io_data(dio, chain->bref.data_off);
2060 * COW (unless a dedup).
2062 KKASSERT(chain->dio != NULL);
2063 if (chain->data != (void *)bdata && dedup_off == 0) {
2064 bcopy(chain->data, bdata, chain->bytes);
2066 } else if (wasinitial == 0 && dedup_off == 0) {
2068 * We have a problem. We were asked to COW but
2069 * we don't have any data to COW with!
2071 panic("hammer2_chain_modify: having a COW %p\n",
2076 * Retire the old buffer, replace with the new. Dirty or
2077 * redirty the new buffer.
2079 * WARNING! The system buffer cache may have already flushed
2080 * the buffer, so we must be sure to [re]dirty it
2081 * for further modification.
2083 * If dedup_off was supplied, the caller is not
2084 * expected to make any further modification to the
2087 * WARNING! hammer2_get_gdata() assumes dio never transitions
2088 * through NULL in order to optimize away unnecessary
2094 if ((tio = chain->dio) != NULL)
2095 hammer2_io_bqrelse(&tio);
2096 chain->data = (void *)bdata;
2099 hammer2_io_setdirty(dio);
2103 panic("hammer2_chain_modify: illegal non-embedded type %d",
2110 * setflush on parent indicating that the parent must recurse down
2111 * to us. Do not call on chain itself which might already have it
2115 hammer2_chain_setflush(chain->parent);
2116 lockmgr(&chain->diolk, LK_RELEASE);
2118 return (chain->error);
2122 * Modify the chain associated with an inode.
2125 hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
2126 hammer2_tid_t mtid, int flags)
2130 hammer2_inode_modify(ip);
2131 error = hammer2_chain_modify(chain, mtid, 0, flags);
2137 * Volume header data locks
2140 hammer2_voldata_lock(hammer2_dev_t *hmp)
2142 lockmgr(&hmp->vollk, LK_EXCLUSIVE);
2146 hammer2_voldata_unlock(hammer2_dev_t *hmp)
2148 lockmgr(&hmp->vollk, LK_RELEASE);
2152 hammer2_voldata_modify(hammer2_dev_t *hmp)
2154 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
2155 atomic_add_long(&hammer2_count_modified_chains, 1);
2156 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
2157 hammer2_pfs_memory_inc(hmp->vchain.pmp);
2162 * This function returns the chain at the nearest key within the specified
2163 * range. The returned chain will be referenced but not locked.
2165 * This function will recurse through chain->rbtree as necessary and will
2166 * return a *key_nextp suitable for iteration. *key_nextp is only set if
2167 * the iteration value is less than the current value of *key_nextp.
2169 * The caller should use (*key_nextp) to calculate the actual range of
2170 * the returned element, which will be (key_beg to *key_nextp - 1), because
2171 * there might be another element which is superior to the returned element
2174 * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
2175 * chains continue to be returned. On EOF (*key_nextp) may overflow since
2176 * it will wind up being (key_end + 1).
2178 * WARNING! Must be called with child's spinlock held. Spinlock remains
2179 * held through the operation.
2181 struct hammer2_chain_find_info {
2182 hammer2_chain_t *best;
2183 hammer2_key_t key_beg;
2184 hammer2_key_t key_end;
2185 hammer2_key_t key_next;
2188 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
2189 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
2193 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
2194 hammer2_key_t key_beg, hammer2_key_t key_end)
2196 struct hammer2_chain_find_info info;
2199 info.key_beg = key_beg;
2200 info.key_end = key_end;
2201 info.key_next = *key_nextp;
2203 RB_SCAN(hammer2_chain_tree, &parent->core.rbtree,
2204 hammer2_chain_find_cmp, hammer2_chain_find_callback,
2206 *key_nextp = info.key_next;
2208 kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
2209 parent, key_beg, key_end, *key_nextp);
2217 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
2219 struct hammer2_chain_find_info *info = data;
2220 hammer2_key_t child_beg;
2221 hammer2_key_t child_end;
2223 child_beg = child->bref.key;
2224 child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
2226 if (child_end < info->key_beg)
2228 if (child_beg > info->key_end)
2235 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
2237 struct hammer2_chain_find_info *info = data;
2238 hammer2_chain_t *best;
2239 hammer2_key_t child_end;
2242 * WARNING! Layerq is scanned forwards, exact matches should keep
2243 * the existing info->best.
2245 if ((best = info->best) == NULL) {
2247 * No previous best. Assign best
2250 } else if (best->bref.key <= info->key_beg &&
2251 child->bref.key <= info->key_beg) {
2256 /*info->best = child;*/
2257 } else if (child->bref.key < best->bref.key) {
2259 * Child has a nearer key and best is not flush with key_beg.
2260 * Set best to child. Truncate key_next to the old best key.
2263 if (info->key_next > best->bref.key || info->key_next == 0)
2264 info->key_next = best->bref.key;
2265 } else if (child->bref.key == best->bref.key) {
2267 * If our current best is flush with the child then this
2268 * is an illegal overlap.
2270 * key_next will automatically be limited to the smaller of
2271 * the two end-points.
2277 * Keep the current best but truncate key_next to the child's
2280 * key_next will also automatically be limited to the smaller
2281 * of the two end-points (probably not necessary for this case
2282 * but we do it anyway).
2284 if (info->key_next > child->bref.key || info->key_next == 0)
2285 info->key_next = child->bref.key;
2289 * Always truncate key_next based on child's end-of-range.
2291 child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
2292 if (child_end && (info->key_next > child_end || info->key_next == 0))
2293 info->key_next = child_end;
2299 * Retrieve the specified chain from a media blockref, creating the
2300 * in-memory chain structure which reflects it. The returned chain is
2301 * held and locked according to (how) (HAMMER2_RESOLVE_*). The caller must
2302 * handle crc-checks and so forth, and should check chain->error before
2303 * assuming that the data is good.
2305 * To handle insertion races pass the INSERT_RACE flag along with the
2306 * generation number of the core. NULL will be returned if the generation
2307 * number changes before we have a chance to insert the chain. Insert
2308 * races can occur because the parent might be held shared.
2310 * Caller must hold the parent locked shared or exclusive since we may
2311 * need the parent's bref array to find our block.
2313 * WARNING! chain->pmp is always set to NULL for any chain representing
2314 * part of the super-root topology.
2317 hammer2_chain_get(hammer2_chain_t *parent, int generation,
2318 hammer2_blockref_t *bref, int how)
2320 hammer2_dev_t *hmp = parent->hmp;
2321 hammer2_chain_t *chain;
2325 * Allocate a chain structure representing the existing media
2326 * entry. Resulting chain has one ref and is not locked.
2328 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
2329 chain = hammer2_chain_alloc(hmp, NULL, bref);
2331 chain = hammer2_chain_alloc(hmp, parent->pmp, bref);
2332 /* ref'd chain returned */
2335 * Flag that the chain is in the parent's blockmap so delete/flush
2336 * knows what to do with it.
2338 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
2341 * chain must be locked to avoid unexpected ripouts
2343 hammer2_chain_lock(chain, how);
2346 * Link the chain into its parent. A spinlock is required to safely
2347 * access the RBTREE, and it is possible to collide with another
2348 * hammer2_chain_get() operation because the caller might only hold
2349 * a shared lock on the parent.
2351 * NOTE: Get races can occur quite often when we distribute
2352 * asynchronous read-aheads across multiple threads.
2354 KKASSERT(parent->refs > 0);
2355 error = hammer2_chain_insert(parent, chain,
2356 HAMMER2_CHAIN_INSERT_SPIN |
2357 HAMMER2_CHAIN_INSERT_RACE,
2360 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
2361 /*kprintf("chain %p get race\n", chain);*/
2362 hammer2_chain_unlock(chain);
2363 hammer2_chain_drop(chain);
2366 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
2370 * Return our new chain referenced but not locked, or NULL if
2377 * Lookup initialization/completion API
2380 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
2382 hammer2_chain_ref(parent);
2383 if (flags & HAMMER2_LOOKUP_SHARED) {
2384 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
2385 HAMMER2_RESOLVE_SHARED);
2387 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2393 hammer2_chain_lookup_done(hammer2_chain_t *parent)
2396 hammer2_chain_unlock(parent);
2397 hammer2_chain_drop(parent);
2402 * Take the locked chain and return a locked parent. The chain remains
2403 * locked on return, but may have to be temporarily unlocked to acquire
2404 * the parent. Because of this, (chain) must be stable and cannot be
2405 * deleted while it was temporarily unlocked (typically means that (chain)
2408 * Pass HAMMER2_RESOLVE_* flags in flags.
2410 * This will work even if the chain is errored, and the caller can check
2411 * parent->error on return if desired since the parent will be locked.
2413 * This function handles the lock order reversal.
2416 hammer2_chain_getparent(hammer2_chain_t *chain, int flags)
2418 hammer2_chain_t *parent;
2421 * Be careful of order, chain must be unlocked before parent
2422 * is locked below to avoid a deadlock. Try it trivially first.
2424 parent = chain->parent;
2426 panic("hammer2_chain_getparent: no parent");
2427 hammer2_chain_ref(parent);
2428 if (hammer2_chain_lock(parent, flags|HAMMER2_RESOLVE_NONBLOCK) == 0)
2432 hammer2_chain_unlock(chain);
2433 hammer2_chain_lock(parent, flags);
2434 hammer2_chain_lock(chain, flags);
2437 * Parent relinking races are quite common. We have to get
2438 * it right or we will blow up the block table.
2440 if (chain->parent == parent)
2442 hammer2_chain_unlock(parent);
2443 hammer2_chain_drop(parent);
2445 parent = chain->parent;
2447 panic("hammer2_chain_getparent: no parent");
2448 hammer2_chain_ref(parent);
2454 * Take the locked chain and return a locked parent. The chain is unlocked
2455 * and dropped. *chainp is set to the returned parent as a convenience.
2456 * Pass HAMMER2_RESOLVE_* flags in flags.
2458 * This will work even if the chain is errored, and the caller can check
2459 * parent->error on return if desired since the parent will be locked.
2461 * The chain does NOT need to be stable. We use a tracking structure
2462 * to track the expected parent if the chain is deleted out from under us.
2464 * This function handles the lock order reversal.
2467 hammer2_chain_repparent(hammer2_chain_t **chainp, int flags)
2469 hammer2_chain_t *chain;
2470 hammer2_chain_t *parent;
2471 struct hammer2_reptrack reptrack;
2472 struct hammer2_reptrack **repp;
2475 * Be careful of order, chain must be unlocked before parent
2476 * is locked below to avoid a deadlock. Try it trivially first.
2479 parent = chain->parent;
2480 if (parent == NULL) {
2481 hammer2_spin_unex(&chain->core.spin);
2482 panic("hammer2_chain_repparent: no parent");
2484 hammer2_chain_ref(parent);
2485 if (hammer2_chain_lock(parent, flags|HAMMER2_RESOLVE_NONBLOCK) == 0) {
2486 hammer2_chain_unlock(chain);
2487 hammer2_chain_drop(chain);
2494 * Ok, now it gets a bit nasty. There are multiple situations where
2495 * the parent might be in the middle of a deletion, or where the child
2496 * (chain) might be deleted the instant we let go of its lock.
2497 * We can potentially end up in a no-win situation!
2499 * In particular, the indirect_maintenance() case can cause these
2502 * To deal with this we install a reptrack structure in the parent
2503 * This reptrack structure 'owns' the parent ref and will automatically
2504 * migrate to the parent's parent if the parent is deleted permanently.
2506 hammer2_spin_init(&reptrack.spin, "h2reptrk");
2507 reptrack.chain = parent;
2508 hammer2_chain_ref(parent); /* for the reptrack */
2510 hammer2_spin_ex(&parent->core.spin);
2511 reptrack.next = parent->core.reptrack;
2512 parent->core.reptrack = &reptrack;
2513 hammer2_spin_unex(&parent->core.spin);
2515 hammer2_chain_unlock(chain);
2516 hammer2_chain_drop(chain);
2517 chain = NULL; /* gone */
2520 * At the top of this loop, chain is gone and parent is refd both
2521 * by us explicitly AND via our reptrack. We are attempting to
2525 hammer2_chain_lock(parent, flags);
2527 if (reptrack.chain == parent)
2529 hammer2_chain_unlock(parent);
2530 hammer2_chain_drop(parent);
2532 kprintf("hammer2: debug REPTRACK %p->%p\n",
2533 parent, reptrack.chain);
2534 hammer2_spin_ex(&reptrack.spin);
2535 parent = reptrack.chain;
2536 hammer2_chain_ref(parent);
2537 hammer2_spin_unex(&reptrack.spin);
2541 * Once parent is locked and matches our reptrack, our reptrack
2542 * will be stable and we have our parent. We can unlink our
2545 * WARNING! Remember that the chain lock might be shared. Chains
2546 * locked shared have stable parent linkages.
2548 hammer2_spin_ex(&parent->core.spin);
2549 repp = &parent->core.reptrack;
2550 while (*repp != &reptrack)
2551 repp = &(*repp)->next;
2552 *repp = reptrack.next;
2553 hammer2_spin_unex(&parent->core.spin);
2555 hammer2_chain_drop(parent); /* reptrack ref */
2556 *chainp = parent; /* return parent lock+ref */
2562 * Dispose of any linked reptrack structures in (chain) by shifting them to
2563 * (parent). Both (chain) and (parent) must be exclusively locked.
2565 * This is interlocked against any children of (chain) on the other side.
2566 * No children so remain as-of when this is called so we can test
2567 * core.reptrack without holding the spin-lock.
2569 * Used whenever the caller intends to permanently delete chains related
2570 * to topological recursions (BREF_TYPE_INDIRECT, BREF_TYPE_FREEMAP_NODE),
2571 * where the chains underneath the node being deleted are given a new parent
2572 * above the node being deleted.
2576 hammer2_chain_repchange(hammer2_chain_t *parent, hammer2_chain_t *chain)
2578 struct hammer2_reptrack *reptrack;
2580 KKASSERT(chain->core.live_count == 0 && RB_EMPTY(&chain->core.rbtree));
2581 while (chain->core.reptrack) {
2582 hammer2_spin_ex(&parent->core.spin);
2583 hammer2_spin_ex(&chain->core.spin);
2584 reptrack = chain->core.reptrack;
2585 if (reptrack == NULL) {
2586 hammer2_spin_unex(&chain->core.spin);
2587 hammer2_spin_unex(&parent->core.spin);
2590 hammer2_spin_ex(&reptrack->spin);
2591 chain->core.reptrack = reptrack->next;
2592 reptrack->chain = parent;
2593 reptrack->next = parent->core.reptrack;
2594 parent->core.reptrack = reptrack;
2595 hammer2_chain_ref(parent); /* reptrack */
2597 hammer2_spin_unex(&chain->core.spin);
2598 hammer2_spin_unex(&parent->core.spin);
2599 kprintf("hammer2: debug repchange %p %p->%p\n",
2600 reptrack, chain, parent);
2601 hammer2_chain_drop(chain); /* reptrack */
2606 * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
2607 * (*parentp) typically points to an inode but can also point to a related
2608 * indirect block and this function will recurse upwards and find the inode
2609 * or the nearest undeleted indirect block covering the key range.
2611 * This function unconditionally sets *errorp, replacing any previous value.
2613 * (*parentp) must be exclusive or shared locked (depending on flags) and
2614 * referenced and can be an inode or an existing indirect block within the
2617 * If (*parent) is errored out, this function will not attempt to recurse
2618 * the radix tree and will return NULL along with an appropriate *errorp.
2619 * If NULL is returned and *errorp is 0, the requested lookup could not be
2622 * On return (*parentp) will be modified to point at the deepest parent chain
2623 * element encountered during the search, as a helper for an insertion or
2626 * The new (*parentp) will be locked shared or exclusive (depending on flags),
2627 * and referenced, and the old will be unlocked and dereferenced (no change
2628 * if they are both the same). This is particularly important if the caller
2629 * wishes to insert a new chain, (*parentp) will be set properly even if NULL
2630 * is returned, as long as no error occurred.
2632 * The matching chain will be returned locked according to flags.
2636 * NULL is returned if no match was found, but (*parentp) will still
2637 * potentially be adjusted.
2639 * On return (*key_nextp) will point to an iterative value for key_beg.
2640 * (If NULL is returned (*key_nextp) is set to (key_end + 1)).
2642 * This function will also recurse up the chain if the key is not within the
2643 * current parent's range. (*parentp) can never be set to NULL. An iteration
2644 * can simply allow (*parentp) to float inside the loop.
2646 * NOTE! chain->data is not always resolved. By default it will not be
2647 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
2648 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
2649 * BREF_TYPE_DATA as the device buffer can alias the logical file
2654 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
2655 hammer2_key_t key_beg, hammer2_key_t key_end,
2656 int *errorp, int flags)
2659 hammer2_chain_t *parent;
2660 hammer2_chain_t *chain;
2661 hammer2_blockref_t *base;
2662 hammer2_blockref_t *bref;
2663 hammer2_blockref_t bsave;
2664 hammer2_key_t scan_beg;
2665 hammer2_key_t scan_end;
2667 int how_always = HAMMER2_RESOLVE_ALWAYS;
2668 int how_maybe = HAMMER2_RESOLVE_MAYBE;
2671 int maxloops = 300000;
2672 volatile hammer2_mtx_t save_mtx;
2674 if (flags & HAMMER2_LOOKUP_ALWAYS) {
2675 how_maybe = how_always;
2676 how = HAMMER2_RESOLVE_ALWAYS;
2677 } else if (flags & HAMMER2_LOOKUP_NODATA) {
2678 how = HAMMER2_RESOLVE_NEVER;
2680 how = HAMMER2_RESOLVE_MAYBE;
2682 if (flags & HAMMER2_LOOKUP_SHARED) {
2683 how_maybe |= HAMMER2_RESOLVE_SHARED;
2684 how_always |= HAMMER2_RESOLVE_SHARED;
2685 how |= HAMMER2_RESOLVE_SHARED;
2689 * Recurse (*parentp) upward if necessary until the parent completely
2690 * encloses the key range or we hit the inode.
2692 * Handle races against the flusher deleting indirect nodes on its
2693 * way back up by continuing to recurse upward past the deletion.
2699 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2700 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2701 scan_beg = parent->bref.key;
2702 scan_end = scan_beg +
2703 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2704 if ((parent->flags & HAMMER2_CHAIN_DELETED) == 0) {
2705 if (key_beg >= scan_beg && key_end <= scan_end)
2708 parent = hammer2_chain_repparent(parentp, how_maybe);
2711 if (--maxloops == 0)
2712 panic("hammer2_chain_lookup: maxloops");
2715 * MATCHIND case that does not require parent->data (do prior to
2716 * parent->error check).
2718 switch(parent->bref.type) {
2719 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2720 case HAMMER2_BREF_TYPE_INDIRECT:
2721 if (flags & HAMMER2_LOOKUP_MATCHIND) {
2722 scan_beg = parent->bref.key;
2723 scan_end = scan_beg +
2724 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2725 if (key_beg == scan_beg && key_end == scan_end) {
2727 hammer2_chain_ref(chain);
2728 hammer2_chain_lock(chain, how_maybe);
2729 *key_nextp = scan_end + 1;
2739 * No lookup is possible if the parent is errored. We delayed
2740 * this check as long as we could to ensure that the parent backup,
2741 * embedded data, and MATCHIND code could still execute.
2743 if (parent->error) {
2744 *errorp = parent->error;
2749 * Locate the blockref array. Currently we do a fully associative
2750 * search through the array.
2752 switch(parent->bref.type) {
2753 case HAMMER2_BREF_TYPE_INODE:
2755 * Special shortcut for embedded data returns the inode
2756 * itself. Callers must detect this condition and access
2757 * the embedded data (the strategy code does this for us).
2759 * This is only applicable to regular files and softlinks.
2761 * We need a second lock on parent. Since we already have
2762 * a lock we must pass LOCKAGAIN to prevent unexpected
2763 * blocking (we don't want to block on a second shared
2764 * ref if an exclusive lock is pending)
2766 if (parent->data->ipdata.meta.op_flags &
2767 HAMMER2_OPFLAG_DIRECTDATA) {
2768 if (flags & HAMMER2_LOOKUP_NODIRECT) {
2770 *key_nextp = key_end + 1;
2773 hammer2_chain_ref(parent);
2774 hammer2_chain_lock(parent, how_always |
2775 HAMMER2_RESOLVE_LOCKAGAIN);
2776 *key_nextp = key_end + 1;
2779 base = &parent->data->ipdata.u.blockset.blockref[0];
2780 count = HAMMER2_SET_COUNT;
2782 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2783 case HAMMER2_BREF_TYPE_INDIRECT:
2785 * Optimize indirect blocks in the INITIAL state to avoid
2788 * Debugging: Enter permanent wait state instead of
2789 * panicing on unexpectedly NULL data for the moment.
2791 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2794 if (parent->data == NULL) {
2795 kprintf("hammer2: unexpected NULL data "
2798 tsleep(parent, 0, "xxx", 0);
2800 base = &parent->data->npdata[0];
2802 count = parent->bytes / sizeof(hammer2_blockref_t);
2804 case HAMMER2_BREF_TYPE_VOLUME:
2805 base = &parent->data->voldata.sroot_blockset.blockref[0];
2806 count = HAMMER2_SET_COUNT;
2808 case HAMMER2_BREF_TYPE_FREEMAP:
2809 base = &parent->data->blkset.blockref[0];
2810 count = HAMMER2_SET_COUNT;
2813 kprintf("hammer2_chain_lookup: unrecognized "
2814 "blockref(B) type: %d",
2817 tsleep(&base, 0, "dead", 0);
2818 panic("hammer2_chain_lookup: unrecognized "
2819 "blockref(B) type: %d",
2821 base = NULL; /* safety */
2822 count = 0; /* safety */
2826 * Merged scan to find next candidate.
2828 * hammer2_base_*() functions require the parent->core.live_* fields
2829 * to be synchronized.
2831 * We need to hold the spinlock to access the block array and RB tree
2832 * and to interlock chain creation.
2834 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2835 hammer2_chain_countbrefs(parent, base, count);
2840 hammer2_spin_ex(&parent->core.spin);
2841 chain = hammer2_combined_find(parent, base, count,
2845 generation = parent->core.generation;
2848 * Exhausted parent chain, iterate.
2851 KKASSERT(chain == NULL);
2852 hammer2_spin_unex(&parent->core.spin);
2853 if (key_beg == key_end) /* short cut single-key case */
2857 * Stop if we reached the end of the iteration.
2859 if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
2860 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2865 * Calculate next key, stop if we reached the end of the
2866 * iteration, otherwise go up one level and loop.
2868 key_beg = parent->bref.key +
2869 ((hammer2_key_t)1 << parent->bref.keybits);
2870 if (key_beg == 0 || key_beg > key_end)
2872 parent = hammer2_chain_repparent(parentp, how_maybe);
2877 * Selected from blockref or in-memory chain.
2880 if (chain == NULL) {
2881 hammer2_spin_unex(&parent->core.spin);
2882 if (bsave.type == HAMMER2_BREF_TYPE_INDIRECT ||
2883 bsave.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2884 chain = hammer2_chain_get(parent, generation,
2887 chain = hammer2_chain_get(parent, generation,
2893 hammer2_chain_ref(chain);
2894 hammer2_spin_unex(&parent->core.spin);
2897 * chain is referenced but not locked. We must lock the
2898 * chain to obtain definitive state.
2900 if (bsave.type == HAMMER2_BREF_TYPE_INDIRECT ||
2901 bsave.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2902 hammer2_chain_lock(chain, how_maybe);
2904 hammer2_chain_lock(chain, how);
2906 KKASSERT(chain->parent == parent);
2908 if (bcmp(&bsave, &chain->bref, sizeof(bsave)) ||
2909 chain->parent != parent) {
2910 hammer2_chain_unlock(chain);
2911 hammer2_chain_drop(chain);
2912 chain = NULL; /* SAFETY */
2918 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2920 * NOTE: Chain's key range is not relevant as there might be
2921 * one-offs within the range that are not deleted.
2923 * NOTE: Lookups can race delete-duplicate because
2924 * delete-duplicate does not lock the parent's core
2925 * (they just use the spinlock on the core).
2927 if (chain->flags & HAMMER2_CHAIN_DELETED) {
2928 kprintf("skip deleted chain %016jx.%02x key=%016jx\n",
2929 chain->bref.data_off, chain->bref.type,
2931 hammer2_chain_unlock(chain);
2932 hammer2_chain_drop(chain);
2933 chain = NULL; /* SAFETY */
2934 key_beg = *key_nextp;
2935 if (key_beg == 0 || key_beg > key_end)
2941 * If the chain element is an indirect block it becomes the new
2942 * parent and we loop on it. We must maintain our top-down locks
2943 * to prevent the flusher from interfering (i.e. doing a
2944 * delete-duplicate and leaving us recursing down a deleted chain).
2946 * The parent always has to be locked with at least RESOLVE_MAYBE
2947 * so we can access its data. It might need a fixup if the caller
2948 * passed incompatible flags. Be careful not to cause a deadlock
2949 * as a data-load requires an exclusive lock.
2951 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
2952 * range is within the requested key range we return the indirect
2953 * block and do NOT loop. This is usually only used to acquire
2956 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2957 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2958 save_mtx = parent->lock;
2959 hammer2_chain_unlock(parent);
2960 hammer2_chain_drop(parent);
2961 *parentp = parent = chain;
2962 chain = NULL; /* SAFETY */
2967 * All done, return the locked chain.
2969 * If the caller does not want a locked chain, replace the lock with
2970 * a ref. Perhaps this can eventually be optimized to not obtain the
2971 * lock in the first place for situations where the data does not
2972 * need to be resolved.
2974 * NOTE! A chain->error must be tested by the caller upon return.
2975 * *errorp is only set based on issues which occur while
2976 * trying to reach the chain.
2982 * After having issued a lookup we can iterate all matching keys.
2984 * If chain is non-NULL we continue the iteration from just after it's index.
2986 * If chain is NULL we assume the parent was exhausted and continue the
2987 * iteration at the next parent.
2989 * If a fatal error occurs (typically an I/O error), a dummy chain is
2990 * returned with chain->error and error-identifying information set. This
2991 * chain will assert if you try to do anything fancy with it.
2993 * XXX Depending on where the error occurs we should allow continued iteration.
2995 * parent must be locked on entry and remains locked throughout. chain's
2996 * lock status must match flags. Chain is always at least referenced.
2998 * WARNING! The MATCHIND flag does not apply to this function.
3001 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
3002 hammer2_key_t *key_nextp,
3003 hammer2_key_t key_beg, hammer2_key_t key_end,
3004 int *errorp, int flags)
3006 hammer2_chain_t *parent;
3010 * Calculate locking flags for upward recursion.
3012 how_maybe = HAMMER2_RESOLVE_MAYBE;
3013 if (flags & HAMMER2_LOOKUP_SHARED)
3014 how_maybe |= HAMMER2_RESOLVE_SHARED;
3020 * Calculate the next index and recalculate the parent if necessary.
3023 key_beg = chain->bref.key +
3024 ((hammer2_key_t)1 << chain->bref.keybits);
3025 hammer2_chain_unlock(chain);
3026 hammer2_chain_drop(chain);
3029 * chain invalid past this point, but we can still do a
3030 * pointer comparison w/parent.
3032 * Any scan where the lookup returned degenerate data embedded
3033 * in the inode has an invalid index and must terminate.
3035 if (chain == parent)
3037 if (key_beg == 0 || key_beg > key_end)
3040 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
3041 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
3043 * We reached the end of the iteration.
3048 * Continue iteration with next parent unless the current
3049 * parent covers the range.
3051 * (This also handles the case of a deleted, empty indirect
3054 key_beg = parent->bref.key +
3055 ((hammer2_key_t)1 << parent->bref.keybits);
3056 if (key_beg == 0 || key_beg > key_end)
3058 parent = hammer2_chain_repparent(parentp, how_maybe);
3064 return (hammer2_chain_lookup(parentp, key_nextp,
3070 * Caller wishes to iterate chains under parent, loading new chains into
3071 * chainp. Caller must initialize *chainp to NULL and *firstp to 1, and
3072 * then call hammer2_chain_scan() repeatedly until a non-zero return.
3073 * During the scan, *firstp will be set to 0 and (*chainp) will be replaced
3074 * with the returned chain for the scan. The returned *chainp will be
3075 * locked and referenced. Any prior contents will be unlocked and dropped.
3077 * Caller should check the return value. A normal scan EOF will return
3078 * exactly HAMMER2_ERROR_EOF. Any other non-zero value indicates an
3079 * error trying to access parent data. Any error in the returned chain
3080 * must be tested separately by the caller.
3082 * (*chainp) is dropped on each scan, but will only be set if the returned
3083 * element itself can recurse. Leaf elements are NOT resolved, loaded, or
3084 * returned via *chainp. The caller will get their bref only.
3086 * The raw scan function is similar to lookup/next but does not seek to a key.
3087 * Blockrefs are iterated via first_bref = (parent, NULL) and
3088 * next_chain = (parent, bref).
3090 * The passed-in parent must be locked and its data resolved. The function
3091 * nominally returns a locked and referenced *chainp != NULL for chains
3092 * the caller might need to recurse on (and will dipose of any *chainp passed
3093 * in). The caller must check the chain->bref.type either way.
3096 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t **chainp,
3097 hammer2_blockref_t *bref, int *firstp,
3101 hammer2_blockref_t *base;
3102 hammer2_blockref_t *bref_ptr;
3104 hammer2_key_t next_key;
3105 hammer2_chain_t *chain = NULL;
3107 int how_always = HAMMER2_RESOLVE_ALWAYS;
3108 int how_maybe = HAMMER2_RESOLVE_MAYBE;
3111 int maxloops = 300000;
3118 * Scan flags borrowed from lookup.
3120 if (flags & HAMMER2_LOOKUP_ALWAYS) {
3121 how_maybe = how_always;
3122 how = HAMMER2_RESOLVE_ALWAYS;
3123 } else if (flags & HAMMER2_LOOKUP_NODATA) {
3124 how = HAMMER2_RESOLVE_NEVER;
3126 how = HAMMER2_RESOLVE_MAYBE;
3128 if (flags & HAMMER2_LOOKUP_SHARED) {
3129 how_maybe |= HAMMER2_RESOLVE_SHARED;
3130 how_always |= HAMMER2_RESOLVE_SHARED;
3131 how |= HAMMER2_RESOLVE_SHARED;
3135 * Calculate key to locate first/next element, unlocking the previous
3136 * element as we go. Be careful, the key calculation can overflow.
3138 * (also reset bref to NULL)
3144 key = bref->key + ((hammer2_key_t)1 << bref->keybits);
3145 if ((chain = *chainp) != NULL) {
3147 hammer2_chain_unlock(chain);
3148 hammer2_chain_drop(chain);
3152 error |= HAMMER2_ERROR_EOF;
3158 if (parent->error) {
3159 error = parent->error;
3162 if (--maxloops == 0)
3163 panic("hammer2_chain_scan: maxloops");
3166 * Locate the blockref array. Currently we do a fully associative
3167 * search through the array.
3169 switch(parent->bref.type) {
3170 case HAMMER2_BREF_TYPE_INODE:
3172 * An inode with embedded data has no sub-chains.
3174 * WARNING! Bulk scan code may pass a static chain marked
3175 * as BREF_TYPE_INODE with a copy of the volume
3176 * root blockset to snapshot the volume.
3178 if (parent->data->ipdata.meta.op_flags &
3179 HAMMER2_OPFLAG_DIRECTDATA) {
3180 error |= HAMMER2_ERROR_EOF;
3183 base = &parent->data->ipdata.u.blockset.blockref[0];
3184 count = HAMMER2_SET_COUNT;
3186 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3187 case HAMMER2_BREF_TYPE_INDIRECT:
3189 * Optimize indirect blocks in the INITIAL state to avoid
3192 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
3195 if (parent->data == NULL)
3196 panic("parent->data is NULL");
3197 base = &parent->data->npdata[0];
3199 count = parent->bytes / sizeof(hammer2_blockref_t);
3201 case HAMMER2_BREF_TYPE_VOLUME:
3202 base = &parent->data->voldata.sroot_blockset.blockref[0];
3203 count = HAMMER2_SET_COUNT;
3205 case HAMMER2_BREF_TYPE_FREEMAP:
3206 base = &parent->data->blkset.blockref[0];
3207 count = HAMMER2_SET_COUNT;
3210 panic("hammer2_chain_scan: unrecognized blockref type: %d",
3212 base = NULL; /* safety */
3213 count = 0; /* safety */
3217 * Merged scan to find next candidate.
3219 * hammer2_base_*() functions require the parent->core.live_* fields
3220 * to be synchronized.
3222 * We need to hold the spinlock to access the block array and RB tree
3223 * and to interlock chain creation.
3225 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
3226 hammer2_chain_countbrefs(parent, base, count);
3230 hammer2_spin_ex(&parent->core.spin);
3231 chain = hammer2_combined_find(parent, base, count,
3233 key, HAMMER2_KEY_MAX,
3235 generation = parent->core.generation;
3238 * Exhausted parent chain, we're done.
3240 if (bref_ptr == NULL) {
3241 hammer2_spin_unex(&parent->core.spin);
3242 KKASSERT(chain == NULL);
3243 error |= HAMMER2_ERROR_EOF;
3248 * Copy into the supplied stack-based blockref.
3253 * Selected from blockref or in-memory chain.
3255 if (chain == NULL) {
3256 switch(bref->type) {
3257 case HAMMER2_BREF_TYPE_INODE:
3258 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3259 case HAMMER2_BREF_TYPE_INDIRECT:
3260 case HAMMER2_BREF_TYPE_VOLUME:
3261 case HAMMER2_BREF_TYPE_FREEMAP:
3263 * Recursion, always get the chain
3265 hammer2_spin_unex(&parent->core.spin);
3266 chain = hammer2_chain_get(parent, generation,
3273 * No recursion, do not waste time instantiating
3274 * a chain, just iterate using the bref.
3276 hammer2_spin_unex(&parent->core.spin);
3281 * Recursion or not we need the chain in order to supply
3284 hammer2_chain_ref(chain);
3285 hammer2_spin_unex(&parent->core.spin);
3286 hammer2_chain_lock(chain, how);
3289 (bcmp(bref, &chain->bref, sizeof(*bref)) ||
3290 chain->parent != parent)) {
3291 hammer2_chain_unlock(chain);
3292 hammer2_chain_drop(chain);
3298 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
3300 * NOTE: chain's key range is not relevant as there might be
3301 * one-offs within the range that are not deleted.
3303 * NOTE: XXX this could create problems with scans used in
3304 * situations other than mount-time recovery.
3306 * NOTE: Lookups can race delete-duplicate because
3307 * delete-duplicate does not lock the parent's core
3308 * (they just use the spinlock on the core).
3310 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3311 hammer2_chain_unlock(chain);
3312 hammer2_chain_drop(chain);
3317 error |= HAMMER2_ERROR_EOF;
3325 * All done, return the bref or NULL, supply chain if necessary.
3333 * Create and return a new hammer2 system memory structure of the specified
3334 * key, type and size and insert it under (*parentp). This is a full
3335 * insertion, based on the supplied key/keybits, and may involve creating
3336 * indirect blocks and moving other chains around via delete/duplicate.
3338 * This call can be made with parent == NULL as long as a non -1 methods
3339 * is supplied. hmp must also be supplied in this situation (otherwise
3340 * hmp is extracted from the supplied parent). The chain will be detached
3341 * from the topology. A later call with both parent and chain can be made
3344 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION
3345 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
3346 * FULL. This typically means that the caller is creating the chain after
3347 * doing a hammer2_chain_lookup().
3349 * (*parentp) must be exclusive locked and may be replaced on return
3350 * depending on how much work the function had to do.
3352 * (*parentp) must not be errored or this function will assert.
3354 * (*chainp) usually starts out NULL and returns the newly created chain,
3355 * but if the caller desires the caller may allocate a disconnected chain
3356 * and pass it in instead.
3358 * This function should NOT be used to insert INDIRECT blocks. It is
3359 * typically used to create/insert inodes and data blocks.
3361 * Caller must pass-in an exclusively locked parent the new chain is to
3362 * be inserted under, and optionally pass-in a disconnected, exclusively
3363 * locked chain to insert (else we create a new chain). The function will
3364 * adjust (*parentp) as necessary, create or connect the chain, and
3365 * return an exclusively locked chain in *chainp.
3367 * When creating a PFSROOT inode under the super-root, pmp is typically NULL
3368 * and will be reassigned.
3370 * NOTE: returns HAMMER_ERROR_* flags
3373 hammer2_chain_create(hammer2_chain_t **parentp, hammer2_chain_t **chainp,
3374 hammer2_dev_t *hmp, hammer2_pfs_t *pmp, int methods,
3375 hammer2_key_t key, int keybits, int type, size_t bytes,
3376 hammer2_tid_t mtid, hammer2_off_t dedup_off, int flags)
3378 hammer2_chain_t *chain;
3379 hammer2_chain_t *parent;
3380 hammer2_blockref_t *base;
3381 hammer2_blockref_t dummy;
3385 int maxloops = 300000;
3388 * Topology may be crossing a PFS boundary.
3392 KKASSERT(hammer2_mtx_owned(&parent->lock));
3393 KKASSERT(parent->error == 0);
3398 if (chain == NULL) {
3400 * First allocate media space and construct the dummy bref,
3401 * then allocate the in-memory chain structure. Set the
3402 * INITIAL flag for fresh chains which do not have embedded
3405 * XXX for now set the check mode of the child based on
3406 * the parent or, if the parent is an inode, the
3407 * specification in the inode.
3409 bzero(&dummy, sizeof(dummy));
3412 dummy.keybits = keybits;
3413 dummy.data_off = hammer2_getradix(bytes);
3416 * Inherit methods from parent by default. Primarily used
3417 * for BREF_TYPE_DATA. Non-data types *must* be set to
3418 * a non-NONE check algorithm.
3421 dummy.methods = parent->bref.methods;
3423 dummy.methods = (uint8_t)methods;
3425 if (type != HAMMER2_BREF_TYPE_DATA &&
3426 HAMMER2_DEC_CHECK(dummy.methods) == HAMMER2_CHECK_NONE) {
3428 HAMMER2_ENC_CHECK(HAMMER2_CHECK_DEFAULT);
3431 chain = hammer2_chain_alloc(hmp, pmp, &dummy);
3434 * Lock the chain manually, chain_lock will load the chain
3435 * which we do NOT want to do. (note: chain->refs is set
3436 * to 1 by chain_alloc() for us, but lockcnt is not).
3439 hammer2_mtx_ex(&chain->lock);
3443 * Set INITIAL to optimize I/O. The flag will generally be
3444 * processed when we call hammer2_chain_modify().
3446 * Recalculate bytes to reflect the actual media block
3447 * allocation. Handle special case radix 0 == 0 bytes.
3449 bytes = (size_t)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
3451 bytes = (hammer2_off_t)1 << bytes;
3452 chain->bytes = bytes;
3455 case HAMMER2_BREF_TYPE_VOLUME:
3456 case HAMMER2_BREF_TYPE_FREEMAP:
3457 panic("hammer2_chain_create: called with volume type");
3459 case HAMMER2_BREF_TYPE_INDIRECT:
3460 panic("hammer2_chain_create: cannot be used to"
3461 "create indirect block");
3463 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3464 panic("hammer2_chain_create: cannot be used to"
3465 "create freemap root or node");
3467 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3468 KKASSERT(bytes == sizeof(chain->data->bmdata));
3470 case HAMMER2_BREF_TYPE_DIRENT:
3471 case HAMMER2_BREF_TYPE_INODE:
3472 case HAMMER2_BREF_TYPE_DATA:
3475 * leave chain->data NULL, set INITIAL
3477 KKASSERT(chain->data == NULL);
3478 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
3483 * We are reattaching a previously deleted chain, possibly
3484 * under a new parent and possibly with a new key/keybits.
3485 * The chain does not have to be in a modified state. The
3486 * UPDATE flag will be set later on in this routine.
3488 * Do NOT mess with the current state of the INITIAL flag.
3490 chain->bref.key = key;
3491 chain->bref.keybits = keybits;
3492 if (chain->flags & HAMMER2_CHAIN_DELETED)
3493 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3494 KKASSERT(chain->parent == NULL);
3498 * Set the appropriate bref flag if requested.
3500 * NOTE! Callers can call this function to move chains without
3501 * knowing about special flags, so don't clear bref flags
3504 if (flags & HAMMER2_INSERT_PFSROOT)
3505 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
3511 * Calculate how many entries we have in the blockref array and
3512 * determine if an indirect block is required when inserting into
3516 if (--maxloops == 0)
3517 panic("hammer2_chain_create: maxloops");
3519 switch(parent->bref.type) {
3520 case HAMMER2_BREF_TYPE_INODE:
3521 if ((parent->data->ipdata.meta.op_flags &
3522 HAMMER2_OPFLAG_DIRECTDATA) != 0) {
3523 kprintf("hammer2: parent set for direct-data! "
3524 "pkey=%016jx ckey=%016jx\n",
3528 KKASSERT((parent->data->ipdata.meta.op_flags &
3529 HAMMER2_OPFLAG_DIRECTDATA) == 0);
3530 KKASSERT(parent->data != NULL);
3531 base = &parent->data->ipdata.u.blockset.blockref[0];
3532 count = HAMMER2_SET_COUNT;
3534 case HAMMER2_BREF_TYPE_INDIRECT:
3535 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3536 if (parent->flags & HAMMER2_CHAIN_INITIAL)
3539 base = &parent->data->npdata[0];
3540 count = parent->bytes / sizeof(hammer2_blockref_t);
3542 case HAMMER2_BREF_TYPE_VOLUME:
3543 KKASSERT(parent->data != NULL);
3544 base = &parent->data->voldata.sroot_blockset.blockref[0];
3545 count = HAMMER2_SET_COUNT;
3547 case HAMMER2_BREF_TYPE_FREEMAP:
3548 KKASSERT(parent->data != NULL);
3549 base = &parent->data->blkset.blockref[0];
3550 count = HAMMER2_SET_COUNT;
3553 panic("hammer2_chain_create: unrecognized blockref type: %d",
3561 * Make sure we've counted the brefs
3563 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
3564 hammer2_chain_countbrefs(parent, base, count);
3566 KASSERT(parent->core.live_count >= 0 &&
3567 parent->core.live_count <= count,
3568 ("bad live_count %d/%d (%02x, %d)",
3569 parent->core.live_count, count,
3570 parent->bref.type, parent->bytes));
3573 * If no free blockref could be found we must create an indirect
3574 * block and move a number of blockrefs into it. With the parent
3575 * locked we can safely lock each child in order to delete+duplicate
3576 * it without causing a deadlock.
3578 * This may return the new indirect block or the old parent depending
3579 * on where the key falls. NULL is returned on error.
3581 if (parent->core.live_count == count) {
3582 hammer2_chain_t *nparent;
3584 KKASSERT((flags & HAMMER2_INSERT_SAMEPARENT) == 0);
3586 nparent = hammer2_chain_create_indirect(parent, key, keybits,
3587 mtid, type, &error);
3588 if (nparent == NULL) {
3590 hammer2_chain_drop(chain);
3594 if (parent != nparent) {
3595 hammer2_chain_unlock(parent);
3596 hammer2_chain_drop(parent);
3597 parent = *parentp = nparent;
3603 * fall through if parent, or skip to here if no parent.
3606 if (chain->flags & HAMMER2_CHAIN_DELETED)
3607 kprintf("Inserting deleted chain @%016jx\n",
3611 * Link the chain into its parent.
3613 if (chain->parent != NULL)
3614 panic("hammer2: hammer2_chain_create: chain already connected");
3615 KKASSERT(chain->parent == NULL);
3617 KKASSERT(parent->core.live_count < count);
3618 hammer2_chain_insert(parent, chain,
3619 HAMMER2_CHAIN_INSERT_SPIN |
3620 HAMMER2_CHAIN_INSERT_LIVE,
3626 * Mark the newly created chain modified. This will cause
3627 * UPDATE to be set and process the INITIAL flag.
3629 * Device buffers are not instantiated for DATA elements
3630 * as these are handled by logical buffers.
3632 * Indirect and freemap node indirect blocks are handled
3633 * by hammer2_chain_create_indirect() and not by this
3636 * Data for all other bref types is expected to be
3637 * instantiated (INODE, LEAF).
3639 switch(chain->bref.type) {
3640 case HAMMER2_BREF_TYPE_DATA:
3641 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3642 case HAMMER2_BREF_TYPE_DIRENT:
3643 case HAMMER2_BREF_TYPE_INODE:
3644 error = hammer2_chain_modify(chain, mtid, dedup_off,
3645 HAMMER2_MODIFY_OPTDATA);
3649 * Remaining types are not supported by this function.
3650 * In particular, INDIRECT and LEAF_NODE types are
3651 * handled by create_indirect().
3653 panic("hammer2_chain_create: bad type: %d",
3660 * When reconnecting a chain we must set UPDATE and
3661 * setflush so the flush recognizes that it must update
3662 * the bref in the parent.
3664 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0)
3665 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
3669 * We must setflush(parent) to ensure that it recurses through to
3670 * chain. setflush(chain) might not work because ONFLUSH is possibly
3671 * already set in the chain (so it won't recurse up to set it in the
3675 hammer2_chain_setflush(parent);
3684 * Move the chain from its old parent to a new parent. The chain must have
3685 * already been deleted or already disconnected (or never associated) with
3686 * a parent. The chain is reassociated with the new parent and the deleted
3687 * flag will be cleared (no longer deleted). The chain's modification state
3690 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION
3691 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
3692 * FULL. This typically means that the caller is creating the chain after
3693 * doing a hammer2_chain_lookup().
3695 * Neither (parent) or (chain) can be errored.
3697 * If (parent) is non-NULL then the chain is inserted under the parent.
3699 * If (parent) is NULL then the newly duplicated chain is not inserted
3700 * anywhere, similar to if it had just been chain_alloc()'d (suitable for
3701 * passing into hammer2_chain_create() after this function returns).
3703 * WARNING! This function calls create which means it can insert indirect
3704 * blocks. This can cause other unrelated chains in the parent to
3705 * be moved to a newly inserted indirect block in addition to the
3709 hammer2_chain_rename(hammer2_chain_t **parentp, hammer2_chain_t *chain,
3710 hammer2_tid_t mtid, int flags)
3712 hammer2_blockref_t *bref;
3714 hammer2_chain_t *parent;
3718 * WARNING! We should never resolve DATA to device buffers
3719 * (XXX allow it if the caller did?), and since
3720 * we currently do not have the logical buffer cache
3721 * buffer in-hand to fix its cached physical offset
3722 * we also force the modify code to not COW it. XXX
3724 * NOTE! We allow error'd chains to be renamed. The bref itself
3725 * is good and can be renamed. The content, however, may
3729 KKASSERT(chain->parent == NULL);
3730 /*KKASSERT(chain->error == 0); allow */
3733 * Now create a duplicate of the chain structure, associating
3734 * it with the same core, making it the same size, pointing it
3735 * to the same bref (the same media block).
3737 * NOTE: Handle special radix == 0 case (means 0 bytes).
3739 bref = &chain->bref;
3740 bytes = (size_t)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
3742 bytes = (hammer2_off_t)1 << bytes;
3745 * If parent is not NULL the duplicated chain will be entered under
3746 * the parent and the UPDATE bit set to tell flush to update
3749 * We must setflush(parent) to ensure that it recurses through to
3750 * chain. setflush(chain) might not work because ONFLUSH is possibly
3751 * already set in the chain (so it won't recurse up to set it in the
3754 * Having both chains locked is extremely important for atomicy.
3756 if (parentp && (parent = *parentp) != NULL) {
3757 KKASSERT(hammer2_mtx_owned(&parent->lock));
3758 KKASSERT(parent->refs > 0);
3759 KKASSERT(parent->error == 0);
3761 hammer2_chain_create(parentp, &chain, NULL, chain->pmp,
3762 HAMMER2_METH_DEFAULT,
3763 bref->key, bref->keybits, bref->type,
3764 chain->bytes, mtid, 0, flags);
3765 KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE);
3766 hammer2_chain_setflush(*parentp);
3771 * This works in tandem with delete_obref() to install a blockref in
3772 * (typically) an indirect block that is associated with the chain being
3773 * moved to *parentp.
3775 * The reason we need this function is that the caller needs to maintain
3776 * the blockref as it was, and not generate a new blockref for what might
3777 * be a modified chain. Otherwise stuff will leak into the flush that
3778 * the flush code's FLUSH_INODE_STOP flag is unable to catch.
3780 * It is EXTREMELY important that we properly set CHAIN_BMAPUPD and
3781 * CHAIN_UPDATE. We must set BMAPUPD if the bref does not match, and
3782 * we must clear CHAIN_UPDATE (that was likely set by the chain_rename) if
3783 * it does. Otherwise we can end up in a situation where H2 is unable to
3784 * clean up the in-memory chain topology.
3786 * The reason for this is that flushes do not generally flush through
3787 * BREF_TYPE_INODE chains and depend on a hammer2_inode_t queued to syncq
3788 * or sideq to properly flush and dispose of the related inode chain's flags.
3789 * Situations where the inode is not actually modified by the frontend,
3790 * but where we have to move the related chains around as we insert or cleanup
3791 * indirect blocks, can leave us with a 'dirty' (non-disposable) in-memory
3792 * inode chain that does not have a hammer2_inode_t associated with it.
3795 hammer2_chain_rename_obref(hammer2_chain_t **parentp, hammer2_chain_t *chain,
3796 hammer2_tid_t mtid, int flags,
3797 hammer2_blockref_t *obref)
3799 hammer2_chain_rename(parentp, chain, mtid, flags);
3801 if (obref->type != HAMMER2_BREF_TYPE_EMPTY) {
3802 hammer2_blockref_t *tbase;
3805 KKASSERT((chain->flags & HAMMER2_CHAIN_BMAPPED) == 0);
3806 hammer2_chain_modify(*parentp, mtid, 0, 0);
3807 tbase = hammer2_chain_base_and_count(*parentp, &tcount);
3808 hammer2_base_insert(*parentp, tbase, tcount, chain, obref);
3809 if (bcmp(obref, &chain->bref, sizeof(chain->bref))) {
3810 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD |
3811 HAMMER2_CHAIN_UPDATE);
3813 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
3819 * Helper function for deleting chains.
3821 * The chain is removed from the live view (the RBTREE) as well as the parent's
3822 * blockmap. Both chain and its parent must be locked.
3824 * parent may not be errored. chain can be errored.
3827 _hammer2_chain_delete_helper(hammer2_chain_t *parent, hammer2_chain_t *chain,
3828 hammer2_tid_t mtid, int flags,
3829 hammer2_blockref_t *obref)
3834 KKASSERT((chain->flags & (HAMMER2_CHAIN_DELETED |
3835 HAMMER2_CHAIN_FICTITIOUS)) == 0);
3836 KKASSERT(chain->parent == parent);
3839 if (chain->flags & HAMMER2_CHAIN_BMAPPED) {
3841 * Chain is blockmapped, so there must be a parent.
3842 * Atomically remove the chain from the parent and remove
3843 * the blockmap entry. The parent must be set modified
3844 * to remove the blockmap entry.
3846 hammer2_blockref_t *base;
3849 KKASSERT(parent != NULL);
3850 KKASSERT(parent->error == 0);
3851 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
3852 error = hammer2_chain_modify(parent, mtid, 0, 0);
3857 * Calculate blockmap pointer
3859 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
3860 hammer2_spin_ex(&chain->core.spin);
3861 hammer2_spin_ex(&parent->core.spin);
3863 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3864 atomic_add_int(&parent->core.live_count, -1);
3865 ++parent->core.generation;
3866 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
3867 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
3868 --parent->core.chain_count;
3869 chain->parent = NULL;
3871 switch(parent->bref.type) {
3872 case HAMMER2_BREF_TYPE_INODE:
3874 * Access the inode's block array. However, there
3875 * is no block array if the inode is flagged
3879 (parent->data->ipdata.meta.op_flags &
3880 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
3882 &parent->data->ipdata.u.blockset.blockref[0];
3886 count = HAMMER2_SET_COUNT;
3888 case HAMMER2_BREF_TYPE_INDIRECT:
3889 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3891 base = &parent->data->npdata[0];
3894 count = parent->bytes / sizeof(hammer2_blockref_t);
3896 case HAMMER2_BREF_TYPE_VOLUME:
3897 base = &parent->data->voldata.
3898 sroot_blockset.blockref[0];
3899 count = HAMMER2_SET_COUNT;
3901 case HAMMER2_BREF_TYPE_FREEMAP:
3902 base = &parent->data->blkset.blockref[0];
3903 count = HAMMER2_SET_COUNT;
3908 panic("_hammer2_chain_delete_helper: "
3909 "unrecognized blockref type: %d",
3914 * delete blockmapped chain from its parent.
3916 * The parent is not affected by any statistics in chain
3917 * which are pending synchronization. That is, there is
3918 * nothing to undo in the parent since they have not yet
3919 * been incorporated into the parent.
3921 * The parent is affected by statistics stored in inodes.
3922 * Those have already been synchronized, so they must be
3923 * undone. XXX split update possible w/delete in middle?
3926 hammer2_base_delete(parent, base, count, chain, obref);
3928 hammer2_spin_unex(&parent->core.spin);
3929 hammer2_spin_unex(&chain->core.spin);
3930 } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
3932 * Chain is not blockmapped but a parent is present.
3933 * Atomically remove the chain from the parent. There is
3934 * no blockmap entry to remove.
3936 * Because chain was associated with a parent but not
3937 * synchronized, the chain's *_count_up fields contain
3938 * inode adjustment statistics which must be undone.
3940 hammer2_spin_ex(&chain->core.spin);
3941 hammer2_spin_ex(&parent->core.spin);
3942 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3943 atomic_add_int(&parent->core.live_count, -1);
3944 ++parent->core.generation;
3945 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
3946 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
3947 --parent->core.chain_count;
3948 chain->parent = NULL;
3949 hammer2_spin_unex(&parent->core.spin);
3950 hammer2_spin_unex(&chain->core.spin);
3953 * Chain is not blockmapped and has no parent. This
3954 * is a degenerate case.
3956 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3963 * Create an indirect block that covers one or more of the elements in the
3964 * current parent. Either returns the existing parent with no locking or
3965 * ref changes or returns the new indirect block locked and referenced
3966 * and leaving the original parent lock/ref intact as well.
3968 * If an error occurs, NULL is returned and *errorp is set to the H2 error.
3970 * The returned chain depends on where the specified key falls.
3972 * The key/keybits for the indirect mode only needs to follow three rules:
3974 * (1) That all elements underneath it fit within its key space and
3976 * (2) That all elements outside it are outside its key space.
3978 * (3) When creating the new indirect block any elements in the current
3979 * parent that fit within the new indirect block's keyspace must be
3980 * moved into the new indirect block.
3982 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
3983 * keyspace the the current parent, but lookup/iteration rules will
3984 * ensure (and must ensure) that rule (2) for all parents leading up
3985 * to the nearest inode or the root volume header is adhered to. This
3986 * is accomplished by always recursing through matching keyspaces in
3987 * the hammer2_chain_lookup() and hammer2_chain_next() API.
3989 * The current implementation calculates the current worst-case keyspace by
3990 * iterating the current parent and then divides it into two halves, choosing
3991 * whichever half has the most elements (not necessarily the half containing
3992 * the requested key).
3994 * We can also opt to use the half with the least number of elements. This
3995 * causes lower-numbered keys (aka logical file offsets) to recurse through
3996 * fewer indirect blocks and higher-numbered keys to recurse through more.
3997 * This also has the risk of not moving enough elements to the new indirect
3998 * block and being forced to create several indirect blocks before the element
4001 * Must be called with an exclusively locked parent.
4003 * NOTE: *errorp set to HAMMER_ERROR_* flags
4005 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
4006 hammer2_key_t *keyp, int keybits,
4007 hammer2_blockref_t *base, int count);
4008 static int hammer2_chain_indkey_file(hammer2_chain_t *parent,
4009 hammer2_key_t *keyp, int keybits,
4010 hammer2_blockref_t *base, int count,
4012 static int hammer2_chain_indkey_dir(hammer2_chain_t *parent,
4013 hammer2_key_t *keyp, int keybits,
4014 hammer2_blockref_t *base, int count,
4018 hammer2_chain_create_indirect(hammer2_chain_t *parent,
4019 hammer2_key_t create_key, int create_bits,
4020 hammer2_tid_t mtid, int for_type, int *errorp)
4023 hammer2_blockref_t *base;
4024 hammer2_blockref_t *bref;
4025 hammer2_blockref_t bsave;
4026 hammer2_blockref_t dummy;
4027 hammer2_chain_t *chain;
4028 hammer2_chain_t *ichain;
4029 hammer2_key_t key = create_key;
4030 hammer2_key_t key_beg;
4031 hammer2_key_t key_end;
4032 hammer2_key_t key_next;
4033 int keybits = create_bits;
4041 int maxloops = 300000;
4044 * Calculate the base blockref pointer or NULL if the chain
4045 * is known to be empty. We need to calculate the array count
4046 * for RB lookups either way.
4049 KKASSERT(hammer2_mtx_owned(&parent->lock));
4052 * Pre-modify the parent now to avoid having to deal with error
4053 * processing if we tried to later (in the middle of our loop).
4055 * We are going to be moving bref's around, the indirect blocks
4056 * cannot be in an initial state. Do not pass MODIFY_OPTDATA.
4058 *errorp = hammer2_chain_modify(parent, mtid, 0, 0);
4060 kprintf("hammer2_create_indirect: error %08x %s\n",
4061 *errorp, hammer2_error_str(*errorp));
4064 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
4066 /*hammer2_chain_modify(&parent, HAMMER2_MODIFY_OPTDATA);*/
4067 base = hammer2_chain_base_and_count(parent, &count);
4070 * How big should our new indirect block be? It has to be at least
4071 * as large as its parent for splits to work properly.
4073 * The freemap uses a specific indirect block size. The number of
4074 * levels are built dynamically and ultimately depend on the size
4075 * volume. Because freemap blocks are taken from the reserved areas
4076 * of the volume our goal is efficiency (fewer levels) and not so
4077 * much to save disk space.
4079 * The first indirect block level for a directory usually uses
4080 * HAMMER2_IND_BYTES_MIN (4KB = 32 directory entries). Due to
4081 * the hash mechanism, this typically gives us a nominal
4082 * 32 * 4 entries with one level of indirection.
4084 * We use HAMMER2_IND_BYTES_NOM (16KB = 128 blockrefs) for FILE
4085 * indirect blocks. The initial 4 entries in the inode gives us
4086 * 256KB. Up to 4 indirect blocks gives us 32MB. Three levels
4087 * of indirection gives us 137GB, and so forth. H2 can support
4088 * huge file sizes but they are not typical, so we try to stick
4089 * with compactness and do not use a larger indirect block size.
4091 * We could use 64KB (PBUFSIZE), giving us 512 blockrefs, but
4092 * due to the way indirect blocks are created this usually winds
4093 * up being extremely inefficient for small files. Even though
4094 * 16KB requires more levels of indirection for very large files,
4095 * the 16KB records can be ganged together into 64KB DIOs.
4097 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
4098 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
4099 nbytes = HAMMER2_FREEMAP_LEVELN_PSIZE;
4100 } else if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
4101 if (parent->data->ipdata.meta.type ==
4102 HAMMER2_OBJTYPE_DIRECTORY)
4103 nbytes = HAMMER2_IND_BYTES_MIN; /* 4KB = 32 entries */
4105 nbytes = HAMMER2_IND_BYTES_NOM; /* 16KB = ~8MB file */
4108 nbytes = HAMMER2_IND_BYTES_NOM;
4110 if (nbytes < count * sizeof(hammer2_blockref_t)) {
4111 KKASSERT(for_type != HAMMER2_BREF_TYPE_FREEMAP_NODE &&
4112 for_type != HAMMER2_BREF_TYPE_FREEMAP_LEAF);
4113 nbytes = count * sizeof(hammer2_blockref_t);
4115 ncount = nbytes / sizeof(hammer2_blockref_t);
4118 * When creating an indirect block for a freemap node or leaf
4119 * the key/keybits must be fitted to static radix levels because
4120 * particular radix levels use particular reserved blocks in the
4123 * This routine calculates the key/radix of the indirect block
4124 * we need to create, and whether it is on the high-side or the
4128 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
4129 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
4130 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
4133 case HAMMER2_BREF_TYPE_DATA:
4134 keybits = hammer2_chain_indkey_file(parent, &key, keybits,
4135 base, count, ncount);
4137 case HAMMER2_BREF_TYPE_DIRENT:
4138 case HAMMER2_BREF_TYPE_INODE:
4139 keybits = hammer2_chain_indkey_dir(parent, &key, keybits,
4140 base, count, ncount);
4143 panic("illegal indirect block for bref type %d", for_type);
4148 * Normalize the key for the radix being represented, keeping the
4149 * high bits and throwing away the low bits.
4151 key &= ~(((hammer2_key_t)1 << keybits) - 1);
4154 * Ok, create our new indirect block
4156 bzero(&dummy, sizeof(dummy));
4157 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
4158 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
4159 dummy.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
4161 dummy.type = HAMMER2_BREF_TYPE_INDIRECT;
4164 dummy.keybits = keybits;
4165 dummy.data_off = hammer2_getradix(nbytes);
4167 HAMMER2_ENC_CHECK(HAMMER2_DEC_CHECK(parent->bref.methods)) |
4168 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
4170 ichain = hammer2_chain_alloc(hmp, parent->pmp, &dummy);
4171 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
4172 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
4173 /* ichain has one ref at this point */
4176 * We have to mark it modified to allocate its block, but use
4177 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
4178 * it won't be acted upon by the flush code.
4180 * XXX remove OPTDATA, we need a fully initialized indirect block to
4181 * be able to move the original blockref.
4183 *errorp = hammer2_chain_modify(ichain, mtid, 0, 0);
4185 kprintf("hammer2_alloc_indirect: error %08x %s\n",
4186 *errorp, hammer2_error_str(*errorp));
4187 hammer2_chain_unlock(ichain);
4188 hammer2_chain_drop(ichain);
4191 KKASSERT((ichain->flags & HAMMER2_CHAIN_INITIAL) == 0);
4194 * Iterate the original parent and move the matching brefs into
4195 * the new indirect block.
4197 * XXX handle flushes.
4200 key_end = HAMMER2_KEY_MAX;
4201 key_next = 0; /* avoid gcc warnings */
4202 hammer2_spin_ex(&parent->core.spin);
4208 * Parent may have been modified, relocating its block array.
4209 * Reload the base pointer.
4211 base = hammer2_chain_base_and_count(parent, &count);
4213 if (++loops > 100000) {
4214 hammer2_spin_unex(&parent->core.spin);
4215 panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
4216 reason, parent, base, count, key_next);
4220 * NOTE: spinlock stays intact, returned chain (if not NULL)
4221 * is not referenced or locked which means that we
4222 * cannot safely check its flagged / deletion status
4225 chain = hammer2_combined_find(parent, base, count,
4229 generation = parent->core.generation;
4232 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4235 * Skip keys that are not within the key/radix of the new
4236 * indirect block. They stay in the parent.
4238 if (rounddown2(key ^ bref->key, (hammer2_key_t)1 << keybits) != 0) {
4239 goto next_key_spinlocked;
4243 * Load the new indirect block by acquiring the related
4244 * chains (potentially from media as it might not be
4245 * in-memory). Then move it to the new parent (ichain).
4247 * chain is referenced but not locked. We must lock the
4248 * chain to obtain definitive state.
4253 * Use chain already present in the RBTREE
4255 hammer2_chain_ref(chain);
4256 hammer2_spin_unex(&parent->core.spin);
4257 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
4260 * Get chain for blockref element. _get returns NULL
4261 * on insertion race.
4263 hammer2_spin_unex(&parent->core.spin);
4264 chain = hammer2_chain_get(parent, generation, &bsave,
4265 HAMMER2_RESOLVE_NEVER);
4266 if (chain == NULL) {
4268 hammer2_spin_ex(&parent->core.spin);
4274 * This is always live so if the chain has been deleted
4275 * we raced someone and we have to retry.
4277 * NOTE: Lookups can race delete-duplicate because
4278 * delete-duplicate does not lock the parent's core
4279 * (they just use the spinlock on the core).
4281 * (note reversed logic for this one)
4283 if (bcmp(&bsave, &chain->bref, sizeof(bsave)) ||
4284 chain->parent != parent ||
4285 (chain->flags & HAMMER2_CHAIN_DELETED)) {
4286 hammer2_chain_unlock(chain);
4287 hammer2_chain_drop(chain);
4288 if (hammer2_debug & 0x0040) {
4289 kprintf("LOST PARENT RETRY "
4290 "RETRY (%p,%p)->%p %08x\n",
4291 parent, chain->parent, chain, chain->flags);
4293 hammer2_spin_ex(&parent->core.spin);
4298 * Shift the chain to the indirect block.
4300 * WARNING! No reason for us to load chain data, pass NOSTATS
4301 * to prevent delete/insert from trying to access
4302 * inode stats (and thus asserting if there is no
4303 * chain->data loaded).
4305 * WARNING! The (parent, chain) deletion may modify the parent
4306 * and invalidate the base pointer.
4308 * WARNING! Parent must already be marked modified, so we
4309 * can assume that chain_delete always suceeds.
4311 * WARNING! hammer2_chain_repchange() does not have to be
4312 * called (and doesn't work anyway because we are
4313 * only doing a partial shift). A recursion that is
4314 * in-progress can continue at the current parent
4315 * and will be able to properly find its next key.
4317 error = hammer2_chain_delete_obref(parent, chain, mtid, 0,
4319 KKASSERT(error == 0);
4320 hammer2_chain_rename_obref(&ichain, chain, mtid, 0, &bsave);
4321 hammer2_chain_unlock(chain);
4322 hammer2_chain_drop(chain);
4323 KKASSERT(parent->refs > 0);
4325 base = NULL; /* safety */
4326 hammer2_spin_ex(&parent->core.spin);
4327 next_key_spinlocked:
4328 if (--maxloops == 0)
4329 panic("hammer2_chain_create_indirect: maxloops");
4331 if (key_next == 0 || key_next > key_end)
4336 hammer2_spin_unex(&parent->core.spin);
4339 * Insert the new indirect block into the parent now that we've
4340 * cleared out some entries in the parent. We calculated a good
4341 * insertion index in the loop above (ichain->index).
4343 * We don't have to set UPDATE here because we mark ichain
4344 * modified down below (so the normal modified -> flush -> set-moved
4345 * sequence applies).
4347 * The insertion shouldn't race as this is a completely new block
4348 * and the parent is locked.
4350 base = NULL; /* safety, parent modify may change address */
4351 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
4352 KKASSERT(parent->core.live_count < count);
4353 hammer2_chain_insert(parent, ichain,
4354 HAMMER2_CHAIN_INSERT_SPIN |
4355 HAMMER2_CHAIN_INSERT_LIVE,
4359 * Make sure flushes propogate after our manual insertion.
4361 hammer2_chain_setflush(ichain);
4362 hammer2_chain_setflush(parent);
4365 * Figure out what to return.
4367 if (rounddown2(create_key ^ key, (hammer2_key_t)1 << keybits)) {
4369 * Key being created is outside the key range,
4370 * return the original parent.
4372 hammer2_chain_unlock(ichain);
4373 hammer2_chain_drop(ichain);
4376 * Otherwise its in the range, return the new parent.
4377 * (leave both the new and old parent locked).
4386 * Do maintenance on an indirect chain. Both parent and chain are locked.
4388 * Returns non-zero if (chain) is deleted, either due to being empty or
4389 * because its children were safely moved into the parent.
4392 hammer2_chain_indirect_maintenance(hammer2_chain_t *parent,
4393 hammer2_chain_t *chain)
4395 hammer2_blockref_t *chain_base;
4396 hammer2_blockref_t *base;
4397 hammer2_blockref_t *bref;
4398 hammer2_blockref_t bsave;
4399 hammer2_key_t key_next;
4400 hammer2_key_t key_beg;
4401 hammer2_key_t key_end;
4402 hammer2_chain_t *sub;
4409 * Make sure we have an accurate live_count
4411 if ((chain->flags & (HAMMER2_CHAIN_INITIAL |
4412 HAMMER2_CHAIN_COUNTEDBREFS)) == 0) {
4413 base = &chain->data->npdata[0];
4414 count = chain->bytes / sizeof(hammer2_blockref_t);
4415 hammer2_chain_countbrefs(chain, base, count);
4419 * If the indirect block is empty we can delete it.
4420 * (ignore deletion error)
4422 if (chain->core.live_count == 0 && RB_EMPTY(&chain->core.rbtree)) {
4423 hammer2_chain_delete(parent, chain,
4424 chain->bref.modify_tid,
4425 HAMMER2_DELETE_PERMANENT);
4426 hammer2_chain_repchange(parent, chain);
4430 base = hammer2_chain_base_and_count(parent, &count);
4432 if ((parent->flags & (HAMMER2_CHAIN_INITIAL |
4433 HAMMER2_CHAIN_COUNTEDBREFS)) == 0) {
4434 hammer2_chain_countbrefs(parent, base, count);
4438 * Determine if we can collapse chain into parent, calculate
4439 * hysteresis for chain emptiness.
4441 if (parent->core.live_count + chain->core.live_count - 1 > count)
4443 chain_count = chain->bytes / sizeof(hammer2_blockref_t);
4444 if (chain->core.live_count > chain_count * 3 / 4)
4448 * Ok, theoretically we can collapse chain's contents into
4449 * parent. chain is locked, but any in-memory children of chain
4450 * are not. For this to work, we must be able to dispose of any
4451 * in-memory children of chain.
4453 * For now require that there are no in-memory children of chain.
4455 * WARNING! Both chain and parent must remain locked across this
4460 * Parent must be marked modified. Don't try to collapse it if we
4461 * can't mark it modified. Once modified, destroy chain to make room
4462 * and to get rid of what will be a conflicting key (this is included
4463 * in the calculation above). Finally, move the children of chain
4464 * into chain's parent.
4466 * This order creates an accounting problem for bref.embed.stats
4467 * because we destroy chain before we remove its children. Any
4468 * elements whos blockref is already synchronized will be counted
4469 * twice. To deal with the problem we clean out chain's stats prior
4472 error = hammer2_chain_modify(parent, 0, 0, 0);
4474 krateprintf(&krate_h2me, "hammer2: indirect_maint: %s\n",
4475 hammer2_error_str(error));
4478 error = hammer2_chain_modify(chain, chain->bref.modify_tid, 0, 0);
4480 krateprintf(&krate_h2me, "hammer2: indirect_maint: %s\n",
4481 hammer2_error_str(error));
4485 chain->bref.embed.stats.inode_count = 0;
4486 chain->bref.embed.stats.data_count = 0;
4487 error = hammer2_chain_delete(parent, chain,
4488 chain->bref.modify_tid,
4489 HAMMER2_DELETE_PERMANENT);
4490 KKASSERT(error == 0);
4493 * The combined_find call requires core.spin to be held. One would
4494 * think there wouldn't be any conflicts since we hold chain
4495 * exclusively locked, but the caching mechanism for 0-ref children
4496 * does not require a chain lock.
4498 hammer2_spin_ex(&chain->core.spin);
4502 key_end = HAMMER2_KEY_MAX;
4504 chain_base = &chain->data->npdata[0];
4505 chain_count = chain->bytes / sizeof(hammer2_blockref_t);
4506 sub = hammer2_combined_find(chain, chain_base, chain_count,
4510 generation = chain->core.generation;
4513 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4517 hammer2_chain_ref(sub);
4518 hammer2_spin_unex(&chain->core.spin);
4519 hammer2_chain_lock(sub, HAMMER2_RESOLVE_NEVER);
4521 hammer2_spin_unex(&chain->core.spin);
4522 sub = hammer2_chain_get(chain, generation, &bsave,
4523 HAMMER2_RESOLVE_NEVER);
4525 hammer2_spin_ex(&chain->core.spin);
4529 if (bcmp(&bsave, &sub->bref, sizeof(bsave)) ||
4530 sub->parent != chain ||
4531 (sub->flags & HAMMER2_CHAIN_DELETED)) {
4532 hammer2_chain_unlock(sub);
4533 hammer2_chain_drop(sub);
4534 hammer2_spin_ex(&chain->core.spin);
4535 sub = NULL; /* safety */
4538 error = hammer2_chain_delete_obref(chain, sub,
4539 sub->bref.modify_tid, 0,
4541 KKASSERT(error == 0);
4542 hammer2_chain_rename_obref(&parent, sub,
4543 sub->bref.modify_tid,
4544 HAMMER2_INSERT_SAMEPARENT, &bsave);
4545 hammer2_chain_unlock(sub);
4546 hammer2_chain_drop(sub);
4547 hammer2_spin_ex(&chain->core.spin);
4553 hammer2_spin_unex(&chain->core.spin);
4555 hammer2_chain_repchange(parent, chain);
4561 * Freemap indirect blocks
4563 * Calculate the keybits and highside/lowside of the freemap node the
4564 * caller is creating.
4566 * This routine will specify the next higher-level freemap key/radix
4567 * representing the lowest-ordered set. By doing so, eventually all
4568 * low-ordered sets will be moved one level down.
4570 * We have to be careful here because the freemap reserves a limited
4571 * number of blocks for a limited number of levels. So we can't just
4572 * push indiscriminately.
4575 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
4576 int keybits, hammer2_blockref_t *base, int count)
4578 hammer2_chain_t *chain;
4579 hammer2_blockref_t *bref;
4581 hammer2_key_t key_beg;
4582 hammer2_key_t key_end;
4583 hammer2_key_t key_next;
4586 int maxloops = 300000;
4594 * Calculate the range of keys in the array being careful to skip
4595 * slots which are overridden with a deletion.
4598 key_end = HAMMER2_KEY_MAX;
4599 hammer2_spin_ex(&parent->core.spin);
4602 if (--maxloops == 0) {
4603 panic("indkey_freemap shit %p %p:%d\n",
4604 parent, base, count);
4606 chain = hammer2_combined_find(parent, base, count,
4618 * Skip deleted chains.
4620 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4621 if (key_next == 0 || key_next > key_end)
4628 * Use the full live (not deleted) element for the scan
4629 * iteration. HAMMER2 does not allow partial replacements.
4631 * XXX should be built into hammer2_combined_find().
4633 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4635 if (keybits > bref->keybits) {
4637 keybits = bref->keybits;
4638 } else if (keybits == bref->keybits && bref->key < key) {
4645 hammer2_spin_unex(&parent->core.spin);
4648 * Return the keybits for a higher-level FREEMAP_NODE covering
4652 case HAMMER2_FREEMAP_LEVEL0_RADIX:
4653 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
4655 case HAMMER2_FREEMAP_LEVEL1_RADIX:
4656 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
4658 case HAMMER2_FREEMAP_LEVEL2_RADIX:
4659 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
4661 case HAMMER2_FREEMAP_LEVEL3_RADIX:
4662 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
4664 case HAMMER2_FREEMAP_LEVEL4_RADIX:
4665 keybits = HAMMER2_FREEMAP_LEVEL5_RADIX;
4667 case HAMMER2_FREEMAP_LEVEL5_RADIX:
4668 panic("hammer2_chain_indkey_freemap: level too high");
4671 panic("hammer2_chain_indkey_freemap: bad radix");
4680 * File indirect blocks
4682 * Calculate the key/keybits for the indirect block to create by scanning
4683 * existing keys. The key being created is also passed in *keyp and can be
4684 * inside or outside the indirect block. Regardless, the indirect block
4685 * must hold at least two keys in order to guarantee sufficient space.
4687 * We use a modified version of the freemap's fixed radix tree, but taylored
4688 * for file data. Basically we configure an indirect block encompassing the
4692 hammer2_chain_indkey_file(hammer2_chain_t *parent, hammer2_key_t *keyp,
4693 int keybits, hammer2_blockref_t *base, int count,
4696 hammer2_chain_t *chain;
4697 hammer2_blockref_t *bref;
4699 hammer2_key_t key_beg;
4700 hammer2_key_t key_end;
4701 hammer2_key_t key_next;
4705 int maxloops = 300000;
4713 * Calculate the range of keys in the array being careful to skip
4714 * slots which are overridden with a deletion.
4716 * Locate the smallest key.
4719 key_end = HAMMER2_KEY_MAX;
4720 hammer2_spin_ex(&parent->core.spin);
4723 if (--maxloops == 0) {
4724 panic("indkey_freemap shit %p %p:%d\n",
4725 parent, base, count);
4727 chain = hammer2_combined_find(parent, base, count,
4739 * Skip deleted chains.
4741 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4742 if (key_next == 0 || key_next > key_end)
4749 * Use the full live (not deleted) element for the scan
4750 * iteration. HAMMER2 does not allow partial replacements.
4752 * XXX should be built into hammer2_combined_find().
4754 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4756 if (keybits > bref->keybits) {
4758 keybits = bref->keybits;
4759 } else if (keybits == bref->keybits && bref->key < key) {
4766 hammer2_spin_unex(&parent->core.spin);
4769 * Calculate the static keybits for a higher-level indirect block
4770 * that contains the key.
4775 case HAMMER2_IND_BYTES_MIN / sizeof(hammer2_blockref_t):
4776 nradix = HAMMER2_IND_RADIX_MIN - HAMMER2_BLOCKREF_RADIX;
4778 case HAMMER2_IND_BYTES_NOM / sizeof(hammer2_blockref_t):
4779 nradix = HAMMER2_IND_RADIX_NOM - HAMMER2_BLOCKREF_RADIX;
4781 case HAMMER2_IND_BYTES_MAX / sizeof(hammer2_blockref_t):
4782 nradix = HAMMER2_IND_RADIX_MAX - HAMMER2_BLOCKREF_RADIX;
4785 panic("bad ncount %d\n", ncount);
4791 * The largest radix that can be returned for an indirect block is
4792 * 63 bits. (The largest practical indirect block radix is actually
4793 * 62 bits because the top-level inode or volume root contains four
4794 * entries, but allow 63 to be returned).
4799 return keybits + nradix;
4805 * Directory indirect blocks.
4807 * Covers both the inode index (directory of inodes), and directory contents
4808 * (filenames hardlinked to inodes).
4810 * Because directory keys are hashed we generally try to cut the space in
4811 * half. We accomodate the inode index (which tends to have linearly
4812 * increasing inode numbers) by ensuring that the keyspace is at least large
4813 * enough to fill up the indirect block being created.
4816 hammer2_chain_indkey_dir(hammer2_chain_t *parent, hammer2_key_t *keyp,
4817 int keybits, hammer2_blockref_t *base, int count,
4820 hammer2_blockref_t *bref;
4821 hammer2_chain_t *chain;
4822 hammer2_key_t key_beg;
4823 hammer2_key_t key_end;
4824 hammer2_key_t key_next;
4829 int maxloops = 300000;
4832 * NOTE: We can't take a shortcut here anymore for inodes because
4833 * the root directory can contain a mix of inodes and directory
4834 * entries (we used to just return 63 if parent->bref.type was
4835 * HAMMER2_BREF_TYPE_INODE.
4842 * Calculate the range of keys in the array being careful to skip
4843 * slots which are overridden with a deletion.
4846 key_end = HAMMER2_KEY_MAX;
4847 hammer2_spin_ex(&parent->core.spin);
4850 if (--maxloops == 0) {
4851 panic("indkey_freemap shit %p %p:%d\n",
4852 parent, base, count);
4854 chain = hammer2_combined_find(parent, base, count,
4868 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4869 if (key_next == 0 || key_next > key_end)
4876 * Use the full live (not deleted) element for the scan
4877 * iteration. HAMMER2 does not allow partial replacements.
4879 * XXX should be built into hammer2_combined_find().
4881 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4884 * Expand our calculated key range (key, keybits) to fit
4885 * the scanned key. nkeybits represents the full range
4886 * that we will later cut in half (two halves @ nkeybits - 1).
4889 if (nkeybits < bref->keybits) {
4890 if (bref->keybits > 64) {
4891 kprintf("bad bref chain %p bref %p\n",
4895 nkeybits = bref->keybits;
4897 while (nkeybits < 64 &&
4898 rounddown2(key ^ bref->key, (hammer2_key_t)1 << nkeybits) != 0) {
4903 * If the new key range is larger we have to determine
4904 * which side of the new key range the existing keys fall
4905 * under by checking the high bit, then collapsing the
4906 * locount into the hicount or vise-versa.
4908 if (keybits != nkeybits) {
4909 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
4920 * The newly scanned key will be in the lower half or the
4921 * upper half of the (new) key range.
4923 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
4932 hammer2_spin_unex(&parent->core.spin);
4933 bref = NULL; /* now invalid (safety) */
4936 * Adjust keybits to represent half of the full range calculated
4937 * above (radix 63 max) for our new indirect block.
4942 * Expand keybits to hold at least ncount elements. ncount will be
4943 * a power of 2. This is to try to completely fill leaf nodes (at
4944 * least for keys which are not hashes).
4946 * We aren't counting 'in' or 'out', we are counting 'high side'
4947 * and 'low side' based on the bit at (1LL << keybits). We want
4948 * everything to be inside in these cases so shift it all to
4949 * the low or high side depending on the new high bit.
4951 while (((hammer2_key_t)1 << keybits) < ncount) {
4953 if (key & ((hammer2_key_t)1 << keybits)) {
4962 if (hicount > locount)
4963 key |= (hammer2_key_t)1 << keybits;
4965 key &= ~(hammer2_key_t)1 << keybits;
4975 * Directory indirect blocks.
4977 * Covers both the inode index (directory of inodes), and directory contents
4978 * (filenames hardlinked to inodes).
4980 * Because directory keys are hashed we generally try to cut the space in
4981 * half. We accomodate the inode index (which tends to have linearly
4982 * increasing inode numbers) by ensuring that the keyspace is at least large
4983 * enough to fill up the indirect block being created.
4986 hammer2_chain_indkey_dir(hammer2_chain_t *parent, hammer2_key_t *keyp,
4987 int keybits, hammer2_blockref_t *base, int count,
4990 hammer2_blockref_t *bref;
4991 hammer2_chain_t *chain;
4992 hammer2_key_t key_beg;
4993 hammer2_key_t key_end;
4994 hammer2_key_t key_next;
4999 int maxloops = 300000;
5002 * Shortcut if the parent is the inode. In this situation the
5003 * parent has 4+1 directory entries and we are creating an indirect
5004 * block capable of holding many more.
5006 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
5015 * Calculate the range of keys in the array being careful to skip
5016 * slots which are overridden with a deletion.
5019 key_end = HAMMER2_KEY_MAX;
5020 hammer2_spin_ex(&parent->core.spin);
5023 if (--maxloops == 0) {
5024 panic("indkey_freemap shit %p %p:%d\n",
5025 parent, base, count);
5027 chain = hammer2_combined_find(parent, base, count,
5041 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
5042 if (key_next == 0 || key_next > key_end)
5049 * Use the full live (not deleted) element for the scan
5050 * iteration. HAMMER2 does not allow partial replacements.
5052 * XXX should be built into hammer2_combined_find().
5054 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
5057 * Expand our calculated key range (key, keybits) to fit
5058 * the scanned key. nkeybits represents the full range
5059 * that we will later cut in half (two halves @ nkeybits - 1).
5062 if (nkeybits < bref->keybits) {
5063 if (bref->keybits > 64) {
5064 kprintf("bad bref chain %p bref %p\n",
5068 nkeybits = bref->keybits;
5070 while (nkeybits < 64 &&
5071 (~(((hammer2_key_t)1 << nkeybits) - 1) &
5072 (key ^ bref->key)) != 0) {
5077 * If the new key range is larger we have to determine
5078 * which side of the new key range the existing keys fall
5079 * under by checking the high bit, then collapsing the
5080 * locount into the hicount or vise-versa.
5082 if (keybits != nkeybits) {
5083 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
5094 * The newly scanned key will be in the lower half or the
5095 * upper half of the (new) key range.
5097 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
5106 hammer2_spin_unex(&parent->core.spin);
5107 bref = NULL; /* now invalid (safety) */
5110 * Adjust keybits to represent half of the full range calculated
5111 * above (radix 63 max) for our new indirect block.
5116 * Expand keybits to hold at least ncount elements. ncount will be
5117 * a power of 2. This is to try to completely fill leaf nodes (at
5118 * least for keys which are not hashes).
5120 * We aren't counting 'in' or 'out', we are counting 'high side'
5121 * and 'low side' based on the bit at (1LL << keybits). We want
5122 * everything to be inside in these cases so shift it all to
5123 * the low or high side depending on the new high bit.
5125 while (((hammer2_key_t)1 << keybits) < ncount) {
5127 if (key & ((hammer2_key_t)1 << keybits)) {
5136 if (hicount > locount)
5137 key |= (hammer2_key_t)1 << keybits;
5139 key &= ~(hammer2_key_t)1 << keybits;
5149 * Sets CHAIN_DELETED and remove the chain's blockref from the parent if
5152 * Both parent and chain must be locked exclusively.
5154 * This function will modify the parent if the blockref requires removal
5155 * from the parent's block table.
5157 * This function is NOT recursive. Any entity already pushed into the
5158 * chain (such as an inode) may still need visibility into its contents,
5159 * as well as the ability to read and modify the contents. For example,
5160 * for an unlinked file which is still open.
5162 * Also note that the flusher is responsible for cleaning up empty
5166 hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
5167 hammer2_tid_t mtid, int flags)
5171 KKASSERT(hammer2_mtx_owned(&chain->lock));
5174 * Nothing to do if already marked.
5176 * We need the spinlock on the core whos RBTREE contains chain
5177 * to protect against races.
5179 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
5180 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
5181 chain->parent == parent);
5182 error = _hammer2_chain_delete_helper(parent, chain,
5187 * Permanent deletions mark the chain as destroyed.
5189 * NOTE: We do not setflush the chain unless the deletion is
5190 * permanent, since the deletion of a chain does not actually
5191 * require it to be flushed.
5194 if (flags & HAMMER2_DELETE_PERMANENT) {
5195 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
5196 hammer2_chain_setflush(chain);
5204 hammer2_chain_delete_obref(hammer2_chain_t *parent, hammer2_chain_t *chain,
5205 hammer2_tid_t mtid, int flags,
5206 hammer2_blockref_t *obref)
5210 KKASSERT(hammer2_mtx_owned(&chain->lock));
5213 * Nothing to do if already marked.
5215 * We need the spinlock on the core whos RBTREE contains chain
5216 * to protect against races.
5218 obref->type = HAMMER2_BREF_TYPE_EMPTY;
5219 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
5220 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
5221 chain->parent == parent);
5222 error = _hammer2_chain_delete_helper(parent, chain,
5223 mtid, flags, obref);
5227 * Permanent deletions mark the chain as destroyed.
5229 * NOTE: We do not setflush the chain unless the deletion is
5230 * permanent, since the deletion of a chain does not actually
5231 * require it to be flushed.
5234 if (flags & HAMMER2_DELETE_PERMANENT) {
5235 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
5236 hammer2_chain_setflush(chain);
5244 * Returns the index of the nearest element in the blockref array >= elm.
5245 * Returns (count) if no element could be found.
5247 * Sets *key_nextp to the next key for loop purposes but does not modify
5248 * it if the next key would be higher than the current value of *key_nextp.
5249 * Note that *key_nexp can overflow to 0, which should be tested by the
5252 * WARNING! Must be called with parent's spinlock held. Spinlock remains
5253 * held through the operation.
5256 hammer2_base_find(hammer2_chain_t *parent,
5257 hammer2_blockref_t *base, int count,
5258 hammer2_key_t *key_nextp,
5259 hammer2_key_t key_beg, hammer2_key_t key_end)
5261 hammer2_blockref_t *scan;
5262 hammer2_key_t scan_end;
5267 * Require the live chain's already have their core's counted
5268 * so we can optimize operations.
5270 KKASSERT(parent->flags & HAMMER2_CHAIN_COUNTEDBREFS);
5275 if (count == 0 || base == NULL)
5279 * Sequential optimization using parent->cache_index. This is
5280 * the most likely scenario.
5282 * We can avoid trailing empty entries on live chains, otherwise
5283 * we might have to check the whole block array.
5285 i = parent->cache_index; /* SMP RACE OK */
5287 limit = parent->core.live_zero;
5292 KKASSERT(i < count);
5298 while (i > 0 && (scan->type == HAMMER2_BREF_TYPE_EMPTY ||
5299 scan->key > key_beg)) {
5303 parent->cache_index = i;
5306 * Search forwards, stop when we find a scan element which
5307 * encloses the key or until we know that there are no further
5311 if (scan->type != HAMMER2_BREF_TYPE_EMPTY) {
5312 scan_end = scan->key +
5313 ((hammer2_key_t)1 << scan->keybits) - 1;
5314 if (scan->key > key_beg || scan_end >= key_beg)
5323 parent->cache_index = i;
5327 scan_end = scan->key +
5328 ((hammer2_key_t)1 << scan->keybits);
5329 if (scan_end && (*key_nextp > scan_end ||
5331 *key_nextp = scan_end;
5339 * Do a combined search and return the next match either from the blockref
5340 * array or from the in-memory chain. Sets *bresp to the returned bref in
5341 * both cases, or sets it to NULL if the search exhausted. Only returns
5342 * a non-NULL chain if the search matched from the in-memory chain.
5344 * When no in-memory chain has been found and a non-NULL bref is returned
5348 * The returned chain is not locked or referenced. Use the returned bref
5349 * to determine if the search exhausted or not. Iterate if the base find
5350 * is chosen but matches a deleted chain.
5352 * WARNING! Must be called with parent's spinlock held. Spinlock remains
5353 * held through the operation.
5356 hammer2_combined_find(hammer2_chain_t *parent,
5357 hammer2_blockref_t *base, int count,
5358 hammer2_key_t *key_nextp,
5359 hammer2_key_t key_beg, hammer2_key_t key_end,
5360 hammer2_blockref_t **bresp)
5362 hammer2_blockref_t *bref;
5363 hammer2_chain_t *chain;
5367 * Lookup in block array and in rbtree.
5369 *key_nextp = key_end + 1;
5370 i = hammer2_base_find(parent, base, count, key_nextp,
5372 chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
5377 if (i == count && chain == NULL) {
5383 * Only chain matched.
5386 bref = &chain->bref;
5391 * Only blockref matched.
5393 if (chain == NULL) {
5399 * Both in-memory and blockref matched, select the nearer element.
5401 * If both are flush with the left-hand side or both are the
5402 * same distance away, select the chain. In this situation the
5403 * chain must have been loaded from the matching blockmap.
5405 if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
5406 chain->bref.key == base[i].key) {
5407 KKASSERT(chain->bref.key == base[i].key);
5408 bref = &chain->bref;
5413 * Select the nearer key
5415 if (chain->bref.key < base[i].key) {
5416 bref = &chain->bref;
5423 * If the bref is out of bounds we've exhausted our search.
5426 if (bref->key > key_end) {
5436 * Locate the specified block array element and delete it. The element
5439 * The spin lock on the related chain must be held.
5441 * NOTE: live_count was adjusted when the chain was deleted, so it does not
5442 * need to be adjusted when we commit the media change.
5445 hammer2_base_delete(hammer2_chain_t *parent,
5446 hammer2_blockref_t *base, int count,
5447 hammer2_chain_t *chain,
5448 hammer2_blockref_t *obref)
5450 hammer2_blockref_t *elm = &chain->bref;
5451 hammer2_blockref_t *scan;
5452 hammer2_key_t key_next;
5456 * Delete element. Expect the element to exist.
5458 * XXX see caller, flush code not yet sophisticated enough to prevent
5459 * re-flushed in some cases.
5461 key_next = 0; /* max range */
5462 i = hammer2_base_find(parent, base, count, &key_next,
5463 elm->key, elm->key);
5465 if (i == count || scan->type == HAMMER2_BREF_TYPE_EMPTY ||
5466 scan->key != elm->key ||
5467 ((chain->flags & HAMMER2_CHAIN_BMAPUPD) == 0 &&
5468 scan->keybits != elm->keybits)) {
5469 hammer2_spin_unex(&parent->core.spin);
5470 panic("delete base %p element not found at %d/%d elm %p\n",
5471 base, i, count, elm);
5476 * Update stats and zero the entry.
5478 * NOTE: Handle radix == 0 (0 bytes) case.
5480 if ((int)(scan->data_off & HAMMER2_OFF_MASK_RADIX)) {
5481 parent->bref.embed.stats.data_count -= (hammer2_off_t)1 <<
5482 (int)(scan->data_off & HAMMER2_OFF_MASK_RADIX);
5484 switch(scan->type) {
5485 case HAMMER2_BREF_TYPE_INODE:
5486 --parent->bref.embed.stats.inode_count;
5488 case HAMMER2_BREF_TYPE_DATA:
5489 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5490 atomic_set_int(&chain->flags,
5491 HAMMER2_CHAIN_HINT_LEAF_COUNT);
5493 if (parent->bref.leaf_count)
5494 --parent->bref.leaf_count;
5497 case HAMMER2_BREF_TYPE_INDIRECT:
5498 if (scan->type != HAMMER2_BREF_TYPE_DATA) {
5499 parent->bref.embed.stats.data_count -=
5500 scan->embed.stats.data_count;
5501 parent->bref.embed.stats.inode_count -=
5502 scan->embed.stats.inode_count;
5504 if (scan->type == HAMMER2_BREF_TYPE_INODE)
5506 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5507 atomic_set_int(&chain->flags,
5508 HAMMER2_CHAIN_HINT_LEAF_COUNT);
5510 if (parent->bref.leaf_count <= scan->leaf_count)
5511 parent->bref.leaf_count = 0;
5513 parent->bref.leaf_count -= scan->leaf_count;
5516 case HAMMER2_BREF_TYPE_DIRENT:
5517 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5518 atomic_set_int(&chain->flags,
5519 HAMMER2_CHAIN_HINT_LEAF_COUNT);
5521 if (parent->bref.leaf_count)
5522 --parent->bref.leaf_count;
5530 bzero(scan, sizeof(*scan));
5533 * We can only optimize parent->core.live_zero for live chains.
5535 if (parent->core.live_zero == i + 1) {
5536 while (--i >= 0 && base[i].type == HAMMER2_BREF_TYPE_EMPTY)
5538 parent->core.live_zero = i + 1;
5542 * Clear appropriate blockmap flags in chain.
5544 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED |
5545 HAMMER2_CHAIN_BMAPUPD);
5549 * Insert the specified element. The block array must not already have the
5550 * element and must have space available for the insertion.
5552 * The spin lock on the related chain must be held.
5554 * NOTE: live_count was adjusted when the chain was deleted, so it does not
5555 * need to be adjusted when we commit the media change.
5558 hammer2_base_insert(hammer2_chain_t *parent,
5559 hammer2_blockref_t *base, int count,
5560 hammer2_chain_t *chain, hammer2_blockref_t *elm)
5562 hammer2_key_t key_next;
5571 * Insert new element. Expect the element to not already exist
5572 * unless we are replacing it.
5574 * XXX see caller, flush code not yet sophisticated enough to prevent
5575 * re-flushed in some cases.
5577 key_next = 0; /* max range */
5578 i = hammer2_base_find(parent, base, count, &key_next,
5579 elm->key, elm->key);
5582 * Shortcut fill optimization, typical ordered insertion(s) may not
5585 KKASSERT(i >= 0 && i <= count);
5588 * Set appropriate blockmap flags in chain (if not NULL)
5591 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
5594 * Update stats and zero the entry
5596 if ((int)(elm->data_off & HAMMER2_OFF_MASK_RADIX)) {
5597 parent->bref.embed.stats.data_count += (hammer2_off_t)1 <<
5598 (int)(elm->data_off & HAMMER2_OFF_MASK_RADIX);
5601 case HAMMER2_BREF_TYPE_INODE:
5602 ++parent->bref.embed.stats.inode_count;
5604 case HAMMER2_BREF_TYPE_DATA:
5605 if (parent->bref.leaf_count != HAMMER2_BLOCKREF_LEAF_MAX)
5606 ++parent->bref.leaf_count;
5608 case HAMMER2_BREF_TYPE_INDIRECT:
5609 if (elm->type != HAMMER2_BREF_TYPE_DATA) {
5610 parent->bref.embed.stats.data_count +=
5611 elm->embed.stats.data_count;
5612 parent->bref.embed.stats.inode_count +=
5613 elm->embed.stats.inode_count;
5615 if (elm->type == HAMMER2_BREF_TYPE_INODE)
5617 if (parent->bref.leaf_count + elm->leaf_count <
5618 HAMMER2_BLOCKREF_LEAF_MAX) {
5619 parent->bref.leaf_count += elm->leaf_count;
5621 parent->bref.leaf_count = HAMMER2_BLOCKREF_LEAF_MAX;
5624 case HAMMER2_BREF_TYPE_DIRENT:
5625 if (parent->bref.leaf_count != HAMMER2_BLOCKREF_LEAF_MAX)
5626 ++parent->bref.leaf_count;
5634 * We can only optimize parent->core.live_zero for live chains.
5636 if (i == count && parent->core.live_zero < count) {
5637 i = parent->core.live_zero++;
5642 xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
5643 if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
5644 hammer2_spin_unex(&parent->core.spin);
5645 panic("insert base %p overlapping elements at %d elm %p\n",
5650 * Try to find an empty slot before or after.
5654 while (j > 0 || k < count) {
5656 if (j >= 0 && base[j].type == HAMMER2_BREF_TYPE_EMPTY) {
5660 bcopy(&base[j+1], &base[j],
5661 (i - j - 1) * sizeof(*base));
5667 if (k < count && base[k].type == HAMMER2_BREF_TYPE_EMPTY) {
5668 bcopy(&base[i], &base[i+1],
5669 (k - i) * sizeof(hammer2_blockref_t));
5673 * We can only update parent->core.live_zero for live
5676 if (parent->core.live_zero <= k)
5677 parent->core.live_zero = k + 1;
5682 panic("hammer2_base_insert: no room!");
5689 for (l = 0; l < count; ++l) {
5690 if (base[l].type != HAMMER2_BREF_TYPE_EMPTY) {
5691 key_next = base[l].key +
5692 ((hammer2_key_t)1 << base[l].keybits) - 1;
5696 while (++l < count) {
5697 if (base[l].type != HAMMER2_BREF_TYPE_EMPTY) {
5698 if (base[l].key <= key_next)
5699 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
5700 key_next = base[l].key +
5701 ((hammer2_key_t)1 << base[l].keybits) - 1;
5711 * Sort the blockref array for the chain. Used by the flush code to
5712 * sort the blockref[] array.
5714 * The chain must be exclusively locked AND spin-locked.
5716 typedef hammer2_blockref_t *hammer2_blockref_p;
5720 hammer2_base_sort_callback(const void *v1, const void *v2)
5722 hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
5723 hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
5726 * Make sure empty elements are placed at the end of the array
5728 if (bref1->type == HAMMER2_BREF_TYPE_EMPTY) {
5729 if (bref2->type == HAMMER2_BREF_TYPE_EMPTY)
5732 } else if (bref2->type == HAMMER2_BREF_TYPE_EMPTY) {
5739 if (bref1->key < bref2->key)
5741 if (bref1->key > bref2->key)
5747 hammer2_base_sort(hammer2_chain_t *chain)
5749 hammer2_blockref_t *base;
5752 switch(chain->bref.type) {
5753 case HAMMER2_BREF_TYPE_INODE:
5755 * Special shortcut for embedded data returns the inode
5756 * itself. Callers must detect this condition and access
5757 * the embedded data (the strategy code does this for us).
5759 * This is only applicable to regular files and softlinks.
5761 if (chain->data->ipdata.meta.op_flags &
5762 HAMMER2_OPFLAG_DIRECTDATA) {
5765 base = &chain->data->ipdata.u.blockset.blockref[0];
5766 count = HAMMER2_SET_COUNT;
5768 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
5769 case HAMMER2_BREF_TYPE_INDIRECT:
5771 * Optimize indirect blocks in the INITIAL state to avoid
5774 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
5775 base = &chain->data->npdata[0];
5776 count = chain->bytes / sizeof(hammer2_blockref_t);
5778 case HAMMER2_BREF_TYPE_VOLUME:
5779 base = &chain->data->voldata.sroot_blockset.blockref[0];
5780 count = HAMMER2_SET_COUNT;
5782 case HAMMER2_BREF_TYPE_FREEMAP:
5783 base = &chain->data->blkset.blockref[0];
5784 count = HAMMER2_SET_COUNT;
5787 kprintf("hammer2_chain_lookup: unrecognized "
5788 "blockref(A) type: %d",
5791 tsleep(&base, 0, "dead", 0);
5792 panic("hammer2_base_sort: unrecognized "
5793 "blockref(A) type: %d",
5795 base = NULL; /* safety */
5796 count = 0; /* safety */
5798 kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
5804 * Chain memory management
5807 hammer2_chain_wait(hammer2_chain_t *chain)
5809 tsleep(chain, 0, "chnflw", 1);
5812 const hammer2_media_data_t *
5813 hammer2_chain_rdata(hammer2_chain_t *chain)
5815 KKASSERT(chain->data != NULL);
5816 return (chain->data);
5819 hammer2_media_data_t *
5820 hammer2_chain_wdata(hammer2_chain_t *chain)
5822 KKASSERT(chain->data != NULL);
5823 return (chain->data);
5827 * Set the check data for a chain. This can be a heavy-weight operation
5828 * and typically only runs on-flush. For file data check data is calculated
5829 * when the logical buffers are flushed.
5832 hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata)
5834 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_NOTTESTED);
5836 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
5837 case HAMMER2_CHECK_NONE:
5839 case HAMMER2_CHECK_DISABLED:
5841 case HAMMER2_CHECK_ISCSI32:
5842 chain->bref.check.iscsi32.value =
5843 hammer2_icrc32(bdata, chain->bytes);
5845 case HAMMER2_CHECK_XXHASH64:
5846 chain->bref.check.xxhash64.value =
5847 XXH64(bdata, chain->bytes, XXH_HAMMER2_SEED);
5849 case HAMMER2_CHECK_SHA192:
5851 SHA256_CTX hash_ctx;
5853 uint8_t digest[SHA256_DIGEST_LENGTH];
5854 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
5857 SHA256_Init(&hash_ctx);
5858 SHA256_Update(&hash_ctx, bdata, chain->bytes);
5859 SHA256_Final(u.digest, &hash_ctx);
5860 u.digest64[2] ^= u.digest64[3];
5862 chain->bref.check.sha192.data,
5863 sizeof(chain->bref.check.sha192.data));
5866 case HAMMER2_CHECK_FREEMAP:
5867 chain->bref.check.freemap.icrc32 =
5868 hammer2_icrc32(bdata, chain->bytes);
5871 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
5872 chain->bref.methods);
5878 * Characterize a failed check code and try to trace back to the inode.
5881 hammer2_characterize_failed_chain(hammer2_chain_t *chain, uint64_t check,
5884 hammer2_chain_t *lchain;
5885 hammer2_chain_t *ochain;
5888 did = krateprintf(&krate_h2chk,
5889 "chain %016jx.%02x (%s) meth=%02x CHECK FAIL "
5890 "(flags=%08x, bref/data ",
5891 chain->bref.data_off,
5893 hammer2_bref_type_str(&chain->bref),
5894 chain->bref.methods,
5900 kprintf("%08x/%08x)\n",
5901 chain->bref.check.iscsi32.value,
5904 kprintf("%016jx/%016jx)\n",
5905 chain->bref.check.xxhash64.value,
5910 * Run up the chains to try to find the governing inode so we
5913 * XXX This error reporting is not really MPSAFE
5917 while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
5919 chain = chain->parent;
5922 if (chain && chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
5923 ((chain->bref.flags & HAMMER2_BREF_FLAG_PFSROOT) == 0 ||
5924 (lchain->bref.key & HAMMER2_DIRHASH_VISIBLE))) {
5925 kprintf(" Resides at/in inode %ld\n",
5927 } else if (chain && chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
5928 kprintf(" Resides in inode index - CRITICAL!!!\n");
5930 kprintf(" Resides in root index - CRITICAL!!!\n");
5933 const char *pfsname = "UNKNOWN";
5937 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
5938 if (ochain->pmp->pfs_hmps[i] == ochain->hmp &&
5939 ochain->pmp->pfs_names[i]) {
5940 pfsname = ochain->pmp->pfs_names[i];
5945 kprintf(" In pfs %s on device %s\n",
5946 pfsname, ochain->hmp->devrepname);
5951 * Returns non-zero on success, 0 on failure.
5954 hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata)
5960 if (chain->flags & HAMMER2_CHAIN_NOTTESTED)
5963 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
5964 case HAMMER2_CHECK_NONE:
5967 case HAMMER2_CHECK_DISABLED:
5970 case HAMMER2_CHECK_ISCSI32:
5971 check32 = hammer2_icrc32(bdata, chain->bytes);
5972 r = (chain->bref.check.iscsi32.value == check32);
5974 hammer2_characterize_failed_chain(chain, check32, 32);
5976 hammer2_process_icrc32 += chain->bytes;
5978 case HAMMER2_CHECK_XXHASH64:
5979 check64 = XXH64(bdata, chain->bytes, XXH_HAMMER2_SEED);
5980 r = (chain->bref.check.xxhash64.value == check64);
5982 hammer2_characterize_failed_chain(chain, check64, 64);
5984 hammer2_process_xxhash64 += chain->bytes;
5986 case HAMMER2_CHECK_SHA192:
5988 SHA256_CTX hash_ctx;
5990 uint8_t digest[SHA256_DIGEST_LENGTH];
5991 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
5994 SHA256_Init(&hash_ctx);
5995 SHA256_Update(&hash_ctx, bdata, chain->bytes);
5996 SHA256_Final(u.digest, &hash_ctx);
5997 u.digest64[2] ^= u.digest64[3];
5999 chain->bref.check.sha192.data,
6000 sizeof(chain->bref.check.sha192.data)) == 0) {
6004 krateprintf(&krate_h2chk,
6005 "chain %016jx.%02x meth=%02x "
6007 chain->bref.data_off,
6009 chain->bref.methods);
6013 case HAMMER2_CHECK_FREEMAP:
6014 r = (chain->bref.check.freemap.icrc32 ==
6015 hammer2_icrc32(bdata, chain->bytes));
6019 did = krateprintf(&krate_h2chk,
6020 "chain %016jx.%02x meth=%02x "
6022 chain->bref.data_off,
6024 chain->bref.methods);
6026 kprintf("freemap.icrc %08x icrc32 %08x (%d)\n",
6027 chain->bref.check.freemap.icrc32,
6028 hammer2_icrc32(bdata, chain->bytes),
6031 kprintf("dio %p buf %016jx,%d "
6034 chain->dio->bp->b_loffset,
6035 chain->dio->bp->b_bufsize,
6037 chain->dio->bp->b_data);
6043 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
6044 chain->bref.methods);
6052 * Acquire the chain and parent representing the specified inode for the
6053 * device at the specified cluster index.
6055 * The flags passed in are LOOKUP flags, not RESOLVE flags.
6057 * If we are unable to locate the inode, HAMMER2_ERROR_EIO is returned and
6058 * *chainp will be NULL. *parentp may still be set error or not, or NULL
6059 * if the parent itself could not be resolved.
6061 * The caller may pass-in a locked *parentp and/or *chainp, or neither.
6062 * They will be unlocked and released by this function. The *parentp and
6063 * *chainp representing the located inode are returned locked.
6066 hammer2_chain_inode_find(hammer2_pfs_t *pmp, hammer2_key_t inum,
6067 int clindex, int flags,
6068 hammer2_chain_t **parentp, hammer2_chain_t **chainp)
6070 hammer2_chain_t *parent;
6071 hammer2_chain_t *rchain;
6072 hammer2_key_t key_dummy;
6073 hammer2_inode_t *ip;
6077 resolve_flags = (flags & HAMMER2_LOOKUP_SHARED) ?
6078 HAMMER2_RESOLVE_SHARED : 0;
6081 * Caller expects us to replace these.
6084 hammer2_chain_unlock(*chainp);
6085 hammer2_chain_drop(*chainp);
6089 hammer2_chain_unlock(*parentp);
6090 hammer2_chain_drop(*parentp);
6095 * Be very careful, this is a backend function and we CANNOT
6096 * lock any frontend inode structure we find. But we have to
6097 * look the inode up this way first in case it exists but is
6098 * detached from the radix tree.
6100 ip = hammer2_inode_lookup(pmp, inum);
6102 *chainp = hammer2_inode_chain_and_parent(ip, clindex,
6105 hammer2_inode_drop(ip);
6108 hammer2_chain_unlock(*chainp);
6109 hammer2_chain_drop(*chainp);
6112 hammer2_chain_unlock(*parentp);
6113 hammer2_chain_drop(*parentp);
6119 * Inodes hang off of the iroot (bit 63 is clear, differentiating
6120 * inodes from root directory entries in the key lookup).
6122 parent = hammer2_inode_chain(pmp->iroot, clindex, resolve_flags);
6125 rchain = hammer2_chain_lookup(&parent, &key_dummy,
6129 error = HAMMER2_ERROR_EIO;
6138 * Used by the bulkscan code to snapshot the synchronized storage for
6139 * a volume, allowing it to be scanned concurrently against normal
6143 hammer2_chain_bulksnap(hammer2_dev_t *hmp)
6145 hammer2_chain_t *copy;
6147 copy = hammer2_chain_alloc(hmp, hmp->spmp, &hmp->vchain.bref);
6148 copy->data = kmalloc(sizeof(copy->data->voldata),
6151 hammer2_voldata_lock(hmp);
6152 copy->data->voldata = hmp->volsync;
6153 hammer2_voldata_unlock(hmp);
6159 hammer2_chain_bulkdrop(hammer2_chain_t *copy)
6161 KKASSERT(copy->bref.type == HAMMER2_BREF_TYPE_VOLUME);
6162 KKASSERT(copy->data);
6163 kfree(copy->data, copy->hmp->mchain);
6165 atomic_add_long(&hammer2_chain_allocs, -1);
6166 hammer2_chain_drop(copy);
6170 * Returns non-zero if the chain (INODE or DIRENT) matches the
6174 hammer2_chain_dirent_test(hammer2_chain_t *chain, const char *name,
6177 const hammer2_inode_data_t *ripdata;
6178 const hammer2_dirent_head_t *den;
6180 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
6181 ripdata = &chain->data->ipdata;
6182 if (ripdata->meta.name_len == name_len &&
6183 bcmp(ripdata->filename, name, name_len) == 0) {
6187 if (chain->bref.type == HAMMER2_BREF_TYPE_DIRENT &&
6188 chain->bref.embed.dirent.namlen == name_len) {
6189 den = &chain->bref.embed.dirent;
6190 if (name_len > sizeof(chain->bref.check.buf) &&
6191 bcmp(chain->data->buf, name, name_len) == 0) {
6194 if (name_len <= sizeof(chain->bref.check.buf) &&
6195 bcmp(chain->bref.check.buf, name, name_len) == 0) {