hammer2 - Allow simple 'mount @label <target>' shortcut for snapshots
[dragonfly.git] / sys / vfs / hammer2 / hammer2_chain.c
1 /*
2  * Copyright (c) 2011-2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * and Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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
17  *    distribution.
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.
21  *
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
33  * SUCH DAMAGE.
34  */
35 /*
36  * This subsystem implements most of the core support functions for
37  * the hammer2_chain structure.
38  *
39  * Chains are the in-memory version on media objects (volume header, inodes,
40  * indirect blocks, data blocks, etc).  Chains represent a portion of the
41  * HAMMER2 topology.
42  *
43  * Chains are no-longer delete-duplicated.  Instead, the original in-memory
44  * chain will be moved along with its block reference (e.g. for things like
45  * renames, hardlink operations, modifications, etc), and will be indexed
46  * on a secondary list for flush handling instead of propagating a flag
47  * upward to the root.
48  *
49  * Concurrent front-end operations can still run against backend flushes
50  * as long as they do not cross the current flush boundary.  An operation
51  * running above the current flush (in areas not yet flushed) can become
52  * part of the current flush while ano peration running below the current
53  * flush can become part of the next flush.
54  */
55 #include <sys/cdefs.h>
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/types.h>
59 #include <sys/lock.h>
60 #include <sys/kern_syscall.h>
61 #include <sys/uuid.h>
62
63 #include <crypto/sha2/sha2.h>
64
65 #include "hammer2.h"
66
67 static hammer2_chain_t *hammer2_chain_create_indirect(
68                 hammer2_chain_t *parent,
69                 hammer2_key_t key, int keybits,
70                 hammer2_tid_t mtid, int for_type, int *errorp);
71 static hammer2_io_t *hammer2_chain_drop_data(hammer2_chain_t *chain);
72 static hammer2_chain_t *hammer2_combined_find(
73                 hammer2_chain_t *parent,
74                 hammer2_blockref_t *base, int count,
75                 hammer2_key_t *key_nextp,
76                 hammer2_key_t key_beg, hammer2_key_t key_end,
77                 hammer2_blockref_t **bresp);
78
79 /*
80  * Basic RBTree for chains (core->rbtree and core->dbtree).  Chains cannot
81  * overlap in the RB trees.  Deleted chains are moved from rbtree to either
82  * dbtree or to dbq.
83  *
84  * Chains in delete-duplicate sequences can always iterate through core_entry
85  * to locate the live version of the chain.
86  */
87 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
88
89 int
90 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
91 {
92         hammer2_key_t c1_beg;
93         hammer2_key_t c1_end;
94         hammer2_key_t c2_beg;
95         hammer2_key_t c2_end;
96
97         /*
98          * Compare chains.  Overlaps are not supposed to happen and catch
99          * any software issues early we count overlaps as a match.
100          */
101         c1_beg = chain1->bref.key;
102         c1_end = c1_beg + ((hammer2_key_t)1 << chain1->bref.keybits) - 1;
103         c2_beg = chain2->bref.key;
104         c2_end = c2_beg + ((hammer2_key_t)1 << chain2->bref.keybits) - 1;
105
106         if (c1_end < c2_beg)    /* fully to the left */
107                 return(-1);
108         if (c1_beg > c2_end)    /* fully to the right */
109                 return(1);
110         return(0);              /* overlap (must not cross edge boundary) */
111 }
112
113 /*
114  * Assert that a chain has no media data associated with it.
115  */
116 static __inline void
117 hammer2_chain_assert_no_data(hammer2_chain_t *chain)
118 {
119         KKASSERT(chain->dio == NULL);
120         if (chain->bref.type != HAMMER2_BREF_TYPE_VOLUME &&
121             chain->bref.type != HAMMER2_BREF_TYPE_FREEMAP &&
122             chain->data) {
123                 panic("hammer2_assert_no_data: chain %p still has data", chain);
124         }
125 }
126
127 /*
128  * Make a chain visible to the flusher.  The flusher needs to be able to
129  * do flushes of subdirectory chains or single files so it does a top-down
130  * recursion using the ONFLUSH flag for the recursion.  It locates MODIFIED
131  * or UPDATE chains and flushes back up the chain to the volume root.
132  *
133  * This routine sets ONFLUSH upward until it hits the volume root.  For
134  * simplicity we ignore PFSROOT boundaries whos rules can be complex.
135  * Extra ONFLUSH flagging doesn't hurt the filesystem.
136  */
137 void
138 hammer2_chain_setflush(hammer2_chain_t *chain)
139 {
140         hammer2_chain_t *parent;
141
142         if ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
143                 hammer2_spin_sh(&chain->core.spin);
144                 while ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
145                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH);
146                         if ((parent = chain->parent) == NULL)
147                                 break;
148                         hammer2_spin_sh(&parent->core.spin);
149                         hammer2_spin_unsh(&chain->core.spin);
150                         chain = parent;
151                 }
152                 hammer2_spin_unsh(&chain->core.spin);
153         }
154 }
155
156 /*
157  * Allocate a new disconnected chain element representing the specified
158  * bref.  chain->refs is set to 1 and the passed bref is copied to
159  * chain->bref.  chain->bytes is derived from the bref.
160  *
161  * chain->pmp inherits pmp unless the chain is an inode (other than the
162  * super-root inode).
163  *
164  * NOTE: Returns a referenced but unlocked (because there is no core) chain.
165  */
166 hammer2_chain_t *
167 hammer2_chain_alloc(hammer2_dev_t *hmp, hammer2_pfs_t *pmp,
168                     hammer2_blockref_t *bref)
169 {
170         hammer2_chain_t *chain;
171         u_int bytes;
172
173         /*
174          * Special case - radix of 0 indicates a chain that does not
175          * need a data reference (context is completely embedded in the
176          * bref).
177          */
178         if ((int)(bref->data_off & HAMMER2_OFF_MASK_RADIX))
179                 bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
180         else
181                 bytes = 0;
182
183         atomic_add_long(&hammer2_chain_allocs, 1);
184
185         /*
186          * Construct the appropriate system structure.
187          */
188         switch(bref->type) {
189         case HAMMER2_BREF_TYPE_DIRENT:
190         case HAMMER2_BREF_TYPE_INODE:
191         case HAMMER2_BREF_TYPE_INDIRECT:
192         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
193         case HAMMER2_BREF_TYPE_DATA:
194         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
195                 /*
196                  * Chain's are really only associated with the hmp but we
197                  * maintain a pmp association for per-mount memory tracking
198                  * purposes.  The pmp can be NULL.
199                  */
200                 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
201                 break;
202         case HAMMER2_BREF_TYPE_VOLUME:
203         case HAMMER2_BREF_TYPE_FREEMAP:
204                 /*
205                  * Only hammer2_chain_bulksnap() calls this function with these
206                  * types.
207                  */
208                 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
209                 break;
210         default:
211                 chain = NULL;
212                 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
213                       bref->type);
214         }
215
216         /*
217          * Initialize the new chain structure.  pmp must be set to NULL for
218          * chains belonging to the super-root topology of a device mount.
219          */
220         if (pmp == hmp->spmp)
221                 chain->pmp = NULL;
222         else
223                 chain->pmp = pmp;
224         chain->hmp = hmp;
225         chain->bref = *bref;
226         chain->bytes = bytes;
227         chain->refs = 1;
228         chain->flags = HAMMER2_CHAIN_ALLOCATED;
229
230         /*
231          * Set the PFS boundary flag if this chain represents a PFS root.
232          */
233         if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
234                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_PFSBOUNDARY);
235         hammer2_chain_core_init(chain);
236
237         return (chain);
238 }
239
240 /*
241  * Initialize a chain's core structure.  This structure used to be allocated
242  * but is now embedded.
243  *
244  * The core is not locked.  No additional refs on the chain are made.
245  * (trans) must not be NULL if (core) is not NULL.
246  */
247 void
248 hammer2_chain_core_init(hammer2_chain_t *chain)
249 {
250         /*
251          * Fresh core under nchain (no multi-homing of ochain's
252          * sub-tree).
253          */
254         RB_INIT(&chain->core.rbtree);   /* live chains */
255         hammer2_mtx_init(&chain->lock, "h2chain");
256 }
257
258 /*
259  * Add a reference to a chain element, preventing its destruction.
260  *
261  * (can be called with spinlock held)
262  */
263 void
264 hammer2_chain_ref(hammer2_chain_t *chain)
265 {
266         if (atomic_fetchadd_int(&chain->refs, 1) == 0) {
267                 /*
268                  * 0->non-zero transition must ensure that chain is removed
269                  * from the LRU list.
270                  *
271                  * NOTE: Already holding lru_spin here so we cannot call
272                  *       hammer2_chain_ref() to get it off lru_list, do
273                  *       it manually.
274                  */
275                 if (chain->flags & HAMMER2_CHAIN_ONLRU) {
276                         hammer2_pfs_t *pmp = chain->pmp;
277                         hammer2_spin_ex(&pmp->lru_spin);
278                         if (chain->flags & HAMMER2_CHAIN_ONLRU) {
279                                 atomic_add_int(&pmp->lru_count, -1);
280                                 atomic_clear_int(&chain->flags,
281                                                  HAMMER2_CHAIN_ONLRU);
282                                 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
283                         }
284                         hammer2_spin_unex(&pmp->lru_spin);
285                 }
286         }
287 #if 0
288         kprintf("REFC %p %d %08x\n", chain, chain->refs - 1, chain->flags);
289         print_backtrace(8);
290 #endif
291 }
292
293 /*
294  * Ref a locked chain and force the data to be held across an unlock.
295  * Chain must be currently locked.  The user of the chain who desires
296  * to release the hold must call hammer2_chain_lock_unhold() to relock
297  * and unhold the chain, then unlock normally, or may simply call
298  * hammer2_chain_drop_unhold() (which is safer against deadlocks).
299  */
300 void
301 hammer2_chain_ref_hold(hammer2_chain_t *chain)
302 {
303         atomic_add_int(&chain->lockcnt, 1);
304         hammer2_chain_ref(chain);
305 }
306
307 /*
308  * Insert the chain in the core rbtree.
309  *
310  * Normal insertions are placed in the live rbtree.  Insertion of a deleted
311  * chain is a special case used by the flush code that is placed on the
312  * unstaged deleted list to avoid confusing the live view.
313  */
314 #define HAMMER2_CHAIN_INSERT_SPIN       0x0001
315 #define HAMMER2_CHAIN_INSERT_LIVE       0x0002
316 #define HAMMER2_CHAIN_INSERT_RACE       0x0004
317
318 static
319 int
320 hammer2_chain_insert(hammer2_chain_t *parent, hammer2_chain_t *chain,
321                      int flags, int generation)
322 {
323         hammer2_chain_t *xchain;
324         int error = 0;
325
326         if (flags & HAMMER2_CHAIN_INSERT_SPIN)
327                 hammer2_spin_ex(&parent->core.spin);
328
329         /*
330          * Interlocked by spinlock, check for race
331          */
332         if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
333             parent->core.generation != generation) {
334                 error = HAMMER2_ERROR_EAGAIN;
335                 goto failed;
336         }
337
338         /*
339          * Insert chain
340          */
341         xchain = RB_INSERT(hammer2_chain_tree, &parent->core.rbtree, chain);
342         KASSERT(xchain == NULL,
343                 ("hammer2_chain_insert: collision %p %p (key=%016jx)",
344                 chain, xchain, chain->bref.key));
345         atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
346         chain->parent = parent;
347         ++parent->core.chain_count;
348         ++parent->core.generation;      /* XXX incs for _get() too, XXX */
349
350         /*
351          * We have to keep track of the effective live-view blockref count
352          * so the create code knows when to push an indirect block.
353          */
354         if (flags & HAMMER2_CHAIN_INSERT_LIVE)
355                 atomic_add_int(&parent->core.live_count, 1);
356 failed:
357         if (flags & HAMMER2_CHAIN_INSERT_SPIN)
358                 hammer2_spin_unex(&parent->core.spin);
359         return error;
360 }
361
362 /*
363  * Drop the caller's reference to the chain.  When the ref count drops to
364  * zero this function will try to disassociate the chain from its parent and
365  * deallocate it, then recursely drop the parent using the implied ref
366  * from the chain's chain->parent.
367  *
368  * Nobody should own chain's mutex on the 1->0 transition, unless this drop
369  * races an acquisition by another cpu.  Therefore we can loop if we are
370  * unable to acquire the mutex, and refs is unlikely to be 1 unless we again
371  * race against another drop.
372  */
373 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain);
374
375 void
376 hammer2_chain_drop(hammer2_chain_t *chain)
377 {
378         u_int refs;
379
380         if (hammer2_debug & 0x200000)
381                 Debugger("drop");
382 #if 0
383         kprintf("DROP %p %d %08x\n", chain, chain->refs - 1, chain->flags);
384         print_backtrace(8);
385 #endif
386
387         KKASSERT(chain->refs > 0);
388
389         while (chain) {
390                 refs = chain->refs;
391                 cpu_ccfence();
392                 KKASSERT(refs > 0);
393
394                 if (refs == 1) {
395                         if (mtx_lock_ex_try(&chain->lock) == 0)
396                                 chain = hammer2_chain_lastdrop(chain);
397                         /* retry the same chain */
398                 } else {
399                         if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
400                                 break;
401                         /* retry the same chain */
402                 }
403                 cpu_pause();
404         }
405 }
406
407 /*
408  * Unhold a held and probably not-locked chain, ensure that the data is
409  * dropped on the 1->0 transition of lockcnt by obtaining an exclusive
410  * lock and then simply unlocking the chain.
411  */
412 void
413 hammer2_chain_drop_unhold(hammer2_chain_t *chain)
414 {
415         u_int lockcnt;
416         int iter = 0;
417
418         for (;;) {
419                 lockcnt = chain->lockcnt;
420                 cpu_ccfence();
421                 if (lockcnt > 1) {
422                         if (atomic_cmpset_int(&chain->lockcnt,
423                                               lockcnt, lockcnt - 1)) {
424                                 break;
425                         }
426                 } else if (mtx_lock_ex_try(&chain->lock) == 0) {
427                         hammer2_chain_unlock(chain);
428                         break;
429                 } else {
430                         /*
431                          * This situation can easily occur on SMP due to
432                          * the gap inbetween the 1->0 transition and the
433                          * final unlock.  We cannot safely block on the
434                          * mutex because lockcnt might go above 1.
435                          *
436                          * XXX Sleep for one tick if it takes too long.
437                          */
438                         if (++iter > 1000) {
439                                 if (iter > 1000 + hz) {
440                                         kprintf("hammer2: h2race1 %p\n", chain);
441                                         iter = 1000;
442                                 }
443                                 tsleep(&iter, 0, "h2race1", 1);
444                         }
445                         cpu_pause();
446                 }
447         }
448         hammer2_chain_drop(chain);
449 }
450
451 /*
452  * Handles the (potential) last drop of chain->refs from 1->0.  Called with
453  * the mutex exclusively locked, refs == 1, and lockcnt 0.  SMP races are
454  * possible against refs and lockcnt.  We must dispose of the mutex on chain.
455  *
456  * This function returns an unlocked chain for recursive drop or NULL.  It
457  * can return the same chain if it determines it has raced another ref.
458  *
459  * --
460  *
461  * When two chains need to be recursively dropped we use the chain we
462  * would otherwise free to placehold the additional chain.  It's a bit
463  * convoluted but we can't just recurse without potentially blowing out
464  * the kernel stack.
465  *
466  * The chain cannot be freed if it has any children.
467  * The chain cannot be freed if flagged MODIFIED unless we can dispose of it.
468  * The chain cannot be freed if flagged UPDATE unless we can dispose of it.
469  * Any dedup registration can remain intact.
470  *
471  * The core spinlock is allowed to nest child-to-parent (not parent-to-child).
472  */
473 static
474 hammer2_chain_t *
475 hammer2_chain_lastdrop(hammer2_chain_t *chain)
476 {
477         hammer2_pfs_t *pmp;
478         hammer2_dev_t *hmp;
479         hammer2_chain_t *parent;
480         hammer2_chain_t *rdrop;
481 #if 0
482         hammer2_io_t *dio;
483 #endif
484
485 #if 0
486         /*
487          * On last drop if there is no parent and data_off is good (at
488          * least does not represent the volume root), the modified chain
489          * is probably going to be destroyed.  We have to make sure that
490          * the data area is not registered for dedup.
491          *
492          * XXX removed. In fact, we do not have to make sure that the
493          *     data area is not registered for dedup.  The data area
494          *     can, in fact, still be used for dedup because it is
495          *     still allocated in the freemap and the underlying I/O
496          *     will still be flushed.
497          */
498         if (chain->parent == NULL &&
499             (chain->flags & HAMMER2_CHAIN_MODIFIED) &&
500             (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
501                 hmp = chain->hmp;
502                 hammer2_io_dedup_delete(hmp, chain->bref.type,
503                                         chain->bref.data_off, chain->bytes);
504         }
505 #endif
506         /*
507          * We need chain's spinlock to interlock the sub-tree test.
508          * We already have chain's mutex, protecting chain->parent.
509          *
510          * Remember that chain->refs can be in flux.
511          */
512         hammer2_spin_ex(&chain->core.spin);
513
514         if ((parent = chain->parent) != NULL) {
515                 /*
516                  * If the chain has a parent the UPDATE bit prevents scrapping
517                  * as the chain is needed to properly flush the parent.  Try
518                  * to complete the 1->0 transition and return NULL.  Retry
519                  * (return chain) if we are unable to complete the 1->0
520                  * transition, else return NULL (nothing more to do).
521                  *
522                  * If the chain has a parent the MODIFIED bit prevents
523                  * scrapping.
524                  *
525                  * Chains with UPDATE/MODIFIED are *not* put on the LRU list!
526                  */
527                 if (chain->flags & (HAMMER2_CHAIN_UPDATE |
528                                     HAMMER2_CHAIN_MODIFIED)) {
529                         if (atomic_cmpset_int(&chain->refs, 1, 0)) {
530                                 hammer2_spin_unex(&chain->core.spin);
531 #if 0
532                                 dio = hammer2_chain_drop_data(chain, 0);
533                                 if (dio)
534                                         hammer2_io_bqrelse(&dio);
535 #endif
536                                 hammer2_chain_assert_no_data(chain);
537                                 hammer2_mtx_unlock(&chain->lock);
538                                 chain = NULL;
539                         } else {
540                                 hammer2_spin_unex(&chain->core.spin);
541                                 hammer2_mtx_unlock(&chain->lock);
542                         }
543                         return (chain);
544                 }
545                 /* spinlock still held */
546         } else {
547                 /*
548                  * The chain has no parent and can be flagged for destruction.
549                  * Since it has no parent, UPDATE can also be cleared.
550                  */
551                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
552                 if (chain->flags & HAMMER2_CHAIN_UPDATE)
553                         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
554
555                 /*
556                  * If the chain has children we must still flush the chain.
557                  * Any dedup is already handled by the underlying DIO, so
558                  * we do not have to specifically flush it here.
559                  *
560                  * In the case where it has children, the DESTROY flag test
561                  * in the flush code will prevent unnecessary flushes of
562                  * MODIFIED chains that are not flagged DEDUP so don't worry
563                  * about that here.
564                  */
565                 if (chain->core.chain_count) {
566                         /*
567                          * Put on flushq (should ensure refs > 1), retry
568                          * the drop.
569                          */
570                         hammer2_spin_unex(&chain->core.spin);
571                         hammer2_delayed_flush(chain);
572                         hammer2_mtx_unlock(&chain->lock);
573
574                         return(chain);  /* retry drop */
575                 }
576
577                 /*
578                  * Otherwise we can scrap the MODIFIED bit if it is set,
579                  * and continue along the freeing path.
580                  *
581                  * Be sure to clean-out any dedup bits.  Without a parent
582                  * this chain will no longer be visible to the flush code.
583                  * Easy check data_off to avoid the volume root.
584                  */
585                 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
586                         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
587                         atomic_add_long(&hammer2_count_modified_chains, -1);
588                         if (chain->pmp)
589                                 hammer2_pfs_memory_wakeup(chain->pmp);
590                 }
591                 /* spinlock still held */
592         }
593
594         /* spinlock still held */
595 #if 0
596         dio = NULL;
597 #endif
598
599         /*
600          * If any children exist we must leave the chain intact with refs == 0.
601          * They exist because chains are retained below us which have refs or
602          * may require flushing.
603          *
604          * Retry (return chain) if we fail to transition the refs to 0, else
605          * return NULL indication nothing more to do.
606          *
607          * Chains with children are NOT put on the LRU list.
608          */
609         if (chain->core.chain_count) {
610                 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
611                         hammer2_spin_unex(&chain->core.spin);
612                         hammer2_chain_assert_no_data(chain);
613                         hammer2_mtx_unlock(&chain->lock);
614                         chain = NULL;
615                 } else {
616                         hammer2_spin_unex(&chain->core.spin);
617                         hammer2_mtx_unlock(&chain->lock);
618                 }
619                 return (chain);
620         }
621         /* spinlock still held */
622         /* no chains left under us */
623
624         /*
625          * chain->core has no children left so no accessors can get to our
626          * chain from there.  Now we have to lock the parent core to interlock
627          * remaining possible accessors that might bump chain's refs before
628          * we can safely drop chain's refs with intent to free the chain.
629          */
630         hmp = chain->hmp;
631         pmp = chain->pmp;       /* can be NULL */
632         rdrop = NULL;
633
634         parent = chain->parent;
635
636         /*
637          * WARNING! chain's spin lock is still held here, and other spinlocks
638          *          will be acquired and released in the code below.  We
639          *          cannot be making fancy procedure calls!
640          */
641
642         /*
643          * We can cache the chain if it is associated with a pmp
644          * and not flagged as being destroyed or requesting a full
645          * release.  In this situation the chain is not removed
646          * from its parent, i.e. it can still be looked up.
647          *
648          * We intentionally do not cache DATA chains because these
649          * were likely used to load data into the logical buffer cache
650          * and will not be accessed again for some time.
651          */
652         if ((chain->flags &
653              (HAMMER2_CHAIN_DESTROY | HAMMER2_CHAIN_RELEASE)) == 0 &&
654             chain->pmp &&
655             chain->bref.type != HAMMER2_BREF_TYPE_DATA) {
656                 if (parent)
657                         hammer2_spin_ex(&parent->core.spin);
658                 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
659                         /*
660                          * 1->0 transition failed, retry.  Do not drop
661                          * the chain's data yet!
662                          */
663                         if (parent)
664                                 hammer2_spin_unex(&parent->core.spin);
665                         hammer2_spin_unex(&chain->core.spin);
666                         hammer2_mtx_unlock(&chain->lock);
667
668                         return(chain);
669                 }
670
671                 /*
672                  * Success
673                  */
674 #if 0
675                 dio = hammer2_chain_drop_data(chain, 1);
676 #endif
677                 hammer2_chain_assert_no_data(chain);
678
679                 KKASSERT((chain->flags & HAMMER2_CHAIN_ONLRU) == 0);
680                 hammer2_spin_ex(&pmp->lru_spin);
681                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
682                 TAILQ_INSERT_TAIL(&pmp->lru_list, chain, lru_node);
683
684                 /*
685                  * If we are over the LRU limit we need to drop something.
686                  */
687                 if (pmp->lru_count > HAMMER2_LRU_LIMIT) {
688                         rdrop = TAILQ_FIRST(&pmp->lru_list);
689                         atomic_clear_int(&rdrop->flags, HAMMER2_CHAIN_ONLRU);
690                         TAILQ_REMOVE(&pmp->lru_list, rdrop, lru_node);
691                         atomic_add_int(&rdrop->refs, 1);
692                         atomic_set_int(&rdrop->flags, HAMMER2_CHAIN_RELEASE);
693                 } else {
694                         atomic_add_int(&pmp->lru_count, 1);
695                 }
696                 hammer2_spin_unex(&pmp->lru_spin);
697                 if (parent) {
698                         hammer2_spin_unex(&parent->core.spin);
699                         parent = NULL;  /* safety */
700                 }
701                 hammer2_spin_unex(&chain->core.spin);
702                 hammer2_mtx_unlock(&chain->lock);
703 #if 0
704                 if (dio)
705                         hammer2_io_bqrelse(&dio);
706 #endif
707
708                 return rdrop;
709                 /* NOT REACHED */
710         }
711
712         /*
713          * Spinlock the parent and try to drop the last ref on chain.
714          * On success determine if we should dispose of the chain
715          * (remove the chain from its parent, etc).
716          *
717          * (normal core locks are top-down recursive but we define
718          * core spinlocks as bottom-up recursive, so this is safe).
719          */
720         if (parent) {
721                 hammer2_spin_ex(&parent->core.spin);
722                 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
723 #if 0
724                         /* XXX remove, don't try to drop data on fail */
725                         hammer2_spin_unex(&parent->core.spin);
726                         dio = hammer2_chain_drop_data(chain, 0);
727                         hammer2_spin_unex(&chain->core.spin);
728                         if (dio)
729                                 hammer2_io_bqrelse(&dio);
730 #endif
731                         /*
732                          * 1->0 transition failed, retry.
733                          */
734                         hammer2_spin_unex(&parent->core.spin);
735                         hammer2_spin_unex(&chain->core.spin);
736                         hammer2_mtx_unlock(&chain->lock);
737
738                         return(chain);
739                 }
740
741                 /*
742                  * 1->0 transition successful, parent spin held to prevent
743                  * new lookups, chain spinlock held to protect parent field.
744                  * Remove chain from the parent.
745                  *
746                  * If the chain is being removed from the parent's btree but
747                  * is not bmapped, we have to adjust live_count downward.  If
748                  * it is bmapped then the blockref is retained in the parent
749                  * as is its associated live_count.  This case can occur when
750                  * a chain added to the topology is unable to flush and is
751                  * then later deleted.
752                  */
753                 if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
754                         if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) &&
755                             (chain->flags & HAMMER2_CHAIN_BMAPPED) == 0) {
756                                 atomic_add_int(&parent->core.live_count, -1);
757                         }
758                         RB_REMOVE(hammer2_chain_tree,
759                                   &parent->core.rbtree, chain);
760                         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
761                         --parent->core.chain_count;
762                         chain->parent = NULL;
763                 }
764
765                 /*
766                  * If our chain was the last chain in the parent's core the
767                  * core is now empty and its parent might have to be
768                  * re-dropped if it has 0 refs.
769                  */
770                 if (parent->core.chain_count == 0) {
771                         rdrop = parent;
772                         atomic_add_int(&rdrop->refs, 1);
773                         /*
774                         if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0)
775                                 rdrop = NULL;
776                         */
777                 }
778                 hammer2_spin_unex(&parent->core.spin);
779                 parent = NULL;  /* safety */
780                 /* FALL THROUGH */
781         } else {
782                 /*
783                  * No-parent case.
784                  */
785                 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
786                         /*
787                          * 1->0 transition failed, retry.
788                          */
789                         hammer2_spin_unex(&parent->core.spin);
790                         hammer2_spin_unex(&chain->core.spin);
791                         hammer2_mtx_unlock(&chain->lock);
792
793                         return(chain);
794                 }
795         }
796
797         /*
798          * Successful 1->0 transition, no parent, no children... no way for
799          * anyone to ref this chain any more.  We can clean-up and free it.
800          *
801          * We still have the core spinlock, and core's chain_count is 0.
802          * Any parent spinlock is gone.
803          */
804         hammer2_spin_unex(&chain->core.spin);
805         hammer2_chain_assert_no_data(chain);
806         hammer2_mtx_unlock(&chain->lock);
807         KKASSERT(RB_EMPTY(&chain->core.rbtree) &&
808                  chain->core.chain_count == 0);
809
810         /*
811          * All locks are gone, no pointers remain to the chain, finish
812          * freeing it.
813          */
814         KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE |
815                                   HAMMER2_CHAIN_MODIFIED)) == 0);
816 #if 0
817         dio = hammer2_chain_drop_data(chain, 1);
818         if (dio)
819                 hammer2_io_bqrelse(&dio);
820 #endif
821
822         /*
823          * Once chain resources are gone we can use the now dead chain
824          * structure to placehold what might otherwise require a recursive
825          * drop, because we have potentially two things to drop and can only
826          * return one directly.
827          */
828         if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
829                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ALLOCATED);
830                 chain->hmp = NULL;
831                 kfree(chain, hmp->mchain);
832         }
833
834         /*
835          * Possible chaining loop when parent re-drop needed.
836          */
837         return(rdrop);
838 }
839
840 /*
841  * On last lock release.
842  */
843 static hammer2_io_t *
844 hammer2_chain_drop_data(hammer2_chain_t *chain)
845 {
846         hammer2_io_t *dio;
847
848         if ((dio = chain->dio) != NULL) {
849                 chain->dio = NULL;
850                 chain->data = NULL;
851         } else {
852                 switch(chain->bref.type) {
853                 case HAMMER2_BREF_TYPE_VOLUME:
854                 case HAMMER2_BREF_TYPE_FREEMAP:
855                         break;
856                 default:
857                         if (chain->data != NULL) {
858                                 hammer2_spin_unex(&chain->core.spin);
859                                 panic("chain data not null: "
860                                       "chain %p bref %016jx.%02x "
861                                       "refs %d parent %p dio %p data %p",
862                                       chain, chain->bref.data_off,
863                                       chain->bref.type, chain->refs,
864                                       chain->parent,
865                                       chain->dio, chain->data);
866                         }
867                         KKASSERT(chain->data == NULL);
868                         break;
869                 }
870         }
871         return dio;
872 }
873
874 /*
875  * Lock a referenced chain element, acquiring its data with I/O if necessary,
876  * and specify how you would like the data to be resolved.
877  *
878  * If an I/O or other fatal error occurs, chain->error will be set to non-zero.
879  *
880  * The lock is allowed to recurse, multiple locking ops will aggregate
881  * the requested resolve types.  Once data is assigned it will not be
882  * removed until the last unlock.
883  *
884  * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
885  *                         (typically used to avoid device/logical buffer
886  *                          aliasing for data)
887  *
888  * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
889  *                         the INITIAL-create state (indirect blocks only).
890  *
891  *                         Do not resolve data elements for DATA chains.
892  *                         (typically used to avoid device/logical buffer
893  *                          aliasing for data)
894  *
895  * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
896  *
897  * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
898  *                         it will be locked exclusive.
899  *
900  * NOTE: Embedded elements (volume header, inodes) are always resolved
901  *       regardless.
902  *
903  * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
904  *       element will instantiate and zero its buffer, and flush it on
905  *       release.
906  *
907  * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
908  *       so as not to instantiate a device buffer, which could alias against
909  *       a logical file buffer.  However, if ALWAYS is specified the
910  *       device buffer will be instantiated anyway.
911  *
912  * WARNING! This function blocks on I/O if data needs to be fetched.  This
913  *          blocking can run concurrent with other compatible lock holders
914  *          who do not need data returning.  The lock is not upgraded to
915  *          exclusive during a data fetch, a separate bit is used to
916  *          interlock I/O.  However, an exclusive lock holder can still count
917  *          on being interlocked against an I/O fetch managed by a shared
918  *          lock holder.
919  */
920 void
921 hammer2_chain_lock(hammer2_chain_t *chain, int how)
922 {
923         /*
924          * Ref and lock the element.  Recursive locks are allowed.
925          */
926         KKASSERT(chain->refs > 0);
927         atomic_add_int(&chain->lockcnt, 1);
928
929         /*
930          * Get the appropriate lock.  If LOCKAGAIN is flagged with SHARED
931          * the caller expects a shared lock to already be present and we
932          * are giving it another ref.  This case must importantly not block
933          * if there is a pending exclusive lock request.
934          */
935         if (how & HAMMER2_RESOLVE_SHARED) {
936                 if (how & HAMMER2_RESOLVE_LOCKAGAIN) {
937                         hammer2_mtx_sh_again(&chain->lock);
938                 } else {
939                         hammer2_mtx_sh(&chain->lock);
940                 }
941         } else {
942                 hammer2_mtx_ex(&chain->lock);
943         }
944         ++curthread->td_tracker;
945
946         /*
947          * If we already have a valid data pointer no further action is
948          * necessary.
949          */
950         if (chain->data)
951                 return;
952
953         /*
954          * Do we have to resolve the data?  This is generally only
955          * applicable to HAMMER2_BREF_TYPE_DATA which is special-cased.
956          * Other BREF types expects the data to be there.
957          */
958         switch(how & HAMMER2_RESOLVE_MASK) {
959         case HAMMER2_RESOLVE_NEVER:
960                 return;
961         case HAMMER2_RESOLVE_MAYBE:
962                 if (chain->flags & HAMMER2_CHAIN_INITIAL)
963                         return;
964                 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
965                         return;
966 #if 0
967                 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
968                         return;
969                 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
970                         return;
971 #endif
972                 /* fall through */
973         case HAMMER2_RESOLVE_ALWAYS:
974         default:
975                 break;
976         }
977
978         /*
979          * Caller requires data
980          */
981         hammer2_chain_load_data(chain);
982 }
983
984 /*
985  * Lock the chain, retain the hold, and drop the data persistence count.
986  * The data should remain valid because we never transitioned lockcnt
987  * through 0.
988  */
989 void
990 hammer2_chain_lock_unhold(hammer2_chain_t *chain, int how)
991 {
992         hammer2_chain_lock(chain, how);
993         atomic_add_int(&chain->lockcnt, -1);
994 }
995
996 #if 0
997 /*
998  * Downgrade an exclusive chain lock to a shared chain lock.
999  *
1000  * NOTE: There is no upgrade equivalent due to the ease of
1001  *       deadlocks in that direction.
1002  */
1003 void
1004 hammer2_chain_lock_downgrade(hammer2_chain_t *chain)
1005 {
1006         hammer2_mtx_downgrade(&chain->lock);
1007 }
1008 #endif
1009
1010 #if 0
1011 /*
1012  * Obtains a second shared lock on the chain, does not account the second
1013  * shared lock as being owned by the current thread.
1014  *
1015  * Caller must already own a shared lock on this chain.
1016  *
1017  * The lock function is required to obtain the second shared lock without
1018  * blocking on pending exclusive requests.
1019  */
1020 void
1021 hammer2_chain_push_shared_lock(hammer2_chain_t *chain)
1022 {
1023         hammer2_mtx_sh_again(&chain->lock);
1024         atomic_add_int(&chain->lockcnt, 1);
1025         /* do not count in td_tracker for this thread */
1026 }
1027
1028 /*
1029  * Accounts for a shared lock that was pushed to us as being owned by our
1030  * thread.
1031  */
1032 void
1033 hammer2_chain_pull_shared_lock(hammer2_chain_t *chain)
1034 {
1035         ++curthread->td_tracker;
1036 }
1037 #endif
1038
1039 /*
1040  * Issue I/O and install chain->data.  Caller must hold a chain lock, lock
1041  * may be of any type.
1042  *
1043  * Once chain->data is set it cannot be disposed of until all locks are
1044  * released.
1045  */
1046 void
1047 hammer2_chain_load_data(hammer2_chain_t *chain)
1048 {
1049         hammer2_blockref_t *bref;
1050         hammer2_dev_t *hmp;
1051         hammer2_io_t *dio;
1052         char *bdata;
1053         int error;
1054
1055         /*
1056          * Degenerate case, data already present, or chain has no media
1057          * reference to load.
1058          */
1059         if (chain->data)
1060                 return;
1061         if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0)
1062                 return;
1063
1064         hmp = chain->hmp;
1065         KKASSERT(hmp != NULL);
1066
1067         /*
1068          * Gain the IOINPROG bit, interlocked block.
1069          */
1070         for (;;) {
1071                 u_int oflags;
1072                 u_int nflags;
1073
1074                 oflags = chain->flags;
1075                 cpu_ccfence();
1076                 if (oflags & HAMMER2_CHAIN_IOINPROG) {
1077                         nflags = oflags | HAMMER2_CHAIN_IOSIGNAL;
1078                         tsleep_interlock(&chain->flags, 0);
1079                         if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1080                                 tsleep(&chain->flags, PINTERLOCKED,
1081                                         "h2iocw", 0);
1082                         }
1083                         /* retry */
1084                 } else {
1085                         nflags = oflags | HAMMER2_CHAIN_IOINPROG;
1086                         if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1087                                 break;
1088                         }
1089                         /* retry */
1090                 }
1091         }
1092
1093         /*
1094          * We own CHAIN_IOINPROG
1095          *
1096          * Degenerate case if we raced another load.
1097          */
1098         if (chain->data)
1099                 goto done;
1100
1101         /*
1102          * We must resolve to a device buffer, either by issuing I/O or
1103          * by creating a zero-fill element.  We do not mark the buffer
1104          * dirty when creating a zero-fill element (the hammer2_chain_modify()
1105          * API must still be used to do that).
1106          *
1107          * The device buffer is variable-sized in powers of 2 down
1108          * to HAMMER2_MIN_ALLOC (typically 1K).  A 64K physical storage
1109          * chunk always contains buffers of the same size. (XXX)
1110          *
1111          * The minimum physical IO size may be larger than the variable
1112          * block size.
1113          */
1114         bref = &chain->bref;
1115
1116         /*
1117          * The getblk() optimization can only be used on newly created
1118          * elements if the physical block size matches the request.
1119          */
1120         if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1121                 error = hammer2_io_new(hmp, bref->type,
1122                                        bref->data_off, chain->bytes,
1123                                        &chain->dio);
1124         } else {
1125                 error = hammer2_io_bread(hmp, bref->type,
1126                                          bref->data_off, chain->bytes,
1127                                          &chain->dio);
1128                 hammer2_adjreadcounter(&chain->bref, chain->bytes);
1129         }
1130         if (error) {
1131                 chain->error = HAMMER2_ERROR_EIO;
1132                 kprintf("hammer2_chain_lock: I/O error %016jx: %d\n",
1133                         (intmax_t)bref->data_off, error);
1134                 hammer2_io_bqrelse(&chain->dio);
1135                 goto done;
1136         }
1137         chain->error = 0;
1138
1139         /*
1140          * This isn't perfect and can be ignored on OSs which do not have
1141          * an indication as to whether a buffer is coming from cache or
1142          * if I/O was actually issued for the read.  TESTEDGOOD will work
1143          * pretty well without the B_IOISSUED logic because chains are
1144          * cached, but in that situation (without B_IOISSUED) it will not
1145          * detect whether a re-read via I/O is corrupted verses the original
1146          * read.
1147          *
1148          * We can't re-run the CRC on every fresh lock.  That would be
1149          * insanely expensive.
1150          *
1151          * If the underlying kernel buffer covers the entire chain we can
1152          * use the B_IOISSUED indication to determine if we have to re-run
1153          * the CRC on chain data for chains that managed to stay cached
1154          * across the kernel disposal of the original buffer.
1155          */
1156         if ((dio = chain->dio) != NULL && dio->bp) {
1157                 struct buf *bp = dio->bp;
1158
1159                 if (dio->psize == chain->bytes &&
1160                     (bp->b_flags & B_IOISSUED)) {
1161                         atomic_clear_int(&chain->flags,
1162                                          HAMMER2_CHAIN_TESTEDGOOD);
1163                         bp->b_flags &= ~B_IOISSUED;
1164                 }
1165         }
1166
1167         /*
1168          * NOTE: A locked chain's data cannot be modified without first
1169          *       calling hammer2_chain_modify().
1170          */
1171
1172         /*
1173          * Clear INITIAL.  In this case we used io_new() and the buffer has
1174          * been zero'd and marked dirty.
1175          */
1176         bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
1177
1178         if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1179                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1180                 chain->bref.flags |= HAMMER2_BREF_FLAG_ZERO;
1181         } else if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
1182                 /*
1183                  * check data not currently synchronized due to
1184                  * modification.  XXX assumes data stays in the buffer
1185                  * cache, which might not be true (need biodep on flush
1186                  * to calculate crc?  or simple crc?).
1187                  */
1188         } else if ((chain->flags & HAMMER2_CHAIN_TESTEDGOOD) == 0) {
1189                 if (hammer2_chain_testcheck(chain, bdata) == 0) {
1190                         chain->error = HAMMER2_ERROR_CHECK;
1191                 } else {
1192                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_TESTEDGOOD);
1193                 }
1194         }
1195
1196         /*
1197          * Setup the data pointer, either pointing it to an embedded data
1198          * structure and copying the data from the buffer, or pointing it
1199          * into the buffer.
1200          *
1201          * The buffer is not retained when copying to an embedded data
1202          * structure in order to avoid potential deadlocks or recursions
1203          * on the same physical buffer.
1204          *
1205          * WARNING! Other threads can start using the data the instant we
1206          *          set chain->data non-NULL.
1207          */
1208         switch (bref->type) {
1209         case HAMMER2_BREF_TYPE_VOLUME:
1210         case HAMMER2_BREF_TYPE_FREEMAP:
1211                 /*
1212                  * Copy data from bp to embedded buffer
1213                  */
1214                 panic("hammer2_chain_load_data: unresolved volume header");
1215                 break;
1216         case HAMMER2_BREF_TYPE_DIRENT:
1217                 KKASSERT(chain->bytes != 0);
1218                 /* fall through */
1219         case HAMMER2_BREF_TYPE_INODE:
1220         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1221         case HAMMER2_BREF_TYPE_INDIRECT:
1222         case HAMMER2_BREF_TYPE_DATA:
1223         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1224         default:
1225                 /*
1226                  * Point data at the device buffer and leave dio intact.
1227                  */
1228                 chain->data = (void *)bdata;
1229                 break;
1230         }
1231
1232         /*
1233          * Release HAMMER2_CHAIN_IOINPROG and signal waiters if requested.
1234          */
1235 done:
1236         for (;;) {
1237                 u_int oflags;
1238                 u_int nflags;
1239
1240                 oflags = chain->flags;
1241                 nflags = oflags & ~(HAMMER2_CHAIN_IOINPROG |
1242                                     HAMMER2_CHAIN_IOSIGNAL);
1243                 KKASSERT(oflags & HAMMER2_CHAIN_IOINPROG);
1244                 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
1245                         if (oflags & HAMMER2_CHAIN_IOSIGNAL)
1246                                 wakeup(&chain->flags);
1247                         break;
1248                 }
1249         }
1250 }
1251
1252 /*
1253  * Unlock and deref a chain element.
1254  *
1255  * Remember that the presence of children under chain prevent the chain's
1256  * destruction but do not add additional references, so the dio will still
1257  * be dropped.
1258  */
1259 void
1260 hammer2_chain_unlock(hammer2_chain_t *chain)
1261 {
1262         hammer2_io_t *dio;
1263         u_int lockcnt;
1264         int iter = 0;
1265
1266         --curthread->td_tracker;
1267
1268         /*
1269          * If multiple locks are present (or being attempted) on this
1270          * particular chain we can just unlock, drop refs, and return.
1271          *
1272          * Otherwise fall-through on the 1->0 transition.
1273          */
1274         for (;;) {
1275                 lockcnt = chain->lockcnt;
1276                 KKASSERT(lockcnt > 0);
1277                 cpu_ccfence();
1278                 if (lockcnt > 1) {
1279                         if (atomic_cmpset_int(&chain->lockcnt,
1280                                               lockcnt, lockcnt - 1)) {
1281                                 hammer2_mtx_unlock(&chain->lock);
1282                                 return;
1283                         }
1284                 } else if (hammer2_mtx_upgrade_try(&chain->lock) == 0) {
1285                         /* while holding the mutex exclusively */
1286                         if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
1287                                 break;
1288                 } else {
1289                         /*
1290                          * This situation can easily occur on SMP due to
1291                          * the gap inbetween the 1->0 transition and the
1292                          * final unlock.  We cannot safely block on the
1293                          * mutex because lockcnt might go above 1.
1294                          *
1295                          * XXX Sleep for one tick if it takes too long.
1296                          */
1297                         if (++iter > 1000) {
1298                                 if (iter > 1000 + hz) {
1299                                         kprintf("hammer2: h2race2 %p\n", chain);
1300                                         iter = 1000;
1301                                 }
1302                                 tsleep(&iter, 0, "h2race2", 1);
1303                         }
1304                         cpu_pause();
1305                 }
1306                 /* retry */
1307         }
1308
1309         /*
1310          * Last unlock / mutex upgraded to exclusive.  Drop the data
1311          * reference.
1312          */
1313         dio = hammer2_chain_drop_data(chain);
1314         if (dio)
1315                 hammer2_io_bqrelse(&dio);
1316         hammer2_mtx_unlock(&chain->lock);
1317 }
1318
1319 /*
1320  * Unlock and hold chain data intact
1321  */
1322 void
1323 hammer2_chain_unlock_hold(hammer2_chain_t *chain)
1324 {
1325         atomic_add_int(&chain->lockcnt, 1);
1326         hammer2_chain_unlock(chain);
1327 }
1328
1329 /*
1330  * Helper to obtain the blockref[] array base and count for a chain.
1331  *
1332  * XXX Not widely used yet, various use cases need to be validated and
1333  *     converted to use this function.
1334  */
1335 static
1336 hammer2_blockref_t *
1337 hammer2_chain_base_and_count(hammer2_chain_t *parent, int *countp)
1338 {
1339         hammer2_blockref_t *base;
1340         int count;
1341
1342         if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1343                 base = NULL;
1344
1345                 switch(parent->bref.type) {
1346                 case HAMMER2_BREF_TYPE_INODE:
1347                         count = HAMMER2_SET_COUNT;
1348                         break;
1349                 case HAMMER2_BREF_TYPE_INDIRECT:
1350                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1351                         count = parent->bytes / sizeof(hammer2_blockref_t);
1352                         break;
1353                 case HAMMER2_BREF_TYPE_VOLUME:
1354                         count = HAMMER2_SET_COUNT;
1355                         break;
1356                 case HAMMER2_BREF_TYPE_FREEMAP:
1357                         count = HAMMER2_SET_COUNT;
1358                         break;
1359                 default:
1360                         panic("hammer2_chain_create_indirect: "
1361                               "unrecognized blockref type: %d",
1362                               parent->bref.type);
1363                         count = 0;
1364                         break;
1365                 }
1366         } else {
1367                 switch(parent->bref.type) {
1368                 case HAMMER2_BREF_TYPE_INODE:
1369                         base = &parent->data->ipdata.u.blockset.blockref[0];
1370                         count = HAMMER2_SET_COUNT;
1371                         break;
1372                 case HAMMER2_BREF_TYPE_INDIRECT:
1373                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1374                         base = &parent->data->npdata[0];
1375                         count = parent->bytes / sizeof(hammer2_blockref_t);
1376                         break;
1377                 case HAMMER2_BREF_TYPE_VOLUME:
1378                         base = &parent->data->voldata.
1379                                         sroot_blockset.blockref[0];
1380                         count = HAMMER2_SET_COUNT;
1381                         break;
1382                 case HAMMER2_BREF_TYPE_FREEMAP:
1383                         base = &parent->data->blkset.blockref[0];
1384                         count = HAMMER2_SET_COUNT;
1385                         break;
1386                 default:
1387                         panic("hammer2_chain_create_indirect: "
1388                               "unrecognized blockref type: %d",
1389                               parent->bref.type);
1390                         count = 0;
1391                         break;
1392                 }
1393         }
1394         *countp = count;
1395
1396         return base;
1397 }
1398
1399 /*
1400  * This counts the number of live blockrefs in a block array and
1401  * also calculates the point at which all remaining blockrefs are empty.
1402  * This routine can only be called on a live chain.
1403  *
1404  * Caller holds the chain locked, but possibly with a shared lock.  We
1405  * must use an exclusive spinlock to prevent corruption.
1406  *
1407  * NOTE: Flag is not set until after the count is complete, allowing
1408  *       callers to test the flag without holding the spinlock.
1409  *
1410  * NOTE: If base is NULL the related chain is still in the INITIAL
1411  *       state and there are no blockrefs to count.
1412  *
1413  * NOTE: live_count may already have some counts accumulated due to
1414  *       creation and deletion and could even be initially negative.
1415  */
1416 void
1417 hammer2_chain_countbrefs(hammer2_chain_t *chain,
1418                          hammer2_blockref_t *base, int count)
1419 {
1420         hammer2_spin_ex(&chain->core.spin);
1421         if ((chain->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) {
1422                 if (base) {
1423                         while (--count >= 0) {
1424                                 if (base[count].type)
1425                                         break;
1426                         }
1427                         chain->core.live_zero = count + 1;
1428                         while (count >= 0) {
1429                                 if (base[count].type)
1430                                         atomic_add_int(&chain->core.live_count,
1431                                                        1);
1432                                 --count;
1433                         }
1434                 } else {
1435                         chain->core.live_zero = 0;
1436                 }
1437                 /* else do not modify live_count */
1438                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_COUNTEDBREFS);
1439         }
1440         hammer2_spin_unex(&chain->core.spin);
1441 }
1442
1443 /*
1444  * Resize the chain's physical storage allocation in-place.  This function does
1445  * not usually adjust the data pointer and must be followed by (typically) a
1446  * hammer2_chain_modify() call to copy any old data over and adjust the
1447  * data pointer.
1448  *
1449  * Chains can be resized smaller without reallocating the storage.  Resizing
1450  * larger will reallocate the storage.  Excess or prior storage is reclaimed
1451  * asynchronously at a later time.
1452  *
1453  * An nradix value of 0 is special-cased to mean that the storage should
1454  * be disassociated, that is the chain is being resized to 0 bytes (not 1
1455  * byte).
1456  *
1457  * Must be passed an exclusively locked parent and chain.
1458  *
1459  * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
1460  * to avoid instantiating a device buffer that conflicts with the vnode data
1461  * buffer.  However, because H2 can compress or encrypt data, the chain may
1462  * have a dio assigned to it in those situations, and they do not conflict.
1463  *
1464  * XXX return error if cannot resize.
1465  */
1466 int
1467 hammer2_chain_resize(hammer2_chain_t *chain,
1468                      hammer2_tid_t mtid, hammer2_off_t dedup_off,
1469                      int nradix, int flags)
1470 {
1471         hammer2_dev_t *hmp;
1472         size_t obytes;
1473         size_t nbytes;
1474         int error;
1475
1476         hmp = chain->hmp;
1477
1478         /*
1479          * Only data and indirect blocks can be resized for now.
1480          * (The volu root, inodes, and freemap elements use a fixed size).
1481          */
1482         KKASSERT(chain != &hmp->vchain);
1483         KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1484                  chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1485                  chain->bref.type == HAMMER2_BREF_TYPE_DIRENT);
1486
1487         /*
1488          * Nothing to do if the element is already the proper size
1489          */
1490         obytes = chain->bytes;
1491         nbytes = (nradix) ? (1U << nradix) : 0;
1492         if (obytes == nbytes)
1493                 return (chain->error);
1494
1495         /*
1496          * Make sure the old data is instantiated so we can copy it.  If this
1497          * is a data block, the device data may be superfluous since the data
1498          * might be in a logical block, but compressed or encrypted data is
1499          * another matter.
1500          *
1501          * NOTE: The modify will set BMAPUPD for us if BMAPPED is set.
1502          */
1503         error = hammer2_chain_modify(chain, mtid, dedup_off, 0);
1504         if (error)
1505                 return error;
1506
1507         /*
1508          * Relocate the block, even if making it smaller (because different
1509          * block sizes may be in different regions).
1510          *
1511          * NOTE: Operation does not copy the data and may only be used
1512          *        to resize data blocks in-place, or directory entry blocks
1513          *        which are about to be modified in some manner.
1514          */
1515         error = hammer2_freemap_alloc(chain, nbytes);
1516         if (error)
1517                 return error;
1518
1519         chain->bytes = nbytes;
1520
1521         /*
1522          * We don't want the followup chain_modify() to try to copy data
1523          * from the old (wrong-sized) buffer.  It won't know how much to
1524          * copy.  This case should only occur during writes when the
1525          * originator already has the data to write in-hand.
1526          */
1527         if (chain->dio) {
1528                 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1529                          chain->bref.type == HAMMER2_BREF_TYPE_DIRENT);
1530                 hammer2_io_brelse(&chain->dio);
1531                 chain->data = NULL;
1532         }
1533         return (chain->error);
1534 }
1535
1536 /*
1537  * Set the chain modified so its data can be changed by the caller, or
1538  * install deduplicated data.  The caller must call this routine for each
1539  * set of modifications it makes, even if the chain is already flagged
1540  * MODIFIED.
1541  *
1542  * Sets bref.modify_tid to mtid only if mtid != 0.  Note that bref.modify_tid
1543  * is a CLC (cluster level change) field and is not updated by parent
1544  * propagation during a flush.
1545  *
1546  * Returns an appropriate HAMMER2_ERROR_* code, which will generally reflect
1547  * chain->error except for HAMMER2_ERROR_ENOSPC.  If the allocation fails
1548  * due to no space available, HAMMER2_ERROR_ENOSPC is returned and the chain
1549  * remains unmodified with its old data ref intact and chain->error
1550  * unchanged.
1551  *
1552  *                               Dedup Handling
1553  *
1554  * If the DEDUPABLE flag is set in the chain the storage must be reallocated
1555  * even if the chain is still flagged MODIFIED.  In this case the chain's
1556  * DEDUPABLE flag will be cleared once the new storage has been assigned.
1557  *
1558  * If the caller passes a non-zero dedup_off we will use it to assign the
1559  * new storage.  The MODIFIED flag will be *CLEARED* in this case, and
1560  * DEDUPABLE will be set (NOTE: the UPDATE flag is always set).  The caller
1561  * must not modify the data content upon return.
1562  */
1563 int
1564 hammer2_chain_modify(hammer2_chain_t *chain, hammer2_tid_t mtid,
1565                      hammer2_off_t dedup_off, int flags)
1566 {
1567         hammer2_blockref_t obref;
1568         hammer2_dev_t *hmp;
1569         hammer2_io_t *dio;
1570         int error;
1571         int wasinitial;
1572         int setmodified;
1573         int setupdate;
1574         int newmod;
1575         char *bdata;
1576
1577         hmp = chain->hmp;
1578         obref = chain->bref;
1579         KKASSERT((chain->flags & HAMMER2_CHAIN_FICTITIOUS) == 0);
1580
1581         /*
1582          * Data is not optional for freemap chains (we must always be sure
1583          * to copy the data on COW storage allocations).
1584          */
1585         if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1586             chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1587                 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1588                          (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1589         }
1590
1591         /*
1592          * Data must be resolved if already assigned, unless explicitly
1593          * flagged otherwise.  If we cannot safety load the data the
1594          * modification fails and we return early.
1595          */
1596         if (chain->data == NULL && chain->bytes != 0 &&
1597             (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1598             (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1599                 hammer2_chain_load_data(chain);
1600                 if (chain->error)
1601                         return (chain->error);
1602         }
1603         error = 0;
1604
1605         /*
1606          * Set MODIFIED to indicate that the chain has been modified.  A new
1607          * allocation is required when modifying a chain.
1608          *
1609          * Set UPDATE to ensure that the blockref is updated in the parent.
1610          *
1611          * If MODIFIED is already set determine if we can reuse the assigned
1612          * data block or if we need a new data block.
1613          */
1614         if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1615                 /*
1616                  * Must set modified bit.
1617                  */
1618                 atomic_add_long(&hammer2_count_modified_chains, 1);
1619                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1620                 hammer2_pfs_memory_inc(chain->pmp);  /* can be NULL */
1621                 setmodified = 1;
1622
1623                 /*
1624                  * We may be able to avoid a copy-on-write if the chain's
1625                  * check mode is set to NONE and the chain's current
1626                  * modify_tid is beyond the last explicit snapshot tid.
1627                  *
1628                  * This implements HAMMER2's overwrite-in-place feature.
1629                  *
1630                  * NOTE! This data-block cannot be used as a de-duplication
1631                  *       source when the check mode is set to NONE.
1632                  */
1633                 if ((chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1634                      chain->bref.type == HAMMER2_BREF_TYPE_DIRENT) &&
1635                     (chain->flags & HAMMER2_CHAIN_INITIAL) == 0 &&
1636                     (chain->flags & HAMMER2_CHAIN_DEDUPABLE) == 0 &&
1637                     HAMMER2_DEC_CHECK(chain->bref.methods) ==
1638                      HAMMER2_CHECK_NONE &&
1639                     chain->pmp &&
1640                     chain->bref.modify_tid >
1641                      chain->pmp->iroot->meta.pfs_lsnap_tid) {
1642                         /*
1643                          * Sector overwrite allowed.
1644                          */
1645                         newmod = 0;
1646                 } else {
1647                         /*
1648                          * Sector overwrite not allowed, must copy-on-write.
1649                          */
1650                         newmod = 1;
1651                 }
1652         } else if (chain->flags & HAMMER2_CHAIN_DEDUPABLE) {
1653                 /*
1654                  * If the modified chain was registered for dedup we need
1655                  * a new allocation.  This only happens for delayed-flush
1656                  * chains (i.e. which run through the front-end buffer
1657                  * cache).
1658                  */
1659                 newmod = 1;
1660                 setmodified = 0;
1661         } else {
1662                 /*
1663                  * Already flagged modified, no new allocation is needed.
1664                  */
1665                 newmod = 0;
1666                 setmodified = 0;
1667         }
1668
1669         /*
1670          * Flag parent update required.
1671          */
1672         if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
1673                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1674                 setupdate = 1;
1675         } else {
1676                 setupdate = 0;
1677         }
1678
1679         /*
1680          * The modification or re-modification requires an allocation and
1681          * possible COW.  If an error occurs, the previous content and data
1682          * reference is retained and the modification fails.
1683          *
1684          * If dedup_off is non-zero, the caller is requesting a deduplication
1685          * rather than a modification.  The MODIFIED bit is not set and the
1686          * data offset is set to the deduplication offset.  The data cannot
1687          * be modified.
1688          *
1689          * NOTE: The dedup offset is allowed to be in a partially free state
1690          *       and we must be sure to reset it to a fully allocated state
1691          *       to force two bulkfree passes to free it again.
1692          *
1693          * NOTE: Only applicable when chain->bytes != 0.
1694          *
1695          * XXX can a chain already be marked MODIFIED without a data
1696          * assignment?  If not, assert here instead of testing the case.
1697          */
1698         if (chain != &hmp->vchain && chain != &hmp->fchain &&
1699             chain->bytes) {
1700                 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1701                      newmod
1702                 ) {
1703                         /*
1704                          * NOTE: We do not have to remove the dedup
1705                          *       registration because the area is still
1706                          *       allocated and the underlying DIO will
1707                          *       still be flushed.
1708                          */
1709                         if (dedup_off) {
1710                                 chain->bref.data_off = dedup_off;
1711                                 chain->bytes = 1 << (dedup_off &
1712                                                      HAMMER2_OFF_MASK_RADIX);
1713                                 chain->error = 0;
1714                                 atomic_clear_int(&chain->flags,
1715                                                  HAMMER2_CHAIN_MODIFIED);
1716                                 atomic_add_long(&hammer2_count_modified_chains,
1717                                                 -1);
1718                                 if (chain->pmp)
1719                                         hammer2_pfs_memory_wakeup(chain->pmp);
1720                                 hammer2_freemap_adjust(hmp, &chain->bref,
1721                                                 HAMMER2_FREEMAP_DORECOVER);
1722                                 atomic_set_int(&chain->flags,
1723                                                 HAMMER2_CHAIN_DEDUPABLE);
1724                         } else {
1725                                 error = hammer2_freemap_alloc(chain,
1726                                                               chain->bytes);
1727                                 atomic_clear_int(&chain->flags,
1728                                                 HAMMER2_CHAIN_DEDUPABLE);
1729                         }
1730                 }
1731         }
1732
1733         /*
1734          * Stop here if error.  We have to undo any flag bits we might
1735          * have set above.
1736          */
1737         if (error) {
1738                 if (setmodified) {
1739                         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1740                         atomic_add_long(&hammer2_count_modified_chains, -1);
1741                         if (chain->pmp)
1742                                 hammer2_pfs_memory_wakeup(chain->pmp);
1743                 }
1744                 if (setupdate) {
1745                         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1746                 }
1747                 return error;
1748         }
1749
1750         /*
1751          * Update mirror_tid and modify_tid.  modify_tid is only updated
1752          * if not passed as zero (during flushes, parent propagation passes
1753          * the value 0).
1754          *
1755          * NOTE: chain->pmp could be the device spmp.
1756          */
1757         chain->bref.mirror_tid = hmp->voldata.mirror_tid + 1;
1758         if (mtid)
1759                 chain->bref.modify_tid = mtid;
1760
1761         /*
1762          * Set BMAPUPD to tell the flush code that an existing blockmap entry
1763          * requires updating as well as to tell the delete code that the
1764          * chain's blockref might not exactly match (in terms of physical size
1765          * or block offset) the one in the parent's blocktable.  The base key
1766          * of course will still match.
1767          */
1768         if (chain->flags & HAMMER2_CHAIN_BMAPPED)
1769                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD);
1770
1771         /*
1772          * Short-cut data blocks which the caller does not need an actual
1773          * data reference to (aka OPTDATA), as long as the chain does not
1774          * already have a data pointer to the data.  This generally means
1775          * that the modifications are being done via the logical buffer cache.
1776          * The INITIAL flag relates only to the device data buffer and thus
1777          * remains unchange in this situation.
1778          *
1779          * This code also handles bytes == 0 (most dirents).
1780          */
1781         if (chain->bref.type == HAMMER2_BREF_TYPE_DATA &&
1782             (flags & HAMMER2_MODIFY_OPTDATA) &&
1783             chain->data == NULL) {
1784                 KKASSERT(chain->dio == NULL);
1785                 goto skip2;
1786         }
1787
1788         /*
1789          * Clearing the INITIAL flag (for indirect blocks) indicates that
1790          * we've processed the uninitialized storage allocation.
1791          *
1792          * If this flag is already clear we are likely in a copy-on-write
1793          * situation but we have to be sure NOT to bzero the storage if
1794          * no data is present.
1795          */
1796         if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1797                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1798                 wasinitial = 1;
1799         } else {
1800                 wasinitial = 0;
1801         }
1802
1803         /*
1804          * Instantiate data buffer and possibly execute COW operation
1805          */
1806         switch(chain->bref.type) {
1807         case HAMMER2_BREF_TYPE_VOLUME:
1808         case HAMMER2_BREF_TYPE_FREEMAP:
1809                 /*
1810                  * The data is embedded, no copy-on-write operation is
1811                  * needed.
1812                  */
1813                 KKASSERT(chain->dio == NULL);
1814                 break;
1815         case HAMMER2_BREF_TYPE_DIRENT:
1816                 /*
1817                  * The data might be fully embedded.
1818                  */
1819                 if (chain->bytes == 0) {
1820                         KKASSERT(chain->dio == NULL);
1821                         break;
1822                 }
1823                 /* fall through */
1824         case HAMMER2_BREF_TYPE_INODE:
1825         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1826         case HAMMER2_BREF_TYPE_DATA:
1827         case HAMMER2_BREF_TYPE_INDIRECT:
1828         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1829                 /*
1830                  * Perform the copy-on-write operation
1831                  *
1832                  * zero-fill or copy-on-write depending on whether
1833                  * chain->data exists or not and set the dirty state for
1834                  * the new buffer.  hammer2_io_new() will handle the
1835                  * zero-fill.
1836                  *
1837                  * If a dedup_off was supplied this is an existing block
1838                  * and no COW, copy, or further modification is required.
1839                  */
1840                 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1841
1842                 if (wasinitial && dedup_off == 0) {
1843                         error = hammer2_io_new(hmp, chain->bref.type,
1844                                                chain->bref.data_off,
1845                                                chain->bytes, &dio);
1846                 } else {
1847                         error = hammer2_io_bread(hmp, chain->bref.type,
1848                                                  chain->bref.data_off,
1849                                                  chain->bytes, &dio);
1850                 }
1851                 hammer2_adjreadcounter(&chain->bref, chain->bytes);
1852
1853                 /*
1854                  * If an I/O error occurs make sure callers cannot accidently
1855                  * modify the old buffer's contents and corrupt the filesystem.
1856                  */
1857                 if (error) {
1858                         kprintf("hammer2_chain_modify: hmp=%p I/O error\n",
1859                                 hmp);
1860                         chain->error = HAMMER2_ERROR_EIO;
1861                         hammer2_io_brelse(&dio);
1862                         hammer2_io_brelse(&chain->dio);
1863                         chain->data = NULL;
1864                         break;
1865                 }
1866                 chain->error = 0;
1867                 bdata = hammer2_io_data(dio, chain->bref.data_off);
1868
1869                 if (chain->data) {
1870                         /*
1871                          * COW (unless a dedup).
1872                          */
1873                         KKASSERT(chain->dio != NULL);
1874                         if (chain->data != (void *)bdata && dedup_off == 0) {
1875                                 bcopy(chain->data, bdata, chain->bytes);
1876                         }
1877                 } else if (wasinitial == 0) {
1878                         /*
1879                          * We have a problem.  We were asked to COW but
1880                          * we don't have any data to COW with!
1881                          */
1882                         panic("hammer2_chain_modify: having a COW %p\n",
1883                               chain);
1884                 }
1885
1886                 /*
1887                  * Retire the old buffer, replace with the new.  Dirty or
1888                  * redirty the new buffer.
1889                  *
1890                  * WARNING! The system buffer cache may have already flushed
1891                  *          the buffer, so we must be sure to [re]dirty it
1892                  *          for further modification.
1893                  *
1894                  *          If dedup_off was supplied, the caller is not
1895                  *          expected to make any further modification to the
1896                  *          buffer.
1897                  */
1898                 if (chain->dio)
1899                         hammer2_io_bqrelse(&chain->dio);
1900                 chain->data = (void *)bdata;
1901                 chain->dio = dio;
1902                 if (dedup_off == 0)
1903                         hammer2_io_setdirty(dio);
1904                 break;
1905         default:
1906                 panic("hammer2_chain_modify: illegal non-embedded type %d",
1907                       chain->bref.type);
1908                 break;
1909
1910         }
1911 skip2:
1912         /*
1913          * setflush on parent indicating that the parent must recurse down
1914          * to us.  Do not call on chain itself which might already have it
1915          * set.
1916          */
1917         if (chain->parent)
1918                 hammer2_chain_setflush(chain->parent);
1919         return (chain->error);
1920 }
1921
1922 /*
1923  * Modify the chain associated with an inode.
1924  */
1925 int
1926 hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
1927                         hammer2_tid_t mtid, int flags)
1928 {
1929         int error;
1930
1931         hammer2_inode_modify(ip);
1932         error = hammer2_chain_modify(chain, mtid, 0, flags);
1933
1934         return error;
1935 }
1936
1937 /*
1938  * Volume header data locks
1939  */
1940 void
1941 hammer2_voldata_lock(hammer2_dev_t *hmp)
1942 {
1943         lockmgr(&hmp->vollk, LK_EXCLUSIVE);
1944 }
1945
1946 void
1947 hammer2_voldata_unlock(hammer2_dev_t *hmp)
1948 {
1949         lockmgr(&hmp->vollk, LK_RELEASE);
1950 }
1951
1952 void
1953 hammer2_voldata_modify(hammer2_dev_t *hmp)
1954 {
1955         if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1956                 atomic_add_long(&hammer2_count_modified_chains, 1);
1957                 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
1958                 hammer2_pfs_memory_inc(hmp->vchain.pmp);
1959         }
1960 }
1961
1962 /*
1963  * This function returns the chain at the nearest key within the specified
1964  * range.  The returned chain will be referenced but not locked.
1965  *
1966  * This function will recurse through chain->rbtree as necessary and will
1967  * return a *key_nextp suitable for iteration.  *key_nextp is only set if
1968  * the iteration value is less than the current value of *key_nextp.
1969  *
1970  * The caller should use (*key_nextp) to calculate the actual range of
1971  * the returned element, which will be (key_beg to *key_nextp - 1), because
1972  * there might be another element which is superior to the returned element
1973  * and overlaps it.
1974  *
1975  * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
1976  * chains continue to be returned.  On EOF (*key_nextp) may overflow since
1977  * it will wind up being (key_end + 1).
1978  *
1979  * WARNING!  Must be called with child's spinlock held.  Spinlock remains
1980  *           held through the operation.
1981  */
1982 struct hammer2_chain_find_info {
1983         hammer2_chain_t         *best;
1984         hammer2_key_t           key_beg;
1985         hammer2_key_t           key_end;
1986         hammer2_key_t           key_next;
1987 };
1988
1989 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
1990 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
1991
1992 static
1993 hammer2_chain_t *
1994 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
1995                           hammer2_key_t key_beg, hammer2_key_t key_end)
1996 {
1997         struct hammer2_chain_find_info info;
1998
1999         info.best = NULL;
2000         info.key_beg = key_beg;
2001         info.key_end = key_end;
2002         info.key_next = *key_nextp;
2003
2004         RB_SCAN(hammer2_chain_tree, &parent->core.rbtree,
2005                 hammer2_chain_find_cmp, hammer2_chain_find_callback,
2006                 &info);
2007         *key_nextp = info.key_next;
2008 #if 0
2009         kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
2010                 parent, key_beg, key_end, *key_nextp);
2011 #endif
2012
2013         return (info.best);
2014 }
2015
2016 static
2017 int
2018 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
2019 {
2020         struct hammer2_chain_find_info *info = data;
2021         hammer2_key_t child_beg;
2022         hammer2_key_t child_end;
2023
2024         child_beg = child->bref.key;
2025         child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
2026
2027         if (child_end < info->key_beg)
2028                 return(-1);
2029         if (child_beg > info->key_end)
2030                 return(1);
2031         return(0);
2032 }
2033
2034 static
2035 int
2036 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
2037 {
2038         struct hammer2_chain_find_info *info = data;
2039         hammer2_chain_t *best;
2040         hammer2_key_t child_end;
2041
2042         /*
2043          * WARNING! Layerq is scanned forwards, exact matches should keep
2044          *          the existing info->best.
2045          */
2046         if ((best = info->best) == NULL) {
2047                 /*
2048                  * No previous best.  Assign best
2049                  */
2050                 info->best = child;
2051         } else if (best->bref.key <= info->key_beg &&
2052                    child->bref.key <= info->key_beg) {
2053                 /*
2054                  * Illegal overlap.
2055                  */
2056                 KKASSERT(0);
2057                 /*info->best = child;*/
2058         } else if (child->bref.key < best->bref.key) {
2059                 /*
2060                  * Child has a nearer key and best is not flush with key_beg.
2061                  * Set best to child.  Truncate key_next to the old best key.
2062                  */
2063                 info->best = child;
2064                 if (info->key_next > best->bref.key || info->key_next == 0)
2065                         info->key_next = best->bref.key;
2066         } else if (child->bref.key == best->bref.key) {
2067                 /*
2068                  * If our current best is flush with the child then this
2069                  * is an illegal overlap.
2070                  *
2071                  * key_next will automatically be limited to the smaller of
2072                  * the two end-points.
2073                  */
2074                 KKASSERT(0);
2075                 info->best = child;
2076         } else {
2077                 /*
2078                  * Keep the current best but truncate key_next to the child's
2079                  * base.
2080                  *
2081                  * key_next will also automatically be limited to the smaller
2082                  * of the two end-points (probably not necessary for this case
2083                  * but we do it anyway).
2084                  */
2085                 if (info->key_next > child->bref.key || info->key_next == 0)
2086                         info->key_next = child->bref.key;
2087         }
2088
2089         /*
2090          * Always truncate key_next based on child's end-of-range.
2091          */
2092         child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
2093         if (child_end && (info->key_next > child_end || info->key_next == 0))
2094                 info->key_next = child_end;
2095
2096         return(0);
2097 }
2098
2099 /*
2100  * Retrieve the specified chain from a media blockref, creating the
2101  * in-memory chain structure which reflects it.  The returned chain is
2102  * held but not locked.  The caller must lock it to crc-check and
2103  * dereference its data, and should check chain->error after locking
2104  * before assuming that the data is good.
2105  *
2106  * To handle insertion races pass the INSERT_RACE flag along with the
2107  * generation number of the core.  NULL will be returned if the generation
2108  * number changes before we have a chance to insert the chain.  Insert
2109  * races can occur because the parent might be held shared.
2110  *
2111  * Caller must hold the parent locked shared or exclusive since we may
2112  * need the parent's bref array to find our block.
2113  *
2114  * WARNING! chain->pmp is always set to NULL for any chain representing
2115  *          part of the super-root topology.
2116  */
2117 hammer2_chain_t *
2118 hammer2_chain_get(hammer2_chain_t *parent, int generation,
2119                   hammer2_blockref_t *bref)
2120 {
2121         hammer2_dev_t *hmp = parent->hmp;
2122         hammer2_chain_t *chain;
2123         int error;
2124
2125         /*
2126          * Allocate a chain structure representing the existing media
2127          * entry.  Resulting chain has one ref and is not locked.
2128          */
2129         if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
2130                 chain = hammer2_chain_alloc(hmp, NULL, bref);
2131         else
2132                 chain = hammer2_chain_alloc(hmp, parent->pmp, bref);
2133         /* ref'd chain returned */
2134
2135         /*
2136          * Flag that the chain is in the parent's blockmap so delete/flush
2137          * knows what to do with it.
2138          */
2139         atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
2140
2141         /*
2142          * Link the chain into its parent.  A spinlock is required to safely
2143          * access the RBTREE, and it is possible to collide with another
2144          * hammer2_chain_get() operation because the caller might only hold
2145          * a shared lock on the parent.
2146          *
2147          * NOTE: Get races can occur quite often when we distribute
2148          *       asynchronous read-aheads across multiple threads.
2149          */
2150         KKASSERT(parent->refs > 0);
2151         error = hammer2_chain_insert(parent, chain,
2152                                      HAMMER2_CHAIN_INSERT_SPIN |
2153                                      HAMMER2_CHAIN_INSERT_RACE,
2154                                      generation);
2155         if (error) {
2156                 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
2157                 /*kprintf("chain %p get race\n", chain);*/
2158                 hammer2_chain_drop(chain);
2159                 chain = NULL;
2160         } else {
2161                 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
2162         }
2163
2164         /*
2165          * Return our new chain referenced but not locked, or NULL if
2166          * a race occurred.
2167          */
2168         return (chain);
2169 }
2170
2171 /*
2172  * Lookup initialization/completion API
2173  */
2174 hammer2_chain_t *
2175 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
2176 {
2177         hammer2_chain_ref(parent);
2178         if (flags & HAMMER2_LOOKUP_SHARED) {
2179                 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
2180                                            HAMMER2_RESOLVE_SHARED);
2181         } else {
2182                 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2183         }
2184         return (parent);
2185 }
2186
2187 void
2188 hammer2_chain_lookup_done(hammer2_chain_t *parent)
2189 {
2190         if (parent) {
2191                 hammer2_chain_unlock(parent);
2192                 hammer2_chain_drop(parent);
2193         }
2194 }
2195
2196 /*
2197  * Take the locked chain and return a locked parent.  The chain remains
2198  * locked on return.
2199  *
2200  * This will work even if the chain is errored, and the caller can check
2201  * parent->error on return if desired since the parent will be locked.
2202  *
2203  * This function handles the lock order reversal.
2204  */
2205 hammer2_chain_t *
2206 hammer2_chain_getparent(hammer2_chain_t *chain, int how)
2207 {
2208         hammer2_chain_t *parent;
2209
2210         /*
2211          * Be careful of order, chain must be unlocked before parent
2212          * is locked below to avoid a deadlock.
2213          *
2214          * Safe access to fu->parent requires fu's core spinlock.
2215          */
2216 again:
2217         hammer2_spin_ex(&chain->core.spin);
2218         parent = chain->parent;
2219         if (parent == NULL) {
2220                 hammer2_spin_unex(&chain->core.spin);
2221                 panic("hammer2_chain_getparent: no parent");
2222         }
2223         hammer2_chain_ref(parent);
2224         hammer2_spin_unex(&chain->core.spin);
2225
2226         hammer2_chain_unlock(chain);
2227         hammer2_chain_lock(parent, how);
2228         hammer2_chain_lock(chain, how);
2229
2230         /*
2231          * Parent relinking races are quite common.  We have to get it right
2232          * or we will blow up the block table.
2233          */
2234         if (chain->parent != parent) {
2235                 hammer2_chain_unlock(parent);
2236                 hammer2_chain_drop(parent);
2237                 goto again;
2238         }
2239         return parent;
2240 }
2241
2242 /*
2243  * Take the locked chain and return a locked parent.  The chain is unlocked
2244  * and dropped.  *chainp is set to the returned parent as a convenience.
2245  *
2246  * This will work even if the chain is errored, and the caller can check
2247  * parent->error on return if desired since the parent will be locked.
2248  *
2249  * This function handles the lock order reversal.
2250  */
2251 hammer2_chain_t *
2252 hammer2_chain_repparent(hammer2_chain_t **chainp, int how)
2253 {
2254         hammer2_chain_t *chain;
2255         hammer2_chain_t *parent;
2256
2257         /*
2258          * Be careful of order, chain must be unlocked before parent
2259          * is locked below to avoid a deadlock.
2260          *
2261          * Safe access to fu->parent requires fu's core spinlock.
2262          */
2263         chain = *chainp;
2264 again:
2265         hammer2_spin_ex(&chain->core.spin);
2266         parent = chain->parent;
2267         if (parent == NULL) {
2268                 hammer2_spin_unex(&chain->core.spin);
2269                 panic("hammer2_chain_getparent: no parent");
2270         }
2271         hammer2_chain_ref(parent);
2272         hammer2_spin_unex(&chain->core.spin);
2273
2274         hammer2_chain_unlock(chain);
2275         hammer2_chain_lock(parent, how);
2276
2277         /*
2278          * Parent relinking races are quite common.  We have to get it right
2279          * or we will blow up the block table.
2280          */
2281         if (chain->parent != parent) {
2282                 hammer2_chain_lock(chain, how);
2283                 hammer2_chain_unlock(parent);
2284                 hammer2_chain_drop(parent);
2285                 goto again;
2286         }
2287         hammer2_chain_drop(chain);
2288         *chainp = parent;
2289
2290         return parent;
2291 }
2292
2293 /*
2294  * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
2295  * (*parentp) typically points to an inode but can also point to a related
2296  * indirect block and this function will recurse upwards and find the inode
2297  * again.
2298  *
2299  * This function unconditionally sets *errorp, replacing any previous value.
2300  *
2301  * (*parentp) must be exclusively locked and referenced and can be an inode
2302  * or an existing indirect block within the inode.  If (*parent) is errored
2303  * out, this function will not attempt to recurse the radix tree and
2304  * will return NULL along with an appropriate *errorp.  If NULL is returned
2305  * and *errorp is 0, the requested lookup could not be located.
2306  *
2307  * On return (*parentp) will be modified to point at the deepest parent chain
2308  * element encountered during the search, as a helper for an insertion or
2309  * deletion.   The new (*parentp) will be locked and referenced and the old
2310  * will be unlocked and dereferenced (no change if they are both the same).
2311  * This is particularly important if the caller wishes to insert a new chain,
2312  * (*parentp) will be set properly even if NULL is returned, as long as no
2313  * error occurred.
2314  *
2315  * The matching chain will be returned exclusively locked.  If NOLOCK is
2316  * requested the chain will be returned only referenced.  Note that the
2317  * parent chain must always be locked shared or exclusive, matching the
2318  * HAMMER2_LOOKUP_SHARED flag.  We can conceivably lock it SHARED temporarily
2319  * when NOLOCK is specified but that complicates matters if *parentp must
2320  * inherit the chain.
2321  *
2322  * NOLOCK also implies NODATA, since an unlocked chain usually has a NULL
2323  * data pointer or can otherwise be in flux.
2324  *
2325  * NULL is returned if no match was found, but (*parentp) will still
2326  * potentially be adjusted.
2327  *
2328  * On return (*key_nextp) will point to an iterative value for key_beg.
2329  * (If NULL is returned (*key_nextp) is set to (key_end + 1)).
2330  *
2331  * This function will also recurse up the chain if the key is not within the
2332  * current parent's range.  (*parentp) can never be set to NULL.  An iteration
2333  * can simply allow (*parentp) to float inside the loop.
2334  *
2335  * NOTE!  chain->data is not always resolved.  By default it will not be
2336  *        resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF.  Use
2337  *        HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
2338  *        BREF_TYPE_DATA as the device buffer can alias the logical file
2339  *        buffer).
2340  */
2341
2342 hammer2_chain_t *
2343 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
2344                      hammer2_key_t key_beg, hammer2_key_t key_end,
2345                      int *errorp, int flags)
2346 {
2347         hammer2_dev_t *hmp;
2348         hammer2_chain_t *parent;
2349         hammer2_chain_t *chain;
2350         hammer2_blockref_t *base;
2351         hammer2_blockref_t *bref;
2352         hammer2_blockref_t bcopy;
2353         hammer2_key_t scan_beg;
2354         hammer2_key_t scan_end;
2355         int count = 0;
2356         int how_always = HAMMER2_RESOLVE_ALWAYS;
2357         int how_maybe = HAMMER2_RESOLVE_MAYBE;
2358         int how;
2359         int generation;
2360         int maxloops = 300000;
2361
2362         if (flags & HAMMER2_LOOKUP_ALWAYS) {
2363                 how_maybe = how_always;
2364                 how = HAMMER2_RESOLVE_ALWAYS;
2365         } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2366                 how = HAMMER2_RESOLVE_NEVER;
2367         } else {
2368                 how = HAMMER2_RESOLVE_MAYBE;
2369         }
2370         if (flags & HAMMER2_LOOKUP_SHARED) {
2371                 how_maybe |= HAMMER2_RESOLVE_SHARED;
2372                 how_always |= HAMMER2_RESOLVE_SHARED;
2373                 how |= HAMMER2_RESOLVE_SHARED;
2374         }
2375
2376         /*
2377          * Recurse (*parentp) upward if necessary until the parent completely
2378          * encloses the key range or we hit the inode.
2379          *
2380          * Handle races against the flusher deleting indirect nodes on its
2381          * way back up by continuing to recurse upward past the deletion.
2382          */
2383         parent = *parentp;
2384         hmp = parent->hmp;
2385         *errorp = 0;
2386
2387         while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2388                parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2389                 scan_beg = parent->bref.key;
2390                 scan_end = scan_beg +
2391                            ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2392                 if ((parent->flags & HAMMER2_CHAIN_DELETED) == 0) {
2393                         if (key_beg >= scan_beg && key_end <= scan_end)
2394                                 break;
2395                 }
2396                 parent = hammer2_chain_repparent(parentp, how_maybe);
2397         }
2398 again:
2399
2400         if (--maxloops == 0)
2401                 panic("hammer2_chain_lookup: maxloops");
2402         /*
2403          * Locate the blockref array.  Currently we do a fully associative
2404          * search through the array.
2405          */
2406         switch(parent->bref.type) {
2407         case HAMMER2_BREF_TYPE_INODE:
2408                 /*
2409                  * Special shortcut for embedded data returns the inode
2410                  * itself.  Callers must detect this condition and access
2411                  * the embedded data (the strategy code does this for us).
2412                  *
2413                  * This is only applicable to regular files and softlinks.
2414                  *
2415                  * We need a second lock on parent.  Since we already have
2416                  * a lock we must pass LOCKAGAIN to prevent unexpected
2417                  * blocking (we don't want to block on a second shared
2418                  * ref if an exclusive lock is pending)
2419                  */
2420                 if (parent->data->ipdata.meta.op_flags &
2421                     HAMMER2_OPFLAG_DIRECTDATA) {
2422                         if (flags & HAMMER2_LOOKUP_NODIRECT) {
2423                                 chain = NULL;
2424                                 *key_nextp = key_end + 1;
2425                                 goto done;
2426                         }
2427                         hammer2_chain_ref(parent);
2428                         if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0)
2429                                 hammer2_chain_lock(parent,
2430                                                    how_always |
2431                                                     HAMMER2_RESOLVE_LOCKAGAIN);
2432                         *key_nextp = key_end + 1;
2433                         return (parent);
2434                 }
2435                 base = &parent->data->ipdata.u.blockset.blockref[0];
2436                 count = HAMMER2_SET_COUNT;
2437                 break;
2438         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2439         case HAMMER2_BREF_TYPE_INDIRECT:
2440                 /*
2441                  * Handle MATCHIND on the parent
2442                  */
2443                 if (flags & HAMMER2_LOOKUP_MATCHIND) {
2444                         scan_beg = parent->bref.key;
2445                         scan_end = scan_beg +
2446                                ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2447                         if (key_beg == scan_beg && key_end == scan_end) {
2448                                 chain = parent;
2449                                 hammer2_chain_ref(chain);
2450                                 hammer2_chain_lock(chain, how_maybe);
2451                                 *key_nextp = scan_end + 1;
2452                                 goto done;
2453                         }
2454                 }
2455
2456                 /*
2457                  * Optimize indirect blocks in the INITIAL state to avoid
2458                  * I/O.
2459                  */
2460                 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2461                         base = NULL;
2462                 } else {
2463                         if (parent->data == NULL) {
2464                                 kprintf("parent->data is NULL %p\n", parent);
2465                                 while (1)
2466                                         tsleep(parent, 0, "xxx", 0);
2467                         }
2468                         base = &parent->data->npdata[0];
2469                 }
2470                 count = parent->bytes / sizeof(hammer2_blockref_t);
2471                 break;
2472         case HAMMER2_BREF_TYPE_VOLUME:
2473                 base = &parent->data->voldata.sroot_blockset.blockref[0];
2474                 count = HAMMER2_SET_COUNT;
2475                 break;
2476         case HAMMER2_BREF_TYPE_FREEMAP:
2477                 base = &parent->data->blkset.blockref[0];
2478                 count = HAMMER2_SET_COUNT;
2479                 break;
2480         default:
2481                 kprintf("hammer2_chain_lookup: unrecognized "
2482                         "blockref(B) type: %d",
2483                         parent->bref.type);
2484                 while (1)
2485                         tsleep(&base, 0, "dead", 0);
2486                 panic("hammer2_chain_lookup: unrecognized "
2487                       "blockref(B) type: %d",
2488                       parent->bref.type);
2489                 base = NULL;    /* safety */
2490                 count = 0;      /* safety */
2491         }
2492
2493         /*
2494          * No lookup is possible if the parent is errored.  We delayed
2495          * this check as long as we could to ensure that the parent backup,
2496          * embedded data, and MATCHIND code could still execute.
2497          */
2498         if (parent->error) {
2499                 *errorp = parent->error;
2500                 return NULL;
2501         }
2502
2503         /*
2504          * Merged scan to find next candidate.
2505          *
2506          * hammer2_base_*() functions require the parent->core.live_* fields
2507          * to be synchronized.
2508          *
2509          * We need to hold the spinlock to access the block array and RB tree
2510          * and to interlock chain creation.
2511          */
2512         if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2513                 hammer2_chain_countbrefs(parent, base, count);
2514
2515         /*
2516          * Combined search
2517          */
2518         hammer2_spin_ex(&parent->core.spin);
2519         chain = hammer2_combined_find(parent, base, count,
2520                                       key_nextp,
2521                                       key_beg, key_end,
2522                                       &bref);
2523         generation = parent->core.generation;
2524
2525         /*
2526          * Exhausted parent chain, iterate.
2527          */
2528         if (bref == NULL) {
2529                 hammer2_spin_unex(&parent->core.spin);
2530                 if (key_beg == key_end) /* short cut single-key case */
2531                         return (NULL);
2532
2533                 /*
2534                  * Stop if we reached the end of the iteration.
2535                  */
2536                 if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
2537                     parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2538                         return (NULL);
2539                 }
2540
2541                 /*
2542                  * Calculate next key, stop if we reached the end of the
2543                  * iteration, otherwise go up one level and loop.
2544                  */
2545                 key_beg = parent->bref.key +
2546                           ((hammer2_key_t)1 << parent->bref.keybits);
2547                 if (key_beg == 0 || key_beg > key_end)
2548                         return (NULL);
2549                 parent = hammer2_chain_repparent(parentp, how_maybe);
2550                 goto again;
2551         }
2552
2553         /*
2554          * Selected from blockref or in-memory chain.
2555          */
2556         if (chain == NULL) {
2557                 bcopy = *bref;
2558                 hammer2_spin_unex(&parent->core.spin);
2559                 chain = hammer2_chain_get(parent, generation,
2560                                           &bcopy);
2561                 if (chain == NULL) {
2562                         /*
2563                         kprintf("retry lookup parent %p keys %016jx:%016jx\n",
2564                                 parent, key_beg, key_end);
2565                         */
2566                         goto again;
2567                 }
2568                 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2569                         hammer2_chain_drop(chain);
2570                         goto again;
2571                 }
2572         } else {
2573                 hammer2_chain_ref(chain);
2574                 hammer2_spin_unex(&parent->core.spin);
2575         }
2576
2577         /*
2578          * chain is referenced but not locked.  We must lock the chain
2579          * to obtain definitive state.
2580          */
2581         if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2582             chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2583                 hammer2_chain_lock(chain, how_maybe);
2584         } else {
2585                 hammer2_chain_lock(chain, how);
2586         }
2587         KKASSERT(chain->parent == parent);
2588
2589         /*
2590          * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2591          *
2592          * NOTE: Chain's key range is not relevant as there might be
2593          *       one-offs within the range that are not deleted.
2594          *
2595          * NOTE: Lookups can race delete-duplicate because
2596          *       delete-duplicate does not lock the parent's core
2597          *       (they just use the spinlock on the core).
2598          */
2599         if (chain->flags & HAMMER2_CHAIN_DELETED) {
2600                 kprintf("skip deleted chain %016jx.%02x key=%016jx\n",
2601                         chain->bref.data_off, chain->bref.type,
2602                         chain->bref.key);
2603                 hammer2_chain_unlock(chain);
2604                 hammer2_chain_drop(chain);
2605                 key_beg = *key_nextp;
2606                 if (key_beg == 0 || key_beg > key_end)
2607                         return(NULL);
2608                 goto again;
2609         }
2610
2611         /*
2612          * If the chain element is an indirect block it becomes the new
2613          * parent and we loop on it.  We must maintain our top-down locks
2614          * to prevent the flusher from interfering (i.e. doing a
2615          * delete-duplicate and leaving us recursing down a deleted chain).
2616          *
2617          * The parent always has to be locked with at least RESOLVE_MAYBE
2618          * so we can access its data.  It might need a fixup if the caller
2619          * passed incompatible flags.  Be careful not to cause a deadlock
2620          * as a data-load requires an exclusive lock.
2621          *
2622          * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
2623          * range is within the requested key range we return the indirect
2624          * block and do NOT loop.  This is usually only used to acquire
2625          * freemap nodes.
2626          */
2627         if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2628             chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2629                 hammer2_chain_unlock(parent);
2630                 hammer2_chain_drop(parent);
2631                 *parentp = parent = chain;
2632                 goto again;
2633         }
2634 done:
2635         /*
2636          * All done, return the chain.
2637          *
2638          * If the caller does not want a locked chain, replace the lock with
2639          * a ref.  Perhaps this can eventually be optimized to not obtain the
2640          * lock in the first place for situations where the data does not
2641          * need to be resolved.
2642          *
2643          * NOTE! A chain->error must be tested by the caller upon return.
2644          *       *errorp is only set based on issues which occur while
2645          *       trying to reach the chain.
2646          */
2647         if (chain) {
2648                 if (flags & HAMMER2_LOOKUP_NOLOCK)
2649                         hammer2_chain_unlock(chain);
2650         }
2651         return (chain);
2652 }
2653
2654 /*
2655  * After having issued a lookup we can iterate all matching keys.
2656  *
2657  * If chain is non-NULL we continue the iteration from just after it's index.
2658  *
2659  * If chain is NULL we assume the parent was exhausted and continue the
2660  * iteration at the next parent.
2661  *
2662  * If a fatal error occurs (typically an I/O error), a dummy chain is
2663  * returned with chain->error and error-identifying information set.  This
2664  * chain will assert if you try to do anything fancy with it.
2665  *
2666  * XXX Depending on where the error occurs we should allow continued iteration.
2667  *
2668  * parent must be locked on entry and remains locked throughout.  chain's
2669  * lock status must match flags.  Chain is always at least referenced.
2670  *
2671  * WARNING!  The MATCHIND flag does not apply to this function.
2672  */
2673 hammer2_chain_t *
2674 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
2675                    hammer2_key_t *key_nextp,
2676                    hammer2_key_t key_beg, hammer2_key_t key_end,
2677                    int *errorp, int flags)
2678 {
2679         hammer2_chain_t *parent;
2680         int how_maybe;
2681
2682         /*
2683          * Calculate locking flags for upward recursion.
2684          */
2685         how_maybe = HAMMER2_RESOLVE_MAYBE;
2686         if (flags & HAMMER2_LOOKUP_SHARED)
2687                 how_maybe |= HAMMER2_RESOLVE_SHARED;
2688
2689         parent = *parentp;
2690         *errorp = 0;
2691
2692         /*
2693          * Calculate the next index and recalculate the parent if necessary.
2694          */
2695         if (chain) {
2696                 key_beg = chain->bref.key +
2697                           ((hammer2_key_t)1 << chain->bref.keybits);
2698                 if ((flags & (HAMMER2_LOOKUP_NOLOCK |
2699                               HAMMER2_LOOKUP_NOUNLOCK)) == 0) {
2700                         hammer2_chain_unlock(chain);
2701                 }
2702                 hammer2_chain_drop(chain);
2703
2704                 /*
2705                  * chain invalid past this point, but we can still do a
2706                  * pointer comparison w/parent.
2707                  *
2708                  * Any scan where the lookup returned degenerate data embedded
2709                  * in the inode has an invalid index and must terminate.
2710                  */
2711                 if (chain == parent)
2712                         return(NULL);
2713                 if (key_beg == 0 || key_beg > key_end)
2714                         return(NULL);
2715                 chain = NULL;
2716         } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
2717                    parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2718                 /*
2719                  * We reached the end of the iteration.
2720                  */
2721                 return (NULL);
2722         } else {
2723                 /*
2724                  * Continue iteration with next parent unless the current
2725                  * parent covers the range.
2726                  *
2727                  * (This also handles the case of a deleted, empty indirect
2728                  * node).
2729                  */
2730                 key_beg = parent->bref.key +
2731                           ((hammer2_key_t)1 << parent->bref.keybits);
2732                 if (key_beg == 0 || key_beg > key_end)
2733                         return (NULL);
2734                 parent = hammer2_chain_repparent(parentp, how_maybe);
2735         }
2736
2737         /*
2738          * And execute
2739          */
2740         return (hammer2_chain_lookup(parentp, key_nextp,
2741                                      key_beg, key_end,
2742                                      errorp, flags));
2743 }
2744
2745 /*
2746  * Caller wishes to iterate chains under parent, loading new chains into
2747  * chainp.  Caller must initialize *chainp to NULL and *firstp to 1, and
2748  * then call hammer2_chain_scan() repeatedly until a non-zero return.
2749  * During the scan, *firstp will be set to 0 and (*chainp) will be replaced
2750  * with the returned chain for the scan.  The returned *chainp will be
2751  * locked and referenced.  Any prior contents will be unlocked and dropped.
2752  *
2753  * Caller should check the return value.  A normal scan EOF will return
2754  * exactly HAMMER2_ERROR_EOF.  Any other non-zero value indicates an
2755  * error trying to access parent data.  Any error in the returned chain
2756  * must be tested separately by the caller.
2757  *
2758  * (*chainp) is dropped on each scan, but will only be set if the returned
2759  * element itself can recurse.  Leaf elements are NOT resolved, loaded, or
2760  * returned via *chainp.  The caller will get their bref only.
2761  *
2762  * The raw scan function is similar to lookup/next but does not seek to a key.
2763  * Blockrefs are iterated via first_bref = (parent, NULL) and
2764  * next_chain = (parent, bref).
2765  *
2766  * The passed-in parent must be locked and its data resolved.  The function
2767  * nominally returns a locked and referenced *chainp != NULL for chains
2768  * the caller might need to recurse on (and will dipose of any *chainp passed
2769  * in).  The caller must check the chain->bref.type either way.
2770  */
2771 int
2772 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t **chainp,
2773                    hammer2_blockref_t *bref, int *firstp,
2774                    int flags)
2775 {
2776         hammer2_dev_t *hmp;
2777         hammer2_blockref_t *base;
2778         hammer2_blockref_t *bref_ptr;
2779         hammer2_key_t key;
2780         hammer2_key_t next_key;
2781         hammer2_chain_t *chain = NULL;
2782         int count = 0;
2783         int how_always = HAMMER2_RESOLVE_ALWAYS;
2784         int how_maybe = HAMMER2_RESOLVE_MAYBE;
2785         int how;
2786         int generation;
2787         int maxloops = 300000;
2788         int error;
2789
2790         hmp = parent->hmp;
2791         error = 0;
2792
2793         /*
2794          * Scan flags borrowed from lookup.
2795          */
2796         if (flags & HAMMER2_LOOKUP_ALWAYS) {
2797                 how_maybe = how_always;
2798                 how = HAMMER2_RESOLVE_ALWAYS;
2799         } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2800                 how = HAMMER2_RESOLVE_NEVER;
2801         } else {
2802                 how = HAMMER2_RESOLVE_MAYBE;
2803         }
2804         if (flags & HAMMER2_LOOKUP_SHARED) {
2805                 how_maybe |= HAMMER2_RESOLVE_SHARED;
2806                 how_always |= HAMMER2_RESOLVE_SHARED;
2807                 how |= HAMMER2_RESOLVE_SHARED;
2808         }
2809
2810         /*
2811          * Calculate key to locate first/next element, unlocking the previous
2812          * element as we go.  Be careful, the key calculation can overflow.
2813          *
2814          * (also reset bref to NULL)
2815          */
2816         if (*firstp) {
2817                 key = 0;
2818                 *firstp = 0;
2819         } else {
2820                 key = bref->key + ((hammer2_key_t)1 << bref->keybits);
2821                 if ((chain = *chainp) != NULL) {
2822                         *chainp = NULL;
2823                         hammer2_chain_unlock(chain);
2824                         hammer2_chain_drop(chain);
2825                         chain = NULL;
2826                 }
2827                 if (key == 0) {
2828                         error |= HAMMER2_ERROR_EOF;
2829                         goto done;
2830                 }
2831         }
2832
2833 again:
2834         if (parent->error) {
2835                 error = parent->error;
2836                 goto done;
2837         }
2838         if (--maxloops == 0)
2839                 panic("hammer2_chain_scan: maxloops");
2840
2841         /*
2842          * Locate the blockref array.  Currently we do a fully associative
2843          * search through the array.
2844          */
2845         switch(parent->bref.type) {
2846         case HAMMER2_BREF_TYPE_INODE:
2847                 /*
2848                  * An inode with embedded data has no sub-chains.
2849                  *
2850                  * WARNING! Bulk scan code may pass a static chain marked
2851                  *          as BREF_TYPE_INODE with a copy of the volume
2852                  *          root blockset to snapshot the volume.
2853                  */
2854                 if (parent->data->ipdata.meta.op_flags &
2855                     HAMMER2_OPFLAG_DIRECTDATA) {
2856                         error |= HAMMER2_ERROR_EOF;
2857                         goto done;
2858                 }
2859                 base = &parent->data->ipdata.u.blockset.blockref[0];
2860                 count = HAMMER2_SET_COUNT;
2861                 break;
2862         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2863         case HAMMER2_BREF_TYPE_INDIRECT:
2864                 /*
2865                  * Optimize indirect blocks in the INITIAL state to avoid
2866                  * I/O.
2867                  */
2868                 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2869                         base = NULL;
2870                 } else {
2871                         if (parent->data == NULL)
2872                                 panic("parent->data is NULL");
2873                         base = &parent->data->npdata[0];
2874                 }
2875                 count = parent->bytes / sizeof(hammer2_blockref_t);
2876                 break;
2877         case HAMMER2_BREF_TYPE_VOLUME:
2878                 base = &parent->data->voldata.sroot_blockset.blockref[0];
2879                 count = HAMMER2_SET_COUNT;
2880                 break;
2881         case HAMMER2_BREF_TYPE_FREEMAP:
2882                 base = &parent->data->blkset.blockref[0];
2883                 count = HAMMER2_SET_COUNT;
2884                 break;
2885         default:
2886                 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2887                       parent->bref.type);
2888                 base = NULL;    /* safety */
2889                 count = 0;      /* safety */
2890         }
2891
2892         /*
2893          * Merged scan to find next candidate.
2894          *
2895          * hammer2_base_*() functions require the parent->core.live_* fields
2896          * to be synchronized.
2897          *
2898          * We need to hold the spinlock to access the block array and RB tree
2899          * and to interlock chain creation.
2900          */
2901         if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
2902                 hammer2_chain_countbrefs(parent, base, count);
2903
2904         next_key = 0;
2905         bref_ptr = NULL;
2906         hammer2_spin_ex(&parent->core.spin);
2907         chain = hammer2_combined_find(parent, base, count,
2908                                       &next_key,
2909                                       key, HAMMER2_KEY_MAX,
2910                                       &bref_ptr);
2911         generation = parent->core.generation;
2912
2913         /*
2914          * Exhausted parent chain, we're done.
2915          */
2916         if (bref_ptr == NULL) {
2917                 hammer2_spin_unex(&parent->core.spin);
2918                 KKASSERT(chain == NULL);
2919                 error |= HAMMER2_ERROR_EOF;
2920                 goto done;
2921         }
2922
2923         /*
2924          * Copy into the supplied stack-based blockref.
2925          */
2926         *bref = *bref_ptr;
2927
2928         /*
2929          * Selected from blockref or in-memory chain.
2930          */
2931         if (chain == NULL) {
2932                 switch(bref->type) {
2933                 case HAMMER2_BREF_TYPE_INODE:
2934                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2935                 case HAMMER2_BREF_TYPE_INDIRECT:
2936                 case HAMMER2_BREF_TYPE_VOLUME:
2937                 case HAMMER2_BREF_TYPE_FREEMAP:
2938                         /*
2939                          * Recursion, always get the chain
2940                          */
2941                         hammer2_spin_unex(&parent->core.spin);
2942                         chain = hammer2_chain_get(parent, generation, bref);
2943                         if (chain == NULL) {
2944                                 kprintf("retry scan parent %p keys %016jx\n",
2945                                         parent, key);
2946                                 goto again;
2947                         }
2948                         if (bcmp(bref, bref_ptr, sizeof(*bref))) {
2949                                 hammer2_chain_drop(chain);
2950                                 chain = NULL;
2951                                 goto again;
2952                         }
2953                         break;
2954                 default:
2955                         /*
2956                          * No recursion, do not waste time instantiating
2957                          * a chain, just iterate using the bref.
2958                          */
2959                         hammer2_spin_unex(&parent->core.spin);
2960                         break;
2961                 }
2962         } else {
2963                 /*
2964                  * Recursion or not we need the chain in order to supply
2965                  * the bref.
2966                  */
2967                 hammer2_chain_ref(chain);
2968                 hammer2_spin_unex(&parent->core.spin);
2969         }
2970
2971         /*
2972          * chain is referenced but not locked.  We must lock the chain
2973          * to obtain definitive state.
2974          */
2975         if (chain)
2976                 hammer2_chain_lock(chain, how);
2977
2978         /*
2979          * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2980          *
2981          * NOTE: chain's key range is not relevant as there might be
2982          *       one-offs within the range that are not deleted.
2983          *
2984          * NOTE: XXX this could create problems with scans used in
2985          *       situations other than mount-time recovery.
2986          *
2987          * NOTE: Lookups can race delete-duplicate because
2988          *       delete-duplicate does not lock the parent's core
2989          *       (they just use the spinlock on the core).
2990          */
2991         if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
2992                 hammer2_chain_unlock(chain);
2993                 hammer2_chain_drop(chain);
2994                 chain = NULL;
2995
2996                 key = next_key;
2997                 if (key == 0) {
2998                         error |= HAMMER2_ERROR_EOF;
2999                         goto done;
3000                 }
3001                 goto again;
3002         }
3003
3004 done:
3005         /*
3006          * All done, return the bref or NULL, supply chain if necessary.
3007          */
3008         if (chain)
3009                 *chainp = chain;
3010         return (error);
3011 }
3012
3013 /*
3014  * Create and return a new hammer2 system memory structure of the specified
3015  * key, type and size and insert it under (*parentp).  This is a full
3016  * insertion, based on the supplied key/keybits, and may involve creating
3017  * indirect blocks and moving other chains around via delete/duplicate.
3018  *
3019  * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION
3020  * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
3021  * FULL.  This typically means that the caller is creating the chain after
3022  * doing a hammer2_chain_lookup().
3023  *
3024  * (*parentp) must be exclusive locked and may be replaced on return
3025  * depending on how much work the function had to do.
3026  *
3027  * (*parentp) must not be errored or this function will assert.
3028  *
3029  * (*chainp) usually starts out NULL and returns the newly created chain,
3030  * but if the caller desires the caller may allocate a disconnected chain
3031  * and pass it in instead.
3032  *
3033  * This function should NOT be used to insert INDIRECT blocks.  It is
3034  * typically used to create/insert inodes and data blocks.
3035  *
3036  * Caller must pass-in an exclusively locked parent the new chain is to
3037  * be inserted under, and optionally pass-in a disconnected, exclusively
3038  * locked chain to insert (else we create a new chain).  The function will
3039  * adjust (*parentp) as necessary, create or connect the chain, and
3040  * return an exclusively locked chain in *chainp.
3041  *
3042  * When creating a PFSROOT inode under the super-root, pmp is typically NULL
3043  * and will be reassigned.
3044  *
3045  * NOTE: returns HAMMER_ERROR_* flags
3046  */
3047 int
3048 hammer2_chain_create(hammer2_chain_t **parentp, hammer2_chain_t **chainp,
3049                      hammer2_pfs_t *pmp, int methods,
3050                      hammer2_key_t key, int keybits, int type, size_t bytes,
3051                      hammer2_tid_t mtid, hammer2_off_t dedup_off, int flags)
3052 {
3053         hammer2_dev_t *hmp;
3054         hammer2_chain_t *chain;
3055         hammer2_chain_t *parent;
3056         hammer2_blockref_t *base;
3057         hammer2_blockref_t dummy;
3058         int allocated = 0;
3059         int error = 0;
3060         int count;
3061         int maxloops = 300000;
3062
3063         /*
3064          * Topology may be crossing a PFS boundary.
3065          */
3066         parent = *parentp;
3067         KKASSERT(hammer2_mtx_owned(&parent->lock));
3068         KKASSERT(parent->error == 0);
3069         hmp = parent->hmp;
3070         chain = *chainp;
3071
3072         if (chain == NULL) {
3073                 /*
3074                  * First allocate media space and construct the dummy bref,
3075                  * then allocate the in-memory chain structure.  Set the
3076                  * INITIAL flag for fresh chains which do not have embedded
3077                  * data.
3078                  *
3079                  * XXX for now set the check mode of the child based on
3080                  *     the parent or, if the parent is an inode, the
3081                  *     specification in the inode.
3082                  */
3083                 bzero(&dummy, sizeof(dummy));
3084                 dummy.type = type;
3085                 dummy.key = key;
3086                 dummy.keybits = keybits;
3087                 dummy.data_off = hammer2_getradix(bytes);
3088
3089                 /*
3090                  * Inherit methods from parent by default.  Primarily used
3091                  * for BREF_TYPE_DATA.  Non-data types *must* be set to
3092                  * a non-NONE check algorithm.
3093                  */
3094                 if (methods == -1)
3095                         dummy.methods = parent->bref.methods;
3096                 else
3097                         dummy.methods = (uint8_t)methods;
3098
3099                 if (type != HAMMER2_BREF_TYPE_DATA &&
3100                     HAMMER2_DEC_CHECK(dummy.methods) == HAMMER2_CHECK_NONE) {
3101                         dummy.methods |=
3102                                 HAMMER2_ENC_CHECK(HAMMER2_CHECK_DEFAULT);
3103                 }
3104
3105                 chain = hammer2_chain_alloc(hmp, pmp, &dummy);
3106
3107                 /*
3108                  * Lock the chain manually, chain_lock will load the chain
3109                  * which we do NOT want to do.  (note: chain->refs is set
3110                  * to 1 by chain_alloc() for us, but lockcnt is not).
3111                  */
3112                 chain->lockcnt = 1;
3113                 hammer2_mtx_ex(&chain->lock);
3114                 allocated = 1;
3115                 ++curthread->td_tracker;
3116
3117                 /*
3118                  * Set INITIAL to optimize I/O.  The flag will generally be
3119                  * processed when we call hammer2_chain_modify().
3120                  *
3121                  * Recalculate bytes to reflect the actual media block
3122                  * allocation.  Handle special case radix 0 == 0 bytes.
3123                  */
3124                 bytes = (size_t)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
3125                 if (bytes)
3126                         bytes = (hammer2_off_t)1 << bytes;
3127                 chain->bytes = bytes;
3128
3129                 switch(type) {
3130                 case HAMMER2_BREF_TYPE_VOLUME:
3131                 case HAMMER2_BREF_TYPE_FREEMAP:
3132                         panic("hammer2_chain_create: called with volume type");
3133                         break;
3134                 case HAMMER2_BREF_TYPE_INDIRECT:
3135                         panic("hammer2_chain_create: cannot be used to"
3136                               "create indirect block");
3137                         break;
3138                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3139                         panic("hammer2_chain_create: cannot be used to"
3140                               "create freemap root or node");
3141                         break;
3142                 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3143                         KKASSERT(bytes == sizeof(chain->data->bmdata));
3144                         /* fall through */
3145                 case HAMMER2_BREF_TYPE_DIRENT:
3146                 case HAMMER2_BREF_TYPE_INODE:
3147                 case HAMMER2_BREF_TYPE_DATA:
3148                 default:
3149                         /*
3150                          * leave chain->data NULL, set INITIAL
3151                          */
3152                         KKASSERT(chain->data == NULL);
3153                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
3154                         break;
3155                 }
3156         } else {
3157                 /*
3158                  * We are reattaching a previously deleted chain, possibly
3159                  * under a new parent and possibly with a new key/keybits.
3160                  * The chain does not have to be in a modified state.  The
3161                  * UPDATE flag will be set later on in this routine.
3162                  *
3163                  * Do NOT mess with the current state of the INITIAL flag.
3164                  */
3165                 chain->bref.key = key;
3166                 chain->bref.keybits = keybits;
3167                 if (chain->flags & HAMMER2_CHAIN_DELETED)
3168                         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3169                 KKASSERT(chain->parent == NULL);
3170         }
3171         if (flags & HAMMER2_INSERT_PFSROOT)
3172                 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
3173         else
3174                 chain->bref.flags &= ~HAMMER2_BREF_FLAG_PFSROOT;
3175
3176         /*
3177          * Calculate how many entries we have in the blockref array and
3178          * determine if an indirect block is required.
3179          */
3180 again:
3181         if (--maxloops == 0)
3182                 panic("hammer2_chain_create: maxloops");
3183
3184         switch(parent->bref.type) {
3185         case HAMMER2_BREF_TYPE_INODE:
3186                 if ((parent->data->ipdata.meta.op_flags &
3187                      HAMMER2_OPFLAG_DIRECTDATA) != 0) {
3188                         kprintf("hammer2: parent set for direct-data! "
3189                                 "pkey=%016jx ckey=%016jx\n",
3190                                 parent->bref.key,
3191                                 chain->bref.key);
3192                 }
3193                 KKASSERT((parent->data->ipdata.meta.op_flags &
3194                           HAMMER2_OPFLAG_DIRECTDATA) == 0);
3195                 KKASSERT(parent->data != NULL);
3196                 base = &parent->data->ipdata.u.blockset.blockref[0];
3197                 count = HAMMER2_SET_COUNT;
3198                 break;
3199         case HAMMER2_BREF_TYPE_INDIRECT:
3200         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3201                 if (parent->flags & HAMMER2_CHAIN_INITIAL)
3202                         base = NULL;
3203                 else
3204                         base = &parent->data->npdata[0];
3205                 count = parent->bytes / sizeof(hammer2_blockref_t);
3206                 break;
3207         case HAMMER2_BREF_TYPE_VOLUME:
3208                 KKASSERT(parent->data != NULL);
3209                 base = &parent->data->voldata.sroot_blockset.blockref[0];
3210                 count = HAMMER2_SET_COUNT;
3211                 break;
3212         case HAMMER2_BREF_TYPE_FREEMAP:
3213                 KKASSERT(parent->data != NULL);
3214                 base = &parent->data->blkset.blockref[0];
3215                 count = HAMMER2_SET_COUNT;
3216                 break;
3217         default:
3218                 panic("hammer2_chain_create: unrecognized blockref type: %d",
3219                       parent->bref.type);
3220                 base = NULL;
3221                 count = 0;
3222                 break;
3223         }
3224
3225         /*
3226          * Make sure we've counted the brefs
3227          */
3228         if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
3229                 hammer2_chain_countbrefs(parent, base, count);
3230
3231         KASSERT(parent->core.live_count >= 0 &&
3232                 parent->core.live_count <= count,
3233                 ("bad live_count %d/%d (%02x, %d)",
3234                         parent->core.live_count, count,
3235                         parent->bref.type, parent->bytes));
3236
3237         /*
3238          * If no free blockref could be found we must create an indirect
3239          * block and move a number of blockrefs into it.  With the parent
3240          * locked we can safely lock each child in order to delete+duplicate
3241          * it without causing a deadlock.
3242          *
3243          * This may return the new indirect block or the old parent depending
3244          * on where the key falls.  NULL is returned on error.
3245          */
3246         if (parent->core.live_count == count) {
3247                 hammer2_chain_t *nparent;
3248
3249                 KKASSERT((flags & HAMMER2_INSERT_SAMEPARENT) == 0);
3250
3251                 nparent = hammer2_chain_create_indirect(parent, key, keybits,
3252                                                         mtid, type, &error);
3253                 if (nparent == NULL) {
3254                         if (allocated)
3255                                 hammer2_chain_drop(chain);
3256                         chain = NULL;
3257                         goto done;
3258                 }
3259                 if (parent != nparent) {
3260                         hammer2_chain_unlock(parent);
3261                         hammer2_chain_drop(parent);
3262                         parent = *parentp = nparent;
3263                 }
3264                 goto again;
3265         }
3266
3267         if (chain->flags & HAMMER2_CHAIN_DELETED)
3268                 kprintf("Inserting deleted chain @%016jx\n",
3269                         chain->bref.key);
3270
3271         /*
3272          * Link the chain into its parent.
3273          */
3274         if (chain->parent != NULL)
3275                 panic("hammer2: hammer2_chain_create: chain already connected");
3276         KKASSERT(chain->parent == NULL);
3277         KKASSERT(parent->core.live_count < count);
3278         hammer2_chain_insert(parent, chain,
3279                              HAMMER2_CHAIN_INSERT_SPIN |
3280                              HAMMER2_CHAIN_INSERT_LIVE,
3281                              0);
3282
3283         if (allocated) {
3284                 /*
3285                  * Mark the newly created chain modified.  This will cause
3286                  * UPDATE to be set and process the INITIAL flag.
3287                  *
3288                  * Device buffers are not instantiated for DATA elements
3289                  * as these are handled by logical buffers.
3290                  *
3291                  * Indirect and freemap node indirect blocks are handled
3292                  * by hammer2_chain_create_indirect() and not by this
3293                  * function.
3294                  *
3295                  * Data for all other bref types is expected to be
3296                  * instantiated (INODE, LEAF).
3297                  */
3298                 switch(chain->bref.type) {
3299                 case HAMMER2_BREF_TYPE_DATA:
3300                 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3301                 case HAMMER2_BREF_TYPE_DIRENT:
3302                 case HAMMER2_BREF_TYPE_INODE:
3303                         error = hammer2_chain_modify(chain, mtid, dedup_off,
3304                                                      HAMMER2_MODIFY_OPTDATA);
3305                         break;
3306                 default:
3307                         /*
3308                          * Remaining types are not supported by this function.
3309                          * In particular, INDIRECT and LEAF_NODE types are
3310                          * handled by create_indirect().
3311                          */
3312                         panic("hammer2_chain_create: bad type: %d",
3313                               chain->bref.type);
3314                         /* NOT REACHED */
3315                         break;
3316                 }
3317         } else {
3318                 /*
3319                  * When reconnecting a chain we must set UPDATE and
3320                  * setflush so the flush recognizes that it must update
3321                  * the bref in the parent.
3322                  */
3323                 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0)
3324                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
3325         }
3326
3327         /*
3328          * We must setflush(parent) to ensure that it recurses through to
3329          * chain.  setflush(chain) might not work because ONFLUSH is possibly
3330          * already set in the chain (so it won't recurse up to set it in the
3331          * parent).
3332          */
3333         hammer2_chain_setflush(parent);
3334
3335 done:
3336         *chainp = chain;
3337
3338         return (error);
3339 }
3340
3341 /*
3342  * Move the chain from its old parent to a new parent.  The chain must have
3343  * already been deleted or already disconnected (or never associated) with
3344  * a parent.  The chain is reassociated with the new parent and the deleted
3345  * flag will be cleared (no longer deleted).  The chain's modification state
3346  * is not altered.
3347  *
3348  * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION
3349  * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
3350  * FULL.  This typically means that the caller is creating the chain after
3351  * doing a hammer2_chain_lookup().
3352  *
3353  * A non-NULL bref is typically passed when key and keybits must be overridden.
3354  * Note that hammer2_cluster_duplicate() *ONLY* uses the key and keybits fields
3355  * from a passed-in bref and uses the old chain's bref for everything else.
3356  *
3357  * Neither (parent) or (chain) can be errored.
3358  *
3359  * If (parent) is non-NULL then the chain is inserted under the parent.
3360  *
3361  * If (parent) is NULL then the newly duplicated chain is not inserted
3362  * anywhere, similar to if it had just been chain_alloc()'d (suitable for
3363  * passing into hammer2_chain_create() after this function returns).
3364  *
3365  * WARNING! This function calls create which means it can insert indirect
3366  *          blocks.  This can cause other unrelated chains in the parent to
3367  *          be moved to a newly inserted indirect block in addition to the
3368  *          specific chain.
3369  */
3370 void
3371 hammer2_chain_rename(hammer2_blockref_t *bref,
3372                      hammer2_chain_t **parentp, hammer2_chain_t *chain,
3373                      hammer2_tid_t mtid, int flags)
3374 {
3375         hammer2_dev_t *hmp;
3376         hammer2_chain_t *parent;
3377         size_t bytes;
3378
3379         /*
3380          * WARNING!  We should never resolve DATA to device buffers
3381          *           (XXX allow it if the caller did?), and since
3382          *           we currently do not have the logical buffer cache
3383          *           buffer in-hand to fix its cached physical offset
3384          *           we also force the modify code to not COW it. XXX
3385          */
3386         hmp = chain->hmp;
3387         KKASSERT(chain->parent == NULL);
3388         KKASSERT(chain->error == 0);
3389
3390         /*
3391          * Now create a duplicate of the chain structure, associating
3392          * it with the same core, making it the same size, pointing it
3393          * to the same bref (the same media block).
3394          *
3395          * NOTE: Handle special radix == 0 case (means 0 bytes).
3396          */
3397         if (bref == NULL)
3398                 bref = &chain->bref;
3399         bytes = (size_t)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
3400         if (bytes)
3401                 bytes = (hammer2_off_t)1 << bytes;
3402
3403         /*
3404          * If parent is not NULL the duplicated chain will be entered under
3405          * the parent and the UPDATE bit set to tell flush to update
3406          * the blockref.
3407          *
3408          * We must setflush(parent) to ensure that it recurses through to
3409          * chain.  setflush(chain) might not work because ONFLUSH is possibly
3410          * already set in the chain (so it won't recurse up to set it in the
3411          * parent).
3412          *
3413          * Having both chains locked is extremely important for atomicy.
3414          */
3415         if (parentp && (parent = *parentp) != NULL) {
3416                 KKASSERT(hammer2_mtx_owned(&parent->lock));
3417                 KKASSERT(parent->refs > 0);
3418                 KKASSERT(parent->error == 0);
3419
3420                 hammer2_chain_create(parentp, &chain,
3421                                      chain->pmp, HAMMER2_METH_DEFAULT,
3422                                      bref->key, bref->keybits, bref->type,
3423                                      chain->bytes, mtid, 0, flags);
3424                 KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE);
3425                 hammer2_chain_setflush(*parentp);
3426         }
3427 }
3428
3429 /*
3430  * Helper function for deleting chains.
3431  *
3432  * The chain is removed from the live view (the RBTREE) as well as the parent's
3433  * blockmap.  Both chain and its parent must be locked.
3434  *
3435  * parent may not be errored.  chain can be errored.
3436  */
3437 static int
3438 _hammer2_chain_delete_helper(hammer2_chain_t *parent, hammer2_chain_t *chain,
3439                              hammer2_tid_t mtid, int flags)
3440 {
3441         hammer2_dev_t *hmp;
3442         int error = 0;
3443
3444         KKASSERT((chain->flags & (HAMMER2_CHAIN_DELETED |
3445                                   HAMMER2_CHAIN_FICTITIOUS)) == 0);
3446         KKASSERT(chain->parent == parent);
3447         hmp = chain->hmp;
3448
3449         if (chain->flags & HAMMER2_CHAIN_BMAPPED) {
3450                 /*
3451                  * Chain is blockmapped, so there must be a parent.
3452                  * Atomically remove the chain from the parent and remove
3453                  * the blockmap entry.  The parent must be set modified
3454                  * to remove the blockmap entry.
3455                  */
3456                 hammer2_blockref_t *base;
3457                 int count;
3458
3459                 KKASSERT(parent != NULL);
3460                 KKASSERT(parent->error == 0);
3461                 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
3462                 error = hammer2_chain_modify(parent, mtid, 0, 0);
3463                 if (error)
3464                         goto done;
3465
3466                 /*
3467                  * Calculate blockmap pointer
3468                  */
3469                 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
3470                 hammer2_spin_ex(&chain->core.spin);
3471                 hammer2_spin_ex(&parent->core.spin);
3472
3473                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3474                 atomic_add_int(&parent->core.live_count, -1);
3475                 ++parent->core.generation;
3476                 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
3477                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
3478                 --parent->core.chain_count;
3479                 chain->parent = NULL;
3480
3481                 switch(parent->bref.type) {
3482                 case HAMMER2_BREF_TYPE_INODE:
3483                         /*
3484                          * Access the inode's block array.  However, there
3485                          * is no block array if the inode is flagged
3486                          * DIRECTDATA.
3487                          */
3488                         if (parent->data &&
3489                             (parent->data->ipdata.meta.op_flags &
3490                              HAMMER2_OPFLAG_DIRECTDATA) == 0) {
3491                                 base =
3492                                    &parent->data->ipdata.u.blockset.blockref[0];
3493                         } else {
3494                                 base = NULL;
3495                         }
3496                         count = HAMMER2_SET_COUNT;
3497                         break;
3498                 case HAMMER2_BREF_TYPE_INDIRECT:
3499                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3500                         if (parent->data)
3501                                 base = &parent->data->npdata[0];
3502                         else
3503                                 base = NULL;
3504                         count = parent->bytes / sizeof(hammer2_blockref_t);
3505                         break;
3506                 case HAMMER2_BREF_TYPE_VOLUME:
3507                         base = &parent->data->voldata.
3508                                         sroot_blockset.blockref[0];
3509                         count = HAMMER2_SET_COUNT;
3510                         break;
3511                 case HAMMER2_BREF_TYPE_FREEMAP:
3512                         base = &parent->data->blkset.blockref[0];
3513                         count = HAMMER2_SET_COUNT;
3514                         break;
3515                 default:
3516                         base = NULL;
3517                         count = 0;
3518                         panic("hammer2_flush_pass2: "
3519                               "unrecognized blockref type: %d",
3520                               parent->bref.type);
3521                 }
3522
3523                 /*
3524                  * delete blockmapped chain from its parent.
3525                  *
3526                  * The parent is not affected by any statistics in chain
3527                  * which are pending synchronization.  That is, there is
3528                  * nothing to undo in the parent since they have not yet
3529                  * been incorporated into the parent.
3530                  *
3531                  * The parent is affected by statistics stored in inodes.
3532                  * Those have already been synchronized, so they must be
3533                  * undone.  XXX split update possible w/delete in middle?
3534                  */
3535                 if (base) {
3536                         hammer2_base_delete(parent, base, count, chain);
3537                 }
3538                 hammer2_spin_unex(&parent->core.spin);
3539                 hammer2_spin_unex(&chain->core.spin);
3540         } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
3541                 /*
3542                  * Chain is not blockmapped but a parent is present.
3543                  * Atomically remove the chain from the parent.  There is
3544                  * no blockmap entry to remove.
3545                  *
3546                  * Because chain was associated with a parent but not
3547                  * synchronized, the chain's *_count_up fields contain
3548                  * inode adjustment statistics which must be undone.
3549                  */
3550                 hammer2_spin_ex(&chain->core.spin);
3551                 hammer2_spin_ex(&parent->core.spin);
3552                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3553                 atomic_add_int(&parent->core.live_count, -1);
3554                 ++parent->core.generation;
3555                 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
3556                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
3557                 --parent->core.chain_count;
3558                 chain->parent = NULL;
3559                 hammer2_spin_unex(&parent->core.spin);
3560                 hammer2_spin_unex(&chain->core.spin);
3561         } else {
3562                 /*
3563                  * Chain is not blockmapped and has no parent.  This
3564                  * is a degenerate case.
3565                  */
3566                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
3567         }
3568 done:
3569         return error;
3570 }
3571
3572 /*
3573  * Create an indirect block that covers one or more of the elements in the
3574  * current parent.  Either returns the existing parent with no locking or
3575  * ref changes or returns the new indirect block locked and referenced
3576  * and leaving the original parent lock/ref intact as well.
3577  *
3578  * If an error occurs, NULL is returned and *errorp is set to the H2 error.
3579  *
3580  * The returned chain depends on where the specified key falls.
3581  *
3582  * The key/keybits for the indirect mode only needs to follow three rules:
3583  *
3584  * (1) That all elements underneath it fit within its key space and
3585  *
3586  * (2) That all elements outside it are outside its key space.
3587  *
3588  * (3) When creating the new indirect block any elements in the current
3589  *     parent that fit within the new indirect block's keyspace must be
3590  *     moved into the new indirect block.
3591  *
3592  * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
3593  *     keyspace the the current parent, but lookup/iteration rules will
3594  *     ensure (and must ensure) that rule (2) for all parents leading up
3595  *     to the nearest inode or the root volume header is adhered to.  This
3596  *     is accomplished by always recursing through matching keyspaces in
3597  *     the hammer2_chain_lookup() and hammer2_chain_next() API.
3598  *
3599  * The current implementation calculates the current worst-case keyspace by
3600  * iterating the current parent and then divides it into two halves, choosing
3601  * whichever half has the most elements (not necessarily the half containing
3602  * the requested key).
3603  *
3604  * We can also opt to use the half with the least number of elements.  This
3605  * causes lower-numbered keys (aka logical file offsets) to recurse through
3606  * fewer indirect blocks and higher-numbered keys to recurse through more.
3607  * This also has the risk of not moving enough elements to the new indirect
3608  * block and being forced to create several indirect blocks before the element
3609  * can be inserted.
3610  *
3611  * Must be called with an exclusively locked parent.
3612  *
3613  * NOTE: *errorp set to HAMMER_ERROR_* flags
3614  */
3615 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
3616                                 hammer2_key_t *keyp, int keybits,
3617                                 hammer2_blockref_t *base, int count);
3618 static int hammer2_chain_indkey_file(hammer2_chain_t *parent,
3619                                 hammer2_key_t *keyp, int keybits,
3620                                 hammer2_blockref_t *base, int count,
3621                                 int ncount);
3622 static int hammer2_chain_indkey_dir(hammer2_chain_t *parent,
3623                                 hammer2_key_t *keyp, int keybits,
3624                                 hammer2_blockref_t *base, int count,
3625                                 int ncount);
3626 static
3627 hammer2_chain_t *
3628 hammer2_chain_create_indirect(hammer2_chain_t *parent,
3629                               hammer2_key_t create_key, int create_bits,
3630                               hammer2_tid_t mtid, int for_type, int *errorp)
3631 {
3632         hammer2_dev_t *hmp;
3633         hammer2_blockref_t *base;
3634         hammer2_blockref_t *bref;
3635         hammer2_blockref_t bcopy;
3636         hammer2_chain_t *chain;
3637         hammer2_chain_t *ichain;
3638         hammer2_chain_t dummy;
3639         hammer2_key_t key = create_key;
3640         hammer2_key_t key_beg;
3641         hammer2_key_t key_end;
3642         hammer2_key_t key_next;
3643         int keybits = create_bits;
3644         int count;
3645         int ncount;
3646         int nbytes;
3647         int loops;
3648         int error;
3649         int reason;
3650         int generation;
3651         int maxloops = 300000;
3652
3653         /*
3654          * Calculate the base blockref pointer or NULL if the chain
3655          * is known to be empty.  We need to calculate the array count
3656          * for RB lookups either way.
3657          */
3658         hmp = parent->hmp;
3659         KKASSERT(hammer2_mtx_owned(&parent->lock));
3660
3661         /*
3662          * Pre-modify the parent now to avoid having to deal with error
3663          * processing if we tried to later (in the middle of our loop).
3664          */
3665         *errorp = hammer2_chain_modify(parent, mtid, 0, 0);
3666         if (*errorp) {
3667                 kprintf("hammer2_create_indirect: error %08x %s\n",
3668                         *errorp, hammer2_error_str(*errorp));
3669                 return NULL;
3670         }
3671
3672         /*hammer2_chain_modify(&parent, HAMMER2_MODIFY_OPTDATA);*/
3673         base = hammer2_chain_base_and_count(parent, &count);
3674
3675         /*
3676          * dummy used in later chain allocation (no longer used for lookups).
3677          */
3678         bzero(&dummy, sizeof(dummy));
3679
3680         /*
3681          * How big should our new indirect block be?  It has to be at least
3682          * as large as its parent for splits to work properly.
3683          *
3684          * The freemap uses a specific indirect block size.  The number of
3685          * levels are built dynamically and ultimately depend on the size
3686          * volume.  Because freemap blocks are taken from the reserved areas
3687          * of the volume our goal is efficiency (fewer levels) and not so
3688          * much to save disk space.
3689          *
3690          * The first indirect block level for a directory usually uses
3691          * HAMMER2_IND_BYTES_MIN (4KB = 32 directory entries).  Due to
3692          * the hash mechanism, this typically gives us a nominal
3693          * 32 * 4 entries with one level of indirection.
3694          *
3695          * We use HAMMER2_IND_BYTES_NOM (16KB = 128 blockrefs) for FILE
3696          * indirect blocks.  The initial 4 entries in the inode gives us
3697          * 256KB.  Up to 4 indirect blocks gives us 32MB.  Three levels
3698          * of indirection gives us 137GB, and so forth.  H2 can support
3699          * huge file sizes but they are not typical, so we try to stick
3700          * with compactness and do not use a larger indirect block size.
3701          *
3702          * We could use 64KB (PBUFSIZE), giving us 512 blockrefs, but
3703          * due to the way indirect blocks are created this usually winds
3704          * up being extremely inefficient for small files.  Even though
3705          * 16KB requires more levels of indirection for very large files,
3706          * the 16KB records can be ganged together into 64KB DIOs.
3707          */
3708         if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
3709             for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
3710                 nbytes = HAMMER2_FREEMAP_LEVELN_PSIZE;
3711         } else if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
3712                 if (parent->data->ipdata.meta.type ==
3713                     HAMMER2_OBJTYPE_DIRECTORY)
3714                         nbytes = HAMMER2_IND_BYTES_MIN; /* 4KB = 32 entries */
3715                 else
3716                         nbytes = HAMMER2_IND_BYTES_NOM; /* 16KB = ~8MB file */
3717
3718         } else {
3719                 nbytes = HAMMER2_IND_BYTES_NOM;
3720         }
3721         if (nbytes < count * sizeof(hammer2_blockref_t)) {
3722                 KKASSERT(for_type != HAMMER2_BREF_TYPE_FREEMAP_NODE &&
3723                          for_type != HAMMER2_BREF_TYPE_FREEMAP_LEAF);
3724                 nbytes = count * sizeof(hammer2_blockref_t);
3725         }
3726         ncount = nbytes / sizeof(hammer2_blockref_t);
3727
3728         /*
3729          * When creating an indirect block for a freemap node or leaf
3730          * the key/keybits must be fitted to static radix levels because
3731          * particular radix levels use particular reserved blocks in the
3732          * related zone.
3733          *
3734          * This routine calculates the key/radix of the indirect block
3735          * we need to create, and whether it is on the high-side or the
3736          * low-side.
3737          */
3738         switch(for_type) {
3739         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3740         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3741                 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
3742                                                        base, count);
3743                 break;
3744         case HAMMER2_BREF_TYPE_DATA:
3745                 keybits = hammer2_chain_indkey_file(parent, &key, keybits,
3746                                                     base, count, ncount);
3747                 break;
3748         case HAMMER2_BREF_TYPE_DIRENT:
3749         case HAMMER2_BREF_TYPE_INODE:
3750                 keybits = hammer2_chain_indkey_dir(parent, &key, keybits,
3751                                                    base, count, ncount);
3752                 break;
3753         default:
3754                 panic("illegal indirect block for bref type %d", for_type);
3755                 break;
3756         }
3757
3758         /*
3759          * Normalize the key for the radix being represented, keeping the
3760          * high bits and throwing away the low bits.
3761          */
3762         key &= ~(((hammer2_key_t)1 << keybits) - 1);
3763
3764         /*
3765          * Ok, create our new indirect block
3766          */
3767         if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
3768             for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
3769                 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
3770         } else {
3771                 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
3772         }
3773         dummy.bref.key = key;
3774         dummy.bref.keybits = keybits;
3775         dummy.bref.data_off = hammer2_getradix(nbytes);
3776         dummy.bref.methods =
3777                 HAMMER2_ENC_CHECK(HAMMER2_DEC_CHECK(parent->bref.methods)) |
3778                 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
3779
3780         ichain = hammer2_chain_alloc(hmp, parent->pmp, &dummy.bref);
3781         atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
3782         hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
3783         /* ichain has one ref at this point */
3784
3785         /*
3786          * We have to mark it modified to allocate its block, but use
3787          * OPTDATA to allow it to remain in the INITIAL state.  Otherwise
3788          * it won't be acted upon by the flush code.
3789          */
3790         *errorp = hammer2_chain_modify(ichain, mtid, 0, HAMMER2_MODIFY_OPTDATA);
3791         if (*errorp) {
3792                 kprintf("hammer2_alloc_indirect: error %08x %s\n",
3793                         *errorp, hammer2_error_str(*errorp));
3794                 hammer2_chain_unlock(ichain);
3795                 hammer2_chain_drop(ichain);
3796                 return NULL;
3797         }
3798
3799         /*
3800          * Iterate the original parent and move the matching brefs into
3801          * the new indirect block.
3802          *
3803          * XXX handle flushes.
3804          */
3805         key_beg = 0;
3806         key_end = HAMMER2_KEY_MAX;
3807         key_next = 0;   /* avoid gcc warnings */
3808         hammer2_spin_ex(&parent->core.spin);
3809         loops = 0;
3810         reason = 0;
3811
3812         for (;;) {
3813                 /*
3814                  * Parent may have been modified, relocating its block array.
3815                  * Reload the base pointer.
3816                  */
3817                 base = hammer2_chain_base_and_count(parent, &count);
3818
3819                 if (++loops > 100000) {
3820                     hammer2_spin_unex(&parent->core.spin);
3821                     panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
3822                           reason, parent, base, count, key_next);
3823                 }
3824
3825                 /*
3826                  * NOTE: spinlock stays intact, returned chain (if not NULL)
3827                  *       is not referenced or locked which means that we
3828                  *       cannot safely check its flagged / deletion status
3829                  *       until we lock it.
3830                  */
3831                 chain = hammer2_combined_find(parent, base, count,
3832                                               &key_next,
3833                                               key_beg, key_end,
3834                                               &bref);
3835                 generation = parent->core.generation;
3836                 if (bref == NULL)
3837                         break;
3838                 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3839
3840                 /*
3841                  * Skip keys that are not within the key/radix of the new
3842                  * indirect block.  They stay in the parent.
3843                  */
3844                 if ((~(((hammer2_key_t)1 << keybits) - 1) &
3845                     (key ^ bref->key)) != 0) {
3846                         goto next_key_spinlocked;
3847                 }
3848
3849                 /*
3850                  * Load the new indirect block by acquiring the related
3851                  * chains (potentially from media as it might not be
3852                  * in-memory).  Then move it to the new parent (ichain).
3853                  *
3854                  * chain is referenced but not locked.  We must lock the
3855                  * chain to obtain definitive state.
3856                  */
3857                 if (chain) {
3858                         /*
3859                          * Use chain already present in the RBTREE
3860                          */
3861                         hammer2_chain_ref(chain);
3862                         hammer2_spin_unex(&parent->core.spin);
3863                         hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
3864                 } else {
3865                         /*
3866                          * Get chain for blockref element.  _get returns NULL
3867                          * on insertion race.
3868                          */
3869                         bcopy = *bref;
3870                         hammer2_spin_unex(&parent->core.spin);
3871                         chain = hammer2_chain_get(parent, generation, &bcopy);
3872                         if (chain == NULL) {
3873                                 reason = 1;
3874                                 hammer2_spin_ex(&parent->core.spin);
3875                                 continue;
3876                         }
3877                         hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER);
3878                         if (bcmp(&bcopy, bref, sizeof(bcopy))) {
3879                                 reason = 2;
3880                                 hammer2_chain_unlock(chain);
3881                                 hammer2_chain_drop(chain);
3882                                 hammer2_spin_ex(&parent->core.spin);
3883                                 continue;
3884                         }
3885                 }
3886
3887                 /*
3888                  * This is always live so if the chain has been deleted
3889                  * we raced someone and we have to retry.
3890                  *
3891                  * NOTE: Lookups can race delete-duplicate because
3892                  *       delete-duplicate does not lock the parent's core
3893                  *       (they just use the spinlock on the core).
3894                  *
3895                  *       (note reversed logic for this one)
3896                  */
3897                 if (chain->parent != parent ||
3898                     (chain->flags & HAMMER2_CHAIN_DELETED)) {
3899                         hammer2_chain_unlock(chain);
3900                         hammer2_chain_drop(chain);
3901                         kprintf("hammer2_chain_create_indirect "
3902                                 "RETRY (%p,%p)->%p %08x\n",
3903                                 parent, chain->parent, chain, chain->flags);
3904                         hammer2_spin_ex(&parent->core.spin);
3905                         continue;
3906                 }
3907
3908                 /*
3909                  * Shift the chain to the indirect block.
3910                  *
3911                  * WARNING! No reason for us to load chain data, pass NOSTATS
3912                  *          to prevent delete/insert from trying to access
3913                  *          inode stats (and thus asserting if there is no
3914                  *          chain->data loaded).
3915                  *
3916                  * WARNING! The (parent, chain) deletion may modify the parent
3917                  *          and invalidate the base pointer.
3918                  *
3919                  * WARNING! Parent must already be marked modified, so we
3920                  *          can assume that chain_delete always suceeds.
3921                  */
3922                 error = hammer2_chain_delete(parent, chain, mtid, 0);
3923                 KKASSERT(error == 0);
3924                 hammer2_chain_rename(NULL, &ichain, chain, mtid, 0);
3925                 hammer2_chain_unlock(chain);
3926                 hammer2_chain_drop(chain);
3927                 KKASSERT(parent->refs > 0);
3928                 chain = NULL;
3929                 base = NULL;    /* safety */
3930                 hammer2_spin_ex(&parent->core.spin);
3931 next_key_spinlocked:
3932                 if (--maxloops == 0)
3933                         panic("hammer2_chain_create_indirect: maxloops");
3934                 reason = 4;
3935                 if (key_next == 0 || key_next > key_end)
3936                         break;
3937                 key_beg = key_next;
3938                 /* loop */
3939         }
3940         hammer2_spin_unex(&parent->core.spin);
3941
3942         /*
3943          * Insert the new indirect block into the parent now that we've
3944          * cleared out some entries in the parent.  We calculated a good
3945          * insertion index in the loop above (ichain->index).
3946          *
3947          * We don't have to set UPDATE here because we mark ichain
3948          * modified down below (so the normal modified -> flush -> set-moved
3949          * sequence applies).
3950          *
3951          * The insertion shouldn't race as this is a completely new block
3952          * and the parent is locked.
3953          */
3954         base = NULL;    /* safety, parent modify may change address */
3955         KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
3956         KKASSERT(parent->core.live_count < count);
3957         hammer2_chain_insert(parent, ichain,
3958                              HAMMER2_CHAIN_INSERT_SPIN |
3959                              HAMMER2_CHAIN_INSERT_LIVE,
3960                              0);
3961
3962         /*
3963          * Make sure flushes propogate after our manual insertion.
3964          */
3965         hammer2_chain_setflush(ichain);
3966         hammer2_chain_setflush(parent);
3967
3968         /*
3969          * Figure out what to return.
3970          */
3971         if (~(((hammer2_key_t)1 << keybits) - 1) &
3972                    (create_key ^ key)) {
3973                 /*
3974                  * Key being created is outside the key range,
3975                  * return the original parent.
3976                  */
3977                 hammer2_chain_unlock(ichain);
3978                 hammer2_chain_drop(ichain);
3979         } else {
3980                 /*
3981                  * Otherwise its in the range, return the new parent.
3982                  * (leave both the new and old parent locked).
3983                  */
3984                 parent = ichain;
3985         }
3986
3987         return(parent);
3988 }
3989
3990 /*
3991  * Do maintenance on an indirect chain.  Both parent and chain are locked.
3992  *
3993  * Returns non-zero if (chain) is deleted, either due to being empty or
3994  * because its children were safely moved into the parent.
3995  */
3996 int
3997 hammer2_chain_indirect_maintenance(hammer2_chain_t *parent,
3998                                    hammer2_chain_t *chain)
3999 {
4000         hammer2_blockref_t *chain_base;
4001         hammer2_blockref_t *base;
4002         hammer2_blockref_t *bref;
4003         hammer2_blockref_t bcopy;
4004         hammer2_key_t key_next;
4005         hammer2_key_t key_beg;
4006         hammer2_key_t key_end;
4007         hammer2_chain_t *sub;
4008         int chain_count;
4009         int count;
4010         int generation;
4011
4012         /*
4013          * Make sure we have an accurate live_count
4014          */
4015         if ((chain->flags & (HAMMER2_CHAIN_INITIAL |
4016                              HAMMER2_CHAIN_COUNTEDBREFS)) == 0) {
4017                 base = &chain->data->npdata[0];
4018                 count = chain->bytes / sizeof(hammer2_blockref_t);
4019                 hammer2_chain_countbrefs(chain, base, count);
4020         }
4021
4022         /*
4023          * If the indirect block is empty we can delete it.
4024          */
4025         if (chain->core.live_count == 0 && RB_EMPTY(&chain->core.rbtree)) {
4026                 hammer2_chain_delete(parent, chain,
4027                                      chain->bref.modify_tid,
4028                                      HAMMER2_DELETE_PERMANENT);
4029                 return 1;
4030         }
4031
4032         base = hammer2_chain_base_and_count(parent, &count);
4033
4034         if ((parent->flags & (HAMMER2_CHAIN_INITIAL |
4035                              HAMMER2_CHAIN_COUNTEDBREFS)) == 0) {
4036                 hammer2_chain_countbrefs(parent, base, count);
4037         }
4038
4039         /*
4040          * Determine if we can collapse chain into parent, calculate
4041          * hysteresis for chain emptiness.
4042          */
4043         if (parent->core.live_count + chain->core.live_count - 1 > count)
4044                 return 0;
4045         chain_count = chain->bytes / sizeof(hammer2_blockref_t);
4046         if (chain->core.live_count > chain_count * 3 / 4)
4047                 return 0;
4048
4049         /*
4050          * Ok, theoretically we can collapse chain's contents into
4051          * parent.  chain is locked, but any in-memory children of chain
4052          * are not.  For this to work, we must be able to dispose of any
4053          * in-memory children of chain.
4054          *
4055          * For now require that there are no in-memory children of chain.
4056          *
4057          * WARNING! Both chain and parent must remain locked across this
4058          *          entire operation.
4059          */
4060
4061         /*
4062          * Parent must be marked modified.  Don't try to collapse it if we
4063          * can't mark it modified.  Once modified, destroy chain to make room
4064          * and to get rid of what will be a conflicting key (this is included
4065          * in the calculation above).  Finally, move the children of chain
4066          * into chain's parent.
4067          *
4068          * This order creates an accounting problem for bref.embed.stats
4069          * because we destroy chain before we remove its children.  Any
4070          * elements whos blockref is already synchronized will be counted
4071          * twice.  To deal with the problem we clean out chain's stats prior
4072          * to deleting it.
4073          */
4074         if (hammer2_chain_modify(parent, 0, 0, 0)) {
4075                 return 0;
4076         }
4077
4078         chain->bref.embed.stats.inode_count = 0;
4079         chain->bref.embed.stats.data_count = 0;
4080         hammer2_chain_delete(parent, chain,
4081                              chain->bref.modify_tid,
4082                              HAMMER2_DELETE_PERMANENT);
4083
4084         /*
4085          * The combined_find call requires core.spin to be held.  One would
4086          * think there wouldn't be any conflicts since we hold chain
4087          * exclusively locked, but the caching mechanism for 0-ref children
4088          * does not require a chain lock.
4089          */
4090         hammer2_spin_ex(&chain->core.spin);
4091
4092         key_next = 0;
4093         key_beg = 0;
4094         key_end = HAMMER2_KEY_MAX;
4095         for (;;) {
4096                 chain_base = &chain->data->npdata[0];
4097                 chain_count = chain->bytes / sizeof(hammer2_blockref_t);
4098                 sub = hammer2_combined_find(chain, chain_base, chain_count,
4099                                             &key_next,
4100                                             key_beg, key_end,
4101                                             &bref);
4102                 generation = chain->core.generation;
4103                 if (bref == NULL)
4104                         break;
4105                 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4106
4107                 if (sub) {
4108                         hammer2_chain_ref(sub);
4109                         hammer2_spin_unex(&chain->core.spin);
4110                         hammer2_chain_lock(sub, HAMMER2_RESOLVE_NEVER);
4111                         if (sub->parent != chain ||
4112                             (sub->flags & HAMMER2_CHAIN_DELETED)) {
4113                                 hammer2_chain_unlock(sub);
4114                                 hammer2_chain_drop(sub);
4115                                 hammer2_spin_ex(&chain->core.spin);
4116                                 continue;
4117                         }
4118                 } else {
4119                         bcopy = *bref;
4120                         hammer2_spin_unex(&chain->core.spin);
4121                         sub = hammer2_chain_get(chain, generation, &bcopy);
4122                         if (sub == NULL) {
4123                                 hammer2_spin_ex(&chain->core.spin);
4124                                 continue;
4125                         }
4126                         hammer2_chain_lock(sub, HAMMER2_RESOLVE_NEVER);
4127                         if (bcmp(&bcopy, bref, sizeof(bcopy)) != 0) {
4128                                 hammer2_chain_unlock(sub);
4129                                 hammer2_chain_drop(sub);
4130                                 hammer2_spin_ex(&chain->core.spin);
4131                                 continue;
4132                         }
4133                 }
4134                 hammer2_chain_delete(chain, sub,
4135                                      sub->bref.modify_tid, 0);
4136                 hammer2_chain_rename(NULL, &parent, sub,
4137                                      sub->bref.modify_tid,
4138                                      HAMMER2_INSERT_SAMEPARENT);
4139                 hammer2_chain_unlock(sub);
4140                 hammer2_chain_drop(sub);
4141                 hammer2_spin_ex(&chain->core.spin);
4142
4143                 if (key_next == 0)
4144                         break;
4145                 key_beg = key_next;
4146         }
4147         hammer2_spin_unex(&chain->core.spin);
4148
4149         return 1;
4150 }
4151
4152 /*
4153  * Freemap indirect blocks
4154  *
4155  * Calculate the keybits and highside/lowside of the freemap node the
4156  * caller is creating.
4157  *
4158  * This routine will specify the next higher-level freemap key/radix
4159  * representing the lowest-ordered set.  By doing so, eventually all
4160  * low-ordered sets will be moved one level down.
4161  *
4162  * We have to be careful here because the freemap reserves a limited
4163  * number of blocks for a limited number of levels.  So we can't just
4164  * push indiscriminately.
4165  */
4166 int
4167 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
4168                              int keybits, hammer2_blockref_t *base, int count)
4169 {
4170         hammer2_chain_t *chain;
4171         hammer2_blockref_t *bref;
4172         hammer2_key_t key;
4173         hammer2_key_t key_beg;
4174         hammer2_key_t key_end;
4175         hammer2_key_t key_next;
4176         int locount;
4177         int hicount;
4178         int maxloops = 300000;
4179
4180         key = *keyp;
4181         locount = 0;
4182         hicount = 0;
4183         keybits = 64;
4184
4185         /*
4186          * Calculate the range of keys in the array being careful to skip
4187          * slots which are overridden with a deletion.
4188          */
4189         key_beg = 0;
4190         key_end = HAMMER2_KEY_MAX;
4191         hammer2_spin_ex(&parent->core.spin);
4192
4193         for (;;) {
4194                 if (--maxloops == 0) {
4195                         panic("indkey_freemap shit %p %p:%d\n",
4196                               parent, base, count);
4197                 }
4198                 chain = hammer2_combined_find(parent, base, count,
4199                                               &key_next,
4200                                               key_beg, key_end,
4201                                               &bref);
4202
4203                 /*
4204                  * Exhausted search
4205                  */
4206                 if (bref == NULL)
4207                         break;
4208
4209                 /*
4210                  * Skip deleted chains.
4211                  */
4212                 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4213                         if (key_next == 0 || key_next > key_end)
4214                                 break;
4215                         key_beg = key_next;
4216                         continue;
4217                 }
4218
4219                 /*
4220                  * Use the full live (not deleted) element for the scan
4221                  * iteration.  HAMMER2 does not allow partial replacements.
4222                  *
4223                  * XXX should be built into hammer2_combined_find().
4224                  */
4225                 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4226
4227                 if (keybits > bref->keybits) {
4228                         key = bref->key;
4229                         keybits = bref->keybits;
4230                 } else if (keybits == bref->keybits && bref->key < key) {
4231                         key = bref->key;
4232                 }
4233                 if (key_next == 0)
4234                         break;
4235                 key_beg = key_next;
4236         }
4237         hammer2_spin_unex(&parent->core.spin);
4238
4239         /*
4240          * Return the keybits for a higher-level FREEMAP_NODE covering
4241          * this node.
4242          */
4243         switch(keybits) {
4244         case HAMMER2_FREEMAP_LEVEL0_RADIX:
4245                 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
4246                 break;
4247         case HAMMER2_FREEMAP_LEVEL1_RADIX:
4248                 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
4249                 break;
4250         case HAMMER2_FREEMAP_LEVEL2_RADIX:
4251                 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
4252                 break;
4253         case HAMMER2_FREEMAP_LEVEL3_RADIX:
4254                 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
4255                 break;
4256         case HAMMER2_FREEMAP_LEVEL4_RADIX:
4257                 keybits = HAMMER2_FREEMAP_LEVEL5_RADIX;
4258                 break;
4259         case HAMMER2_FREEMAP_LEVEL5_RADIX:
4260                 panic("hammer2_chain_indkey_freemap: level too high");
4261                 break;
4262         default:
4263                 panic("hammer2_chain_indkey_freemap: bad radix");
4264                 break;
4265         }
4266         *keyp = key;
4267
4268         return (keybits);
4269 }
4270
4271 /*
4272  * File indirect blocks
4273  *
4274  * Calculate the key/keybits for the indirect block to create by scanning
4275  * existing keys.  The key being created is also passed in *keyp and can be
4276  * inside or outside the indirect block.  Regardless, the indirect block
4277  * must hold at least two keys in order to guarantee sufficient space.
4278  *
4279  * We use a modified version of the freemap's fixed radix tree, but taylored
4280  * for file data.  Basically we configure an indirect block encompassing the
4281  * smallest key.
4282  */
4283 static int
4284 hammer2_chain_indkey_file(hammer2_chain_t *parent, hammer2_key_t *keyp,
4285                             int keybits, hammer2_blockref_t *base, int count,
4286                             int ncount)
4287 {
4288         hammer2_chain_t *chain;
4289         hammer2_blockref_t *bref;
4290         hammer2_key_t key;
4291         hammer2_key_t key_beg;
4292         hammer2_key_t key_end;
4293         hammer2_key_t key_next;
4294         int nradix;
4295         int locount;
4296         int hicount;
4297         int maxloops = 300000;
4298
4299         key = *keyp;
4300         locount = 0;
4301         hicount = 0;
4302         keybits = 64;
4303
4304         /*
4305          * Calculate the range of keys in the array being careful to skip
4306          * slots which are overridden with a deletion.
4307          *
4308          * Locate the smallest key.
4309          */
4310         key_beg = 0;
4311         key_end = HAMMER2_KEY_MAX;
4312         hammer2_spin_ex(&parent->core.spin);
4313
4314         for (;;) {
4315                 if (--maxloops == 0) {
4316                         panic("indkey_freemap shit %p %p:%d\n",
4317                               parent, base, count);
4318                 }
4319                 chain = hammer2_combined_find(parent, base, count,
4320                                               &key_next,
4321                                               key_beg, key_end,
4322                                               &bref);
4323
4324                 /*
4325                  * Exhausted search
4326                  */
4327                 if (bref == NULL)
4328                         break;
4329
4330                 /*
4331                  * Skip deleted chains.
4332                  */
4333                 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4334                         if (key_next == 0 || key_next > key_end)
4335                                 break;
4336                         key_beg = key_next;
4337                         continue;
4338                 }
4339
4340                 /*
4341                  * Use the full live (not deleted) element for the scan
4342                  * iteration.  HAMMER2 does not allow partial replacements.
4343                  *
4344                  * XXX should be built into hammer2_combined_find().
4345                  */
4346                 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4347
4348                 if (keybits > bref->keybits) {
4349                         key = bref->key;
4350                         keybits = bref->keybits;
4351                 } else if (keybits == bref->keybits && bref->key < key) {
4352                         key = bref->key;
4353                 }
4354                 if (key_next == 0)
4355                         break;
4356                 key_beg = key_next;
4357         }
4358         hammer2_spin_unex(&parent->core.spin);
4359
4360         /*
4361          * Calculate the static keybits for a higher-level indirect block
4362          * that contains the key.
4363          */
4364         *keyp = key;
4365
4366         switch(ncount) {
4367         case HAMMER2_IND_BYTES_MIN / sizeof(hammer2_blockref_t):
4368                 nradix = HAMMER2_IND_RADIX_MIN - HAMMER2_BLOCKREF_RADIX;
4369                 break;
4370         case HAMMER2_IND_BYTES_NOM / sizeof(hammer2_blockref_t):
4371                 nradix = HAMMER2_IND_RADIX_NOM - HAMMER2_BLOCKREF_RADIX;
4372                 break;
4373         case HAMMER2_IND_BYTES_MAX / sizeof(hammer2_blockref_t):
4374                 nradix = HAMMER2_IND_RADIX_MAX - HAMMER2_BLOCKREF_RADIX;
4375                 break;
4376         default:
4377                 panic("bad ncount %d\n", ncount);
4378                 nradix = 0;
4379                 break;
4380         }
4381
4382         /*
4383          * The largest radix that can be returned for an indirect block is
4384          * 63 bits.  (The largest practical indirect block radix is actually
4385          * 62 bits because the top-level inode or volume root contains four
4386          * entries, but allow 63 to be returned).
4387          */
4388         if (nradix >= 64)
4389                 nradix = 63;
4390
4391         return keybits + nradix;
4392 }
4393
4394 #if 1
4395
4396 /*
4397  * Directory indirect blocks.
4398  *
4399  * Covers both the inode index (directory of inodes), and directory contents
4400  * (filenames hardlinked to inodes).
4401  *
4402  * Because directory keys are hashed we generally try to cut the space in
4403  * half.  We accomodate the inode index (which tends to have linearly
4404  * increasing inode numbers) by ensuring that the keyspace is at least large
4405  * enough to fill up the indirect block being created.
4406  */
4407 static int
4408 hammer2_chain_indkey_dir(hammer2_chain_t *parent, hammer2_key_t *keyp,
4409                          int keybits, hammer2_blockref_t *base, int count,
4410                          int ncount)
4411 {
4412         hammer2_blockref_t *bref;
4413         hammer2_chain_t *chain;
4414         hammer2_key_t key_beg;
4415         hammer2_key_t key_end;
4416         hammer2_key_t key_next;
4417         hammer2_key_t key;
4418         int nkeybits;
4419         int locount;
4420         int hicount;
4421         int maxloops = 300000;
4422
4423         /*
4424          * Shortcut if the parent is the inode.  In this situation the
4425          * parent has 4+1 directory entries and we are creating an indirect
4426          * block capable of holding many more.
4427          */
4428         if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
4429                 return 63;
4430         }
4431
4432         key = *keyp;
4433         locount = 0;
4434         hicount = 0;
4435
4436         /*
4437          * Calculate the range of keys in the array being careful to skip
4438          * slots which are overridden with a deletion.
4439          */
4440         key_beg = 0;
4441         key_end = HAMMER2_KEY_MAX;
4442         hammer2_spin_ex(&parent->core.spin);
4443
4444         for (;;) {
4445                 if (--maxloops == 0) {
4446                         panic("indkey_freemap shit %p %p:%d\n",
4447                               parent, base, count);
4448                 }
4449                 chain = hammer2_combined_find(parent, base, count,
4450                                               &key_next,
4451                                               key_beg, key_end,
4452                                               &bref);
4453
4454                 /*
4455                  * Exhausted search
4456                  */
4457                 if (bref == NULL)
4458                         break;
4459
4460                 /*
4461                  * Deleted object
4462                  */
4463                 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4464                         if (key_next == 0 || key_next > key_end)
4465                                 break;
4466                         key_beg = key_next;
4467                         continue;
4468                 }
4469
4470                 /*
4471                  * Use the full live (not deleted) element for the scan
4472                  * iteration.  HAMMER2 does not allow partial replacements.
4473                  *
4474                  * XXX should be built into hammer2_combined_find().
4475                  */
4476                 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4477
4478                 /*
4479                  * Expand our calculated key range (key, keybits) to fit
4480                  * the scanned key.  nkeybits represents the full range
4481                  * that we will later cut in half (two halves @ nkeybits - 1).
4482                  */
4483                 nkeybits = keybits;
4484                 if (nkeybits < bref->keybits) {
4485                         if (bref->keybits > 64) {
4486                                 kprintf("bad bref chain %p bref %p\n",
4487                                         chain, bref);
4488                                 Debugger("fubar");
4489                         }
4490                         nkeybits = bref->keybits;
4491                 }
4492                 while (nkeybits < 64 &&
4493                        (~(((hammer2_key_t)1 << nkeybits) - 1) &
4494                         (key ^ bref->key)) != 0) {
4495                         ++nkeybits;
4496                 }
4497
4498                 /*
4499                  * If the new key range is larger we have to determine
4500                  * which side of the new key range the existing keys fall
4501                  * under by checking the high bit, then collapsing the
4502                  * locount into the hicount or vise-versa.
4503                  */
4504                 if (keybits != nkeybits) {
4505                         if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
4506                                 hicount += locount;
4507                                 locount = 0;
4508                         } else {
4509                                 locount += hicount;
4510                                 hicount = 0;
4511                         }
4512                         keybits = nkeybits;
4513                 }
4514
4515                 /*
4516                  * The newly scanned key will be in the lower half or the
4517                  * upper half of the (new) key range.
4518                  */
4519                 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
4520                         ++hicount;
4521                 else
4522                         ++locount;
4523
4524                 if (key_next == 0)
4525                         break;
4526                 key_beg = key_next;
4527         }
4528         hammer2_spin_unex(&parent->core.spin);
4529         bref = NULL;    /* now invalid (safety) */
4530
4531         /*
4532          * Adjust keybits to represent half of the full range calculated
4533          * above (radix 63 max) for our new indirect block.
4534          */
4535         --keybits;
4536
4537         /*
4538          * Expand keybits to hold at least ncount elements.  ncount will be
4539          * a power of 2.  This is to try to completely fill leaf nodes (at
4540          * least for keys which are not hashes).
4541          *
4542          * We aren't counting 'in' or 'out', we are counting 'high side'
4543          * and 'low side' based on the bit at (1LL << keybits).  We want
4544          * everything to be inside in these cases so shift it all to
4545          * the low or high side depending on the new high bit.
4546          */
4547         while (((hammer2_key_t)1 << keybits) < ncount) {
4548                 ++keybits;
4549                 if (key & ((hammer2_key_t)1 << keybits)) {
4550                         hicount += locount;
4551                         locount = 0;
4552                 } else {
4553                         locount += hicount;
4554                         hicount = 0;
4555                 }
4556         }
4557
4558         if (hicount > locount)
4559                 key |= (hammer2_key_t)1 << keybits;
4560         else
4561                 key &= ~(hammer2_key_t)1 << keybits;
4562
4563         *keyp = key;
4564
4565         return (keybits);
4566 }
4567
4568 #else
4569
4570 /*
4571  * Directory indirect blocks.
4572  *
4573  * Covers both the inode index (directory of inodes), and directory contents
4574  * (filenames hardlinked to inodes).
4575  *
4576  * Because directory keys are hashed we generally try to cut the space in
4577  * half.  We accomodate the inode index (which tends to have linearly
4578  * increasing inode numbers) by ensuring that the keyspace is at least large
4579  * enough to fill up the indirect block being created.
4580  */
4581 static int
4582 hammer2_chain_indkey_dir(hammer2_chain_t *parent, hammer2_key_t *keyp,
4583                          int keybits, hammer2_blockref_t *base, int count,
4584                          int ncount)
4585 {
4586         hammer2_blockref_t *bref;
4587         hammer2_chain_t *chain;
4588         hammer2_key_t key_beg;
4589         hammer2_key_t key_end;
4590         hammer2_key_t key_next;
4591         hammer2_key_t key;
4592         int nkeybits;
4593         int locount;
4594         int hicount;
4595         int maxloops = 300000;
4596
4597         /*
4598          * Shortcut if the parent is the inode.  In this situation the
4599          * parent has 4+1 directory entries and we are creating an indirect
4600          * block capable of holding many more.
4601          */
4602         if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
4603                 return 63;
4604         }
4605
4606         key = *keyp;
4607         locount = 0;
4608         hicount = 0;
4609
4610         /*
4611          * Calculate the range of keys in the array being careful to skip
4612          * slots which are overridden with a deletion.
4613          */
4614         key_beg = 0;
4615         key_end = HAMMER2_KEY_MAX;
4616         hammer2_spin_ex(&parent->core.spin);
4617
4618         for (;;) {
4619                 if (--maxloops == 0) {
4620                         panic("indkey_freemap shit %p %p:%d\n",
4621                               parent, base, count);
4622                 }
4623                 chain = hammer2_combined_find(parent, base, count,
4624                                               &key_next,
4625                                               key_beg, key_end,
4626                                               &bref);
4627
4628                 /*
4629                  * Exhausted search
4630                  */
4631                 if (bref == NULL)
4632                         break;
4633
4634                 /*
4635                  * Deleted object
4636                  */
4637                 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
4638                         if (key_next == 0 || key_next > key_end)
4639                                 break;
4640                         key_beg = key_next;
4641                         continue;
4642                 }
4643
4644                 /*
4645                  * Use the full live (not deleted) element for the scan
4646                  * iteration.  HAMMER2 does not allow partial replacements.
4647                  *
4648                  * XXX should be built into hammer2_combined_find().
4649                  */
4650                 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
4651
4652                 /*
4653                  * Expand our calculated key range (key, keybits) to fit
4654                  * the scanned key.  nkeybits represents the full range
4655                  * that we will later cut in half (two halves @ nkeybits - 1).
4656                  */
4657                 nkeybits = keybits;
4658                 if (nkeybits < bref->keybits) {
4659                         if (bref->keybits > 64) {
4660                                 kprintf("bad bref chain %p bref %p\n",
4661                                         chain, bref);
4662                                 Debugger("fubar");
4663                         }
4664                         nkeybits = bref->keybits;
4665                 }
4666                 while (nkeybits < 64 &&
4667                        (~(((hammer2_key_t)1 << nkeybits) - 1) &
4668                         (key ^ bref->key)) != 0) {
4669                         ++nkeybits;
4670                 }
4671
4672                 /*
4673                  * If the new key range is larger we have to determine
4674                  * which side of the new key range the existing keys fall
4675                  * under by checking the high bit, then collapsing the
4676                  * locount into the hicount or vise-versa.
4677                  */
4678                 if (keybits != nkeybits) {
4679                         if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
4680                                 hicount += locount;
4681                                 locount = 0;
4682                         } else {
4683                                 locount += hicount;
4684                                 hicount = 0;
4685                         }
4686                         keybits = nkeybits;
4687                 }
4688
4689                 /*
4690                  * The newly scanned key will be in the lower half or the
4691                  * upper half of the (new) key range.
4692                  */
4693                 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
4694                         ++hicount;
4695                 else
4696                         ++locount;
4697
4698                 if (key_next == 0)
4699                         break;
4700                 key_beg = key_next;
4701         }
4702         hammer2_spin_unex(&parent->core.spin);
4703         bref = NULL;    /* now invalid (safety) */
4704
4705         /*
4706          * Adjust keybits to represent half of the full range calculated
4707          * above (radix 63 max) for our new indirect block.
4708          */
4709         --keybits;
4710
4711         /*
4712          * Expand keybits to hold at least ncount elements.  ncount will be
4713          * a power of 2.  This is to try to completely fill leaf nodes (at
4714          * least for keys which are not hashes).
4715          *
4716          * We aren't counting 'in' or 'out', we are counting 'high side'
4717          * and 'low side' based on the bit at (1LL << keybits).  We want
4718          * everything to be inside in these cases so shift it all to
4719          * the low or high side depending on the new high bit.
4720          */
4721         while (((hammer2_key_t)1 << keybits) < ncount) {
4722                 ++keybits;
4723                 if (key & ((hammer2_key_t)1 << keybits)) {
4724                         hicount += locount;
4725                         locount = 0;
4726                 } else {
4727                         locount += hicount;
4728                         hicount = 0;
4729                 }
4730         }
4731
4732         if (hicount > locount)
4733                 key |= (hammer2_key_t)1 << keybits;
4734         else
4735                 key &= ~(hammer2_key_t)1 << keybits;
4736
4737         *keyp = key;
4738
4739         return (keybits);
4740 }
4741
4742 #endif
4743
4744 /*
4745  * Sets CHAIN_DELETED and remove the chain's blockref from the parent if
4746  * it exists.
4747  *
4748  * Both parent and chain must be locked exclusively.
4749  *
4750  * This function will modify the parent if the blockref requires removal
4751  * from the parent's block table.
4752  *
4753  * This function is NOT recursive.  Any entity already pushed into the
4754  * chain (such as an inode) may still need visibility into its contents,
4755  * as well as the ability to read and modify the contents.  For example,
4756  * for an unlinked file which is still open.
4757  *
4758  * Also note that the flusher is responsible for cleaning up empty
4759  * indirect blocks.
4760  */
4761 int
4762 hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
4763                      hammer2_tid_t mtid, int flags)
4764 {
4765         int error = 0;
4766
4767         KKASSERT(hammer2_mtx_owned(&chain->lock));
4768
4769         /*
4770          * Nothing to do if already marked.
4771          *
4772          * We need the spinlock on the core whos RBTREE contains chain
4773          * to protect against races.
4774          */
4775         if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
4776                 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 &&
4777                          chain->parent == parent);
4778                 error = _hammer2_chain_delete_helper(parent, chain,
4779                                                      mtid, flags);
4780         }
4781
4782         /*
4783          * Permanent deletions mark the chain as destroyed.
4784          */
4785         if (error == 0) {
4786                 if (flags & HAMMER2_DELETE_PERMANENT)
4787                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
4788                 hammer2_chain_setflush(chain);
4789         }
4790
4791         return error;
4792 }
4793
4794 /*
4795  * Returns the index of the nearest element in the blockref array >= elm.
4796  * Returns (count) if no element could be found.
4797  *
4798  * Sets *key_nextp to the next key for loop purposes but does not modify
4799  * it if the next key would be higher than the current value of *key_nextp.
4800  * Note that *key_nexp can overflow to 0, which should be tested by the
4801  * caller.
4802  *
4803  * WARNING!  Must be called with parent's spinlock held.  Spinlock remains
4804  *           held through the operation.
4805  */
4806 static int
4807 hammer2_base_find(hammer2_chain_t *parent,
4808                   hammer2_blockref_t *base, int count,
4809                   hammer2_key_t *key_nextp,
4810                   hammer2_key_t key_beg, hammer2_key_t key_end)
4811 {
4812         hammer2_blockref_t *scan;
4813         hammer2_key_t scan_end;
4814         int i;
4815         int limit;
4816
4817         /*
4818          * Require the live chain's already have their core's counted
4819          * so we can optimize operations.
4820          */
4821         KKASSERT(parent->flags & HAMMER2_CHAIN_COUNTEDBREFS);
4822
4823         /*
4824          * Degenerate case
4825          */
4826         if (count == 0 || base == NULL)
4827                 return(count);
4828
4829         /*
4830          * Sequential optimization using parent->cache_index.  This is
4831          * the most likely scenario.
4832          *
4833          * We can avoid trailing empty entries on live chains, otherwise
4834          * we might have to check the whole block array.
4835          */
4836         i = parent->cache_index;        /* SMP RACE OK */
4837         cpu_ccfence();
4838         limit = parent->core.live_zero;
4839         if (i >= limit)
4840                 i = limit - 1;
4841         if (i < 0)
4842                 i = 0;
4843         KKASSERT(i < count);
4844
4845         /*
4846          * Search backwards
4847          */
4848         scan = &base[i];
4849         while (i > 0 && (scan->type == 0 || scan->key > key_beg)) {
4850                 --scan;
4851                 --i;
4852         }
4853         parent->cache_index = i;
4854
4855         /*
4856          * Search forwards, stop when we find a scan element which
4857          * encloses the key or until we know that there are no further
4858          * elements.
4859          */
4860         while (i < count) {
4861                 if (scan->type != 0) {
4862                         scan_end = scan->key +
4863                                    ((hammer2_key_t)1 << scan->keybits) - 1;
4864                         if (scan->key > key_beg || scan_end >= key_beg)
4865                                 break;
4866                 }
4867                 if (i >= limit)
4868                         return (count);
4869                 ++scan;
4870                 ++i;
4871         }
4872         if (i != count) {
4873                 parent->cache_index = i;
4874                 if (i >= limit) {
4875                         i = count;
4876                 } else {
4877                         scan_end = scan->key +
4878                                    ((hammer2_key_t)1 << scan->keybits);
4879                         if (scan_end && (*key_nextp > scan_end ||
4880                                          *key_nextp == 0)) {
4881                                 *key_nextp = scan_end;
4882                         }
4883                 }
4884         }
4885         return (i);
4886 }
4887
4888 /*
4889  * Do a combined search and return the next match either from the blockref
4890  * array or from the in-memory chain.  Sets *bresp to the returned bref in
4891  * both cases, or sets it to NULL if the search exhausted.  Only returns
4892  * a non-NULL chain if the search matched from the in-memory chain.
4893  *
4894  * When no in-memory chain has been found and a non-NULL bref is returned
4895  * in *bresp.
4896  *
4897  *
4898  * The returned chain is not locked or referenced.  Use the returned bref
4899  * to determine if the search exhausted or not.  Iterate if the base find
4900  * is chosen but matches a deleted chain.
4901  *
4902  * WARNING!  Must be called with parent's spinlock held.  Spinlock remains
4903  *           held through the operation.
4904  */
4905 hammer2_chain_t *
4906 hammer2_combined_find(hammer2_chain_t *parent,
4907                       hammer2_blockref_t *base, int count,
4908                       hammer2_key_t *key_nextp,
4909                       hammer2_key_t key_beg, hammer2_key_t key_end,
4910                       hammer2_blockref_t **bresp)
4911 {
4912         hammer2_blockref_t *bref;
4913         hammer2_chain_t *chain;
4914         int i;
4915
4916         /*
4917          * Lookup in block array and in rbtree.
4918          */
4919         *key_nextp = key_end + 1;
4920         i = hammer2_base_find(parent, base, count, key_nextp,
4921                               key_beg, key_end);
4922         chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
4923
4924         /*
4925          * Neither matched
4926          */
4927         if (i == count && chain == NULL) {
4928                 *bresp = NULL;
4929                 return(NULL);
4930         }
4931
4932         /*
4933          * Only chain matched.
4934          */
4935         if (i == count) {
4936                 bref = &chain->bref;
4937                 goto found;
4938         }
4939
4940         /*
4941          * Only blockref matched.
4942          */
4943         if (chain == NULL) {
4944                 bref = &base[i];
4945                 goto found;
4946         }
4947
4948         /*
4949          * Both in-memory and blockref matched, select the nearer element.
4950          *
4951          * If both are flush with the left-hand side or both are the
4952          * same distance away, select the chain.  In this situation the
4953          * chain must have been loaded from the matching blockmap.
4954          */
4955         if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
4956             chain->bref.key == base[i].key) {
4957                 KKASSERT(chain->bref.key == base[i].key);
4958                 bref = &chain->bref;
4959                 goto found;
4960         }
4961
4962         /*
4963          * Select the nearer key
4964          */
4965         if (chain->bref.key < base[i].key) {
4966                 bref = &chain->bref;
4967         } else {
4968                 bref = &base[i];
4969                 chain = NULL;
4970         }
4971
4972         /*
4973          * If the bref is out of bounds we've exhausted our search.
4974          */
4975 found:
4976         if (bref->key > key_end) {
4977                 *bresp = NULL;
4978                 chain = NULL;
4979         } else {
4980                 *bresp = bref;
4981         }
4982         return(chain);
4983 }
4984
4985 /*
4986  * Locate the specified block array element and delete it.  The element
4987  * must exist.
4988  *
4989  * The spin lock on the related chain must be held.
4990  *
4991  * NOTE: live_count was adjusted when the chain was deleted, so it does not
4992  *       need to be adjusted when we commit the media change.
4993  */
4994 void
4995 hammer2_base_delete(hammer2_chain_t *parent,
4996                     hammer2_blockref_t *base, int count,
4997                     hammer2_chain_t *chain)
4998 {
4999         hammer2_blockref_t *elm = &chain->bref;
5000         hammer2_blockref_t *scan;
5001         hammer2_key_t key_next;
5002         int i;
5003
5004         /*
5005          * Delete element.  Expect the element to exist.
5006          *
5007          * XXX see caller, flush code not yet sophisticated enough to prevent
5008          *     re-flushed in some cases.
5009          */
5010         key_next = 0; /* max range */
5011         i = hammer2_base_find(parent, base, count, &key_next,
5012                               elm->key, elm->key);
5013         scan = &base[i];
5014         if (i == count || scan->type == 0 ||
5015             scan->key != elm->key ||
5016             ((chain->flags & HAMMER2_CHAIN_BMAPUPD) == 0 &&
5017              scan->keybits != elm->keybits)) {
5018                 hammer2_spin_unex(&parent->core.spin);
5019                 panic("delete base %p element not found at %d/%d elm %p\n",
5020                       base, i, count, elm);
5021                 return;
5022         }
5023
5024         /*
5025          * Update stats and zero the entry.
5026          *
5027          * NOTE: Handle radix == 0 (0 bytes) case.
5028          */
5029         if ((int)(scan->data_off & HAMMER2_OFF_MASK_RADIX)) {
5030                 parent->bref.embed.stats.data_count -= (hammer2_off_t)1 <<
5031                                 (int)(scan->data_off & HAMMER2_OFF_MASK_RADIX);
5032         }
5033         switch(scan->type) {
5034         case HAMMER2_BREF_TYPE_INODE:
5035                 --parent->bref.embed.stats.inode_count;
5036                 /* fall through */
5037         case HAMMER2_BREF_TYPE_DATA:
5038                 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5039                         atomic_set_int(&chain->flags,
5040                                        HAMMER2_CHAIN_HINT_LEAF_COUNT);
5041                 } else {
5042                         if (parent->bref.leaf_count)
5043                                 --parent->bref.leaf_count;
5044                 }
5045                 /* fall through */
5046         case HAMMER2_BREF_TYPE_INDIRECT:
5047                 if (scan->type != HAMMER2_BREF_TYPE_DATA) {
5048                         parent->bref.embed.stats.data_count -=
5049                                 scan->embed.stats.data_count;
5050                         parent->bref.embed.stats.inode_count -=
5051                                 scan->embed.stats.inode_count;
5052                 }
5053                 if (scan->type == HAMMER2_BREF_TYPE_INODE)
5054                         break;
5055                 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5056                         atomic_set_int(&chain->flags,
5057                                        HAMMER2_CHAIN_HINT_LEAF_COUNT);
5058                 } else {
5059                         if (parent->bref.leaf_count <= scan->leaf_count)
5060                                 parent->bref.leaf_count = 0;
5061                         else
5062                                 parent->bref.leaf_count -= scan->leaf_count;
5063                 }
5064                 break;
5065         case HAMMER2_BREF_TYPE_DIRENT:
5066                 if (parent->bref.leaf_count == HAMMER2_BLOCKREF_LEAF_MAX) {
5067                         atomic_set_int(&chain->flags,
5068                                        HAMMER2_CHAIN_HINT_LEAF_COUNT);
5069                 } else {
5070                         if (parent->bref.leaf_count)
5071                                 --parent->bref.leaf_count;
5072                 }
5073         default:
5074                 break;
5075         }
5076
5077         bzero(scan, sizeof(*scan));
5078
5079         /*
5080          * We can only optimize parent->core.live_zero for live chains.
5081          */
5082         if (parent->core.live_zero == i + 1) {
5083                 while (--i >= 0 && base[i].type == 0)
5084                         ;
5085                 parent->core.live_zero = i + 1;
5086         }
5087
5088         /*
5089          * Clear appropriate blockmap flags in chain.
5090          */
5091         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED |
5092                                         HAMMER2_CHAIN_BMAPUPD);
5093 }
5094
5095 /*
5096  * Insert the specified element.  The block array must not already have the
5097  * element and must have space available for the insertion.
5098  *
5099  * The spin lock on the related chain must be held.
5100  *
5101  * NOTE: live_count was adjusted when the chain was deleted, so it does not
5102  *       need to be adjusted when we commit the media change.
5103  */
5104 void
5105 hammer2_base_insert(hammer2_chain_t *parent,
5106                     hammer2_blockref_t *base, int count,
5107                     hammer2_chain_t *chain, hammer2_blockref_t *elm)
5108 {
5109         hammer2_key_t key_next;
5110         hammer2_key_t xkey;
5111         int i;
5112         int j;
5113         int k;
5114         int l;
5115         int u = 1;
5116
5117         /*
5118          * Insert new element.  Expect the element to not already exist
5119          * unless we are replacing it.
5120          *
5121          * XXX see caller, flush code not yet sophisticated enough to prevent
5122          *     re-flushed in some cases.
5123          */
5124         key_next = 0; /* max range */
5125         i = hammer2_base_find(parent, base, count, &key_next,
5126                               elm->key, elm->key);
5127
5128         /*
5129          * Shortcut fill optimization, typical ordered insertion(s) may not
5130          * require a search.
5131          */
5132         KKASSERT(i >= 0 && i <= count);
5133
5134         /*
5135          * Set appropriate blockmap flags in chain (if not NULL)
5136          */
5137         if (chain)
5138                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
5139
5140         /*
5141          * Update stats and zero the entry
5142          */
5143         if ((int)(elm->data_off & HAMMER2_OFF_MASK_RADIX)) {
5144                 parent->bref.embed.stats.data_count += (hammer2_off_t)1 <<
5145                                 (int)(elm->data_off & HAMMER2_OFF_MASK_RADIX);
5146         }
5147         switch(elm->type) {
5148         case HAMMER2_BREF_TYPE_INODE:
5149                 ++parent->bref.embed.stats.inode_count;
5150                 /* fall through */
5151         case HAMMER2_BREF_TYPE_DATA:
5152                 if (parent->bref.leaf_count != HAMMER2_BLOCKREF_LEAF_MAX)
5153                         ++parent->bref.leaf_count;
5154                 /* fall through */
5155         case HAMMER2_BREF_TYPE_INDIRECT:
5156                 if (elm->type != HAMMER2_BREF_TYPE_DATA) {
5157                         parent->bref.embed.stats.data_count +=
5158                                 elm->embed.stats.data_count;
5159                         parent->bref.embed.stats.inode_count +=
5160                                 elm->embed.stats.inode_count;
5161                 }
5162                 if (elm->type == HAMMER2_BREF_TYPE_INODE)
5163                         break;
5164                 if (parent->bref.leaf_count + elm->leaf_count <
5165                     HAMMER2_BLOCKREF_LEAF_MAX) {
5166                         parent->bref.leaf_count += elm->leaf_count;
5167                 } else {
5168                         parent->bref.leaf_count = HAMMER2_BLOCKREF_LEAF_MAX;
5169                 }
5170                 break;
5171         case HAMMER2_BREF_TYPE_DIRENT:
5172                 if (parent->bref.leaf_count != HAMMER2_BLOCKREF_LEAF_MAX)
5173                         ++parent->bref.leaf_count;
5174                 break;
5175         default:
5176                 break;
5177         }
5178
5179
5180         /*
5181          * We can only optimize parent->core.live_zero for live chains.
5182          */
5183         if (i == count && parent->core.live_zero < count) {
5184                 i = parent->core.live_zero++;
5185                 base[i] = *elm;
5186                 return;
5187         }
5188
5189         xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
5190         if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
5191                 hammer2_spin_unex(&parent->core.spin);
5192                 panic("insert base %p overlapping elements at %d elm %p\n",
5193                       base, i, elm);
5194         }
5195
5196         /*
5197          * Try to find an empty slot before or after.
5198          */
5199         j = i;
5200         k = i;
5201         while (j > 0 || k < count) {
5202                 --j;
5203                 if (j >= 0 && base[j].type == 0) {
5204                         if (j == i - 1) {
5205                                 base[j] = *elm;
5206                         } else {
5207                                 bcopy(&base[j+1], &base[j],
5208                                       (i - j - 1) * sizeof(*base));
5209                                 base[i - 1] = *elm;
5210                         }
5211                         goto validate;
5212                 }
5213                 ++k;
5214                 if (k < count && base[k].type == 0) {
5215                         bcopy(&base[i], &base[i+1],
5216                               (k - i) * sizeof(hammer2_blockref_t));
5217                         base[i] = *elm;
5218
5219                         /*
5220                          * We can only update parent->core.live_zero for live
5221                          * chains.
5222                          */
5223                         if (parent->core.live_zero <= k)
5224                                 parent->core.live_zero = k + 1;
5225                         u = 2;
5226                         goto validate;
5227                 }
5228         }
5229         panic("hammer2_base_insert: no room!");
5230
5231         /*
5232          * Debugging
5233          */
5234 validate:
5235         key_next = 0;
5236         for (l = 0; l < count; ++l) {
5237                 if (base[l].type) {
5238                         key_next = base[l].key +
5239                                    ((hammer2_key_t)1 << base[l].keybits) - 1;
5240                         break;
5241                 }
5242         }
5243         while (++l < count) {
5244                 if (base[l].type) {
5245                         if (base[l].key <= key_next)
5246                                 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
5247                         key_next = base[l].key +
5248                                    ((hammer2_key_t)1 << base[l].keybits) - 1;
5249
5250                 }
5251         }
5252
5253 }
5254
5255 #if 0
5256
5257 /*
5258  * Sort the blockref array for the chain.  Used by the flush code to
5259  * sort the blockref[] array.
5260  *
5261  * The chain must be exclusively locked AND spin-locked.
5262  */
5263 typedef hammer2_blockref_t *hammer2_blockref_p;
5264
5265 static
5266 int
5267 hammer2_base_sort_callback(const void *v1, const void *v2)
5268 {
5269         hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
5270         hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
5271
5272         /*
5273          * Make sure empty elements are placed at the end of the array
5274          */
5275         if (bref1->type == 0) {
5276                 if (bref2->type == 0)
5277                         return(0);
5278                 return(1);
5279         } else if (bref2->type == 0) {
5280                 return(-1);
5281         }
5282
5283         /*
5284          * Sort by key
5285          */
5286         if (bref1->key < bref2->key)
5287                 return(-1);
5288         if (bref1->key > bref2->key)
5289                 return(1);
5290         return(0);
5291 }
5292
5293 void
5294 hammer2_base_sort(hammer2_chain_t *chain)
5295 {
5296         hammer2_blockref_t *base;
5297         int count;
5298
5299         switch(chain->bref.type) {
5300         case HAMMER2_BREF_TYPE_INODE:
5301                 /*
5302                  * Special shortcut for embedded data returns the inode
5303                  * itself.  Callers must detect this condition and access
5304                  * the embedded data (the strategy code does this for us).
5305                  *
5306                  * This is only applicable to regular files and softlinks.
5307                  */
5308                 if (chain->data->ipdata.meta.op_flags &
5309                     HAMMER2_OPFLAG_DIRECTDATA) {
5310                         return;
5311                 }
5312                 base = &chain->data->ipdata.u.blockset.blockref[0];
5313                 count = HAMMER2_SET_COUNT;
5314                 break;
5315         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
5316         case HAMMER2_BREF_TYPE_INDIRECT:
5317                 /*
5318                  * Optimize indirect blocks in the INITIAL state to avoid
5319                  * I/O.
5320                  */
5321                 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
5322                 base = &chain->data->npdata[0];
5323                 count = chain->bytes / sizeof(hammer2_blockref_t);
5324                 break;
5325         case HAMMER2_BREF_TYPE_VOLUME:
5326                 base = &chain->data->voldata.sroot_blockset.blockref[0];
5327                 count = HAMMER2_SET_COUNT;
5328                 break;
5329         case HAMMER2_BREF_TYPE_FREEMAP:
5330                 base = &chain->data->blkset.blockref[0];
5331                 count = HAMMER2_SET_COUNT;
5332                 break;
5333         default:
5334                 kprintf("hammer2_chain_lookup: unrecognized "
5335                         "blockref(A) type: %d",
5336                         chain->bref.type);
5337                 while (1)
5338                         tsleep(&base, 0, "dead", 0);
5339                 panic("hammer2_chain_lookup: unrecognized "
5340                       "blockref(A) type: %d",
5341                       chain->bref.type);
5342                 base = NULL;    /* safety */
5343                 count = 0;      /* safety */
5344         }
5345         kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
5346 }
5347
5348 #endif
5349
5350 /*
5351  * Chain memory management
5352  */
5353 void
5354 hammer2_chain_wait(hammer2_chain_t *chain)
5355 {
5356         tsleep(chain, 0, "chnflw", 1);
5357 }
5358
5359 const hammer2_media_data_t *
5360 hammer2_chain_rdata(hammer2_chain_t *chain)
5361 {
5362         KKASSERT(chain->data != NULL);
5363         return (chain->data);
5364 }
5365
5366 hammer2_media_data_t *
5367 hammer2_chain_wdata(hammer2_chain_t *chain)
5368 {
5369         KKASSERT(chain->data != NULL);
5370         return (chain->data);
5371 }
5372
5373 /*
5374  * Set the check data for a chain.  This can be a heavy-weight operation
5375  * and typically only runs on-flush.  For file data check data is calculated
5376  * when the logical buffers are flushed.
5377  */
5378 void
5379 hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata)
5380 {
5381         chain->bref.flags &= ~HAMMER2_BREF_FLAG_ZERO;
5382
5383         switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
5384         case HAMMER2_CHECK_NONE:
5385                 break;
5386         case HAMMER2_CHECK_DISABLED:
5387                 break;
5388         case HAMMER2_CHECK_ISCSI32:
5389                 chain->bref.check.iscsi32.value =
5390                         hammer2_icrc32(bdata, chain->bytes);
5391                 break;
5392         case HAMMER2_CHECK_XXHASH64:
5393                 chain->bref.check.xxhash64.value =
5394                         XXH64(bdata, chain->bytes, XXH_HAMMER2_SEED);
5395                 break;
5396         case HAMMER2_CHECK_SHA192:
5397                 {
5398                         SHA256_CTX hash_ctx;
5399                         union {
5400                                 uint8_t digest[SHA256_DIGEST_LENGTH];
5401                                 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
5402                         } u;
5403
5404                         SHA256_Init(&hash_ctx);
5405                         SHA256_Update(&hash_ctx, bdata, chain->bytes);
5406                         SHA256_Final(u.digest, &hash_ctx);
5407                         u.digest64[2] ^= u.digest64[3];
5408                         bcopy(u.digest,
5409                               chain->bref.check.sha192.data,
5410                               sizeof(chain->bref.check.sha192.data));
5411                 }
5412                 break;
5413         case HAMMER2_CHECK_FREEMAP:
5414                 chain->bref.check.freemap.icrc32 =
5415                         hammer2_icrc32(bdata, chain->bytes);
5416                 break;
5417         default:
5418                 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
5419                         chain->bref.methods);
5420                 break;
5421         }
5422 }
5423
5424 int
5425 hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata)
5426 {
5427         uint32_t check32;
5428         uint64_t check64;
5429         int r;
5430
5431         if (chain->bref.flags & HAMMER2_BREF_FLAG_ZERO)
5432                 return 1;
5433
5434         switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
5435         case HAMMER2_CHECK_NONE:
5436                 r = 1;
5437                 break;
5438         case HAMMER2_CHECK_DISABLED:
5439                 r = 1;
5440                 break;
5441         case HAMMER2_CHECK_ISCSI32:
5442                 check32 = hammer2_icrc32(bdata, chain->bytes);
5443                 r = (chain->bref.check.iscsi32.value == check32);
5444                 if (r == 0) {
5445                         kprintf("chain %016jx.%02x meth=%02x CHECK FAIL "
5446                                 "(flags=%08x, bref/data %08x/%08x)\n",
5447                                 chain->bref.data_off,
5448                                 chain->bref.type,
5449                                 chain->bref.methods,
5450                                 chain->flags,
5451                                 chain->bref.check.iscsi32.value,
5452                                 check32);
5453                 }
5454                 hammer2_check_icrc32 += chain->bytes;
5455                 break;
5456         case HAMMER2_CHECK_XXHASH64:
5457                 check64 = XXH64(bdata, chain->bytes, XXH_HAMMER2_SEED);
5458                 r = (chain->bref.check.xxhash64.value == check64);
5459                 if (r == 0) {
5460                         kprintf("chain %016jx.%02x key=%016jx "
5461                                 "meth=%02x CHECK FAIL "
5462                                 "(flags=%08x, bref/data %016jx/%016jx)\n",
5463                                 chain->bref.data_off,
5464                                 chain->bref.type,
5465                                 chain->bref.key,
5466                                 chain->bref.methods,
5467                                 chain->flags,
5468                                 chain->bref.check.xxhash64.value,
5469                                 check64);
5470                 }
5471                 hammer2_check_xxhash64 += chain->bytes;
5472                 break;
5473         case HAMMER2_CHECK_SHA192:
5474                 {
5475                         SHA256_CTX hash_ctx;
5476                         union {
5477                                 uint8_t digest[SHA256_DIGEST_LENGTH];
5478                                 uint64_t digest64[SHA256_DIGEST_LENGTH/8];
5479                         } u;
5480
5481                         SHA256_Init(&hash_ctx);
5482                         SHA256_Update(&hash_ctx, bdata, chain->bytes);
5483                         SHA256_Final(u.digest, &hash_ctx);
5484                         u.digest64[2] ^= u.digest64[3];
5485                         if (bcmp(u.digest,
5486                                  chain->bref.check.sha192.data,
5487                                  sizeof(chain->bref.check.sha192.data)) == 0) {
5488                                 r = 1;
5489                         } else {
5490                                 r = 0;
5491                                 kprintf("chain %016jx.%02x meth=%02x "
5492                                         "CHECK FAIL\n",
5493                                         chain->bref.data_off,
5494                                         chain->bref.type,
5495                                         chain->bref.methods);
5496                         }
5497                 }
5498                 break;
5499         case HAMMER2_CHECK_FREEMAP:
5500                 r = (chain->bref.check.freemap.icrc32 ==
5501                      hammer2_icrc32(bdata, chain->bytes));
5502                 if (r == 0) {
5503                         kprintf("chain %016jx.%02x meth=%02x "
5504                                 "CHECK FAIL\n",
5505                                 chain->bref.data_off,
5506                                 chain->bref.type,
5507                                 chain->bref.methods);
5508                         kprintf("freemap.icrc %08x icrc32 %08x (%d)\n",
5509                                 chain->bref.check.freemap.icrc32,
5510                                 hammer2_icrc32(bdata, chain->bytes),
5511                                                chain->bytes);
5512                         if (chain->dio)
5513                                 kprintf("dio %p buf %016jx,%d bdata %p/%p\n",
5514                                         chain->dio, chain->dio->bp->b_loffset,
5515                                         chain->dio->bp->b_bufsize, bdata,
5516                                         chain->dio->bp->b_data);
5517                 }
5518
5519                 break;
5520         default:
5521                 kprintf("hammer2_chain_setcheck: unknown check type %02x\n",
5522                         chain->bref.methods);
5523                 r = 1;
5524                 break;
5525         }
5526         return r;
5527 }
5528
5529 /*
5530  * Acquire the chain and parent representing the specified inode for the
5531  * device at the specified cluster index.
5532  *
5533  * The flags passed in are LOOKUP flags, not RESOLVE flags.
5534  *
5535  * If we are unable to locate the hardlink, INVAL is returned and *chainp
5536  * will be NULL.  *parentp may still be set error or not, or NULL if the
5537  * parent itself could not be resolved.
5538  *
5539  * Caller must pass-in a valid or NULL *parentp or *chainp.  The passed-in
5540  * *parentp and *chainp will be unlocked if not NULL.
5541  */
5542 int
5543 hammer2_chain_inode_find(hammer2_pfs_t *pmp, hammer2_key_t inum,
5544                          int clindex, int flags,
5545                          hammer2_chain_t **parentp, hammer2_chain_t **chainp)
5546 {
5547         hammer2_chain_t *parent;
5548         hammer2_chain_t *rchain;
5549         hammer2_key_t key_dummy;
5550         int resolve_flags;
5551         int error;
5552
5553         resolve_flags = (flags & HAMMER2_LOOKUP_SHARED) ?
5554                         HAMMER2_RESOLVE_SHARED : 0;
5555
5556         /*
5557          * Caller expects us to replace these.
5558          */
5559         if (*chainp) {
5560                 hammer2_chain_unlock(*chainp);
5561                 hammer2_chain_drop(*chainp);
5562                 *chainp = NULL;
5563         }
5564         if (*parentp) {
5565                 hammer2_chain_unlock(*parentp);
5566                 hammer2_chain_drop(*parentp);
5567                 *parentp = NULL;
5568         }
5569
5570         /*
5571          * Inodes hang off of the iroot (bit 63 is clear, differentiating
5572          * inodes from root directory entries in the key lookup).
5573          */
5574         parent = hammer2_inode_chain(pmp->iroot, clindex, resolve_flags);
5575         rchain = NULL;
5576         if (parent) {
5577                 rchain = hammer2_chain_lookup(&parent, &key_dummy,
5578                                               inum, inum,
5579                                               &error, flags);
5580         } else {
5581                 error = HAMMER2_ERROR_EIO;
5582         }
5583         *parentp = parent;
5584         *chainp = rchain;
5585
5586         return error;
5587 }
5588
5589 /*
5590  * Used by the bulkscan code to snapshot the synchronized storage for
5591  * a volume, allowing it to be scanned concurrently against normal
5592  * operation.
5593  */
5594 hammer2_chain_t *
5595 hammer2_chain_bulksnap(hammer2_dev_t *hmp)
5596 {
5597         hammer2_chain_t *copy;
5598
5599         copy = hammer2_chain_alloc(hmp, hmp->spmp, &hmp->vchain.bref);
5600         copy->data = kmalloc(sizeof(copy->data->voldata),
5601                              hmp->mchain,
5602                              M_WAITOK | M_ZERO);
5603         hammer2_voldata_lock(hmp);
5604         copy->data->voldata = hmp->volsync;
5605         hammer2_voldata_unlock(hmp);
5606
5607         return copy;
5608 }
5609
5610 void
5611 hammer2_chain_bulkdrop(hammer2_chain_t *copy)
5612 {
5613         KKASSERT(copy->bref.type == HAMMER2_BREF_TYPE_VOLUME);
5614         KKASSERT(copy->data);
5615         kfree(copy->data, copy->hmp->mchain);
5616         copy->data = NULL;
5617         atomic_add_long(&hammer2_chain_allocs, -1);
5618         hammer2_chain_drop(copy);
5619 }
5620
5621 /*
5622  * Create a snapshot of the specified (chain) with the specified label.
5623  * The originating hammer2_inode must be exclusively locked for
5624  * safety.  The device's bulklk should be held by the caller.  The caller
5625  * is responsible for synchronizing the filesystem to storage before
5626  * taking the snapshot.
5627  */
5628 int
5629 hammer2_chain_snapshot(hammer2_chain_t *chain, hammer2_ioc_pfs_t *pmp,
5630                        hammer2_tid_t mtid)
5631 {
5632         hammer2_dev_t *hmp;
5633         const hammer2_inode_data_t *ripdata;
5634         hammer2_inode_data_t *wipdata;
5635         hammer2_chain_t *nchain;
5636         hammer2_inode_t *nip;
5637         size_t name_len;
5638         hammer2_key_t lhc;
5639         struct vattr vat;
5640 #if 0
5641         uuid_t opfs_clid;
5642 #endif
5643         int error;
5644
5645         kprintf("snapshot %s\n", pmp->name);
5646
5647         name_len = strlen(pmp->name);
5648         lhc = hammer2_dirhash(pmp->name, name_len);
5649
5650         /*
5651          * Get the clid
5652          */
5653         ripdata = &chain->data->ipdata;
5654 #if 0
5655         opfs_clid = ripdata->meta.pfs_clid;
5656 #endif
5657         hmp = chain->hmp;
5658
5659         /*
5660          * Create the snapshot directory under the super-root
5661          *
5662          * Set PFS type, generate a unique filesystem id, and generate
5663          * a cluster id.  Use the same clid when snapshotting a PFS root,
5664          * which theoretically allows the snapshot to be used as part of
5665          * the same cluster (perhaps as a cache).
5666          *
5667          * Copy the (flushed) blockref array.  Theoretically we could use
5668          * chain_duplicate() but it becomes difficult to disentangle
5669          * the shared core so for now just brute-force it.
5670          */
5671         VATTR_NULL(&vat);
5672         vat.va_type = VDIR;
5673         vat.va_mode = 0755;
5674         hammer2_chain_unlock(chain);
5675         nip = hammer2_inode_create(hmp->spmp->iroot, hmp->spmp->iroot,
5676                                    &vat, proc0.p_ucred,
5677                                    pmp->name, name_len, 0,
5678                                    1, 0, 0,
5679                                    HAMMER2_INSERT_PFSROOT, &error);
5680         hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
5681
5682         if (nip) {
5683                 hammer2_inode_modify(nip);
5684                 nchain = hammer2_inode_chain(nip, 0, HAMMER2_RESOLVE_ALWAYS);
5685                 error = hammer2_chain_modify(nchain, mtid, 0, 0);
5686                 KKASSERT(error == 0);
5687                 wipdata = &nchain->data->ipdata;
5688
5689                 nip->meta.pfs_type = HAMMER2_PFSTYPE_MASTER;
5690                 nip->meta.pfs_subtype = HAMMER2_PFSSUBTYPE_SNAPSHOT;
5691                 nip->meta.op_flags |= HAMMER2_OPFLAG_PFSROOT;
5692                 kern_uuidgen(&nip->meta.pfs_fsid, 1);
5693
5694 #if 0
5695                 /*
5696                  * Give the snapshot its own private cluster id.  As a
5697                  * snapshot no further synchronization with the original
5698                  * cluster will be done.
5699                  */
5700                 if (chain->flags & HAMMER2_CHAIN_PFSBOUNDARY)
5701                         nip->meta.pfs_clid = opfs_clid;
5702                 else
5703                         kern_uuidgen(&nip->meta.pfs_clid, 1);
5704 #endif
5705                 kern_uuidgen(&nip->meta.pfs_clid, 1);
5706                 nchain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
5707
5708                 /* XXX hack blockset copy */
5709                 /* XXX doesn't work with real cluster */
5710                 wipdata->meta = nip->meta;
5711                 wipdata->u.blockset = ripdata->u.blockset;
5712
5713                 hammer2_flush(nchain, 1);
5714                 KKASSERT(wipdata == &nchain->data->ipdata);
5715                 hammer2_pfsalloc(nchain, wipdata, nchain->bref.modify_tid, 0);
5716
5717                 hammer2_chain_unlock(nchain);
5718                 hammer2_chain_drop(nchain);
5719                 hammer2_inode_chain_sync(nip);
5720                 hammer2_inode_unlock(nip);
5721                 hammer2_inode_run_sideq(hmp->spmp);
5722         }
5723         return (error);
5724 }
5725
5726 /*
5727  * Returns non-zero if the chain (INODE or DIRENT) matches the
5728  * filename.
5729  */
5730 int
5731 hammer2_chain_dirent_test(hammer2_chain_t *chain, const char *name,
5732                           size_t name_len)
5733 {
5734         const hammer2_inode_data_t *ripdata;
5735         const hammer2_dirent_head_t *den;
5736
5737         if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
5738                 ripdata = &chain->data->ipdata;
5739                 if (ripdata->meta.name_len == name_len &&
5740                     bcmp(ripdata->filename, name, name_len) == 0) {
5741                         return 1;
5742                 }
5743         }
5744         if (chain->bref.type == HAMMER2_BREF_TYPE_DIRENT &&
5745            chain->bref.embed.dirent.namlen == name_len) {
5746                 den = &chain->bref.embed.dirent;
5747                 if (name_len > sizeof(chain->bref.check.buf) &&
5748                     bcmp(chain->data->buf, name, name_len) == 0) {
5749                         return 1;
5750                 }
5751                 if (name_len <= sizeof(chain->bref.check.buf) &&
5752                     bcmp(chain->bref.check.buf, name, name_len) == 0) {
5753                         return 1;
5754                 }
5755         }
5756         return 0;
5757 }