2 * Copyright (c) 2011-2015 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * and Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This subsystem implements most of the core support functions for
37 * the hammer2_chain structure.
39 * Chains are the in-memory version on media objects (volume header, inodes,
40 * indirect blocks, data blocks, etc). Chains represent a portion of the
43 * Chains are no-longer delete-duplicated. Instead, the original in-memory
44 * chain will be moved along with its block reference (e.g. for things like
45 * renames, hardlink operations, modifications, etc), and will be indexed
46 * on a secondary list for flush handling instead of propagating a flag
49 * Concurrent front-end operations can still run against backend flushes
50 * as long as they do not cross the current flush boundary. An operation
51 * running above the current flush (in areas not yet flushed) can become
52 * part of the current flush while ano peration running below the current
53 * flush can become part of the next flush.
55 #include <sys/cdefs.h>
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/types.h>
60 #include <sys/kern_syscall.h>
63 #include <crypto/sha2/sha2.h>
67 static int hammer2_indirect_optimize; /* XXX SYSCTL */
69 static hammer2_chain_t *hammer2_chain_create_indirect(
70 hammer2_chain_t *parent,
71 hammer2_key_t key, int keybits,
72 hammer2_tid_t mtid, int for_type, int *errorp);
73 static void hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop);
74 static hammer2_chain_t *hammer2_combined_find(
75 hammer2_chain_t *parent,
76 hammer2_blockref_t *base, int count,
77 int *cache_indexp, hammer2_key_t *key_nextp,
78 hammer2_key_t key_beg, hammer2_key_t key_end,
79 hammer2_blockref_t **bresp);
82 * Basic RBTree for chains (core->rbtree and core->dbtree). Chains cannot
83 * overlap in the RB trees. Deleted chains are moved from rbtree to either
86 * Chains in delete-duplicate sequences can always iterate through core_entry
87 * to locate the live version of the chain.
89 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
92 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
100 * Compare chains. Overlaps are not supposed to happen and catch
101 * any software issues early we count overlaps as a match.
103 c1_beg = chain1->bref.key;
104 c1_end = c1_beg + ((hammer2_key_t)1 << chain1->bref.keybits) - 1;
105 c2_beg = chain2->bref.key;
106 c2_end = c2_beg + ((hammer2_key_t)1 << chain2->bref.keybits) - 1;
108 if (c1_end < c2_beg) /* fully to the left */
110 if (c1_beg > c2_end) /* fully to the right */
112 return(0); /* overlap (must not cross edge boundary) */
117 hammer2_isclusterable(hammer2_chain_t *chain)
119 if (hammer2_cluster_enable) {
120 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
121 chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
122 chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
130 * Make a chain visible to the flusher. The flusher needs to be able to
131 * do flushes of subdirectory chains or single files so it does a top-down
132 * recursion using the ONFLUSH flag for the recursion. It locates MODIFIED
133 * or UPDATE chains and flushes back up the chain to the volume root.
135 * This routine sets ONFLUSH upward until it hits the volume root. For
136 * simplicity we ignore PFSROOT boundaries whos rules can be complex.
137 * Extra ONFLUSH flagging doesn't hurt the filesystem.
140 hammer2_chain_setflush(hammer2_chain_t *chain)
142 hammer2_chain_t *parent;
144 if ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
145 hammer2_spin_sh(&chain->core.spin);
146 while ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
147 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH);
148 if ((parent = chain->parent) == NULL)
150 hammer2_spin_sh(&parent->core.spin);
151 hammer2_spin_unsh(&chain->core.spin);
154 hammer2_spin_unsh(&chain->core.spin);
159 * Allocate a new disconnected chain element representing the specified
160 * bref. chain->refs is set to 1 and the passed bref is copied to
161 * chain->bref. chain->bytes is derived from the bref.
163 * chain->pmp inherits pmp unless the chain is an inode (other than the
166 * NOTE: Returns a referenced but unlocked (because there is no core) chain.
169 hammer2_chain_alloc(hammer2_dev_t *hmp, hammer2_pfs_t *pmp,
170 hammer2_blockref_t *bref)
172 hammer2_chain_t *chain;
173 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
176 * Construct the appropriate system structure.
179 case HAMMER2_BREF_TYPE_INODE:
180 case HAMMER2_BREF_TYPE_INDIRECT:
181 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
182 case HAMMER2_BREF_TYPE_DATA:
183 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
185 * Chain's are really only associated with the hmp but we
186 * maintain a pmp association for per-mount memory tracking
187 * purposes. The pmp can be NULL.
189 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
191 case HAMMER2_BREF_TYPE_VOLUME:
192 case HAMMER2_BREF_TYPE_FREEMAP:
194 * Only hammer2_chain_bulksnap() calls this function with these
197 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
201 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
206 * Initialize the new chain structure. pmp must be set to NULL for
207 * chains belonging to the super-root topology of a device mount.
209 if (pmp == hmp->spmp)
215 chain->bytes = bytes;
217 chain->flags = HAMMER2_CHAIN_ALLOCATED;
220 * Set the PFS boundary flag if this chain represents a PFS root.
222 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
223 chain->flags |= HAMMER2_CHAIN_PFSBOUNDARY;
224 hammer2_chain_core_init(chain);
230 * Initialize a chain's core structure. This structure used to be allocated
231 * but is now embedded.
233 * The core is not locked. No additional refs on the chain are made.
234 * (trans) must not be NULL if (core) is not NULL.
237 hammer2_chain_core_init(hammer2_chain_t *chain)
240 * Fresh core under nchain (no multi-homing of ochain's
243 RB_INIT(&chain->core.rbtree); /* live chains */
244 hammer2_mtx_init(&chain->lock, "h2chain");
248 * Add a reference to a chain element, preventing its destruction.
250 * (can be called with spinlock held)
253 hammer2_chain_ref(hammer2_chain_t *chain)
255 atomic_add_int(&chain->refs, 1);
257 kprintf("REFC %p %d %08x\n", chain, chain->refs - 1, chain->flags);
263 * Insert the chain in the core rbtree.
265 * Normal insertions are placed in the live rbtree. Insertion of a deleted
266 * chain is a special case used by the flush code that is placed on the
267 * unstaged deleted list to avoid confusing the live view.
269 #define HAMMER2_CHAIN_INSERT_SPIN 0x0001
270 #define HAMMER2_CHAIN_INSERT_LIVE 0x0002
271 #define HAMMER2_CHAIN_INSERT_RACE 0x0004
275 hammer2_chain_insert(hammer2_chain_t *parent, hammer2_chain_t *chain,
276 int flags, int generation)
278 hammer2_chain_t *xchain;
281 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
282 hammer2_spin_ex(&parent->core.spin);
285 * Interlocked by spinlock, check for race
287 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
288 parent->core.generation != generation) {
296 xchain = RB_INSERT(hammer2_chain_tree, &parent->core.rbtree, chain);
297 KASSERT(xchain == NULL,
298 ("hammer2_chain_insert: collision %p %p", chain, xchain));
299 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
300 chain->parent = parent;
301 ++parent->core.chain_count;
302 ++parent->core.generation; /* XXX incs for _get() too, XXX */
305 * We have to keep track of the effective live-view blockref count
306 * so the create code knows when to push an indirect block.
308 if (flags & HAMMER2_CHAIN_INSERT_LIVE)
309 atomic_add_int(&parent->core.live_count, 1);
311 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
312 hammer2_spin_unex(&parent->core.spin);
317 * Drop the caller's reference to the chain. When the ref count drops to
318 * zero this function will try to disassociate the chain from its parent and
319 * deallocate it, then recursely drop the parent using the implied ref
320 * from the chain's chain->parent.
322 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain);
325 hammer2_chain_drop(hammer2_chain_t *chain)
330 if (hammer2_debug & 0x200000)
333 kprintf("DROP %p %d %08x\n", chain, chain->refs - 1, chain->flags);
337 if (chain->flags & HAMMER2_CHAIN_UPDATE)
339 if (chain->flags & HAMMER2_CHAIN_MODIFIED)
341 KKASSERT(chain->refs > need);
349 chain = hammer2_chain_lastdrop(chain);
351 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
353 /* retry the same chain */
359 * Safe handling of the 1->0 transition on chain. Returns a chain for
360 * recursive drop or NULL, possibly returning the same chain if the atomic
363 * Whem two chains need to be recursively dropped we use the chain
364 * we would otherwise free to placehold the additional chain. It's a bit
365 * convoluted but we can't just recurse without potentially blowing out
368 * The chain cannot be freed if it has any children.
370 * The core spinlock is allowed nest child-to-parent (not parent-to-child).
374 hammer2_chain_lastdrop(hammer2_chain_t *chain)
378 hammer2_chain_t *parent;
379 hammer2_chain_t *rdrop;
382 * Spinlock the core and check to see if it is empty. If it is
383 * not empty we leave chain intact with refs == 0. The elements
384 * in core->rbtree are associated with other chains contemporary
385 * with ours but not with our chain directly.
387 hammer2_spin_ex(&chain->core.spin);
390 * We can't free non-stale chains with children until we are
391 * able to free the children because there might be a flush
392 * dependency. Flushes of stale children (which should also
393 * have their deleted flag set) short-cut recursive flush
394 * dependencies and can be freed here. Any flushes which run
395 * through stale children due to the flush synchronization
396 * point should have a FLUSH_* bit set in the chain and not
397 * reach lastdrop at this time.
399 * NOTE: We return (chain) on failure to retry.
401 if (chain->core.chain_count) {
402 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
403 hammer2_spin_unex(&chain->core.spin);
404 chain = NULL; /* success */
406 hammer2_spin_unex(&chain->core.spin);
410 /* no chains left under us */
413 * chain->core has no children left so no accessors can get to our
414 * chain from there. Now we have to lock the parent core to interlock
415 * remaining possible accessors that might bump chain's refs before
416 * we can safely drop chain's refs with intent to free the chain.
419 pmp = chain->pmp; /* can be NULL */
423 * Spinlock the parent and try to drop the last ref on chain.
424 * On success remove chain from its parent, otherwise return NULL.
426 * (normal core locks are top-down recursive but we define core
427 * spinlocks as bottom-up recursive, so this is safe).
429 if ((parent = chain->parent) != NULL) {
430 hammer2_spin_ex(&parent->core.spin);
431 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
432 /* 1->0 transition failed */
433 hammer2_spin_unex(&parent->core.spin);
434 hammer2_spin_unex(&chain->core.spin);
435 return(chain); /* retry */
439 * 1->0 transition successful, remove chain from its
442 if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
443 RB_REMOVE(hammer2_chain_tree,
444 &parent->core.rbtree, chain);
445 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
446 --parent->core.chain_count;
447 chain->parent = NULL;
451 * If our chain was the last chain in the parent's core the
452 * core is now empty and its parent might have to be
453 * re-dropped if it has 0 refs.
455 if (parent->core.chain_count == 0) {
457 if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0) {
461 hammer2_spin_unex(&parent->core.spin);
462 parent = NULL; /* safety */
466 * Successful 1->0 transition and the chain can be destroyed now.
468 * We still have the core spinlock, and core's chain_count is 0.
469 * Any parent spinlock is gone.
471 hammer2_spin_unex(&chain->core.spin);
472 KKASSERT(RB_EMPTY(&chain->core.rbtree) &&
473 chain->core.chain_count == 0);
476 * All spin locks are gone, finish freeing stuff.
478 KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE |
479 HAMMER2_CHAIN_MODIFIED)) == 0);
480 hammer2_chain_drop_data(chain, 1);
482 KKASSERT(chain->dio == NULL);
485 * Once chain resources are gone we can use the now dead chain
486 * structure to placehold what might otherwise require a recursive
487 * drop, because we have potentially two things to drop and can only
488 * return one directly.
490 if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
491 chain->flags &= ~HAMMER2_CHAIN_ALLOCATED;
493 kfree(chain, hmp->mchain);
497 * Possible chaining loop when parent re-drop needed.
503 * On either last lock release or last drop
506 hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop)
508 /*hammer2_dev_t *hmp = chain->hmp;*/
510 switch(chain->bref.type) {
511 case HAMMER2_BREF_TYPE_VOLUME:
512 case HAMMER2_BREF_TYPE_FREEMAP:
517 KKASSERT(chain->data == NULL);
523 * Lock a referenced chain element, acquiring its data with I/O if necessary,
524 * and specify how you would like the data to be resolved.
526 * If an I/O or other fatal error occurs, chain->error will be set to non-zero.
528 * The lock is allowed to recurse, multiple locking ops will aggregate
529 * the requested resolve types. Once data is assigned it will not be
530 * removed until the last unlock.
532 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
533 * (typically used to avoid device/logical buffer
536 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
537 * the INITIAL-create state (indirect blocks only).
539 * Do not resolve data elements for DATA chains.
540 * (typically used to avoid device/logical buffer
543 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
545 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
546 * it will be locked exclusive.
548 * NOTE: Embedded elements (volume header, inodes) are always resolved
551 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
552 * element will instantiate and zero its buffer, and flush it on
555 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
556 * so as not to instantiate a device buffer, which could alias against
557 * a logical file buffer. However, if ALWAYS is specified the
558 * device buffer will be instantiated anyway.
560 * WARNING! This function blocks on I/O if data needs to be fetched. This
561 * blocking can run concurrent with other compatible lock holders
562 * who do not need data returning. The lock is not upgraded to
563 * exclusive during a data fetch, a separate bit is used to
564 * interlock I/O. However, an exclusive lock holder can still count
565 * on being interlocked against an I/O fetch managed by a shared
569 hammer2_chain_lock(hammer2_chain_t *chain, int how)
572 * Ref and lock the element. Recursive locks are allowed.
574 KKASSERT(chain->refs > 0);
575 atomic_add_int(&chain->lockcnt, 1);
578 * Get the appropriate lock.
580 if (how & HAMMER2_RESOLVE_SHARED)
581 hammer2_mtx_sh(&chain->lock);
583 hammer2_mtx_ex(&chain->lock);
586 * If we already have a valid data pointer no further action is
593 * Do we have to resolve the data?
595 switch(how & HAMMER2_RESOLVE_MASK) {
596 case HAMMER2_RESOLVE_NEVER:
598 case HAMMER2_RESOLVE_MAYBE:
599 if (chain->flags & HAMMER2_CHAIN_INITIAL)
601 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
604 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
606 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
610 case HAMMER2_RESOLVE_ALWAYS:
616 * Caller requires data
618 hammer2_chain_load_data(chain);
622 * Issue I/O and install chain->data. Caller must hold a chain lock, lock
623 * may be of any type.
625 * Once chain->data is set it cannot be disposed of until all locks are
629 hammer2_chain_load_data(hammer2_chain_t *chain)
631 hammer2_blockref_t *bref;
637 * Degenerate case, data already present.
643 KKASSERT(hmp != NULL);
646 * Gain the IOINPROG bit, interlocked block.
652 oflags = chain->flags;
654 if (oflags & HAMMER2_CHAIN_IOINPROG) {
655 nflags = oflags | HAMMER2_CHAIN_IOSIGNAL;
656 tsleep_interlock(&chain->flags, 0);
657 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
658 tsleep(&chain->flags, PINTERLOCKED,
663 nflags = oflags | HAMMER2_CHAIN_IOINPROG;
664 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
672 * We own CHAIN_IOINPROG
674 * Degenerate case if we raced another load.
680 * We must resolve to a device buffer, either by issuing I/O or
681 * by creating a zero-fill element. We do not mark the buffer
682 * dirty when creating a zero-fill element (the hammer2_chain_modify()
683 * API must still be used to do that).
685 * The device buffer is variable-sized in powers of 2 down
686 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
687 * chunk always contains buffers of the same size. (XXX)
689 * The minimum physical IO size may be larger than the variable
695 * The getblk() optimization can only be used on newly created
696 * elements if the physical block size matches the request.
698 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
699 error = hammer2_io_new(hmp, bref->data_off, chain->bytes,
702 error = hammer2_io_bread(hmp, bref->data_off, chain->bytes,
704 hammer2_adjreadcounter(&chain->bref, chain->bytes);
707 chain->error = HAMMER2_ERROR_IO;
708 kprintf("hammer2_chain_lock: I/O error %016jx: %d\n",
709 (intmax_t)bref->data_off, error);
710 hammer2_io_bqrelse(&chain->dio);
716 * NOTE: A locked chain's data cannot be modified without first
717 * calling hammer2_chain_modify().
721 * Clear INITIAL. In this case we used io_new() and the buffer has
722 * been zero'd and marked dirty.
724 bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
725 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
726 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
727 chain->bref.flags |= HAMMER2_BREF_FLAG_ZERO;
728 } else if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
730 * check data not currently synchronized due to
731 * modification. XXX assumes data stays in the buffer
732 * cache, which might not be true (need biodep on flush
733 * to calculate crc? or simple crc?).
736 if (hammer2_chain_testcheck(chain, bdata) == 0) {
737 kprintf("chain %016jx.%02x meth=%02x "
738 "CHECK FAIL %08x (flags=%08x)\n",
739 chain->bref.data_off,
742 hammer2_icrc32(bdata, chain->bytes),
744 chain->error = HAMMER2_ERROR_CHECK;
749 * Setup the data pointer, either pointing it to an embedded data
750 * structure and copying the data from the buffer, or pointing it
753 * The buffer is not retained when copying to an embedded data
754 * structure in order to avoid potential deadlocks or recursions
755 * on the same physical buffer.
757 * WARNING! Other threads can start using the data the instant we
758 * set chain->data non-NULL.
760 switch (bref->type) {
761 case HAMMER2_BREF_TYPE_VOLUME:
762 case HAMMER2_BREF_TYPE_FREEMAP:
764 * Copy data from bp to embedded buffer
766 panic("hammer2_chain_lock: called on unresolved volume header");
768 case HAMMER2_BREF_TYPE_INODE:
769 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
770 case HAMMER2_BREF_TYPE_INDIRECT:
771 case HAMMER2_BREF_TYPE_DATA:
772 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
775 * Point data at the device buffer and leave dio intact.
777 chain->data = (void *)bdata;
782 * Release HAMMER2_CHAIN_IOINPROG and signal waiters if requested.
789 oflags = chain->flags;
790 nflags = oflags & ~(HAMMER2_CHAIN_IOINPROG |
791 HAMMER2_CHAIN_IOSIGNAL);
792 KKASSERT(oflags & HAMMER2_CHAIN_IOINPROG);
793 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
794 if (oflags & HAMMER2_CHAIN_IOSIGNAL)
795 wakeup(&chain->flags);
802 * Unlock and deref a chain element.
804 * On the last lock release any non-embedded data (chain->dio) will be
808 hammer2_chain_unlock(hammer2_chain_t *chain)
810 hammer2_mtx_state_t ostate;
815 * If multiple locks are present (or being attempted) on this
816 * particular chain we can just unlock, drop refs, and return.
818 * Otherwise fall-through on the 1->0 transition.
821 lockcnt = chain->lockcnt;
822 KKASSERT(lockcnt > 0);
825 if (atomic_cmpset_int(&chain->lockcnt,
826 lockcnt, lockcnt - 1)) {
827 hammer2_mtx_unlock(&chain->lock);
831 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
838 * On the 1->0 transition we upgrade the core lock (if necessary)
839 * to exclusive for terminal processing. If after upgrading we find
840 * that lockcnt is non-zero, another thread is racing us and will
841 * handle the unload for us later on, so just cleanup and return
842 * leaving the data/io intact
844 * Otherwise if lockcnt is still 0 it is possible for it to become
845 * non-zero and race, but since we hold the core->lock exclusively
846 * all that will happen is that the chain will be reloaded after we
849 ostate = hammer2_mtx_upgrade(&chain->lock);
850 if (chain->lockcnt) {
851 hammer2_mtx_unlock(&chain->lock);
856 * Shortcut the case if the data is embedded or not resolved.
858 * Do NOT NULL out chain->data (e.g. inode data), it might be
861 if (chain->dio == NULL) {
862 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0)
863 hammer2_chain_drop_data(chain, 0);
864 hammer2_mtx_unlock(&chain->lock);
871 if (hammer2_io_isdirty(chain->dio)) {
872 switch(chain->bref.type) {
873 case HAMMER2_BREF_TYPE_DATA:
874 counterp = &hammer2_iod_file_write;
876 case HAMMER2_BREF_TYPE_INODE:
877 counterp = &hammer2_iod_meta_write;
879 case HAMMER2_BREF_TYPE_INDIRECT:
880 counterp = &hammer2_iod_indr_write;
882 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
883 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
884 counterp = &hammer2_iod_fmap_write;
887 counterp = &hammer2_iod_volu_write;
890 *counterp += chain->bytes;
896 * NOTE: Freemap leaf's use reserved blocks and thus no aliasing
900 hammer2_io_bqrelse(&chain->dio);
901 hammer2_mtx_unlock(&chain->lock);
905 * This counts the number of live blockrefs in a block array and
906 * also calculates the point at which all remaining blockrefs are empty.
907 * This routine can only be called on a live chain (DUPLICATED flag not set).
909 * NOTE: Flag is not set until after the count is complete, allowing
910 * callers to test the flag without holding the spinlock.
912 * NOTE: If base is NULL the related chain is still in the INITIAL
913 * state and there are no blockrefs to count.
915 * NOTE: live_count may already have some counts accumulated due to
916 * creation and deletion and could even be initially negative.
919 hammer2_chain_countbrefs(hammer2_chain_t *chain,
920 hammer2_blockref_t *base, int count)
922 hammer2_spin_ex(&chain->core.spin);
923 if ((chain->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) {
925 while (--count >= 0) {
926 if (base[count].type)
929 chain->core.live_zero = count + 1;
931 if (base[count].type)
932 atomic_add_int(&chain->core.live_count,
937 chain->core.live_zero = 0;
939 /* else do not modify live_count */
940 atomic_set_int(&chain->flags, HAMMER2_CHAIN_COUNTEDBREFS);
942 hammer2_spin_unex(&chain->core.spin);
946 * Resize the chain's physical storage allocation in-place. This function does
947 * not adjust the data pointer and must be followed by (typically) a
948 * hammer2_chain_modify() call to copy any old data over and adjust the
951 * Chains can be resized smaller without reallocating the storage. Resizing
952 * larger will reallocate the storage. Excess or prior storage is reclaimed
953 * asynchronously at a later time.
955 * Must be passed an exclusively locked parent and chain.
957 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
958 * to avoid instantiating a device buffer that conflicts with the vnode data
959 * buffer. However, because H2 can compress or encrypt data, the chain may
960 * have a dio assigned to it in those situations, and they do not conflict.
962 * XXX return error if cannot resize.
965 hammer2_chain_resize(hammer2_inode_t *ip,
966 hammer2_chain_t *parent, hammer2_chain_t *chain,
967 hammer2_tid_t mtid, hammer2_off_t dedup_off,
968 int nradix, int flags)
977 * Only data and indirect blocks can be resized for now.
978 * (The volu root, inodes, and freemap elements use a fixed size).
980 KKASSERT(chain != &hmp->vchain);
981 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
982 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
983 KKASSERT(chain->parent == parent);
986 * Nothing to do if the element is already the proper size
988 obytes = chain->bytes;
989 nbytes = 1U << nradix;
990 if (obytes == nbytes)
994 * Make sure the old data is instantiated so we can copy it. If this
995 * is a data block, the device data may be superfluous since the data
996 * might be in a logical block, but compressed or encrypted data is
999 * NOTE: The modify will set BMAPUPD for us if BMAPPED is set.
1001 hammer2_chain_modify(chain, mtid, dedup_off, 0);
1004 * Relocate the block, even if making it smaller (because different
1005 * block sizes may be in different regions).
1007 * (data blocks only, we aren't copying the storage here).
1009 hammer2_freemap_alloc(chain, nbytes);
1010 chain->bytes = nbytes;
1011 /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */
1014 * We don't want the followup chain_modify() to try to copy data
1015 * from the old (wrong-sized) buffer. It won't know how much to
1016 * copy. This case should only occur during writes when the
1017 * originator already has the data to write in-hand.
1020 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA);
1021 hammer2_io_brelse(&chain->dio);
1027 * Set the chain modified so its data can be changed by the caller.
1029 * Sets bref.modify_tid to mtid only if mtid != 0. Note that bref.modify_tid
1030 * is a CLC (cluster level change) field and is not updated by parent
1031 * propagation during a flush.
1033 * If the caller passes a non-zero dedup_off we assign data_off to that
1034 * instead of allocating a ne block. Caller must not modify the data already
1035 * present at the target offset.
1038 hammer2_chain_modify(hammer2_chain_t *chain, hammer2_tid_t mtid,
1039 hammer2_off_t dedup_off, int flags)
1041 hammer2_blockref_t obref;
1050 obref = chain->bref;
1051 KKASSERT((chain->flags & HAMMER2_CHAIN_FICTITIOUS) == 0);
1054 * Data is not optional for freemap chains (we must always be sure
1055 * to copy the data on COW storage allocations).
1057 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1058 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1059 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1060 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1064 * Data must be resolved if already assigned, unless explicitly
1065 * flagged otherwise.
1067 if (chain->data == NULL && (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1068 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1069 hammer2_chain_load_data(chain);
1073 * Set MODIFIED to indicate that the chain has been modified.
1074 * Set UPDATE to ensure that the blockref is updated in the parent.
1076 if ((chain->flags & (HAMMER2_CHAIN_DEDUP | HAMMER2_CHAIN_MODIFIED)) ==
1077 (HAMMER2_CHAIN_DEDUP | HAMMER2_CHAIN_MODIFIED)) {
1079 } else if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1080 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1081 hammer2_chain_ref(chain);
1082 hammer2_pfs_memory_inc(chain->pmp); /* can be NULL */
1087 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
1088 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1089 hammer2_chain_ref(chain);
1091 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DEDUP);
1094 * The modification or re-modification requires an allocation and
1097 * If dedup_off is non-zero, caller already has a data offset
1098 * containing the caller's desired data. The dedup offset is
1099 * allowed to be in a partially free state and we must be sure
1100 * to reset it to a fully allocated state to force two bulkfree
1101 * passes to free it again.
1103 * XXX can a chain already be marked MODIFIED without a data
1104 * assignment? If not, assert here instead of testing the case.
1106 if (chain != &hmp->vchain && chain != &hmp->fchain) {
1107 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1111 chain->bref.data_off = dedup_off;
1112 chain->bytes = 1 << (dedup_off &
1113 HAMMER2_OFF_MASK_RADIX);
1114 atomic_set_int(&chain->flags,
1115 HAMMER2_CHAIN_DEDUP);
1116 hammer2_freemap_adjust(hmp, &chain->bref,
1117 HAMMER2_FREEMAP_DORECOVER);
1119 hammer2_freemap_alloc(chain, chain->bytes);
1121 /* XXX failed allocation */
1126 * Update mirror_tid and modify_tid. modify_tid is only updated
1127 * if not passed as zero (during flushes, parent propagation passes
1130 * NOTE: chain->pmp could be the device spmp.
1132 chain->bref.mirror_tid = hmp->voldata.mirror_tid + 1;
1134 chain->bref.modify_tid = mtid;
1137 * Set BMAPUPD to tell the flush code that an existing blockmap entry
1138 * requires updating as well as to tell the delete code that the
1139 * chain's blockref might not exactly match (in terms of physical size
1140 * or block offset) the one in the parent's blocktable. The base key
1141 * of course will still match.
1143 if (chain->flags & HAMMER2_CHAIN_BMAPPED)
1144 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD);
1147 * Short-cut data blocks which the caller does not need an actual
1148 * data reference to (aka OPTDATA), as long as the chain does not
1149 * already have a data pointer to the data. This generally means
1150 * that the modifications are being done via the logical buffer cache.
1151 * The INITIAL flag relates only to the device data buffer and thus
1152 * remains unchange in this situation.
1154 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA &&
1155 (flags & HAMMER2_MODIFY_OPTDATA) &&
1156 chain->data == NULL) {
1161 * Clearing the INITIAL flag (for indirect blocks) indicates that
1162 * we've processed the uninitialized storage allocation.
1164 * If this flag is already clear we are likely in a copy-on-write
1165 * situation but we have to be sure NOT to bzero the storage if
1166 * no data is present.
1168 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1169 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1176 * Instantiate data buffer and possibly execute COW operation
1178 switch(chain->bref.type) {
1179 case HAMMER2_BREF_TYPE_VOLUME:
1180 case HAMMER2_BREF_TYPE_FREEMAP:
1182 * The data is embedded, no copy-on-write operation is
1185 KKASSERT(chain->dio == NULL);
1187 case HAMMER2_BREF_TYPE_INODE:
1188 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1189 case HAMMER2_BREF_TYPE_DATA:
1190 case HAMMER2_BREF_TYPE_INDIRECT:
1191 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1193 * Perform the copy-on-write operation
1195 * zero-fill or copy-on-write depending on whether
1196 * chain->data exists or not and set the dirty state for
1197 * the new buffer. hammer2_io_new() will handle the
1200 * If a dedup_off was supplied this is an existing block
1201 * and no COW, copy, or further modification is required.
1203 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1205 if (wasinitial && dedup_off == 0) {
1206 error = hammer2_io_new(hmp, chain->bref.data_off,
1207 chain->bytes, &dio);
1209 error = hammer2_io_bread(hmp, chain->bref.data_off,
1210 chain->bytes, &dio);
1212 hammer2_adjreadcounter(&chain->bref, chain->bytes);
1215 * If an I/O error occurs make sure callers cannot accidently
1216 * modify the old buffer's contents and corrupt the filesystem.
1219 kprintf("hammer2_chain_modify: hmp=%p I/O error\n",
1221 chain->error = HAMMER2_ERROR_IO;
1222 hammer2_io_brelse(&dio);
1223 hammer2_io_brelse(&chain->dio);
1228 bdata = hammer2_io_data(dio, chain->bref.data_off);
1232 * COW (unless a dedup).
1234 KKASSERT(chain->dio != NULL);
1235 if (chain->data != (void *)bdata && dedup_off == 0) {
1236 bcopy(chain->data, bdata, chain->bytes);
1238 } else if (wasinitial == 0) {
1240 * We have a problem. We were asked to COW but
1241 * we don't have any data to COW with!
1243 panic("hammer2_chain_modify: having a COW %p\n",
1248 * Retire the old buffer, replace with the new. Dirty or
1249 * redirty the new buffer.
1251 * WARNING! The system buffer cache may have already flushed
1252 * the buffer, so we must be sure to [re]dirty it
1253 * for further modification.
1255 * If dedup_off was supplied, the caller is not
1256 * expected to make any further modification to the
1260 hammer2_io_bqrelse(&chain->dio);
1261 chain->data = (void *)bdata;
1264 hammer2_io_setdirty(dio);
1267 panic("hammer2_chain_modify: illegal non-embedded type %d",
1274 * setflush on parent indicating that the parent must recurse down
1275 * to us. Do not call on chain itself which might already have it
1279 hammer2_chain_setflush(chain->parent);
1283 * Modify the chain associated with an inode.
1286 hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
1287 hammer2_tid_t mtid, int flags)
1289 hammer2_inode_modify(ip);
1290 hammer2_chain_modify(chain, mtid, 0, flags);
1294 * Volume header data locks
1297 hammer2_voldata_lock(hammer2_dev_t *hmp)
1299 lockmgr(&hmp->vollk, LK_EXCLUSIVE);
1303 hammer2_voldata_unlock(hammer2_dev_t *hmp)
1305 lockmgr(&hmp->vollk, LK_RELEASE);
1309 hammer2_voldata_modify(hammer2_dev_t *hmp)
1311 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1312 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
1313 hammer2_chain_ref(&hmp->vchain);
1314 hammer2_pfs_memory_inc(hmp->vchain.pmp);
1319 * This function returns the chain at the nearest key within the specified
1320 * range. The returned chain will be referenced but not locked.
1322 * This function will recurse through chain->rbtree as necessary and will
1323 * return a *key_nextp suitable for iteration. *key_nextp is only set if
1324 * the iteration value is less than the current value of *key_nextp.
1326 * The caller should use (*key_nextp) to calculate the actual range of
1327 * the returned element, which will be (key_beg to *key_nextp - 1), because
1328 * there might be another element which is superior to the returned element
1331 * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
1332 * chains continue to be returned. On EOF (*key_nextp) may overflow since
1333 * it will wind up being (key_end + 1).
1335 * WARNING! Must be called with child's spinlock held. Spinlock remains
1336 * held through the operation.
1338 struct hammer2_chain_find_info {
1339 hammer2_chain_t *best;
1340 hammer2_key_t key_beg;
1341 hammer2_key_t key_end;
1342 hammer2_key_t key_next;
1345 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
1346 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
1350 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
1351 hammer2_key_t key_beg, hammer2_key_t key_end)
1353 struct hammer2_chain_find_info info;
1356 info.key_beg = key_beg;
1357 info.key_end = key_end;
1358 info.key_next = *key_nextp;
1360 RB_SCAN(hammer2_chain_tree, &parent->core.rbtree,
1361 hammer2_chain_find_cmp, hammer2_chain_find_callback,
1363 *key_nextp = info.key_next;
1365 kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
1366 parent, key_beg, key_end, *key_nextp);
1374 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
1376 struct hammer2_chain_find_info *info = data;
1377 hammer2_key_t child_beg;
1378 hammer2_key_t child_end;
1380 child_beg = child->bref.key;
1381 child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
1383 if (child_end < info->key_beg)
1385 if (child_beg > info->key_end)
1392 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
1394 struct hammer2_chain_find_info *info = data;
1395 hammer2_chain_t *best;
1396 hammer2_key_t child_end;
1399 * WARNING! Do not discard DUPLICATED chains, it is possible that
1400 * we are catching an insertion half-way done. If a
1401 * duplicated chain turns out to be the best choice the
1402 * caller will re-check its flags after locking it.
1404 * WARNING! Layerq is scanned forwards, exact matches should keep
1405 * the existing info->best.
1407 if ((best = info->best) == NULL) {
1409 * No previous best. Assign best
1412 } else if (best->bref.key <= info->key_beg &&
1413 child->bref.key <= info->key_beg) {
1418 /*info->best = child;*/
1419 } else if (child->bref.key < best->bref.key) {
1421 * Child has a nearer key and best is not flush with key_beg.
1422 * Set best to child. Truncate key_next to the old best key.
1425 if (info->key_next > best->bref.key || info->key_next == 0)
1426 info->key_next = best->bref.key;
1427 } else if (child->bref.key == best->bref.key) {
1429 * If our current best is flush with the child then this
1430 * is an illegal overlap.
1432 * key_next will automatically be limited to the smaller of
1433 * the two end-points.
1439 * Keep the current best but truncate key_next to the child's
1442 * key_next will also automatically be limited to the smaller
1443 * of the two end-points (probably not necessary for this case
1444 * but we do it anyway).
1446 if (info->key_next > child->bref.key || info->key_next == 0)
1447 info->key_next = child->bref.key;
1451 * Always truncate key_next based on child's end-of-range.
1453 child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
1454 if (child_end && (info->key_next > child_end || info->key_next == 0))
1455 info->key_next = child_end;
1461 * Retrieve the specified chain from a media blockref, creating the
1462 * in-memory chain structure which reflects it.
1464 * To handle insertion races pass the INSERT_RACE flag along with the
1465 * generation number of the core. NULL will be returned if the generation
1466 * number changes before we have a chance to insert the chain. Insert
1467 * races can occur because the parent might be held shared.
1469 * Caller must hold the parent locked shared or exclusive since we may
1470 * need the parent's bref array to find our block.
1472 * WARNING! chain->pmp is always set to NULL for any chain representing
1473 * part of the super-root topology.
1476 hammer2_chain_get(hammer2_chain_t *parent, int generation,
1477 hammer2_blockref_t *bref)
1479 hammer2_dev_t *hmp = parent->hmp;
1480 hammer2_chain_t *chain;
1484 * Allocate a chain structure representing the existing media
1485 * entry. Resulting chain has one ref and is not locked.
1487 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
1488 chain = hammer2_chain_alloc(hmp, NULL, bref);
1490 chain = hammer2_chain_alloc(hmp, parent->pmp, bref);
1491 /* ref'd chain returned */
1494 * Flag that the chain is in the parent's blockmap so delete/flush
1495 * knows what to do with it.
1497 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
1500 * Link the chain into its parent. A spinlock is required to safely
1501 * access the RBTREE, and it is possible to collide with another
1502 * hammer2_chain_get() operation because the caller might only hold
1503 * a shared lock on the parent.
1505 KKASSERT(parent->refs > 0);
1506 error = hammer2_chain_insert(parent, chain,
1507 HAMMER2_CHAIN_INSERT_SPIN |
1508 HAMMER2_CHAIN_INSERT_RACE,
1511 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
1512 kprintf("chain %p get race\n", chain);
1513 hammer2_chain_drop(chain);
1516 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
1520 * Return our new chain referenced but not locked, or NULL if
1527 * Lookup initialization/completion API
1530 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
1532 hammer2_chain_ref(parent);
1533 if (flags & HAMMER2_LOOKUP_SHARED) {
1534 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1535 HAMMER2_RESOLVE_SHARED);
1537 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1543 hammer2_chain_lookup_done(hammer2_chain_t *parent)
1546 hammer2_chain_unlock(parent);
1547 hammer2_chain_drop(parent);
1552 hammer2_chain_getparent(hammer2_chain_t **parentp, int how)
1554 hammer2_chain_t *oparent;
1555 hammer2_chain_t *nparent;
1558 * Be careful of order, oparent must be unlocked before nparent
1559 * is locked below to avoid a deadlock.
1562 hammer2_spin_ex(&oparent->core.spin);
1563 nparent = oparent->parent;
1564 hammer2_chain_ref(nparent);
1565 hammer2_spin_unex(&oparent->core.spin);
1567 hammer2_chain_unlock(oparent);
1568 hammer2_chain_drop(oparent);
1572 hammer2_chain_lock(nparent, how);
1579 * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
1580 * (*parentp) typically points to an inode but can also point to a related
1581 * indirect block and this function will recurse upwards and find the inode
1584 * (*parentp) must be exclusively locked and referenced and can be an inode
1585 * or an existing indirect block within the inode.
1587 * On return (*parentp) will be modified to point at the deepest parent chain
1588 * element encountered during the search, as a helper for an insertion or
1589 * deletion. The new (*parentp) will be locked and referenced and the old
1590 * will be unlocked and dereferenced (no change if they are both the same).
1592 * The matching chain will be returned exclusively locked. If NOLOCK is
1593 * requested the chain will be returned only referenced. Note that the
1594 * parent chain must always be locked shared or exclusive, matching the
1595 * HAMMER2_LOOKUP_SHARED flag. We can conceivably lock it SHARED temporarily
1596 * when NOLOCK is specified but that complicates matters if *parentp must
1597 * inherit the chain.
1599 * NOLOCK also implies NODATA, since an unlocked chain usually has a NULL
1600 * data pointer or can otherwise be in flux.
1602 * NULL is returned if no match was found, but (*parentp) will still
1603 * potentially be adjusted.
1605 * If a fatal error occurs (typically an I/O error), a dummy chain is
1606 * returned with chain->error and error-identifying information set. This
1607 * chain will assert if you try to do anything fancy with it.
1609 * XXX Depending on where the error occurs we should allow continued iteration.
1611 * On return (*key_nextp) will point to an iterative value for key_beg.
1612 * (If NULL is returned (*key_nextp) is set to (key_end + 1)).
1614 * This function will also recurse up the chain if the key is not within the
1615 * current parent's range. (*parentp) can never be set to NULL. An iteration
1616 * can simply allow (*parentp) to float inside the loop.
1618 * NOTE! chain->data is not always resolved. By default it will not be
1619 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
1620 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
1621 * BREF_TYPE_DATA as the device buffer can alias the logical file
1625 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
1626 hammer2_key_t key_beg, hammer2_key_t key_end,
1627 int *cache_indexp, int flags)
1630 hammer2_chain_t *parent;
1631 hammer2_chain_t *chain;
1632 hammer2_blockref_t *base;
1633 hammer2_blockref_t *bref;
1634 hammer2_blockref_t bcopy;
1635 hammer2_key_t scan_beg;
1636 hammer2_key_t scan_end;
1638 int how_always = HAMMER2_RESOLVE_ALWAYS;
1639 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1642 int maxloops = 300000;
1644 if (flags & HAMMER2_LOOKUP_ALWAYS) {
1645 how_maybe = how_always;
1646 how = HAMMER2_RESOLVE_ALWAYS;
1647 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
1648 how = HAMMER2_RESOLVE_NEVER;
1650 how = HAMMER2_RESOLVE_MAYBE;
1652 if (flags & HAMMER2_LOOKUP_SHARED) {
1653 how_maybe |= HAMMER2_RESOLVE_SHARED;
1654 how_always |= HAMMER2_RESOLVE_SHARED;
1655 how |= HAMMER2_RESOLVE_SHARED;
1659 * Recurse (*parentp) upward if necessary until the parent completely
1660 * encloses the key range or we hit the inode.
1662 * This function handles races against the flusher doing a delete-
1663 * duplicate above us and re-homes the parent to the duplicate in
1664 * that case, otherwise we'd wind up recursing down a stale chain.
1669 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1670 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1671 scan_beg = parent->bref.key;
1672 scan_end = scan_beg +
1673 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1674 if (key_beg >= scan_beg && key_end <= scan_end)
1676 parent = hammer2_chain_getparent(parentp, how_maybe);
1680 if (--maxloops == 0)
1681 panic("hammer2_chain_lookup: maxloops");
1683 * Locate the blockref array. Currently we do a fully associative
1684 * search through the array.
1686 switch(parent->bref.type) {
1687 case HAMMER2_BREF_TYPE_INODE:
1689 * Special shortcut for embedded data returns the inode
1690 * itself. Callers must detect this condition and access
1691 * the embedded data (the strategy code does this for us).
1693 * This is only applicable to regular files and softlinks.
1695 if (parent->data->ipdata.meta.op_flags &
1696 HAMMER2_OPFLAG_DIRECTDATA) {
1697 if (flags & HAMMER2_LOOKUP_NODIRECT) {
1699 *key_nextp = key_end + 1;
1702 hammer2_chain_ref(parent);
1703 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0)
1704 hammer2_chain_lock(parent, how_always);
1705 *key_nextp = key_end + 1;
1708 base = &parent->data->ipdata.u.blockset.blockref[0];
1709 count = HAMMER2_SET_COUNT;
1711 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1712 case HAMMER2_BREF_TYPE_INDIRECT:
1714 * Handle MATCHIND on the parent
1716 if (flags & HAMMER2_LOOKUP_MATCHIND) {
1717 scan_beg = parent->bref.key;
1718 scan_end = scan_beg +
1719 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1720 if (key_beg == scan_beg && key_end == scan_end) {
1722 hammer2_chain_ref(chain);
1723 hammer2_chain_lock(chain, how_maybe);
1724 *key_nextp = scan_end + 1;
1729 * Optimize indirect blocks in the INITIAL state to avoid
1732 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1735 if (parent->data == NULL)
1736 panic("parent->data is NULL");
1737 base = &parent->data->npdata[0];
1739 count = parent->bytes / sizeof(hammer2_blockref_t);
1741 case HAMMER2_BREF_TYPE_VOLUME:
1742 base = &parent->data->voldata.sroot_blockset.blockref[0];
1743 count = HAMMER2_SET_COUNT;
1745 case HAMMER2_BREF_TYPE_FREEMAP:
1746 base = &parent->data->blkset.blockref[0];
1747 count = HAMMER2_SET_COUNT;
1750 kprintf("hammer2_chain_lookup: unrecognized "
1751 "blockref(B) type: %d",
1754 tsleep(&base, 0, "dead", 0);
1755 panic("hammer2_chain_lookup: unrecognized "
1756 "blockref(B) type: %d",
1758 base = NULL; /* safety */
1759 count = 0; /* safety */
1763 * Merged scan to find next candidate.
1765 * hammer2_base_*() functions require the parent->core.live_* fields
1766 * to be synchronized.
1768 * We need to hold the spinlock to access the block array and RB tree
1769 * and to interlock chain creation.
1771 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
1772 hammer2_chain_countbrefs(parent, base, count);
1777 hammer2_spin_ex(&parent->core.spin);
1778 chain = hammer2_combined_find(parent, base, count,
1779 cache_indexp, key_nextp,
1782 generation = parent->core.generation;
1785 * Exhausted parent chain, iterate.
1788 hammer2_spin_unex(&parent->core.spin);
1789 if (key_beg == key_end) /* short cut single-key case */
1793 * Stop if we reached the end of the iteration.
1795 if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1796 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1801 * Calculate next key, stop if we reached the end of the
1802 * iteration, otherwise go up one level and loop.
1804 key_beg = parent->bref.key +
1805 ((hammer2_key_t)1 << parent->bref.keybits);
1806 if (key_beg == 0 || key_beg > key_end)
1808 parent = hammer2_chain_getparent(parentp, how_maybe);
1813 * Selected from blockref or in-memory chain.
1815 if (chain == NULL) {
1817 hammer2_spin_unex(&parent->core.spin);
1818 chain = hammer2_chain_get(parent, generation,
1820 if (chain == NULL) {
1821 kprintf("retry lookup parent %p keys %016jx:%016jx\n",
1822 parent, key_beg, key_end);
1825 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
1826 hammer2_chain_drop(chain);
1830 hammer2_chain_ref(chain);
1831 hammer2_spin_unex(&parent->core.spin);
1835 * chain is referenced but not locked. We must lock the chain
1836 * to obtain definitive DUPLICATED/DELETED state
1838 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1839 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1840 hammer2_chain_lock(chain, how_maybe);
1842 hammer2_chain_lock(chain, how);
1846 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
1848 * NOTE: Chain's key range is not relevant as there might be
1849 * one-offs within the range that are not deleted.
1851 * NOTE: Lookups can race delete-duplicate because
1852 * delete-duplicate does not lock the parent's core
1853 * (they just use the spinlock on the core). We must
1854 * check for races by comparing the DUPLICATED flag before
1855 * releasing the spinlock with the flag after locking the
1858 if (chain->flags & HAMMER2_CHAIN_DELETED) {
1859 hammer2_chain_unlock(chain);
1860 hammer2_chain_drop(chain);
1861 key_beg = *key_nextp;
1862 if (key_beg == 0 || key_beg > key_end)
1868 * If the chain element is an indirect block it becomes the new
1869 * parent and we loop on it. We must maintain our top-down locks
1870 * to prevent the flusher from interfering (i.e. doing a
1871 * delete-duplicate and leaving us recursing down a deleted chain).
1873 * The parent always has to be locked with at least RESOLVE_MAYBE
1874 * so we can access its data. It might need a fixup if the caller
1875 * passed incompatible flags. Be careful not to cause a deadlock
1876 * as a data-load requires an exclusive lock.
1878 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
1879 * range is within the requested key range we return the indirect
1880 * block and do NOT loop. This is usually only used to acquire
1883 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1884 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1885 hammer2_chain_unlock(parent);
1886 hammer2_chain_drop(parent);
1887 *parentp = parent = chain;
1892 * All done, return the chain.
1894 * If the caller does not want a locked chain, replace the lock with
1895 * a ref. Perhaps this can eventually be optimized to not obtain the
1896 * lock in the first place for situations where the data does not
1897 * need to be resolved.
1900 if (flags & HAMMER2_LOOKUP_NOLOCK)
1901 hammer2_chain_unlock(chain);
1908 * After having issued a lookup we can iterate all matching keys.
1910 * If chain is non-NULL we continue the iteration from just after it's index.
1912 * If chain is NULL we assume the parent was exhausted and continue the
1913 * iteration at the next parent.
1915 * If a fatal error occurs (typically an I/O error), a dummy chain is
1916 * returned with chain->error and error-identifying information set. This
1917 * chain will assert if you try to do anything fancy with it.
1919 * XXX Depending on where the error occurs we should allow continued iteration.
1921 * parent must be locked on entry and remains locked throughout. chain's
1922 * lock status must match flags. Chain is always at least referenced.
1924 * WARNING! The MATCHIND flag does not apply to this function.
1927 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
1928 hammer2_key_t *key_nextp,
1929 hammer2_key_t key_beg, hammer2_key_t key_end,
1930 int *cache_indexp, int flags)
1932 hammer2_chain_t *parent;
1936 * Calculate locking flags for upward recursion.
1938 how_maybe = HAMMER2_RESOLVE_MAYBE;
1939 if (flags & HAMMER2_LOOKUP_SHARED)
1940 how_maybe |= HAMMER2_RESOLVE_SHARED;
1945 * Calculate the next index and recalculate the parent if necessary.
1948 key_beg = chain->bref.key +
1949 ((hammer2_key_t)1 << chain->bref.keybits);
1950 if ((flags & (HAMMER2_LOOKUP_NOLOCK |
1951 HAMMER2_LOOKUP_NOUNLOCK)) == 0) {
1952 hammer2_chain_unlock(chain);
1954 hammer2_chain_drop(chain);
1957 * chain invalid past this point, but we can still do a
1958 * pointer comparison w/parent.
1960 * Any scan where the lookup returned degenerate data embedded
1961 * in the inode has an invalid index and must terminate.
1963 if (chain == parent)
1965 if (key_beg == 0 || key_beg > key_end)
1968 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1969 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1971 * We reached the end of the iteration.
1976 * Continue iteration with next parent unless the current
1977 * parent covers the range.
1979 key_beg = parent->bref.key +
1980 ((hammer2_key_t)1 << parent->bref.keybits);
1981 if (key_beg == 0 || key_beg > key_end)
1983 parent = hammer2_chain_getparent(parentp, how_maybe);
1989 return (hammer2_chain_lookup(parentp, key_nextp,
1991 cache_indexp, flags));
1995 * The raw scan function is similar to lookup/next but does not seek to a key.
1996 * Blockrefs are iterated via first_bref = (parent, NULL) and
1997 * next_chain = (parent, bref).
1999 * The passed-in parent must be locked and its data resolved. The function
2000 * nominally returns a locked and referenced *chainp != NULL for chains
2001 * the caller might need to recurse on (and will dipose of any *chainp passed
2002 * in). The caller must check the chain->bref.type either way.
2004 * *chainp is not set for leaf elements.
2006 * This function takes a pointer to a stack-based bref structure whos
2007 * contents is updated for each iteration. The same pointer is returned,
2008 * or NULL when the iteration is complete. *firstp must be set to 1 for
2009 * the first ieration. This function will set it to 0.
2011 hammer2_blockref_t *
2012 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t **chainp,
2013 hammer2_blockref_t *bref, int *firstp,
2014 int *cache_indexp, int flags)
2017 hammer2_blockref_t *base;
2018 hammer2_blockref_t *bref_ptr;
2020 hammer2_key_t next_key;
2021 hammer2_chain_t *chain = NULL;
2023 int how_always = HAMMER2_RESOLVE_ALWAYS;
2024 int how_maybe = HAMMER2_RESOLVE_MAYBE;
2027 int maxloops = 300000;
2032 * Scan flags borrowed from lookup.
2034 if (flags & HAMMER2_LOOKUP_ALWAYS) {
2035 how_maybe = how_always;
2036 how = HAMMER2_RESOLVE_ALWAYS;
2037 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2038 how = HAMMER2_RESOLVE_NEVER;
2040 how = HAMMER2_RESOLVE_MAYBE;
2042 if (flags & HAMMER2_LOOKUP_SHARED) {
2043 how_maybe |= HAMMER2_RESOLVE_SHARED;
2044 how_always |= HAMMER2_RESOLVE_SHARED;
2045 how |= HAMMER2_RESOLVE_SHARED;
2049 * Calculate key to locate first/next element, unlocking the previous
2050 * element as we go. Be careful, the key calculation can overflow.
2052 * (also reset bref to NULL)
2058 key = bref->key + ((hammer2_key_t)1 << bref->keybits);
2059 if ((chain = *chainp) != NULL) {
2061 hammer2_chain_unlock(chain);
2062 hammer2_chain_drop(chain);
2072 KKASSERT(parent->error == 0); /* XXX case not handled yet */
2073 if (--maxloops == 0)
2074 panic("hammer2_chain_scan: maxloops");
2076 * Locate the blockref array. Currently we do a fully associative
2077 * search through the array.
2079 switch(parent->bref.type) {
2080 case HAMMER2_BREF_TYPE_INODE:
2082 * An inode with embedded data has no sub-chains.
2084 * WARNING! Bulk scan code may pass a static chain marked
2085 * as BREF_TYPE_INODE with a copy of the volume
2086 * root blockset to snapshot the volume.
2088 if (parent->data->ipdata.meta.op_flags &
2089 HAMMER2_OPFLAG_DIRECTDATA) {
2093 base = &parent->data->ipdata.u.blockset.blockref[0];
2094 count = HAMMER2_SET_COUNT;
2096 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2097 case HAMMER2_BREF_TYPE_INDIRECT:
2099 * Optimize indirect blocks in the INITIAL state to avoid
2102 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2105 if (parent->data == NULL)
2106 panic("parent->data is NULL");
2107 base = &parent->data->npdata[0];
2109 count = parent->bytes / sizeof(hammer2_blockref_t);
2111 case HAMMER2_BREF_TYPE_VOLUME:
2112 base = &parent->data->voldata.sroot_blockset.blockref[0];
2113 count = HAMMER2_SET_COUNT;
2115 case HAMMER2_BREF_TYPE_FREEMAP:
2116 base = &parent->data->blkset.blockref[0];
2117 count = HAMMER2_SET_COUNT;
2120 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2122 base = NULL; /* safety */
2123 count = 0; /* safety */
2127 * Merged scan to find next candidate.
2129 * hammer2_base_*() functions require the parent->core.live_* fields
2130 * to be synchronized.
2132 * We need to hold the spinlock to access the block array and RB tree
2133 * and to interlock chain creation.
2135 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2136 hammer2_chain_countbrefs(parent, base, count);
2140 hammer2_spin_ex(&parent->core.spin);
2141 chain = hammer2_combined_find(parent, base, count,
2142 cache_indexp, &next_key,
2143 key, HAMMER2_KEY_MAX,
2145 generation = parent->core.generation;
2148 * Exhausted parent chain, we're done.
2150 if (bref_ptr == NULL) {
2151 hammer2_spin_unex(&parent->core.spin);
2152 KKASSERT(chain == NULL);
2158 * Copy into the supplied stack-based blockref.
2163 * Selected from blockref or in-memory chain.
2165 if (chain == NULL) {
2166 switch(bref->type) {
2167 case HAMMER2_BREF_TYPE_INODE:
2168 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2169 case HAMMER2_BREF_TYPE_INDIRECT:
2170 case HAMMER2_BREF_TYPE_VOLUME:
2171 case HAMMER2_BREF_TYPE_FREEMAP:
2173 * Recursion, always get the chain
2175 hammer2_spin_unex(&parent->core.spin);
2176 chain = hammer2_chain_get(parent, generation, bref);
2177 if (chain == NULL) {
2178 kprintf("retry scan parent %p keys %016jx\n",
2182 if (bcmp(bref, bref_ptr, sizeof(*bref))) {
2183 hammer2_chain_drop(chain);
2190 * No recursion, do not waste time instantiating
2191 * a chain, just iterate using the bref.
2193 hammer2_spin_unex(&parent->core.spin);
2198 * Recursion or not we need the chain in order to supply
2201 hammer2_chain_ref(chain);
2202 hammer2_spin_unex(&parent->core.spin);
2206 * chain is referenced but not locked. We must lock the chain
2207 * to obtain definitive DUPLICATED/DELETED state
2210 hammer2_chain_lock(chain, how);
2213 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2215 * NOTE: chain's key range is not relevant as there might be
2216 * one-offs within the range that are not deleted.
2218 * NOTE: XXX this could create problems with scans used in
2219 * situations other than mount-time recovery.
2221 * NOTE: Lookups can race delete-duplicate because
2222 * delete-duplicate does not lock the parent's core
2223 * (they just use the spinlock on the core). We must
2224 * check for races by comparing the DUPLICATED flag before
2225 * releasing the spinlock with the flag after locking the
2228 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
2229 hammer2_chain_unlock(chain);
2230 hammer2_chain_drop(chain);
2243 * All done, return the bref or NULL, supply chain if necessary.
2251 * Create and return a new hammer2 system memory structure of the specified
2252 * key, type and size and insert it under (*parentp). This is a full
2253 * insertion, based on the supplied key/keybits, and may involve creating
2254 * indirect blocks and moving other chains around via delete/duplicate.
2256 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION
2257 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2258 * FULL. This typically means that the caller is creating the chain after
2259 * doing a hammer2_chain_lookup().
2261 * (*parentp) must be exclusive locked and may be replaced on return
2262 * depending on how much work the function had to do.
2264 * (*parentp) must not be errored or this function will assert.
2266 * (*chainp) usually starts out NULL and returns the newly created chain,
2267 * but if the caller desires the caller may allocate a disconnected chain
2268 * and pass it in instead.
2270 * This function should NOT be used to insert INDIRECT blocks. It is
2271 * typically used to create/insert inodes and data blocks.
2273 * Caller must pass-in an exclusively locked parent the new chain is to
2274 * be inserted under, and optionally pass-in a disconnected, exclusively
2275 * locked chain to insert (else we create a new chain). The function will
2276 * adjust (*parentp) as necessary, create or connect the chain, and
2277 * return an exclusively locked chain in *chainp.
2279 * When creating a PFSROOT inode under the super-root, pmp is typically NULL
2280 * and will be reassigned.
2283 hammer2_chain_create(hammer2_chain_t **parentp,
2284 hammer2_chain_t **chainp, hammer2_pfs_t *pmp,
2285 hammer2_key_t key, int keybits, int type, size_t bytes,
2286 hammer2_tid_t mtid, hammer2_off_t dedup_off, int flags)
2289 hammer2_chain_t *chain;
2290 hammer2_chain_t *parent;
2291 hammer2_blockref_t *base;
2292 hammer2_blockref_t dummy;
2296 int maxloops = 300000;
2299 * Topology may be crossing a PFS boundary.
2302 KKASSERT(hammer2_mtx_owned(&parent->lock));
2303 KKASSERT(parent->error == 0);
2307 if (chain == NULL) {
2309 * First allocate media space and construct the dummy bref,
2310 * then allocate the in-memory chain structure. Set the
2311 * INITIAL flag for fresh chains which do not have embedded
2314 * XXX for now set the check mode of the child based on
2315 * the parent or, if the parent is an inode, the
2316 * specification in the inode.
2318 bzero(&dummy, sizeof(dummy));
2321 dummy.keybits = keybits;
2322 dummy.data_off = hammer2_getradix(bytes);
2323 dummy.methods = parent->bref.methods;
2324 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE &&
2326 dummy.methods &= ~HAMMER2_ENC_CHECK(-1);
2327 dummy.methods |= HAMMER2_ENC_CHECK(
2328 parent->data->ipdata.meta.check_algo);
2331 chain = hammer2_chain_alloc(hmp, pmp, &dummy);
2334 * Lock the chain manually, chain_lock will load the chain
2335 * which we do NOT want to do. (note: chain->refs is set
2336 * to 1 by chain_alloc() for us, but lockcnt is not).
2339 hammer2_mtx_ex(&chain->lock);
2343 * Set INITIAL to optimize I/O. The flag will generally be
2344 * processed when we call hammer2_chain_modify().
2346 * Recalculate bytes to reflect the actual media block
2349 bytes = (hammer2_off_t)1 <<
2350 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
2351 chain->bytes = bytes;
2354 case HAMMER2_BREF_TYPE_VOLUME:
2355 case HAMMER2_BREF_TYPE_FREEMAP:
2356 panic("hammer2_chain_create: called with volume type");
2358 case HAMMER2_BREF_TYPE_INDIRECT:
2359 panic("hammer2_chain_create: cannot be used to"
2360 "create indirect block");
2362 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2363 panic("hammer2_chain_create: cannot be used to"
2364 "create freemap root or node");
2366 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2367 KKASSERT(bytes == sizeof(chain->data->bmdata));
2369 case HAMMER2_BREF_TYPE_INODE:
2370 case HAMMER2_BREF_TYPE_DATA:
2373 * leave chain->data NULL, set INITIAL
2375 KKASSERT(chain->data == NULL);
2376 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
2381 * We are reattaching a previously deleted chain, possibly
2382 * under a new parent and possibly with a new key/keybits.
2383 * The chain does not have to be in a modified state. The
2384 * UPDATE flag will be set later on in this routine.
2386 * Do NOT mess with the current state of the INITIAL flag.
2388 chain->bref.key = key;
2389 chain->bref.keybits = keybits;
2390 if (chain->flags & HAMMER2_CHAIN_DELETED)
2391 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2392 KKASSERT(chain->parent == NULL);
2394 if (flags & HAMMER2_INSERT_PFSROOT)
2395 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
2397 chain->bref.flags &= ~HAMMER2_BREF_FLAG_PFSROOT;
2400 * Calculate how many entries we have in the blockref array and
2401 * determine if an indirect block is required.
2404 if (--maxloops == 0)
2405 panic("hammer2_chain_create: maxloops");
2407 switch(parent->bref.type) {
2408 case HAMMER2_BREF_TYPE_INODE:
2409 KKASSERT((parent->data->ipdata.meta.op_flags &
2410 HAMMER2_OPFLAG_DIRECTDATA) == 0);
2411 KKASSERT(parent->data != NULL);
2412 base = &parent->data->ipdata.u.blockset.blockref[0];
2413 count = HAMMER2_SET_COUNT;
2415 case HAMMER2_BREF_TYPE_INDIRECT:
2416 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2417 if (parent->flags & HAMMER2_CHAIN_INITIAL)
2420 base = &parent->data->npdata[0];
2421 count = parent->bytes / sizeof(hammer2_blockref_t);
2423 case HAMMER2_BREF_TYPE_VOLUME:
2424 KKASSERT(parent->data != NULL);
2425 base = &parent->data->voldata.sroot_blockset.blockref[0];
2426 count = HAMMER2_SET_COUNT;
2428 case HAMMER2_BREF_TYPE_FREEMAP:
2429 KKASSERT(parent->data != NULL);
2430 base = &parent->data->blkset.blockref[0];
2431 count = HAMMER2_SET_COUNT;
2434 panic("hammer2_chain_create: unrecognized blockref type: %d",
2442 * Make sure we've counted the brefs
2444 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2445 hammer2_chain_countbrefs(parent, base, count);
2447 KKASSERT(parent->core.live_count >= 0 &&
2448 parent->core.live_count <= count);
2451 * If no free blockref could be found we must create an indirect
2452 * block and move a number of blockrefs into it. With the parent
2453 * locked we can safely lock each child in order to delete+duplicate
2454 * it without causing a deadlock.
2456 * This may return the new indirect block or the old parent depending
2457 * on where the key falls. NULL is returned on error.
2459 if (parent->core.live_count == count) {
2460 hammer2_chain_t *nparent;
2462 nparent = hammer2_chain_create_indirect(parent, key, keybits,
2463 mtid, type, &error);
2464 if (nparent == NULL) {
2466 hammer2_chain_drop(chain);
2470 if (parent != nparent) {
2471 hammer2_chain_unlock(parent);
2472 hammer2_chain_drop(parent);
2473 parent = *parentp = nparent;
2479 * Link the chain into its parent.
2481 if (chain->parent != NULL)
2482 panic("hammer2: hammer2_chain_create: chain already connected");
2483 KKASSERT(chain->parent == NULL);
2484 hammer2_chain_insert(parent, chain,
2485 HAMMER2_CHAIN_INSERT_SPIN |
2486 HAMMER2_CHAIN_INSERT_LIVE,
2491 * Mark the newly created chain modified. This will cause
2492 * UPDATE to be set and process the INITIAL flag.
2494 * Device buffers are not instantiated for DATA elements
2495 * as these are handled by logical buffers.
2497 * Indirect and freemap node indirect blocks are handled
2498 * by hammer2_chain_create_indirect() and not by this
2501 * Data for all other bref types is expected to be
2502 * instantiated (INODE, LEAF).
2504 switch(chain->bref.type) {
2505 case HAMMER2_BREF_TYPE_DATA:
2506 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2507 case HAMMER2_BREF_TYPE_INODE:
2508 hammer2_chain_modify(chain, mtid, dedup_off,
2509 HAMMER2_MODIFY_OPTDATA);
2513 * Remaining types are not supported by this function.
2514 * In particular, INDIRECT and LEAF_NODE types are
2515 * handled by create_indirect().
2517 panic("hammer2_chain_create: bad type: %d",
2524 * When reconnecting a chain we must set UPDATE and
2525 * setflush so the flush recognizes that it must update
2526 * the bref in the parent.
2528 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
2529 hammer2_chain_ref(chain);
2530 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
2535 * We must setflush(parent) to ensure that it recurses through to
2536 * chain. setflush(chain) might not work because ONFLUSH is possibly
2537 * already set in the chain (so it won't recurse up to set it in the
2540 hammer2_chain_setflush(parent);
2549 * Move the chain from its old parent to a new parent. The chain must have
2550 * already been deleted or already disconnected (or never associated) with
2551 * a parent. The chain is reassociated with the new parent and the deleted
2552 * flag will be cleared (no longer deleted). The chain's modification state
2555 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION
2556 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2557 * FULL. This typically means that the caller is creating the chain after
2558 * doing a hammer2_chain_lookup().
2560 * A non-NULL bref is typically passed when key and keybits must be overridden.
2561 * Note that hammer2_cluster_duplicate() *ONLY* uses the key and keybits fields
2562 * from a passed-in bref and uses the old chain's bref for everything else.
2564 * Neither (parent) or (chain) can be errored.
2566 * If (parent) is non-NULL then the new duplicated chain is inserted under
2569 * If (parent) is NULL then the newly duplicated chain is not inserted
2570 * anywhere, similar to if it had just been chain_alloc()'d (suitable for
2571 * passing into hammer2_chain_create() after this function returns).
2573 * WARNING! This function calls create which means it can insert indirect
2574 * blocks. This can cause other unrelated chains in the parent to
2575 * be moved to a newly inserted indirect block in addition to the
2579 hammer2_chain_rename(hammer2_blockref_t *bref,
2580 hammer2_chain_t **parentp, hammer2_chain_t *chain,
2581 hammer2_tid_t mtid, int flags)
2584 hammer2_chain_t *parent;
2588 * WARNING! We should never resolve DATA to device buffers
2589 * (XXX allow it if the caller did?), and since
2590 * we currently do not have the logical buffer cache
2591 * buffer in-hand to fix its cached physical offset
2592 * we also force the modify code to not COW it. XXX
2595 KKASSERT(chain->parent == NULL);
2596 KKASSERT(chain->error == 0);
2599 * Now create a duplicate of the chain structure, associating
2600 * it with the same core, making it the same size, pointing it
2601 * to the same bref (the same media block).
2604 bref = &chain->bref;
2605 bytes = (hammer2_off_t)1 <<
2606 (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
2609 * If parent is not NULL the duplicated chain will be entered under
2610 * the parent and the UPDATE bit set to tell flush to update
2613 * We must setflush(parent) to ensure that it recurses through to
2614 * chain. setflush(chain) might not work because ONFLUSH is possibly
2615 * already set in the chain (so it won't recurse up to set it in the
2618 * Having both chains locked is extremely important for atomicy.
2620 if (parentp && (parent = *parentp) != NULL) {
2621 KKASSERT(hammer2_mtx_owned(&parent->lock));
2622 KKASSERT(parent->refs > 0);
2623 KKASSERT(parent->error == 0);
2625 hammer2_chain_create(parentp, &chain, chain->pmp,
2626 bref->key, bref->keybits, bref->type,
2627 chain->bytes, mtid, 0, flags);
2628 KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE);
2629 hammer2_chain_setflush(*parentp);
2634 * Helper function for deleting chains.
2636 * The chain is removed from the live view (the RBTREE) as well as the parent's
2637 * blockmap. Both chain and its parent must be locked.
2639 * parent may not be errored. chain can be errored.
2642 _hammer2_chain_delete_helper(hammer2_chain_t *parent, hammer2_chain_t *chain,
2643 hammer2_tid_t mtid, int flags)
2647 KKASSERT((chain->flags & (HAMMER2_CHAIN_DELETED |
2648 HAMMER2_CHAIN_FICTITIOUS)) == 0);
2649 KKASSERT(chain->parent == parent);
2652 if (chain->flags & HAMMER2_CHAIN_BMAPPED) {
2654 * Chain is blockmapped, so there must be a parent.
2655 * Atomically remove the chain from the parent and remove
2656 * the blockmap entry.
2658 hammer2_blockref_t *base;
2661 KKASSERT(parent != NULL);
2662 KKASSERT(parent->error == 0);
2663 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
2664 hammer2_chain_modify(parent, mtid, 0, HAMMER2_MODIFY_OPTDATA);
2667 * Calculate blockmap pointer
2669 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
2670 hammer2_spin_ex(&parent->core.spin);
2672 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2673 atomic_add_int(&parent->core.live_count, -1);
2674 ++parent->core.generation;
2675 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2676 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2677 --parent->core.chain_count;
2678 chain->parent = NULL;
2680 switch(parent->bref.type) {
2681 case HAMMER2_BREF_TYPE_INODE:
2683 * Access the inode's block array. However, there
2684 * is no block array if the inode is flagged
2685 * DIRECTDATA. The DIRECTDATA case typicaly only
2686 * occurs when a hardlink has been shifted up the
2687 * tree and the original inode gets replaced with
2688 * an OBJTYPE_HARDLINK placeholding inode.
2691 (parent->data->ipdata.meta.op_flags &
2692 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
2694 &parent->data->ipdata.u.blockset.blockref[0];
2698 count = HAMMER2_SET_COUNT;
2700 case HAMMER2_BREF_TYPE_INDIRECT:
2701 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2703 base = &parent->data->npdata[0];
2706 count = parent->bytes / sizeof(hammer2_blockref_t);
2708 case HAMMER2_BREF_TYPE_VOLUME:
2709 base = &parent->data->voldata.
2710 sroot_blockset.blockref[0];
2711 count = HAMMER2_SET_COUNT;
2713 case HAMMER2_BREF_TYPE_FREEMAP:
2714 base = &parent->data->blkset.blockref[0];
2715 count = HAMMER2_SET_COUNT;
2720 panic("hammer2_flush_pass2: "
2721 "unrecognized blockref type: %d",
2726 * delete blockmapped chain from its parent.
2728 * The parent is not affected by any statistics in chain
2729 * which are pending synchronization. That is, there is
2730 * nothing to undo in the parent since they have not yet
2731 * been incorporated into the parent.
2733 * The parent is affected by statistics stored in inodes.
2734 * Those have already been synchronized, so they must be
2735 * undone. XXX split update possible w/delete in middle?
2738 int cache_index = -1;
2739 hammer2_base_delete(parent, base, count,
2740 &cache_index, chain);
2742 hammer2_spin_unex(&parent->core.spin);
2743 } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
2745 * Chain is not blockmapped but a parent is present.
2746 * Atomically remove the chain from the parent. There is
2747 * no blockmap entry to remove.
2749 * Because chain was associated with a parent but not
2750 * synchronized, the chain's *_count_up fields contain
2751 * inode adjustment statistics which must be undone.
2753 hammer2_spin_ex(&parent->core.spin);
2754 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2755 atomic_add_int(&parent->core.live_count, -1);
2756 ++parent->core.generation;
2757 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2758 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2759 --parent->core.chain_count;
2760 chain->parent = NULL;
2761 hammer2_spin_unex(&parent->core.spin);
2764 * Chain is not blockmapped and has no parent. This
2765 * is a degenerate case.
2767 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2772 * Create an indirect block that covers one or more of the elements in the
2773 * current parent. Either returns the existing parent with no locking or
2774 * ref changes or returns the new indirect block locked and referenced
2775 * and leaving the original parent lock/ref intact as well.
2777 * If an error occurs, NULL is returned and *errorp is set to the error.
2779 * The returned chain depends on where the specified key falls.
2781 * The key/keybits for the indirect mode only needs to follow three rules:
2783 * (1) That all elements underneath it fit within its key space and
2785 * (2) That all elements outside it are outside its key space.
2787 * (3) When creating the new indirect block any elements in the current
2788 * parent that fit within the new indirect block's keyspace must be
2789 * moved into the new indirect block.
2791 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
2792 * keyspace the the current parent, but lookup/iteration rules will
2793 * ensure (and must ensure) that rule (2) for all parents leading up
2794 * to the nearest inode or the root volume header is adhered to. This
2795 * is accomplished by always recursing through matching keyspaces in
2796 * the hammer2_chain_lookup() and hammer2_chain_next() API.
2798 * The current implementation calculates the current worst-case keyspace by
2799 * iterating the current parent and then divides it into two halves, choosing
2800 * whichever half has the most elements (not necessarily the half containing
2801 * the requested key).
2803 * We can also opt to use the half with the least number of elements. This
2804 * causes lower-numbered keys (aka logical file offsets) to recurse through
2805 * fewer indirect blocks and higher-numbered keys to recurse through more.
2806 * This also has the risk of not moving enough elements to the new indirect
2807 * block and being forced to create several indirect blocks before the element
2810 * Must be called with an exclusively locked parent.
2812 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
2813 hammer2_key_t *keyp, int keybits,
2814 hammer2_blockref_t *base, int count);
2815 static int hammer2_chain_indkey_normal(hammer2_chain_t *parent,
2816 hammer2_key_t *keyp, int keybits,
2817 hammer2_blockref_t *base, int count);
2820 hammer2_chain_create_indirect(hammer2_chain_t *parent,
2821 hammer2_key_t create_key, int create_bits,
2822 hammer2_tid_t mtid, int for_type, int *errorp)
2825 hammer2_blockref_t *base;
2826 hammer2_blockref_t *bref;
2827 hammer2_blockref_t bcopy;
2828 hammer2_chain_t *chain;
2829 hammer2_chain_t *ichain;
2830 hammer2_chain_t dummy;
2831 hammer2_key_t key = create_key;
2832 hammer2_key_t key_beg;
2833 hammer2_key_t key_end;
2834 hammer2_key_t key_next;
2835 int keybits = create_bits;
2842 int maxloops = 300000;
2845 * Calculate the base blockref pointer or NULL if the chain
2846 * is known to be empty. We need to calculate the array count
2847 * for RB lookups either way.
2851 KKASSERT(hammer2_mtx_owned(&parent->lock));
2853 /*hammer2_chain_modify(&parent, HAMMER2_MODIFY_OPTDATA);*/
2854 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2857 switch(parent->bref.type) {
2858 case HAMMER2_BREF_TYPE_INODE:
2859 count = HAMMER2_SET_COUNT;
2861 case HAMMER2_BREF_TYPE_INDIRECT:
2862 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2863 count = parent->bytes / sizeof(hammer2_blockref_t);
2865 case HAMMER2_BREF_TYPE_VOLUME:
2866 count = HAMMER2_SET_COUNT;
2868 case HAMMER2_BREF_TYPE_FREEMAP:
2869 count = HAMMER2_SET_COUNT;
2872 panic("hammer2_chain_create_indirect: "
2873 "unrecognized blockref type: %d",
2879 switch(parent->bref.type) {
2880 case HAMMER2_BREF_TYPE_INODE:
2881 base = &parent->data->ipdata.u.blockset.blockref[0];
2882 count = HAMMER2_SET_COUNT;
2884 case HAMMER2_BREF_TYPE_INDIRECT:
2885 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2886 base = &parent->data->npdata[0];
2887 count = parent->bytes / sizeof(hammer2_blockref_t);
2889 case HAMMER2_BREF_TYPE_VOLUME:
2890 base = &parent->data->voldata.
2891 sroot_blockset.blockref[0];
2892 count = HAMMER2_SET_COUNT;
2894 case HAMMER2_BREF_TYPE_FREEMAP:
2895 base = &parent->data->blkset.blockref[0];
2896 count = HAMMER2_SET_COUNT;
2899 panic("hammer2_chain_create_indirect: "
2900 "unrecognized blockref type: %d",
2908 * dummy used in later chain allocation (no longer used for lookups).
2910 bzero(&dummy, sizeof(dummy));
2913 * When creating an indirect block for a freemap node or leaf
2914 * the key/keybits must be fitted to static radix levels because
2915 * particular radix levels use particular reserved blocks in the
2918 * This routine calculates the key/radix of the indirect block
2919 * we need to create, and whether it is on the high-side or the
2922 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2923 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2924 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
2927 keybits = hammer2_chain_indkey_normal(parent, &key, keybits,
2932 * Normalize the key for the radix being represented, keeping the
2933 * high bits and throwing away the low bits.
2935 key &= ~(((hammer2_key_t)1 << keybits) - 1);
2938 * How big should our new indirect block be? It has to be at least
2939 * as large as its parent.
2941 * The freemap uses a specific indirect block size.
2943 * The first indirect block level down from an inode typically
2944 * uses LBUFSIZE (16384), else it uses PBUFSIZE (65536).
2946 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2947 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2948 nbytes = HAMMER2_FREEMAP_LEVELN_PSIZE;
2949 } else if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
2950 nbytes = HAMMER2_IND_BYTES_MIN;
2952 nbytes = HAMMER2_IND_BYTES_MAX;
2954 if (nbytes < count * sizeof(hammer2_blockref_t)) {
2955 KKASSERT(for_type != HAMMER2_BREF_TYPE_FREEMAP_NODE &&
2956 for_type != HAMMER2_BREF_TYPE_FREEMAP_LEAF);
2957 nbytes = count * sizeof(hammer2_blockref_t);
2961 * Ok, create our new indirect block
2963 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2964 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2965 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
2967 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
2969 dummy.bref.key = key;
2970 dummy.bref.keybits = keybits;
2971 dummy.bref.data_off = hammer2_getradix(nbytes);
2972 dummy.bref.methods = parent->bref.methods;
2974 ichain = hammer2_chain_alloc(hmp, parent->pmp, &dummy.bref);
2975 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
2976 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
2977 /* ichain has one ref at this point */
2980 * We have to mark it modified to allocate its block, but use
2981 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
2982 * it won't be acted upon by the flush code.
2984 hammer2_chain_modify(ichain, mtid, 0, HAMMER2_MODIFY_OPTDATA);
2987 * Iterate the original parent and move the matching brefs into
2988 * the new indirect block.
2990 * XXX handle flushes.
2993 key_end = HAMMER2_KEY_MAX;
2995 hammer2_spin_ex(&parent->core.spin);
3000 if (++loops > 100000) {
3001 hammer2_spin_unex(&parent->core.spin);
3002 panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
3003 reason, parent, base, count, key_next);
3007 * NOTE: spinlock stays intact, returned chain (if not NULL)
3008 * is not referenced or locked which means that we
3009 * cannot safely check its flagged / deletion status
3012 chain = hammer2_combined_find(parent, base, count,
3013 &cache_index, &key_next,
3016 generation = parent->core.generation;
3019 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3022 * Skip keys that are not within the key/radix of the new
3023 * indirect block. They stay in the parent.
3025 if ((~(((hammer2_key_t)1 << keybits) - 1) &
3026 (key ^ bref->key)) != 0) {
3027 goto next_key_spinlocked;
3031 * Load the new indirect block by acquiring the related
3032 * chains (potentially from media as it might not be
3033 * in-memory). Then move it to the new parent (ichain)
3034 * via DELETE-DUPLICATE.
3036 * chain is referenced but not locked. We must lock the
3037 * chain to obtain definitive DUPLICATED/DELETED state
3041 * Use chain already present in the RBTREE
3043 hammer2_chain_ref(chain);
3044 hammer2_spin_unex(&parent->core.spin);
3045 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
3048 * Get chain for blockref element. _get returns NULL
3049 * on insertion race.
3052 hammer2_spin_unex(&parent->core.spin);
3053 chain = hammer2_chain_get(parent, generation, &bcopy);
3054 if (chain == NULL) {
3056 hammer2_spin_ex(&parent->core.spin);
3059 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
3060 kprintf("REASON 2\n");
3062 hammer2_chain_drop(chain);
3063 hammer2_spin_ex(&parent->core.spin);
3066 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
3070 * This is always live so if the chain has been deleted
3071 * we raced someone and we have to retry.
3073 * NOTE: Lookups can race delete-duplicate because
3074 * delete-duplicate does not lock the parent's core
3075 * (they just use the spinlock on the core). We must
3076 * check for races by comparing the DUPLICATED flag before
3077 * releasing the spinlock with the flag after locking the
3080 * (note reversed logic for this one)
3082 if (chain->flags & HAMMER2_CHAIN_DELETED) {
3083 hammer2_chain_unlock(chain);
3084 hammer2_chain_drop(chain);
3089 * Shift the chain to the indirect block.
3091 * WARNING! No reason for us to load chain data, pass NOSTATS
3092 * to prevent delete/insert from trying to access
3093 * inode stats (and thus asserting if there is no
3094 * chain->data loaded).
3096 hammer2_chain_delete(parent, chain, mtid, 0);
3097 hammer2_chain_rename(NULL, &ichain, chain, mtid, 0);
3098 hammer2_chain_unlock(chain);
3099 hammer2_chain_drop(chain);
3100 KKASSERT(parent->refs > 0);
3103 hammer2_spin_ex(&parent->core.spin);
3104 next_key_spinlocked:
3105 if (--maxloops == 0)
3106 panic("hammer2_chain_create_indirect: maxloops");
3108 if (key_next == 0 || key_next > key_end)
3113 hammer2_spin_unex(&parent->core.spin);
3116 * Insert the new indirect block into the parent now that we've
3117 * cleared out some entries in the parent. We calculated a good
3118 * insertion index in the loop above (ichain->index).
3120 * We don't have to set UPDATE here because we mark ichain
3121 * modified down below (so the normal modified -> flush -> set-moved
3122 * sequence applies).
3124 * The insertion shouldn't race as this is a completely new block
3125 * and the parent is locked.
3127 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
3128 hammer2_chain_insert(parent, ichain,
3129 HAMMER2_CHAIN_INSERT_SPIN |
3130 HAMMER2_CHAIN_INSERT_LIVE,
3134 * Make sure flushes propogate after our manual insertion.
3136 hammer2_chain_setflush(ichain);
3137 hammer2_chain_setflush(parent);
3140 * Figure out what to return.
3142 if (~(((hammer2_key_t)1 << keybits) - 1) &
3143 (create_key ^ key)) {
3145 * Key being created is outside the key range,
3146 * return the original parent.
3148 hammer2_chain_unlock(ichain);
3149 hammer2_chain_drop(ichain);
3152 * Otherwise its in the range, return the new parent.
3153 * (leave both the new and old parent locked).
3162 * Calculate the keybits and highside/lowside of the freemap node the
3163 * caller is creating.
3165 * This routine will specify the next higher-level freemap key/radix
3166 * representing the lowest-ordered set. By doing so, eventually all
3167 * low-ordered sets will be moved one level down.
3169 * We have to be careful here because the freemap reserves a limited
3170 * number of blocks for a limited number of levels. So we can't just
3171 * push indiscriminately.
3174 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
3175 int keybits, hammer2_blockref_t *base, int count)
3177 hammer2_chain_t *chain;
3178 hammer2_blockref_t *bref;
3180 hammer2_key_t key_beg;
3181 hammer2_key_t key_end;
3182 hammer2_key_t key_next;
3186 int maxloops = 300000;
3194 * Calculate the range of keys in the array being careful to skip
3195 * slots which are overridden with a deletion.
3198 key_end = HAMMER2_KEY_MAX;
3200 hammer2_spin_ex(&parent->core.spin);
3203 if (--maxloops == 0) {
3204 panic("indkey_freemap shit %p %p:%d\n",
3205 parent, base, count);
3207 chain = hammer2_combined_find(parent, base, count,
3208 &cache_index, &key_next,
3219 * Skip deleted chains.
3221 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3222 if (key_next == 0 || key_next > key_end)
3229 * Use the full live (not deleted) element for the scan
3230 * iteration. HAMMER2 does not allow partial replacements.
3232 * XXX should be built into hammer2_combined_find().
3234 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3236 if (keybits > bref->keybits) {
3238 keybits = bref->keybits;
3239 } else if (keybits == bref->keybits && bref->key < key) {
3246 hammer2_spin_unex(&parent->core.spin);
3249 * Return the keybits for a higher-level FREEMAP_NODE covering
3253 case HAMMER2_FREEMAP_LEVEL0_RADIX:
3254 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
3256 case HAMMER2_FREEMAP_LEVEL1_RADIX:
3257 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
3259 case HAMMER2_FREEMAP_LEVEL2_RADIX:
3260 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
3262 case HAMMER2_FREEMAP_LEVEL3_RADIX:
3263 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
3265 case HAMMER2_FREEMAP_LEVEL4_RADIX:
3266 keybits = HAMMER2_FREEMAP_LEVEL5_RADIX;
3268 case HAMMER2_FREEMAP_LEVEL5_RADIX:
3269 panic("hammer2_chain_indkey_freemap: level too high");
3272 panic("hammer2_chain_indkey_freemap: bad radix");
3281 * Calculate the keybits and highside/lowside of the indirect block the
3282 * caller is creating.
3285 hammer2_chain_indkey_normal(hammer2_chain_t *parent, hammer2_key_t *keyp,
3286 int keybits, hammer2_blockref_t *base, int count)
3288 hammer2_blockref_t *bref;
3289 hammer2_chain_t *chain;
3290 hammer2_key_t key_beg;
3291 hammer2_key_t key_end;
3292 hammer2_key_t key_next;
3298 int maxloops = 300000;
3305 * Calculate the range of keys in the array being careful to skip
3306 * slots which are overridden with a deletion. Once the scan
3307 * completes we will cut the key range in half and shift half the
3308 * range into the new indirect block.
3311 key_end = HAMMER2_KEY_MAX;
3313 hammer2_spin_ex(&parent->core.spin);
3316 if (--maxloops == 0) {
3317 panic("indkey_freemap shit %p %p:%d\n",
3318 parent, base, count);
3320 chain = hammer2_combined_find(parent, base, count,
3321 &cache_index, &key_next,
3332 * NOTE: No need to check DUPLICATED here because we do
3333 * not release the spinlock.
3335 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3336 if (key_next == 0 || key_next > key_end)
3343 * Use the full live (not deleted) element for the scan
3344 * iteration. HAMMER2 does not allow partial replacements.
3346 * XXX should be built into hammer2_combined_find().
3348 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3351 * Expand our calculated key range (key, keybits) to fit
3352 * the scanned key. nkeybits represents the full range
3353 * that we will later cut in half (two halves @ nkeybits - 1).
3356 if (nkeybits < bref->keybits) {
3357 if (bref->keybits > 64) {
3358 kprintf("bad bref chain %p bref %p\n",
3362 nkeybits = bref->keybits;
3364 while (nkeybits < 64 &&
3365 (~(((hammer2_key_t)1 << nkeybits) - 1) &
3366 (key ^ bref->key)) != 0) {
3371 * If the new key range is larger we have to determine
3372 * which side of the new key range the existing keys fall
3373 * under by checking the high bit, then collapsing the
3374 * locount into the hicount or vise-versa.
3376 if (keybits != nkeybits) {
3377 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
3388 * The newly scanned key will be in the lower half or the
3389 * upper half of the (new) key range.
3391 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
3400 hammer2_spin_unex(&parent->core.spin);
3401 bref = NULL; /* now invalid (safety) */
3404 * Adjust keybits to represent half of the full range calculated
3405 * above (radix 63 max)
3410 * Select whichever half contains the most elements. Theoretically
3411 * we can select either side as long as it contains at least one
3412 * element (in order to ensure that a free slot is present to hold
3413 * the indirect block).
3415 if (hammer2_indirect_optimize) {
3417 * Insert node for least number of keys, this will arrange
3418 * the first few blocks of a large file or the first few
3419 * inodes in a directory with fewer indirect blocks when
3422 if (hicount < locount && hicount != 0)
3423 key |= (hammer2_key_t)1 << keybits;
3425 key &= ~(hammer2_key_t)1 << keybits;
3428 * Insert node for most number of keys, best for heavily
3431 if (hicount > locount)
3432 key |= (hammer2_key_t)1 << keybits;
3434 key &= ~(hammer2_key_t)1 << keybits;
3442 * Sets CHAIN_DELETED and remove the chain's blockref from the parent if
3445 * Both parent and chain must be locked exclusively.
3447 * This function will modify the parent if the blockref requires removal
3448 * from the parent's block table.
3450 * This function is NOT recursive. Any entity already pushed into the
3451 * chain (such as an inode) may still need visibility into its contents,
3452 * as well as the ability to read and modify the contents. For example,
3453 * for an unlinked file which is still open.
3456 hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
3457 hammer2_tid_t mtid, int flags)
3459 KKASSERT(hammer2_mtx_owned(&chain->lock));
3462 * Nothing to do if already marked.
3464 * We need the spinlock on the core whos RBTREE contains chain
3465 * to protect against races.
3467 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
3468 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
3469 chain->parent == parent);
3470 _hammer2_chain_delete_helper(parent, chain, mtid, flags);
3474 * To avoid losing track of a permanent deletion we add the chain
3475 * to the delayed flush queue. If were to flush it right now the
3476 * parent would end up in a modified state and generate I/O.
3477 * The delayed queue gives the parent a chance to be deleted to
3480 if (flags & HAMMER2_DELETE_PERMANENT) {
3481 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
3482 hammer2_delayed_flush(chain);
3484 /* XXX might not be needed */
3485 hammer2_chain_setflush(chain);
3490 * Returns the index of the nearest element in the blockref array >= elm.
3491 * Returns (count) if no element could be found.
3493 * Sets *key_nextp to the next key for loop purposes but does not modify
3494 * it if the next key would be higher than the current value of *key_nextp.
3495 * Note that *key_nexp can overflow to 0, which should be tested by the
3498 * (*cache_indexp) is a heuristic and can be any value without effecting
3501 * WARNING! Must be called with parent's spinlock held. Spinlock remains
3502 * held through the operation.
3505 hammer2_base_find(hammer2_chain_t *parent,
3506 hammer2_blockref_t *base, int count,
3507 int *cache_indexp, hammer2_key_t *key_nextp,
3508 hammer2_key_t key_beg, hammer2_key_t key_end)
3510 hammer2_blockref_t *scan;
3511 hammer2_key_t scan_end;
3516 * Require the live chain's already have their core's counted
3517 * so we can optimize operations.
3519 KKASSERT(parent->flags & HAMMER2_CHAIN_COUNTEDBREFS);
3524 if (count == 0 || base == NULL)
3528 * Sequential optimization using *cache_indexp. This is the most
3531 * We can avoid trailing empty entries on live chains, otherwise
3532 * we might have to check the whole block array.
3536 limit = parent->core.live_zero;
3541 KKASSERT(i < count);
3547 while (i > 0 && (scan->type == 0 || scan->key > key_beg)) {
3554 * Search forwards, stop when we find a scan element which
3555 * encloses the key or until we know that there are no further
3559 if (scan->type != 0) {
3560 scan_end = scan->key +
3561 ((hammer2_key_t)1 << scan->keybits) - 1;
3562 if (scan->key > key_beg || scan_end >= key_beg)
3575 scan_end = scan->key +
3576 ((hammer2_key_t)1 << scan->keybits);
3577 if (scan_end && (*key_nextp > scan_end ||
3579 *key_nextp = scan_end;
3587 * Do a combined search and return the next match either from the blockref
3588 * array or from the in-memory chain. Sets *bresp to the returned bref in
3589 * both cases, or sets it to NULL if the search exhausted. Only returns
3590 * a non-NULL chain if the search matched from the in-memory chain.
3592 * When no in-memory chain has been found and a non-NULL bref is returned
3596 * The returned chain is not locked or referenced. Use the returned bref
3597 * to determine if the search exhausted or not. Iterate if the base find
3598 * is chosen but matches a deleted chain.
3600 * WARNING! Must be called with parent's spinlock held. Spinlock remains
3601 * held through the operation.
3603 static hammer2_chain_t *
3604 hammer2_combined_find(hammer2_chain_t *parent,
3605 hammer2_blockref_t *base, int count,
3606 int *cache_indexp, hammer2_key_t *key_nextp,
3607 hammer2_key_t key_beg, hammer2_key_t key_end,
3608 hammer2_blockref_t **bresp)
3610 hammer2_blockref_t *bref;
3611 hammer2_chain_t *chain;
3615 * Lookup in block array and in rbtree.
3617 *key_nextp = key_end + 1;
3618 i = hammer2_base_find(parent, base, count, cache_indexp,
3619 key_nextp, key_beg, key_end);
3620 chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
3625 if (i == count && chain == NULL) {
3631 * Only chain matched.
3634 bref = &chain->bref;
3639 * Only blockref matched.
3641 if (chain == NULL) {
3647 * Both in-memory and blockref matched, select the nearer element.
3649 * If both are flush with the left-hand side or both are the
3650 * same distance away, select the chain. In this situation the
3651 * chain must have been loaded from the matching blockmap.
3653 if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
3654 chain->bref.key == base[i].key) {
3655 KKASSERT(chain->bref.key == base[i].key);
3656 bref = &chain->bref;
3661 * Select the nearer key
3663 if (chain->bref.key < base[i].key) {
3664 bref = &chain->bref;
3671 * If the bref is out of bounds we've exhausted our search.
3674 if (bref->key > key_end) {
3684 * Locate the specified block array element and delete it. The element
3687 * The spin lock on the related chain must be held.
3689 * NOTE: live_count was adjusted when the chain was deleted, so it does not
3690 * need to be adjusted when we commit the media change.
3693 hammer2_base_delete(hammer2_chain_t *parent,
3694 hammer2_blockref_t *base, int count,
3695 int *cache_indexp, hammer2_chain_t *chain)
3697 hammer2_blockref_t *elm = &chain->bref;
3698 hammer2_key_t key_next;
3702 * Delete element. Expect the element to exist.
3704 * XXX see caller, flush code not yet sophisticated enough to prevent
3705 * re-flushed in some cases.
3707 key_next = 0; /* max range */
3708 i = hammer2_base_find(parent, base, count, cache_indexp,
3709 &key_next, elm->key, elm->key);
3710 if (i == count || base[i].type == 0 ||
3711 base[i].key != elm->key ||
3712 ((chain->flags & HAMMER2_CHAIN_BMAPUPD) == 0 &&
3713 base[i].keybits != elm->keybits)) {
3714 hammer2_spin_unex(&parent->core.spin);
3715 panic("delete base %p element not found at %d/%d elm %p\n",
3716 base, i, count, elm);
3721 * Update stats and zero the entry
3723 parent->bref.data_count -= base[i].data_count;
3724 parent->bref.data_count -= (hammer2_off_t)1 <<
3725 (int)(base[i].data_off & HAMMER2_OFF_MASK_RADIX);
3726 parent->bref.inode_count -= base[i].inode_count;
3727 if (base[i].type == HAMMER2_BREF_TYPE_INODE)
3728 parent->bref.inode_count -= 1;
3730 bzero(&base[i], sizeof(*base));
3733 * We can only optimize parent->core.live_zero for live chains.
3735 if (parent->core.live_zero == i + 1) {
3736 while (--i >= 0 && base[i].type == 0)
3738 parent->core.live_zero = i + 1;
3742 * Clear appropriate blockmap flags in chain.
3744 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED |
3745 HAMMER2_CHAIN_BMAPUPD);
3749 * Insert the specified element. The block array must not already have the
3750 * element and must have space available for the insertion.
3752 * The spin lock on the related chain must be held.
3754 * NOTE: live_count was adjusted when the chain was deleted, so it does not
3755 * need to be adjusted when we commit the media change.
3758 hammer2_base_insert(hammer2_chain_t *parent,
3759 hammer2_blockref_t *base, int count,
3760 int *cache_indexp, hammer2_chain_t *chain)
3762 hammer2_blockref_t *elm = &chain->bref;
3763 hammer2_key_t key_next;
3772 * Insert new element. Expect the element to not already exist
3773 * unless we are replacing it.
3775 * XXX see caller, flush code not yet sophisticated enough to prevent
3776 * re-flushed in some cases.
3778 key_next = 0; /* max range */
3779 i = hammer2_base_find(parent, base, count, cache_indexp,
3780 &key_next, elm->key, elm->key);
3783 * Shortcut fill optimization, typical ordered insertion(s) may not
3786 KKASSERT(i >= 0 && i <= count);
3789 * Set appropriate blockmap flags in chain.
3791 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
3794 * Update stats and zero the entry
3796 parent->bref.data_count += elm->data_count;
3797 parent->bref.data_count += (hammer2_off_t)1 <<
3798 (int)(elm->data_off & HAMMER2_OFF_MASK_RADIX);
3799 parent->bref.inode_count += elm->inode_count;
3800 if (elm->type == HAMMER2_BREF_TYPE_INODE)
3801 parent->bref.inode_count += 1;
3805 * We can only optimize parent->core.live_zero for live chains.
3807 if (i == count && parent->core.live_zero < count) {
3808 i = parent->core.live_zero++;
3813 xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
3814 if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
3815 hammer2_spin_unex(&parent->core.spin);
3816 panic("insert base %p overlapping elements at %d elm %p\n",
3821 * Try to find an empty slot before or after.
3825 while (j > 0 || k < count) {
3827 if (j >= 0 && base[j].type == 0) {
3831 bcopy(&base[j+1], &base[j],
3832 (i - j - 1) * sizeof(*base));
3838 if (k < count && base[k].type == 0) {
3839 bcopy(&base[i], &base[i+1],
3840 (k - i) * sizeof(hammer2_blockref_t));
3844 * We can only update parent->core.live_zero for live
3847 if (parent->core.live_zero <= k)
3848 parent->core.live_zero = k + 1;
3853 panic("hammer2_base_insert: no room!");
3860 for (l = 0; l < count; ++l) {
3862 key_next = base[l].key +
3863 ((hammer2_key_t)1 << base[l].keybits) - 1;
3867 while (++l < count) {
3869 if (base[l].key <= key_next)
3870 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
3871 key_next = base[l].key +
3872 ((hammer2_key_t)1 << base[l].keybits) - 1;
3882 * Sort the blockref array for the chain. Used by the flush code to
3883 * sort the blockref[] array.
3885 * The chain must be exclusively locked AND spin-locked.
3887 typedef hammer2_blockref_t *hammer2_blockref_p;
3891 hammer2_base_sort_callback(const void *v1, const void *v2)
3893 hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
3894 hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
3897 * Make sure empty elements are placed at the end of the array
3899 if (bref1->type == 0) {
3900 if (bref2->type == 0)
3903 } else if (bref2->type == 0) {
3910 if (bref1->key < bref2->key)
3912 if (bref1->key > bref2->key)
3918 hammer2_base_sort(hammer2_chain_t *chain)
3920 hammer2_blockref_t *base;
3923 switch(chain->bref.type) {
3924 case HAMMER2_BREF_TYPE_INODE:
3926 * Special shortcut for embedded data returns the inode
3927 * itself. Callers must detect this condition and access
3928 * the embedded data (the strategy code does this for us).
3930 * This is only applicable to regular files and softlinks.
3932 if (chain->data->ipdata.meta.op_flags &
3933 HAMMER2_OPFLAG_DIRECTDATA) {
3936 base = &chain->data->ipdata.u.blockset.blockref[0];
3937 count = HAMMER2_SET_COUNT;
3939 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3940 case HAMMER2_BREF_TYPE_INDIRECT:
3942 * Optimize indirect blocks in the INITIAL state to avoid
3945 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
3946 base = &chain->data->npdata[0];
3947 count = chain->bytes / sizeof(hammer2_blockref_t);
3949 case HAMMER2_BREF_TYPE_VOLUME:
3950 base = &chain->data->voldata.sroot_blockset.blockref[0];
3951 count = HAMMER2_SET_COUNT;
3953 case HAMMER2_BREF_TYPE_FREEMAP:
3954 base = &chain->data->blkset.blockref[0];
3955 count = HAMMER2_SET_COUNT;
3958 kprintf("hammer2_chain_lookup: unrecognized "
3959 "blockref(A) type: %d",
3962 tsleep(&base, 0, "dead", 0);
3963 panic("hammer2_chain_lookup: unrecognized "
3964 "blockref(A) type: %d",
3966 base = NULL; /* safety */
3967 count = 0; /* safety */
3969 kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
3975 * Chain memory management
3978 hammer2_chain_wait(hammer2_chain_t *chain)
3980 tsleep(chain, 0, "chnflw", 1);
3983 const hammer2_media_data_t *
3984 hammer2_chain_rdata(hammer2_chain_t *chain)
3986 KKASSERT(chain->data != NULL);
3987 return (chain->data);
3990 hammer2_media_data_t *
3991 hammer2_chain_wdata(hammer2_chain_t *chain)
3993 KKASSERT(chain->data != NULL);
3994 return (chain->data);
3998 * Set the check data for a chain. This can be a heavy-weight operation
3999 * and typically only runs on-flush. For file data check data is calculated
4000 * when the logical buffers are flushed.
4003 hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata)
4005 chain->bref.flags &= ~HAMMER2_BREF_FLAG_ZERO;
4007 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
4008 case HAMMER2_CHECK_NONE:
4010 case HAMMER2_CHECK_DISABLED:
4012 case HAMMER2_CHECK_ISCSI32:
4013 chain->bref.check.iscsi32.value =
4014 hammer2_icrc32(bdata, chain->bytes);
4016 case HAMMER2_CHECK_CRC64:
4017 chain->bref.check.crc64.value = 0;
4020 case HAMMER2_CHECK_SHA192:
4022 SHA256_CTX hash_ctx;
4024 uint8_t digest[SHA256_DIGEST_LENGTH];
4025 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
4028 SHA256_Init(&hash_ctx);
4029 SHA256_Update(&hash_ctx, bdata, chain->bytes);
4030 SHA256_Final(u.digest, &hash_ctx);
4031 u.digest64[2] ^= u.digest64[3];
4033 chain->bref.check.sha192.data,
4034 sizeof(chain->bref.check.sha192.data));
4037 case HAMMER2_CHECK_FREEMAP:
4038 chain->bref.check.freemap.icrc32 =
4039 hammer2_icrc32(bdata, chain->bytes);
4042 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
4043 chain->bref.methods);
4049 hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata)
4053 if (chain->bref.flags & HAMMER2_BREF_FLAG_ZERO)
4056 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
4057 case HAMMER2_CHECK_NONE:
4060 case HAMMER2_CHECK_DISABLED:
4063 case HAMMER2_CHECK_ISCSI32:
4064 r = (chain->bref.check.iscsi32.value ==
4065 hammer2_icrc32(bdata, chain->bytes));
4067 case HAMMER2_CHECK_CRC64:
4068 r = (chain->bref.check.crc64.value == 0);
4071 case HAMMER2_CHECK_SHA192:
4073 SHA256_CTX hash_ctx;
4075 uint8_t digest[SHA256_DIGEST_LENGTH];
4076 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
4079 SHA256_Init(&hash_ctx);
4080 SHA256_Update(&hash_ctx, bdata, chain->bytes);
4081 SHA256_Final(u.digest, &hash_ctx);
4082 u.digest64[2] ^= u.digest64[3];
4084 chain->bref.check.sha192.data,
4085 sizeof(chain->bref.check.sha192.data)) == 0) {
4092 case HAMMER2_CHECK_FREEMAP:
4093 r = (chain->bref.check.freemap.icrc32 ==
4094 hammer2_icrc32(bdata, chain->bytes));
4096 kprintf("freemap.icrc %08x icrc32 %08x (%d)\n",
4097 chain->bref.check.freemap.icrc32,
4098 hammer2_icrc32(bdata, chain->bytes), chain->bytes);
4100 kprintf("dio %p buf %016jx,%d bdata %p/%p\n",
4101 chain->dio, chain->dio->bp->b_loffset, chain->dio->bp->b_bufsize, bdata, chain->dio->bp->b_data);
4106 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
4107 chain->bref.methods);
4115 * The caller presents a shared-locked (parent, chain) where the chain
4116 * is of type HAMMER2_OBJTYPE_HARDLINK. The caller must hold the ip
4117 * structure representing the inode locked to prevent
4118 * consolidation/deconsolidation races.
4120 * We locate the hardlink in the current or a common parent directory.
4122 * If we are unable to locate the hardlink, EIO is returned and
4123 * (*chainp) is unlocked and dropped.
4126 hammer2_chain_hardlink_find(hammer2_inode_t *dip,
4127 hammer2_chain_t **parentp,
4128 hammer2_chain_t **chainp,
4131 hammer2_chain_t *parent;
4132 hammer2_chain_t *rchain;
4133 hammer2_key_t key_dummy;
4135 int cache_index = -1;
4138 * Obtain the key for the hardlink from *chainp.
4141 lhc = rchain->data->ipdata.meta.inum;
4142 hammer2_chain_unlock(rchain);
4143 hammer2_chain_drop(rchain);
4148 rchain = hammer2_chain_lookup(parentp, &key_dummy,
4150 &cache_index, flags);
4155 * Iterate parents, handle parent rename races by retrying
4163 parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
4166 if (parent->bref.flags & HAMMER2_BREF_FLAG_PFSROOT)
4168 if (parent->parent == NULL)
4170 parent = parent->parent;
4171 hammer2_chain_ref(parent);
4172 hammer2_chain_unlock(*parentp);
4173 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
4175 if ((*parentp)->parent == parent) {
4176 hammer2_chain_drop(*parentp);
4179 hammer2_chain_unlock(parent);
4180 hammer2_chain_drop(parent);
4181 hammer2_chain_lock(*parentp,
4182 HAMMER2_RESOLVE_ALWAYS |
4184 parent = NULL; /* safety */
4192 return (rchain ? EINVAL : 0);
4196 * Used by the bulkscan code to snapshot the synchronized storage for
4197 * a volume, allowing it to be scanned concurrently against normal
4201 hammer2_chain_bulksnap(hammer2_chain_t *chain)
4203 hammer2_chain_t *copy;
4205 copy = hammer2_chain_alloc(chain->hmp, chain->pmp, &chain->bref);
4206 switch(chain->bref.type) {
4207 case HAMMER2_BREF_TYPE_VOLUME:
4208 copy->data = kmalloc(sizeof(copy->data->voldata),
4211 hammer2_spin_ex(&chain->core.spin);
4212 copy->data->voldata = chain->data->voldata;
4213 hammer2_spin_unex(&chain->core.spin);
4215 case HAMMER2_BREF_TYPE_FREEMAP:
4216 copy->data = kmalloc(sizeof(hammer2_blockset_t),
4219 hammer2_spin_ex(&chain->core.spin);
4220 copy->data->blkset = chain->data->blkset;
4221 hammer2_spin_unex(&chain->core.spin);
4230 hammer2_chain_bulkdrop(hammer2_chain_t *copy)
4232 switch(copy->bref.type) {
4233 case HAMMER2_BREF_TYPE_VOLUME:
4234 case HAMMER2_BREF_TYPE_FREEMAP:
4235 KKASSERT(copy->data);
4236 kfree(copy->data, copy->hmp->mchain);
4241 hammer2_chain_drop(copy);
4245 * Create a snapshot of the specified {parent, ochain} with the specified
4246 * label. The originating hammer2_inode must be exclusively locked for
4249 * The ioctl code has already synced the filesystem.
4252 hammer2_chain_snapshot(hammer2_chain_t *chain, hammer2_ioc_pfs_t *pmp,
4256 const hammer2_inode_data_t *ripdata;
4257 hammer2_inode_data_t *wipdata;
4258 hammer2_chain_t *nchain;
4259 hammer2_inode_t *nip;
4268 kprintf("snapshot %s\n", pmp->name);
4270 name_len = strlen(pmp->name);
4271 lhc = hammer2_dirhash(pmp->name, name_len);
4276 ripdata = &chain->data->ipdata;
4278 opfs_clid = ripdata->meta.pfs_clid;
4283 * Create the snapshot directory under the super-root
4285 * Set PFS type, generate a unique filesystem id, and generate
4286 * a cluster id. Use the same clid when snapshotting a PFS root,
4287 * which theoretically allows the snapshot to be used as part of
4288 * the same cluster (perhaps as a cache).
4290 * Copy the (flushed) blockref array. Theoretically we could use
4291 * chain_duplicate() but it becomes difficult to disentangle
4292 * the shared core so for now just brute-force it.
4297 nip = hammer2_inode_create(hmp->spmp->iroot, &vat, proc0.p_ucred,
4298 pmp->name, name_len, 0,
4300 HAMMER2_INSERT_PFSROOT, &error);
4303 hammer2_inode_modify(nip);
4304 nchain = hammer2_inode_chain(nip, 0, HAMMER2_RESOLVE_ALWAYS);
4305 hammer2_chain_modify(nchain, mtid, 0, 0);
4306 wipdata = &nchain->data->ipdata;
4308 nip->meta.pfs_type = HAMMER2_PFSTYPE_MASTER;
4309 nip->meta.pfs_subtype = HAMMER2_PFSSUBTYPE_SNAPSHOT;
4310 nip->meta.op_flags |= HAMMER2_OPFLAG_PFSROOT;
4311 kern_uuidgen(&nip->meta.pfs_fsid, 1);
4314 * Give the snapshot its own private cluster id. As a
4315 * snapshot no further synchronization with the original
4316 * cluster will be done.
4319 if (chain->flags & HAMMER2_CHAIN_PFSBOUNDARY)
4320 nip->meta.pfs_clid = opfs_clid;
4322 kern_uuidgen(&nip->meta.pfs_clid, 1);
4324 kern_uuidgen(&nip->meta.pfs_clid, 1);
4325 nchain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
4327 /* XXX hack blockset copy */
4328 /* XXX doesn't work with real cluster */
4329 wipdata->meta = nip->meta;
4330 wipdata->u.blockset = ripdata->u.blockset;
4331 hammer2_flush(nchain, 1);
4332 hammer2_chain_unlock(nchain);
4333 hammer2_chain_drop(nchain);
4334 hammer2_inode_unlock(nip);