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 KKASSERT(chain->refs > 0);
410 if (hammer2_mtx_ex_try(&chain->lock) == 0)
411 chain = hammer2_chain_lastdrop(chain, 0);
412 /* retry the same chain, or chain from lastdrop */
414 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
416 /* retry the same chain */
423 * Unhold a held and probably not-locked chain, ensure that the data is
424 * dropped on the 1->0 transition of lockcnt by obtaining an exclusive
425 * lock and then simply unlocking the chain.
428 hammer2_chain_unhold(hammer2_chain_t *chain)
434 lockcnt = chain->lockcnt;
437 if (atomic_cmpset_int(&chain->lockcnt,
438 lockcnt, lockcnt - 1)) {
441 } else if (hammer2_mtx_ex_try(&chain->lock) == 0) {
442 hammer2_chain_unlock(chain);
446 * This situation can easily occur on SMP due to
447 * the gap inbetween the 1->0 transition and the
448 * final unlock. We cannot safely block on the
449 * mutex because lockcnt might go above 1.
451 * XXX Sleep for one tick if it takes too long.
454 if (iter > 1000 + hz) {
455 kprintf("hammer2: h2race1 %p\n", chain);
458 tsleep(&iter, 0, "h2race1", 1);
466 hammer2_chain_drop_unhold(hammer2_chain_t *chain)
468 hammer2_chain_unhold(chain);
469 hammer2_chain_drop(chain);
473 hammer2_chain_rehold(hammer2_chain_t *chain)
475 hammer2_chain_lock(chain, HAMMER2_RESOLVE_SHARED);
476 atomic_add_int(&chain->lockcnt, 1);
477 hammer2_chain_unlock(chain);
481 * Handles the (potential) last drop of chain->refs from 1->0. Called with
482 * the mutex exclusively locked, refs == 1, and lockcnt 0. SMP races are
483 * possible against refs and lockcnt. We must dispose of the mutex on chain.
485 * This function returns an unlocked chain for recursive drop or NULL. It
486 * can return the same chain if it determines it has raced another ref.
490 * When two chains need to be recursively dropped we use the chain we
491 * would otherwise free to placehold the additional chain. It's a bit
492 * convoluted but we can't just recurse without potentially blowing out
495 * The chain cannot be freed if it has any children.
496 * The chain cannot be freed if flagged MODIFIED unless we can dispose of it.
497 * The chain cannot be freed if flagged UPDATE unless we can dispose of it.
498 * Any dedup registration can remain intact.
500 * The core spinlock is allowed to nest child-to-parent (not parent-to-child).
504 hammer2_chain_lastdrop(hammer2_chain_t *chain, int depth)
508 hammer2_chain_t *parent;
509 hammer2_chain_t *rdrop;
512 * We need chain's spinlock to interlock the sub-tree test.
513 * We already have chain's mutex, protecting chain->parent.
515 * Remember that chain->refs can be in flux.
517 hammer2_spin_ex(&chain->core.spin);
519 if (chain->parent != NULL) {
521 * If the chain has a parent the UPDATE bit prevents scrapping
522 * as the chain is needed to properly flush the parent. Try
523 * to complete the 1->0 transition and return NULL. Retry
524 * (return chain) if we are unable to complete the 1->0
525 * transition, else return NULL (nothing more to do).
527 * If the chain has a parent the MODIFIED bit prevents
530 * Chains with UPDATE/MODIFIED are *not* put on the LRU list!
532 if (chain->flags & (HAMMER2_CHAIN_UPDATE |
533 HAMMER2_CHAIN_MODIFIED)) {
534 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
535 hammer2_spin_unex(&chain->core.spin);
536 hammer2_chain_assert_no_data(chain);
537 hammer2_mtx_unlock(&chain->lock);
540 hammer2_spin_unex(&chain->core.spin);
541 hammer2_mtx_unlock(&chain->lock);
545 /* spinlock still held */
546 } else if (chain->bref.type == HAMMER2_BREF_TYPE_VOLUME ||
547 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP) {
549 * Retain the static vchain and fchain. Clear bits that
550 * are not relevant. Do not clear the MODIFIED bit,
551 * and certainly do not put it on the delayed-flush queue.
553 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
556 * The chain has no parent and can be flagged for destruction.
557 * Since it has no parent, UPDATE can also be cleared.
559 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
560 if (chain->flags & HAMMER2_CHAIN_UPDATE)
561 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
564 * If the chain has children we must propagate the DESTROY
565 * flag downward and rip the disconnected topology apart.
566 * This is accomplished by calling hammer2_flush() on the
569 * Any dedup is already handled by the underlying DIO, so
570 * we do not have to specifically flush it here.
572 if (chain->core.chain_count) {
573 hammer2_spin_unex(&chain->core.spin);
574 hammer2_flush(chain, HAMMER2_FLUSH_TOP |
576 hammer2_mtx_unlock(&chain->lock);
578 return(chain); /* retry drop */
582 * Otherwise we can scrap the MODIFIED bit if it is set,
583 * and continue along the freeing path.
585 * Be sure to clean-out any dedup bits. Without a parent
586 * this chain will no longer be visible to the flush code.
587 * Easy check data_off to avoid the volume root.
589 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
590 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
591 atomic_add_long(&hammer2_count_modified_chains, -1);
593 hammer2_pfs_memory_wakeup(chain->pmp, -1);
595 /* spinlock still held */
598 /* spinlock still held */
601 * If any children exist we must leave the chain intact with refs == 0.
602 * They exist because chains are retained below us which have refs or
603 * may require flushing.
605 * Retry (return chain) if we fail to transition the refs to 0, else
606 * return NULL indication nothing more to do.
608 * Chains with children are NOT put on the LRU list.
610 if (chain->core.chain_count) {
611 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
612 hammer2_spin_unex(&chain->core.spin);
613 hammer2_chain_assert_no_data(chain);
614 hammer2_mtx_unlock(&chain->lock);
617 hammer2_spin_unex(&chain->core.spin);
618 hammer2_mtx_unlock(&chain->lock);
622 /* spinlock still held */
623 /* no chains left under us */
626 * chain->core has no children left so no accessors can get to our
627 * chain from there. Now we have to lock the parent core to interlock
628 * remaining possible accessors that might bump chain's refs before
629 * we can safely drop chain's refs with intent to free the chain.
632 pmp = chain->pmp; /* can be NULL */
635 parent = chain->parent;
638 * WARNING! chain's spin lock is still held here, and other spinlocks
639 * will be acquired and released in the code below. We
640 * cannot be making fancy procedure calls!
644 * We can cache the chain if it is associated with a pmp
645 * and not flagged as being destroyed or requesting a full
646 * release. In this situation the chain is not removed
647 * from its parent, i.e. it can still be looked up.
649 * We intentionally do not cache DATA chains because these
650 * were likely used to load data into the logical buffer cache
651 * and will not be accessed again for some time.
654 (HAMMER2_CHAIN_DESTROY | HAMMER2_CHAIN_RELEASE)) == 0 &&
656 chain->bref.type != HAMMER2_BREF_TYPE_DATA) {
658 hammer2_spin_ex(&parent->core.spin);
659 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
661 * 1->0 transition failed, retry. Do not drop
662 * the chain's data yet!
665 hammer2_spin_unex(&parent->core.spin);
666 hammer2_spin_unex(&chain->core.spin);
667 hammer2_mtx_unlock(&chain->lock);
675 hammer2_chain_assert_no_data(chain);
678 * Make sure we are on the LRU list, clean up excessive
679 * LRU entries. We can only really drop one but there might
680 * be other entries that we can remove from the lru_list
683 * NOTE: HAMMER2_CHAIN_ONLRU may only be safely set when
684 * chain->core.spin AND pmp->lru_spin are held, but
685 * can be safely cleared only holding pmp->lru_spin.
687 if ((chain->flags & HAMMER2_CHAIN_ONLRU) == 0) {
688 hammer2_spin_ex(&pmp->lru_spin);
689 if ((chain->flags & HAMMER2_CHAIN_ONLRU) == 0) {
690 atomic_set_int(&chain->flags,
691 HAMMER2_CHAIN_ONLRU);
692 TAILQ_INSERT_TAIL(&pmp->lru_list,
694 atomic_add_int(&pmp->lru_count, 1);
696 if (pmp->lru_count < HAMMER2_LRU_LIMIT)
697 depth = 1; /* disable lru_list flush */
698 hammer2_spin_unex(&pmp->lru_spin);
700 /* disable lru flush */
705 hammer2_spin_unex(&parent->core.spin);
706 parent = NULL; /* safety */
708 hammer2_spin_unex(&chain->core.spin);
709 hammer2_mtx_unlock(&chain->lock);
712 * lru_list hysteresis (see above for depth overrides).
713 * Note that depth also prevents excessive lastdrop recursion.
716 hammer2_chain_lru_flush(pmp);
723 * Make sure we are not on the LRU list.
725 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
726 hammer2_spin_ex(&pmp->lru_spin);
727 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
728 atomic_add_int(&pmp->lru_count, -1);
729 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
730 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
732 hammer2_spin_unex(&pmp->lru_spin);
736 * Spinlock the parent and try to drop the last ref on chain.
737 * On success determine if we should dispose of the chain
738 * (remove the chain from its parent, etc).
740 * (normal core locks are top-down recursive but we define
741 * core spinlocks as bottom-up recursive, so this is safe).
744 hammer2_spin_ex(&parent->core.spin);
745 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
747 * 1->0 transition failed, retry.
749 hammer2_spin_unex(&parent->core.spin);
750 hammer2_spin_unex(&chain->core.spin);
751 hammer2_mtx_unlock(&chain->lock);
757 * 1->0 transition successful, parent spin held to prevent
758 * new lookups, chain spinlock held to protect parent field.
759 * Remove chain from the parent.
761 * If the chain is being removed from the parent's btree but
762 * is not bmapped, we have to adjust live_count downward. If
763 * it is bmapped then the blockref is retained in the parent
764 * as is its associated live_count. This case can occur when
765 * a chain added to the topology is unable to flush and is
766 * then later deleted.
768 if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
769 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) &&
770 (chain->flags & HAMMER2_CHAIN_BMAPPED) == 0) {
771 atomic_add_int(&parent->core.live_count, -1);
773 RB_REMOVE(hammer2_chain_tree,
774 &parent->core.rbtree, chain);
775 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
776 --parent->core.chain_count;
777 chain->parent = NULL;
781 * If our chain was the last chain in the parent's core the
782 * core is now empty and its parent might have to be
783 * re-dropped if it has 0 refs.
785 if (parent->core.chain_count == 0) {
787 atomic_add_int(&rdrop->refs, 1);
789 if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0)
793 hammer2_spin_unex(&parent->core.spin);
794 parent = NULL; /* safety */
800 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
802 * 1->0 transition failed, retry.
804 hammer2_spin_unex(&parent->core.spin);
805 hammer2_spin_unex(&chain->core.spin);
806 hammer2_mtx_unlock(&chain->lock);
813 * Successful 1->0 transition, no parent, no children... no way for
814 * anyone to ref this chain any more. We can clean-up and free it.
816 * We still have the core spinlock, and core's chain_count is 0.
817 * Any parent spinlock is gone.
819 hammer2_spin_unex(&chain->core.spin);
820 hammer2_chain_assert_no_data(chain);
821 hammer2_mtx_unlock(&chain->lock);
822 KKASSERT(RB_EMPTY(&chain->core.rbtree) &&
823 chain->core.chain_count == 0);
826 * All locks are gone, no pointers remain to the chain, finish
829 KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE |
830 HAMMER2_CHAIN_MODIFIED)) == 0);
833 * Once chain resources are gone we can use the now dead chain
834 * structure to placehold what might otherwise require a recursive
835 * drop, because we have potentially two things to drop and can only
836 * return one directly.
838 if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
839 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ALLOCATED);
841 kfree(chain, hmp->mchain);
845 * Possible chaining loop when parent re-drop needed.
851 * Heuristical flush of the LRU, try to reduce the number of entries
852 * on the LRU to (HAMMER2_LRU_LIMIT * 2 / 3). This procedure is called
853 * only when lru_count exceeds HAMMER2_LRU_LIMIT.
857 hammer2_chain_lru_flush(hammer2_pfs_t *pmp)
859 hammer2_chain_t *chain;
863 hammer2_spin_ex(&pmp->lru_spin);
864 while (pmp->lru_count > HAMMER2_LRU_LIMIT * 2 / 3) {
866 * Pick a chain off the lru_list, just recycle it quickly
867 * if LRUHINT is set (the chain was ref'd but left on
868 * the lru_list, so cycle to the end).
870 chain = TAILQ_FIRST(&pmp->lru_list);
871 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
873 if (chain->flags & HAMMER2_CHAIN_LRUHINT) {
874 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_LRUHINT);
875 TAILQ_INSERT_TAIL(&pmp->lru_list, chain, lru_node);
881 * Ok, we are off the LRU. We must adjust refs before we
882 * can safely clear the ONLRU flag.
884 atomic_add_int(&pmp->lru_count, -1);
885 if (atomic_cmpset_int(&chain->refs, 0, 1)) {
886 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
887 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
890 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
893 hammer2_spin_unex(&pmp->lru_spin);
898 * If we picked a chain off the lru list we may be able to lastdrop
899 * it. Use a depth of 1 to prevent excessive lastdrop recursion.
909 if (hammer2_mtx_ex_try(&chain->lock) == 0)
910 chain = hammer2_chain_lastdrop(chain, 1);
911 /* retry the same chain, or chain from lastdrop */
913 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
915 /* retry the same chain */
923 * On last lock release.
925 static hammer2_io_t *
926 hammer2_chain_drop_data(hammer2_chain_t *chain)
930 if ((dio = chain->dio) != NULL) {
934 switch(chain->bref.type) {
935 case HAMMER2_BREF_TYPE_VOLUME:
936 case HAMMER2_BREF_TYPE_FREEMAP:
939 if (chain->data != NULL) {
940 hammer2_spin_unex(&chain->core.spin);
941 panic("chain data not null: "
942 "chain %p bref %016jx.%02x "
943 "refs %d parent %p dio %p data %p",
944 chain, chain->bref.data_off,
945 chain->bref.type, chain->refs,
947 chain->dio, chain->data);
949 KKASSERT(chain->data == NULL);
957 * Lock a referenced chain element, acquiring its data with I/O if necessary,
958 * and specify how you would like the data to be resolved.
960 * If an I/O or other fatal error occurs, chain->error will be set to non-zero.
962 * The lock is allowed to recurse, multiple locking ops will aggregate
963 * the requested resolve types. Once data is assigned it will not be
964 * removed until the last unlock.
966 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
967 * (typically used to avoid device/logical buffer
970 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
971 * the INITIAL-create state (indirect blocks only).
973 * Do not resolve data elements for DATA chains.
974 * (typically used to avoid device/logical buffer
977 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
979 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
980 * it will be locked exclusive.
982 * HAMMER2_RESOLVE_NONBLOCK- (flag) The chain is locked non-blocking. If
983 * the lock fails, EAGAIN is returned.
985 * NOTE: Embedded elements (volume header, inodes) are always resolved
988 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
989 * element will instantiate and zero its buffer, and flush it on
992 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
993 * so as not to instantiate a device buffer, which could alias against
994 * a logical file buffer. However, if ALWAYS is specified the
995 * device buffer will be instantiated anyway.
997 * NOTE: The return value is always 0 unless NONBLOCK is specified, in which
998 * case it can be either 0 or EAGAIN.
1000 * WARNING! This function blocks on I/O if data needs to be fetched. This
1001 * blocking can run concurrent with other compatible lock holders
1002 * who do not need data returning. The lock is not upgraded to
1003 * exclusive during a data fetch, a separate bit is used to
1004 * interlock I/O. However, an exclusive lock holder can still count
1005 * on being interlocked against an I/O fetch managed by a shared
1009 hammer2_chain_lock(hammer2_chain_t *chain, int how)
1011 KKASSERT(chain->refs > 0);
1013 if (how & HAMMER2_RESOLVE_NONBLOCK) {
1015 * We still have to bump lockcnt before acquiring the lock,
1016 * even for non-blocking operation, because the unlock code
1017 * live-loops on lockcnt == 1 when dropping the last lock.
1019 * If the non-blocking operation fails we have to use an
1020 * unhold sequence to undo the mess.
1022 * NOTE: LOCKAGAIN must always succeed without blocking,
1023 * even if NONBLOCK is specified.
1025 atomic_add_int(&chain->lockcnt, 1);
1026 if (how & HAMMER2_RESOLVE_SHARED) {
1027 if (how & HAMMER2_RESOLVE_LOCKAGAIN) {
1028 hammer2_mtx_sh_again(&chain->lock);
1030 if (hammer2_mtx_sh_try(&chain->lock) != 0) {
1031 hammer2_chain_unhold(chain);
1036 if (hammer2_mtx_ex_try(&chain->lock) != 0) {
1037 hammer2_chain_unhold(chain);
1043 * Get the appropriate lock. If LOCKAGAIN is flagged with
1044 * SHARED the caller expects a shared lock to already be
1045 * present and we are giving it another ref. This case must
1046 * importantly not block if there is a pending exclusive lock
1049 atomic_add_int(&chain->lockcnt, 1);
1050 if (how & HAMMER2_RESOLVE_SHARED) {
1051 if (how & HAMMER2_RESOLVE_LOCKAGAIN) {
1052 hammer2_mtx_sh_again(&chain->lock);
1054 hammer2_mtx_sh(&chain->lock);
1057 hammer2_mtx_ex(&chain->lock);
1062 * If we already have a valid data pointer make sure the data is
1063 * synchronized to the current cpu, and then no further action is
1068 hammer2_io_bkvasync(chain->dio);
1073 * Do we have to resolve the data? This is generally only
1074 * applicable to HAMMER2_BREF_TYPE_DATA which is special-cased.
1075 * Other BREF types expects the data to be there.
1077 switch(how & HAMMER2_RESOLVE_MASK) {
1078 case HAMMER2_RESOLVE_NEVER:
1080 case HAMMER2_RESOLVE_MAYBE:
1081 if (chain->flags & HAMMER2_CHAIN_INITIAL)
1083 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
1086 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
1088 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
1092 case HAMMER2_RESOLVE_ALWAYS:
1098 * Caller requires data
1100 hammer2_chain_load_data(chain);
1106 * Lock the chain, retain the hold, and drop the data persistence count.
1107 * The data should remain valid because we never transitioned lockcnt
1111 hammer2_chain_lock_unhold(hammer2_chain_t *chain, int how)
1113 hammer2_chain_lock(chain, how);
1114 atomic_add_int(&chain->lockcnt, -1);
1119 * Downgrade an exclusive chain lock to a shared chain lock.
1121 * NOTE: There is no upgrade equivalent due to the ease of
1122 * deadlocks in that direction.
1125 hammer2_chain_lock_downgrade(hammer2_chain_t *chain)
1127 hammer2_mtx_downgrade(&chain->lock);
1132 * Issue I/O and install chain->data. Caller must hold a chain lock, lock
1133 * may be of any type.
1135 * Once chain->data is set it cannot be disposed of until all locks are
1138 * Make sure the data is synchronized to the current cpu.
1141 hammer2_chain_load_data(hammer2_chain_t *chain)
1143 hammer2_blockref_t *bref;
1150 * Degenerate case, data already present, or chain has no media
1151 * reference to load.
1153 KKASSERT(chain->lock.mtx_lock & MTX_MASK);
1156 hammer2_io_bkvasync(chain->dio);
1159 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0)
1163 KKASSERT(hmp != NULL);
1166 * Gain the IOINPROG bit, interlocked block.
1172 oflags = chain->flags;
1174 if (oflags & HAMMER2_CHAIN_IOINPROG) {
1175 nflags = oflags | HAMMER2_CHAIN_IOSIGNAL;
1176 tsleep_interlock(&chain->flags, 0);
1177 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1178 tsleep(&chain->flags, PINTERLOCKED,
1183 nflags = oflags | HAMMER2_CHAIN_IOINPROG;
1184 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1192 * We own CHAIN_IOINPROG
1194 * Degenerate case if we raced another load.
1198 hammer2_io_bkvasync(chain->dio);
1203 * We must resolve to a device buffer, either by issuing I/O or
1204 * by creating a zero-fill element. We do not mark the buffer
1205 * dirty when creating a zero-fill element (the hammer2_chain_modify()
1206 * API must still be used to do that).
1208 * The device buffer is variable-sized in powers of 2 down
1209 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
1210 * chunk always contains buffers of the same size. (XXX)
1212 * The minimum physical IO size may be larger than the variable
1215 bref = &chain->bref;
1218 * The getblk() optimization can only be used on newly created
1219 * elements if the physical block size matches the request.
1221 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1222 error = hammer2_io_new(hmp, bref->type,
1223 bref->data_off, chain->bytes,
1226 error = hammer2_io_bread(hmp, bref->type,
1227 bref->data_off, chain->bytes,
1229 hammer2_adjreadcounter(chain->bref.type, chain->bytes);
1232 chain->error = HAMMER2_ERROR_EIO;
1233 kprintf("hammer2_chain_load_data: I/O error %016jx: %d\n",
1234 (intmax_t)bref->data_off, error);
1235 hammer2_io_bqrelse(&chain->dio);
1241 * This isn't perfect and can be ignored on OSs which do not have
1242 * an indication as to whether a buffer is coming from cache or
1243 * if I/O was actually issued for the read. TESTEDGOOD will work
1244 * pretty well without the B_IOISSUED logic because chains are
1245 * cached, but in that situation (without B_IOISSUED) it will not
1246 * detect whether a re-read via I/O is corrupted verses the original
1249 * We can't re-run the CRC on every fresh lock. That would be
1250 * insanely expensive.
1252 * If the underlying kernel buffer covers the entire chain we can
1253 * use the B_IOISSUED indication to determine if we have to re-run
1254 * the CRC on chain data for chains that managed to stay cached
1255 * across the kernel disposal of the original buffer.
1257 if ((dio = chain->dio) != NULL && dio->bp) {
1258 struct buf *bp = dio->bp;
1260 if (dio->psize == chain->bytes &&
1261 (bp->b_flags & B_IOISSUED)) {
1262 atomic_clear_int(&chain->flags,
1263 HAMMER2_CHAIN_TESTEDGOOD);
1264 bp->b_flags &= ~B_IOISSUED;
1269 * NOTE: A locked chain's data cannot be modified without first
1270 * calling hammer2_chain_modify().
1274 * NOTE: hammer2_io_data() call issues bkvasync()
1276 bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
1278 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1280 * Clear INITIAL. In this case we used io_new() and the
1281 * buffer has been zero'd and marked dirty.
1283 * CHAIN_MODIFIED has not been set yet, and we leave it
1284 * that way for now. Set a temporary CHAIN_NOTTESTED flag
1285 * to prevent hammer2_chain_testcheck() from trying to match
1286 * a check code that has not yet been generated. This bit
1287 * should NOT end up on the actual media.
1289 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1290 atomic_set_int(&chain->flags, HAMMER2_CHAIN_NOTTESTED);
1291 } else if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
1293 * check data not currently synchronized due to
1294 * modification. XXX assumes data stays in the buffer
1295 * cache, which might not be true (need biodep on flush
1296 * to calculate crc? or simple crc?).
1298 } else if ((chain->flags & HAMMER2_CHAIN_TESTEDGOOD) == 0) {
1299 if (hammer2_chain_testcheck(chain, bdata) == 0) {
1300 chain->error = HAMMER2_ERROR_CHECK;
1302 atomic_set_int(&chain->flags, HAMMER2_CHAIN_TESTEDGOOD);
1307 * Setup the data pointer, either pointing it to an embedded data
1308 * structure and copying the data from the buffer, or pointing it
1311 * The buffer is not retained when copying to an embedded data
1312 * structure in order to avoid potential deadlocks or recursions
1313 * on the same physical buffer.
1315 * WARNING! Other threads can start using the data the instant we
1316 * set chain->data non-NULL.
1318 switch (bref->type) {
1319 case HAMMER2_BREF_TYPE_VOLUME:
1320 case HAMMER2_BREF_TYPE_FREEMAP:
1322 * Copy data from bp to embedded buffer
1324 panic("hammer2_chain_load_data: unresolved volume header");
1326 case HAMMER2_BREF_TYPE_DIRENT:
1327 KKASSERT(chain->bytes != 0);
1329 case HAMMER2_BREF_TYPE_INODE:
1330 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1331 case HAMMER2_BREF_TYPE_INDIRECT:
1332 case HAMMER2_BREF_TYPE_DATA:
1333 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1336 * Point data at the device buffer and leave dio intact.
1338 chain->data = (void *)bdata;
1343 * Release HAMMER2_CHAIN_IOINPROG and signal waiters if requested.
1350 oflags = chain->flags;
1351 nflags = oflags & ~(HAMMER2_CHAIN_IOINPROG |
1352 HAMMER2_CHAIN_IOSIGNAL);
1353 KKASSERT(oflags & HAMMER2_CHAIN_IOINPROG);
1354 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1355 if (oflags & HAMMER2_CHAIN_IOSIGNAL)
1356 wakeup(&chain->flags);
1363 * Unlock and deref a chain element.
1365 * Remember that the presence of children under chain prevent the chain's
1366 * destruction but do not add additional references, so the dio will still
1370 hammer2_chain_unlock(hammer2_chain_t *chain)
1377 * If multiple locks are present (or being attempted) on this
1378 * particular chain we can just unlock, drop refs, and return.
1380 * Otherwise fall-through on the 1->0 transition.
1383 lockcnt = chain->lockcnt;
1384 KKASSERT(lockcnt > 0);
1387 if (atomic_cmpset_int(&chain->lockcnt,
1388 lockcnt, lockcnt - 1)) {
1389 hammer2_mtx_unlock(&chain->lock);
1392 } else if (hammer2_mtx_upgrade_try(&chain->lock) == 0) {
1393 /* while holding the mutex exclusively */
1394 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
1398 * This situation can easily occur on SMP due to
1399 * the gap inbetween the 1->0 transition and the
1400 * final unlock. We cannot safely block on the
1401 * mutex because lockcnt might go above 1.
1403 * XXX Sleep for one tick if it takes too long.
1405 if (++iter > 1000) {
1406 if (iter > 1000 + hz) {
1407 kprintf("hammer2: h2race2 %p\n", chain);
1410 tsleep(&iter, 0, "h2race2", 1);
1418 * Last unlock / mutex upgraded to exclusive. Drop the data
1421 dio = hammer2_chain_drop_data(chain);
1423 hammer2_io_bqrelse(&dio);
1424 hammer2_mtx_unlock(&chain->lock);
1428 * Unlock and hold chain data intact
1431 hammer2_chain_unlock_hold(hammer2_chain_t *chain)
1433 atomic_add_int(&chain->lockcnt, 1);
1434 hammer2_chain_unlock(chain);
1438 * Helper to obtain the blockref[] array base and count for a chain.
1440 * XXX Not widely used yet, various use cases need to be validated and
1441 * converted to use this function.
1444 hammer2_blockref_t *
1445 hammer2_chain_base_and_count(hammer2_chain_t *parent, int *countp)
1447 hammer2_blockref_t *base;
1450 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1453 switch(parent->bref.type) {
1454 case HAMMER2_BREF_TYPE_INODE:
1455 count = HAMMER2_SET_COUNT;
1457 case HAMMER2_BREF_TYPE_INDIRECT:
1458 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1459 count = parent->bytes / sizeof(hammer2_blockref_t);
1461 case HAMMER2_BREF_TYPE_VOLUME:
1462 count = HAMMER2_SET_COUNT;
1464 case HAMMER2_BREF_TYPE_FREEMAP:
1465 count = HAMMER2_SET_COUNT;
1468 panic("hammer2_chain_base_and_count: "
1469 "unrecognized blockref type: %d",
1475 switch(parent->bref.type) {
1476 case HAMMER2_BREF_TYPE_INODE:
1477 base = &parent->data->ipdata.u.blockset.blockref[0];
1478 count = HAMMER2_SET_COUNT;
1480 case HAMMER2_BREF_TYPE_INDIRECT:
1481 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1482 base = &parent->data->npdata[0];
1483 count = parent->bytes / sizeof(hammer2_blockref_t);
1485 case HAMMER2_BREF_TYPE_VOLUME:
1486 base = &parent->data->voldata.
1487 sroot_blockset.blockref[0];
1488 count = HAMMER2_SET_COUNT;
1490 case HAMMER2_BREF_TYPE_FREEMAP:
1491 base = &parent->data->blkset.blockref[0];
1492 count = HAMMER2_SET_COUNT;
1495 panic("hammer2_chain_base_and_count: "
1496 "unrecognized blockref type: %d",
1508 * This counts the number of live blockrefs in a block array and
1509 * also calculates the point at which all remaining blockrefs are empty.
1510 * This routine can only be called on a live chain.
1512 * Caller holds the chain locked, but possibly with a shared lock. We
1513 * must use an exclusive spinlock to prevent corruption.
1515 * NOTE: Flag is not set until after the count is complete, allowing
1516 * callers to test the flag without holding the spinlock.
1518 * NOTE: If base is NULL the related chain is still in the INITIAL
1519 * state and there are no blockrefs to count.
1521 * NOTE: live_count may already have some counts accumulated due to
1522 * creation and deletion and could even be initially negative.
1525 hammer2_chain_countbrefs(hammer2_chain_t *chain,
1526 hammer2_blockref_t *base, int count)
1528 hammer2_spin_ex(&chain->core.spin);
1529 if ((chain->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) {
1531 while (--count >= 0) {
1532 if (base[count].type != HAMMER2_BREF_TYPE_EMPTY)
1535 chain->core.live_zero = count + 1;
1536 while (count >= 0) {
1537 if (base[count].type != HAMMER2_BREF_TYPE_EMPTY)
1538 atomic_add_int(&chain->core.live_count,
1543 chain->core.live_zero = 0;
1545 /* else do not modify live_count */
1546 atomic_set_int(&chain->flags, HAMMER2_CHAIN_COUNTEDBREFS);
1548 hammer2_spin_unex(&chain->core.spin);
1552 * Resize the chain's physical storage allocation in-place. This function does
1553 * not usually adjust the data pointer and must be followed by (typically) a
1554 * hammer2_chain_modify() call to copy any old data over and adjust the
1557 * Chains can be resized smaller without reallocating the storage. Resizing
1558 * larger will reallocate the storage. Excess or prior storage is reclaimed
1559 * asynchronously at a later time.
1561 * An nradix value of 0 is special-cased to mean that the storage should
1562 * be disassociated, that is the chain is being resized to 0 bytes (not 1
1565 * Must be passed an exclusively locked parent and chain.
1567 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
1568 * to avoid instantiating a device buffer that conflicts with the vnode data
1569 * buffer. However, because H2 can compress or encrypt data, the chain may
1570 * have a dio assigned to it in those situations, and they do not conflict.
1572 * XXX return error if cannot resize.
1575 hammer2_chain_resize(hammer2_chain_t *chain,
1576 hammer2_tid_t mtid, hammer2_off_t dedup_off,
1577 int nradix, int flags)
1587 * Only data and indirect blocks can be resized for now.
1588 * (The volu root, inodes, and freemap elements use a fixed size).
1590 KKASSERT(chain != &hmp->vchain);
1591 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1592 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1593 chain->bref.type == HAMMER2_BREF_TYPE_DIRENT);
1596 * Nothing to do if the element is already the proper size
1598 obytes = chain->bytes;
1599 nbytes = (nradix) ? (1U << nradix) : 0;
1600 if (obytes == nbytes)
1601 return (chain->error);
1604 * Make sure the old data is instantiated so we can copy it. If this
1605 * is a data block, the device data may be superfluous since the data
1606 * might be in a logical block, but compressed or encrypted data is
1609 * NOTE: The modify will set BMAPUPD for us if BMAPPED is set.
1611 error = hammer2_chain_modify(chain, mtid, dedup_off, 0);
1616 * Reallocate the block, even if making it smaller (because different
1617 * block sizes may be in different regions).
1619 * NOTE: Operation does not copy the data and may only be used
1620 * to resize data blocks in-place, or directory entry blocks
1621 * which are about to be modified in some manner.
1623 error = hammer2_freemap_alloc(chain, nbytes);
1627 chain->bytes = nbytes;
1630 * We don't want the followup chain_modify() to try to copy data
1631 * from the old (wrong-sized) buffer. It won't know how much to
1632 * copy. This case should only occur during writes when the
1633 * originator already has the data to write in-hand.
1636 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1637 chain->bref.type == HAMMER2_BREF_TYPE_DIRENT);
1638 hammer2_io_brelse(&chain->dio);
1641 return (chain->error);
1645 * Set the chain modified so its data can be changed by the caller, or
1646 * install deduplicated data. The caller must call this routine for each
1647 * set of modifications it makes, even if the chain is already flagged
1650 * Sets bref.modify_tid to mtid only if mtid != 0. Note that bref.modify_tid
1651 * is a CLC (cluster level change) field and is not updated by parent
1652 * propagation during a flush.
1654 * Returns an appropriate HAMMER2_ERROR_* code, which will generally reflect
1655 * chain->error except for HAMMER2_ERROR_ENOSPC. If the allocation fails
1656 * due to no space available, HAMMER2_ERROR_ENOSPC is returned and the chain
1657 * remains unmodified with its old data ref intact and chain->error
1662 * If the DEDUPABLE flag is set in the chain the storage must be reallocated
1663 * even if the chain is still flagged MODIFIED. In this case the chain's
1664 * DEDUPABLE flag will be cleared once the new storage has been assigned.
1666 * If the caller passes a non-zero dedup_off we will use it to assign the
1667 * new storage. The MODIFIED flag will be *CLEARED* in this case, and
1668 * DEDUPABLE will be set (NOTE: the UPDATE flag is always set). The caller
1669 * must not modify the data content upon return.
1672 hammer2_chain_modify(hammer2_chain_t *chain, hammer2_tid_t mtid,
1673 hammer2_off_t dedup_off, int flags)
1675 hammer2_blockref_t obref;
1686 obref = chain->bref;
1687 KKASSERT(chain->lock.mtx_lock & MTX_EXCLUSIVE);
1690 * Data is not optional for freemap chains (we must always be sure
1691 * to copy the data on COW storage allocations).
1693 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1694 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1695 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1696 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1700 * Data must be resolved if already assigned, unless explicitly
1701 * flagged otherwise. If we cannot safety load the data the
1702 * modification fails and we return early.
1704 if (chain->data == NULL && chain->bytes != 0 &&
1705 (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1706 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1707 hammer2_chain_load_data(chain);
1709 return (chain->error);
1714 * Set MODIFIED to indicate that the chain has been modified. A new
1715 * allocation is required when modifying a chain.
1717 * Set UPDATE to ensure that the blockref is updated in the parent.
1719 * If MODIFIED is already set determine if we can reuse the assigned
1720 * data block or if we need a new data block.
1722 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1724 * Must set modified bit.
1726 atomic_add_long(&hammer2_count_modified_chains, 1);
1727 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1728 hammer2_pfs_memory_inc(chain->pmp); /* can be NULL */
1732 * We may be able to avoid a copy-on-write if the chain's
1733 * check mode is set to NONE and the chain's current
1734 * modify_tid is beyond the last explicit snapshot tid.
1736 * This implements HAMMER2's overwrite-in-place feature.
1738 * NOTE! This data-block cannot be used as a de-duplication
1739 * source when the check mode is set to NONE.
1741 if ((chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1742 chain->bref.type == HAMMER2_BREF_TYPE_DIRENT) &&
1743 (chain->flags & HAMMER2_CHAIN_INITIAL) == 0 &&
1744 (chain->flags & HAMMER2_CHAIN_DEDUPABLE) == 0 &&
1745 HAMMER2_DEC_CHECK(chain->bref.methods) ==
1746 HAMMER2_CHECK_NONE &&
1748 chain->bref.modify_tid >
1749 chain->pmp->iroot->meta.pfs_lsnap_tid) {
1751 * Sector overwrite allowed.
1754 } else if ((hmp->hflags & HMNT2_EMERG) &&
1756 chain->bref.modify_tid >
1757 chain->pmp->iroot->meta.pfs_lsnap_tid) {
1759 * If in emergency delete mode then do a modify-in-
1760 * place on any chain type belonging to the PFS as
1761 * long as it doesn't mess up a snapshot. We might
1762 * be forced to do this anyway a little further down
1763 * in the code if the allocation fails.
1765 * Also note that in emergency mode, these modify-in-
1766 * place operations are NOT SAFE. A storage failure,
1767 * power failure, or panic can corrupt the filesystem.
1772 * Sector overwrite not allowed, must copy-on-write.
1776 } else if (chain->flags & HAMMER2_CHAIN_DEDUPABLE) {
1778 * If the modified chain was registered for dedup we need
1779 * a new allocation. This only happens for delayed-flush
1780 * chains (i.e. which run through the front-end buffer
1787 * Already flagged modified, no new allocation is needed.
1794 * Flag parent update required.
1796 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
1797 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1804 * The XOP code returns held but unlocked focus chains. This
1805 * prevents the chain from being destroyed but does not prevent
1806 * it from being modified. diolk is used to interlock modifications
1807 * against XOP frontend accesses to the focus.
1809 * This allows us to theoretically avoid deadlocking the frontend
1810 * if one of the backends lock up by not formally locking the
1811 * focused chain in the frontend. In addition, the synchronization
1812 * code relies on this mechanism to avoid deadlocking concurrent
1813 * synchronization threads.
1815 lockmgr(&chain->diolk, LK_EXCLUSIVE);
1818 * The modification or re-modification requires an allocation and
1819 * possible COW. If an error occurs, the previous content and data
1820 * reference is retained and the modification fails.
1822 * If dedup_off is non-zero, the caller is requesting a deduplication
1823 * rather than a modification. The MODIFIED bit is not set and the
1824 * data offset is set to the deduplication offset. The data cannot
1827 * NOTE: The dedup offset is allowed to be in a partially free state
1828 * and we must be sure to reset it to a fully allocated state
1829 * to force two bulkfree passes to free it again.
1831 * NOTE: Only applicable when chain->bytes != 0.
1833 * XXX can a chain already be marked MODIFIED without a data
1834 * assignment? If not, assert here instead of testing the case.
1836 if (chain != &hmp->vchain && chain != &hmp->fchain &&
1838 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1842 * NOTE: We do not have to remove the dedup
1843 * registration because the area is still
1844 * allocated and the underlying DIO will
1848 chain->bref.data_off = dedup_off;
1849 chain->bytes = 1 << (dedup_off &
1850 HAMMER2_OFF_MASK_RADIX);
1852 atomic_clear_int(&chain->flags,
1853 HAMMER2_CHAIN_MODIFIED);
1854 atomic_add_long(&hammer2_count_modified_chains,
1857 hammer2_pfs_memory_wakeup(
1860 hammer2_freemap_adjust(hmp, &chain->bref,
1861 HAMMER2_FREEMAP_DORECOVER);
1862 atomic_set_int(&chain->flags,
1863 HAMMER2_CHAIN_DEDUPABLE);
1865 error = hammer2_freemap_alloc(chain,
1867 atomic_clear_int(&chain->flags,
1868 HAMMER2_CHAIN_DEDUPABLE);
1871 * If we are unable to allocate a new block
1872 * but we are in emergency mode, issue a
1873 * warning to the console and reuse the same
1876 * We behave as if the allocation were
1879 * THIS IS IMPORTANT: These modifications
1880 * are virtually guaranteed to corrupt any
1881 * snapshots related to this filesystem.
1883 if (error && (hmp->hflags & HMNT2_EMERG)) {
1885 chain->bref.flags |=
1886 HAMMER2_BREF_FLAG_EMERG_MIP;
1888 krateprintf(&krate_h2em,
1889 "hammer2: Emergency Mode WARNING: "
1890 "Operation will likely corrupt "
1891 "related snapshot: "
1892 "%016jx.%02x key=%016jx\n",
1893 chain->bref.data_off,
1896 } else if (error == 0) {
1897 chain->bref.flags &=
1898 ~HAMMER2_BREF_FLAG_EMERG_MIP;
1905 * Stop here if error. We have to undo any flag bits we might
1910 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1911 atomic_add_long(&hammer2_count_modified_chains, -1);
1913 hammer2_pfs_memory_wakeup(chain->pmp, -1);
1916 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1918 lockmgr(&chain->diolk, LK_RELEASE);
1924 * Update mirror_tid and modify_tid. modify_tid is only updated
1925 * if not passed as zero (during flushes, parent propagation passes
1928 * NOTE: chain->pmp could be the device spmp.
1930 chain->bref.mirror_tid = hmp->voldata.mirror_tid + 1;
1932 chain->bref.modify_tid = mtid;
1935 * Set BMAPUPD to tell the flush code that an existing blockmap entry
1936 * requires updating as well as to tell the delete code that the
1937 * chain's blockref might not exactly match (in terms of physical size
1938 * or block offset) the one in the parent's blocktable. The base key
1939 * of course will still match.
1941 if (chain->flags & HAMMER2_CHAIN_BMAPPED)
1942 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD);
1945 * Short-cut data block handling when the caller does not need an
1946 * actual data reference to (aka OPTDATA), as long as the chain does
1947 * not already have a data pointer to the data and no de-duplication
1950 * This generally means that the modifications are being done via the
1951 * logical buffer cache.
1953 * NOTE: If deduplication occurred we have to run through the data
1954 * stuff to clear INITIAL, and the caller will likely want to
1955 * assign the check code anyway. Leaving INITIAL set on a
1956 * dedup can be deadly (it can cause the block to be zero'd!).
1958 * This code also handles bytes == 0 (most dirents).
1960 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA &&
1961 (flags & HAMMER2_MODIFY_OPTDATA) &&
1962 chain->data == NULL) {
1963 if (dedup_off == 0) {
1964 KKASSERT(chain->dio == NULL);
1970 * Clearing the INITIAL flag (for indirect blocks) indicates that
1971 * we've processed the uninitialized storage allocation.
1973 * If this flag is already clear we are likely in a copy-on-write
1974 * situation but we have to be sure NOT to bzero the storage if
1975 * no data is present.
1977 * Clearing of NOTTESTED is allowed if the MODIFIED bit is set,
1979 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1980 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1987 * Instantiate data buffer and possibly execute COW operation
1989 switch(chain->bref.type) {
1990 case HAMMER2_BREF_TYPE_VOLUME:
1991 case HAMMER2_BREF_TYPE_FREEMAP:
1993 * The data is embedded, no copy-on-write operation is
1996 KKASSERT(chain->dio == NULL);
1998 case HAMMER2_BREF_TYPE_DIRENT:
2000 * The data might be fully embedded.
2002 if (chain->bytes == 0) {
2003 KKASSERT(chain->dio == NULL);
2007 case HAMMER2_BREF_TYPE_INODE:
2008 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2009 case HAMMER2_BREF_TYPE_DATA:
2010 case HAMMER2_BREF_TYPE_INDIRECT:
2011 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2013 * Perform the copy-on-write operation
2015 * zero-fill or copy-on-write depending on whether
2016 * chain->data exists or not and set the dirty state for
2017 * the new buffer. hammer2_io_new() will handle the
2020 * If a dedup_off was supplied this is an existing block
2021 * and no COW, copy, or further modification is required.
2023 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
2025 if (wasinitial && dedup_off == 0) {
2026 error = hammer2_io_new(hmp, chain->bref.type,
2027 chain->bref.data_off,
2028 chain->bytes, &dio);
2030 error = hammer2_io_bread(hmp, chain->bref.type,
2031 chain->bref.data_off,
2032 chain->bytes, &dio);
2034 hammer2_adjreadcounter(chain->bref.type, chain->bytes);
2037 * If an I/O error occurs make sure callers cannot accidently
2038 * modify the old buffer's contents and corrupt the filesystem.
2040 * NOTE: hammer2_io_data() call issues bkvasync()
2043 kprintf("hammer2_chain_modify: hmp=%p I/O error\n",
2045 chain->error = HAMMER2_ERROR_EIO;
2046 hammer2_io_brelse(&dio);
2047 hammer2_io_brelse(&chain->dio);
2052 bdata = hammer2_io_data(dio, chain->bref.data_off);
2056 * COW (unless a dedup).
2058 KKASSERT(chain->dio != NULL);
2059 if (chain->data != (void *)bdata && dedup_off == 0) {
2060 bcopy(chain->data, bdata, chain->bytes);
2062 } else if (wasinitial == 0 && dedup_off == 0) {
2064 * We have a problem. We were asked to COW but
2065 * we don't have any data to COW with!
2067 panic("hammer2_chain_modify: having a COW %p\n",
2072 * Retire the old buffer, replace with the new. Dirty or
2073 * redirty the new buffer.
2075 * WARNING! The system buffer cache may have already flushed
2076 * the buffer, so we must be sure to [re]dirty it
2077 * for further modification.
2079 * If dedup_off was supplied, the caller is not
2080 * expected to make any further modification to the
2083 * WARNING! hammer2_get_gdata() assumes dio never transitions
2084 * through NULL in order to optimize away unnecessary
2090 if ((tio = chain->dio) != NULL)
2091 hammer2_io_bqrelse(&tio);
2092 chain->data = (void *)bdata;
2095 hammer2_io_setdirty(dio);
2099 panic("hammer2_chain_modify: illegal non-embedded type %d",
2106 * setflush on parent indicating that the parent must recurse down
2107 * to us. Do not call on chain itself which might already have it
2111 hammer2_chain_setflush(chain->parent);
2112 lockmgr(&chain->diolk, LK_RELEASE);
2114 return (chain->error);
2118 * Modify the chain associated with an inode.
2121 hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
2122 hammer2_tid_t mtid, int flags)
2126 hammer2_inode_modify(ip);
2127 error = hammer2_chain_modify(chain, mtid, 0, flags);
2133 * Volume header data locks
2136 hammer2_voldata_lock(hammer2_dev_t *hmp)
2138 lockmgr(&hmp->vollk, LK_EXCLUSIVE);
2142 hammer2_voldata_unlock(hammer2_dev_t *hmp)
2144 lockmgr(&hmp->vollk, LK_RELEASE);
2148 hammer2_voldata_modify(hammer2_dev_t *hmp)
2150 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
2151 atomic_add_long(&hammer2_count_modified_chains, 1);
2152 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
2153 hammer2_pfs_memory_inc(hmp->vchain.pmp);
2158 * This function returns the chain at the nearest key within the specified
2159 * range. The returned chain will be referenced but not locked.
2161 * This function will recurse through chain->rbtree as necessary and will
2162 * return a *key_nextp suitable for iteration. *key_nextp is only set if
2163 * the iteration value is less than the current value of *key_nextp.
2165 * The caller should use (*key_nextp) to calculate the actual range of
2166 * the returned element, which will be (key_beg to *key_nextp - 1), because
2167 * there might be another element which is superior to the returned element
2170 * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
2171 * chains continue to be returned. On EOF (*key_nextp) may overflow since
2172 * it will wind up being (key_end + 1).
2174 * WARNING! Must be called with child's spinlock held. Spinlock remains
2175 * held through the operation.
2177 struct hammer2_chain_find_info {
2178 hammer2_chain_t *best;
2179 hammer2_key_t key_beg;
2180 hammer2_key_t key_end;
2181 hammer2_key_t key_next;
2184 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
2185 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
2189 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
2190 hammer2_key_t key_beg, hammer2_key_t key_end)
2192 struct hammer2_chain_find_info info;
2195 info.key_beg = key_beg;
2196 info.key_end = key_end;
2197 info.key_next = *key_nextp;
2199 RB_SCAN(hammer2_chain_tree, &parent->core.rbtree,
2200 hammer2_chain_find_cmp, hammer2_chain_find_callback,
2202 *key_nextp = info.key_next;
2204 kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
2205 parent, key_beg, key_end, *key_nextp);
2213 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
2215 struct hammer2_chain_find_info *info = data;
2216 hammer2_key_t child_beg;
2217 hammer2_key_t child_end;
2219 child_beg = child->bref.key;
2220 child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
2222 if (child_end < info->key_beg)
2224 if (child_beg > info->key_end)
2231 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
2233 struct hammer2_chain_find_info *info = data;
2234 hammer2_chain_t *best;
2235 hammer2_key_t child_end;
2238 * WARNING! Layerq is scanned forwards, exact matches should keep
2239 * the existing info->best.
2241 if ((best = info->best) == NULL) {
2243 * No previous best. Assign best
2246 } else if (best->bref.key <= info->key_beg &&
2247 child->bref.key <= info->key_beg) {
2252 /*info->best = child;*/
2253 } else if (child->bref.key < best->bref.key) {
2255 * Child has a nearer key and best is not flush with key_beg.
2256 * Set best to child. Truncate key_next to the old best key.
2259 if (info->key_next > best->bref.key || info->key_next == 0)
2260 info->key_next = best->bref.key;
2261 } else if (child->bref.key == best->bref.key) {
2263 * If our current best is flush with the child then this
2264 * is an illegal overlap.
2266 * key_next will automatically be limited to the smaller of
2267 * the two end-points.
2273 * Keep the current best but truncate key_next to the child's
2276 * key_next will also automatically be limited to the smaller
2277 * of the two end-points (probably not necessary for this case
2278 * but we do it anyway).
2280 if (info->key_next > child->bref.key || info->key_next == 0)
2281 info->key_next = child->bref.key;
2285 * Always truncate key_next based on child's end-of-range.
2287 child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
2288 if (child_end && (info->key_next > child_end || info->key_next == 0))
2289 info->key_next = child_end;
2295 * Retrieve the specified chain from a media blockref, creating the
2296 * in-memory chain structure which reflects it. The returned chain is
2297 * held and locked according to (how) (HAMMER2_RESOLVE_*). The caller must
2298 * handle crc-checks and so forth, and should check chain->error before
2299 * assuming that the data is good.
2301 * To handle insertion races pass the INSERT_RACE flag along with the
2302 * generation number of the core. NULL will be returned if the generation
2303 * number changes before we have a chance to insert the chain. Insert
2304 * races can occur because the parent might be held shared.
2306 * Caller must hold the parent locked shared or exclusive since we may
2307 * need the parent's bref array to find our block.
2309 * WARNING! chain->pmp is always set to NULL for any chain representing
2310 * part of the super-root topology.
2313 hammer2_chain_get(hammer2_chain_t *parent, int generation,
2314 hammer2_blockref_t *bref, int how)
2316 hammer2_dev_t *hmp = parent->hmp;
2317 hammer2_chain_t *chain;
2321 * Allocate a chain structure representing the existing media
2322 * entry. Resulting chain has one ref and is not locked.
2324 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
2325 chain = hammer2_chain_alloc(hmp, NULL, bref);
2327 chain = hammer2_chain_alloc(hmp, parent->pmp, bref);
2328 /* ref'd chain returned */
2331 * Flag that the chain is in the parent's blockmap so delete/flush
2332 * knows what to do with it.
2334 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
2337 * chain must be locked to avoid unexpected ripouts
2339 hammer2_chain_lock(chain, how);
2342 * Link the chain into its parent. A spinlock is required to safely
2343 * access the RBTREE, and it is possible to collide with another
2344 * hammer2_chain_get() operation because the caller might only hold
2345 * a shared lock on the parent.
2347 * NOTE: Get races can occur quite often when we distribute
2348 * asynchronous read-aheads across multiple threads.
2350 KKASSERT(parent->refs > 0);
2351 error = hammer2_chain_insert(parent, chain,
2352 HAMMER2_CHAIN_INSERT_SPIN |
2353 HAMMER2_CHAIN_INSERT_RACE,
2356 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
2357 /*kprintf("chain %p get race\n", chain);*/
2358 hammer2_chain_unlock(chain);
2359 hammer2_chain_drop(chain);
2362 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
2366 * Return our new chain referenced but not locked, or NULL if
2373 * Lookup initialization/completion API
2376 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
2378 hammer2_chain_ref(parent);
2379 if (flags & HAMMER2_LOOKUP_SHARED) {
2380 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
2381 HAMMER2_RESOLVE_SHARED);
2383 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2389 hammer2_chain_lookup_done(hammer2_chain_t *parent)
2392 hammer2_chain_unlock(parent);
2393 hammer2_chain_drop(parent);
2398 * Take the locked chain and return a locked parent. The chain remains
2399 * locked on return, but may have to be temporarily unlocked to acquire
2400 * the parent. Because of this, (chain) must be stable and cannot be
2401 * deleted while it was temporarily unlocked (typically means that (chain)
2404 * Pass HAMMER2_RESOLVE_* flags in flags.
2406 * This will work even if the chain is errored, and the caller can check
2407 * parent->error on return if desired since the parent will be locked.
2409 * This function handles the lock order reversal.
2412 hammer2_chain_getparent(hammer2_chain_t *chain, int flags)
2414 hammer2_chain_t *parent;
2417 * Be careful of order, chain must be unlocked before parent
2418 * is locked below to avoid a deadlock. Try it trivially first.
2420 parent = chain->parent;
2422 panic("hammer2_chain_getparent: no parent");
2423 hammer2_chain_ref(parent);
2424 if (hammer2_chain_lock(parent, flags|HAMMER2_RESOLVE_NONBLOCK) == 0)
2428 hammer2_chain_unlock(chain);
2429 hammer2_chain_lock(parent, flags);
2430 hammer2_chain_lock(chain, flags);
2433 * Parent relinking races are quite common. We have to get
2434 * it right or we will blow up the block table.
2436 if (chain->parent == parent)
2438 hammer2_chain_unlock(parent);
2439 hammer2_chain_drop(parent);
2441 parent = chain->parent;
2443 panic("hammer2_chain_getparent: no parent");
2444 hammer2_chain_ref(parent);
2450 * Take the locked chain and return a locked parent. The chain is unlocked
2451 * and dropped. *chainp is set to the returned parent as a convenience.
2452 * Pass HAMMER2_RESOLVE_* flags in flags.
2454 * This will work even if the chain is errored, and the caller can check
2455 * parent->error on return if desired since the parent will be locked.
2457 * The chain does NOT need to be stable. We use a tracking structure
2458 * to track the expected parent if the chain is deleted out from under us.
2460 * This function handles the lock order reversal.
2463 hammer2_chain_repparent(hammer2_chain_t **chainp, int flags)
2465 hammer2_chain_t *chain;
2466 hammer2_chain_t *parent;
2467 struct hammer2_reptrack reptrack;
2468 struct hammer2_reptrack **repp;
2471 * Be careful of order, chain must be unlocked before parent
2472 * is locked below to avoid a deadlock. Try it trivially first.
2475 parent = chain->parent;
2476 if (parent == NULL) {
2477 hammer2_spin_unex(&chain->core.spin);
2478 panic("hammer2_chain_repparent: no parent");
2480 hammer2_chain_ref(parent);
2481 if (hammer2_chain_lock(parent, flags|HAMMER2_RESOLVE_NONBLOCK) == 0) {
2482 hammer2_chain_unlock(chain);
2483 hammer2_chain_drop(chain);
2490 * Ok, now it gets a bit nasty. There are multiple situations where
2491 * the parent might be in the middle of a deletion, or where the child
2492 * (chain) might be deleted the instant we let go of its lock.
2493 * We can potentially end up in a no-win situation!
2495 * In particular, the indirect_maintenance() case can cause these
2498 * To deal with this we install a reptrack structure in the parent
2499 * This reptrack structure 'owns' the parent ref and will automatically
2500 * migrate to the parent's parent if the parent is deleted permanently.
2502 hammer2_spin_init(&reptrack.spin, "h2reptrk");
2503 reptrack.chain = parent;
2504 hammer2_chain_ref(parent); /* for the reptrack */
2506 hammer2_spin_ex(&parent->core.spin);
2507 reptrack.next = parent->core.reptrack;
2508 parent->core.reptrack = &reptrack;
2509 hammer2_spin_unex(&parent->core.spin);
2511 hammer2_chain_unlock(chain);
2512 hammer2_chain_drop(chain);
2513 chain = NULL; /* gone */
2516 * At the top of this loop, chain is gone and parent is refd both
2517 * by us explicitly AND via our reptrack. We are attempting to
2521 hammer2_chain_lock(parent, flags);
2523 if (reptrack.chain == parent)
2525 hammer2_chain_unlock(parent);
2526 hammer2_chain_drop(parent);
2528 kprintf("hammer2: debug REPTRACK %p->%p\n",
2529 parent, reptrack.chain);
2530 hammer2_spin_ex(&reptrack.spin);
2531 parent = reptrack.chain;
2532 hammer2_chain_ref(parent);
2533 hammer2_spin_unex(&reptrack.spin);
2537 * Once parent is locked and matches our reptrack, our reptrack
2538 * will be stable and we have our parent. We can unlink our
2541 * WARNING! Remember that the chain lock might be shared. Chains
2542 * locked shared have stable parent linkages.
2544 hammer2_spin_ex(&parent->core.spin);
2545 repp = &parent->core.reptrack;
2546 while (*repp != &reptrack)
2547 repp = &(*repp)->next;
2548 *repp = reptrack.next;
2549 hammer2_spin_unex(&parent->core.spin);
2551 hammer2_chain_drop(parent); /* reptrack ref */
2552 *chainp = parent; /* return parent lock+ref */
2558 * Dispose of any linked reptrack structures in (chain) by shifting them to
2559 * (parent). Both (chain) and (parent) must be exclusively locked.
2561 * This is interlocked against any children of (chain) on the other side.
2562 * No children so remain as-of when this is called so we can test
2563 * core.reptrack without holding the spin-lock.
2565 * Used whenever the caller intends to permanently delete chains related
2566 * to topological recursions (BREF_TYPE_INDIRECT, BREF_TYPE_FREEMAP_NODE),
2567 * where the chains underneath the node being deleted are given a new parent
2568 * above the node being deleted.
2572 hammer2_chain_repchange(hammer2_chain_t *parent, hammer2_chain_t *chain)
2574 struct hammer2_reptrack *reptrack;
2576 KKASSERT(chain->core.live_count == 0 && RB_EMPTY(&chain->core.rbtree));
2577 while (chain->core.reptrack) {
2578 hammer2_spin_ex(&parent->core.spin);
2579 hammer2_spin_ex(&chain->core.spin);
2580 reptrack = chain->core.reptrack;
2581 if (reptrack == NULL) {
2582 hammer2_spin_unex(&chain->core.spin);
2583 hammer2_spin_unex(&parent->core.spin);
2586 hammer2_spin_ex(&reptrack->spin);
2587 chain->core.reptrack = reptrack->next;
2588 reptrack->chain = parent;
2589 reptrack->next = parent->core.reptrack;
2590 parent->core.reptrack = reptrack;
2591 hammer2_chain_ref(parent); /* reptrack */
2593 hammer2_spin_unex(&chain->core.spin);
2594 hammer2_spin_unex(&parent->core.spin);
2595 kprintf("hammer2: debug repchange %p %p->%p\n",
2596 reptrack, chain, parent);
2597 hammer2_chain_drop(chain); /* reptrack */
2602 * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
2603 * (*parentp) typically points to an inode but can also point to a related
2604 * indirect block and this function will recurse upwards and find the inode
2605 * or the nearest undeleted indirect block covering the key range.
2607 * This function unconditionally sets *errorp, replacing any previous value.
2609 * (*parentp) must be exclusive or shared locked (depending on flags) and
2610 * referenced and can be an inode or an existing indirect block within the
2613 * If (*parent) is errored out, this function will not attempt to recurse
2614 * the radix tree and will return NULL along with an appropriate *errorp.
2615 * If NULL is returned and *errorp is 0, the requested lookup could not be
2618 * On return (*parentp) will be modified to point at the deepest parent chain
2619 * element encountered during the search, as a helper for an insertion or
2622 * The new (*parentp) will be locked shared or exclusive (depending on flags),
2623 * and referenced, and the old will be unlocked and dereferenced (no change
2624 * if they are both the same). This is particularly important if the caller
2625 * wishes to insert a new chain, (*parentp) will be set properly even if NULL
2626 * is returned, as long as no error occurred.
2628 * The matching chain will be returned locked according to flags.
2632 * NULL is returned if no match was found, but (*parentp) will still
2633 * potentially be adjusted.
2635 * On return (*key_nextp) will point to an iterative value for key_beg.
2636 * (If NULL is returned (*key_nextp) is set to (key_end + 1)).
2638 * This function will also recurse up the chain if the key is not within the
2639 * current parent's range. (*parentp) can never be set to NULL. An iteration
2640 * can simply allow (*parentp) to float inside the loop.
2642 * NOTE! chain->data is not always resolved. By default it will not be
2643 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
2644 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
2645 * BREF_TYPE_DATA as the device buffer can alias the logical file
2650 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
2651 hammer2_key_t key_beg, hammer2_key_t key_end,
2652 int *errorp, int flags)
2655 hammer2_chain_t *parent;
2656 hammer2_chain_t *chain;
2657 hammer2_blockref_t *base;
2658 hammer2_blockref_t *bref;
2659 hammer2_blockref_t bsave;
2660 hammer2_key_t scan_beg;
2661 hammer2_key_t scan_end;
2663 int how_always = HAMMER2_RESOLVE_ALWAYS;
2664 int how_maybe = HAMMER2_RESOLVE_MAYBE;
2667 int maxloops = 300000;
2668 volatile hammer2_mtx_t save_mtx;
2670 if (flags & HAMMER2_LOOKUP_ALWAYS) {
2671 how_maybe = how_always;
2672 how = HAMMER2_RESOLVE_ALWAYS;
2673 } else if (flags & HAMMER2_LOOKUP_NODATA) {
2674 how = HAMMER2_RESOLVE_NEVER;
2676 how = HAMMER2_RESOLVE_MAYBE;
2678 if (flags & HAMMER2_LOOKUP_SHARED) {
2679 how_maybe |= HAMMER2_RESOLVE_SHARED;
2680 how_always |= HAMMER2_RESOLVE_SHARED;
2681 how |= HAMMER2_RESOLVE_SHARED;
2685 * Recurse (*parentp) upward if necessary until the parent completely
2686 * encloses the key range or we hit the inode.
2688 * Handle races against the flusher deleting indirect nodes on its
2689 * way back up by continuing to recurse upward past the deletion.
2695 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2696 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2697 scan_beg = parent->bref.key;
2698 scan_end = scan_beg +
2699 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2700 if ((parent->flags & HAMMER2_CHAIN_DELETED) == 0) {
2701 if (key_beg >= scan_beg && key_end <= scan_end)
2704 parent = hammer2_chain_repparent(parentp, how_maybe);
2707 if (--maxloops == 0)
2708 panic("hammer2_chain_lookup: maxloops");
2711 * MATCHIND case that does not require parent->data (do prior to
2712 * parent->error check).
2714 switch(parent->bref.type) {
2715 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2716 case HAMMER2_BREF_TYPE_INDIRECT:
2717 if (flags & HAMMER2_LOOKUP_MATCHIND) {
2718 scan_beg = parent->bref.key;
2719 scan_end = scan_beg +
2720 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2721 if (key_beg == scan_beg && key_end == scan_end) {
2723 hammer2_chain_ref(chain);
2724 hammer2_chain_lock(chain, how_maybe);
2725 *key_nextp = scan_end + 1;
2735 * No lookup is possible if the parent is errored. We delayed
2736 * this check as long as we could to ensure that the parent backup,
2737 * embedded data, and MATCHIND code could still execute.
2739 if (parent->error) {
2740 *errorp = parent->error;
2745 * Locate the blockref array. Currently we do a fully associative
2746 * search through the array.
2748 switch(parent->bref.type) {
2749 case HAMMER2_BREF_TYPE_INODE:
2751 * Special shortcut for embedded data returns the inode
2752 * itself. Callers must detect this condition and access
2753 * the embedded data (the strategy code does this for us).
2755 * This is only applicable to regular files and softlinks.
2757 * We need a second lock on parent. Since we already have
2758 * a lock we must pass LOCKAGAIN to prevent unexpected
2759 * blocking (we don't want to block on a second shared
2760 * ref if an exclusive lock is pending)
2762 if (parent->data->ipdata.meta.op_flags &
2763 HAMMER2_OPFLAG_DIRECTDATA) {
2764 if (flags & HAMMER2_LOOKUP_NODIRECT) {
2766 *key_nextp = key_end + 1;
2769 hammer2_chain_ref(parent);
2770 hammer2_chain_lock(parent, how_always |
2771 HAMMER2_RESOLVE_LOCKAGAIN);
2772 *key_nextp = key_end + 1;
2775 base = &parent->data->ipdata.u.blockset.blockref[0];
2776 count = HAMMER2_SET_COUNT;
2778 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2779 case HAMMER2_BREF_TYPE_INDIRECT:
2781 * Optimize indirect blocks in the INITIAL state to avoid
2784 * Debugging: Enter permanent wait state instead of
2785 * panicing on unexpectedly NULL data for the moment.
2787 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2790 if (parent->data == NULL) {
2791 kprintf("hammer2: unexpected NULL data "
2794 tsleep(parent, 0, "xxx", 0);
2796 base = &parent->data->npdata[0];
2798 count = parent->bytes / sizeof(hammer2_blockref_t);
2800 case HAMMER2_BREF_TYPE_VOLUME:
2801 base = &parent->data->voldata.sroot_blockset.blockref[0];
2802 count = HAMMER2_SET_COUNT;
2804 case HAMMER2_BREF_TYPE_FREEMAP:
2805 base = &parent->data->blkset.blockref[0];
2806 count = HAMMER2_SET_COUNT;
2809 kprintf("hammer2_chain_lookup: unrecognized "
2810 "blockref(B) type: %d",
2813 tsleep(&base, 0, "dead", 0);
2814 panic("hammer2_chain_lookup: unrecognized "
2815 "blockref(B) type: %d",
2817 base = NULL; /* safety */
2818 count = 0; /* safety */
2822 * Merged scan to find next candidate.
2824 * hammer2_base_*() functions require the parent->core.live_* fields
2825 * to be synchronized.
2827 * We need to hold the spinlock to access the block array and RB tree
2828 * and to interlock chain creation.
2830 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2831 hammer2_chain_countbrefs(parent, base, count);
2836 hammer2_spin_ex(&parent->core.spin);
2837 chain = hammer2_combined_find(parent, base, count,
2841 generation = parent->core.generation;
2844 * Exhausted parent chain, iterate.
2847 KKASSERT(chain == NULL);
2848 hammer2_spin_unex(&parent->core.spin);
2849 if (key_beg == key_end) /* short cut single-key case */
2853 * Stop if we reached the end of the iteration.
2855 if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
2856 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2861 * Calculate next key, stop if we reached the end of the
2862 * iteration, otherwise go up one level and loop.
2864 key_beg = parent->bref.key +
2865 ((hammer2_key_t)1 << parent->bref.keybits);
2866 if (key_beg == 0 || key_beg > key_end)
2868 parent = hammer2_chain_repparent(parentp, how_maybe);
2873 * Selected from blockref or in-memory chain.
2876 if (chain == NULL) {
2877 hammer2_spin_unex(&parent->core.spin);
2878 if (bsave.type == HAMMER2_BREF_TYPE_INDIRECT ||
2879 bsave.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2880 chain = hammer2_chain_get(parent, generation,
2883 chain = hammer2_chain_get(parent, generation,
2889 hammer2_chain_ref(chain);
2890 hammer2_spin_unex(&parent->core.spin);
2893 * chain is referenced but not locked. We must lock the
2894 * chain to obtain definitive state.
2896 if (bsave.type == HAMMER2_BREF_TYPE_INDIRECT ||
2897 bsave.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2898 hammer2_chain_lock(chain, how_maybe);
2900 hammer2_chain_lock(chain, how);
2902 KKASSERT(chain->parent == parent);
2904 if (bcmp(&bsave, &chain->bref, sizeof(bsave)) ||
2905 chain->parent != parent) {
2906 hammer2_chain_unlock(chain);
2907 hammer2_chain_drop(chain);
2908 chain = NULL; /* SAFETY */
2914 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2916 * NOTE: Chain's key range is not relevant as there might be
2917 * one-offs within the range that are not deleted.
2919 * NOTE: Lookups can race delete-duplicate because
2920 * delete-duplicate does not lock the parent's core
2921 * (they just use the spinlock on the core).
2923 if (chain->flags & HAMMER2_CHAIN_DELETED) {
2924 kprintf("skip deleted chain %016jx.%02x key=%016jx\n",
2925 chain->bref.data_off, chain->bref.type,
2927 hammer2_chain_unlock(chain);
2928 hammer2_chain_drop(chain);
2929 chain = NULL; /* SAFETY */
2930 key_beg = *key_nextp;
2931 if (key_beg == 0 || key_beg > key_end)
2937 * If the chain element is an indirect block it becomes the new
2938 * parent and we loop on it. We must maintain our top-down locks
2939 * to prevent the flusher from interfering (i.e. doing a
2940 * delete-duplicate and leaving us recursing down a deleted chain).
2942 * The parent always has to be locked with at least RESOLVE_MAYBE
2943 * so we can access its data. It might need a fixup if the caller
2944 * passed incompatible flags. Be careful not to cause a deadlock
2945 * as a data-load requires an exclusive lock.
2947 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
2948 * range is within the requested key range we return the indirect
2949 * block and do NOT loop. This is usually only used to acquire
2952 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2953 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2954 save_mtx = parent->lock;
2955 hammer2_chain_unlock(parent);
2956 hammer2_chain_drop(parent);
2957 *parentp = parent = chain;
2958 chain = NULL; /* SAFETY */
2963 * All done, return the locked chain.
2965 * If the caller does not want a locked chain, replace the lock with
2966 * a ref. Perhaps this can eventually be optimized to not obtain the
2967 * lock in the first place for situations where the data does not
2968 * need to be resolved.
2970 * NOTE! A chain->error must be tested by the caller upon return.
2971 * *errorp is only set based on issues which occur while
2972 * trying to reach the chain.
2978 * After having issued a lookup we can iterate all matching keys.
2980 * If chain is non-NULL we continue the iteration from just after it's index.
2982 * If chain is NULL we assume the parent was exhausted and continue the
2983 * iteration at the next parent.
2985 * If a fatal error occurs (typically an I/O error), a dummy chain is
2986 * returned with chain->error and error-identifying information set. This
2987 * chain will assert if you try to do anything fancy with it.
2989 * XXX Depending on where the error occurs we should allow continued iteration.
2991 * parent must be locked on entry and remains locked throughout. chain's
2992 * lock status must match flags. Chain is always at least referenced.
2994 * WARNING! The MATCHIND flag does not apply to this function.
2997 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
2998 hammer2_key_t *key_nextp,
2999 hammer2_key_t key_beg, hammer2_key_t key_end,
3000 int *errorp, int flags)
3002 hammer2_chain_t *parent;
3006 * Calculate locking flags for upward recursion.
3008 how_maybe = HAMMER2_RESOLVE_MAYBE;
3009 if (flags & HAMMER2_LOOKUP_SHARED)
3010 how_maybe |= HAMMER2_RESOLVE_SHARED;
3016 * Calculate the next index and recalculate the parent if necessary.
3019 key_beg = chain->bref.key +
3020 ((hammer2_key_t)1 << chain->bref.keybits);
3021 hammer2_chain_unlock(chain);
3022 hammer2_chain_drop(chain);
3025 * chain invalid past this point, but we can still do a
3026 * pointer comparison w/parent.
3028 * Any scan where the lookup returned degenerate data embedded
3029 * in the inode has an invalid index and must terminate.
3031 if (chain == parent)
3033 if (key_beg == 0 || key_beg > key_end)
3036 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
3037 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
3039 * We reached the end of the iteration.
3044 * Continue iteration with next parent unless the current
3045 * parent covers the range.
3047 * (This also handles the case of a deleted, empty indirect
3050 key_beg = parent->bref.key +
3051 ((hammer2_key_t)1 << parent->bref.keybits);
3052 if (key_beg == 0 || key_beg > key_end)
3054 parent = hammer2_chain_repparent(parentp, how_maybe);
3060 return (hammer2_chain_lookup(parentp, key_nextp,
3066 * Caller wishes to iterate chains under parent, loading new chains into
3067 * chainp. Caller must initialize *chainp to NULL and *firstp to 1, and
3068 * then call hammer2_chain_scan() repeatedly until a non-zero return.
3069 * During the scan, *firstp will be set to 0 and (*chainp) will be replaced
3070 * with the returned chain for the scan. The returned *chainp will be
3071 * locked and referenced. Any prior contents will be unlocked and dropped.
3073 * Caller should check the return value. A normal scan EOF will return
3074 * exactly HAMMER2_ERROR_EOF. Any other non-zero value indicates an
3075 * error trying to access parent data. Any error in the returned chain
3076 * must be tested separately by the caller.
3078 * (*chainp) is dropped on each scan, but will only be set if the returned
3079 * element itself can recurse. Leaf elements are NOT resolved, loaded, or
3080 * returned via *chainp. The caller will get their bref only.
3082 * The raw scan function is similar to lookup/next but does not seek to a key.
3083 * Blockrefs are iterated via first_bref = (parent, NULL) and
3084 * next_chain = (parent, bref).
3086 * The passed-in parent must be locked and its data resolved. The function
3087 * nominally returns a locked and referenced *chainp != NULL for chains
3088 * the caller might need to recurse on (and will dipose of any *chainp passed
3089 * in). The caller must check the chain->bref.type either way.
3092 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t **chainp,
3093 hammer2_blockref_t *bref, int *firstp,
3097 hammer2_blockref_t *base;
3098 hammer2_blockref_t *bref_ptr;
3100 hammer2_key_t next_key;
3101 hammer2_chain_t *chain = NULL;
3103 int how_always = HAMMER2_RESOLVE_ALWAYS;
3104 int how_maybe = HAMMER2_RESOLVE_MAYBE;
3107 int maxloops = 300000;
3114 * Scan flags borrowed from lookup.
3116 if (flags & HAMMER2_LOOKUP_ALWAYS) {
3117 how_maybe = how_always;
3118 how = HAMMER2_RESOLVE_ALWAYS;
3119 } else if (flags & HAMMER2_LOOKUP_NODATA) {
3120 how = HAMMER2_RESOLVE_NEVER;
3122 how = HAMMER2_RESOLVE_MAYBE;
3124 if (flags & HAMMER2_LOOKUP_SHARED) {
3125 how_maybe |= HAMMER2_RESOLVE_SHARED;
3126 how_always |= HAMMER2_RESOLVE_SHARED;
3127 how |= HAMMER2_RESOLVE_SHARED;
3131 * Calculate key to locate first/next element, unlocking the previous
3132 * element as we go. Be careful, the key calculation can overflow.
3134 * (also reset bref to NULL)
3140 key = bref->key + ((hammer2_key_t)1 << bref->keybits);
3141 if ((chain = *chainp) != NULL) {
3143 hammer2_chain_unlock(chain);
3144 hammer2_chain_drop(chain);
3148 error |= HAMMER2_ERROR_EOF;
3154 if (parent->error) {
3155 error = parent->error;
3158 if (--maxloops == 0)
3159 panic("hammer2_chain_scan: maxloops");
3162 * Locate the blockref array. Currently we do a fully associative
3163 * search through the array.
3165 switch(parent->bref.type) {
3166 case HAMMER2_BREF_TYPE_INODE:
3168 * An inode with embedded data has no sub-chains.
3170 * WARNING! Bulk scan code may pass a static chain marked
3171 * as BREF_TYPE_INODE with a copy of the volume
3172 * root blockset to snapshot the volume.
3174 if (parent->data->ipdata.meta.op_flags &
3175 HAMMER2_OPFLAG_DIRECTDATA) {
3176 error |= HAMMER2_ERROR_EOF;
3179 base = &parent->data->ipdata.u.blockset.blockref[0];
3180 count = HAMMER2_SET_COUNT;
3182 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3183 case HAMMER2_BREF_TYPE_INDIRECT:
3185 * Optimize indirect blocks in the INITIAL state to avoid
3188 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
3191 if (parent->data == NULL)
3192 panic("parent->data is NULL");
3193 base = &parent->data->npdata[0];
3195 count = parent->bytes / sizeof(hammer2_blockref_t);
3197 case HAMMER2_BREF_TYPE_VOLUME:
3198 base = &parent->data->voldata.sroot_blockset.blockref[0];
3199 count = HAMMER2_SET_COUNT;
3201 case HAMMER2_BREF_TYPE_FREEMAP:
3202 base = &parent->data->blkset.blockref[0];
3203 count = HAMMER2_SET_COUNT;
3206 panic("hammer2_chain_scan: unrecognized blockref type: %d",
3208 base = NULL; /* safety */
3209 count = 0; /* safety */
3213 * Merged scan to find next candidate.
3215 * hammer2_base_*() functions require the parent->core.live_* fields
3216 * to be synchronized.
3218 * We need to hold the spinlock to access the block array and RB tree
3219 * and to interlock chain creation.
3221 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
3222 hammer2_chain_countbrefs(parent, base, count);
3226 hammer2_spin_ex(&parent->core.spin);
3227 chain = hammer2_combined_find(parent, base, count,
3229 key, HAMMER2_KEY_MAX,
3231 generation = parent->core.generation;
3234 * Exhausted parent chain, we're done.
3236 if (bref_ptr == NULL) {
3237 hammer2_spin_unex(&parent->core.spin);
3238 KKASSERT(chain == NULL);
3239 error |= HAMMER2_ERROR_EOF;
3244 * Copy into the supplied stack-based blockref.
3249 * Selected from blockref or in-memory chain.
3251 if (chain == NULL) {
3252 switch(bref->type) {
3253 case HAMMER2_BREF_TYPE_INODE:
3254 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3255 case HAMMER2_BREF_TYPE_INDIRECT:
3256 case HAMMER2_BREF_TYPE_VOLUME:
3257 case HAMMER2_BREF_TYPE_FREEMAP:
3259 * Recursion, always get the chain
3261 hammer2_spin_unex(&parent->core.spin);
3262 chain = hammer2_chain_get(parent, generation,
3269 * No recursion, do not waste time instantiating
3270 * a chain, just iterate using the bref.
3272 hammer2_spin_unex(&parent->core.spin);
3277 * Recursion or not we need the chain in order to supply
3280 hammer2_chain_ref(chain);
3281 hammer2_spin_unex(&parent->core.spin);
3282 hammer2_chain_lock(chain, how);
3285 (bcmp(bref, &chain->bref, sizeof(*bref)) ||
3286 chain->parent != parent)) {
3287 hammer2_chain_unlock(chain);
3288 hammer2_chain_drop(chain);
3294 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
3296 * NOTE: chain's key range is not relevant as there might be
3297 * one-offs within the range that are not deleted.
3299 * NOTE: XXX this could create problems with scans used in
3300 * situations other than mount-time recovery.
3302 * NOTE: Lookups can race delete-duplicate because
3303 * delete-duplicate does not lock the parent's core
3304 * (they just use the spinlock on the core).
3306 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3307 hammer2_chain_unlock(chain);
3308 hammer2_chain_drop(chain);
3313 error |= HAMMER2_ERROR_EOF;
3321 * All done, return the bref or NULL, supply chain if necessary.
3329 * Create and return a new hammer2 system memory structure of the specified
3330 * key, type and size and insert it under (*parentp). This is a full
3331 * insertion, based on the supplied key/keybits, and may involve creating
3332 * indirect blocks and moving other chains around via delete/duplicate.
3334 * This call can be made with parent == NULL as long as a non -1 methods
3335 * is supplied. hmp must also be supplied in this situation (otherwise
3336 * hmp is extracted from the supplied parent). The chain will be detached
3337 * from the topology. A later call with both parent and chain can be made
3340 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION
3341 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
3342 * FULL. This typically means that the caller is creating the chain after
3343 * doing a hammer2_chain_lookup().
3345 * (*parentp) must be exclusive locked and may be replaced on return
3346 * depending on how much work the function had to do.
3348 * (*parentp) must not be errored or this function will assert.
3350 * (*chainp) usually starts out NULL and returns the newly created chain,
3351 * but if the caller desires the caller may allocate a disconnected chain
3352 * and pass it in instead.
3354 * This function should NOT be used to insert INDIRECT blocks. It is
3355 * typically used to create/insert inodes and data blocks.
3357 * Caller must pass-in an exclusively locked parent the new chain is to
3358 * be inserted under, and optionally pass-in a disconnected, exclusively
3359 * locked chain to insert (else we create a new chain). The function will
3360 * adjust (*parentp) as necessary, create or connect the chain, and
3361 * return an exclusively locked chain in *chainp.
3363 * When creating a PFSROOT inode under the super-root, pmp is typically NULL
3364 * and will be reassigned.
3366 * NOTE: returns HAMMER_ERROR_* flags
3369 hammer2_chain_create(hammer2_chain_t **parentp, hammer2_chain_t **chainp,
3370 hammer2_dev_t *hmp, hammer2_pfs_t *pmp, int methods,
3371 hammer2_key_t key, int keybits, int type, size_t bytes,
3372 hammer2_tid_t mtid, hammer2_off_t dedup_off, int flags)
3374 hammer2_chain_t *chain;
3375 hammer2_chain_t *parent;
3376 hammer2_blockref_t *base;
3377 hammer2_blockref_t dummy;
3381 int maxloops = 300000;
3384 * Topology may be crossing a PFS boundary.
3388 KKASSERT(hammer2_mtx_owned(&parent->lock));
3389 KKASSERT(parent->error == 0);
3394 if (chain == NULL) {
3396 * First allocate media space and construct the dummy bref,
3397 * then allocate the in-memory chain structure. Set the
3398 * INITIAL flag for fresh chains which do not have embedded
3401 * XXX for now set the check mode of the child based on
3402 * the parent or, if the parent is an inode, the
3403 * specification in the inode.
3405 bzero(&dummy, sizeof(dummy));
3408 dummy.keybits = keybits;
3409 dummy.data_off = hammer2_getradix(bytes);
3412 * Inherit methods from parent by default. Primarily used
3413 * for BREF_TYPE_DATA. Non-data types *must* be set to
3414 * a non-NONE check algorithm.
3417 dummy.methods = parent->bref.methods;
3419 dummy.methods = (uint8_t)methods;
3421 if (type != HAMMER2_BREF_TYPE_DATA &&
3422 HAMMER2_DEC_CHECK(dummy.methods) == HAMMER2_CHECK_NONE) {
3424 HAMMER2_ENC_CHECK(HAMMER2_CHECK_DEFAULT);
3427 chain = hammer2_chain_alloc(hmp, pmp, &dummy);
3430 * Lock the chain manually, chain_lock will load the chain
3431 * which we do NOT want to do. (note: chain->refs is set
3432 * to 1 by chain_alloc() for us, but lockcnt is not).
3435 hammer2_mtx_ex(&chain->lock);
3439 * Set INITIAL to optimize I/O. The flag will generally be
3440 * processed when we call hammer2_chain_modify().
3442 * Recalculate bytes to reflect the actual media block
3443 * allocation. Handle special case radix 0 == 0 bytes.
3445 bytes = (size_t)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
3447 bytes = (hammer2_off_t)1 << bytes;
3448 chain->bytes = bytes;
3451 case HAMMER2_BREF_TYPE_VOLUME:
3452 case HAMMER2_BREF_TYPE_FREEMAP:
3453 panic("hammer2_chain_create: called with volume type");
3455 case HAMMER2_BREF_TYPE_INDIRECT:
3456 panic("hammer2_chain_create: cannot be used to"
3457 "create indirect block");
3459 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3460 panic("hammer2_chain_create: cannot be used to"
3461 "create freemap root or node");
3463 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3464 KKASSERT(bytes == sizeof(chain->data->bmdata));
3466 case HAMMER2_BREF_TYPE_DIRENT:
3467 case HAMMER2_BREF_TYPE_INODE:
3468 case HAMMER2_BREF_TYPE_DATA:
3471 * leave chain->data NULL, set INITIAL
3473 KKASSERT(chain->data == NULL);
3474 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
3479 * We are reattaching a previously deleted chain, possibly
3480 * under a new parent and possibly with a new key/keybits.
3481 * The chain does not have to be in a modified state. The
3482 * UPDATE flag will be set later on in this routine.
3484 * Do NOT mess with the current state of the INITIAL flag.
3486 chain->bref.key = key;
3487 chain->bref.keybits = keybits;
3488 if (chain->flags & HAMMER2_CHAIN_DELETED)
3489 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3490 KKASSERT(chain->parent == NULL);
3494 * Set the appropriate bref flag if requested.
3496 * NOTE! Callers can call this function to move chains without
3497 * knowing about special flags, so don't clear bref flags
3500 if (flags & HAMMER2_INSERT_PFSROOT)
3501 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
3507 * Calculate how many entries we have in the blockref array and
3508 * determine if an indirect block is required when inserting into
3512 if (--maxloops == 0)
3513 panic("hammer2_chain_create: maxloops");
3515 switch(parent->bref.type) {
3516 case HAMMER2_BREF_TYPE_INODE:
3517 if ((parent->data->ipdata.meta.op_flags &
3518 HAMMER2_OPFLAG_DIRECTDATA) != 0) {
3519 kprintf("hammer2: parent set for direct-data! "
3520 "pkey=%016jx ckey=%016jx\n",
3524 KKASSERT((parent->data->ipdata.meta.op_flags &
3525 HAMMER2_OPFLAG_DIRECTDATA) == 0);
3526 KKASSERT(parent->data != NULL);
3527 base = &parent->data->ipdata.u.blockset.blockref[0];
3528 count = HAMMER2_SET_COUNT;
3530 case HAMMER2_BREF_TYPE_INDIRECT:
3531 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3532 if (parent->flags & HAMMER2_CHAIN_INITIAL)
3535 base = &parent->data->npdata[0];
3536 count = parent->bytes / sizeof(hammer2_blockref_t);
3538 case HAMMER2_BREF_TYPE_VOLUME:
3539 KKASSERT(parent->data != NULL);
3540 base = &parent->data->voldata.sroot_blockset.blockref[0];
3541 count = HAMMER2_SET_COUNT;
3543 case HAMMER2_BREF_TYPE_FREEMAP:
3544 KKASSERT(parent->data != NULL);
3545 base = &parent->data->blkset.blockref[0];
3546 count = HAMMER2_SET_COUNT;
3549 panic("hammer2_chain_create: unrecognized blockref type: %d",
3557 * Make sure we've counted the brefs
3559 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
3560 hammer2_chain_countbrefs(parent, base, count);
3562 KASSERT(parent->core.live_count >= 0 &&
3563 parent->core.live_count <= count,
3564 ("bad live_count %d/%d (%02x, %d)",
3565 parent->core.live_count, count,
3566 parent->bref.type, parent->bytes));
3569 * If no free blockref could be found we must create an indirect
3570 * block and move a number of blockrefs into it. With the parent
3571 * locked we can safely lock each child in order to delete+duplicate
3572 * it without causing a deadlock.
3574 * This may return the new indirect block or the old parent depending
3575 * on where the key falls. NULL is returned on error.
3577 if (parent->core.live_count == count) {
3578 hammer2_chain_t *nparent;
3580 KKASSERT((flags & HAMMER2_INSERT_SAMEPARENT) == 0);
3582 nparent = hammer2_chain_create_indirect(parent, key, keybits,
3583 mtid, type, &error);
3584 if (nparent == NULL) {
3586 hammer2_chain_drop(chain);
3590 if (parent != nparent) {
3591 hammer2_chain_unlock(parent);
3592 hammer2_chain_drop(parent);
3593 parent = *parentp = nparent;
3599 * fall through if parent, or skip to here if no parent.
3602 if (chain->flags & HAMMER2_CHAIN_DELETED)
3603 kprintf("Inserting deleted chain @%016jx\n",
3607 * Link the chain into its parent.
3609 if (chain->parent != NULL)
3610 panic("hammer2: hammer2_chain_create: chain already connected");
3611 KKASSERT(chain->parent == NULL);
3613 KKASSERT(parent->core.live_count < count);
3614 hammer2_chain_insert(parent, chain,
3615 HAMMER2_CHAIN_INSERT_SPIN |
3616 HAMMER2_CHAIN_INSERT_LIVE,
3622 * Mark the newly created chain modified. This will cause
3623 * UPDATE to be set and process the INITIAL flag.
3625 * Device buffers are not instantiated for DATA elements
3626 * as these are handled by logical buffers.
3628 * Indirect and freemap node indirect blocks are handled
3629 * by hammer2_chain_create_indirect() and not by this
3632 * Data for all other bref types is expected to be
3633 * instantiated (INODE, LEAF).
3635 switch(chain->bref.type) {
3636 case HAMMER2_BREF_TYPE_DATA:
3637 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3638 case HAMMER2_BREF_TYPE_DIRENT:
3639 case HAMMER2_BREF_TYPE_INODE:
3640 error = hammer2_chain_modify(chain, mtid, dedup_off,
3641 HAMMER2_MODIFY_OPTDATA);
3645 * Remaining types are not supported by this function.
3646 * In particular, INDIRECT and LEAF_NODE types are
3647 * handled by create_indirect().
3649 panic("hammer2_chain_create: bad type: %d",
3656 * When reconnecting a chain we must set UPDATE and
3657 * setflush so the flush recognizes that it must update
3658 * the bref in the parent.
3660 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0)
3661 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
3665 * We must setflush(parent) to ensure that it recurses through to
3666 * chain. setflush(chain) might not work because ONFLUSH is possibly
3667 * already set in the chain (so it won't recurse up to set it in the
3671 hammer2_chain_setflush(parent);
3680 * Move the chain from its old parent to a new parent. The chain must have
3681 * already been deleted or already disconnected (or never associated) with
3682 * a parent. The chain is reassociated with the new parent and the deleted
3683 * flag will be cleared (no longer deleted). The chain's modification state
3686 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION
3687 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
3688 * FULL. This typically means that the caller is creating the chain after
3689 * doing a hammer2_chain_lookup().
3691 * Neither (parent) or (chain) can be errored.
3693 * If (parent) is non-NULL then the chain is inserted under the parent.
3695 * If (parent) is NULL then the newly duplicated chain is not inserted
3696 * anywhere, similar to if it had just been chain_alloc()'d (suitable for
3697 * passing into hammer2_chain_create() after this function returns).
3699 * WARNING! This function calls create which means it can insert indirect
3700 * blocks. This can cause other unrelated chains in the parent to
3701 * be moved to a newly inserted indirect block in addition to the
3705 hammer2_chain_rename(hammer2_chain_t **parentp, hammer2_chain_t *chain,
3706 hammer2_tid_t mtid, int flags)
3708 hammer2_blockref_t *bref;
3710 hammer2_chain_t *parent;
3714 * WARNING! We should never resolve DATA to device buffers
3715 * (XXX allow it if the caller did?), and since
3716 * we currently do not have the logical buffer cache
3717 * buffer in-hand to fix its cached physical offset
3718 * we also force the modify code to not COW it. XXX
3720 * NOTE! We allow error'd chains to be renamed. The bref itself
3721 * is good and can be renamed. The content, however, may
3725 KKASSERT(chain->parent == NULL);
3726 /*KKASSERT(chain->error == 0); allow */
3729 * Now create a duplicate of the chain structure, associating
3730 * it with the same core, making it the same size, pointing it
3731 * to the same bref (the same media block).
3733 * NOTE: Handle special radix == 0 case (means 0 bytes).
3735 bref = &chain->bref;
3736 bytes = (size_t)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
3738 bytes = (hammer2_off_t)1 << bytes;
3741 * If parent is not NULL the duplicated chain will be entered under
3742 * the parent and the UPDATE bit set to tell flush to update
3745 * We must setflush(parent) to ensure that it recurses through to
3746 * chain. setflush(chain) might not work because ONFLUSH is possibly
3747 * already set in the chain (so it won't recurse up to set it in the
3750 * Having both chains locked is extremely important for atomicy.
3752 if (parentp && (parent = *parentp) != NULL) {
3753 KKASSERT(hammer2_mtx_owned(&parent->lock));
3754 KKASSERT(parent->refs > 0);
3755 KKASSERT(parent->error == 0);
3757 hammer2_chain_create(parentp, &chain, NULL, chain->pmp,
3758 HAMMER2_METH_DEFAULT,
3759 bref->key, bref->keybits, bref->type,
3760 chain->bytes, mtid, 0, flags);
3761 KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE);
3762 hammer2_chain_setflush(*parentp);
3767 * This works in tandem with delete_obref() to install a blockref in
3768 * (typically) an indirect block that is associated with the chain being
3769 * moved to *parentp.
3771 * The reason we need this function is that the caller needs to maintain
3772 * the blockref as it was, and not generate a new blockref for what might
3773 * be a modified chain. Otherwise stuff will leak into the flush that
3774 * the flush code's FLUSH_INODE_STOP flag is unable to catch.
3776 * It is EXTREMELY important that we properly set CHAIN_BMAPUPD and
3777 * CHAIN_UPDATE. We must set BMAPUPD if the bref does not match, and
3778 * we must clear CHAIN_UPDATE (that was likely set by the chain_rename) if
3779 * it does. Otherwise we can end up in a situation where H2 is unable to
3780 * clean up the in-memory chain topology.
3782 * The reason for this is that flushes do not generally flush through
3783 * BREF_TYPE_INODE chains and depend on a hammer2_inode_t queued to syncq
3784 * or sideq to properly flush and dispose of the related inode chain's flags.
3785 * Situations where the inode is not actually modified by the frontend,
3786 * but where we have to move the related chains around as we insert or cleanup
3787 * indirect blocks, can leave us with a 'dirty' (non-disposable) in-memory
3788 * inode chain that does not have a hammer2_inode_t associated with it.
3791 hammer2_chain_rename_obref(hammer2_chain_t **parentp, hammer2_chain_t *chain,
3792 hammer2_tid_t mtid, int flags,
3793 hammer2_blockref_t *obref)
3795 hammer2_chain_rename(parentp, chain, mtid, flags);
3797 if (obref->type != HAMMER2_BREF_TYPE_EMPTY) {
3798 hammer2_blockref_t *tbase;
3801 KKASSERT((chain->flags & HAMMER2_CHAIN_BMAPPED) == 0);
3802 hammer2_chain_modify(*parentp, mtid, 0, 0);
3803 tbase = hammer2_chain_base_and_count(*parentp, &tcount);
3804 hammer2_base_insert(*parentp, tbase, tcount, chain, obref);
3805 if (bcmp(obref, &chain->bref, sizeof(chain->bref))) {
3806 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD |
3807 HAMMER2_CHAIN_UPDATE);
3809 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
3815 * Helper function for deleting chains.
3817 * The chain is removed from the live view (the RBTREE) as well as the parent's
3818 * blockmap. Both chain and its parent must be locked.
3820 * parent may not be errored. chain can be errored.
3823 _hammer2_chain_delete_helper(hammer2_chain_t *parent, hammer2_chain_t *chain,
3824 hammer2_tid_t mtid, int flags,
3825 hammer2_blockref_t *obref)
3830 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
3831 KKASSERT(chain->parent == parent);
3834 if (chain->flags & HAMMER2_CHAIN_BMAPPED) {
3836 * Chain is blockmapped, so there must be a parent.
3837 * Atomically remove the chain from the parent and remove
3838 * the blockmap entry. The parent must be set modified
3839 * to remove the blockmap entry.
3841 hammer2_blockref_t *base;
3844 KKASSERT(parent != NULL);
3845 KKASSERT(parent->error == 0);
3846 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
3847 error = hammer2_chain_modify(parent, mtid, 0, 0);
3852 * Calculate blockmap pointer
3854 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
3855 hammer2_spin_ex(&chain->core.spin);
3856 hammer2_spin_ex(&parent->core.spin);
3858 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3859 atomic_add_int(&parent->core.live_count, -1);
3860 ++parent->core.generation;
3861 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
3862 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
3863 --parent->core.chain_count;
3864 chain->parent = NULL;
3866 switch(parent->bref.type) {
3867 case HAMMER2_BREF_TYPE_INODE:
3869 * Access the inode's block array. However, there
3870 * is no block array if the inode is flagged
3874 (parent->data->ipdata.meta.op_flags &
3875 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
3877 &parent->data->ipdata.u.blockset.blockref[0];
3881 count = HAMMER2_SET_COUNT;
3883 case HAMMER2_BREF_TYPE_INDIRECT:
3884 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3886 base = &parent->data->npdata[0];
3889 count = parent->bytes / sizeof(hammer2_blockref_t);
3891 case HAMMER2_BREF_TYPE_VOLUME:
3892 base = &parent->data->voldata.
3893 sroot_blockset.blockref[0];
3894 count = HAMMER2_SET_COUNT;
3896 case HAMMER2_BREF_TYPE_FREEMAP:
3897 base = &parent->data->blkset.blockref[0];
3898 count = HAMMER2_SET_COUNT;
3903 panic("_hammer2_chain_delete_helper: "
3904 "unrecognized blockref type: %d",
3909 * delete blockmapped chain from its parent.
3911 * The parent is not affected by any statistics in chain
3912 * which are pending synchronization. That is, there is
3913 * nothing to undo in the parent since they have not yet
3914 * been incorporated into the parent.
3916 * The parent is affected by statistics stored in inodes.
3917 * Those have already been synchronized, so they must be
3918 * undone. XXX split update possible w/delete in middle?
3921 hammer2_base_delete(parent, base, count, chain, obref);
3923 hammer2_spin_unex(&parent->core.spin);
3924 hammer2_spin_unex(&chain->core.spin);
3925 } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
3927 * Chain is not blockmapped but a parent is present.
3928 * Atomically remove the chain from the parent. There is
3929 * no blockmap entry to remove.
3931 * Because chain was associated with a parent but not
3932 * synchronized, the chain's *_count_up fields contain
3933 * inode adjustment statistics which must be undone.
3935 hammer2_spin_ex(&chain->core.spin);
3936 hammer2_spin_ex(&parent->core.spin);
3937 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3938 atomic_add_int(&parent->core.live_count, -1);
3939 ++parent->core.generation;
3940 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
3941 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
3942 --parent->core.chain_count;
3943 chain->parent = NULL;
3944 hammer2_spin_unex(&parent->core.spin);
3945 hammer2_spin_unex(&chain->core.spin);
3948 * Chain is not blockmapped and has no parent. This
3949 * is a degenerate case.
3951 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3958 * Create an indirect block that covers one or more of the elements in the
3959 * current parent. Either returns the existing parent with no locking or
3960 * ref changes or returns the new indirect block locked and referenced
3961 * and leaving the original parent lock/ref intact as well.
3963 * If an error occurs, NULL is returned and *errorp is set to the H2 error.
3965 * The returned chain depends on where the specified key falls.
3967 * The key/keybits for the indirect mode only needs to follow three rules:
3969 * (1) That all elements underneath it fit within its key space and
3971 * (2) That all elements outside it are outside its key space.
3973 * (3) When creating the new indirect block any elements in the current
3974 * parent that fit within the new indirect block's keyspace must be
3975 * moved into the new indirect block.
3977 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
3978 * keyspace the the current parent, but lookup/iteration rules will
3979 * ensure (and must ensure) that rule (2) for all parents leading up
3980 * to the nearest inode or the root volume header is adhered to. This
3981 * is accomplished by always recursing through matching keyspaces in
3982 * the hammer2_chain_lookup() and hammer2_chain_next() API.
3984 * The current implementation calculates the current worst-case keyspace by
3985 * iterating the current parent and then divides it into two halves, choosing
3986 * whichever half has the most elements (not necessarily the half containing
3987 * the requested key).
3989 * We can also opt to use the half with the least number of elements. This
3990 * causes lower-numbered keys (aka logical file offsets) to recurse through
3991 * fewer indirect blocks and higher-numbered keys to recurse through more.
3992 * This also has the risk of not moving enough elements to the new indirect
3993 * block and being forced to create several indirect blocks before the element
3996 * Must be called with an exclusively locked parent.
3998 * NOTE: *errorp set to HAMMER_ERROR_* flags
4000 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
4001 hammer2_key_t *keyp, int keybits,
4002 hammer2_blockref_t *base, int count);
4003 static int hammer2_chain_indkey_file(hammer2_chain_t *parent,
4004 hammer2_key_t *keyp, int keybits,
4005 hammer2_blockref_t *base, int count,
4007 static int hammer2_chain_indkey_dir(hammer2_chain_t *parent,
4008 hammer2_key_t *keyp, int keybits,
4009 hammer2_blockref_t *base, int count,
4013 hammer2_chain_create_indirect(hammer2_chain_t *parent,
4014 hammer2_key_t create_key, int create_bits,
4015 hammer2_tid_t mtid, int for_type, int *errorp)
4018 hammer2_blockref_t *base;
4019 hammer2_blockref_t *bref;
4020 hammer2_blockref_t bsave;
4021 hammer2_blockref_t dummy;
4022 hammer2_chain_t *chain;
4023 hammer2_chain_t *ichain;
4024 hammer2_key_t key = create_key;
4025 hammer2_key_t key_beg;
4026 hammer2_key_t key_end;
4027 hammer2_key_t key_next;
4028 int keybits = create_bits;
4036 int maxloops = 300000;
4039 * Calculate the base blockref pointer or NULL if the chain
4040 * is known to be empty. We need to calculate the array count
4041 * for RB lookups either way.
4044 KKASSERT(hammer2_mtx_owned(&parent->lock));
4047 * Pre-modify the parent now to avoid having to deal with error
4048 * processing if we tried to later (in the middle of our loop).
4050 * We are going to be moving bref's around, the indirect blocks
4051 * cannot be in an initial state. Do not pass MODIFY_OPTDATA.
4053 *errorp = hammer2_chain_modify(parent, mtid, 0, 0);
4055 kprintf("hammer2_chain_create_indirect: error %08x %s\n",
4056 *errorp, hammer2_error_str(*errorp));
4059 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
4061 /*hammer2_chain_modify(&parent, HAMMER2_MODIFY_OPTDATA);*/
4062 base = hammer2_chain_base_and_count(parent, &count);
4065 * How big should our new indirect block be? It has to be at least
4066 * as large as its parent for splits to work properly.
4068 * The freemap uses a specific indirect block size. The number of
4069 * levels are built dynamically and ultimately depend on the size
4070 * volume. Because freemap blocks are taken from the reserved areas
4071 * of the volume our goal is efficiency (fewer levels) and not so
4072 * much to save disk space.
4074 * The first indirect block level for a directory usually uses
4075 * HAMMER2_IND_BYTES_MIN (4KB = 32 directory entries). Due to
4076 * the hash mechanism, this typically gives us a nominal
4077 * 32 * 4 entries with one level of indirection.
4079 * We use HAMMER2_IND_BYTES_NOM (16KB = 128 blockrefs) for FILE
4080 * indirect blocks. The initial 4 entries in the inode gives us
4081 * 256KB. Up to 4 indirect blocks gives us 32MB. Three levels
4082 * of indirection gives us 137GB, and so forth. H2 can support
4083 * huge file sizes but they are not typical, so we try to stick
4084 * with compactness and do not use a larger indirect block size.
4086 * We could use 64KB (PBUFSIZE), giving us 512 blockrefs, but
4087 * due to the way indirect blocks are created this usually winds
4088 * up being extremely inefficient for small files. Even though
4089 * 16KB requires more levels of indirection for very large files,
4090 * the 16KB records can be ganged together into 64KB DIOs.
4092 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
4093 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
4094 nbytes = HAMMER2_FREEMAP_LEVELN_PSIZE;
4095 } else if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
4096 if (parent->data->ipdata.meta.type ==
4097 HAMMER2_OBJTYPE_DIRECTORY)
4098 nbytes = HAMMER2_IND_BYTES_MIN; /* 4KB = 32 entries */
4100 nbytes = HAMMER2_IND_BYTES_NOM; /* 16KB = ~8MB file */
4103 nbytes = HAMMER2_IND_BYTES_NOM;
4105 if (nbytes < count * sizeof(hammer2_blockref_t)) {
4106 KKASSERT(for_type != HAMMER2_BREF_TYPE_FREEMAP_NODE &&
4107 for_type != HAMMER2_BREF_TYPE_FREEMAP_LEAF);
4108 nbytes = count * sizeof(hammer2_blockref_t);
4110 ncount = nbytes / sizeof(hammer2_blockref_t);
4113 * When creating an indirect block for a freemap node or leaf
4114 * the key/keybits must be fitted to static radix levels because
4115 * particular radix levels use particular reserved blocks in the
4118 * This routine calculates the key/radix of the indirect block
4119 * we need to create, and whether it is on the high-side or the
4123 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
4124 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
4125 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
4128 case HAMMER2_BREF_TYPE_DATA:
4129 keybits = hammer2_chain_indkey_file(parent, &key, keybits,
4130 base, count, ncount);
4132 case HAMMER2_BREF_TYPE_DIRENT:
4133 case HAMMER2_BREF_TYPE_INODE:
4134 keybits = hammer2_chain_indkey_dir(parent, &key, keybits,
4135 base, count, ncount);
4138 panic("illegal indirect block for bref type %d", for_type);
4143 * Normalize the key for the radix being represented, keeping the
4144 * high bits and throwing away the low bits.
4146 key &= ~(((hammer2_key_t)1 << keybits) - 1);
4149 * Ok, create our new indirect block
4151 bzero(&dummy, sizeof(dummy));
4152 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
4153 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
4154 dummy.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
4156 dummy.type = HAMMER2_BREF_TYPE_INDIRECT;
4159 dummy.keybits = keybits;
4160 dummy.data_off = hammer2_getradix(nbytes);
4162 HAMMER2_ENC_CHECK(HAMMER2_DEC_CHECK(parent->bref.methods)) |
4163 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
4165 ichain = hammer2_chain_alloc(hmp, parent->pmp, &dummy);
4166 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
4167 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
4168 /* ichain has one ref at this point */
4171 * We have to mark it modified to allocate its block, but use
4172 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
4173 * it won't be acted upon by the flush code.
4175 * XXX remove OPTDATA, we need a fully initialized indirect block to
4176 * be able to move the original blockref.
4178 *errorp = hammer2_chain_modify(ichain, mtid, 0, 0);
4180 kprintf("hammer2_chain_create_indirect: error %08x %s\n",
4181 *errorp, hammer2_error_str(*errorp));
4182 hammer2_chain_unlock(ichain);
4183 hammer2_chain_drop(ichain);
4186 KKASSERT((ichain->flags & HAMMER2_CHAIN_INITIAL) == 0);
4189 * Iterate the original parent and move the matching brefs into
4190 * the new indirect block.
4192 * XXX handle flushes.
4195 key_end = HAMMER2_KEY_MAX;
4196 key_next = 0; /* avoid gcc warnings */
4197 hammer2_spin_ex(&parent->core.spin);
4203 * Parent may have been modified, relocating its block array.
4204 * Reload the base pointer.
4206 base = hammer2_chain_base_and_count(parent, &count);
4208 if (++loops > 100000) {
4209 hammer2_spin_unex(&parent->core.spin);
4210 panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
4211 reason, parent, base, count, key_next);
4215 * NOTE: spinlock stays intact, returned chain (if not NULL)
4216 * is not referenced or locked which means that we
4217 * cannot safely check its flagged / deletion status
4220 chain = hammer2_combined_find(parent, base, count,
4224 generation = parent->core.generation;
4227 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4230 * Skip keys that are not within the key/radix of the new
4231 * indirect block. They stay in the parent.
4233 if (rounddown2(key ^ bref->key, (hammer2_key_t)1 << keybits) != 0) {
4234 goto next_key_spinlocked;
4238 * Load the new indirect block by acquiring the related
4239 * chains (potentially from media as it might not be
4240 * in-memory). Then move it to the new parent (ichain).
4242 * chain is referenced but not locked. We must lock the
4243 * chain to obtain definitive state.
4248 * Use chain already present in the RBTREE
4250 hammer2_chain_ref(chain);
4251 hammer2_spin_unex(&parent->core.spin);
4252 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
4255 * Get chain for blockref element. _get returns NULL
4256 * on insertion race.
4258 hammer2_spin_unex(&parent->core.spin);
4259 chain = hammer2_chain_get(parent, generation, &bsave,
4260 HAMMER2_RESOLVE_NEVER);
4261 if (chain == NULL) {
4263 hammer2_spin_ex(&parent->core.spin);
4269 * This is always live so if the chain has been deleted
4270 * we raced someone and we have to retry.
4272 * NOTE: Lookups can race delete-duplicate because
4273 * delete-duplicate does not lock the parent's core
4274 * (they just use the spinlock on the core).
4276 * (note reversed logic for this one)
4278 if (bcmp(&bsave, &chain->bref, sizeof(bsave)) ||
4279 chain->parent != parent ||
4280 (chain->flags & HAMMER2_CHAIN_DELETED)) {
4281 hammer2_chain_unlock(chain);
4282 hammer2_chain_drop(chain);
4283 if (hammer2_debug & 0x0040) {
4284 kprintf("LOST PARENT RETRY "
4285 "RETRY (%p,%p)->%p %08x\n",
4286 parent, chain->parent, chain, chain->flags);
4288 hammer2_spin_ex(&parent->core.spin);
4293 * Shift the chain to the indirect block.
4295 * WARNING! No reason for us to load chain data, pass NOSTATS
4296 * to prevent delete/insert from trying to access
4297 * inode stats (and thus asserting if there is no
4298 * chain->data loaded).
4300 * WARNING! The (parent, chain) deletion may modify the parent
4301 * and invalidate the base pointer.
4303 * WARNING! Parent must already be marked modified, so we
4304 * can assume that chain_delete always suceeds.
4306 * WARNING! hammer2_chain_repchange() does not have to be
4307 * called (and doesn't work anyway because we are
4308 * only doing a partial shift). A recursion that is
4309 * in-progress can continue at the current parent
4310 * and will be able to properly find its next key.
4312 error = hammer2_chain_delete_obref(parent, chain, mtid, 0,
4314 KKASSERT(error == 0);
4315 hammer2_chain_rename_obref(&ichain, chain, mtid, 0, &bsave);
4316 hammer2_chain_unlock(chain);
4317 hammer2_chain_drop(chain);
4318 KKASSERT(parent->refs > 0);
4320 base = NULL; /* safety */
4321 hammer2_spin_ex(&parent->core.spin);
4322 next_key_spinlocked:
4323 if (--maxloops == 0)
4324 panic("hammer2_chain_create_indirect: maxloops");
4326 if (key_next == 0 || key_next > key_end)
4331 hammer2_spin_unex(&parent->core.spin);
4334 * Insert the new indirect block into the parent now that we've
4335 * cleared out some entries in the parent. We calculated a good
4336 * insertion index in the loop above (ichain->index).
4338 * We don't have to set UPDATE here because we mark ichain
4339 * modified down below (so the normal modified -> flush -> set-moved
4340 * sequence applies).
4342 * The insertion shouldn't race as this is a completely new block
4343 * and the parent is locked.
4345 base = NULL; /* safety, parent modify may change address */
4346 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
4347 KKASSERT(parent->core.live_count < count);
4348 hammer2_chain_insert(parent, ichain,
4349 HAMMER2_CHAIN_INSERT_SPIN |
4350 HAMMER2_CHAIN_INSERT_LIVE,
4354 * Make sure flushes propogate after our manual insertion.
4356 hammer2_chain_setflush(ichain);
4357 hammer2_chain_setflush(parent);
4360 * Figure out what to return.
4362 if (rounddown2(create_key ^ key, (hammer2_key_t)1 << keybits)) {
4364 * Key being created is outside the key range,
4365 * return the original parent.
4367 hammer2_chain_unlock(ichain);
4368 hammer2_chain_drop(ichain);
4371 * Otherwise its in the range, return the new parent.
4372 * (leave both the new and old parent locked).
4381 * Do maintenance on an indirect chain. Both parent and chain are locked.
4383 * Returns non-zero if (chain) is deleted, either due to being empty or
4384 * because its children were safely moved into the parent.
4387 hammer2_chain_indirect_maintenance(hammer2_chain_t *parent,
4388 hammer2_chain_t *chain)
4390 hammer2_blockref_t *chain_base;
4391 hammer2_blockref_t *base;
4392 hammer2_blockref_t *bref;
4393 hammer2_blockref_t bsave;
4394 hammer2_key_t key_next;
4395 hammer2_key_t key_beg;
4396 hammer2_key_t key_end;
4397 hammer2_chain_t *sub;
4404 * Make sure we have an accurate live_count
4406 if ((chain->flags & (HAMMER2_CHAIN_INITIAL |
4407 HAMMER2_CHAIN_COUNTEDBREFS)) == 0) {
4408 base = &chain->data->npdata[0];
4409 count = chain->bytes / sizeof(hammer2_blockref_t);
4410 hammer2_chain_countbrefs(chain, base, count);
4414 * If the indirect block is empty we can delete it.
4415 * (ignore deletion error)
4417 if (chain->core.live_count == 0 && RB_EMPTY(&chain->core.rbtree)) {
4418 hammer2_chain_delete(parent, chain,
4419 chain->bref.modify_tid,
4420 HAMMER2_DELETE_PERMANENT);
4421 hammer2_chain_repchange(parent, chain);
4425 base = hammer2_chain_base_and_count(parent, &count);
4427 if ((parent->flags & (HAMMER2_CHAIN_INITIAL |
4428 HAMMER2_CHAIN_COUNTEDBREFS)) == 0) {
4429 hammer2_chain_countbrefs(parent, base, count);
4433 * Determine if we can collapse chain into parent, calculate
4434 * hysteresis for chain emptiness.
4436 if (parent->core.live_count + chain->core.live_count - 1 > count)
4438 chain_count = chain->bytes / sizeof(hammer2_blockref_t);
4439 if (chain->core.live_count > chain_count * 3 / 4)
4443 * Ok, theoretically we can collapse chain's contents into
4444 * parent. chain is locked, but any in-memory children of chain
4445 * are not. For this to work, we must be able to dispose of any
4446 * in-memory children of chain.
4448 * For now require that there are no in-memory children of chain.
4450 * WARNING! Both chain and parent must remain locked across this
4455 * Parent must be marked modified. Don't try to collapse it if we
4456 * can't mark it modified. Once modified, destroy chain to make room
4457 * and to get rid of what will be a conflicting key (this is included
4458 * in the calculation above). Finally, move the children of chain
4459 * into chain's parent.
4461 * This order creates an accounting problem for bref.embed.stats
4462 * because we destroy chain before we remove its children. Any
4463 * elements whos blockref is already synchronized will be counted
4464 * twice. To deal with the problem we clean out chain's stats prior
4467 error = hammer2_chain_modify(parent, 0, 0, 0);
4469 krateprintf(&krate_h2me, "hammer2: indirect_maint: %s\n",
4470 hammer2_error_str(error));
4473 error = hammer2_chain_modify(chain, chain->bref.modify_tid, 0, 0);
4475 krateprintf(&krate_h2me, "hammer2: indirect_maint: %s\n",
4476 hammer2_error_str(error));
4480 chain->bref.embed.stats.inode_count = 0;
4481 chain->bref.embed.stats.data_count = 0;
4482 error = hammer2_chain_delete(parent, chain,
4483 chain->bref.modify_tid,
4484 HAMMER2_DELETE_PERMANENT);
4485 KKASSERT(error == 0);
4488 * The combined_find call requires core.spin to be held. One would
4489 * think there wouldn't be any conflicts since we hold chain
4490 * exclusively locked, but the caching mechanism for 0-ref children
4491 * does not require a chain lock.
4493 hammer2_spin_ex(&chain->core.spin);
4497 key_end = HAMMER2_KEY_MAX;
4499 chain_base = &chain->data->npdata[0];
4500 chain_count = chain->bytes / sizeof(hammer2_blockref_t);
4501 sub = hammer2_combined_find(chain, chain_base, chain_count,
4505 generation = chain->core.generation;
4508 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4512 hammer2_chain_ref(sub);
4513 hammer2_spin_unex(&chain->core.spin);
4514 hammer2_chain_lock(sub, HAMMER2_RESOLVE_NEVER);
4516 hammer2_spin_unex(&chain->core.spin);
4517 sub = hammer2_chain_get(chain, generation, &bsave,
4518 HAMMER2_RESOLVE_NEVER);
4520 hammer2_spin_ex(&chain->core.spin);
4524 if (bcmp(&bsave, &sub->bref, sizeof(bsave)) ||
4525 sub->parent != chain ||
4526 (sub->flags & HAMMER2_CHAIN_DELETED)) {
4527 hammer2_chain_unlock(sub);
4528 hammer2_chain_drop(sub);
4529 hammer2_spin_ex(&chain->core.spin);
4530 sub = NULL; /* safety */
4533 error = hammer2_chain_delete_obref(chain, sub,
4534 sub->bref.modify_tid, 0,
4536 KKASSERT(error == 0);
4537 hammer2_chain_rename_obref(&parent, sub,
4538 sub->bref.modify_tid,
4539 HAMMER2_INSERT_SAMEPARENT, &bsave);
4540 hammer2_chain_unlock(sub);
4541 hammer2_chain_drop(sub);
4542 hammer2_spin_ex(&chain->core.spin);
4548 hammer2_spin_unex(&chain->core.spin);
4550 hammer2_chain_repchange(parent, chain);
4556 * Freemap indirect blocks
4558 * Calculate the keybits and highside/lowside of the freemap node the
4559 * caller is creating.
4561 * This routine will specify the next higher-level freemap key/radix
4562 * representing the lowest-ordered set. By doing so, eventually all
4563 * low-ordered sets will be moved one level down.
4565 * We have to be careful here because the freemap reserves a limited
4566 * number of blocks for a limited number of levels. So we can't just
4567 * push indiscriminately.
4570 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
4571 int keybits, hammer2_blockref_t *base, int count)
4573 hammer2_chain_t *chain;
4574 hammer2_blockref_t *bref;
4576 hammer2_key_t key_beg;
4577 hammer2_key_t key_end;
4578 hammer2_key_t key_next;
4581 int maxloops = 300000;
4589 * Calculate the range of keys in the array being careful to skip
4590 * slots which are overridden with a deletion.
4593 key_end = HAMMER2_KEY_MAX;
4594 hammer2_spin_ex(&parent->core.spin);
4597 if (--maxloops == 0) {
4598 panic("indkey_freemap shit %p %p:%d\n",
4599 parent, base, count);
4601 chain = hammer2_combined_find(parent, base, count,
4613 * Skip deleted chains.
4615 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4616 if (key_next == 0 || key_next > key_end)
4623 * Use the full live (not deleted) element for the scan
4624 * iteration. HAMMER2 does not allow partial replacements.
4626 * XXX should be built into hammer2_combined_find().
4628 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4630 if (keybits > bref->keybits) {
4632 keybits = bref->keybits;
4633 } else if (keybits == bref->keybits && bref->key < key) {
4640 hammer2_spin_unex(&parent->core.spin);
4643 * Return the keybits for a higher-level FREEMAP_NODE covering
4647 case HAMMER2_FREEMAP_LEVEL0_RADIX:
4648 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
4650 case HAMMER2_FREEMAP_LEVEL1_RADIX:
4651 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
4653 case HAMMER2_FREEMAP_LEVEL2_RADIX:
4654 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
4656 case HAMMER2_FREEMAP_LEVEL3_RADIX:
4657 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
4659 case HAMMER2_FREEMAP_LEVEL4_RADIX:
4660 keybits = HAMMER2_FREEMAP_LEVEL5_RADIX;
4662 case HAMMER2_FREEMAP_LEVEL5_RADIX:
4663 panic("hammer2_chain_indkey_freemap: level too high");
4666 panic("hammer2_chain_indkey_freemap: bad radix");
4675 * File indirect blocks
4677 * Calculate the key/keybits for the indirect block to create by scanning
4678 * existing keys. The key being created is also passed in *keyp and can be
4679 * inside or outside the indirect block. Regardless, the indirect block
4680 * must hold at least two keys in order to guarantee sufficient space.
4682 * We use a modified version of the freemap's fixed radix tree, but taylored
4683 * for file data. Basically we configure an indirect block encompassing the
4687 hammer2_chain_indkey_file(hammer2_chain_t *parent, hammer2_key_t *keyp,
4688 int keybits, hammer2_blockref_t *base, int count,
4691 hammer2_chain_t *chain;
4692 hammer2_blockref_t *bref;
4694 hammer2_key_t key_beg;
4695 hammer2_key_t key_end;
4696 hammer2_key_t key_next;
4700 int maxloops = 300000;
4708 * Calculate the range of keys in the array being careful to skip
4709 * slots which are overridden with a deletion.
4711 * Locate the smallest key.
4714 key_end = HAMMER2_KEY_MAX;
4715 hammer2_spin_ex(&parent->core.spin);
4718 if (--maxloops == 0) {
4719 panic("indkey_freemap shit %p %p:%d\n",
4720 parent, base, count);
4722 chain = hammer2_combined_find(parent, base, count,
4734 * Skip deleted chains.
4736 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4737 if (key_next == 0 || key_next > key_end)
4744 * Use the full live (not deleted) element for the scan
4745 * iteration. HAMMER2 does not allow partial replacements.
4747 * XXX should be built into hammer2_combined_find().
4749 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4751 if (keybits > bref->keybits) {
4753 keybits = bref->keybits;
4754 } else if (keybits == bref->keybits && bref->key < key) {
4761 hammer2_spin_unex(&parent->core.spin);
4764 * Calculate the static keybits for a higher-level indirect block
4765 * that contains the key.
4770 case HAMMER2_IND_BYTES_MIN / sizeof(hammer2_blockref_t):
4771 nradix = HAMMER2_IND_RADIX_MIN - HAMMER2_BLOCKREF_RADIX;
4773 case HAMMER2_IND_BYTES_NOM / sizeof(hammer2_blockref_t):
4774 nradix = HAMMER2_IND_RADIX_NOM - HAMMER2_BLOCKREF_RADIX;
4776 case HAMMER2_IND_BYTES_MAX / sizeof(hammer2_blockref_t):
4777 nradix = HAMMER2_IND_RADIX_MAX - HAMMER2_BLOCKREF_RADIX;
4780 panic("bad ncount %d\n", ncount);
4786 * The largest radix that can be returned for an indirect block is
4787 * 63 bits. (The largest practical indirect block radix is actually
4788 * 62 bits because the top-level inode or volume root contains four
4789 * entries, but allow 63 to be returned).
4794 return keybits + nradix;
4800 * Directory indirect blocks.
4802 * Covers both the inode index (directory of inodes), and directory contents
4803 * (filenames hardlinked to inodes).
4805 * Because directory keys are hashed we generally try to cut the space in
4806 * half. We accomodate the inode index (which tends to have linearly
4807 * increasing inode numbers) by ensuring that the keyspace is at least large
4808 * enough to fill up the indirect block being created.
4811 hammer2_chain_indkey_dir(hammer2_chain_t *parent, hammer2_key_t *keyp,
4812 int keybits, hammer2_blockref_t *base, int count,
4815 hammer2_blockref_t *bref;
4816 hammer2_chain_t *chain;
4817 hammer2_key_t key_beg;
4818 hammer2_key_t key_end;
4819 hammer2_key_t key_next;
4824 int maxloops = 300000;
4827 * NOTE: We can't take a shortcut here anymore for inodes because
4828 * the root directory can contain a mix of inodes and directory
4829 * entries (we used to just return 63 if parent->bref.type was
4830 * HAMMER2_BREF_TYPE_INODE.
4837 * Calculate the range of keys in the array being careful to skip
4838 * slots which are overridden with a deletion.
4841 key_end = HAMMER2_KEY_MAX;
4842 hammer2_spin_ex(&parent->core.spin);
4845 if (--maxloops == 0) {
4846 panic("indkey_freemap shit %p %p:%d\n",
4847 parent, base, count);
4849 chain = hammer2_combined_find(parent, base, count,
4863 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4864 if (key_next == 0 || key_next > key_end)
4871 * Use the full live (not deleted) element for the scan
4872 * iteration. HAMMER2 does not allow partial replacements.
4874 * XXX should be built into hammer2_combined_find().
4876 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4879 * Expand our calculated key range (key, keybits) to fit
4880 * the scanned key. nkeybits represents the full range
4881 * that we will later cut in half (two halves @ nkeybits - 1).
4884 if (nkeybits < bref->keybits) {
4885 if (bref->keybits > 64) {
4886 kprintf("bad bref chain %p bref %p\n",
4890 nkeybits = bref->keybits;
4892 while (nkeybits < 64 &&
4893 rounddown2(key ^ bref->key, (hammer2_key_t)1 << nkeybits) != 0) {
4898 * If the new key range is larger we have to determine
4899 * which side of the new key range the existing keys fall
4900 * under by checking the high bit, then collapsing the
4901 * locount into the hicount or vise-versa.
4903 if (keybits != nkeybits) {
4904 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
4915 * The newly scanned key will be in the lower half or the
4916 * upper half of the (new) key range.
4918 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
4927 hammer2_spin_unex(&parent->core.spin);
4928 bref = NULL; /* now invalid (safety) */
4931 * Adjust keybits to represent half of the full range calculated
4932 * above (radix 63 max) for our new indirect block.
4937 * Expand keybits to hold at least ncount elements. ncount will be
4938 * a power of 2. This is to try to completely fill leaf nodes (at
4939 * least for keys which are not hashes).
4941 * We aren't counting 'in' or 'out', we are counting 'high side'
4942 * and 'low side' based on the bit at (1LL << keybits). We want
4943 * everything to be inside in these cases so shift it all to
4944 * the low or high side depending on the new high bit.
4946 while (((hammer2_key_t)1 << keybits) < ncount) {
4948 if (key & ((hammer2_key_t)1 << keybits)) {
4957 if (hicount > locount)
4958 key |= (hammer2_key_t)1 << keybits;
4960 key &= ~(hammer2_key_t)1 << keybits;
4970 * Directory indirect blocks.
4972 * Covers both the inode index (directory of inodes), and directory contents
4973 * (filenames hardlinked to inodes).
4975 * Because directory keys are hashed we generally try to cut the space in
4976 * half. We accomodate the inode index (which tends to have linearly
4977 * increasing inode numbers) by ensuring that the keyspace is at least large
4978 * enough to fill up the indirect block being created.
4981 hammer2_chain_indkey_dir(hammer2_chain_t *parent, hammer2_key_t *keyp,
4982 int keybits, hammer2_blockref_t *base, int count,
4985 hammer2_blockref_t *bref;
4986 hammer2_chain_t *chain;
4987 hammer2_key_t key_beg;
4988 hammer2_key_t key_end;
4989 hammer2_key_t key_next;
4994 int maxloops = 300000;
4997 * Shortcut if the parent is the inode. In this situation the
4998 * parent has 4+1 directory entries and we are creating an indirect
4999 * block capable of holding many more.
5001 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
5010 * Calculate the range of keys in the array being careful to skip
5011 * slots which are overridden with a deletion.
5014 key_end = HAMMER2_KEY_MAX;
5015 hammer2_spin_ex(&parent->core.spin);
5018 if (--maxloops == 0) {
5019 panic("indkey_freemap shit %p %p:%d\n",
5020 parent, base, count);
5022 chain = hammer2_combined_find(parent, base, count,
5036 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
5037 if (key_next == 0 || key_next > key_end)
5044 * Use the full live (not deleted) element for the scan
5045 * iteration. HAMMER2 does not allow partial replacements.
5047 * XXX should be built into hammer2_combined_find().
5049 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
5052 * Expand our calculated key range (key, keybits) to fit
5053 * the scanned key. nkeybits represents the full range
5054 * that we will later cut in half (two halves @ nkeybits - 1).
5057 if (nkeybits < bref->keybits) {
5058 if (bref->keybits > 64) {
5059 kprintf("bad bref chain %p bref %p\n",
5063 nkeybits = bref->keybits;
5065 while (nkeybits < 64 &&
5066 (~(((hammer2_key_t)1 << nkeybits) - 1) &
5067 (key ^ bref->key)) != 0) {
5072 * If the new key range is larger we have to determine
5073 * which side of the new key range the existing keys fall
5074 * under by checking the high bit, then collapsing the
5075 * locount into the hicount or vise-versa.
5077 if (keybits != nkeybits) {
5078 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
5089 * The newly scanned key will be in the lower half or the
5090 * upper half of the (new) key range.
5092 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
5101 hammer2_spin_unex(&parent->core.spin);
5102 bref = NULL; /* now invalid (safety) */
5105 * Adjust keybits to represent half of the full range calculated
5106 * above (radix 63 max) for our new indirect block.
5111 * Expand keybits to hold at least ncount elements. ncount will be
5112 * a power of 2. This is to try to completely fill leaf nodes (at
5113 * least for keys which are not hashes).
5115 * We aren't counting 'in' or 'out', we are counting 'high side'
5116 * and 'low side' based on the bit at (1LL << keybits). We want
5117 * everything to be inside in these cases so shift it all to
5118 * the low or high side depending on the new high bit.
5120 while (((hammer2_key_t)1 << keybits) < ncount) {
5122 if (key & ((hammer2_key_t)1 << keybits)) {
5131 if (hicount > locount)
5132 key |= (hammer2_key_t)1 << keybits;
5134 key &= ~(hammer2_key_t)1 << keybits;
5144 * Sets CHAIN_DELETED and remove the chain's blockref from the parent if
5147 * Both parent and chain must be locked exclusively.
5149 * This function will modify the parent if the blockref requires removal
5150 * from the parent's block table.
5152 * This function is NOT recursive. Any entity already pushed into the
5153 * chain (such as an inode) may still need visibility into its contents,
5154 * as well as the ability to read and modify the contents. For example,
5155 * for an unlinked file which is still open.
5157 * Also note that the flusher is responsible for cleaning up empty
5161 hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
5162 hammer2_tid_t mtid, int flags)
5166 KKASSERT(hammer2_mtx_owned(&chain->lock));
5169 * Nothing to do if already marked.
5171 * We need the spinlock on the core whos RBTREE contains chain
5172 * to protect against races.
5174 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
5175 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
5176 chain->parent == parent);
5177 error = _hammer2_chain_delete_helper(parent, chain,
5182 * Permanent deletions mark the chain as destroyed.
5184 * NOTE: We do not setflush the chain unless the deletion is
5185 * permanent, since the deletion of a chain does not actually
5186 * require it to be flushed.
5189 if (flags & HAMMER2_DELETE_PERMANENT) {
5190 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
5191 hammer2_chain_setflush(chain);
5199 hammer2_chain_delete_obref(hammer2_chain_t *parent, hammer2_chain_t *chain,
5200 hammer2_tid_t mtid, int flags,
5201 hammer2_blockref_t *obref)
5205 KKASSERT(hammer2_mtx_owned(&chain->lock));
5208 * Nothing to do if already marked.
5210 * We need the spinlock on the core whos RBTREE contains chain
5211 * to protect against races.
5213 obref->type = HAMMER2_BREF_TYPE_EMPTY;
5214 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
5215 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
5216 chain->parent == parent);
5217 error = _hammer2_chain_delete_helper(parent, chain,
5218 mtid, flags, obref);
5222 * Permanent deletions mark the chain as destroyed.
5224 * NOTE: We do not setflush the chain unless the deletion is
5225 * permanent, since the deletion of a chain does not actually
5226 * require it to be flushed.
5229 if (flags & HAMMER2_DELETE_PERMANENT) {
5230 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
5231 hammer2_chain_setflush(chain);
5239 * Returns the index of the nearest element in the blockref array >= elm.
5240 * Returns (count) if no element could be found.
5242 * Sets *key_nextp to the next key for loop purposes but does not modify
5243 * it if the next key would be higher than the current value of *key_nextp.
5244 * Note that *key_nexp can overflow to 0, which should be tested by the
5247 * WARNING! Must be called with parent's spinlock held. Spinlock remains
5248 * held through the operation.
5251 hammer2_base_find(hammer2_chain_t *parent,
5252 hammer2_blockref_t *base, int count,
5253 hammer2_key_t *key_nextp,
5254 hammer2_key_t key_beg, hammer2_key_t key_end)
5256 hammer2_blockref_t *scan;
5257 hammer2_key_t scan_end;
5262 * Require the live chain's already have their core's counted
5263 * so we can optimize operations.
5265 KKASSERT(parent->flags & HAMMER2_CHAIN_COUNTEDBREFS);
5270 if (count == 0 || base == NULL)
5274 * Sequential optimization using parent->cache_index. This is
5275 * the most likely scenario.
5277 * We can avoid trailing empty entries on live chains, otherwise
5278 * we might have to check the whole block array.
5280 i = parent->cache_index; /* SMP RACE OK */
5282 limit = parent->core.live_zero;
5287 KKASSERT(i < count);
5293 while (i > 0 && (scan->type == HAMMER2_BREF_TYPE_EMPTY ||
5294 scan->key > key_beg)) {
5298 parent->cache_index = i;
5301 * Search forwards, stop when we find a scan element which
5302 * encloses the key or until we know that there are no further
5306 if (scan->type != HAMMER2_BREF_TYPE_EMPTY) {
5307 scan_end = scan->key +
5308 ((hammer2_key_t)1 << scan->keybits) - 1;
5309 if (scan->key > key_beg || scan_end >= key_beg)
5318 parent->cache_index = i;
5322 scan_end = scan->key +
5323 ((hammer2_key_t)1 << scan->keybits);
5324 if (scan_end && (*key_nextp > scan_end ||
5326 *key_nextp = scan_end;
5334 * Do a combined search and return the next match either from the blockref
5335 * array or from the in-memory chain. Sets *bresp to the returned bref in
5336 * both cases, or sets it to NULL if the search exhausted. Only returns
5337 * a non-NULL chain if the search matched from the in-memory chain.
5339 * When no in-memory chain has been found and a non-NULL bref is returned
5343 * The returned chain is not locked or referenced. Use the returned bref
5344 * to determine if the search exhausted or not. Iterate if the base find
5345 * is chosen but matches a deleted chain.
5347 * WARNING! Must be called with parent's spinlock held. Spinlock remains
5348 * held through the operation.
5351 hammer2_combined_find(hammer2_chain_t *parent,
5352 hammer2_blockref_t *base, int count,
5353 hammer2_key_t *key_nextp,
5354 hammer2_key_t key_beg, hammer2_key_t key_end,
5355 hammer2_blockref_t **bresp)
5357 hammer2_blockref_t *bref;
5358 hammer2_chain_t *chain;
5362 * Lookup in block array and in rbtree.
5364 *key_nextp = key_end + 1;
5365 i = hammer2_base_find(parent, base, count, key_nextp,
5367 chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
5372 if (i == count && chain == NULL) {
5378 * Only chain matched.
5381 bref = &chain->bref;
5386 * Only blockref matched.
5388 if (chain == NULL) {
5394 * Both in-memory and blockref matched, select the nearer element.
5396 * If both are flush with the left-hand side or both are the
5397 * same distance away, select the chain. In this situation the
5398 * chain must have been loaded from the matching blockmap.
5400 if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
5401 chain->bref.key == base[i].key) {
5402 KKASSERT(chain->bref.key == base[i].key);
5403 bref = &chain->bref;
5408 * Select the nearer key
5410 if (chain->bref.key < base[i].key) {
5411 bref = &chain->bref;
5418 * If the bref is out of bounds we've exhausted our search.
5421 if (bref->key > key_end) {
5431 * Locate the specified block array element and delete it. The element
5434 * The spin lock on the related chain must be held.
5436 * NOTE: live_count was adjusted when the chain was deleted, so it does not
5437 * need to be adjusted when we commit the media change.
5440 hammer2_base_delete(hammer2_chain_t *parent,
5441 hammer2_blockref_t *base, int count,
5442 hammer2_chain_t *chain,
5443 hammer2_blockref_t *obref)
5445 hammer2_blockref_t *elm = &chain->bref;
5446 hammer2_blockref_t *scan;
5447 hammer2_key_t key_next;
5451 * Delete element. Expect the element to exist.
5453 * XXX see caller, flush code not yet sophisticated enough to prevent
5454 * re-flushed in some cases.
5456 key_next = 0; /* max range */
5457 i = hammer2_base_find(parent, base, count, &key_next,
5458 elm->key, elm->key);
5460 if (i == count || scan->type == HAMMER2_BREF_TYPE_EMPTY ||
5461 scan->key != elm->key ||
5462 ((chain->flags & HAMMER2_CHAIN_BMAPUPD) == 0 &&
5463 scan->keybits != elm->keybits)) {
5464 hammer2_spin_unex(&parent->core.spin);
5465 panic("delete base %p element not found at %d/%d elm %p\n",
5466 base, i, count, elm);
5471 * Update stats and zero the entry.
5473 * NOTE: Handle radix == 0 (0 bytes) case.
5475 if ((int)(scan->data_off & HAMMER2_OFF_MASK_RADIX)) {
5476 parent->bref.embed.stats.data_count -= (hammer2_off_t)1 <<
5477 (int)(scan->data_off & HAMMER2_OFF_MASK_RADIX);
5479 switch(scan->type) {
5480 case HAMMER2_BREF_TYPE_INODE:
5481 --parent->bref.embed.stats.inode_count;
5483 case HAMMER2_BREF_TYPE_DATA:
5484 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5485 atomic_set_int(&chain->flags,
5486 HAMMER2_CHAIN_HINT_LEAF_COUNT);
5488 if (parent->bref.leaf_count)
5489 --parent->bref.leaf_count;
5492 case HAMMER2_BREF_TYPE_INDIRECT:
5493 if (scan->type != HAMMER2_BREF_TYPE_DATA) {
5494 parent->bref.embed.stats.data_count -=
5495 scan->embed.stats.data_count;
5496 parent->bref.embed.stats.inode_count -=
5497 scan->embed.stats.inode_count;
5499 if (scan->type == HAMMER2_BREF_TYPE_INODE)
5501 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5502 atomic_set_int(&chain->flags,
5503 HAMMER2_CHAIN_HINT_LEAF_COUNT);
5505 if (parent->bref.leaf_count <= scan->leaf_count)
5506 parent->bref.leaf_count = 0;
5508 parent->bref.leaf_count -= scan->leaf_count;
5511 case HAMMER2_BREF_TYPE_DIRENT:
5512 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5513 atomic_set_int(&chain->flags,
5514 HAMMER2_CHAIN_HINT_LEAF_COUNT);
5516 if (parent->bref.leaf_count)
5517 --parent->bref.leaf_count;
5525 bzero(scan, sizeof(*scan));
5528 * We can only optimize parent->core.live_zero for live chains.
5530 if (parent->core.live_zero == i + 1) {
5531 while (--i >= 0 && base[i].type == HAMMER2_BREF_TYPE_EMPTY)
5533 parent->core.live_zero = i + 1;
5537 * Clear appropriate blockmap flags in chain.
5539 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED |
5540 HAMMER2_CHAIN_BMAPUPD);
5544 * Insert the specified element. The block array must not already have the
5545 * element and must have space available for the insertion.
5547 * The spin lock on the related chain must be held.
5549 * NOTE: live_count was adjusted when the chain was deleted, so it does not
5550 * need to be adjusted when we commit the media change.
5553 hammer2_base_insert(hammer2_chain_t *parent,
5554 hammer2_blockref_t *base, int count,
5555 hammer2_chain_t *chain, hammer2_blockref_t *elm)
5557 hammer2_key_t key_next;
5566 * Insert new element. Expect the element to not already exist
5567 * unless we are replacing it.
5569 * XXX see caller, flush code not yet sophisticated enough to prevent
5570 * re-flushed in some cases.
5572 key_next = 0; /* max range */
5573 i = hammer2_base_find(parent, base, count, &key_next,
5574 elm->key, elm->key);
5577 * Shortcut fill optimization, typical ordered insertion(s) may not
5580 KKASSERT(i >= 0 && i <= count);
5583 * Set appropriate blockmap flags in chain (if not NULL)
5586 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
5589 * Update stats and zero the entry
5591 if ((int)(elm->data_off & HAMMER2_OFF_MASK_RADIX)) {
5592 parent->bref.embed.stats.data_count += (hammer2_off_t)1 <<
5593 (int)(elm->data_off & HAMMER2_OFF_MASK_RADIX);
5596 case HAMMER2_BREF_TYPE_INODE:
5597 ++parent->bref.embed.stats.inode_count;
5599 case HAMMER2_BREF_TYPE_DATA:
5600 if (parent->bref.leaf_count != HAMMER2_BLOCKREF_LEAF_MAX)
5601 ++parent->bref.leaf_count;
5603 case HAMMER2_BREF_TYPE_INDIRECT:
5604 if (elm->type != HAMMER2_BREF_TYPE_DATA) {
5605 parent->bref.embed.stats.data_count +=
5606 elm->embed.stats.data_count;
5607 parent->bref.embed.stats.inode_count +=
5608 elm->embed.stats.inode_count;
5610 if (elm->type == HAMMER2_BREF_TYPE_INODE)
5612 if (parent->bref.leaf_count + elm->leaf_count <
5613 HAMMER2_BLOCKREF_LEAF_MAX) {
5614 parent->bref.leaf_count += elm->leaf_count;
5616 parent->bref.leaf_count = HAMMER2_BLOCKREF_LEAF_MAX;
5619 case HAMMER2_BREF_TYPE_DIRENT:
5620 if (parent->bref.leaf_count != HAMMER2_BLOCKREF_LEAF_MAX)
5621 ++parent->bref.leaf_count;
5629 * We can only optimize parent->core.live_zero for live chains.
5631 if (i == count && parent->core.live_zero < count) {
5632 i = parent->core.live_zero++;
5637 xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
5638 if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
5639 hammer2_spin_unex(&parent->core.spin);
5640 panic("insert base %p overlapping elements at %d elm %p\n",
5645 * Try to find an empty slot before or after.
5649 while (j > 0 || k < count) {
5651 if (j >= 0 && base[j].type == HAMMER2_BREF_TYPE_EMPTY) {
5655 bcopy(&base[j+1], &base[j],
5656 (i - j - 1) * sizeof(*base));
5662 if (k < count && base[k].type == HAMMER2_BREF_TYPE_EMPTY) {
5663 bcopy(&base[i], &base[i+1],
5664 (k - i) * sizeof(hammer2_blockref_t));
5668 * We can only update parent->core.live_zero for live
5671 if (parent->core.live_zero <= k)
5672 parent->core.live_zero = k + 1;
5677 panic("hammer2_base_insert: no room!");
5684 for (l = 0; l < count; ++l) {
5685 if (base[l].type != HAMMER2_BREF_TYPE_EMPTY) {
5686 key_next = base[l].key +
5687 ((hammer2_key_t)1 << base[l].keybits) - 1;
5691 while (++l < count) {
5692 if (base[l].type != HAMMER2_BREF_TYPE_EMPTY) {
5693 if (base[l].key <= key_next)
5694 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
5695 key_next = base[l].key +
5696 ((hammer2_key_t)1 << base[l].keybits) - 1;
5706 * Sort the blockref array for the chain. Used by the flush code to
5707 * sort the blockref[] array.
5709 * The chain must be exclusively locked AND spin-locked.
5711 typedef hammer2_blockref_t *hammer2_blockref_p;
5715 hammer2_base_sort_callback(const void *v1, const void *v2)
5717 hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
5718 hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
5721 * Make sure empty elements are placed at the end of the array
5723 if (bref1->type == HAMMER2_BREF_TYPE_EMPTY) {
5724 if (bref2->type == HAMMER2_BREF_TYPE_EMPTY)
5727 } else if (bref2->type == HAMMER2_BREF_TYPE_EMPTY) {
5734 if (bref1->key < bref2->key)
5736 if (bref1->key > bref2->key)
5742 hammer2_base_sort(hammer2_chain_t *chain)
5744 hammer2_blockref_t *base;
5747 switch(chain->bref.type) {
5748 case HAMMER2_BREF_TYPE_INODE:
5750 * Special shortcut for embedded data returns the inode
5751 * itself. Callers must detect this condition and access
5752 * the embedded data (the strategy code does this for us).
5754 * This is only applicable to regular files and softlinks.
5756 if (chain->data->ipdata.meta.op_flags &
5757 HAMMER2_OPFLAG_DIRECTDATA) {
5760 base = &chain->data->ipdata.u.blockset.blockref[0];
5761 count = HAMMER2_SET_COUNT;
5763 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
5764 case HAMMER2_BREF_TYPE_INDIRECT:
5766 * Optimize indirect blocks in the INITIAL state to avoid
5769 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
5770 base = &chain->data->npdata[0];
5771 count = chain->bytes / sizeof(hammer2_blockref_t);
5773 case HAMMER2_BREF_TYPE_VOLUME:
5774 base = &chain->data->voldata.sroot_blockset.blockref[0];
5775 count = HAMMER2_SET_COUNT;
5777 case HAMMER2_BREF_TYPE_FREEMAP:
5778 base = &chain->data->blkset.blockref[0];
5779 count = HAMMER2_SET_COUNT;
5782 kprintf("hammer2_base_sort: unrecognized "
5783 "blockref(A) type: %d",
5786 tsleep(&base, 0, "dead", 0);
5787 panic("hammer2_base_sort: unrecognized "
5788 "blockref(A) type: %d",
5790 base = NULL; /* safety */
5791 count = 0; /* safety */
5793 kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
5799 * Chain memory management
5802 hammer2_chain_wait(hammer2_chain_t *chain)
5804 tsleep(chain, 0, "chnflw", 1);
5807 const hammer2_media_data_t *
5808 hammer2_chain_rdata(hammer2_chain_t *chain)
5810 KKASSERT(chain->data != NULL);
5811 return (chain->data);
5814 hammer2_media_data_t *
5815 hammer2_chain_wdata(hammer2_chain_t *chain)
5817 KKASSERT(chain->data != NULL);
5818 return (chain->data);
5822 * Set the check data for a chain. This can be a heavy-weight operation
5823 * and typically only runs on-flush. For file data check data is calculated
5824 * when the logical buffers are flushed.
5827 hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata)
5829 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_NOTTESTED);
5831 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
5832 case HAMMER2_CHECK_NONE:
5834 case HAMMER2_CHECK_DISABLED:
5836 case HAMMER2_CHECK_ISCSI32:
5837 chain->bref.check.iscsi32.value =
5838 hammer2_icrc32(bdata, chain->bytes);
5840 case HAMMER2_CHECK_XXHASH64:
5841 chain->bref.check.xxhash64.value =
5842 XXH64(bdata, chain->bytes, XXH_HAMMER2_SEED);
5844 case HAMMER2_CHECK_SHA192:
5846 SHA256_CTX hash_ctx;
5848 uint8_t digest[SHA256_DIGEST_LENGTH];
5849 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
5852 SHA256_Init(&hash_ctx);
5853 SHA256_Update(&hash_ctx, bdata, chain->bytes);
5854 SHA256_Final(u.digest, &hash_ctx);
5855 u.digest64[2] ^= u.digest64[3];
5857 chain->bref.check.sha192.data,
5858 sizeof(chain->bref.check.sha192.data));
5861 case HAMMER2_CHECK_FREEMAP:
5862 chain->bref.check.freemap.icrc32 =
5863 hammer2_icrc32(bdata, chain->bytes);
5866 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
5867 chain->bref.methods);
5873 * Characterize a failed check code and try to trace back to the inode.
5876 hammer2_characterize_failed_chain(hammer2_chain_t *chain, uint64_t check,
5879 hammer2_chain_t *lchain;
5880 hammer2_chain_t *ochain;
5883 did = krateprintf(&krate_h2chk,
5884 "chain %016jx.%02x (%s) meth=%02x CHECK FAIL "
5885 "(flags=%08x, bref/data ",
5886 chain->bref.data_off,
5888 hammer2_bref_type_str(chain->bref.type),
5889 chain->bref.methods,
5895 kprintf("%08x/%08x)\n",
5896 chain->bref.check.iscsi32.value,
5899 kprintf("%016jx/%016jx)\n",
5900 chain->bref.check.xxhash64.value,
5905 * Run up the chains to try to find the governing inode so we
5908 * XXX This error reporting is not really MPSAFE
5912 while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
5914 chain = chain->parent;
5917 if (chain && chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
5918 ((chain->bref.flags & HAMMER2_BREF_FLAG_PFSROOT) == 0 ||
5919 (lchain->bref.key & HAMMER2_DIRHASH_VISIBLE))) {
5920 kprintf(" Resides at/in inode %ld\n",
5922 } else if (chain && chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
5923 kprintf(" Resides in inode index - CRITICAL!!!\n");
5925 kprintf(" Resides in root index - CRITICAL!!!\n");
5928 const char *pfsname = "UNKNOWN";
5932 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
5933 if (ochain->pmp->pfs_hmps[i] == ochain->hmp &&
5934 ochain->pmp->pfs_names[i]) {
5935 pfsname = ochain->pmp->pfs_names[i];
5940 kprintf(" In pfs %s on device %s\n",
5941 pfsname, ochain->hmp->devrepname);
5946 * Returns non-zero on success, 0 on failure.
5949 hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata)
5955 if (chain->flags & HAMMER2_CHAIN_NOTTESTED)
5958 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
5959 case HAMMER2_CHECK_NONE:
5962 case HAMMER2_CHECK_DISABLED:
5965 case HAMMER2_CHECK_ISCSI32:
5966 check32 = hammer2_icrc32(bdata, chain->bytes);
5967 r = (chain->bref.check.iscsi32.value == check32);
5969 hammer2_characterize_failed_chain(chain, check32, 32);
5971 hammer2_process_icrc32 += chain->bytes;
5973 case HAMMER2_CHECK_XXHASH64:
5974 check64 = XXH64(bdata, chain->bytes, XXH_HAMMER2_SEED);
5975 r = (chain->bref.check.xxhash64.value == check64);
5977 hammer2_characterize_failed_chain(chain, check64, 64);
5979 hammer2_process_xxhash64 += chain->bytes;
5981 case HAMMER2_CHECK_SHA192:
5983 SHA256_CTX hash_ctx;
5985 uint8_t digest[SHA256_DIGEST_LENGTH];
5986 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
5989 SHA256_Init(&hash_ctx);
5990 SHA256_Update(&hash_ctx, bdata, chain->bytes);
5991 SHA256_Final(u.digest, &hash_ctx);
5992 u.digest64[2] ^= u.digest64[3];
5994 chain->bref.check.sha192.data,
5995 sizeof(chain->bref.check.sha192.data)) == 0) {
5999 krateprintf(&krate_h2chk,
6000 "chain %016jx.%02x meth=%02x "
6002 chain->bref.data_off,
6004 chain->bref.methods);
6008 case HAMMER2_CHECK_FREEMAP:
6009 r = (chain->bref.check.freemap.icrc32 ==
6010 hammer2_icrc32(bdata, chain->bytes));
6014 did = krateprintf(&krate_h2chk,
6015 "chain %016jx.%02x meth=%02x "
6017 chain->bref.data_off,
6019 chain->bref.methods);
6021 kprintf("freemap.icrc %08x icrc32 %08x (%d)\n",
6022 chain->bref.check.freemap.icrc32,
6023 hammer2_icrc32(bdata, chain->bytes),
6026 kprintf("dio %p buf %016jx,%d "
6029 chain->dio->bp->b_loffset,
6030 chain->dio->bp->b_bufsize,
6032 chain->dio->bp->b_data);
6038 kprintf("hammer2_chain_testcheck: unknown check type %02x\n",
6039 chain->bref.methods);
6047 * Acquire the chain and parent representing the specified inode for the
6048 * device at the specified cluster index.
6050 * The flags passed in are LOOKUP flags, not RESOLVE flags.
6052 * If we are unable to locate the inode, HAMMER2_ERROR_EIO is returned and
6053 * *chainp will be NULL. *parentp may still be set error or not, or NULL
6054 * if the parent itself could not be resolved.
6056 * The caller may pass-in a locked *parentp and/or *chainp, or neither.
6057 * They will be unlocked and released by this function. The *parentp and
6058 * *chainp representing the located inode are returned locked.
6061 hammer2_chain_inode_find(hammer2_pfs_t *pmp, hammer2_key_t inum,
6062 int clindex, int flags,
6063 hammer2_chain_t **parentp, hammer2_chain_t **chainp)
6065 hammer2_chain_t *parent;
6066 hammer2_chain_t *rchain;
6067 hammer2_key_t key_dummy;
6068 hammer2_inode_t *ip;
6072 resolve_flags = (flags & HAMMER2_LOOKUP_SHARED) ?
6073 HAMMER2_RESOLVE_SHARED : 0;
6076 * Caller expects us to replace these.
6079 hammer2_chain_unlock(*chainp);
6080 hammer2_chain_drop(*chainp);
6084 hammer2_chain_unlock(*parentp);
6085 hammer2_chain_drop(*parentp);
6090 * Be very careful, this is a backend function and we CANNOT
6091 * lock any frontend inode structure we find. But we have to
6092 * look the inode up this way first in case it exists but is
6093 * detached from the radix tree.
6095 ip = hammer2_inode_lookup(pmp, inum);
6097 *chainp = hammer2_inode_chain_and_parent(ip, clindex,
6100 hammer2_inode_drop(ip);
6103 hammer2_chain_unlock(*chainp);
6104 hammer2_chain_drop(*chainp);
6107 hammer2_chain_unlock(*parentp);
6108 hammer2_chain_drop(*parentp);
6114 * Inodes hang off of the iroot (bit 63 is clear, differentiating
6115 * inodes from root directory entries in the key lookup).
6117 parent = hammer2_inode_chain(pmp->iroot, clindex, resolve_flags);
6120 rchain = hammer2_chain_lookup(&parent, &key_dummy,
6124 error = HAMMER2_ERROR_EIO;
6133 * Used by the bulkscan code to snapshot the synchronized storage for
6134 * a volume, allowing it to be scanned concurrently against normal
6138 hammer2_chain_bulksnap(hammer2_dev_t *hmp)
6140 hammer2_chain_t *copy;
6142 copy = hammer2_chain_alloc(hmp, hmp->spmp, &hmp->vchain.bref);
6143 copy->data = kmalloc(sizeof(copy->data->voldata),
6146 hammer2_voldata_lock(hmp);
6147 copy->data->voldata = hmp->volsync;
6148 hammer2_voldata_unlock(hmp);
6154 hammer2_chain_bulkdrop(hammer2_chain_t *copy)
6156 KKASSERT(copy->bref.type == HAMMER2_BREF_TYPE_VOLUME);
6157 KKASSERT(copy->data);
6158 kfree(copy->data, copy->hmp->mchain);
6160 atomic_add_long(&hammer2_chain_allocs, -1);
6161 hammer2_chain_drop(copy);
6165 * Returns non-zero if the chain (INODE or DIRENT) matches the
6169 hammer2_chain_dirent_test(hammer2_chain_t *chain, const char *name,
6172 const hammer2_inode_data_t *ripdata;
6173 const hammer2_dirent_head_t *den;
6175 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
6176 ripdata = &chain->data->ipdata;
6177 if (ripdata->meta.name_len == name_len &&
6178 bcmp(ripdata->filename, name, name_len) == 0) {
6182 if (chain->bref.type == HAMMER2_BREF_TYPE_DIRENT &&
6183 chain->bref.embed.dirent.namlen == name_len) {
6184 den = &chain->bref.embed.dirent;
6185 if (name_len > sizeof(chain->bref.check.buf) &&
6186 bcmp(chain->data->buf, name, name_len) == 0) {
6189 if (name_len <= sizeof(chain->bref.check.buf) &&
6190 bcmp(chain->bref.check.buf, name, name_len) == 0) {