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
70 * modified elements. Stop when we hit a chain already flagged
71 * SUBMODIFIED, but ignore the SUBMODIFIED bit that might be set
74 * SUBMODIFIED is not set on the chain passed in.
76 * XXX rename of parent can create a SMP race
79 hammer2_chain_parent_setsubmod(hammer2_mount_t *hmp, hammer2_chain_t *chain)
81 hammer2_chain_t *parent;
83 parent = chain->parent;
84 while (parent && (parent->flags & HAMMER2_CHAIN_SUBMODIFIED) == 0) {
85 atomic_set_int(&parent->flags, HAMMER2_CHAIN_SUBMODIFIED);
86 parent = parent->parent;
91 * Allocate a new disconnected chain element representing the specified
92 * bref. The chain element is locked exclusively and refs is set to 1.
94 * This essentially allocates a system memory structure representing one
95 * of the media structure types, including inodes.
98 hammer2_chain_alloc(hammer2_mount_t *hmp, hammer2_blockref_t *bref)
100 hammer2_chain_t *chain;
102 hammer2_indblock_t *np;
104 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
107 * Construct the appropriate system structure.
110 case HAMMER2_BREF_TYPE_INODE:
111 ip = kmalloc(sizeof(*ip), hmp->minode, M_WAITOK | M_ZERO);
114 lockinit(&chain->lk, "inode", 0, LK_CANRECURSE);
117 case HAMMER2_BREF_TYPE_INDIRECT:
118 np = kmalloc(sizeof(*np), hmp->mchain, M_WAITOK | M_ZERO);
121 lockinit(&chain->lk, "iblk", 0, LK_CANRECURSE);
123 case HAMMER2_BREF_TYPE_DATA:
124 dp = kmalloc(sizeof(*dp), hmp->mchain, M_WAITOK | M_ZERO);
127 lockinit(&chain->lk, "dblk", 0, LK_CANRECURSE);
129 case HAMMER2_BREF_TYPE_VOLUME:
131 panic("hammer2_chain_alloc volume type illegal for op");
134 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
139 * Only set bref_flush if the bref has a real media offset, otherwise
140 * the caller has to wait for the chain to be modified/block-allocated
141 * before a blockref can be synchronized with its (future) parent.
144 if (bref->data_off & ~HAMMER2_OFF_MASK_RADIX)
145 chain->bref_flush = *bref;
146 chain->index = -1; /* not yet assigned */
148 chain->bytes = bytes;
149 lockmgr(&chain->lk, LK_EXCLUSIVE);
155 * Free a disconnected chain element
158 hammer2_chain_free(hammer2_mount_t *hmp, hammer2_chain_t *chain)
162 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
163 chain->bref.type == HAMMER2_BREF_TYPE_VOLUME) {
167 KKASSERT(chain->bp == NULL);
168 KKASSERT(chain->data == NULL);
169 KKASSERT(chain->bref.type != HAMMER2_BREF_TYPE_INODE ||
170 chain->u.ip->vp == NULL);
172 if ((mem = chain->u.mem) != NULL) {
174 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
175 kfree(mem, hmp->minode);
177 kfree(mem, hmp->mchain);
182 * Add a reference to a chain element (for shared access). The chain
183 * element must already have at least 1 ref controlled by the caller.
186 hammer2_chain_ref(hammer2_mount_t *hmp, hammer2_chain_t *chain)
188 KKASSERT(chain->refs > 0);
189 atomic_add_int(&chain->refs, 1);
193 * Drop the callers reference to the chain element. If the ref count
194 * reaches zero the chain element and its related structure (typically an
195 * inode or indirect block) will be freed and the parent will be
196 * recursively dropped.
198 * MOVED and MODIFIED elements hold additional references so it should not
199 * be possible for the count on a modified element to drop to 0.
201 * The chain element must NOT be locked by the caller.
203 * The parent might or might not be locked by the caller but if so it
204 * will also be referenced so we shouldn't recurse upward.
207 hammer2_chain_drop(hammer2_mount_t *hmp, hammer2_chain_t *chain)
209 hammer2_chain_t *parent;
217 KKASSERT(chain != &hmp->vchain);
218 parent = chain->parent;
220 lockmgr(&parent->lk, LK_EXCLUSIVE);
221 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
223 * Succeeded, recurse and drop parent.
224 * These chain elements should be synchronized
225 * so no delta data or inode count updates
228 KKASSERT((chain->flags &
229 (HAMMER2_CHAIN_MOVED |
230 HAMMER2_CHAIN_MODIFIED)) == 0);
231 if (!(chain->flags & HAMMER2_CHAIN_DELETED)) {
232 SPLAY_REMOVE(hammer2_chain_splay,
233 &parent->shead, chain);
234 atomic_set_int(&chain->flags,
235 HAMMER2_CHAIN_DELETED);
236 /* parent refs dropped via recursion */
238 chain->parent = NULL;
240 lockmgr(&parent->lk, LK_RELEASE);
241 hammer2_chain_free(hmp, chain);
243 /* recurse on parent */
246 lockmgr(&parent->lk, LK_RELEASE);
247 /* retry the same chain */
250 if (atomic_cmpset_int(&chain->refs, refs, refs - 1)) {
252 * Succeeded, count did not reach zero so
253 * cut out of the loop.
257 /* retry the same chain */
263 * Ref and lock a chain element, acquiring its data with I/O if necessary,
264 * and specify how you would like the data to be resolved.
266 * Returns 0 on success or an error code if the data could not be acquired.
267 * The chain element is locked either way.
269 * The lock is allowed to recurse, multiple locking ops will aggregate
270 * the requested resolve types. Once data is assigned it will not be
271 * removed until the last unlock.
273 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
274 * (typically used to avoid device/logical buffer
277 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
278 * the INITIAL-create state (indirect blocks only).
280 * Do not resolve data elements for DATA chains.
281 * (typically used to avoid device/logical buffer
284 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
287 * NOTE: Embedded elements (volume header, inodes) are always resolved
290 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
291 * element will instantiate and zero its buffer, and flush it on
294 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
295 * so as not to instantiate a device buffer, which could alias against
296 * a logical file buffer. However, if ALWAYS is specified the
297 * device buffer will be instantiated anyway.
300 hammer2_chain_lock(hammer2_mount_t *hmp, hammer2_chain_t *chain, int how)
302 hammer2_blockref_t *bref;
311 * Lock the element. Under certain conditions this might end up
312 * being a recursive lock.
314 KKASSERT(chain->refs > 0);
315 atomic_add_int(&chain->refs, 1);
316 lockmgr(&chain->lk, LK_EXCLUSIVE);
319 * If we already have a valid data pointer no further action is
326 * Do we have to resolve the data?
329 case HAMMER2_RESOLVE_NEVER:
331 case HAMMER2_RESOLVE_MAYBE:
332 if (chain->flags & HAMMER2_CHAIN_INITIAL)
334 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
337 case HAMMER2_RESOLVE_ALWAYS:
342 * We must resolve to a device buffer, either by issuing I/O or
343 * by creating a zero-fill element. We do not mark the buffer
344 * dirty when creating a zero-fill element (the hammer2_chain_modify()
345 * API must still be used to do that).
347 * The device buffer is variable-sized in powers of 2 down
348 * to HAMMER2_MINALLOCSIZE (typically 1K). A 64K physical storage
349 * chunk always contains buffers of the same size. (XXX)
351 * The minimum physical IO size may be larger than the variable
356 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
357 bbytes = HAMMER2_MINIOSIZE;
358 pbase = bref->data_off & ~(hammer2_off_t)(bbytes - 1);
359 peof = (pbase + HAMMER2_PBUFSIZE64) & ~HAMMER2_PBUFMASK64;
360 boff = bref->data_off & HAMMER2_OFF_MASK & (bbytes - 1);
361 KKASSERT(pbase != 0);
364 * The getblk() optimization can only be used on newly created
365 * elements if the physical block size matches the request.
367 if ((chain->flags & HAMMER2_CHAIN_INITIAL) &&
368 chain->bytes == bbytes) {
369 chain->bp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
371 } else if (hammer2_cluster_enable) {
372 error = cluster_read(hmp->devvp, peof, pbase, bbytes,
373 HAMMER2_PBUFSIZE, HAMMER2_PBUFSIZE,
376 error = bread(hmp->devvp, pbase, bbytes, &chain->bp);
380 kprintf("hammer2_chain_get: I/O error %016jx: %d\n",
381 (intmax_t)pbase, error);
388 * Zero the data area if the chain is in the INITIAL-create state.
389 * Mark the buffer for bdwrite().
391 bdata = (char *)chain->bp->b_data + boff;
392 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
393 bzero(bdata, chain->bytes);
394 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
398 * Setup the data pointer, either pointing it to an embedded data
399 * structure and copying the data from the buffer, or pointing it
402 * The buffer is not retained when copying to an embedded data
403 * structure in order to avoid potential deadlocks or recursions
404 * on the same physical buffer.
406 switch (bref->type) {
407 case HAMMER2_BREF_TYPE_VOLUME:
409 * Copy data from bp to embedded buffer
411 panic("hammer2_chain_lock: called on unresolved volume header");
414 KKASSERT(pbase == 0);
415 KKASSERT(chain->bytes == HAMMER2_PBUFSIZE);
416 bcopy(bdata, &hmp->voldata, chain->bytes);
417 chain->data = (void *)&hmp->voldata;
422 case HAMMER2_BREF_TYPE_INODE:
424 * Copy data from bp to embedded buffer, do not retain the
427 bcopy(bdata, &chain->u.ip->ip_data, chain->bytes);
428 chain->data = (void *)&chain->u.ip->ip_data;
432 case HAMMER2_BREF_TYPE_INDIRECT:
433 case HAMMER2_BREF_TYPE_DATA:
436 * Point data at the device buffer and leave bp intact.
438 chain->data = (void *)bdata;
445 * Unlock and deref a chain element.
447 * On the last lock release any non-embedded data (chain->bp) will be
451 hammer2_chain_unlock(hammer2_mount_t *hmp, hammer2_chain_t *chain)
456 * Undo a recursive lock
458 * XXX shared locks not handled properly
460 if (lockcountnb(&chain->lk) > 1) {
461 KKASSERT(chain->refs > 1);
462 atomic_add_int(&chain->refs, -1);
463 lockmgr(&chain->lk, LK_RELEASE);
468 * Shortcut the case if the data is embedded or not resolved.
470 * Do NOT null-out pointers to embedded data (e.g. inode).
472 * The DIRTYBP flag is non-applicable in this situation and can
473 * be cleared to keep the flags state clean.
475 if (chain->bp == NULL) {
476 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
477 lockmgr(&chain->lk, LK_RELEASE);
478 hammer2_chain_drop(hmp, chain);
485 if ((chain->flags & HAMMER2_CHAIN_DIRTYBP) == 0) {
487 } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
488 switch(chain->bref.type) {
489 case HAMMER2_BREF_TYPE_DATA:
490 counterp = &hammer2_ioa_file_write;
492 case HAMMER2_BREF_TYPE_INODE:
493 counterp = &hammer2_ioa_meta_write;
495 case HAMMER2_BREF_TYPE_INDIRECT:
496 counterp = &hammer2_ioa_indr_write;
499 counterp = &hammer2_ioa_volu_write;
504 switch(chain->bref.type) {
505 case HAMMER2_BREF_TYPE_DATA:
506 counterp = &hammer2_iod_file_write;
508 case HAMMER2_BREF_TYPE_INODE:
509 counterp = &hammer2_iod_meta_write;
511 case HAMMER2_BREF_TYPE_INDIRECT:
512 counterp = &hammer2_iod_indr_write;
515 counterp = &hammer2_iod_volu_write;
524 * If a device buffer was used for data be sure to destroy the
525 * buffer when we are done to avoid aliases (XXX what about the
526 * underlying VM pages?).
528 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
529 chain->bp->b_flags |= B_RELBUF;
532 * The DIRTYBP flag tracks whether we have to bdwrite() the buffer
533 * or not. The flag will get re-set when chain_modify() is called,
534 * even if MODIFIED is already set, allowing the OS to retire the
535 * buffer independent of a hammer2 flus.
538 if (chain->flags & HAMMER2_CHAIN_DIRTYBP) {
539 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
540 if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
541 atomic_clear_int(&chain->flags,
542 HAMMER2_CHAIN_IOFLUSH);
543 chain->bp->b_flags |= B_RELBUF;
544 cluster_awrite(chain->bp);
546 chain->bp->b_flags |= B_CLUSTEROK;
550 if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
551 atomic_clear_int(&chain->flags,
552 HAMMER2_CHAIN_IOFLUSH);
553 chain->bp->b_flags |= B_RELBUF;
556 /* bp might still be dirty */
561 lockmgr(&chain->lk, LK_RELEASE);
562 hammer2_chain_drop(hmp, chain);
566 * Resize the chain's physical storage allocation. Chains can be resized
567 * smaller without reallocating the storage. Resizing larger will reallocate
570 * Must be passed a locked chain.
572 * If you want the resize code to copy the data to the new block then the
573 * caller should lock the chain RESOLVE_MAYBE or RESOLVE_ALWAYS.
575 * If the caller already holds a logical buffer containing the data and
576 * intends to bdwrite() that buffer resolve with RESOLVE_NEVER. The resize
577 * operation will then not copy the data.
579 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
580 * to avoid instantiating a device buffer that conflicts with the vnode
583 * XXX flags currently ignored, uses chain->bp to detect data/no-data.
586 hammer2_chain_resize(hammer2_inode_t *ip, hammer2_chain_t *chain,
587 int nradix, int flags)
589 hammer2_mount_t *hmp = ip->hmp;
600 * Only data and indirect blocks can be resized for now
602 KKASSERT(chain != &hmp->vchain);
603 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
604 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
607 * Nothing to do if the element is already the proper size
609 obytes = chain->bytes;
610 nbytes = 1U << nradix;
611 if (obytes == nbytes)
615 * Set MODIFIED and add a chain ref to prevent destruction. Both
616 * modified flags share the same ref.
618 * If the chain is already marked MODIFIED then we can safely
619 * return the previous allocation to the pool without having to
620 * worry about snapshots.
622 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
623 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED |
624 HAMMER2_CHAIN_MODIFY_TID);
625 hammer2_chain_ref(hmp, chain);
627 hammer2_freemap_free(hmp, chain->bref.data_off,
632 * Relocate the block, even if making it smaller (because different
633 * block sizes may be in different regions).
635 chain->bref.data_off = hammer2_freemap_alloc(hmp, chain->bref.type,
637 chain->bytes = nbytes;
638 ip->delta_dcount += (ssize_t)(nbytes - obytes); /* XXX atomic */
641 * The device buffer may be larger than the allocation size.
643 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
644 bbytes = HAMMER2_MINIOSIZE;
645 pbase = chain->bref.data_off & ~(hammer2_off_t)(bbytes - 1);
646 boff = chain->bref.data_off & HAMMER2_OFF_MASK & (bbytes - 1);
649 * Only copy the data if resolved, otherwise the caller is
653 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
654 chain->bref.type == HAMMER2_BREF_TYPE_DATA);
655 KKASSERT(chain != &hmp->vchain); /* safety */
658 * The getblk() optimization can only be used if the
659 * physical block size matches the request.
661 if (nbytes == bbytes) {
662 nbp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
665 error = bread(hmp->devvp, pbase, bbytes, &nbp);
666 KKASSERT(error == 0);
668 bdata = (char *)nbp->b_data + boff;
670 if (nbytes < obytes) {
671 bcopy(chain->data, bdata, nbytes);
673 bcopy(chain->data, bdata, obytes);
674 bzero(bdata + obytes, nbytes - obytes);
678 * NOTE: The INITIAL state of the chain is left intact.
679 * We depend on hammer2_chain_modify() to do the
682 * NOTE: We set B_NOCACHE to throw away the previous bp and
683 * any VM backing store, even if it was dirty.
684 * Otherwise we run the risk of a logical/device
685 * conflict on reallocation.
687 chain->bp->b_flags |= B_RELBUF | B_NOCACHE;
690 chain->data = (void *)bdata;
691 hammer2_chain_modify(hmp, chain, 0);
695 * Make sure the chain is marked MOVED and SUBMOD is set in the
696 * parent(s) so the adjustments are picked up by flush.
698 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
699 hammer2_chain_ref(hmp, chain);
700 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
702 hammer2_chain_parent_setsubmod(hmp, chain);
706 * Convert a locked chain that was retrieved read-only to read-write.
708 * If not already marked modified a new physical block will be allocated
709 * and assigned to the bref.
711 * Non-data blocks - The chain should be locked to at least the RESOLVE_MAYBE
712 * level or the COW operation will not work.
714 * Data blocks - The chain is usually locked RESOLVE_NEVER so as not to
715 * run the data through the device buffers.
718 hammer2_chain_modify(hammer2_mount_t *hmp, hammer2_chain_t *chain, int flags)
728 * Tells flush that modify_tid must be updated, otherwise only
729 * mirror_tid is updated. This is the default.
731 if ((flags & HAMMER2_MODIFY_NO_MODIFY_TID) == 0)
732 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFY_TID);
735 * If the chain is already marked MODIFIED we can just return.
737 * However, it is possible that a prior lock/modify sequence
738 * retired the buffer. During this lock/modify sequence MODIFIED
739 * may still be set but the buffer could wind up clean. Since
740 * the caller is going to modify the buffer further we have to
741 * be sure that DIRTYBP is set again.
743 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
744 if ((flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
748 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
753 * Set MODIFIED and add a chain ref to prevent destruction. Both
754 * modified flags share the same ref.
756 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
757 hammer2_chain_ref(hmp, chain);
760 * We must allocate the copy-on-write block.
762 * If the data is embedded no other action is required.
764 * If the data is not embedded we acquire and clear the
765 * new block. If chain->data is not NULL we then do the
766 * copy-on-write. chain->data will then be repointed to the new
767 * buffer and the old buffer will be released.
769 * For newly created elements with no prior allocation we go
770 * through the copy-on-write steps except without the copying part.
772 if (chain != &hmp->vchain) {
773 if ((hammer2_debug & 0x0001) &&
774 (chain->bref.data_off & HAMMER2_OFF_MASK)) {
775 kprintf("Replace %d\n", chain->bytes);
777 chain->bref.data_off =
778 hammer2_freemap_alloc(hmp, chain->bref.type,
780 /* XXX failed allocation */
784 * If data instantiation is optional and the chain has no current
785 * data association (typical for DATA and newly-created INDIRECT
786 * elements), don't instantiate the buffer now.
788 if ((flags & HAMMER2_MODIFY_OPTDATA) && chain->bp == NULL)
793 * Setting the DIRTYBP flag will cause the buffer to be dirtied or
794 * written-out on unlock. This bit is independent of the MODIFIED
795 * bit because the chain may still need meta-data adjustments done
796 * by virtue of MODIFIED for its parent, and the buffer can be
797 * flushed out (possibly multiple times) by the OS before that.
799 * Clearing the INITIAL flag (for indirect blocks) indicates that
800 * a zero-fill buffer has been instantiated.
802 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
803 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
806 * We currently should never instantiate a device buffer for a
809 KKASSERT(chain->bref.type != HAMMER2_BREF_TYPE_DATA);
812 * Execute COW operation
814 switch(chain->bref.type) {
815 case HAMMER2_BREF_TYPE_VOLUME:
816 case HAMMER2_BREF_TYPE_INODE:
818 * The data is embedded, no copy-on-write operation is
821 KKASSERT(chain->bp == NULL);
823 case HAMMER2_BREF_TYPE_DATA:
824 case HAMMER2_BREF_TYPE_INDIRECT:
826 * Perform the copy-on-write operation
828 KKASSERT(chain != &hmp->vchain); /* safety */
830 * The device buffer may be larger than the allocation size.
832 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
833 bbytes = HAMMER2_MINIOSIZE;
834 pbase = chain->bref.data_off & ~(hammer2_off_t)(bbytes - 1);
835 boff = chain->bref.data_off & HAMMER2_OFF_MASK & (bbytes - 1);
838 * The getblk() optimization can only be used if the
839 * physical block size matches the request.
841 if (chain->bytes == bbytes) {
842 nbp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
845 error = bread(hmp->devvp, pbase, bbytes, &nbp);
846 KKASSERT(error == 0);
848 bdata = (char *)nbp->b_data + boff;
851 * Copy or zero-fill on write depending on whether
852 * chain->data exists or not.
855 bcopy(chain->data, bdata, chain->bytes);
856 KKASSERT(chain->bp != NULL);
858 bzero(bdata, chain->bytes);
861 chain->bp->b_flags |= B_RELBUF;
868 panic("hammer2_chain_modify: illegal non-embedded type %d",
874 if ((flags & HAMMER2_MODIFY_NOSUB) == 0)
875 hammer2_chain_parent_setsubmod(hmp, chain);
879 * Mark the volume as having been modified. This short-cut version
880 * does not have to lock the volume's chain, which allows the ioctl
881 * code to make adjustments to connections without deadlocking.
884 hammer2_modify_volume(hammer2_mount_t *hmp)
886 hammer2_voldata_lock(hmp);
887 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED_AUX);
888 hammer2_voldata_unlock(hmp);
892 * Locate an in-memory chain. The parent must be locked. The in-memory
893 * chain is returned or NULL if no in-memory chain is present.
895 * NOTE: A chain on-media might exist for this index when NULL is returned.
898 hammer2_chain_find(hammer2_mount_t *hmp, hammer2_chain_t *parent, int index)
900 hammer2_chain_t dummy;
901 hammer2_chain_t *chain;
904 chain = SPLAY_FIND(hammer2_chain_splay, &parent->shead, &dummy);
909 * Return a locked chain structure with all associated data acquired.
911 * Caller must lock the parent on call, the returned child will be locked.
914 hammer2_chain_get(hammer2_mount_t *hmp, hammer2_chain_t *parent,
915 int index, int flags)
917 hammer2_blockref_t *bref;
918 hammer2_chain_t *chain;
919 hammer2_chain_t dummy;
923 * Figure out how to lock. MAYBE can be used to optimized
924 * the initial-create state for indirect blocks.
926 if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK))
927 how = HAMMER2_RESOLVE_NEVER;
929 how = HAMMER2_RESOLVE_MAYBE;
932 * First see if we have a (possibly modified) chain element cached
933 * for this (parent, index). Acquire the data if necessary.
935 * If chain->data is non-NULL the chain should already be marked
939 chain = SPLAY_FIND(hammer2_chain_splay, &parent->shead, &dummy);
941 if (flags & HAMMER2_LOOKUP_NOLOCK)
942 hammer2_chain_ref(hmp, chain);
944 hammer2_chain_lock(hmp, chain, how);
949 * the get function must always succeed, panic if there's no
952 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
953 panic("hammer2_chain_get: Missing bref(1)");
958 * Otherwise lookup the bref and issue I/O (switch on the parent)
960 switch(parent->bref.type) {
961 case HAMMER2_BREF_TYPE_INODE:
962 KKASSERT(index >= 0 && index < HAMMER2_SET_COUNT);
963 bref = &parent->data->ipdata.u.blockset.blockref[index];
965 case HAMMER2_BREF_TYPE_INDIRECT:
966 KKASSERT(parent->data != NULL);
967 KKASSERT(index >= 0 &&
968 index < parent->bytes / sizeof(hammer2_blockref_t));
969 bref = &parent->data->npdata.blockref[index];
971 case HAMMER2_BREF_TYPE_VOLUME:
972 KKASSERT(index >= 0 && index < HAMMER2_SET_COUNT);
973 bref = &hmp->voldata.sroot_blockset.blockref[index];
977 panic("hammer2_chain_get: unrecognized blockref type: %d",
980 if (bref->type == 0) {
981 panic("hammer2_chain_get: Missing bref(2)");
986 * Allocate a chain structure representing the existing media
989 * The locking operation we do later will issue I/O to read it.
991 chain = hammer2_chain_alloc(hmp, bref);
994 * Link the chain into its parent. Caller is expected to hold an
995 * exclusive lock on the parent.
997 chain->parent = parent;
998 chain->index = index;
999 if (SPLAY_INSERT(hammer2_chain_splay, &parent->shead, chain))
1000 panic("hammer2_chain_link: collision");
1001 KKASSERT(parent->refs > 0);
1002 atomic_add_int(&parent->refs, 1); /* for splay entry */
1005 * Additional linkage for inodes. Reuse the parent pointer to
1006 * find the parent directory.
1008 if (bref->type == HAMMER2_BREF_TYPE_INODE) {
1009 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT)
1010 parent = parent->parent;
1011 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
1012 chain->u.ip->pip = parent->u.ip;
1013 chain->u.ip->pmp = parent->u.ip->pmp;
1014 chain->u.ip->depth = parent->u.ip->depth + 1;
1019 * Our new chain structure has already been referenced and locked
1020 * but the lock code handles the I/O so call it to resolve the data.
1021 * Then release one of our two exclusive locks.
1023 * If NOLOCK is set the release will release the one-and-only lock.
1025 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0) {
1026 hammer2_chain_lock(hmp, chain, how); /* recusive lock */
1027 hammer2_chain_drop(hmp, chain); /* excess ref */
1029 lockmgr(&chain->lk, LK_RELEASE); /* from alloc */
1035 * Locate any key between key_beg and key_end inclusive. (*parentp)
1036 * typically points to an inode but can also point to a related indirect
1037 * block and this function will recurse upwards and find the inode again.
1039 * WARNING! THIS DOES NOT RETURN KEYS IN LOGICAL KEY ORDER! ANY KEY
1040 * WITHIN THE RANGE CAN BE RETURNED. HOWEVER, AN ITERATION
1041 * WHICH PICKS UP WHERE WE LEFT OFF WILL CONTINUE THE SCAN.
1043 * (*parentp) must be exclusively locked and referenced and can be an inode
1044 * or an existing indirect block within the inode.
1046 * On return (*parentp) will be modified to point at the deepest parent chain
1047 * element encountered during the search, as a helper for an insertion or
1048 * deletion. The new (*parentp) will be locked and referenced and the old
1049 * will be unlocked and dereferenced (no change if they are both the same).
1051 * The matching chain will be returned exclusively locked and referenced.
1053 * NULL is returned if no match was found, but (*parentp) will still
1054 * potentially be adjusted.
1056 * This function will also recurse up the chain if the key is not within the
1057 * current parent's range. (*parentp) can never be set to NULL. An iteration
1058 * can simply allow (*parentp) to float inside the loop.
1061 hammer2_chain_lookup(hammer2_mount_t *hmp, hammer2_chain_t **parentp,
1062 hammer2_key_t key_beg, hammer2_key_t key_end,
1065 hammer2_chain_t *parent;
1066 hammer2_chain_t *chain;
1067 hammer2_chain_t *tmp;
1068 hammer2_blockref_t *base;
1069 hammer2_blockref_t *bref;
1070 hammer2_key_t scan_beg;
1071 hammer2_key_t scan_end;
1076 * Recurse (*parentp) upward if necessary until the parent completely
1077 * encloses the key range or we hit the inode.
1080 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT) {
1081 scan_beg = parent->bref.key;
1082 scan_end = scan_beg +
1083 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1084 if (key_beg >= scan_beg && key_end <= scan_end)
1086 hammer2_chain_ref(hmp, parent); /* ref old parent */
1087 hammer2_chain_unlock(hmp, parent); /* unlock old parent */
1088 parent = parent->parent;
1089 /* lock new parent */
1090 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_MAYBE);
1091 hammer2_chain_drop(hmp, *parentp); /* drop old parent */
1092 *parentp = parent; /* new parent */
1097 * Locate the blockref array. Currently we do a fully associative
1098 * search through the array.
1100 switch(parent->bref.type) {
1101 case HAMMER2_BREF_TYPE_INODE:
1103 * Special shortcut for embedded data returns the inode
1104 * itself. Callers must detect this condition and access
1105 * the embedded data (the strategy code does this for us).
1107 * This is only applicable to regular files and softlinks.
1109 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1110 if (flags & HAMMER2_LOOKUP_NOLOCK)
1111 hammer2_chain_ref(hmp, parent);
1113 hammer2_chain_lock(hmp, parent,
1114 HAMMER2_RESOLVE_ALWAYS);
1117 base = &parent->data->ipdata.u.blockset.blockref[0];
1118 count = HAMMER2_SET_COUNT;
1120 case HAMMER2_BREF_TYPE_INDIRECT:
1122 * Optimize indirect blocks in the INITIAL state to avoid
1125 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1128 if (parent->data == NULL)
1129 panic("parent->data is NULL");
1130 base = &parent->data->npdata.blockref[0];
1132 count = parent->bytes / sizeof(hammer2_blockref_t);
1134 case HAMMER2_BREF_TYPE_VOLUME:
1135 base = &hmp->voldata.sroot_blockset.blockref[0];
1136 count = HAMMER2_SET_COUNT;
1139 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
1141 base = NULL; /* safety */
1142 count = 0; /* safety */
1146 * If the element and key overlap we use the element.
1149 for (i = 0; i < count; ++i) {
1150 tmp = hammer2_chain_find(hmp, parent, i);
1153 KKASSERT(bref->type != 0);
1154 } else if (base == NULL || base[i].type == 0) {
1159 scan_beg = bref->key;
1160 scan_end = scan_beg + ((hammer2_key_t)1 << bref->keybits) - 1;
1161 if (key_beg <= scan_end && key_end >= scan_beg)
1165 if (key_beg == key_end)
1167 return (hammer2_chain_next(hmp, parentp, NULL,
1168 key_beg, key_end, flags));
1172 * Acquire the new chain element. If the chain element is an
1173 * indirect block we must search recursively.
1175 chain = hammer2_chain_get(hmp, parent, i, flags);
1180 * If the chain element is an indirect block it becomes the new
1181 * parent and we loop on it.
1183 * The parent always has to be locked with at least RESOLVE_MAYBE,
1184 * so it might need a fixup if the caller passed incompatible flags.
1186 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT) {
1187 hammer2_chain_unlock(hmp, parent);
1188 *parentp = parent = chain;
1189 if (flags & HAMMER2_LOOKUP_NOLOCK) {
1190 hammer2_chain_lock(hmp, chain, HAMMER2_RESOLVE_MAYBE);
1191 hammer2_chain_drop(hmp, chain); /* excess ref */
1192 } else if (flags & HAMMER2_LOOKUP_NODATA) {
1193 hammer2_chain_lock(hmp, chain, HAMMER2_RESOLVE_MAYBE);
1194 hammer2_chain_unlock(hmp, chain);
1200 * All done, return chain
1206 * After having issued a lookup we can iterate all matching keys.
1208 * If chain is non-NULL we continue the iteration from just after it's index.
1210 * If chain is NULL we assume the parent was exhausted and continue the
1211 * iteration at the next parent.
1213 * parent must be locked on entry and remains locked throughout. chain's
1214 * lock status must match flags.
1217 hammer2_chain_next(hammer2_mount_t *hmp, hammer2_chain_t **parentp,
1218 hammer2_chain_t *chain,
1219 hammer2_key_t key_beg, hammer2_key_t key_end,
1222 hammer2_chain_t *parent;
1223 hammer2_chain_t *tmp;
1224 hammer2_blockref_t *base;
1225 hammer2_blockref_t *bref;
1226 hammer2_key_t scan_beg;
1227 hammer2_key_t scan_end;
1235 * Calculate the next index and recalculate the parent if necessary.
1239 * Continue iteration within current parent. If not NULL
1240 * the passed-in chain may or may not be locked, based on
1241 * the LOOKUP_NOLOCK flag (passed in as returned from lookup
1244 i = chain->index + 1;
1245 if (flags & HAMMER2_LOOKUP_NOLOCK)
1246 hammer2_chain_drop(hmp, chain);
1248 hammer2_chain_unlock(hmp, chain);
1251 * Any scan where the lookup returned degenerate data embedded
1252 * in the inode has an invalid index and must terminate.
1254 if (chain == parent)
1257 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT) {
1259 * We reached the end of the iteration.
1264 * Continue iteration with next parent unless the current
1265 * parent covers the range.
1267 hammer2_chain_t *nparent;
1269 scan_beg = parent->bref.key;
1270 scan_end = scan_beg +
1271 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1272 if (key_beg >= scan_beg && key_end <= scan_end)
1275 i = parent->index + 1;
1276 nparent = parent->parent;
1277 hammer2_chain_ref(hmp, nparent); /* ref new parent */
1278 hammer2_chain_unlock(hmp, parent); /* unlock old parent */
1279 /* lock new parent */
1280 hammer2_chain_lock(hmp, nparent, HAMMER2_RESOLVE_MAYBE);
1281 hammer2_chain_drop(hmp, nparent); /* drop excess ref */
1282 *parentp = parent = nparent;
1287 * Locate the blockref array. Currently we do a fully associative
1288 * search through the array.
1290 switch(parent->bref.type) {
1291 case HAMMER2_BREF_TYPE_INODE:
1292 base = &parent->data->ipdata.u.blockset.blockref[0];
1293 count = HAMMER2_SET_COUNT;
1295 case HAMMER2_BREF_TYPE_INDIRECT:
1296 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1299 KKASSERT(parent->data != NULL);
1300 base = &parent->data->npdata.blockref[0];
1302 count = parent->bytes / sizeof(hammer2_blockref_t);
1304 case HAMMER2_BREF_TYPE_VOLUME:
1305 base = &hmp->voldata.sroot_blockset.blockref[0];
1306 count = HAMMER2_SET_COUNT;
1309 panic("hammer2_chain_next: unrecognized blockref type: %d",
1311 base = NULL; /* safety */
1312 count = 0; /* safety */
1315 KKASSERT(i <= count);
1318 * Look for the key. If we are unable to find a match and an exact
1319 * match was requested we return NULL. If a range was requested we
1320 * run hammer2_chain_next() to iterate.
1324 tmp = hammer2_chain_find(hmp, parent, i);
1327 } else if (base == NULL || base[i].type == 0) {
1333 scan_beg = bref->key;
1334 scan_end = scan_beg + ((hammer2_key_t)1 << bref->keybits) - 1;
1335 if (key_beg <= scan_end && key_end >= scan_beg)
1341 * If we couldn't find a match recurse up a parent to continue the
1348 * Acquire the new chain element. If the chain element is an
1349 * indirect block we must search recursively.
1351 chain = hammer2_chain_get(hmp, parent, i, flags);
1356 * If the chain element is an indirect block it becomes the new
1357 * parent and we loop on it.
1359 * The parent always has to be locked with at least RESOLVE_MAYBE,
1360 * so it might need a fixup if the caller passed incompatible flags.
1362 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT) {
1363 hammer2_chain_unlock(hmp, parent);
1364 *parentp = parent = chain;
1366 if (flags & HAMMER2_LOOKUP_NOLOCK) {
1367 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_MAYBE);
1368 hammer2_chain_drop(hmp, parent); /* excess ref */
1369 } else if (flags & HAMMER2_LOOKUP_NODATA) {
1370 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_MAYBE);
1371 hammer2_chain_unlock(hmp, parent);
1378 * All done, return chain
1384 * Create and return a new hammer2 system memory structure of the specified
1385 * key, type and size and insert it RELATIVE TO (PARENT).
1387 * (parent) is typically either an inode or an indirect block, acquired
1388 * acquired as a side effect of issuing a prior failed lookup. parent
1389 * must be locked and held. Do not pass the inode chain to this function
1390 * unless that is the chain returned by the failed lookup.
1392 * Non-indirect types will automatically allocate indirect blocks as required
1393 * if the new item does not fit in the current (parent).
1395 * Indirect types will move a portion of the existing blockref array in
1396 * (parent) into the new indirect type and then use one of the free slots
1397 * to emplace the new indirect type.
1399 * A new locked, referenced chain element is returned of the specified type.
1400 * The element may or may not have a data area associated with it:
1402 * VOLUME not allowed here
1403 * INODE embedded data are will be set-up
1404 * INDIRECT not allowed here
1405 * DATA no data area will be set-up (caller is expected
1406 * to have logical buffers, we don't want to alias
1407 * the data onto device buffers!).
1410 hammer2_chain_create(hammer2_mount_t *hmp, hammer2_chain_t *parent,
1411 hammer2_chain_t *chain,
1412 hammer2_key_t key, int keybits, int type, size_t bytes)
1414 hammer2_blockref_t dummy;
1415 hammer2_blockref_t *base;
1416 hammer2_chain_t dummy_chain;
1417 int unlock_parent = 0;
1422 if (chain == NULL) {
1424 * First allocate media space and construct the dummy bref,
1425 * then allocate the in-memory chain structure.
1427 bzero(&dummy, sizeof(dummy));
1430 dummy.keybits = keybits;
1431 dummy.data_off = hammer2_bytes_to_radix(bytes);
1432 chain = hammer2_chain_alloc(hmp, &dummy);
1436 * We do NOT set INITIAL here (yet). INITIAL is only
1437 * used for indirect blocks.
1439 * Recalculate bytes to reflect the actual media block
1442 bytes = (hammer2_off_t)1 <<
1443 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
1444 chain->bytes = bytes;
1447 case HAMMER2_BREF_TYPE_VOLUME:
1448 panic("hammer2_chain_create: called with volume type");
1450 case HAMMER2_BREF_TYPE_INODE:
1451 KKASSERT(bytes == HAMMER2_INODE_BYTES);
1452 chain->data = (void *)&chain->u.ip->ip_data;
1454 case HAMMER2_BREF_TYPE_INDIRECT:
1455 panic("hammer2_chain_create: cannot be used to"
1456 "create indirect block");
1458 case HAMMER2_BREF_TYPE_DATA:
1460 /* leave chain->data NULL */
1461 KKASSERT(chain->data == NULL);
1466 * Potentially update the chain's key/keybits.
1468 chain->bref.key = key;
1469 chain->bref.keybits = keybits;
1474 * Locate a free blockref in the parent's array
1476 switch(parent->bref.type) {
1477 case HAMMER2_BREF_TYPE_INODE:
1478 KKASSERT((parent->u.ip->ip_data.op_flags &
1479 HAMMER2_OPFLAG_DIRECTDATA) == 0);
1480 KKASSERT(parent->data != NULL);
1481 base = &parent->data->ipdata.u.blockset.blockref[0];
1482 count = HAMMER2_SET_COUNT;
1484 case HAMMER2_BREF_TYPE_INDIRECT:
1485 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1488 KKASSERT(parent->data != NULL);
1489 base = &parent->data->npdata.blockref[0];
1491 count = parent->bytes / sizeof(hammer2_blockref_t);
1493 case HAMMER2_BREF_TYPE_VOLUME:
1494 KKASSERT(parent->data != NULL);
1495 base = &hmp->voldata.sroot_blockset.blockref[0];
1496 count = HAMMER2_SET_COUNT;
1499 panic("hammer2_chain_create: unrecognized blockref type: %d",
1506 * Scan for an unallocated bref, also skipping any slots occupied
1507 * by in-memory chain elements that may not yet have been updated
1508 * in the parent's bref array.
1510 bzero(&dummy_chain, sizeof(dummy_chain));
1511 for (i = 0; i < count; ++i) {
1513 dummy_chain.index = i;
1514 if (SPLAY_FIND(hammer2_chain_splay,
1515 &parent->shead, &dummy_chain) == NULL) {
1518 } else if (base[i].type == 0) {
1519 dummy_chain.index = i;
1520 if (SPLAY_FIND(hammer2_chain_splay,
1521 &parent->shead, &dummy_chain) == NULL) {
1528 * If no free blockref could be found we must create an indirect
1529 * block and move a number of blockrefs into it. With the parent
1530 * locked we can safely lock each child in order to move it without
1531 * causing a deadlock.
1533 * This may return the new indirect block or the old parent depending
1534 * on where the key falls.
1537 hammer2_chain_t *nparent;
1539 nparent = hammer2_chain_create_indirect(hmp, parent,
1541 if (nparent == NULL) {
1543 hammer2_chain_free(hmp, chain);
1547 if (parent != nparent) {
1549 hammer2_chain_unlock(hmp, parent);
1557 * Link the chain into its parent. Later on we will have to set
1558 * the MOVED bit in situations where we don't mark the new chain
1559 * as being modified.
1561 if (chain->parent != NULL)
1562 panic("hammer2: hammer2_chain_create: chain already connected");
1563 KKASSERT(chain->parent == NULL);
1564 chain->parent = parent;
1566 if (SPLAY_INSERT(hammer2_chain_splay, &parent->shead, chain))
1567 panic("hammer2_chain_link: collision");
1568 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
1569 KKASSERT(parent->refs > 0);
1570 atomic_add_int(&parent->refs, 1);
1573 * Additional linkage for inodes. Reuse the parent pointer to
1574 * find the parent directory.
1576 * Cumulative adjustments are inherited on [re]attach and will
1577 * propagate up the tree on the next flush.
1579 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1580 hammer2_chain_t *scan = parent;
1581 hammer2_inode_t *ip = chain->u.ip;
1583 while (scan->bref.type == HAMMER2_BREF_TYPE_INDIRECT)
1584 scan = scan->parent;
1585 if (scan->bref.type == HAMMER2_BREF_TYPE_INODE) {
1586 ip->pip = scan->u.ip;
1587 ip->pmp = scan->u.ip->pmp;
1588 ip->depth = scan->u.ip->depth + 1;
1589 ip->pip->delta_icount += ip->ip_data.inode_count;
1590 ip->pip->delta_dcount += ip->ip_data.data_count;
1591 ++ip->pip->delta_icount;
1596 * (allocated) indicates that this is a newly-created chain element
1597 * rather than a renamed chain element. In this situation we want
1598 * to place the chain element in the MODIFIED state.
1600 * The data area will be set up as follows:
1602 * VOLUME not allowed here.
1604 * INODE embedded data are will be set-up.
1606 * INDIRECT not allowed here.
1608 * DATA no data area will be set-up (caller is expected
1609 * to have logical buffers, we don't want to alias
1610 * the data onto device buffers!).
1613 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1614 hammer2_chain_modify(hmp, chain,
1615 HAMMER2_MODIFY_OPTDATA);
1616 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT) {
1617 /* not supported in this function */
1618 panic("hammer2_chain_create: bad type");
1619 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1620 hammer2_chain_modify(hmp, chain,
1621 HAMMER2_MODIFY_OPTDATA);
1623 hammer2_chain_modify(hmp, chain, 0);
1627 * When reconnecting inodes we have to call setsubmod()
1628 * to ensure that its state propagates up the newly
1631 * Make sure MOVED is set but do not update bref_flush. If
1632 * the chain is undergoing modification bref_flush will be
1633 * updated when it gets flushed. If it is not then the
1634 * bref may not have been flushed yet and we do not want to
1635 * set MODIFIED here as this could result in unnecessary
1638 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
1639 hammer2_chain_ref(hmp, chain);
1640 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
1642 hammer2_chain_parent_setsubmod(hmp, chain);
1647 hammer2_chain_unlock(hmp, parent);
1652 * Create an indirect block that covers one or more of the elements in the
1653 * current parent. Either returns the existing parent with no locking or
1654 * ref changes or returns the new indirect block locked and referenced
1655 * and leaving the original parent lock/ref intact as well.
1657 * The returned chain depends on where the specified key falls.
1659 * The key/keybits for the indirect mode only needs to follow three rules:
1661 * (1) That all elements underneath it fit within its key space and
1663 * (2) That all elements outside it are outside its key space.
1665 * (3) When creating the new indirect block any elements in the current
1666 * parent that fit within the new indirect block's keyspace must be
1667 * moved into the new indirect block.
1669 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
1670 * keyspace the the current parent, but lookup/iteration rules will
1671 * ensure (and must ensure) that rule (2) for all parents leading up
1672 * to the nearest inode or the root volume header is adhered to. This
1673 * is accomplished by always recursing through matching keyspaces in
1674 * the hammer2_chain_lookup() and hammer2_chain_next() API.
1676 * The current implementation calculates the current worst-case keyspace by
1677 * iterating the current parent and then divides it into two halves, choosing
1678 * whichever half has the most elements (not necessarily the half containing
1679 * the requested key).
1681 * We can also opt to use the half with the least number of elements. This
1682 * causes lower-numbered keys (aka logical file offsets) to recurse through
1683 * fewer indirect blocks and higher-numbered keys to recurse through more.
1684 * This also has the risk of not moving enough elements to the new indirect
1685 * block and being forced to create several indirect blocks before the element
1690 hammer2_chain_create_indirect(hammer2_mount_t *hmp, hammer2_chain_t *parent,
1691 hammer2_key_t create_key, int create_bits)
1693 hammer2_blockref_t *base;
1694 hammer2_blockref_t *bref;
1695 hammer2_chain_t *chain;
1696 hammer2_chain_t *ichain;
1697 hammer2_chain_t dummy;
1698 hammer2_key_t key = create_key;
1699 int keybits = create_bits;
1707 * Calculate the base blockref pointer or NULL if the chain
1708 * is known to be empty. We need to calculate the array count
1709 * for SPLAY lookups either way.
1711 hammer2_chain_modify(hmp, parent, HAMMER2_MODIFY_OPTDATA);
1712 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1715 switch(parent->bref.type) {
1716 case HAMMER2_BREF_TYPE_INODE:
1717 count = HAMMER2_SET_COUNT;
1719 case HAMMER2_BREF_TYPE_INDIRECT:
1720 count = parent->bytes / sizeof(hammer2_blockref_t);
1722 case HAMMER2_BREF_TYPE_VOLUME:
1723 count = HAMMER2_SET_COUNT;
1726 panic("hammer2_chain_create_indirect: "
1727 "unrecognized blockref type: %d",
1733 switch(parent->bref.type) {
1734 case HAMMER2_BREF_TYPE_INODE:
1735 base = &parent->data->ipdata.u.blockset.blockref[0];
1736 count = HAMMER2_SET_COUNT;
1738 case HAMMER2_BREF_TYPE_INDIRECT:
1739 base = &parent->data->npdata.blockref[0];
1740 count = parent->bytes / sizeof(hammer2_blockref_t);
1742 case HAMMER2_BREF_TYPE_VOLUME:
1743 base = &hmp->voldata.sroot_blockset.blockref[0];
1744 count = HAMMER2_SET_COUNT;
1747 panic("hammer2_chain_create_indirect: "
1748 "unrecognized blockref type: %d",
1756 * Scan for an unallocated bref, also skipping any slots occupied
1757 * by in-memory chain elements which may not yet have been updated
1758 * in the parent's bref array.
1760 bzero(&dummy, sizeof(dummy));
1761 for (i = 0; i < count; ++i) {
1765 chain = SPLAY_FIND(hammer2_chain_splay, &parent->shead, &dummy);
1767 bref = &chain->bref;
1768 } else if (base && base[i].type) {
1775 * Expand our calculated key range (key, keybits) to fit
1776 * the scanned key. nkeybits represents the full range
1777 * that we will later cut in half (two halves @ nkeybits - 1).
1780 if (nkeybits < bref->keybits)
1781 nkeybits = bref->keybits;
1782 while (nkeybits < 64 &&
1783 (~(((hammer2_key_t)1 << nkeybits) - 1) &
1784 (key ^ bref->key)) != 0) {
1789 * If the new key range is larger we have to determine
1790 * which side of the new key range the existing keys fall
1791 * under by checking the high bit, then collapsing the
1792 * locount into the hicount or vise-versa.
1794 if (keybits != nkeybits) {
1795 if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
1806 * The newly scanned key will be in the lower half or the
1807 * higher half of the (new) key range.
1809 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
1816 * Adjust keybits to represent half of the full range calculated
1817 * above (radix 63 max)
1822 * Select whichever half contains the most elements. Theoretically
1823 * we can select either side as long as it contains at least one
1824 * element (in order to ensure that a free slot is present to hold
1825 * the indirect block).
1827 key &= ~(((hammer2_key_t)1 << keybits) - 1);
1828 if (hammer2_indirect_optimize) {
1830 * Insert node for least number of keys, this will arrange
1831 * the first few blocks of a large file or the first few
1832 * inodes in a directory with fewer indirect blocks when
1835 if (hicount < locount && hicount != 0)
1836 key |= (hammer2_key_t)1 << keybits;
1838 key &= ~(hammer2_key_t)1 << keybits;
1841 * Insert node for most number of keys, best for heavily
1844 if (hicount > locount)
1845 key |= (hammer2_key_t)1 << keybits;
1847 key &= ~(hammer2_key_t)1 << keybits;
1851 * How big should our new indirect block be? It has to be at least
1852 * as large as its parent.
1854 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE)
1855 nbytes = HAMMER2_IND_BYTES_MIN;
1857 nbytes = HAMMER2_IND_BYTES_MAX;
1858 if (nbytes < count * sizeof(hammer2_blockref_t))
1859 nbytes = count * sizeof(hammer2_blockref_t);
1862 * Ok, create our new indirect block
1864 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
1865 dummy.bref.key = key;
1866 dummy.bref.keybits = keybits;
1867 dummy.bref.data_off = hammer2_bytes_to_radix(nbytes);
1868 ichain = hammer2_chain_alloc(hmp, &dummy.bref);
1869 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
1872 * Iterate the original parent and move the matching brefs into
1873 * the new indirect block.
1875 for (i = 0; i < count; ++i) {
1877 * For keying purposes access the bref from the media or
1878 * from our in-memory cache. In cases where the in-memory
1879 * cache overrides the media the keyrefs will be the same
1880 * anyway so we can avoid checking the cache when the media
1884 chain = SPLAY_FIND(hammer2_chain_splay, &parent->shead, &dummy);
1886 bref = &chain->bref;
1887 } else if (base && base[i].type) {
1890 if (ichain->index < 0)
1896 * Skip keys not in the chosen half (low or high), only bit
1897 * (keybits - 1) needs to be compared but for safety we
1898 * will compare all msb bits plus that bit again.
1900 if ((~(((hammer2_key_t)1 << keybits) - 1) &
1901 (key ^ bref->key)) != 0) {
1906 * This element is being moved from the parent, its slot
1907 * is available for our new indirect block.
1909 if (ichain->index < 0)
1913 * Load the new indirect block by acquiring or allocating
1914 * the related chain entries, then simply move them to the
1915 * new parent (ichain).
1917 * When adjusting the parent/child relationship we must
1918 * set the MOVED bit but we do NOT update bref_flush
1919 * because otherwise we might synchronize a bref that has
1920 * not yet been flushed. We depend on chain's bref_flush
1921 * either being correct or the chain being in a MODIFIED
1924 * We do not want to set MODIFIED here as this would result
1925 * in unnecessary reallocations.
1927 * We must still set SUBMODIFIED in the parent but we do
1928 * that after the loop.
1930 * XXX we really need a lock here but we don't need the
1931 * data. NODATA feature needed.
1933 chain = hammer2_chain_get(hmp, parent, i,
1934 HAMMER2_LOOKUP_NODATA);
1935 SPLAY_REMOVE(hammer2_chain_splay, &parent->shead, chain);
1936 if (SPLAY_INSERT(hammer2_chain_splay, &ichain->shead, chain))
1937 panic("hammer2_chain_create_indirect: collision");
1938 chain->parent = ichain;
1940 bzero(&base[i], sizeof(base[i]));
1941 atomic_add_int(&parent->refs, -1);
1942 atomic_add_int(&ichain->refs, 1);
1943 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
1944 hammer2_chain_ref(hmp, chain);
1945 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
1947 hammer2_chain_unlock(hmp, chain);
1948 KKASSERT(parent->refs > 0);
1953 * Insert the new indirect block into the parent now that we've
1954 * cleared out some entries in the parent. We calculated a good
1955 * insertion index in the loop above (ichain->index).
1957 * We don't have to set MOVED here because we mark ichain modified
1958 * down below (so the normal modified -> flush -> set-moved sequence
1961 KKASSERT(ichain->index >= 0);
1962 if (SPLAY_INSERT(hammer2_chain_splay, &parent->shead, ichain))
1963 panic("hammer2_chain_create_indirect: ichain insertion");
1964 ichain->parent = parent;
1965 atomic_add_int(&parent->refs, 1);
1968 * Mark the new indirect block modified after insertion, which
1969 * will propagate up through parent all the way to the root and
1970 * also allocate the physical block in ichain for our caller,
1971 * and assign ichain->data to a pre-zero'd space (because there
1972 * is not prior data to copy into it).
1974 * We have to set SUBMODIFIED in ichain's flags manually so the
1975 * flusher knows it has to recurse through it to get to all of
1976 * our moved blocks, then call setsubmod() to set the bit
1979 hammer2_chain_modify(hmp, ichain, HAMMER2_MODIFY_OPTDATA);
1980 hammer2_chain_parent_setsubmod(hmp, ichain);
1981 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_SUBMODIFIED);
1984 * Figure out what to return.
1986 if (create_bits > keybits) {
1988 * Key being created is way outside the key range,
1989 * return the original parent.
1991 hammer2_chain_unlock(hmp, ichain);
1992 } else if (~(((hammer2_key_t)1 << keybits) - 1) &
1993 (create_key ^ key)) {
1995 * Key being created is outside the key range,
1996 * return the original parent.
1998 hammer2_chain_unlock(hmp, ichain);
2001 * Otherwise its in the range, return the new parent.
2002 * (leave both the new and old parent locked).
2011 * Physically delete the specified chain element. Note that inodes with
2012 * open descriptors should not be deleted (as with other filesystems) until
2013 * the last open descriptor is closed.
2015 * This routine will remove the chain element from its parent and potentially
2016 * also recurse upward and delete indirect blocks which become empty as a
2019 * The caller must pass a pointer to the chain's parent, also locked and
2020 * referenced. (*parentp) will be modified in a manner similar to a lookup
2021 * or iteration when indirect blocks are also deleted as a side effect.
2023 * XXX This currently does not adhere to the MOVED flag protocol in that
2024 * the removal is immediately indicated in the parent's blockref[]
2028 hammer2_chain_delete(hammer2_mount_t *hmp, hammer2_chain_t *parent,
2029 hammer2_chain_t *chain, int retain)
2031 hammer2_blockref_t *base;
2032 hammer2_inode_t *ip;
2035 if (chain->parent != parent)
2036 panic("hammer2_chain_delete: parent mismatch");
2039 * Mark the parent modified so our base[] pointer remains valid
2040 * while we move entries. For the optimized indirect block
2041 * case mark the parent moved instead.
2043 * Calculate the blockref reference in the parent
2045 switch(parent->bref.type) {
2046 case HAMMER2_BREF_TYPE_INODE:
2047 hammer2_chain_modify(hmp, parent, HAMMER2_MODIFY_NO_MODIFY_TID);
2048 base = &parent->data->ipdata.u.blockset.blockref[0];
2049 count = HAMMER2_SET_COUNT;
2051 case HAMMER2_BREF_TYPE_INDIRECT:
2052 hammer2_chain_modify(hmp, parent, HAMMER2_MODIFY_OPTDATA |
2053 HAMMER2_MODIFY_NO_MODIFY_TID);
2054 if (parent->flags & HAMMER2_CHAIN_INITIAL)
2057 base = &parent->data->npdata.blockref[0];
2058 count = parent->bytes / sizeof(hammer2_blockref_t);
2060 case HAMMER2_BREF_TYPE_VOLUME:
2061 hammer2_chain_modify(hmp, parent, HAMMER2_MODIFY_NO_MODIFY_TID);
2062 base = &hmp->voldata.sroot_blockset.blockref[0];
2063 count = HAMMER2_SET_COUNT;
2066 panic("hammer2_chain_delete: unrecognized blockref type: %d",
2073 * Disconnect the bref in the parent, remove the chain, and
2074 * disconnect in-memory fields from the parent.
2076 KKASSERT(chain->index >= 0 && chain->index < count);
2078 bzero(&base[chain->index], sizeof(*base));
2080 SPLAY_REMOVE(hammer2_chain_splay, &parent->shead, chain);
2081 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2082 atomic_add_int(&parent->refs, -1); /* for splay entry */
2084 chain->parent = NULL;
2087 * Cumulative adjustments must be propagated to the parent inode
2088 * when deleting and synchronized to ip.
2090 * NOTE: We do not propagate ip->delta_*count to the parent because
2091 * these represent adjustments that have not yet been
2092 * propagated upward, so we don't need to remove them from
2095 * Clear the pointer to the parent inode.
2097 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2100 ip->pip->delta_icount -= ip->ip_data.inode_count;
2101 ip->pip->delta_dcount -= ip->ip_data.data_count;
2102 ip->ip_data.inode_count += ip->delta_icount;
2103 ip->ip_data.data_count += ip->delta_dcount;
2104 ip->delta_icount = 0;
2105 ip->delta_dcount = 0;
2106 --ip->pip->delta_icount;
2109 chain->u.ip->depth = 0;
2113 * If retain is 0 the deletion is permanent. Because the chain is
2114 * no longer connected to the topology a flush will have no
2115 * visibility into it. We must dispose of the references related
2116 * to the MODIFIED and MOVED flags, otherwise the ref count will
2117 * never transition to 0.
2119 * If retain is non-zero the deleted element is likely an inode
2120 * which the vnops frontend will mark DESTROYED and flush. In that
2121 * situation we must retain the flags for any open file descriptors
2122 * on the (removed) inode. The final close will destroy the
2123 * disconnected chain.
2126 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
2127 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
2128 hammer2_chain_drop(hmp, chain);
2130 if (chain->flags & HAMMER2_CHAIN_MOVED) {
2131 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MOVED);
2132 hammer2_chain_drop(hmp, chain);
2137 * The chain is still likely referenced, possibly even by a vnode
2138 * (if an inode), so defer further action until the chain gets
2144 * Recursively flush the specified chain. The chain is locked and
2145 * referenced by the caller and will remain so on return. The chain
2146 * will remain referenced throughout but can temporarily lose its
2147 * lock during the recursion to avoid unnecessarily stalling user
2152 TAILQ_HEAD(flush_deferral_list, hammer2_chain);
2154 struct hammer2_flush_info {
2155 struct flush_deferral_list flush_list;
2157 hammer2_tid_t modify_tid;
2160 typedef struct hammer2_flush_info hammer2_flush_info_t;
2163 hammer2_chain_flush_pass1(hammer2_mount_t *hmp, hammer2_chain_t *chain,
2164 hammer2_flush_info_t *info)
2166 hammer2_blockref_t *bref;
2167 hammer2_off_t pbase;
2176 * If we hit the stack recursion depth limit defer the operation.
2177 * The controller of the info structure will execute the deferral
2178 * list and then retry.
2180 * This is only applicable if SUBMODIFIED is set. After a reflush
2181 * SUBMODIFIED will probably be cleared and we want to drop through
2182 * to finish processing the current element so our direct parent
2183 * can process the results.
2185 if (info->depth == HAMMER2_FLUSH_DEPTH_LIMIT &&
2186 (chain->flags & HAMMER2_CHAIN_SUBMODIFIED)) {
2187 if ((chain->flags & HAMMER2_CHAIN_DEFERRED) == 0) {
2188 hammer2_chain_ref(hmp, chain);
2189 TAILQ_INSERT_TAIL(&info->flush_list,
2191 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DEFERRED);
2196 if (hammer2_debug & 0x0008)
2197 kprintf("%*.*sCHAIN type=%d@%08jx %p/%d %04x {\n",
2198 info->depth, info->depth, "",
2199 chain->bref.type, chain->bref.data_off,
2200 chain, chain->refs, chain->flags);
2203 * If SUBMODIFIED is set we recurse the flush and adjust the
2204 * blockrefs accordingly.
2206 * NOTE: Looping on SUBMODIFIED can prevent a flush from ever
2207 * finishing in the face of filesystem activity.
2209 if (chain->flags & HAMMER2_CHAIN_SUBMODIFIED) {
2210 hammer2_chain_t *child;
2211 hammer2_chain_t *next;
2212 hammer2_blockref_t *base;
2216 * Clear SUBMODIFIED to catch races. Note that if any
2217 * child has to be flushed SUBMODIFIED will wind up being
2218 * set again (for next time), but this does not stop us from
2219 * synchronizing block updates which occurred.
2221 * We don't want to set our chain to MODIFIED gratuitously.
2223 /* XXX SUBMODIFIED not interlocked, can race */
2224 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_SUBMODIFIED);
2227 * Flush the children and update the blockrefs in the chain.
2228 * Be careful of ripouts during the loop.
2230 next = SPLAY_MIN(hammer2_chain_splay, &chain->shead);
2232 hammer2_chain_ref(hmp, next);
2233 while ((child = next) != NULL) {
2234 next = SPLAY_NEXT(hammer2_chain_splay,
2235 &chain->shead, child);
2237 hammer2_chain_ref(hmp, next);
2239 * We only recurse if SUBMODIFIED (internal node)
2240 * or MODIFIED (internal node or leaf) is set.
2241 * However, we must still track whether any MOVED
2242 * entries are present to determine if the chain's
2243 * blockref's need updating or not.
2245 if ((child->flags & (HAMMER2_CHAIN_SUBMODIFIED |
2246 HAMMER2_CHAIN_MODIFIED |
2247 HAMMER2_CHAIN_MODIFIED_AUX)) == 0) {
2248 hammer2_chain_drop(hmp, child);
2251 hammer2_chain_lock(hmp, child, HAMMER2_RESOLVE_MAYBE);
2252 hammer2_chain_drop(hmp, child);
2253 if (child->parent != chain ||
2254 (child->flags & (HAMMER2_CHAIN_SUBMODIFIED |
2255 HAMMER2_CHAIN_MODIFIED |
2256 HAMMER2_CHAIN_MODIFIED_AUX)) == 0) {
2257 hammer2_chain_unlock(hmp, child);
2262 * Propagate the DESTROYED flag if found set, then
2263 * recurse the flush.
2265 if ((chain->flags & HAMMER2_CHAIN_DESTROYED) &&
2266 (child->flags & HAMMER2_CHAIN_DESTROYED) == 0) {
2267 atomic_set_int(&child->flags,
2268 HAMMER2_CHAIN_DESTROYED |
2269 HAMMER2_CHAIN_SUBMODIFIED);
2272 hammer2_chain_flush_pass1(hmp, child, info);
2274 hammer2_chain_unlock(hmp, child);
2278 * Now synchronize any block updates.
2280 next = SPLAY_MIN(hammer2_chain_splay, &chain->shead);
2282 hammer2_chain_ref(hmp, next);
2283 while ((child = next) != NULL) {
2284 next = SPLAY_NEXT(hammer2_chain_splay,
2285 &chain->shead, child);
2287 hammer2_chain_ref(hmp, next);
2288 if ((child->flags & HAMMER2_CHAIN_MOVED) == 0) {
2289 hammer2_chain_drop(hmp, child);
2292 hammer2_chain_lock(hmp, child, HAMMER2_RESOLVE_NEVER);
2293 hammer2_chain_drop(hmp, child);
2294 if (child->parent != chain ||
2295 (child->flags & HAMMER2_CHAIN_MOVED) == 0) {
2296 hammer2_chain_unlock(hmp, child);
2300 hammer2_chain_modify(hmp, chain,
2301 HAMMER2_MODIFY_NO_MODIFY_TID);
2303 switch(chain->bref.type) {
2304 case HAMMER2_BREF_TYPE_INODE:
2305 KKASSERT((chain->data->ipdata.op_flags &
2306 HAMMER2_OPFLAG_DIRECTDATA) == 0);
2307 base = &chain->data->ipdata.u.blockset.
2309 count = HAMMER2_SET_COUNT;
2311 case HAMMER2_BREF_TYPE_INDIRECT:
2312 base = &chain->data->npdata.blockref[0];
2313 count = chain->bytes /
2314 sizeof(hammer2_blockref_t);
2316 case HAMMER2_BREF_TYPE_VOLUME:
2317 base = &hmp->voldata.sroot_blockset.blockref[0];
2318 count = HAMMER2_SET_COUNT;
2322 panic("hammer2_chain_get: "
2323 "unrecognized blockref type: %d",
2327 KKASSERT(child->index >= 0);
2328 base[child->index] = child->bref_flush;
2330 if (chain->bref.mirror_tid <
2331 child->bref_flush.mirror_tid) {
2332 chain->bref.mirror_tid =
2333 child->bref_flush.mirror_tid;
2336 if (chain->bref.type == HAMMER2_BREF_TYPE_VOLUME &&
2337 hmp->voldata.mirror_tid <
2338 child->bref_flush.mirror_tid) {
2339 hmp->voldata.mirror_tid =
2340 child->bref_flush.mirror_tid;
2342 atomic_clear_int(&child->flags, HAMMER2_CHAIN_MOVED);
2343 hammer2_chain_drop(hmp, child); /* MOVED flag */
2344 hammer2_chain_unlock(hmp, child);
2349 * If destroying the object we unconditonally clear the MODIFIED
2350 * and MOVED bits, and we destroy the buffer without writing it
2353 * We don't bother updating the hash/crc or the chain bref.
2355 * NOTE: The destroy'd object's bref has already been updated.
2356 * so we can clear MOVED without propagating mirror_tid
2357 * or modify_tid upward.
2359 * XXX allocations for unflushed data can be returned to the
2362 if (chain->flags & HAMMER2_CHAIN_DESTROYED) {
2363 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
2365 chain->bp->b_flags |= B_INVAL|B_RELBUF;
2367 atomic_clear_int(&chain->flags,
2368 HAMMER2_CHAIN_MODIFIED |
2369 HAMMER2_CHAIN_MODIFY_TID);
2370 hammer2_chain_drop(hmp, chain);
2372 if (chain->flags & HAMMER2_CHAIN_MODIFIED_AUX) {
2373 atomic_clear_int(&chain->flags,
2374 HAMMER2_CHAIN_MODIFIED_AUX);
2376 if (chain->flags & HAMMER2_CHAIN_MOVED) {
2377 atomic_clear_int(&chain->flags,
2378 HAMMER2_CHAIN_MOVED);
2379 hammer2_chain_drop(hmp, chain);
2385 * Flush this chain entry only if it is marked modified.
2387 if ((chain->flags & (HAMMER2_CHAIN_MODIFIED |
2388 HAMMER2_CHAIN_MODIFIED_AUX)) == 0) {
2393 * Synchronize cumulative data and inode count adjustments to
2394 * the inode and propagate the deltas upward to the parent.
2396 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2397 hammer2_inode_t *ip;
2400 ip->ip_data.inode_count += ip->delta_icount;
2401 ip->ip_data.data_count += ip->delta_dcount;
2403 ip->pip->delta_icount += ip->delta_icount;
2404 ip->pip->delta_dcount += ip->delta_dcount;
2406 ip->delta_icount = 0;
2407 ip->delta_dcount = 0;
2411 * Flush if MODIFIED or MODIFIED_AUX is set. MODIFIED_AUX is only
2412 * used by the volume header (&hmp->vchain).
2414 if ((chain->flags & (HAMMER2_CHAIN_MODIFIED |
2415 HAMMER2_CHAIN_MODIFIED_AUX)) == 0) {
2418 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED_AUX);
2421 * Clear MODIFIED and set HAMMER2_CHAIN_MOVED. The caller
2422 * will re-test the MOVED bit. We must also update the mirror_tid
2423 * and modify_tid fields as appropriate.
2425 * bits own a single chain ref and the MOVED bit owns its own
2428 chain->bref.mirror_tid = info->modify_tid;
2429 if (chain->flags & HAMMER2_CHAIN_MODIFY_TID)
2430 chain->bref.modify_tid = info->modify_tid;
2431 wasmodified = (chain->flags & HAMMER2_CHAIN_MODIFIED) != 0;
2432 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED |
2433 HAMMER2_CHAIN_MODIFY_TID);
2435 if (chain->flags & HAMMER2_CHAIN_MOVED) {
2437 * Drop the ref from the MODIFIED bit we cleared.
2440 hammer2_chain_drop(hmp, chain);
2443 * If we were MODIFIED we inherit the ref from clearing
2444 * that bit, otherwise we need another ref.
2446 if (wasmodified == 0)
2447 hammer2_chain_ref(hmp, chain);
2448 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
2450 chain->bref_flush = chain->bref;
2453 * If this is part of a recursive flush we can go ahead and write
2454 * out the buffer cache buffer and pass a new bref back up the chain.
2456 * This will never be a volume header.
2458 switch(chain->bref.type) {
2459 case HAMMER2_BREF_TYPE_VOLUME:
2461 * The volume header is flushed manually by the syncer, not
2465 case HAMMER2_BREF_TYPE_DATA:
2467 * Data elements have already been flushed via the logical
2468 * file buffer cache. Their hash was set in the bref by
2469 * the vop_write code.
2471 * Make sure the buffer(s) have been flushed out here.
2473 bbytes = chain->bytes;
2474 pbase = chain->bref.data_off & ~(hammer2_off_t)(bbytes - 1);
2475 boff = chain->bref.data_off & HAMMER2_OFF_MASK & (bbytes - 1);
2477 bp = getblk(hmp->devvp, pbase, bbytes, GETBLK_NOWAIT, 0);
2479 if ((bp->b_flags & (B_CACHE | B_DIRTY)) ==
2480 (B_CACHE | B_DIRTY)) {
2484 bp->b_flags |= B_RELBUF;
2489 case HAMMER2_BREF_TYPE_INDIRECT:
2491 * Indirect blocks may be in an INITIAL state. Use the
2492 * chain_lock() call to ensure that the buffer has been
2493 * instantiated (even though it is already locked the buffer
2494 * might not have been instantiated).
2496 * Only write the buffer out if it is dirty, it is possible
2497 * the operating system had already written out the buffer.
2499 hammer2_chain_lock(hmp, chain, HAMMER2_RESOLVE_ALWAYS);
2500 KKASSERT(chain->bp != NULL);
2503 if ((chain->flags & HAMMER2_CHAIN_DIRTYBP) ||
2504 (bp->b_flags & B_DIRTY)) {
2511 hammer2_chain_unlock(hmp, chain);
2515 * Embedded elements have to be flushed out.
2517 KKASSERT(chain->data != NULL);
2518 KKASSERT(chain->bp == NULL);
2519 bref = &chain->bref;
2521 KKASSERT((bref->data_off & HAMMER2_OFF_MASK) != 0);
2523 if (chain->bp == NULL) {
2525 * The data is embedded, we have to acquire the
2526 * buffer cache buffer and copy the data into it.
2528 if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
2529 bbytes = HAMMER2_MINIOSIZE;
2530 pbase = bref->data_off & ~(hammer2_off_t)(bbytes - 1);
2531 boff = bref->data_off & HAMMER2_OFF_MASK & (bbytes - 1);
2534 * The getblk() optimization can only be used if the
2535 * physical block size matches the request.
2537 if (chain->bytes == bbytes) {
2538 bp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
2541 error = bread(hmp->devvp, pbase, bbytes, &bp);
2542 KKASSERT(error == 0);
2544 bdata = (char *)bp->b_data + boff;
2547 * Copy the data to the buffer, mark the buffer
2548 * dirty, and convert the chain to unmodified.
2550 * We expect we might have to make adjustments to
2551 * non-data delayed-write buffers when doing an
2552 * actual flush so use bawrite() instead of
2553 * cluster_awrite() here.
2555 bcopy(chain->data, bdata, chain->bytes);
2556 bp->b_flags |= B_CLUSTEROK;
2559 chain->bref.check.iscsi32.value =
2560 hammer2_icrc32(chain->data, chain->bytes);
2561 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
2562 ++hammer2_iod_meta_write;
2564 ++hammer2_iod_indr_write;
2566 chain->bref.check.iscsi32.value =
2567 hammer2_icrc32(chain->data, chain->bytes);
2572 * Adjustments to the bref. The caller will use this to adjust
2573 * our chain's pointer to this chain element.
2575 bref = &chain->bref;
2577 switch(bref->type) {
2578 case HAMMER2_BREF_TYPE_VOLUME:
2579 KKASSERT(chain->data != NULL);
2580 KKASSERT(chain->bp == NULL);
2582 hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT1]=
2584 (char *)&hmp->voldata +
2585 HAMMER2_VOLUME_ICRC1_OFF,
2586 HAMMER2_VOLUME_ICRC1_SIZE);
2587 hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT0]=
2589 (char *)&hmp->voldata +
2590 HAMMER2_VOLUME_ICRC0_OFF,
2591 HAMMER2_VOLUME_ICRC0_SIZE);
2592 hmp->voldata.icrc_volheader =
2594 (char *)&hmp->voldata +
2595 HAMMER2_VOLUME_ICRCVH_OFF,
2596 HAMMER2_VOLUME_ICRCVH_SIZE);
2603 if (hammer2_debug & 0x0008) {
2604 kprintf("%*.*s} %p/%d %04x ",
2605 info->depth, info->depth, "",
2606 chain, chain->refs, chain->flags);
2612 * PASS2 - not yet implemented (should be called only with the root chain?)
2615 hammer2_chain_flush_pass2(hammer2_mount_t *hmp, hammer2_chain_t *chain)
2621 * Stand-alone flush. If the chain is unable to completely flush we have
2622 * to be sure that SUBMODIFIED propagates up the parent chain. We must not
2623 * clear the MOVED bit after flushing in this situation or our desynchronized
2624 * bref will not properly update in the parent.
2626 * This routine can be called from several places but the most important
2627 * is from the hammer2_vop_reclaim() function. We want to try to completely
2628 * clean out the inode structure to prevent disconnected inodes from
2629 * building up and blowing out the kmalloc pool.
2631 * If modify_tid is 0 (usual case), a new modify_tid is allocated and
2632 * applied to the flush. The depth-limit handling code is the only
2633 * code which passes a non-zero modify_tid to hammer2_chain_flush().
2636 hammer2_chain_flush(hammer2_mount_t *hmp, hammer2_chain_t *chain,
2637 hammer2_tid_t modify_tid)
2639 hammer2_chain_t *parent;
2640 hammer2_chain_t *scan;
2641 hammer2_blockref_t *base;
2642 hammer2_flush_info_t info;
2647 * Execute the recursive flush and handle deferrals.
2649 * Chains can be ridiculously long (thousands deep), so to
2650 * avoid blowing out the kernel stack the recursive flush has a
2651 * depth limit. Elements at the limit are placed on a list
2652 * for re-execution after the stack has been popped.
2654 bzero(&info, sizeof(info));
2655 TAILQ_INIT(&info.flush_list);
2657 if (modify_tid == 0) {
2658 hammer2_voldata_lock(hmp);
2659 info.modify_tid = hmp->voldata.alloc_tid++;
2660 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED_AUX);
2661 hammer2_voldata_unlock(hmp);
2663 info.modify_tid = modify_tid;
2671 hammer2_chain_flush_pass1(hmp, chain, &info);
2674 while ((scan = TAILQ_FIRST(&info.flush_list)) != NULL) {
2676 * Secondary recursion. Note that a reference is
2677 * retained from the element's presence on the
2680 KKASSERT(scan->flags & HAMMER2_CHAIN_DEFERRED);
2681 TAILQ_REMOVE(&info.flush_list, scan, flush_node);
2682 atomic_clear_int(&scan->flags, HAMMER2_CHAIN_DEFERRED);
2685 * Now that we've popped back up we can do a secondary
2686 * recursion on the deferred elements.
2688 if (hammer2_debug & 0x0040)
2689 kprintf("defered flush %p\n", scan);
2690 hammer2_chain_lock(hmp, scan, HAMMER2_RESOLVE_MAYBE);
2691 hammer2_chain_flush(hmp, scan, info.modify_tid);
2692 hammer2_chain_unlock(hmp, scan);
2695 * Only flag a reflush if SUBMODIFIED is no longer
2696 * set. If SUBMODIFIED is set the element will just
2697 * wind up on our flush_list again.
2699 if ((scan->flags & (HAMMER2_CHAIN_SUBMODIFIED |
2700 HAMMER2_CHAIN_MODIFIED |
2701 HAMMER2_CHAIN_MODIFIED_AUX)) == 0) {
2704 hammer2_chain_drop(hmp, scan);
2706 if ((hammer2_debug & 0x0040) && reflush)
2707 kprintf("reflush %p\n", chain);
2711 * The SUBMODIFIED bit must propagate upward if the chain could not
2712 * be completely flushed.
2714 if (chain->flags & (HAMMER2_CHAIN_SUBMODIFIED |
2715 HAMMER2_CHAIN_MODIFIED |
2716 HAMMER2_CHAIN_MODIFIED_AUX |
2717 HAMMER2_CHAIN_MOVED)) {
2718 hammer2_chain_parent_setsubmod(hmp, chain);
2722 * If the only thing left is a simple bref update try to
2723 * pro-actively update the parent, otherwise return early.
2725 parent = chain->parent;
2726 if (parent == NULL) {
2729 if (chain->bref.type != HAMMER2_BREF_TYPE_INODE ||
2730 (chain->flags & (HAMMER2_CHAIN_SUBMODIFIED |
2731 HAMMER2_CHAIN_MODIFIED |
2732 HAMMER2_CHAIN_MODIFIED_AUX |
2733 HAMMER2_CHAIN_MOVED)) != HAMMER2_CHAIN_MOVED) {
2738 * We are locking backwards so allow the lock to fail
2740 if (lockmgr(&parent->lk, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
2745 * We are updating brefs but we have to call chain_modify()
2746 * because our caller is not being run from a recursive flush.
2748 * This will also chain up the parent list and set the SUBMODIFIED
2751 * We do not want to set HAMMER2_CHAIN_MODIFY_TID here because the
2752 * modification is only related to updating a bref in the parent.
2754 * When updating the blockset embedded in the volume header we must
2755 * also update voldata.mirror_tid.
2757 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_MAYBE);
2758 hammer2_chain_modify(hmp, parent, HAMMER2_MODIFY_NO_MODIFY_TID);
2760 switch(parent->bref.type) {
2761 case HAMMER2_BREF_TYPE_INODE:
2762 base = &parent->data->ipdata.u.blockset.
2764 count = HAMMER2_SET_COUNT;
2766 case HAMMER2_BREF_TYPE_INDIRECT:
2767 base = &parent->data->npdata.blockref[0];
2768 count = parent->bytes /
2769 sizeof(hammer2_blockref_t);
2771 case HAMMER2_BREF_TYPE_VOLUME:
2772 base = &hmp->voldata.sroot_blockset.blockref[0];
2773 count = HAMMER2_SET_COUNT;
2774 if (chain->flags & HAMMER2_CHAIN_MOVED) {
2775 if (hmp->voldata.mirror_tid < chain->bref.mirror_tid) {
2776 hmp->voldata.mirror_tid =
2777 chain->bref.mirror_tid;
2783 panic("hammer2_chain_flush: "
2784 "unrecognized blockref type: %d",
2789 * Update the blockref in the parent. We do not have to set
2790 * MOVED in the parent because the parent has been marked modified,
2791 * so the flush sequence will pick up the bref change.
2793 * We do have to propagate mirror_tid upward.
2795 KKASSERT(chain->index >= 0 &&
2796 chain->index < count);
2797 KKASSERT(chain->parent == parent);
2798 if (chain->flags & HAMMER2_CHAIN_MOVED) {
2799 base[chain->index] = chain->bref_flush;
2800 if (parent->bref.mirror_tid < chain->bref_flush.mirror_tid)
2801 parent->bref.mirror_tid = chain->bref_flush.mirror_tid;
2802 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MOVED);
2803 hammer2_chain_drop(hmp, chain);
2804 } else if (bcmp(&base[chain->index], &chain->bref_flush,
2805 sizeof(chain->bref)) != 0) {
2806 panic("hammer2: unflagged bref update(2)");
2809 lockmgr(&parent->lk, LK_RELEASE); /* release manual lockmgr op */
2810 hammer2_chain_unlock(hmp, parent);