2 * Copyright (c) 2011-2013 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 * by 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 and hammer2_chain_core structures.
39 * Chains represent the filesystem media topology in-memory. Any given
40 * chain can represent an inode, indirect block, data, or other types
43 * This module provides APIs for direct and indirect block searches,
44 * iterations, recursions, creation, deletion, replication, and snapshot
45 * views (used by the flush and snapshot code).
47 * Generally speaking any modification made to a chain must propagate all
48 * the way back to the volume header, issuing copy-on-write updates to the
49 * blockref tables all the way up. Any chain except the volume header itself
50 * can be flushed to disk at any time, in any order. None of it matters
51 * until we get to the point where we want to synchronize the volume header
52 * (see the flush code).
54 * The chain structure supports snapshot views in time, which are primarily
55 * used until the related data and meta-data is flushed to allow the
56 * filesystem to make snapshots without requiring it to first flush,
57 * and to allow the filesystem flush and modify the filesystem concurrently
58 * with minimal or no stalls.
60 #include <sys/cdefs.h>
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/types.h>
65 #include <sys/kern_syscall.h>
70 static int hammer2_indirect_optimize; /* XXX SYSCTL */
72 static hammer2_chain_t *hammer2_chain_create_indirect(
73 hammer2_trans_t *trans, hammer2_chain_t *parent,
74 hammer2_key_t key, int keybits, int for_type, int *errorp);
75 static void adjreadcounter(hammer2_blockref_t *bref, size_t bytes);
78 * We use a red-black tree to guarantee safe lookups under shared locks.
80 * Chains can be overloaded onto the same index, creating a different
81 * view of a blockref table based on a transaction id. The RBTREE
82 * deconflicts the view by sub-sorting on delete_tid.
84 * NOTE: Any 'current' chain which is not yet deleted will have a
85 * delete_tid of HAMMER2_MAX_TID (0xFFF....FFFLLU).
87 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
90 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
92 if (chain1->index < chain2->index)
94 if (chain1->index > chain2->index)
96 if (chain1->delete_tid < chain2->delete_tid)
98 if (chain1->delete_tid > chain2->delete_tid)
105 hammer2_isclusterable(hammer2_chain_t *chain)
107 if (hammer2_cluster_enable) {
108 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
109 chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
110 chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
118 * Recursively set the SUBMODIFIED flag up to the root starting at chain's
119 * parent. SUBMODIFIED is not set in chain itself.
121 * This function only operates on current-time transactions and is not
122 * used during flushes. Instead, the flush code manages the flag itself.
125 hammer2_chain_setsubmod(hammer2_trans_t *trans, hammer2_chain_t *chain)
127 hammer2_chain_core_t *above;
129 if (trans->flags & HAMMER2_TRANS_ISFLUSH)
131 while ((above = chain->above) != NULL) {
132 spin_lock(&above->cst.spin);
133 chain = above->first_parent;
134 while (hammer2_chain_refactor_test(chain, 1))
135 chain = chain->next_parent;
136 atomic_set_int(&chain->flags, HAMMER2_CHAIN_SUBMODIFIED);
137 spin_unlock(&above->cst.spin);
142 * Allocate a new disconnected chain element representing the specified
143 * bref. chain->refs is set to 1 and the passed bref is copied to
144 * chain->bref. chain->bytes is derived from the bref.
146 * chain->core is NOT allocated and the media data and bp pointers are left
147 * NULL. The caller must call chain_core_alloc() to allocate or associate
148 * a core with the chain.
150 * NOTE: Returns a referenced but unlocked (because there is no core) chain.
153 hammer2_chain_alloc(hammer2_mount_t *hmp, hammer2_trans_t *trans,
154 hammer2_blockref_t *bref)
156 hammer2_chain_t *chain;
157 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
160 * Construct the appropriate system structure.
163 case HAMMER2_BREF_TYPE_INODE:
164 case HAMMER2_BREF_TYPE_INDIRECT:
165 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
166 case HAMMER2_BREF_TYPE_DATA:
167 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
168 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
170 case HAMMER2_BREF_TYPE_VOLUME:
171 case HAMMER2_BREF_TYPE_FREEMAP:
173 panic("hammer2_chain_alloc volume type illegal for op");
176 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
182 chain->index = -1; /* not yet assigned */
183 chain->bytes = bytes;
185 chain->flags = HAMMER2_CHAIN_ALLOCATED;
186 chain->delete_tid = HAMMER2_MAX_TID;
188 chain->modify_tid = trans->sync_tid;
194 * Associate an existing core with the chain or allocate a new core.
196 * The core is not locked. No additional refs on the chain are made.
199 hammer2_chain_core_alloc(hammer2_chain_t *chain, hammer2_chain_core_t *core)
201 hammer2_chain_t **scanp;
203 KKASSERT(chain->core == NULL);
204 KKASSERT(chain->next_parent == NULL);
207 core = kmalloc(sizeof(*core), chain->hmp->mchain,
209 RB_INIT(&core->rbtree);
212 ccms_cst_init(&core->cst, chain);
213 core->first_parent = chain;
215 atomic_add_int(&core->sharecnt, 1);
217 spin_lock(&core->cst.spin);
218 if (core->first_parent == NULL) {
219 core->first_parent = chain;
221 scanp = &core->first_parent;
223 scanp = &(*scanp)->next_parent;
225 hammer2_chain_ref(chain); /* next_parent link */
227 spin_unlock(&core->cst.spin);
232 * Add a reference to a chain element, preventing its destruction.
235 hammer2_chain_ref(hammer2_chain_t *chain)
237 atomic_add_int(&chain->refs, 1);
241 * Drop the caller's reference to the chain. When the ref count drops to
242 * zero this function will disassociate the chain from its parent and
243 * deallocate it, then recursely drop the parent using the implied ref
244 * from the chain's chain->parent.
246 * WARNING! Just because we are able to deallocate a chain doesn't mean
247 * that chain->core->rbtree is empty. There can still be a sharecnt
248 * on chain->core and RBTREE entries that refer to different parents.
250 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain);
253 hammer2_chain_drop(hammer2_chain_t *chain)
259 if (chain->flags & HAMMER2_CHAIN_MOVED)
261 if (chain->flags & HAMMER2_CHAIN_MODIFIED)
263 KKASSERT(chain->refs > need);
272 chain = hammer2_chain_lastdrop(chain);
274 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
276 /* retry the same chain */
282 * Safe handling of the 1->0 transition on chain. Returns a chain for
283 * recursive drop or NULL, possibly returning the same chain of the atomic
286 * The cst spinlock is allowed nest child-to-parent (not parent-to-child).
290 hammer2_chain_lastdrop(hammer2_chain_t *chain)
292 hammer2_mount_t *hmp;
293 hammer2_chain_core_t *above;
294 hammer2_chain_core_t *core;
295 hammer2_chain_t *rdrop1;
296 hammer2_chain_t *rdrop2;
299 * Spinlock the core and check to see if it is empty. If it is
300 * not empty we leave chain intact with refs == 0.
302 if ((core = chain->core) != NULL) {
303 spin_lock(&core->cst.spin);
304 if (RB_ROOT(&core->rbtree)) {
305 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
306 /* 1->0 transition successful */
307 spin_unlock(&core->cst.spin);
310 /* 1->0 transition failed, retry */
311 spin_unlock(&core->cst.spin);
322 * Spinlock the parent and try to drop the last ref. On success
323 * remove chain from its parent.
325 if ((above = chain->above) != NULL) {
326 spin_lock(&above->cst.spin);
327 if (!atomic_cmpset_int(&chain->refs, 1, 0)) {
328 /* 1->0 transition failed */
329 spin_unlock(&above->cst.spin);
331 spin_unlock(&core->cst.spin);
337 * 1->0 transition successful
339 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
340 RB_REMOVE(hammer2_chain_tree, &above->rbtree, chain);
341 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
345 * Calculate a chain to return for a recursive drop.
347 * XXX this needs help, we have a potential deep-recursion
348 * problem which we try to address but sometimes we wind up
349 * with two elements that have to be dropped.
351 * If the chain has an associated core with refs at 0
352 * the chain must be the first in the core's linked list
353 * by definition, and we will recursively drop the ref
354 * implied by the chain->next_parent field.
356 * Otherwise if the rbtree containing chain is empty we try
357 * to recursively drop our parent (only the first one could
358 * possibly have refs == 0 since the rest are linked via
361 * Otherwise we try to recursively drop a sibling.
363 if (chain->next_parent) {
364 KKASSERT(core != NULL);
365 rdrop1 = chain->next_parent;
367 if (RB_EMPTY(&above->rbtree)) {
368 rdrop2 = above->first_parent;
369 if (rdrop2 == NULL || rdrop2->refs ||
370 atomic_cmpset_int(&rdrop2->refs, 0, 1) == 0) {
374 rdrop2 = RB_ROOT(&above->rbtree);
375 if (atomic_cmpset_int(&rdrop2->refs, 0, 1) == 0)
378 spin_unlock(&above->cst.spin);
379 above = NULL; /* safety */
381 if (chain->next_parent) {
382 KKASSERT(core != NULL);
383 rdrop1 = chain->next_parent;
388 * We still have the core spinlock (if core is non-NULL). The
389 * above spinlock is gone.
392 KKASSERT(core->first_parent == chain);
393 if (chain->next_parent) {
394 /* parent should already be set */
395 KKASSERT(rdrop1 == chain->next_parent);
397 core->first_parent = chain->next_parent;
398 chain->next_parent = NULL;
401 if (atomic_fetchadd_int(&core->sharecnt, -1) == 1) {
403 * On the 1->0 transition of core we can destroy
406 spin_unlock(&core->cst.spin);
407 KKASSERT(core->cst.count == 0);
408 KKASSERT(core->cst.upgrade == 0);
409 kfree(core, hmp->mchain);
411 spin_unlock(&core->cst.spin);
413 core = NULL; /* safety */
417 * All spin locks are gone, finish freeing stuff.
419 KKASSERT((chain->flags & (HAMMER2_CHAIN_MOVED |
420 HAMMER2_CHAIN_MODIFIED)) == 0);
422 switch(chain->bref.type) {
423 case HAMMER2_BREF_TYPE_VOLUME:
424 case HAMMER2_BREF_TYPE_FREEMAP:
427 case HAMMER2_BREF_TYPE_INODE:
429 kfree(chain->data, hmp->minode);
433 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
435 kfree(chain->data, hmp->mchain);
440 KKASSERT(chain->data == NULL);
444 KKASSERT(chain->bp == NULL);
447 if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
448 chain->flags &= ~HAMMER2_CHAIN_ALLOCATED;
449 kfree(chain, hmp->mchain);
451 if (rdrop1 && rdrop2) {
452 hammer2_chain_drop(rdrop1);
461 * Ref and lock a chain element, acquiring its data with I/O if necessary,
462 * and specify how you would like the data to be resolved.
464 * Returns 0 on success or an error code if the data could not be acquired.
465 * The chain element is locked on return regardless of whether an error
468 * The lock is allowed to recurse, multiple locking ops will aggregate
469 * the requested resolve types. Once data is assigned it will not be
470 * removed until the last unlock.
472 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
473 * (typically used to avoid device/logical buffer
476 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
477 * the INITIAL-create state (indirect blocks only).
479 * Do not resolve data elements for DATA chains.
480 * (typically used to avoid device/logical buffer
483 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
485 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
486 * it will be locked exclusive.
488 * NOTE: Embedded elements (volume header, inodes) are always resolved
491 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
492 * element will instantiate and zero its buffer, and flush it on
495 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
496 * so as not to instantiate a device buffer, which could alias against
497 * a logical file buffer. However, if ALWAYS is specified the
498 * device buffer will be instantiated anyway.
500 * WARNING! If data must be fetched a shared lock will temporarily be
501 * upgraded to exclusive. However, a deadlock can occur if
502 * the caller owns more than one shared lock.
505 hammer2_chain_lock(hammer2_chain_t *chain, int how)
507 hammer2_mount_t *hmp;
508 hammer2_chain_core_t *core;
509 hammer2_blockref_t *bref;
520 * Ref and lock the element. Recursive locks are allowed.
522 if ((how & HAMMER2_RESOLVE_NOREF) == 0)
523 hammer2_chain_ref(chain);
524 atomic_add_int(&chain->lockcnt, 1);
527 KKASSERT(hmp != NULL);
530 * Get the appropriate lock.
533 if (how & HAMMER2_RESOLVE_SHARED)
534 ccms_thread_lock(&core->cst, CCMS_STATE_SHARED);
536 ccms_thread_lock(&core->cst, CCMS_STATE_EXCLUSIVE);
539 * If we already have a valid data pointer no further action is
546 * Do we have to resolve the data?
548 switch(how & HAMMER2_RESOLVE_MASK) {
549 case HAMMER2_RESOLVE_NEVER:
551 case HAMMER2_RESOLVE_MAYBE:
552 if (chain->flags & HAMMER2_CHAIN_INITIAL)
554 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
557 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
560 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
563 case HAMMER2_RESOLVE_ALWAYS:
568 * Upgrade to an exclusive lock so we can safely manipulate the
569 * buffer cache. If another thread got to it before us we
572 ostate = ccms_thread_lock_upgrade(&core->cst);
574 ccms_thread_lock_downgrade(&core->cst, ostate);
579 * We must resolve to a device buffer, either by issuing I/O or
580 * by creating a zero-fill element. We do not mark the buffer
581 * dirty when creating a zero-fill element (the hammer2_chain_modify()
582 * API must still be used to do that).
584 * The device buffer is variable-sized in powers of 2 down
585 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
586 * chunk always contains buffers of the same size. (XXX)
588 * The minimum physical IO size may be larger than the variable
593 psize = hammer2_devblksize(chain->bytes);
594 pmask = (hammer2_off_t)psize - 1;
595 pbase = bref->data_off & ~pmask;
596 boff = bref->data_off & (HAMMER2_OFF_MASK & pmask);
597 KKASSERT(pbase != 0);
598 peof = (pbase + HAMMER2_SEGMASK64) & ~HAMMER2_SEGMASK64;
601 * The getblk() optimization can only be used on newly created
602 * elements if the physical block size matches the request.
604 if ((chain->flags & HAMMER2_CHAIN_INITIAL) &&
605 chain->bytes == psize) {
606 chain->bp = getblk(hmp->devvp, pbase, psize, 0, 0);
608 } else if (hammer2_isclusterable(chain)) {
609 error = cluster_read(hmp->devvp, peof, pbase, psize,
610 psize, HAMMER2_PBUFSIZE*4,
612 adjreadcounter(&chain->bref, chain->bytes);
614 error = bread(hmp->devvp, pbase, psize, &chain->bp);
615 adjreadcounter(&chain->bref, chain->bytes);
619 kprintf("hammer2_chain_get: I/O error %016jx: %d\n",
620 (intmax_t)pbase, error);
623 ccms_thread_lock_downgrade(&core->cst, ostate);
628 * Zero the data area if the chain is in the INITIAL-create state.
629 * Mark the buffer for bdwrite(). This clears the INITIAL state
630 * but does not mark the chain modified.
632 bdata = (char *)chain->bp->b_data + boff;
633 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
634 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
635 bzero(bdata, chain->bytes);
636 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
640 * Setup the data pointer, either pointing it to an embedded data
641 * structure and copying the data from the buffer, or pointing it
644 * The buffer is not retained when copying to an embedded data
645 * structure in order to avoid potential deadlocks or recursions
646 * on the same physical buffer.
648 switch (bref->type) {
649 case HAMMER2_BREF_TYPE_VOLUME:
650 case HAMMER2_BREF_TYPE_FREEMAP:
652 * Copy data from bp to embedded buffer
654 panic("hammer2_chain_lock: called on unresolved volume header");
657 KKASSERT(pbase == 0);
658 KKASSERT(chain->bytes == HAMMER2_PBUFSIZE);
659 bcopy(bdata, &hmp->voldata, chain->bytes);
660 chain->data = (void *)&hmp->voldata;
665 case HAMMER2_BREF_TYPE_INODE:
667 * Copy data from bp to embedded buffer, do not retain the
670 KKASSERT(chain->bytes == sizeof(chain->data->ipdata));
671 atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
672 chain->data = kmalloc(sizeof(chain->data->ipdata),
673 hmp->minode, M_WAITOK | M_ZERO);
674 bcopy(bdata, &chain->data->ipdata, chain->bytes);
678 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
679 KKASSERT(chain->bytes == sizeof(chain->data->bmdata));
680 atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
681 chain->data = kmalloc(sizeof(chain->data->bmdata),
682 hmp->mchain, M_WAITOK | M_ZERO);
683 bcopy(bdata, &chain->data->bmdata, chain->bytes);
687 case HAMMER2_BREF_TYPE_INDIRECT:
688 case HAMMER2_BREF_TYPE_DATA:
689 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
692 * Point data at the device buffer and leave bp intact.
694 chain->data = (void *)bdata;
699 * Make sure the bp is not specifically owned by this thread before
700 * restoring to a possibly shared lock, so another hammer2 thread
704 BUF_KERNPROC(chain->bp);
705 ccms_thread_lock_downgrade(&core->cst, ostate);
710 * Unlock and deref a chain element.
712 * On the last lock release any non-embedded data (chain->bp) will be
716 hammer2_chain_unlock(hammer2_chain_t *chain)
718 hammer2_chain_core_t *core = chain->core;
724 * The core->cst lock can be shared across several chains so we
725 * need to track the per-chain lockcnt separately.
727 * If multiple locks are present (or being attempted) on this
728 * particular chain we can just unlock, drop refs, and return.
730 * Otherwise fall-through on the 1->0 transition.
733 lockcnt = chain->lockcnt;
734 KKASSERT(lockcnt > 0);
737 if (atomic_cmpset_int(&chain->lockcnt,
738 lockcnt, lockcnt - 1)) {
739 ccms_thread_unlock(&core->cst);
740 hammer2_chain_drop(chain);
744 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
751 * On the 1->0 transition we upgrade the core lock (if necessary)
752 * to exclusive for terminal processing. If after upgrading we find
753 * that lockcnt is non-zero, another thread is racing us and will
754 * handle the unload for us later on, so just cleanup and return
755 * leaving the data/bp intact
757 * Otherwise if lockcnt is still 0 it is possible for it to become
758 * non-zero and race, but since we hold the core->cst lock
759 * exclusively all that will happen is that the chain will be
760 * reloaded after we unload it.
762 ostate = ccms_thread_lock_upgrade(&core->cst);
763 if (chain->lockcnt) {
764 ccms_thread_unlock_upgraded(&core->cst, ostate);
765 hammer2_chain_drop(chain);
770 * Shortcut the case if the data is embedded or not resolved.
772 * Do NOT NULL out chain->data (e.g. inode data), it might be
775 * The DIRTYBP flag is non-applicable in this situation and can
776 * be cleared to keep the flags state clean.
778 if (chain->bp == NULL) {
779 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
780 ccms_thread_unlock_upgraded(&core->cst, ostate);
781 hammer2_chain_drop(chain);
788 if ((chain->flags & HAMMER2_CHAIN_DIRTYBP) == 0) {
790 } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
791 switch(chain->bref.type) {
792 case HAMMER2_BREF_TYPE_DATA:
793 counterp = &hammer2_ioa_file_write;
795 case HAMMER2_BREF_TYPE_INODE:
796 counterp = &hammer2_ioa_meta_write;
798 case HAMMER2_BREF_TYPE_INDIRECT:
799 counterp = &hammer2_ioa_indr_write;
801 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
802 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
803 counterp = &hammer2_ioa_fmap_write;
806 counterp = &hammer2_ioa_volu_write;
809 *counterp += chain->bytes;
811 switch(chain->bref.type) {
812 case HAMMER2_BREF_TYPE_DATA:
813 counterp = &hammer2_iod_file_write;
815 case HAMMER2_BREF_TYPE_INODE:
816 counterp = &hammer2_iod_meta_write;
818 case HAMMER2_BREF_TYPE_INDIRECT:
819 counterp = &hammer2_iod_indr_write;
821 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
822 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
823 counterp = &hammer2_iod_fmap_write;
826 counterp = &hammer2_iod_volu_write;
829 *counterp += chain->bytes;
835 * If a device buffer was used for data be sure to destroy the
836 * buffer when we are done to avoid aliases (XXX what about the
837 * underlying VM pages?).
839 * NOTE: Freemap leaf's use reserved blocks and thus no aliasing
842 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
843 chain->bp->b_flags |= B_RELBUF;
846 * The DIRTYBP flag tracks whether we have to bdwrite() the buffer
847 * or not. The flag will get re-set when chain_modify() is called,
848 * even if MODIFIED is already set, allowing the OS to retire the
849 * buffer independent of a hammer2 flus.
852 if (chain->flags & HAMMER2_CHAIN_DIRTYBP) {
853 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
854 if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
855 atomic_clear_int(&chain->flags,
856 HAMMER2_CHAIN_IOFLUSH);
857 chain->bp->b_flags |= B_RELBUF;
858 cluster_awrite(chain->bp);
860 chain->bp->b_flags |= B_CLUSTEROK;
864 if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
865 atomic_clear_int(&chain->flags,
866 HAMMER2_CHAIN_IOFLUSH);
867 chain->bp->b_flags |= B_RELBUF;
870 /* bp might still be dirty */
875 ccms_thread_unlock_upgraded(&core->cst, ostate);
876 hammer2_chain_drop(chain);
880 * Resize the chain's physical storage allocation in-place. This may
881 * replace the passed-in chain with a new chain.
883 * Chains can be resized smaller without reallocating the storage.
884 * Resizing larger will reallocate the storage.
886 * Must be passed an exclusively locked parent and chain, returns a new
887 * exclusively locked chain at the same index and unlocks the old chain.
888 * Flushes the buffer if necessary.
890 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
891 * to avoid instantiating a device buffer that conflicts with the vnode
892 * data buffer. That is, the passed-in bp is a logical buffer, whereas
893 * any chain-oriented bp would be a device buffer.
895 * XXX flags currently ignored, uses chain->bp to detect data/no-data.
896 * XXX return error if cannot resize.
899 hammer2_chain_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
901 hammer2_chain_t *parent, hammer2_chain_t **chainp,
902 int nradix, int flags)
904 hammer2_mount_t *hmp = trans->hmp;
905 hammer2_chain_t *chain = *chainp;
913 * Only data and indirect blocks can be resized for now.
914 * (The volu root, inodes, and freemap elements use a fixed size).
916 KKASSERT(chain != &hmp->vchain);
917 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
918 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
921 * Nothing to do if the element is already the proper size
923 obytes = chain->bytes;
924 nbytes = 1U << nradix;
925 if (obytes == nbytes)
929 * Delete the old chain and duplicate it at the same (parent, index),
930 * returning a new chain. This allows the old chain to still be
931 * used by the flush code. Duplication occurs in-place.
933 * The parent does not have to be locked for the delete/duplicate call,
934 * but is in this particular code path.
936 * NOTE: If we are not crossing a synchronization point the
937 * duplication code will simply reuse the existing chain
940 hammer2_chain_delete_duplicate(trans, &chain, 0);
943 * Set MODIFIED and add a chain ref to prevent destruction. Both
944 * modified flags share the same ref. (duplicated chains do not
945 * start out MODIFIED unless possibly if the duplication code
946 * decided to reuse the existing chain as-is).
948 * If the chain is already marked MODIFIED then we can safely
949 * return the previous allocation to the pool without having to
950 * worry about snapshots. XXX check flush synchronization.
952 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
953 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
954 hammer2_chain_ref(chain);
958 * Relocate the block, even if making it smaller (because different
959 * block sizes may be in different regions).
961 hammer2_freemap_alloc(trans, &chain->bref, nbytes);
962 chain->bytes = nbytes;
963 /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */
966 * The device buffer may be larger than the allocation size.
968 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
969 bbytes = HAMMER2_MINIOSIZE;
970 pbase = chain->bref.data_off & ~(hammer2_off_t)(bbytes - 1);
971 boff = chain->bref.data_off & HAMMER2_OFF_MASK & (bbytes - 1);
974 * For now just support it on DATA chains (and not on indirect
977 KKASSERT(chain->bp == NULL);
980 * Make sure the chain is marked MOVED and SUBMOD is set in the
981 * parent(s) so the adjustments are picked up by flush.
983 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
984 hammer2_chain_ref(chain);
985 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
987 hammer2_chain_setsubmod(trans, chain);
992 * Set a chain modified, making it read-write and duplicating it if necessary.
993 * This function will assign a new physical block to the chain if necessary
995 * Duplication of already-modified chains is possible when the modification
996 * crosses a flush synchronization boundary.
998 * Non-data blocks - The chain should be locked to at least the RESOLVE_MAYBE
999 * level or the COW operation will not work.
1001 * Data blocks - The chain is usually locked RESOLVE_NEVER so as not to
1002 * run the data through the device buffers.
1004 * This function may return a different chain than was passed, in which case
1005 * the old chain will be unlocked and the new chain will be locked.
1007 * ip->chain may be adjusted by hammer2_chain_modify_ip().
1009 hammer2_inode_data_t *
1010 hammer2_chain_modify_ip(hammer2_trans_t *trans, hammer2_inode_t *ip,
1011 hammer2_chain_t **chainp, int flags)
1013 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1014 hammer2_chain_modify(trans, chainp, flags);
1015 if (ip->chain != *chainp)
1016 hammer2_inode_repoint(ip, NULL, *chainp);
1017 return(&ip->chain->data->ipdata);
1021 hammer2_chain_modify(hammer2_trans_t *trans, hammer2_chain_t **chainp,
1024 hammer2_mount_t *hmp = trans->hmp;
1025 hammer2_chain_t *chain;
1026 hammer2_off_t pbase;
1027 hammer2_off_t pmask;
1029 hammer2_tid_t flush_tid;
1038 * Data must be resolved if already assigned unless explicitly
1039 * flagged otherwise.
1042 if (chain->data == NULL && (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1043 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1044 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1045 hammer2_chain_unlock(chain);
1049 * data is not optional for freemap chains (we must always be sure
1050 * to copy the data on COW storage allocations).
1052 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1053 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1054 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1055 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1059 * If the chain is already marked MODIFIED we can usually just
1060 * return. However, if a modified chain is modified again in
1061 * a synchronization-point-crossing manner we have to issue a
1062 * delete/duplicate on the chain to avoid flush interference.
1064 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
1066 * Which flush_tid do we need to check? If the chain is
1067 * related to the freemap we have to use the freemap flush
1068 * tid (free_flush_tid), otherwise we use the normal filesystem
1069 * flush tid (topo_flush_tid). The two flush domains are
1070 * almost completely independent of each other.
1072 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1073 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1074 flush_tid = hmp->topo_flush_tid; /* XXX */
1075 goto skipxx; /* XXX */
1077 flush_tid = hmp->topo_flush_tid;
1083 if (chain->modify_tid <= flush_tid &&
1084 trans->sync_tid > flush_tid) {
1086 * Modifications cross synchronization point,
1087 * requires delete-duplicate.
1089 KKASSERT((flags & HAMMER2_MODIFY_ASSERTNOCOPY) == 0);
1090 hammer2_chain_delete_duplicate(trans, chainp, 0);
1092 /* fall through using duplicate */
1096 * Quick return path, set DIRTYBP to ensure that
1097 * the later retirement of bp will write it out.
1099 * quick return path also needs the modify_tid
1103 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
1104 if ((flags & HAMMER2_MODIFY_NO_MODIFY_TID) == 0)
1105 chain->bref.modify_tid = trans->sync_tid;
1106 chain->modify_tid = trans->sync_tid;
1111 * modify_tid is only update for primary modifications, not for
1112 * propagated brefs. mirror_tid will be updated regardless during
1113 * the flush, no need to set it here.
1115 if ((flags & HAMMER2_MODIFY_NO_MODIFY_TID) == 0)
1116 chain->bref.modify_tid = trans->sync_tid;
1119 * Set MODIFIED and add a chain ref to prevent destruction. Both
1120 * modified flags share the same ref.
1122 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1123 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1124 hammer2_chain_ref(chain);
1128 * Adjust chain->modify_tid so the flusher knows when the
1129 * modification occurred.
1131 chain->modify_tid = trans->sync_tid;
1134 * The modification or re-modification requires an allocation and
1137 * We normally always allocate new storage here. If storage exists
1138 * and MODIFY_NOREALLOC is passed in, we do not allocate new storage.
1140 if (chain != &hmp->vchain &&
1141 chain != &hmp->fchain &&
1142 ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1143 (flags & HAMMER2_MODIFY_NOREALLOC) == 0)
1145 hammer2_freemap_alloc(trans, &chain->bref, chain->bytes);
1146 /* XXX failed allocation */
1150 * Do not COW if OPTDATA is set. INITIAL flag remains unchanged.
1151 * (OPTDATA does not prevent [re]allocation of storage, only the
1152 * related copy-on-write op).
1154 if (flags & HAMMER2_MODIFY_OPTDATA)
1158 * Clearing the INITIAL flag (for indirect blocks) indicates that
1159 * we've processed the uninitialized storage allocation.
1161 * If this flag is already clear we are likely in a copy-on-write
1162 * situation but we have to be sure NOT to bzero the storage if
1163 * no data is present.
1165 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1166 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1173 * We currently should never instantiate a device buffer for a
1174 * file data chain. (We definitely can for a freemap chain).
1176 KKASSERT(chain->bref.type != HAMMER2_BREF_TYPE_DATA);
1179 * Instantiate data buffer and possibly execute COW operation
1181 switch(chain->bref.type) {
1182 case HAMMER2_BREF_TYPE_VOLUME:
1183 case HAMMER2_BREF_TYPE_FREEMAP:
1184 case HAMMER2_BREF_TYPE_INODE:
1185 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1187 * The data is embedded, no copy-on-write operation is
1190 KKASSERT(chain->bp == NULL);
1192 case HAMMER2_BREF_TYPE_DATA:
1193 case HAMMER2_BREF_TYPE_INDIRECT:
1194 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1196 * Perform the copy-on-write operation
1198 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1200 psize = hammer2_devblksize(chain->bytes);
1201 pmask = (hammer2_off_t)psize - 1;
1202 pbase = chain->bref.data_off & ~pmask;
1203 boff = chain->bref.data_off & (HAMMER2_OFF_MASK & pmask);
1204 KKASSERT(pbase != 0);
1205 peof = (pbase + HAMMER2_SEGMASK64) & ~HAMMER2_SEGMASK64;
1208 * The getblk() optimization can only be used if the
1209 * chain element size matches the physical block size.
1211 if (chain->bp && chain->bp->b_loffset == pbase) {
1214 } else if (chain->bytes == psize) {
1215 nbp = getblk(hmp->devvp, pbase, psize, 0, 0);
1217 } else if (hammer2_isclusterable(chain)) {
1218 error = cluster_read(hmp->devvp, peof, pbase, psize,
1219 psize, HAMMER2_PBUFSIZE*4,
1221 adjreadcounter(&chain->bref, chain->bytes);
1223 error = bread(hmp->devvp, pbase, psize, &nbp);
1224 adjreadcounter(&chain->bref, chain->bytes);
1226 KKASSERT(error == 0);
1227 bdata = (char *)nbp->b_data + boff;
1230 * Copy or zero-fill on write depending on whether
1231 * chain->data exists or not. Retire the existing bp
1232 * based on the DIRTYBP flag. Set the DIRTYBP flag to
1233 * indicate that retirement of nbp should use bdwrite().
1236 KKASSERT(chain->bp != NULL);
1237 if (chain->data != bdata) {
1238 bcopy(chain->data, bdata, chain->bytes);
1240 } else if (wasinitial) {
1241 bzero(bdata, chain->bytes);
1244 * We have a problem. We were asked to COW but
1245 * we don't have any data to COW with!
1247 panic("hammer2_chain_modify: having a COW %p\n",
1250 if (chain->bp != nbp) {
1252 if (chain->flags & HAMMER2_CHAIN_DIRTYBP) {
1253 chain->bp->b_flags |= B_CLUSTEROK;
1256 chain->bp->b_flags |= B_RELBUF;
1262 chain->data = bdata;
1263 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
1266 panic("hammer2_chain_modify: illegal non-embedded type %d",
1272 hammer2_chain_setsubmod(trans, chain);
1276 * Mark the volume as having been modified. This short-cut version
1277 * does not have to lock the volume's chain, which allows the ioctl
1278 * code to make adjustments to connections without deadlocking. XXX
1280 * No ref is made on vchain when flagging it MODIFIED.
1283 hammer2_modify_volume(hammer2_mount_t *hmp)
1285 hammer2_voldata_lock(hmp);
1286 hammer2_voldata_unlock(hmp, 1);
1290 * Locate an in-memory chain. The parent must be locked. The in-memory
1291 * chain is returned with a reference and without a lock, or NULL
1294 * This function returns the chain at the specified index with the highest
1295 * delete_tid. The caller must check whether the chain is flagged
1296 * CHAIN_DELETED or not. However, because chain iterations can be removed
1297 * from memory we must ALSO check that DELETED chains are not flushed. A
1298 * DELETED chain which has been flushed must be ignored (the caller must
1299 * check the parent's blockref array).
1301 * NOTE: If no chain is found the caller usually must check the on-media
1302 * array to determine if a blockref exists at the index.
1304 struct hammer2_chain_find_info {
1305 hammer2_chain_t *best;
1306 hammer2_tid_t delete_tid;
1312 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
1314 struct hammer2_chain_find_info *info = data;
1316 if (child->index < info->index)
1318 if (child->index > info->index)
1325 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
1327 struct hammer2_chain_find_info *info = data;
1329 if (info->delete_tid < child->delete_tid) {
1330 info->delete_tid = child->delete_tid;
1338 hammer2_chain_find_locked(hammer2_chain_t *parent, int index)
1340 struct hammer2_chain_find_info info;
1341 hammer2_chain_t *child;
1344 info.delete_tid = 0;
1347 RB_SCAN(hammer2_chain_tree, &parent->core->rbtree,
1348 hammer2_chain_find_cmp, hammer2_chain_find_callback,
1356 hammer2_chain_find(hammer2_chain_t *parent, int index)
1358 hammer2_chain_t *child;
1360 spin_lock(&parent->core->cst.spin);
1361 child = hammer2_chain_find_locked(parent, index);
1363 hammer2_chain_ref(child);
1364 spin_unlock(&parent->core->cst.spin);
1370 * Return a locked chain structure with all associated data acquired.
1371 * (if LOOKUP_NOLOCK is requested the returned chain is only referenced).
1373 * Caller must hold the parent locked shared or exclusive since we may
1374 * need the parent's bref array to find our block.
1376 * The returned child is locked as requested. If NOLOCK, the returned
1377 * child is still at least referenced.
1380 hammer2_chain_get(hammer2_chain_t *parent, int index, int flags)
1382 hammer2_blockref_t *bref;
1383 hammer2_mount_t *hmp = parent->hmp;
1384 hammer2_chain_core_t *above = parent->core;
1385 hammer2_chain_t *chain;
1386 hammer2_chain_t dummy;
1390 * Figure out how to lock. MAYBE can be used to optimized
1391 * the initial-create state for indirect blocks.
1393 if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK))
1394 how = HAMMER2_RESOLVE_NEVER;
1396 how = HAMMER2_RESOLVE_MAYBE;
1397 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK))
1398 how |= HAMMER2_RESOLVE_SHARED;
1402 * First see if we have a (possibly modified) chain element cached
1403 * for this (parent, index). Acquire the data if necessary.
1405 * If chain->data is non-NULL the chain should already be marked
1409 dummy.index = index;
1410 dummy.delete_tid = HAMMER2_MAX_TID;
1411 spin_lock(&above->cst.spin);
1412 chain = RB_FIND(hammer2_chain_tree, &above->rbtree, &dummy);
1414 hammer2_chain_ref(chain);
1415 spin_unlock(&above->cst.spin);
1416 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0)
1417 hammer2_chain_lock(chain, how | HAMMER2_RESOLVE_NOREF);
1420 spin_unlock(&above->cst.spin);
1423 * The parent chain must not be in the INITIAL state.
1425 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1426 panic("hammer2_chain_get: Missing bref(1)");
1431 * No RBTREE entry found, lookup the bref and issue I/O (switch on
1432 * the parent's bref to determine where and how big the array is).
1434 switch(parent->bref.type) {
1435 case HAMMER2_BREF_TYPE_INODE:
1436 KKASSERT(index >= 0 && index < HAMMER2_SET_COUNT);
1437 bref = &parent->data->ipdata.u.blockset.blockref[index];
1439 case HAMMER2_BREF_TYPE_INDIRECT:
1440 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1441 KKASSERT(parent->data != NULL);
1442 KKASSERT(index >= 0 &&
1443 index < parent->bytes / sizeof(hammer2_blockref_t));
1444 bref = &parent->data->npdata.blockref[index];
1446 case HAMMER2_BREF_TYPE_VOLUME:
1447 KKASSERT(index >= 0 && index < HAMMER2_SET_COUNT);
1448 bref = &hmp->voldata.sroot_blockset.blockref[index];
1450 case HAMMER2_BREF_TYPE_FREEMAP:
1451 KKASSERT(index >= 0 && index < HAMMER2_SET_COUNT);
1452 bref = &hmp->voldata.freemap_blockset.blockref[index];
1456 panic("hammer2_chain_get: unrecognized blockref type: %d",
1459 if (bref->type == 0) {
1460 panic("hammer2_chain_get: Missing bref(2)");
1465 * Allocate a chain structure representing the existing media
1466 * entry. Resulting chain has one ref and is not locked.
1468 * The locking operation we do later will issue I/O to read it.
1470 chain = hammer2_chain_alloc(hmp, NULL, bref);
1471 hammer2_chain_core_alloc(chain, NULL); /* ref'd chain returned */
1474 * Link the chain into its parent. A spinlock is required to safely
1475 * access the RBTREE, and it is possible to collide with another
1476 * hammer2_chain_get() operation because the caller might only hold
1477 * a shared lock on the parent.
1479 KKASSERT(parent->refs > 0);
1480 spin_lock(&above->cst.spin);
1481 chain->above = above;
1482 chain->index = index;
1483 if (RB_INSERT(hammer2_chain_tree, &above->rbtree, chain)) {
1484 chain->above = NULL;
1486 spin_unlock(&above->cst.spin);
1487 hammer2_chain_drop(chain);
1490 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
1491 spin_unlock(&above->cst.spin);
1494 * Our new chain is referenced but NOT locked. Lock the chain
1495 * below. The locking operation also resolves its data.
1497 * If NOLOCK is set the release will release the one-and-only lock.
1499 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0) {
1500 hammer2_chain_lock(chain, how); /* recusive lock */
1501 hammer2_chain_drop(chain); /* excess ref */
1507 * Lookup initialization/completion API
1510 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
1512 if (flags & HAMMER2_LOOKUP_SHARED) {
1513 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1514 HAMMER2_RESOLVE_SHARED);
1516 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1522 hammer2_chain_lookup_done(hammer2_chain_t *parent)
1525 hammer2_chain_unlock(parent);
1530 hammer2_chain_getparent(hammer2_chain_t **parentp, int how)
1532 hammer2_chain_t *oparent;
1533 hammer2_chain_t *nparent;
1534 hammer2_chain_core_t *above;
1537 above = oparent->above;
1539 spin_lock(&above->cst.spin);
1540 nparent = above->first_parent;
1541 while (hammer2_chain_refactor_test(nparent, 1))
1542 nparent = nparent->next_parent;
1543 hammer2_chain_ref(nparent); /* protect nparent, use in lock */
1544 spin_unlock(&above->cst.spin);
1546 hammer2_chain_unlock(oparent);
1547 hammer2_chain_lock(nparent, how | HAMMER2_RESOLVE_NOREF);
1554 * Locate any key between key_beg and key_end inclusive. (*parentp)
1555 * typically points to an inode but can also point to a related indirect
1556 * block and this function will recurse upwards and find the inode again.
1558 * WARNING! THIS DOES NOT RETURN KEYS IN LOGICAL KEY ORDER! ANY KEY
1559 * WITHIN THE RANGE CAN BE RETURNED. HOWEVER, AN ITERATION
1560 * WHICH PICKS UP WHERE WE LEFT OFF WILL CONTINUE THE SCAN
1561 * AND ALL IN-RANGE KEYS WILL EVENTUALLY BE RETURNED (NOT
1562 * NECESSARILY IN ORDER).
1564 * (*parentp) must be exclusively locked and referenced and can be an inode
1565 * or an existing indirect block within the inode.
1567 * On return (*parentp) will be modified to point at the deepest parent chain
1568 * element encountered during the search, as a helper for an insertion or
1569 * deletion. The new (*parentp) will be locked and referenced and the old
1570 * will be unlocked and dereferenced (no change if they are both the same).
1572 * The matching chain will be returned exclusively locked. If NOLOCK is
1573 * requested the chain will be returned only referenced.
1575 * NULL is returned if no match was found, but (*parentp) will still
1576 * potentially be adjusted.
1578 * This function will also recurse up the chain if the key is not within the
1579 * current parent's range. (*parentp) can never be set to NULL. An iteration
1580 * can simply allow (*parentp) to float inside the loop.
1582 * NOTE! chain->data is not always resolved. By default it will not be
1583 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
1584 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
1585 * BREF_TYPE_DATA as the device buffer can alias the logical file
1589 hammer2_chain_lookup(hammer2_chain_t **parentp,
1590 hammer2_key_t key_beg, hammer2_key_t key_end,
1593 hammer2_mount_t *hmp;
1594 hammer2_chain_t *parent;
1595 hammer2_chain_t *chain;
1596 hammer2_chain_t *tmp;
1597 hammer2_blockref_t *base;
1598 hammer2_blockref_t *bref;
1599 hammer2_key_t scan_beg;
1600 hammer2_key_t scan_end;
1603 int how_always = HAMMER2_RESOLVE_ALWAYS;
1604 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1606 if (flags & HAMMER2_LOOKUP_ALWAYS)
1607 how_maybe = how_always;
1609 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK)) {
1610 how_maybe |= HAMMER2_RESOLVE_SHARED;
1611 how_always |= HAMMER2_RESOLVE_SHARED;
1615 * Recurse (*parentp) upward if necessary until the parent completely
1616 * encloses the key range or we hit the inode.
1621 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1622 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1623 scan_beg = parent->bref.key;
1624 scan_end = scan_beg +
1625 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1626 if (key_beg >= scan_beg && key_end <= scan_end)
1628 parent = hammer2_chain_getparent(parentp, how_maybe);
1633 * Locate the blockref array. Currently we do a fully associative
1634 * search through the array.
1636 switch(parent->bref.type) {
1637 case HAMMER2_BREF_TYPE_INODE:
1639 * Special shortcut for embedded data returns the inode
1640 * itself. Callers must detect this condition and access
1641 * the embedded data (the strategy code does this for us).
1643 * This is only applicable to regular files and softlinks.
1645 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1646 if (flags & HAMMER2_LOOKUP_NOLOCK)
1647 hammer2_chain_ref(parent);
1649 hammer2_chain_lock(parent, how_always);
1652 base = &parent->data->ipdata.u.blockset.blockref[0];
1653 count = HAMMER2_SET_COUNT;
1655 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1656 case HAMMER2_BREF_TYPE_INDIRECT:
1658 * Handle MATCHIND on the parent
1660 if (flags & HAMMER2_LOOKUP_MATCHIND) {
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 hammer2_chain_lock(chain, how_maybe);
1671 * Optimize indirect blocks in the INITIAL state to avoid
1674 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1677 if (parent->data == NULL)
1678 panic("parent->data is NULL");
1679 base = &parent->data->npdata.blockref[0];
1681 count = parent->bytes / sizeof(hammer2_blockref_t);
1683 case HAMMER2_BREF_TYPE_VOLUME:
1684 base = &hmp->voldata.sroot_blockset.blockref[0];
1685 count = HAMMER2_SET_COUNT;
1687 case HAMMER2_BREF_TYPE_FREEMAP:
1688 base = &hmp->voldata.freemap_blockset.blockref[0];
1689 count = HAMMER2_SET_COUNT;
1692 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
1694 base = NULL; /* safety */
1695 count = 0; /* safety */
1699 * If the element and key overlap we use the element.
1701 * NOTE! Deleted elements are effectively invisible. Deletions
1702 * proactively clear the parent bref to the deleted child
1703 * so we do not try to shadow here to avoid parent updates
1704 * (which would be difficult since multiple deleted elements
1705 * might represent different flush synchronization points).
1708 scan_beg = 0; /* avoid compiler warning */
1709 scan_end = 0; /* avoid compiler warning */
1711 for (i = 0; i < count; ++i) {
1712 tmp = hammer2_chain_find(parent, i);
1714 if (tmp->flags & HAMMER2_CHAIN_DELETED) {
1715 hammer2_chain_drop(tmp);
1719 KKASSERT(bref->type != 0);
1720 } else if (base == NULL || base[i].type == 0) {
1725 scan_beg = bref->key;
1726 scan_end = scan_beg + ((hammer2_key_t)1 << bref->keybits) - 1;
1728 hammer2_chain_drop(tmp);
1729 if (key_beg <= scan_end && key_end >= scan_beg)
1733 if (key_beg == key_end)
1735 return (hammer2_chain_next(parentp, NULL,
1736 key_beg, key_end, flags));
1740 * Acquire the new chain element. If the chain element is an
1741 * indirect block we must search recursively.
1743 * It is possible for the tmp chain above to be removed from
1744 * the RBTREE but the parent lock ensures it would not have been
1745 * destroyed from the media, so the chain_get() code will simply
1746 * reload it from the media in that case.
1748 chain = hammer2_chain_get(parent, i, flags);
1753 * If the chain element is an indirect block it becomes the new
1754 * parent and we loop on it.
1756 * The parent always has to be locked with at least RESOLVE_MAYBE
1757 * so we can access its data. It might need a fixup if the caller
1758 * passed incompatible flags. Be careful not to cause a deadlock
1759 * as a data-load requires an exclusive lock.
1761 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
1762 * range is within the requested key range we return the indirect
1763 * block and do NOT loop. This is usually only used to acquire
1766 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1767 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1768 hammer2_chain_unlock(parent);
1769 *parentp = parent = chain;
1770 if (flags & HAMMER2_LOOKUP_NOLOCK) {
1771 hammer2_chain_lock(chain,
1773 HAMMER2_RESOLVE_NOREF);
1774 } else if ((flags & HAMMER2_LOOKUP_NODATA) &&
1775 chain->data == NULL) {
1776 hammer2_chain_ref(chain);
1777 hammer2_chain_unlock(chain);
1778 hammer2_chain_lock(chain,
1780 HAMMER2_RESOLVE_NOREF);
1786 * All done, return the chain
1792 * After having issued a lookup we can iterate all matching keys.
1794 * If chain is non-NULL we continue the iteration from just after it's index.
1796 * If chain is NULL we assume the parent was exhausted and continue the
1797 * iteration at the next parent.
1799 * parent must be locked on entry and remains locked throughout. chain's
1800 * lock status must match flags. Chain is always at least referenced.
1802 * WARNING! The MATCHIND flag does not apply to this function.
1805 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
1806 hammer2_key_t key_beg, hammer2_key_t key_end,
1809 hammer2_mount_t *hmp;
1810 hammer2_chain_t *parent;
1811 hammer2_chain_t *tmp;
1812 hammer2_blockref_t *base;
1813 hammer2_blockref_t *bref;
1814 hammer2_key_t scan_beg;
1815 hammer2_key_t scan_end;
1817 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1820 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK))
1821 how_maybe |= HAMMER2_RESOLVE_SHARED;
1828 * Calculate the next index and recalculate the parent if necessary.
1832 * Continue iteration within current parent. If not NULL
1833 * the passed-in chain may or may not be locked, based on
1834 * the LOOKUP_NOLOCK flag (passed in as returned from lookup
1837 i = chain->index + 1;
1838 if (flags & HAMMER2_LOOKUP_NOLOCK)
1839 hammer2_chain_drop(chain);
1841 hammer2_chain_unlock(chain);
1844 * Any scan where the lookup returned degenerate data embedded
1845 * in the inode has an invalid index and must terminate.
1847 if (chain == parent)
1850 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1851 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1853 * We reached the end of the iteration.
1858 * Continue iteration with next parent unless the current
1859 * parent covers the range.
1861 scan_beg = parent->bref.key;
1862 scan_end = scan_beg +
1863 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1864 if (key_beg >= scan_beg && key_end <= scan_end)
1867 i = parent->index + 1;
1868 parent = hammer2_chain_getparent(parentp, how_maybe);
1873 * Locate the blockref array. Currently we do a fully associative
1874 * search through the array.
1876 switch(parent->bref.type) {
1877 case HAMMER2_BREF_TYPE_INODE:
1878 base = &parent->data->ipdata.u.blockset.blockref[0];
1879 count = HAMMER2_SET_COUNT;
1881 case HAMMER2_BREF_TYPE_INDIRECT:
1882 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1883 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1886 KKASSERT(parent->data != NULL);
1887 base = &parent->data->npdata.blockref[0];
1889 count = parent->bytes / sizeof(hammer2_blockref_t);
1891 case HAMMER2_BREF_TYPE_VOLUME:
1892 base = &hmp->voldata.sroot_blockset.blockref[0];
1893 count = HAMMER2_SET_COUNT;
1895 case HAMMER2_BREF_TYPE_FREEMAP:
1896 base = &hmp->voldata.freemap_blockset.blockref[0];
1897 count = HAMMER2_SET_COUNT;
1900 panic("hammer2_chain_next: unrecognized blockref type: %d",
1902 base = NULL; /* safety */
1903 count = 0; /* safety */
1906 KKASSERT(i <= count);
1909 * Look for the key. If we are unable to find a match and an exact
1910 * match was requested we return NULL. If a range was requested we
1911 * run hammer2_chain_next() to iterate.
1913 * NOTE! Deleted elements are effectively invisible. Deletions
1914 * proactively clear the parent bref to the deleted child
1915 * so we do not try to shadow here to avoid parent updates
1916 * (which would be difficult since multiple deleted elements
1917 * might represent different flush synchronization points).
1920 scan_beg = 0; /* avoid compiler warning */
1921 scan_end = 0; /* avoid compiler warning */
1924 tmp = hammer2_chain_find(parent, i);
1926 if (tmp->flags & HAMMER2_CHAIN_DELETED) {
1927 hammer2_chain_drop(tmp);
1932 } else if (base == NULL || base[i].type == 0) {
1938 scan_beg = bref->key;
1939 scan_end = scan_beg + ((hammer2_key_t)1 << bref->keybits) - 1;
1941 hammer2_chain_drop(tmp);
1942 if (key_beg <= scan_end && key_end >= scan_beg)
1948 * If we couldn't find a match recurse up a parent to continue the
1955 * Acquire the new chain element. If the chain element is an
1956 * indirect block we must search recursively.
1958 chain = hammer2_chain_get(parent, i, flags);
1963 * If the chain element is an indirect block it becomes the new
1964 * parent and we loop on it.
1966 * The parent always has to be locked with at least RESOLVE_MAYBE
1967 * so we can access its data. It might need a fixup if the caller
1968 * passed incompatible flags. Be careful not to cause a deadlock
1969 * as a data-load requires an exclusive lock.
1971 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
1972 * range is within the requested key range we return the indirect
1973 * block and do NOT loop. This is usually only used to acquire
1976 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1977 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1978 if ((flags & HAMMER2_LOOKUP_MATCHIND) == 0 ||
1979 key_beg > scan_beg || key_end < scan_end) {
1980 hammer2_chain_unlock(parent);
1981 *parentp = parent = chain;
1983 if (flags & HAMMER2_LOOKUP_NOLOCK) {
1984 hammer2_chain_lock(parent,
1986 HAMMER2_RESOLVE_NOREF);
1987 } else if ((flags & HAMMER2_LOOKUP_NODATA) &&
1988 parent->data == NULL) {
1989 hammer2_chain_ref(parent);
1990 hammer2_chain_unlock(parent);
1991 hammer2_chain_lock(parent,
1993 HAMMER2_RESOLVE_NOREF);
2001 * All done, return chain
2007 * Create and return a new hammer2 system memory structure of the specified
2008 * key, type and size and insert it under (*parentp). This is a full
2009 * insertion, based on the supplied key/keybits, and may involve creating
2010 * indirect blocks and moving other chains around via delete/duplicate.
2012 * (*parentp) must be exclusive locked and may be replaced on return
2013 * depending on how much work the function had to do.
2015 * (*chainp) usually starts out NULL and returns the newly created chain,
2016 * but if the caller desires the caller may allocate a disconnected chain
2017 * and pass it in instead. (It is also possible for the caller to use
2018 * chain_duplicate() to create a disconnected chain, manipulate it, then
2019 * pass it into this function to insert it).
2021 * This function should NOT be used to insert INDIRECT blocks. It is
2022 * typically used to create/insert inodes and data blocks.
2024 * Caller must pass-in an exclusively locked parent the new chain is to
2025 * be inserted under, and optionally pass-in a disconnected, exclusively
2026 * locked chain to insert (else we create a new chain). The function will
2027 * adjust (*parentp) as necessary, create or connect the chain, and
2028 * return an exclusively locked chain in *chainp.
2031 hammer2_chain_create(hammer2_trans_t *trans, hammer2_chain_t **parentp,
2032 hammer2_chain_t **chainp,
2033 hammer2_key_t key, int keybits, int type, size_t bytes)
2035 hammer2_mount_t *hmp;
2036 hammer2_chain_t *chain;
2037 hammer2_chain_t *child;
2038 hammer2_chain_t *parent = *parentp;
2039 hammer2_chain_core_t *above;
2040 hammer2_blockref_t dummy;
2041 hammer2_blockref_t *base;
2047 above = parent->core;
2048 KKASSERT(ccms_thread_lock_owned(&above->cst));
2052 if (chain == NULL) {
2054 * First allocate media space and construct the dummy bref,
2055 * then allocate the in-memory chain structure. Set the
2056 * INITIAL flag for fresh chains.
2058 bzero(&dummy, sizeof(dummy));
2061 dummy.keybits = keybits;
2062 dummy.data_off = hammer2_getradix(bytes);
2063 dummy.methods = parent->bref.methods;
2064 chain = hammer2_chain_alloc(hmp, trans, &dummy);
2065 hammer2_chain_core_alloc(chain, NULL);
2067 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
2070 * Lock the chain manually, chain_lock will load the chain
2071 * which we do NOT want to do. (note: chain->refs is set
2072 * to 1 by chain_alloc() for us, but lockcnt is not).
2075 ccms_thread_lock(&chain->core->cst, CCMS_STATE_EXCLUSIVE);
2079 * We do NOT set INITIAL here (yet). INITIAL is only
2080 * used for indirect blocks.
2082 * Recalculate bytes to reflect the actual media block
2085 bytes = (hammer2_off_t)1 <<
2086 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
2087 chain->bytes = bytes;
2090 case HAMMER2_BREF_TYPE_VOLUME:
2091 case HAMMER2_BREF_TYPE_FREEMAP:
2092 panic("hammer2_chain_create: called with volume type");
2094 case HAMMER2_BREF_TYPE_INODE:
2095 KKASSERT(bytes == HAMMER2_INODE_BYTES);
2096 atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
2097 chain->data = kmalloc(sizeof(chain->data->ipdata),
2098 hmp->minode, M_WAITOK | M_ZERO);
2100 case HAMMER2_BREF_TYPE_INDIRECT:
2101 panic("hammer2_chain_create: cannot be used to"
2102 "create indirect block");
2104 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2105 panic("hammer2_chain_create: cannot be used to"
2106 "create freemap root or node");
2108 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2109 KKASSERT(bytes == sizeof(chain->data->bmdata));
2110 atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
2111 chain->data = kmalloc(sizeof(chain->data->bmdata),
2112 hmp->mchain, M_WAITOK | M_ZERO);
2114 case HAMMER2_BREF_TYPE_DATA:
2116 /* leave chain->data NULL */
2117 KKASSERT(chain->data == NULL);
2122 * Potentially update the existing chain's key/keybits.
2124 * Do NOT mess with the current state of the INITIAL flag.
2126 chain->bref.key = key;
2127 chain->bref.keybits = keybits;
2128 KKASSERT(chain->above == NULL);
2132 above = parent->core;
2135 * Locate a free blockref in the parent's array
2137 switch(parent->bref.type) {
2138 case HAMMER2_BREF_TYPE_INODE:
2139 KKASSERT((parent->data->ipdata.op_flags &
2140 HAMMER2_OPFLAG_DIRECTDATA) == 0);
2141 KKASSERT(parent->data != NULL);
2142 base = &parent->data->ipdata.u.blockset.blockref[0];
2143 count = HAMMER2_SET_COUNT;
2145 case HAMMER2_BREF_TYPE_INDIRECT:
2146 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2147 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2150 KKASSERT(parent->data != NULL);
2151 base = &parent->data->npdata.blockref[0];
2153 count = parent->bytes / sizeof(hammer2_blockref_t);
2155 case HAMMER2_BREF_TYPE_VOLUME:
2156 KKASSERT(parent->data != NULL);
2157 base = &hmp->voldata.sroot_blockset.blockref[0];
2158 count = HAMMER2_SET_COUNT;
2160 case HAMMER2_BREF_TYPE_FREEMAP:
2161 KKASSERT(parent->data != NULL);
2162 base = &hmp->voldata.freemap_blockset.blockref[0];
2163 count = HAMMER2_SET_COUNT;
2166 panic("hammer2_chain_create: unrecognized blockref type: %d",
2173 * Scan for an unallocated bref, also skipping any slots occupied
2174 * by in-memory chain elements that may not yet have been updated
2175 * in the parent's bref array.
2177 * We don't have to hold the spinlock to save an empty slot as
2178 * new slots can only transition from empty if the parent is
2179 * locked exclusively.
2181 spin_lock(&above->cst.spin);
2182 for (i = 0; i < count; ++i) {
2183 child = hammer2_chain_find_locked(parent, i);
2185 if (child->flags & HAMMER2_CHAIN_DELETED)
2191 if (base[i].type == 0)
2194 spin_unlock(&above->cst.spin);
2197 * If no free blockref could be found we must create an indirect
2198 * block and move a number of blockrefs into it. With the parent
2199 * locked we can safely lock each child in order to move it without
2200 * causing a deadlock.
2202 * This may return the new indirect block or the old parent depending
2203 * on where the key falls. NULL is returned on error.
2206 hammer2_chain_t *nparent;
2208 nparent = hammer2_chain_create_indirect(trans, parent,
2211 if (nparent == NULL) {
2213 hammer2_chain_drop(chain);
2217 if (parent != nparent) {
2218 hammer2_chain_unlock(parent);
2219 parent = *parentp = nparent;
2225 * Link the chain into its parent. Later on we will have to set
2226 * the MOVED bit in situations where we don't mark the new chain
2227 * as being modified.
2229 if (chain->above != NULL)
2230 panic("hammer2: hammer2_chain_create: chain already connected");
2231 KKASSERT(chain->above == NULL);
2232 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
2234 chain->above = above;
2236 spin_lock(&above->cst.spin);
2237 if (RB_INSERT(hammer2_chain_tree, &above->rbtree, chain))
2238 panic("hammer2_chain_create: collision");
2239 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2240 spin_unlock(&above->cst.spin);
2244 * Mark the newly created chain modified.
2246 * Device buffers are not instantiated for DATA elements
2247 * as these are handled by logical buffers.
2249 * Indirect and freemap node indirect blocks are handled
2250 * by hammer2_chain_create_indirect() and not by this
2253 * Data for all other bref types is expected to be
2254 * instantiated (INODE, LEAF).
2256 switch(chain->bref.type) {
2257 case HAMMER2_BREF_TYPE_DATA:
2258 hammer2_chain_modify(trans, &chain,
2259 HAMMER2_MODIFY_OPTDATA |
2260 HAMMER2_MODIFY_ASSERTNOCOPY);
2262 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2263 case HAMMER2_BREF_TYPE_INODE:
2264 hammer2_chain_modify(trans, &chain,
2265 HAMMER2_MODIFY_ASSERTNOCOPY);
2269 * Remaining types are not supported by this function.
2270 * In particular, INDIRECT and LEAF_NODE types are
2271 * handled by create_indirect().
2273 panic("hammer2_chain_create: bad type: %d",
2280 * When reconnecting a chain we must set MOVED and setsubmod
2281 * so the flush recognizes that it must update the bref in
2284 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
2285 hammer2_chain_ref(chain);
2286 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
2288 hammer2_chain_setsubmod(trans, chain);
2298 * Replace (*chainp) with a duplicate. The original *chainp is unlocked
2299 * and the replacement will be returned locked. Both the original and the
2300 * new chain will share the same RBTREE (have the same chain->core), with
2301 * the new chain becoming the 'current' chain (meaning it is the first in
2302 * the linked list at core->chain_first).
2304 * If (parent, i) then the new duplicated chain is inserted under the parent
2305 * at the specified index (the parent must not have a ref at that index).
2307 * If (NULL, -1) then the new duplicated chain is not inserted anywhere,
2308 * similar to if it had just been chain_alloc()'d (suitable for passing into
2309 * hammer2_chain_create() after this function returns).
2311 * NOTE! Duplication is used in order to retain the original topology to
2312 * support flush synchronization points. Both the original and the
2313 * new chain will have the same transaction id and thus the operation
2314 * appears atomic w/regards to media flushes.
2316 static void hammer2_chain_dup_fixup(hammer2_chain_t *ochain,
2317 hammer2_chain_t *nchain);
2320 hammer2_chain_duplicate(hammer2_trans_t *trans, hammer2_chain_t *parent, int i,
2321 hammer2_chain_t **chainp, hammer2_blockref_t *bref)
2323 hammer2_mount_t *hmp = trans->hmp;
2324 hammer2_blockref_t *base;
2325 hammer2_chain_t *ochain;
2326 hammer2_chain_t *nchain;
2327 hammer2_chain_t *scan;
2328 hammer2_chain_core_t *above;
2335 * First create a duplicate of the chain structure, associating
2336 * it with the same core, making it the same size, pointing it
2337 * to the same bref (the same media block).
2341 bref = &ochain->bref;
2342 nchain = hammer2_chain_alloc(hmp, trans, bref);
2343 hammer2_chain_core_alloc(nchain, ochain->core);
2344 bytes = (hammer2_off_t)1 <<
2345 (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
2346 nchain->bytes = bytes;
2347 nchain->modify_tid = ochain->modify_tid;
2349 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_NEVER);
2350 hammer2_chain_dup_fixup(ochain, nchain);
2353 * If parent is not NULL, insert into the parent at the requested
2354 * index. The newly duplicated chain must be marked MOVED and
2355 * SUBMODIFIED set in its parent(s).
2357 * Having both chains locked is extremely important for atomicy.
2361 * Locate a free blockref in the parent's array
2363 above = parent->core;
2364 KKASSERT(ccms_thread_lock_owned(&above->cst));
2366 switch(parent->bref.type) {
2367 case HAMMER2_BREF_TYPE_INODE:
2368 KKASSERT((parent->data->ipdata.op_flags &
2369 HAMMER2_OPFLAG_DIRECTDATA) == 0);
2370 KKASSERT(parent->data != NULL);
2371 base = &parent->data->ipdata.u.blockset.blockref[0];
2372 count = HAMMER2_SET_COUNT;
2374 case HAMMER2_BREF_TYPE_INDIRECT:
2375 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2376 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2379 KKASSERT(parent->data != NULL);
2380 base = &parent->data->npdata.blockref[0];
2382 count = parent->bytes / sizeof(hammer2_blockref_t);
2384 case HAMMER2_BREF_TYPE_VOLUME:
2385 KKASSERT(parent->data != NULL);
2386 base = &hmp->voldata.sroot_blockset.blockref[0];
2387 count = HAMMER2_SET_COUNT;
2389 case HAMMER2_BREF_TYPE_FREEMAP:
2390 KKASSERT(parent->data != NULL);
2391 base = &hmp->voldata.freemap_blockset.blockref[0];
2392 count = HAMMER2_SET_COUNT;
2395 panic("hammer2_chain_create: unrecognized "
2396 "blockref type: %d",
2401 KKASSERT(i >= 0 && i < count);
2403 KKASSERT((nchain->flags & HAMMER2_CHAIN_DELETED) == 0);
2404 KKASSERT(parent->refs > 0);
2406 spin_lock(&above->cst.spin);
2407 nchain->above = above;
2409 scan = hammer2_chain_find_locked(parent, i);
2410 KKASSERT(base == NULL || base[i].type == 0 ||
2412 (scan->flags & HAMMER2_CHAIN_DELETED));
2413 if (RB_INSERT(hammer2_chain_tree, &above->rbtree,
2415 panic("hammer2_chain_duplicate: collision");
2417 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_ONRBTREE);
2418 spin_unlock(&above->cst.spin);
2420 if ((nchain->flags & HAMMER2_CHAIN_MOVED) == 0) {
2421 hammer2_chain_ref(nchain);
2422 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_MOVED);
2424 hammer2_chain_setsubmod(trans, nchain);
2428 * We have to unlock ochain to flush any dirty data, asserting the
2429 * case (data == NULL) to catch any extra locks that might have been
2430 * present, then transfer state to nchain.
2432 oflags = ochain->flags;
2433 odata = ochain->data;
2434 hammer2_chain_unlock(ochain);
2435 KKASSERT((ochain->flags & HAMMER2_CHAIN_EMBEDDED) ||
2436 ochain->data == NULL);
2438 if (oflags & HAMMER2_CHAIN_INITIAL)
2439 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_INITIAL);
2442 * WARNING! We should never resolve DATA to device buffers
2443 * (XXX allow it if the caller did?), and since
2444 * we currently do not have the logical buffer cache
2445 * buffer in-hand to fix its cached physical offset
2446 * we also force the modify code to not COW it. XXX
2448 if (oflags & HAMMER2_CHAIN_MODIFIED) {
2449 if (nchain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2450 hammer2_chain_modify(trans, &nchain,
2451 HAMMER2_MODIFY_OPTDATA |
2452 HAMMER2_MODIFY_NOREALLOC |
2453 HAMMER2_MODIFY_ASSERTNOCOPY);
2454 } else if (oflags & HAMMER2_CHAIN_INITIAL) {
2455 hammer2_chain_modify(trans, &nchain,
2456 HAMMER2_MODIFY_OPTDATA |
2457 HAMMER2_MODIFY_ASSERTNOCOPY);
2459 hammer2_chain_modify(trans, &nchain,
2460 HAMMER2_MODIFY_ASSERTNOCOPY);
2462 hammer2_chain_drop(nchain);
2464 if (nchain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2465 hammer2_chain_drop(nchain);
2466 } else if (oflags & HAMMER2_CHAIN_INITIAL) {
2467 hammer2_chain_drop(nchain);
2469 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS |
2470 HAMMER2_RESOLVE_NOREF);
2471 hammer2_chain_unlock(nchain);
2474 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_SUBMODIFIED);
2480 * When the chain is in the INITIAL state we must still
2481 * ensure that a block has been assigned so MOVED processing
2482 * works as expected.
2484 KKASSERT (nchain->bref.type != HAMMER2_BREF_TYPE_DATA);
2485 hammer2_chain_modify(trans, &nchain,
2486 HAMMER2_MODIFY_OPTDATA |
2487 HAMMER2_MODIFY_ASSERTNOCOPY);
2490 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_MAYBE |
2491 HAMMER2_RESOLVE_NOREF); /* eat excess ref */
2492 hammer2_chain_unlock(nchain);
2496 * Special in-place delete-duplicate sequence which does not require a
2497 * locked parent. (*chainp) is marked DELETED and atomically replaced
2498 * with a duplicate. Atomicy is at the very-fine spin-lock level in
2499 * order to ensure that lookups do not race us.
2502 hammer2_chain_delete_duplicate(hammer2_trans_t *trans, hammer2_chain_t **chainp,
2505 hammer2_mount_t *hmp = trans->hmp;
2506 hammer2_chain_t *ochain;
2507 hammer2_chain_t *nchain;
2508 hammer2_chain_core_t *above;
2514 * First create a duplicate of the chain structure
2517 nchain = hammer2_chain_alloc(hmp, trans, &ochain->bref); /* 1 ref */
2518 if (flags & HAMMER2_DELDUP_RECORE)
2519 hammer2_chain_core_alloc(nchain, NULL);
2521 hammer2_chain_core_alloc(nchain, ochain->core);
2522 above = ochain->above;
2524 bytes = (hammer2_off_t)1 <<
2525 (int)(ochain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
2526 nchain->bytes = bytes;
2527 nchain->modify_tid = ochain->modify_tid;
2530 * Lock nchain and insert into ochain's core hierarchy, marking
2531 * ochain DELETED at the same time. Having both chains locked
2532 * is extremely important for atomicy.
2534 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_NEVER);
2535 hammer2_chain_dup_fixup(ochain, nchain);
2536 /* extra ref still present from original allocation */
2538 nchain->index = ochain->index;
2540 spin_lock(&above->cst.spin);
2541 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_ONRBTREE);
2542 ochain->delete_tid = trans->sync_tid;
2543 nchain->above = above;
2544 atomic_set_int(&ochain->flags, HAMMER2_CHAIN_DELETED);
2545 if ((ochain->flags & HAMMER2_CHAIN_MOVED) == 0) {
2546 hammer2_chain_ref(ochain);
2547 atomic_set_int(&ochain->flags, HAMMER2_CHAIN_MOVED);
2549 if (RB_INSERT(hammer2_chain_tree, &above->rbtree, nchain)) {
2550 panic("hammer2_chain_delete_duplicate: collision");
2552 spin_unlock(&above->cst.spin);
2555 * We have to unlock ochain to flush any dirty data, asserting the
2556 * case (data == NULL) to catch any extra locks that might have been
2557 * present, then transfer state to nchain.
2559 oflags = ochain->flags;
2560 odata = ochain->data;
2561 hammer2_chain_unlock(ochain); /* replacing ochain */
2562 KKASSERT(ochain->bref.type == HAMMER2_BREF_TYPE_INODE ||
2563 ochain->data == NULL);
2565 if (oflags & HAMMER2_CHAIN_INITIAL)
2566 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_INITIAL);
2569 * WARNING! We should never resolve DATA to device buffers
2570 * (XXX allow it if the caller did?), and since
2571 * we currently do not have the logical buffer cache
2572 * buffer in-hand to fix its cached physical offset
2573 * we also force the modify code to not COW it. XXX
2575 if (oflags & HAMMER2_CHAIN_MODIFIED) {
2576 if (nchain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2577 hammer2_chain_modify(trans, &nchain,
2578 HAMMER2_MODIFY_OPTDATA |
2579 HAMMER2_MODIFY_NOREALLOC |
2580 HAMMER2_MODIFY_ASSERTNOCOPY);
2581 } else if (oflags & HAMMER2_CHAIN_INITIAL) {
2582 hammer2_chain_modify(trans, &nchain,
2583 HAMMER2_MODIFY_OPTDATA |
2584 HAMMER2_MODIFY_ASSERTNOCOPY);
2586 hammer2_chain_modify(trans, &nchain,
2587 HAMMER2_MODIFY_ASSERTNOCOPY);
2589 hammer2_chain_drop(nchain);
2591 if (nchain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2592 hammer2_chain_drop(nchain);
2593 } else if (oflags & HAMMER2_CHAIN_INITIAL) {
2594 hammer2_chain_drop(nchain);
2596 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS |
2597 HAMMER2_RESOLVE_NOREF);
2598 hammer2_chain_unlock(nchain);
2603 * Unconditionally set the MOVED and SUBMODIFIED bit to force
2604 * update of parent bref and indirect blockrefs during flush.
2606 if ((nchain->flags & HAMMER2_CHAIN_MOVED) == 0) {
2607 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_MOVED);
2608 hammer2_chain_ref(nchain);
2610 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_SUBMODIFIED);
2611 hammer2_chain_setsubmod(trans, nchain);
2616 * Helper function to fixup inodes. The caller procedure stack may hold
2617 * multiple locks on ochain if it represents an inode, preventing our
2618 * unlock from retiring its state to the buffer cache.
2620 * In this situation any attempt to access the buffer cache could result
2621 * either in stale data or a deadlock. Work around the problem by copying
2622 * the embedded data directly.
2626 hammer2_chain_dup_fixup(hammer2_chain_t *ochain, hammer2_chain_t *nchain)
2628 if (ochain->data == NULL)
2630 switch(ochain->bref.type) {
2631 case HAMMER2_BREF_TYPE_INODE:
2632 KKASSERT(nchain->data == NULL);
2633 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_EMBEDDED);
2634 nchain->data = kmalloc(sizeof(nchain->data->ipdata),
2635 ochain->hmp->minode, M_WAITOK | M_ZERO);
2636 nchain->data->ipdata = ochain->data->ipdata;
2638 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2639 KKASSERT(nchain->data == NULL);
2640 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_EMBEDDED);
2641 nchain->data = kmalloc(sizeof(nchain->data->bmdata),
2642 ochain->hmp->mchain, M_WAITOK | M_ZERO);
2643 nchain->data->bmdata = ochain->data->bmdata;
2651 * Create a snapshot of the specified {parent, chain} with the specified
2654 * (a) We create a duplicate connected to the super-root as the specified
2657 * (b) We issue a restricted flush using the current transaction on the
2660 * (c) We disconnect and reallocate the duplicate's core.
2663 hammer2_chain_snapshot(hammer2_trans_t *trans, hammer2_inode_t *ip,
2664 hammer2_ioc_pfs_t *pfs)
2666 hammer2_mount_t *hmp = trans->hmp;
2667 hammer2_chain_t *chain;
2668 hammer2_chain_t *nchain;
2669 hammer2_chain_t *parent;
2670 hammer2_inode_data_t *ipdata;
2671 size_t name_len = strlen(pfs->name);
2672 hammer2_key_t lhc = hammer2_dirhash(pfs->name, name_len);
2676 * Create disconnected duplicate
2678 KKASSERT((trans->flags & HAMMER2_TRANS_RESTRICTED) == 0);
2680 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_MAYBE);
2681 hammer2_chain_duplicate(trans, NULL, -1, &nchain, NULL);
2682 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_RECYCLE |
2683 HAMMER2_CHAIN_SNAPSHOT);
2686 * Create named entry in the super-root.
2688 parent = hammer2_chain_lookup_init(hmp->schain, 0);
2690 while (error == 0) {
2691 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
2694 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
2696 hammer2_chain_unlock(chain);
2700 hammer2_chain_create(trans, &parent, &nchain, lhc, 0,
2701 HAMMER2_BREF_TYPE_INODE,
2702 HAMMER2_INODE_BYTES);
2703 hammer2_chain_modify(trans, &nchain, HAMMER2_MODIFY_ASSERTNOCOPY);
2704 hammer2_chain_lookup_done(parent);
2705 parent = NULL; /* safety */
2710 ipdata = &nchain->data->ipdata;
2711 ipdata->name_key = lhc;
2712 ipdata->name_len = name_len;
2713 ksnprintf(ipdata->filename, sizeof(ipdata->filename), "%s", pfs->name);
2716 * Set PFS type, generate a unique filesystem id, and generate
2717 * a cluster id. Use the same clid when snapshotting a PFS root,
2718 * which theoretically allows the snapshot to be used as part of
2719 * the same cluster (perhaps as a cache).
2721 ipdata->pfs_type = HAMMER2_PFSTYPE_SNAPSHOT;
2722 kern_uuidgen(&ipdata->pfs_fsid, 1);
2723 if (ip->chain == ip->pmp->rchain)
2724 ipdata->pfs_clid = ip->chain->data->ipdata.pfs_clid;
2726 kern_uuidgen(&ipdata->pfs_clid, 1);
2729 * Issue a restricted flush of the snapshot. This is a synchronous
2732 trans->flags |= HAMMER2_TRANS_RESTRICTED;
2733 kprintf("SNAPSHOTA\n");
2734 tsleep(trans, 0, "snapslp", hz*4);
2735 kprintf("SNAPSHOTB\n");
2736 hammer2_chain_flush(trans, nchain);
2737 trans->flags &= ~HAMMER2_TRANS_RESTRICTED;
2741 * Remove the link b/c nchain is a snapshot and snapshots don't
2742 * follow CHAIN_DELETED semantics ?
2747 KKASSERT(chain->duplink == nchain);
2748 KKASSERT(chain->core == nchain->core);
2749 KKASSERT(nchain->refs >= 2);
2750 chain->duplink = nchain->duplink;
2751 atomic_clear_int(&nchain->flags, HAMMER2_CHAIN_DUPTARGET);
2752 hammer2_chain_drop(nchain);
2755 kprintf("snapshot %s nchain->refs %d nchain->flags %08x\n",
2756 pfs->name, nchain->refs, nchain->flags);
2757 hammer2_chain_unlock(nchain);
2763 * Create an indirect block that covers one or more of the elements in the
2764 * current parent. Either returns the existing parent with no locking or
2765 * ref changes or returns the new indirect block locked and referenced
2766 * and leaving the original parent lock/ref intact as well.
2768 * If an error occurs, NULL is returned and *errorp is set to the error.
2770 * The returned chain depends on where the specified key falls.
2772 * The key/keybits for the indirect mode only needs to follow three rules:
2774 * (1) That all elements underneath it fit within its key space and
2776 * (2) That all elements outside it are outside its key space.
2778 * (3) When creating the new indirect block any elements in the current
2779 * parent that fit within the new indirect block's keyspace must be
2780 * moved into the new indirect block.
2782 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
2783 * keyspace the the current parent, but lookup/iteration rules will
2784 * ensure (and must ensure) that rule (2) for all parents leading up
2785 * to the nearest inode or the root volume header is adhered to. This
2786 * is accomplished by always recursing through matching keyspaces in
2787 * the hammer2_chain_lookup() and hammer2_chain_next() API.
2789 * The current implementation calculates the current worst-case keyspace by
2790 * iterating the current parent and then divides it into two halves, choosing
2791 * whichever half has the most elements (not necessarily the half containing
2792 * the requested key).
2794 * We can also opt to use the half with the least number of elements. This
2795 * causes lower-numbered keys (aka logical file offsets) to recurse through
2796 * fewer indirect blocks and higher-numbered keys to recurse through more.
2797 * This also has the risk of not moving enough elements to the new indirect
2798 * block and being forced to create several indirect blocks before the element
2801 * Must be called with an exclusively locked parent.
2803 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
2804 hammer2_key_t *keyp, int keybits,
2805 hammer2_blockref_t *base, int count);
2806 static int hammer2_chain_indkey_normal(hammer2_chain_t *parent,
2807 hammer2_key_t *keyp, int keybits,
2808 hammer2_blockref_t *base, int count);
2811 hammer2_chain_create_indirect(hammer2_trans_t *trans, hammer2_chain_t *parent,
2812 hammer2_key_t create_key, int create_bits,
2813 int for_type, int *errorp)
2815 hammer2_mount_t *hmp = trans->hmp;
2816 hammer2_chain_core_t *above;
2817 hammer2_chain_core_t *icore;
2818 hammer2_blockref_t *base;
2819 hammer2_blockref_t *bref;
2820 hammer2_chain_t *chain;
2821 hammer2_chain_t *child;
2822 hammer2_chain_t *ichain;
2823 hammer2_chain_t dummy;
2824 hammer2_key_t key = create_key;
2825 int keybits = create_bits;
2831 * Calculate the base blockref pointer or NULL if the chain
2832 * is known to be empty. We need to calculate the array count
2833 * for RB lookups either way.
2835 KKASSERT(ccms_thread_lock_owned(&parent->core->cst));
2837 above = parent->core;
2839 /*hammer2_chain_modify(trans, &parent, HAMMER2_MODIFY_OPTDATA);*/
2840 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2843 switch(parent->bref.type) {
2844 case HAMMER2_BREF_TYPE_INODE:
2845 count = HAMMER2_SET_COUNT;
2847 case HAMMER2_BREF_TYPE_INDIRECT:
2848 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2849 count = parent->bytes / sizeof(hammer2_blockref_t);
2851 case HAMMER2_BREF_TYPE_VOLUME:
2852 count = HAMMER2_SET_COUNT;
2854 case HAMMER2_BREF_TYPE_FREEMAP:
2855 count = HAMMER2_SET_COUNT;
2858 panic("hammer2_chain_create_indirect: "
2859 "unrecognized blockref type: %d",
2865 switch(parent->bref.type) {
2866 case HAMMER2_BREF_TYPE_INODE:
2867 base = &parent->data->ipdata.u.blockset.blockref[0];
2868 count = HAMMER2_SET_COUNT;
2870 case HAMMER2_BREF_TYPE_INDIRECT:
2871 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2872 base = &parent->data->npdata.blockref[0];
2873 count = parent->bytes / sizeof(hammer2_blockref_t);
2875 case HAMMER2_BREF_TYPE_VOLUME:
2876 base = &hmp->voldata.sroot_blockset.blockref[0];
2877 count = HAMMER2_SET_COUNT;
2879 case HAMMER2_BREF_TYPE_FREEMAP:
2880 base = &hmp->voldata.freemap_blockset.blockref[0];
2881 count = HAMMER2_SET_COUNT;
2884 panic("hammer2_chain_create_indirect: "
2885 "unrecognized blockref type: %d",
2893 * dummy used in later chain allocation (no longer used for lookups).
2895 bzero(&dummy, sizeof(dummy));
2896 dummy.delete_tid = HAMMER2_MAX_TID;
2899 * When creating an indirect block for a freemap node or leaf
2900 * the key/keybits must be fitted to static radix levels because
2901 * particular radix levels use particular reserved blocks in the
2904 * This routine calculates the key/radix of the indirect block
2905 * we need to create, and whether it is on the high-side or the
2908 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2909 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2910 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
2913 keybits = hammer2_chain_indkey_normal(parent, &key, keybits,
2918 * Normalize the key for the radix being represented, keeping the
2919 * high bits and throwing away the low bits.
2921 key &= ~(((hammer2_key_t)1 << keybits) - 1);
2924 * How big should our new indirect block be? It has to be at least
2925 * as large as its parent.
2927 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE)
2928 nbytes = HAMMER2_IND_BYTES_MIN;
2930 nbytes = HAMMER2_IND_BYTES_MAX;
2931 if (nbytes < count * sizeof(hammer2_blockref_t))
2932 nbytes = count * sizeof(hammer2_blockref_t);
2935 * Ok, create our new indirect block
2937 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2938 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2939 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
2941 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
2943 dummy.bref.key = key;
2944 dummy.bref.keybits = keybits;
2945 dummy.bref.data_off = hammer2_getradix(nbytes);
2946 dummy.bref.methods = parent->bref.methods;
2948 ichain = hammer2_chain_alloc(hmp, trans, &dummy.bref);
2949 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
2950 hammer2_chain_core_alloc(ichain, NULL);
2951 icore = ichain->core;
2952 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
2953 hammer2_chain_drop(ichain); /* excess ref from alloc */
2956 * We have to mark it modified to allocate its block, but use
2957 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
2958 * it won't be acted upon by the flush code.
2960 * XXX leave the node unmodified, depend on the SUBMODIFIED
2961 * flush to assign and modify parent blocks.
2963 hammer2_chain_modify(trans, &ichain, HAMMER2_MODIFY_OPTDATA);
2966 * Iterate the original parent and move the matching brefs into
2967 * the new indirect block.
2969 * At the same time locate an empty slot (or what will become an
2970 * empty slot) and assign the new indirect block to that slot.
2972 * XXX handle flushes.
2974 spin_lock(&above->cst.spin);
2975 for (i = 0; i < count; ++i) {
2977 * For keying purposes access the bref from the media or
2978 * from our in-memory cache. In cases where the in-memory
2979 * cache overrides the media the keyrefs will be the same
2980 * anyway so we can avoid checking the cache when the media
2983 child = hammer2_chain_find_locked(parent, i);
2985 if (child->flags & HAMMER2_CHAIN_DELETED) {
2986 if (ichain->index < 0)
2990 bref = &child->bref;
2991 } else if (base && base[i].type) {
2994 if (ichain->index < 0)
3000 * Skip keys that are not within the key/radix of the new
3001 * indirect block. They stay in the parent.
3003 if ((~(((hammer2_key_t)1 << keybits) - 1) &
3004 (key ^ bref->key)) != 0) {
3009 * This element is being moved from the parent, its slot
3010 * is available for our new indirect block.
3012 if (ichain->index < 0)
3016 * Load the new indirect block by acquiring or allocating
3017 * the related chain entries, then move them to the new
3018 * parent (ichain) by deleting them from their old location
3019 * and inserting a duplicate of the chain and any modified
3020 * sub-chain in the new location.
3022 * We must set MOVED in the chain being duplicated and
3023 * SUBMODIFIED in the parent(s) so the flush code knows
3024 * what is going on. The latter is done after the loop.
3026 * WARNING! above->cst.spin must be held when parent is
3027 * modified, even though we own the full blown lock,
3028 * to deal with setsubmod and rename races.
3029 * (XXX remove this req).
3031 spin_unlock(&above->cst.spin);
3032 chain = hammer2_chain_get(parent, i, HAMMER2_LOOKUP_NODATA);
3033 hammer2_chain_delete(trans, chain);
3034 hammer2_chain_duplicate(trans, ichain, i, &chain, NULL);
3035 hammer2_chain_unlock(chain);
3036 KKASSERT(parent->refs > 0);
3038 spin_lock(&above->cst.spin);
3040 spin_unlock(&above->cst.spin);
3043 * Insert the new indirect block into the parent now that we've
3044 * cleared out some entries in the parent. We calculated a good
3045 * insertion index in the loop above (ichain->index).
3047 * We don't have to set MOVED here because we mark ichain modified
3048 * down below (so the normal modified -> flush -> set-moved sequence
3051 * The insertion shouldn't race as this is a completely new block
3052 * and the parent is locked.
3054 if (ichain->index < 0)
3055 kprintf("indirect parent %p count %d key %016jx/%d\n",
3056 parent, count, (intmax_t)key, keybits);
3057 KKASSERT(ichain->index >= 0);
3058 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
3059 spin_lock(&above->cst.spin);
3060 if (RB_INSERT(hammer2_chain_tree, &above->rbtree, ichain))
3061 panic("hammer2_chain_create_indirect: ichain insertion");
3062 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_ONRBTREE);
3063 ichain->above = above;
3064 spin_unlock(&above->cst.spin);
3067 * Mark the new indirect block modified after insertion, which
3068 * will propagate up through parent all the way to the root and
3069 * also allocate the physical block in ichain for our caller,
3070 * and assign ichain->data to a pre-zero'd space (because there
3071 * is not prior data to copy into it).
3073 * We have to set SUBMODIFIED in ichain's flags manually so the
3074 * flusher knows it has to recurse through it to get to all of
3075 * our moved blocks, then call setsubmod() to set the bit
3078 /*hammer2_chain_modify(trans, &ichain, HAMMER2_MODIFY_OPTDATA);*/
3079 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_SUBMODIFIED);
3080 hammer2_chain_setsubmod(trans, ichain);
3083 * Figure out what to return.
3085 if (~(((hammer2_key_t)1 << keybits) - 1) &
3086 (create_key ^ key)) {
3088 * Key being created is outside the key range,
3089 * return the original parent.
3091 hammer2_chain_unlock(ichain);
3094 * Otherwise its in the range, return the new parent.
3095 * (leave both the new and old parent locked).
3104 * Calculate the keybits and highside/lowside of the freemap node the
3105 * caller is creating.
3107 * This routine will specify the next higher-level freemap key/radix
3108 * representing the lowest-ordered set. By doing so, eventually all
3109 * low-ordered sets will be moved one level down.
3111 * We have to be careful here because the freemap reserves a limited
3112 * number of blocks for a limited number of levels. So we can't just
3113 * push indiscriminately.
3116 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
3117 int keybits, hammer2_blockref_t *base, int count)
3119 hammer2_chain_core_t *above;
3120 hammer2_chain_t *child;
3121 hammer2_blockref_t *bref;
3128 above = parent->core;
3134 * Calculate the range of keys in the array being careful to skip
3135 * slots which are overridden with a deletion.
3137 spin_lock(&above->cst.spin);
3138 for (i = 0; i < count; ++i) {
3139 child = hammer2_chain_find_locked(parent, i);
3141 if (child->flags & HAMMER2_CHAIN_DELETED)
3143 bref = &child->bref;
3144 } else if (base && base[i].type) {
3150 if (keybits > bref->keybits) {
3152 keybits = bref->keybits;
3153 } else if (keybits == bref->keybits && bref->key < key) {
3157 spin_unlock(&above->cst.spin);
3160 * Return the keybits for a higher-level FREEMAP_NODE covering
3164 case HAMMER2_FREEMAP_LEVEL0_RADIX:
3165 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
3167 case HAMMER2_FREEMAP_LEVEL1_RADIX:
3168 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
3170 case HAMMER2_FREEMAP_LEVEL2_RADIX:
3171 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
3173 case HAMMER2_FREEMAP_LEVEL3_RADIX:
3174 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
3176 case HAMMER2_FREEMAP_LEVEL4_RADIX:
3177 panic("hammer2_chain_indkey_freemap: level too high");
3180 panic("hammer2_chain_indkey_freemap: bad radix");
3189 * Calculate the keybits and highside/lowside of the indirect block the
3190 * caller is creating.
3193 hammer2_chain_indkey_normal(hammer2_chain_t *parent, hammer2_key_t *keyp,
3194 int keybits, hammer2_blockref_t *base, int count)
3196 hammer2_chain_core_t *above;
3197 hammer2_chain_t *child;
3198 hammer2_blockref_t *bref;
3206 above = parent->core;
3211 * Calculate the range of keys in the array being careful to skip
3212 * slots which are overridden with a deletion. Once the scan
3213 * completes we will cut the key range in half and shift half the
3214 * range into the new indirect block.
3216 spin_lock(&above->cst.spin);
3217 for (i = 0; i < count; ++i) {
3218 child = hammer2_chain_find_locked(parent, i);
3220 if (child->flags & HAMMER2_CHAIN_DELETED)
3222 bref = &child->bref;
3223 } else if (base && base[i].type) {
3230 * Expand our calculated key range (key, keybits) to fit
3231 * the scanned key. nkeybits represents the full range
3232 * that we will later cut in half (two halves @ nkeybits - 1).
3235 if (nkeybits < bref->keybits) {
3236 if (bref->keybits > 64) {
3237 kprintf("bad bref index %d chain %p bref %p\n",
3241 nkeybits = bref->keybits;
3243 while (nkeybits < 64 &&
3244 (~(((hammer2_key_t)1 << nkeybits) - 1) &
3245 (key ^ bref->key)) != 0) {
3250 * If the new key range is larger we have to determine
3251 * which side of the new key range the existing keys fall
3252 * under by checking the high bit, then collapsing the
3253 * locount into the hicount or vise-versa.
3255 if (keybits != nkeybits) {
3256 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
3267 * The newly scanned key will be in the lower half or the
3268 * higher half of the (new) key range.
3270 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
3275 spin_unlock(&above->cst.spin);
3276 bref = NULL; /* now invalid (safety) */
3279 * Adjust keybits to represent half of the full range calculated
3280 * above (radix 63 max)
3285 * Select whichever half contains the most elements. Theoretically
3286 * we can select either side as long as it contains at least one
3287 * element (in order to ensure that a free slot is present to hold
3288 * the indirect block).
3290 if (hammer2_indirect_optimize) {
3292 * Insert node for least number of keys, this will arrange
3293 * the first few blocks of a large file or the first few
3294 * inodes in a directory with fewer indirect blocks when
3297 if (hicount < locount && hicount != 0)
3298 key |= (hammer2_key_t)1 << keybits;
3300 key &= ~(hammer2_key_t)1 << keybits;
3303 * Insert node for most number of keys, best for heavily
3306 if (hicount > locount)
3307 key |= (hammer2_key_t)1 << keybits;
3309 key &= ~(hammer2_key_t)1 << keybits;
3317 * Sets CHAIN_DELETED and CHAIN_MOVED in the chain being deleted and
3318 * set chain->delete_tid.
3320 * This function does NOT generate a modification to the parent. It
3321 * would be nearly impossible to figure out which parent to modify anyway.
3322 * Such modifications are handled by the flush code and are properly merged
3323 * using the flush synchronization point.
3325 * The find/get code will properly overload the RBTREE check on top of
3326 * the bref check to detect deleted entries.
3328 * This function is NOT recursive. Any entity already pushed into the
3329 * chain (such as an inode) may still need visibility into its contents,
3330 * as well as the ability to read and modify the contents. For example,
3331 * for an unlinked file which is still open.
3333 * NOTE: This function does NOT set chain->modify_tid, allowing future
3334 * code to distinguish between live and deleted chains by testing
3337 * NOTE: Deletions normally do not occur in the middle of a duplication
3338 * chain but we use a trick for hardlink migration that refactors
3339 * the originating inode without deleting it, so we make no assumptions
3343 hammer2_chain_delete(hammer2_trans_t *trans, hammer2_chain_t *chain)
3345 KKASSERT(ccms_thread_lock_owned(&chain->core->cst));
3348 * Nothing to do if already marked.
3350 if (chain->flags & HAMMER2_CHAIN_DELETED)
3354 * We must set MOVED along with DELETED for the flush code to
3355 * recognize the operation and properly disconnect the chain
3358 * The setting of DELETED causes finds, lookups, and _next iterations
3359 * to no longer recognize the chain. RB_SCAN()s will still have
3360 * visibility (needed for flush serialization points).
3362 * We need the spinlock on the core whos RBTREE contains chain
3363 * to protect against races.
3365 spin_lock(&chain->above->cst.spin);
3366 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3367 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
3368 hammer2_chain_ref(chain);
3369 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
3371 chain->delete_tid = trans->sync_tid;
3372 spin_unlock(&chain->above->cst.spin);
3373 hammer2_chain_setsubmod(trans, chain);
3377 hammer2_chain_wait(hammer2_chain_t *chain)
3379 tsleep(chain, 0, "chnflw", 1);
3384 adjreadcounter(hammer2_blockref_t *bref, size_t bytes)
3388 switch(bref->type) {
3389 case HAMMER2_BREF_TYPE_DATA:
3390 counterp = &hammer2_iod_file_read;
3392 case HAMMER2_BREF_TYPE_INODE:
3393 counterp = &hammer2_iod_meta_read;
3395 case HAMMER2_BREF_TYPE_INDIRECT:
3396 counterp = &hammer2_iod_indr_read;
3398 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3399 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3400 counterp = &hammer2_iod_fmap_read;
3403 counterp = &hammer2_iod_volu_read;