2 * Copyright (c) 2011-2014 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * and Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This subsystem implements most of the core support functions for
37 * the hammer2_chain structure.
39 * Chains are the in-memory version on media objects (volume header, inodes,
40 * indirect blocks, data blocks, etc). Chains represent a portion of the
43 * Chains are no-longer delete-duplicated. Instead, the original in-memory
44 * chain will be moved along with its block reference (e.g. for things like
45 * renames, hardlink operations, modifications, etc), and will be indexed
46 * on a secondary list for flush handling instead of propagating a flag
49 * Concurrent front-end operations can still run against backend flushes
50 * as long as they do not cross the current flush boundary. An operation
51 * running above the current flush (in areas not yet flushed) can become
52 * part of the current flush while ano peration running below the current
53 * flush can become part of the next flush.
55 #include <sys/cdefs.h>
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/types.h>
60 #include <sys/kern_syscall.h>
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, int for_type, int *errorp);
72 static void hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop);
73 static hammer2_chain_t *hammer2_combined_find(
74 hammer2_chain_t *parent,
75 hammer2_blockref_t *base, int count,
76 int *cache_indexp, hammer2_key_t *key_nextp,
77 hammer2_key_t key_beg, hammer2_key_t key_end,
78 hammer2_blockref_t **bresp);
81 * Basic RBTree for chains (core->rbtree and core->dbtree). Chains cannot
82 * overlap in the RB trees. Deleted chains are moved from rbtree to either
85 * Chains in delete-duplicate sequences can always iterate through core_entry
86 * to locate the live version of the chain.
88 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
91 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
99 * Compare chains. Overlaps are not supposed to happen and catch
100 * any software issues early we count overlaps as a match.
102 c1_beg = chain1->bref.key;
103 c1_end = c1_beg + ((hammer2_key_t)1 << chain1->bref.keybits) - 1;
104 c2_beg = chain2->bref.key;
105 c2_end = c2_beg + ((hammer2_key_t)1 << chain2->bref.keybits) - 1;
107 if (c1_end < c2_beg) /* fully to the left */
109 if (c1_beg > c2_end) /* fully to the right */
111 return(0); /* overlap (must not cross edge boundary) */
116 hammer2_isclusterable(hammer2_chain_t *chain)
118 if (hammer2_cluster_enable) {
119 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
120 chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
121 chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
129 * Make a chain visible to the flusher. The flusher needs to be able to
130 * do flushes of subdirectory chains or single files so it does a top-down
131 * recursion using the ONFLUSH flag for the recursion. It locates MODIFIED
132 * or UPDATE chains and flushes back up the chain to the volume root.
134 * This routine sets ONFLUSH upward until it hits the volume root. For
135 * simplicity we ignore PFSROOT boundaries whos rules can be complex.
136 * Extra ONFLUSH flagging doesn't hurt the filesystem.
139 hammer2_chain_setflush(hammer2_chain_t *chain)
141 hammer2_chain_t *parent;
143 if ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
144 hammer2_spin_sh(&chain->core.spin);
145 while ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
146 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH);
147 if ((parent = chain->parent) == NULL)
149 hammer2_spin_sh(&parent->core.spin);
150 hammer2_spin_unsh(&chain->core.spin);
153 hammer2_spin_unsh(&chain->core.spin);
158 * Allocate a new disconnected chain element representing the specified
159 * bref. chain->refs is set to 1 and the passed bref is copied to
160 * chain->bref. chain->bytes is derived from the bref.
162 * chain->pmp inherits pmp unless the chain is an inode (other than the
165 * NOTE: Returns a referenced but unlocked (because there is no core) chain.
168 hammer2_chain_alloc(hammer2_dev_t *hmp, hammer2_pfs_t *pmp,
169 hammer2_blockref_t *bref)
171 hammer2_chain_t *chain;
172 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
175 * Construct the appropriate system structure.
178 case HAMMER2_BREF_TYPE_INODE:
179 case HAMMER2_BREF_TYPE_INDIRECT:
180 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
181 case HAMMER2_BREF_TYPE_DATA:
182 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
184 * Chain's are really only associated with the hmp but we
185 * maintain a pmp association for per-mount memory tracking
186 * purposes. The pmp can be NULL.
188 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
190 case HAMMER2_BREF_TYPE_VOLUME:
191 case HAMMER2_BREF_TYPE_FREEMAP:
193 panic("hammer2_chain_alloc volume type illegal for op");
196 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
201 * Initialize the new chain structure. pmp must be set to NULL for
202 * chains belonging to the super-root topology of a device mount.
204 if (pmp == hmp->spmp)
210 chain->bytes = bytes;
212 chain->flags = HAMMER2_CHAIN_ALLOCATED;
215 * Set the PFS boundary flag if this chain represents a PFS root.
217 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
218 chain->flags |= HAMMER2_CHAIN_PFSBOUNDARY;
219 hammer2_chain_core_init(chain);
225 * Initialize a chain's core structure. This structure used to be allocated
226 * but is now embedded.
228 * The core is not locked. No additional refs on the chain are made.
229 * (trans) must not be NULL if (core) is not NULL.
232 hammer2_chain_core_init(hammer2_chain_t *chain)
235 * Fresh core under nchain (no multi-homing of ochain's
238 RB_INIT(&chain->core.rbtree); /* live chains */
239 hammer2_mtx_init(&chain->lock, "h2chain");
243 * Add a reference to a chain element, preventing its destruction.
245 * (can be called with spinlock held)
248 hammer2_chain_ref(hammer2_chain_t *chain)
250 atomic_add_int(&chain->refs, 1);
252 kprintf("REFC %p %d %08x\n", chain, chain->refs - 1, chain->flags);
258 * Insert the chain in the core rbtree.
260 * Normal insertions are placed in the live rbtree. Insertion of a deleted
261 * chain is a special case used by the flush code that is placed on the
262 * unstaged deleted list to avoid confusing the live view.
264 #define HAMMER2_CHAIN_INSERT_SPIN 0x0001
265 #define HAMMER2_CHAIN_INSERT_LIVE 0x0002
266 #define HAMMER2_CHAIN_INSERT_RACE 0x0004
270 hammer2_chain_insert(hammer2_chain_t *parent, hammer2_chain_t *chain,
271 int flags, int generation)
273 hammer2_chain_t *xchain;
276 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
277 hammer2_spin_ex(&parent->core.spin);
280 * Interlocked by spinlock, check for race
282 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
283 parent->core.generation != generation) {
291 xchain = RB_INSERT(hammer2_chain_tree, &parent->core.rbtree, chain);
292 KASSERT(xchain == NULL,
293 ("hammer2_chain_insert: collision %p %p", chain, xchain));
294 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
295 chain->parent = parent;
296 ++parent->core.chain_count;
297 ++parent->core.generation; /* XXX incs for _get() too, XXX */
300 * We have to keep track of the effective live-view blockref count
301 * so the create code knows when to push an indirect block.
303 if (flags & HAMMER2_CHAIN_INSERT_LIVE)
304 atomic_add_int(&parent->core.live_count, 1);
306 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
307 hammer2_spin_unex(&parent->core.spin);
312 * Drop the caller's reference to the chain. When the ref count drops to
313 * zero this function will try to disassociate the chain from its parent and
314 * deallocate it, then recursely drop the parent using the implied ref
315 * from the chain's chain->parent.
317 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain);
320 hammer2_chain_drop(hammer2_chain_t *chain)
325 if (hammer2_debug & 0x200000)
328 kprintf("DROP %p %d %08x\n", chain, chain->refs - 1, chain->flags);
332 if (chain->flags & HAMMER2_CHAIN_UPDATE)
334 if (chain->flags & HAMMER2_CHAIN_MODIFIED)
336 KKASSERT(chain->refs > need);
344 chain = hammer2_chain_lastdrop(chain);
346 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
348 /* retry the same chain */
354 * Safe handling of the 1->0 transition on chain. Returns a chain for
355 * recursive drop or NULL, possibly returning the same chain if the atomic
358 * Whem two chains need to be recursively dropped we use the chain
359 * we would otherwise free to placehold the additional chain. It's a bit
360 * convoluted but we can't just recurse without potentially blowing out
363 * The chain cannot be freed if it has any children.
365 * The core spinlock is allowed nest child-to-parent (not parent-to-child).
369 hammer2_chain_lastdrop(hammer2_chain_t *chain)
373 hammer2_chain_t *parent;
374 hammer2_chain_t *rdrop;
377 * Spinlock the core and check to see if it is empty. If it is
378 * not empty we leave chain intact with refs == 0. The elements
379 * in core->rbtree are associated with other chains contemporary
380 * with ours but not with our chain directly.
382 hammer2_spin_ex(&chain->core.spin);
385 * We can't free non-stale chains with children until we are
386 * able to free the children because there might be a flush
387 * dependency. Flushes of stale children (which should also
388 * have their deleted flag set) short-cut recursive flush
389 * dependencies and can be freed here. Any flushes which run
390 * through stale children due to the flush synchronization
391 * point should have a FLUSH_* bit set in the chain and not
392 * reach lastdrop at this time.
394 * NOTE: We return (chain) on failure to retry.
396 if (chain->core.chain_count) {
397 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
398 hammer2_spin_unex(&chain->core.spin);
399 chain = NULL; /* success */
401 hammer2_spin_unex(&chain->core.spin);
405 /* no chains left under us */
408 * chain->core has no children left so no accessors can get to our
409 * chain from there. Now we have to lock the parent core to interlock
410 * remaining possible accessors that might bump chain's refs before
411 * we can safely drop chain's refs with intent to free the chain.
414 pmp = chain->pmp; /* can be NULL */
418 * Spinlock the parent and try to drop the last ref on chain.
419 * On success remove chain from its parent, otherwise return NULL.
421 * (normal core locks are top-down recursive but we define core
422 * spinlocks as bottom-up recursive, so this is safe).
424 if ((parent = chain->parent) != NULL) {
425 hammer2_spin_ex(&parent->core.spin);
426 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
427 /* 1->0 transition failed */
428 hammer2_spin_unex(&parent->core.spin);
429 hammer2_spin_unex(&chain->core.spin);
430 return(chain); /* retry */
434 * 1->0 transition successful, remove chain from its
437 if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
438 RB_REMOVE(hammer2_chain_tree,
439 &parent->core.rbtree, chain);
440 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
441 --parent->core.chain_count;
442 chain->parent = NULL;
446 * If our chain was the last chain in the parent's core the
447 * core is now empty and its parent might have to be
448 * re-dropped if it has 0 refs.
450 if (parent->core.chain_count == 0) {
452 if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0) {
456 hammer2_spin_unex(&parent->core.spin);
457 parent = NULL; /* safety */
461 * Successful 1->0 transition and the chain can be destroyed now.
463 * We still have the core spinlock, and core's chain_count is 0.
464 * Any parent spinlock is gone.
466 hammer2_spin_unex(&chain->core.spin);
467 KKASSERT(RB_EMPTY(&chain->core.rbtree) &&
468 chain->core.chain_count == 0);
471 * All spin locks are gone, finish freeing stuff.
473 KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE |
474 HAMMER2_CHAIN_MODIFIED)) == 0);
475 hammer2_chain_drop_data(chain, 1);
477 KKASSERT(chain->dio == NULL);
480 * Once chain resources are gone we can use the now dead chain
481 * structure to placehold what might otherwise require a recursive
482 * drop, because we have potentially two things to drop and can only
483 * return one directly.
485 if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
486 chain->flags &= ~HAMMER2_CHAIN_ALLOCATED;
488 kfree(chain, hmp->mchain);
492 * Possible chaining loop when parent re-drop needed.
498 * On either last lock release or last drop
501 hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop)
503 /*hammer2_dev_t *hmp = chain->hmp;*/
505 switch(chain->bref.type) {
506 case HAMMER2_BREF_TYPE_VOLUME:
507 case HAMMER2_BREF_TYPE_FREEMAP:
512 KKASSERT(chain->data == NULL);
518 * Lock a referenced chain element, acquiring its data with I/O if necessary,
519 * and specify how you would like the data to be resolved.
521 * If an I/O or other fatal error occurs, chain->error will be set to non-zero.
523 * The lock is allowed to recurse, multiple locking ops will aggregate
524 * the requested resolve types. Once data is assigned it will not be
525 * removed until the last unlock.
527 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
528 * (typically used to avoid device/logical buffer
531 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
532 * the INITIAL-create state (indirect blocks only).
534 * Do not resolve data elements for DATA chains.
535 * (typically used to avoid device/logical buffer
538 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
540 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
541 * it will be locked exclusive.
543 * NOTE: Embedded elements (volume header, inodes) are always resolved
546 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
547 * element will instantiate and zero its buffer, and flush it on
550 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
551 * so as not to instantiate a device buffer, which could alias against
552 * a logical file buffer. However, if ALWAYS is specified the
553 * device buffer will be instantiated anyway.
555 * WARNING! This function blocks on I/O if data needs to be fetched. This
556 * blocking can run concurrent with other compatible lock holders
557 * who do not need data returning. The lock is not upgraded to
558 * exclusive during a data fetch, a separate bit is used to
559 * interlock I/O. However, an exclusive lock holder can still count
560 * on being interlocked against an I/O fetch managed by a shared
564 hammer2_chain_lock(hammer2_chain_t *chain, int how)
567 * Ref and lock the element. Recursive locks are allowed.
569 KKASSERT(chain->refs > 0);
570 atomic_add_int(&chain->lockcnt, 1);
573 * Get the appropriate lock.
575 if (how & HAMMER2_RESOLVE_SHARED)
576 hammer2_mtx_sh(&chain->lock);
578 hammer2_mtx_ex(&chain->lock);
581 * If we already have a valid data pointer no further action is
588 * Do we have to resolve the data?
590 switch(how & HAMMER2_RESOLVE_MASK) {
591 case HAMMER2_RESOLVE_NEVER:
593 case HAMMER2_RESOLVE_MAYBE:
594 if (chain->flags & HAMMER2_CHAIN_INITIAL)
596 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
599 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
601 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
605 case HAMMER2_RESOLVE_ALWAYS:
611 * Caller requires data
613 hammer2_chain_load_data(chain);
617 * Issue I/O and install chain->data. Caller must hold a chain lock, lock
618 * may be of any type.
620 * Once chain->data is set it cannot be disposed of until all locks are
624 hammer2_chain_load_data(hammer2_chain_t *chain)
626 hammer2_blockref_t *bref;
632 * Degenerate case, data already present.
638 KKASSERT(hmp != NULL);
641 * Gain the IOINPROG bit, interlocked block.
647 oflags = chain->flags;
649 if (oflags & HAMMER2_CHAIN_IOINPROG) {
650 nflags = oflags | HAMMER2_CHAIN_IOSIGNAL;
651 tsleep_interlock(&chain->flags, 0);
652 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
653 tsleep(&chain->flags, PINTERLOCKED,
658 nflags = oflags | HAMMER2_CHAIN_IOINPROG;
659 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
667 * We own CHAIN_IOINPROG
669 * Degenerate case if we raced another load.
675 * We must resolve to a device buffer, either by issuing I/O or
676 * by creating a zero-fill element. We do not mark the buffer
677 * dirty when creating a zero-fill element (the hammer2_chain_modify()
678 * API must still be used to do that).
680 * The device buffer is variable-sized in powers of 2 down
681 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
682 * chunk always contains buffers of the same size. (XXX)
684 * The minimum physical IO size may be larger than the variable
690 * The getblk() optimization can only be used on newly created
691 * elements if the physical block size matches the request.
693 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
694 error = hammer2_io_new(hmp, bref->data_off, chain->bytes,
697 error = hammer2_io_bread(hmp, bref->data_off, chain->bytes,
699 hammer2_adjreadcounter(&chain->bref, chain->bytes);
702 chain->error = HAMMER2_ERROR_IO;
703 kprintf("hammer2_chain_lock: I/O error %016jx: %d\n",
704 (intmax_t)bref->data_off, error);
705 hammer2_io_bqrelse(&chain->dio);
711 * NOTE: A locked chain's data cannot be modified without first
712 * calling hammer2_chain_modify().
716 * Clear INITIAL. In this case we used io_new() and the buffer has
717 * been zero'd and marked dirty.
719 bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
720 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
721 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
722 chain->bref.flags |= HAMMER2_BREF_FLAG_ZERO;
723 } else if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
725 * check data not currently synchronized due to
726 * modification. XXX assumes data stays in the buffer
727 * cache, which might not be true (need biodep on flush
728 * to calculate crc? or simple crc?).
731 if (hammer2_chain_testcheck(chain, bdata) == 0) {
732 kprintf("chain %016jx.%02x meth=%02x "
733 "CHECK FAIL %08x (flags=%08x)\n",
734 chain->bref.data_off,
737 hammer2_icrc32(bdata, chain->bytes),
739 chain->error = HAMMER2_ERROR_CHECK;
744 * Setup the data pointer, either pointing it to an embedded data
745 * structure and copying the data from the buffer, or pointing it
748 * The buffer is not retained when copying to an embedded data
749 * structure in order to avoid potential deadlocks or recursions
750 * on the same physical buffer.
752 * WARNING! Other threads can start using the data the instant we
753 * set chain->data non-NULL.
755 switch (bref->type) {
756 case HAMMER2_BREF_TYPE_VOLUME:
757 case HAMMER2_BREF_TYPE_FREEMAP:
759 * Copy data from bp to embedded buffer
761 panic("hammer2_chain_lock: called on unresolved volume header");
763 case HAMMER2_BREF_TYPE_INODE:
764 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
765 case HAMMER2_BREF_TYPE_INDIRECT:
766 case HAMMER2_BREF_TYPE_DATA:
767 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
770 * Point data at the device buffer and leave dio intact.
772 chain->data = (void *)bdata;
777 * Release HAMMER2_CHAIN_IOINPROG and signal waiters if requested.
784 oflags = chain->flags;
785 nflags = oflags & ~(HAMMER2_CHAIN_IOINPROG |
786 HAMMER2_CHAIN_IOSIGNAL);
787 KKASSERT(oflags & HAMMER2_CHAIN_IOINPROG);
788 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
789 if (oflags & HAMMER2_CHAIN_IOSIGNAL)
790 wakeup(&chain->flags);
797 * Unlock and deref a chain element.
799 * On the last lock release any non-embedded data (chain->dio) will be
803 hammer2_chain_unlock(hammer2_chain_t *chain)
805 hammer2_mtx_state_t ostate;
810 * If multiple locks are present (or being attempted) on this
811 * particular chain we can just unlock, drop refs, and return.
813 * Otherwise fall-through on the 1->0 transition.
816 lockcnt = chain->lockcnt;
817 KKASSERT(lockcnt > 0);
820 if (atomic_cmpset_int(&chain->lockcnt,
821 lockcnt, lockcnt - 1)) {
822 hammer2_mtx_unlock(&chain->lock);
826 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
833 * On the 1->0 transition we upgrade the core lock (if necessary)
834 * to exclusive for terminal processing. If after upgrading we find
835 * that lockcnt is non-zero, another thread is racing us and will
836 * handle the unload for us later on, so just cleanup and return
837 * leaving the data/io intact
839 * Otherwise if lockcnt is still 0 it is possible for it to become
840 * non-zero and race, but since we hold the core->lock exclusively
841 * all that will happen is that the chain will be reloaded after we
844 ostate = hammer2_mtx_upgrade(&chain->lock);
845 if (chain->lockcnt) {
846 hammer2_mtx_unlock(&chain->lock);
851 * Shortcut the case if the data is embedded or not resolved.
853 * Do NOT NULL out chain->data (e.g. inode data), it might be
856 if (chain->dio == NULL) {
857 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0)
858 hammer2_chain_drop_data(chain, 0);
859 hammer2_mtx_unlock(&chain->lock);
866 if (hammer2_io_isdirty(chain->dio) == 0) {
868 } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
869 switch(chain->bref.type) {
870 case HAMMER2_BREF_TYPE_DATA:
871 counterp = &hammer2_ioa_file_write;
873 case HAMMER2_BREF_TYPE_INODE:
874 counterp = &hammer2_ioa_meta_write;
876 case HAMMER2_BREF_TYPE_INDIRECT:
877 counterp = &hammer2_ioa_indr_write;
879 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
880 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
881 counterp = &hammer2_ioa_fmap_write;
884 counterp = &hammer2_ioa_volu_write;
887 *counterp += chain->bytes;
889 switch(chain->bref.type) {
890 case HAMMER2_BREF_TYPE_DATA:
891 counterp = &hammer2_iod_file_write;
893 case HAMMER2_BREF_TYPE_INODE:
894 counterp = &hammer2_iod_meta_write;
896 case HAMMER2_BREF_TYPE_INDIRECT:
897 counterp = &hammer2_iod_indr_write;
899 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
900 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
901 counterp = &hammer2_iod_fmap_write;
904 counterp = &hammer2_iod_volu_write;
907 *counterp += chain->bytes;
913 * If a device buffer was used for data be sure to destroy the
914 * buffer when we are done to avoid aliases (XXX what about the
915 * underlying VM pages?).
917 * NOTE: Freemap leaf's use reserved blocks and thus no aliasing
920 * NOTE: The isdirty check tracks whether we have to bdwrite() the
921 * buffer or not. The buffer might already be dirty. The
922 * flag is re-set when chain_modify() is called, even if
923 * MODIFIED is already set, allowing the OS to retire the
924 * buffer independent of a hammer2 flush.
927 if ((chain->flags & HAMMER2_CHAIN_IOFLUSH) &&
928 hammer2_io_isdirty(chain->dio)) {
929 hammer2_io_bawrite(&chain->dio);
931 hammer2_io_bqrelse(&chain->dio);
933 hammer2_mtx_unlock(&chain->lock);
937 * This counts the number of live blockrefs in a block array and
938 * also calculates the point at which all remaining blockrefs are empty.
939 * This routine can only be called on a live chain (DUPLICATED flag not set).
941 * NOTE: Flag is not set until after the count is complete, allowing
942 * callers to test the flag without holding the spinlock.
944 * NOTE: If base is NULL the related chain is still in the INITIAL
945 * state and there are no blockrefs to count.
947 * NOTE: live_count may already have some counts accumulated due to
948 * creation and deletion and could even be initially negative.
951 hammer2_chain_countbrefs(hammer2_chain_t *chain,
952 hammer2_blockref_t *base, int count)
954 hammer2_spin_ex(&chain->core.spin);
955 if ((chain->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) {
957 while (--count >= 0) {
958 if (base[count].type)
961 chain->core.live_zero = count + 1;
963 if (base[count].type)
964 atomic_add_int(&chain->core.live_count,
969 chain->core.live_zero = 0;
971 /* else do not modify live_count */
972 atomic_set_int(&chain->flags, HAMMER2_CHAIN_COUNTEDBREFS);
974 hammer2_spin_unex(&chain->core.spin);
978 * Resize the chain's physical storage allocation in-place. This function does
979 * not adjust the data pointer and must be followed by (typically) a
980 * hammer2_chain_modify() call to copy any old data over and adjust the
983 * Chains can be resized smaller without reallocating the storage. Resizing
984 * larger will reallocate the storage. Excess or prior storage is reclaimed
985 * asynchronously at a later time.
987 * Must be passed an exclusively locked parent and chain.
989 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
990 * to avoid instantiating a device buffer that conflicts with the vnode data
991 * buffer. However, because H2 can compress or encrypt data, the chain may
992 * have a dio assigned to it in those situations, and they do not conflict.
994 * XXX return error if cannot resize.
997 hammer2_chain_resize(hammer2_inode_t *ip,
998 hammer2_chain_t *parent, hammer2_chain_t *chain,
999 int nradix, int flags)
1008 * Only data and indirect blocks can be resized for now.
1009 * (The volu root, inodes, and freemap elements use a fixed size).
1011 KKASSERT(chain != &hmp->vchain);
1012 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1013 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
1014 KKASSERT(chain->parent == parent);
1017 * Nothing to do if the element is already the proper size
1019 obytes = chain->bytes;
1020 nbytes = 1U << nradix;
1021 if (obytes == nbytes)
1025 * Make sure the old data is instantiated so we can copy it. If this
1026 * is a data block, the device data may be superfluous since the data
1027 * might be in a logical block, but compressed or encrypted data is
1030 * NOTE: The modify will set BMAPUPD for us if BMAPPED is set.
1032 hammer2_chain_modify(chain, 0);
1035 * Relocate the block, even if making it smaller (because different
1036 * block sizes may be in different regions).
1038 * (data blocks only, we aren't copying the storage here).
1040 hammer2_freemap_alloc(chain, nbytes);
1041 chain->bytes = nbytes;
1042 /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */
1045 * We don't want the followup chain_modify() to try to copy data
1046 * from the old (wrong-sized) buffer. It won't know how much to
1047 * copy. This case should only occur during writes when the
1048 * originator already has the data to write in-hand.
1051 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA);
1052 hammer2_io_brelse(&chain->dio);
1058 hammer2_chain_modify(hammer2_chain_t *chain, int flags)
1060 hammer2_blockref_t obref;
1069 obref = chain->bref;
1070 KKASSERT((chain->flags & HAMMER2_CHAIN_FICTITIOUS) == 0);
1073 * Data is not optional for freemap chains (we must always be sure
1074 * to copy the data on COW storage allocations).
1076 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1077 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1078 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1079 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1083 * Data must be resolved if already assigned, unless explicitly
1084 * flagged otherwise.
1086 if (chain->data == NULL && (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1087 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1088 hammer2_chain_load_data(chain);
1092 * Set MODIFIED to indicate that the chain has been modified.
1093 * Set UPDATE to ensure that the blockref is updated in the parent.
1095 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1096 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1097 hammer2_chain_ref(chain);
1098 hammer2_pfs_memory_inc(chain->pmp); /* can be NULL */
1103 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
1104 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1105 hammer2_chain_ref(chain);
1109 * The modification or re-modification requires an allocation and
1112 * We normally always allocate new storage here. If storage exists
1113 * and MODIFY_NOREALLOC is passed in, we do not allocate new storage.
1115 if (chain != &hmp->vchain && chain != &hmp->fchain) {
1116 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1117 ((flags & HAMMER2_MODIFY_NOREALLOC) == 0 && newmod)
1119 hammer2_freemap_alloc(chain, chain->bytes);
1120 /* XXX failed allocation */
1125 * Update mirror_tid and modify_tid. modify_tid is only updated
1126 * automatically by this function when used from the frontend.
1127 * Flushes and synchronization adjust the flag manually.
1129 * NOTE: chain->pmp could be the device spmp.
1131 chain->bref.mirror_tid = hmp->voldata.mirror_tid + 1;
1132 if (chain->pmp && (flags & HAMMER2_MODIFY_KEEPMODIFY) == 0) {
1133 /* XXX HAMMER2_TRANS_ISFLUSH */
1134 chain->bref.modify_tid = chain->pmp->modify_tid;
1138 * Set BMAPUPD to tell the flush code that an existing blockmap entry
1139 * requires updating as well as to tell the delete code that the
1140 * chain's blockref might not exactly match (in terms of physical size
1141 * or block offset) the one in the parent's blocktable. The base key
1142 * of course will still match.
1144 if (chain->flags & HAMMER2_CHAIN_BMAPPED)
1145 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD);
1148 * Short-cut data blocks which the caller does not need an actual
1149 * data reference to (aka OPTDATA), as long as the chain does not
1150 * already have a data pointer to the data. This generally means
1151 * that the modifications are being done via the logical buffer cache.
1152 * The INITIAL flag relates only to the device data buffer and thus
1153 * remains unchange in this situation.
1155 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA &&
1156 (flags & HAMMER2_MODIFY_OPTDATA) &&
1157 chain->data == NULL) {
1162 * Clearing the INITIAL flag (for indirect blocks) indicates that
1163 * we've processed the uninitialized storage allocation.
1165 * If this flag is already clear we are likely in a copy-on-write
1166 * situation but we have to be sure NOT to bzero the storage if
1167 * no data is present.
1169 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1170 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1177 * Instantiate data buffer and possibly execute COW operation
1179 switch(chain->bref.type) {
1180 case HAMMER2_BREF_TYPE_VOLUME:
1181 case HAMMER2_BREF_TYPE_FREEMAP:
1183 * The data is embedded, no copy-on-write operation is
1186 KKASSERT(chain->dio == NULL);
1188 case HAMMER2_BREF_TYPE_INODE:
1189 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1190 case HAMMER2_BREF_TYPE_DATA:
1191 case HAMMER2_BREF_TYPE_INDIRECT:
1192 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1194 * Perform the copy-on-write operation
1196 * zero-fill or copy-on-write depending on whether
1197 * chain->data exists or not and set the dirty state for
1198 * the new buffer. hammer2_io_new() will handle the
1201 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1204 error = hammer2_io_new(hmp, chain->bref.data_off,
1205 chain->bytes, &dio);
1207 error = hammer2_io_bread(hmp, chain->bref.data_off,
1208 chain->bytes, &dio);
1210 hammer2_adjreadcounter(&chain->bref, chain->bytes);
1213 * If an I/O error occurs make sure callers cannot accidently
1214 * modify the old buffer's contents and corrupt the filesystem.
1217 kprintf("hammer2_chain_modify: hmp=%p I/O error\n",
1219 chain->error = HAMMER2_ERROR_IO;
1220 hammer2_io_brelse(&dio);
1221 hammer2_io_brelse(&chain->dio);
1226 bdata = hammer2_io_data(dio, chain->bref.data_off);
1229 KKASSERT(chain->dio != NULL);
1230 if (chain->data != (void *)bdata) {
1231 bcopy(chain->data, bdata, chain->bytes);
1233 } else if (wasinitial == 0) {
1235 * We have a problem. We were asked to COW but
1236 * we don't have any data to COW with!
1238 panic("hammer2_chain_modify: having a COW %p\n",
1243 * Retire the old buffer, replace with the new. Dirty or
1244 * redirty the new buffer.
1246 * WARNING! The system buffer cache may have already flushed
1247 * the buffer, so we must be sure to [re]dirty it
1248 * for further modification.
1251 hammer2_io_brelse(&chain->dio);
1252 chain->data = (void *)bdata;
1254 hammer2_io_setdirty(dio); /* modified by bcopy above */
1257 panic("hammer2_chain_modify: illegal non-embedded type %d",
1264 * setflush on parent indicating that the parent must recurse down
1265 * to us. Do not call on chain itself which might already have it
1269 hammer2_chain_setflush(chain->parent);
1273 * Modify the chain associated with an inode.
1276 hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain, int flags)
1278 hammer2_inode_modify(ip);
1279 hammer2_chain_modify(chain, flags);
1283 * Volume header data locks
1286 hammer2_voldata_lock(hammer2_dev_t *hmp)
1288 lockmgr(&hmp->vollk, LK_EXCLUSIVE);
1292 hammer2_voldata_unlock(hammer2_dev_t *hmp)
1294 lockmgr(&hmp->vollk, LK_RELEASE);
1298 hammer2_voldata_modify(hammer2_dev_t *hmp)
1300 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1301 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
1302 hammer2_chain_ref(&hmp->vchain);
1303 hammer2_pfs_memory_inc(hmp->vchain.pmp);
1308 * This function returns the chain at the nearest key within the specified
1309 * range. The returned chain will be referenced but not locked.
1311 * This function will recurse through chain->rbtree as necessary and will
1312 * return a *key_nextp suitable for iteration. *key_nextp is only set if
1313 * the iteration value is less than the current value of *key_nextp.
1315 * The caller should use (*key_nextp) to calculate the actual range of
1316 * the returned element, which will be (key_beg to *key_nextp - 1), because
1317 * there might be another element which is superior to the returned element
1320 * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
1321 * chains continue to be returned. On EOF (*key_nextp) may overflow since
1322 * it will wind up being (key_end + 1).
1324 * WARNING! Must be called with child's spinlock held. Spinlock remains
1325 * held through the operation.
1327 struct hammer2_chain_find_info {
1328 hammer2_chain_t *best;
1329 hammer2_key_t key_beg;
1330 hammer2_key_t key_end;
1331 hammer2_key_t key_next;
1334 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
1335 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
1339 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
1340 hammer2_key_t key_beg, hammer2_key_t key_end)
1342 struct hammer2_chain_find_info info;
1345 info.key_beg = key_beg;
1346 info.key_end = key_end;
1347 info.key_next = *key_nextp;
1349 RB_SCAN(hammer2_chain_tree, &parent->core.rbtree,
1350 hammer2_chain_find_cmp, hammer2_chain_find_callback,
1352 *key_nextp = info.key_next;
1354 kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
1355 parent, key_beg, key_end, *key_nextp);
1363 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
1365 struct hammer2_chain_find_info *info = data;
1366 hammer2_key_t child_beg;
1367 hammer2_key_t child_end;
1369 child_beg = child->bref.key;
1370 child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
1372 if (child_end < info->key_beg)
1374 if (child_beg > info->key_end)
1381 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
1383 struct hammer2_chain_find_info *info = data;
1384 hammer2_chain_t *best;
1385 hammer2_key_t child_end;
1388 * WARNING! Do not discard DUPLICATED chains, it is possible that
1389 * we are catching an insertion half-way done. If a
1390 * duplicated chain turns out to be the best choice the
1391 * caller will re-check its flags after locking it.
1393 * WARNING! Layerq is scanned forwards, exact matches should keep
1394 * the existing info->best.
1396 if ((best = info->best) == NULL) {
1398 * No previous best. Assign best
1401 } else if (best->bref.key <= info->key_beg &&
1402 child->bref.key <= info->key_beg) {
1407 /*info->best = child;*/
1408 } else if (child->bref.key < best->bref.key) {
1410 * Child has a nearer key and best is not flush with key_beg.
1411 * Set best to child. Truncate key_next to the old best key.
1414 if (info->key_next > best->bref.key || info->key_next == 0)
1415 info->key_next = best->bref.key;
1416 } else if (child->bref.key == best->bref.key) {
1418 * If our current best is flush with the child then this
1419 * is an illegal overlap.
1421 * key_next will automatically be limited to the smaller of
1422 * the two end-points.
1428 * Keep the current best but truncate key_next to the child's
1431 * key_next will also automatically be limited to the smaller
1432 * of the two end-points (probably not necessary for this case
1433 * but we do it anyway).
1435 if (info->key_next > child->bref.key || info->key_next == 0)
1436 info->key_next = child->bref.key;
1440 * Always truncate key_next based on child's end-of-range.
1442 child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
1443 if (child_end && (info->key_next > child_end || info->key_next == 0))
1444 info->key_next = child_end;
1450 * Retrieve the specified chain from a media blockref, creating the
1451 * in-memory chain structure which reflects it.
1453 * To handle insertion races pass the INSERT_RACE flag along with the
1454 * generation number of the core. NULL will be returned if the generation
1455 * number changes before we have a chance to insert the chain. Insert
1456 * races can occur because the parent might be held shared.
1458 * Caller must hold the parent locked shared or exclusive since we may
1459 * need the parent's bref array to find our block.
1461 * WARNING! chain->pmp is always set to NULL for any chain representing
1462 * part of the super-root topology.
1465 hammer2_chain_get(hammer2_chain_t *parent, int generation,
1466 hammer2_blockref_t *bref)
1468 hammer2_dev_t *hmp = parent->hmp;
1469 hammer2_chain_t *chain;
1473 * Allocate a chain structure representing the existing media
1474 * entry. Resulting chain has one ref and is not locked.
1476 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
1477 chain = hammer2_chain_alloc(hmp, NULL, bref);
1479 chain = hammer2_chain_alloc(hmp, parent->pmp, bref);
1480 /* ref'd chain returned */
1483 * Flag that the chain is in the parent's blockmap so delete/flush
1484 * knows what to do with it.
1486 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
1489 * Link the chain into its parent. A spinlock is required to safely
1490 * access the RBTREE, and it is possible to collide with another
1491 * hammer2_chain_get() operation because the caller might only hold
1492 * a shared lock on the parent.
1494 KKASSERT(parent->refs > 0);
1495 error = hammer2_chain_insert(parent, chain,
1496 HAMMER2_CHAIN_INSERT_SPIN |
1497 HAMMER2_CHAIN_INSERT_RACE,
1500 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
1501 kprintf("chain %p get race\n", chain);
1502 hammer2_chain_drop(chain);
1505 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
1509 * Return our new chain referenced but not locked, or NULL if
1516 * Lookup initialization/completion API
1519 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
1521 hammer2_chain_ref(parent);
1522 if (flags & HAMMER2_LOOKUP_SHARED) {
1523 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1524 HAMMER2_RESOLVE_SHARED);
1526 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1532 hammer2_chain_lookup_done(hammer2_chain_t *parent)
1535 hammer2_chain_unlock(parent);
1536 hammer2_chain_drop(parent);
1541 hammer2_chain_getparent(hammer2_chain_t **parentp, int how)
1543 hammer2_chain_t *oparent;
1544 hammer2_chain_t *nparent;
1547 * Be careful of order, oparent must be unlocked before nparent
1548 * is locked below to avoid a deadlock.
1551 hammer2_spin_ex(&oparent->core.spin);
1552 nparent = oparent->parent;
1553 hammer2_chain_ref(nparent);
1554 hammer2_spin_unex(&oparent->core.spin);
1556 hammer2_chain_unlock(oparent);
1557 hammer2_chain_drop(oparent);
1561 hammer2_chain_lock(nparent, how);
1568 * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
1569 * (*parentp) typically points to an inode but can also point to a related
1570 * indirect block and this function will recurse upwards and find the inode
1573 * (*parentp) must be exclusively locked and referenced and can be an inode
1574 * or an existing indirect block within the inode.
1576 * On return (*parentp) will be modified to point at the deepest parent chain
1577 * element encountered during the search, as a helper for an insertion or
1578 * deletion. The new (*parentp) will be locked and referenced and the old
1579 * will be unlocked and dereferenced (no change if they are both the same).
1581 * The matching chain will be returned exclusively locked. If NOLOCK is
1582 * requested the chain will be returned only referenced. Note that the
1583 * parent chain must always be locked shared or exclusive, matching the
1584 * HAMMER2_LOOKUP_SHARED flag. We can conceivably lock it SHARED temporarily
1585 * when NOLOCK is specified but that complicates matters if *parentp must
1586 * inherit the chain.
1588 * NOLOCK also implies NODATA, since an unlocked chain usually has a NULL
1589 * data pointer or can otherwise be in flux.
1591 * NULL is returned if no match was found, but (*parentp) will still
1592 * potentially be adjusted.
1594 * If a fatal error occurs (typically an I/O error), a dummy chain is
1595 * returned with chain->error and error-identifying information set. This
1596 * chain will assert if you try to do anything fancy with it.
1598 * XXX Depending on where the error occurs we should allow continued iteration.
1600 * On return (*key_nextp) will point to an iterative value for key_beg.
1601 * (If NULL is returned (*key_nextp) is set to (key_end + 1)).
1603 * This function will also recurse up the chain if the key is not within the
1604 * current parent's range. (*parentp) can never be set to NULL. An iteration
1605 * can simply allow (*parentp) to float inside the loop.
1607 * NOTE! chain->data is not always resolved. By default it will not be
1608 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
1609 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
1610 * BREF_TYPE_DATA as the device buffer can alias the logical file
1614 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
1615 hammer2_key_t key_beg, hammer2_key_t key_end,
1616 int *cache_indexp, int flags)
1619 hammer2_chain_t *parent;
1620 hammer2_chain_t *chain;
1621 hammer2_blockref_t *base;
1622 hammer2_blockref_t *bref;
1623 hammer2_blockref_t bcopy;
1624 hammer2_key_t scan_beg;
1625 hammer2_key_t scan_end;
1627 int how_always = HAMMER2_RESOLVE_ALWAYS;
1628 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1631 int maxloops = 300000;
1633 if (flags & HAMMER2_LOOKUP_ALWAYS) {
1634 how_maybe = how_always;
1635 how = HAMMER2_RESOLVE_ALWAYS;
1636 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
1637 how = HAMMER2_RESOLVE_NEVER;
1639 how = HAMMER2_RESOLVE_MAYBE;
1641 if (flags & HAMMER2_LOOKUP_SHARED) {
1642 how_maybe |= HAMMER2_RESOLVE_SHARED;
1643 how_always |= HAMMER2_RESOLVE_SHARED;
1644 how |= HAMMER2_RESOLVE_SHARED;
1648 * Recurse (*parentp) upward if necessary until the parent completely
1649 * encloses the key range or we hit the inode.
1651 * This function handles races against the flusher doing a delete-
1652 * duplicate above us and re-homes the parent to the duplicate in
1653 * that case, otherwise we'd wind up recursing down a stale chain.
1658 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1659 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1660 scan_beg = parent->bref.key;
1661 scan_end = scan_beg +
1662 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1663 if (key_beg >= scan_beg && key_end <= scan_end)
1665 parent = hammer2_chain_getparent(parentp, how_maybe);
1669 if (--maxloops == 0)
1670 panic("hammer2_chain_lookup: maxloops");
1672 * Locate the blockref array. Currently we do a fully associative
1673 * search through the array.
1675 switch(parent->bref.type) {
1676 case HAMMER2_BREF_TYPE_INODE:
1678 * Special shortcut for embedded data returns the inode
1679 * itself. Callers must detect this condition and access
1680 * the embedded data (the strategy code does this for us).
1682 * This is only applicable to regular files and softlinks.
1684 if (parent->data->ipdata.meta.op_flags &
1685 HAMMER2_OPFLAG_DIRECTDATA) {
1686 if (flags & HAMMER2_LOOKUP_NODIRECT) {
1688 *key_nextp = key_end + 1;
1691 hammer2_chain_ref(parent);
1692 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0)
1693 hammer2_chain_lock(parent, how_always);
1694 *key_nextp = key_end + 1;
1697 base = &parent->data->ipdata.u.blockset.blockref[0];
1698 count = HAMMER2_SET_COUNT;
1700 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1701 case HAMMER2_BREF_TYPE_INDIRECT:
1703 * Handle MATCHIND on the parent
1705 if (flags & HAMMER2_LOOKUP_MATCHIND) {
1706 scan_beg = parent->bref.key;
1707 scan_end = scan_beg +
1708 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1709 if (key_beg == scan_beg && key_end == scan_end) {
1711 hammer2_chain_ref(chain);
1712 hammer2_chain_lock(chain, how_maybe);
1713 *key_nextp = scan_end + 1;
1718 * Optimize indirect blocks in the INITIAL state to avoid
1721 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1724 if (parent->data == NULL)
1725 panic("parent->data is NULL");
1726 base = &parent->data->npdata[0];
1728 count = parent->bytes / sizeof(hammer2_blockref_t);
1730 case HAMMER2_BREF_TYPE_VOLUME:
1731 base = &hmp->voldata.sroot_blockset.blockref[0];
1732 count = HAMMER2_SET_COUNT;
1734 case HAMMER2_BREF_TYPE_FREEMAP:
1735 base = &hmp->voldata.freemap_blockset.blockref[0];
1736 count = HAMMER2_SET_COUNT;
1739 kprintf("hammer2_chain_lookup: unrecognized "
1740 "blockref(B) type: %d",
1743 tsleep(&base, 0, "dead", 0);
1744 panic("hammer2_chain_lookup: unrecognized "
1745 "blockref(B) type: %d",
1747 base = NULL; /* safety */
1748 count = 0; /* safety */
1752 * Merged scan to find next candidate.
1754 * hammer2_base_*() functions require the parent->core.live_* fields
1755 * to be synchronized.
1757 * We need to hold the spinlock to access the block array and RB tree
1758 * and to interlock chain creation.
1760 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
1761 hammer2_chain_countbrefs(parent, base, count);
1766 hammer2_spin_ex(&parent->core.spin);
1767 chain = hammer2_combined_find(parent, base, count,
1768 cache_indexp, key_nextp,
1771 generation = parent->core.generation;
1774 * Exhausted parent chain, iterate.
1777 hammer2_spin_unex(&parent->core.spin);
1778 if (key_beg == key_end) /* short cut single-key case */
1782 * Stop if we reached the end of the iteration.
1784 if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1785 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1790 * Calculate next key, stop if we reached the end of the
1791 * iteration, otherwise go up one level and loop.
1793 key_beg = parent->bref.key +
1794 ((hammer2_key_t)1 << parent->bref.keybits);
1795 if (key_beg == 0 || key_beg > key_end)
1797 parent = hammer2_chain_getparent(parentp, how_maybe);
1802 * Selected from blockref or in-memory chain.
1804 if (chain == NULL) {
1806 hammer2_spin_unex(&parent->core.spin);
1807 chain = hammer2_chain_get(parent, generation,
1809 if (chain == NULL) {
1810 kprintf("retry lookup parent %p keys %016jx:%016jx\n",
1811 parent, key_beg, key_end);
1814 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
1815 hammer2_chain_drop(chain);
1819 hammer2_chain_ref(chain);
1820 hammer2_spin_unex(&parent->core.spin);
1824 * chain is referenced but not locked. We must lock the chain
1825 * to obtain definitive DUPLICATED/DELETED state
1827 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1828 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1829 hammer2_chain_lock(chain, how_maybe);
1831 hammer2_chain_lock(chain, how);
1835 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
1837 * NOTE: Chain's key range is not relevant as there might be
1838 * one-offs within the range that are not deleted.
1840 * NOTE: Lookups can race delete-duplicate because
1841 * delete-duplicate does not lock the parent's core
1842 * (they just use the spinlock on the core). We must
1843 * check for races by comparing the DUPLICATED flag before
1844 * releasing the spinlock with the flag after locking the
1847 if (chain->flags & HAMMER2_CHAIN_DELETED) {
1848 hammer2_chain_unlock(chain);
1849 hammer2_chain_drop(chain);
1850 key_beg = *key_nextp;
1851 if (key_beg == 0 || key_beg > key_end)
1857 * If the chain element is an indirect block it becomes the new
1858 * parent and we loop on it. We must maintain our top-down locks
1859 * to prevent the flusher from interfering (i.e. doing a
1860 * delete-duplicate and leaving us recursing down a deleted chain).
1862 * The parent always has to be locked with at least RESOLVE_MAYBE
1863 * so we can access its data. It might need a fixup if the caller
1864 * passed incompatible flags. Be careful not to cause a deadlock
1865 * as a data-load requires an exclusive lock.
1867 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
1868 * range is within the requested key range we return the indirect
1869 * block and do NOT loop. This is usually only used to acquire
1872 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1873 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1874 hammer2_chain_unlock(parent);
1875 hammer2_chain_drop(parent);
1876 *parentp = parent = chain;
1881 * All done, return the chain.
1883 * If the caller does not want a locked chain, replace the lock with
1884 * a ref. Perhaps this can eventually be optimized to not obtain the
1885 * lock in the first place for situations where the data does not
1886 * need to be resolved.
1889 if (flags & HAMMER2_LOOKUP_NOLOCK)
1890 hammer2_chain_unlock(chain);
1897 * After having issued a lookup we can iterate all matching keys.
1899 * If chain is non-NULL we continue the iteration from just after it's index.
1901 * If chain is NULL we assume the parent was exhausted and continue the
1902 * iteration at the next parent.
1904 * If a fatal error occurs (typically an I/O error), a dummy chain is
1905 * returned with chain->error and error-identifying information set. This
1906 * chain will assert if you try to do anything fancy with it.
1908 * XXX Depending on where the error occurs we should allow continued iteration.
1910 * parent must be locked on entry and remains locked throughout. chain's
1911 * lock status must match flags. Chain is always at least referenced.
1913 * WARNING! The MATCHIND flag does not apply to this function.
1916 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
1917 hammer2_key_t *key_nextp,
1918 hammer2_key_t key_beg, hammer2_key_t key_end,
1919 int *cache_indexp, int flags)
1921 hammer2_chain_t *parent;
1925 * Calculate locking flags for upward recursion.
1927 how_maybe = HAMMER2_RESOLVE_MAYBE;
1928 if (flags & HAMMER2_LOOKUP_SHARED)
1929 how_maybe |= HAMMER2_RESOLVE_SHARED;
1934 * Calculate the next index and recalculate the parent if necessary.
1937 key_beg = chain->bref.key +
1938 ((hammer2_key_t)1 << chain->bref.keybits);
1939 if ((flags & (HAMMER2_LOOKUP_NOLOCK |
1940 HAMMER2_LOOKUP_NOUNLOCK)) == 0) {
1941 hammer2_chain_unlock(chain);
1943 hammer2_chain_drop(chain);
1946 * chain invalid past this point, but we can still do a
1947 * pointer comparison w/parent.
1949 * Any scan where the lookup returned degenerate data embedded
1950 * in the inode has an invalid index and must terminate.
1952 if (chain == parent)
1954 if (key_beg == 0 || key_beg > key_end)
1957 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1958 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1960 * We reached the end of the iteration.
1965 * Continue iteration with next parent unless the current
1966 * parent covers the range.
1968 key_beg = parent->bref.key +
1969 ((hammer2_key_t)1 << parent->bref.keybits);
1970 if (key_beg == 0 || key_beg > key_end)
1972 parent = hammer2_chain_getparent(parentp, how_maybe);
1978 return (hammer2_chain_lookup(parentp, key_nextp,
1980 cache_indexp, flags));
1984 * The raw scan function is similar to lookup/next but does not seek to a key.
1985 * Blockrefs are iterated via first_chain = (parent, NULL) and
1986 * next_chain = (parent, chain).
1988 * The passed-in parent must be locked and its data resolved. The returned
1989 * chain will be locked. Pass chain == NULL to acquire the first sub-chain
1990 * under parent and then iterate with the passed-in chain (which this
1991 * function will unlock).
1994 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t *chain,
1995 int *cache_indexp, int flags)
1998 hammer2_blockref_t *base;
1999 hammer2_blockref_t *bref;
2000 hammer2_blockref_t bcopy;
2002 hammer2_key_t next_key;
2004 int how_always = HAMMER2_RESOLVE_ALWAYS;
2005 int how_maybe = HAMMER2_RESOLVE_MAYBE;
2008 int maxloops = 300000;
2013 * Scan flags borrowed from lookup.
2015 if (flags & HAMMER2_LOOKUP_ALWAYS) {
2016 how_maybe = how_always;
2017 how = HAMMER2_RESOLVE_ALWAYS;
2018 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2019 how = HAMMER2_RESOLVE_NEVER;
2021 how = HAMMER2_RESOLVE_MAYBE;
2023 if (flags & HAMMER2_LOOKUP_SHARED) {
2024 how_maybe |= HAMMER2_RESOLVE_SHARED;
2025 how_always |= HAMMER2_RESOLVE_SHARED;
2026 how |= HAMMER2_RESOLVE_SHARED;
2030 * Calculate key to locate first/next element, unlocking the previous
2031 * element as we go. Be careful, the key calculation can overflow.
2034 key = chain->bref.key +
2035 ((hammer2_key_t)1 << chain->bref.keybits);
2036 hammer2_chain_unlock(chain);
2037 hammer2_chain_drop(chain);
2046 KKASSERT(parent->error == 0); /* XXX case not handled yet */
2047 if (--maxloops == 0)
2048 panic("hammer2_chain_scan: maxloops");
2050 * Locate the blockref array. Currently we do a fully associative
2051 * search through the array.
2053 switch(parent->bref.type) {
2054 case HAMMER2_BREF_TYPE_INODE:
2056 * An inode with embedded data has no sub-chains.
2058 if (parent->data->ipdata.meta.op_flags &
2059 HAMMER2_OPFLAG_DIRECTDATA) {
2062 base = &parent->data->ipdata.u.blockset.blockref[0];
2063 count = HAMMER2_SET_COUNT;
2065 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2066 case HAMMER2_BREF_TYPE_INDIRECT:
2068 * Optimize indirect blocks in the INITIAL state to avoid
2071 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2074 if (parent->data == NULL)
2075 panic("parent->data is NULL");
2076 base = &parent->data->npdata[0];
2078 count = parent->bytes / sizeof(hammer2_blockref_t);
2080 case HAMMER2_BREF_TYPE_VOLUME:
2081 base = &hmp->voldata.sroot_blockset.blockref[0];
2082 count = HAMMER2_SET_COUNT;
2084 case HAMMER2_BREF_TYPE_FREEMAP:
2085 base = &hmp->voldata.freemap_blockset.blockref[0];
2086 count = HAMMER2_SET_COUNT;
2089 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2091 base = NULL; /* safety */
2092 count = 0; /* safety */
2096 * Merged scan to find next candidate.
2098 * hammer2_base_*() functions require the parent->core.live_* fields
2099 * to be synchronized.
2101 * We need to hold the spinlock to access the block array and RB tree
2102 * and to interlock chain creation.
2104 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2105 hammer2_chain_countbrefs(parent, base, count);
2108 hammer2_spin_ex(&parent->core.spin);
2109 chain = hammer2_combined_find(parent, base, count,
2110 cache_indexp, &next_key,
2111 key, HAMMER2_KEY_MAX,
2113 generation = parent->core.generation;
2116 * Exhausted parent chain, we're done.
2119 hammer2_spin_unex(&parent->core.spin);
2120 KKASSERT(chain == NULL);
2125 * Selected from blockref or in-memory chain.
2127 if (chain == NULL) {
2129 hammer2_spin_unex(&parent->core.spin);
2130 chain = hammer2_chain_get(parent, generation, &bcopy);
2131 if (chain == NULL) {
2132 kprintf("retry scan parent %p keys %016jx\n",
2136 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2137 hammer2_chain_drop(chain);
2142 hammer2_chain_ref(chain);
2143 hammer2_spin_unex(&parent->core.spin);
2147 * chain is referenced but not locked. We must lock the chain
2148 * to obtain definitive DUPLICATED/DELETED state
2150 hammer2_chain_lock(chain, how);
2153 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2155 * NOTE: chain's key range is not relevant as there might be
2156 * one-offs within the range that are not deleted.
2158 * NOTE: XXX this could create problems with scans used in
2159 * situations other than mount-time recovery.
2161 * NOTE: Lookups can race delete-duplicate because
2162 * delete-duplicate does not lock the parent's core
2163 * (they just use the spinlock on the core). We must
2164 * check for races by comparing the DUPLICATED flag before
2165 * releasing the spinlock with the flag after locking the
2168 if (chain->flags & HAMMER2_CHAIN_DELETED) {
2169 hammer2_chain_unlock(chain);
2170 hammer2_chain_drop(chain);
2181 * All done, return the chain or NULL
2187 * Create and return a new hammer2 system memory structure of the specified
2188 * key, type and size and insert it under (*parentp). This is a full
2189 * insertion, based on the supplied key/keybits, and may involve creating
2190 * indirect blocks and moving other chains around via delete/duplicate.
2192 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION
2193 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2194 * FULL. This typically means that the caller is creating the chain after
2195 * doing a hammer2_chain_lookup().
2197 * (*parentp) must be exclusive locked and may be replaced on return
2198 * depending on how much work the function had to do.
2200 * (*parentp) must not be errored or this function will assert.
2202 * (*chainp) usually starts out NULL and returns the newly created chain,
2203 * but if the caller desires the caller may allocate a disconnected chain
2204 * and pass it in instead.
2206 * This function should NOT be used to insert INDIRECT blocks. It is
2207 * typically used to create/insert inodes and data blocks.
2209 * Caller must pass-in an exclusively locked parent the new chain is to
2210 * be inserted under, and optionally pass-in a disconnected, exclusively
2211 * locked chain to insert (else we create a new chain). The function will
2212 * adjust (*parentp) as necessary, create or connect the chain, and
2213 * return an exclusively locked chain in *chainp.
2215 * When creating a PFSROOT inode under the super-root, pmp is typically NULL
2216 * and will be reassigned.
2219 hammer2_chain_create(hammer2_chain_t **parentp,
2220 hammer2_chain_t **chainp, hammer2_pfs_t *pmp,
2221 hammer2_key_t key, int keybits, int type, size_t bytes,
2225 hammer2_chain_t *chain;
2226 hammer2_chain_t *parent;
2227 hammer2_blockref_t *base;
2228 hammer2_blockref_t dummy;
2232 int maxloops = 300000;
2235 * Topology may be crossing a PFS boundary.
2238 KKASSERT(hammer2_mtx_owned(&parent->lock));
2239 KKASSERT(parent->error == 0);
2243 if (chain == NULL) {
2245 * First allocate media space and construct the dummy bref,
2246 * then allocate the in-memory chain structure. Set the
2247 * INITIAL flag for fresh chains which do not have embedded
2250 * XXX for now set the check mode of the child based on
2251 * the parent or, if the parent is an inode, the
2252 * specification in the inode.
2254 bzero(&dummy, sizeof(dummy));
2257 dummy.keybits = keybits;
2258 dummy.data_off = hammer2_getradix(bytes);
2259 dummy.methods = parent->bref.methods;
2260 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE &&
2262 dummy.methods &= ~HAMMER2_ENC_CHECK(-1);
2263 dummy.methods |= HAMMER2_ENC_CHECK(
2264 parent->data->ipdata.meta.check_algo);
2267 chain = hammer2_chain_alloc(hmp, pmp, &dummy);
2270 * Lock the chain manually, chain_lock will load the chain
2271 * which we do NOT want to do. (note: chain->refs is set
2272 * to 1 by chain_alloc() for us, but lockcnt is not).
2275 hammer2_mtx_ex(&chain->lock);
2279 * Set INITIAL to optimize I/O. The flag will generally be
2280 * processed when we call hammer2_chain_modify().
2282 * Recalculate bytes to reflect the actual media block
2285 bytes = (hammer2_off_t)1 <<
2286 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
2287 chain->bytes = bytes;
2290 case HAMMER2_BREF_TYPE_VOLUME:
2291 case HAMMER2_BREF_TYPE_FREEMAP:
2292 panic("hammer2_chain_create: called with volume type");
2294 case HAMMER2_BREF_TYPE_INDIRECT:
2295 panic("hammer2_chain_create: cannot be used to"
2296 "create indirect block");
2298 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2299 panic("hammer2_chain_create: cannot be used to"
2300 "create freemap root or node");
2302 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2303 KKASSERT(bytes == sizeof(chain->data->bmdata));
2305 case HAMMER2_BREF_TYPE_INODE:
2306 case HAMMER2_BREF_TYPE_DATA:
2309 * leave chain->data NULL, set INITIAL
2311 KKASSERT(chain->data == NULL);
2312 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
2317 * We are reattaching a previously deleted chain, possibly
2318 * under a new parent and possibly with a new key/keybits.
2319 * The chain does not have to be in a modified state. The
2320 * UPDATE flag will be set later on in this routine.
2322 * Do NOT mess with the current state of the INITIAL flag.
2324 chain->bref.key = key;
2325 chain->bref.keybits = keybits;
2326 if (chain->flags & HAMMER2_CHAIN_DELETED)
2327 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2328 KKASSERT(chain->parent == NULL);
2330 if (flags & HAMMER2_INSERT_PFSROOT)
2331 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
2333 chain->bref.flags &= ~HAMMER2_BREF_FLAG_PFSROOT;
2336 * Calculate how many entries we have in the blockref array and
2337 * determine if an indirect block is required.
2340 if (--maxloops == 0)
2341 panic("hammer2_chain_create: maxloops");
2343 switch(parent->bref.type) {
2344 case HAMMER2_BREF_TYPE_INODE:
2345 KKASSERT((parent->data->ipdata.meta.op_flags &
2346 HAMMER2_OPFLAG_DIRECTDATA) == 0);
2347 KKASSERT(parent->data != NULL);
2348 base = &parent->data->ipdata.u.blockset.blockref[0];
2349 count = HAMMER2_SET_COUNT;
2351 case HAMMER2_BREF_TYPE_INDIRECT:
2352 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2353 if (parent->flags & HAMMER2_CHAIN_INITIAL)
2356 base = &parent->data->npdata[0];
2357 count = parent->bytes / sizeof(hammer2_blockref_t);
2359 case HAMMER2_BREF_TYPE_VOLUME:
2360 KKASSERT(parent->data != NULL);
2361 base = &hmp->voldata.sroot_blockset.blockref[0];
2362 count = HAMMER2_SET_COUNT;
2364 case HAMMER2_BREF_TYPE_FREEMAP:
2365 KKASSERT(parent->data != NULL);
2366 base = &hmp->voldata.freemap_blockset.blockref[0];
2367 count = HAMMER2_SET_COUNT;
2370 panic("hammer2_chain_create: unrecognized blockref type: %d",
2378 * Make sure we've counted the brefs
2380 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2381 hammer2_chain_countbrefs(parent, base, count);
2383 KKASSERT(parent->core.live_count >= 0 &&
2384 parent->core.live_count <= count);
2387 * If no free blockref could be found we must create an indirect
2388 * block and move a number of blockrefs into it. With the parent
2389 * locked we can safely lock each child in order to delete+duplicate
2390 * it without causing a deadlock.
2392 * This may return the new indirect block or the old parent depending
2393 * on where the key falls. NULL is returned on error.
2395 if (parent->core.live_count == count) {
2396 hammer2_chain_t *nparent;
2398 nparent = hammer2_chain_create_indirect(parent, key, keybits,
2400 if (nparent == NULL) {
2402 hammer2_chain_drop(chain);
2406 if (parent != nparent) {
2407 hammer2_chain_unlock(parent);
2408 hammer2_chain_drop(parent);
2409 parent = *parentp = nparent;
2415 * Link the chain into its parent.
2417 if (chain->parent != NULL)
2418 panic("hammer2: hammer2_chain_create: chain already connected");
2419 KKASSERT(chain->parent == NULL);
2420 hammer2_chain_insert(parent, chain,
2421 HAMMER2_CHAIN_INSERT_SPIN |
2422 HAMMER2_CHAIN_INSERT_LIVE,
2427 * Mark the newly created chain modified. This will cause
2428 * UPDATE to be set and process the INITIAL flag.
2430 * Device buffers are not instantiated for DATA elements
2431 * as these are handled by logical buffers.
2433 * Indirect and freemap node indirect blocks are handled
2434 * by hammer2_chain_create_indirect() and not by this
2437 * Data for all other bref types is expected to be
2438 * instantiated (INODE, LEAF).
2440 switch(chain->bref.type) {
2441 case HAMMER2_BREF_TYPE_DATA:
2442 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2443 case HAMMER2_BREF_TYPE_INODE:
2444 hammer2_chain_modify(chain, HAMMER2_MODIFY_OPTDATA);
2448 * Remaining types are not supported by this function.
2449 * In particular, INDIRECT and LEAF_NODE types are
2450 * handled by create_indirect().
2452 panic("hammer2_chain_create: bad type: %d",
2459 * When reconnecting a chain we must set UPDATE and
2460 * setflush so the flush recognizes that it must update
2461 * the bref in the parent.
2463 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
2464 hammer2_chain_ref(chain);
2465 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
2470 * We must setflush(parent) to ensure that it recurses through to
2471 * chain. setflush(chain) might not work because ONFLUSH is possibly
2472 * already set in the chain (so it won't recurse up to set it in the
2475 hammer2_chain_setflush(parent);
2484 * Move the chain from its old parent to a new parent. The chain must have
2485 * already been deleted or already disconnected (or never associated) with
2486 * a parent. The chain is reassociated with the new parent and the deleted
2487 * flag will be cleared (no longer deleted). The chain's modification state
2490 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION
2491 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2492 * FULL. This typically means that the caller is creating the chain after
2493 * doing a hammer2_chain_lookup().
2495 * A non-NULL bref is typically passed when key and keybits must be overridden.
2496 * Note that hammer2_cluster_duplicate() *ONLY* uses the key and keybits fields
2497 * from a passed-in bref and uses the old chain's bref for everything else.
2499 * Neither (parent) or (chain) can be errored.
2501 * If (parent) is non-NULL then the new duplicated chain is inserted under
2504 * If (parent) is NULL then the newly duplicated chain is not inserted
2505 * anywhere, similar to if it had just been chain_alloc()'d (suitable for
2506 * passing into hammer2_chain_create() after this function returns).
2508 * WARNING! This function calls create which means it can insert indirect
2509 * blocks. This can cause other unrelated chains in the parent to
2510 * be moved to a newly inserted indirect block in addition to the
2514 hammer2_chain_rename(hammer2_blockref_t *bref,
2515 hammer2_chain_t **parentp, hammer2_chain_t *chain,
2519 hammer2_chain_t *parent;
2523 * WARNING! We should never resolve DATA to device buffers
2524 * (XXX allow it if the caller did?), and since
2525 * we currently do not have the logical buffer cache
2526 * buffer in-hand to fix its cached physical offset
2527 * we also force the modify code to not COW it. XXX
2530 KKASSERT(chain->parent == NULL);
2531 KKASSERT(chain->error == 0);
2534 * Now create a duplicate of the chain structure, associating
2535 * it with the same core, making it the same size, pointing it
2536 * to the same bref (the same media block).
2539 bref = &chain->bref;
2540 bytes = (hammer2_off_t)1 <<
2541 (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
2544 * If parent is not NULL the duplicated chain will be entered under
2545 * the parent and the UPDATE bit set to tell flush to update
2548 * We must setflush(parent) to ensure that it recurses through to
2549 * chain. setflush(chain) might not work because ONFLUSH is possibly
2550 * already set in the chain (so it won't recurse up to set it in the
2553 * Having both chains locked is extremely important for atomicy.
2555 if (parentp && (parent = *parentp) != NULL) {
2556 KKASSERT(hammer2_mtx_owned(&parent->lock));
2557 KKASSERT(parent->refs > 0);
2558 KKASSERT(parent->error == 0);
2560 hammer2_chain_create(parentp, &chain, chain->pmp,
2561 bref->key, bref->keybits, bref->type,
2562 chain->bytes, flags);
2563 KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE);
2564 hammer2_chain_setflush(*parentp);
2569 * Helper function for deleting chains.
2571 * The chain is removed from the live view (the RBTREE) as well as the parent's
2572 * blockmap. Both chain and its parent must be locked.
2574 * parent may not be errored. chain can be errored.
2577 _hammer2_chain_delete_helper(hammer2_chain_t *parent, hammer2_chain_t *chain,
2582 KKASSERT((chain->flags & (HAMMER2_CHAIN_DELETED |
2583 HAMMER2_CHAIN_FICTITIOUS)) == 0);
2584 KKASSERT(chain->parent == parent);
2587 if (chain->flags & HAMMER2_CHAIN_BMAPPED) {
2589 * Chain is blockmapped, so there must be a parent.
2590 * Atomically remove the chain from the parent and remove
2591 * the blockmap entry.
2593 hammer2_blockref_t *base;
2596 KKASSERT(parent != NULL);
2597 KKASSERT(parent->error == 0);
2598 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
2599 hammer2_chain_modify(parent, HAMMER2_MODIFY_OPTDATA);
2602 * Calculate blockmap pointer
2604 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
2605 hammer2_spin_ex(&parent->core.spin);
2607 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2608 atomic_add_int(&parent->core.live_count, -1);
2609 ++parent->core.generation;
2610 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2611 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2612 --parent->core.chain_count;
2613 chain->parent = NULL;
2615 switch(parent->bref.type) {
2616 case HAMMER2_BREF_TYPE_INODE:
2618 * Access the inode's block array. However, there
2619 * is no block array if the inode is flagged
2620 * DIRECTDATA. The DIRECTDATA case typicaly only
2621 * occurs when a hardlink has been shifted up the
2622 * tree and the original inode gets replaced with
2623 * an OBJTYPE_HARDLINK placeholding inode.
2626 (parent->data->ipdata.meta.op_flags &
2627 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
2629 &parent->data->ipdata.u.blockset.blockref[0];
2633 count = HAMMER2_SET_COUNT;
2635 case HAMMER2_BREF_TYPE_INDIRECT:
2636 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2638 base = &parent->data->npdata[0];
2641 count = parent->bytes / sizeof(hammer2_blockref_t);
2643 case HAMMER2_BREF_TYPE_VOLUME:
2644 base = &hmp->voldata.sroot_blockset.blockref[0];
2645 count = HAMMER2_SET_COUNT;
2647 case HAMMER2_BREF_TYPE_FREEMAP:
2648 base = &parent->data->npdata[0];
2649 count = HAMMER2_SET_COUNT;
2654 panic("hammer2_flush_pass2: "
2655 "unrecognized blockref type: %d",
2660 * delete blockmapped chain from its parent.
2662 * The parent is not affected by any statistics in chain
2663 * which are pending synchronization. That is, there is
2664 * nothing to undo in the parent since they have not yet
2665 * been incorporated into the parent.
2667 * The parent is affected by statistics stored in inodes.
2668 * Those have already been synchronized, so they must be
2669 * undone. XXX split update possible w/delete in middle?
2672 int cache_index = -1;
2673 hammer2_base_delete(parent, base, count,
2674 &cache_index, chain);
2676 hammer2_spin_unex(&parent->core.spin);
2677 } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
2679 * Chain is not blockmapped but a parent is present.
2680 * Atomically remove the chain from the parent. There is
2681 * no blockmap entry to remove.
2683 * Because chain was associated with a parent but not
2684 * synchronized, the chain's *_count_up fields contain
2685 * inode adjustment statistics which must be undone.
2687 hammer2_spin_ex(&parent->core.spin);
2688 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2689 atomic_add_int(&parent->core.live_count, -1);
2690 ++parent->core.generation;
2691 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2692 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2693 --parent->core.chain_count;
2694 chain->parent = NULL;
2695 hammer2_spin_unex(&parent->core.spin);
2698 * Chain is not blockmapped and has no parent. This
2699 * is a degenerate case.
2701 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2706 * Create an indirect block that covers one or more of the elements in the
2707 * current parent. Either returns the existing parent with no locking or
2708 * ref changes or returns the new indirect block locked and referenced
2709 * and leaving the original parent lock/ref intact as well.
2711 * If an error occurs, NULL is returned and *errorp is set to the error.
2713 * The returned chain depends on where the specified key falls.
2715 * The key/keybits for the indirect mode only needs to follow three rules:
2717 * (1) That all elements underneath it fit within its key space and
2719 * (2) That all elements outside it are outside its key space.
2721 * (3) When creating the new indirect block any elements in the current
2722 * parent that fit within the new indirect block's keyspace must be
2723 * moved into the new indirect block.
2725 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
2726 * keyspace the the current parent, but lookup/iteration rules will
2727 * ensure (and must ensure) that rule (2) for all parents leading up
2728 * to the nearest inode or the root volume header is adhered to. This
2729 * is accomplished by always recursing through matching keyspaces in
2730 * the hammer2_chain_lookup() and hammer2_chain_next() API.
2732 * The current implementation calculates the current worst-case keyspace by
2733 * iterating the current parent and then divides it into two halves, choosing
2734 * whichever half has the most elements (not necessarily the half containing
2735 * the requested key).
2737 * We can also opt to use the half with the least number of elements. This
2738 * causes lower-numbered keys (aka logical file offsets) to recurse through
2739 * fewer indirect blocks and higher-numbered keys to recurse through more.
2740 * This also has the risk of not moving enough elements to the new indirect
2741 * block and being forced to create several indirect blocks before the element
2744 * Must be called with an exclusively locked parent.
2746 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
2747 hammer2_key_t *keyp, int keybits,
2748 hammer2_blockref_t *base, int count);
2749 static int hammer2_chain_indkey_normal(hammer2_chain_t *parent,
2750 hammer2_key_t *keyp, int keybits,
2751 hammer2_blockref_t *base, int count);
2754 hammer2_chain_create_indirect(hammer2_chain_t *parent,
2755 hammer2_key_t create_key, int create_bits,
2756 int for_type, int *errorp)
2759 hammer2_blockref_t *base;
2760 hammer2_blockref_t *bref;
2761 hammer2_blockref_t bcopy;
2762 hammer2_chain_t *chain;
2763 hammer2_chain_t *ichain;
2764 hammer2_chain_t dummy;
2765 hammer2_key_t key = create_key;
2766 hammer2_key_t key_beg;
2767 hammer2_key_t key_end;
2768 hammer2_key_t key_next;
2769 int keybits = create_bits;
2776 int maxloops = 300000;
2779 * Calculate the base blockref pointer or NULL if the chain
2780 * is known to be empty. We need to calculate the array count
2781 * for RB lookups either way.
2785 KKASSERT(hammer2_mtx_owned(&parent->lock));
2787 /*hammer2_chain_modify(&parent, HAMMER2_MODIFY_OPTDATA);*/
2788 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2791 switch(parent->bref.type) {
2792 case HAMMER2_BREF_TYPE_INODE:
2793 count = HAMMER2_SET_COUNT;
2795 case HAMMER2_BREF_TYPE_INDIRECT:
2796 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2797 count = parent->bytes / sizeof(hammer2_blockref_t);
2799 case HAMMER2_BREF_TYPE_VOLUME:
2800 count = HAMMER2_SET_COUNT;
2802 case HAMMER2_BREF_TYPE_FREEMAP:
2803 count = HAMMER2_SET_COUNT;
2806 panic("hammer2_chain_create_indirect: "
2807 "unrecognized blockref type: %d",
2813 switch(parent->bref.type) {
2814 case HAMMER2_BREF_TYPE_INODE:
2815 base = &parent->data->ipdata.u.blockset.blockref[0];
2816 count = HAMMER2_SET_COUNT;
2818 case HAMMER2_BREF_TYPE_INDIRECT:
2819 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2820 base = &parent->data->npdata[0];
2821 count = parent->bytes / sizeof(hammer2_blockref_t);
2823 case HAMMER2_BREF_TYPE_VOLUME:
2824 base = &hmp->voldata.sroot_blockset.blockref[0];
2825 count = HAMMER2_SET_COUNT;
2827 case HAMMER2_BREF_TYPE_FREEMAP:
2828 base = &hmp->voldata.freemap_blockset.blockref[0];
2829 count = HAMMER2_SET_COUNT;
2832 panic("hammer2_chain_create_indirect: "
2833 "unrecognized blockref type: %d",
2841 * dummy used in later chain allocation (no longer used for lookups).
2843 bzero(&dummy, sizeof(dummy));
2846 * When creating an indirect block for a freemap node or leaf
2847 * the key/keybits must be fitted to static radix levels because
2848 * particular radix levels use particular reserved blocks in the
2851 * This routine calculates the key/radix of the indirect block
2852 * we need to create, and whether it is on the high-side or the
2855 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2856 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2857 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
2860 keybits = hammer2_chain_indkey_normal(parent, &key, keybits,
2865 * Normalize the key for the radix being represented, keeping the
2866 * high bits and throwing away the low bits.
2868 key &= ~(((hammer2_key_t)1 << keybits) - 1);
2871 * How big should our new indirect block be? It has to be at least
2872 * as large as its parent.
2874 * The freemap uses a specific indirect block size.
2876 * The first indirect block level down from an inode typically
2877 * uses LBUFSIZE (16384), else it uses PBUFSIZE (65536).
2879 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2880 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2881 nbytes = HAMMER2_FREEMAP_LEVELN_PSIZE;
2882 } else if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
2883 nbytes = HAMMER2_IND_BYTES_MIN;
2885 nbytes = HAMMER2_IND_BYTES_MAX;
2887 if (nbytes < count * sizeof(hammer2_blockref_t)) {
2888 KKASSERT(for_type != HAMMER2_BREF_TYPE_FREEMAP_NODE &&
2889 for_type != HAMMER2_BREF_TYPE_FREEMAP_LEAF);
2890 nbytes = count * sizeof(hammer2_blockref_t);
2894 * Ok, create our new indirect block
2896 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2897 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2898 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
2900 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
2902 dummy.bref.key = key;
2903 dummy.bref.keybits = keybits;
2904 dummy.bref.data_off = hammer2_getradix(nbytes);
2905 dummy.bref.methods = parent->bref.methods;
2907 ichain = hammer2_chain_alloc(hmp, parent->pmp, &dummy.bref);
2908 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
2909 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
2910 /* ichain has one ref at this point */
2913 * We have to mark it modified to allocate its block, but use
2914 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
2915 * it won't be acted upon by the flush code.
2917 hammer2_chain_modify(ichain, HAMMER2_MODIFY_OPTDATA);
2920 * Iterate the original parent and move the matching brefs into
2921 * the new indirect block.
2923 * XXX handle flushes.
2926 key_end = HAMMER2_KEY_MAX;
2928 hammer2_spin_ex(&parent->core.spin);
2933 if (++loops > 100000) {
2934 hammer2_spin_unex(&parent->core.spin);
2935 panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
2936 reason, parent, base, count, key_next);
2940 * NOTE: spinlock stays intact, returned chain (if not NULL)
2941 * is not referenced or locked which means that we
2942 * cannot safely check its flagged / deletion status
2945 chain = hammer2_combined_find(parent, base, count,
2946 &cache_index, &key_next,
2949 generation = parent->core.generation;
2952 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
2955 * Skip keys that are not within the key/radix of the new
2956 * indirect block. They stay in the parent.
2958 if ((~(((hammer2_key_t)1 << keybits) - 1) &
2959 (key ^ bref->key)) != 0) {
2960 goto next_key_spinlocked;
2964 * Load the new indirect block by acquiring the related
2965 * chains (potentially from media as it might not be
2966 * in-memory). Then move it to the new parent (ichain)
2967 * via DELETE-DUPLICATE.
2969 * chain is referenced but not locked. We must lock the
2970 * chain to obtain definitive DUPLICATED/DELETED state
2974 * Use chain already present in the RBTREE
2976 hammer2_chain_ref(chain);
2977 hammer2_spin_unex(&parent->core.spin);
2978 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
2981 * Get chain for blockref element. _get returns NULL
2982 * on insertion race.
2985 hammer2_spin_unex(&parent->core.spin);
2986 chain = hammer2_chain_get(parent, generation, &bcopy);
2987 if (chain == NULL) {
2989 hammer2_spin_ex(&parent->core.spin);
2992 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2993 kprintf("REASON 2\n");
2995 hammer2_chain_drop(chain);
2996 hammer2_spin_ex(&parent->core.spin);
2999 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
3003 * This is always live so if the chain has been deleted
3004 * we raced someone and we have to retry.
3006 * NOTE: Lookups can race delete-duplicate because
3007 * delete-duplicate does not lock the parent's core
3008 * (they just use the spinlock on the core). We must
3009 * check for races by comparing the DUPLICATED flag before
3010 * releasing the spinlock with the flag after locking the
3013 * (note reversed logic for this one)
3015 if (chain->flags & HAMMER2_CHAIN_DELETED) {
3016 hammer2_chain_unlock(chain);
3017 hammer2_chain_drop(chain);
3022 * Shift the chain to the indirect block.
3024 * WARNING! No reason for us to load chain data, pass NOSTATS
3025 * to prevent delete/insert from trying to access
3026 * inode stats (and thus asserting if there is no
3027 * chain->data loaded).
3029 hammer2_chain_delete(parent, chain, 0);
3030 hammer2_chain_rename(NULL, &ichain, chain, 0);
3031 hammer2_chain_unlock(chain);
3032 hammer2_chain_drop(chain);
3033 KKASSERT(parent->refs > 0);
3036 hammer2_spin_ex(&parent->core.spin);
3037 next_key_spinlocked:
3038 if (--maxloops == 0)
3039 panic("hammer2_chain_create_indirect: maxloops");
3041 if (key_next == 0 || key_next > key_end)
3046 hammer2_spin_unex(&parent->core.spin);
3049 * Insert the new indirect block into the parent now that we've
3050 * cleared out some entries in the parent. We calculated a good
3051 * insertion index in the loop above (ichain->index).
3053 * We don't have to set UPDATE here because we mark ichain
3054 * modified down below (so the normal modified -> flush -> set-moved
3055 * sequence applies).
3057 * The insertion shouldn't race as this is a completely new block
3058 * and the parent is locked.
3060 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
3061 hammer2_chain_insert(parent, ichain,
3062 HAMMER2_CHAIN_INSERT_SPIN |
3063 HAMMER2_CHAIN_INSERT_LIVE,
3067 * Make sure flushes propogate after our manual insertion.
3069 hammer2_chain_setflush(ichain);
3070 hammer2_chain_setflush(parent);
3073 * Figure out what to return.
3075 if (~(((hammer2_key_t)1 << keybits) - 1) &
3076 (create_key ^ key)) {
3078 * Key being created is outside the key range,
3079 * return the original parent.
3081 hammer2_chain_unlock(ichain);
3082 hammer2_chain_drop(ichain);
3085 * Otherwise its in the range, return the new parent.
3086 * (leave both the new and old parent locked).
3095 * Calculate the keybits and highside/lowside of the freemap node the
3096 * caller is creating.
3098 * This routine will specify the next higher-level freemap key/radix
3099 * representing the lowest-ordered set. By doing so, eventually all
3100 * low-ordered sets will be moved one level down.
3102 * We have to be careful here because the freemap reserves a limited
3103 * number of blocks for a limited number of levels. So we can't just
3104 * push indiscriminately.
3107 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
3108 int keybits, hammer2_blockref_t *base, int count)
3110 hammer2_chain_t *chain;
3111 hammer2_blockref_t *bref;
3113 hammer2_key_t key_beg;
3114 hammer2_key_t key_end;
3115 hammer2_key_t key_next;
3119 int maxloops = 300000;
3127 * Calculate the range of keys in the array being careful to skip
3128 * slots which are overridden with a deletion.
3131 key_end = HAMMER2_KEY_MAX;
3133 hammer2_spin_ex(&parent->core.spin);
3136 if (--maxloops == 0) {
3137 panic("indkey_freemap shit %p %p:%d\n",
3138 parent, base, count);
3140 chain = hammer2_combined_find(parent, base, count,
3141 &cache_index, &key_next,
3152 * Skip deleted chains.
3154 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3155 if (key_next == 0 || key_next > key_end)
3162 * Use the full live (not deleted) element for the scan
3163 * iteration. HAMMER2 does not allow partial replacements.
3165 * XXX should be built into hammer2_combined_find().
3167 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3169 if (keybits > bref->keybits) {
3171 keybits = bref->keybits;
3172 } else if (keybits == bref->keybits && bref->key < key) {
3179 hammer2_spin_unex(&parent->core.spin);
3182 * Return the keybits for a higher-level FREEMAP_NODE covering
3186 case HAMMER2_FREEMAP_LEVEL0_RADIX:
3187 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
3189 case HAMMER2_FREEMAP_LEVEL1_RADIX:
3190 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
3192 case HAMMER2_FREEMAP_LEVEL2_RADIX:
3193 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
3195 case HAMMER2_FREEMAP_LEVEL3_RADIX:
3196 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
3198 case HAMMER2_FREEMAP_LEVEL4_RADIX:
3199 keybits = HAMMER2_FREEMAP_LEVEL5_RADIX;
3201 case HAMMER2_FREEMAP_LEVEL5_RADIX:
3202 panic("hammer2_chain_indkey_freemap: level too high");
3205 panic("hammer2_chain_indkey_freemap: bad radix");
3214 * Calculate the keybits and highside/lowside of the indirect block the
3215 * caller is creating.
3218 hammer2_chain_indkey_normal(hammer2_chain_t *parent, hammer2_key_t *keyp,
3219 int keybits, hammer2_blockref_t *base, int count)
3221 hammer2_blockref_t *bref;
3222 hammer2_chain_t *chain;
3223 hammer2_key_t key_beg;
3224 hammer2_key_t key_end;
3225 hammer2_key_t key_next;
3231 int maxloops = 300000;
3238 * Calculate the range of keys in the array being careful to skip
3239 * slots which are overridden with a deletion. Once the scan
3240 * completes we will cut the key range in half and shift half the
3241 * range into the new indirect block.
3244 key_end = HAMMER2_KEY_MAX;
3246 hammer2_spin_ex(&parent->core.spin);
3249 if (--maxloops == 0) {
3250 panic("indkey_freemap shit %p %p:%d\n",
3251 parent, base, count);
3253 chain = hammer2_combined_find(parent, base, count,
3254 &cache_index, &key_next,
3265 * NOTE: No need to check DUPLICATED here because we do
3266 * not release the spinlock.
3268 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3269 if (key_next == 0 || key_next > key_end)
3276 * Use the full live (not deleted) element for the scan
3277 * iteration. HAMMER2 does not allow partial replacements.
3279 * XXX should be built into hammer2_combined_find().
3281 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3284 * Expand our calculated key range (key, keybits) to fit
3285 * the scanned key. nkeybits represents the full range
3286 * that we will later cut in half (two halves @ nkeybits - 1).
3289 if (nkeybits < bref->keybits) {
3290 if (bref->keybits > 64) {
3291 kprintf("bad bref chain %p bref %p\n",
3295 nkeybits = bref->keybits;
3297 while (nkeybits < 64 &&
3298 (~(((hammer2_key_t)1 << nkeybits) - 1) &
3299 (key ^ bref->key)) != 0) {
3304 * If the new key range is larger we have to determine
3305 * which side of the new key range the existing keys fall
3306 * under by checking the high bit, then collapsing the
3307 * locount into the hicount or vise-versa.
3309 if (keybits != nkeybits) {
3310 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
3321 * The newly scanned key will be in the lower half or the
3322 * upper half of the (new) key range.
3324 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
3333 hammer2_spin_unex(&parent->core.spin);
3334 bref = NULL; /* now invalid (safety) */
3337 * Adjust keybits to represent half of the full range calculated
3338 * above (radix 63 max)
3343 * Select whichever half contains the most elements. Theoretically
3344 * we can select either side as long as it contains at least one
3345 * element (in order to ensure that a free slot is present to hold
3346 * the indirect block).
3348 if (hammer2_indirect_optimize) {
3350 * Insert node for least number of keys, this will arrange
3351 * the first few blocks of a large file or the first few
3352 * inodes in a directory with fewer indirect blocks when
3355 if (hicount < locount && hicount != 0)
3356 key |= (hammer2_key_t)1 << keybits;
3358 key &= ~(hammer2_key_t)1 << keybits;
3361 * Insert node for most number of keys, best for heavily
3364 if (hicount > locount)
3365 key |= (hammer2_key_t)1 << keybits;
3367 key &= ~(hammer2_key_t)1 << keybits;
3375 * Sets CHAIN_DELETED and remove the chain's blockref from the parent if
3378 * Both parent and chain must be locked exclusively.
3380 * This function will modify the parent if the blockref requires removal
3381 * from the parent's block table.
3383 * This function is NOT recursive. Any entity already pushed into the
3384 * chain (such as an inode) may still need visibility into its contents,
3385 * as well as the ability to read and modify the contents. For example,
3386 * for an unlinked file which is still open.
3389 hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
3392 KKASSERT(hammer2_mtx_owned(&chain->lock));
3395 * Nothing to do if already marked.
3397 * We need the spinlock on the core whos RBTREE contains chain
3398 * to protect against races.
3400 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
3401 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
3402 chain->parent == parent);
3403 _hammer2_chain_delete_helper(parent, chain, flags);
3407 * To avoid losing track of a permanent deletion we add the chain
3408 * to the delayed flush queue. If were to flush it right now the
3409 * parent would end up in a modified state and generate I/O.
3410 * The delayed queue gives the parent a chance to be deleted to
3413 if (flags & HAMMER2_DELETE_PERMANENT) {
3414 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
3415 hammer2_delayed_flush(chain);
3417 /* XXX might not be needed */
3418 hammer2_chain_setflush(chain);
3423 * Returns the index of the nearest element in the blockref array >= elm.
3424 * Returns (count) if no element could be found.
3426 * Sets *key_nextp to the next key for loop purposes but does not modify
3427 * it if the next key would be higher than the current value of *key_nextp.
3428 * Note that *key_nexp can overflow to 0, which should be tested by the
3431 * (*cache_indexp) is a heuristic and can be any value without effecting
3434 * WARNING! Must be called with parent's spinlock held. Spinlock remains
3435 * held through the operation.
3438 hammer2_base_find(hammer2_chain_t *parent,
3439 hammer2_blockref_t *base, int count,
3440 int *cache_indexp, hammer2_key_t *key_nextp,
3441 hammer2_key_t key_beg, hammer2_key_t key_end)
3443 hammer2_blockref_t *scan;
3444 hammer2_key_t scan_end;
3449 * Require the live chain's already have their core's counted
3450 * so we can optimize operations.
3452 KKASSERT(parent->flags & HAMMER2_CHAIN_COUNTEDBREFS);
3457 if (count == 0 || base == NULL)
3461 * Sequential optimization using *cache_indexp. This is the most
3464 * We can avoid trailing empty entries on live chains, otherwise
3465 * we might have to check the whole block array.
3469 limit = parent->core.live_zero;
3474 KKASSERT(i < count);
3480 while (i > 0 && (scan->type == 0 || scan->key > key_beg)) {
3487 * Search forwards, stop when we find a scan element which
3488 * encloses the key or until we know that there are no further
3492 if (scan->type != 0) {
3493 scan_end = scan->key +
3494 ((hammer2_key_t)1 << scan->keybits) - 1;
3495 if (scan->key > key_beg || scan_end >= key_beg)
3508 scan_end = scan->key +
3509 ((hammer2_key_t)1 << scan->keybits);
3510 if (scan_end && (*key_nextp > scan_end ||
3512 *key_nextp = scan_end;
3520 * Do a combined search and return the next match either from the blockref
3521 * array or from the in-memory chain. Sets *bresp to the returned bref in
3522 * both cases, or sets it to NULL if the search exhausted. Only returns
3523 * a non-NULL chain if the search matched from the in-memory chain.
3525 * When no in-memory chain has been found and a non-NULL bref is returned
3529 * The returned chain is not locked or referenced. Use the returned bref
3530 * to determine if the search exhausted or not. Iterate if the base find
3531 * is chosen but matches a deleted chain.
3533 * WARNING! Must be called with parent's spinlock held. Spinlock remains
3534 * held through the operation.
3536 static hammer2_chain_t *
3537 hammer2_combined_find(hammer2_chain_t *parent,
3538 hammer2_blockref_t *base, int count,
3539 int *cache_indexp, hammer2_key_t *key_nextp,
3540 hammer2_key_t key_beg, hammer2_key_t key_end,
3541 hammer2_blockref_t **bresp)
3543 hammer2_blockref_t *bref;
3544 hammer2_chain_t *chain;
3548 * Lookup in block array and in rbtree.
3550 *key_nextp = key_end + 1;
3551 i = hammer2_base_find(parent, base, count, cache_indexp,
3552 key_nextp, key_beg, key_end);
3553 chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
3558 if (i == count && chain == NULL) {
3564 * Only chain matched.
3567 bref = &chain->bref;
3572 * Only blockref matched.
3574 if (chain == NULL) {
3580 * Both in-memory and blockref matched, select the nearer element.
3582 * If both are flush with the left-hand side or both are the
3583 * same distance away, select the chain. In this situation the
3584 * chain must have been loaded from the matching blockmap.
3586 if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
3587 chain->bref.key == base[i].key) {
3588 KKASSERT(chain->bref.key == base[i].key);
3589 bref = &chain->bref;
3594 * Select the nearer key
3596 if (chain->bref.key < base[i].key) {
3597 bref = &chain->bref;
3604 * If the bref is out of bounds we've exhausted our search.
3607 if (bref->key > key_end) {
3617 * Locate the specified block array element and delete it. The element
3620 * The spin lock on the related chain must be held.
3622 * NOTE: live_count was adjusted when the chain was deleted, so it does not
3623 * need to be adjusted when we commit the media change.
3626 hammer2_base_delete(hammer2_chain_t *parent,
3627 hammer2_blockref_t *base, int count,
3628 int *cache_indexp, hammer2_chain_t *chain)
3630 hammer2_blockref_t *elm = &chain->bref;
3631 hammer2_key_t key_next;
3635 * Delete element. Expect the element to exist.
3637 * XXX see caller, flush code not yet sophisticated enough to prevent
3638 * re-flushed in some cases.
3640 key_next = 0; /* max range */
3641 i = hammer2_base_find(parent, base, count, cache_indexp,
3642 &key_next, elm->key, elm->key);
3643 if (i == count || base[i].type == 0 ||
3644 base[i].key != elm->key ||
3645 ((chain->flags & HAMMER2_CHAIN_BMAPUPD) == 0 &&
3646 base[i].keybits != elm->keybits)) {
3647 hammer2_spin_unex(&parent->core.spin);
3648 panic("delete base %p element not found at %d/%d elm %p\n",
3649 base, i, count, elm);
3654 * Update stats and zero the entry
3656 parent->bref.data_count -= base[i].data_count;
3657 parent->bref.data_count -= (hammer2_off_t)1 <<
3658 (int)(base[i].data_off & HAMMER2_OFF_MASK_RADIX);
3659 parent->bref.inode_count -= base[i].inode_count;
3660 if (base[i].type == HAMMER2_BREF_TYPE_INODE)
3661 parent->bref.inode_count -= 1;
3663 bzero(&base[i], sizeof(*base));
3666 * We can only optimize parent->core.live_zero for live chains.
3668 if (parent->core.live_zero == i + 1) {
3669 while (--i >= 0 && base[i].type == 0)
3671 parent->core.live_zero = i + 1;
3675 * Clear appropriate blockmap flags in chain.
3677 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED |
3678 HAMMER2_CHAIN_BMAPUPD);
3682 * Insert the specified element. The block array must not already have the
3683 * element and must have space available for the insertion.
3685 * The spin lock on the related chain must be held.
3687 * NOTE: live_count was adjusted when the chain was deleted, so it does not
3688 * need to be adjusted when we commit the media change.
3691 hammer2_base_insert(hammer2_chain_t *parent,
3692 hammer2_blockref_t *base, int count,
3693 int *cache_indexp, hammer2_chain_t *chain)
3695 hammer2_blockref_t *elm = &chain->bref;
3696 hammer2_key_t key_next;
3705 * Insert new element. Expect the element to not already exist
3706 * unless we are replacing it.
3708 * XXX see caller, flush code not yet sophisticated enough to prevent
3709 * re-flushed in some cases.
3711 key_next = 0; /* max range */
3712 i = hammer2_base_find(parent, base, count, cache_indexp,
3713 &key_next, elm->key, elm->key);
3716 * Shortcut fill optimization, typical ordered insertion(s) may not
3719 KKASSERT(i >= 0 && i <= count);
3722 * Set appropriate blockmap flags in chain.
3724 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
3727 * Update stats and zero the entry
3729 parent->bref.data_count += elm->data_count;
3730 parent->bref.data_count += (hammer2_off_t)1 <<
3731 (int)(elm->data_off & HAMMER2_OFF_MASK_RADIX);
3732 parent->bref.inode_count += elm->inode_count;
3733 if (elm->type == HAMMER2_BREF_TYPE_INODE)
3734 parent->bref.inode_count += 1;
3738 * We can only optimize parent->core.live_zero for live chains.
3740 if (i == count && parent->core.live_zero < count) {
3741 i = parent->core.live_zero++;
3746 xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
3747 if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
3748 hammer2_spin_unex(&parent->core.spin);
3749 panic("insert base %p overlapping elements at %d elm %p\n",
3754 * Try to find an empty slot before or after.
3758 while (j > 0 || k < count) {
3760 if (j >= 0 && base[j].type == 0) {
3764 bcopy(&base[j+1], &base[j],
3765 (i - j - 1) * sizeof(*base));
3771 if (k < count && base[k].type == 0) {
3772 bcopy(&base[i], &base[i+1],
3773 (k - i) * sizeof(hammer2_blockref_t));
3777 * We can only update parent->core.live_zero for live
3780 if (parent->core.live_zero <= k)
3781 parent->core.live_zero = k + 1;
3786 panic("hammer2_base_insert: no room!");
3793 for (l = 0; l < count; ++l) {
3795 key_next = base[l].key +
3796 ((hammer2_key_t)1 << base[l].keybits) - 1;
3800 while (++l < count) {
3802 if (base[l].key <= key_next)
3803 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
3804 key_next = base[l].key +
3805 ((hammer2_key_t)1 << base[l].keybits) - 1;
3815 * Sort the blockref array for the chain. Used by the flush code to
3816 * sort the blockref[] array.
3818 * The chain must be exclusively locked AND spin-locked.
3820 typedef hammer2_blockref_t *hammer2_blockref_p;
3824 hammer2_base_sort_callback(const void *v1, const void *v2)
3826 hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
3827 hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
3830 * Make sure empty elements are placed at the end of the array
3832 if (bref1->type == 0) {
3833 if (bref2->type == 0)
3836 } else if (bref2->type == 0) {
3843 if (bref1->key < bref2->key)
3845 if (bref1->key > bref2->key)
3851 hammer2_base_sort(hammer2_chain_t *chain)
3853 hammer2_blockref_t *base;
3856 switch(chain->bref.type) {
3857 case HAMMER2_BREF_TYPE_INODE:
3859 * Special shortcut for embedded data returns the inode
3860 * itself. Callers must detect this condition and access
3861 * the embedded data (the strategy code does this for us).
3863 * This is only applicable to regular files and softlinks.
3865 if (chain->data->ipdata.meta.op_flags &
3866 HAMMER2_OPFLAG_DIRECTDATA) {
3869 base = &chain->data->ipdata.u.blockset.blockref[0];
3870 count = HAMMER2_SET_COUNT;
3872 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3873 case HAMMER2_BREF_TYPE_INDIRECT:
3875 * Optimize indirect blocks in the INITIAL state to avoid
3878 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
3879 base = &chain->data->npdata[0];
3880 count = chain->bytes / sizeof(hammer2_blockref_t);
3882 case HAMMER2_BREF_TYPE_VOLUME:
3883 base = &chain->hmp->voldata.sroot_blockset.blockref[0];
3884 count = HAMMER2_SET_COUNT;
3886 case HAMMER2_BREF_TYPE_FREEMAP:
3887 base = &chain->hmp->voldata.freemap_blockset.blockref[0];
3888 count = HAMMER2_SET_COUNT;
3891 kprintf("hammer2_chain_lookup: unrecognized "
3892 "blockref(A) type: %d",
3895 tsleep(&base, 0, "dead", 0);
3896 panic("hammer2_chain_lookup: unrecognized "
3897 "blockref(A) type: %d",
3899 base = NULL; /* safety */
3900 count = 0; /* safety */
3902 kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
3908 * Chain memory management
3911 hammer2_chain_wait(hammer2_chain_t *chain)
3913 tsleep(chain, 0, "chnflw", 1);
3916 const hammer2_media_data_t *
3917 hammer2_chain_rdata(hammer2_chain_t *chain)
3919 KKASSERT(chain->data != NULL);
3920 return (chain->data);
3923 hammer2_media_data_t *
3924 hammer2_chain_wdata(hammer2_chain_t *chain)
3926 KKASSERT(chain->data != NULL);
3927 return (chain->data);
3931 * Set the check data for a chain. This can be a heavy-weight operation
3932 * and typically only runs on-flush. For file data check data is calculated
3933 * when the logical buffers are flushed.
3936 hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata)
3938 chain->bref.flags &= ~HAMMER2_BREF_FLAG_ZERO;
3940 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
3941 case HAMMER2_CHECK_NONE:
3943 case HAMMER2_CHECK_DISABLED:
3945 case HAMMER2_CHECK_ISCSI32:
3946 chain->bref.check.iscsi32.value =
3947 hammer2_icrc32(bdata, chain->bytes);
3949 case HAMMER2_CHECK_CRC64:
3950 chain->bref.check.crc64.value = 0;
3953 case HAMMER2_CHECK_SHA192:
3955 SHA256_CTX hash_ctx;
3957 uint8_t digest[SHA256_DIGEST_LENGTH];
3958 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
3961 SHA256_Init(&hash_ctx);
3962 SHA256_Update(&hash_ctx, bdata, chain->bytes);
3963 SHA256_Final(u.digest, &hash_ctx);
3964 u.digest64[2] ^= u.digest64[3];
3966 chain->bref.check.sha192.data,
3967 sizeof(chain->bref.check.sha192.data));
3970 case HAMMER2_CHECK_FREEMAP:
3971 chain->bref.check.freemap.icrc32 =
3972 hammer2_icrc32(bdata, chain->bytes);
3975 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
3976 chain->bref.methods);
3982 hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata)
3986 if (chain->bref.flags & HAMMER2_BREF_FLAG_ZERO)
3989 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
3990 case HAMMER2_CHECK_NONE:
3993 case HAMMER2_CHECK_DISABLED:
3996 case HAMMER2_CHECK_ISCSI32:
3997 r = (chain->bref.check.iscsi32.value ==
3998 hammer2_icrc32(bdata, chain->bytes));
4000 case HAMMER2_CHECK_CRC64:
4001 r = (chain->bref.check.crc64.value == 0);
4004 case HAMMER2_CHECK_SHA192:
4006 SHA256_CTX hash_ctx;
4008 uint8_t digest[SHA256_DIGEST_LENGTH];
4009 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
4012 SHA256_Init(&hash_ctx);
4013 SHA256_Update(&hash_ctx, bdata, chain->bytes);
4014 SHA256_Final(u.digest, &hash_ctx);
4015 u.digest64[2] ^= u.digest64[3];
4017 chain->bref.check.sha192.data,
4018 sizeof(chain->bref.check.sha192.data)) == 0) {
4025 case HAMMER2_CHECK_FREEMAP:
4026 r = (chain->bref.check.freemap.icrc32 ==
4027 hammer2_icrc32(bdata, chain->bytes));
4029 kprintf("freemap.icrc %08x icrc32 %08x (%d)\n",
4030 chain->bref.check.freemap.icrc32,
4031 hammer2_icrc32(bdata, chain->bytes), chain->bytes);
4033 kprintf("dio %p buf %016jx,%d bdata %p/%p\n",
4034 chain->dio, chain->dio->bp->b_loffset, chain->dio->bp->b_bufsize, bdata, chain->dio->bp->b_data);
4039 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
4040 chain->bref.methods);
4048 * The caller presents a shared-locked (parent, chain) where the chain
4049 * is of type HAMMER2_OBJTYPE_HARDLINK. The caller must hold the ip
4050 * structure representing the inode locked to prevent
4051 * consolidation/deconsolidation races.
4053 * We locate the hardlink in the current or a common parent directory.
4055 * If we are unable to locate the hardlink, EIO is returned and
4056 * (*chainp) is unlocked and dropped.
4059 hammer2_chain_hardlink_find(hammer2_inode_t *dip,
4060 hammer2_chain_t **parentp,
4061 hammer2_chain_t **chainp,
4064 hammer2_chain_t *parent;
4065 hammer2_chain_t *rchain;
4066 hammer2_key_t key_dummy;
4068 int cache_index = -1;
4071 * Obtain the key for the hardlink from *chainp.
4074 lhc = rchain->data->ipdata.meta.inum;
4075 hammer2_chain_unlock(rchain);
4076 hammer2_chain_drop(rchain);
4081 rchain = hammer2_chain_lookup(parentp, &key_dummy,
4083 &cache_index, flags);
4088 * Iterate parents, handle parent rename races by retrying
4096 parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
4099 if (parent->bref.flags & HAMMER2_BREF_FLAG_PFSROOT)
4101 if (parent->parent == NULL)
4103 parent = parent->parent;
4104 hammer2_chain_ref(parent);
4105 hammer2_chain_unlock(*parentp);
4106 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
4108 if ((*parentp)->parent == parent) {
4109 hammer2_chain_drop(*parentp);
4112 hammer2_chain_unlock(parent);
4113 hammer2_chain_drop(parent);
4114 hammer2_chain_lock(*parentp,
4115 HAMMER2_RESOLVE_ALWAYS |
4117 parent = NULL; /* safety */
4125 return (rchain ? EINVAL : 0);
4129 * Create a snapshot of the specified {parent, ochain} with the specified
4130 * label. The originating hammer2_inode must be exclusively locked for
4133 * The ioctl code has already synced the filesystem.
4136 hammer2_chain_snapshot(hammer2_chain_t *chain, hammer2_ioc_pfs_t *pmp)
4139 const hammer2_inode_data_t *ripdata;
4140 hammer2_inode_data_t *wipdata;
4141 hammer2_chain_t *nchain;
4142 hammer2_inode_t *nip;
4151 kprintf("snapshot %s\n", pmp->name);
4153 name_len = strlen(pmp->name);
4154 lhc = hammer2_dirhash(pmp->name, name_len);
4159 ripdata = &chain->data->ipdata;
4161 opfs_clid = ripdata->meta.pfs_clid;
4166 * Create the snapshot directory under the super-root
4168 * Set PFS type, generate a unique filesystem id, and generate
4169 * a cluster id. Use the same clid when snapshotting a PFS root,
4170 * which theoretically allows the snapshot to be used as part of
4171 * the same cluster (perhaps as a cache).
4173 * Copy the (flushed) blockref array. Theoretically we could use
4174 * chain_duplicate() but it becomes difficult to disentangle
4175 * the shared core so for now just brute-force it.
4180 nip = hammer2_inode_create(hmp->spmp->iroot, &vat, proc0.p_ucred,
4181 pmp->name, name_len, 0,
4183 HAMMER2_INSERT_PFSROOT, &error);
4186 hammer2_inode_modify(nip);
4187 nchain = hammer2_inode_chain(nip, 0, HAMMER2_RESOLVE_ALWAYS);
4188 hammer2_chain_modify(nchain, 0);
4189 wipdata = &nchain->data->ipdata;
4191 nip->meta.pfs_type = HAMMER2_PFSTYPE_MASTER;
4192 nip->meta.pfs_subtype = HAMMER2_PFSSUBTYPE_SNAPSHOT;
4193 nip->meta.op_flags |= HAMMER2_OPFLAG_PFSROOT;
4194 kern_uuidgen(&nip->meta.pfs_fsid, 1);
4197 * Give the snapshot its own private cluster id. As a
4198 * snapshot no further synchronization with the original
4199 * cluster will be done.
4202 if (chain->flags & HAMMER2_CHAIN_PFSBOUNDARY)
4203 nip->meta.pfs_clid = opfs_clid;
4205 kern_uuidgen(&nip->meta.pfs_clid, 1);
4207 kern_uuidgen(&nip->meta.pfs_clid, 1);
4208 nchain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
4210 /* XXX hack blockset copy */
4211 /* XXX doesn't work with real cluster */
4212 wipdata->meta = nip->meta;
4213 wipdata->u.blockset = ripdata->u.blockset;
4214 hammer2_flush(nchain, 1);
4215 hammer2_chain_unlock(nchain);
4216 hammer2_chain_drop(nchain);
4217 hammer2_inode_unlock(nip);