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>
69 static int hammer2_indirect_optimize; /* XXX SYSCTL */
71 static hammer2_chain_t *hammer2_chain_create_indirect(
72 hammer2_trans_t *trans, hammer2_chain_t *parent,
73 hammer2_key_t key, int keybits, int *errorp);
76 * We use a red-black tree to guarantee safe lookups under shared locks.
78 * Chains can be overloaded onto the same index, creating a different
79 * view of a blockref table based on a transaction id. The RBTREE
80 * deconflicts the view by sub-sorting on delete_tid.
82 * NOTE: Any 'current' chain which is not yet deleted will have a
83 * delete_tid of HAMMER2_MAX_TID (0xFFF....FFFLLU).
85 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
88 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
90 if (chain1->index < chain2->index)
92 if (chain1->index > chain2->index)
94 if (chain1->delete_tid < chain2->delete_tid)
96 if (chain1->delete_tid > chain2->delete_tid)
102 * Flag chain->parent SUBMODIFIED recursively up to the root. The
103 * recursion can terminate when a parent is encountered with SUBMODIFIED
104 * already set. The flag is NOT set on the passed-in chain.
106 * This can be confusing because even though chains are multi-homed,
107 * each chain has a specific idea of its parent (chain->parent) which
110 * This flag is used by the flusher's downward recursion to detect
111 * modifications and can only be cleared bottom-up.
113 * The parent pointer is protected by all the modified children below it
114 * and cannot be changed until they have all been flushed. However, setsubmod
115 * operations on new modifications can race flushes in progress, so we use
116 * the chain->core->cst.spin lock to handle collisions.
119 hammer2_chain_parent_setsubmod(hammer2_chain_t *chain)
121 hammer2_chain_t *parent;
122 hammer2_chain_core_t *core;
124 while ((parent = chain->parent) != NULL) {
126 spin_lock(&core->cst.spin);
127 if (parent->flags & HAMMER2_CHAIN_SUBMODIFIED) {
128 spin_unlock(&core->cst.spin);
131 atomic_set_int(&parent->flags, HAMMER2_CHAIN_SUBMODIFIED);
132 spin_unlock(&core->cst.spin);
138 * Allocate a new disconnected chain element representing the specified
139 * bref. chain->refs is set to 1 and the passed bref is copied to
140 * chain->bref. chain->bytes is derived from the bref.
142 * chain->core is NOT allocated and the media data and bp pointers are left
143 * NULL. The caller must call chain_core_alloc() to allocate or associate
144 * a core with the chain.
146 * NOTE: Returns a referenced but unlocked (because there is no core) chain.
149 hammer2_chain_alloc(hammer2_mount_t *hmp, hammer2_blockref_t *bref)
151 hammer2_chain_t *chain;
152 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
155 * Construct the appropriate system structure.
158 case HAMMER2_BREF_TYPE_INODE:
159 case HAMMER2_BREF_TYPE_INDIRECT:
160 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
161 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
162 case HAMMER2_BREF_TYPE_DATA:
163 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
164 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
166 case HAMMER2_BREF_TYPE_VOLUME:
168 panic("hammer2_chain_alloc volume type illegal for op");
171 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
177 chain->index = -1; /* not yet assigned */
178 chain->bytes = bytes;
180 chain->flags = HAMMER2_CHAIN_ALLOCATED;
181 chain->delete_tid = HAMMER2_MAX_TID;
187 * Associate an existing core with the chain or allocate a new core.
189 * The core is not locked. No additional refs on the chain are made.
192 hammer2_chain_core_alloc(hammer2_chain_t *chain, hammer2_chain_core_t *core)
194 KKASSERT(chain->core == NULL);
197 core = kmalloc(sizeof(*core), chain->hmp->mchain,
199 RB_INIT(&core->rbtree);
202 ccms_cst_init(&core->cst, chain);
204 atomic_add_int(&core->sharecnt, 1);
210 * Deallocate a chain after the caller has transitioned its refs to 0
211 * and disassociated it from its parent.
213 * We must drop sharecnt on the core (if any) and handle its 1->0 transition
217 hammer2_chain_dealloc(hammer2_chain_t *chain)
219 hammer2_chain_core_t *core;
222 * Chain's flags are expected to be sane.
224 KKASSERT((chain->flags & (HAMMER2_CHAIN_MOVED |
225 HAMMER2_CHAIN_MODIFIED |
226 HAMMER2_CHAIN_ONRBTREE)) == 0);
227 KKASSERT(chain->duplink == NULL);
230 * Disconnect chain->core from chain and free core if it was the
231 * last core. If any children are present in the core's rbtree
232 * they cannot have a pointer to our chain by definition because
233 * our chain's refs have dropped to 0. If this is the last sharecnt
234 * on core, then core's rbtree must be empty by definition.
236 if ((core = chain->core) != NULL) {
238 * Other chains may reference the same core so the core's
239 * spinlock is needed to safely disconnect it.
241 spin_lock(&core->cst.spin);
243 if (atomic_fetchadd_int(&core->sharecnt, -1) == 1) {
244 spin_unlock(&core->cst.spin);
245 KKASSERT(RB_EMPTY(&core->rbtree));
246 KKASSERT(core->cst.count == 0);
247 KKASSERT(core->cst.upgrade == 0);
248 kfree(core, chain->hmp->mchain);
250 spin_unlock(&core->cst.spin);
252 core = NULL; /* safety */
256 * Finally free the structure and return for possible recursion.
258 hammer2_chain_free(chain);
262 * Free a disconnected chain element.
265 hammer2_chain_free(hammer2_chain_t *chain)
267 hammer2_mount_t *hmp = chain->hmp;
269 switch(chain->bref.type) {
270 case HAMMER2_BREF_TYPE_VOLUME:
273 case HAMMER2_BREF_TYPE_INODE:
275 kfree(chain->data, hmp->minode);
280 KKASSERT(chain->data == NULL);
284 KKASSERT(chain->core == NULL);
285 KKASSERT(chain->bp == NULL);
288 if (chain->flags & HAMMER2_CHAIN_ALLOCATED)
289 kfree(chain, hmp->mchain);
293 * Add a reference to a chain element, preventing its destruction.
296 hammer2_chain_ref(hammer2_chain_t *chain)
298 atomic_add_int(&chain->refs, 1);
302 * Drop the caller's reference to the chain. When the ref count drops to
303 * zero this function will disassociate the chain from its parent and
304 * deallocate it, then recursely drop the parent using the implied ref
305 * from the chain's chain->parent.
307 * WARNING! Just because we are able to deallocate a chain doesn't mean
308 * that chain->core->rbtree is empty. There can still be a sharecnt
309 * on chain->core and RBTREE entries that refer to different parents.
311 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain);
314 hammer2_chain_drop(hammer2_chain_t *chain)
325 chain = hammer2_chain_lastdrop(chain);
326 /* recursively drop parent or retry same */
327 } else if (atomic_cmpset_int(&chain->refs, 1, 0)) {
328 hammer2_chain_dealloc(chain);
330 /* no parent to recurse on */
332 /* retry the same chain */
335 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
337 /* retry the same chain */
343 * Safe handling of the 1->0 transition on chain when the chain has a
346 * NOTE: A chain can only be removed from its parent core's RBTREE on
347 * the 1->0 transition by definition. No other code is allowed
348 * to remove chain from its RBTREE, so no race is possible.
352 hammer2_chain_lastdrop(hammer2_chain_t *chain)
354 hammer2_chain_t *parent;
355 hammer2_chain_core_t *parent_core;
357 parent = chain->parent;
358 parent_core = parent->core;
359 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
361 spin_lock(&parent_core->cst.spin);
362 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
363 RB_REMOVE(hammer2_chain_tree, &parent_core->rbtree, chain);
364 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
365 chain->parent = NULL; /* NULL field, must drop implied ref */
366 spin_unlock(&parent_core->cst.spin);
367 if (chain->duplink) {
368 hammer2_chain_drop(chain->duplink);
369 chain->duplink = NULL;
371 hammer2_chain_dealloc(chain);
372 chain = parent; /* recursively drop parent */
374 spin_unlock(&parent_core->cst.spin);
380 * Ref and lock a chain element, acquiring its data with I/O if necessary,
381 * and specify how you would like the data to be resolved.
383 * Returns 0 on success or an error code if the data could not be acquired.
384 * The chain element is locked either way.
386 * The lock is allowed to recurse, multiple locking ops will aggregate
387 * the requested resolve types. Once data is assigned it will not be
388 * removed until the last unlock.
390 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
391 * (typically used to avoid device/logical buffer
394 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
395 * the INITIAL-create state (indirect blocks only).
397 * Do not resolve data elements for DATA chains.
398 * (typically used to avoid device/logical buffer
401 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
403 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
404 * it will be locked exclusive.
406 * NOTE: Embedded elements (volume header, inodes) are always resolved
409 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
410 * element will instantiate and zero its buffer, and flush it on
413 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
414 * so as not to instantiate a device buffer, which could alias against
415 * a logical file buffer. However, if ALWAYS is specified the
416 * device buffer will be instantiated anyway.
418 * WARNING! If data must be fetched a shared lock will temporarily be
419 * upgraded to exclusive. However, a deadlock can occur if
420 * the caller owns more than one shared lock.
423 hammer2_chain_lock(hammer2_chain_t *chain, int how)
425 hammer2_mount_t *hmp;
426 hammer2_chain_core_t *core;
427 hammer2_blockref_t *bref;
437 * Ref and lock the element. Recursive locks are allowed.
439 if ((how & HAMMER2_RESOLVE_NOREF) == 0)
440 hammer2_chain_ref(chain);
442 KKASSERT(hmp != NULL);
445 * Get the appropriate lock.
448 if (how & HAMMER2_RESOLVE_SHARED)
449 ccms_thread_lock(&core->cst, CCMS_STATE_SHARED);
451 ccms_thread_lock(&core->cst, CCMS_STATE_EXCLUSIVE);
454 * If we already have a valid data pointer no further action is
461 * Do we have to resolve the data?
463 switch(how & HAMMER2_RESOLVE_MASK) {
464 case HAMMER2_RESOLVE_NEVER:
466 case HAMMER2_RESOLVE_MAYBE:
467 if (chain->flags & HAMMER2_CHAIN_INITIAL)
469 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
471 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
474 case HAMMER2_RESOLVE_ALWAYS:
479 * Upgrade to an exclusive lock so we can safely manipulate the
480 * buffer cache. If another thread got to it before us we
483 ostate = ccms_thread_lock_upgrade(&core->cst);
485 ccms_thread_lock_restore(&core->cst, ostate);
490 * We must resolve to a device buffer, either by issuing I/O or
491 * by creating a zero-fill element. We do not mark the buffer
492 * dirty when creating a zero-fill element (the hammer2_chain_modify()
493 * API must still be used to do that).
495 * The device buffer is variable-sized in powers of 2 down
496 * to HAMMER2_MINALLOCSIZE (typically 1K). A 64K physical storage
497 * chunk always contains buffers of the same size. (XXX)
499 * The minimum physical IO size may be larger than the variable
504 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
505 bbytes = HAMMER2_MINIOSIZE;
506 pbase = bref->data_off & ~(hammer2_off_t)(bbytes - 1);
507 peof = (pbase + HAMMER2_PBUFSIZE64) & ~HAMMER2_PBUFMASK64;
508 boff = bref->data_off & HAMMER2_OFF_MASK & (bbytes - 1);
509 KKASSERT(pbase != 0);
512 * The getblk() optimization can only be used on newly created
513 * elements if the physical block size matches the request.
515 if ((chain->flags & HAMMER2_CHAIN_INITIAL) &&
516 chain->bytes == bbytes) {
517 chain->bp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
519 } else if (hammer2_cluster_enable) {
520 error = cluster_read(hmp->devvp, peof, pbase, bbytes,
521 HAMMER2_PBUFSIZE, HAMMER2_PBUFSIZE,
524 error = bread(hmp->devvp, pbase, bbytes, &chain->bp);
528 kprintf("hammer2_chain_get: I/O error %016jx: %d\n",
529 (intmax_t)pbase, error);
532 ccms_thread_lock_restore(&core->cst, ostate);
537 * Zero the data area if the chain is in the INITIAL-create state.
538 * Mark the buffer for bdwrite().
540 bdata = (char *)chain->bp->b_data + boff;
541 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
542 bzero(bdata, chain->bytes);
543 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
547 * Setup the data pointer, either pointing it to an embedded data
548 * structure and copying the data from the buffer, or pointing it
551 * The buffer is not retained when copying to an embedded data
552 * structure in order to avoid potential deadlocks or recursions
553 * on the same physical buffer.
555 switch (bref->type) {
556 case HAMMER2_BREF_TYPE_VOLUME:
558 * Copy data from bp to embedded buffer
560 panic("hammer2_chain_lock: called on unresolved volume header");
563 KKASSERT(pbase == 0);
564 KKASSERT(chain->bytes == HAMMER2_PBUFSIZE);
565 bcopy(bdata, &hmp->voldata, chain->bytes);
566 chain->data = (void *)&hmp->voldata;
571 case HAMMER2_BREF_TYPE_INODE:
573 * Copy data from bp to embedded buffer, do not retain the
576 KKASSERT(chain->bytes == sizeof(chain->data->ipdata));
577 chain->data = kmalloc(sizeof(chain->data->ipdata),
578 hmp->minode, M_WAITOK | M_ZERO);
579 bcopy(bdata, &chain->data->ipdata, chain->bytes);
583 case HAMMER2_BREF_TYPE_INDIRECT:
584 case HAMMER2_BREF_TYPE_DATA:
585 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
586 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
587 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
590 * Point data at the device buffer and leave bp intact.
592 chain->data = (void *)bdata;
597 * Make sure the bp is not specifically owned by this thread before
598 * restoring to a possibly shared lock, so another hammer2 thread
602 BUF_KERNPROC(chain->bp);
603 ccms_thread_lock_restore(&core->cst, ostate);
608 * Unlock and deref a chain element.
610 * On the last lock release any non-embedded data (chain->bp) will be
614 hammer2_chain_unlock(hammer2_chain_t *chain)
616 hammer2_chain_core_t *core = chain->core;
620 * Release the CST lock but with a special 1->0 transition case
621 * to also drop the refs on chain. Multiple CST locks only
623 * Returns non-zero if lock references remain. When zero is
624 * returned the last lock reference is retained and any shared
625 * lock is upgraded to an exclusive lock for final disposition.
627 if (ccms_thread_unlock_zero(&core->cst)) {
628 KKASSERT(chain->refs > 1);
629 atomic_add_int(&chain->refs, -1);
634 * Shortcut the case if the data is embedded or not resolved.
636 * Do NOT NULL out chain->data (e.g. inode data), it might be
639 * The DIRTYBP flag is non-applicable in this situation and can
640 * be cleared to keep the flags state clean.
642 if (chain->bp == NULL) {
643 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
644 ccms_thread_unlock(&core->cst);
645 hammer2_chain_drop(chain);
652 if ((chain->flags & HAMMER2_CHAIN_DIRTYBP) == 0) {
654 } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
655 switch(chain->bref.type) {
656 case HAMMER2_BREF_TYPE_DATA:
657 counterp = &hammer2_ioa_file_write;
659 case HAMMER2_BREF_TYPE_INODE:
660 counterp = &hammer2_ioa_meta_write;
662 case HAMMER2_BREF_TYPE_INDIRECT:
663 counterp = &hammer2_ioa_indr_write;
665 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
666 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
667 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
668 counterp = &hammer2_ioa_fmap_write;
671 counterp = &hammer2_ioa_volu_write;
676 switch(chain->bref.type) {
677 case HAMMER2_BREF_TYPE_DATA:
678 counterp = &hammer2_iod_file_write;
680 case HAMMER2_BREF_TYPE_INODE:
681 counterp = &hammer2_iod_meta_write;
683 case HAMMER2_BREF_TYPE_INDIRECT:
684 counterp = &hammer2_iod_indr_write;
686 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
687 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
688 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
689 counterp = &hammer2_iod_fmap_write;
692 counterp = &hammer2_iod_volu_write;
701 * If a device buffer was used for data be sure to destroy the
702 * buffer when we are done to avoid aliases (XXX what about the
703 * underlying VM pages?).
705 * NOTE: Freemap leaf's use reserved blocks and thus no aliasing
708 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
709 chain->bp->b_flags |= B_RELBUF;
712 * The DIRTYBP flag tracks whether we have to bdwrite() the buffer
713 * or not. The flag will get re-set when chain_modify() is called,
714 * even if MODIFIED is already set, allowing the OS to retire the
715 * buffer independent of a hammer2 flus.
718 if (chain->flags & HAMMER2_CHAIN_DIRTYBP) {
719 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
720 if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
721 atomic_clear_int(&chain->flags,
722 HAMMER2_CHAIN_IOFLUSH);
723 chain->bp->b_flags |= B_RELBUF;
724 cluster_awrite(chain->bp);
726 chain->bp->b_flags |= B_CLUSTEROK;
730 if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
731 atomic_clear_int(&chain->flags,
732 HAMMER2_CHAIN_IOFLUSH);
733 chain->bp->b_flags |= B_RELBUF;
736 /* bp might still be dirty */
741 ccms_thread_unlock(&core->cst);
742 hammer2_chain_drop(chain);
746 * Resize the chain's physical storage allocation in-place. This may
747 * replace the passed-in chain with a new chain.
749 * Chains can be resized smaller without reallocating the storage.
750 * Resizing larger will reallocate the storage.
752 * Must be passed an exclusively locked parent and chain, returns a new
753 * exclusively locked chain at the same index and unlocks the old chain.
754 * Flushes the buffer if necessary.
756 * If you want the resize code to copy the data to the new block then the
757 * caller should lock the chain RESOLVE_MAYBE or RESOLVE_ALWAYS.
759 * If the caller already holds a logical buffer containing the data and
760 * intends to bdwrite() that buffer resolve with RESOLVE_NEVER. The resize
761 * operation will then not copy the (stale) data from the media.
763 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
764 * to avoid instantiating a device buffer that conflicts with the vnode
767 * XXX flags currently ignored, uses chain->bp to detect data/no-data.
768 * XXX return error if cannot resize.
771 hammer2_chain_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
773 hammer2_chain_t *parent, hammer2_chain_t **chainp,
774 int nradix, int flags)
776 hammer2_mount_t *hmp = trans->hmp;
777 hammer2_chain_t *chain = *chainp;
788 * Only data and indirect blocks can be resized for now.
789 * (The volu root, inodes, and freemap elements use a fixed size).
791 KKASSERT(chain != &hmp->vchain);
792 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
793 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
796 * Nothing to do if the element is already the proper size
798 obytes = chain->bytes;
799 nbytes = 1U << nradix;
800 if (obytes == nbytes)
804 * Delete the old chain and duplicate it at the same (parent, index),
805 * returning a new chain. This allows the old chain to still be
806 * used by the flush code. Duplication occurs in-place.
808 * NOTE: If we are not crossing a synchronization point the
809 * duplication code will simply reuse the existing chain
812 hammer2_chain_delete(trans, parent, chain);
813 hammer2_chain_duplicate(trans, parent, chain->index, &chain);
816 * Set MODIFIED and add a chain ref to prevent destruction. Both
817 * modified flags share the same ref. (duplicated chains do not
818 * start out MODIFIED unless possibly if the duplication code
819 * decided to reuse the existing chain as-is).
821 * If the chain is already marked MODIFIED then we can safely
822 * return the previous allocation to the pool without having to
823 * worry about snapshots.
825 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
826 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
827 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
828 hammer2_chain_ref(chain);
830 hammer2_freemap_free(hmp, chain->bref.data_off,
835 * Relocate the block, even if making it smaller (because different
836 * block sizes may be in different regions).
838 chain->bref.data_off = hammer2_freemap_alloc(hmp, chain->bref.type,
840 chain->bytes = nbytes;
841 /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */
844 * The device buffer may be larger than the allocation size.
846 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
847 bbytes = HAMMER2_MINIOSIZE;
848 pbase = chain->bref.data_off & ~(hammer2_off_t)(bbytes - 1);
849 boff = chain->bref.data_off & HAMMER2_OFF_MASK & (bbytes - 1);
852 * Only copy the data if resolved, otherwise the caller is
856 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
857 chain->bref.type == HAMMER2_BREF_TYPE_DATA);
858 KKASSERT(chain != &hmp->vchain); /* safety */
861 * The getblk() optimization can only be used if the
862 * physical block size matches the request.
864 if (nbytes == bbytes) {
865 nbp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
868 error = bread(hmp->devvp, pbase, bbytes, &nbp);
869 KKASSERT(error == 0);
871 bdata = (char *)nbp->b_data + boff;
874 * chain->bp and chain->data represent the on-disk version
875 * of the data, where as the passed-in bp is usually a
876 * more up-to-date logical buffer. However, there is no
877 * need to synchronize the more up-to-date data in (bp)
878 * as it will do that on its own when it flushes.
880 if (nbytes < obytes) {
881 bcopy(chain->data, bdata, nbytes);
883 bcopy(chain->data, bdata, obytes);
884 bzero(bdata + obytes, nbytes - obytes);
888 * NOTE: The INITIAL state of the chain is left intact.
889 * We depend on hammer2_chain_modify() to do the
892 * NOTE: We set B_NOCACHE to throw away the previous bp and
893 * any VM backing store, even if it was dirty.
894 * Otherwise we run the risk of a logical/device
895 * conflict on reallocation.
897 chain->bp->b_flags |= B_RELBUF | B_NOCACHE;
900 chain->data = (void *)bdata;
901 hammer2_chain_modify(trans, chain, 0);
905 * Make sure the chain is marked MOVED and SUBMOD is set in the
906 * parent(s) so the adjustments are picked up by flush.
908 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
909 hammer2_chain_ref(chain);
910 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
912 hammer2_chain_parent_setsubmod(chain);
916 * Convert a locked chain that was retrieved read-only to read-write.
918 * If not already marked modified a new physical block will be allocated
919 * and assigned to the bref.
921 * If already modified and the new modification crosses a synchronization
922 * point the chain is duplicated in order to allow the flush to synchronize
923 * the old chain. The new chain replaces the old.
925 * Non-data blocks - The chain should be locked to at least the RESOLVE_MAYBE
926 * level or the COW operation will not work.
928 * Data blocks - The chain is usually locked RESOLVE_NEVER so as not to
929 * run the data through the device buffers.
931 * This function may return a different chain than was passed, in which case
932 * the old chain will be unlocked and the new chain will be locked.
935 hammer2_chain_modify(hammer2_trans_t *trans, hammer2_chain_t *chain, int flags)
937 hammer2_mount_t *hmp = trans->hmp;
946 * modify_tid is only update for primary modifications, not for
947 * propagated brefs. mirror_tid will be updated regardless during
948 * the flush, no need to set it here.
950 if ((flags & HAMMER2_MODIFY_NO_MODIFY_TID) == 0)
951 chain->bref.modify_tid = trans->sync_tid;
954 * If the chain is already marked MODIFIED we can just return.
956 * However, it is possible that a prior lock/modify sequence
957 * retired the buffer. During this lock/modify sequence MODIFIED
958 * may still be set but the buffer could wind up clean. Since
959 * the caller is going to modify the buffer further we have to
960 * be sure that DIRTYBP is set again.
962 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
963 if ((flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
967 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
972 * Set MODIFIED and add a chain ref to prevent destruction. Both
973 * modified flags share the same ref.
975 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
976 hammer2_chain_ref(chain);
979 * We must allocate the copy-on-write block.
981 * If the data is embedded no other action is required.
983 * If the data is not embedded we acquire and clear the
984 * new block. If chain->data is not NULL we then do the
985 * copy-on-write. chain->data will then be repointed to the new
986 * buffer and the old buffer will be released.
988 * For newly created elements with no prior allocation we go
989 * through the copy-on-write steps except without the copying part.
991 if (chain != &hmp->vchain) {
992 if ((hammer2_debug & 0x0001) &&
993 (chain->bref.data_off & HAMMER2_OFF_MASK)) {
994 kprintf("Replace %d\n", chain->bytes);
996 chain->bref.data_off =
997 hammer2_freemap_alloc(hmp, chain->bref.type,
999 /* XXX failed allocation */
1003 * If data instantiation is optional and the chain has no current
1004 * data association (typical for DATA and newly-created INDIRECT
1005 * elements), don't instantiate the buffer now.
1007 if ((flags & HAMMER2_MODIFY_OPTDATA) && chain->bp == NULL)
1012 * Setting the DIRTYBP flag will cause the buffer to be dirtied or
1013 * written-out on unlock. This bit is independent of the MODIFIED
1014 * bit because the chain may still need meta-data adjustments done
1015 * by virtue of MODIFIED for its parent, and the buffer can be
1016 * flushed out (possibly multiple times) by the OS before that.
1018 * Clearing the INITIAL flag (for indirect blocks) indicates that
1019 * a zero-fill buffer has been instantiated.
1021 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
1022 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1025 * We currently should never instantiate a device buffer for a
1026 * file data chain. (We definitely can for a freemap chain).
1028 KKASSERT(chain->bref.type != HAMMER2_BREF_TYPE_DATA);
1031 * Execute COW operation
1033 switch(chain->bref.type) {
1034 case HAMMER2_BREF_TYPE_VOLUME:
1035 case HAMMER2_BREF_TYPE_INODE:
1037 * The data is embedded, no copy-on-write operation is
1040 KKASSERT(chain->bp == NULL);
1042 case HAMMER2_BREF_TYPE_DATA:
1043 case HAMMER2_BREF_TYPE_INDIRECT:
1044 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
1045 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1046 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1048 * Perform the copy-on-write operation
1050 KKASSERT(chain != &hmp->vchain); /* safety */
1052 * The device buffer may be larger than the allocation size.
1054 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
1055 bbytes = HAMMER2_MINIOSIZE;
1056 pbase = chain->bref.data_off & ~(hammer2_off_t)(bbytes - 1);
1057 boff = chain->bref.data_off & HAMMER2_OFF_MASK & (bbytes - 1);
1060 * The getblk() optimization can only be used if the
1061 * physical block size matches the request.
1063 if (chain->bytes == bbytes) {
1064 nbp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
1067 error = bread(hmp->devvp, pbase, bbytes, &nbp);
1068 KKASSERT(error == 0);
1070 bdata = (char *)nbp->b_data + boff;
1073 * Copy or zero-fill on write depending on whether
1074 * chain->data exists or not.
1077 bcopy(chain->data, bdata, chain->bytes);
1078 KKASSERT(chain->bp != NULL);
1080 bzero(bdata, chain->bytes);
1083 chain->bp->b_flags |= B_RELBUF;
1087 chain->data = bdata;
1090 panic("hammer2_chain_modify: illegal non-embedded type %d",
1096 if ((flags & HAMMER2_MODIFY_NOSUB) == 0)
1097 hammer2_chain_parent_setsubmod(chain);
1101 * Mark the volume as having been modified. This short-cut version
1102 * does not have to lock the volume's chain, which allows the ioctl
1103 * code to make adjustments to connections without deadlocking. XXX
1105 * No ref is made on vchain when flagging it MODIFIED.
1108 hammer2_modify_volume(hammer2_mount_t *hmp)
1110 hammer2_voldata_lock(hmp);
1111 hammer2_voldata_unlock(hmp, 1);
1115 * Locate an in-memory chain. The parent must be locked. The in-memory
1116 * chain is returned with a reference and without a lock, or NULL
1119 * NOTE: A chain on-media might exist for this index when NULL is returned.
1121 * NOTE: Can only be used to locate chains which have not been deleted.
1124 hammer2_chain_find(hammer2_chain_t *parent, int index)
1126 hammer2_chain_t dummy;
1127 hammer2_chain_t *chain;
1130 dummy.index = index;
1131 dummy.delete_tid = HAMMER2_MAX_TID;
1132 spin_lock(&parent->core->cst.spin);
1133 chain = RB_FIND(hammer2_chain_tree, &parent->core->rbtree, &dummy);
1135 hammer2_chain_ref(chain);
1136 spin_unlock(&parent->core->cst.spin);
1142 * Return a locked chain structure with all associated data acquired.
1143 * (if LOOKUP_NOLOCK is requested the returned chain is only referenced).
1145 * Caller must hold the parent locked shared or exclusive since we may
1146 * need the parent's bref array to find our block.
1148 * The returned child is locked as requested. If NOLOCK, the returned
1149 * child is still at least referenced.
1152 hammer2_chain_get(hammer2_chain_t *parent, int index, int flags)
1154 hammer2_blockref_t *bref;
1155 hammer2_mount_t *hmp = parent->hmp;
1156 hammer2_chain_t *chain;
1157 hammer2_chain_t dummy;
1161 * Figure out how to lock. MAYBE can be used to optimized
1162 * the initial-create state for indirect blocks.
1164 if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK))
1165 how = HAMMER2_RESOLVE_NEVER;
1167 how = HAMMER2_RESOLVE_MAYBE;
1168 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK))
1169 how |= HAMMER2_RESOLVE_SHARED;
1173 * First see if we have a (possibly modified) chain element cached
1174 * for this (parent, index). Acquire the data if necessary.
1176 * If chain->data is non-NULL the chain should already be marked
1180 dummy.index = index;
1181 dummy.delete_tid = HAMMER2_MAX_TID;
1182 spin_lock(&parent->core->cst.spin);
1183 chain = RB_FIND(hammer2_chain_tree, &parent->core->rbtree, &dummy);
1185 hammer2_chain_ref(chain);
1186 spin_unlock(&parent->core->cst.spin);
1187 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0)
1188 hammer2_chain_lock(chain, how | HAMMER2_RESOLVE_NOREF);
1191 spin_unlock(&parent->core->cst.spin);
1194 * The parent chain must not be in the INITIAL state.
1196 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1197 panic("hammer2_chain_get: Missing bref(1)");
1202 * No RBTREE entry found, lookup the bref and issue I/O (switch on
1203 * the parent's bref to determine where and how big the array is).
1205 switch(parent->bref.type) {
1206 case HAMMER2_BREF_TYPE_INODE:
1207 KKASSERT(index >= 0 && index < HAMMER2_SET_COUNT);
1208 bref = &parent->data->ipdata.u.blockset.blockref[index];
1210 case HAMMER2_BREF_TYPE_INDIRECT:
1211 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
1212 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1213 KKASSERT(parent->data != NULL);
1214 KKASSERT(index >= 0 &&
1215 index < parent->bytes / sizeof(hammer2_blockref_t));
1216 bref = &parent->data->npdata.blockref[index];
1218 case HAMMER2_BREF_TYPE_VOLUME:
1219 KKASSERT(index >= 0 && index < HAMMER2_SET_COUNT);
1220 bref = &hmp->voldata.sroot_blockset.blockref[index];
1224 panic("hammer2_chain_get: unrecognized blockref type: %d",
1227 if (bref->type == 0) {
1228 panic("hammer2_chain_get: Missing bref(2)");
1233 * Allocate a chain structure representing the existing media
1234 * entry. Resulting chain has one ref and is not locked.
1236 * The locking operation we do later will issue I/O to read it.
1238 chain = hammer2_chain_alloc(hmp, bref);
1239 hammer2_chain_core_alloc(chain, NULL); /* ref'd chain returned */
1242 * Link the chain into its parent. A spinlock is required to safely
1243 * access the RBTREE, and it is possible to collide with another
1244 * hammer2_chain_get() operation because the caller might only hold
1245 * a shared lock on the parent.
1247 KKASSERT(parent->refs > 0);
1248 spin_lock(&parent->core->cst.spin);
1249 chain->parent = parent;
1250 chain->index = index;
1251 if (RB_INSERT(hammer2_chain_tree, &parent->core->rbtree, chain)) {
1252 chain->parent = NULL;
1254 spin_unlock(&parent->core->cst.spin);
1255 hammer2_chain_drop(chain);
1258 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
1259 hammer2_chain_ref(parent); /* chain->parent ref */
1260 spin_unlock(&parent->core->cst.spin);
1263 * Our new chain is referenced but NOT locked. Lock the chain
1264 * below. The locking operation also resolves its data.
1266 * If NOLOCK is set the release will release the one-and-only lock.
1268 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0) {
1269 hammer2_chain_lock(chain, how); /* recusive lock */
1270 hammer2_chain_drop(chain); /* excess ref */
1276 * Lookup initialization/completion API
1279 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
1281 if (flags & HAMMER2_LOOKUP_SHARED) {
1282 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1283 HAMMER2_RESOLVE_SHARED);
1285 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1291 hammer2_chain_lookup_done(hammer2_chain_t *parent)
1294 hammer2_chain_unlock(parent);
1299 * Locate any key between key_beg and key_end inclusive. (*parentp)
1300 * typically points to an inode but can also point to a related indirect
1301 * block and this function will recurse upwards and find the inode again.
1303 * WARNING! THIS DOES NOT RETURN KEYS IN LOGICAL KEY ORDER! ANY KEY
1304 * WITHIN THE RANGE CAN BE RETURNED. HOWEVER, AN ITERATION
1305 * WHICH PICKS UP WHERE WE LEFT OFF WILL CONTINUE THE SCAN.
1307 * (*parentp) must be exclusively locked and referenced and can be an inode
1308 * or an existing indirect block within the inode.
1310 * On return (*parentp) will be modified to point at the deepest parent chain
1311 * element encountered during the search, as a helper for an insertion or
1312 * deletion. The new (*parentp) will be locked and referenced and the old
1313 * will be unlocked and dereferenced (no change if they are both the same).
1315 * The matching chain will be returned exclusively locked. If NOLOCK is
1316 * requested the chain will be returned only referenced.
1318 * NULL is returned if no match was found, but (*parentp) will still
1319 * potentially be adjusted.
1321 * This function will also recurse up the chain if the key is not within the
1322 * current parent's range. (*parentp) can never be set to NULL. An iteration
1323 * can simply allow (*parentp) to float inside the loop.
1326 hammer2_chain_lookup(hammer2_chain_t **parentp,
1327 hammer2_key_t key_beg, hammer2_key_t key_end,
1330 hammer2_mount_t *hmp;
1331 hammer2_chain_t *parent;
1332 hammer2_chain_t *chain;
1333 hammer2_chain_t *tmp;
1334 hammer2_blockref_t *base;
1335 hammer2_blockref_t *bref;
1336 hammer2_key_t scan_beg;
1337 hammer2_key_t scan_end;
1340 int how_always = HAMMER2_RESOLVE_ALWAYS;
1341 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1343 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK)) {
1344 how_maybe |= HAMMER2_RESOLVE_SHARED;
1345 how_always |= HAMMER2_RESOLVE_SHARED;
1349 * Recurse (*parentp) upward if necessary until the parent completely
1350 * encloses the key range or we hit the inode.
1355 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1356 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1357 scan_beg = parent->bref.key;
1358 scan_end = scan_beg +
1359 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1360 if (key_beg >= scan_beg && key_end <= scan_end)
1362 hammer2_chain_ref(parent); /* ref old parent */
1363 hammer2_chain_unlock(parent); /* unlock old parent */
1364 parent = parent->parent;
1365 /* lock new parent */
1366 hammer2_chain_lock(parent, how_maybe);
1367 hammer2_chain_drop(*parentp); /* drop old parent */
1368 *parentp = parent; /* new parent */
1373 * Locate the blockref array. Currently we do a fully associative
1374 * search through the array.
1376 switch(parent->bref.type) {
1377 case HAMMER2_BREF_TYPE_INODE:
1379 * Special shortcut for embedded data returns the inode
1380 * itself. Callers must detect this condition and access
1381 * the embedded data (the strategy code does this for us).
1383 * This is only applicable to regular files and softlinks.
1385 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1386 if (flags & HAMMER2_LOOKUP_NOLOCK)
1387 hammer2_chain_ref(parent);
1389 hammer2_chain_lock(parent, how_always);
1392 base = &parent->data->ipdata.u.blockset.blockref[0];
1393 count = HAMMER2_SET_COUNT;
1395 case HAMMER2_BREF_TYPE_INDIRECT:
1396 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
1397 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1399 * Optimize indirect blocks in the INITIAL state to avoid
1402 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1405 if (parent->data == NULL)
1406 panic("parent->data is NULL");
1407 base = &parent->data->npdata.blockref[0];
1409 count = parent->bytes / sizeof(hammer2_blockref_t);
1411 case HAMMER2_BREF_TYPE_VOLUME:
1412 base = &hmp->voldata.sroot_blockset.blockref[0];
1413 count = HAMMER2_SET_COUNT;
1416 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
1418 base = NULL; /* safety */
1419 count = 0; /* safety */
1423 * If the element and key overlap we use the element.
1425 * NOTE! Deleted elements are effectively invisible. Deletions
1426 * proactively clear the parent bref to the deleted child
1427 * so we do not try to shadow here to avoid parent updates
1428 * (which would be difficult since multiple deleted elements
1429 * might represent different flush synchronization points).
1432 for (i = 0; i < count; ++i) {
1433 tmp = hammer2_chain_find(parent, i);
1435 KKASSERT((tmp->flags & HAMMER2_CHAIN_DELETED) == 0);
1437 KKASSERT(bref->type != 0);
1438 } else if (base == NULL || base[i].type == 0) {
1443 scan_beg = bref->key;
1444 scan_end = scan_beg + ((hammer2_key_t)1 << bref->keybits) - 1;
1446 hammer2_chain_drop(tmp);
1447 if (key_beg <= scan_end && key_end >= scan_beg)
1451 if (key_beg == key_end)
1453 return (hammer2_chain_next(parentp, NULL,
1454 key_beg, key_end, flags));
1458 * Acquire the new chain element. If the chain element is an
1459 * indirect block we must search recursively.
1461 * It is possible for the tmp chain above to be removed from
1462 * the RBTREE but the parent lock ensures it would not have been
1463 * destroyed from the media, so the chain_get() code will simply
1464 * reload it from the media in that case.
1466 chain = hammer2_chain_get(parent, i, flags);
1471 * If the chain element is an indirect block it becomes the new
1472 * parent and we loop on it.
1474 * The parent always has to be locked with at least RESOLVE_MAYBE
1475 * so we can access its data. It might need a fixup if the caller
1476 * passed incompatible flags. Be careful not to cause a deadlock
1477 * as a data-load requires an exclusive lock.
1479 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1480 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1481 hammer2_chain_unlock(parent);
1482 *parentp = parent = chain;
1483 if (flags & HAMMER2_LOOKUP_NOLOCK) {
1484 hammer2_chain_lock(chain, how_maybe);
1485 hammer2_chain_drop(chain); /* excess ref */
1486 } else if ((flags & HAMMER2_LOOKUP_NODATA) &&
1487 chain->data == NULL) {
1488 hammer2_chain_ref(chain);
1489 hammer2_chain_unlock(chain);
1490 hammer2_chain_lock(chain, how_maybe |
1491 HAMMER2_RESOLVE_NOREF);
1497 * All done, return the chain
1503 * After having issued a lookup we can iterate all matching keys.
1505 * If chain is non-NULL we continue the iteration from just after it's index.
1507 * If chain is NULL we assume the parent was exhausted and continue the
1508 * iteration at the next parent.
1510 * parent must be locked on entry and remains locked throughout. chain's
1511 * lock status must match flags. Chain is always at least referenced.
1514 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
1515 hammer2_key_t key_beg, hammer2_key_t key_end,
1518 hammer2_mount_t *hmp;
1519 hammer2_chain_t *parent;
1520 hammer2_chain_t *tmp;
1521 hammer2_blockref_t *base;
1522 hammer2_blockref_t *bref;
1523 hammer2_key_t scan_beg;
1524 hammer2_key_t scan_end;
1526 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1529 if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK))
1530 how_maybe |= HAMMER2_RESOLVE_SHARED;
1537 * Calculate the next index and recalculate the parent if necessary.
1541 * Continue iteration within current parent. If not NULL
1542 * the passed-in chain may or may not be locked, based on
1543 * the LOOKUP_NOLOCK flag (passed in as returned from lookup
1546 i = chain->index + 1;
1547 if (flags & HAMMER2_LOOKUP_NOLOCK)
1548 hammer2_chain_drop(chain);
1550 hammer2_chain_unlock(chain);
1553 * Any scan where the lookup returned degenerate data embedded
1554 * in the inode has an invalid index and must terminate.
1556 if (chain == parent)
1559 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1560 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1562 * We reached the end of the iteration.
1567 * Continue iteration with next parent unless the current
1568 * parent covers the range.
1570 hammer2_chain_t *nparent;
1572 scan_beg = parent->bref.key;
1573 scan_end = scan_beg +
1574 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1575 if (key_beg >= scan_beg && key_end <= scan_end)
1578 i = parent->index + 1;
1579 nparent = parent->parent;
1580 hammer2_chain_ref(nparent); /* ref new parent */
1581 hammer2_chain_unlock(parent); /* unlock old parent */
1582 /* lock new parent */
1583 hammer2_chain_lock(nparent, how_maybe);
1584 hammer2_chain_drop(nparent); /* drop excess ref */
1585 *parentp = parent = nparent;
1590 * Locate the blockref array. Currently we do a fully associative
1591 * search through the array.
1593 switch(parent->bref.type) {
1594 case HAMMER2_BREF_TYPE_INODE:
1595 base = &parent->data->ipdata.u.blockset.blockref[0];
1596 count = HAMMER2_SET_COUNT;
1598 case HAMMER2_BREF_TYPE_INDIRECT:
1599 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
1600 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1601 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1604 KKASSERT(parent->data != NULL);
1605 base = &parent->data->npdata.blockref[0];
1607 count = parent->bytes / sizeof(hammer2_blockref_t);
1609 case HAMMER2_BREF_TYPE_VOLUME:
1610 base = &hmp->voldata.sroot_blockset.blockref[0];
1611 count = HAMMER2_SET_COUNT;
1614 panic("hammer2_chain_next: unrecognized blockref type: %d",
1616 base = NULL; /* safety */
1617 count = 0; /* safety */
1620 KKASSERT(i <= count);
1623 * Look for the key. If we are unable to find a match and an exact
1624 * match was requested we return NULL. If a range was requested we
1625 * run hammer2_chain_next() to iterate.
1627 * NOTE! Deleted elements are effectively invisible. Deletions
1628 * proactively clear the parent bref to the deleted child
1629 * so we do not try to shadow here to avoid parent updates
1630 * (which would be difficult since multiple deleted elements
1631 * might represent different flush synchronization points).
1635 tmp = hammer2_chain_find(parent, i);
1637 KKASSERT((tmp->flags & HAMMER2_CHAIN_DELETED) == 0);
1639 } else if (base == NULL || base[i].type == 0) {
1645 scan_beg = bref->key;
1646 scan_end = scan_beg + ((hammer2_key_t)1 << bref->keybits) - 1;
1648 hammer2_chain_drop(tmp);
1649 if (key_beg <= scan_end && key_end >= scan_beg)
1655 * If we couldn't find a match recurse up a parent to continue the
1662 * Acquire the new chain element. If the chain element is an
1663 * indirect block we must search recursively.
1665 chain = hammer2_chain_get(parent, i, flags);
1670 * If the chain element is an indirect block it becomes the new
1671 * parent and we loop on it.
1673 * The parent always has to be locked with at least RESOLVE_MAYBE
1674 * so we can access its data. It might need a fixup if the caller
1675 * passed incompatible flags. Be careful not to cause a deadlock
1676 * as a data-load requires an exclusive lock.
1678 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1679 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1680 hammer2_chain_unlock(parent);
1681 *parentp = parent = chain;
1683 if (flags & HAMMER2_LOOKUP_NOLOCK) {
1684 hammer2_chain_lock(parent, how_maybe);
1685 hammer2_chain_drop(parent); /* excess ref */
1686 } else if ((flags & HAMMER2_LOOKUP_NODATA) &&
1687 parent->data == NULL) {
1688 hammer2_chain_ref(parent);
1689 hammer2_chain_unlock(parent);
1690 hammer2_chain_lock(parent, how_maybe |
1691 HAMMER2_RESOLVE_NOREF);
1698 * All done, return chain
1704 * Create and return a new hammer2 system memory structure of the specified
1705 * key, type and size and insert it RELATIVE TO (PARENT).
1707 * (parent) is typically either an inode or an indirect block, acquired
1708 * acquired as a side effect of issuing a prior failed lookup. parent
1709 * must be locked and held. Do not pass the inode chain to this function
1710 * unless that is the chain returned by the failed lookup.
1712 * (chain) is either NULL, a newly allocated chain, or a chain allocated
1713 * via hammer2_chain_duplicate(). When not NULL, the passed-in chain must
1714 * NOT be attached to any parent, and will be attached by this function.
1715 * This mechanic is used by the rename code.
1717 * Non-indirect types will automatically allocate indirect blocks as required
1718 * if the new item does not fit in the current (parent).
1720 * Indirect types will move a portion of the existing blockref array in
1721 * (parent) into the new indirect type and then use one of the free slots
1722 * to emplace the new indirect type.
1724 * A new locked chain element is returned of the specified type. The
1725 * element may or may not have a data area associated with it:
1727 * VOLUME not allowed here
1728 * INODE kmalloc()'d data area is set up
1729 * INDIRECT not allowed here
1730 * DATA no data area will be set-up (caller is expected
1731 * to have logical buffers, we don't want to alias
1732 * the data onto device buffers!).
1734 * Requires an exclusively locked parent.
1737 hammer2_chain_create(hammer2_trans_t *trans, hammer2_chain_t *parent,
1738 hammer2_chain_t **chainp,
1739 hammer2_key_t key, int keybits, int type, size_t bytes)
1741 hammer2_mount_t *hmp;
1742 hammer2_chain_t *chain;
1743 hammer2_blockref_t dummy;
1744 hammer2_blockref_t *base;
1745 hammer2_chain_t dummy_chain;
1746 int unlock_parent = 0;
1752 KKASSERT(ccms_thread_lock_owned(&parent->core->cst));
1756 if (chain == NULL) {
1758 * First allocate media space and construct the dummy bref,
1759 * then allocate the in-memory chain structure.
1761 bzero(&dummy, sizeof(dummy));
1764 dummy.keybits = keybits;
1765 dummy.data_off = hammer2_allocsize(bytes);
1766 dummy.methods = parent->bref.methods;
1767 chain = hammer2_chain_alloc(hmp, &dummy);
1768 hammer2_chain_core_alloc(chain, NULL);
1769 ccms_thread_lock(&chain->core->cst, CCMS_STATE_EXCLUSIVE);
1773 * We do NOT set INITIAL here (yet). INITIAL is only
1774 * used for indirect blocks.
1776 * Recalculate bytes to reflect the actual media block
1779 bytes = (hammer2_off_t)1 <<
1780 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
1781 chain->bytes = bytes;
1784 case HAMMER2_BREF_TYPE_VOLUME:
1785 panic("hammer2_chain_create: called with volume type");
1787 case HAMMER2_BREF_TYPE_INODE:
1788 KKASSERT(bytes == HAMMER2_INODE_BYTES);
1789 chain->data = kmalloc(sizeof(chain->data->ipdata),
1790 hmp->minode, M_WAITOK | M_ZERO);
1792 case HAMMER2_BREF_TYPE_INDIRECT:
1793 panic("hammer2_chain_create: cannot be used to"
1794 "create indirect block");
1796 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
1797 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1798 panic("hammer2_chain_create: cannot be used to"
1799 "create freemap root or node");
1801 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1802 case HAMMER2_BREF_TYPE_DATA:
1804 /* leave chain->data NULL */
1805 KKASSERT(chain->data == NULL);
1810 * Potentially update the chain's key/keybits.
1812 chain->bref.key = key;
1813 chain->bref.keybits = keybits;
1818 * Locate a free blockref in the parent's array
1820 switch(parent->bref.type) {
1821 case HAMMER2_BREF_TYPE_INODE:
1822 KKASSERT((parent->data->ipdata.op_flags &
1823 HAMMER2_OPFLAG_DIRECTDATA) == 0);
1824 KKASSERT(parent->data != NULL);
1825 base = &parent->data->ipdata.u.blockset.blockref[0];
1826 count = HAMMER2_SET_COUNT;
1828 case HAMMER2_BREF_TYPE_INDIRECT:
1829 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
1830 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1831 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1834 KKASSERT(parent->data != NULL);
1835 base = &parent->data->npdata.blockref[0];
1837 count = parent->bytes / sizeof(hammer2_blockref_t);
1839 case HAMMER2_BREF_TYPE_VOLUME:
1840 KKASSERT(parent->data != NULL);
1841 base = &hmp->voldata.sroot_blockset.blockref[0];
1842 count = HAMMER2_SET_COUNT;
1845 panic("hammer2_chain_create: unrecognized blockref type: %d",
1852 * Scan for an unallocated bref, also skipping any slots occupied
1853 * by in-memory chain elements that may not yet have been updated
1854 * in the parent's bref array.
1856 * We don't have to hold the spinlock to save an empty slot as
1857 * new slots can only transition from empty if the parent is
1858 * locked exclusively.
1860 bzero(&dummy_chain, sizeof(dummy_chain));
1861 dummy_chain.delete_tid = HAMMER2_MAX_TID;
1863 spin_lock(&parent->core->cst.spin);
1864 for (i = 0; i < count; ++i) {
1866 dummy_chain.index = i;
1867 if (RB_FIND(hammer2_chain_tree,
1868 &parent->core->rbtree, &dummy_chain) == NULL) {
1871 } else if (base[i].type == 0) {
1872 dummy_chain.index = i;
1873 if (RB_FIND(hammer2_chain_tree,
1874 &parent->core->rbtree, &dummy_chain) == NULL) {
1879 spin_unlock(&parent->core->cst.spin);
1882 * If no free blockref could be found we must create an indirect
1883 * block and move a number of blockrefs into it. With the parent
1884 * locked we can safely lock each child in order to move it without
1885 * causing a deadlock.
1887 * This may return the new indirect block or the old parent depending
1888 * on where the key falls. NULL is returned on error.
1891 hammer2_chain_t *nparent;
1893 nparent = hammer2_chain_create_indirect(trans, parent,
1896 if (nparent == NULL) {
1898 hammer2_chain_free(chain);
1902 if (parent != nparent) {
1904 hammer2_chain_unlock(parent);
1912 * Link the chain into its parent. Later on we will have to set
1913 * the MOVED bit in situations where we don't mark the new chain
1914 * as being modified.
1916 if (chain->parent != NULL)
1917 panic("hammer2: hammer2_chain_create: chain already connected");
1918 KKASSERT(chain->parent == NULL);
1919 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
1921 chain->parent = parent;
1923 KKASSERT(parent->refs > 0);
1924 spin_lock(&parent->core->cst.spin);
1925 if (RB_INSERT(hammer2_chain_tree, &parent->core->rbtree, chain))
1926 panic("hammer2_chain_link: collision");
1927 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
1928 hammer2_chain_ref(parent); /* chain->parent ref */
1929 spin_unlock(&parent->core->cst.spin);
1932 * (allocated) indicates that this is a newly-created chain element
1933 * rather than a renamed chain element. In this situation we want
1934 * to place the chain element in the MODIFIED state.
1936 * The data area will be set up as follows:
1938 * VOLUME not allowed here.
1940 * INODE embedded data are will be set-up.
1942 * INDIRECT not allowed here.
1944 * DATA no data area will be set-up (caller is expected
1945 * to have logical buffers, we don't want to alias
1946 * the data onto device buffers!).
1949 switch(chain->bref.type) {
1950 case HAMMER2_BREF_TYPE_DATA:
1951 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1952 hammer2_chain_modify(trans, chain,
1953 HAMMER2_MODIFY_OPTDATA);
1955 case HAMMER2_BREF_TYPE_INDIRECT:
1956 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
1957 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1958 /* not supported in this function */
1959 panic("hammer2_chain_create: bad type");
1960 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1961 hammer2_chain_modify(trans, chain,
1962 HAMMER2_MODIFY_OPTDATA);
1965 hammer2_chain_modify(trans, chain, 0);
1970 * When reconnecting inodes we have to call setsubmod()
1971 * to ensure that its state propagates up the newly
1974 * Make sure MOVED is set but do not update bref_flush. If
1975 * the chain is undergoing modification bref_flush will be
1976 * updated when it gets flushed. If it is not then the
1977 * bref may not have been flushed yet and we do not want to
1978 * set MODIFIED here as this could result in unnecessary
1981 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
1982 hammer2_chain_ref(chain);
1983 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
1985 hammer2_chain_parent_setsubmod(chain);
1991 hammer2_chain_unlock(parent);
1996 * Replace (*chainp) with a duplicate. The original *chainp is unlocked
1997 * and the replacement will be returned locked. Both the original and the
1998 * new chain will share the same RBTREE (have the same chain->core), with
1999 * the new chain becoming the 'current' chain (meaning it is the first in
2000 * the linked list at core->chain_first).
2002 * If (parent, i) then the new duplicated chain is inserted under the parent
2003 * at the specified index (the parent must not have a ref at that index).
2005 * If (NULL, -1) then the new duplicated chain is not inserted anywhere,
2006 * similar to if it had just been chain_alloc()'d (suitable for passing into
2007 * hammer2_chain_create() after this function returns).
2009 * NOTE! Duplication is used in order to retain the original topology to
2010 * support flush synchronization points. Both the original and the
2011 * new chain will have the same transaction id and thus the operation
2012 * appears atomic on the media.
2015 hammer2_chain_duplicate(hammer2_trans_t *trans, hammer2_chain_t *parent,
2016 int i, hammer2_chain_t **chainp)
2018 hammer2_mount_t *hmp = trans->hmp;
2019 hammer2_blockref_t *base;
2020 hammer2_chain_t *chain;
2025 * First create a duplicate of the chain structure, associating
2026 * it with the same core, making it the same size, pointing it
2027 * to the same bref (the same media block), and copying any inline
2030 KKASSERT(((*chainp)->flags & HAMMER2_CHAIN_INITIAL) == 0);
2031 chain = hammer2_chain_alloc(hmp, &(*chainp)->bref);
2032 hammer2_chain_core_alloc(chain, (*chainp)->core);
2034 bytes = (hammer2_off_t)1 <<
2035 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
2036 chain->bytes = bytes;
2038 switch(chain->bref.type) {
2039 case HAMMER2_BREF_TYPE_VOLUME:
2040 panic("hammer2_chain_duplicate: cannot be called w/volhdr");
2042 case HAMMER2_BREF_TYPE_INODE:
2043 KKASSERT(bytes == HAMMER2_INODE_BYTES);
2044 if ((*chainp)->data) {
2045 chain->data = kmalloc(sizeof(chain->data->ipdata),
2046 hmp->minode, M_WAITOK | M_ZERO);
2047 chain->data->ipdata = (*chainp)->data->ipdata;
2050 case HAMMER2_BREF_TYPE_INDIRECT:
2052 panic("hammer2_chain_duplicate: cannot be used to"
2053 "create an indirect block");
2056 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
2057 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2058 panic("hammer2_chain_duplicate: cannot be used to"
2059 "create a freemap root or node");
2061 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2062 case HAMMER2_BREF_TYPE_DATA:
2064 /* leave chain->data NULL */
2065 KKASSERT(chain->data == NULL);
2070 * Both chains must be locked for us to be able to set the
2071 * duplink. To avoid buffer cache deadlocks we do not try
2072 * to resolve the new chain until after we've unlocked the
2075 hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
2076 KKASSERT((*chainp)->duplink == NULL);
2077 (*chainp)->duplink = chain; /* inherits excess ref from alloc */
2078 hammer2_chain_unlock(*chainp);
2080 hammer2_chain_lock(chain, HAMMER2_RESOLVE_MAYBE);
2081 hammer2_chain_unlock(chain);
2085 * If parent is not NULL, insert into the parent at the requested
2086 * index. The newly duplicated chain must be marked MOVED and
2087 * SUBMODIFIED set in its parent(s).
2091 * Locate a free blockref in the parent's array
2093 KKASSERT(ccms_thread_lock_owned(&parent->core->cst));
2094 switch(parent->bref.type) {
2095 case HAMMER2_BREF_TYPE_INODE:
2096 KKASSERT((parent->data->ipdata.op_flags &
2097 HAMMER2_OPFLAG_DIRECTDATA) == 0);
2098 KKASSERT(parent->data != NULL);
2099 base = &parent->data->ipdata.u.blockset.blockref[0];
2100 count = HAMMER2_SET_COUNT;
2102 case HAMMER2_BREF_TYPE_INDIRECT:
2103 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
2104 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2105 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2108 KKASSERT(parent->data != NULL);
2109 base = &parent->data->npdata.blockref[0];
2111 count = parent->bytes / sizeof(hammer2_blockref_t);
2113 case HAMMER2_BREF_TYPE_VOLUME:
2114 KKASSERT(parent->data != NULL);
2115 base = &hmp->voldata.sroot_blockset.blockref[0];
2116 count = HAMMER2_SET_COUNT;
2119 panic("hammer2_chain_create: unrecognized "
2120 "blockref type: %d",
2125 KKASSERT(i >= 0 && i < count);
2126 KKASSERT(base == NULL || base[i].type == 0);
2128 chain->parent = parent;
2130 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
2131 KKASSERT(parent->refs > 0);
2132 spin_lock(&parent->core->cst.spin);
2133 if (RB_INSERT(hammer2_chain_tree, &parent->core->rbtree, chain))
2134 panic("hammer2_chain_link: collision");
2135 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2136 hammer2_chain_ref(parent); /* chain->parent ref */
2137 spin_unlock(&parent->core->cst.spin);
2139 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
2140 hammer2_chain_ref(chain);
2141 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
2143 hammer2_chain_parent_setsubmod(chain);
2148 * Create an indirect block that covers one or more of the elements in the
2149 * current parent. Either returns the existing parent with no locking or
2150 * ref changes or returns the new indirect block locked and referenced
2151 * and leaving the original parent lock/ref intact as well.
2153 * If an error occurs, NULL is returned and *errorp is set to the error.
2155 * The returned chain depends on where the specified key falls.
2157 * The key/keybits for the indirect mode only needs to follow three rules:
2159 * (1) That all elements underneath it fit within its key space and
2161 * (2) That all elements outside it are outside its key space.
2163 * (3) When creating the new indirect block any elements in the current
2164 * parent that fit within the new indirect block's keyspace must be
2165 * moved into the new indirect block.
2167 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
2168 * keyspace the the current parent, but lookup/iteration rules will
2169 * ensure (and must ensure) that rule (2) for all parents leading up
2170 * to the nearest inode or the root volume header is adhered to. This
2171 * is accomplished by always recursing through matching keyspaces in
2172 * the hammer2_chain_lookup() and hammer2_chain_next() API.
2174 * The current implementation calculates the current worst-case keyspace by
2175 * iterating the current parent and then divides it into two halves, choosing
2176 * whichever half has the most elements (not necessarily the half containing
2177 * the requested key).
2179 * We can also opt to use the half with the least number of elements. This
2180 * causes lower-numbered keys (aka logical file offsets) to recurse through
2181 * fewer indirect blocks and higher-numbered keys to recurse through more.
2182 * This also has the risk of not moving enough elements to the new indirect
2183 * block and being forced to create several indirect blocks before the element
2186 * Must be called with an exclusively locked parent.
2190 hammer2_chain_create_indirect(hammer2_trans_t *trans, hammer2_chain_t *parent,
2191 hammer2_key_t create_key, int create_bits,
2194 hammer2_mount_t *hmp = trans->hmp;
2195 hammer2_blockref_t *base;
2196 hammer2_blockref_t *bref;
2197 hammer2_chain_t *chain;
2198 hammer2_chain_t *ichain;
2199 hammer2_chain_t dummy;
2200 hammer2_key_t key = create_key;
2201 int keybits = create_bits;
2209 * Calculate the base blockref pointer or NULL if the chain
2210 * is known to be empty. We need to calculate the array count
2211 * for RB lookups either way.
2213 KKASSERT(ccms_thread_lock_owned(&parent->core->cst));
2216 hammer2_chain_modify(trans, parent, HAMMER2_MODIFY_OPTDATA);
2217 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2220 switch(parent->bref.type) {
2221 case HAMMER2_BREF_TYPE_INODE:
2222 count = HAMMER2_SET_COUNT;
2224 case HAMMER2_BREF_TYPE_INDIRECT:
2225 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
2226 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2227 count = parent->bytes / sizeof(hammer2_blockref_t);
2229 case HAMMER2_BREF_TYPE_VOLUME:
2230 count = HAMMER2_SET_COUNT;
2233 panic("hammer2_chain_create_indirect: "
2234 "unrecognized blockref type: %d",
2240 switch(parent->bref.type) {
2241 case HAMMER2_BREF_TYPE_INODE:
2242 base = &parent->data->ipdata.u.blockset.blockref[0];
2243 count = HAMMER2_SET_COUNT;
2245 case HAMMER2_BREF_TYPE_INDIRECT:
2246 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
2247 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2248 base = &parent->data->npdata.blockref[0];
2249 count = parent->bytes / sizeof(hammer2_blockref_t);
2251 case HAMMER2_BREF_TYPE_VOLUME:
2252 base = &hmp->voldata.sroot_blockset.blockref[0];
2253 count = HAMMER2_SET_COUNT;
2256 panic("hammer2_chain_create_indirect: "
2257 "unrecognized blockref type: %d",
2265 * Scan for an unallocated bref, also skipping any slots occupied
2266 * by in-memory chain elements which may not yet have been updated
2267 * in the parent's bref array.
2269 * Deleted elements are ignored.
2271 bzero(&dummy, sizeof(dummy));
2272 dummy.delete_tid = HAMMER2_MAX_TID;
2274 spin_lock(&parent->core->cst.spin);
2275 for (i = 0; i < count; ++i) {
2279 chain = RB_FIND(hammer2_chain_tree, &parent->core->rbtree,
2282 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
2283 bref = &chain->bref;
2284 } else if (base && base[i].type) {
2291 * Expand our calculated key range (key, keybits) to fit
2292 * the scanned key. nkeybits represents the full range
2293 * that we will later cut in half (two halves @ nkeybits - 1).
2296 if (nkeybits < bref->keybits)
2297 nkeybits = bref->keybits;
2298 while (nkeybits < 64 &&
2299 (~(((hammer2_key_t)1 << nkeybits) - 1) &
2300 (key ^ bref->key)) != 0) {
2305 * If the new key range is larger we have to determine
2306 * which side of the new key range the existing keys fall
2307 * under by checking the high bit, then collapsing the
2308 * locount into the hicount or vise-versa.
2310 if (keybits != nkeybits) {
2311 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
2322 * The newly scanned key will be in the lower half or the
2323 * higher half of the (new) key range.
2325 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
2330 spin_unlock(&parent->core->cst.spin);
2331 bref = NULL; /* now invalid (safety) */
2334 * Adjust keybits to represent half of the full range calculated
2335 * above (radix 63 max)
2340 * Select whichever half contains the most elements. Theoretically
2341 * we can select either side as long as it contains at least one
2342 * element (in order to ensure that a free slot is present to hold
2343 * the indirect block).
2345 key &= ~(((hammer2_key_t)1 << keybits) - 1);
2346 if (hammer2_indirect_optimize) {
2348 * Insert node for least number of keys, this will arrange
2349 * the first few blocks of a large file or the first few
2350 * inodes in a directory with fewer indirect blocks when
2353 if (hicount < locount && hicount != 0)
2354 key |= (hammer2_key_t)1 << keybits;
2356 key &= ~(hammer2_key_t)1 << keybits;
2359 * Insert node for most number of keys, best for heavily
2362 if (hicount > locount)
2363 key |= (hammer2_key_t)1 << keybits;
2365 key &= ~(hammer2_key_t)1 << keybits;
2369 * How big should our new indirect block be? It has to be at least
2370 * as large as its parent.
2372 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE)
2373 nbytes = HAMMER2_IND_BYTES_MIN;
2375 nbytes = HAMMER2_IND_BYTES_MAX;
2376 if (nbytes < count * sizeof(hammer2_blockref_t))
2377 nbytes = count * sizeof(hammer2_blockref_t);
2380 * Ok, create our new indirect block
2382 switch(parent->bref.type) {
2383 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
2384 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2385 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
2388 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
2391 dummy.bref.key = key;
2392 dummy.bref.keybits = keybits;
2393 dummy.bref.data_off = hammer2_allocsize(nbytes);
2394 dummy.bref.methods = parent->bref.methods;
2396 ichain = hammer2_chain_alloc(hmp, &dummy.bref);
2397 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
2398 hammer2_chain_core_alloc(ichain, NULL);
2399 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
2400 hammer2_chain_drop(ichain); /* excess ref from alloc */
2403 * Iterate the original parent and move the matching brefs into
2404 * the new indirect block.
2406 * XXX handle flushes.
2408 spin_lock(&parent->core->cst.spin);
2409 for (i = 0; i < count; ++i) {
2411 * For keying purposes access the bref from the media or
2412 * from our in-memory cache. In cases where the in-memory
2413 * cache overrides the media the keyrefs will be the same
2414 * anyway so we can avoid checking the cache when the media
2418 chain = RB_FIND(hammer2_chain_tree, &parent->core->rbtree,
2421 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
2422 bref = &chain->bref;
2423 } else if (base && base[i].type) {
2426 if (ichain->index < 0)
2432 * Skip keys not in the chosen half (low or high), only bit
2433 * (keybits - 1) needs to be compared but for safety we
2434 * will compare all msb bits plus that bit again.
2436 if ((~(((hammer2_key_t)1 << keybits) - 1) &
2437 (key ^ bref->key)) != 0) {
2442 * This element is being moved from the parent, its slot
2443 * is available for our new indirect block.
2445 if (ichain->index < 0)
2449 * Load the new indirect block by acquiring or allocating
2450 * the related chain entries, then move them to the new
2451 * parent (ichain) by deleting them from their old location
2452 * and inserting a duplicate of the chain and any modified
2453 * sub-chain in the new location.
2455 * We must set MOVED in the chain being duplicated and
2456 * SUBMODIFIED in the parent(s) so the flush code knows
2457 * what is going on. The latter is done after the loop.
2459 * WARNING! chain->cst.spin must be held when chain->parent is
2460 * modified, even though we own the full blown lock,
2461 * to deal with setsubmod and rename races.
2462 * (XXX remove this req).
2464 spin_unlock(&parent->core->cst.spin);
2465 chain = hammer2_chain_get(parent, i, HAMMER2_LOOKUP_NODATA);
2466 hammer2_chain_delete(trans, parent, chain);
2467 hammer2_chain_duplicate(trans, ichain, i, &chain);
2471 bzero(&base[i], sizeof(base[i]));
2473 hammer2_chain_unlock(chain);
2474 KKASSERT(parent->refs > 0);
2476 spin_lock(&parent->core->cst.spin);
2478 spin_unlock(&parent->core->cst.spin);
2481 * Insert the new indirect block into the parent now that we've
2482 * cleared out some entries in the parent. We calculated a good
2483 * insertion index in the loop above (ichain->index).
2485 * We don't have to set MOVED here because we mark ichain modified
2486 * down below (so the normal modified -> flush -> set-moved sequence
2489 * The insertion shouldn't race as this is a completely new block
2490 * and the parent is locked.
2492 KKASSERT(ichain->index >= 0);
2493 KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
2494 spin_lock(&parent->core->cst.spin);
2495 if (RB_INSERT(hammer2_chain_tree, &parent->core->rbtree, ichain))
2496 panic("hammer2_chain_create_indirect: ichain insertion");
2497 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_ONRBTREE);
2498 ichain->parent = parent;
2499 hammer2_chain_ref(parent); /* ichain->parent ref */
2500 spin_unlock(&parent->core->cst.spin);
2503 * Mark the new indirect block modified after insertion, which
2504 * will propagate up through parent all the way to the root and
2505 * also allocate the physical block in ichain for our caller,
2506 * and assign ichain->data to a pre-zero'd space (because there
2507 * is not prior data to copy into it).
2509 * We have to set SUBMODIFIED in ichain's flags manually so the
2510 * flusher knows it has to recurse through it to get to all of
2511 * our moved blocks, then call setsubmod() to set the bit
2514 hammer2_chain_modify(trans, ichain, HAMMER2_MODIFY_OPTDATA);
2515 hammer2_chain_parent_setsubmod(ichain);
2516 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_SUBMODIFIED);
2519 * Figure out what to return.
2521 if (create_bits > keybits) {
2523 * Key being created is way outside the key range,
2524 * return the original parent.
2526 hammer2_chain_unlock(ichain);
2527 } else if (~(((hammer2_key_t)1 << keybits) - 1) &
2528 (create_key ^ key)) {
2530 * Key being created is outside the key range,
2531 * return the original parent.
2533 hammer2_chain_unlock(ichain);
2536 * Otherwise its in the range, return the new parent.
2537 * (leave both the new and old parent locked).
2546 * Sets CHAIN_DELETED and CHAIN_MOVED in the chain being deleted and
2547 * remove the parent's bref reference to chain, generating a modification
2550 * We do not attempt to defer adjustment of the parent bref to the chain
2551 * as this could become quite complex with multiple deletions / replacements.
2552 * Intead, a modification is generated in the parent which can cause it to
2553 * be duplicated if the current parent's data is required for a flush in
2556 * NOTE: We can trivially adjust the parent if it is in the INITIAL state.
2558 * NOTE: The flush code handles the actual removal of the chain from
2559 * the BTREE (also, depending on synchronization points, the
2560 * chain may still be relevant to the flush).
2562 * NOTE: chain->delete_tid distinguishes deleted chains from live chains,
2563 * by setting it to something less than HAMMER2_MAX_TID the
2564 * chain_lookup(), chain_next(), and chain_get() functions will
2565 * not have visibility.
2567 * This function is NOT recursive. Any entity already pushed into the
2568 * chain (such as an inode) may still need visibility into its contents,
2569 * as well as the ability to read and modify the contents. For example,
2570 * for an unlinked file which is still open.
2573 hammer2_chain_delete(hammer2_trans_t *trans, hammer2_chain_t *parent,
2574 hammer2_chain_t *chain)
2576 hammer2_mount_t *hmp = trans->hmp;
2577 hammer2_blockref_t *base;
2580 if (chain->parent != parent)
2581 panic("hammer2_chain_delete: parent mismatch");
2582 KKASSERT(ccms_thread_lock_owned(&parent->core->cst));
2585 * Nothing to do if already marked.
2587 if (chain->flags & HAMMER2_CHAIN_DELETED)
2591 * Mark the parent modified so our base[] pointer remains valid
2592 * while we move entries. For the optimized indirect block
2593 * case mark the parent moved instead.
2595 * Calculate the blockref reference in the parent and zero it out.
2597 switch(parent->bref.type) {
2598 case HAMMER2_BREF_TYPE_INODE:
2599 hammer2_chain_modify(trans, parent,
2600 HAMMER2_MODIFY_NO_MODIFY_TID);
2601 base = &parent->data->ipdata.u.blockset.blockref[0];
2602 count = HAMMER2_SET_COUNT;
2604 case HAMMER2_BREF_TYPE_INDIRECT:
2605 case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
2606 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2607 hammer2_chain_modify(trans, parent,
2608 HAMMER2_MODIFY_OPTDATA |
2609 HAMMER2_MODIFY_NO_MODIFY_TID);
2610 if (parent->flags & HAMMER2_CHAIN_INITIAL)
2613 base = &parent->data->npdata.blockref[0];
2614 count = parent->bytes / sizeof(hammer2_blockref_t);
2616 case HAMMER2_BREF_TYPE_VOLUME:
2617 hammer2_chain_modify(trans, parent,
2618 HAMMER2_MODIFY_NO_MODIFY_TID);
2619 base = &hmp->voldata.sroot_blockset.blockref[0];
2620 count = HAMMER2_SET_COUNT;
2623 panic("hammer2_chain_delete: unrecognized blockref type: %d",
2625 base = NULL; /* NOT REACHED */
2626 count = 0; /* NOT REACHED */
2627 break; /* NOT REACHED */
2629 KKASSERT(chain->index >= 0 && chain->index < count);
2632 * Clean out the blockref immediately.
2635 bzero(&base[chain->index], sizeof(*base));
2638 * Must set MOVED along with DELETED for the flush code to recognize
2639 * the operation and properly disconnect the chain in-memory.
2641 * The setting of DELETED causes finds, lookups, and _next iterations
2642 * to no longer recognize the chain. RB_SCAN()s will still have
2643 * visibility (needed for flush serialization points).
2645 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2646 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
2647 hammer2_chain_ref(chain);
2648 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
2650 chain->delete_tid = trans->sync_tid;
2651 hammer2_chain_parent_setsubmod(chain);
2655 hammer2_chain_wait(hammer2_chain_t *chain)
2657 tsleep(chain, 0, "chnflw", 1);