2 * Copyright (c) 2011-2015 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * and Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This subsystem implements most of the core support functions for
37 * the hammer2_chain structure.
39 * Chains are the in-memory version on media objects (volume header, inodes,
40 * indirect blocks, data blocks, etc). Chains represent a portion of the
43 * Chains are no-longer delete-duplicated. Instead, the original in-memory
44 * chain will be moved along with its block reference (e.g. for things like
45 * renames, hardlink operations, modifications, etc), and will be indexed
46 * on a secondary list for flush handling instead of propagating a flag
49 * Concurrent front-end operations can still run against backend flushes
50 * as long as they do not cross the current flush boundary. An operation
51 * running above the current flush (in areas not yet flushed) can become
52 * part of the current flush while ano peration running below the current
53 * flush can become part of the next flush.
55 #include <sys/cdefs.h>
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/types.h>
60 #include <sys/kern_syscall.h>
63 #include <crypto/sha2/sha2.h>
67 static int hammer2_indirect_optimize; /* XXX SYSCTL */
69 static hammer2_chain_t *hammer2_chain_create_indirect(
70 hammer2_chain_t *parent,
71 hammer2_key_t key, int keybits,
72 hammer2_tid_t mtid, int for_type, int *errorp);
73 static void hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop);
74 static hammer2_chain_t *hammer2_combined_find(
75 hammer2_chain_t *parent,
76 hammer2_blockref_t *base, int count,
77 int *cache_indexp, hammer2_key_t *key_nextp,
78 hammer2_key_t key_beg, hammer2_key_t key_end,
79 hammer2_blockref_t **bresp);
82 * Basic RBTree for chains (core->rbtree and core->dbtree). Chains cannot
83 * overlap in the RB trees. Deleted chains are moved from rbtree to either
86 * Chains in delete-duplicate sequences can always iterate through core_entry
87 * to locate the live version of the chain.
89 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
92 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
100 * Compare chains. Overlaps are not supposed to happen and catch
101 * any software issues early we count overlaps as a match.
103 c1_beg = chain1->bref.key;
104 c1_end = c1_beg + ((hammer2_key_t)1 << chain1->bref.keybits) - 1;
105 c2_beg = chain2->bref.key;
106 c2_end = c2_beg + ((hammer2_key_t)1 << chain2->bref.keybits) - 1;
108 if (c1_end < c2_beg) /* fully to the left */
110 if (c1_beg > c2_end) /* fully to the right */
112 return(0); /* overlap (must not cross edge boundary) */
117 hammer2_isclusterable(hammer2_chain_t *chain)
119 if (hammer2_cluster_enable) {
120 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
121 chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
122 chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
130 * Make a chain visible to the flusher. The flusher needs to be able to
131 * do flushes of subdirectory chains or single files so it does a top-down
132 * recursion using the ONFLUSH flag for the recursion. It locates MODIFIED
133 * or UPDATE chains and flushes back up the chain to the volume root.
135 * This routine sets ONFLUSH upward until it hits the volume root. For
136 * simplicity we ignore PFSROOT boundaries whos rules can be complex.
137 * Extra ONFLUSH flagging doesn't hurt the filesystem.
140 hammer2_chain_setflush(hammer2_chain_t *chain)
142 hammer2_chain_t *parent;
144 if ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
145 hammer2_spin_sh(&chain->core.spin);
146 while ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
147 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH);
148 if ((parent = chain->parent) == NULL)
150 hammer2_spin_sh(&parent->core.spin);
151 hammer2_spin_unsh(&chain->core.spin);
154 hammer2_spin_unsh(&chain->core.spin);
159 * Allocate a new disconnected chain element representing the specified
160 * bref. chain->refs is set to 1 and the passed bref is copied to
161 * chain->bref. chain->bytes is derived from the bref.
163 * chain->pmp inherits pmp unless the chain is an inode (other than the
166 * NOTE: Returns a referenced but unlocked (because there is no core) chain.
169 hammer2_chain_alloc(hammer2_dev_t *hmp, hammer2_pfs_t *pmp,
170 hammer2_blockref_t *bref)
172 hammer2_chain_t *chain;
173 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
176 * Construct the appropriate system structure.
179 case HAMMER2_BREF_TYPE_INODE:
180 case HAMMER2_BREF_TYPE_INDIRECT:
181 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
182 case HAMMER2_BREF_TYPE_DATA:
183 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
185 * Chain's are really only associated with the hmp but we
186 * maintain a pmp association for per-mount memory tracking
187 * purposes. The pmp can be NULL.
189 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
191 case HAMMER2_BREF_TYPE_VOLUME:
192 case HAMMER2_BREF_TYPE_FREEMAP:
194 * Only hammer2_chain_bulksnap() calls this function with these
197 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
201 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
206 * Initialize the new chain structure. pmp must be set to NULL for
207 * chains belonging to the super-root topology of a device mount.
209 if (pmp == hmp->spmp)
215 chain->bytes = bytes;
217 chain->flags = HAMMER2_CHAIN_ALLOCATED;
220 * Set the PFS boundary flag if this chain represents a PFS root.
222 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
223 chain->flags |= HAMMER2_CHAIN_PFSBOUNDARY;
224 hammer2_chain_core_init(chain);
230 * Initialize a chain's core structure. This structure used to be allocated
231 * but is now embedded.
233 * The core is not locked. No additional refs on the chain are made.
234 * (trans) must not be NULL if (core) is not NULL.
237 hammer2_chain_core_init(hammer2_chain_t *chain)
240 * Fresh core under nchain (no multi-homing of ochain's
243 RB_INIT(&chain->core.rbtree); /* live chains */
244 hammer2_mtx_init(&chain->lock, "h2chain");
248 * Add a reference to a chain element, preventing its destruction.
250 * (can be called with spinlock held)
253 hammer2_chain_ref(hammer2_chain_t *chain)
255 atomic_add_int(&chain->refs, 1);
257 kprintf("REFC %p %d %08x\n", chain, chain->refs - 1, chain->flags);
263 * Insert the chain in the core rbtree.
265 * Normal insertions are placed in the live rbtree. Insertion of a deleted
266 * chain is a special case used by the flush code that is placed on the
267 * unstaged deleted list to avoid confusing the live view.
269 #define HAMMER2_CHAIN_INSERT_SPIN 0x0001
270 #define HAMMER2_CHAIN_INSERT_LIVE 0x0002
271 #define HAMMER2_CHAIN_INSERT_RACE 0x0004
275 hammer2_chain_insert(hammer2_chain_t *parent, hammer2_chain_t *chain,
276 int flags, int generation)
278 hammer2_chain_t *xchain;
281 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
282 hammer2_spin_ex(&parent->core.spin);
285 * Interlocked by spinlock, check for race
287 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
288 parent->core.generation != generation) {
296 xchain = RB_INSERT(hammer2_chain_tree, &parent->core.rbtree, chain);
297 KASSERT(xchain == NULL,
298 ("hammer2_chain_insert: collision %p %p", chain, xchain));
299 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
300 chain->parent = parent;
301 ++parent->core.chain_count;
302 ++parent->core.generation; /* XXX incs for _get() too, XXX */
305 * We have to keep track of the effective live-view blockref count
306 * so the create code knows when to push an indirect block.
308 if (flags & HAMMER2_CHAIN_INSERT_LIVE)
309 atomic_add_int(&parent->core.live_count, 1);
311 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
312 hammer2_spin_unex(&parent->core.spin);
317 * Drop the caller's reference to the chain. When the ref count drops to
318 * zero this function will try to disassociate the chain from its parent and
319 * deallocate it, then recursely drop the parent using the implied ref
320 * from the chain's chain->parent.
322 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain);
325 hammer2_chain_drop(hammer2_chain_t *chain)
329 if (hammer2_debug & 0x200000)
332 kprintf("DROP %p %d %08x\n", chain, chain->refs - 1, chain->flags);
336 KKASSERT(chain->refs > 0);
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.
364 * The chain cannot be freed if flagged MODIFIED unless we can dispose of that.
365 * The chain cannot be freed if flagged UPDATE unless we can dispose of that.
367 * The core spinlock is allowed nest child-to-parent (not parent-to-child).
371 hammer2_chain_lastdrop(hammer2_chain_t *chain)
375 hammer2_chain_t *parent;
376 hammer2_chain_t *rdrop;
379 * Critical field access.
381 hammer2_spin_ex(&chain->core.spin);
385 * If the chain has a parent the UPDATE bit prevents scrapping
386 * as the chain is needed to properly flush the parent. Try
387 * to complete the 1->0 transition and return NULL. Retry
388 * (return chain) if we are unable to complete the 1->0
389 * transition, else return NULL (nothing more to do).
391 * If the chain has a parent the MODIFIED bit prevents
394 if (chain->flags & (HAMMER2_CHAIN_UPDATE |
395 HAMMER2_CHAIN_MODIFIED)) {
396 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
397 hammer2_spin_unex(&chain->core.spin);
400 hammer2_spin_unex(&chain->core.spin);
404 /* spinlock still held */
407 * The chain has no parent and can be flagged for destruction.
408 * Since it has no parent, UPDATE can also be cleared.
410 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
411 if (chain->flags & HAMMER2_CHAIN_UPDATE)
412 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
415 * If the chain has children or if it has been MODIFIED and
416 * also recorded for DEDUP, we must still flush the chain.
418 * In the case where it has children, the DESTROY flag test
419 * in the flush code will prevent unnecessary flushes of
420 * MODIFIED chains that are not flagged DEDUP so don't worry
423 if (chain->core.chain_count ||
424 (chain->flags & (HAMMER2_CHAIN_MODIFIED |
425 HAMMER2_CHAIN_DEDUP)) ==
426 (HAMMER2_CHAIN_MODIFIED |
427 HAMMER2_CHAIN_DEDUP)) {
429 * Put on flushq (should ensure refs > 1), retry
432 hammer2_spin_unex(&chain->core.spin);
433 hammer2_delayed_flush(chain);
434 return(chain); /* retry drop */
438 * Otherwise we can scrap the MODIFIED bit if it is set,
439 * and continue along the freeing path.
441 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
442 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
443 atomic_add_long(&hammer2_count_modified_chains, -1);
445 /* spinlock still held */
449 * If any children exist we must leave the chain intact with refs == 0.
450 * They exist because chains are retained below us which have refs or
451 * may require flushing. This case can occur when parent != NULL.
453 * Retry (return chain) if we fail to transition the refs to 0, else
454 * return NULL indication nothing more to do.
456 if (chain->core.chain_count) {
457 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
458 hammer2_spin_unex(&chain->core.spin);
461 hammer2_spin_unex(&chain->core.spin);
465 /* spinlock still held */
466 /* no chains left under us */
469 * chain->core has no children left so no accessors can get to our
470 * chain from there. Now we have to lock the parent core to interlock
471 * remaining possible accessors that might bump chain's refs before
472 * we can safely drop chain's refs with intent to free the chain.
475 pmp = chain->pmp; /* can be NULL */
479 * Spinlock the parent and try to drop the last ref on chain.
480 * On success remove chain from its parent, otherwise return NULL.
482 * (normal core locks are top-down recursive but we define core
483 * spinlocks as bottom-up recursive, so this is safe).
485 if ((parent = chain->parent) != NULL) {
486 hammer2_spin_ex(&parent->core.spin);
487 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
488 /* 1->0 transition failed, retry */
489 hammer2_spin_unex(&parent->core.spin);
490 hammer2_spin_unex(&chain->core.spin);
495 * 1->0 transition successful, remove chain from the
498 if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
499 RB_REMOVE(hammer2_chain_tree,
500 &parent->core.rbtree, chain);
501 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
502 --parent->core.chain_count;
503 chain->parent = NULL;
507 * If our chain was the last chain in the parent's core the
508 * core is now empty and its parent might have to be
509 * re-dropped if it has 0 refs.
511 if (parent->core.chain_count == 0) {
513 atomic_add_int(&rdrop->refs, 1);
515 if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0)
519 hammer2_spin_unex(&parent->core.spin);
520 parent = NULL; /* safety */
524 * Successful 1->0 transition and the chain can be destroyed now.
526 * We still have the core spinlock, and core's chain_count is 0.
527 * Any parent spinlock is gone.
529 hammer2_spin_unex(&chain->core.spin);
530 KKASSERT(RB_EMPTY(&chain->core.rbtree) &&
531 chain->core.chain_count == 0);
534 * All spin locks are gone, no pointers remain to the chain, finish
537 KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE |
538 HAMMER2_CHAIN_MODIFIED)) == 0);
539 hammer2_chain_drop_data(chain, 1);
541 KKASSERT(chain->dio == NULL);
544 * Once chain resources are gone we can use the now dead chain
545 * structure to placehold what might otherwise require a recursive
546 * drop, because we have potentially two things to drop and can only
547 * return one directly.
549 if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
550 chain->flags &= ~HAMMER2_CHAIN_ALLOCATED;
552 kfree(chain, hmp->mchain);
556 * Possible chaining loop when parent re-drop needed.
562 * On either last lock release or last drop
565 hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop)
567 /*hammer2_dev_t *hmp = chain->hmp;*/
569 switch(chain->bref.type) {
570 case HAMMER2_BREF_TYPE_VOLUME:
571 case HAMMER2_BREF_TYPE_FREEMAP:
576 KKASSERT(chain->data == NULL);
582 * Lock a referenced chain element, acquiring its data with I/O if necessary,
583 * and specify how you would like the data to be resolved.
585 * If an I/O or other fatal error occurs, chain->error will be set to non-zero.
587 * The lock is allowed to recurse, multiple locking ops will aggregate
588 * the requested resolve types. Once data is assigned it will not be
589 * removed until the last unlock.
591 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
592 * (typically used to avoid device/logical buffer
595 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
596 * the INITIAL-create state (indirect blocks only).
598 * Do not resolve data elements for DATA chains.
599 * (typically used to avoid device/logical buffer
602 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
604 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
605 * it will be locked exclusive.
607 * NOTE: Embedded elements (volume header, inodes) are always resolved
610 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
611 * element will instantiate and zero its buffer, and flush it on
614 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
615 * so as not to instantiate a device buffer, which could alias against
616 * a logical file buffer. However, if ALWAYS is specified the
617 * device buffer will be instantiated anyway.
619 * WARNING! This function blocks on I/O if data needs to be fetched. This
620 * blocking can run concurrent with other compatible lock holders
621 * who do not need data returning. The lock is not upgraded to
622 * exclusive during a data fetch, a separate bit is used to
623 * interlock I/O. However, an exclusive lock holder can still count
624 * on being interlocked against an I/O fetch managed by a shared
628 hammer2_chain_lock(hammer2_chain_t *chain, int how)
631 * Ref and lock the element. Recursive locks are allowed.
633 KKASSERT(chain->refs > 0);
634 atomic_add_int(&chain->lockcnt, 1);
637 * Get the appropriate lock.
639 if (how & HAMMER2_RESOLVE_SHARED)
640 hammer2_mtx_sh(&chain->lock);
642 hammer2_mtx_ex(&chain->lock);
643 ++curthread->td_tracker;
646 * If we already have a valid data pointer no further action is
653 * Do we have to resolve the data?
655 switch(how & HAMMER2_RESOLVE_MASK) {
656 case HAMMER2_RESOLVE_NEVER:
658 case HAMMER2_RESOLVE_MAYBE:
659 if (chain->flags & HAMMER2_CHAIN_INITIAL)
661 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
664 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
666 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
670 case HAMMER2_RESOLVE_ALWAYS:
676 * Caller requires data
678 hammer2_chain_load_data(chain);
682 * Obtains a second shared lock on the chain, does not account the second
683 * shared lock as being owned by the current thread.
685 * Caller must already own a shared lock on this chain.
688 hammer2_chain_push_shared_lock(hammer2_chain_t *chain)
690 hammer2_mtx_sh(&chain->lock);
691 atomic_add_int(&chain->lockcnt, 1);
692 /* do not count in td_tracker for this thread */
696 * Accounts for a shared lock that was pushed to us as being owned by our
700 hammer2_chain_pull_shared_lock(hammer2_chain_t *chain)
702 ++curthread->td_tracker;
706 * Issue I/O and install chain->data. Caller must hold a chain lock, lock
707 * may be of any type.
709 * Once chain->data is set it cannot be disposed of until all locks are
713 hammer2_chain_load_data(hammer2_chain_t *chain)
715 hammer2_blockref_t *bref;
721 * Degenerate case, data already present.
727 KKASSERT(hmp != NULL);
730 * Gain the IOINPROG bit, interlocked block.
736 oflags = chain->flags;
738 if (oflags & HAMMER2_CHAIN_IOINPROG) {
739 nflags = oflags | HAMMER2_CHAIN_IOSIGNAL;
740 tsleep_interlock(&chain->flags, 0);
741 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
742 tsleep(&chain->flags, PINTERLOCKED,
747 nflags = oflags | HAMMER2_CHAIN_IOINPROG;
748 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
756 * We own CHAIN_IOINPROG
758 * Degenerate case if we raced another load.
764 * We must resolve to a device buffer, either by issuing I/O or
765 * by creating a zero-fill element. We do not mark the buffer
766 * dirty when creating a zero-fill element (the hammer2_chain_modify()
767 * API must still be used to do that).
769 * The device buffer is variable-sized in powers of 2 down
770 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
771 * chunk always contains buffers of the same size. (XXX)
773 * The minimum physical IO size may be larger than the variable
779 * The getblk() optimization can only be used on newly created
780 * elements if the physical block size matches the request.
782 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
783 error = hammer2_io_new(hmp, bref->data_off, chain->bytes,
786 error = hammer2_io_bread(hmp, bref->data_off, chain->bytes,
788 hammer2_adjreadcounter(&chain->bref, chain->bytes);
791 chain->error = HAMMER2_ERROR_IO;
792 kprintf("hammer2_chain_lock: I/O error %016jx: %d\n",
793 (intmax_t)bref->data_off, error);
794 hammer2_io_bqrelse(&chain->dio);
800 * NOTE: A locked chain's data cannot be modified without first
801 * calling hammer2_chain_modify().
805 * Clear INITIAL. In this case we used io_new() and the buffer has
806 * been zero'd and marked dirty.
808 bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
809 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
810 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
811 chain->bref.flags |= HAMMER2_BREF_FLAG_ZERO;
812 } else if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
814 * check data not currently synchronized due to
815 * modification. XXX assumes data stays in the buffer
816 * cache, which might not be true (need biodep on flush
817 * to calculate crc? or simple crc?).
820 if (hammer2_chain_testcheck(chain, bdata) == 0) {
821 kprintf("chain %016jx.%02x meth=%02x "
822 "CHECK FAIL %08x (flags=%08x)\n",
823 chain->bref.data_off,
826 hammer2_icrc32(bdata, chain->bytes),
828 chain->error = HAMMER2_ERROR_CHECK;
833 * Setup the data pointer, either pointing it to an embedded data
834 * structure and copying the data from the buffer, or pointing it
837 * The buffer is not retained when copying to an embedded data
838 * structure in order to avoid potential deadlocks or recursions
839 * on the same physical buffer.
841 * WARNING! Other threads can start using the data the instant we
842 * set chain->data non-NULL.
844 switch (bref->type) {
845 case HAMMER2_BREF_TYPE_VOLUME:
846 case HAMMER2_BREF_TYPE_FREEMAP:
848 * Copy data from bp to embedded buffer
850 panic("hammer2_chain_lock: called on unresolved volume header");
852 case HAMMER2_BREF_TYPE_INODE:
853 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
854 case HAMMER2_BREF_TYPE_INDIRECT:
855 case HAMMER2_BREF_TYPE_DATA:
856 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
859 * Point data at the device buffer and leave dio intact.
861 chain->data = (void *)bdata;
866 * Release HAMMER2_CHAIN_IOINPROG and signal waiters if requested.
873 oflags = chain->flags;
874 nflags = oflags & ~(HAMMER2_CHAIN_IOINPROG |
875 HAMMER2_CHAIN_IOSIGNAL);
876 KKASSERT(oflags & HAMMER2_CHAIN_IOINPROG);
877 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
878 if (oflags & HAMMER2_CHAIN_IOSIGNAL)
879 wakeup(&chain->flags);
886 * Unlock and deref a chain element.
888 * On the last lock release any non-embedded data (chain->dio) will be
892 hammer2_chain_unlock(hammer2_chain_t *chain)
894 hammer2_mtx_state_t ostate;
898 --curthread->td_tracker;
900 * If multiple locks are present (or being attempted) on this
901 * particular chain we can just unlock, drop refs, and return.
903 * Otherwise fall-through on the 1->0 transition.
906 lockcnt = chain->lockcnt;
907 KKASSERT(lockcnt > 0);
910 if (atomic_cmpset_int(&chain->lockcnt,
911 lockcnt, lockcnt - 1)) {
912 hammer2_mtx_unlock(&chain->lock);
916 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
923 * On the 1->0 transition we upgrade the core lock (if necessary)
924 * to exclusive for terminal processing. If after upgrading we find
925 * that lockcnt is non-zero, another thread is racing us and will
926 * handle the unload for us later on, so just cleanup and return
927 * leaving the data/io intact
929 * Otherwise if lockcnt is still 0 it is possible for it to become
930 * non-zero and race, but since we hold the core->lock exclusively
931 * all that will happen is that the chain will be reloaded after we
934 ostate = hammer2_mtx_upgrade(&chain->lock);
935 if (chain->lockcnt) {
936 hammer2_mtx_unlock(&chain->lock);
941 * Shortcut the case if the data is embedded or not resolved.
942 * Only drop non-DIO-based data if the chain is not modified.
944 * Do NOT NULL out chain->data (e.g. inode data), it might be
947 if (chain->dio == NULL) {
948 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0)
949 hammer2_chain_drop_data(chain, 0);
950 hammer2_mtx_unlock(&chain->lock);
957 if (hammer2_io_isdirty(chain->dio)) {
958 switch(chain->bref.type) {
959 case HAMMER2_BREF_TYPE_DATA:
960 counterp = &hammer2_iod_file_write;
962 case HAMMER2_BREF_TYPE_INODE:
963 counterp = &hammer2_iod_meta_write;
965 case HAMMER2_BREF_TYPE_INDIRECT:
966 counterp = &hammer2_iod_indr_write;
968 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
969 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
970 counterp = &hammer2_iod_fmap_write;
973 counterp = &hammer2_iod_volu_write;
976 *counterp += chain->bytes;
982 * NOTE: Freemap leaf's use reserved blocks and thus no aliasing
986 hammer2_io_bqrelse(&chain->dio);
987 hammer2_mtx_unlock(&chain->lock);
991 * Helper to obtain the blockref[] array base and count for a chain.
993 * XXX Not widely used yet, various use cases need to be validated and
994 * converted to use this function.
998 hammer2_chain_base_and_count(hammer2_chain_t *parent, int *countp)
1000 hammer2_blockref_t *base;
1003 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1006 switch(parent->bref.type) {
1007 case HAMMER2_BREF_TYPE_INODE:
1008 count = HAMMER2_SET_COUNT;
1010 case HAMMER2_BREF_TYPE_INDIRECT:
1011 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1012 count = parent->bytes / sizeof(hammer2_blockref_t);
1014 case HAMMER2_BREF_TYPE_VOLUME:
1015 count = HAMMER2_SET_COUNT;
1017 case HAMMER2_BREF_TYPE_FREEMAP:
1018 count = HAMMER2_SET_COUNT;
1021 panic("hammer2_chain_create_indirect: "
1022 "unrecognized blockref type: %d",
1028 switch(parent->bref.type) {
1029 case HAMMER2_BREF_TYPE_INODE:
1030 base = &parent->data->ipdata.u.blockset.blockref[0];
1031 count = HAMMER2_SET_COUNT;
1033 case HAMMER2_BREF_TYPE_INDIRECT:
1034 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1035 base = &parent->data->npdata[0];
1036 count = parent->bytes / sizeof(hammer2_blockref_t);
1038 case HAMMER2_BREF_TYPE_VOLUME:
1039 base = &parent->data->voldata.
1040 sroot_blockset.blockref[0];
1041 count = HAMMER2_SET_COUNT;
1043 case HAMMER2_BREF_TYPE_FREEMAP:
1044 base = &parent->data->blkset.blockref[0];
1045 count = HAMMER2_SET_COUNT;
1048 panic("hammer2_chain_create_indirect: "
1049 "unrecognized blockref type: %d",
1061 * This counts the number of live blockrefs in a block array and
1062 * also calculates the point at which all remaining blockrefs are empty.
1063 * This routine can only be called on a live chain (DUPLICATED flag not set).
1065 * NOTE: Flag is not set until after the count is complete, allowing
1066 * callers to test the flag without holding the spinlock.
1068 * NOTE: If base is NULL the related chain is still in the INITIAL
1069 * state and there are no blockrefs to count.
1071 * NOTE: live_count may already have some counts accumulated due to
1072 * creation and deletion and could even be initially negative.
1075 hammer2_chain_countbrefs(hammer2_chain_t *chain,
1076 hammer2_blockref_t *base, int count)
1078 hammer2_spin_ex(&chain->core.spin);
1079 if ((chain->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) {
1081 while (--count >= 0) {
1082 if (base[count].type)
1085 chain->core.live_zero = count + 1;
1086 while (count >= 0) {
1087 if (base[count].type)
1088 atomic_add_int(&chain->core.live_count,
1093 chain->core.live_zero = 0;
1095 /* else do not modify live_count */
1096 atomic_set_int(&chain->flags, HAMMER2_CHAIN_COUNTEDBREFS);
1098 hammer2_spin_unex(&chain->core.spin);
1102 * Resize the chain's physical storage allocation in-place. This function does
1103 * not adjust the data pointer and must be followed by (typically) a
1104 * hammer2_chain_modify() call to copy any old data over and adjust the
1107 * Chains can be resized smaller without reallocating the storage. Resizing
1108 * larger will reallocate the storage. Excess or prior storage is reclaimed
1109 * asynchronously at a later time.
1111 * Must be passed an exclusively locked parent and chain.
1113 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
1114 * to avoid instantiating a device buffer that conflicts with the vnode data
1115 * buffer. However, because H2 can compress or encrypt data, the chain may
1116 * have a dio assigned to it in those situations, and they do not conflict.
1118 * XXX return error if cannot resize.
1121 hammer2_chain_resize(hammer2_inode_t *ip,
1122 hammer2_chain_t *parent, hammer2_chain_t *chain,
1123 hammer2_tid_t mtid, hammer2_off_t dedup_off,
1124 int nradix, int flags)
1133 * Only data and indirect blocks can be resized for now.
1134 * (The volu root, inodes, and freemap elements use a fixed size).
1136 KKASSERT(chain != &hmp->vchain);
1137 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1138 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
1139 KKASSERT(chain->parent == parent);
1142 * Nothing to do if the element is already the proper size
1144 obytes = chain->bytes;
1145 nbytes = 1U << nradix;
1146 if (obytes == nbytes)
1150 * Make sure the old data is instantiated so we can copy it. If this
1151 * is a data block, the device data may be superfluous since the data
1152 * might be in a logical block, but compressed or encrypted data is
1155 * NOTE: The modify will set BMAPUPD for us if BMAPPED is set.
1157 hammer2_chain_modify(chain, mtid, dedup_off, 0);
1160 * Relocate the block, even if making it smaller (because different
1161 * block sizes may be in different regions).
1163 * (data blocks only, we aren't copying the storage here).
1165 hammer2_freemap_alloc(chain, nbytes);
1166 chain->bytes = nbytes;
1167 /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */
1170 * We don't want the followup chain_modify() to try to copy data
1171 * from the old (wrong-sized) buffer. It won't know how much to
1172 * copy. This case should only occur during writes when the
1173 * originator already has the data to write in-hand.
1176 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA);
1177 hammer2_io_brelse(&chain->dio);
1183 * Set the chain modified so its data can be changed by the caller.
1185 * Sets bref.modify_tid to mtid only if mtid != 0. Note that bref.modify_tid
1186 * is a CLC (cluster level change) field and is not updated by parent
1187 * propagation during a flush.
1189 * If the caller passes a non-zero dedup_off we assign data_off to that
1190 * instead of allocating a ne block. Caller must not modify the data already
1191 * present at the target offset.
1194 hammer2_chain_modify(hammer2_chain_t *chain, hammer2_tid_t mtid,
1195 hammer2_off_t dedup_off, int flags)
1197 hammer2_blockref_t obref;
1206 obref = chain->bref;
1207 KKASSERT((chain->flags & HAMMER2_CHAIN_FICTITIOUS) == 0);
1210 * Data is not optional for freemap chains (we must always be sure
1211 * to copy the data on COW storage allocations).
1213 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1214 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1215 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1216 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1220 * Data must be resolved if already assigned, unless explicitly
1221 * flagged otherwise.
1223 if (chain->data == NULL && (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1224 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1225 hammer2_chain_load_data(chain);
1229 * Set MODIFIED to indicate that the chain has been modified.
1230 * Set UPDATE to ensure that the blockref is updated in the parent.
1232 if ((chain->flags & (HAMMER2_CHAIN_DEDUP | HAMMER2_CHAIN_MODIFIED)) ==
1233 (HAMMER2_CHAIN_DEDUP | HAMMER2_CHAIN_MODIFIED)) {
1235 * Modified already set but a new allocation is needed
1236 * anyway because we recorded this data_off for possible
1240 } else if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1242 * Must set modified bit.
1244 atomic_add_long(&hammer2_count_modified_chains, 1);
1245 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1246 hammer2_pfs_memory_inc(chain->pmp); /* can be NULL */
1250 * Already flagged modified, no new allocation is needed.
1256 * Flag parent update required, clear DEDUP flag (already processed
1259 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0)
1260 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1261 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DEDUP);
1264 * The modification or re-modification requires an allocation and
1267 * If dedup_off is non-zero, caller already has a data offset
1268 * containing the caller's desired data. The dedup offset is
1269 * allowed to be in a partially free state and we must be sure
1270 * to reset it to a fully allocated state to force two bulkfree
1271 * passes to free it again.
1273 * XXX can a chain already be marked MODIFIED without a data
1274 * assignment? If not, assert here instead of testing the case.
1276 if (chain != &hmp->vchain && chain != &hmp->fchain) {
1277 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1281 chain->bref.data_off = dedup_off;
1282 chain->bytes = 1 << (dedup_off &
1283 HAMMER2_OFF_MASK_RADIX);
1284 atomic_set_int(&chain->flags,
1285 HAMMER2_CHAIN_DEDUP);
1286 hammer2_freemap_adjust(hmp, &chain->bref,
1287 HAMMER2_FREEMAP_DORECOVER);
1289 hammer2_freemap_alloc(chain, chain->bytes);
1291 /* XXX failed allocation */
1296 * Update mirror_tid and modify_tid. modify_tid is only updated
1297 * if not passed as zero (during flushes, parent propagation passes
1300 * NOTE: chain->pmp could be the device spmp.
1302 chain->bref.mirror_tid = hmp->voldata.mirror_tid + 1;
1304 chain->bref.modify_tid = mtid;
1307 * Set BMAPUPD to tell the flush code that an existing blockmap entry
1308 * requires updating as well as to tell the delete code that the
1309 * chain's blockref might not exactly match (in terms of physical size
1310 * or block offset) the one in the parent's blocktable. The base key
1311 * of course will still match.
1313 if (chain->flags & HAMMER2_CHAIN_BMAPPED)
1314 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD);
1317 * Short-cut data blocks which the caller does not need an actual
1318 * data reference to (aka OPTDATA), as long as the chain does not
1319 * already have a data pointer to the data. This generally means
1320 * that the modifications are being done via the logical buffer cache.
1321 * The INITIAL flag relates only to the device data buffer and thus
1322 * remains unchange in this situation.
1324 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA &&
1325 (flags & HAMMER2_MODIFY_OPTDATA) &&
1326 chain->data == NULL) {
1331 * Clearing the INITIAL flag (for indirect blocks) indicates that
1332 * we've processed the uninitialized storage allocation.
1334 * If this flag is already clear we are likely in a copy-on-write
1335 * situation but we have to be sure NOT to bzero the storage if
1336 * no data is present.
1338 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1339 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1346 * Instantiate data buffer and possibly execute COW operation
1348 switch(chain->bref.type) {
1349 case HAMMER2_BREF_TYPE_VOLUME:
1350 case HAMMER2_BREF_TYPE_FREEMAP:
1352 * The data is embedded, no copy-on-write operation is
1355 KKASSERT(chain->dio == NULL);
1357 case HAMMER2_BREF_TYPE_INODE:
1358 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1359 case HAMMER2_BREF_TYPE_DATA:
1360 case HAMMER2_BREF_TYPE_INDIRECT:
1361 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1363 * Perform the copy-on-write operation
1365 * zero-fill or copy-on-write depending on whether
1366 * chain->data exists or not and set the dirty state for
1367 * the new buffer. hammer2_io_new() will handle the
1370 * If a dedup_off was supplied this is an existing block
1371 * and no COW, copy, or further modification is required.
1373 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1375 if (wasinitial && dedup_off == 0) {
1376 error = hammer2_io_new(hmp, chain->bref.data_off,
1377 chain->bytes, &dio);
1379 error = hammer2_io_bread(hmp, chain->bref.data_off,
1380 chain->bytes, &dio);
1382 hammer2_adjreadcounter(&chain->bref, chain->bytes);
1385 * If an I/O error occurs make sure callers cannot accidently
1386 * modify the old buffer's contents and corrupt the filesystem.
1389 kprintf("hammer2_chain_modify: hmp=%p I/O error\n",
1391 chain->error = HAMMER2_ERROR_IO;
1392 hammer2_io_brelse(&dio);
1393 hammer2_io_brelse(&chain->dio);
1398 bdata = hammer2_io_data(dio, chain->bref.data_off);
1402 * COW (unless a dedup).
1404 KKASSERT(chain->dio != NULL);
1405 if (chain->data != (void *)bdata && dedup_off == 0) {
1406 bcopy(chain->data, bdata, chain->bytes);
1408 } else if (wasinitial == 0) {
1410 * We have a problem. We were asked to COW but
1411 * we don't have any data to COW with!
1413 panic("hammer2_chain_modify: having a COW %p\n",
1418 * Retire the old buffer, replace with the new. Dirty or
1419 * redirty the new buffer.
1421 * WARNING! The system buffer cache may have already flushed
1422 * the buffer, so we must be sure to [re]dirty it
1423 * for further modification.
1425 * If dedup_off was supplied, the caller is not
1426 * expected to make any further modification to the
1430 hammer2_io_bqrelse(&chain->dio);
1431 chain->data = (void *)bdata;
1434 hammer2_io_setdirty(dio);
1437 panic("hammer2_chain_modify: illegal non-embedded type %d",
1444 * setflush on parent indicating that the parent must recurse down
1445 * to us. Do not call on chain itself which might already have it
1449 hammer2_chain_setflush(chain->parent);
1453 * Modify the chain associated with an inode.
1456 hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
1457 hammer2_tid_t mtid, int flags)
1459 hammer2_inode_modify(ip);
1460 hammer2_chain_modify(chain, mtid, 0, flags);
1464 * Volume header data locks
1467 hammer2_voldata_lock(hammer2_dev_t *hmp)
1469 lockmgr(&hmp->vollk, LK_EXCLUSIVE);
1473 hammer2_voldata_unlock(hammer2_dev_t *hmp)
1475 lockmgr(&hmp->vollk, LK_RELEASE);
1479 hammer2_voldata_modify(hammer2_dev_t *hmp)
1481 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1482 atomic_add_long(&hammer2_count_modified_chains, 1);
1483 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
1484 hammer2_pfs_memory_inc(hmp->vchain.pmp);
1489 * This function returns the chain at the nearest key within the specified
1490 * range. The returned chain will be referenced but not locked.
1492 * This function will recurse through chain->rbtree as necessary and will
1493 * return a *key_nextp suitable for iteration. *key_nextp is only set if
1494 * the iteration value is less than the current value of *key_nextp.
1496 * The caller should use (*key_nextp) to calculate the actual range of
1497 * the returned element, which will be (key_beg to *key_nextp - 1), because
1498 * there might be another element which is superior to the returned element
1501 * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
1502 * chains continue to be returned. On EOF (*key_nextp) may overflow since
1503 * it will wind up being (key_end + 1).
1505 * WARNING! Must be called with child's spinlock held. Spinlock remains
1506 * held through the operation.
1508 struct hammer2_chain_find_info {
1509 hammer2_chain_t *best;
1510 hammer2_key_t key_beg;
1511 hammer2_key_t key_end;
1512 hammer2_key_t key_next;
1515 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
1516 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
1520 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
1521 hammer2_key_t key_beg, hammer2_key_t key_end)
1523 struct hammer2_chain_find_info info;
1526 info.key_beg = key_beg;
1527 info.key_end = key_end;
1528 info.key_next = *key_nextp;
1530 RB_SCAN(hammer2_chain_tree, &parent->core.rbtree,
1531 hammer2_chain_find_cmp, hammer2_chain_find_callback,
1533 *key_nextp = info.key_next;
1535 kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
1536 parent, key_beg, key_end, *key_nextp);
1544 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
1546 struct hammer2_chain_find_info *info = data;
1547 hammer2_key_t child_beg;
1548 hammer2_key_t child_end;
1550 child_beg = child->bref.key;
1551 child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
1553 if (child_end < info->key_beg)
1555 if (child_beg > info->key_end)
1562 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
1564 struct hammer2_chain_find_info *info = data;
1565 hammer2_chain_t *best;
1566 hammer2_key_t child_end;
1569 * WARNING! Do not discard DUPLICATED chains, it is possible that
1570 * we are catching an insertion half-way done. If a
1571 * duplicated chain turns out to be the best choice the
1572 * caller will re-check its flags after locking it.
1574 * WARNING! Layerq is scanned forwards, exact matches should keep
1575 * the existing info->best.
1577 if ((best = info->best) == NULL) {
1579 * No previous best. Assign best
1582 } else if (best->bref.key <= info->key_beg &&
1583 child->bref.key <= info->key_beg) {
1588 /*info->best = child;*/
1589 } else if (child->bref.key < best->bref.key) {
1591 * Child has a nearer key and best is not flush with key_beg.
1592 * Set best to child. Truncate key_next to the old best key.
1595 if (info->key_next > best->bref.key || info->key_next == 0)
1596 info->key_next = best->bref.key;
1597 } else if (child->bref.key == best->bref.key) {
1599 * If our current best is flush with the child then this
1600 * is an illegal overlap.
1602 * key_next will automatically be limited to the smaller of
1603 * the two end-points.
1609 * Keep the current best but truncate key_next to the child's
1612 * key_next will also automatically be limited to the smaller
1613 * of the two end-points (probably not necessary for this case
1614 * but we do it anyway).
1616 if (info->key_next > child->bref.key || info->key_next == 0)
1617 info->key_next = child->bref.key;
1621 * Always truncate key_next based on child's end-of-range.
1623 child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
1624 if (child_end && (info->key_next > child_end || info->key_next == 0))
1625 info->key_next = child_end;
1631 * Retrieve the specified chain from a media blockref, creating the
1632 * in-memory chain structure which reflects it.
1634 * To handle insertion races pass the INSERT_RACE flag along with the
1635 * generation number of the core. NULL will be returned if the generation
1636 * number changes before we have a chance to insert the chain. Insert
1637 * races can occur because the parent might be held shared.
1639 * Caller must hold the parent locked shared or exclusive since we may
1640 * need the parent's bref array to find our block.
1642 * WARNING! chain->pmp is always set to NULL for any chain representing
1643 * part of the super-root topology.
1646 hammer2_chain_get(hammer2_chain_t *parent, int generation,
1647 hammer2_blockref_t *bref)
1649 hammer2_dev_t *hmp = parent->hmp;
1650 hammer2_chain_t *chain;
1654 * Allocate a chain structure representing the existing media
1655 * entry. Resulting chain has one ref and is not locked.
1657 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
1658 chain = hammer2_chain_alloc(hmp, NULL, bref);
1660 chain = hammer2_chain_alloc(hmp, parent->pmp, bref);
1661 /* ref'd chain returned */
1664 * Flag that the chain is in the parent's blockmap so delete/flush
1665 * knows what to do with it.
1667 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
1670 * Link the chain into its parent. A spinlock is required to safely
1671 * access the RBTREE, and it is possible to collide with another
1672 * hammer2_chain_get() operation because the caller might only hold
1673 * a shared lock on the parent.
1675 KKASSERT(parent->refs > 0);
1676 error = hammer2_chain_insert(parent, chain,
1677 HAMMER2_CHAIN_INSERT_SPIN |
1678 HAMMER2_CHAIN_INSERT_RACE,
1681 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
1682 kprintf("chain %p get race\n", chain);
1683 hammer2_chain_drop(chain);
1686 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
1690 * Return our new chain referenced but not locked, or NULL if
1697 * Lookup initialization/completion API
1700 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
1702 hammer2_chain_ref(parent);
1703 if (flags & HAMMER2_LOOKUP_SHARED) {
1704 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1705 HAMMER2_RESOLVE_SHARED);
1707 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1713 hammer2_chain_lookup_done(hammer2_chain_t *parent)
1716 hammer2_chain_unlock(parent);
1717 hammer2_chain_drop(parent);
1722 hammer2_chain_getparent(hammer2_chain_t **parentp, int how)
1724 hammer2_chain_t *oparent;
1725 hammer2_chain_t *nparent;
1728 * Be careful of order, oparent must be unlocked before nparent
1729 * is locked below to avoid a deadlock.
1732 hammer2_spin_ex(&oparent->core.spin);
1733 nparent = oparent->parent;
1734 hammer2_chain_ref(nparent);
1735 hammer2_spin_unex(&oparent->core.spin);
1737 hammer2_chain_unlock(oparent);
1738 hammer2_chain_drop(oparent);
1742 hammer2_chain_lock(nparent, how);
1749 * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
1750 * (*parentp) typically points to an inode but can also point to a related
1751 * indirect block and this function will recurse upwards and find the inode
1754 * (*parentp) must be exclusively locked and referenced and can be an inode
1755 * or an existing indirect block within the inode.
1757 * On return (*parentp) will be modified to point at the deepest parent chain
1758 * element encountered during the search, as a helper for an insertion or
1759 * deletion. The new (*parentp) will be locked and referenced and the old
1760 * will be unlocked and dereferenced (no change if they are both the same).
1762 * The matching chain will be returned exclusively locked. If NOLOCK is
1763 * requested the chain will be returned only referenced. Note that the
1764 * parent chain must always be locked shared or exclusive, matching the
1765 * HAMMER2_LOOKUP_SHARED flag. We can conceivably lock it SHARED temporarily
1766 * when NOLOCK is specified but that complicates matters if *parentp must
1767 * inherit the chain.
1769 * NOLOCK also implies NODATA, since an unlocked chain usually has a NULL
1770 * data pointer or can otherwise be in flux.
1772 * NULL is returned if no match was found, but (*parentp) will still
1773 * potentially be adjusted.
1775 * If a fatal error occurs (typically an I/O error), a dummy chain is
1776 * returned with chain->error and error-identifying information set. This
1777 * chain will assert if you try to do anything fancy with it.
1779 * XXX Depending on where the error occurs we should allow continued iteration.
1781 * On return (*key_nextp) will point to an iterative value for key_beg.
1782 * (If NULL is returned (*key_nextp) is set to (key_end + 1)).
1784 * This function will also recurse up the chain if the key is not within the
1785 * current parent's range. (*parentp) can never be set to NULL. An iteration
1786 * can simply allow (*parentp) to float inside the loop.
1788 * NOTE! chain->data is not always resolved. By default it will not be
1789 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
1790 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
1791 * BREF_TYPE_DATA as the device buffer can alias the logical file
1795 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
1796 hammer2_key_t key_beg, hammer2_key_t key_end,
1797 int *cache_indexp, int flags)
1800 hammer2_chain_t *parent;
1801 hammer2_chain_t *chain;
1802 hammer2_blockref_t *base;
1803 hammer2_blockref_t *bref;
1804 hammer2_blockref_t bcopy;
1805 hammer2_key_t scan_beg;
1806 hammer2_key_t scan_end;
1808 int how_always = HAMMER2_RESOLVE_ALWAYS;
1809 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1812 int maxloops = 300000;
1814 if (flags & HAMMER2_LOOKUP_ALWAYS) {
1815 how_maybe = how_always;
1816 how = HAMMER2_RESOLVE_ALWAYS;
1817 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
1818 how = HAMMER2_RESOLVE_NEVER;
1820 how = HAMMER2_RESOLVE_MAYBE;
1822 if (flags & HAMMER2_LOOKUP_SHARED) {
1823 how_maybe |= HAMMER2_RESOLVE_SHARED;
1824 how_always |= HAMMER2_RESOLVE_SHARED;
1825 how |= HAMMER2_RESOLVE_SHARED;
1829 * Recurse (*parentp) upward if necessary until the parent completely
1830 * encloses the key range or we hit the inode.
1832 * This function handles races against the flusher doing a delete-
1833 * duplicate above us and re-homes the parent to the duplicate in
1834 * that case, otherwise we'd wind up recursing down a stale chain.
1839 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1840 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1841 scan_beg = parent->bref.key;
1842 scan_end = scan_beg +
1843 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1844 if (key_beg >= scan_beg && key_end <= scan_end)
1846 parent = hammer2_chain_getparent(parentp, how_maybe);
1850 if (--maxloops == 0)
1851 panic("hammer2_chain_lookup: maxloops");
1853 * Locate the blockref array. Currently we do a fully associative
1854 * search through the array.
1856 switch(parent->bref.type) {
1857 case HAMMER2_BREF_TYPE_INODE:
1859 * Special shortcut for embedded data returns the inode
1860 * itself. Callers must detect this condition and access
1861 * the embedded data (the strategy code does this for us).
1863 * This is only applicable to regular files and softlinks.
1865 if (parent->data->ipdata.meta.op_flags &
1866 HAMMER2_OPFLAG_DIRECTDATA) {
1867 if (flags & HAMMER2_LOOKUP_NODIRECT) {
1869 *key_nextp = key_end + 1;
1872 hammer2_chain_ref(parent);
1873 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0)
1874 hammer2_chain_lock(parent, how_always);
1875 *key_nextp = key_end + 1;
1878 base = &parent->data->ipdata.u.blockset.blockref[0];
1879 count = HAMMER2_SET_COUNT;
1881 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1882 case HAMMER2_BREF_TYPE_INDIRECT:
1884 * Handle MATCHIND on the parent
1886 if (flags & HAMMER2_LOOKUP_MATCHIND) {
1887 scan_beg = parent->bref.key;
1888 scan_end = scan_beg +
1889 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1890 if (key_beg == scan_beg && key_end == scan_end) {
1892 hammer2_chain_ref(chain);
1893 hammer2_chain_lock(chain, how_maybe);
1894 *key_nextp = scan_end + 1;
1899 * Optimize indirect blocks in the INITIAL state to avoid
1902 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1905 if (parent->data == NULL)
1906 panic("parent->data is NULL");
1907 base = &parent->data->npdata[0];
1909 count = parent->bytes / sizeof(hammer2_blockref_t);
1911 case HAMMER2_BREF_TYPE_VOLUME:
1912 base = &parent->data->voldata.sroot_blockset.blockref[0];
1913 count = HAMMER2_SET_COUNT;
1915 case HAMMER2_BREF_TYPE_FREEMAP:
1916 base = &parent->data->blkset.blockref[0];
1917 count = HAMMER2_SET_COUNT;
1920 kprintf("hammer2_chain_lookup: unrecognized "
1921 "blockref(B) type: %d",
1924 tsleep(&base, 0, "dead", 0);
1925 panic("hammer2_chain_lookup: unrecognized "
1926 "blockref(B) type: %d",
1928 base = NULL; /* safety */
1929 count = 0; /* safety */
1933 * Merged scan to find next candidate.
1935 * hammer2_base_*() functions require the parent->core.live_* fields
1936 * to be synchronized.
1938 * We need to hold the spinlock to access the block array and RB tree
1939 * and to interlock chain creation.
1941 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
1942 hammer2_chain_countbrefs(parent, base, count);
1947 hammer2_spin_ex(&parent->core.spin);
1948 chain = hammer2_combined_find(parent, base, count,
1949 cache_indexp, key_nextp,
1952 generation = parent->core.generation;
1955 * Exhausted parent chain, iterate.
1958 hammer2_spin_unex(&parent->core.spin);
1959 if (key_beg == key_end) /* short cut single-key case */
1963 * Stop if we reached the end of the iteration.
1965 if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1966 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1971 * Calculate next key, stop if we reached the end of the
1972 * iteration, otherwise go up one level and loop.
1974 key_beg = parent->bref.key +
1975 ((hammer2_key_t)1 << parent->bref.keybits);
1976 if (key_beg == 0 || key_beg > key_end)
1978 parent = hammer2_chain_getparent(parentp, how_maybe);
1983 * Selected from blockref or in-memory chain.
1985 if (chain == NULL) {
1987 hammer2_spin_unex(&parent->core.spin);
1988 chain = hammer2_chain_get(parent, generation,
1990 if (chain == NULL) {
1991 kprintf("retry lookup parent %p keys %016jx:%016jx\n",
1992 parent, key_beg, key_end);
1995 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
1996 hammer2_chain_drop(chain);
2000 hammer2_chain_ref(chain);
2001 hammer2_spin_unex(&parent->core.spin);
2005 * chain is referenced but not locked. We must lock the chain
2006 * to obtain definitive DUPLICATED/DELETED state
2008 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2009 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2010 hammer2_chain_lock(chain, how_maybe);
2012 hammer2_chain_lock(chain, how);
2016 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2018 * NOTE: Chain's key range is not relevant as there might be
2019 * one-offs within the range that are not deleted.
2021 * NOTE: Lookups can race delete-duplicate because
2022 * delete-duplicate does not lock the parent's core
2023 * (they just use the spinlock on the core). We must
2024 * check for races by comparing the DUPLICATED flag before
2025 * releasing the spinlock with the flag after locking the
2028 if (chain->flags & HAMMER2_CHAIN_DELETED) {
2029 hammer2_chain_unlock(chain);
2030 hammer2_chain_drop(chain);
2031 key_beg = *key_nextp;
2032 if (key_beg == 0 || key_beg > key_end)
2038 * If the chain element is an indirect block it becomes the new
2039 * parent and we loop on it. We must maintain our top-down locks
2040 * to prevent the flusher from interfering (i.e. doing a
2041 * delete-duplicate and leaving us recursing down a deleted chain).
2043 * The parent always has to be locked with at least RESOLVE_MAYBE
2044 * so we can access its data. It might need a fixup if the caller
2045 * passed incompatible flags. Be careful not to cause a deadlock
2046 * as a data-load requires an exclusive lock.
2048 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
2049 * range is within the requested key range we return the indirect
2050 * block and do NOT loop. This is usually only used to acquire
2053 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2054 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2055 hammer2_chain_unlock(parent);
2056 hammer2_chain_drop(parent);
2057 *parentp = parent = chain;
2062 * All done, return the chain.
2064 * If the caller does not want a locked chain, replace the lock with
2065 * a ref. Perhaps this can eventually be optimized to not obtain the
2066 * lock in the first place for situations where the data does not
2067 * need to be resolved.
2070 if (flags & HAMMER2_LOOKUP_NOLOCK)
2071 hammer2_chain_unlock(chain);
2078 * After having issued a lookup we can iterate all matching keys.
2080 * If chain is non-NULL we continue the iteration from just after it's index.
2082 * If chain is NULL we assume the parent was exhausted and continue the
2083 * iteration at the next parent.
2085 * If a fatal error occurs (typically an I/O error), a dummy chain is
2086 * returned with chain->error and error-identifying information set. This
2087 * chain will assert if you try to do anything fancy with it.
2089 * XXX Depending on where the error occurs we should allow continued iteration.
2091 * parent must be locked on entry and remains locked throughout. chain's
2092 * lock status must match flags. Chain is always at least referenced.
2094 * WARNING! The MATCHIND flag does not apply to this function.
2097 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
2098 hammer2_key_t *key_nextp,
2099 hammer2_key_t key_beg, hammer2_key_t key_end,
2100 int *cache_indexp, int flags)
2102 hammer2_chain_t *parent;
2106 * Calculate locking flags for upward recursion.
2108 how_maybe = HAMMER2_RESOLVE_MAYBE;
2109 if (flags & HAMMER2_LOOKUP_SHARED)
2110 how_maybe |= HAMMER2_RESOLVE_SHARED;
2115 * Calculate the next index and recalculate the parent if necessary.
2118 key_beg = chain->bref.key +
2119 ((hammer2_key_t)1 << chain->bref.keybits);
2120 if ((flags & (HAMMER2_LOOKUP_NOLOCK |
2121 HAMMER2_LOOKUP_NOUNLOCK)) == 0) {
2122 hammer2_chain_unlock(chain);
2124 hammer2_chain_drop(chain);
2127 * chain invalid past this point, but we can still do a
2128 * pointer comparison w/parent.
2130 * Any scan where the lookup returned degenerate data embedded
2131 * in the inode has an invalid index and must terminate.
2133 if (chain == parent)
2135 if (key_beg == 0 || key_beg > key_end)
2138 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
2139 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2141 * We reached the end of the iteration.
2146 * Continue iteration with next parent unless the current
2147 * parent covers the range.
2149 key_beg = parent->bref.key +
2150 ((hammer2_key_t)1 << parent->bref.keybits);
2151 if (key_beg == 0 || key_beg > key_end)
2153 parent = hammer2_chain_getparent(parentp, how_maybe);
2159 return (hammer2_chain_lookup(parentp, key_nextp,
2161 cache_indexp, flags));
2165 * The raw scan function is similar to lookup/next but does not seek to a key.
2166 * Blockrefs are iterated via first_bref = (parent, NULL) and
2167 * next_chain = (parent, bref).
2169 * The passed-in parent must be locked and its data resolved. The function
2170 * nominally returns a locked and referenced *chainp != NULL for chains
2171 * the caller might need to recurse on (and will dipose of any *chainp passed
2172 * in). The caller must check the chain->bref.type either way.
2174 * *chainp is not set for leaf elements.
2176 * This function takes a pointer to a stack-based bref structure whos
2177 * contents is updated for each iteration. The same pointer is returned,
2178 * or NULL when the iteration is complete. *firstp must be set to 1 for
2179 * the first ieration. This function will set it to 0.
2181 hammer2_blockref_t *
2182 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t **chainp,
2183 hammer2_blockref_t *bref, int *firstp,
2184 int *cache_indexp, int flags)
2187 hammer2_blockref_t *base;
2188 hammer2_blockref_t *bref_ptr;
2190 hammer2_key_t next_key;
2191 hammer2_chain_t *chain = NULL;
2193 int how_always = HAMMER2_RESOLVE_ALWAYS;
2194 int how_maybe = HAMMER2_RESOLVE_MAYBE;
2197 int maxloops = 300000;
2202 * Scan flags borrowed from lookup.
2204 if (flags & HAMMER2_LOOKUP_ALWAYS) {
2205 how_maybe = how_always;
2206 how = HAMMER2_RESOLVE_ALWAYS;
2207 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2208 how = HAMMER2_RESOLVE_NEVER;
2210 how = HAMMER2_RESOLVE_MAYBE;
2212 if (flags & HAMMER2_LOOKUP_SHARED) {
2213 how_maybe |= HAMMER2_RESOLVE_SHARED;
2214 how_always |= HAMMER2_RESOLVE_SHARED;
2215 how |= HAMMER2_RESOLVE_SHARED;
2219 * Calculate key to locate first/next element, unlocking the previous
2220 * element as we go. Be careful, the key calculation can overflow.
2222 * (also reset bref to NULL)
2228 key = bref->key + ((hammer2_key_t)1 << bref->keybits);
2229 if ((chain = *chainp) != NULL) {
2231 hammer2_chain_unlock(chain);
2232 hammer2_chain_drop(chain);
2242 KKASSERT(parent->error == 0); /* XXX case not handled yet */
2243 if (--maxloops == 0)
2244 panic("hammer2_chain_scan: maxloops");
2246 * Locate the blockref array. Currently we do a fully associative
2247 * search through the array.
2249 switch(parent->bref.type) {
2250 case HAMMER2_BREF_TYPE_INODE:
2252 * An inode with embedded data has no sub-chains.
2254 * WARNING! Bulk scan code may pass a static chain marked
2255 * as BREF_TYPE_INODE with a copy of the volume
2256 * root blockset to snapshot the volume.
2258 if (parent->data->ipdata.meta.op_flags &
2259 HAMMER2_OPFLAG_DIRECTDATA) {
2263 base = &parent->data->ipdata.u.blockset.blockref[0];
2264 count = HAMMER2_SET_COUNT;
2266 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2267 case HAMMER2_BREF_TYPE_INDIRECT:
2269 * Optimize indirect blocks in the INITIAL state to avoid
2272 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2275 if (parent->data == NULL)
2276 panic("parent->data is NULL");
2277 base = &parent->data->npdata[0];
2279 count = parent->bytes / sizeof(hammer2_blockref_t);
2281 case HAMMER2_BREF_TYPE_VOLUME:
2282 base = &parent->data->voldata.sroot_blockset.blockref[0];
2283 count = HAMMER2_SET_COUNT;
2285 case HAMMER2_BREF_TYPE_FREEMAP:
2286 base = &parent->data->blkset.blockref[0];
2287 count = HAMMER2_SET_COUNT;
2290 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2292 base = NULL; /* safety */
2293 count = 0; /* safety */
2297 * Merged scan to find next candidate.
2299 * hammer2_base_*() functions require the parent->core.live_* fields
2300 * to be synchronized.
2302 * We need to hold the spinlock to access the block array and RB tree
2303 * and to interlock chain creation.
2305 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2306 hammer2_chain_countbrefs(parent, base, count);
2310 hammer2_spin_ex(&parent->core.spin);
2311 chain = hammer2_combined_find(parent, base, count,
2312 cache_indexp, &next_key,
2313 key, HAMMER2_KEY_MAX,
2315 generation = parent->core.generation;
2318 * Exhausted parent chain, we're done.
2320 if (bref_ptr == NULL) {
2321 hammer2_spin_unex(&parent->core.spin);
2322 KKASSERT(chain == NULL);
2328 * Copy into the supplied stack-based blockref.
2333 * Selected from blockref or in-memory chain.
2335 if (chain == NULL) {
2336 switch(bref->type) {
2337 case HAMMER2_BREF_TYPE_INODE:
2338 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2339 case HAMMER2_BREF_TYPE_INDIRECT:
2340 case HAMMER2_BREF_TYPE_VOLUME:
2341 case HAMMER2_BREF_TYPE_FREEMAP:
2343 * Recursion, always get the chain
2345 hammer2_spin_unex(&parent->core.spin);
2346 chain = hammer2_chain_get(parent, generation, bref);
2347 if (chain == NULL) {
2348 kprintf("retry scan parent %p keys %016jx\n",
2352 if (bcmp(bref, bref_ptr, sizeof(*bref))) {
2353 hammer2_chain_drop(chain);
2360 * No recursion, do not waste time instantiating
2361 * a chain, just iterate using the bref.
2363 hammer2_spin_unex(&parent->core.spin);
2368 * Recursion or not we need the chain in order to supply
2371 hammer2_chain_ref(chain);
2372 hammer2_spin_unex(&parent->core.spin);
2376 * chain is referenced but not locked. We must lock the chain
2377 * to obtain definitive DUPLICATED/DELETED state
2380 hammer2_chain_lock(chain, how);
2383 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2385 * NOTE: chain's key range is not relevant as there might be
2386 * one-offs within the range that are not deleted.
2388 * NOTE: XXX this could create problems with scans used in
2389 * situations other than mount-time recovery.
2391 * NOTE: Lookups can race delete-duplicate because
2392 * delete-duplicate does not lock the parent's core
2393 * (they just use the spinlock on the core). We must
2394 * check for races by comparing the DUPLICATED flag before
2395 * releasing the spinlock with the flag after locking the
2398 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
2399 hammer2_chain_unlock(chain);
2400 hammer2_chain_drop(chain);
2413 * All done, return the bref or NULL, supply chain if necessary.
2421 * Create and return a new hammer2 system memory structure of the specified
2422 * key, type and size and insert it under (*parentp). This is a full
2423 * insertion, based on the supplied key/keybits, and may involve creating
2424 * indirect blocks and moving other chains around via delete/duplicate.
2426 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION
2427 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2428 * FULL. This typically means that the caller is creating the chain after
2429 * doing a hammer2_chain_lookup().
2431 * (*parentp) must be exclusive locked and may be replaced on return
2432 * depending on how much work the function had to do.
2434 * (*parentp) must not be errored or this function will assert.
2436 * (*chainp) usually starts out NULL and returns the newly created chain,
2437 * but if the caller desires the caller may allocate a disconnected chain
2438 * and pass it in instead.
2440 * This function should NOT be used to insert INDIRECT blocks. It is
2441 * typically used to create/insert inodes and data blocks.
2443 * Caller must pass-in an exclusively locked parent the new chain is to
2444 * be inserted under, and optionally pass-in a disconnected, exclusively
2445 * locked chain to insert (else we create a new chain). The function will
2446 * adjust (*parentp) as necessary, create or connect the chain, and
2447 * return an exclusively locked chain in *chainp.
2449 * When creating a PFSROOT inode under the super-root, pmp is typically NULL
2450 * and will be reassigned.
2453 hammer2_chain_create(hammer2_chain_t **parentp,
2454 hammer2_chain_t **chainp, hammer2_pfs_t *pmp,
2455 hammer2_key_t key, int keybits, int type, size_t bytes,
2456 hammer2_tid_t mtid, hammer2_off_t dedup_off, int flags)
2459 hammer2_chain_t *chain;
2460 hammer2_chain_t *parent;
2461 hammer2_blockref_t *base;
2462 hammer2_blockref_t dummy;
2466 int maxloops = 300000;
2469 * Topology may be crossing a PFS boundary.
2472 KKASSERT(hammer2_mtx_owned(&parent->lock));
2473 KKASSERT(parent->error == 0);
2477 if (chain == NULL) {
2479 * First allocate media space and construct the dummy bref,
2480 * then allocate the in-memory chain structure. Set the
2481 * INITIAL flag for fresh chains which do not have embedded
2484 * XXX for now set the check mode of the child based on
2485 * the parent or, if the parent is an inode, the
2486 * specification in the inode.
2488 bzero(&dummy, sizeof(dummy));
2491 dummy.keybits = keybits;
2492 dummy.data_off = hammer2_getradix(bytes);
2493 dummy.methods = parent->bref.methods;
2494 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE &&
2496 dummy.methods &= ~HAMMER2_ENC_CHECK(-1);
2497 dummy.methods |= HAMMER2_ENC_CHECK(
2498 parent->data->ipdata.meta.check_algo);
2501 chain = hammer2_chain_alloc(hmp, pmp, &dummy);
2504 * Lock the chain manually, chain_lock will load the chain
2505 * which we do NOT want to do. (note: chain->refs is set
2506 * to 1 by chain_alloc() for us, but lockcnt is not).
2509 hammer2_mtx_ex(&chain->lock);
2511 ++curthread->td_tracker;
2514 * Set INITIAL to optimize I/O. The flag will generally be
2515 * processed when we call hammer2_chain_modify().
2517 * Recalculate bytes to reflect the actual media block
2520 bytes = (hammer2_off_t)1 <<
2521 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
2522 chain->bytes = bytes;
2525 case HAMMER2_BREF_TYPE_VOLUME:
2526 case HAMMER2_BREF_TYPE_FREEMAP:
2527 panic("hammer2_chain_create: called with volume type");
2529 case HAMMER2_BREF_TYPE_INDIRECT:
2530 panic("hammer2_chain_create: cannot be used to"
2531 "create indirect block");
2533 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2534 panic("hammer2_chain_create: cannot be used to"
2535 "create freemap root or node");
2537 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2538 KKASSERT(bytes == sizeof(chain->data->bmdata));
2540 case HAMMER2_BREF_TYPE_INODE:
2541 case HAMMER2_BREF_TYPE_DATA:
2544 * leave chain->data NULL, set INITIAL
2546 KKASSERT(chain->data == NULL);
2547 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
2552 * We are reattaching a previously deleted chain, possibly
2553 * under a new parent and possibly with a new key/keybits.
2554 * The chain does not have to be in a modified state. The
2555 * UPDATE flag will be set later on in this routine.
2557 * Do NOT mess with the current state of the INITIAL flag.
2559 chain->bref.key = key;
2560 chain->bref.keybits = keybits;
2561 if (chain->flags & HAMMER2_CHAIN_DELETED)
2562 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2563 KKASSERT(chain->parent == NULL);
2565 if (flags & HAMMER2_INSERT_PFSROOT)
2566 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
2568 chain->bref.flags &= ~HAMMER2_BREF_FLAG_PFSROOT;
2571 * Calculate how many entries we have in the blockref array and
2572 * determine if an indirect block is required.
2575 if (--maxloops == 0)
2576 panic("hammer2_chain_create: maxloops");
2578 switch(parent->bref.type) {
2579 case HAMMER2_BREF_TYPE_INODE:
2580 KKASSERT((parent->data->ipdata.meta.op_flags &
2581 HAMMER2_OPFLAG_DIRECTDATA) == 0);
2582 KKASSERT(parent->data != NULL);
2583 base = &parent->data->ipdata.u.blockset.blockref[0];
2584 count = HAMMER2_SET_COUNT;
2586 case HAMMER2_BREF_TYPE_INDIRECT:
2587 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2588 if (parent->flags & HAMMER2_CHAIN_INITIAL)
2591 base = &parent->data->npdata[0];
2592 count = parent->bytes / sizeof(hammer2_blockref_t);
2594 case HAMMER2_BREF_TYPE_VOLUME:
2595 KKASSERT(parent->data != NULL);
2596 base = &parent->data->voldata.sroot_blockset.blockref[0];
2597 count = HAMMER2_SET_COUNT;
2599 case HAMMER2_BREF_TYPE_FREEMAP:
2600 KKASSERT(parent->data != NULL);
2601 base = &parent->data->blkset.blockref[0];
2602 count = HAMMER2_SET_COUNT;
2605 panic("hammer2_chain_create: unrecognized blockref type: %d",
2613 * Make sure we've counted the brefs
2615 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2616 hammer2_chain_countbrefs(parent, base, count);
2618 KKASSERT(parent->core.live_count >= 0 &&
2619 parent->core.live_count <= count);
2622 * If no free blockref could be found we must create an indirect
2623 * block and move a number of blockrefs into it. With the parent
2624 * locked we can safely lock each child in order to delete+duplicate
2625 * it without causing a deadlock.
2627 * This may return the new indirect block or the old parent depending
2628 * on where the key falls. NULL is returned on error.
2630 if (parent->core.live_count == count) {
2631 hammer2_chain_t *nparent;
2633 nparent = hammer2_chain_create_indirect(parent, key, keybits,
2634 mtid, type, &error);
2635 if (nparent == NULL) {
2637 hammer2_chain_drop(chain);
2641 if (parent != nparent) {
2642 hammer2_chain_unlock(parent);
2643 hammer2_chain_drop(parent);
2644 parent = *parentp = nparent;
2650 * Link the chain into its parent.
2652 if (chain->parent != NULL)
2653 panic("hammer2: hammer2_chain_create: chain already connected");
2654 KKASSERT(chain->parent == NULL);
2655 hammer2_chain_insert(parent, chain,
2656 HAMMER2_CHAIN_INSERT_SPIN |
2657 HAMMER2_CHAIN_INSERT_LIVE,
2662 * Mark the newly created chain modified. This will cause
2663 * UPDATE to be set and process the INITIAL flag.
2665 * Device buffers are not instantiated for DATA elements
2666 * as these are handled by logical buffers.
2668 * Indirect and freemap node indirect blocks are handled
2669 * by hammer2_chain_create_indirect() and not by this
2672 * Data for all other bref types is expected to be
2673 * instantiated (INODE, LEAF).
2675 switch(chain->bref.type) {
2676 case HAMMER2_BREF_TYPE_DATA:
2677 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2678 case HAMMER2_BREF_TYPE_INODE:
2679 hammer2_chain_modify(chain, mtid, dedup_off,
2680 HAMMER2_MODIFY_OPTDATA);
2684 * Remaining types are not supported by this function.
2685 * In particular, INDIRECT and LEAF_NODE types are
2686 * handled by create_indirect().
2688 panic("hammer2_chain_create: bad type: %d",
2695 * When reconnecting a chain we must set UPDATE and
2696 * setflush so the flush recognizes that it must update
2697 * the bref in the parent.
2699 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0)
2700 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
2704 * We must setflush(parent) to ensure that it recurses through to
2705 * chain. setflush(chain) might not work because ONFLUSH is possibly
2706 * already set in the chain (so it won't recurse up to set it in the
2709 hammer2_chain_setflush(parent);
2718 * Move the chain from its old parent to a new parent. The chain must have
2719 * already been deleted or already disconnected (or never associated) with
2720 * a parent. The chain is reassociated with the new parent and the deleted
2721 * flag will be cleared (no longer deleted). The chain's modification state
2724 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION
2725 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2726 * FULL. This typically means that the caller is creating the chain after
2727 * doing a hammer2_chain_lookup().
2729 * A non-NULL bref is typically passed when key and keybits must be overridden.
2730 * Note that hammer2_cluster_duplicate() *ONLY* uses the key and keybits fields
2731 * from a passed-in bref and uses the old chain's bref for everything else.
2733 * Neither (parent) or (chain) can be errored.
2735 * If (parent) is non-NULL then the new duplicated chain is inserted under
2738 * If (parent) is NULL then the newly duplicated chain is not inserted
2739 * anywhere, similar to if it had just been chain_alloc()'d (suitable for
2740 * passing into hammer2_chain_create() after this function returns).
2742 * WARNING! This function calls create which means it can insert indirect
2743 * blocks. This can cause other unrelated chains in the parent to
2744 * be moved to a newly inserted indirect block in addition to the
2748 hammer2_chain_rename(hammer2_blockref_t *bref,
2749 hammer2_chain_t **parentp, hammer2_chain_t *chain,
2750 hammer2_tid_t mtid, int flags)
2753 hammer2_chain_t *parent;
2757 * WARNING! We should never resolve DATA to device buffers
2758 * (XXX allow it if the caller did?), and since
2759 * we currently do not have the logical buffer cache
2760 * buffer in-hand to fix its cached physical offset
2761 * we also force the modify code to not COW it. XXX
2764 KKASSERT(chain->parent == NULL);
2765 KKASSERT(chain->error == 0);
2768 * Now create a duplicate of the chain structure, associating
2769 * it with the same core, making it the same size, pointing it
2770 * to the same bref (the same media block).
2773 bref = &chain->bref;
2774 bytes = (hammer2_off_t)1 <<
2775 (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
2778 * If parent is not NULL the duplicated chain will be entered under
2779 * the parent and the UPDATE bit set to tell flush to update
2782 * We must setflush(parent) to ensure that it recurses through to
2783 * chain. setflush(chain) might not work because ONFLUSH is possibly
2784 * already set in the chain (so it won't recurse up to set it in the
2787 * Having both chains locked is extremely important for atomicy.
2789 if (parentp && (parent = *parentp) != NULL) {
2790 KKASSERT(hammer2_mtx_owned(&parent->lock));
2791 KKASSERT(parent->refs > 0);
2792 KKASSERT(parent->error == 0);
2794 hammer2_chain_create(parentp, &chain, chain->pmp,
2795 bref->key, bref->keybits, bref->type,
2796 chain->bytes, mtid, 0, flags);
2797 KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE);
2798 hammer2_chain_setflush(*parentp);
2803 * Helper function for deleting chains.
2805 * The chain is removed from the live view (the RBTREE) as well as the parent's
2806 * blockmap. Both chain and its parent must be locked.
2808 * parent may not be errored. chain can be errored.
2811 _hammer2_chain_delete_helper(hammer2_chain_t *parent, hammer2_chain_t *chain,
2812 hammer2_tid_t mtid, int flags)
2816 KKASSERT((chain->flags & (HAMMER2_CHAIN_DELETED |
2817 HAMMER2_CHAIN_FICTITIOUS)) == 0);
2818 KKASSERT(chain->parent == parent);
2821 if (chain->flags & HAMMER2_CHAIN_BMAPPED) {
2823 * Chain is blockmapped, so there must be a parent.
2824 * Atomically remove the chain from the parent and remove
2825 * the blockmap entry. The parent must be set modified
2826 * to remove the blockmap entry.
2828 hammer2_blockref_t *base;
2831 KKASSERT(parent != NULL);
2832 KKASSERT(parent->error == 0);
2833 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
2834 hammer2_chain_modify(parent, mtid, 0, HAMMER2_MODIFY_OPTDATA);
2837 * Calculate blockmap pointer
2839 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
2840 hammer2_spin_ex(&parent->core.spin);
2842 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2843 atomic_add_int(&parent->core.live_count, -1);
2844 ++parent->core.generation;
2845 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2846 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2847 --parent->core.chain_count;
2848 chain->parent = NULL;
2850 switch(parent->bref.type) {
2851 case HAMMER2_BREF_TYPE_INODE:
2853 * Access the inode's block array. However, there
2854 * is no block array if the inode is flagged
2855 * DIRECTDATA. The DIRECTDATA case typicaly only
2856 * occurs when a hardlink has been shifted up the
2857 * tree and the original inode gets replaced with
2858 * an OBJTYPE_HARDLINK placeholding inode.
2861 (parent->data->ipdata.meta.op_flags &
2862 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
2864 &parent->data->ipdata.u.blockset.blockref[0];
2868 count = HAMMER2_SET_COUNT;
2870 case HAMMER2_BREF_TYPE_INDIRECT:
2871 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2873 base = &parent->data->npdata[0];
2876 count = parent->bytes / sizeof(hammer2_blockref_t);
2878 case HAMMER2_BREF_TYPE_VOLUME:
2879 base = &parent->data->voldata.
2880 sroot_blockset.blockref[0];
2881 count = HAMMER2_SET_COUNT;
2883 case HAMMER2_BREF_TYPE_FREEMAP:
2884 base = &parent->data->blkset.blockref[0];
2885 count = HAMMER2_SET_COUNT;
2890 panic("hammer2_flush_pass2: "
2891 "unrecognized blockref type: %d",
2896 * delete blockmapped chain from its parent.
2898 * The parent is not affected by any statistics in chain
2899 * which are pending synchronization. That is, there is
2900 * nothing to undo in the parent since they have not yet
2901 * been incorporated into the parent.
2903 * The parent is affected by statistics stored in inodes.
2904 * Those have already been synchronized, so they must be
2905 * undone. XXX split update possible w/delete in middle?
2908 int cache_index = -1;
2909 hammer2_base_delete(parent, base, count,
2910 &cache_index, chain);
2912 hammer2_spin_unex(&parent->core.spin);
2913 } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
2915 * Chain is not blockmapped but a parent is present.
2916 * Atomically remove the chain from the parent. There is
2917 * no blockmap entry to remove.
2919 * Because chain was associated with a parent but not
2920 * synchronized, the chain's *_count_up fields contain
2921 * inode adjustment statistics which must be undone.
2923 hammer2_spin_ex(&parent->core.spin);
2924 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2925 atomic_add_int(&parent->core.live_count, -1);
2926 ++parent->core.generation;
2927 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2928 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2929 --parent->core.chain_count;
2930 chain->parent = NULL;
2931 hammer2_spin_unex(&parent->core.spin);
2934 * Chain is not blockmapped and has no parent. This
2935 * is a degenerate case.
2937 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2942 * Create an indirect block that covers one or more of the elements in the
2943 * current parent. Either returns the existing parent with no locking or
2944 * ref changes or returns the new indirect block locked and referenced
2945 * and leaving the original parent lock/ref intact as well.
2947 * If an error occurs, NULL is returned and *errorp is set to the error.
2949 * The returned chain depends on where the specified key falls.
2951 * The key/keybits for the indirect mode only needs to follow three rules:
2953 * (1) That all elements underneath it fit within its key space and
2955 * (2) That all elements outside it are outside its key space.
2957 * (3) When creating the new indirect block any elements in the current
2958 * parent that fit within the new indirect block's keyspace must be
2959 * moved into the new indirect block.
2961 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
2962 * keyspace the the current parent, but lookup/iteration rules will
2963 * ensure (and must ensure) that rule (2) for all parents leading up
2964 * to the nearest inode or the root volume header is adhered to. This
2965 * is accomplished by always recursing through matching keyspaces in
2966 * the hammer2_chain_lookup() and hammer2_chain_next() API.
2968 * The current implementation calculates the current worst-case keyspace by
2969 * iterating the current parent and then divides it into two halves, choosing
2970 * whichever half has the most elements (not necessarily the half containing
2971 * the requested key).
2973 * We can also opt to use the half with the least number of elements. This
2974 * causes lower-numbered keys (aka logical file offsets) to recurse through
2975 * fewer indirect blocks and higher-numbered keys to recurse through more.
2976 * This also has the risk of not moving enough elements to the new indirect
2977 * block and being forced to create several indirect blocks before the element
2980 * Must be called with an exclusively locked parent.
2982 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
2983 hammer2_key_t *keyp, int keybits,
2984 hammer2_blockref_t *base, int count);
2985 static int hammer2_chain_indkey_normal(hammer2_chain_t *parent,
2986 hammer2_key_t *keyp, int keybits,
2987 hammer2_blockref_t *base, int count);
2990 hammer2_chain_create_indirect(hammer2_chain_t *parent,
2991 hammer2_key_t create_key, int create_bits,
2992 hammer2_tid_t mtid, int for_type, int *errorp)
2995 hammer2_blockref_t *base;
2996 hammer2_blockref_t *bref;
2997 hammer2_blockref_t bcopy;
2998 hammer2_chain_t *chain;
2999 hammer2_chain_t *ichain;
3000 hammer2_chain_t dummy;
3001 hammer2_key_t key = create_key;
3002 hammer2_key_t key_beg;
3003 hammer2_key_t key_end;
3004 hammer2_key_t key_next;
3005 int keybits = create_bits;
3012 int maxloops = 300000;
3015 * Calculate the base blockref pointer or NULL if the chain
3016 * is known to be empty. We need to calculate the array count
3017 * for RB lookups either way.
3021 KKASSERT(hammer2_mtx_owned(&parent->lock));
3023 /*hammer2_chain_modify(&parent, HAMMER2_MODIFY_OPTDATA);*/
3024 base = hammer2_chain_base_and_count(parent, &count);
3027 * dummy used in later chain allocation (no longer used for lookups).
3029 bzero(&dummy, sizeof(dummy));
3032 * When creating an indirect block for a freemap node or leaf
3033 * the key/keybits must be fitted to static radix levels because
3034 * particular radix levels use particular reserved blocks in the
3037 * This routine calculates the key/radix of the indirect block
3038 * we need to create, and whether it is on the high-side or the
3041 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
3042 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
3043 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
3046 keybits = hammer2_chain_indkey_normal(parent, &key, keybits,
3051 * Normalize the key for the radix being represented, keeping the
3052 * high bits and throwing away the low bits.
3054 key &= ~(((hammer2_key_t)1 << keybits) - 1);
3057 * How big should our new indirect block be? It has to be at least
3058 * as large as its parent.
3060 * The freemap uses a specific indirect block size.
3062 * The first indirect block level down from an inode typically
3063 * uses LBUFSIZE (16384), else it uses PBUFSIZE (65536).
3065 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
3066 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
3067 nbytes = HAMMER2_FREEMAP_LEVELN_PSIZE;
3068 } else if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
3069 nbytes = HAMMER2_IND_BYTES_MIN;
3071 nbytes = HAMMER2_IND_BYTES_MAX;
3073 if (nbytes < count * sizeof(hammer2_blockref_t)) {
3074 KKASSERT(for_type != HAMMER2_BREF_TYPE_FREEMAP_NODE &&
3075 for_type != HAMMER2_BREF_TYPE_FREEMAP_LEAF);
3076 nbytes = count * sizeof(hammer2_blockref_t);
3080 * Ok, create our new indirect block
3082 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
3083 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
3084 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
3086 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
3088 dummy.bref.key = key;
3089 dummy.bref.keybits = keybits;
3090 dummy.bref.data_off = hammer2_getradix(nbytes);
3091 dummy.bref.methods = parent->bref.methods;
3093 ichain = hammer2_chain_alloc(hmp, parent->pmp, &dummy.bref);
3094 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
3095 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
3096 /* ichain has one ref at this point */
3099 * We have to mark it modified to allocate its block, but use
3100 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
3101 * it won't be acted upon by the flush code.
3103 hammer2_chain_modify(ichain, mtid, 0, HAMMER2_MODIFY_OPTDATA);
3106 * Iterate the original parent and move the matching brefs into
3107 * the new indirect block.
3109 * XXX handle flushes.
3112 key_end = HAMMER2_KEY_MAX;
3114 hammer2_spin_ex(&parent->core.spin);
3120 * Parent may have been modified, relocating its block array.
3121 * Reload the base pointer.
3123 base = hammer2_chain_base_and_count(parent, &count);
3125 if (++loops > 100000) {
3126 hammer2_spin_unex(&parent->core.spin);
3127 panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
3128 reason, parent, base, count, key_next);
3132 * NOTE: spinlock stays intact, returned chain (if not NULL)
3133 * is not referenced or locked which means that we
3134 * cannot safely check its flagged / deletion status
3137 chain = hammer2_combined_find(parent, base, count,
3138 &cache_index, &key_next,
3141 generation = parent->core.generation;
3144 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3147 * Skip keys that are not within the key/radix of the new
3148 * indirect block. They stay in the parent.
3150 if ((~(((hammer2_key_t)1 << keybits) - 1) &
3151 (key ^ bref->key)) != 0) {
3152 goto next_key_spinlocked;
3156 * Load the new indirect block by acquiring the related
3157 * chains (potentially from media as it might not be
3158 * in-memory). Then move it to the new parent (ichain)
3159 * via DELETE-DUPLICATE.
3161 * chain is referenced but not locked. We must lock the
3162 * chain to obtain definitive DUPLICATED/DELETED state
3166 * Use chain already present in the RBTREE
3168 hammer2_chain_ref(chain);
3169 hammer2_spin_unex(&parent->core.spin);
3170 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
3173 * Get chain for blockref element. _get returns NULL
3174 * on insertion race.
3177 hammer2_spin_unex(&parent->core.spin);
3178 chain = hammer2_chain_get(parent, generation, &bcopy);
3179 if (chain == NULL) {
3181 hammer2_spin_ex(&parent->core.spin);
3184 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
3185 kprintf("REASON 2\n");
3187 hammer2_chain_drop(chain);
3188 hammer2_spin_ex(&parent->core.spin);
3191 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
3195 * This is always live so if the chain has been deleted
3196 * we raced someone and we have to retry.
3198 * NOTE: Lookups can race delete-duplicate because
3199 * delete-duplicate does not lock the parent's core
3200 * (they just use the spinlock on the core). We must
3201 * check for races by comparing the DUPLICATED flag before
3202 * releasing the spinlock with the flag after locking the
3205 * (note reversed logic for this one)
3207 if (chain->flags & HAMMER2_CHAIN_DELETED) {
3208 hammer2_chain_unlock(chain);
3209 hammer2_chain_drop(chain);
3214 * Shift the chain to the indirect block.
3216 * WARNING! No reason for us to load chain data, pass NOSTATS
3217 * to prevent delete/insert from trying to access
3218 * inode stats (and thus asserting if there is no
3219 * chain->data loaded).
3221 * WARNING! The (parent, chain) deletion may modify the parent
3222 * and invalidate the base pointer.
3224 hammer2_chain_delete(parent, chain, mtid, 0);
3225 hammer2_chain_rename(NULL, &ichain, chain, mtid, 0);
3226 hammer2_chain_unlock(chain);
3227 hammer2_chain_drop(chain);
3228 KKASSERT(parent->refs > 0);
3230 base = NULL; /* safety */
3232 hammer2_spin_ex(&parent->core.spin);
3233 next_key_spinlocked:
3234 if (--maxloops == 0)
3235 panic("hammer2_chain_create_indirect: maxloops");
3237 if (key_next == 0 || key_next > key_end)
3242 hammer2_spin_unex(&parent->core.spin);
3245 * Insert the new indirect block into the parent now that we've
3246 * cleared out some entries in the parent. We calculated a good
3247 * insertion index in the loop above (ichain->index).
3249 * We don't have to set UPDATE here because we mark ichain
3250 * modified down below (so the normal modified -> flush -> set-moved
3251 * sequence applies).
3253 * The insertion shouldn't race as this is a completely new block
3254 * and the parent is locked.
3256 base = NULL; /* safety, parent modify may change address */
3257 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
3258 hammer2_chain_insert(parent, ichain,
3259 HAMMER2_CHAIN_INSERT_SPIN |
3260 HAMMER2_CHAIN_INSERT_LIVE,
3264 * Make sure flushes propogate after our manual insertion.
3266 hammer2_chain_setflush(ichain);
3267 hammer2_chain_setflush(parent);
3270 * Figure out what to return.
3272 if (~(((hammer2_key_t)1 << keybits) - 1) &
3273 (create_key ^ key)) {
3275 * Key being created is outside the key range,
3276 * return the original parent.
3278 hammer2_chain_unlock(ichain);
3279 hammer2_chain_drop(ichain);
3282 * Otherwise its in the range, return the new parent.
3283 * (leave both the new and old parent locked).
3292 * Calculate the keybits and highside/lowside of the freemap node the
3293 * caller is creating.
3295 * This routine will specify the next higher-level freemap key/radix
3296 * representing the lowest-ordered set. By doing so, eventually all
3297 * low-ordered sets will be moved one level down.
3299 * We have to be careful here because the freemap reserves a limited
3300 * number of blocks for a limited number of levels. So we can't just
3301 * push indiscriminately.
3304 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
3305 int keybits, hammer2_blockref_t *base, int count)
3307 hammer2_chain_t *chain;
3308 hammer2_blockref_t *bref;
3310 hammer2_key_t key_beg;
3311 hammer2_key_t key_end;
3312 hammer2_key_t key_next;
3316 int maxloops = 300000;
3324 * Calculate the range of keys in the array being careful to skip
3325 * slots which are overridden with a deletion.
3328 key_end = HAMMER2_KEY_MAX;
3330 hammer2_spin_ex(&parent->core.spin);
3333 if (--maxloops == 0) {
3334 panic("indkey_freemap shit %p %p:%d\n",
3335 parent, base, count);
3337 chain = hammer2_combined_find(parent, base, count,
3338 &cache_index, &key_next,
3349 * Skip deleted chains.
3351 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3352 if (key_next == 0 || key_next > key_end)
3359 * Use the full live (not deleted) element for the scan
3360 * iteration. HAMMER2 does not allow partial replacements.
3362 * XXX should be built into hammer2_combined_find().
3364 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3366 if (keybits > bref->keybits) {
3368 keybits = bref->keybits;
3369 } else if (keybits == bref->keybits && bref->key < key) {
3376 hammer2_spin_unex(&parent->core.spin);
3379 * Return the keybits for a higher-level FREEMAP_NODE covering
3383 case HAMMER2_FREEMAP_LEVEL0_RADIX:
3384 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
3386 case HAMMER2_FREEMAP_LEVEL1_RADIX:
3387 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
3389 case HAMMER2_FREEMAP_LEVEL2_RADIX:
3390 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
3392 case HAMMER2_FREEMAP_LEVEL3_RADIX:
3393 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
3395 case HAMMER2_FREEMAP_LEVEL4_RADIX:
3396 keybits = HAMMER2_FREEMAP_LEVEL5_RADIX;
3398 case HAMMER2_FREEMAP_LEVEL5_RADIX:
3399 panic("hammer2_chain_indkey_freemap: level too high");
3402 panic("hammer2_chain_indkey_freemap: bad radix");
3411 * Calculate the keybits and highside/lowside of the indirect block the
3412 * caller is creating.
3415 hammer2_chain_indkey_normal(hammer2_chain_t *parent, hammer2_key_t *keyp,
3416 int keybits, hammer2_blockref_t *base, int count)
3418 hammer2_blockref_t *bref;
3419 hammer2_chain_t *chain;
3420 hammer2_key_t key_beg;
3421 hammer2_key_t key_end;
3422 hammer2_key_t key_next;
3428 int maxloops = 300000;
3435 * Calculate the range of keys in the array being careful to skip
3436 * slots which are overridden with a deletion. Once the scan
3437 * completes we will cut the key range in half and shift half the
3438 * range into the new indirect block.
3441 key_end = HAMMER2_KEY_MAX;
3443 hammer2_spin_ex(&parent->core.spin);
3446 if (--maxloops == 0) {
3447 panic("indkey_freemap shit %p %p:%d\n",
3448 parent, base, count);
3450 chain = hammer2_combined_find(parent, base, count,
3451 &cache_index, &key_next,
3462 * NOTE: No need to check DUPLICATED here because we do
3463 * not release the spinlock.
3465 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3466 if (key_next == 0 || key_next > key_end)
3473 * Use the full live (not deleted) element for the scan
3474 * iteration. HAMMER2 does not allow partial replacements.
3476 * XXX should be built into hammer2_combined_find().
3478 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3481 * Expand our calculated key range (key, keybits) to fit
3482 * the scanned key. nkeybits represents the full range
3483 * that we will later cut in half (two halves @ nkeybits - 1).
3486 if (nkeybits < bref->keybits) {
3487 if (bref->keybits > 64) {
3488 kprintf("bad bref chain %p bref %p\n",
3492 nkeybits = bref->keybits;
3494 while (nkeybits < 64 &&
3495 (~(((hammer2_key_t)1 << nkeybits) - 1) &
3496 (key ^ bref->key)) != 0) {
3501 * If the new key range is larger we have to determine
3502 * which side of the new key range the existing keys fall
3503 * under by checking the high bit, then collapsing the
3504 * locount into the hicount or vise-versa.
3506 if (keybits != nkeybits) {
3507 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
3518 * The newly scanned key will be in the lower half or the
3519 * upper half of the (new) key range.
3521 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
3530 hammer2_spin_unex(&parent->core.spin);
3531 bref = NULL; /* now invalid (safety) */
3534 * Adjust keybits to represent half of the full range calculated
3535 * above (radix 63 max)
3540 * Select whichever half contains the most elements. Theoretically
3541 * we can select either side as long as it contains at least one
3542 * element (in order to ensure that a free slot is present to hold
3543 * the indirect block).
3545 if (hammer2_indirect_optimize) {
3547 * Insert node for least number of keys, this will arrange
3548 * the first few blocks of a large file or the first few
3549 * inodes in a directory with fewer indirect blocks when
3552 if (hicount < locount && hicount != 0)
3553 key |= (hammer2_key_t)1 << keybits;
3555 key &= ~(hammer2_key_t)1 << keybits;
3558 * Insert node for most number of keys, best for heavily
3561 if (hicount > locount)
3562 key |= (hammer2_key_t)1 << keybits;
3564 key &= ~(hammer2_key_t)1 << keybits;
3572 * Sets CHAIN_DELETED and remove the chain's blockref from the parent if
3575 * Both parent and chain must be locked exclusively.
3577 * This function will modify the parent if the blockref requires removal
3578 * from the parent's block table.
3580 * This function is NOT recursive. Any entity already pushed into the
3581 * chain (such as an inode) may still need visibility into its contents,
3582 * as well as the ability to read and modify the contents. For example,
3583 * for an unlinked file which is still open.
3586 hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
3587 hammer2_tid_t mtid, int flags)
3589 KKASSERT(hammer2_mtx_owned(&chain->lock));
3592 * Nothing to do if already marked.
3594 * We need the spinlock on the core whos RBTREE contains chain
3595 * to protect against races.
3597 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
3598 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
3599 chain->parent == parent);
3600 _hammer2_chain_delete_helper(parent, chain, mtid, flags);
3604 * Permanent deletions mark the chain as destroyed. H
3606 if (flags & HAMMER2_DELETE_PERMANENT) {
3607 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
3609 /* XXX might not be needed */
3610 hammer2_chain_setflush(chain);
3615 * Returns the index of the nearest element in the blockref array >= elm.
3616 * Returns (count) if no element could be found.
3618 * Sets *key_nextp to the next key for loop purposes but does not modify
3619 * it if the next key would be higher than the current value of *key_nextp.
3620 * Note that *key_nexp can overflow to 0, which should be tested by the
3623 * (*cache_indexp) is a heuristic and can be any value without effecting
3626 * WARNING! Must be called with parent's spinlock held. Spinlock remains
3627 * held through the operation.
3630 hammer2_base_find(hammer2_chain_t *parent,
3631 hammer2_blockref_t *base, int count,
3632 int *cache_indexp, hammer2_key_t *key_nextp,
3633 hammer2_key_t key_beg, hammer2_key_t key_end)
3635 hammer2_blockref_t *scan;
3636 hammer2_key_t scan_end;
3641 * Require the live chain's already have their core's counted
3642 * so we can optimize operations.
3644 KKASSERT(parent->flags & HAMMER2_CHAIN_COUNTEDBREFS);
3649 if (count == 0 || base == NULL)
3653 * Sequential optimization using *cache_indexp. This is the most
3656 * We can avoid trailing empty entries on live chains, otherwise
3657 * we might have to check the whole block array.
3661 limit = parent->core.live_zero;
3666 KKASSERT(i < count);
3672 while (i > 0 && (scan->type == 0 || scan->key > key_beg)) {
3679 * Search forwards, stop when we find a scan element which
3680 * encloses the key or until we know that there are no further
3684 if (scan->type != 0) {
3685 scan_end = scan->key +
3686 ((hammer2_key_t)1 << scan->keybits) - 1;
3687 if (scan->key > key_beg || scan_end >= key_beg)
3700 scan_end = scan->key +
3701 ((hammer2_key_t)1 << scan->keybits);
3702 if (scan_end && (*key_nextp > scan_end ||
3704 *key_nextp = scan_end;
3712 * Do a combined search and return the next match either from the blockref
3713 * array or from the in-memory chain. Sets *bresp to the returned bref in
3714 * both cases, or sets it to NULL if the search exhausted. Only returns
3715 * a non-NULL chain if the search matched from the in-memory chain.
3717 * When no in-memory chain has been found and a non-NULL bref is returned
3721 * The returned chain is not locked or referenced. Use the returned bref
3722 * to determine if the search exhausted or not. Iterate if the base find
3723 * is chosen but matches a deleted chain.
3725 * WARNING! Must be called with parent's spinlock held. Spinlock remains
3726 * held through the operation.
3728 static hammer2_chain_t *
3729 hammer2_combined_find(hammer2_chain_t *parent,
3730 hammer2_blockref_t *base, int count,
3731 int *cache_indexp, hammer2_key_t *key_nextp,
3732 hammer2_key_t key_beg, hammer2_key_t key_end,
3733 hammer2_blockref_t **bresp)
3735 hammer2_blockref_t *bref;
3736 hammer2_chain_t *chain;
3740 * Lookup in block array and in rbtree.
3742 *key_nextp = key_end + 1;
3743 i = hammer2_base_find(parent, base, count, cache_indexp,
3744 key_nextp, key_beg, key_end);
3745 chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
3750 if (i == count && chain == NULL) {
3756 * Only chain matched.
3759 bref = &chain->bref;
3764 * Only blockref matched.
3766 if (chain == NULL) {
3772 * Both in-memory and blockref matched, select the nearer element.
3774 * If both are flush with the left-hand side or both are the
3775 * same distance away, select the chain. In this situation the
3776 * chain must have been loaded from the matching blockmap.
3778 if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
3779 chain->bref.key == base[i].key) {
3780 KKASSERT(chain->bref.key == base[i].key);
3781 bref = &chain->bref;
3786 * Select the nearer key
3788 if (chain->bref.key < base[i].key) {
3789 bref = &chain->bref;
3796 * If the bref is out of bounds we've exhausted our search.
3799 if (bref->key > key_end) {
3809 * Locate the specified block array element and delete it. The element
3812 * The spin lock on the related chain must be held.
3814 * NOTE: live_count was adjusted when the chain was deleted, so it does not
3815 * need to be adjusted when we commit the media change.
3818 hammer2_base_delete(hammer2_chain_t *parent,
3819 hammer2_blockref_t *base, int count,
3820 int *cache_indexp, hammer2_chain_t *chain)
3822 hammer2_blockref_t *elm = &chain->bref;
3823 hammer2_key_t key_next;
3827 * Delete element. Expect the element to exist.
3829 * XXX see caller, flush code not yet sophisticated enough to prevent
3830 * re-flushed in some cases.
3832 key_next = 0; /* max range */
3833 i = hammer2_base_find(parent, base, count, cache_indexp,
3834 &key_next, elm->key, elm->key);
3835 if (i == count || base[i].type == 0 ||
3836 base[i].key != elm->key ||
3837 ((chain->flags & HAMMER2_CHAIN_BMAPUPD) == 0 &&
3838 base[i].keybits != elm->keybits)) {
3839 hammer2_spin_unex(&parent->core.spin);
3840 panic("delete base %p element not found at %d/%d elm %p\n",
3841 base, i, count, elm);
3846 * Update stats and zero the entry
3848 parent->bref.data_count -= base[i].data_count;
3849 parent->bref.data_count -= (hammer2_off_t)1 <<
3850 (int)(base[i].data_off & HAMMER2_OFF_MASK_RADIX);
3851 parent->bref.inode_count -= base[i].inode_count;
3852 if (base[i].type == HAMMER2_BREF_TYPE_INODE)
3853 parent->bref.inode_count -= 1;
3855 bzero(&base[i], sizeof(*base));
3858 * We can only optimize parent->core.live_zero for live chains.
3860 if (parent->core.live_zero == i + 1) {
3861 while (--i >= 0 && base[i].type == 0)
3863 parent->core.live_zero = i + 1;
3867 * Clear appropriate blockmap flags in chain.
3869 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED |
3870 HAMMER2_CHAIN_BMAPUPD);
3874 * Insert the specified element. The block array must not already have the
3875 * element and must have space available for the insertion.
3877 * The spin lock on the related chain must be held.
3879 * NOTE: live_count was adjusted when the chain was deleted, so it does not
3880 * need to be adjusted when we commit the media change.
3883 hammer2_base_insert(hammer2_chain_t *parent,
3884 hammer2_blockref_t *base, int count,
3885 int *cache_indexp, hammer2_chain_t *chain)
3887 hammer2_blockref_t *elm = &chain->bref;
3888 hammer2_key_t key_next;
3897 * Insert new element. Expect the element to not already exist
3898 * unless we are replacing it.
3900 * XXX see caller, flush code not yet sophisticated enough to prevent
3901 * re-flushed in some cases.
3903 key_next = 0; /* max range */
3904 i = hammer2_base_find(parent, base, count, cache_indexp,
3905 &key_next, elm->key, elm->key);
3908 * Shortcut fill optimization, typical ordered insertion(s) may not
3911 KKASSERT(i >= 0 && i <= count);
3914 * Set appropriate blockmap flags in chain.
3916 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
3919 * Update stats and zero the entry
3921 parent->bref.data_count += elm->data_count;
3922 parent->bref.data_count += (hammer2_off_t)1 <<
3923 (int)(elm->data_off & HAMMER2_OFF_MASK_RADIX);
3924 parent->bref.inode_count += elm->inode_count;
3925 if (elm->type == HAMMER2_BREF_TYPE_INODE)
3926 parent->bref.inode_count += 1;
3930 * We can only optimize parent->core.live_zero for live chains.
3932 if (i == count && parent->core.live_zero < count) {
3933 i = parent->core.live_zero++;
3938 xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
3939 if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
3940 hammer2_spin_unex(&parent->core.spin);
3941 panic("insert base %p overlapping elements at %d elm %p\n",
3946 * Try to find an empty slot before or after.
3950 while (j > 0 || k < count) {
3952 if (j >= 0 && base[j].type == 0) {
3956 bcopy(&base[j+1], &base[j],
3957 (i - j - 1) * sizeof(*base));
3963 if (k < count && base[k].type == 0) {
3964 bcopy(&base[i], &base[i+1],
3965 (k - i) * sizeof(hammer2_blockref_t));
3969 * We can only update parent->core.live_zero for live
3972 if (parent->core.live_zero <= k)
3973 parent->core.live_zero = k + 1;
3978 panic("hammer2_base_insert: no room!");
3985 for (l = 0; l < count; ++l) {
3987 key_next = base[l].key +
3988 ((hammer2_key_t)1 << base[l].keybits) - 1;
3992 while (++l < count) {
3994 if (base[l].key <= key_next)
3995 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
3996 key_next = base[l].key +
3997 ((hammer2_key_t)1 << base[l].keybits) - 1;
4007 * Sort the blockref array for the chain. Used by the flush code to
4008 * sort the blockref[] array.
4010 * The chain must be exclusively locked AND spin-locked.
4012 typedef hammer2_blockref_t *hammer2_blockref_p;
4016 hammer2_base_sort_callback(const void *v1, const void *v2)
4018 hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
4019 hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
4022 * Make sure empty elements are placed at the end of the array
4024 if (bref1->type == 0) {
4025 if (bref2->type == 0)
4028 } else if (bref2->type == 0) {
4035 if (bref1->key < bref2->key)
4037 if (bref1->key > bref2->key)
4043 hammer2_base_sort(hammer2_chain_t *chain)
4045 hammer2_blockref_t *base;
4048 switch(chain->bref.type) {
4049 case HAMMER2_BREF_TYPE_INODE:
4051 * Special shortcut for embedded data returns the inode
4052 * itself. Callers must detect this condition and access
4053 * the embedded data (the strategy code does this for us).
4055 * This is only applicable to regular files and softlinks.
4057 if (chain->data->ipdata.meta.op_flags &
4058 HAMMER2_OPFLAG_DIRECTDATA) {
4061 base = &chain->data->ipdata.u.blockset.blockref[0];
4062 count = HAMMER2_SET_COUNT;
4064 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
4065 case HAMMER2_BREF_TYPE_INDIRECT:
4067 * Optimize indirect blocks in the INITIAL state to avoid
4070 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
4071 base = &chain->data->npdata[0];
4072 count = chain->bytes / sizeof(hammer2_blockref_t);
4074 case HAMMER2_BREF_TYPE_VOLUME:
4075 base = &chain->data->voldata.sroot_blockset.blockref[0];
4076 count = HAMMER2_SET_COUNT;
4078 case HAMMER2_BREF_TYPE_FREEMAP:
4079 base = &chain->data->blkset.blockref[0];
4080 count = HAMMER2_SET_COUNT;
4083 kprintf("hammer2_chain_lookup: unrecognized "
4084 "blockref(A) type: %d",
4087 tsleep(&base, 0, "dead", 0);
4088 panic("hammer2_chain_lookup: unrecognized "
4089 "blockref(A) type: %d",
4091 base = NULL; /* safety */
4092 count = 0; /* safety */
4094 kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
4100 * Chain memory management
4103 hammer2_chain_wait(hammer2_chain_t *chain)
4105 tsleep(chain, 0, "chnflw", 1);
4108 const hammer2_media_data_t *
4109 hammer2_chain_rdata(hammer2_chain_t *chain)
4111 KKASSERT(chain->data != NULL);
4112 return (chain->data);
4115 hammer2_media_data_t *
4116 hammer2_chain_wdata(hammer2_chain_t *chain)
4118 KKASSERT(chain->data != NULL);
4119 return (chain->data);
4123 * Set the check data for a chain. This can be a heavy-weight operation
4124 * and typically only runs on-flush. For file data check data is calculated
4125 * when the logical buffers are flushed.
4128 hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata)
4130 chain->bref.flags &= ~HAMMER2_BREF_FLAG_ZERO;
4132 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
4133 case HAMMER2_CHECK_NONE:
4135 case HAMMER2_CHECK_DISABLED:
4137 case HAMMER2_CHECK_ISCSI32:
4138 chain->bref.check.iscsi32.value =
4139 hammer2_icrc32(bdata, chain->bytes);
4141 case HAMMER2_CHECK_CRC64:
4142 chain->bref.check.crc64.value = 0;
4145 case HAMMER2_CHECK_SHA192:
4147 SHA256_CTX hash_ctx;
4149 uint8_t digest[SHA256_DIGEST_LENGTH];
4150 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
4153 SHA256_Init(&hash_ctx);
4154 SHA256_Update(&hash_ctx, bdata, chain->bytes);
4155 SHA256_Final(u.digest, &hash_ctx);
4156 u.digest64[2] ^= u.digest64[3];
4158 chain->bref.check.sha192.data,
4159 sizeof(chain->bref.check.sha192.data));
4162 case HAMMER2_CHECK_FREEMAP:
4163 chain->bref.check.freemap.icrc32 =
4164 hammer2_icrc32(bdata, chain->bytes);
4167 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
4168 chain->bref.methods);
4174 hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata)
4178 if (chain->bref.flags & HAMMER2_BREF_FLAG_ZERO)
4181 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
4182 case HAMMER2_CHECK_NONE:
4185 case HAMMER2_CHECK_DISABLED:
4188 case HAMMER2_CHECK_ISCSI32:
4189 r = (chain->bref.check.iscsi32.value ==
4190 hammer2_icrc32(bdata, chain->bytes));
4192 case HAMMER2_CHECK_CRC64:
4193 r = (chain->bref.check.crc64.value == 0);
4196 case HAMMER2_CHECK_SHA192:
4198 SHA256_CTX hash_ctx;
4200 uint8_t digest[SHA256_DIGEST_LENGTH];
4201 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
4204 SHA256_Init(&hash_ctx);
4205 SHA256_Update(&hash_ctx, bdata, chain->bytes);
4206 SHA256_Final(u.digest, &hash_ctx);
4207 u.digest64[2] ^= u.digest64[3];
4209 chain->bref.check.sha192.data,
4210 sizeof(chain->bref.check.sha192.data)) == 0) {
4217 case HAMMER2_CHECK_FREEMAP:
4218 r = (chain->bref.check.freemap.icrc32 ==
4219 hammer2_icrc32(bdata, chain->bytes));
4221 kprintf("freemap.icrc %08x icrc32 %08x (%d)\n",
4222 chain->bref.check.freemap.icrc32,
4223 hammer2_icrc32(bdata, chain->bytes), chain->bytes);
4225 kprintf("dio %p buf %016jx,%d bdata %p/%p\n",
4226 chain->dio, chain->dio->bp->b_loffset, chain->dio->bp->b_bufsize, bdata, chain->dio->bp->b_data);
4231 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
4232 chain->bref.methods);
4240 * The caller presents a shared-locked (parent, chain) where the chain
4241 * is of type HAMMER2_OBJTYPE_HARDLINK. The caller must hold the ip
4242 * structure representing the inode locked to prevent
4243 * consolidation/deconsolidation races.
4245 * The flags passed in are LOOKUP flags, not RESOLVE flags. Only
4246 * HAMMER2_LOOKUP_SHARED is supported.
4248 * We locate the hardlink in the current or a common parent directory.
4250 * If we are unable to locate the hardlink, EIO is returned and
4251 * (*chainp) is unlocked and dropped.
4254 hammer2_chain_hardlink_find(hammer2_inode_t *dip,
4255 hammer2_chain_t **parentp,
4256 hammer2_chain_t **chainp,
4259 hammer2_chain_t *parent;
4260 hammer2_chain_t *rchain;
4261 hammer2_key_t key_dummy;
4263 int cache_index = -1;
4266 resolve_flags = (flags & HAMMER2_LOOKUP_SHARED) ?
4267 HAMMER2_RESOLVE_SHARED : 0;
4270 * Obtain the key for the hardlink from *chainp.
4273 lhc = rchain->data->ipdata.meta.inum;
4274 hammer2_chain_unlock(rchain);
4275 hammer2_chain_drop(rchain);
4280 rchain = hammer2_chain_lookup(parentp, &key_dummy,
4282 &cache_index, flags);
4287 * Iterate parents, handle parent rename races by retrying
4295 parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
4298 if (parent->bref.flags & HAMMER2_BREF_FLAG_PFSROOT)
4300 if (parent->parent == NULL)
4302 parent = parent->parent;
4303 hammer2_chain_ref(parent);
4304 hammer2_chain_unlock(*parentp);
4305 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
4307 if ((*parentp)->parent == parent) {
4308 hammer2_chain_drop(*parentp);
4311 hammer2_chain_unlock(parent);
4312 hammer2_chain_drop(parent);
4313 hammer2_chain_lock(*parentp,
4314 HAMMER2_RESOLVE_ALWAYS |
4316 parent = NULL; /* safety */
4324 return (rchain ? EINVAL : 0);
4328 * Used by the bulkscan code to snapshot the synchronized storage for
4329 * a volume, allowing it to be scanned concurrently against normal
4333 hammer2_chain_bulksnap(hammer2_chain_t *chain)
4335 hammer2_chain_t *copy;
4337 copy = hammer2_chain_alloc(chain->hmp, chain->pmp, &chain->bref);
4338 switch(chain->bref.type) {
4339 case HAMMER2_BREF_TYPE_VOLUME:
4340 copy->data = kmalloc(sizeof(copy->data->voldata),
4343 hammer2_spin_ex(&chain->core.spin);
4344 copy->data->voldata = chain->data->voldata;
4345 hammer2_spin_unex(&chain->core.spin);
4347 case HAMMER2_BREF_TYPE_FREEMAP:
4348 copy->data = kmalloc(sizeof(hammer2_blockset_t),
4351 hammer2_spin_ex(&chain->core.spin);
4352 copy->data->blkset = chain->data->blkset;
4353 hammer2_spin_unex(&chain->core.spin);
4362 hammer2_chain_bulkdrop(hammer2_chain_t *copy)
4364 switch(copy->bref.type) {
4365 case HAMMER2_BREF_TYPE_VOLUME:
4366 case HAMMER2_BREF_TYPE_FREEMAP:
4367 KKASSERT(copy->data);
4368 kfree(copy->data, copy->hmp->mchain);
4373 hammer2_chain_drop(copy);
4377 * Create a snapshot of the specified {parent, ochain} with the specified
4378 * label. The originating hammer2_inode must be exclusively locked for
4381 * The ioctl code has already synced the filesystem.
4384 hammer2_chain_snapshot(hammer2_chain_t *chain, hammer2_ioc_pfs_t *pmp,
4388 const hammer2_inode_data_t *ripdata;
4389 hammer2_inode_data_t *wipdata;
4390 hammer2_chain_t *nchain;
4391 hammer2_inode_t *nip;
4400 kprintf("snapshot %s\n", pmp->name);
4402 name_len = strlen(pmp->name);
4403 lhc = hammer2_dirhash(pmp->name, name_len);
4408 ripdata = &chain->data->ipdata;
4410 opfs_clid = ripdata->meta.pfs_clid;
4415 * Create the snapshot directory under the super-root
4417 * Set PFS type, generate a unique filesystem id, and generate
4418 * a cluster id. Use the same clid when snapshotting a PFS root,
4419 * which theoretically allows the snapshot to be used as part of
4420 * the same cluster (perhaps as a cache).
4422 * Copy the (flushed) blockref array. Theoretically we could use
4423 * chain_duplicate() but it becomes difficult to disentangle
4424 * the shared core so for now just brute-force it.
4429 nip = hammer2_inode_create(hmp->spmp->iroot, &vat, proc0.p_ucred,
4430 pmp->name, name_len, 0,
4432 HAMMER2_INSERT_PFSROOT, &error);
4435 hammer2_inode_modify(nip);
4436 nchain = hammer2_inode_chain(nip, 0, HAMMER2_RESOLVE_ALWAYS);
4437 hammer2_chain_modify(nchain, mtid, 0, 0);
4438 wipdata = &nchain->data->ipdata;
4440 nip->meta.pfs_type = HAMMER2_PFSTYPE_MASTER;
4441 nip->meta.pfs_subtype = HAMMER2_PFSSUBTYPE_SNAPSHOT;
4442 nip->meta.op_flags |= HAMMER2_OPFLAG_PFSROOT;
4443 kern_uuidgen(&nip->meta.pfs_fsid, 1);
4446 * Give the snapshot its own private cluster id. As a
4447 * snapshot no further synchronization with the original
4448 * cluster will be done.
4451 if (chain->flags & HAMMER2_CHAIN_PFSBOUNDARY)
4452 nip->meta.pfs_clid = opfs_clid;
4454 kern_uuidgen(&nip->meta.pfs_clid, 1);
4456 kern_uuidgen(&nip->meta.pfs_clid, 1);
4457 nchain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
4459 /* XXX hack blockset copy */
4460 /* XXX doesn't work with real cluster */
4461 wipdata->meta = nip->meta;
4462 wipdata->u.blockset = ripdata->u.blockset;
4463 hammer2_flush(nchain, 1);
4464 hammer2_chain_unlock(nchain);
4465 hammer2_chain_drop(nchain);
4466 hammer2_inode_unlock(nip);