2 * Copyright (c) 2011-2014 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>
65 static int hammer2_indirect_optimize; /* XXX SYSCTL */
67 static hammer2_chain_t *hammer2_chain_create_indirect(
68 hammer2_trans_t *trans, hammer2_chain_t *parent,
69 hammer2_key_t key, int keybits, int for_type, int *errorp);
70 static void hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop);
71 static hammer2_chain_t *hammer2_combined_find(
72 hammer2_chain_t *parent,
73 hammer2_blockref_t *base, int count,
74 int *cache_indexp, hammer2_key_t *key_nextp,
75 hammer2_key_t key_beg, hammer2_key_t key_end,
76 hammer2_blockref_t **bresp);
79 * Basic RBTree for chains (core->rbtree and core->dbtree). Chains cannot
80 * overlap in the RB trees. Deleted chains are moved from rbtree to either
83 * Chains in delete-duplicate sequences can always iterate through core_entry
84 * to locate the live version of the chain.
86 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
89 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
97 * Compare chains. Overlaps are not supposed to happen and catch
98 * any software issues early we count overlaps as a match.
100 c1_beg = chain1->bref.key;
101 c1_end = c1_beg + ((hammer2_key_t)1 << chain1->bref.keybits) - 1;
102 c2_beg = chain2->bref.key;
103 c2_end = c2_beg + ((hammer2_key_t)1 << chain2->bref.keybits) - 1;
105 if (c1_end < c2_beg) /* fully to the left */
107 if (c1_beg > c2_end) /* fully to the right */
109 return(0); /* overlap (must not cross edge boundary) */
114 hammer2_isclusterable(hammer2_chain_t *chain)
116 if (hammer2_cluster_enable) {
117 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
118 chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
119 chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
127 * Make a chain visible to the flusher. The flusher needs to be able to
128 * do flushes of a subdirectory chains or single files so it does a top-down
129 * recursion using the ONFLUSH flag for the recursion. It locates MODIFIED
130 * or UPDATE chains and flushes back up the chain to the root.
133 hammer2_chain_setflush(hammer2_trans_t *trans, hammer2_chain_t *chain)
135 hammer2_chain_t *parent;
137 if ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
138 spin_lock(&chain->core.cst.spin);
139 while ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
140 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH);
141 if ((parent = chain->parent) == NULL)
143 spin_lock(&parent->core.cst.spin);
144 spin_unlock(&chain->core.cst.spin);
147 spin_unlock(&chain->core.cst.spin);
152 * Allocate a new disconnected chain element representing the specified
153 * bref. chain->refs is set to 1 and the passed bref is copied to
154 * chain->bref. chain->bytes is derived from the bref.
156 * chain->core is NOT allocated and the media data and bp pointers are left
157 * NULL. The caller must call chain_core_alloc() to allocate or associate
158 * a core with the chain.
160 * chain->pmp inherits pmp unless the chain is an inode (other than the
163 * NOTE: Returns a referenced but unlocked (because there is no core) chain.
166 hammer2_chain_alloc(hammer2_mount_t *hmp, hammer2_pfsmount_t *pmp,
167 hammer2_trans_t *trans, hammer2_blockref_t *bref)
169 hammer2_chain_t *chain;
170 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
173 * Construct the appropriate system structure.
176 case HAMMER2_BREF_TYPE_INODE:
177 case HAMMER2_BREF_TYPE_INDIRECT:
178 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
179 case HAMMER2_BREF_TYPE_DATA:
180 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
182 * Chain's are really only associated with the hmp but we
183 * maintain a pmp association for per-mount memory tracking
184 * purposes. The pmp can be NULL.
186 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
188 case HAMMER2_BREF_TYPE_VOLUME:
189 case HAMMER2_BREF_TYPE_FREEMAP:
191 panic("hammer2_chain_alloc volume type illegal for op");
194 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
199 * Initialize the new chain structure.
204 chain->bytes = bytes;
206 chain->flags = HAMMER2_CHAIN_ALLOCATED;
209 * Set the PFS boundary flag if this chain represents a PFS root.
211 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
212 chain->flags |= HAMMER2_CHAIN_PFSBOUNDARY;
218 * Associate an existing core with the chain or allocate a new core.
220 * The core is not locked. No additional refs on the chain are made.
221 * (trans) must not be NULL if (core) is not NULL.
223 * When chains are delete-duplicated during flushes we insert nchain on
224 * the ownerq after ochain instead of at the end in order to give the
225 * drop code visibility in the correct order, otherwise drops can be missed.
228 hammer2_chain_core_alloc(hammer2_trans_t *trans, hammer2_chain_t *chain)
230 hammer2_chain_core_t *core = &chain->core;
233 * Fresh core under nchain (no multi-homing of ochain's
236 RB_INIT(&core->rbtree); /* live chains */
237 ccms_cst_init(&core->cst, chain);
241 * Add a reference to a chain element, preventing its destruction.
243 * (can be called with spinlock held)
246 hammer2_chain_ref(hammer2_chain_t *chain)
248 atomic_add_int(&chain->refs, 1);
252 * Insert the chain in the core rbtree.
254 * Normal insertions are placed in the live rbtree. Insertion of a deleted
255 * chain is a special case used by the flush code that is placed on the
256 * unstaged deleted list to avoid confusing the live view.
258 #define HAMMER2_CHAIN_INSERT_SPIN 0x0001
259 #define HAMMER2_CHAIN_INSERT_LIVE 0x0002
260 #define HAMMER2_CHAIN_INSERT_RACE 0x0004
264 hammer2_chain_insert(hammer2_chain_t *parent, hammer2_chain_t *chain,
265 int flags, int generation)
267 hammer2_chain_t *xchain;
270 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
271 spin_lock(&parent->core.cst.spin);
274 * Interlocked by spinlock, check for race
276 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
277 parent->core.generation != generation) {
285 xchain = RB_INSERT(hammer2_chain_tree, &parent->core.rbtree, chain);
286 KASSERT(xchain == NULL,
287 ("hammer2_chain_insert: collision %p %p", chain, xchain));
288 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
289 chain->parent = parent;
290 ++parent->core.chain_count;
291 ++parent->core.generation; /* XXX incs for _get() too, XXX */
294 * We have to keep track of the effective live-view blockref count
295 * so the create code knows when to push an indirect block.
297 if (flags & HAMMER2_CHAIN_INSERT_LIVE)
298 atomic_add_int(&parent->core.live_count, 1);
300 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
301 spin_unlock(&parent->core.cst.spin);
306 * Drop the caller's reference to the chain. When the ref count drops to
307 * zero this function will try to disassociate the chain from its parent and
308 * deallocate it, then recursely drop the parent using the implied ref
309 * from the chain's chain->parent.
311 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain);
314 hammer2_chain_drop(hammer2_chain_t *chain)
319 if (hammer2_debug & 0x200000)
322 if (chain->flags & HAMMER2_CHAIN_UPDATE)
324 if (chain->flags & HAMMER2_CHAIN_MODIFIED)
326 KKASSERT(chain->refs > need);
334 chain = hammer2_chain_lastdrop(chain);
336 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
338 /* retry the same chain */
344 * Safe handling of the 1->0 transition on chain. Returns a chain for
345 * recursive drop or NULL, possibly returning the same chain if the atomic
348 * Whem two chains need to be recursively dropped we use the chain
349 * we would otherwise free to placehold the additional chain. It's a bit
350 * convoluted but we can't just recurse without potentially blowing out
353 * The chain cannot be freed if it has a non-empty core (children) or
354 * it is not at the head of ownerq.
356 * The cst spinlock is allowed nest child-to-parent (not parent-to-child).
360 hammer2_chain_lastdrop(hammer2_chain_t *chain)
362 hammer2_pfsmount_t *pmp;
363 hammer2_mount_t *hmp;
364 hammer2_chain_t *parent;
365 hammer2_chain_t *rdrop;
368 * Spinlock the core and check to see if it is empty. If it is
369 * not empty we leave chain intact with refs == 0. The elements
370 * in core->rbtree are associated with other chains contemporary
371 * with ours but not with our chain directly.
373 spin_lock(&chain->core.cst.spin);
376 * We can't free non-stale chains with children until we are
377 * able to free the children because there might be a flush
378 * dependency. Flushes of stale children (which should also
379 * have their deleted flag set) short-cut recursive flush
380 * dependencies and can be freed here. Any flushes which run
381 * through stale children due to the flush synchronization
382 * point should have a FLUSH_* bit set in the chain and not
383 * reach lastdrop at this time.
385 * NOTE: We return (chain) on failure to retry.
387 if (chain->core.chain_count) {
388 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
389 spin_unlock(&chain->core.cst.spin);
390 chain = NULL; /* success */
392 spin_unlock(&chain->core.cst.spin);
396 /* no chains left under us */
399 * chain->core has no children left so no accessors can get to our
400 * chain from there. Now we have to lock the parent core to interlock
401 * remaining possible accessors that might bump chain's refs before
402 * we can safely drop chain's refs with intent to free the chain.
405 pmp = chain->pmp; /* can be NULL */
409 * Spinlock the parent and try to drop the last ref on chain.
410 * On success remove chain from its parent, otherwise return NULL.
412 * (normal core locks are top-down recursive but we define core
413 * spinlocks as bottom-up recursive, so this is safe).
415 if ((parent = chain->parent) != NULL) {
416 spin_lock(&parent->core.cst.spin);
417 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
418 /* 1->0 transition failed */
419 spin_unlock(&parent->core.cst.spin);
420 spin_unlock(&chain->core.cst.spin);
421 return(chain); /* retry */
425 * 1->0 transition successful, remove chain from its
428 if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
429 RB_REMOVE(hammer2_chain_tree,
430 &parent->core.rbtree, chain);
431 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
432 --parent->core.chain_count;
433 chain->parent = NULL;
437 * If our chain was the last chain in the parent's core the
438 * core is now empty and its parent might have to be
439 * re-dropped if it has 0 refs.
441 if (parent->core.chain_count == 0) {
443 if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0) {
447 spin_unlock(&parent->core.cst.spin);
448 parent = NULL; /* safety */
452 * Successful 1->0 transition and the chain can be destroyed now.
454 * We still have the core spinlock, and core's chain_count is 0.
455 * Any parent spinlock is gone.
457 spin_unlock(&chain->core.cst.spin);
458 KKASSERT(RB_EMPTY(&chain->core.rbtree) &&
459 chain->core.chain_count == 0);
460 KKASSERT(chain->core.cst.count == 0);
461 KKASSERT(chain->core.cst.upgrade == 0);
464 * All spin locks are gone, finish freeing stuff.
466 KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE |
467 HAMMER2_CHAIN_MODIFIED)) == 0);
468 hammer2_chain_drop_data(chain, 1);
470 KKASSERT(chain->dio == NULL);
473 * Once chain resources are gone we can use the now dead chain
474 * structure to placehold what might otherwise require a recursive
475 * drop, because we have potentially two things to drop and can only
476 * return one directly.
478 if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
479 chain->flags &= ~HAMMER2_CHAIN_ALLOCATED;
481 kfree(chain, hmp->mchain);
485 * Possible chaining loop when parent re-drop needed.
491 * On either last lock release or last drop
494 hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop)
496 /*hammer2_mount_t *hmp = chain->hmp;*/
498 switch(chain->bref.type) {
499 case HAMMER2_BREF_TYPE_VOLUME:
500 case HAMMER2_BREF_TYPE_FREEMAP:
505 KKASSERT(chain->data == NULL);
511 * Ref and lock a chain element, acquiring its data with I/O if necessary,
512 * and specify how you would like the data to be resolved.
514 * Returns 0 on success or an error code if the data could not be acquired.
515 * The chain element is locked on return regardless of whether an error
518 * The lock is allowed to recurse, multiple locking ops will aggregate
519 * the requested resolve types. Once data is assigned it will not be
520 * removed until the last unlock.
522 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
523 * (typically used to avoid device/logical buffer
526 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
527 * the INITIAL-create state (indirect blocks only).
529 * Do not resolve data elements for DATA chains.
530 * (typically used to avoid device/logical buffer
533 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
535 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
536 * it will be locked exclusive.
538 * NOTE: Embedded elements (volume header, inodes) are always resolved
541 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
542 * element will instantiate and zero its buffer, and flush it on
545 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
546 * so as not to instantiate a device buffer, which could alias against
547 * a logical file buffer. However, if ALWAYS is specified the
548 * device buffer will be instantiated anyway.
550 * WARNING! If data must be fetched a shared lock will temporarily be
551 * upgraded to exclusive. However, a deadlock can occur if
552 * the caller owns more than one shared lock.
555 hammer2_chain_lock(hammer2_chain_t *chain, int how)
557 hammer2_mount_t *hmp;
558 hammer2_blockref_t *bref;
564 * Ref and lock the element. Recursive locks are allowed.
566 if ((how & HAMMER2_RESOLVE_NOREF) == 0)
567 hammer2_chain_ref(chain);
568 atomic_add_int(&chain->lockcnt, 1);
571 KKASSERT(hmp != NULL);
574 * Get the appropriate lock.
576 if (how & HAMMER2_RESOLVE_SHARED)
577 ccms_thread_lock(&chain->core.cst, CCMS_STATE_SHARED);
579 ccms_thread_lock(&chain->core.cst, CCMS_STATE_EXCLUSIVE);
582 * If we already have a valid data pointer no further action is
589 * Do we have to resolve the data?
591 switch(how & HAMMER2_RESOLVE_MASK) {
592 case HAMMER2_RESOLVE_NEVER:
594 case HAMMER2_RESOLVE_MAYBE:
595 if (chain->flags & HAMMER2_CHAIN_INITIAL)
597 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
600 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
603 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
606 case HAMMER2_RESOLVE_ALWAYS:
611 * Upgrade to an exclusive lock so we can safely manipulate the
612 * buffer cache. If another thread got to it before us we
615 ostate = ccms_thread_lock_upgrade(&chain->core.cst);
617 ccms_thread_lock_downgrade(&chain->core.cst, ostate);
622 * We must resolve to a device buffer, either by issuing I/O or
623 * by creating a zero-fill element. We do not mark the buffer
624 * dirty when creating a zero-fill element (the hammer2_chain_modify()
625 * API must still be used to do that).
627 * The device buffer is variable-sized in powers of 2 down
628 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
629 * chunk always contains buffers of the same size. (XXX)
631 * The minimum physical IO size may be larger than the variable
637 * The getblk() optimization can only be used on newly created
638 * elements if the physical block size matches the request.
640 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
641 error = hammer2_io_new(hmp, bref->data_off, chain->bytes,
644 error = hammer2_io_bread(hmp, bref->data_off, chain->bytes,
646 hammer2_adjreadcounter(&chain->bref, chain->bytes);
650 kprintf("hammer2_chain_lock: I/O error %016jx: %d\n",
651 (intmax_t)bref->data_off, error);
652 hammer2_io_bqrelse(&chain->dio);
653 ccms_thread_lock_downgrade(&chain->core.cst, ostate);
659 * No need for this, always require that hammer2_chain_modify()
660 * be called before any modifying operations.
662 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) &&
663 !hammer2_io_isdirty(chain->dio)) {
664 hammer2_io_setdirty(chain->dio);
669 * Clear INITIAL. In this case we used io_new() and the buffer has
670 * been zero'd and marked dirty.
672 bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
673 if (chain->flags & HAMMER2_CHAIN_INITIAL)
674 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
677 * Setup the data pointer, either pointing it to an embedded data
678 * structure and copying the data from the buffer, or pointing it
681 * The buffer is not retained when copying to an embedded data
682 * structure in order to avoid potential deadlocks or recursions
683 * on the same physical buffer.
685 switch (bref->type) {
686 case HAMMER2_BREF_TYPE_VOLUME:
687 case HAMMER2_BREF_TYPE_FREEMAP:
689 * Copy data from bp to embedded buffer
691 panic("hammer2_chain_lock: called on unresolved volume header");
693 case HAMMER2_BREF_TYPE_INODE:
694 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
695 case HAMMER2_BREF_TYPE_INDIRECT:
696 case HAMMER2_BREF_TYPE_DATA:
697 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
700 * Point data at the device buffer and leave dio intact.
702 chain->data = (void *)bdata;
705 ccms_thread_lock_downgrade(&chain->core.cst, ostate);
710 * This basically calls hammer2_io_breadcb() but does some pre-processing
711 * of the chain first to handle certain cases.
714 hammer2_chain_load_async(hammer2_cluster_t *cluster,
715 void (*callback)(hammer2_io_t *dio,
716 hammer2_cluster_t *cluster,
717 hammer2_chain_t *chain,
718 void *arg_p, off_t arg_o),
721 hammer2_chain_t *chain;
722 hammer2_mount_t *hmp;
723 struct hammer2_io *dio;
724 hammer2_blockref_t *bref;
729 * If no chain specified see if any chain data is available and use
730 * that, otherwise begin an I/O iteration using the first chain.
733 for (i = 0; i < cluster->nchains; ++i) {
734 chain = cluster->array[i];
735 if (chain && chain->data)
738 if (i == cluster->nchains) {
739 chain = cluster->array[0];
744 callback(NULL, cluster, chain, arg_p, (off_t)i);
749 * We must resolve to a device buffer, either by issuing I/O or
750 * by creating a zero-fill element. We do not mark the buffer
751 * dirty when creating a zero-fill element (the hammer2_chain_modify()
752 * API must still be used to do that).
754 * The device buffer is variable-sized in powers of 2 down
755 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
756 * chunk always contains buffers of the same size. (XXX)
758 * The minimum physical IO size may be larger than the variable
765 * The getblk() optimization can only be used on newly created
766 * elements if the physical block size matches the request.
768 if ((chain->flags & HAMMER2_CHAIN_INITIAL) &&
769 chain->bytes == hammer2_devblksize(chain->bytes)) {
770 error = hammer2_io_new(hmp, bref->data_off, chain->bytes, &dio);
771 KKASSERT(error == 0);
772 callback(dio, cluster, chain, arg_p, (off_t)i);
777 * Otherwise issue a read
779 hammer2_adjreadcounter(&chain->bref, chain->bytes);
780 hammer2_io_breadcb(hmp, bref->data_off, chain->bytes,
781 callback, cluster, chain, arg_p, (off_t)i);
785 * Unlock and deref a chain element.
787 * On the last lock release any non-embedded data (chain->dio) will be
791 hammer2_chain_unlock(hammer2_chain_t *chain)
798 * The core->cst lock can be shared across several chains so we
799 * need to track the per-chain lockcnt separately.
801 * If multiple locks are present (or being attempted) on this
802 * particular chain we can just unlock, drop refs, and return.
804 * Otherwise fall-through on the 1->0 transition.
807 lockcnt = chain->lockcnt;
808 KKASSERT(lockcnt > 0);
811 if (atomic_cmpset_int(&chain->lockcnt,
812 lockcnt, lockcnt - 1)) {
813 ccms_thread_unlock(&chain->core.cst);
814 hammer2_chain_drop(chain);
818 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
825 * On the 1->0 transition we upgrade the core lock (if necessary)
826 * to exclusive for terminal processing. If after upgrading we find
827 * that lockcnt is non-zero, another thread is racing us and will
828 * handle the unload for us later on, so just cleanup and return
829 * leaving the data/io intact
831 * Otherwise if lockcnt is still 0 it is possible for it to become
832 * non-zero and race, but since we hold the core->cst lock
833 * exclusively all that will happen is that the chain will be
834 * reloaded after we unload it.
836 ostate = ccms_thread_lock_upgrade(&chain->core.cst);
837 if (chain->lockcnt) {
838 ccms_thread_unlock_upgraded(&chain->core.cst, ostate);
839 hammer2_chain_drop(chain);
844 * Shortcut the case if the data is embedded or not resolved.
846 * Do NOT NULL out chain->data (e.g. inode data), it might be
849 if (chain->dio == NULL) {
850 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0)
851 hammer2_chain_drop_data(chain, 0);
852 ccms_thread_unlock_upgraded(&chain->core.cst, ostate);
853 hammer2_chain_drop(chain);
860 if (hammer2_io_isdirty(chain->dio) == 0) {
862 } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
863 switch(chain->bref.type) {
864 case HAMMER2_BREF_TYPE_DATA:
865 counterp = &hammer2_ioa_file_write;
867 case HAMMER2_BREF_TYPE_INODE:
868 counterp = &hammer2_ioa_meta_write;
870 case HAMMER2_BREF_TYPE_INDIRECT:
871 counterp = &hammer2_ioa_indr_write;
873 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
874 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
875 counterp = &hammer2_ioa_fmap_write;
878 counterp = &hammer2_ioa_volu_write;
881 *counterp += chain->bytes;
883 switch(chain->bref.type) {
884 case HAMMER2_BREF_TYPE_DATA:
885 counterp = &hammer2_iod_file_write;
887 case HAMMER2_BREF_TYPE_INODE:
888 counterp = &hammer2_iod_meta_write;
890 case HAMMER2_BREF_TYPE_INDIRECT:
891 counterp = &hammer2_iod_indr_write;
893 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
894 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
895 counterp = &hammer2_iod_fmap_write;
898 counterp = &hammer2_iod_volu_write;
901 *counterp += chain->bytes;
907 * If a device buffer was used for data be sure to destroy the
908 * buffer when we are done to avoid aliases (XXX what about the
909 * underlying VM pages?).
911 * NOTE: Freemap leaf's use reserved blocks and thus no aliasing
914 * NOTE: The isdirty check tracks whether we have to bdwrite() the
915 * buffer or not. The buffer might already be dirty. The
916 * flag is re-set when chain_modify() is called, even if
917 * MODIFIED is already set, allowing the OS to retire the
918 * buffer independent of a hammer2 flush.
921 if ((chain->flags & HAMMER2_CHAIN_IOFLUSH) &&
922 hammer2_io_isdirty(chain->dio)) {
923 hammer2_io_bawrite(&chain->dio);
925 hammer2_io_bqrelse(&chain->dio);
927 ccms_thread_unlock_upgraded(&chain->core.cst, ostate);
928 hammer2_chain_drop(chain);
932 * This counts the number of live blockrefs in a block array and
933 * also calculates the point at which all remaining blockrefs are empty.
934 * This routine can only be called on a live chain (DUPLICATED flag not set).
936 * NOTE: Flag is not set until after the count is complete, allowing
937 * callers to test the flag without holding the spinlock.
939 * NOTE: If base is NULL the related chain is still in the INITIAL
940 * state and there are no blockrefs to count.
942 * NOTE: live_count may already have some counts accumulated due to
943 * creation and deletion and could even be initially negative.
946 hammer2_chain_countbrefs(hammer2_chain_t *chain,
947 hammer2_blockref_t *base, int count)
949 spin_lock(&chain->core.cst.spin);
950 if ((chain->core.flags & HAMMER2_CORE_COUNTEDBREFS) == 0) {
952 while (--count >= 0) {
953 if (base[count].type)
956 chain->core.live_zero = count + 1;
958 if (base[count].type)
959 atomic_add_int(&chain->core.live_count,
964 chain->core.live_zero = 0;
966 /* else do not modify live_count */
967 atomic_set_int(&chain->core.flags, HAMMER2_CORE_COUNTEDBREFS);
969 spin_unlock(&chain->core.cst.spin);
973 * Resize the chain's physical storage allocation in-place. This will
974 * modify the passed-in chain. Chains can be resized smaller without
975 * reallocating the storage. Resizing larger will reallocate the storage.
976 * Excess or prior storage is reclaimed asynchronously at a later time.
978 * Must be passed an exclusively locked parent and chain.
980 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
981 * to avoid instantiating a device buffer that conflicts with the vnode data
982 * buffer. That is, the passed-in bp is a logical buffer, whereas any
983 * chain-oriented bp would be a device buffer.
985 * XXX return error if cannot resize.
988 hammer2_chain_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
989 hammer2_chain_t *parent, hammer2_chain_t *chain,
990 int nradix, int flags)
992 hammer2_mount_t *hmp;
999 * Only data and indirect blocks can be resized for now.
1000 * (The volu root, inodes, and freemap elements use a fixed size).
1002 KKASSERT(chain != &hmp->vchain);
1003 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1004 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
1007 * Nothing to do if the element is already the proper size
1009 obytes = chain->bytes;
1010 nbytes = 1U << nradix;
1011 if (obytes == nbytes)
1015 * The parent does not have to be locked for the delete/duplicate call,
1016 * but is in this particular code path.
1018 * NOTE: If we are not crossing a synchronization point the
1019 * duplication code will simply reuse the existing chain
1022 * NOTE: The modify will set BMAPUPD for us if BMAPPED is set.
1024 hammer2_chain_modify(trans, chain, 0);
1027 * Relocate the block, even if making it smaller (because different
1028 * block sizes may be in different regions).
1030 * (data blocks only, we aren't copying the storage here).
1032 hammer2_freemap_alloc(trans, chain, nbytes);
1033 chain->bytes = nbytes;
1034 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_FORCECOW);
1035 /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */
1038 * For now just support it on DATA chains (and not on indirect
1041 KKASSERT(chain->dio == NULL);
1047 * REMOVED - see cluster code
1049 * Set a chain modified, making it read-write and duplicating it if necessary.
1050 * This function will assign a new physical block to the chain if necessary
1052 * Duplication of already-modified chains is possible when the modification
1053 * crosses a flush synchronization boundary.
1055 * Non-data blocks - The chain should be locked to at least the RESOLVE_MAYBE
1056 * level or the COW operation will not work.
1058 * Data blocks - The chain is usually locked RESOLVE_NEVER so as not to
1059 * run the data through the device buffers.
1061 * This function may return a different chain than was passed, in which case
1062 * the old chain will be unlocked and the new chain will be locked.
1064 * ip->chain may be adjusted by hammer2_chain_modify_ip().
1066 hammer2_inode_data_t *
1067 hammer2_chain_modify_ip(hammer2_trans_t *trans, hammer2_inode_t *ip,
1068 hammer2_chain_t **chainp, int flags)
1070 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1071 hammer2_chain_modify(trans, chainp, flags);
1072 if (ip->chain != *chainp)
1073 hammer2_inode_repoint(ip, NULL, *chainp);
1075 vsetisdirty(ip->vp);
1076 return(&ip->chain->data->ipdata);
1082 hammer2_chain_modify(hammer2_trans_t *trans, hammer2_chain_t *chain, int flags)
1084 hammer2_mount_t *hmp;
1094 * data is not optional for freemap chains (we must always be sure
1095 * to copy the data on COW storage allocations).
1097 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1098 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1099 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1100 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1104 * Data must be resolved if already assigned unless explicitly
1105 * flagged otherwise.
1107 if (chain->data == NULL && (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1108 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1109 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1110 hammer2_chain_unlock(chain);
1114 * Otherwise do initial-chain handling. Set MODIFIED to indicate
1115 * that the chain has been modified. Set UPDATE to ensure that
1116 * the blockref is updated in the parent.
1118 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1119 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1120 hammer2_chain_ref(chain);
1121 hammer2_pfs_memory_inc(chain->pmp);
1126 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
1127 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1128 hammer2_chain_ref(chain);
1132 * The modification or re-modification requires an allocation and
1135 * We normally always allocate new storage here. If storage exists
1136 * and MODIFY_NOREALLOC is passed in, we do not allocate new storage.
1138 if (chain != &hmp->vchain && chain != &hmp->fchain) {
1139 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1140 ((flags & HAMMER2_MODIFY_NOREALLOC) == 0 && newmod)
1142 hammer2_freemap_alloc(trans, chain, chain->bytes);
1143 /* XXX failed allocation */
1144 } else if (chain->flags & HAMMER2_CHAIN_FORCECOW) {
1145 hammer2_freemap_alloc(trans, chain, chain->bytes);
1146 /* XXX failed allocation */
1148 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_FORCECOW);
1152 * Set BMAPUPD to tell the flush code that an existing blockmap entry
1153 * requires updating as well as to tell the delete code that the
1154 * chain's blockref might not exactly match (in terms of physical size
1155 * or block offset) the one in the parent's blocktable. The base key
1156 * of course will still match.
1158 if (chain->flags & HAMMER2_CHAIN_BMAPPED)
1159 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD);
1162 * Do not COW BREF_TYPE_DATA when OPTDATA is set. This is because
1163 * data modifications are done via the logical buffer cache so COWing
1164 * it here would result in unnecessary extra copies (and possibly extra
1165 * block reallocations). The INITIAL flag remains unchanged in this
1168 * (This is a bit of a hack).
1170 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA &&
1171 (flags & HAMMER2_MODIFY_OPTDATA)) {
1176 * Clearing the INITIAL flag (for indirect blocks) indicates that
1177 * we've processed the uninitialized storage allocation.
1179 * If this flag is already clear we are likely in a copy-on-write
1180 * situation but we have to be sure NOT to bzero the storage if
1181 * no data is present.
1183 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1184 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1191 * Instantiate data buffer and possibly execute COW operation
1193 switch(chain->bref.type) {
1194 case HAMMER2_BREF_TYPE_VOLUME:
1195 case HAMMER2_BREF_TYPE_FREEMAP:
1197 * The data is embedded, no copy-on-write operation is
1200 KKASSERT(chain->dio == NULL);
1202 case HAMMER2_BREF_TYPE_INODE:
1203 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1204 case HAMMER2_BREF_TYPE_DATA:
1205 case HAMMER2_BREF_TYPE_INDIRECT:
1206 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1208 * Perform the copy-on-write operation
1210 * zero-fill or copy-on-write depending on whether
1211 * chain->data exists or not and set the dirty state for
1212 * the new buffer. hammer2_io_new() will handle the
1215 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1218 error = hammer2_io_new(hmp, chain->bref.data_off,
1219 chain->bytes, &dio);
1221 error = hammer2_io_bread(hmp, chain->bref.data_off,
1222 chain->bytes, &dio);
1224 hammer2_adjreadcounter(&chain->bref, chain->bytes);
1225 KKASSERT(error == 0);
1227 bdata = hammer2_io_data(dio, chain->bref.data_off);
1230 KKASSERT(chain->dio != NULL);
1231 if (chain->data != (void *)bdata) {
1232 bcopy(chain->data, bdata, chain->bytes);
1234 } else if (wasinitial == 0) {
1236 * We have a problem. We were asked to COW but
1237 * we don't have any data to COW with!
1239 panic("hammer2_chain_modify: having a COW %p\n",
1244 * Retire the old buffer, replace with the new
1247 hammer2_io_brelse(&chain->dio);
1248 chain->data = (void *)bdata;
1250 hammer2_io_setdirty(dio); /* modified by bcopy above */
1253 panic("hammer2_chain_modify: illegal non-embedded type %d",
1260 * setflush on parent indicating that the parent must recurse down
1261 * to us. Do not call on chain itself which might already have it
1265 hammer2_chain_setflush(trans, chain->parent);
1269 * Volume header data locks
1272 hammer2_voldata_lock(hammer2_mount_t *hmp)
1274 lockmgr(&hmp->vollk, LK_EXCLUSIVE);
1278 hammer2_voldata_unlock(hammer2_mount_t *hmp)
1280 lockmgr(&hmp->vollk, LK_RELEASE);
1284 hammer2_voldata_modify(hammer2_mount_t *hmp)
1286 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1287 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
1288 hammer2_chain_ref(&hmp->vchain);
1289 hammer2_pfs_memory_inc(hmp->vchain.pmp);
1294 * This function returns the chain at the nearest key within the specified
1295 * range. The returned chain will be referenced but not locked.
1297 * This function will recurse through chain->rbtree as necessary and will
1298 * return a *key_nextp suitable for iteration. *key_nextp is only set if
1299 * the iteration value is less than the current value of *key_nextp.
1301 * The caller should use (*key_nextp) to calculate the actual range of
1302 * the returned element, which will be (key_beg to *key_nextp - 1), because
1303 * there might be another element which is superior to the returned element
1306 * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
1307 * chains continue to be returned. On EOF (*key_nextp) may overflow since
1308 * it will wind up being (key_end + 1).
1310 * WARNING! Must be called with child's spinlock held. Spinlock remains
1311 * held through the operation.
1313 struct hammer2_chain_find_info {
1314 hammer2_chain_t *best;
1315 hammer2_key_t key_beg;
1316 hammer2_key_t key_end;
1317 hammer2_key_t key_next;
1320 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
1321 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
1325 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
1326 hammer2_key_t key_beg, hammer2_key_t key_end)
1328 struct hammer2_chain_find_info info;
1331 info.key_beg = key_beg;
1332 info.key_end = key_end;
1333 info.key_next = *key_nextp;
1335 RB_SCAN(hammer2_chain_tree, &parent->core.rbtree,
1336 hammer2_chain_find_cmp, hammer2_chain_find_callback,
1338 *key_nextp = info.key_next;
1340 kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
1341 parent, key_beg, key_end, *key_nextp);
1349 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
1351 struct hammer2_chain_find_info *info = data;
1352 hammer2_key_t child_beg;
1353 hammer2_key_t child_end;
1355 child_beg = child->bref.key;
1356 child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
1358 if (child_end < info->key_beg)
1360 if (child_beg > info->key_end)
1367 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
1369 struct hammer2_chain_find_info *info = data;
1370 hammer2_chain_t *best;
1371 hammer2_key_t child_end;
1374 * WARNING! Do not discard DUPLICATED chains, it is possible that
1375 * we are catching an insertion half-way done. If a
1376 * duplicated chain turns out to be the best choice the
1377 * caller will re-check its flags after locking it.
1379 * WARNING! Layerq is scanned forwards, exact matches should keep
1380 * the existing info->best.
1382 if ((best = info->best) == NULL) {
1384 * No previous best. Assign best
1387 } else if (best->bref.key <= info->key_beg &&
1388 child->bref.key <= info->key_beg) {
1393 /*info->best = child;*/
1394 } else if (child->bref.key < best->bref.key) {
1396 * Child has a nearer key and best is not flush with key_beg.
1397 * Set best to child. Truncate key_next to the old best key.
1400 if (info->key_next > best->bref.key || info->key_next == 0)
1401 info->key_next = best->bref.key;
1402 } else if (child->bref.key == best->bref.key) {
1404 * If our current best is flush with the child then this
1405 * is an illegal overlap.
1407 * key_next will automatically be limited to the smaller of
1408 * the two end-points.
1414 * Keep the current best but truncate key_next to the child's
1417 * key_next will also automatically be limited to the smaller
1418 * of the two end-points (probably not necessary for this case
1419 * but we do it anyway).
1421 if (info->key_next > child->bref.key || info->key_next == 0)
1422 info->key_next = child->bref.key;
1426 * Always truncate key_next based on child's end-of-range.
1428 child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
1429 if (child_end && (info->key_next > child_end || info->key_next == 0))
1430 info->key_next = child_end;
1436 * Retrieve the specified chain from a media blockref, creating the
1437 * in-memory chain structure which reflects it.
1439 * To handle insertion races pass the INSERT_RACE flag along with the
1440 * generation number of the core. NULL will be returned if the generation
1441 * number changes before we have a chance to insert the chain. Insert
1442 * races can occur because the parent might be held shared.
1444 * Caller must hold the parent locked shared or exclusive since we may
1445 * need the parent's bref array to find our block.
1447 * WARNING! chain->pmp is left NULL if the bref represents a PFS mount
1451 hammer2_chain_get(hammer2_chain_t *parent, int generation,
1452 hammer2_blockref_t *bref)
1454 hammer2_mount_t *hmp = parent->hmp;
1455 hammer2_chain_t *chain;
1459 * Allocate a chain structure representing the existing media
1460 * entry. Resulting chain has one ref and is not locked.
1462 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
1463 chain = hammer2_chain_alloc(hmp, NULL, NULL, bref);
1465 chain = hammer2_chain_alloc(hmp, parent->pmp, NULL, bref);
1466 hammer2_chain_core_alloc(NULL, chain);
1467 /* ref'd chain returned */
1470 * Flag that the chain is in the parent's blockmap so delete/flush
1471 * knows what to do with it.
1473 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
1476 * Link the chain into its parent. A spinlock is required to safely
1477 * access the RBTREE, and it is possible to collide with another
1478 * hammer2_chain_get() operation because the caller might only hold
1479 * a shared lock on the parent.
1481 KKASSERT(parent->refs > 0);
1482 error = hammer2_chain_insert(parent, chain,
1483 HAMMER2_CHAIN_INSERT_SPIN |
1484 HAMMER2_CHAIN_INSERT_RACE,
1487 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
1488 kprintf("chain %p get race\n", chain);
1489 hammer2_chain_drop(chain);
1492 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
1496 * Return our new chain referenced but not locked, or NULL if
1503 * Lookup initialization/completion API
1506 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
1508 if (flags & HAMMER2_LOOKUP_SHARED) {
1509 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1510 HAMMER2_RESOLVE_SHARED);
1512 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1518 hammer2_chain_lookup_done(hammer2_chain_t *parent)
1521 hammer2_chain_unlock(parent);
1526 hammer2_chain_getparent(hammer2_chain_t **parentp, int how)
1528 hammer2_chain_t *oparent;
1529 hammer2_chain_t *nparent;
1532 * Be careful of order, oparent must be unlocked before nparent
1533 * is locked below to avoid a deadlock.
1536 spin_lock(&oparent->core.cst.spin);
1537 nparent = oparent->parent;
1538 hammer2_chain_ref(nparent);
1539 spin_unlock(&oparent->core.cst.spin);
1541 hammer2_chain_unlock(oparent);
1545 hammer2_chain_lock(nparent, how | HAMMER2_RESOLVE_NOREF);
1552 * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
1553 * (*parentp) typically points to an inode but can also point to a related
1554 * indirect block and this function will recurse upwards and find the inode
1557 * (*parentp) must be exclusively locked and referenced and can be an inode
1558 * or an existing indirect block within the inode.
1560 * On return (*parentp) will be modified to point at the deepest parent chain
1561 * element encountered during the search, as a helper for an insertion or
1562 * deletion. The new (*parentp) will be locked and referenced and the old
1563 * will be unlocked and dereferenced (no change if they are both the same).
1565 * The matching chain will be returned exclusively locked. If NOLOCK is
1566 * requested the chain will be returned only referenced.
1568 * NULL is returned if no match was found, but (*parentp) will still
1569 * potentially be adjusted.
1571 * On return (*key_nextp) will point to an iterative value for key_beg.
1572 * (If NULL is returned (*key_nextp) is set to key_end).
1574 * This function will also recurse up the chain if the key is not within the
1575 * current parent's range. (*parentp) can never be set to NULL. An iteration
1576 * can simply allow (*parentp) to float inside the loop.
1578 * NOTE! chain->data is not always resolved. By default it will not be
1579 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
1580 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
1581 * BREF_TYPE_DATA as the device buffer can alias the logical file
1585 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
1586 hammer2_key_t key_beg, hammer2_key_t key_end,
1587 int *cache_indexp, int flags, int *ddflagp)
1589 hammer2_mount_t *hmp;
1590 hammer2_chain_t *parent;
1591 hammer2_chain_t *chain;
1592 hammer2_blockref_t *base;
1593 hammer2_blockref_t *bref;
1594 hammer2_blockref_t bcopy;
1595 hammer2_key_t scan_beg;
1596 hammer2_key_t scan_end;
1598 int how_always = HAMMER2_RESOLVE_ALWAYS;
1599 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1602 int maxloops = 300000;
1605 if (flags & HAMMER2_LOOKUP_ALWAYS) {
1606 how_maybe = how_always;
1607 how = HAMMER2_RESOLVE_ALWAYS;
1608 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
1609 how = HAMMER2_RESOLVE_NEVER;
1611 how = HAMMER2_RESOLVE_MAYBE;
1613 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK)) {
1614 how_maybe |= HAMMER2_RESOLVE_SHARED;
1615 how_always |= HAMMER2_RESOLVE_SHARED;
1616 how |= HAMMER2_RESOLVE_SHARED;
1620 * Recurse (*parentp) upward if necessary until the parent completely
1621 * encloses the key range or we hit the inode.
1623 * This function handles races against the flusher doing a delete-
1624 * duplicate above us and re-homes the parent to the duplicate in
1625 * that case, otherwise we'd wind up recursing down a stale chain.
1630 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1631 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1632 scan_beg = parent->bref.key;
1633 scan_end = scan_beg +
1634 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1635 if (key_beg >= scan_beg && key_end <= scan_end)
1637 parent = hammer2_chain_getparent(parentp, how_maybe);
1641 if (--maxloops == 0)
1642 panic("hammer2_chain_lookup: maxloops");
1644 * Locate the blockref array. Currently we do a fully associative
1645 * search through the array.
1647 switch(parent->bref.type) {
1648 case HAMMER2_BREF_TYPE_INODE:
1650 * Special shortcut for embedded data returns the inode
1651 * itself. Callers must detect this condition and access
1652 * the embedded data (the strategy code does this for us).
1654 * This is only applicable to regular files and softlinks.
1656 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1657 if (flags & HAMMER2_LOOKUP_NOLOCK)
1658 hammer2_chain_ref(parent);
1660 hammer2_chain_lock(parent, how_always);
1661 *key_nextp = key_end + 1;
1665 base = &parent->data->ipdata.u.blockset.blockref[0];
1666 count = HAMMER2_SET_COUNT;
1668 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1669 case HAMMER2_BREF_TYPE_INDIRECT:
1671 * Handle MATCHIND on the parent
1673 if (flags & HAMMER2_LOOKUP_MATCHIND) {
1674 scan_beg = parent->bref.key;
1675 scan_end = scan_beg +
1676 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1677 if (key_beg == scan_beg && key_end == scan_end) {
1679 hammer2_chain_lock(chain, how_maybe);
1680 *key_nextp = scan_end + 1;
1685 * Optimize indirect blocks in the INITIAL state to avoid
1688 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1691 if (parent->data == NULL)
1692 panic("parent->data is NULL");
1693 base = &parent->data->npdata[0];
1695 count = parent->bytes / sizeof(hammer2_blockref_t);
1697 case HAMMER2_BREF_TYPE_VOLUME:
1698 base = &hmp->voldata.sroot_blockset.blockref[0];
1699 count = HAMMER2_SET_COUNT;
1701 case HAMMER2_BREF_TYPE_FREEMAP:
1702 base = &hmp->voldata.freemap_blockset.blockref[0];
1703 count = HAMMER2_SET_COUNT;
1706 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
1708 base = NULL; /* safety */
1709 count = 0; /* safety */
1713 * Merged scan to find next candidate.
1715 * hammer2_base_*() functions require the parent->core.live_* fields
1716 * to be synchronized.
1718 * We need to hold the spinlock to access the block array and RB tree
1719 * and to interlock chain creation.
1721 if ((parent->core.flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
1722 hammer2_chain_countbrefs(parent, base, count);
1727 spin_lock(&parent->core.cst.spin);
1728 chain = hammer2_combined_find(parent, base, count,
1729 cache_indexp, key_nextp,
1732 generation = parent->core.generation;
1735 * Exhausted parent chain, iterate.
1738 spin_unlock(&parent->core.cst.spin);
1739 if (key_beg == key_end) /* short cut single-key case */
1743 * Stop if we reached the end of the iteration.
1745 if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1746 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1751 * Calculate next key, stop if we reached the end of the
1752 * iteration, otherwise go up one level and loop.
1754 key_beg = parent->bref.key +
1755 ((hammer2_key_t)1 << parent->bref.keybits);
1756 if (key_beg == 0 || key_beg > key_end)
1758 parent = hammer2_chain_getparent(parentp, how_maybe);
1763 * Selected from blockref or in-memory chain.
1765 if (chain == NULL) {
1767 spin_unlock(&parent->core.cst.spin);
1768 chain = hammer2_chain_get(parent, generation,
1770 if (chain == NULL) {
1771 kprintf("retry lookup parent %p keys %016jx:%016jx\n",
1772 parent, key_beg, key_end);
1775 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
1776 hammer2_chain_drop(chain);
1780 hammer2_chain_ref(chain);
1781 spin_unlock(&parent->core.cst.spin);
1785 * chain is referenced but not locked. We must lock the chain
1786 * to obtain definitive DUPLICATED/DELETED state
1788 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1789 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1790 hammer2_chain_lock(chain, how_maybe | HAMMER2_RESOLVE_NOREF);
1792 hammer2_chain_lock(chain, how | HAMMER2_RESOLVE_NOREF);
1796 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
1798 * NOTE: Chain's key range is not relevant as there might be
1799 * one-offs within the range that are not deleted.
1801 * NOTE: Lookups can race delete-duplicate because
1802 * delete-duplicate does not lock the parent's core
1803 * (they just use the spinlock on the core). We must
1804 * check for races by comparing the DUPLICATED flag before
1805 * releasing the spinlock with the flag after locking the
1808 if (chain->flags & HAMMER2_CHAIN_DELETED) {
1809 hammer2_chain_unlock(chain);
1810 key_beg = *key_nextp;
1811 if (key_beg == 0 || key_beg > key_end)
1817 * If the chain element is an indirect block it becomes the new
1818 * parent and we loop on it. We must maintain our top-down locks
1819 * to prevent the flusher from interfering (i.e. doing a
1820 * delete-duplicate and leaving us recursing down a deleted chain).
1822 * The parent always has to be locked with at least RESOLVE_MAYBE
1823 * so we can access its data. It might need a fixup if the caller
1824 * passed incompatible flags. Be careful not to cause a deadlock
1825 * as a data-load requires an exclusive lock.
1827 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
1828 * range is within the requested key range we return the indirect
1829 * block and do NOT loop. This is usually only used to acquire
1832 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1833 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1834 hammer2_chain_unlock(parent);
1835 *parentp = parent = chain;
1840 * All done, return the chain
1846 * After having issued a lookup we can iterate all matching keys.
1848 * If chain is non-NULL we continue the iteration from just after it's index.
1850 * If chain is NULL we assume the parent was exhausted and continue the
1851 * iteration at the next parent.
1853 * parent must be locked on entry and remains locked throughout. chain's
1854 * lock status must match flags. Chain is always at least referenced.
1856 * WARNING! The MATCHIND flag does not apply to this function.
1859 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
1860 hammer2_key_t *key_nextp,
1861 hammer2_key_t key_beg, hammer2_key_t key_end,
1862 int *cache_indexp, int flags)
1864 hammer2_chain_t *parent;
1869 * Calculate locking flags for upward recursion.
1871 how_maybe = HAMMER2_RESOLVE_MAYBE;
1872 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK))
1873 how_maybe |= HAMMER2_RESOLVE_SHARED;
1878 * Calculate the next index and recalculate the parent if necessary.
1881 key_beg = chain->bref.key +
1882 ((hammer2_key_t)1 << chain->bref.keybits);
1883 if (flags & HAMMER2_LOOKUP_NOLOCK)
1884 hammer2_chain_drop(chain);
1886 hammer2_chain_unlock(chain);
1889 * Any scan where the lookup returned degenerate data embedded
1890 * in the inode has an invalid index and must terminate.
1892 if (chain == parent)
1894 if (key_beg == 0 || key_beg > key_end)
1897 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1898 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1900 * We reached the end of the iteration.
1905 * Continue iteration with next parent unless the current
1906 * parent covers the range.
1908 key_beg = parent->bref.key +
1909 ((hammer2_key_t)1 << parent->bref.keybits);
1910 if (key_beg == 0 || key_beg > key_end)
1912 parent = hammer2_chain_getparent(parentp, how_maybe);
1918 return (hammer2_chain_lookup(parentp, key_nextp,
1920 cache_indexp, flags, &ddflag));
1924 * The raw scan function is similar to lookup/next but does not seek to a key.
1925 * Blockrefs are iterated via first_chain = (parent, NULL) and
1926 * next_chain = (parent, chain).
1928 * The passed-in parent must be locked and its data resolved. The returned
1929 * chain will be locked. Pass chain == NULL to acquire the first sub-chain
1930 * under parent and then iterate with the passed-in chain (which this
1931 * function will unlock).
1934 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t *chain,
1935 int *cache_indexp, int flags)
1937 hammer2_mount_t *hmp;
1938 hammer2_blockref_t *base;
1939 hammer2_blockref_t *bref;
1940 hammer2_blockref_t bcopy;
1942 hammer2_key_t next_key;
1944 int how_always = HAMMER2_RESOLVE_ALWAYS;
1945 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1948 int maxloops = 300000;
1953 * Scan flags borrowed from lookup
1955 if (flags & HAMMER2_LOOKUP_ALWAYS) {
1956 how_maybe = how_always;
1957 how = HAMMER2_RESOLVE_ALWAYS;
1958 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
1959 how = HAMMER2_RESOLVE_NEVER;
1961 how = HAMMER2_RESOLVE_MAYBE;
1963 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK)) {
1964 how_maybe |= HAMMER2_RESOLVE_SHARED;
1965 how_always |= HAMMER2_RESOLVE_SHARED;
1966 how |= HAMMER2_RESOLVE_SHARED;
1970 * Calculate key to locate first/next element, unlocking the previous
1971 * element as we go. Be careful, the key calculation can overflow.
1974 key = chain->bref.key +
1975 ((hammer2_key_t)1 << chain->bref.keybits);
1976 hammer2_chain_unlock(chain);
1985 if (--maxloops == 0)
1986 panic("hammer2_chain_scan: maxloops");
1988 * Locate the blockref array. Currently we do a fully associative
1989 * search through the array.
1991 switch(parent->bref.type) {
1992 case HAMMER2_BREF_TYPE_INODE:
1994 * An inode with embedded data has no sub-chains.
1996 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA)
1998 base = &parent->data->ipdata.u.blockset.blockref[0];
1999 count = HAMMER2_SET_COUNT;
2001 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2002 case HAMMER2_BREF_TYPE_INDIRECT:
2004 * Optimize indirect blocks in the INITIAL state to avoid
2007 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2010 if (parent->data == NULL)
2011 panic("parent->data is NULL");
2012 base = &parent->data->npdata[0];
2014 count = parent->bytes / sizeof(hammer2_blockref_t);
2016 case HAMMER2_BREF_TYPE_VOLUME:
2017 base = &hmp->voldata.sroot_blockset.blockref[0];
2018 count = HAMMER2_SET_COUNT;
2020 case HAMMER2_BREF_TYPE_FREEMAP:
2021 base = &hmp->voldata.freemap_blockset.blockref[0];
2022 count = HAMMER2_SET_COUNT;
2025 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2027 base = NULL; /* safety */
2028 count = 0; /* safety */
2032 * Merged scan to find next candidate.
2034 * hammer2_base_*() functions require the parent->core.live_* fields
2035 * to be synchronized.
2037 * We need to hold the spinlock to access the block array and RB tree
2038 * and to interlock chain creation.
2040 if ((parent->core.flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
2041 hammer2_chain_countbrefs(parent, base, count);
2044 spin_lock(&parent->core.cst.spin);
2045 chain = hammer2_combined_find(parent, base, count,
2046 cache_indexp, &next_key,
2047 key, HAMMER2_KEY_MAX,
2049 generation = parent->core.generation;
2052 * Exhausted parent chain, we're done.
2055 spin_unlock(&parent->core.cst.spin);
2056 KKASSERT(chain == NULL);
2061 * Selected from blockref or in-memory chain.
2063 if (chain == NULL) {
2065 spin_unlock(&parent->core.cst.spin);
2066 chain = hammer2_chain_get(parent, generation, &bcopy);
2067 if (chain == NULL) {
2068 kprintf("retry scan parent %p keys %016jx\n",
2072 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2073 hammer2_chain_drop(chain);
2078 hammer2_chain_ref(chain);
2079 spin_unlock(&parent->core.cst.spin);
2083 * chain is referenced but not locked. We must lock the chain
2084 * to obtain definitive DUPLICATED/DELETED state
2086 hammer2_chain_lock(chain, how | HAMMER2_RESOLVE_NOREF);
2089 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2091 * NOTE: chain's key range is not relevant as there might be
2092 * one-offs within the range that are not deleted.
2094 * NOTE: XXX this could create problems with scans used in
2095 * situations other than mount-time recovery.
2097 * NOTE: Lookups can race delete-duplicate because
2098 * delete-duplicate does not lock the parent's core
2099 * (they just use the spinlock on the core). We must
2100 * check for races by comparing the DUPLICATED flag before
2101 * releasing the spinlock with the flag after locking the
2104 if (chain->flags & HAMMER2_CHAIN_DELETED) {
2105 hammer2_chain_unlock(chain);
2116 * All done, return the chain or NULL
2122 * Create and return a new hammer2 system memory structure of the specified
2123 * key, type and size and insert it under (*parentp). This is a full
2124 * insertion, based on the supplied key/keybits, and may involve creating
2125 * indirect blocks and moving other chains around via delete/duplicate.
2127 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION
2128 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2129 * FULL. This typically means that the caller is creating the chain after
2130 * doing a hammer2_chain_lookup().
2132 * (*parentp) must be exclusive locked and may be replaced on return
2133 * depending on how much work the function had to do.
2135 * (*chainp) usually starts out NULL and returns the newly created chain,
2136 * but if the caller desires the caller may allocate a disconnected chain
2137 * and pass it in instead.
2139 * This function should NOT be used to insert INDIRECT blocks. It is
2140 * typically used to create/insert inodes and data blocks.
2142 * Caller must pass-in an exclusively locked parent the new chain is to
2143 * be inserted under, and optionally pass-in a disconnected, exclusively
2144 * locked chain to insert (else we create a new chain). The function will
2145 * adjust (*parentp) as necessary, create or connect the chain, and
2146 * return an exclusively locked chain in *chainp.
2149 hammer2_chain_create(hammer2_trans_t *trans, hammer2_chain_t **parentp,
2150 hammer2_chain_t **chainp, hammer2_pfsmount_t *pmp,
2151 hammer2_key_t key, int keybits, int type, size_t bytes)
2153 hammer2_mount_t *hmp;
2154 hammer2_chain_t *chain;
2155 hammer2_chain_t *parent;
2156 hammer2_blockref_t *base;
2157 hammer2_blockref_t dummy;
2161 int maxloops = 300000;
2164 * Topology may be crossing a PFS boundary.
2167 KKASSERT(ccms_thread_lock_owned(&parent->core.cst));
2171 if (chain == NULL) {
2173 * First allocate media space and construct the dummy bref,
2174 * then allocate the in-memory chain structure. Set the
2175 * INITIAL flag for fresh chains which do not have embedded
2178 bzero(&dummy, sizeof(dummy));
2181 dummy.keybits = keybits;
2182 dummy.data_off = hammer2_getradix(bytes);
2183 dummy.methods = parent->bref.methods;
2184 chain = hammer2_chain_alloc(hmp, pmp, trans, &dummy);
2185 hammer2_chain_core_alloc(trans, chain);
2188 * Lock the chain manually, chain_lock will load the chain
2189 * which we do NOT want to do. (note: chain->refs is set
2190 * to 1 by chain_alloc() for us, but lockcnt is not).
2193 ccms_thread_lock(&chain->core.cst, CCMS_STATE_EXCLUSIVE);
2197 * We do NOT set INITIAL here (yet). INITIAL is only
2198 * used for indirect blocks.
2200 * Recalculate bytes to reflect the actual media block
2203 bytes = (hammer2_off_t)1 <<
2204 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
2205 chain->bytes = bytes;
2208 case HAMMER2_BREF_TYPE_VOLUME:
2209 case HAMMER2_BREF_TYPE_FREEMAP:
2210 panic("hammer2_chain_create: called with volume type");
2212 case HAMMER2_BREF_TYPE_INDIRECT:
2213 panic("hammer2_chain_create: cannot be used to"
2214 "create indirect block");
2216 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2217 panic("hammer2_chain_create: cannot be used to"
2218 "create freemap root or node");
2220 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2221 KKASSERT(bytes == sizeof(chain->data->bmdata));
2223 case HAMMER2_BREF_TYPE_INODE:
2224 case HAMMER2_BREF_TYPE_DATA:
2227 * leave chain->data NULL, set INITIAL
2229 KKASSERT(chain->data == NULL);
2230 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
2235 * We are reattaching a previously deleted chain, possibly
2236 * under a new parent and possibly with a new key/keybits.
2237 * The chain does not have to be in a modified state. The
2238 * UPDATE flag will be set later on in this routine.
2240 * Do NOT mess with the current state of the INITIAL flag.
2242 chain->bref.key = key;
2243 chain->bref.keybits = keybits;
2244 if (chain->flags & HAMMER2_CHAIN_DELETED)
2245 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2246 KKASSERT(chain->parent == NULL);
2250 * Calculate how many entries we have in the blockref array and
2251 * determine if an indirect block is required.
2254 if (--maxloops == 0)
2255 panic("hammer2_chain_create: maxloops");
2257 switch(parent->bref.type) {
2258 case HAMMER2_BREF_TYPE_INODE:
2259 KKASSERT((parent->data->ipdata.op_flags &
2260 HAMMER2_OPFLAG_DIRECTDATA) == 0);
2261 KKASSERT(parent->data != NULL);
2262 base = &parent->data->ipdata.u.blockset.blockref[0];
2263 count = HAMMER2_SET_COUNT;
2265 case HAMMER2_BREF_TYPE_INDIRECT:
2266 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2267 if (parent->flags & HAMMER2_CHAIN_INITIAL)
2270 base = &parent->data->npdata[0];
2271 count = parent->bytes / sizeof(hammer2_blockref_t);
2273 case HAMMER2_BREF_TYPE_VOLUME:
2274 KKASSERT(parent->data != NULL);
2275 base = &hmp->voldata.sroot_blockset.blockref[0];
2276 count = HAMMER2_SET_COUNT;
2278 case HAMMER2_BREF_TYPE_FREEMAP:
2279 KKASSERT(parent->data != NULL);
2280 base = &hmp->voldata.freemap_blockset.blockref[0];
2281 count = HAMMER2_SET_COUNT;
2284 panic("hammer2_chain_create: unrecognized blockref type: %d",
2292 * Make sure we've counted the brefs
2294 if ((parent->core.flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
2295 hammer2_chain_countbrefs(parent, base, count);
2297 KKASSERT(parent->core.live_count >= 0 &&
2298 parent->core.live_count <= count);
2301 * If no free blockref could be found we must create an indirect
2302 * block and move a number of blockrefs into it. With the parent
2303 * locked we can safely lock each child in order to delete+duplicate
2304 * it without causing a deadlock.
2306 * This may return the new indirect block or the old parent depending
2307 * on where the key falls. NULL is returned on error.
2309 if (parent->core.live_count == count) {
2310 hammer2_chain_t *nparent;
2312 nparent = hammer2_chain_create_indirect(trans, parent,
2315 if (nparent == NULL) {
2317 hammer2_chain_drop(chain);
2321 if (parent != nparent) {
2322 hammer2_chain_unlock(parent);
2323 parent = *parentp = nparent;
2329 * Link the chain into its parent.
2331 if (chain->parent != NULL)
2332 panic("hammer2: hammer2_chain_create: chain already connected");
2333 KKASSERT(chain->parent == NULL);
2334 hammer2_chain_insert(parent, chain,
2335 HAMMER2_CHAIN_INSERT_SPIN |
2336 HAMMER2_CHAIN_INSERT_LIVE,
2341 * Mark the newly created chain modified. This will cause
2344 * Device buffers are not instantiated for DATA elements
2345 * as these are handled by logical buffers.
2347 * Indirect and freemap node indirect blocks are handled
2348 * by hammer2_chain_create_indirect() and not by this
2351 * Data for all other bref types is expected to be
2352 * instantiated (INODE, LEAF).
2354 switch(chain->bref.type) {
2355 case HAMMER2_BREF_TYPE_DATA:
2356 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2357 case HAMMER2_BREF_TYPE_INODE:
2358 hammer2_chain_modify(trans, chain,
2359 HAMMER2_MODIFY_OPTDATA);
2363 * Remaining types are not supported by this function.
2364 * In particular, INDIRECT and LEAF_NODE types are
2365 * handled by create_indirect().
2367 panic("hammer2_chain_create: bad type: %d",
2374 * When reconnecting a chain we must set UPDATE and
2375 * setflush so the flush recognizes that it must update
2376 * the bref in the parent.
2378 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
2379 hammer2_chain_ref(chain);
2380 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
2385 * We must setflush(parent) to ensure that it recurses through to
2386 * chain. setflush(chain) might not work because ONFLUSH is possibly
2387 * already set in the chain (so it won't recurse up to set it in the
2390 hammer2_chain_setflush(trans, parent);
2399 * Move the chain from its old parent to a new parent. The chain must have
2400 * already been deleted or already disconnected (or never associated) with
2401 * a parent. The chain is reassociated with the new parent and the deleted
2402 * flag will be cleared (no longer deleted). The chain's modification state
2405 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION
2406 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2407 * FULL. This typically means that the caller is creating the chain after
2408 * doing a hammer2_chain_lookup().
2410 * A non-NULL bref is typically passed when key and keybits must be overridden.
2411 * Note that hammer2_cluster_duplicate() *ONLY* uses the key and keybits fields
2412 * from a passed-in bref and uses the old chain's bref for everything else.
2414 * If (parent) is non-NULL then the new duplicated chain is inserted under
2417 * If (parent) is NULL then the newly duplicated chain is not inserted
2418 * anywhere, similar to if it had just been chain_alloc()'d (suitable for
2419 * passing into hammer2_chain_create() after this function returns).
2421 * WARNING! This function calls create which means it can insert indirect
2422 * blocks. This can cause other unrelated chains in the parent to
2423 * be moved to a newly inserted indirect block in addition to the
2427 hammer2_chain_rename(hammer2_trans_t *trans, hammer2_blockref_t *bref,
2428 hammer2_chain_t **parentp, hammer2_chain_t *chain)
2430 hammer2_mount_t *hmp;
2431 hammer2_chain_t *parent;
2435 * WARNING! We should never resolve DATA to device buffers
2436 * (XXX allow it if the caller did?), and since
2437 * we currently do not have the logical buffer cache
2438 * buffer in-hand to fix its cached physical offset
2439 * we also force the modify code to not COW it. XXX
2442 KKASSERT(chain->parent == NULL);
2445 * Now create a duplicate of the chain structure, associating
2446 * it with the same core, making it the same size, pointing it
2447 * to the same bref (the same media block).
2450 bref = &chain->bref;
2451 bytes = (hammer2_off_t)1 <<
2452 (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
2455 * If parent is not NULL the duplicated chain will be entered under
2456 * the parent and the UPDATE bit set to tell flush to update
2459 * We must setflush(parent) to ensure that it recurses through to
2460 * chain. setflush(chain) might not work because ONFLUSH is possibly
2461 * already set in the chain (so it won't recurse up to set it in the
2464 * Having both chains locked is extremely important for atomicy.
2466 if (parentp && (parent = *parentp) != NULL) {
2467 KKASSERT(ccms_thread_lock_owned(&parent->core.cst));
2468 KKASSERT(parent->refs > 0);
2470 hammer2_chain_create(trans, parentp, &chain, chain->pmp,
2471 bref->key, bref->keybits, bref->type,
2473 KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE);
2474 hammer2_chain_setflush(trans, *parentp);
2479 * Helper function for deleting chains.
2481 * The chain is removed from the live view (the RBTREE) as well as the parent's
2482 * blockmap. Both chain and its parent must be locked.
2485 _hammer2_chain_delete_helper(hammer2_trans_t *trans,
2486 hammer2_chain_t *parent, hammer2_chain_t *chain)
2488 hammer2_mount_t *hmp;
2490 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
2493 if (chain->flags & HAMMER2_CHAIN_BMAPPED) {
2495 * Chain is blockmapped, so there must be a parent.
2496 * Atomically remove the chain from the parent and remove
2497 * the blockmap entry.
2499 hammer2_blockref_t *base;
2502 KKASSERT(parent != NULL);
2503 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
2504 hammer2_chain_modify(trans, parent,
2505 HAMMER2_MODIFY_OPTDATA);
2508 * Calculate blockmap pointer
2510 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
2511 spin_lock(&parent->core.cst.spin);
2513 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2514 atomic_add_int(&parent->core.live_count, -1);
2515 ++parent->core.generation;
2516 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2517 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2518 --parent->core.chain_count;
2519 chain->parent = NULL;
2521 switch(parent->bref.type) {
2522 case HAMMER2_BREF_TYPE_INODE:
2524 * Access the inode's block array. However, there
2525 * is no block array if the inode is flagged
2526 * DIRECTDATA. The DIRECTDATA case typicaly only
2527 * occurs when a hardlink has been shifted up the
2528 * tree and the original inode gets replaced with
2529 * an OBJTYPE_HARDLINK placeholding inode.
2532 (parent->data->ipdata.op_flags &
2533 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
2535 &parent->data->ipdata.u.blockset.blockref[0];
2539 count = HAMMER2_SET_COUNT;
2541 case HAMMER2_BREF_TYPE_INDIRECT:
2542 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2544 base = &parent->data->npdata[0];
2547 count = parent->bytes / sizeof(hammer2_blockref_t);
2549 case HAMMER2_BREF_TYPE_VOLUME:
2550 base = &hmp->voldata.sroot_blockset.blockref[0];
2551 count = HAMMER2_SET_COUNT;
2553 case HAMMER2_BREF_TYPE_FREEMAP:
2554 base = &parent->data->npdata[0];
2555 count = HAMMER2_SET_COUNT;
2560 panic("hammer2_flush_pass2: "
2561 "unrecognized blockref type: %d",
2565 int cache_index = -1;
2566 hammer2_base_delete(trans, parent, base, count,
2567 &cache_index, chain);
2569 spin_unlock(&parent->core.cst.spin);
2570 } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
2572 * Chain is not blockmapped but a parent is present.
2573 * Atomically remove the chain from the parent. There is
2574 * no blockmap entry to remove.
2576 spin_lock(&parent->core.cst.spin);
2577 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2578 atomic_add_int(&parent->core.live_count, -1);
2579 ++parent->core.generation;
2580 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2581 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2582 --parent->core.chain_count;
2583 chain->parent = NULL;
2584 spin_unlock(&parent->core.cst.spin);
2587 * Chain is not blockmapped and has no parent. This
2588 * is a degenerate case.
2590 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2595 * Create an indirect block that covers one or more of the elements in the
2596 * current parent. Either returns the existing parent with no locking or
2597 * ref changes or returns the new indirect block locked and referenced
2598 * and leaving the original parent lock/ref intact as well.
2600 * If an error occurs, NULL is returned and *errorp is set to the error.
2602 * The returned chain depends on where the specified key falls.
2604 * The key/keybits for the indirect mode only needs to follow three rules:
2606 * (1) That all elements underneath it fit within its key space and
2608 * (2) That all elements outside it are outside its key space.
2610 * (3) When creating the new indirect block any elements in the current
2611 * parent that fit within the new indirect block's keyspace must be
2612 * moved into the new indirect block.
2614 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
2615 * keyspace the the current parent, but lookup/iteration rules will
2616 * ensure (and must ensure) that rule (2) for all parents leading up
2617 * to the nearest inode or the root volume header is adhered to. This
2618 * is accomplished by always recursing through matching keyspaces in
2619 * the hammer2_chain_lookup() and hammer2_chain_next() API.
2621 * The current implementation calculates the current worst-case keyspace by
2622 * iterating the current parent and then divides it into two halves, choosing
2623 * whichever half has the most elements (not necessarily the half containing
2624 * the requested key).
2626 * We can also opt to use the half with the least number of elements. This
2627 * causes lower-numbered keys (aka logical file offsets) to recurse through
2628 * fewer indirect blocks and higher-numbered keys to recurse through more.
2629 * This also has the risk of not moving enough elements to the new indirect
2630 * block and being forced to create several indirect blocks before the element
2633 * Must be called with an exclusively locked parent.
2635 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
2636 hammer2_key_t *keyp, int keybits,
2637 hammer2_blockref_t *base, int count);
2638 static int hammer2_chain_indkey_normal(hammer2_chain_t *parent,
2639 hammer2_key_t *keyp, int keybits,
2640 hammer2_blockref_t *base, int count);
2643 hammer2_chain_create_indirect(hammer2_trans_t *trans, hammer2_chain_t *parent,
2644 hammer2_key_t create_key, int create_bits,
2645 int for_type, int *errorp)
2647 hammer2_mount_t *hmp;
2648 hammer2_blockref_t *base;
2649 hammer2_blockref_t *bref;
2650 hammer2_blockref_t bcopy;
2651 hammer2_chain_t *chain;
2652 hammer2_chain_t *ichain;
2653 hammer2_chain_t dummy;
2654 hammer2_key_t key = create_key;
2655 hammer2_key_t key_beg;
2656 hammer2_key_t key_end;
2657 hammer2_key_t key_next;
2658 int keybits = create_bits;
2665 int maxloops = 300000;
2668 * Calculate the base blockref pointer or NULL if the chain
2669 * is known to be empty. We need to calculate the array count
2670 * for RB lookups either way.
2674 KKASSERT(ccms_thread_lock_owned(&parent->core.cst));
2676 /*hammer2_chain_modify(trans, &parent, HAMMER2_MODIFY_OPTDATA);*/
2677 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2680 switch(parent->bref.type) {
2681 case HAMMER2_BREF_TYPE_INODE:
2682 count = HAMMER2_SET_COUNT;
2684 case HAMMER2_BREF_TYPE_INDIRECT:
2685 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2686 count = parent->bytes / sizeof(hammer2_blockref_t);
2688 case HAMMER2_BREF_TYPE_VOLUME:
2689 count = HAMMER2_SET_COUNT;
2691 case HAMMER2_BREF_TYPE_FREEMAP:
2692 count = HAMMER2_SET_COUNT;
2695 panic("hammer2_chain_create_indirect: "
2696 "unrecognized blockref type: %d",
2702 switch(parent->bref.type) {
2703 case HAMMER2_BREF_TYPE_INODE:
2704 base = &parent->data->ipdata.u.blockset.blockref[0];
2705 count = HAMMER2_SET_COUNT;
2707 case HAMMER2_BREF_TYPE_INDIRECT:
2708 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2709 base = &parent->data->npdata[0];
2710 count = parent->bytes / sizeof(hammer2_blockref_t);
2712 case HAMMER2_BREF_TYPE_VOLUME:
2713 base = &hmp->voldata.sroot_blockset.blockref[0];
2714 count = HAMMER2_SET_COUNT;
2716 case HAMMER2_BREF_TYPE_FREEMAP:
2717 base = &hmp->voldata.freemap_blockset.blockref[0];
2718 count = HAMMER2_SET_COUNT;
2721 panic("hammer2_chain_create_indirect: "
2722 "unrecognized blockref type: %d",
2730 * dummy used in later chain allocation (no longer used for lookups).
2732 bzero(&dummy, sizeof(dummy));
2735 * When creating an indirect block for a freemap node or leaf
2736 * the key/keybits must be fitted to static radix levels because
2737 * particular radix levels use particular reserved blocks in the
2740 * This routine calculates the key/radix of the indirect block
2741 * we need to create, and whether it is on the high-side or the
2744 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2745 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2746 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
2749 keybits = hammer2_chain_indkey_normal(parent, &key, keybits,
2754 * Normalize the key for the radix being represented, keeping the
2755 * high bits and throwing away the low bits.
2757 key &= ~(((hammer2_key_t)1 << keybits) - 1);
2760 * How big should our new indirect block be? It has to be at least
2761 * as large as its parent.
2763 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE)
2764 nbytes = HAMMER2_IND_BYTES_MIN;
2766 nbytes = HAMMER2_IND_BYTES_MAX;
2767 if (nbytes < count * sizeof(hammer2_blockref_t))
2768 nbytes = count * sizeof(hammer2_blockref_t);
2771 * Ok, create our new indirect block
2773 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2774 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2775 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
2777 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
2779 dummy.bref.key = key;
2780 dummy.bref.keybits = keybits;
2781 dummy.bref.data_off = hammer2_getradix(nbytes);
2782 dummy.bref.methods = parent->bref.methods;
2784 ichain = hammer2_chain_alloc(hmp, parent->pmp, trans, &dummy.bref);
2785 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
2786 hammer2_chain_core_alloc(trans, ichain);
2787 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
2788 hammer2_chain_drop(ichain); /* excess ref from alloc */
2791 * We have to mark it modified to allocate its block, but use
2792 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
2793 * it won't be acted upon by the flush code.
2795 hammer2_chain_modify(trans, ichain, HAMMER2_MODIFY_OPTDATA);
2798 * Iterate the original parent and move the matching brefs into
2799 * the new indirect block.
2801 * XXX handle flushes.
2804 key_end = HAMMER2_KEY_MAX;
2806 spin_lock(&parent->core.cst.spin);
2811 if (++loops > 100000) {
2812 spin_unlock(&parent->core.cst.spin);
2813 panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
2814 reason, parent, base, count, key_next);
2818 * NOTE: spinlock stays intact, returned chain (if not NULL)
2819 * is not referenced or locked which means that we
2820 * cannot safely check its flagged / deletion status
2823 chain = hammer2_combined_find(parent, base, count,
2824 &cache_index, &key_next,
2827 generation = parent->core.generation;
2830 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
2833 * Skip keys that are not within the key/radix of the new
2834 * indirect block. They stay in the parent.
2836 if ((~(((hammer2_key_t)1 << keybits) - 1) &
2837 (key ^ bref->key)) != 0) {
2838 goto next_key_spinlocked;
2842 * Load the new indirect block by acquiring the related
2843 * chains (potentially from media as it might not be
2844 * in-memory). Then move it to the new parent (ichain)
2845 * via DELETE-DUPLICATE.
2847 * chain is referenced but not locked. We must lock the
2848 * chain to obtain definitive DUPLICATED/DELETED state
2852 * Use chain already present in the RBTREE
2854 hammer2_chain_ref(chain);
2855 spin_unlock(&parent->core.cst.spin);
2856 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER |
2857 HAMMER2_RESOLVE_NOREF);
2860 * Get chain for blockref element. _get returns NULL
2861 * on insertion race.
2864 spin_unlock(&parent->core.cst.spin);
2865 chain = hammer2_chain_get(parent, generation, &bcopy);
2866 if (chain == NULL) {
2868 spin_lock(&parent->core.cst.spin);
2871 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2872 kprintf("REASON 2\n");
2874 hammer2_chain_drop(chain);
2875 spin_lock(&parent->core.cst.spin);
2878 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER |
2879 HAMMER2_RESOLVE_NOREF);
2883 * This is always live so if the chain has been deleted
2884 * we raced someone and we have to retry.
2886 * NOTE: Lookups can race delete-duplicate because
2887 * delete-duplicate does not lock the parent's core
2888 * (they just use the spinlock on the core). We must
2889 * check for races by comparing the DUPLICATED flag before
2890 * releasing the spinlock with the flag after locking the
2893 * (note reversed logic for this one)
2895 if (chain->flags & HAMMER2_CHAIN_DELETED) {
2896 hammer2_chain_unlock(chain);
2901 * Shift the chain to the indirect block.
2903 * WARNING! Can cause held-over chains to require a refactor.
2904 * Fortunately we have none (our locked chains are
2905 * passed into and modified by the call).
2907 hammer2_chain_delete(trans, parent, chain, 0);
2908 hammer2_chain_rename(trans, NULL, &ichain, chain);
2909 hammer2_chain_unlock(chain);
2910 KKASSERT(parent->refs > 0);
2913 spin_lock(&parent->core.cst.spin);
2914 next_key_spinlocked:
2915 if (--maxloops == 0)
2916 panic("hammer2_chain_create_indirect: maxloops");
2918 if (key_next == 0 || key_next > key_end)
2923 spin_unlock(&parent->core.cst.spin);
2926 * Insert the new indirect block into the parent now that we've
2927 * cleared out some entries in the parent. We calculated a good
2928 * insertion index in the loop above (ichain->index).
2930 * We don't have to set UPDATE here because we mark ichain
2931 * modified down below (so the normal modified -> flush -> set-moved
2932 * sequence applies).
2934 * The insertion shouldn't race as this is a completely new block
2935 * and the parent is locked.
2937 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
2938 hammer2_chain_insert(parent, ichain,
2939 HAMMER2_CHAIN_INSERT_SPIN |
2940 HAMMER2_CHAIN_INSERT_LIVE,
2944 * Make sure flushes propogate after our manual insertion.
2946 hammer2_chain_setflush(trans, ichain);
2947 hammer2_chain_setflush(trans, parent);
2950 * Figure out what to return.
2952 if (~(((hammer2_key_t)1 << keybits) - 1) &
2953 (create_key ^ key)) {
2955 * Key being created is outside the key range,
2956 * return the original parent.
2958 hammer2_chain_unlock(ichain);
2961 * Otherwise its in the range, return the new parent.
2962 * (leave both the new and old parent locked).
2971 * Calculate the keybits and highside/lowside of the freemap node the
2972 * caller is creating.
2974 * This routine will specify the next higher-level freemap key/radix
2975 * representing the lowest-ordered set. By doing so, eventually all
2976 * low-ordered sets will be moved one level down.
2978 * We have to be careful here because the freemap reserves a limited
2979 * number of blocks for a limited number of levels. So we can't just
2980 * push indiscriminately.
2983 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
2984 int keybits, hammer2_blockref_t *base, int count)
2986 hammer2_chain_t *chain;
2987 hammer2_blockref_t *bref;
2989 hammer2_key_t key_beg;
2990 hammer2_key_t key_end;
2991 hammer2_key_t key_next;
2995 int maxloops = 300000;
3003 * Calculate the range of keys in the array being careful to skip
3004 * slots which are overridden with a deletion.
3007 key_end = HAMMER2_KEY_MAX;
3009 spin_lock(&parent->core.cst.spin);
3012 if (--maxloops == 0) {
3013 panic("indkey_freemap shit %p %p:%d\n",
3014 parent, base, count);
3016 chain = hammer2_combined_find(parent, base, count,
3017 &cache_index, &key_next,
3028 * Skip deleted chains.
3030 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3031 if (key_next == 0 || key_next > key_end)
3038 * Use the full live (not deleted) element for the scan
3039 * iteration. HAMMER2 does not allow partial replacements.
3041 * XXX should be built into hammer2_combined_find().
3043 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3045 if (keybits > bref->keybits) {
3047 keybits = bref->keybits;
3048 } else if (keybits == bref->keybits && bref->key < key) {
3055 spin_unlock(&parent->core.cst.spin);
3058 * Return the keybits for a higher-level FREEMAP_NODE covering
3062 case HAMMER2_FREEMAP_LEVEL0_RADIX:
3063 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
3065 case HAMMER2_FREEMAP_LEVEL1_RADIX:
3066 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
3068 case HAMMER2_FREEMAP_LEVEL2_RADIX:
3069 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
3071 case HAMMER2_FREEMAP_LEVEL3_RADIX:
3072 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
3074 case HAMMER2_FREEMAP_LEVEL4_RADIX:
3075 panic("hammer2_chain_indkey_freemap: level too high");
3078 panic("hammer2_chain_indkey_freemap: bad radix");
3087 * Calculate the keybits and highside/lowside of the indirect block the
3088 * caller is creating.
3091 hammer2_chain_indkey_normal(hammer2_chain_t *parent, hammer2_key_t *keyp,
3092 int keybits, hammer2_blockref_t *base, int count)
3094 hammer2_blockref_t *bref;
3095 hammer2_chain_t *chain;
3096 hammer2_key_t key_beg;
3097 hammer2_key_t key_end;
3098 hammer2_key_t key_next;
3104 int maxloops = 300000;
3111 * Calculate the range of keys in the array being careful to skip
3112 * slots which are overridden with a deletion. Once the scan
3113 * completes we will cut the key range in half and shift half the
3114 * range into the new indirect block.
3117 key_end = HAMMER2_KEY_MAX;
3119 spin_lock(&parent->core.cst.spin);
3122 if (--maxloops == 0) {
3123 panic("indkey_freemap shit %p %p:%d\n",
3124 parent, base, count);
3126 chain = hammer2_combined_find(parent, base, count,
3127 &cache_index, &key_next,
3138 * NOTE: No need to check DUPLICATED here because we do
3139 * not release the spinlock.
3141 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3142 if (key_next == 0 || key_next > key_end)
3149 * Use the full live (not deleted) element for the scan
3150 * iteration. HAMMER2 does not allow partial replacements.
3152 * XXX should be built into hammer2_combined_find().
3154 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3157 * Expand our calculated key range (key, keybits) to fit
3158 * the scanned key. nkeybits represents the full range
3159 * that we will later cut in half (two halves @ nkeybits - 1).
3162 if (nkeybits < bref->keybits) {
3163 if (bref->keybits > 64) {
3164 kprintf("bad bref chain %p bref %p\n",
3168 nkeybits = bref->keybits;
3170 while (nkeybits < 64 &&
3171 (~(((hammer2_key_t)1 << nkeybits) - 1) &
3172 (key ^ bref->key)) != 0) {
3177 * If the new key range is larger we have to determine
3178 * which side of the new key range the existing keys fall
3179 * under by checking the high bit, then collapsing the
3180 * locount into the hicount or vise-versa.
3182 if (keybits != nkeybits) {
3183 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
3194 * The newly scanned key will be in the lower half or the
3195 * upper half of the (new) key range.
3197 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
3206 spin_unlock(&parent->core.cst.spin);
3207 bref = NULL; /* now invalid (safety) */
3210 * Adjust keybits to represent half of the full range calculated
3211 * above (radix 63 max)
3216 * Select whichever half contains the most elements. Theoretically
3217 * we can select either side as long as it contains at least one
3218 * element (in order to ensure that a free slot is present to hold
3219 * the indirect block).
3221 if (hammer2_indirect_optimize) {
3223 * Insert node for least number of keys, this will arrange
3224 * the first few blocks of a large file or the first few
3225 * inodes in a directory with fewer indirect blocks when
3228 if (hicount < locount && hicount != 0)
3229 key |= (hammer2_key_t)1 << keybits;
3231 key &= ~(hammer2_key_t)1 << keybits;
3234 * Insert node for most number of keys, best for heavily
3237 if (hicount > locount)
3238 key |= (hammer2_key_t)1 << keybits;
3240 key &= ~(hammer2_key_t)1 << keybits;
3248 * Sets CHAIN_DELETED and remove the chain's blockref from the parent if
3251 * Both parent and chain must be locked exclusively.
3253 * This function will modify the parent if the blockref requires removal
3254 * from the parent's block table.
3256 * This function is NOT recursive. Any entity already pushed into the
3257 * chain (such as an inode) may still need visibility into its contents,
3258 * as well as the ability to read and modify the contents. For example,
3259 * for an unlinked file which is still open.
3262 hammer2_chain_delete(hammer2_trans_t *trans, hammer2_chain_t *parent,
3263 hammer2_chain_t *chain, int flags)
3265 KKASSERT(ccms_thread_lock_owned(&chain->core.cst));
3268 * Nothing to do if already marked.
3270 * We need the spinlock on the core whos RBTREE contains chain
3271 * to protect against races.
3273 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
3274 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
3275 chain->parent == parent);
3276 _hammer2_chain_delete_helper(trans, parent, chain);
3279 if (flags & HAMMER2_DELETE_PERMANENT) {
3280 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
3281 hammer2_flush(trans, chain);
3283 /* XXX might not be needed */
3284 hammer2_chain_setflush(trans, chain);
3289 * Returns the index of the nearest element in the blockref array >= elm.
3290 * Returns (count) if no element could be found.
3292 * Sets *key_nextp to the next key for loop purposes but does not modify
3293 * it if the next key would be higher than the current value of *key_nextp.
3294 * Note that *key_nexp can overflow to 0, which should be tested by the
3297 * (*cache_indexp) is a heuristic and can be any value without effecting
3300 * WARNING! Must be called with parent's spinlock held. Spinlock remains
3301 * held through the operation.
3304 hammer2_base_find(hammer2_chain_t *parent,
3305 hammer2_blockref_t *base, int count,
3306 int *cache_indexp, hammer2_key_t *key_nextp,
3307 hammer2_key_t key_beg, hammer2_key_t key_end)
3309 hammer2_blockref_t *scan;
3310 hammer2_key_t scan_end;
3315 * Require the live chain's already have their core's counted
3316 * so we can optimize operations.
3318 KKASSERT(parent->core.flags & HAMMER2_CORE_COUNTEDBREFS);
3323 if (count == 0 || base == NULL)
3327 * Sequential optimization using *cache_indexp. This is the most
3330 * We can avoid trailing empty entries on live chains, otherwise
3331 * we might have to check the whole block array.
3335 limit = parent->core.live_zero;
3340 KKASSERT(i < count);
3346 while (i > 0 && (scan->type == 0 || scan->key > key_beg)) {
3353 * Search forwards, stop when we find a scan element which
3354 * encloses the key or until we know that there are no further
3358 if (scan->type != 0) {
3359 scan_end = scan->key +
3360 ((hammer2_key_t)1 << scan->keybits) - 1;
3361 if (scan->key > key_beg || scan_end >= key_beg)
3374 scan_end = scan->key +
3375 ((hammer2_key_t)1 << scan->keybits);
3376 if (scan_end && (*key_nextp > scan_end ||
3378 *key_nextp = scan_end;
3386 * Do a combined search and return the next match either from the blockref
3387 * array or from the in-memory chain. Sets *bresp to the returned bref in
3388 * both cases, or sets it to NULL if the search exhausted. Only returns
3389 * a non-NULL chain if the search matched from the in-memory chain.
3391 * When no in-memory chain has been found and a non-NULL bref is returned
3395 * The returned chain is not locked or referenced. Use the returned bref
3396 * to determine if the search exhausted or not. Iterate if the base find
3397 * is chosen but matches a deleted chain.
3399 * WARNING! Must be called with parent's spinlock held. Spinlock remains
3400 * held through the operation.
3402 static hammer2_chain_t *
3403 hammer2_combined_find(hammer2_chain_t *parent,
3404 hammer2_blockref_t *base, int count,
3405 int *cache_indexp, hammer2_key_t *key_nextp,
3406 hammer2_key_t key_beg, hammer2_key_t key_end,
3407 hammer2_blockref_t **bresp)
3409 hammer2_blockref_t *bref;
3410 hammer2_chain_t *chain;
3414 * Lookup in block array and in rbtree.
3416 *key_nextp = key_end + 1;
3417 i = hammer2_base_find(parent, base, count, cache_indexp,
3418 key_nextp, key_beg, key_end);
3419 chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
3424 if (i == count && chain == NULL) {
3430 * Only chain matched.
3433 bref = &chain->bref;
3438 * Only blockref matched.
3440 if (chain == NULL) {
3446 * Both in-memory and blockref matched, select the nearer element.
3448 * If both are flush with the left-hand side or both are the
3449 * same distance away, select the chain. In this situation the
3450 * chain must have been loaded from the matching blockmap.
3452 if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
3453 chain->bref.key == base[i].key) {
3454 KKASSERT(chain->bref.key == base[i].key);
3455 bref = &chain->bref;
3460 * Select the nearer key
3462 if (chain->bref.key < base[i].key) {
3463 bref = &chain->bref;
3470 * If the bref is out of bounds we've exhausted our search.
3473 if (bref->key > key_end) {
3483 * Locate the specified block array element and delete it. The element
3486 * The spin lock on the related chain must be held.
3488 * NOTE: live_count was adjusted when the chain was deleted, so it does not
3489 * need to be adjusted when we commit the media change.
3492 hammer2_base_delete(hammer2_trans_t *trans, hammer2_chain_t *parent,
3493 hammer2_blockref_t *base, int count,
3494 int *cache_indexp, hammer2_chain_t *chain)
3496 hammer2_blockref_t *elm = &chain->bref;
3497 hammer2_key_t key_next;
3501 * Delete element. Expect the element to exist.
3503 * XXX see caller, flush code not yet sophisticated enough to prevent
3504 * re-flushed in some cases.
3506 key_next = 0; /* max range */
3507 i = hammer2_base_find(parent, base, count, cache_indexp,
3508 &key_next, elm->key, elm->key);
3509 if (i == count || base[i].type == 0 ||
3510 base[i].key != elm->key ||
3511 ((chain->flags & HAMMER2_CHAIN_BMAPUPD) == 0 &&
3512 base[i].keybits != elm->keybits)) {
3513 spin_unlock(&parent->core.cst.spin);
3514 panic("delete base %p element not found at %d/%d elm %p\n",
3515 base, i, count, elm);
3518 bzero(&base[i], sizeof(*base));
3521 * We can only optimize parent->core.live_zero for live chains.
3523 if (parent->core.live_zero == i + 1) {
3524 while (--i >= 0 && base[i].type == 0)
3526 parent->core.live_zero = i + 1;
3530 * Clear appropriate blockmap flags in chain.
3532 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED |
3533 HAMMER2_CHAIN_BMAPUPD);
3537 * Insert the specified element. The block array must not already have the
3538 * element and must have space available for the insertion.
3540 * The spin lock on the related chain must be held.
3542 * NOTE: live_count was adjusted when the chain was deleted, so it does not
3543 * need to be adjusted when we commit the media change.
3546 hammer2_base_insert(hammer2_trans_t *trans __unused, hammer2_chain_t *parent,
3547 hammer2_blockref_t *base, int count,
3548 int *cache_indexp, hammer2_chain_t *chain)
3550 hammer2_blockref_t *elm = &chain->bref;
3551 hammer2_key_t key_next;
3560 * Insert new element. Expect the element to not already exist
3561 * unless we are replacing it.
3563 * XXX see caller, flush code not yet sophisticated enough to prevent
3564 * re-flushed in some cases.
3566 key_next = 0; /* max range */
3567 i = hammer2_base_find(parent, base, count, cache_indexp,
3568 &key_next, elm->key, elm->key);
3571 * Shortcut fill optimization, typical ordered insertion(s) may not
3574 KKASSERT(i >= 0 && i <= count);
3577 * Set appropriate blockmap flags in chain.
3579 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
3582 * We can only optimize parent->core.live_zero for live chains.
3584 if (i == count && parent->core.live_zero < count) {
3585 i = parent->core.live_zero++;
3590 xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
3591 if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
3592 spin_unlock(&parent->core.cst.spin);
3593 panic("insert base %p overlapping elements at %d elm %p\n",
3598 * Try to find an empty slot before or after.
3602 while (j > 0 || k < count) {
3604 if (j >= 0 && base[j].type == 0) {
3608 bcopy(&base[j+1], &base[j],
3609 (i - j - 1) * sizeof(*base));
3615 if (k < count && base[k].type == 0) {
3616 bcopy(&base[i], &base[i+1],
3617 (k - i) * sizeof(hammer2_blockref_t));
3621 * We can only update parent->core.live_zero for live
3624 if (parent->core.live_zero <= k)
3625 parent->core.live_zero = k + 1;
3630 panic("hammer2_base_insert: no room!");
3637 for (l = 0; l < count; ++l) {
3639 key_next = base[l].key +
3640 ((hammer2_key_t)1 << base[l].keybits) - 1;
3644 while (++l < count) {
3646 if (base[l].key <= key_next)
3647 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
3648 key_next = base[l].key +
3649 ((hammer2_key_t)1 << base[l].keybits) - 1;
3659 * Sort the blockref array for the chain. Used by the flush code to
3660 * sort the blockref[] array.
3662 * The chain must be exclusively locked AND spin-locked.
3664 typedef hammer2_blockref_t *hammer2_blockref_p;
3668 hammer2_base_sort_callback(const void *v1, const void *v2)
3670 hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
3671 hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
3674 * Make sure empty elements are placed at the end of the array
3676 if (bref1->type == 0) {
3677 if (bref2->type == 0)
3680 } else if (bref2->type == 0) {
3687 if (bref1->key < bref2->key)
3689 if (bref1->key > bref2->key)
3695 hammer2_base_sort(hammer2_chain_t *chain)
3697 hammer2_blockref_t *base;
3700 switch(chain->bref.type) {
3701 case HAMMER2_BREF_TYPE_INODE:
3703 * Special shortcut for embedded data returns the inode
3704 * itself. Callers must detect this condition and access
3705 * the embedded data (the strategy code does this for us).
3707 * This is only applicable to regular files and softlinks.
3709 if (chain->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA)
3711 base = &chain->data->ipdata.u.blockset.blockref[0];
3712 count = HAMMER2_SET_COUNT;
3714 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3715 case HAMMER2_BREF_TYPE_INDIRECT:
3717 * Optimize indirect blocks in the INITIAL state to avoid
3720 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
3721 base = &chain->data->npdata[0];
3722 count = chain->bytes / sizeof(hammer2_blockref_t);
3724 case HAMMER2_BREF_TYPE_VOLUME:
3725 base = &chain->hmp->voldata.sroot_blockset.blockref[0];
3726 count = HAMMER2_SET_COUNT;
3728 case HAMMER2_BREF_TYPE_FREEMAP:
3729 base = &chain->hmp->voldata.freemap_blockset.blockref[0];
3730 count = HAMMER2_SET_COUNT;
3733 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
3735 base = NULL; /* safety */
3736 count = 0; /* safety */
3738 kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
3744 * Chain memory management
3747 hammer2_chain_wait(hammer2_chain_t *chain)
3749 tsleep(chain, 0, "chnflw", 1);