2 * Copyright (c) 2011-2012 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 handles direct and indirect block searches, recursions,
37 * creation, and deletion. Chains of blockrefs are tracked and modifications
38 * are flag for propagation... eventually all the way back to the volume
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/types.h>
51 static int hammer2_indirect_optimize; /* XXX SYSCTL */
53 static hammer2_chain_t *hammer2_chain_create_indirect(
54 hammer2_mount_t *hmp, hammer2_chain_t *parent,
55 hammer2_key_t key, int keybits);
60 SPLAY_GENERATE(hammer2_chain_splay, hammer2_chain, snode, hammer2_chain_cmp);
63 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
65 return(chain2->index - chain1->index);
69 * Recursively mark the parent chain elements so flushes can find
72 * NOTE: The flush code will modify a SUBMODIFIED-flagged chain
73 * during the flush recursion after clearing the parent's
74 * SUBMODIFIED bit. We don't want to re-set the parent's
75 * SUBMODIFIED bit in this case!
77 * XXX rename of parent can create a SMP race
80 hammer2_chain_parent_setsubmod(hammer2_mount_t *hmp, hammer2_chain_t *chain)
82 hammer2_chain_t *parent;
84 if ((chain->flags & HAMMER2_CHAIN_SUBMODIFIED) == 0) {
85 parent = chain->parent;
87 (parent->flags & HAMMER2_CHAIN_SUBMODIFIED) == 0) {
88 atomic_set_int(&parent->flags,
89 HAMMER2_CHAIN_SUBMODIFIED);
90 parent = parent->parent;
96 * Allocate a new disconnected chain element representing the specified
97 * bref. The chain element is locked exclusively and refs is set to 1.
99 * This essentially allocates a system memory structure representing one
100 * of the media structure types, including inodes.
103 hammer2_chain_alloc(hammer2_mount_t *hmp, hammer2_blockref_t *bref)
105 hammer2_chain_t *chain;
107 hammer2_indblock_t *np;
109 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
112 * Construct the appropriate system structure.
115 case HAMMER2_BREF_TYPE_INODE:
116 ip = kmalloc(sizeof(*ip), hmp->minode, M_WAITOK | M_ZERO);
119 lockinit(&chain->lk, "inode", 0, LK_CANRECURSE);
122 case HAMMER2_BREF_TYPE_INDIRECT:
123 np = kmalloc(sizeof(*np), hmp->mchain, M_WAITOK | M_ZERO);
126 lockinit(&chain->lk, "iblk", 0, LK_CANRECURSE);
128 case HAMMER2_BREF_TYPE_DATA:
129 dp = kmalloc(sizeof(*dp), hmp->mchain, M_WAITOK | M_ZERO);
132 lockinit(&chain->lk, "dblk", 0, LK_CANRECURSE);
134 case HAMMER2_BREF_TYPE_VOLUME:
136 panic("hammer2_chain_alloc volume type illegal for op");
139 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
143 chain->index = -1; /* not yet assigned */
145 chain->bytes = bytes;
146 lockmgr(&chain->lk, LK_EXCLUSIVE);
152 * Free a disconnected chain element
155 hammer2_chain_free(hammer2_mount_t *hmp, hammer2_chain_t *chain)
159 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
160 chain->bref.type == HAMMER2_BREF_TYPE_VOLUME) {
164 KKASSERT(chain->bp == NULL);
165 KKASSERT(chain->data == NULL);
166 KKASSERT(chain->bref.type != HAMMER2_BREF_TYPE_INODE ||
167 chain->u.ip->vp == NULL);
169 if ((mem = chain->u.mem) != NULL) {
171 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
172 kfree(mem, hmp->minode);
174 kfree(mem, hmp->mchain);
179 * Add a reference to a chain element (for shared access). The chain
180 * element must already have at least 1 ref controlled by the caller.
183 hammer2_chain_ref(hammer2_mount_t *hmp, hammer2_chain_t *chain)
185 KKASSERT(chain->refs > 0);
186 atomic_add_int(&chain->refs, 1);
190 * Drop the callers reference to the chain element. If the ref count
191 * reaches zero the chain element and its related structure (typically an
192 * inode or indirect block) will be freed and the parent will be
193 * recursively dropped.
195 * Modified elements hold an additional reference so it should not be
196 * possible for the count on a modified element to drop to 0.
198 * The chain element must NOT be locked by the caller.
200 * The parent might or might not be locked by the caller but if so it
201 * will also be referenced so we shouldn't recurse upward.
204 hammer2_chain_drop(hammer2_mount_t *hmp, hammer2_chain_t *chain)
206 hammer2_chain_t *parent;
214 KKASSERT(chain != &hmp->vchain);
215 parent = chain->parent;
217 lockmgr(&parent->lk, LK_EXCLUSIVE);
218 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
220 * Succeeded, recurse and drop parent
222 if (!(chain->flags & HAMMER2_CHAIN_DELETED)) {
223 SPLAY_REMOVE(hammer2_chain_splay,
224 &parent->shead, chain);
225 atomic_set_int(&chain->flags,
226 HAMMER2_CHAIN_DELETED);
227 /* parent refs dropped via recursion */
229 chain->parent = NULL;
231 lockmgr(&parent->lk, LK_RELEASE);
232 hammer2_chain_free(hmp, chain);
234 /* recurse on parent */
237 lockmgr(&parent->lk, LK_RELEASE);
238 /* retry the same chain */
241 if (atomic_cmpset_int(&chain->refs, refs, refs - 1)) {
243 * Succeeded, count did not reach zero so
244 * cut out of the loop.
248 /* retry the same chain */
254 * Ref and lock a chain element, acquiring its data with I/O if necessary,
255 * and specify how you would like the data to be resolved.
257 * Returns 0 on success or an error code if the data could not be acquired.
258 * The chain element is locked either way.
260 * The lock is allowed to recurse, multiple locking ops will aggregate
261 * the requested resolve types. Once data is assigned it will not be
262 * removed until the last unlock.
264 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
265 * (typically used to avoid device/logical buffer
268 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
269 * the INITIAL-create state (indirect blocks only).
271 * Do not resolve data elements for DATA chains.
272 * (typically used to avoid device/logical buffer
275 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
278 * NOTE: Embedded elements (volume header, inodes) are always resolved
281 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
282 * element will instantiate and zero its buffer, and flush it on
285 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
286 * so as not to instantiate a device buffer, which could alias against
287 * a logical file buffer. However, if ALWAYS is specified the
288 * device buffer will be instantiated anyway.
291 hammer2_chain_lock(hammer2_mount_t *hmp, hammer2_chain_t *chain, int how)
293 hammer2_blockref_t *bref;
302 * Lock the element. Under certain conditions this might end up
303 * being a recursive lock.
305 KKASSERT(chain->refs > 0);
306 atomic_add_int(&chain->refs, 1);
307 lockmgr(&chain->lk, LK_EXCLUSIVE);
310 * If we already have a valid data pointer no further action is
317 * Do we have to resolve the data?
320 case HAMMER2_RESOLVE_NEVER:
322 case HAMMER2_RESOLVE_MAYBE:
323 if (chain->flags & HAMMER2_CHAIN_INITIAL)
325 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
328 case HAMMER2_RESOLVE_ALWAYS:
333 * We must resolve to a device buffer, either by issuing I/O or
334 * by creating a zero-fill element. We do not mark the buffer
335 * dirty when creating a zero-fill element (the hammer2_chain_modify()
336 * API must still be used to do that).
338 * The device buffer is variable-sized in powers of 2 down
339 * to HAMMER2_MINALLOCSIZE (typically 1K). A 64K physical storage
340 * chunk always contains buffers of the same size. (XXX)
342 * The minimum physical IO size may be larger than the variable
347 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
348 bbytes = HAMMER2_MINIOSIZE;
349 pbase = bref->data_off & ~(hammer2_off_t)(bbytes - 1);
350 peof = (pbase + HAMMER2_PBUFSIZE64) & ~HAMMER2_PBUFMASK64;
351 boff = bref->data_off & HAMMER2_OFF_MASK & (bbytes - 1);
352 KKASSERT(pbase != 0);
355 * The getblk() optimization can only be used on newly created
356 * elements if the physical block size matches the request.
358 if ((chain->flags & HAMMER2_CHAIN_INITIAL) &&
359 chain->bytes == bbytes) {
360 chain->bp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
362 } else if (hammer2_cluster_enable) {
363 error = cluster_read(hmp->devvp, peof, pbase, bbytes,
364 HAMMER2_PBUFSIZE, HAMMER2_PBUFSIZE,
367 error = bread(hmp->devvp, pbase, bbytes, &chain->bp);
371 kprintf("hammer2_chain_get: I/O error %016jx: %d\n",
372 (intmax_t)pbase, error);
379 * Zero the data area if the chain is in the INITIAL-create state
381 bdata = (char *)chain->bp->b_data + boff;
382 if (chain->flags & HAMMER2_CHAIN_INITIAL)
383 bzero(bdata, chain->bytes);
386 * Setup the data pointer, either pointing it to an embedded data
387 * structure and copying the data from the buffer, or pointing it
390 * The buffer is not retained when copying to an embedded data
391 * structure in order to avoid potential deadlocks or recursions
392 * on the same physical buffer.
394 switch (bref->type) {
395 case HAMMER2_BREF_TYPE_VOLUME:
397 * Copy data from bp to embedded buffer
399 panic("hammer2_chain_lock: called on unresolved volume header");
402 KKASSERT(pbase == 0);
403 KKASSERT(chain->bytes == HAMMER2_PBUFSIZE);
404 bcopy(bdata, &hmp->voldata, chain->bytes);
405 chain->data = (void *)&hmp->voldata;
410 case HAMMER2_BREF_TYPE_INODE:
412 * Copy data from bp to embedded buffer, do not retain the
415 bcopy(bdata, &chain->u.ip->ip_data, chain->bytes);
416 chain->data = (void *)&chain->u.ip->ip_data;
420 case HAMMER2_BREF_TYPE_INDIRECT:
421 case HAMMER2_BREF_TYPE_DATA:
424 * Point data at the device buffer and leave bp intact.
426 chain->data = (void *)bdata;
433 * Unlock and deref a chain element.
435 * On the last lock release any non-embedded data (chain->bp) will be
439 hammer2_chain_unlock(hammer2_mount_t *hmp, hammer2_chain_t *chain)
444 * Undo a recursive lock
446 if (lockcountnb(&chain->lk) > 1) {
447 KKASSERT(chain->refs > 1);
448 atomic_add_int(&chain->refs, -1);
449 lockmgr(&chain->lk, LK_RELEASE);
454 * Shortcut the case if the data is embedded or not resolved.
455 * Do NOT null-out pointers to embedded data (e.g. inode).
457 if (chain->bp == NULL) {
458 lockmgr(&chain->lk, LK_RELEASE);
459 hammer2_chain_drop(hmp, chain);
466 if ((chain->flags & HAMMER2_CHAIN_DIRTYBP) == 0) {
468 } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
469 switch(chain->bref.type) {
470 case HAMMER2_BREF_TYPE_DATA:
471 counterp = &hammer2_ioa_file_write;
473 case HAMMER2_BREF_TYPE_INODE:
474 counterp = &hammer2_ioa_meta_write;
476 case HAMMER2_BREF_TYPE_INDIRECT:
477 counterp = &hammer2_ioa_indr_write;
480 counterp = &hammer2_ioa_volu_write;
485 switch(chain->bref.type) {
486 case HAMMER2_BREF_TYPE_DATA:
487 counterp = &hammer2_iod_file_write;
489 case HAMMER2_BREF_TYPE_INODE:
490 counterp = &hammer2_iod_meta_write;
492 case HAMMER2_BREF_TYPE_INDIRECT:
493 counterp = &hammer2_iod_indr_write;
496 counterp = &hammer2_iod_volu_write;
505 * If a device buffer was used for data be sure to destroy the
506 * buffer when we are done to avoid aliases (XXX what about the
507 * underlying VM pages?).
509 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
510 chain->bp->b_flags |= B_RELBUF;
513 if (chain->flags & HAMMER2_CHAIN_DIRTYBP) {
514 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
515 if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
516 atomic_clear_int(&chain->flags,
517 HAMMER2_CHAIN_IOFLUSH);
518 chain->bp->b_flags |= B_RELBUF;
519 cluster_awrite(chain->bp);
521 chain->bp->b_flags |= B_CLUSTEROK;
525 if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
526 atomic_clear_int(&chain->flags,
527 HAMMER2_CHAIN_IOFLUSH);
528 chain->bp->b_flags |= B_RELBUF;
531 /* bp might still be dirty */
536 lockmgr(&chain->lk, LK_RELEASE);
537 hammer2_chain_drop(hmp, chain);
541 * Resize the chain's physical storage allocation. Chains can be resized
542 * smaller without reallocating the storage. Resizing larger will reallocate
545 * Must be passed a locked chain. If you want the resize to copy the data
546 * you should lock the chain with RESOLVE_MAYBE or RESOLVE_ALWAYS, otherwise
547 * the resize operation will not copy the data.
549 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
550 * to avoid instantiating a device buffer that conflicts with the vnode
553 * XXX flags currently ignored, uses chain->bp to detect data/no-data.
556 hammer2_chain_resize(hammer2_mount_t *hmp, hammer2_chain_t *chain,
557 int nradix, int flags)
569 * Only data and indirect blocks can be resized for now
571 KKASSERT(chain != &hmp->vchain);
572 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
573 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
576 * Nothing to do if the element is already the proper size
578 obytes = chain->bytes;
579 nbytes = 1U << nradix;
580 if (obytes == nbytes)
584 * Set MODIFIED and add a chain ref to prevent destruction. Both
585 * modified flags share the same ref.
587 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
588 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED |
589 HAMMER2_CHAIN_MODIFY_TID);
590 hammer2_chain_ref(hmp, chain);
594 * Relocate the block, even if making it smaller (because different
595 * block sizes may be in different regions).
597 chain->bref.data_off = hammer2_freemap_alloc(hmp, chain->bref.type,
599 chain->bytes = nbytes;
602 * The device buffer may be larger than the allocation size.
604 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
605 bbytes = HAMMER2_MINIOSIZE;
606 pbase = chain->bref.data_off & ~(hammer2_off_t)(bbytes - 1);
607 boff = chain->bref.data_off & HAMMER2_OFF_MASK & (bbytes - 1);
610 * Only copy the data if resolved, otherwise the caller is
614 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
615 chain->bref.type == HAMMER2_BREF_TYPE_DATA);
616 KKASSERT(chain != &hmp->vchain); /* safety */
619 * The getblk() optimization can only be used if the
620 * physical block size matches the request.
622 if (nbytes == bbytes) {
623 nbp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
626 error = bread(hmp->devvp, pbase, bbytes, &nbp);
627 KKASSERT(error == 0);
629 bdata = (char *)nbp->b_data + boff;
631 if (nbytes < obytes) {
632 bcopy(chain->data, bdata, nbytes);
634 bcopy(chain->data, bdata, obytes);
635 bzero(bdata + obytes, nbytes - obytes);
639 * NOTE: The INITIAL state of the chain is left intact.
641 * NOTE: Because of the reallocation we have to set DIRTYBP
642 * if INITIAL is not set.
644 chain->bp->b_flags |= B_RELBUF;
647 chain->data = (void *)bdata;
648 if ((chain->flags & HAMMER2_CHAIN_INITIAL) == 0)
649 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
651 hammer2_chain_parent_setsubmod(hmp, chain);
655 * Convert a locked chain that was retrieved read-only to read-write.
657 * If not already marked modified a new physical block will be allocated
658 * and assigned to the bref.
660 * Non-data blocks - The chain should be locked to at least the RESOLVE_MAYBE
661 * level or the COW operation will not work.
663 * Data blocks - The chain is usually locked RESOLVE_NEVER so as not to
664 * run the data through the device buffers.
667 hammer2_chain_modify(hammer2_mount_t *hmp, hammer2_chain_t *chain, int flags)
677 * Tells flush that modify_tid must be updated, otherwise only
678 * mirror_tid is updated. This is the default.
680 if ((flags & HAMMER2_MODIFY_NO_MODIFY_TID) == 0)
681 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFY_TID);
684 * If the chain is already marked MODIFIED we can just return.
686 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
687 if ((flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
695 * Set MODIFIED and add a chain ref to prevent destruction. Both
696 * modified flags share the same ref.
698 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
699 hammer2_chain_ref(hmp, chain);
702 * We must allocate the copy-on-write block.
704 * If the data is embedded no other action is required.
706 * If the data is not embedded we acquire and clear the
707 * new block. If chain->data is not NULL we then do the
708 * copy-on-write. chain->data will then be repointed to the new
709 * buffer and the old buffer will be released.
711 * For newly created elements with no prior allocation we go
712 * through the copy-on-write steps except without the copying part.
714 if (chain != &hmp->vchain) {
715 if ((hammer2_debug & 0x0001) &&
716 (chain->bref.data_off & HAMMER2_OFF_MASK)) {
717 kprintf("Replace %d\n", chain->bytes);
719 chain->bref.data_off =
720 hammer2_freemap_alloc(hmp, chain->bref.type,
722 /* XXX failed allocation */
726 * If data instantiation is optional and the chain has no current
727 * data association (typical for DATA and newly-created INDIRECT
728 * elements), don't instantiate the buffer now.
730 if ((flags & HAMMER2_MODIFY_OPTDATA) && chain->bp == NULL)
735 * Setting the DIRTYBP flag will cause the buffer to be dirtied or
736 * written-out on unlock. This bit is independent of the MODIFIED
737 * bit because the chain may still need meta-data adjustments done
738 * by virtue of MODIFIED for its parent, and the buffer can be
739 * flushed out (possibly multiple times) by the OS before that.
741 * Clearing the INITIAL flag (for indirect blocks) indicates that
742 * a zero-fill buffer has been instantiated.
744 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
745 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
748 * We currently should never instantiate a device buffer for a
751 KKASSERT(chain->bref.type != HAMMER2_BREF_TYPE_DATA);
754 * Execute COW operation
756 switch(chain->bref.type) {
757 case HAMMER2_BREF_TYPE_VOLUME:
758 case HAMMER2_BREF_TYPE_INODE:
760 * The data is embedded, no copy-on-write operation is
763 KKASSERT(chain->bp == NULL);
765 case HAMMER2_BREF_TYPE_DATA:
766 case HAMMER2_BREF_TYPE_INDIRECT:
768 * Perform the copy-on-write operation
770 KKASSERT(chain != &hmp->vchain); /* safety */
772 * The device buffer may be larger than the allocation size.
774 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
775 bbytes = HAMMER2_MINIOSIZE;
776 pbase = chain->bref.data_off & ~(hammer2_off_t)(bbytes - 1);
777 boff = chain->bref.data_off & HAMMER2_OFF_MASK & (bbytes - 1);
780 * The getblk() optimization can only be used if the
781 * physical block size matches the request.
783 if (chain->bytes == bbytes) {
784 nbp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
787 error = bread(hmp->devvp, pbase, bbytes, &nbp);
788 KKASSERT(error == 0);
790 bdata = (char *)nbp->b_data + boff;
793 * Copy or zero-fill on write depending on whether
794 * chain->data exists or not.
797 bcopy(chain->data, bdata, chain->bytes);
798 KKASSERT(chain->bp != NULL);
800 bzero(bdata, chain->bytes);
803 chain->bp->b_flags |= B_RELBUF;
810 panic("hammer2_chain_modify: illegal non-embedded type %d",
816 if ((flags & HAMMER2_MODIFY_NOSUB) == 0)
817 hammer2_chain_parent_setsubmod(hmp, chain);
821 * Mark the volume as having been modified. This short-cut version
822 * does not have to lock the volume's chain, which allows the ioctl
823 * code to make adjustments to connections without deadlocking.
826 hammer2_modify_volume(hammer2_mount_t *hmp)
828 hammer2_voldata_lock(hmp);
829 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED_AUX);
830 hammer2_voldata_unlock(hmp);
834 * Locate an in-memory chain. The parent must be locked. The in-memory
835 * chain is returned or NULL if no in-memory chain is present.
837 * NOTE: A chain on-media might exist for this index when NULL is returned.
840 hammer2_chain_find(hammer2_mount_t *hmp, hammer2_chain_t *parent, int index)
842 hammer2_chain_t dummy;
843 hammer2_chain_t *chain;
846 chain = SPLAY_FIND(hammer2_chain_splay, &parent->shead, &dummy);
851 * Return a locked chain structure with all associated data acquired.
853 * Caller must lock the parent on call, the returned child will be locked.
856 hammer2_chain_get(hammer2_mount_t *hmp, hammer2_chain_t *parent,
857 int index, int flags)
859 hammer2_blockref_t *bref;
860 hammer2_chain_t *chain;
861 hammer2_chain_t dummy;
865 * Figure out how to lock. MAYBE can be used to optimized
866 * the initial-create state for indirect blocks.
868 if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK))
869 how = HAMMER2_RESOLVE_NEVER;
871 how = HAMMER2_RESOLVE_MAYBE;
874 * First see if we have a (possibly modified) chain element cached
875 * for this (parent, index). Acquire the data if necessary.
877 * If chain->data is non-NULL the chain should already be marked
881 chain = SPLAY_FIND(hammer2_chain_splay, &parent->shead, &dummy);
883 if (flags & HAMMER2_LOOKUP_NOLOCK)
884 hammer2_chain_ref(hmp, chain);
886 hammer2_chain_lock(hmp, chain, how);
891 * the get function must always succeed, panic if there's no
894 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
895 panic("hammer2_chain_get: Missing bref(1)");
900 * Otherwise lookup the bref and issue I/O (switch on the parent)
902 switch(parent->bref.type) {
903 case HAMMER2_BREF_TYPE_INODE:
904 KKASSERT(index >= 0 && index < HAMMER2_SET_COUNT);
905 bref = &parent->data->ipdata.u.blockset.blockref[index];
907 case HAMMER2_BREF_TYPE_INDIRECT:
908 KKASSERT(parent->data != NULL);
909 KKASSERT(index >= 0 &&
910 index < parent->bytes / sizeof(hammer2_blockref_t));
911 bref = &parent->data->npdata.blockref[index];
913 case HAMMER2_BREF_TYPE_VOLUME:
914 KKASSERT(index >= 0 && index < HAMMER2_SET_COUNT);
915 bref = &hmp->voldata.sroot_blockset.blockref[index];
919 panic("hammer2_chain_get: unrecognized blockref type: %d",
922 if (bref->type == 0) {
923 panic("hammer2_chain_get: Missing bref(2)");
928 * Allocate a chain structure representing the existing media
931 * The locking operation we do later will issue I/O to read it.
933 chain = hammer2_chain_alloc(hmp, bref);
936 * Link the chain into its parent. Caller is expected to hold an
937 * exclusive lock on the parent.
939 chain->parent = parent;
940 chain->index = index;
941 if (SPLAY_INSERT(hammer2_chain_splay, &parent->shead, chain))
942 panic("hammer2_chain_link: collision");
943 KKASSERT(parent->refs > 0);
944 atomic_add_int(&parent->refs, 1); /* for splay entry */
947 * Additional linkage for inodes. Reuse the parent pointer to
948 * find the parent directory.
950 if (bref->type == HAMMER2_BREF_TYPE_INODE) {
951 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT)
952 parent = parent->parent;
953 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
954 chain->u.ip->pip = parent->u.ip;
955 chain->u.ip->pmp = parent->u.ip->pmp;
956 chain->u.ip->depth = parent->u.ip->depth + 1;
961 * Our new chain structure has already been referenced and locked
962 * but the lock code handles the I/O so call it to resolve the data.
963 * Then release one of our two exclusive locks.
965 * If NOLOCK is set the release will release the one-and-only lock.
967 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0) {
968 hammer2_chain_lock(hmp, chain, how); /* recusive lock */
969 hammer2_chain_drop(hmp, chain); /* excess ref */
971 lockmgr(&chain->lk, LK_RELEASE); /* from alloc */
977 * Locate any key between key_beg and key_end inclusive. (*parentp)
978 * typically points to an inode but can also point to a related indirect
979 * block and this function will recurse upwards and find the inode again.
981 * WARNING! THIS DOES NOT RETURN KEYS IN LOGICAL KEY ORDER! ANY KEY
982 * WITHIN THE RANGE CAN BE RETURNED. HOWEVER, AN ITERATION
983 * WHICH PICKS UP WHERE WE LEFT OFF WILL CONTINUE THE SCAN.
985 * (*parentp) must be exclusively locked and referenced and can be an inode
986 * or an existing indirect block within the inode.
988 * On return (*parentp) will be modified to point at the deepest parent chain
989 * element encountered during the search, as a helper for an insertion or
990 * deletion. The new (*parentp) will be locked and referenced and the old
991 * will be unlocked and dereferenced (no change if they are both the same).
993 * The matching chain will be returned exclusively locked and referenced.
995 * NULL is returned if no match was found, but (*parentp) will still
996 * potentially be adjusted.
998 * This function will also recurse up the chain if the key is not within the
999 * current parent's range. (*parentp) can never be set to NULL. An iteration
1000 * can simply allow (*parentp) to float inside the loop.
1003 hammer2_chain_lookup(hammer2_mount_t *hmp, hammer2_chain_t **parentp,
1004 hammer2_key_t key_beg, hammer2_key_t key_end,
1007 hammer2_chain_t *parent;
1008 hammer2_chain_t *chain;
1009 hammer2_chain_t *tmp;
1010 hammer2_blockref_t *base;
1011 hammer2_blockref_t *bref;
1012 hammer2_key_t scan_beg;
1013 hammer2_key_t scan_end;
1018 * Recurse (*parentp) upward if necessary until the parent completely
1019 * encloses the key range or we hit the inode.
1022 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT) {
1023 scan_beg = parent->bref.key;
1024 scan_end = scan_beg +
1025 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1026 if (key_beg >= scan_beg && key_end <= scan_end)
1028 hammer2_chain_ref(hmp, parent); /* ref old parent */
1029 hammer2_chain_unlock(hmp, parent); /* unlock old parent */
1030 parent = parent->parent;
1031 /* lock new parent */
1032 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_MAYBE);
1033 hammer2_chain_drop(hmp, *parentp); /* drop old parent */
1034 *parentp = parent; /* new parent */
1039 * Locate the blockref array. Currently we do a fully associative
1040 * search through the array.
1042 switch(parent->bref.type) {
1043 case HAMMER2_BREF_TYPE_INODE:
1045 * Special shortcut for embedded data returns the inode
1046 * itself. Callers must detect this condition and access
1047 * the embedded data (the strategy code does this for us).
1049 * This is only applicable to regular files and softlinks.
1051 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1052 if (flags & HAMMER2_LOOKUP_NOLOCK)
1053 hammer2_chain_ref(hmp, parent);
1055 hammer2_chain_lock(hmp, parent,
1056 HAMMER2_RESOLVE_ALWAYS);
1059 base = &parent->data->ipdata.u.blockset.blockref[0];
1060 count = HAMMER2_SET_COUNT;
1062 case HAMMER2_BREF_TYPE_INDIRECT:
1064 * Optimize indirect blocks in the INITIAL state to avoid
1067 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1070 if (parent->data == NULL)
1071 panic("parent->data is NULL");
1072 base = &parent->data->npdata.blockref[0];
1074 count = parent->bytes / sizeof(hammer2_blockref_t);
1076 case HAMMER2_BREF_TYPE_VOLUME:
1077 base = &hmp->voldata.sroot_blockset.blockref[0];
1078 count = HAMMER2_SET_COUNT;
1081 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
1083 base = NULL; /* safety */
1084 count = 0; /* safety */
1088 * If the element and key overlap we use the element.
1091 for (i = 0; i < count; ++i) {
1092 tmp = hammer2_chain_find(hmp, parent, i);
1095 KKASSERT(bref->type != 0);
1096 } else if (base == NULL || base[i].type == 0) {
1101 scan_beg = bref->key;
1102 scan_end = scan_beg + ((hammer2_key_t)1 << bref->keybits) - 1;
1103 if (key_beg <= scan_end && key_end >= scan_beg)
1107 if (key_beg == key_end)
1109 return (hammer2_chain_next(hmp, parentp, NULL,
1110 key_beg, key_end, flags));
1114 * Acquire the new chain element. If the chain element is an
1115 * indirect block we must search recursively.
1117 chain = hammer2_chain_get(hmp, parent, i, flags);
1122 * If the chain element is an indirect block it becomes the new
1123 * parent and we loop on it.
1125 * The parent always has to be locked with at least RESOLVE_MAYBE,
1126 * so it might need a fixup if the caller passed incompatible flags.
1128 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT) {
1129 hammer2_chain_unlock(hmp, parent);
1130 *parentp = parent = chain;
1131 if (flags & HAMMER2_LOOKUP_NOLOCK) {
1132 hammer2_chain_lock(hmp, chain, HAMMER2_RESOLVE_MAYBE);
1133 hammer2_chain_drop(hmp, chain); /* excess ref */
1134 } else if (flags & HAMMER2_LOOKUP_NODATA) {
1135 hammer2_chain_lock(hmp, chain, HAMMER2_RESOLVE_MAYBE);
1136 hammer2_chain_unlock(hmp, chain);
1142 * All done, return chain
1148 * After having issued a lookup we can iterate all matching keys.
1150 * If chain is non-NULL we continue the iteration from just after it's index.
1152 * If chain is NULL we assume the parent was exhausted and continue the
1153 * iteration at the next parent.
1155 * parent must be locked on entry and remains locked throughout. chain's
1156 * lock status must match flags.
1159 hammer2_chain_next(hammer2_mount_t *hmp, hammer2_chain_t **parentp,
1160 hammer2_chain_t *chain,
1161 hammer2_key_t key_beg, hammer2_key_t key_end,
1164 hammer2_chain_t *parent;
1165 hammer2_chain_t *tmp;
1166 hammer2_blockref_t *base;
1167 hammer2_blockref_t *bref;
1168 hammer2_key_t scan_beg;
1169 hammer2_key_t scan_end;
1177 * Calculate the next index and recalculate the parent if necessary.
1181 * Continue iteration within current parent. If not NULL
1182 * the passed-in chain may or may not be locked, based on
1183 * the LOOKUP_NOLOCK flag (passed in as returned from lookup
1186 i = chain->index + 1;
1187 if (flags & HAMMER2_LOOKUP_NOLOCK)
1188 hammer2_chain_drop(hmp, chain);
1190 hammer2_chain_unlock(hmp, chain);
1193 * Any scan where the lookup returned degenerate data embedded
1194 * in the inode has an invalid index and must terminate.
1196 if (chain == parent)
1199 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT) {
1201 * We reached the end of the iteration.
1206 * Continue iteration with next parent unless the current
1207 * parent covers the range.
1209 hammer2_chain_t *nparent;
1211 scan_beg = parent->bref.key;
1212 scan_end = scan_beg +
1213 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1214 if (key_beg >= scan_beg && key_end <= scan_end)
1217 i = parent->index + 1;
1218 nparent = parent->parent;
1219 hammer2_chain_ref(hmp, nparent); /* ref new parent */
1220 hammer2_chain_unlock(hmp, parent); /* unlock old parent */
1221 /* lock new parent */
1222 hammer2_chain_lock(hmp, nparent, HAMMER2_RESOLVE_MAYBE);
1223 hammer2_chain_drop(hmp, nparent); /* drop excess ref */
1224 *parentp = parent = nparent;
1229 * Locate the blockref array. Currently we do a fully associative
1230 * search through the array.
1232 switch(parent->bref.type) {
1233 case HAMMER2_BREF_TYPE_INODE:
1234 base = &parent->data->ipdata.u.blockset.blockref[0];
1235 count = HAMMER2_SET_COUNT;
1237 case HAMMER2_BREF_TYPE_INDIRECT:
1238 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1241 KKASSERT(parent->data != NULL);
1242 base = &parent->data->npdata.blockref[0];
1244 count = parent->bytes / sizeof(hammer2_blockref_t);
1246 case HAMMER2_BREF_TYPE_VOLUME:
1247 base = &hmp->voldata.sroot_blockset.blockref[0];
1248 count = HAMMER2_SET_COUNT;
1251 panic("hammer2_chain_next: unrecognized blockref type: %d",
1253 base = NULL; /* safety */
1254 count = 0; /* safety */
1257 KKASSERT(i <= count);
1260 * Look for the key. If we are unable to find a match and an exact
1261 * match was requested we return NULL. If a range was requested we
1262 * run hammer2_chain_next() to iterate.
1266 tmp = hammer2_chain_find(hmp, parent, i);
1269 } else if (base == NULL || base[i].type == 0) {
1275 scan_beg = bref->key;
1276 scan_end = scan_beg + ((hammer2_key_t)1 << bref->keybits) - 1;
1277 if (key_beg <= scan_end && key_end >= scan_beg)
1283 * If we couldn't find a match recurse up a parent to continue the
1290 * Acquire the new chain element. If the chain element is an
1291 * indirect block we must search recursively.
1293 chain = hammer2_chain_get(hmp, parent, i, flags);
1298 * If the chain element is an indirect block it becomes the new
1299 * parent and we loop on it.
1301 * The parent always has to be locked with at least RESOLVE_MAYBE,
1302 * so it might need a fixup if the caller passed incompatible flags.
1304 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT) {
1305 hammer2_chain_unlock(hmp, parent);
1306 *parentp = parent = chain;
1308 if (flags & HAMMER2_LOOKUP_NOLOCK) {
1309 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_MAYBE);
1310 hammer2_chain_drop(hmp, parent); /* excess ref */
1311 } else if (flags & HAMMER2_LOOKUP_NODATA) {
1312 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_MAYBE);
1313 hammer2_chain_unlock(hmp, parent);
1320 * All done, return chain
1326 * Create and return a new hammer2 system memory structure of the specified
1327 * key, type and size and insert it RELATIVE TO (PARENT).
1329 * (parent) is typically either an inode or an indirect block, acquired
1330 * acquired as a side effect of issuing a prior failed lookup. parent
1331 * must be locked and held. Do not pass the inode chain to this function
1332 * unless that is the chain returned by the failed lookup.
1334 * Non-indirect types will automatically allocate indirect blocks as required
1335 * if the new item does not fit in the current (parent).
1337 * Indirect types will move a portion of the existing blockref array in
1338 * (parent) into the new indirect type and then use one of the free slots
1339 * to emplace the new indirect type.
1341 * A new locked, referenced chain element is returned of the specified type.
1342 * The element may or may not have a data area associated with it:
1344 * VOLUME not allowed here
1345 * INODE embedded data are will be set-up
1346 * INDIRECT not allowed here
1347 * DATA no data area will be set-up (caller is expected
1348 * to have logical buffers, we don't want to alias
1349 * the data onto device buffers!).
1352 hammer2_chain_create(hammer2_mount_t *hmp, hammer2_chain_t *parent,
1353 hammer2_chain_t *chain,
1354 hammer2_key_t key, int keybits, int type, size_t bytes)
1356 hammer2_blockref_t dummy;
1357 hammer2_blockref_t *base;
1358 hammer2_chain_t dummy_chain;
1359 int unlock_parent = 0;
1364 if (chain == NULL) {
1366 * First allocate media space and construct the dummy bref,
1367 * then allocate the in-memory chain structure.
1369 bzero(&dummy, sizeof(dummy));
1372 dummy.keybits = keybits;
1373 dummy.data_off = hammer2_bytes_to_radix(bytes);
1374 chain = hammer2_chain_alloc(hmp, &dummy);
1378 * We do NOT set INITIAL here (yet). INITIAL is only
1379 * used for indirect blocks.
1381 * Recalculate bytes to reflect the actual media block
1384 bytes = (hammer2_off_t)1 <<
1385 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
1386 chain->bytes = bytes;
1389 case HAMMER2_BREF_TYPE_VOLUME:
1390 panic("hammer2_chain_create: called with volume type");
1392 case HAMMER2_BREF_TYPE_INODE:
1393 KKASSERT(bytes == HAMMER2_INODE_BYTES);
1394 chain->data = (void *)&chain->u.ip->ip_data;
1396 case HAMMER2_BREF_TYPE_INDIRECT:
1397 panic("hammer2_chain_create: cannot be used to"
1398 "create indirect block");
1400 case HAMMER2_BREF_TYPE_DATA:
1402 /* leave chain->data NULL */
1403 KKASSERT(chain->data == NULL);
1408 * Potentially update the chain's key/keybits.
1410 chain->bref.key = key;
1411 chain->bref.keybits = keybits;
1416 * Locate a free blockref in the parent's array
1418 switch(parent->bref.type) {
1419 case HAMMER2_BREF_TYPE_INODE:
1420 KKASSERT(parent->data != NULL);
1421 base = &parent->data->ipdata.u.blockset.blockref[0];
1422 count = HAMMER2_SET_COUNT;
1424 case HAMMER2_BREF_TYPE_INDIRECT:
1425 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1428 KKASSERT(parent->data != NULL);
1429 base = &parent->data->npdata.blockref[0];
1431 count = parent->bytes / sizeof(hammer2_blockref_t);
1433 case HAMMER2_BREF_TYPE_VOLUME:
1434 KKASSERT(parent->data != NULL);
1435 base = &hmp->voldata.sroot_blockset.blockref[0];
1436 count = HAMMER2_SET_COUNT;
1439 panic("hammer2_chain_create: unrecognized blockref type: %d",
1446 * Scan for an unallocated bref, also skipping any slots occupied
1447 * by in-memory chain elements that may not yet have been updated
1448 * in the parent's bref array.
1450 bzero(&dummy_chain, sizeof(dummy_chain));
1451 for (i = 0; i < count; ++i) {
1453 dummy_chain.index = i;
1454 if (SPLAY_FIND(hammer2_chain_splay,
1455 &parent->shead, &dummy_chain) == NULL) {
1458 } else if (base[i].type == 0) {
1459 dummy_chain.index = i;
1460 if (SPLAY_FIND(hammer2_chain_splay,
1461 &parent->shead, &dummy_chain) == NULL) {
1468 * If no free blockref count be found we must create an indirect
1469 * block and move a number of blockrefs into it. With the parent
1470 * locked we can safely lock each child in order to move it without
1471 * causing a deadlock.
1473 * This may return the new indirect block or the old parent depending
1474 * on where the key falls.
1477 hammer2_chain_t *nparent;
1479 nparent = hammer2_chain_create_indirect(hmp, parent,
1481 if (nparent == NULL) {
1483 hammer2_chain_free(hmp, chain);
1487 if (parent != nparent) {
1489 hammer2_chain_unlock(hmp, parent);
1497 * Link the chain into its parent.
1499 if (chain->parent != NULL)
1500 panic("hammer2: hammer2_chain_create: chain already connected");
1501 KKASSERT(chain->parent == NULL);
1502 chain->parent = parent;
1504 if (SPLAY_INSERT(hammer2_chain_splay, &parent->shead, chain))
1505 panic("hammer2_chain_link: collision");
1506 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
1507 KKASSERT(parent->refs > 0);
1508 atomic_add_int(&parent->refs, 1);
1511 * Additional linkage for inodes. Reuse the parent pointer to
1512 * find the parent directory.
1514 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1515 hammer2_chain_t *scan = parent;
1516 while (scan->bref.type == HAMMER2_BREF_TYPE_INDIRECT)
1517 scan = scan->parent;
1518 if (scan->bref.type == HAMMER2_BREF_TYPE_INODE) {
1519 chain->u.ip->pip = scan->u.ip;
1520 chain->u.ip->pmp = scan->u.ip->pmp;
1521 chain->u.ip->depth = scan->u.ip->depth + 1;
1526 * (allocated) indicates that this is a newly-created chain element
1527 * rather than a renamed chain element. In this situation we want
1528 * to place the chain element in the MODIFIED state.
1530 * The data area will be set up as follows:
1532 * VOLUME not allowed here.
1534 * INODE embedded data are will be set-up.
1536 * INDIRECT not allowed here.
1538 * DATA no data area will be set-up (caller is expected
1539 * to have logical buffers, we don't want to alias
1540 * the data onto device buffers!).
1543 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1544 hammer2_chain_modify(hmp, chain,
1545 HAMMER2_MODIFY_OPTDATA);
1546 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT) {
1547 /* not supported in this function */
1548 panic("hammer2_chain_create: bad type");
1549 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1550 hammer2_chain_modify(hmp, chain,
1551 HAMMER2_MODIFY_OPTDATA);
1553 hammer2_chain_modify(hmp, chain, 0);
1557 * When reconnecting inodes we have to call setsubmod()
1558 * to ensure that its state propagates up the newly
1561 * We cannot depend on the chain being in a MODIFIED
1562 * state, or it might already be in that state, so
1563 * even if the parent calls hammer2_chain_modify()
1564 * MOVED might not get set. Thus we have to set it
1567 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
1568 hammer2_chain_ref(hmp, chain);
1569 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
1571 hammer2_chain_parent_setsubmod(hmp, chain);
1576 hammer2_chain_unlock(hmp, parent);
1581 * Create an indirect block that covers one or more of the elements in the
1582 * current parent. Either returns the existing parent with no locking or
1583 * ref changes or returns the new indirect block locked and referenced,
1584 * depending on what the specified key falls into.
1586 * The key/keybits for the indirect mode only needs to follow three rules:
1588 * (1) That all elements underneath it fit within its key space and
1590 * (2) That all elements outside it are outside its key space.
1592 * (3) When creating the new indirect block any elements in the current
1593 * parent that fit within the new indirect block's keyspace must be
1594 * moved into the new indirect block.
1596 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
1597 * keyspace the the current parent, but lookup/iteration rules will
1598 * ensure (and must ensure) that rule (2) for all parents leading up
1599 * to the nearest inode or the root volume header is adhered to. This
1600 * is accomplished by always recursing through matching keyspaces in
1601 * the hammer2_chain_lookup() and hammer2_chain_next() API.
1603 * The current implementation calculates the current worst-case keyspace by
1604 * iterating the current parent and then divides it into two halves, choosing
1605 * whichever half has the most elements (not necessarily the half containing
1606 * the requested key).
1608 * We can also opt to use the half with the least number of elements. This
1609 * causes lower-numbered keys (aka logical file offsets) to recurse through
1610 * fewer indirect blocks and higher-numbered keys to recurse through more.
1611 * This also has the risk of not moving enough elements to the new indirect
1612 * block and being forced to create several indirect blocks before the element
1617 hammer2_chain_create_indirect(hammer2_mount_t *hmp, hammer2_chain_t *parent,
1618 hammer2_key_t create_key, int create_bits)
1620 hammer2_blockref_t *base;
1621 hammer2_blockref_t *bref;
1622 hammer2_chain_t *chain;
1623 hammer2_chain_t *ichain;
1624 hammer2_chain_t dummy;
1625 hammer2_key_t key = create_key;
1626 int keybits = create_bits;
1634 * Calculate the base blockref pointer or NULL if the chain
1635 * is known to be empty.
1637 hammer2_chain_modify(hmp, parent, HAMMER2_MODIFY_OPTDATA);
1638 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1642 * We still need to calculate the count for SPLAY lookups
1644 switch(parent->bref.type) {
1645 case HAMMER2_BREF_TYPE_INODE:
1646 count = HAMMER2_SET_COUNT;
1648 case HAMMER2_BREF_TYPE_INDIRECT:
1649 count = parent->bytes / sizeof(hammer2_blockref_t);
1651 case HAMMER2_BREF_TYPE_VOLUME:
1652 count = HAMMER2_SET_COUNT;
1655 panic("hammer2_chain_create_indirect: "
1656 "unrecognized blockref type: %d",
1663 * Locate a free blockref in the parent's array
1665 switch(parent->bref.type) {
1666 case HAMMER2_BREF_TYPE_INODE:
1667 base = &parent->data->ipdata.u.blockset.blockref[0];
1668 count = HAMMER2_SET_COUNT;
1670 case HAMMER2_BREF_TYPE_INDIRECT:
1671 base = &parent->data->npdata.blockref[0];
1672 count = parent->bytes / sizeof(hammer2_blockref_t);
1674 case HAMMER2_BREF_TYPE_VOLUME:
1675 base = &hmp->voldata.sroot_blockset.blockref[0];
1676 count = HAMMER2_SET_COUNT;
1679 panic("hammer2_chain_create_indirect: "
1680 "unrecognized blockref type: %d",
1688 * Scan for an unallocated bref, also skipping any slots occupied
1689 * by in-memory chain elements that may not yet have been updated
1690 * in the parent's bref array.
1692 bzero(&dummy, sizeof(dummy));
1693 for (i = 0; i < count; ++i) {
1697 * Optimize the case where the parent is still in its
1698 * initially created state.
1700 if (base == NULL || base[i].type == 0) {
1702 chain = SPLAY_FIND(hammer2_chain_splay,
1703 &parent->shead, &dummy);
1706 bref = &chain->bref;
1712 * Expand our calculated key range (key, keybits) to fit
1713 * the scanned key. nkeybits represents the full range
1714 * that we will later cut in half (two halves @ nkeybits - 1).
1717 if (nkeybits < bref->keybits)
1718 nkeybits = bref->keybits;
1719 while ((~(((hammer2_key_t)1 << nkeybits) - 1) &
1720 (key ^ bref->key)) != 0) {
1725 * If the new key range is larger we have to determine
1726 * which side of the new key range the existing keys fall
1727 * under by checking the high bit, then collapsing the
1728 * locount into the hicount or vise-versa.
1730 if (keybits != nkeybits) {
1731 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
1742 * The newly scanned key will be in the lower half or the
1743 * higher half of the (new) key range.
1745 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
1752 * Adjust keybits to represent half of the full range calculated
1758 * Select whichever half contains the most elements. Theoretically
1759 * we can select either side as long as it contains at least one
1760 * element (in order to ensure that a free slot is present to hold
1761 * the indirect block).
1763 key &= ~(((hammer2_key_t)1 << keybits) - 1);
1764 if (hammer2_indirect_optimize) {
1766 * Insert node for least number of keys, this will arrange
1767 * the first few blocks of a large file or the first few
1768 * inodes in a directory with fewer indirect blocks when
1771 if (hicount < locount && hicount != 0)
1772 key |= (hammer2_key_t)1 << keybits;
1774 key &= ~(hammer2_key_t)1 << keybits;
1777 * Insert node for most number of keys, best for heavily
1780 if (hicount > locount)
1781 key |= (hammer2_key_t)1 << keybits;
1783 key &= ~(hammer2_key_t)1 << keybits;
1787 * How big should our new indirect block be? It has to be at least
1788 * as large as its parent.
1790 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE)
1791 nbytes = HAMMER2_IND_BYTES_MIN;
1793 nbytes = HAMMER2_IND_BYTES_MAX;
1794 if (nbytes < count * sizeof(hammer2_blockref_t))
1795 nbytes = count * sizeof(hammer2_blockref_t);
1798 * Ok, create our new indirect block
1800 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
1801 dummy.bref.key = key;
1802 dummy.bref.keybits = keybits;
1803 dummy.bref.data_off = hammer2_bytes_to_radix(nbytes);
1804 ichain = hammer2_chain_alloc(hmp, &dummy.bref);
1805 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
1808 * Iterate the original parent and move the matching brefs into
1809 * the new indirect block.
1811 for (i = 0; i < count; ++i) {
1813 * For keying purposes access the bref from the media or
1814 * from our in-memory cache. In cases where the in-memory
1815 * cache overrides the media the keyrefs will be the same
1816 * anyway so we can avoid checking the cache when the media
1819 if (base == NULL || base[i].type == 0) {
1821 chain = SPLAY_FIND(hammer2_chain_splay,
1822 &parent->shead, &dummy);
1823 if (chain == NULL) {
1825 * Select index indirect block is placed in
1827 if (ichain->index < 0)
1831 bref = &chain->bref;
1837 * Skip keys not in the chosen half (low or high), only bit
1838 * (keybits - 1) needs to be compared but for safety we
1839 * will compare all msb bits plus that bit again.
1841 if ((~(((hammer2_key_t)1 << keybits) - 1) &
1842 (key ^ bref->key)) != 0) {
1847 * This element is being moved, its slot is available
1848 * for our indirect block.
1850 if (ichain->index < 0)
1854 * Load the new indirect block by acquiring or allocating
1855 * the related chain entries, then simply move it to the
1856 * new parent (ichain).
1858 * Flagging the new chain entry MOVED will cause a flush
1859 * to synchronize its block into the new indirect block.
1860 * The chain is unlocked after being moved but needs to
1861 * retain a reference for the MOVED state
1863 * We must still set SUBMODIFIED in the parent but we do
1864 * that after the loop.
1866 * XXX we really need a lock here but we don't need the
1867 * data. NODATA feature needed.
1869 chain = hammer2_chain_get(hmp, parent, i,
1870 HAMMER2_LOOKUP_NODATA);
1871 SPLAY_REMOVE(hammer2_chain_splay, &parent->shead, chain);
1872 if (SPLAY_INSERT(hammer2_chain_splay, &ichain->shead, chain))
1873 panic("hammer2_chain_create_indirect: collision");
1874 chain->parent = ichain;
1876 bzero(&base[i], sizeof(base[i]));
1877 atomic_add_int(&parent->refs, -1);
1878 atomic_add_int(&ichain->refs, 1);
1879 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
1880 hammer2_chain_ref(hmp, chain);
1881 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
1883 hammer2_chain_unlock(hmp, chain);
1884 KKASSERT(parent->refs > 0);
1889 * Insert the new indirect block into the parent now that we've
1890 * cleared out some entries in the parent. We calculated a good
1891 * insertion index in the loop above (ichain->index).
1893 KKASSERT(ichain->index >= 0);
1894 if (SPLAY_INSERT(hammer2_chain_splay, &parent->shead, ichain))
1895 panic("hammer2_chain_create_indirect: ichain insertion");
1896 ichain->parent = parent;
1897 atomic_add_int(&parent->refs, 1);
1900 * Mark the new indirect block modified after insertion, which
1901 * will propagate up through parent all the way to the root and
1902 * also allocate the physical block in ichain for our caller,
1903 * and assign ichain->data to a pre-zero'd space (because there
1904 * is not prior data to copy into it).
1906 * We have to set SUBMODIFIED in ichain's flags manually so the
1907 * flusher knows it has to recurse through it to get to all of
1908 * our moved blocks, then call setsubmod() to set the bit
1911 hammer2_chain_modify(hmp, ichain, HAMMER2_MODIFY_OPTDATA);
1912 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_SUBMODIFIED);
1913 hammer2_chain_parent_setsubmod(hmp, ichain);
1916 * Figure out what to return.
1918 if (create_bits >= keybits) {
1920 * Key being created is way outside the key range,
1921 * return the original parent.
1923 hammer2_chain_unlock(hmp, ichain);
1924 } else if (~(((hammer2_key_t)1 << keybits) - 1) &
1925 (create_key ^ key)) {
1927 * Key being created is outside the key range,
1928 * return the original parent.
1930 hammer2_chain_unlock(hmp, ichain);
1933 * Otherwise its in the range, return the new parent.
1942 * Physically delete the specified chain element. Note that inodes with
1943 * open descriptors should not be deleted (as with other filesystems) until
1944 * the last open descriptor is closed.
1946 * This routine will remove the chain element from its parent and potentially
1947 * also recurse upward and delete indirect blocks which become empty as a
1950 * The caller must pass a pointer to the chain's parent, also locked and
1951 * referenced. (*parentp) will be modified in a manner similar to a lookup
1952 * or iteration when indirect blocks are also deleted as a side effect.
1955 hammer2_chain_delete(hammer2_mount_t *hmp, hammer2_chain_t *parent,
1956 hammer2_chain_t *chain)
1958 hammer2_blockref_t *base;
1961 if (chain->parent != parent)
1962 panic("hammer2_chain_delete: parent mismatch");
1965 * Mark the parent modified so our base[] pointer remains valid
1966 * while we move entries. For the optimized indirect block
1967 * case mark the parent moved instead.
1969 * Calculate the blockref reference in the parent
1971 switch(parent->bref.type) {
1972 case HAMMER2_BREF_TYPE_INODE:
1973 hammer2_chain_modify(hmp, parent, HAMMER2_MODIFY_NO_MODIFY_TID);
1974 base = &parent->data->ipdata.u.blockset.blockref[0];
1975 count = HAMMER2_SET_COUNT;
1977 case HAMMER2_BREF_TYPE_INDIRECT:
1978 hammer2_chain_modify(hmp, parent, HAMMER2_MODIFY_OPTDATA |
1979 HAMMER2_MODIFY_NO_MODIFY_TID);
1980 if (parent->flags & HAMMER2_CHAIN_INITIAL)
1983 base = &parent->data->npdata.blockref[0];
1984 count = parent->bytes / sizeof(hammer2_blockref_t);
1986 case HAMMER2_BREF_TYPE_VOLUME:
1987 hammer2_chain_modify(hmp, parent, HAMMER2_MODIFY_NO_MODIFY_TID);
1988 base = &hmp->voldata.sroot_blockset.blockref[0];
1989 count = HAMMER2_SET_COUNT;
1992 panic("hammer2_chain_delete: unrecognized blockref type: %d",
1999 * Disconnect the bref in the parent, remove the chain, and
2000 * disconnect in-memory fields from the parent.
2002 KKASSERT(chain->index >= 0 && chain->index < count);
2004 bzero(&base[chain->index], sizeof(*base));
2006 SPLAY_REMOVE(hammer2_chain_splay, &parent->shead, chain);
2007 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2008 atomic_add_int(&parent->refs, -1); /* for splay entry */
2010 chain->parent = NULL;
2013 * If this is an inode clear the pip.
2015 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2016 chain->u.ip->pip = NULL;
2017 chain->u.ip->depth = 0;
2021 * The chain is still likely referenced, possibly even by a vnode
2022 * (if an inode), so defer further action until the chain gets
2028 * Recursively flush the specified chain. The chain is locked and
2029 * referenced by the caller and will remain so on return. The chain
2030 * will remain referenced throughout but can temporarily lose its
2031 * lock during the recursion to avoid unnecessarily stalling user
2036 TAILQ_HEAD(flush_deferral_list, hammer2_chain);
2038 struct hammer2_flush_info {
2039 struct flush_deferral_list flush_list;
2041 hammer2_tid_t modify_tid;
2044 typedef struct hammer2_flush_info hammer2_flush_info_t;
2047 hammer2_chain_flush_pass1(hammer2_mount_t *hmp, hammer2_chain_t *chain,
2048 hammer2_flush_info_t *info)
2050 hammer2_blockref_t *bref;
2051 hammer2_off_t pbase;
2059 * If we hit the stack recursion depth limit defer the operation.
2060 * The controller of the info structure will execute the deferral
2061 * list and then retry.
2063 * This is only applicable if SUBMODIFIED is set. After a reflush
2064 * SUBMODIFIED will probably be cleared and we want to drop through
2065 * to finish processing the current element so our direct parent
2066 * can process the results.
2068 if (info->depth == HAMMER2_FLUSH_DEPTH_LIMIT &&
2069 (chain->flags & HAMMER2_CHAIN_SUBMODIFIED)) {
2070 if ((chain->flags & HAMMER2_CHAIN_DEFERRED) == 0) {
2071 hammer2_chain_ref(hmp, chain);
2072 TAILQ_INSERT_TAIL(&info->flush_list,
2074 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DEFERRED);
2079 if (hammer2_debug & 0x0008)
2080 kprintf("%*.*sCHAIN type=%d@%08jx %p/%d %04x {\n",
2081 info->depth, info->depth, "",
2082 chain->bref.type, chain->bref.data_off,
2083 chain, chain->refs, chain->flags);
2086 * Flush any children of this chain.
2088 * NOTE: If we use a while() here an active filesystem can
2089 * prevent the flush from ever finishing.
2091 if (chain->flags & HAMMER2_CHAIN_SUBMODIFIED) {
2092 hammer2_blockref_t *base;
2093 hammer2_chain_t *child;
2094 hammer2_chain_t *next;
2096 int submodified = 0;
2100 * Clear SUBMODIFIED now. Flag any races during the flush
2101 * with the (submodified) local variable and re-arm it
2102 * as necessary after the loop is done.
2104 * Delaying the setting of the chain to MODIFIED can reduce
2107 * Modifications to the children will propagate up, forcing
2108 * us to become modified and copy-on-write too. Be sure
2109 * to modify chain (as a side effect of the recursive
2110 * flush) ONLY if it is actually being modified by the
2113 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_SUBMODIFIED);
2116 * Flush the children and update the blockrefs in the chain.
2117 * Be careful of ripouts during the loop.
2119 next = SPLAY_MIN(hammer2_chain_splay, &chain->shead);
2120 while ((child = next) != NULL) {
2121 next = SPLAY_NEXT(hammer2_chain_splay,
2122 &chain->shead, child);
2124 * We only recurse if SUBMODIFIED (internal node)
2125 * or MODIFIED (internal node or leaf) is set.
2126 * However, we must still track whether any MOVED
2127 * entries are present to determine if the chain's
2128 * blockref's need updating or not.
2130 if (child->flags & HAMMER2_CHAIN_MOVED)
2132 if ((child->flags & (HAMMER2_CHAIN_SUBMODIFIED |
2133 HAMMER2_CHAIN_MODIFIED |
2134 HAMMER2_CHAIN_MODIFIED_AUX)) == 0) {
2139 * Propagate the DESTROYED flag if found set, then
2140 * recurse the flush.
2142 hammer2_chain_lock(hmp, child, HAMMER2_RESOLVE_MAYBE);
2143 if ((chain->flags & HAMMER2_CHAIN_DESTROYED) &&
2144 (child->flags & HAMMER2_CHAIN_DESTROYED) == 0) {
2145 atomic_set_int(&child->flags,
2146 HAMMER2_CHAIN_DESTROYED |
2147 HAMMER2_CHAIN_SUBMODIFIED);
2150 hammer2_chain_flush_pass1(hmp, child, info);
2154 * No point loading blockrefs yet if the
2155 * child (recursively) is still dirty.
2157 if (child->flags & (HAMMER2_CHAIN_SUBMODIFIED |
2158 HAMMER2_CHAIN_MODIFIED |
2159 HAMMER2_CHAIN_MODIFIED_AUX)) {
2161 if (hammer2_debug & 0x0008)
2164 if (child->flags & HAMMER2_CHAIN_MOVED) {
2165 if (hammer2_debug & 0x0008)
2169 if (hammer2_debug & 0x0008)
2171 hammer2_chain_unlock(hmp, child);
2175 * If the sub-tree was not completely synced we currently do
2176 * not attempt to propagate the bref all the way back up.
2177 * Our bref pointers to the children are not updated yet in
2178 * this situation but the children will have CHAIN_MOVED set
2179 * and cannot be destroyed until the parent synchronizes
2182 * If the sub-tree had to be recursed the bref propagates
2183 * back up and may require 'chain' to become modified.
2187 (chain->flags & HAMMER2_CHAIN_SUBMODIFIED)) {
2189 * No point loading up the blockrefs if submodified
2190 * got re-set. The modified and flushed children
2191 * will have set HAMMER2_CHAIN_MOVED and cannot be
2192 * freed until we've synchronized the case.
2194 * NOTE: Even though we cleared the SUBMODIFIED flag
2195 * it can still get re-set by operations
2196 * occuring under our chain, so check both.
2198 atomic_set_int(&chain->flags,
2199 HAMMER2_CHAIN_SUBMODIFIED);
2200 } else if (submoved) {
2202 * Ok, we can modify the blockrefs in this chain
2203 * entry. Mark it modified. Calculate the
2204 * blockref array after marking it modified (since
2205 * that may change the underlying data ptr).
2207 * NOTE: We only do this if submoved != 0, otherwise
2208 * there may not be any changes and setting
2209 * the chain modified will re-arm the MOVED
2210 * bit recursively, resulting in O(N^2)
2213 * NOTE: We don't want hammer2_chain_modify() to
2214 * recursively set the SUBMODIFIED flag
2215 * upward in this case!
2217 hammer2_chain_modify(hmp, chain,
2218 HAMMER2_MODIFY_NOSUB |
2219 HAMMER2_MODIFY_NO_MODIFY_TID);
2221 switch(chain->bref.type) {
2222 case HAMMER2_BREF_TYPE_INODE:
2223 base = &chain->data->ipdata.u.blockset.
2225 count = HAMMER2_SET_COUNT;
2227 case HAMMER2_BREF_TYPE_INDIRECT:
2228 base = &chain->data->npdata.blockref[0];
2229 count = chain->bytes /
2230 sizeof(hammer2_blockref_t);
2232 case HAMMER2_BREF_TYPE_VOLUME:
2233 base = &hmp->voldata.sroot_blockset.blockref[0];
2234 count = HAMMER2_SET_COUNT;
2238 panic("hammer2_chain_get: "
2239 "unrecognized blockref type: %d",
2244 * Update the blockrefs.
2246 * When updating the blockset embedded in the volume
2247 * header we must also update voldata.mirror_tid.
2249 next = SPLAY_MIN(hammer2_chain_splay, &chain->shead);
2250 while ((child = next) != NULL) {
2251 next = SPLAY_NEXT(hammer2_chain_splay,
2252 &chain->shead, child);
2253 KKASSERT(child->index >= 0 &&
2254 child->index < count);
2255 hammer2_chain_lock(hmp, child,
2256 HAMMER2_RESOLVE_NEVER);
2257 if (child->flags & HAMMER2_CHAIN_MOVED) {
2258 base[child->index] = child->bref;
2259 if (chain->bref.mirror_tid <
2260 child->bref.mirror_tid) {
2261 chain->bref.mirror_tid =
2262 child->bref.mirror_tid;
2264 if (chain->bref.type ==
2265 HAMMER2_BREF_TYPE_VOLUME &&
2266 hmp->voldata.mirror_tid <
2267 child->bref.mirror_tid) {
2268 hmp->voldata.mirror_tid =
2269 child->bref.mirror_tid;
2271 atomic_clear_int(&child->flags,
2272 HAMMER2_CHAIN_MOVED);
2273 hammer2_chain_drop(hmp, child);
2274 } else if (bcmp(&base[child->index],
2276 sizeof(child->bref)) != 0) {
2277 panic("hammer2: unflagged bref update");
2279 hammer2_chain_unlock(hmp, child);
2285 * If destroying the object we unconditonally clear the MODIFIED
2286 * and MOVED bits, and we destroy the buffer without writing it
2289 * We don't bother updating the hash/crc or the chain bref.
2291 * NOTE: The destroy'd object's bref has already been updated.
2292 * so we can clear MOVED without propagating mirror_tid
2293 * or modify_tid upward.
2295 * XXX allocations for unflushed data can be returned to the
2298 if (chain->flags & HAMMER2_CHAIN_DESTROYED) {
2299 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
2301 chain->bp->b_flags |= B_INVAL|B_RELBUF;
2303 atomic_clear_int(&chain->flags,
2304 HAMMER2_CHAIN_MODIFIED |
2305 HAMMER2_CHAIN_MODIFY_TID);
2306 hammer2_chain_drop(hmp, chain);
2308 if (chain->flags & HAMMER2_CHAIN_MODIFIED_AUX) {
2309 atomic_clear_int(&chain->flags,
2310 HAMMER2_CHAIN_MODIFIED_AUX);
2312 if (chain->flags & HAMMER2_CHAIN_MOVED) {
2313 atomic_clear_int(&chain->flags,
2314 HAMMER2_CHAIN_MOVED);
2315 hammer2_chain_drop(hmp, chain);
2321 * Flush this chain entry only if it is marked modified.
2323 if ((chain->flags & (HAMMER2_CHAIN_MODIFIED |
2324 HAMMER2_CHAIN_MODIFIED_AUX)) == 0) {
2329 * Clear MODIFIED and set HAMMER2_CHAIN_MOVED. The caller
2330 * will re-test the MOVED bit. We must also update the mirror_tid
2331 * and modify_tid fields as appropriate.
2333 * bits own a single chain ref and the MOVED bit owns its own
2336 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
2337 chain->bref.mirror_tid = info->modify_tid;
2338 if (chain->flags & HAMMER2_CHAIN_MODIFY_TID)
2339 chain->bref.modify_tid = info->modify_tid;
2340 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED |
2341 HAMMER2_CHAIN_MODIFY_TID);
2342 if (chain->flags & HAMMER2_CHAIN_MOVED) {
2343 hammer2_chain_drop(hmp, chain);
2345 /* inherit ref from the MODIFIED we cleared */
2346 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
2349 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED_AUX);
2352 * If this is part of a recursive flush we can go ahead and write
2353 * out the buffer cache buffer and pass a new bref back up the chain.
2355 * This will never be a volume header.
2357 switch(chain->bref.type) {
2358 case HAMMER2_BREF_TYPE_VOLUME:
2360 * The volume header is flushed manually by the syncer, not
2364 case HAMMER2_BREF_TYPE_DATA:
2366 * Data elements have already been flushed via the logical
2367 * file buffer cache. Their hash was set in the bref by
2368 * the vop_write code.
2370 * Make sure the buffer(s) have been flushed out here.
2373 bbytes = chain->bytes;
2374 pbase = chain->bref.data_off & ~(hammer2_off_t)(bbytes - 1);
2375 boff = chain->bref.data_off & HAMMER2_OFF_MASK & (bbytes - 1);
2377 bp = getblk(hmp->devvp, pbase, bbytes, GETBLK_NOWAIT, 0);
2379 if ((bp->b_flags & (B_CACHE | B_DIRTY)) ==
2380 (B_CACHE | B_DIRTY)) {
2384 bp->b_flags |= B_RELBUF;
2390 case HAMMER2_BREF_TYPE_INDIRECT:
2392 * Indirect blocks may be in an INITIAL state.
2397 * Embedded elements have to be flushed out.
2399 KKASSERT(chain->data != NULL);
2400 bref = &chain->bref;
2402 KKASSERT((bref->data_off & HAMMER2_OFF_MASK) != 0);
2404 if (chain->bp == NULL) {
2406 * The data is embedded, we have to acquire the
2407 * buffer cache buffer and copy the data into it.
2409 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
2410 bbytes = HAMMER2_MINIOSIZE;
2411 pbase = bref->data_off & ~(hammer2_off_t)(bbytes - 1);
2412 boff = bref->data_off & HAMMER2_OFF_MASK & (bbytes - 1);
2415 * The getblk() optimization can only be used if the
2416 * physical block size matches the request.
2418 if (chain->bytes == bbytes) {
2419 bp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
2422 error = bread(hmp->devvp, pbase, bbytes, &bp);
2423 KKASSERT(error == 0);
2425 bdata = (char *)bp->b_data + boff;
2428 * Copy the data to the buffer, mark the buffer
2429 * dirty, and convert the chain to unmodified.
2431 bcopy(chain->data, bdata, chain->bytes);
2432 bp->b_flags |= B_CLUSTEROK;
2435 chain->bref.check.iscsi32.value =
2436 hammer2_icrc32(chain->data, chain->bytes);
2437 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
2438 ++hammer2_iod_meta_write;
2440 ++hammer2_iod_indr_write;
2442 chain->bref.check.iscsi32.value =
2443 hammer2_icrc32(chain->data, chain->bytes);
2448 * Adjustments to the bref. The caller will use this to adjust
2449 * our chain's pointer to this chain element.
2451 bref = &chain->bref;
2453 switch(bref->type) {
2454 case HAMMER2_BREF_TYPE_VOLUME:
2455 KKASSERT(chain->data != NULL);
2456 KKASSERT(chain->bp == NULL);
2458 hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT1]=
2460 (char *)&hmp->voldata +
2461 HAMMER2_VOLUME_ICRC1_OFF,
2462 HAMMER2_VOLUME_ICRC1_SIZE);
2463 hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT0]=
2465 (char *)&hmp->voldata +
2466 HAMMER2_VOLUME_ICRC0_OFF,
2467 HAMMER2_VOLUME_ICRC0_SIZE);
2468 hmp->voldata.icrc_volheader =
2470 (char *)&hmp->voldata +
2471 HAMMER2_VOLUME_ICRCVH_OFF,
2472 HAMMER2_VOLUME_ICRCVH_SIZE);
2479 if (hammer2_debug & 0x0008) {
2480 kprintf("%*.*s} %p/%d %04x ",
2481 info->depth, info->depth, "",
2482 chain, chain->refs, chain->flags);
2488 * PASS2 - not yet implemented (should be called only with the root chain?)
2491 hammer2_chain_flush_pass2(hammer2_mount_t *hmp, hammer2_chain_t *chain)
2497 * Stand-alone flush. If the chain is unable to completely flush we have
2498 * to be sure that SUBMODIFIED propagates up the parent chain.
2500 * This routine can be called from several places but the most important
2501 * is from the hammer2_vop_reclaim() function. We want to try to completely
2502 * clean out the inode structure to prevent disconnected inodes from
2503 * building up and blowing out the kmalloc pool.
2505 * If modify_tid is 0 (usual case), a new modify_tid is allocated and
2506 * applied to the flush. The depth-limit handling code is the only
2507 * code which passes a non-zero modify_tid to hammer2_chain_flush().
2510 hammer2_chain_flush(hammer2_mount_t *hmp, hammer2_chain_t *chain,
2511 hammer2_tid_t modify_tid)
2513 hammer2_chain_t *parent;
2514 hammer2_chain_t *scan;
2515 hammer2_blockref_t *base;
2516 hammer2_flush_info_t info;
2521 * Execute the recursive flush and handle deferrals.
2523 * Chains can be ridiculously long (thousands deep), so to
2524 * avoid blowing out the kernel stack the recursive flush has a
2525 * depth limit. Elements at the limit are placed on a list
2526 * for re-execution after the stack has been popped.
2528 bzero(&info, sizeof(info));
2529 TAILQ_INIT(&info.flush_list);
2531 if (modify_tid == 0) {
2532 hammer2_voldata_lock(hmp);
2533 info.modify_tid = hmp->voldata.alloc_tid++;
2534 hammer2_voldata_unlock(hmp);
2536 info.modify_tid = modify_tid;
2544 hammer2_chain_flush_pass1(hmp, chain, &info);
2547 while ((scan = TAILQ_FIRST(&info.flush_list)) != NULL) {
2549 * Secondary recursion. Note that a reference is
2550 * retained from the element's presence on the
2553 KKASSERT(scan->flags & HAMMER2_CHAIN_DEFERRED);
2554 TAILQ_REMOVE(&info.flush_list, scan, flush_node);
2555 atomic_clear_int(&scan->flags, HAMMER2_CHAIN_DEFERRED);
2558 * Now that we've popped back up we can do a secondary
2559 * recursion on the deferred elements.
2561 if (hammer2_debug & 0x0040)
2562 kprintf("defered flush %p\n", scan);
2563 hammer2_chain_lock(hmp, scan, HAMMER2_RESOLVE_MAYBE);
2564 hammer2_chain_flush(hmp, scan, info.modify_tid);
2565 hammer2_chain_unlock(hmp, scan);
2568 * Only flag a reflush if SUBMODIFIED is no longer
2569 * set. If SUBMODIFIED is set the element will just
2570 * wind up on our flush_list again.
2572 if ((scan->flags & (HAMMER2_CHAIN_SUBMODIFIED |
2573 HAMMER2_CHAIN_MODIFIED |
2574 HAMMER2_CHAIN_MODIFIED_AUX)) == 0) {
2577 hammer2_chain_drop(hmp, scan);
2579 if ((hammer2_debug & 0x0040) && reflush)
2580 kprintf("reflush %p\n", chain);
2584 * The SUBMODIFIED bit must propagate upward if the chain could not
2585 * be completely flushed.
2587 if (chain->flags & (HAMMER2_CHAIN_SUBMODIFIED |
2588 HAMMER2_CHAIN_MODIFIED |
2589 HAMMER2_CHAIN_MODIFIED_AUX |
2590 HAMMER2_CHAIN_MOVED)) {
2591 hammer2_chain_parent_setsubmod(hmp, chain);
2595 * If the only thing left is a simple bref update try to
2596 * pro-actively update the parent, otherwise return early.
2598 parent = chain->parent;
2599 if (parent == NULL) {
2602 if (chain->bref.type != HAMMER2_BREF_TYPE_INODE ||
2603 (chain->flags & (HAMMER2_CHAIN_SUBMODIFIED |
2604 HAMMER2_CHAIN_MODIFIED |
2605 HAMMER2_CHAIN_MODIFIED_AUX |
2606 HAMMER2_CHAIN_MOVED)) != HAMMER2_CHAIN_MOVED) {
2611 * We are locking backwards so allow the lock to fail
2613 if (lockmgr(&parent->lk, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
2618 * We are updating brefs but we have to call chain_modify()
2619 * because our caller is not being run from a recursive flush.
2621 * This will also chain up the parent list and set the SUBMODIFIED
2624 * We do not want to set HAMMER2_CHAIN_MODIFY_TID here because the
2625 * modification is only related to updating a bref in the parent.
2627 * When updating the blockset embedded in the volume header we must
2628 * also update voldata.mirror_tid.
2630 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_MAYBE);
2631 hammer2_chain_modify(hmp, parent, HAMMER2_MODIFY_NO_MODIFY_TID);
2633 switch(parent->bref.type) {
2634 case HAMMER2_BREF_TYPE_INODE:
2635 base = &parent->data->ipdata.u.blockset.
2637 count = HAMMER2_SET_COUNT;
2639 case HAMMER2_BREF_TYPE_INDIRECT:
2640 base = &parent->data->npdata.blockref[0];
2641 count = parent->bytes /
2642 sizeof(hammer2_blockref_t);
2644 case HAMMER2_BREF_TYPE_VOLUME:
2645 base = &hmp->voldata.sroot_blockset.blockref[0];
2646 count = HAMMER2_SET_COUNT;
2647 if (chain->flags & HAMMER2_CHAIN_MOVED) {
2648 if (hmp->voldata.mirror_tid < chain->bref.mirror_tid) {
2649 hmp->voldata.mirror_tid =
2650 chain->bref.mirror_tid;
2656 panic("hammer2_chain_flush: "
2657 "unrecognized blockref type: %d",
2662 * Update the blockref in the parent. We do not have to set
2663 * MOVED in the parent because SUBMODIFIED has already been
2664 * set, so a normal flush will pick up the changes and propagate
2665 * them upward for us.
2667 * We do have to propagate mirror_tid upward.
2669 KKASSERT(chain->index >= 0 &&
2670 chain->index < count);
2671 if (chain->flags & HAMMER2_CHAIN_MOVED) {
2672 base[chain->index] = chain->bref;
2673 if (parent->bref.mirror_tid < chain->bref.mirror_tid)
2674 parent->bref.mirror_tid = chain->bref.mirror_tid;
2675 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MOVED);
2676 hammer2_chain_drop(hmp, chain);
2677 } else if (bcmp(&base[chain->index],
2679 sizeof(chain->bref)) != 0) {
2680 panic("hammer2: unflagged bref update(2)");
2683 lockmgr(&parent->lk, LK_RELEASE); /* release manual lockmgr op */
2684 hammer2_chain_unlock(hmp, parent);