2 * Copyright (c) 2011-2014 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * and Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This subsystem implements most of the core support functions for
37 * the hammer2_chain structure.
39 * Chains are the in-memory version on media objects (volume header, inodes,
40 * indirect blocks, data blocks, etc). Chains represent a portion of the
43 * Chains are no-longer delete-duplicated. Instead, the original in-memory
44 * chain will be moved along with its block reference (e.g. for things like
45 * renames, hardlink operations, modifications, etc), and will be indexed
46 * on a secondary list for flush handling instead of propagating a flag
49 * Concurrent front-end operations can still run against backend flushes
50 * as long as they do not cross the current flush boundary. An operation
51 * running above the current flush (in areas not yet flushed) can become
52 * part of the current flush while ano peration running below the current
53 * flush can become part of the next flush.
55 #include <sys/cdefs.h>
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/types.h>
60 #include <sys/kern_syscall.h>
63 #include <crypto/sha2/sha2.h>
67 static int hammer2_indirect_optimize; /* XXX SYSCTL */
69 static hammer2_chain_t *hammer2_chain_create_indirect(
70 hammer2_chain_t *parent,
71 hammer2_key_t key, int keybits,
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 panic("hammer2_chain_alloc volume type illegal for op");
197 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
202 * Initialize the new chain structure. pmp must be set to NULL for
203 * chains belonging to the super-root topology of a device mount.
205 if (pmp == hmp->spmp)
211 chain->bytes = bytes;
213 chain->flags = HAMMER2_CHAIN_ALLOCATED;
216 * Set the PFS boundary flag if this chain represents a PFS root.
218 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
219 chain->flags |= HAMMER2_CHAIN_PFSBOUNDARY;
220 hammer2_chain_core_init(chain);
226 * Initialize a chain's core structure. This structure used to be allocated
227 * but is now embedded.
229 * The core is not locked. No additional refs on the chain are made.
230 * (trans) must not be NULL if (core) is not NULL.
233 hammer2_chain_core_init(hammer2_chain_t *chain)
236 * Fresh core under nchain (no multi-homing of ochain's
239 RB_INIT(&chain->core.rbtree); /* live chains */
240 hammer2_mtx_init(&chain->lock, "h2chain");
244 * Add a reference to a chain element, preventing its destruction.
246 * (can be called with spinlock held)
249 hammer2_chain_ref(hammer2_chain_t *chain)
251 atomic_add_int(&chain->refs, 1);
253 kprintf("REFC %p %d %08x\n", chain, chain->refs - 1, chain->flags);
259 * Insert the chain in the core rbtree.
261 * Normal insertions are placed in the live rbtree. Insertion of a deleted
262 * chain is a special case used by the flush code that is placed on the
263 * unstaged deleted list to avoid confusing the live view.
265 #define HAMMER2_CHAIN_INSERT_SPIN 0x0001
266 #define HAMMER2_CHAIN_INSERT_LIVE 0x0002
267 #define HAMMER2_CHAIN_INSERT_RACE 0x0004
271 hammer2_chain_insert(hammer2_chain_t *parent, hammer2_chain_t *chain,
272 int flags, int generation)
274 hammer2_chain_t *xchain;
277 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
278 hammer2_spin_ex(&parent->core.spin);
281 * Interlocked by spinlock, check for race
283 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
284 parent->core.generation != generation) {
292 xchain = RB_INSERT(hammer2_chain_tree, &parent->core.rbtree, chain);
293 KASSERT(xchain == NULL,
294 ("hammer2_chain_insert: collision %p %p", chain, xchain));
295 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
296 chain->parent = parent;
297 ++parent->core.chain_count;
298 ++parent->core.generation; /* XXX incs for _get() too, XXX */
301 * We have to keep track of the effective live-view blockref count
302 * so the create code knows when to push an indirect block.
304 if (flags & HAMMER2_CHAIN_INSERT_LIVE)
305 atomic_add_int(&parent->core.live_count, 1);
307 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
308 hammer2_spin_unex(&parent->core.spin);
313 * Drop the caller's reference to the chain. When the ref count drops to
314 * zero this function will try to disassociate the chain from its parent and
315 * deallocate it, then recursely drop the parent using the implied ref
316 * from the chain's chain->parent.
318 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain);
321 hammer2_chain_drop(hammer2_chain_t *chain)
326 if (hammer2_debug & 0x200000)
329 kprintf("DROP %p %d %08x\n", chain, chain->refs - 1, chain->flags);
333 if (chain->flags & HAMMER2_CHAIN_UPDATE)
335 if (chain->flags & HAMMER2_CHAIN_MODIFIED)
337 KKASSERT(chain->refs > need);
345 chain = hammer2_chain_lastdrop(chain);
347 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
349 /* retry the same chain */
355 * Safe handling of the 1->0 transition on chain. Returns a chain for
356 * recursive drop or NULL, possibly returning the same chain if the atomic
359 * Whem two chains need to be recursively dropped we use the chain
360 * we would otherwise free to placehold the additional chain. It's a bit
361 * convoluted but we can't just recurse without potentially blowing out
364 * The chain cannot be freed if it has any children.
366 * The core spinlock is allowed nest child-to-parent (not parent-to-child).
370 hammer2_chain_lastdrop(hammer2_chain_t *chain)
374 hammer2_chain_t *parent;
375 hammer2_chain_t *rdrop;
378 * Spinlock the core and check to see if it is empty. If it is
379 * not empty we leave chain intact with refs == 0. The elements
380 * in core->rbtree are associated with other chains contemporary
381 * with ours but not with our chain directly.
383 hammer2_spin_ex(&chain->core.spin);
386 * We can't free non-stale chains with children until we are
387 * able to free the children because there might be a flush
388 * dependency. Flushes of stale children (which should also
389 * have their deleted flag set) short-cut recursive flush
390 * dependencies and can be freed here. Any flushes which run
391 * through stale children due to the flush synchronization
392 * point should have a FLUSH_* bit set in the chain and not
393 * reach lastdrop at this time.
395 * NOTE: We return (chain) on failure to retry.
397 if (chain->core.chain_count) {
398 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
399 hammer2_spin_unex(&chain->core.spin);
400 chain = NULL; /* success */
402 hammer2_spin_unex(&chain->core.spin);
406 /* no chains left under us */
409 * chain->core has no children left so no accessors can get to our
410 * chain from there. Now we have to lock the parent core to interlock
411 * remaining possible accessors that might bump chain's refs before
412 * we can safely drop chain's refs with intent to free the chain.
415 pmp = chain->pmp; /* can be NULL */
419 * Spinlock the parent and try to drop the last ref on chain.
420 * On success remove chain from its parent, otherwise return NULL.
422 * (normal core locks are top-down recursive but we define core
423 * spinlocks as bottom-up recursive, so this is safe).
425 if ((parent = chain->parent) != NULL) {
426 hammer2_spin_ex(&parent->core.spin);
427 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
428 /* 1->0 transition failed */
429 hammer2_spin_unex(&parent->core.spin);
430 hammer2_spin_unex(&chain->core.spin);
431 return(chain); /* retry */
435 * 1->0 transition successful, remove chain from its
438 if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
439 RB_REMOVE(hammer2_chain_tree,
440 &parent->core.rbtree, chain);
441 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
442 --parent->core.chain_count;
443 chain->parent = NULL;
447 * If our chain was the last chain in the parent's core the
448 * core is now empty and its parent might have to be
449 * re-dropped if it has 0 refs.
451 if (parent->core.chain_count == 0) {
453 if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0) {
457 hammer2_spin_unex(&parent->core.spin);
458 parent = NULL; /* safety */
462 * Successful 1->0 transition and the chain can be destroyed now.
464 * We still have the core spinlock, and core's chain_count is 0.
465 * Any parent spinlock is gone.
467 hammer2_spin_unex(&chain->core.spin);
468 KKASSERT(RB_EMPTY(&chain->core.rbtree) &&
469 chain->core.chain_count == 0);
472 * All spin locks are gone, finish freeing stuff.
474 KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE |
475 HAMMER2_CHAIN_MODIFIED)) == 0);
476 hammer2_chain_drop_data(chain, 1);
478 KKASSERT(chain->dio == NULL);
481 * Once chain resources are gone we can use the now dead chain
482 * structure to placehold what might otherwise require a recursive
483 * drop, because we have potentially two things to drop and can only
484 * return one directly.
486 if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
487 chain->flags &= ~HAMMER2_CHAIN_ALLOCATED;
489 kfree(chain, hmp->mchain);
493 * Possible chaining loop when parent re-drop needed.
499 * On either last lock release or last drop
502 hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop)
504 /*hammer2_dev_t *hmp = chain->hmp;*/
506 switch(chain->bref.type) {
507 case HAMMER2_BREF_TYPE_VOLUME:
508 case HAMMER2_BREF_TYPE_FREEMAP:
513 KKASSERT(chain->data == NULL);
519 * Lock a referenced chain element, acquiring its data with I/O if necessary,
520 * and specify how you would like the data to be resolved.
522 * If an I/O or other fatal error occurs, chain->error will be set to non-zero.
524 * The lock is allowed to recurse, multiple locking ops will aggregate
525 * the requested resolve types. Once data is assigned it will not be
526 * removed until the last unlock.
528 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
529 * (typically used to avoid device/logical buffer
532 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
533 * the INITIAL-create state (indirect blocks only).
535 * Do not resolve data elements for DATA chains.
536 * (typically used to avoid device/logical buffer
539 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
541 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
542 * it will be locked exclusive.
544 * NOTE: Embedded elements (volume header, inodes) are always resolved
547 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
548 * element will instantiate and zero its buffer, and flush it on
551 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
552 * so as not to instantiate a device buffer, which could alias against
553 * a logical file buffer. However, if ALWAYS is specified the
554 * device buffer will be instantiated anyway.
556 * WARNING! This function blocks on I/O if data needs to be fetched. This
557 * blocking can run concurrent with other compatible lock holders
558 * who do not need data returning. The lock is not upgraded to
559 * exclusive during a data fetch, a separate bit is used to
560 * interlock I/O. However, an exclusive lock holder can still count
561 * on being interlocked against an I/O fetch managed by a shared
565 hammer2_chain_lock(hammer2_chain_t *chain, int how)
568 * Ref and lock the element. Recursive locks are allowed.
570 KKASSERT(chain->refs > 0);
571 atomic_add_int(&chain->lockcnt, 1);
574 * Get the appropriate lock.
576 if (how & HAMMER2_RESOLVE_SHARED)
577 hammer2_mtx_sh(&chain->lock);
579 hammer2_mtx_ex(&chain->lock);
582 * If we already have a valid data pointer no further action is
589 * Do we have to resolve the data?
591 switch(how & HAMMER2_RESOLVE_MASK) {
592 case HAMMER2_RESOLVE_NEVER:
594 case HAMMER2_RESOLVE_MAYBE:
595 if (chain->flags & HAMMER2_CHAIN_INITIAL)
597 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
600 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
602 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
606 case HAMMER2_RESOLVE_ALWAYS:
612 * Caller requires data
614 hammer2_chain_load_data(chain);
618 * Issue I/O and install chain->data. Caller must hold a chain lock, lock
619 * may be of any type.
621 * Once chain->data is set it cannot be disposed of until all locks are
625 hammer2_chain_load_data(hammer2_chain_t *chain)
627 hammer2_blockref_t *bref;
633 * Degenerate case, data already present.
639 KKASSERT(hmp != NULL);
642 * Gain the IOINPROG bit, interlocked block.
648 oflags = chain->flags;
650 if (oflags & HAMMER2_CHAIN_IOINPROG) {
651 nflags = oflags | HAMMER2_CHAIN_IOSIGNAL;
652 tsleep_interlock(&chain->flags, 0);
653 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
654 tsleep(&chain->flags, PINTERLOCKED,
659 nflags = oflags | HAMMER2_CHAIN_IOINPROG;
660 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
668 * We own CHAIN_IOINPROG
670 * Degenerate case if we raced another load.
676 * We must resolve to a device buffer, either by issuing I/O or
677 * by creating a zero-fill element. We do not mark the buffer
678 * dirty when creating a zero-fill element (the hammer2_chain_modify()
679 * API must still be used to do that).
681 * The device buffer is variable-sized in powers of 2 down
682 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
683 * chunk always contains buffers of the same size. (XXX)
685 * The minimum physical IO size may be larger than the variable
691 * The getblk() optimization can only be used on newly created
692 * elements if the physical block size matches the request.
694 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
695 error = hammer2_io_new(hmp, bref->data_off, chain->bytes,
698 error = hammer2_io_bread(hmp, bref->data_off, chain->bytes,
700 hammer2_adjreadcounter(&chain->bref, chain->bytes);
703 chain->error = HAMMER2_ERROR_IO;
704 kprintf("hammer2_chain_lock: I/O error %016jx: %d\n",
705 (intmax_t)bref->data_off, error);
706 hammer2_io_bqrelse(&chain->dio);
712 * NOTE: A locked chain's data cannot be modified without first
713 * calling hammer2_chain_modify().
717 * Clear INITIAL. In this case we used io_new() and the buffer has
718 * been zero'd and marked dirty.
720 bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
721 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
722 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
723 chain->bref.flags |= HAMMER2_BREF_FLAG_ZERO;
724 } else if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
726 * check data not currently synchronized due to
727 * modification. XXX assumes data stays in the buffer
728 * cache, which might not be true (need biodep on flush
729 * to calculate crc? or simple crc?).
732 if (hammer2_chain_testcheck(chain, bdata) == 0) {
733 kprintf("chain %016jx.%02x meth=%02x "
734 "CHECK FAIL %08x (flags=%08x)\n",
735 chain->bref.data_off,
738 hammer2_icrc32(bdata, chain->bytes),
740 chain->error = HAMMER2_ERROR_CHECK;
745 * Setup the data pointer, either pointing it to an embedded data
746 * structure and copying the data from the buffer, or pointing it
749 * The buffer is not retained when copying to an embedded data
750 * structure in order to avoid potential deadlocks or recursions
751 * on the same physical buffer.
753 * WARNING! Other threads can start using the data the instant we
754 * set chain->data non-NULL.
756 switch (bref->type) {
757 case HAMMER2_BREF_TYPE_VOLUME:
758 case HAMMER2_BREF_TYPE_FREEMAP:
760 * Copy data from bp to embedded buffer
762 panic("hammer2_chain_lock: called on unresolved volume header");
764 case HAMMER2_BREF_TYPE_INODE:
765 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
766 case HAMMER2_BREF_TYPE_INDIRECT:
767 case HAMMER2_BREF_TYPE_DATA:
768 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
771 * Point data at the device buffer and leave dio intact.
773 chain->data = (void *)bdata;
778 * Release HAMMER2_CHAIN_IOINPROG and signal waiters if requested.
785 oflags = chain->flags;
786 nflags = oflags & ~(HAMMER2_CHAIN_IOINPROG |
787 HAMMER2_CHAIN_IOSIGNAL);
788 KKASSERT(oflags & HAMMER2_CHAIN_IOINPROG);
789 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
790 if (oflags & HAMMER2_CHAIN_IOSIGNAL)
791 wakeup(&chain->flags);
798 * Unlock and deref a chain element.
800 * On the last lock release any non-embedded data (chain->dio) will be
804 hammer2_chain_unlock(hammer2_chain_t *chain)
806 hammer2_mtx_state_t ostate;
811 * If multiple locks are present (or being attempted) on this
812 * particular chain we can just unlock, drop refs, and return.
814 * Otherwise fall-through on the 1->0 transition.
817 lockcnt = chain->lockcnt;
818 KKASSERT(lockcnt > 0);
821 if (atomic_cmpset_int(&chain->lockcnt,
822 lockcnt, lockcnt - 1)) {
823 hammer2_mtx_unlock(&chain->lock);
827 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
834 * On the 1->0 transition we upgrade the core lock (if necessary)
835 * to exclusive for terminal processing. If after upgrading we find
836 * that lockcnt is non-zero, another thread is racing us and will
837 * handle the unload for us later on, so just cleanup and return
838 * leaving the data/io intact
840 * Otherwise if lockcnt is still 0 it is possible for it to become
841 * non-zero and race, but since we hold the core->lock exclusively
842 * all that will happen is that the chain will be reloaded after we
845 ostate = hammer2_mtx_upgrade(&chain->lock);
846 if (chain->lockcnt) {
847 hammer2_mtx_unlock(&chain->lock);
852 * Shortcut the case if the data is embedded or not resolved.
854 * Do NOT NULL out chain->data (e.g. inode data), it might be
857 if (chain->dio == NULL) {
858 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0)
859 hammer2_chain_drop_data(chain, 0);
860 hammer2_mtx_unlock(&chain->lock);
867 if (hammer2_io_isdirty(chain->dio) == 0) {
869 } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
870 switch(chain->bref.type) {
871 case HAMMER2_BREF_TYPE_DATA:
872 counterp = &hammer2_ioa_file_write;
874 case HAMMER2_BREF_TYPE_INODE:
875 counterp = &hammer2_ioa_meta_write;
877 case HAMMER2_BREF_TYPE_INDIRECT:
878 counterp = &hammer2_ioa_indr_write;
880 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
881 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
882 counterp = &hammer2_ioa_fmap_write;
885 counterp = &hammer2_ioa_volu_write;
888 *counterp += chain->bytes;
890 switch(chain->bref.type) {
891 case HAMMER2_BREF_TYPE_DATA:
892 counterp = &hammer2_iod_file_write;
894 case HAMMER2_BREF_TYPE_INODE:
895 counterp = &hammer2_iod_meta_write;
897 case HAMMER2_BREF_TYPE_INDIRECT:
898 counterp = &hammer2_iod_indr_write;
900 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
901 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
902 counterp = &hammer2_iod_fmap_write;
905 counterp = &hammer2_iod_volu_write;
908 *counterp += chain->bytes;
914 * If a device buffer was used for data be sure to destroy the
915 * buffer when we are done to avoid aliases (XXX what about the
916 * underlying VM pages?).
918 * NOTE: Freemap leaf's use reserved blocks and thus no aliasing
921 * NOTE: The isdirty check tracks whether we have to bdwrite() the
922 * buffer or not. The buffer might already be dirty. The
923 * flag is re-set when chain_modify() is called, even if
924 * MODIFIED is already set, allowing the OS to retire the
925 * buffer independent of a hammer2 flush.
928 if ((chain->flags & HAMMER2_CHAIN_IOFLUSH) &&
929 hammer2_io_isdirty(chain->dio)) {
930 hammer2_io_bawrite(&chain->dio);
932 hammer2_io_bqrelse(&chain->dio);
934 hammer2_mtx_unlock(&chain->lock);
938 * This counts the number of live blockrefs in a block array and
939 * also calculates the point at which all remaining blockrefs are empty.
940 * This routine can only be called on a live chain (DUPLICATED flag not set).
942 * NOTE: Flag is not set until after the count is complete, allowing
943 * callers to test the flag without holding the spinlock.
945 * NOTE: If base is NULL the related chain is still in the INITIAL
946 * state and there are no blockrefs to count.
948 * NOTE: live_count may already have some counts accumulated due to
949 * creation and deletion and could even be initially negative.
952 hammer2_chain_countbrefs(hammer2_chain_t *chain,
953 hammer2_blockref_t *base, int count)
955 hammer2_spin_ex(&chain->core.spin);
956 if ((chain->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) {
958 while (--count >= 0) {
959 if (base[count].type)
962 chain->core.live_zero = count + 1;
964 if (base[count].type)
965 atomic_add_int(&chain->core.live_count,
970 chain->core.live_zero = 0;
972 /* else do not modify live_count */
973 atomic_set_int(&chain->flags, HAMMER2_CHAIN_COUNTEDBREFS);
975 hammer2_spin_unex(&chain->core.spin);
979 * Resize the chain's physical storage allocation in-place. This function does
980 * not adjust the data pointer and must be followed by (typically) a
981 * hammer2_chain_modify() call to copy any old data over and adjust the
984 * Chains can be resized smaller without reallocating the storage. Resizing
985 * larger will reallocate the storage. Excess or prior storage is reclaimed
986 * asynchronously at a later time.
988 * Must be passed an exclusively locked parent and chain.
990 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
991 * to avoid instantiating a device buffer that conflicts with the vnode data
992 * buffer. However, because H2 can compress or encrypt data, the chain may
993 * have a dio assigned to it in those situations, and they do not conflict.
995 * XXX return error if cannot resize.
998 hammer2_chain_resize(hammer2_inode_t *ip,
999 hammer2_chain_t *parent, hammer2_chain_t *chain,
1000 hammer2_tid_t mtid, int nradix, int flags)
1009 * Only data and indirect blocks can be resized for now.
1010 * (The volu root, inodes, and freemap elements use a fixed size).
1012 KKASSERT(chain != &hmp->vchain);
1013 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1014 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
1015 KKASSERT(chain->parent == parent);
1018 * Nothing to do if the element is already the proper size
1020 obytes = chain->bytes;
1021 nbytes = 1U << nradix;
1022 if (obytes == nbytes)
1026 * Make sure the old data is instantiated so we can copy it. If this
1027 * is a data block, the device data may be superfluous since the data
1028 * might be in a logical block, but compressed or encrypted data is
1031 * NOTE: The modify will set BMAPUPD for us if BMAPPED is set.
1033 hammer2_chain_modify(chain, mtid, 0);
1036 * Relocate the block, even if making it smaller (because different
1037 * block sizes may be in different regions).
1039 * (data blocks only, we aren't copying the storage here).
1041 hammer2_freemap_alloc(chain, nbytes);
1042 chain->bytes = nbytes;
1043 /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */
1046 * We don't want the followup chain_modify() to try to copy data
1047 * from the old (wrong-sized) buffer. It won't know how much to
1048 * copy. This case should only occur during writes when the
1049 * originator already has the data to write in-hand.
1052 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA);
1053 hammer2_io_brelse(&chain->dio);
1059 hammer2_chain_modify(hammer2_chain_t *chain,
1060 hammer2_tid_t mtid, int flags)
1062 hammer2_blockref_t obref;
1071 obref = chain->bref;
1072 KKASSERT((chain->flags & HAMMER2_CHAIN_FICTITIOUS) == 0);
1075 * Data is not optional for freemap chains (we must always be sure
1076 * to copy the data on COW storage allocations).
1078 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1079 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1080 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1081 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1085 * Data must be resolved if already assigned, unless explicitly
1086 * flagged otherwise.
1088 if (chain->data == NULL && (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1089 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1090 hammer2_chain_load_data(chain);
1094 * Set MODIFIED to indicate that the chain has been modified.
1095 * Set UPDATE to ensure that the blockref is updated in the parent.
1097 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1098 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1099 hammer2_chain_ref(chain);
1100 hammer2_pfs_memory_inc(chain->pmp); /* can be NULL */
1105 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
1106 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1107 hammer2_chain_ref(chain);
1111 * The modification or re-modification requires an allocation and
1114 * We normally always allocate new storage here. If storage exists
1115 * and MODIFY_NOREALLOC is passed in, we do not allocate new storage.
1117 if (chain != &hmp->vchain && chain != &hmp->fchain) {
1118 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1119 ((flags & HAMMER2_MODIFY_NOREALLOC) == 0 && newmod)
1121 hammer2_freemap_alloc(chain, chain->bytes);
1122 /* XXX failed allocation */
1127 * Update mirror_tid and modify_tid. modify_tid is only updated
1128 * automatically by this function when used from the frontend.
1129 * Flushes and synchronization adjust the flag manually.
1131 * NOTE: chain->pmp could be the device spmp.
1133 KKASSERT(mtid != 0);
1134 chain->bref.mirror_tid = hmp->voldata.mirror_tid + 1;
1135 chain->bref.modify_tid = mtid;
1138 * Set BMAPUPD to tell the flush code that an existing blockmap entry
1139 * requires updating as well as to tell the delete code that the
1140 * chain's blockref might not exactly match (in terms of physical size
1141 * or block offset) the one in the parent's blocktable. The base key
1142 * of course will still match.
1144 if (chain->flags & HAMMER2_CHAIN_BMAPPED)
1145 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD);
1148 * Short-cut data blocks which the caller does not need an actual
1149 * data reference to (aka OPTDATA), as long as the chain does not
1150 * already have a data pointer to the data. This generally means
1151 * that the modifications are being done via the logical buffer cache.
1152 * The INITIAL flag relates only to the device data buffer and thus
1153 * remains unchange in this situation.
1155 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA &&
1156 (flags & HAMMER2_MODIFY_OPTDATA) &&
1157 chain->data == NULL) {
1162 * Clearing the INITIAL flag (for indirect blocks) indicates that
1163 * we've processed the uninitialized storage allocation.
1165 * If this flag is already clear we are likely in a copy-on-write
1166 * situation but we have to be sure NOT to bzero the storage if
1167 * no data is present.
1169 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1170 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1177 * Instantiate data buffer and possibly execute COW operation
1179 switch(chain->bref.type) {
1180 case HAMMER2_BREF_TYPE_VOLUME:
1181 case HAMMER2_BREF_TYPE_FREEMAP:
1183 * The data is embedded, no copy-on-write operation is
1186 KKASSERT(chain->dio == NULL);
1188 case HAMMER2_BREF_TYPE_INODE:
1189 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1190 case HAMMER2_BREF_TYPE_DATA:
1191 case HAMMER2_BREF_TYPE_INDIRECT:
1192 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1194 * Perform the copy-on-write operation
1196 * zero-fill or copy-on-write depending on whether
1197 * chain->data exists or not and set the dirty state for
1198 * the new buffer. hammer2_io_new() will handle the
1201 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1204 error = hammer2_io_new(hmp, chain->bref.data_off,
1205 chain->bytes, &dio);
1207 error = hammer2_io_bread(hmp, chain->bref.data_off,
1208 chain->bytes, &dio);
1210 hammer2_adjreadcounter(&chain->bref, chain->bytes);
1213 * If an I/O error occurs make sure callers cannot accidently
1214 * modify the old buffer's contents and corrupt the filesystem.
1217 kprintf("hammer2_chain_modify: hmp=%p I/O error\n",
1219 chain->error = HAMMER2_ERROR_IO;
1220 hammer2_io_brelse(&dio);
1221 hammer2_io_brelse(&chain->dio);
1226 bdata = hammer2_io_data(dio, chain->bref.data_off);
1229 KKASSERT(chain->dio != NULL);
1230 if (chain->data != (void *)bdata) {
1231 bcopy(chain->data, bdata, chain->bytes);
1233 } else if (wasinitial == 0) {
1235 * We have a problem. We were asked to COW but
1236 * we don't have any data to COW with!
1238 panic("hammer2_chain_modify: having a COW %p\n",
1243 * Retire the old buffer, replace with the new. Dirty or
1244 * redirty the new buffer.
1246 * WARNING! The system buffer cache may have already flushed
1247 * the buffer, so we must be sure to [re]dirty it
1248 * for further modification.
1251 hammer2_io_brelse(&chain->dio);
1252 chain->data = (void *)bdata;
1254 hammer2_io_setdirty(dio); /* modified by bcopy above */
1257 panic("hammer2_chain_modify: illegal non-embedded type %d",
1264 * setflush on parent indicating that the parent must recurse down
1265 * to us. Do not call on chain itself which might already have it
1269 hammer2_chain_setflush(chain->parent);
1273 * Modify the chain associated with an inode.
1276 hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
1277 hammer2_tid_t mtid, int flags)
1279 hammer2_inode_modify(ip);
1280 hammer2_chain_modify(chain, mtid, flags);
1284 * Volume header data locks
1287 hammer2_voldata_lock(hammer2_dev_t *hmp)
1289 lockmgr(&hmp->vollk, LK_EXCLUSIVE);
1293 hammer2_voldata_unlock(hammer2_dev_t *hmp)
1295 lockmgr(&hmp->vollk, LK_RELEASE);
1299 hammer2_voldata_modify(hammer2_dev_t *hmp)
1301 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1302 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
1303 hammer2_chain_ref(&hmp->vchain);
1304 hammer2_pfs_memory_inc(hmp->vchain.pmp);
1309 * This function returns the chain at the nearest key within the specified
1310 * range. The returned chain will be referenced but not locked.
1312 * This function will recurse through chain->rbtree as necessary and will
1313 * return a *key_nextp suitable for iteration. *key_nextp is only set if
1314 * the iteration value is less than the current value of *key_nextp.
1316 * The caller should use (*key_nextp) to calculate the actual range of
1317 * the returned element, which will be (key_beg to *key_nextp - 1), because
1318 * there might be another element which is superior to the returned element
1321 * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
1322 * chains continue to be returned. On EOF (*key_nextp) may overflow since
1323 * it will wind up being (key_end + 1).
1325 * WARNING! Must be called with child's spinlock held. Spinlock remains
1326 * held through the operation.
1328 struct hammer2_chain_find_info {
1329 hammer2_chain_t *best;
1330 hammer2_key_t key_beg;
1331 hammer2_key_t key_end;
1332 hammer2_key_t key_next;
1335 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
1336 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
1340 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
1341 hammer2_key_t key_beg, hammer2_key_t key_end)
1343 struct hammer2_chain_find_info info;
1346 info.key_beg = key_beg;
1347 info.key_end = key_end;
1348 info.key_next = *key_nextp;
1350 RB_SCAN(hammer2_chain_tree, &parent->core.rbtree,
1351 hammer2_chain_find_cmp, hammer2_chain_find_callback,
1353 *key_nextp = info.key_next;
1355 kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
1356 parent, key_beg, key_end, *key_nextp);
1364 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
1366 struct hammer2_chain_find_info *info = data;
1367 hammer2_key_t child_beg;
1368 hammer2_key_t child_end;
1370 child_beg = child->bref.key;
1371 child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
1373 if (child_end < info->key_beg)
1375 if (child_beg > info->key_end)
1382 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
1384 struct hammer2_chain_find_info *info = data;
1385 hammer2_chain_t *best;
1386 hammer2_key_t child_end;
1389 * WARNING! Do not discard DUPLICATED chains, it is possible that
1390 * we are catching an insertion half-way done. If a
1391 * duplicated chain turns out to be the best choice the
1392 * caller will re-check its flags after locking it.
1394 * WARNING! Layerq is scanned forwards, exact matches should keep
1395 * the existing info->best.
1397 if ((best = info->best) == NULL) {
1399 * No previous best. Assign best
1402 } else if (best->bref.key <= info->key_beg &&
1403 child->bref.key <= info->key_beg) {
1408 /*info->best = child;*/
1409 } else if (child->bref.key < best->bref.key) {
1411 * Child has a nearer key and best is not flush with key_beg.
1412 * Set best to child. Truncate key_next to the old best key.
1415 if (info->key_next > best->bref.key || info->key_next == 0)
1416 info->key_next = best->bref.key;
1417 } else if (child->bref.key == best->bref.key) {
1419 * If our current best is flush with the child then this
1420 * is an illegal overlap.
1422 * key_next will automatically be limited to the smaller of
1423 * the two end-points.
1429 * Keep the current best but truncate key_next to the child's
1432 * key_next will also automatically be limited to the smaller
1433 * of the two end-points (probably not necessary for this case
1434 * but we do it anyway).
1436 if (info->key_next > child->bref.key || info->key_next == 0)
1437 info->key_next = child->bref.key;
1441 * Always truncate key_next based on child's end-of-range.
1443 child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
1444 if (child_end && (info->key_next > child_end || info->key_next == 0))
1445 info->key_next = child_end;
1451 * Retrieve the specified chain from a media blockref, creating the
1452 * in-memory chain structure which reflects it.
1454 * To handle insertion races pass the INSERT_RACE flag along with the
1455 * generation number of the core. NULL will be returned if the generation
1456 * number changes before we have a chance to insert the chain. Insert
1457 * races can occur because the parent might be held shared.
1459 * Caller must hold the parent locked shared or exclusive since we may
1460 * need the parent's bref array to find our block.
1462 * WARNING! chain->pmp is always set to NULL for any chain representing
1463 * part of the super-root topology.
1466 hammer2_chain_get(hammer2_chain_t *parent, int generation,
1467 hammer2_blockref_t *bref)
1469 hammer2_dev_t *hmp = parent->hmp;
1470 hammer2_chain_t *chain;
1474 * Allocate a chain structure representing the existing media
1475 * entry. Resulting chain has one ref and is not locked.
1477 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
1478 chain = hammer2_chain_alloc(hmp, NULL, bref);
1480 chain = hammer2_chain_alloc(hmp, parent->pmp, bref);
1481 /* ref'd chain returned */
1484 * Flag that the chain is in the parent's blockmap so delete/flush
1485 * knows what to do with it.
1487 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
1490 * Link the chain into its parent. A spinlock is required to safely
1491 * access the RBTREE, and it is possible to collide with another
1492 * hammer2_chain_get() operation because the caller might only hold
1493 * a shared lock on the parent.
1495 KKASSERT(parent->refs > 0);
1496 error = hammer2_chain_insert(parent, chain,
1497 HAMMER2_CHAIN_INSERT_SPIN |
1498 HAMMER2_CHAIN_INSERT_RACE,
1501 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
1502 kprintf("chain %p get race\n", chain);
1503 hammer2_chain_drop(chain);
1506 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
1510 * Return our new chain referenced but not locked, or NULL if
1517 * Lookup initialization/completion API
1520 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
1522 hammer2_chain_ref(parent);
1523 if (flags & HAMMER2_LOOKUP_SHARED) {
1524 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1525 HAMMER2_RESOLVE_SHARED);
1527 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1533 hammer2_chain_lookup_done(hammer2_chain_t *parent)
1536 hammer2_chain_unlock(parent);
1537 hammer2_chain_drop(parent);
1542 hammer2_chain_getparent(hammer2_chain_t **parentp, int how)
1544 hammer2_chain_t *oparent;
1545 hammer2_chain_t *nparent;
1548 * Be careful of order, oparent must be unlocked before nparent
1549 * is locked below to avoid a deadlock.
1552 hammer2_spin_ex(&oparent->core.spin);
1553 nparent = oparent->parent;
1554 hammer2_chain_ref(nparent);
1555 hammer2_spin_unex(&oparent->core.spin);
1557 hammer2_chain_unlock(oparent);
1558 hammer2_chain_drop(oparent);
1562 hammer2_chain_lock(nparent, how);
1569 * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
1570 * (*parentp) typically points to an inode but can also point to a related
1571 * indirect block and this function will recurse upwards and find the inode
1574 * (*parentp) must be exclusively locked and referenced and can be an inode
1575 * or an existing indirect block within the inode.
1577 * On return (*parentp) will be modified to point at the deepest parent chain
1578 * element encountered during the search, as a helper for an insertion or
1579 * deletion. The new (*parentp) will be locked and referenced and the old
1580 * will be unlocked and dereferenced (no change if they are both the same).
1582 * The matching chain will be returned exclusively locked. If NOLOCK is
1583 * requested the chain will be returned only referenced. Note that the
1584 * parent chain must always be locked shared or exclusive, matching the
1585 * HAMMER2_LOOKUP_SHARED flag. We can conceivably lock it SHARED temporarily
1586 * when NOLOCK is specified but that complicates matters if *parentp must
1587 * inherit the chain.
1589 * NOLOCK also implies NODATA, since an unlocked chain usually has a NULL
1590 * data pointer or can otherwise be in flux.
1592 * NULL is returned if no match was found, but (*parentp) will still
1593 * potentially be adjusted.
1595 * If a fatal error occurs (typically an I/O error), a dummy chain is
1596 * returned with chain->error and error-identifying information set. This
1597 * chain will assert if you try to do anything fancy with it.
1599 * XXX Depending on where the error occurs we should allow continued iteration.
1601 * On return (*key_nextp) will point to an iterative value for key_beg.
1602 * (If NULL is returned (*key_nextp) is set to (key_end + 1)).
1604 * This function will also recurse up the chain if the key is not within the
1605 * current parent's range. (*parentp) can never be set to NULL. An iteration
1606 * can simply allow (*parentp) to float inside the loop.
1608 * NOTE! chain->data is not always resolved. By default it will not be
1609 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
1610 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
1611 * BREF_TYPE_DATA as the device buffer can alias the logical file
1615 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
1616 hammer2_key_t key_beg, hammer2_key_t key_end,
1617 int *cache_indexp, int flags)
1620 hammer2_chain_t *parent;
1621 hammer2_chain_t *chain;
1622 hammer2_blockref_t *base;
1623 hammer2_blockref_t *bref;
1624 hammer2_blockref_t bcopy;
1625 hammer2_key_t scan_beg;
1626 hammer2_key_t scan_end;
1628 int how_always = HAMMER2_RESOLVE_ALWAYS;
1629 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1632 int maxloops = 300000;
1634 if (flags & HAMMER2_LOOKUP_ALWAYS) {
1635 how_maybe = how_always;
1636 how = HAMMER2_RESOLVE_ALWAYS;
1637 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
1638 how = HAMMER2_RESOLVE_NEVER;
1640 how = HAMMER2_RESOLVE_MAYBE;
1642 if (flags & HAMMER2_LOOKUP_SHARED) {
1643 how_maybe |= HAMMER2_RESOLVE_SHARED;
1644 how_always |= HAMMER2_RESOLVE_SHARED;
1645 how |= HAMMER2_RESOLVE_SHARED;
1649 * Recurse (*parentp) upward if necessary until the parent completely
1650 * encloses the key range or we hit the inode.
1652 * This function handles races against the flusher doing a delete-
1653 * duplicate above us and re-homes the parent to the duplicate in
1654 * that case, otherwise we'd wind up recursing down a stale chain.
1659 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1660 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1661 scan_beg = parent->bref.key;
1662 scan_end = scan_beg +
1663 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1664 if (key_beg >= scan_beg && key_end <= scan_end)
1666 parent = hammer2_chain_getparent(parentp, how_maybe);
1670 if (--maxloops == 0)
1671 panic("hammer2_chain_lookup: maxloops");
1673 * Locate the blockref array. Currently we do a fully associative
1674 * search through the array.
1676 switch(parent->bref.type) {
1677 case HAMMER2_BREF_TYPE_INODE:
1679 * Special shortcut for embedded data returns the inode
1680 * itself. Callers must detect this condition and access
1681 * the embedded data (the strategy code does this for us).
1683 * This is only applicable to regular files and softlinks.
1685 if (parent->data->ipdata.meta.op_flags &
1686 HAMMER2_OPFLAG_DIRECTDATA) {
1687 if (flags & HAMMER2_LOOKUP_NODIRECT) {
1689 *key_nextp = key_end + 1;
1692 hammer2_chain_ref(parent);
1693 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0)
1694 hammer2_chain_lock(parent, how_always);
1695 *key_nextp = key_end + 1;
1698 base = &parent->data->ipdata.u.blockset.blockref[0];
1699 count = HAMMER2_SET_COUNT;
1701 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1702 case HAMMER2_BREF_TYPE_INDIRECT:
1704 * Handle MATCHIND on the parent
1706 if (flags & HAMMER2_LOOKUP_MATCHIND) {
1707 scan_beg = parent->bref.key;
1708 scan_end = scan_beg +
1709 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1710 if (key_beg == scan_beg && key_end == scan_end) {
1712 hammer2_chain_ref(chain);
1713 hammer2_chain_lock(chain, how_maybe);
1714 *key_nextp = scan_end + 1;
1719 * Optimize indirect blocks in the INITIAL state to avoid
1722 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1725 if (parent->data == NULL)
1726 panic("parent->data is NULL");
1727 base = &parent->data->npdata[0];
1729 count = parent->bytes / sizeof(hammer2_blockref_t);
1731 case HAMMER2_BREF_TYPE_VOLUME:
1732 base = &hmp->voldata.sroot_blockset.blockref[0];
1733 count = HAMMER2_SET_COUNT;
1735 case HAMMER2_BREF_TYPE_FREEMAP:
1736 base = &hmp->voldata.freemap_blockset.blockref[0];
1737 count = HAMMER2_SET_COUNT;
1740 kprintf("hammer2_chain_lookup: unrecognized "
1741 "blockref(B) type: %d",
1744 tsleep(&base, 0, "dead", 0);
1745 panic("hammer2_chain_lookup: unrecognized "
1746 "blockref(B) type: %d",
1748 base = NULL; /* safety */
1749 count = 0; /* safety */
1753 * Merged scan to find next candidate.
1755 * hammer2_base_*() functions require the parent->core.live_* fields
1756 * to be synchronized.
1758 * We need to hold the spinlock to access the block array and RB tree
1759 * and to interlock chain creation.
1761 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
1762 hammer2_chain_countbrefs(parent, base, count);
1767 hammer2_spin_ex(&parent->core.spin);
1768 chain = hammer2_combined_find(parent, base, count,
1769 cache_indexp, key_nextp,
1772 generation = parent->core.generation;
1775 * Exhausted parent chain, iterate.
1778 hammer2_spin_unex(&parent->core.spin);
1779 if (key_beg == key_end) /* short cut single-key case */
1783 * Stop if we reached the end of the iteration.
1785 if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1786 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1791 * Calculate next key, stop if we reached the end of the
1792 * iteration, otherwise go up one level and loop.
1794 key_beg = parent->bref.key +
1795 ((hammer2_key_t)1 << parent->bref.keybits);
1796 if (key_beg == 0 || key_beg > key_end)
1798 parent = hammer2_chain_getparent(parentp, how_maybe);
1803 * Selected from blockref or in-memory chain.
1805 if (chain == NULL) {
1807 hammer2_spin_unex(&parent->core.spin);
1808 chain = hammer2_chain_get(parent, generation,
1810 if (chain == NULL) {
1811 kprintf("retry lookup parent %p keys %016jx:%016jx\n",
1812 parent, key_beg, key_end);
1815 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
1816 hammer2_chain_drop(chain);
1820 hammer2_chain_ref(chain);
1821 hammer2_spin_unex(&parent->core.spin);
1825 * chain is referenced but not locked. We must lock the chain
1826 * to obtain definitive DUPLICATED/DELETED state
1828 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1829 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1830 hammer2_chain_lock(chain, how_maybe);
1832 hammer2_chain_lock(chain, how);
1836 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
1838 * NOTE: Chain's key range is not relevant as there might be
1839 * one-offs within the range that are not deleted.
1841 * NOTE: Lookups can race delete-duplicate because
1842 * delete-duplicate does not lock the parent's core
1843 * (they just use the spinlock on the core). We must
1844 * check for races by comparing the DUPLICATED flag before
1845 * releasing the spinlock with the flag after locking the
1848 if (chain->flags & HAMMER2_CHAIN_DELETED) {
1849 hammer2_chain_unlock(chain);
1850 hammer2_chain_drop(chain);
1851 key_beg = *key_nextp;
1852 if (key_beg == 0 || key_beg > key_end)
1858 * If the chain element is an indirect block it becomes the new
1859 * parent and we loop on it. We must maintain our top-down locks
1860 * to prevent the flusher from interfering (i.e. doing a
1861 * delete-duplicate and leaving us recursing down a deleted chain).
1863 * The parent always has to be locked with at least RESOLVE_MAYBE
1864 * so we can access its data. It might need a fixup if the caller
1865 * passed incompatible flags. Be careful not to cause a deadlock
1866 * as a data-load requires an exclusive lock.
1868 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
1869 * range is within the requested key range we return the indirect
1870 * block and do NOT loop. This is usually only used to acquire
1873 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1874 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1875 hammer2_chain_unlock(parent);
1876 hammer2_chain_drop(parent);
1877 *parentp = parent = chain;
1882 * All done, return the chain.
1884 * If the caller does not want a locked chain, replace the lock with
1885 * a ref. Perhaps this can eventually be optimized to not obtain the
1886 * lock in the first place for situations where the data does not
1887 * need to be resolved.
1890 if (flags & HAMMER2_LOOKUP_NOLOCK)
1891 hammer2_chain_unlock(chain);
1898 * After having issued a lookup we can iterate all matching keys.
1900 * If chain is non-NULL we continue the iteration from just after it's index.
1902 * If chain is NULL we assume the parent was exhausted and continue the
1903 * iteration at the next parent.
1905 * If a fatal error occurs (typically an I/O error), a dummy chain is
1906 * returned with chain->error and error-identifying information set. This
1907 * chain will assert if you try to do anything fancy with it.
1909 * XXX Depending on where the error occurs we should allow continued iteration.
1911 * parent must be locked on entry and remains locked throughout. chain's
1912 * lock status must match flags. Chain is always at least referenced.
1914 * WARNING! The MATCHIND flag does not apply to this function.
1917 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
1918 hammer2_key_t *key_nextp,
1919 hammer2_key_t key_beg, hammer2_key_t key_end,
1920 int *cache_indexp, int flags)
1922 hammer2_chain_t *parent;
1926 * Calculate locking flags for upward recursion.
1928 how_maybe = HAMMER2_RESOLVE_MAYBE;
1929 if (flags & HAMMER2_LOOKUP_SHARED)
1930 how_maybe |= HAMMER2_RESOLVE_SHARED;
1935 * Calculate the next index and recalculate the parent if necessary.
1938 key_beg = chain->bref.key +
1939 ((hammer2_key_t)1 << chain->bref.keybits);
1940 if ((flags & (HAMMER2_LOOKUP_NOLOCK |
1941 HAMMER2_LOOKUP_NOUNLOCK)) == 0) {
1942 hammer2_chain_unlock(chain);
1944 hammer2_chain_drop(chain);
1947 * chain invalid past this point, but we can still do a
1948 * pointer comparison w/parent.
1950 * Any scan where the lookup returned degenerate data embedded
1951 * in the inode has an invalid index and must terminate.
1953 if (chain == parent)
1955 if (key_beg == 0 || key_beg > key_end)
1958 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1959 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1961 * We reached the end of the iteration.
1966 * Continue iteration with next parent unless the current
1967 * parent covers the range.
1969 key_beg = parent->bref.key +
1970 ((hammer2_key_t)1 << parent->bref.keybits);
1971 if (key_beg == 0 || key_beg > key_end)
1973 parent = hammer2_chain_getparent(parentp, how_maybe);
1979 return (hammer2_chain_lookup(parentp, key_nextp,
1981 cache_indexp, flags));
1985 * The raw scan function is similar to lookup/next but does not seek to a key.
1986 * Blockrefs are iterated via first_chain = (parent, NULL) and
1987 * next_chain = (parent, chain).
1989 * The passed-in parent must be locked and its data resolved. The returned
1990 * chain will be locked. Pass chain == NULL to acquire the first sub-chain
1991 * under parent and then iterate with the passed-in chain (which this
1992 * function will unlock).
1995 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t *chain,
1996 int *cache_indexp, int flags)
1999 hammer2_blockref_t *base;
2000 hammer2_blockref_t *bref;
2001 hammer2_blockref_t bcopy;
2003 hammer2_key_t next_key;
2005 int how_always = HAMMER2_RESOLVE_ALWAYS;
2006 int how_maybe = HAMMER2_RESOLVE_MAYBE;
2009 int maxloops = 300000;
2014 * Scan flags borrowed from lookup.
2016 if (flags & HAMMER2_LOOKUP_ALWAYS) {
2017 how_maybe = how_always;
2018 how = HAMMER2_RESOLVE_ALWAYS;
2019 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2020 how = HAMMER2_RESOLVE_NEVER;
2022 how = HAMMER2_RESOLVE_MAYBE;
2024 if (flags & HAMMER2_LOOKUP_SHARED) {
2025 how_maybe |= HAMMER2_RESOLVE_SHARED;
2026 how_always |= HAMMER2_RESOLVE_SHARED;
2027 how |= HAMMER2_RESOLVE_SHARED;
2031 * Calculate key to locate first/next element, unlocking the previous
2032 * element as we go. Be careful, the key calculation can overflow.
2035 key = chain->bref.key +
2036 ((hammer2_key_t)1 << chain->bref.keybits);
2037 hammer2_chain_unlock(chain);
2038 hammer2_chain_drop(chain);
2047 KKASSERT(parent->error == 0); /* XXX case not handled yet */
2048 if (--maxloops == 0)
2049 panic("hammer2_chain_scan: maxloops");
2051 * Locate the blockref array. Currently we do a fully associative
2052 * search through the array.
2054 switch(parent->bref.type) {
2055 case HAMMER2_BREF_TYPE_INODE:
2057 * An inode with embedded data has no sub-chains.
2059 if (parent->data->ipdata.meta.op_flags &
2060 HAMMER2_OPFLAG_DIRECTDATA) {
2063 base = &parent->data->ipdata.u.blockset.blockref[0];
2064 count = HAMMER2_SET_COUNT;
2066 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2067 case HAMMER2_BREF_TYPE_INDIRECT:
2069 * Optimize indirect blocks in the INITIAL state to avoid
2072 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2075 if (parent->data == NULL)
2076 panic("parent->data is NULL");
2077 base = &parent->data->npdata[0];
2079 count = parent->bytes / sizeof(hammer2_blockref_t);
2081 case HAMMER2_BREF_TYPE_VOLUME:
2082 base = &hmp->voldata.sroot_blockset.blockref[0];
2083 count = HAMMER2_SET_COUNT;
2085 case HAMMER2_BREF_TYPE_FREEMAP:
2086 base = &hmp->voldata.freemap_blockset.blockref[0];
2087 count = HAMMER2_SET_COUNT;
2090 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2092 base = NULL; /* safety */
2093 count = 0; /* safety */
2097 * Merged scan to find next candidate.
2099 * hammer2_base_*() functions require the parent->core.live_* fields
2100 * to be synchronized.
2102 * We need to hold the spinlock to access the block array and RB tree
2103 * and to interlock chain creation.
2105 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2106 hammer2_chain_countbrefs(parent, base, count);
2109 hammer2_spin_ex(&parent->core.spin);
2110 chain = hammer2_combined_find(parent, base, count,
2111 cache_indexp, &next_key,
2112 key, HAMMER2_KEY_MAX,
2114 generation = parent->core.generation;
2117 * Exhausted parent chain, we're done.
2120 hammer2_spin_unex(&parent->core.spin);
2121 KKASSERT(chain == NULL);
2126 * Selected from blockref or in-memory chain.
2128 if (chain == NULL) {
2130 hammer2_spin_unex(&parent->core.spin);
2131 chain = hammer2_chain_get(parent, generation, &bcopy);
2132 if (chain == NULL) {
2133 kprintf("retry scan parent %p keys %016jx\n",
2137 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2138 hammer2_chain_drop(chain);
2143 hammer2_chain_ref(chain);
2144 hammer2_spin_unex(&parent->core.spin);
2148 * chain is referenced but not locked. We must lock the chain
2149 * to obtain definitive DUPLICATED/DELETED state
2151 hammer2_chain_lock(chain, how);
2154 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2156 * NOTE: chain's key range is not relevant as there might be
2157 * one-offs within the range that are not deleted.
2159 * NOTE: XXX this could create problems with scans used in
2160 * situations other than mount-time recovery.
2162 * NOTE: Lookups can race delete-duplicate because
2163 * delete-duplicate does not lock the parent's core
2164 * (they just use the spinlock on the core). We must
2165 * check for races by comparing the DUPLICATED flag before
2166 * releasing the spinlock with the flag after locking the
2169 if (chain->flags & HAMMER2_CHAIN_DELETED) {
2170 hammer2_chain_unlock(chain);
2171 hammer2_chain_drop(chain);
2182 * All done, return the chain or NULL
2188 * Create and return a new hammer2 system memory structure of the specified
2189 * key, type and size and insert it under (*parentp). This is a full
2190 * insertion, based on the supplied key/keybits, and may involve creating
2191 * indirect blocks and moving other chains around via delete/duplicate.
2193 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION
2194 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2195 * FULL. This typically means that the caller is creating the chain after
2196 * doing a hammer2_chain_lookup().
2198 * (*parentp) must be exclusive locked and may be replaced on return
2199 * depending on how much work the function had to do.
2201 * (*parentp) must not be errored or this function will assert.
2203 * (*chainp) usually starts out NULL and returns the newly created chain,
2204 * but if the caller desires the caller may allocate a disconnected chain
2205 * and pass it in instead.
2207 * This function should NOT be used to insert INDIRECT blocks. It is
2208 * typically used to create/insert inodes and data blocks.
2210 * Caller must pass-in an exclusively locked parent the new chain is to
2211 * be inserted under, and optionally pass-in a disconnected, exclusively
2212 * locked chain to insert (else we create a new chain). The function will
2213 * adjust (*parentp) as necessary, create or connect the chain, and
2214 * return an exclusively locked chain in *chainp.
2216 * When creating a PFSROOT inode under the super-root, pmp is typically NULL
2217 * and will be reassigned.
2220 hammer2_chain_create(hammer2_chain_t **parentp,
2221 hammer2_chain_t **chainp, hammer2_pfs_t *pmp,
2222 hammer2_key_t key, int keybits, int type, size_t bytes,
2223 hammer2_tid_t mtid, int flags)
2226 hammer2_chain_t *chain;
2227 hammer2_chain_t *parent;
2228 hammer2_blockref_t *base;
2229 hammer2_blockref_t dummy;
2233 int maxloops = 300000;
2236 * Topology may be crossing a PFS boundary.
2239 KKASSERT(hammer2_mtx_owned(&parent->lock));
2240 KKASSERT(parent->error == 0);
2244 if (chain == NULL) {
2246 * First allocate media space and construct the dummy bref,
2247 * then allocate the in-memory chain structure. Set the
2248 * INITIAL flag for fresh chains which do not have embedded
2251 * XXX for now set the check mode of the child based on
2252 * the parent or, if the parent is an inode, the
2253 * specification in the inode.
2255 bzero(&dummy, sizeof(dummy));
2258 dummy.keybits = keybits;
2259 dummy.data_off = hammer2_getradix(bytes);
2260 dummy.methods = parent->bref.methods;
2261 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE &&
2263 dummy.methods &= ~HAMMER2_ENC_CHECK(-1);
2264 dummy.methods |= HAMMER2_ENC_CHECK(
2265 parent->data->ipdata.meta.check_algo);
2268 chain = hammer2_chain_alloc(hmp, pmp, &dummy);
2271 * Lock the chain manually, chain_lock will load the chain
2272 * which we do NOT want to do. (note: chain->refs is set
2273 * to 1 by chain_alloc() for us, but lockcnt is not).
2276 hammer2_mtx_ex(&chain->lock);
2280 * Set INITIAL to optimize I/O. The flag will generally be
2281 * processed when we call hammer2_chain_modify().
2283 * Recalculate bytes to reflect the actual media block
2286 bytes = (hammer2_off_t)1 <<
2287 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
2288 chain->bytes = bytes;
2291 case HAMMER2_BREF_TYPE_VOLUME:
2292 case HAMMER2_BREF_TYPE_FREEMAP:
2293 panic("hammer2_chain_create: called with volume type");
2295 case HAMMER2_BREF_TYPE_INDIRECT:
2296 panic("hammer2_chain_create: cannot be used to"
2297 "create indirect block");
2299 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2300 panic("hammer2_chain_create: cannot be used to"
2301 "create freemap root or node");
2303 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2304 KKASSERT(bytes == sizeof(chain->data->bmdata));
2306 case HAMMER2_BREF_TYPE_INODE:
2307 case HAMMER2_BREF_TYPE_DATA:
2310 * leave chain->data NULL, set INITIAL
2312 KKASSERT(chain->data == NULL);
2313 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
2318 * We are reattaching a previously deleted chain, possibly
2319 * under a new parent and possibly with a new key/keybits.
2320 * The chain does not have to be in a modified state. The
2321 * UPDATE flag will be set later on in this routine.
2323 * Do NOT mess with the current state of the INITIAL flag.
2325 chain->bref.key = key;
2326 chain->bref.keybits = keybits;
2327 if (chain->flags & HAMMER2_CHAIN_DELETED)
2328 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2329 KKASSERT(chain->parent == NULL);
2331 if (flags & HAMMER2_INSERT_PFSROOT)
2332 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
2334 chain->bref.flags &= ~HAMMER2_BREF_FLAG_PFSROOT;
2337 * Calculate how many entries we have in the blockref array and
2338 * determine if an indirect block is required.
2341 if (--maxloops == 0)
2342 panic("hammer2_chain_create: maxloops");
2344 switch(parent->bref.type) {
2345 case HAMMER2_BREF_TYPE_INODE:
2346 KKASSERT((parent->data->ipdata.meta.op_flags &
2347 HAMMER2_OPFLAG_DIRECTDATA) == 0);
2348 KKASSERT(parent->data != NULL);
2349 base = &parent->data->ipdata.u.blockset.blockref[0];
2350 count = HAMMER2_SET_COUNT;
2352 case HAMMER2_BREF_TYPE_INDIRECT:
2353 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2354 if (parent->flags & HAMMER2_CHAIN_INITIAL)
2357 base = &parent->data->npdata[0];
2358 count = parent->bytes / sizeof(hammer2_blockref_t);
2360 case HAMMER2_BREF_TYPE_VOLUME:
2361 KKASSERT(parent->data != NULL);
2362 base = &hmp->voldata.sroot_blockset.blockref[0];
2363 count = HAMMER2_SET_COUNT;
2365 case HAMMER2_BREF_TYPE_FREEMAP:
2366 KKASSERT(parent->data != NULL);
2367 base = &hmp->voldata.freemap_blockset.blockref[0];
2368 count = HAMMER2_SET_COUNT;
2371 panic("hammer2_chain_create: unrecognized blockref type: %d",
2379 * Make sure we've counted the brefs
2381 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2382 hammer2_chain_countbrefs(parent, base, count);
2384 KKASSERT(parent->core.live_count >= 0 &&
2385 parent->core.live_count <= count);
2388 * If no free blockref could be found we must create an indirect
2389 * block and move a number of blockrefs into it. With the parent
2390 * locked we can safely lock each child in order to delete+duplicate
2391 * it without causing a deadlock.
2393 * This may return the new indirect block or the old parent depending
2394 * on where the key falls. NULL is returned on error.
2396 if (parent->core.live_count == count) {
2397 hammer2_chain_t *nparent;
2399 nparent = hammer2_chain_create_indirect(parent, key, keybits,
2400 mtid, type, &error);
2401 if (nparent == NULL) {
2403 hammer2_chain_drop(chain);
2407 if (parent != nparent) {
2408 hammer2_chain_unlock(parent);
2409 hammer2_chain_drop(parent);
2410 parent = *parentp = nparent;
2416 * Link the chain into its parent.
2418 if (chain->parent != NULL)
2419 panic("hammer2: hammer2_chain_create: chain already connected");
2420 KKASSERT(chain->parent == NULL);
2421 hammer2_chain_insert(parent, chain,
2422 HAMMER2_CHAIN_INSERT_SPIN |
2423 HAMMER2_CHAIN_INSERT_LIVE,
2428 * Mark the newly created chain modified. This will cause
2429 * UPDATE to be set and process the INITIAL flag.
2431 * Device buffers are not instantiated for DATA elements
2432 * as these are handled by logical buffers.
2434 * Indirect and freemap node indirect blocks are handled
2435 * by hammer2_chain_create_indirect() and not by this
2438 * Data for all other bref types is expected to be
2439 * instantiated (INODE, LEAF).
2441 switch(chain->bref.type) {
2442 case HAMMER2_BREF_TYPE_DATA:
2443 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2444 case HAMMER2_BREF_TYPE_INODE:
2445 hammer2_chain_modify(chain, mtid,
2446 HAMMER2_MODIFY_OPTDATA);
2450 * Remaining types are not supported by this function.
2451 * In particular, INDIRECT and LEAF_NODE types are
2452 * handled by create_indirect().
2454 panic("hammer2_chain_create: bad type: %d",
2461 * When reconnecting a chain we must set UPDATE and
2462 * setflush so the flush recognizes that it must update
2463 * the bref in the parent.
2465 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
2466 hammer2_chain_ref(chain);
2467 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
2472 * We must setflush(parent) to ensure that it recurses through to
2473 * chain. setflush(chain) might not work because ONFLUSH is possibly
2474 * already set in the chain (so it won't recurse up to set it in the
2477 hammer2_chain_setflush(parent);
2486 * Move the chain from its old parent to a new parent. The chain must have
2487 * already been deleted or already disconnected (or never associated) with
2488 * a parent. The chain is reassociated with the new parent and the deleted
2489 * flag will be cleared (no longer deleted). The chain's modification state
2492 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION
2493 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2494 * FULL. This typically means that the caller is creating the chain after
2495 * doing a hammer2_chain_lookup().
2497 * A non-NULL bref is typically passed when key and keybits must be overridden.
2498 * Note that hammer2_cluster_duplicate() *ONLY* uses the key and keybits fields
2499 * from a passed-in bref and uses the old chain's bref for everything else.
2501 * Neither (parent) or (chain) can be errored.
2503 * If (parent) is non-NULL then the new duplicated chain is inserted under
2506 * If (parent) is NULL then the newly duplicated chain is not inserted
2507 * anywhere, similar to if it had just been chain_alloc()'d (suitable for
2508 * passing into hammer2_chain_create() after this function returns).
2510 * WARNING! This function calls create which means it can insert indirect
2511 * blocks. This can cause other unrelated chains in the parent to
2512 * be moved to a newly inserted indirect block in addition to the
2516 hammer2_chain_rename(hammer2_blockref_t *bref,
2517 hammer2_chain_t **parentp, hammer2_chain_t *chain,
2518 hammer2_tid_t mtid, int flags)
2521 hammer2_chain_t *parent;
2525 * WARNING! We should never resolve DATA to device buffers
2526 * (XXX allow it if the caller did?), and since
2527 * we currently do not have the logical buffer cache
2528 * buffer in-hand to fix its cached physical offset
2529 * we also force the modify code to not COW it. XXX
2532 KKASSERT(chain->parent == NULL);
2533 KKASSERT(chain->error == 0);
2536 * Now create a duplicate of the chain structure, associating
2537 * it with the same core, making it the same size, pointing it
2538 * to the same bref (the same media block).
2541 bref = &chain->bref;
2542 bytes = (hammer2_off_t)1 <<
2543 (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
2546 * If parent is not NULL the duplicated chain will be entered under
2547 * the parent and the UPDATE bit set to tell flush to update
2550 * We must setflush(parent) to ensure that it recurses through to
2551 * chain. setflush(chain) might not work because ONFLUSH is possibly
2552 * already set in the chain (so it won't recurse up to set it in the
2555 * Having both chains locked is extremely important for atomicy.
2557 if (parentp && (parent = *parentp) != NULL) {
2558 KKASSERT(hammer2_mtx_owned(&parent->lock));
2559 KKASSERT(parent->refs > 0);
2560 KKASSERT(parent->error == 0);
2562 hammer2_chain_create(parentp, &chain, chain->pmp,
2563 bref->key, bref->keybits, bref->type,
2564 chain->bytes, mtid, flags);
2565 KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE);
2566 hammer2_chain_setflush(*parentp);
2571 * Helper function for deleting chains.
2573 * The chain is removed from the live view (the RBTREE) as well as the parent's
2574 * blockmap. Both chain and its parent must be locked.
2576 * parent may not be errored. chain can be errored.
2579 _hammer2_chain_delete_helper(hammer2_chain_t *parent, hammer2_chain_t *chain,
2580 hammer2_tid_t mtid, int flags)
2584 KKASSERT((chain->flags & (HAMMER2_CHAIN_DELETED |
2585 HAMMER2_CHAIN_FICTITIOUS)) == 0);
2586 KKASSERT(chain->parent == parent);
2589 if (chain->flags & HAMMER2_CHAIN_BMAPPED) {
2591 * Chain is blockmapped, so there must be a parent.
2592 * Atomically remove the chain from the parent and remove
2593 * the blockmap entry.
2595 hammer2_blockref_t *base;
2598 KKASSERT(parent != NULL);
2599 KKASSERT(parent->error == 0);
2600 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
2601 hammer2_chain_modify(parent, mtid, HAMMER2_MODIFY_OPTDATA);
2604 * Calculate blockmap pointer
2606 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
2607 hammer2_spin_ex(&parent->core.spin);
2609 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2610 atomic_add_int(&parent->core.live_count, -1);
2611 ++parent->core.generation;
2612 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2613 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2614 --parent->core.chain_count;
2615 chain->parent = NULL;
2617 switch(parent->bref.type) {
2618 case HAMMER2_BREF_TYPE_INODE:
2620 * Access the inode's block array. However, there
2621 * is no block array if the inode is flagged
2622 * DIRECTDATA. The DIRECTDATA case typicaly only
2623 * occurs when a hardlink has been shifted up the
2624 * tree and the original inode gets replaced with
2625 * an OBJTYPE_HARDLINK placeholding inode.
2628 (parent->data->ipdata.meta.op_flags &
2629 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
2631 &parent->data->ipdata.u.blockset.blockref[0];
2635 count = HAMMER2_SET_COUNT;
2637 case HAMMER2_BREF_TYPE_INDIRECT:
2638 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2640 base = &parent->data->npdata[0];
2643 count = parent->bytes / sizeof(hammer2_blockref_t);
2645 case HAMMER2_BREF_TYPE_VOLUME:
2646 base = &hmp->voldata.sroot_blockset.blockref[0];
2647 count = HAMMER2_SET_COUNT;
2649 case HAMMER2_BREF_TYPE_FREEMAP:
2650 base = &parent->data->npdata[0];
2651 count = HAMMER2_SET_COUNT;
2656 panic("hammer2_flush_pass2: "
2657 "unrecognized blockref type: %d",
2662 * delete blockmapped chain from its parent.
2664 * The parent is not affected by any statistics in chain
2665 * which are pending synchronization. That is, there is
2666 * nothing to undo in the parent since they have not yet
2667 * been incorporated into the parent.
2669 * The parent is affected by statistics stored in inodes.
2670 * Those have already been synchronized, so they must be
2671 * undone. XXX split update possible w/delete in middle?
2674 int cache_index = -1;
2675 hammer2_base_delete(parent, base, count,
2676 &cache_index, chain);
2678 hammer2_spin_unex(&parent->core.spin);
2679 } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
2681 * Chain is not blockmapped but a parent is present.
2682 * Atomically remove the chain from the parent. There is
2683 * no blockmap entry to remove.
2685 * Because chain was associated with a parent but not
2686 * synchronized, the chain's *_count_up fields contain
2687 * inode adjustment statistics which must be undone.
2689 hammer2_spin_ex(&parent->core.spin);
2690 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2691 atomic_add_int(&parent->core.live_count, -1);
2692 ++parent->core.generation;
2693 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2694 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2695 --parent->core.chain_count;
2696 chain->parent = NULL;
2697 hammer2_spin_unex(&parent->core.spin);
2700 * Chain is not blockmapped and has no parent. This
2701 * is a degenerate case.
2703 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2708 * Create an indirect block that covers one or more of the elements in the
2709 * current parent. Either returns the existing parent with no locking or
2710 * ref changes or returns the new indirect block locked and referenced
2711 * and leaving the original parent lock/ref intact as well.
2713 * If an error occurs, NULL is returned and *errorp is set to the error.
2715 * The returned chain depends on where the specified key falls.
2717 * The key/keybits for the indirect mode only needs to follow three rules:
2719 * (1) That all elements underneath it fit within its key space and
2721 * (2) That all elements outside it are outside its key space.
2723 * (3) When creating the new indirect block any elements in the current
2724 * parent that fit within the new indirect block's keyspace must be
2725 * moved into the new indirect block.
2727 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
2728 * keyspace the the current parent, but lookup/iteration rules will
2729 * ensure (and must ensure) that rule (2) for all parents leading up
2730 * to the nearest inode or the root volume header is adhered to. This
2731 * is accomplished by always recursing through matching keyspaces in
2732 * the hammer2_chain_lookup() and hammer2_chain_next() API.
2734 * The current implementation calculates the current worst-case keyspace by
2735 * iterating the current parent and then divides it into two halves, choosing
2736 * whichever half has the most elements (not necessarily the half containing
2737 * the requested key).
2739 * We can also opt to use the half with the least number of elements. This
2740 * causes lower-numbered keys (aka logical file offsets) to recurse through
2741 * fewer indirect blocks and higher-numbered keys to recurse through more.
2742 * This also has the risk of not moving enough elements to the new indirect
2743 * block and being forced to create several indirect blocks before the element
2746 * Must be called with an exclusively locked parent.
2748 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
2749 hammer2_key_t *keyp, int keybits,
2750 hammer2_blockref_t *base, int count);
2751 static int hammer2_chain_indkey_normal(hammer2_chain_t *parent,
2752 hammer2_key_t *keyp, int keybits,
2753 hammer2_blockref_t *base, int count);
2756 hammer2_chain_create_indirect(hammer2_chain_t *parent,
2757 hammer2_key_t create_key, int create_bits,
2758 hammer2_tid_t mtid, int for_type, int *errorp)
2761 hammer2_blockref_t *base;
2762 hammer2_blockref_t *bref;
2763 hammer2_blockref_t bcopy;
2764 hammer2_chain_t *chain;
2765 hammer2_chain_t *ichain;
2766 hammer2_chain_t dummy;
2767 hammer2_key_t key = create_key;
2768 hammer2_key_t key_beg;
2769 hammer2_key_t key_end;
2770 hammer2_key_t key_next;
2771 int keybits = create_bits;
2778 int maxloops = 300000;
2781 * Calculate the base blockref pointer or NULL if the chain
2782 * is known to be empty. We need to calculate the array count
2783 * for RB lookups either way.
2787 KKASSERT(hammer2_mtx_owned(&parent->lock));
2789 /*hammer2_chain_modify(&parent, HAMMER2_MODIFY_OPTDATA);*/
2790 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2793 switch(parent->bref.type) {
2794 case HAMMER2_BREF_TYPE_INODE:
2795 count = HAMMER2_SET_COUNT;
2797 case HAMMER2_BREF_TYPE_INDIRECT:
2798 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2799 count = parent->bytes / sizeof(hammer2_blockref_t);
2801 case HAMMER2_BREF_TYPE_VOLUME:
2802 count = HAMMER2_SET_COUNT;
2804 case HAMMER2_BREF_TYPE_FREEMAP:
2805 count = HAMMER2_SET_COUNT;
2808 panic("hammer2_chain_create_indirect: "
2809 "unrecognized blockref type: %d",
2815 switch(parent->bref.type) {
2816 case HAMMER2_BREF_TYPE_INODE:
2817 base = &parent->data->ipdata.u.blockset.blockref[0];
2818 count = HAMMER2_SET_COUNT;
2820 case HAMMER2_BREF_TYPE_INDIRECT:
2821 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2822 base = &parent->data->npdata[0];
2823 count = parent->bytes / sizeof(hammer2_blockref_t);
2825 case HAMMER2_BREF_TYPE_VOLUME:
2826 base = &hmp->voldata.sroot_blockset.blockref[0];
2827 count = HAMMER2_SET_COUNT;
2829 case HAMMER2_BREF_TYPE_FREEMAP:
2830 base = &hmp->voldata.freemap_blockset.blockref[0];
2831 count = HAMMER2_SET_COUNT;
2834 panic("hammer2_chain_create_indirect: "
2835 "unrecognized blockref type: %d",
2843 * dummy used in later chain allocation (no longer used for lookups).
2845 bzero(&dummy, sizeof(dummy));
2848 * When creating an indirect block for a freemap node or leaf
2849 * the key/keybits must be fitted to static radix levels because
2850 * particular radix levels use particular reserved blocks in the
2853 * This routine calculates the key/radix of the indirect block
2854 * we need to create, and whether it is on the high-side or the
2857 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2858 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2859 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
2862 keybits = hammer2_chain_indkey_normal(parent, &key, keybits,
2867 * Normalize the key for the radix being represented, keeping the
2868 * high bits and throwing away the low bits.
2870 key &= ~(((hammer2_key_t)1 << keybits) - 1);
2873 * How big should our new indirect block be? It has to be at least
2874 * as large as its parent.
2876 * The freemap uses a specific indirect block size.
2878 * The first indirect block level down from an inode typically
2879 * uses LBUFSIZE (16384), else it uses PBUFSIZE (65536).
2881 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2882 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2883 nbytes = HAMMER2_FREEMAP_LEVELN_PSIZE;
2884 } else if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
2885 nbytes = HAMMER2_IND_BYTES_MIN;
2887 nbytes = HAMMER2_IND_BYTES_MAX;
2889 if (nbytes < count * sizeof(hammer2_blockref_t)) {
2890 KKASSERT(for_type != HAMMER2_BREF_TYPE_FREEMAP_NODE &&
2891 for_type != HAMMER2_BREF_TYPE_FREEMAP_LEAF);
2892 nbytes = count * sizeof(hammer2_blockref_t);
2896 * Ok, create our new indirect block
2898 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2899 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2900 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
2902 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
2904 dummy.bref.key = key;
2905 dummy.bref.keybits = keybits;
2906 dummy.bref.data_off = hammer2_getradix(nbytes);
2907 dummy.bref.methods = parent->bref.methods;
2909 ichain = hammer2_chain_alloc(hmp, parent->pmp, &dummy.bref);
2910 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
2911 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
2912 /* ichain has one ref at this point */
2915 * We have to mark it modified to allocate its block, but use
2916 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
2917 * it won't be acted upon by the flush code.
2919 hammer2_chain_modify(ichain, mtid, HAMMER2_MODIFY_OPTDATA);
2922 * Iterate the original parent and move the matching brefs into
2923 * the new indirect block.
2925 * XXX handle flushes.
2928 key_end = HAMMER2_KEY_MAX;
2930 hammer2_spin_ex(&parent->core.spin);
2935 if (++loops > 100000) {
2936 hammer2_spin_unex(&parent->core.spin);
2937 panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
2938 reason, parent, base, count, key_next);
2942 * NOTE: spinlock stays intact, returned chain (if not NULL)
2943 * is not referenced or locked which means that we
2944 * cannot safely check its flagged / deletion status
2947 chain = hammer2_combined_find(parent, base, count,
2948 &cache_index, &key_next,
2951 generation = parent->core.generation;
2954 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
2957 * Skip keys that are not within the key/radix of the new
2958 * indirect block. They stay in the parent.
2960 if ((~(((hammer2_key_t)1 << keybits) - 1) &
2961 (key ^ bref->key)) != 0) {
2962 goto next_key_spinlocked;
2966 * Load the new indirect block by acquiring the related
2967 * chains (potentially from media as it might not be
2968 * in-memory). Then move it to the new parent (ichain)
2969 * via DELETE-DUPLICATE.
2971 * chain is referenced but not locked. We must lock the
2972 * chain to obtain definitive DUPLICATED/DELETED state
2976 * Use chain already present in the RBTREE
2978 hammer2_chain_ref(chain);
2979 hammer2_spin_unex(&parent->core.spin);
2980 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
2983 * Get chain for blockref element. _get returns NULL
2984 * on insertion race.
2987 hammer2_spin_unex(&parent->core.spin);
2988 chain = hammer2_chain_get(parent, generation, &bcopy);
2989 if (chain == NULL) {
2991 hammer2_spin_ex(&parent->core.spin);
2994 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2995 kprintf("REASON 2\n");
2997 hammer2_chain_drop(chain);
2998 hammer2_spin_ex(&parent->core.spin);
3001 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
3005 * This is always live so if the chain has been deleted
3006 * we raced someone and we have to retry.
3008 * NOTE: Lookups can race delete-duplicate because
3009 * delete-duplicate does not lock the parent's core
3010 * (they just use the spinlock on the core). We must
3011 * check for races by comparing the DUPLICATED flag before
3012 * releasing the spinlock with the flag after locking the
3015 * (note reversed logic for this one)
3017 if (chain->flags & HAMMER2_CHAIN_DELETED) {
3018 hammer2_chain_unlock(chain);
3019 hammer2_chain_drop(chain);
3024 * Shift the chain to the indirect block.
3026 * WARNING! No reason for us to load chain data, pass NOSTATS
3027 * to prevent delete/insert from trying to access
3028 * inode stats (and thus asserting if there is no
3029 * chain->data loaded).
3031 hammer2_chain_delete(parent, chain, mtid, 0);
3032 hammer2_chain_rename(NULL, &ichain, chain, mtid, 0);
3033 hammer2_chain_unlock(chain);
3034 hammer2_chain_drop(chain);
3035 KKASSERT(parent->refs > 0);
3038 hammer2_spin_ex(&parent->core.spin);
3039 next_key_spinlocked:
3040 if (--maxloops == 0)
3041 panic("hammer2_chain_create_indirect: maxloops");
3043 if (key_next == 0 || key_next > key_end)
3048 hammer2_spin_unex(&parent->core.spin);
3051 * Insert the new indirect block into the parent now that we've
3052 * cleared out some entries in the parent. We calculated a good
3053 * insertion index in the loop above (ichain->index).
3055 * We don't have to set UPDATE here because we mark ichain
3056 * modified down below (so the normal modified -> flush -> set-moved
3057 * sequence applies).
3059 * The insertion shouldn't race as this is a completely new block
3060 * and the parent is locked.
3062 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
3063 hammer2_chain_insert(parent, ichain,
3064 HAMMER2_CHAIN_INSERT_SPIN |
3065 HAMMER2_CHAIN_INSERT_LIVE,
3069 * Make sure flushes propogate after our manual insertion.
3071 hammer2_chain_setflush(ichain);
3072 hammer2_chain_setflush(parent);
3075 * Figure out what to return.
3077 if (~(((hammer2_key_t)1 << keybits) - 1) &
3078 (create_key ^ key)) {
3080 * Key being created is outside the key range,
3081 * return the original parent.
3083 hammer2_chain_unlock(ichain);
3084 hammer2_chain_drop(ichain);
3087 * Otherwise its in the range, return the new parent.
3088 * (leave both the new and old parent locked).
3097 * Calculate the keybits and highside/lowside of the freemap node the
3098 * caller is creating.
3100 * This routine will specify the next higher-level freemap key/radix
3101 * representing the lowest-ordered set. By doing so, eventually all
3102 * low-ordered sets will be moved one level down.
3104 * We have to be careful here because the freemap reserves a limited
3105 * number of blocks for a limited number of levels. So we can't just
3106 * push indiscriminately.
3109 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
3110 int keybits, hammer2_blockref_t *base, int count)
3112 hammer2_chain_t *chain;
3113 hammer2_blockref_t *bref;
3115 hammer2_key_t key_beg;
3116 hammer2_key_t key_end;
3117 hammer2_key_t key_next;
3121 int maxloops = 300000;
3129 * Calculate the range of keys in the array being careful to skip
3130 * slots which are overridden with a deletion.
3133 key_end = HAMMER2_KEY_MAX;
3135 hammer2_spin_ex(&parent->core.spin);
3138 if (--maxloops == 0) {
3139 panic("indkey_freemap shit %p %p:%d\n",
3140 parent, base, count);
3142 chain = hammer2_combined_find(parent, base, count,
3143 &cache_index, &key_next,
3154 * Skip deleted chains.
3156 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3157 if (key_next == 0 || key_next > key_end)
3164 * Use the full live (not deleted) element for the scan
3165 * iteration. HAMMER2 does not allow partial replacements.
3167 * XXX should be built into hammer2_combined_find().
3169 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3171 if (keybits > bref->keybits) {
3173 keybits = bref->keybits;
3174 } else if (keybits == bref->keybits && bref->key < key) {
3181 hammer2_spin_unex(&parent->core.spin);
3184 * Return the keybits for a higher-level FREEMAP_NODE covering
3188 case HAMMER2_FREEMAP_LEVEL0_RADIX:
3189 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
3191 case HAMMER2_FREEMAP_LEVEL1_RADIX:
3192 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
3194 case HAMMER2_FREEMAP_LEVEL2_RADIX:
3195 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
3197 case HAMMER2_FREEMAP_LEVEL3_RADIX:
3198 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
3200 case HAMMER2_FREEMAP_LEVEL4_RADIX:
3201 keybits = HAMMER2_FREEMAP_LEVEL5_RADIX;
3203 case HAMMER2_FREEMAP_LEVEL5_RADIX:
3204 panic("hammer2_chain_indkey_freemap: level too high");
3207 panic("hammer2_chain_indkey_freemap: bad radix");
3216 * Calculate the keybits and highside/lowside of the indirect block the
3217 * caller is creating.
3220 hammer2_chain_indkey_normal(hammer2_chain_t *parent, hammer2_key_t *keyp,
3221 int keybits, hammer2_blockref_t *base, int count)
3223 hammer2_blockref_t *bref;
3224 hammer2_chain_t *chain;
3225 hammer2_key_t key_beg;
3226 hammer2_key_t key_end;
3227 hammer2_key_t key_next;
3233 int maxloops = 300000;
3240 * Calculate the range of keys in the array being careful to skip
3241 * slots which are overridden with a deletion. Once the scan
3242 * completes we will cut the key range in half and shift half the
3243 * range into the new indirect block.
3246 key_end = HAMMER2_KEY_MAX;
3248 hammer2_spin_ex(&parent->core.spin);
3251 if (--maxloops == 0) {
3252 panic("indkey_freemap shit %p %p:%d\n",
3253 parent, base, count);
3255 chain = hammer2_combined_find(parent, base, count,
3256 &cache_index, &key_next,
3267 * NOTE: No need to check DUPLICATED here because we do
3268 * not release the spinlock.
3270 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3271 if (key_next == 0 || key_next > key_end)
3278 * Use the full live (not deleted) element for the scan
3279 * iteration. HAMMER2 does not allow partial replacements.
3281 * XXX should be built into hammer2_combined_find().
3283 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3286 * Expand our calculated key range (key, keybits) to fit
3287 * the scanned key. nkeybits represents the full range
3288 * that we will later cut in half (two halves @ nkeybits - 1).
3291 if (nkeybits < bref->keybits) {
3292 if (bref->keybits > 64) {
3293 kprintf("bad bref chain %p bref %p\n",
3297 nkeybits = bref->keybits;
3299 while (nkeybits < 64 &&
3300 (~(((hammer2_key_t)1 << nkeybits) - 1) &
3301 (key ^ bref->key)) != 0) {
3306 * If the new key range is larger we have to determine
3307 * which side of the new key range the existing keys fall
3308 * under by checking the high bit, then collapsing the
3309 * locount into the hicount or vise-versa.
3311 if (keybits != nkeybits) {
3312 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
3323 * The newly scanned key will be in the lower half or the
3324 * upper half of the (new) key range.
3326 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
3335 hammer2_spin_unex(&parent->core.spin);
3336 bref = NULL; /* now invalid (safety) */
3339 * Adjust keybits to represent half of the full range calculated
3340 * above (radix 63 max)
3345 * Select whichever half contains the most elements. Theoretically
3346 * we can select either side as long as it contains at least one
3347 * element (in order to ensure that a free slot is present to hold
3348 * the indirect block).
3350 if (hammer2_indirect_optimize) {
3352 * Insert node for least number of keys, this will arrange
3353 * the first few blocks of a large file or the first few
3354 * inodes in a directory with fewer indirect blocks when
3357 if (hicount < locount && hicount != 0)
3358 key |= (hammer2_key_t)1 << keybits;
3360 key &= ~(hammer2_key_t)1 << keybits;
3363 * Insert node for most number of keys, best for heavily
3366 if (hicount > locount)
3367 key |= (hammer2_key_t)1 << keybits;
3369 key &= ~(hammer2_key_t)1 << keybits;
3377 * Sets CHAIN_DELETED and remove the chain's blockref from the parent if
3380 * Both parent and chain must be locked exclusively.
3382 * This function will modify the parent if the blockref requires removal
3383 * from the parent's block table.
3385 * This function is NOT recursive. Any entity already pushed into the
3386 * chain (such as an inode) may still need visibility into its contents,
3387 * as well as the ability to read and modify the contents. For example,
3388 * for an unlinked file which is still open.
3391 hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
3392 hammer2_tid_t mtid, int flags)
3394 KKASSERT(hammer2_mtx_owned(&chain->lock));
3397 * Nothing to do if already marked.
3399 * We need the spinlock on the core whos RBTREE contains chain
3400 * to protect against races.
3402 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
3403 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
3404 chain->parent == parent);
3405 _hammer2_chain_delete_helper(parent, chain, mtid, flags);
3409 * To avoid losing track of a permanent deletion we add the chain
3410 * to the delayed flush queue. If were to flush it right now the
3411 * parent would end up in a modified state and generate I/O.
3412 * The delayed queue gives the parent a chance to be deleted to
3415 if (flags & HAMMER2_DELETE_PERMANENT) {
3416 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
3417 hammer2_delayed_flush(chain);
3419 /* XXX might not be needed */
3420 hammer2_chain_setflush(chain);
3425 * Returns the index of the nearest element in the blockref array >= elm.
3426 * Returns (count) if no element could be found.
3428 * Sets *key_nextp to the next key for loop purposes but does not modify
3429 * it if the next key would be higher than the current value of *key_nextp.
3430 * Note that *key_nexp can overflow to 0, which should be tested by the
3433 * (*cache_indexp) is a heuristic and can be any value without effecting
3436 * WARNING! Must be called with parent's spinlock held. Spinlock remains
3437 * held through the operation.
3440 hammer2_base_find(hammer2_chain_t *parent,
3441 hammer2_blockref_t *base, int count,
3442 int *cache_indexp, hammer2_key_t *key_nextp,
3443 hammer2_key_t key_beg, hammer2_key_t key_end)
3445 hammer2_blockref_t *scan;
3446 hammer2_key_t scan_end;
3451 * Require the live chain's already have their core's counted
3452 * so we can optimize operations.
3454 KKASSERT(parent->flags & HAMMER2_CHAIN_COUNTEDBREFS);
3459 if (count == 0 || base == NULL)
3463 * Sequential optimization using *cache_indexp. This is the most
3466 * We can avoid trailing empty entries on live chains, otherwise
3467 * we might have to check the whole block array.
3471 limit = parent->core.live_zero;
3476 KKASSERT(i < count);
3482 while (i > 0 && (scan->type == 0 || scan->key > key_beg)) {
3489 * Search forwards, stop when we find a scan element which
3490 * encloses the key or until we know that there are no further
3494 if (scan->type != 0) {
3495 scan_end = scan->key +
3496 ((hammer2_key_t)1 << scan->keybits) - 1;
3497 if (scan->key > key_beg || scan_end >= key_beg)
3510 scan_end = scan->key +
3511 ((hammer2_key_t)1 << scan->keybits);
3512 if (scan_end && (*key_nextp > scan_end ||
3514 *key_nextp = scan_end;
3522 * Do a combined search and return the next match either from the blockref
3523 * array or from the in-memory chain. Sets *bresp to the returned bref in
3524 * both cases, or sets it to NULL if the search exhausted. Only returns
3525 * a non-NULL chain if the search matched from the in-memory chain.
3527 * When no in-memory chain has been found and a non-NULL bref is returned
3531 * The returned chain is not locked or referenced. Use the returned bref
3532 * to determine if the search exhausted or not. Iterate if the base find
3533 * is chosen but matches a deleted chain.
3535 * WARNING! Must be called with parent's spinlock held. Spinlock remains
3536 * held through the operation.
3538 static hammer2_chain_t *
3539 hammer2_combined_find(hammer2_chain_t *parent,
3540 hammer2_blockref_t *base, int count,
3541 int *cache_indexp, hammer2_key_t *key_nextp,
3542 hammer2_key_t key_beg, hammer2_key_t key_end,
3543 hammer2_blockref_t **bresp)
3545 hammer2_blockref_t *bref;
3546 hammer2_chain_t *chain;
3550 * Lookup in block array and in rbtree.
3552 *key_nextp = key_end + 1;
3553 i = hammer2_base_find(parent, base, count, cache_indexp,
3554 key_nextp, key_beg, key_end);
3555 chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
3560 if (i == count && chain == NULL) {
3566 * Only chain matched.
3569 bref = &chain->bref;
3574 * Only blockref matched.
3576 if (chain == NULL) {
3582 * Both in-memory and blockref matched, select the nearer element.
3584 * If both are flush with the left-hand side or both are the
3585 * same distance away, select the chain. In this situation the
3586 * chain must have been loaded from the matching blockmap.
3588 if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
3589 chain->bref.key == base[i].key) {
3590 KKASSERT(chain->bref.key == base[i].key);
3591 bref = &chain->bref;
3596 * Select the nearer key
3598 if (chain->bref.key < base[i].key) {
3599 bref = &chain->bref;
3606 * If the bref is out of bounds we've exhausted our search.
3609 if (bref->key > key_end) {
3619 * Locate the specified block array element and delete it. The element
3622 * The spin lock on the related chain must be held.
3624 * NOTE: live_count was adjusted when the chain was deleted, so it does not
3625 * need to be adjusted when we commit the media change.
3628 hammer2_base_delete(hammer2_chain_t *parent,
3629 hammer2_blockref_t *base, int count,
3630 int *cache_indexp, hammer2_chain_t *chain)
3632 hammer2_blockref_t *elm = &chain->bref;
3633 hammer2_key_t key_next;
3637 * Delete element. Expect the element to exist.
3639 * XXX see caller, flush code not yet sophisticated enough to prevent
3640 * re-flushed in some cases.
3642 key_next = 0; /* max range */
3643 i = hammer2_base_find(parent, base, count, cache_indexp,
3644 &key_next, elm->key, elm->key);
3645 if (i == count || base[i].type == 0 ||
3646 base[i].key != elm->key ||
3647 ((chain->flags & HAMMER2_CHAIN_BMAPUPD) == 0 &&
3648 base[i].keybits != elm->keybits)) {
3649 hammer2_spin_unex(&parent->core.spin);
3650 panic("delete base %p element not found at %d/%d elm %p\n",
3651 base, i, count, elm);
3656 * Update stats and zero the entry
3658 parent->bref.data_count -= base[i].data_count;
3659 parent->bref.data_count -= (hammer2_off_t)1 <<
3660 (int)(base[i].data_off & HAMMER2_OFF_MASK_RADIX);
3661 parent->bref.inode_count -= base[i].inode_count;
3662 if (base[i].type == HAMMER2_BREF_TYPE_INODE)
3663 parent->bref.inode_count -= 1;
3665 bzero(&base[i], sizeof(*base));
3668 * We can only optimize parent->core.live_zero for live chains.
3670 if (parent->core.live_zero == i + 1) {
3671 while (--i >= 0 && base[i].type == 0)
3673 parent->core.live_zero = i + 1;
3677 * Clear appropriate blockmap flags in chain.
3679 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED |
3680 HAMMER2_CHAIN_BMAPUPD);
3684 * Insert the specified element. The block array must not already have the
3685 * element and must have space available for the insertion.
3687 * The spin lock on the related chain must be held.
3689 * NOTE: live_count was adjusted when the chain was deleted, so it does not
3690 * need to be adjusted when we commit the media change.
3693 hammer2_base_insert(hammer2_chain_t *parent,
3694 hammer2_blockref_t *base, int count,
3695 int *cache_indexp, hammer2_chain_t *chain)
3697 hammer2_blockref_t *elm = &chain->bref;
3698 hammer2_key_t key_next;
3707 * Insert new element. Expect the element to not already exist
3708 * unless we are replacing it.
3710 * XXX see caller, flush code not yet sophisticated enough to prevent
3711 * re-flushed in some cases.
3713 key_next = 0; /* max range */
3714 i = hammer2_base_find(parent, base, count, cache_indexp,
3715 &key_next, elm->key, elm->key);
3718 * Shortcut fill optimization, typical ordered insertion(s) may not
3721 KKASSERT(i >= 0 && i <= count);
3724 * Set appropriate blockmap flags in chain.
3726 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
3729 * Update stats and zero the entry
3731 parent->bref.data_count += elm->data_count;
3732 parent->bref.data_count += (hammer2_off_t)1 <<
3733 (int)(elm->data_off & HAMMER2_OFF_MASK_RADIX);
3734 parent->bref.inode_count += elm->inode_count;
3735 if (elm->type == HAMMER2_BREF_TYPE_INODE)
3736 parent->bref.inode_count += 1;
3740 * We can only optimize parent->core.live_zero for live chains.
3742 if (i == count && parent->core.live_zero < count) {
3743 i = parent->core.live_zero++;
3748 xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
3749 if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
3750 hammer2_spin_unex(&parent->core.spin);
3751 panic("insert base %p overlapping elements at %d elm %p\n",
3756 * Try to find an empty slot before or after.
3760 while (j > 0 || k < count) {
3762 if (j >= 0 && base[j].type == 0) {
3766 bcopy(&base[j+1], &base[j],
3767 (i - j - 1) * sizeof(*base));
3773 if (k < count && base[k].type == 0) {
3774 bcopy(&base[i], &base[i+1],
3775 (k - i) * sizeof(hammer2_blockref_t));
3779 * We can only update parent->core.live_zero for live
3782 if (parent->core.live_zero <= k)
3783 parent->core.live_zero = k + 1;
3788 panic("hammer2_base_insert: no room!");
3795 for (l = 0; l < count; ++l) {
3797 key_next = base[l].key +
3798 ((hammer2_key_t)1 << base[l].keybits) - 1;
3802 while (++l < count) {
3804 if (base[l].key <= key_next)
3805 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
3806 key_next = base[l].key +
3807 ((hammer2_key_t)1 << base[l].keybits) - 1;
3817 * Sort the blockref array for the chain. Used by the flush code to
3818 * sort the blockref[] array.
3820 * The chain must be exclusively locked AND spin-locked.
3822 typedef hammer2_blockref_t *hammer2_blockref_p;
3826 hammer2_base_sort_callback(const void *v1, const void *v2)
3828 hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
3829 hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
3832 * Make sure empty elements are placed at the end of the array
3834 if (bref1->type == 0) {
3835 if (bref2->type == 0)
3838 } else if (bref2->type == 0) {
3845 if (bref1->key < bref2->key)
3847 if (bref1->key > bref2->key)
3853 hammer2_base_sort(hammer2_chain_t *chain)
3855 hammer2_blockref_t *base;
3858 switch(chain->bref.type) {
3859 case HAMMER2_BREF_TYPE_INODE:
3861 * Special shortcut for embedded data returns the inode
3862 * itself. Callers must detect this condition and access
3863 * the embedded data (the strategy code does this for us).
3865 * This is only applicable to regular files and softlinks.
3867 if (chain->data->ipdata.meta.op_flags &
3868 HAMMER2_OPFLAG_DIRECTDATA) {
3871 base = &chain->data->ipdata.u.blockset.blockref[0];
3872 count = HAMMER2_SET_COUNT;
3874 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3875 case HAMMER2_BREF_TYPE_INDIRECT:
3877 * Optimize indirect blocks in the INITIAL state to avoid
3880 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
3881 base = &chain->data->npdata[0];
3882 count = chain->bytes / sizeof(hammer2_blockref_t);
3884 case HAMMER2_BREF_TYPE_VOLUME:
3885 base = &chain->hmp->voldata.sroot_blockset.blockref[0];
3886 count = HAMMER2_SET_COUNT;
3888 case HAMMER2_BREF_TYPE_FREEMAP:
3889 base = &chain->hmp->voldata.freemap_blockset.blockref[0];
3890 count = HAMMER2_SET_COUNT;
3893 kprintf("hammer2_chain_lookup: unrecognized "
3894 "blockref(A) type: %d",
3897 tsleep(&base, 0, "dead", 0);
3898 panic("hammer2_chain_lookup: unrecognized "
3899 "blockref(A) type: %d",
3901 base = NULL; /* safety */
3902 count = 0; /* safety */
3904 kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
3910 * Chain memory management
3913 hammer2_chain_wait(hammer2_chain_t *chain)
3915 tsleep(chain, 0, "chnflw", 1);
3918 const hammer2_media_data_t *
3919 hammer2_chain_rdata(hammer2_chain_t *chain)
3921 KKASSERT(chain->data != NULL);
3922 return (chain->data);
3925 hammer2_media_data_t *
3926 hammer2_chain_wdata(hammer2_chain_t *chain)
3928 KKASSERT(chain->data != NULL);
3929 return (chain->data);
3933 * Set the check data for a chain. This can be a heavy-weight operation
3934 * and typically only runs on-flush. For file data check data is calculated
3935 * when the logical buffers are flushed.
3938 hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata)
3940 chain->bref.flags &= ~HAMMER2_BREF_FLAG_ZERO;
3942 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
3943 case HAMMER2_CHECK_NONE:
3945 case HAMMER2_CHECK_DISABLED:
3947 case HAMMER2_CHECK_ISCSI32:
3948 chain->bref.check.iscsi32.value =
3949 hammer2_icrc32(bdata, chain->bytes);
3951 case HAMMER2_CHECK_CRC64:
3952 chain->bref.check.crc64.value = 0;
3955 case HAMMER2_CHECK_SHA192:
3957 SHA256_CTX hash_ctx;
3959 uint8_t digest[SHA256_DIGEST_LENGTH];
3960 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
3963 SHA256_Init(&hash_ctx);
3964 SHA256_Update(&hash_ctx, bdata, chain->bytes);
3965 SHA256_Final(u.digest, &hash_ctx);
3966 u.digest64[2] ^= u.digest64[3];
3968 chain->bref.check.sha192.data,
3969 sizeof(chain->bref.check.sha192.data));
3972 case HAMMER2_CHECK_FREEMAP:
3973 chain->bref.check.freemap.icrc32 =
3974 hammer2_icrc32(bdata, chain->bytes);
3977 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
3978 chain->bref.methods);
3984 hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata)
3988 if (chain->bref.flags & HAMMER2_BREF_FLAG_ZERO)
3991 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
3992 case HAMMER2_CHECK_NONE:
3995 case HAMMER2_CHECK_DISABLED:
3998 case HAMMER2_CHECK_ISCSI32:
3999 r = (chain->bref.check.iscsi32.value ==
4000 hammer2_icrc32(bdata, chain->bytes));
4002 case HAMMER2_CHECK_CRC64:
4003 r = (chain->bref.check.crc64.value == 0);
4006 case HAMMER2_CHECK_SHA192:
4008 SHA256_CTX hash_ctx;
4010 uint8_t digest[SHA256_DIGEST_LENGTH];
4011 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
4014 SHA256_Init(&hash_ctx);
4015 SHA256_Update(&hash_ctx, bdata, chain->bytes);
4016 SHA256_Final(u.digest, &hash_ctx);
4017 u.digest64[2] ^= u.digest64[3];
4019 chain->bref.check.sha192.data,
4020 sizeof(chain->bref.check.sha192.data)) == 0) {
4027 case HAMMER2_CHECK_FREEMAP:
4028 r = (chain->bref.check.freemap.icrc32 ==
4029 hammer2_icrc32(bdata, chain->bytes));
4031 kprintf("freemap.icrc %08x icrc32 %08x (%d)\n",
4032 chain->bref.check.freemap.icrc32,
4033 hammer2_icrc32(bdata, chain->bytes), chain->bytes);
4035 kprintf("dio %p buf %016jx,%d bdata %p/%p\n",
4036 chain->dio, chain->dio->bp->b_loffset, chain->dio->bp->b_bufsize, bdata, chain->dio->bp->b_data);
4041 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
4042 chain->bref.methods);
4050 * The caller presents a shared-locked (parent, chain) where the chain
4051 * is of type HAMMER2_OBJTYPE_HARDLINK. The caller must hold the ip
4052 * structure representing the inode locked to prevent
4053 * consolidation/deconsolidation races.
4055 * We locate the hardlink in the current or a common parent directory.
4057 * If we are unable to locate the hardlink, EIO is returned and
4058 * (*chainp) is unlocked and dropped.
4061 hammer2_chain_hardlink_find(hammer2_inode_t *dip,
4062 hammer2_chain_t **parentp,
4063 hammer2_chain_t **chainp,
4066 hammer2_chain_t *parent;
4067 hammer2_chain_t *rchain;
4068 hammer2_key_t key_dummy;
4070 int cache_index = -1;
4073 * Obtain the key for the hardlink from *chainp.
4076 lhc = rchain->data->ipdata.meta.inum;
4077 hammer2_chain_unlock(rchain);
4078 hammer2_chain_drop(rchain);
4083 rchain = hammer2_chain_lookup(parentp, &key_dummy,
4085 &cache_index, flags);
4090 * Iterate parents, handle parent rename races by retrying
4098 parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
4101 if (parent->bref.flags & HAMMER2_BREF_FLAG_PFSROOT)
4103 if (parent->parent == NULL)
4105 parent = parent->parent;
4106 hammer2_chain_ref(parent);
4107 hammer2_chain_unlock(*parentp);
4108 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
4110 if ((*parentp)->parent == parent) {
4111 hammer2_chain_drop(*parentp);
4114 hammer2_chain_unlock(parent);
4115 hammer2_chain_drop(parent);
4116 hammer2_chain_lock(*parentp,
4117 HAMMER2_RESOLVE_ALWAYS |
4119 parent = NULL; /* safety */
4127 return (rchain ? EINVAL : 0);
4131 * Create a snapshot of the specified {parent, ochain} with the specified
4132 * label. The originating hammer2_inode must be exclusively locked for
4135 * The ioctl code has already synced the filesystem.
4138 hammer2_chain_snapshot(hammer2_chain_t *chain, hammer2_ioc_pfs_t *pmp,
4142 const hammer2_inode_data_t *ripdata;
4143 hammer2_inode_data_t *wipdata;
4144 hammer2_chain_t *nchain;
4145 hammer2_inode_t *nip;
4154 kprintf("snapshot %s\n", pmp->name);
4156 name_len = strlen(pmp->name);
4157 lhc = hammer2_dirhash(pmp->name, name_len);
4162 ripdata = &chain->data->ipdata;
4164 opfs_clid = ripdata->meta.pfs_clid;
4169 * Create the snapshot directory under the super-root
4171 * Set PFS type, generate a unique filesystem id, and generate
4172 * a cluster id. Use the same clid when snapshotting a PFS root,
4173 * which theoretically allows the snapshot to be used as part of
4174 * the same cluster (perhaps as a cache).
4176 * Copy the (flushed) blockref array. Theoretically we could use
4177 * chain_duplicate() but it becomes difficult to disentangle
4178 * the shared core so for now just brute-force it.
4183 nip = hammer2_inode_create(hmp->spmp->iroot, &vat, proc0.p_ucred,
4184 pmp->name, name_len, 0,
4186 HAMMER2_INSERT_PFSROOT, &error);
4189 hammer2_inode_modify(nip);
4190 nchain = hammer2_inode_chain(nip, 0, HAMMER2_RESOLVE_ALWAYS);
4191 hammer2_chain_modify(nchain, mtid, 0);
4192 wipdata = &nchain->data->ipdata;
4194 nip->meta.pfs_type = HAMMER2_PFSTYPE_MASTER;
4195 nip->meta.pfs_subtype = HAMMER2_PFSSUBTYPE_SNAPSHOT;
4196 nip->meta.op_flags |= HAMMER2_OPFLAG_PFSROOT;
4197 kern_uuidgen(&nip->meta.pfs_fsid, 1);
4200 * Give the snapshot its own private cluster id. As a
4201 * snapshot no further synchronization with the original
4202 * cluster will be done.
4205 if (chain->flags & HAMMER2_CHAIN_PFSBOUNDARY)
4206 nip->meta.pfs_clid = opfs_clid;
4208 kern_uuidgen(&nip->meta.pfs_clid, 1);
4210 kern_uuidgen(&nip->meta.pfs_clid, 1);
4211 nchain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
4213 /* XXX hack blockset copy */
4214 /* XXX doesn't work with real cluster */
4215 wipdata->meta = nip->meta;
4216 wipdata->u.blockset = ripdata->u.blockset;
4217 hammer2_flush(nchain, mtid, 1);
4218 hammer2_chain_unlock(nchain);
4219 hammer2_chain_drop(nchain);
4220 hammer2_inode_unlock(nip);