hammer2 - Refactor file unlink w/open descriptor
[dragonfly.git] / sys / vfs / hammer2 / hammer2_chain.c
1 /*
2  * Copyright (c) 2011-2013 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  * by 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  * A chain is topologically stable once it has been inserted into the
44  * in-memory topology.  Modifications which copy, move, or resize the chain
45  * are handled via the DELETE-DUPLICATE mechanic where the original chain
46  * stays intact but is marked deleted and a new chain is allocated which
47  * shares the old chain's children.
48  *
49  * This sharing is handled via the hammer2_chain_core structure.
50  *
51  * The DELETE-DUPLICATE mechanism allows the same topological level to contain
52  * many overloadings.  However, our RBTREE mechanics require that there be
53  * no overlaps so we accomplish the overloading by moving conflicting chains
54  * with smaller or equal radii into a sub-RBTREE under the chain being
55  * overloaded.
56  *
57  * DELETE-DUPLICATE is also used when a modification to a chain crosses a
58  * flush synchronization boundary, allowing the flush code to continue flushing
59  * the older version of the topology and not be disrupted by new frontend
60  * operations.
61  *
62  *                              LIVE VS FLUSH VIEW
63  *
64  * All lookup and iterate operations and most modifications are done on the
65  * live view.  During flushes lookups are not normally done and modifications
66  * may be run on the flush view.  However, flushes often needs to allocate
67  * blocks and the freemap_alloc/free code issues lookups.  This code is
68  * special cased to use the live view when called from a flush.
69  *
70  * General chain lookup/iteration functions are NOT aware of the flush view,
71  * they only know about live views.
72  */
73 #include <sys/cdefs.h>
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/types.h>
77 #include <sys/lock.h>
78 #include <sys/kern_syscall.h>
79 #include <sys/uuid.h>
80
81 #include "hammer2.h"
82
83 static int hammer2_indirect_optimize;   /* XXX SYSCTL */
84
85 static hammer2_chain_t *hammer2_chain_create_indirect(
86                 hammer2_trans_t *trans, hammer2_chain_t *parent,
87                 hammer2_key_t key, int keybits, int for_type, int *errorp);
88 static void hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop);
89 static void adjreadcounter(hammer2_blockref_t *bref, size_t bytes);
90 static hammer2_chain_t *hammer2_combined_find(
91                 hammer2_chain_t *parent,
92                 hammer2_blockref_t *base, int count,
93                 int *cache_indexp, hammer2_key_t *key_nextp,
94                 hammer2_key_t key_beg, hammer2_key_t key_end,
95                 hammer2_blockref_t **bresp);
96 static void hammer2_chain_assert_not_present(hammer2_chain_core_t *above,
97                                  hammer2_chain_t *chain);
98
99 hammer2_chain_t *XXChain;
100 int XXChainWhy;
101 /*
102  * Basic RBTree for chains.  Chains cannot overlap within any given
103  * core->rbtree without recursing through chain->rbtree.  We effectively
104  * guarantee this by checking the full range rather than just the first
105  * key element.  By matching on the full range callers can detect when
106  * recursrion through chain->rbtree is needed.
107  *
108  * NOTE: This also means the a delete-duplicate on the same key will
109  *       overload by placing the deleted element in the new element's
110  *       chain->rbtree (when doing a direct replacement).
111  */
112 RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
113
114 int
115 hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
116 {
117         hammer2_key_t c1_beg;
118         hammer2_key_t c1_end;
119         hammer2_key_t c2_beg;
120         hammer2_key_t c2_end;
121
122         c1_beg = chain1->bref.key;
123         c1_end = c1_beg + ((hammer2_key_t)1 << chain1->bref.keybits) - 1;
124         c2_beg = chain2->bref.key;
125         c2_end = c2_beg + ((hammer2_key_t)1 << chain2->bref.keybits) - 1;
126
127         if (c1_end < c2_beg)    /* fully to the left */
128                 return(-1);
129         if (c1_beg > c2_end)    /* fully to the right */
130                 return(1);
131         return(0);              /* overlap (must not cross edge boundary) */
132 }
133
134 static __inline
135 int
136 hammer2_isclusterable(hammer2_chain_t *chain)
137 {
138         if (hammer2_cluster_enable) {
139                 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
140                     chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
141                     chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
142                         return(1);
143                 }
144         }
145         return(0);
146 }
147
148 /*
149  * Recursively set the update_hi flag up to the root starting at chain's
150  * parent->core.  update_hi is not set in chain's core.
151  *
152  * This controls top-down visibility for flushes.  The child has just one
153  * 'above' core, but the core itself can be multi-homed with parents iterated
154  * via core->ownerq.
155  *
156  * This function is not used during a flush (except when the flush is
157  * allocating which requires the live tree).  The flush keeps track of its
158  * recursion itself.
159  *
160  * XXX needs to be optimized to use roll-up TIDs.  update_hi is only really
161  * compared against bref.mirror_tid which itself is only updated by a flush.
162  */
163 void
164 hammer2_chain_setsubmod(hammer2_trans_t *trans, hammer2_chain_t *chain)
165 {
166         hammer2_chain_core_t *above;
167
168         while ((above = chain->above) != NULL) {
169                 spin_lock(&above->cst.spin);
170                 /* XXX optimize */
171                 if (above->update_hi < trans->sync_tid)
172                         above->update_hi = trans->sync_tid;
173                 chain = TAILQ_LAST(&above->ownerq, h2_core_list);
174 #if 0
175                 TAILQ_FOREACH_REVERSE(chain, &above->ownerq,
176                                       h2_core_list, core_entry) {
177                         if (trans->sync_tid >= chain->modify_tid &&
178                             trans->sync_tid <= chain->delete_tid) {
179                                 break;
180                         }
181                 }
182 #endif
183                 spin_unlock(&above->cst.spin);
184         }
185 }
186
187 /*
188  * Allocate a new disconnected chain element representing the specified
189  * bref.  chain->refs is set to 1 and the passed bref is copied to
190  * chain->bref.  chain->bytes is derived from the bref.
191  *
192  * chain->core is NOT allocated and the media data and bp pointers are left
193  * NULL.  The caller must call chain_core_alloc() to allocate or associate
194  * a core with the chain.
195  *
196  * NOTE: Returns a referenced but unlocked (because there is no core) chain.
197  */
198 hammer2_chain_t *
199 hammer2_chain_alloc(hammer2_mount_t *hmp, hammer2_pfsmount_t *pmp,
200                     hammer2_trans_t *trans, hammer2_blockref_t *bref)
201 {
202         hammer2_chain_t *chain;
203         u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
204
205         /*
206          * Construct the appropriate system structure.
207          */
208         switch(bref->type) {
209         case HAMMER2_BREF_TYPE_INODE:
210         case HAMMER2_BREF_TYPE_INDIRECT:
211         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
212         case HAMMER2_BREF_TYPE_DATA:
213         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
214                 /*
215                  * Chain's are really only associated with the hmp but we
216                  * maintain a pmp association for per-mount memory tracking
217                  * purposes.  The pmp can be NULL.
218                  */
219                 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
220                 if (pmp)
221                         chain->pmp = pmp;
222                 break;
223         case HAMMER2_BREF_TYPE_VOLUME:
224         case HAMMER2_BREF_TYPE_FREEMAP:
225                 chain = NULL;
226                 panic("hammer2_chain_alloc volume type illegal for op");
227         default:
228                 chain = NULL;
229                 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
230                       bref->type);
231         }
232
233         chain->hmp = hmp;
234         chain->bref = *bref;
235         chain->bytes = bytes;
236         chain->refs = 1;
237         chain->flags = HAMMER2_CHAIN_ALLOCATED;
238         chain->delete_tid = HAMMER2_MAX_TID;
239
240         /*
241          * Set modify_tid if a transaction is creating the chain.  When
242          * loading a chain from backing store trans is passed as NULL and
243          * modify_tid is left set to 0.
244          */
245         if (trans)
246                 chain->modify_tid = trans->sync_tid;
247
248         return (chain);
249 }
250
251 /*
252  * Associate an existing core with the chain or allocate a new core.
253  *
254  * The core is not locked.  No additional refs on the chain are made.
255  * (trans) must not be NULL if (core) is not NULL.
256  *
257  * When chains are delete-duplicated during flushes we insert nchain on
258  * the ownerq after ochain instead of at the end in order to give the
259  * drop code visibility in the correct order, otherwise drops can be missed.
260  */
261 void
262 hammer2_chain_core_alloc(hammer2_trans_t *trans,
263                          hammer2_chain_t *nchain, hammer2_chain_t *ochain)
264 {
265         hammer2_chain_core_t *core;
266
267         KKASSERT(nchain->core == NULL);
268
269         if (ochain == NULL) {
270                 /*
271                  * Fresh core under nchain (no multi-homing of ochain's
272                  * sub-tree).
273                  */
274                 core = kmalloc(sizeof(*core), nchain->hmp->mchain,
275                                M_WAITOK | M_ZERO);
276                 TAILQ_INIT(&core->layerq);
277                 TAILQ_INIT(&core->ownerq);
278                 core->sharecnt = 1;
279                 core->good = 0x1234;
280                 if (trans)
281                         core->update_hi = trans->sync_tid;
282                 else
283                         core->update_hi = nchain->bref.mirror_tid;
284                 nchain->core = core;
285                 ccms_cst_init(&core->cst, nchain);
286                 TAILQ_INSERT_TAIL(&core->ownerq, nchain, core_entry);
287         } else {
288                 /*
289                  * Propagate the PFSROOT flag which we set on all subdirs
290                  * under the super-root.
291                  */
292                 atomic_set_int(&nchain->flags,
293                                ochain->flags & HAMMER2_CHAIN_PFSROOT);
294
295                 /*
296                  * Duplicating ochain -> nchain.  Set the DUPLICATED flag on
297                  * ochain if nchain is not a snapshot.
298                  *
299                  * It is possible for the DUPLICATED flag to already be
300                  * set when called via a flush operation because flush
301                  * operations may have to work on elements with delete_tid's
302                  * beyond the flush sync_tid.  In this situation we must
303                  * ensure that nchain is placed just after ochain in the
304                  * ownerq and that the DUPLICATED flag is set on nchain so
305                  * 'live' operations skip past it to the correct chain.
306                  *
307                  * The flusher understands the blockref synchronization state
308                  * for any stale chains by observing bref.mirror_tid, which
309                  * delete-duplicate replicates.
310                  *
311                  * WARNING! However, the case is disallowed when the flusher
312                  *          is allocating freemap space because this entails
313                  *          more than just adjusting a block table.
314                  */
315                 if (ochain->flags & HAMMER2_CHAIN_DUPLICATED) {
316                         KKASSERT((trans->flags &
317                                   (HAMMER2_TRANS_ISFLUSH |
318                                    HAMMER2_TRANS_ISALLOCATING)) ==
319                                  HAMMER2_TRANS_ISFLUSH);
320                         atomic_set_int(&nchain->flags,
321                                        HAMMER2_CHAIN_DUPLICATED);
322                 }
323                 if ((nchain->flags & HAMMER2_CHAIN_SNAPSHOT) == 0) {
324                         atomic_set_int(&ochain->flags,
325                                        HAMMER2_CHAIN_DUPLICATED);
326                 }
327                 core = ochain->core;
328                 atomic_add_int(&core->sharecnt, 1);
329
330                 spin_lock(&core->cst.spin);
331                 nchain->core = core;
332
333 #if 0
334                 if (core->update_hi < trans->sync_tid)
335                         core->update_hi = trans->sync_tid;
336 #endif
337
338                 /*
339                  * Maintain ordering for refactor test so we don't skip over
340                  * a snapshot.  Also, during flushes, delete-duplications
341                  * for block-table updates can occur on blocks already
342                  * deleted (delete-duplicated by a later transaction).  We
343                  * must insert nchain after ochain but before the later
344                  * transaction's copy.
345                  */
346                 TAILQ_INSERT_AFTER(&core->ownerq, ochain, nchain, core_entry);
347
348                 spin_unlock(&core->cst.spin);
349         }
350 }
351
352 /*
353  * Add a reference to a chain element, preventing its destruction.
354  */
355 void
356 hammer2_chain_ref(hammer2_chain_t *chain)
357 {
358         atomic_add_int(&chain->refs, 1);
359 }
360
361 /*
362  * Insert the chain in the core rbtree at the first layer
363  * which accepts it (for now we don't sort layers by the transaction tid)
364  */
365 #define HAMMER2_CHAIN_INSERT_SPIN       0x0001
366 #define HAMMER2_CHAIN_INSERT_LIVE       0x0002
367 #define HAMMER2_CHAIN_INSERT_RACE       0x0004
368
369 static
370 int
371 hammer2_chain_insert(hammer2_chain_core_t *above, hammer2_chain_layer_t *layer,
372                      hammer2_chain_t *chain, int flags, int generation)
373 {
374         hammer2_chain_t *xchain;
375         hammer2_chain_layer_t *nlayer;
376         int error = 0;
377
378         if (flags & HAMMER2_CHAIN_INSERT_SPIN)
379                 spin_lock(&above->cst.spin);
380
381         /*
382          * Special case, place the chain in the next most-recent layer as the
383          * specified layer, inserting a layer inbetween if necessary.
384          */
385         if (layer) {
386                 KKASSERT((flags & HAMMER2_CHAIN_INSERT_RACE) == 0);
387                 nlayer = TAILQ_PREV(layer, h2_layer_list, entry);
388                 if (nlayer && RB_INSERT(hammer2_chain_tree,
389                                         &nlayer->rbtree, chain) == NULL) {
390                         layer = nlayer;
391                         goto done;
392                 }
393
394                 spin_unlock(&above->cst.spin);
395                 KKASSERT((flags & HAMMER2_CHAIN_INSERT_LIVE) == 0);
396                 nlayer = kmalloc(sizeof(*nlayer), chain->hmp->mchain,
397                                  M_WAITOK | M_ZERO);
398                 RB_INIT(&nlayer->rbtree);
399                 nlayer->good = 0xABCD;
400                 spin_lock(&above->cst.spin);
401
402                 if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0)
403                         hammer2_chain_assert_not_present(above, chain);
404
405                 TAILQ_INSERT_BEFORE(layer, nlayer, entry);
406                 RB_INSERT(hammer2_chain_tree, &nlayer->rbtree, chain);
407                 layer = nlayer;
408                 goto done;
409         }
410
411         /*
412          * Interlocked by spinlock, check for race
413          */
414         if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
415             above->generation != generation) {
416                 error = EAGAIN;
417                 goto failed;
418         }
419
420         /*
421          * Try to insert, allocate a new layer if a nominal collision
422          * occurs (a collision is different from a SMP race).
423          */
424         layer = TAILQ_FIRST(&above->layerq);
425         xchain = NULL;
426
427         if (layer == NULL ||
428             (xchain = RB_INSERT(hammer2_chain_tree,
429                                 &layer->rbtree, chain)) != NULL) {
430
431                 /*
432                  * Allocate a new layer to resolve the issue.
433                  */
434                 spin_unlock(&above->cst.spin);
435                 layer = kmalloc(sizeof(*layer), chain->hmp->mchain,
436                                 M_WAITOK | M_ZERO);
437                 RB_INIT(&layer->rbtree);
438                 layer->good = 0xABCD;
439                 spin_lock(&above->cst.spin);
440
441                 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
442                     above->generation != generation) {
443                         spin_unlock(&above->cst.spin);
444                         kfree(layer, chain->hmp->mchain);
445                         spin_lock(&above->cst.spin);
446                         error = EAGAIN;
447                         goto failed;
448                 }
449                 hammer2_chain_assert_not_present(above, chain);
450
451                 TAILQ_INSERT_HEAD(&above->layerq, layer, entry);
452                 RB_INSERT(hammer2_chain_tree, &layer->rbtree, chain);
453         }
454 done:
455         chain->above = above;
456         chain->inlayer = layer;
457         ++above->chain_count;
458         ++above->generation;
459         atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
460
461         /*
462          * We have to keep track of the effective live-view blockref count
463          * so the create code knows when to push an indirect block.
464          */
465         if ((flags & HAMMER2_CHAIN_INSERT_LIVE) &&
466             (chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
467                 atomic_add_int(&above->live_count, 1);
468         }
469 failed:
470         if (flags & HAMMER2_CHAIN_INSERT_SPIN)
471                 spin_unlock(&above->cst.spin);
472         return error;
473 }
474
475 /*
476  * Drop the caller's reference to the chain.  When the ref count drops to
477  * zero this function will try to disassociate the chain from its parent and
478  * deallocate it, then recursely drop the parent using the implied ref
479  * from the chain's chain->parent.
480  */
481 static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain,
482                                                struct h2_core_list *delayq);
483
484 void
485 hammer2_chain_drop(hammer2_chain_t *chain)
486 {
487         struct h2_core_list delayq;
488         hammer2_chain_t *scan;
489         u_int refs;
490         u_int need = 0;
491
492         if (hammer2_debug & 0x200000)
493                 Debugger("drop");
494
495         if (chain->flags & HAMMER2_CHAIN_MOVED)
496                 ++need;
497         if (chain->flags & HAMMER2_CHAIN_MODIFIED)
498                 ++need;
499         KKASSERT(chain->refs > need);
500
501         TAILQ_INIT(&delayq);
502
503         while (chain) {
504                 refs = chain->refs;
505                 cpu_ccfence();
506                 KKASSERT(refs > 0);
507
508                 if (refs == 1) {
509                         chain = hammer2_chain_lastdrop(chain, &delayq);
510                 } else {
511                         if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
512                                 break;
513                         /* retry the same chain */
514                 }
515
516                 /*
517                  * When we've exhausted lastdrop chaining pull off of delayq.
518                  * chains on delayq are dead but are used to placehold other
519                  * chains which we added a ref to for the purpose of dropping.
520                  */
521                 if (chain == NULL) {
522                         hammer2_mount_t *hmp;
523
524                         if ((scan = TAILQ_FIRST(&delayq)) != NULL) {
525                                 chain = (void *)scan->data;
526                                 TAILQ_REMOVE(&delayq, scan, core_entry);
527                                 scan->flags &= ~HAMMER2_CHAIN_ALLOCATED;
528                                 hmp = scan->hmp;
529                                 scan->hmp = NULL;
530                                 kfree(scan, hmp->mchain);
531                         }
532                 }
533         }
534 }
535
536 /*
537  * Safe handling of the 1->0 transition on chain.  Returns a chain for
538  * recursive drop or NULL, possibly returning the same chain if the atomic
539  * op fails.
540  *
541  * Whem two chains need to be recursively dropped we use the chain
542  * we would otherwise free to placehold the additional chain.  It's a bit
543  * convoluted but we can't just recurse without potentially blowing out
544  * the kernel stack.
545  *
546  * The chain cannot be freed if it has a non-empty core (children) or
547  * it is not at the head of ownerq.
548  *
549  * The cst spinlock is allowed nest child-to-parent (not parent-to-child).
550  */
551 static
552 hammer2_chain_t *
553 hammer2_chain_lastdrop(hammer2_chain_t *chain, struct h2_core_list *delayq)
554 {
555         hammer2_pfsmount_t *pmp;
556         hammer2_mount_t *hmp;
557         hammer2_chain_core_t *above;
558         hammer2_chain_core_t *core;
559         hammer2_chain_layer_t *layer;
560         hammer2_chain_t *rdrop1;
561         hammer2_chain_t *rdrop2;
562
563         /*
564          * Spinlock the core and check to see if it is empty.  If it is
565          * not empty we leave chain intact with refs == 0.  The elements
566          * in core->rbtree are associated with other chains contemporary
567          * with ours but not with our chain directly.
568          */
569         if ((core = chain->core) != NULL) {
570                 spin_lock(&core->cst.spin);
571
572                 /*
573                  * We can't free non-stale chains with children until we are
574                  * able to free the children because there might be a flush
575                  * dependency.  Flushes of stale children (which should also
576                  * have their deleted flag set) short-cut recursive flush
577                  * dependencies and can be freed here.  Any flushes which run
578                  * through stale children due to the flush synchronization
579                  * point should have the MOVED bit set in the chain and not
580                  * reach lastdrop at this time.
581                  *
582                  * NOTE: We return (chain) on failure to retry.
583                  */
584                 if (core->chain_count &&
585                     (chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0) {
586                         if (atomic_cmpset_int(&chain->refs, 1, 0))
587                                 chain = NULL;   /* success */
588                         spin_unlock(&core->cst.spin);
589                         return(chain);
590                 }
591                 /* no chains left under us */
592
593                 /*
594                  * Various parts of the code might be holding a ref on a
595                  * stale chain as a placemarker which must be iterated to
596                  * locate a later non-stale (live) chain.  We must be sure
597                  * NOT to free the later non-stale chain (which might have
598                  * no refs).  Otherwise mass confusion may result.
599                  *
600                  * The DUPLICATED flag tells us whether the chain is stale
601                  * or not, so the rule is that any chain whos DUPLICATED flag
602                  * is NOT set must also be at the head of the ownerq.
603                  *
604                  * Note that the DELETED flag is not involved.  That is, a
605                  * live chain can represent a deletion that has not yet been
606                  * flushed (or still has refs).
607                  */
608 #if 0
609                 if (TAILQ_NEXT(chain, core_entry) == NULL &&
610                     TAILQ_FIRST(&core->ownerq) != chain) {
611 #endif
612                 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0 &&
613                     TAILQ_FIRST(&core->ownerq) != chain) {
614                         if (atomic_cmpset_int(&chain->refs, 1, 0))
615                                 chain = NULL;   /* success */
616                         spin_unlock(&core->cst.spin);
617                         return(chain);
618                 }
619         }
620
621         /*
622          * chain->core has no children left so no accessors can get to our
623          * chain from there.  Now we have to lock the above core to interlock
624          * remaining possible accessors that might bump chain's refs before
625          * we can safely drop chain's refs with intent to free the chain.
626          */
627         hmp = chain->hmp;
628         pmp = chain->pmp;       /* can be NULL */
629         rdrop1 = NULL;
630         rdrop2 = NULL;
631         layer = NULL;
632
633         /*
634          * Spinlock the parent and try to drop the last ref on chain.
635          * On success remove chain from its parent, otherwise return NULL.
636          *
637          * (normal core locks are top-down recursive but we define core
638          *  spinlocks as bottom-up recursive, so this is safe).
639          */
640         if ((above = chain->above) != NULL) {
641                 spin_lock(&above->cst.spin);
642                 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
643                         /* 1->0 transition failed */
644                         spin_unlock(&above->cst.spin);
645                         if (core)
646                                 spin_unlock(&core->cst.spin);
647                         return(chain);  /* retry */
648                 }
649
650                 /*
651                  * 1->0 transition successful, remove chain from its
652                  * above core.  Track layer for removal/freeing.
653                  */
654                 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
655                 layer = chain->inlayer;
656                 RB_REMOVE(hammer2_chain_tree, &layer->rbtree, chain);
657                 --above->chain_count;
658                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
659                 chain->above = NULL;
660                 chain->inlayer = NULL;
661
662                 if (RB_EMPTY(&layer->rbtree) && layer->refs == 0) {
663                         TAILQ_REMOVE(&above->layerq, layer, entry);
664                 } else {
665                         layer = NULL;
666                 }
667
668                 /*
669                  * If our chain was the last chain in the parent's core the
670                  * core is now empty and its parents might now be droppable.
671                  * Try to drop the first multi-homed parent by gaining a
672                  * ref on it here and then dropping it below.
673                  */
674                 if (above->chain_count == 0) {
675                         rdrop1 = TAILQ_FIRST(&above->ownerq);
676                         if (rdrop1 &&
677                             atomic_cmpset_int(&rdrop1->refs, 0, 1) == 0) {
678                                 rdrop1 = NULL;
679                         }
680                 }
681                 spin_unlock(&above->cst.spin);
682                 above = NULL;   /* safety */
683         }
684
685         /*
686          * Successful 1->0 transition and the chain can be destroyed now.
687          *
688          * We still have the core spinlock (if core is non-NULL), and core's
689          * chain_count is 0.  The above spinlock is gone.
690          *
691          * Remove chain from ownerq.  Once core has no more owners (and no
692          * children which is already the case) we can destroy core.
693          *
694          * If core has more owners we may be able to continue a bottom-up
695          * drop with our next sibling.
696          */
697         if (core) {
698                 chain->core = NULL;
699
700                 TAILQ_REMOVE(&core->ownerq, chain, core_entry);
701                 rdrop2 = TAILQ_FIRST(&core->ownerq);
702                 if (rdrop2 && atomic_cmpset_int(&rdrop2->refs, 0, 1) == 0)
703                         rdrop2 = NULL;
704                 spin_unlock(&core->cst.spin);
705
706                 /*
707                  * We can do the final 1->0 transition with an atomic op
708                  * after releasing core's spinlock.
709                  */
710                 if (atomic_fetchadd_int(&core->sharecnt, -1) == 1) {
711                         /*
712                          * On the 1->0 transition of core we can destroy
713                          * it.  Any remaining layers should no longer be
714                          * referenced or visibile to other threads.
715                          */
716                         KKASSERT(TAILQ_EMPTY(&core->ownerq));
717                         if (layer) {
718                                 layer->good = 0xEF00;
719                                 kfree(layer, hmp->mchain);
720                         }
721                         while ((layer = TAILQ_FIRST(&core->layerq)) != NULL) {
722                                 KKASSERT(layer->refs == 0 &&
723                                          RB_EMPTY(&layer->rbtree));
724                                 TAILQ_REMOVE(&core->layerq, layer, entry);
725                                 layer->good = 0xEF01;
726                                 kfree(layer, hmp->mchain);
727                         }
728                         /* layer now NULL */
729                         KKASSERT(core->cst.count == 0);
730                         KKASSERT(core->cst.upgrade == 0);
731                         core->good = 0x5678;
732                         kfree(core, hmp->mchain);
733                 }
734                 core = NULL;    /* safety */
735         }
736
737         /*
738          * All spin locks are gone, finish freeing stuff.
739          */
740         KKASSERT((chain->flags & (HAMMER2_CHAIN_MOVED |
741                                   HAMMER2_CHAIN_MODIFIED)) == 0);
742         hammer2_chain_drop_data(chain, 1);
743
744         KKASSERT(chain->dio == NULL);
745
746         /*
747          * Free saved empty layer and return chained drop.
748          */
749         if (layer) {
750                 layer->good = 0xEF02;
751                 kfree(layer, hmp->mchain);
752         }
753
754         /*
755          * Once chain resources are gone we can use the now dead chain
756          * structure to placehold what might otherwise require a recursive
757          * drop, because we have potentially two things to drop and can only
758          * return one directly.
759          */
760         if (rdrop1 && rdrop2) {
761                 KKASSERT(chain->flags & HAMMER2_CHAIN_ALLOCATED);
762                 chain->data = (void *)rdrop1;
763                 TAILQ_INSERT_TAIL(delayq, chain, core_entry);
764                 rdrop1 = NULL;
765         } else if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
766                 chain->flags &= ~HAMMER2_CHAIN_ALLOCATED;
767                 chain->hmp = NULL;
768                 kfree(chain, hmp->mchain);
769         }
770
771         /*
772          * Either or both can be NULL.  We already handled the case where
773          * both might not have been NULL.
774          */
775         if (rdrop1)
776                 return(rdrop1);
777         else
778                 return(rdrop2);
779 }
780
781 /*
782  * On either last lock release or last drop
783  */
784 static void
785 hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop)
786 {
787         hammer2_mount_t *hmp = chain->hmp;
788
789         switch(chain->bref.type) {
790         case HAMMER2_BREF_TYPE_VOLUME:
791         case HAMMER2_BREF_TYPE_FREEMAP:
792                 if (lastdrop)
793                         chain->data = NULL;
794                 break;
795         case HAMMER2_BREF_TYPE_INODE:
796                 if (chain->data) {
797                         kfree(chain->data, hmp->mchain);
798                         chain->data = NULL;
799                 }
800                 break;
801         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
802                 if (chain->data) {
803                         kfree(chain->data, hmp->mchain);
804                         chain->data = NULL;
805                 }
806                 break;
807         default:
808                 KKASSERT(chain->data == NULL);
809                 break;
810         }
811 }
812
813 /*
814  * Ref and lock a chain element, acquiring its data with I/O if necessary,
815  * and specify how you would like the data to be resolved.
816  *
817  * Returns 0 on success or an error code if the data could not be acquired.
818  * The chain element is locked on return regardless of whether an error
819  * occurred or not.
820  *
821  * The lock is allowed to recurse, multiple locking ops will aggregate
822  * the requested resolve types.  Once data is assigned it will not be
823  * removed until the last unlock.
824  *
825  * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
826  *                         (typically used to avoid device/logical buffer
827  *                          aliasing for data)
828  *
829  * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
830  *                         the INITIAL-create state (indirect blocks only).
831  *
832  *                         Do not resolve data elements for DATA chains.
833  *                         (typically used to avoid device/logical buffer
834  *                          aliasing for data)
835  *
836  * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
837  *
838  * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
839  *                         it will be locked exclusive.
840  *
841  * NOTE: Embedded elements (volume header, inodes) are always resolved
842  *       regardless.
843  *
844  * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
845  *       element will instantiate and zero its buffer, and flush it on
846  *       release.
847  *
848  * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
849  *       so as not to instantiate a device buffer, which could alias against
850  *       a logical file buffer.  However, if ALWAYS is specified the
851  *       device buffer will be instantiated anyway.
852  *
853  * WARNING! If data must be fetched a shared lock will temporarily be
854  *          upgraded to exclusive.  However, a deadlock can occur if
855  *          the caller owns more than one shared lock.
856  */
857 int
858 hammer2_chain_lock(hammer2_chain_t *chain, int how)
859 {
860         hammer2_mount_t *hmp;
861         hammer2_chain_core_t *core;
862         hammer2_blockref_t *bref;
863         ccms_state_t ostate;
864         char *bdata;
865         int error;
866
867         /*
868          * Ref and lock the element.  Recursive locks are allowed.
869          */
870         if ((how & HAMMER2_RESOLVE_NOREF) == 0)
871                 hammer2_chain_ref(chain);
872         atomic_add_int(&chain->lockcnt, 1);
873
874         hmp = chain->hmp;
875         KKASSERT(hmp != NULL);
876
877         /*
878          * Get the appropriate lock.
879          */
880         core = chain->core;
881         if (how & HAMMER2_RESOLVE_SHARED)
882                 ccms_thread_lock(&core->cst, CCMS_STATE_SHARED);
883         else
884                 ccms_thread_lock(&core->cst, CCMS_STATE_EXCLUSIVE);
885
886         /*
887          * If we already have a valid data pointer no further action is
888          * necessary.
889          */
890         if (chain->data)
891                 return (0);
892
893         /*
894          * Do we have to resolve the data?
895          */
896         switch(how & HAMMER2_RESOLVE_MASK) {
897         case HAMMER2_RESOLVE_NEVER:
898                 return(0);
899         case HAMMER2_RESOLVE_MAYBE:
900                 if (chain->flags & HAMMER2_CHAIN_INITIAL)
901                         return(0);
902                 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
903                         return(0);
904 #if 0
905                 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
906                         return(0);
907 #endif
908                 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
909                         return(0);
910                 /* fall through */
911         case HAMMER2_RESOLVE_ALWAYS:
912                 break;
913         }
914
915         /*
916          * Upgrade to an exclusive lock so we can safely manipulate the
917          * buffer cache.  If another thread got to it before us we
918          * can just return.
919          */
920         ostate = ccms_thread_lock_upgrade(&core->cst);
921         if (chain->data) {
922                 ccms_thread_lock_downgrade(&core->cst, ostate);
923                 return (0);
924         }
925
926         /*
927          * We must resolve to a device buffer, either by issuing I/O or
928          * by creating a zero-fill element.  We do not mark the buffer
929          * dirty when creating a zero-fill element (the hammer2_chain_modify()
930          * API must still be used to do that).
931          *
932          * The device buffer is variable-sized in powers of 2 down
933          * to HAMMER2_MIN_ALLOC (typically 1K).  A 64K physical storage
934          * chunk always contains buffers of the same size. (XXX)
935          *
936          * The minimum physical IO size may be larger than the variable
937          * block size.
938          */
939         bref = &chain->bref;
940
941         /*
942          * The getblk() optimization can only be used on newly created
943          * elements if the physical block size matches the request.
944          */
945         if (chain->flags & HAMMER2_CHAIN_INITIAL) {
946                 error = hammer2_io_new(hmp, bref->data_off, chain->bytes,
947                                         &chain->dio);
948         } else {
949                 error = hammer2_io_bread(hmp, bref->data_off, chain->bytes,
950                                          &chain->dio);
951                 adjreadcounter(&chain->bref, chain->bytes);
952         }
953
954         if (error) {
955                 kprintf("hammer2_chain_lock: I/O error %016jx: %d\n",
956                         (intmax_t)bref->data_off, error);
957                 hammer2_io_bqrelse(&chain->dio);
958                 ccms_thread_lock_downgrade(&core->cst, ostate);
959                 return (error);
960         }
961
962         /*
963          * We can clear the INITIAL state now, we've resolved the buffer
964          * to zeros and marked it dirty with hammer2_io_new().
965          */
966         bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
967         if (chain->flags & HAMMER2_CHAIN_INITIAL) {
968                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
969         }
970
971         /*
972          * Setup the data pointer, either pointing it to an embedded data
973          * structure and copying the data from the buffer, or pointing it
974          * into the buffer.
975          *
976          * The buffer is not retained when copying to an embedded data
977          * structure in order to avoid potential deadlocks or recursions
978          * on the same physical buffer.
979          */
980         switch (bref->type) {
981         case HAMMER2_BREF_TYPE_VOLUME:
982         case HAMMER2_BREF_TYPE_FREEMAP:
983                 /*
984                  * Copy data from bp to embedded buffer
985                  */
986                 panic("hammer2_chain_lock: called on unresolved volume header");
987                 break;
988         case HAMMER2_BREF_TYPE_INODE:
989                 /*
990                  * Copy data from dio to embedded buffer, do not retain the
991                  * device buffer.
992                  */
993                 KKASSERT(chain->bytes == sizeof(chain->data->ipdata));
994                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
995                 chain->data = kmalloc(sizeof(chain->data->ipdata),
996                                       hmp->mchain, M_WAITOK | M_ZERO);
997                 bcopy(bdata, &chain->data->ipdata, chain->bytes);
998                 hammer2_io_bqrelse(&chain->dio);
999                 break;
1000         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1001                 KKASSERT(chain->bytes == sizeof(chain->data->bmdata));
1002                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
1003                 chain->data = kmalloc(sizeof(chain->data->bmdata),
1004                                       hmp->mchain, M_WAITOK | M_ZERO);
1005                 bcopy(bdata, &chain->data->bmdata, chain->bytes);
1006                 hammer2_io_bqrelse(&chain->dio);
1007                 break;
1008         case HAMMER2_BREF_TYPE_INDIRECT:
1009         case HAMMER2_BREF_TYPE_DATA:
1010         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1011         default:
1012                 /*
1013                  * Point data at the device buffer and leave bp intact.
1014                  */
1015                 chain->data = (void *)bdata;
1016                 break;
1017         }
1018         ccms_thread_lock_downgrade(&core->cst, ostate);
1019         return (0);
1020 }
1021
1022 /*
1023  * This basically calls hammer2_io_breadcb() but does some pre-processing
1024  * of the chain first to handle certain cases.
1025  */
1026 void
1027 hammer2_chain_load_async(hammer2_chain_t *chain,
1028                          void (*callback)(hammer2_io_t *dio,
1029                                           hammer2_chain_t *chain,
1030                                           void *arg_p, off_t arg_o),
1031                          void *arg_p, off_t arg_o)
1032 {
1033         hammer2_mount_t *hmp;
1034         struct hammer2_io *dio;
1035         hammer2_blockref_t *bref;
1036         int error;
1037
1038         if (chain->data) {
1039                 callback(NULL, chain, arg_p, arg_o);
1040                 return;
1041         }
1042
1043         /*
1044          * We must resolve to a device buffer, either by issuing I/O or
1045          * by creating a zero-fill element.  We do not mark the buffer
1046          * dirty when creating a zero-fill element (the hammer2_chain_modify()
1047          * API must still be used to do that).
1048          *
1049          * The device buffer is variable-sized in powers of 2 down
1050          * to HAMMER2_MIN_ALLOC (typically 1K).  A 64K physical storage
1051          * chunk always contains buffers of the same size. (XXX)
1052          *
1053          * The minimum physical IO size may be larger than the variable
1054          * block size.
1055          */
1056         bref = &chain->bref;
1057         hmp = chain->hmp;
1058
1059         /*
1060          * The getblk() optimization can only be used on newly created
1061          * elements if the physical block size matches the request.
1062          */
1063         if ((chain->flags & HAMMER2_CHAIN_INITIAL) &&
1064             chain->bytes == hammer2_devblksize(chain->bytes)) {
1065                 error = hammer2_io_new(hmp, bref->data_off, chain->bytes, &dio);
1066                 KKASSERT(error == 0);
1067                 callback(dio, chain, arg_p, arg_o);
1068                 return;
1069         }
1070
1071         /*
1072          * Otherwise issue a read
1073          */
1074         adjreadcounter(&chain->bref, chain->bytes);
1075         hammer2_io_breadcb(hmp, bref->data_off, chain->bytes,
1076                            callback, chain, arg_p, arg_o);
1077 }
1078
1079 /*
1080  * Unlock and deref a chain element.
1081  *
1082  * On the last lock release any non-embedded data (chain->dio) will be
1083  * retired.
1084  */
1085 void
1086 hammer2_chain_unlock(hammer2_chain_t *chain)
1087 {
1088         hammer2_chain_core_t *core = chain->core;
1089         ccms_state_t ostate;
1090         long *counterp;
1091         u_int lockcnt;
1092
1093         /*
1094          * The core->cst lock can be shared across several chains so we
1095          * need to track the per-chain lockcnt separately.
1096          *
1097          * If multiple locks are present (or being attempted) on this
1098          * particular chain we can just unlock, drop refs, and return.
1099          *
1100          * Otherwise fall-through on the 1->0 transition.
1101          */
1102         for (;;) {
1103                 lockcnt = chain->lockcnt;
1104                 KKASSERT(lockcnt > 0);
1105                 cpu_ccfence();
1106                 if (lockcnt > 1) {
1107                         if (atomic_cmpset_int(&chain->lockcnt,
1108                                               lockcnt, lockcnt - 1)) {
1109                                 ccms_thread_unlock(&core->cst);
1110                                 hammer2_chain_drop(chain);
1111                                 return;
1112                         }
1113                 } else {
1114                         if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
1115                                 break;
1116                 }
1117                 /* retry */
1118         }
1119
1120         /*
1121          * On the 1->0 transition we upgrade the core lock (if necessary)
1122          * to exclusive for terminal processing.  If after upgrading we find
1123          * that lockcnt is non-zero, another thread is racing us and will
1124          * handle the unload for us later on, so just cleanup and return
1125          * leaving the data/io intact
1126          *
1127          * Otherwise if lockcnt is still 0 it is possible for it to become
1128          * non-zero and race, but since we hold the core->cst lock
1129          * exclusively all that will happen is that the chain will be
1130          * reloaded after we unload it.
1131          */
1132         ostate = ccms_thread_lock_upgrade(&core->cst);
1133         if (chain->lockcnt) {
1134                 ccms_thread_unlock_upgraded(&core->cst, ostate);
1135                 hammer2_chain_drop(chain);
1136                 return;
1137         }
1138
1139         /*
1140          * Shortcut the case if the data is embedded or not resolved.
1141          *
1142          * Do NOT NULL out chain->data (e.g. inode data), it might be
1143          * dirty.
1144          */
1145         if (chain->dio == NULL) {
1146                 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0)
1147                         hammer2_chain_drop_data(chain, 0);
1148                 ccms_thread_unlock_upgraded(&core->cst, ostate);
1149                 hammer2_chain_drop(chain);
1150                 return;
1151         }
1152
1153         /*
1154          * Statistics
1155          */
1156         if (hammer2_io_isdirty(chain->dio) == 0) {
1157                 ;
1158         } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
1159                 switch(chain->bref.type) {
1160                 case HAMMER2_BREF_TYPE_DATA:
1161                         counterp = &hammer2_ioa_file_write;
1162                         break;
1163                 case HAMMER2_BREF_TYPE_INODE:
1164                         counterp = &hammer2_ioa_meta_write;
1165                         break;
1166                 case HAMMER2_BREF_TYPE_INDIRECT:
1167                         counterp = &hammer2_ioa_indr_write;
1168                         break;
1169                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1170                 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1171                         counterp = &hammer2_ioa_fmap_write;
1172                         break;
1173                 default:
1174                         counterp = &hammer2_ioa_volu_write;
1175                         break;
1176                 }
1177                 *counterp += chain->bytes;
1178         } else {
1179                 switch(chain->bref.type) {
1180                 case HAMMER2_BREF_TYPE_DATA:
1181                         counterp = &hammer2_iod_file_write;
1182                         break;
1183                 case HAMMER2_BREF_TYPE_INODE:
1184                         counterp = &hammer2_iod_meta_write;
1185                         break;
1186                 case HAMMER2_BREF_TYPE_INDIRECT:
1187                         counterp = &hammer2_iod_indr_write;
1188                         break;
1189                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1190                 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1191                         counterp = &hammer2_iod_fmap_write;
1192                         break;
1193                 default:
1194                         counterp = &hammer2_iod_volu_write;
1195                         break;
1196                 }
1197                 *counterp += chain->bytes;
1198         }
1199
1200         /*
1201          * Clean out the dio.
1202          *
1203          * If a device buffer was used for data be sure to destroy the
1204          * buffer when we are done to avoid aliases (XXX what about the
1205          * underlying VM pages?).
1206          *
1207          * NOTE: Freemap leaf's use reserved blocks and thus no aliasing
1208          *       is possible.
1209          *
1210          * NOTE: The isdirty check tracks whether we have to bdwrite() the
1211          *       buffer or not.  The buffer might already be dirty.  The
1212          *       flag is re-set when chain_modify() is called, even if
1213          *       MODIFIED is already set, allowing the OS to retire the
1214          *       buffer independent of a hammer2 flush.
1215          */
1216         chain->data = NULL;
1217         if ((chain->flags & HAMMER2_CHAIN_IOFLUSH) &&
1218             hammer2_io_isdirty(chain->dio)) {
1219                 hammer2_io_bawrite(&chain->dio);
1220         } else {
1221                 hammer2_io_bqrelse(&chain->dio);
1222         }
1223         ccms_thread_unlock_upgraded(&core->cst, ostate);
1224         hammer2_chain_drop(chain);
1225 }
1226
1227 /*
1228  * This counts the number of live blockrefs in a block array and
1229  * also calculates the point at which all remaining blockrefs are empty.
1230  * This routine can only be called on a live chain (DUPLICATED flag not set).
1231  *
1232  * NOTE: Flag is not set until after the count is complete, allowing
1233  *       callers to test the flag without holding the spinlock.
1234  *
1235  * NOTE: If base is NULL the related chain is still in the INITIAL
1236  *       state and there are no blockrefs to count.
1237  *
1238  * NOTE: live_count may already have some counts accumulated due to
1239  *       creation and deletion and could even be initially negative.
1240  */
1241 void
1242 hammer2_chain_countbrefs(hammer2_chain_t *chain,
1243                          hammer2_blockref_t *base, int count)
1244 {
1245         hammer2_chain_core_t *core = chain->core;
1246
1247         KKASSERT((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0);
1248
1249         spin_lock(&core->cst.spin);
1250         if ((core->flags & HAMMER2_CORE_COUNTEDBREFS) == 0) {
1251                 if (base) {
1252                         while (--count >= 0) {
1253                                 if (base[count].type)
1254                                         break;
1255                         }
1256                         core->live_zero = count + 1;
1257                         while (count >= 0) {
1258                                 if (base[count].type)
1259                                         atomic_add_int(&core->live_count, 1);
1260                                 --count;
1261                         }
1262                 } else {
1263                         core->live_zero = 0;
1264                 }
1265                 /* else do not modify live_count */
1266                 atomic_set_int(&core->flags, HAMMER2_CORE_COUNTEDBREFS);
1267         }
1268         spin_unlock(&core->cst.spin);
1269 }
1270
1271 /*
1272  * Resize the chain's physical storage allocation in-place.  This may
1273  * replace the passed-in chain with a new chain.
1274  *
1275  * Chains can be resized smaller without reallocating the storage.
1276  * Resizing larger will reallocate the storage.
1277  *
1278  * Must be passed an exclusively locked parent and chain, returns a new
1279  * exclusively locked chain at the same index and unlocks the old chain.
1280  * Flushes the buffer if necessary.
1281  *
1282  * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
1283  * to avoid instantiating a device buffer that conflicts with the vnode
1284  * data buffer.  That is, the passed-in bp is a logical buffer, whereas
1285  * any chain-oriented bp would be a device buffer.
1286  *
1287  * XXX return error if cannot resize.
1288  */
1289 void
1290 hammer2_chain_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
1291                      hammer2_chain_t *parent, hammer2_chain_t **chainp,
1292                      int nradix, int flags)
1293 {
1294         hammer2_mount_t *hmp;
1295         hammer2_chain_t *chain;
1296         size_t obytes;
1297         size_t nbytes;
1298
1299         chain = *chainp;
1300         hmp = chain->hmp;
1301
1302         /*
1303          * Only data and indirect blocks can be resized for now.
1304          * (The volu root, inodes, and freemap elements use a fixed size).
1305          */
1306         KKASSERT(chain != &hmp->vchain);
1307         KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1308                  chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
1309
1310         /*
1311          * Nothing to do if the element is already the proper size
1312          */
1313         obytes = chain->bytes;
1314         nbytes = 1U << nradix;
1315         if (obytes == nbytes)
1316                 return;
1317
1318         /*
1319          * Delete the old chain and duplicate it at the same (parent, index),
1320          * returning a new chain.  This allows the old chain to still be
1321          * used by the flush code.  The new chain will be returned in a
1322          * modified state.
1323          *
1324          * The parent does not have to be locked for the delete/duplicate call,
1325          * but is in this particular code path.
1326          *
1327          * NOTE: If we are not crossing a synchronization point the
1328          *       duplication code will simply reuse the existing chain
1329          *       structure.
1330          */
1331         hammer2_chain_delete_duplicate(trans, &chain, 0);
1332
1333         /*
1334          * Relocate the block, even if making it smaller (because different
1335          * block sizes may be in different regions).
1336          */
1337         hammer2_freemap_alloc(trans, chain, nbytes);
1338         chain->bytes = nbytes;
1339         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_FORCECOW);
1340         /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */
1341
1342         /*
1343          * For now just support it on DATA chains (and not on indirect
1344          * blocks).
1345          */
1346         KKASSERT(chain->dio == NULL);
1347
1348 #if 0
1349         /*
1350          * Make sure the chain is marked MOVED and propagate the update
1351          * to the root for flush.
1352          */
1353         if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
1354                 hammer2_chain_ref(chain);
1355                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
1356         }
1357         hammer2_chain_setsubmod(trans, chain);
1358 #endif
1359         *chainp = chain;
1360 }
1361
1362 /*
1363  * Set a chain modified, making it read-write and duplicating it if necessary.
1364  * This function will assign a new physical block to the chain if necessary
1365  *
1366  * Duplication of already-modified chains is possible when the modification
1367  * crosses a flush synchronization boundary.
1368  *
1369  * Non-data blocks - The chain should be locked to at least the RESOLVE_MAYBE
1370  *                   level or the COW operation will not work.
1371  *
1372  * Data blocks     - The chain is usually locked RESOLVE_NEVER so as not to
1373  *                   run the data through the device buffers.
1374  *
1375  * This function may return a different chain than was passed, in which case
1376  * the old chain will be unlocked and the new chain will be locked.
1377  *
1378  * ip->chain may be adjusted by hammer2_chain_modify_ip().
1379  */
1380 hammer2_inode_data_t *
1381 hammer2_chain_modify_ip(hammer2_trans_t *trans, hammer2_inode_t *ip,
1382                         hammer2_chain_t **chainp, int flags)
1383 {
1384         atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1385         hammer2_chain_modify(trans, chainp, flags);
1386         if (ip->chain != *chainp)
1387                 hammer2_inode_repoint(ip, NULL, *chainp);
1388         if (ip->vp)
1389                 vsetisdirty(ip->vp);
1390         return(&ip->chain->data->ipdata);
1391 }
1392
1393 void
1394 hammer2_chain_modify(hammer2_trans_t *trans, hammer2_chain_t **chainp,
1395                      int flags)
1396 {
1397         hammer2_mount_t *hmp;
1398         hammer2_chain_t *chain;
1399         hammer2_io_t *dio;
1400         int error;
1401         int wasinitial;
1402         char *bdata;
1403
1404         chain = *chainp;
1405         hmp = chain->hmp;
1406
1407         KKASSERT(chain->bref.mirror_tid != trans->sync_tid ||
1408                  (chain->flags & HAMMER2_CHAIN_MODIFIED));
1409 #if 0
1410         if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP ||
1411             chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1412             chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1413                 kprintf("trans %04jx/%08x MODIFY1 %p.%d [%08x] %016jx/%d %016jx C/D %016jx/%016jx\n",
1414                         trans->sync_tid, trans->flags,
1415                         chain, chain->bref.type, chain->flags,
1416                         chain->bref.key, chain->bref.keybits,
1417                         chain->bref.data_off,
1418                         chain->modify_tid, chain->delete_tid);
1419         }
1420 #endif
1421 #if 0
1422         kprintf("MODIFY %p.%d flags %08x mod=%016jx del=%016jx\n", chain, chain->bref.type, chain->flags, chain->modify_tid, chain->delete_tid);
1423 #endif
1424         /*
1425          * Data must be resolved if already assigned unless explicitly
1426          * flagged otherwise.
1427          */
1428         if (chain->data == NULL && (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1429             (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
1430                 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1431                 hammer2_chain_unlock(chain);
1432         }
1433
1434         /*
1435          * data is not optional for freemap chains (we must always be sure
1436          * to copy the data on COW storage allocations).
1437          */
1438         if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1439             chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1440                 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1441                          (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1442         }
1443
1444         /*
1445          * Determine if a delete-duplicate is needed.
1446          *
1447          * (a) Modify_tid is part of a prior flush
1448          * (b) Transaction is concurrent with a flush (has higher tid)
1449          * (c) and chain is not in the initial state (freshly created)
1450          * (d) and caller didn't request an in-place modification.
1451          *
1452          * The freemap and volume header special chains are never D-Dd.
1453          */
1454         if (chain->modify_tid != trans->sync_tid &&        /* cross boundary */
1455             (flags & HAMMER2_MODIFY_INPLACE) == 0) {       /* from d-d */
1456                 if (chain != &hmp->fchain && chain != &hmp->vchain) {
1457                         KKASSERT((flags & HAMMER2_MODIFY_ASSERTNOCOPY) == 0);
1458                         hammer2_chain_delete_duplicate(trans, chainp, 0);
1459                         chain = *chainp;
1460 #if 0
1461         if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP ||
1462             chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1463             chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1464                 kprintf("trans %04jx/%08x MODIFY2 %p.%d [%08x] %016jx/%d %016jx\n",
1465                         trans->sync_tid, trans->flags,
1466                         chain, chain->bref.type, chain->flags,
1467                         chain->bref.key, chain->bref.keybits,
1468                         chain->bref.data_off);
1469                         return;
1470                 }
1471 #endif
1472                 }
1473
1474                 /*
1475                  * Fall through if fchain or vchain, clearing the CHAIN_FLUSHED
1476                  * flag.  Basically other chains are delete-duplicated and so
1477                  * the duplicated chains of course will not have the FLUSHED
1478                  * flag set, but fchain and vchain are special-cased and the
1479                  * flag must be cleared when changing modify_tid.
1480                  */
1481                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_FLUSHED);
1482         }
1483
1484         /*
1485          * Otherwise do initial-chain handling
1486          */
1487         if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1488                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1489                 hammer2_chain_ref(chain);
1490                 hammer2_chain_memory_inc(chain->pmp);
1491         }
1492 #if 0
1493         /* shouldn't be needed */
1494         if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
1495                 hammer2_chain_ref(chain);
1496                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
1497         }
1498 #endif
1499
1500         /*
1501          * The modification or re-modification requires an allocation and
1502          * possible COW.
1503          *
1504          * We normally always allocate new storage here.  If storage exists
1505          * and MODIFY_NOREALLOC is passed in, we do not allocate new storage.
1506          */
1507         if (chain != &hmp->vchain && chain != &hmp->fchain) {
1508                 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
1509                      ((flags & HAMMER2_MODIFY_NOREALLOC) == 0 &&
1510                       chain->modify_tid != trans->sync_tid)
1511                 ) {
1512                         hammer2_freemap_alloc(trans, chain, chain->bytes);
1513                         /* XXX failed allocation */
1514                 } else if (chain->flags & HAMMER2_CHAIN_FORCECOW) {
1515                         hammer2_freemap_alloc(trans, chain, chain->bytes);
1516                         /* XXX failed allocation */
1517                 }
1518                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_FORCECOW);
1519         }
1520
1521         /*
1522          * Update modify_tid.  XXX special-case vchain/fchain because they
1523          * are always modified in-place.  Otherwise the chain being modified
1524          * must not be part of a future transaction.
1525          */
1526         if (chain == &hmp->vchain || chain == &hmp->fchain) {
1527                 if (chain->modify_tid <= trans->sync_tid)
1528                         chain->modify_tid = trans->sync_tid;
1529         } else {
1530                 KKASSERT(chain->modify_tid <= trans->sync_tid);
1531                 chain->modify_tid = trans->sync_tid;
1532         }
1533
1534         if ((flags & HAMMER2_MODIFY_NO_MODIFY_TID) == 0)
1535                 chain->bref.modify_tid = trans->sync_tid;
1536
1537         /*
1538          * Do not COW if OPTDATA is set.  INITIAL flag remains unchanged.
1539          * (OPTDATA does not prevent [re]allocation of storage, only the
1540          * related copy-on-write op).
1541          */
1542         if (flags & HAMMER2_MODIFY_OPTDATA)
1543                 goto skip2;
1544
1545         /*
1546          * Clearing the INITIAL flag (for indirect blocks) indicates that
1547          * we've processed the uninitialized storage allocation.
1548          *
1549          * If this flag is already clear we are likely in a copy-on-write
1550          * situation but we have to be sure NOT to bzero the storage if
1551          * no data is present.
1552          */
1553         if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1554                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1555                 wasinitial = 1;
1556         } else {
1557                 wasinitial = 0;
1558         }
1559
1560         /*
1561          * Instantiate data buffer and possibly execute COW operation
1562          */
1563         switch(chain->bref.type) {
1564         case HAMMER2_BREF_TYPE_VOLUME:
1565         case HAMMER2_BREF_TYPE_FREEMAP:
1566         case HAMMER2_BREF_TYPE_INODE:
1567         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1568                 /*
1569                  * The data is embedded, no copy-on-write operation is
1570                  * needed.
1571                  */
1572                 KKASSERT(chain->dio == NULL);
1573                 break;
1574         case HAMMER2_BREF_TYPE_DATA:
1575         case HAMMER2_BREF_TYPE_INDIRECT:
1576         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1577                 /*
1578                  * Perform the copy-on-write operation
1579                  */
1580                 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1581
1582                 if (wasinitial) {
1583                         error = hammer2_io_new(hmp, chain->bref.data_off,
1584                                                chain->bytes, &dio);
1585                 } else {
1586                         error = hammer2_io_bread(hmp, chain->bref.data_off,
1587                                                  chain->bytes, &dio);
1588                 }
1589                 adjreadcounter(&chain->bref, chain->bytes);
1590                 KKASSERT(error == 0);
1591
1592                 bdata = hammer2_io_data(dio, chain->bref.data_off);
1593
1594                 /*
1595                  * Copy or zero-fill on write depending on whether
1596                  * chain->data exists or not and set the dirty state for
1597                  * the new buffer.  Retire the existing buffer.
1598                  */
1599                 if (chain->data) {
1600                         KKASSERT(chain->dio != NULL);
1601                         if (chain->data != (void *)bdata) {
1602                                 bcopy(chain->data, bdata, chain->bytes);
1603                         }
1604                 } else if (wasinitial == 0) {
1605                         /*
1606                          * We have a problem.  We were asked to COW but
1607                          * we don't have any data to COW with!
1608                          */
1609                         panic("hammer2_chain_modify: having a COW %p\n",
1610                               chain);
1611                 }
1612                 hammer2_io_brelse(&chain->dio);
1613                 chain->data = (void *)bdata;
1614                 chain->dio = dio;
1615                 hammer2_io_setdirty(dio);       /* modified by bcopy above */
1616                 break;
1617         default:
1618                 panic("hammer2_chain_modify: illegal non-embedded type %d",
1619                       chain->bref.type);
1620                 break;
1621
1622         }
1623 skip2:
1624 #if 0
1625         kprintf("RET2 %p.%d flags %08x mod=%016jx del=%016jx\n", chain, chain->bref.type, chain->flags, chain->modify_tid, chain->delete_tid);
1626 #endif
1627         hammer2_chain_setsubmod(trans, chain);
1628 }
1629
1630 /*
1631  * Mark the volume as having been modified.  This short-cut version
1632  * does not have to lock the volume's chain, which allows the ioctl
1633  * code to make adjustments to connections without deadlocking.  XXX
1634  *
1635  * No ref is made on vchain when flagging it MODIFIED.
1636  */
1637 void
1638 hammer2_modify_volume(hammer2_mount_t *hmp)
1639 {
1640         hammer2_voldata_lock(hmp);
1641         hammer2_voldata_unlock(hmp, 1);
1642 }
1643
1644 /*
1645  * This function returns the chain at the nearest key within the specified
1646  * range with the highest delete_tid.  The core spinlock must be held on
1647  * call and the returned chain will be referenced but not locked.
1648  *
1649  * The returned chain may or may not be in a deleted state.  Note that
1650  * live chains have a delete_tid = MAX_TID.
1651  *
1652  * This function will recurse through chain->rbtree as necessary and will
1653  * return a *key_nextp suitable for iteration.  *key_nextp is only set if
1654  * the iteration value is less than the current value of *key_nextp.
1655  *
1656  * The caller should use (*key_nextp) to calculate the actual range of
1657  * the returned element, which will be (key_beg to *key_nextp - 1), because
1658  * there might be another element which is superior to the returned element
1659  * and overlaps it.
1660  *
1661  * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
1662  * chains continue to be returned.  On EOF (*key_nextp) may overflow since
1663  * it will wind up being (key_end + 1).
1664  */
1665 struct hammer2_chain_find_info {
1666         hammer2_chain_t         *best;
1667         hammer2_key_t           key_beg;
1668         hammer2_key_t           key_end;
1669         hammer2_key_t           key_next;
1670 };
1671
1672 static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
1673 static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
1674
1675 /*
1676  * DEBUGGING - Assert that the chain will not collide.
1677  */
1678 static
1679 void
1680 hammer2_chain_assert_not_present(hammer2_chain_core_t *core,
1681                                  hammer2_chain_t *chain)
1682 {
1683         struct hammer2_chain_find_info info;
1684         hammer2_chain_layer_t *layer;
1685
1686         if (chain->flags & HAMMER2_CHAIN_DELETED)
1687                 return;
1688
1689         info.best = NULL;
1690         info.key_beg = chain->bref.key;
1691         info.key_end = chain->bref.key +
1692                        ((hammer2_key_t)1 << chain->bref.keybits) - 1;
1693         info.key_next = HAMMER2_MAX_KEY;
1694
1695         TAILQ_FOREACH(layer, &core->layerq, entry) {
1696                 KKASSERT(layer->good == 0xABCD);
1697                 RB_SCAN(hammer2_chain_tree, &layer->rbtree,
1698                         hammer2_chain_find_cmp, hammer2_chain_find_callback,
1699                         &info);
1700         }
1701         if (info.best && (info.best->flags & HAMMER2_CHAIN_DELETED) == 0)
1702                 panic("hammer2_chain_assert_not_present: %p/%p\n",
1703                         chain, info.best);
1704 }
1705
1706 static
1707 hammer2_chain_t *
1708 hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
1709                           hammer2_key_t key_beg, hammer2_key_t key_end)
1710 {
1711         struct hammer2_chain_find_info info;
1712         hammer2_chain_layer_t *layer;
1713
1714         info.best = NULL;
1715         info.key_beg = key_beg;
1716         info.key_end = key_end;
1717         info.key_next = *key_nextp;
1718
1719         KKASSERT(parent->core->good == 0x1234);
1720         TAILQ_FOREACH(layer, &parent->core->layerq, entry) {
1721                 KKASSERT(layer->good == 0xABCD);
1722                 RB_SCAN(hammer2_chain_tree, &layer->rbtree,
1723                         hammer2_chain_find_cmp, hammer2_chain_find_callback,
1724                         &info);
1725         }
1726         *key_nextp = info.key_next;
1727 #if 0
1728         kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
1729                 parent, key_beg, key_end, *key_nextp);
1730 #endif
1731
1732         return (info.best);
1733 }
1734
1735 static
1736 int
1737 hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
1738 {
1739         struct hammer2_chain_find_info *info = data;
1740         hammer2_key_t child_beg;
1741         hammer2_key_t child_end;
1742
1743         child_beg = child->bref.key;
1744         child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
1745
1746         if (child_end < info->key_beg)
1747                 return(-1);
1748         if (child_beg > info->key_end)
1749                 return(1);
1750         return(0);
1751 }
1752
1753 static
1754 int
1755 hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
1756 {
1757         struct hammer2_chain_find_info *info = data;
1758         hammer2_chain_t *best;
1759         hammer2_key_t child_end;
1760
1761         /*
1762          * WARNING! Do not discard DUPLICATED chains, it is possible that
1763          *          we are catching an insertion half-way done.  If a
1764          *          duplicated chain turns out to be the best choice the
1765          *          caller will re-check its flags after locking it.
1766          *
1767          * WARNING! Layerq is scanned forwards, exact matches should keep
1768          *          the existing info->best.
1769          */
1770         if ((best = info->best) == NULL) {
1771                 /*
1772                  * No previous best.  Assign best
1773                  */
1774                 info->best = child;
1775         } else if (best->bref.key <= info->key_beg &&
1776                    child->bref.key <= info->key_beg) {
1777                 /*
1778                  * If our current best is flush with key_beg and child is
1779                  * also flush with key_beg choose based on delete_tid.
1780                  *
1781                  * key_next will automatically be limited to the smaller of
1782                  * the two end-points.
1783                  */
1784                 if (child->delete_tid > best->delete_tid)
1785                         info->best = child;
1786         } else if (child->bref.key < best->bref.key) {
1787                 /*
1788                  * Child has a nearer key and best is not flush with key_beg.
1789                  * Truncate key_next to the old best key iff it had a better
1790                  * delete_tid.
1791                  */
1792                 info->best = child;
1793                 if (best->delete_tid >= child->delete_tid &&
1794                     (info->key_next > best->bref.key || info->key_next == 0))
1795                         info->key_next = best->bref.key;
1796         } else if (child->bref.key == best->bref.key) {
1797                 /*
1798                  * If our current best is flush with the child then choose
1799                  * based on delete_tid.
1800                  *
1801                  * key_next will automatically be limited to the smaller of
1802                  * the two end-points.
1803                  */
1804                 if (child->delete_tid > best->delete_tid)
1805                         info->best = child;
1806         } else {
1807                 /*
1808                  * Keep the current best but truncate key_next to the child's
1809                  * base iff the child has a higher delete_tid.
1810                  *
1811                  * key_next will also automatically be limited to the smaller
1812                  * of the two end-points (probably not necessary for this case
1813                  * but we do it anyway).
1814                  */
1815                 if (child->delete_tid >= best->delete_tid &&
1816                     (info->key_next > child->bref.key || info->key_next == 0))
1817                         info->key_next = child->bref.key;
1818         }
1819
1820         /*
1821          * Always truncate key_next based on child's end-of-range.
1822          */
1823         child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
1824         if (child_end && (info->key_next > child_end || info->key_next == 0))
1825                 info->key_next = child_end;
1826
1827         return(0);
1828 }
1829
1830 /*
1831  * Retrieve the specified chain from a media blockref, creating the
1832  * in-memory chain structure which reflects it.  modify_tid will be
1833  * left 0 which forces any modifications to issue a delete-duplicate.
1834  *
1835  * To handle insertion races pass the INSERT_RACE flag along with the
1836  * generation number of the core.  NULL will be returned if the generation
1837  * number changes before we have a chance to insert the chain.  Insert
1838  * races can occur because the parent might be held shared.
1839  *
1840  * Caller must hold the parent locked shared or exclusive since we may
1841  * need the parent's bref array to find our block.
1842  */
1843 hammer2_chain_t *
1844 hammer2_chain_get(hammer2_chain_t *parent, hammer2_blockref_t *bref,
1845                   int generation)
1846 {
1847         hammer2_mount_t *hmp = parent->hmp;
1848         hammer2_chain_core_t *above = parent->core;
1849         hammer2_chain_t *chain;
1850         int error;
1851
1852         /*
1853          * Allocate a chain structure representing the existing media
1854          * entry.  Resulting chain has one ref and is not locked.
1855          */
1856         chain = hammer2_chain_alloc(hmp, parent->pmp, NULL, bref);
1857         hammer2_chain_core_alloc(NULL, chain, NULL);
1858         /* ref'd chain returned */
1859         chain->modify_tid = chain->bref.mirror_tid;
1860
1861         /*
1862          * Link the chain into its parent.  A spinlock is required to safely
1863          * access the RBTREE, and it is possible to collide with another
1864          * hammer2_chain_get() operation because the caller might only hold
1865          * a shared lock on the parent.
1866          */
1867         KKASSERT(parent->refs > 0);
1868         error = hammer2_chain_insert(above, NULL, chain,
1869                                      HAMMER2_CHAIN_INSERT_SPIN |
1870                                      HAMMER2_CHAIN_INSERT_RACE,
1871                                      generation);
1872         if (error) {
1873                 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
1874                 kprintf("chain %p get race\n", chain);
1875                 hammer2_chain_drop(chain);
1876                 chain = NULL;
1877         } else {
1878                 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
1879         }
1880
1881         /*
1882          * Return our new chain referenced but not locked, or NULL if
1883          * a race occurred.
1884          */
1885         return (chain);
1886 }
1887
1888 /*
1889  * Lookup initialization/completion API
1890  */
1891 hammer2_chain_t *
1892 hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
1893 {
1894         if (flags & HAMMER2_LOOKUP_SHARED) {
1895                 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1896                                            HAMMER2_RESOLVE_SHARED);
1897         } else {
1898                 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1899         }
1900         return (parent);
1901 }
1902
1903 void
1904 hammer2_chain_lookup_done(hammer2_chain_t *parent)
1905 {
1906         if (parent)
1907                 hammer2_chain_unlock(parent);
1908 }
1909
1910 static
1911 hammer2_chain_t *
1912 hammer2_chain_getparent(hammer2_chain_t **parentp, int how)
1913 {
1914         hammer2_chain_t *oparent;
1915         hammer2_chain_t *bparent;
1916         hammer2_chain_t *nparent;
1917         hammer2_chain_core_t *above;
1918
1919         oparent = *parentp;
1920         above = oparent->above;
1921
1922         spin_lock(&above->cst.spin);
1923         bparent = TAILQ_FIRST(&above->ownerq);
1924         hammer2_chain_ref(bparent);
1925
1926         /*
1927          * Be careful of order, oparent must be unlocked before nparent
1928          * is locked below to avoid a deadlock.  We might as well delay its
1929          * unlocking until we conveniently no longer have the spinlock (instead
1930          * of cycling the spinlock).
1931          *
1932          * Theoretically our ref on bparent should prevent elements of the
1933          * following chain from going away and prevent above from going away,
1934          * but we still need the spinlock to safely scan the list.
1935          */
1936         for (;;) {
1937                 nparent = bparent;
1938                 while (nparent->flags & HAMMER2_CHAIN_DUPLICATED)
1939                         nparent = TAILQ_NEXT(nparent, core_entry);
1940                 hammer2_chain_ref(nparent);
1941                 spin_unlock(&above->cst.spin);
1942
1943                 if (oparent) {
1944                         hammer2_chain_unlock(oparent);
1945                         oparent = NULL;
1946                 }
1947                 hammer2_chain_lock(nparent, how | HAMMER2_RESOLVE_NOREF);
1948                 hammer2_chain_drop(bparent);
1949
1950                 /*
1951                  * We might have raced a delete-duplicate.
1952                  */
1953                 if ((nparent->flags & HAMMER2_CHAIN_DUPLICATED) == 0)
1954                         break;
1955                 bparent = nparent;
1956                 hammer2_chain_ref(bparent);
1957                 hammer2_chain_unlock(nparent);
1958                 spin_lock(&above->cst.spin);
1959                 /* retry */
1960         }
1961         *parentp = nparent;
1962
1963         return (nparent);
1964 }
1965
1966 /*
1967  * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
1968  * (*parentp) typically points to an inode but can also point to a related
1969  * indirect block and this function will recurse upwards and find the inode
1970  * again.
1971  *
1972  * (*parentp) must be exclusively locked and referenced and can be an inode
1973  * or an existing indirect block within the inode.
1974  *
1975  * On return (*parentp) will be modified to point at the deepest parent chain
1976  * element encountered during the search, as a helper for an insertion or
1977  * deletion.   The new (*parentp) will be locked and referenced and the old
1978  * will be unlocked and dereferenced (no change if they are both the same).
1979  *
1980  * The matching chain will be returned exclusively locked.  If NOLOCK is
1981  * requested the chain will be returned only referenced.
1982  *
1983  * NULL is returned if no match was found, but (*parentp) will still
1984  * potentially be adjusted.
1985  *
1986  * On return (*key_nextp) will point to an iterative value for key_beg.
1987  * (If NULL is returned (*key_nextp) is set to key_end).
1988  *
1989  * This function will also recurse up the chain if the key is not within the
1990  * current parent's range.  (*parentp) can never be set to NULL.  An iteration
1991  * can simply allow (*parentp) to float inside the loop.
1992  *
1993  * NOTE!  chain->data is not always resolved.  By default it will not be
1994  *        resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF.  Use
1995  *        HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
1996  *        BREF_TYPE_DATA as the device buffer can alias the logical file
1997  *        buffer).
1998  */
1999 hammer2_chain_t *
2000 hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
2001                      hammer2_key_t key_beg, hammer2_key_t key_end,
2002                      int *cache_indexp, int flags)
2003 {
2004         hammer2_mount_t *hmp;
2005         hammer2_chain_t *parent;
2006         hammer2_chain_t *chain;
2007         hammer2_blockref_t *base;
2008         hammer2_blockref_t *bref;
2009         hammer2_blockref_t bcopy;
2010         hammer2_key_t scan_beg;
2011         hammer2_key_t scan_end;
2012         hammer2_chain_core_t *above;
2013         int count = 0;
2014         int how_always = HAMMER2_RESOLVE_ALWAYS;
2015         int how_maybe = HAMMER2_RESOLVE_MAYBE;
2016         int how;
2017         int generation;
2018         int maxloops = 300000;
2019         int wasdup;
2020         hammer2_chain_t * volatile xxchain = NULL;
2021         volatile int xxchainwhy;
2022
2023         if (flags & HAMMER2_LOOKUP_ALWAYS) {
2024                 how_maybe = how_always;
2025                 how = HAMMER2_RESOLVE_ALWAYS;
2026         } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2027                 how = HAMMER2_RESOLVE_NEVER;
2028         } else {
2029                 how = HAMMER2_RESOLVE_MAYBE;
2030         }
2031         if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK)) {
2032                 how_maybe |= HAMMER2_RESOLVE_SHARED;
2033                 how_always |= HAMMER2_RESOLVE_SHARED;
2034                 how |= HAMMER2_RESOLVE_SHARED;
2035         }
2036
2037         /*
2038          * Recurse (*parentp) upward if necessary until the parent completely
2039          * encloses the key range or we hit the inode.
2040          *
2041          * This function handles races against the flusher doing a delete-
2042          * duplicate above us and re-homes the parent to the duplicate in
2043          * that case, otherwise we'd wind up recursing down a stale chain.
2044          */
2045         parent = *parentp;
2046         hmp = parent->hmp;
2047
2048         while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2049                parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2050                 scan_beg = parent->bref.key;
2051                 scan_end = scan_beg +
2052                            ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2053                 if (key_beg >= scan_beg && key_end <= scan_end)
2054                         break;
2055                 parent = hammer2_chain_getparent(parentp, how_maybe);
2056         }
2057
2058 again:
2059         if (--maxloops == 0)
2060                 panic("hammer2_chain_lookup: maxloops");
2061         /*
2062          * Locate the blockref array.  Currently we do a fully associative
2063          * search through the array.
2064          */
2065         switch(parent->bref.type) {
2066         case HAMMER2_BREF_TYPE_INODE:
2067                 /*
2068                  * Special shortcut for embedded data returns the inode
2069                  * itself.  Callers must detect this condition and access
2070                  * the embedded data (the strategy code does this for us).
2071                  *
2072                  * This is only applicable to regular files and softlinks.
2073                  */
2074                 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
2075                         if (flags & HAMMER2_LOOKUP_NOLOCK)
2076                                 hammer2_chain_ref(parent);
2077                         else
2078                                 hammer2_chain_lock(parent, how_always);
2079                         *key_nextp = key_end + 1;
2080                         return (parent);
2081                 }
2082                 base = &parent->data->ipdata.u.blockset.blockref[0];
2083                 count = HAMMER2_SET_COUNT;
2084                 break;
2085         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2086         case HAMMER2_BREF_TYPE_INDIRECT:
2087                 /*
2088                  * Handle MATCHIND on the parent
2089                  */
2090                 if (flags & HAMMER2_LOOKUP_MATCHIND) {
2091                         scan_beg = parent->bref.key;
2092                         scan_end = scan_beg +
2093                                ((hammer2_key_t)1 << parent->bref.keybits) - 1;
2094                         if (key_beg == scan_beg && key_end == scan_end) {
2095                                 chain = parent;
2096                                 hammer2_chain_lock(chain, how_maybe);
2097                                 *key_nextp = scan_end + 1;
2098                                 goto done;
2099                         }
2100                 }
2101                 /*
2102                  * Optimize indirect blocks in the INITIAL state to avoid
2103                  * I/O.
2104                  */
2105                 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2106                         base = NULL;
2107                 } else {
2108                         if (parent->data == NULL)
2109                                 panic("parent->data is NULL");
2110                         base = &parent->data->npdata[0];
2111                 }
2112                 count = parent->bytes / sizeof(hammer2_blockref_t);
2113                 break;
2114         case HAMMER2_BREF_TYPE_VOLUME:
2115                 base = &hmp->voldata.sroot_blockset.blockref[0];
2116                 count = HAMMER2_SET_COUNT;
2117                 break;
2118         case HAMMER2_BREF_TYPE_FREEMAP:
2119                 base = &hmp->voldata.freemap_blockset.blockref[0];
2120                 count = HAMMER2_SET_COUNT;
2121                 break;
2122         default:
2123                 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2124                       parent->bref.type);
2125                 base = NULL;    /* safety */
2126                 count = 0;      /* safety */
2127         }
2128
2129         /*
2130          * Merged scan to find next candidate.
2131          *
2132          * hammer2_base_*() functions require the above->live_* fields
2133          * to be synchronized.
2134          *
2135          * We need to hold the spinlock to access the block array and RB tree
2136          * and to interlock chain creation.
2137          */
2138         above = parent->core;
2139         if ((parent->core->flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
2140                 hammer2_chain_countbrefs(parent, base, count);
2141
2142         /*
2143          * Combined search
2144          */
2145         spin_lock(&above->cst.spin);
2146         chain = hammer2_combined_find(parent, base, count,
2147                                       cache_indexp, key_nextp,
2148                                       key_beg, key_end, &bref);
2149         generation = above->generation;
2150
2151         /*
2152          * Exhausted parent chain, iterate.
2153          */
2154         if (bref == NULL) {
2155                 spin_unlock(&above->cst.spin);
2156                 if (key_beg == key_end) /* short cut single-key case */
2157                         return (NULL);
2158                 return (hammer2_chain_next(parentp, NULL, key_nextp,
2159                                            key_beg, key_end,
2160                                            cache_indexp, flags));
2161         }
2162
2163         /*
2164          * Selected from blockref or in-memory chain.
2165          */
2166         if (chain == NULL) {
2167                 bcopy = *bref;
2168                 spin_unlock(&above->cst.spin);
2169                 chain = hammer2_chain_get(parent, &bcopy, generation);
2170                 if (chain == NULL) {
2171                         kprintf("retry lookup parent %p keys %016jx:%016jx\n",
2172                                 parent, key_beg, key_end);
2173                         goto again;
2174                 }
2175                 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2176                         xxchain = chain;
2177                         xxchainwhy = 1;
2178                         hammer2_chain_drop(chain);
2179                         goto again;
2180                 }
2181                 wasdup = 0;
2182         } else {
2183                 hammer2_chain_ref(chain);
2184                 wasdup = ((chain->flags & HAMMER2_CHAIN_DUPLICATED) != 0);
2185                 spin_unlock(&above->cst.spin);
2186         }
2187
2188         /*
2189          * chain is referenced but not locked.  We must lock the chain
2190          * to obtain definitive DUPLICATED/DELETED state
2191          */
2192         if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2193             chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2194                 hammer2_chain_lock(chain, how_maybe | HAMMER2_RESOLVE_NOREF);
2195         } else {
2196                 hammer2_chain_lock(chain, how | HAMMER2_RESOLVE_NOREF);
2197         }
2198
2199         /*
2200          * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2201          *
2202          * NOTE: Chain's key range is not relevant as there might be
2203          *       one-offs within the range that are not deleted.
2204          *
2205          * NOTE: Lookups can race delete-duplicate because
2206          *       delete-duplicate does not lock the parent's core
2207          *       (they just use the spinlock on the core).  We must
2208          *       check for races by comparing the DUPLICATED flag before
2209          *       releasing the spinlock with the flag after locking the
2210          *       chain.
2211          */
2212         if (chain->flags & HAMMER2_CHAIN_DELETED) {
2213                 hammer2_chain_unlock(chain);
2214                 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0 || wasdup) {
2215                         key_beg = *key_nextp;
2216                         if (key_beg == 0 || key_beg > key_end)
2217                                 return(NULL);
2218                 }
2219                 xxchain = chain;
2220                         xxchainwhy = 2;
2221                 goto again;
2222         }
2223
2224         /*
2225          * If the chain element is an indirect block it becomes the new
2226          * parent and we loop on it.  We must maintain our top-down locks
2227          * to prevent the flusher from interfering (i.e. doing a
2228          * delete-duplicate and leaving us recursing down a deleted chain).
2229          *
2230          * The parent always has to be locked with at least RESOLVE_MAYBE
2231          * so we can access its data.  It might need a fixup if the caller
2232          * passed incompatible flags.  Be careful not to cause a deadlock
2233          * as a data-load requires an exclusive lock.
2234          *
2235          * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
2236          * range is within the requested key range we return the indirect
2237          * block and do NOT loop.  This is usually only used to acquire
2238          * freemap nodes.
2239          */
2240         if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
2241             chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2242                 hammer2_chain_unlock(parent);
2243                 *parentp = parent = chain;
2244                 xxchain = chain;
2245                         xxchainwhy = 3;
2246                 goto again;
2247         }
2248 done:
2249         /*
2250          * All done, return the chain
2251          */
2252         XXChain = chain;
2253         XXChainWhy = xxchainwhy;
2254         return (chain);
2255 }
2256
2257 /*
2258  * After having issued a lookup we can iterate all matching keys.
2259  *
2260  * If chain is non-NULL we continue the iteration from just after it's index.
2261  *
2262  * If chain is NULL we assume the parent was exhausted and continue the
2263  * iteration at the next parent.
2264  *
2265  * parent must be locked on entry and remains locked throughout.  chain's
2266  * lock status must match flags.  Chain is always at least referenced.
2267  *
2268  * WARNING!  The MATCHIND flag does not apply to this function.
2269  */
2270 hammer2_chain_t *
2271 hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
2272                    hammer2_key_t *key_nextp,
2273                    hammer2_key_t key_beg, hammer2_key_t key_end,
2274                    int *cache_indexp, int flags)
2275 {
2276         hammer2_chain_t *parent;
2277         int how_maybe;
2278
2279         /*
2280          * Calculate locking flags for upward recursion.
2281          */
2282         how_maybe = HAMMER2_RESOLVE_MAYBE;
2283         if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK))
2284                 how_maybe |= HAMMER2_RESOLVE_SHARED;
2285
2286         parent = *parentp;
2287
2288         /*
2289          * Calculate the next index and recalculate the parent if necessary.
2290          */
2291         if (chain) {
2292                 key_beg = chain->bref.key +
2293                           ((hammer2_key_t)1 << chain->bref.keybits);
2294                 if (flags & HAMMER2_LOOKUP_NOLOCK)
2295                         hammer2_chain_drop(chain);
2296                 else
2297                         hammer2_chain_unlock(chain);
2298
2299                 /*
2300                  * Any scan where the lookup returned degenerate data embedded
2301                  * in the inode has an invalid index and must terminate.
2302                  */
2303                 if (chain == parent)
2304                         return(NULL);
2305                 if (key_beg == 0 || key_beg > key_end)
2306                         return(NULL);
2307                 chain = NULL;
2308         } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
2309                    parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
2310                 /*
2311                  * We reached the end of the iteration.
2312                  */
2313                 return (NULL);
2314         } else {
2315                 /*
2316                  * Continue iteration with next parent unless the current
2317                  * parent covers the range.
2318                  */
2319                 key_beg = parent->bref.key +
2320                           ((hammer2_key_t)1 << parent->bref.keybits);
2321                 if (key_beg == 0 || key_beg > key_end)
2322                         return (NULL);
2323                 parent = hammer2_chain_getparent(parentp, how_maybe);
2324         }
2325
2326         /*
2327          * And execute
2328          */
2329         return (hammer2_chain_lookup(parentp, key_nextp,
2330                                      key_beg, key_end,
2331                                      cache_indexp, flags));
2332 }
2333
2334 /*
2335  * Raw scan functions are similar to lookup/next but do not seek the parent
2336  * chain and do not skip stale chains.  These functions are primarily used
2337  * by the recovery code.
2338  *
2339  * Parent and chain are locked, parent's data must be resolved.  To acquire
2340  * the first sub-chain under parent pass chain == NULL.
2341  */
2342 hammer2_chain_t *
2343 hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t *chain,
2344                    int *cache_indexp, int flags)
2345 {
2346         hammer2_mount_t *hmp;
2347         hammer2_blockref_t *base;
2348         hammer2_blockref_t *bref;
2349         hammer2_blockref_t bcopy;
2350         hammer2_chain_core_t *above;
2351         hammer2_key_t key;
2352         hammer2_key_t next_key;
2353         int count = 0;
2354         int how_always = HAMMER2_RESOLVE_ALWAYS;
2355         int how_maybe = HAMMER2_RESOLVE_MAYBE;
2356         int how;
2357         int generation;
2358         int maxloops = 300000;
2359         int wasdup;
2360
2361         hmp = parent->hmp;
2362
2363         /*
2364          * Scan flags borrowed from lookup
2365          */
2366         if (flags & HAMMER2_LOOKUP_ALWAYS) {
2367                 how_maybe = how_always;
2368                 how = HAMMER2_RESOLVE_ALWAYS;
2369         } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2370                 how = HAMMER2_RESOLVE_NEVER;
2371         } else {
2372                 how = HAMMER2_RESOLVE_MAYBE;
2373         }
2374         if (flags & (HAMMER2_LOOKUP_SHARED | HAMMER2_LOOKUP_NOLOCK)) {
2375                 how_maybe |= HAMMER2_RESOLVE_SHARED;
2376                 how_always |= HAMMER2_RESOLVE_SHARED;
2377                 how |= HAMMER2_RESOLVE_SHARED;
2378         }
2379
2380         /*
2381          * Calculate key to locate first/next element, unlocking the previous
2382          * element as we go.  Be careful, the key calculation can overflow.
2383          */
2384         if (chain) {
2385                 key = chain->bref.key +
2386                       ((hammer2_key_t)1 << chain->bref.keybits);
2387                 hammer2_chain_unlock(chain);
2388                 chain = NULL;
2389                 if (key == 0)
2390                         goto done;
2391         } else {
2392                 key = 0;
2393         }
2394
2395 again:
2396         if (--maxloops == 0)
2397                 panic("hammer2_chain_scan: maxloops");
2398         /*
2399          * Locate the blockref array.  Currently we do a fully associative
2400          * search through the array.
2401          */
2402         switch(parent->bref.type) {
2403         case HAMMER2_BREF_TYPE_INODE:
2404                 /*
2405                  * An inode with embedded data has no sub-chains.
2406                  */
2407                 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA)
2408                         goto done;
2409                 base = &parent->data->ipdata.u.blockset.blockref[0];
2410                 count = HAMMER2_SET_COUNT;
2411                 break;
2412         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2413         case HAMMER2_BREF_TYPE_INDIRECT:
2414                 /*
2415                  * Optimize indirect blocks in the INITIAL state to avoid
2416                  * I/O.
2417                  */
2418                 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2419                         base = NULL;
2420                 } else {
2421                         if (parent->data == NULL)
2422                                 panic("parent->data is NULL");
2423                         base = &parent->data->npdata[0];
2424                 }
2425                 count = parent->bytes / sizeof(hammer2_blockref_t);
2426                 break;
2427         case HAMMER2_BREF_TYPE_VOLUME:
2428                 base = &hmp->voldata.sroot_blockset.blockref[0];
2429                 count = HAMMER2_SET_COUNT;
2430                 break;
2431         case HAMMER2_BREF_TYPE_FREEMAP:
2432                 base = &hmp->voldata.freemap_blockset.blockref[0];
2433                 count = HAMMER2_SET_COUNT;
2434                 break;
2435         default:
2436                 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2437                       parent->bref.type);
2438                 base = NULL;    /* safety */
2439                 count = 0;      /* safety */
2440         }
2441
2442         /*
2443          * Merged scan to find next candidate.
2444          *
2445          * hammer2_base_*() functions require the above->live_* fields
2446          * to be synchronized.
2447          *
2448          * We need to hold the spinlock to access the block array and RB tree
2449          * and to interlock chain creation.
2450          */
2451         if ((parent->core->flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
2452                 hammer2_chain_countbrefs(parent, base, count);
2453
2454         above = parent->core;
2455         next_key = 0;
2456         spin_lock(&above->cst.spin);
2457         chain = hammer2_combined_find(parent, base, count,
2458                                       cache_indexp, &next_key,
2459                                       key, HAMMER2_MAX_KEY, &bref);
2460         generation = above->generation;
2461
2462         /*
2463          * Exhausted parent chain, we're done.
2464          */
2465         if (bref == NULL) {
2466                 spin_unlock(&above->cst.spin);
2467                 KKASSERT(chain == NULL);
2468                 goto done;
2469         }
2470
2471         /*
2472          * Selected from blockref or in-memory chain.
2473          */
2474         if (chain == NULL) {
2475                 bcopy = *bref;
2476                 spin_unlock(&above->cst.spin);
2477                 chain = hammer2_chain_get(parent, &bcopy, generation);
2478                 if (chain == NULL) {
2479                         kprintf("retry scan parent %p keys %016jx\n",
2480                                 parent, key);
2481                         goto again;
2482                 }
2483                 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2484                         hammer2_chain_drop(chain);
2485                         chain = NULL;
2486                         goto again;
2487                 }
2488                 wasdup = 0;
2489         } else {
2490                 hammer2_chain_ref(chain);
2491                 wasdup = ((chain->flags & HAMMER2_CHAIN_DUPLICATED) != 0);
2492                 spin_unlock(&above->cst.spin);
2493         }
2494
2495         /*
2496          * chain is referenced but not locked.  We must lock the chain
2497          * to obtain definitive DUPLICATED/DELETED state
2498          */
2499         hammer2_chain_lock(chain, how | HAMMER2_RESOLVE_NOREF);
2500
2501         /*
2502          * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2503          *
2504          * NOTE: chain's key range is not relevant as there might be
2505          *       one-offs within the range that are not deleted.
2506          *
2507          * NOTE: XXX this could create problems with scans used in
2508          *       situations other than mount-time recovery.
2509          *
2510          * NOTE: Lookups can race delete-duplicate because
2511          *       delete-duplicate does not lock the parent's core
2512          *       (they just use the spinlock on the core).  We must
2513          *       check for races by comparing the DUPLICATED flag before
2514          *       releasing the spinlock with the flag after locking the
2515          *       chain.
2516          */
2517         if (chain->flags & HAMMER2_CHAIN_DELETED) {
2518                 hammer2_chain_unlock(chain);
2519                 chain = NULL;
2520
2521                 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0 || wasdup) {
2522                         key = next_key;
2523                         if (key == 0)
2524                                 goto done;
2525                 }
2526                 goto again;
2527         }
2528
2529 done:
2530         /*
2531          * All done, return the chain or NULL
2532          */
2533         return (chain);
2534 }
2535
2536 /*
2537  * Create and return a new hammer2 system memory structure of the specified
2538  * key, type and size and insert it under (*parentp).  This is a full
2539  * insertion, based on the supplied key/keybits, and may involve creating
2540  * indirect blocks and moving other chains around via delete/duplicate.
2541  *
2542  * (*parentp) must be exclusive locked and may be replaced on return
2543  * depending on how much work the function had to do.
2544  *
2545  * (*chainp) usually starts out NULL and returns the newly created chain,
2546  * but if the caller desires the caller may allocate a disconnected chain
2547  * and pass it in instead.  (It is also possible for the caller to use
2548  * chain_duplicate() to create a disconnected chain, manipulate it, then
2549  * pass it into this function to insert it).
2550  *
2551  * This function should NOT be used to insert INDIRECT blocks.  It is
2552  * typically used to create/insert inodes and data blocks.
2553  *
2554  * Caller must pass-in an exclusively locked parent the new chain is to
2555  * be inserted under, and optionally pass-in a disconnected, exclusively
2556  * locked chain to insert (else we create a new chain).  The function will
2557  * adjust (*parentp) as necessary, create or connect the chain, and
2558  * return an exclusively locked chain in *chainp.
2559  */
2560 int
2561 hammer2_chain_create(hammer2_trans_t *trans, hammer2_chain_t **parentp,
2562                      hammer2_chain_t **chainp,
2563                      hammer2_key_t key, int keybits, int type, size_t bytes)
2564 {
2565         hammer2_mount_t *hmp;
2566         hammer2_chain_t *chain;
2567         hammer2_chain_t *parent = *parentp;
2568         hammer2_chain_core_t *above;
2569         hammer2_blockref_t *base;
2570         hammer2_blockref_t dummy;
2571         int allocated = 0;
2572         int error = 0;
2573         int count;
2574         int maxloops = 300000;
2575
2576         above = parent->core;
2577         KKASSERT(ccms_thread_lock_owned(&above->cst));
2578         hmp = parent->hmp;
2579         chain = *chainp;
2580
2581         if (chain == NULL) {
2582                 /*
2583                  * First allocate media space and construct the dummy bref,
2584                  * then allocate the in-memory chain structure.  Set the
2585                  * INITIAL flag for fresh chains which do not have embedded
2586                  * data.
2587                  */
2588                 bzero(&dummy, sizeof(dummy));
2589                 dummy.type = type;
2590                 dummy.key = key;
2591                 dummy.keybits = keybits;
2592                 dummy.data_off = hammer2_getradix(bytes);
2593                 dummy.methods = parent->bref.methods;
2594                 chain = hammer2_chain_alloc(hmp, parent->pmp, trans, &dummy);
2595                 hammer2_chain_core_alloc(trans, chain, NULL);
2596
2597                 /*
2598                  * Lock the chain manually, chain_lock will load the chain
2599                  * which we do NOT want to do.  (note: chain->refs is set
2600                  * to 1 by chain_alloc() for us, but lockcnt is not).
2601                  */
2602                 chain->lockcnt = 1;
2603                 ccms_thread_lock(&chain->core->cst, CCMS_STATE_EXCLUSIVE);
2604                 allocated = 1;
2605
2606                 /*
2607                  * We do NOT set INITIAL here (yet).  INITIAL is only
2608                  * used for indirect blocks.
2609                  *
2610                  * Recalculate bytes to reflect the actual media block
2611                  * allocation.
2612                  */
2613                 bytes = (hammer2_off_t)1 <<
2614                         (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
2615                 chain->bytes = bytes;
2616
2617                 switch(type) {
2618                 case HAMMER2_BREF_TYPE_VOLUME:
2619                 case HAMMER2_BREF_TYPE_FREEMAP:
2620                         panic("hammer2_chain_create: called with volume type");
2621                         break;
2622                 case HAMMER2_BREF_TYPE_INODE:
2623                         KKASSERT(bytes == HAMMER2_INODE_BYTES);
2624                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
2625                         chain->data = kmalloc(sizeof(chain->data->ipdata),
2626                                               hmp->mchain, M_WAITOK | M_ZERO);
2627                         break;
2628                 case HAMMER2_BREF_TYPE_INDIRECT:
2629                         panic("hammer2_chain_create: cannot be used to"
2630                               "create indirect block");
2631                         break;
2632                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2633                         panic("hammer2_chain_create: cannot be used to"
2634                               "create freemap root or node");
2635                         break;
2636                 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2637                         KKASSERT(bytes == sizeof(chain->data->bmdata));
2638                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_EMBEDDED);
2639                         chain->data = kmalloc(sizeof(chain->data->bmdata),
2640                                               hmp->mchain, M_WAITOK | M_ZERO);
2641                         break;
2642                 case HAMMER2_BREF_TYPE_DATA:
2643                 default:
2644                         /*
2645                          * leave chain->data NULL, set INITIAL
2646                          */
2647                         KKASSERT(chain->data == NULL);
2648                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
2649                         break;
2650                 }
2651         } else {
2652                 /*
2653                  * We are reattaching a chain that has been duplicated and
2654                  * left disconnected under a DIFFERENT parent with potentially
2655                  * different key/keybits.
2656                  *
2657                  * The chain must be modified in the current transaction
2658                  * (the duplication code should have done that for us),
2659                  * and it's modify_tid should be greater than the parent's
2660                  * bref.mirror_tid.  This should cause it to be created under
2661                  * the new parent.
2662                  *
2663                  * If deleted in the same transaction, the create/delete TIDs
2664                  * will be the same and effective the chain will not have
2665                  * existed at all from the point of view of the parent.
2666                  *
2667                  * Do NOT mess with the current state of the INITIAL flag.
2668                  */
2669                 KKASSERT(chain->modify_tid > parent->bref.mirror_tid);
2670                 KKASSERT(chain->modify_tid == trans->sync_tid);
2671                 chain->bref.key = key;
2672                 chain->bref.keybits = keybits;
2673                 /* chain->modify_tid = chain->bref.mirror_tid; */
2674                 KKASSERT(chain->above == NULL);
2675         }
2676
2677         /*
2678          * Calculate how many entries we have in the blockref array and
2679          * determine if an indirect block is required.
2680          */
2681 again:
2682         if (--maxloops == 0)
2683                 panic("hammer2_chain_create: maxloops");
2684         above = parent->core;
2685
2686         switch(parent->bref.type) {
2687         case HAMMER2_BREF_TYPE_INODE:
2688                 KKASSERT((parent->data->ipdata.op_flags &
2689                           HAMMER2_OPFLAG_DIRECTDATA) == 0);
2690                 KKASSERT(parent->data != NULL);
2691                 base = &parent->data->ipdata.u.blockset.blockref[0];
2692                 count = HAMMER2_SET_COUNT;
2693                 break;
2694         case HAMMER2_BREF_TYPE_INDIRECT:
2695         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2696                 if (parent->flags & HAMMER2_CHAIN_INITIAL)
2697                         base = NULL;
2698                 else
2699                         base = &parent->data->npdata[0];
2700                 count = parent->bytes / sizeof(hammer2_blockref_t);
2701                 break;
2702         case HAMMER2_BREF_TYPE_VOLUME:
2703                 KKASSERT(parent->data != NULL);
2704                 base = &hmp->voldata.sroot_blockset.blockref[0];
2705                 count = HAMMER2_SET_COUNT;
2706                 break;
2707         case HAMMER2_BREF_TYPE_FREEMAP:
2708                 KKASSERT(parent->data != NULL);
2709                 base = &hmp->voldata.freemap_blockset.blockref[0];
2710                 count = HAMMER2_SET_COUNT;
2711                 break;
2712         default:
2713                 panic("hammer2_chain_create: unrecognized blockref type: %d",
2714                       parent->bref.type);
2715                 base = NULL;
2716                 count = 0;
2717                 break;
2718         }
2719
2720         /*
2721          * Make sure we've counted the brefs
2722          */
2723         if ((parent->core->flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
2724                 hammer2_chain_countbrefs(parent, base, count);
2725
2726         KKASSERT(above->live_count >= 0 && above->live_count <= count);
2727
2728         /*
2729          * If no free blockref could be found we must create an indirect
2730          * block and move a number of blockrefs into it.  With the parent
2731          * locked we can safely lock each child in order to delete+duplicate
2732          * it without causing a deadlock.
2733          *
2734          * This may return the new indirect block or the old parent depending
2735          * on where the key falls.  NULL is returned on error.
2736          */
2737         if (above->live_count == count) {
2738                 hammer2_chain_t *nparent;
2739
2740                 nparent = hammer2_chain_create_indirect(trans, parent,
2741                                                         key, keybits,
2742                                                         type, &error);
2743                 if (nparent == NULL) {
2744                         if (allocated)
2745                                 hammer2_chain_drop(chain);
2746                         chain = NULL;
2747                         goto done;
2748                 }
2749                 if (parent != nparent) {
2750                         hammer2_chain_unlock(parent);
2751                         parent = *parentp = nparent;
2752                 }
2753                 goto again;
2754         }
2755
2756         /*
2757          * Link the chain into its parent.  Later on we will have to set
2758          * the MOVED bit in situations where we don't mark the new chain
2759          * as being modified.
2760          */
2761         if (chain->above != NULL)
2762                 panic("hammer2: hammer2_chain_create: chain already connected");
2763         KKASSERT(chain->above == NULL);
2764         KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
2765         hammer2_chain_insert(above, NULL, chain,
2766                              HAMMER2_CHAIN_INSERT_SPIN |
2767                              HAMMER2_CHAIN_INSERT_LIVE,
2768                              0);
2769
2770         if (allocated) {
2771                 /*
2772                  * Mark the newly created chain modified.
2773                  *
2774                  * Device buffers are not instantiated for DATA elements
2775                  * as these are handled by logical buffers.
2776                  *
2777                  * Indirect and freemap node indirect blocks are handled
2778                  * by hammer2_chain_create_indirect() and not by this
2779                  * function.
2780                  *
2781                  * Data for all other bref types is expected to be
2782                  * instantiated (INODE, LEAF).
2783                  */
2784                 switch(chain->bref.type) {
2785                 case HAMMER2_BREF_TYPE_DATA:
2786                         hammer2_chain_modify(trans, &chain,
2787                                              HAMMER2_MODIFY_OPTDATA |
2788                                              HAMMER2_MODIFY_ASSERTNOCOPY);
2789                         break;
2790                 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2791                 case HAMMER2_BREF_TYPE_INODE:
2792                         hammer2_chain_modify(trans, &chain,
2793                                              HAMMER2_MODIFY_ASSERTNOCOPY);
2794                         break;
2795                 default:
2796                         /*
2797                          * Remaining types are not supported by this function.
2798                          * In particular, INDIRECT and LEAF_NODE types are
2799                          * handled by create_indirect().
2800                          */
2801                         panic("hammer2_chain_create: bad type: %d",
2802                               chain->bref.type);
2803                         /* NOT REACHED */
2804                         break;
2805                 }
2806         } else {
2807                 /*
2808                  * When reconnecting a chain we must set MOVED and setsubmod
2809                  * so the flush recognizes that it must update the bref in
2810                  * the parent.
2811                  */
2812                 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
2813                         hammer2_chain_ref(chain);
2814                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
2815                 }
2816         }
2817         hammer2_chain_setsubmod(trans, chain);
2818
2819 done:
2820         *chainp = chain;
2821
2822         return (error);
2823 }
2824
2825 /*
2826  * Replace (*chainp) with a duplicate in-memory chain structure which shares
2827  * the same core and media state as the orignal.  The original *chainp is
2828  * unlocked and the replacement will be returned locked.
2829  *
2830  * The old chain must be in a DELETED state unless snapshot is non-zero.
2831  *
2832  * The new chain will be live (i.e. not deleted), and modified.
2833  *
2834  * If (parent) is non-NULL then the new duplicated chain is inserted under
2835  * the parent.
2836  *
2837  * If (parent) is NULL then the newly duplicated chain is not inserted
2838  * anywhere, similar to if it had just been chain_alloc()'d (suitable for
2839  * passing into hammer2_chain_create() after this function returns).
2840  *
2841  * WARNING! This function cannot take snapshots all by itself.  The caller
2842  *          needs to do other massaging for snapshots.
2843  *
2844  * WARNING! This function calls create which means it can insert indirect
2845  *          blocks.  Callers may have to refactor locked chains held across
2846  *          the call (other than the ones passed into the call).
2847  */
2848 static void hammer2_chain_dup_fixup(hammer2_chain_t *ochain,
2849                                     hammer2_chain_t *nchain);
2850
2851 void
2852 hammer2_chain_duplicate(hammer2_trans_t *trans, hammer2_chain_t **parentp,
2853                         hammer2_chain_t **chainp, hammer2_blockref_t *bref,
2854                         int snapshot, int duplicate_reason)
2855 {
2856         hammer2_mount_t *hmp;
2857         hammer2_chain_t *parent;
2858         hammer2_chain_t *ochain;
2859         hammer2_chain_t *nchain;
2860         hammer2_chain_core_t *above;
2861         size_t bytes;
2862
2863         /*
2864          * We want nchain to be our go-to live chain, but ochain may be in
2865          * a MODIFIED state within the current flush synchronization segment.
2866          * Force any further modifications of ochain to do another COW
2867          * operation even if modify_tid indicates that one is not needed.
2868          *
2869          * We don't want to set FORCECOW on nchain simply as an optimization,
2870          * as many duplication calls simply move chains into ichains and
2871          * then delete the original.
2872          *
2873          * WARNING!  We should never resolve DATA to device buffers
2874          *           (XXX allow it if the caller did?), and since
2875          *           we currently do not have the logical buffer cache
2876          *           buffer in-hand to fix its cached physical offset
2877          *           we also force the modify code to not COW it. XXX
2878          */
2879         ochain = *chainp;
2880         hmp = ochain->hmp;
2881         KKASSERT(snapshot == 1 || (ochain->flags & HAMMER2_CHAIN_DELETED));
2882
2883         atomic_set_int(&ochain->flags, HAMMER2_CHAIN_FORCECOW);
2884
2885         /*
2886          * Now create a duplicate of the chain structure, associating
2887          * it with the same core, making it the same size, pointing it
2888          * to the same bref (the same media block).
2889          *
2890          * Give the duplicate the same modify_tid that we previously
2891          * ensured was sufficiently advanced to trigger a block table
2892          * insertion on flush.
2893          *
2894          * NOTE: bref.mirror_tid duplicated by virtue of bref copy in
2895          *       hammer2_chain_alloc()
2896          */
2897         if (bref == NULL)
2898                 bref = &ochain->bref;
2899         if (snapshot) {
2900                 nchain = hammer2_chain_alloc(hmp, NULL, trans, bref);
2901                 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_SNAPSHOT);
2902         } else {
2903                 nchain = hammer2_chain_alloc(hmp, ochain->pmp, trans, bref);
2904         }
2905         hammer2_chain_core_alloc(trans, nchain, ochain);
2906         bytes = (hammer2_off_t)1 <<
2907                 (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
2908         nchain->bytes = bytes;
2909         nchain->modify_tid = ochain->modify_tid;
2910         nchain->inode_reason = ochain->inode_reason + 0x100000;
2911         if (ochain->flags & HAMMER2_CHAIN_INITIAL)
2912                 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_INITIAL);
2913         if (ochain->flags & HAMMER2_CHAIN_UNLINKED)
2914                 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_UNLINKED);
2915
2916         /*
2917          * Fixup (copy) any embedded data.  Non-embedded data relies on the
2918          * media block.  We must unlock ochain before we can access nchain's
2919          * media block because they might share the same bp and deadlock if
2920          * we don't.
2921          */
2922         hammer2_chain_lock(nchain, HAMMER2_RESOLVE_NEVER |
2923                                    HAMMER2_RESOLVE_NOREF);
2924         hammer2_chain_dup_fixup(ochain, nchain);
2925         /* nchain has 1 ref */
2926         hammer2_chain_unlock(ochain);
2927         KKASSERT((ochain->flags & HAMMER2_CHAIN_EMBEDDED) ||
2928                  ochain->data == NULL);
2929
2930         /*
2931          * Place nchain in the modified state, instantiate media data
2932          * if necessary.  Because modify_tid is already completely
2933          * synchronized this should not result in a delete-duplicate.
2934          *
2935          * We want nchain at the target to look like a new insertion.
2936          * Forcing the modification to be INPLACE accomplishes this
2937          * because we get the same nchain with an updated modify_tid.
2938          */
2939         if (nchain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2940                 hammer2_chain_modify(trans, &nchain,
2941                                      HAMMER2_MODIFY_OPTDATA |
2942                                      HAMMER2_MODIFY_NOREALLOC |
2943                                      HAMMER2_MODIFY_INPLACE);
2944         } else if (nchain->flags & HAMMER2_CHAIN_INITIAL) {
2945                 hammer2_chain_modify(trans, &nchain,
2946                                      HAMMER2_MODIFY_OPTDATA |
2947                                      HAMMER2_MODIFY_INPLACE);
2948         } else {
2949                 hammer2_chain_modify(trans, &nchain,
2950                                      HAMMER2_MODIFY_INPLACE);
2951         }
2952
2953         /*
2954          * If parent is not NULL the duplicated chain will be entered under
2955          * the parent and the MOVED bit set.
2956          *
2957          * Having both chains locked is extremely important for atomicy.
2958          */
2959         if (parentp && (parent = *parentp) != NULL) {
2960                 above = parent->core;
2961                 KKASSERT(ccms_thread_lock_owned(&above->cst));
2962                 KKASSERT((nchain->flags & HAMMER2_CHAIN_DELETED) == 0);
2963                 KKASSERT(parent->refs > 0);
2964
2965                 hammer2_chain_create(trans, parentp, &nchain,
2966                                      nchain->bref.key, nchain->bref.keybits,
2967                                      nchain->bref.type, nchain->bytes);
2968                 parent = NULL;
2969
2970                 if ((nchain->flags & HAMMER2_CHAIN_MOVED) == 0) {
2971                         hammer2_chain_ref(nchain);
2972                         atomic_set_int(&nchain->flags, HAMMER2_CHAIN_MOVED);
2973                 }
2974                 hammer2_chain_setsubmod(trans, nchain);
2975         }
2976
2977 #if 0
2978         /*
2979          * Unconditionally set MOVED to force the parent blockrefs to
2980          * update, and adjust update_hi below nchain so nchain's
2981          * blockrefs are updated with the new attachment.
2982          */
2983         if (nchain->core->update_hi < trans->sync_tid) {
2984                 spin_lock(&nchain->core->cst.spin);
2985                 if (nchain->core->update_hi < trans->sync_tid)
2986                         nchain->core->update_hi = trans->sync_tid;
2987                 spin_unlock(&nchain->core->cst.spin);
2988         }
2989 #endif
2990
2991         *chainp = nchain;
2992 }
2993
2994 /*
2995  * Special in-place delete-duplicate sequence which does not require a
2996  * locked parent.  (*chainp) is marked DELETED and atomically replaced
2997  * with a duplicate.  Atomicy is at the very-fine spin-lock level in
2998  * order to ensure that lookups do not race us.
2999  *
3000  * If the old chain is already marked deleted the new chain will also be
3001  * marked deleted.  This case can occur when an inode is removed from the
3002  * filesystem but programs still have an open descriptor to it, and during
3003  * flushes when the flush needs to operate on a chain that is deleted in
3004  * the live view but still alive in the flush view.
3005  *
3006  * The new chain will be marked modified for the current transaction.
3007  */
3008 void
3009 hammer2_chain_delete_duplicate(hammer2_trans_t *trans, hammer2_chain_t **chainp,
3010                                int flags)
3011 {
3012         hammer2_mount_t *hmp;
3013         hammer2_chain_t *ochain;
3014         hammer2_chain_t *nchain;
3015         hammer2_chain_core_t *above;
3016         size_t bytes;
3017
3018         if (hammer2_debug & 0x20000)
3019                 Debugger("dd");
3020
3021         /*
3022          * Note that we do not have to call setsubmod on ochain, calling it
3023          * on nchain is sufficient.
3024          */
3025         ochain = *chainp;
3026         hmp = ochain->hmp;
3027
3028         if (ochain->bref.type == HAMMER2_BREF_TYPE_INODE) {
3029                 KKASSERT(ochain->data);
3030         }
3031
3032         /*
3033          * First create a duplicate of the chain structure.
3034          * (nchain is allocated with one ref).
3035          *
3036          * In the case where nchain inherits ochains core, nchain is
3037          * effectively locked due to ochain being locked (and sharing the
3038          * core), until we can give nchain its own official ock.
3039          */
3040         nchain = hammer2_chain_alloc(hmp, ochain->pmp, trans, &ochain->bref);
3041         if (flags & HAMMER2_DELDUP_RECORE)
3042                 hammer2_chain_core_alloc(trans, nchain, NULL);
3043         else
3044                 hammer2_chain_core_alloc(trans, nchain, ochain);
3045         above = ochain->above;
3046
3047         bytes = (hammer2_off_t)1 <<
3048                 (int)(ochain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
3049         nchain->bytes = bytes;
3050
3051         /*
3052          * Duplicate inherits ochain's live state including its modification
3053          * state.  This function disposes of the original.  Because we are
3054          * doing this in-place under the same parent the block array
3055          * inserted/deleted state does not change.
3056          *
3057          * The caller isn't expected to make further modifications of ochain
3058          * but set the FORCECOW bit anyway, just in case it does.  If ochain
3059          * was previously marked FORCECOW we also flag nchain FORCECOW
3060          * (used during hardlink splits).  FORCECOW forces a reallocation
3061          * of the block when we modify the chain a little later, it does
3062          * not force another delete-duplicate.
3063          *
3064          * NOTE: bref.mirror_tid duplicated by virtue of bref copy in
3065          *       hammer2_chain_alloc()
3066          */
3067         nchain->data_count += ochain->data_count;
3068         nchain->inode_count += ochain->inode_count;
3069         atomic_set_int(&nchain->flags,
3070                        ochain->flags & (HAMMER2_CHAIN_INITIAL |
3071                                         HAMMER2_CHAIN_FORCECOW |
3072                                         HAMMER2_CHAIN_UNLINKED));
3073         atomic_set_int(&ochain->flags, HAMMER2_CHAIN_FORCECOW);
3074         nchain->inode_reason = ochain->inode_reason + 0x1000;
3075
3076         /*
3077          * Lock nchain so both chains are now locked (extremely important
3078          * for atomicy).  Mark ochain deleted and reinsert into the topology
3079          * and insert nchain all in one go.
3080          *
3081          * If the ochain is already deleted it is left alone and nchain
3082          * is inserted into the topology as a deleted chain.  This is
3083          * important because it allows ongoing operations to be executed
3084          * on a deleted inode which still has open descriptors.
3085          *
3086          * The deleted case can also occur when a flush delete-duplicates
3087          * a node which is being concurrently modified by ongoing operations
3088          * in a later transaction.  This creates a problem because the flush
3089          * is intended to update blockrefs which then propagate, allowing
3090          * the original covering in-memory chains to be freed up.  In this
3091          * situation the flush code does NOT free the original covering
3092          * chains and will re-apply them to successive copies.
3093          */
3094         hammer2_chain_lock(nchain, HAMMER2_RESOLVE_NEVER);
3095         hammer2_chain_dup_fixup(ochain, nchain);
3096         /* extra ref still present from original allocation */
3097
3098         KKASSERT(ochain->flags & HAMMER2_CHAIN_ONRBTREE);
3099         spin_lock(&above->cst.spin);
3100         KKASSERT(ochain->flags & HAMMER2_CHAIN_ONRBTREE);
3101
3102         /*
3103          * Ultimately nchain->modify_tid will be set to trans->sync_tid,
3104          * but we can't do that here because we want to call
3105          * hammer2_chain_modify() to reallocate the block (if necessary).
3106          */
3107         nchain->modify_tid = ochain->modify_tid;
3108
3109         if (ochain->flags & HAMMER2_CHAIN_DELETED) {
3110                 /*
3111                  * ochain was deleted
3112                  */
3113                 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_DELETED);
3114                 if (ochain->delete_tid > trans->sync_tid) {
3115                         /*
3116                          * delete-duplicate a chain deleted in a later
3117                          * transaction.  Only allowed on chains created
3118                          * before or during the current transaction (flush
3119                          * code should filter out chains created after the
3120                          * current transaction).
3121                          *
3122                          * To make this work is a bit of a hack.  We convert
3123                          * ochain's delete_tid to the current sync_tid and
3124                          * create a nchain which sets up ochains original
3125                          * delete_tid.
3126                          *
3127                          * This effectively forces ochain to flush as a
3128                          * deletion and nchain as a creation.  Thus MOVED
3129                          * must be set in ochain (it should already be
3130                          * set since it's original delete_tid could not
3131                          * have been flushed yet).  Since ochain's delete_tid
3132                          * has been moved down to sync_tid, a re-flush at
3133                          * sync_tid won't try to delete-duplicate ochain
3134                          * again.
3135                          */
3136                         KKASSERT(ochain->modify_tid <= trans->sync_tid);
3137                         nchain->delete_tid = ochain->delete_tid;
3138                         ochain->delete_tid = trans->sync_tid;
3139                         KKASSERT(ochain->flags & HAMMER2_CHAIN_MOVED);
3140                 } else if (ochain->delete_tid == trans->sync_tid) {
3141                         /*
3142                          * ochain was deleted in the current transaction
3143                          */
3144                         nchain->delete_tid = trans->sync_tid;
3145                 } else {
3146                         /*
3147                          * ochain was deleted in a prior transaction.
3148                          * create and delete nchain in the current
3149                          * transaction.
3150                          *
3151                          * (delete_tid might represent a deleted inode
3152                          *  which still has an open descriptor).
3153                          */
3154                         nchain->delete_tid = trans->sync_tid;
3155                 }
3156                 hammer2_chain_insert(above, ochain->inlayer, nchain, 0, 0);
3157         } else {
3158                 /*
3159                  * ochain was not deleted, delete it in the current
3160                  * transaction.
3161                  */
3162                 KKASSERT(trans->sync_tid >= ochain->modify_tid);
3163                 ochain->delete_tid = trans->sync_tid;
3164                 atomic_set_int(&ochain->flags, HAMMER2_CHAIN_DELETED);
3165                 atomic_add_int(&above->live_count, -1);
3166                 hammer2_chain_insert(above, NULL, nchain,
3167                                      HAMMER2_CHAIN_INSERT_LIVE, 0);
3168         }
3169
3170         if ((ochain->flags & HAMMER2_CHAIN_MOVED) == 0) {
3171                 hammer2_chain_ref(ochain);
3172                 atomic_set_int(&ochain->flags, HAMMER2_CHAIN_MOVED);
3173         }
3174         spin_unlock(&above->cst.spin);
3175
3176         /*
3177          * ochain must be unlocked because ochain and nchain might share
3178          * a buffer cache buffer, so we need to release it so nchain can
3179          * potentially obtain it.
3180          */
3181         hammer2_chain_unlock(ochain);
3182
3183         /*
3184          * Finishing fixing up nchain.  A new block will be allocated if
3185          * crossing a synchronization point (meta-data only).
3186          *
3187          * Calling hammer2_chain_modify() will update modify_tid to
3188          * (typically) trans->sync_tid.
3189          */
3190         if (nchain->bref.type == HAMMER2_BREF_TYPE_DATA) {
3191                 hammer2_chain_modify(trans, &nchain,
3192                                      HAMMER2_MODIFY_OPTDATA |
3193                                      HAMMER2_MODIFY_NOREALLOC |
3194                                      HAMMER2_MODIFY_INPLACE);
3195         } else if (nchain->flags & HAMMER2_CHAIN_INITIAL) {
3196                 hammer2_chain_modify(trans, &nchain,
3197                                      HAMMER2_MODIFY_OPTDATA |
3198                                      HAMMER2_MODIFY_INPLACE);
3199         } else {
3200                 hammer2_chain_modify(trans, &nchain,
3201                                      HAMMER2_MODIFY_INPLACE);
3202         }
3203         hammer2_chain_drop(nchain);
3204
3205         /*
3206          * Unconditionally set MOVED to force the parent blockrefs to
3207          * update as the chain_modify() above won't necessarily do it.
3208          *
3209          * Adjust update_hi below nchain so nchain's blockrefs are updated
3210          * with the new attachment.
3211          */
3212         if ((nchain->flags & HAMMER2_CHAIN_MOVED) == 0) {
3213                 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_MOVED);
3214                 hammer2_chain_ref(nchain);
3215         }
3216 #if 0
3217         if (nchain->core->update_hi < trans->sync_tid) {
3218                 spin_lock(&nchain->core->cst.spin);
3219                 if (nchain->core->update_hi < trans->sync_tid)
3220                         nchain->core->update_hi = trans->sync_tid;
3221                 spin_unlock(&nchain->core->cst.spin);
3222         }
3223 #endif
3224         hammer2_chain_setsubmod(trans, nchain);
3225         *chainp = nchain;
3226 }
3227
3228 /*
3229  * Helper function to fixup inodes.  The caller procedure stack may hold
3230  * multiple locks on ochain if it represents an inode, preventing our
3231  * unlock from retiring its state to the buffer cache.
3232  *
3233  * In this situation any attempt to access the buffer cache could result
3234  * either in stale data or a deadlock.  Work around the problem by copying
3235  * the embedded data directly.
3236  */
3237 static
3238 void
3239 hammer2_chain_dup_fixup(hammer2_chain_t *ochain, hammer2_chain_t *nchain)
3240 {
3241         if (ochain->data == NULL)
3242                 return;
3243         switch(ochain->bref.type) {
3244         case HAMMER2_BREF_TYPE_INODE:
3245                 KKASSERT(nchain->data == NULL);
3246                 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_EMBEDDED);
3247                 nchain->data = kmalloc(sizeof(nchain->data->ipdata),
3248                                        ochain->hmp->mchain, M_WAITOK | M_ZERO);
3249                 nchain->data->ipdata = ochain->data->ipdata;
3250                 break;
3251         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
3252                 KKASSERT(nchain->data == NULL);
3253                 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_EMBEDDED);
3254                 nchain->data = kmalloc(sizeof(nchain->data->bmdata),
3255                                        ochain->hmp->mchain, M_WAITOK | M_ZERO);
3256                 bcopy(ochain->data->bmdata,
3257                       nchain->data->bmdata,
3258                       sizeof(nchain->data->bmdata));
3259                 break;
3260         default:
3261                 break;
3262         }
3263 }
3264
3265 /*
3266  * Create a snapshot of the specified {parent, ochain} with the specified
3267  * label.  The originating hammer2_inode must be exclusively locked for
3268  * safety.
3269  *
3270  * The ioctl code has already synced the filesystem.
3271  */
3272 int
3273 hammer2_chain_snapshot(hammer2_trans_t *trans, hammer2_chain_t **ochainp,
3274                        hammer2_ioc_pfs_t *pfs)
3275 {
3276         hammer2_mount_t *hmp;
3277         hammer2_chain_t *ochain = *ochainp;
3278         hammer2_chain_t *nchain;
3279         hammer2_inode_data_t *ipdata;
3280         hammer2_inode_t *nip;
3281         size_t name_len;
3282         hammer2_key_t lhc;
3283         struct vattr vat;
3284         uuid_t opfs_clid;
3285         int error;
3286
3287         kprintf("snapshot %s ochain->refs %d ochain->flags %08x\n",
3288                 pfs->name, ochain->refs, ochain->flags);
3289
3290         name_len = strlen(pfs->name);
3291         lhc = hammer2_dirhash(pfs->name, name_len);
3292
3293         hmp = ochain->hmp;
3294         opfs_clid = ochain->data->ipdata.pfs_clid;
3295
3296         *ochainp = ochain;
3297
3298         /*
3299          * Create the snapshot directory under the super-root
3300          *
3301          * Set PFS type, generate a unique filesystem id, and generate
3302          * a cluster id.  Use the same clid when snapshotting a PFS root,
3303          * which theoretically allows the snapshot to be used as part of
3304          * the same cluster (perhaps as a cache).
3305          *
3306          * Copy the (flushed) ochain's blockref array.  Theoretically we
3307          * could use chain_duplicate() but it becomes difficult to disentangle
3308          * the shared core so for now just brute-force it.
3309          */
3310         VATTR_NULL(&vat);
3311         vat.va_type = VDIR;
3312         vat.va_mode = 0755;
3313         nchain = NULL;
3314         nip = hammer2_inode_create(trans, hmp->sroot, &vat, proc0.p_ucred,
3315                                    pfs->name, name_len, &nchain, &error);
3316
3317         if (nip) {
3318                 ipdata = hammer2_chain_modify_ip(trans, nip, &nchain, 0);
3319                 ipdata->pfs_type = HAMMER2_PFSTYPE_SNAPSHOT;
3320                 kern_uuidgen(&ipdata->pfs_fsid, 1);
3321                 if (ochain->flags & HAMMER2_CHAIN_PFSROOT)
3322                         ipdata->pfs_clid = opfs_clid;
3323                 else
3324                         kern_uuidgen(&ipdata->pfs_clid, 1);
3325                 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_PFSROOT);
3326                 ipdata->u.blockset = ochain->data->ipdata.u.blockset;
3327
3328                 hammer2_inode_unlock_ex(nip, nchain);
3329         }
3330         return (error);
3331 }
3332
3333 /*
3334  * Create an indirect block that covers one or more of the elements in the
3335  * current parent.  Either returns the existing parent with no locking or
3336  * ref changes or returns the new indirect block locked and referenced
3337  * and leaving the original parent lock/ref intact as well.
3338  *
3339  * If an error occurs, NULL is returned and *errorp is set to the error.
3340  *
3341  * The returned chain depends on where the specified key falls.
3342  *
3343  * The key/keybits for the indirect mode only needs to follow three rules:
3344  *
3345  * (1) That all elements underneath it fit within its key space and
3346  *
3347  * (2) That all elements outside it are outside its key space.
3348  *
3349  * (3) When creating the new indirect block any elements in the current
3350  *     parent that fit within the new indirect block's keyspace must be
3351  *     moved into the new indirect block.
3352  *
3353  * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
3354  *     keyspace the the current parent, but lookup/iteration rules will
3355  *     ensure (and must ensure) that rule (2) for all parents leading up
3356  *     to the nearest inode or the root volume header is adhered to.  This
3357  *     is accomplished by always recursing through matching keyspaces in
3358  *     the hammer2_chain_lookup() and hammer2_chain_next() API.
3359  *
3360  * The current implementation calculates the current worst-case keyspace by
3361  * iterating the current parent and then divides it into two halves, choosing
3362  * whichever half has the most elements (not necessarily the half containing
3363  * the requested key).
3364  *
3365  * We can also opt to use the half with the least number of elements.  This
3366  * causes lower-numbered keys (aka logical file offsets) to recurse through
3367  * fewer indirect blocks and higher-numbered keys to recurse through more.
3368  * This also has the risk of not moving enough elements to the new indirect
3369  * block and being forced to create several indirect blocks before the element
3370  * can be inserted.
3371  *
3372  * Must be called with an exclusively locked parent.
3373  */
3374 static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
3375                                 hammer2_key_t *keyp, int keybits,
3376                                 hammer2_blockref_t *base, int count);
3377 static int hammer2_chain_indkey_normal(hammer2_chain_t *parent,
3378                                 hammer2_key_t *keyp, int keybits,
3379                                 hammer2_blockref_t *base, int count);
3380 static
3381 hammer2_chain_t *
3382 hammer2_chain_create_indirect(hammer2_trans_t *trans, hammer2_chain_t *parent,
3383                               hammer2_key_t create_key, int create_bits,
3384                               int for_type, int *errorp)
3385 {
3386         hammer2_mount_t *hmp;
3387         hammer2_chain_core_t *above;
3388         hammer2_chain_core_t *icore;
3389         hammer2_blockref_t *base;
3390         hammer2_blockref_t *bref;
3391         hammer2_blockref_t bcopy;
3392         hammer2_chain_t *chain;
3393         hammer2_chain_t *ichain;
3394         hammer2_chain_t dummy;
3395         hammer2_key_t key = create_key;
3396         hammer2_key_t key_beg;
3397         hammer2_key_t key_end;
3398         hammer2_key_t key_next;
3399         int keybits = create_bits;
3400         int count;
3401         int nbytes;
3402         int cache_index;
3403         int loops;
3404         int reason;
3405         int generation;
3406         int maxloops = 300000;
3407         int retry_same;
3408         int wasdup;
3409         hammer2_chain_t * volatile xxchain = NULL;
3410
3411         /*
3412          * Calculate the base blockref pointer or NULL if the chain
3413          * is known to be empty.  We need to calculate the array count
3414          * for RB lookups either way.
3415          */
3416         hmp = parent->hmp;
3417         *errorp = 0;
3418         KKASSERT(ccms_thread_lock_owned(&parent->core->cst));
3419         above = parent->core;
3420
3421         /*hammer2_chain_modify(trans, &parent, HAMMER2_MODIFY_OPTDATA);*/
3422         if (parent->flags & HAMMER2_CHAIN_INITIAL) {
3423                 base = NULL;
3424
3425                 switch(parent->bref.type) {
3426                 case HAMMER2_BREF_TYPE_INODE:
3427                         count = HAMMER2_SET_COUNT;
3428                         break;
3429                 case HAMMER2_BREF_TYPE_INDIRECT:
3430                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3431                         count = parent->bytes / sizeof(hammer2_blockref_t);
3432                         break;
3433                 case HAMMER2_BREF_TYPE_VOLUME:
3434                         count = HAMMER2_SET_COUNT;
3435                         break;
3436                 case HAMMER2_BREF_TYPE_FREEMAP:
3437                         count = HAMMER2_SET_COUNT;
3438                         break;
3439                 default:
3440                         panic("hammer2_chain_create_indirect: "
3441                               "unrecognized blockref type: %d",
3442                               parent->bref.type);
3443                         count = 0;
3444                         break;
3445                 }
3446         } else {
3447                 switch(parent->bref.type) {
3448                 case HAMMER2_BREF_TYPE_INODE:
3449                         base = &parent->data->ipdata.u.blockset.blockref[0];
3450                         count = HAMMER2_SET_COUNT;
3451                         break;
3452                 case HAMMER2_BREF_TYPE_INDIRECT:
3453                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
3454                         base = &parent->data->npdata[0];
3455                         count = parent->bytes / sizeof(hammer2_blockref_t);
3456                         break;
3457                 case HAMMER2_BREF_TYPE_VOLUME:
3458                         base = &hmp->voldata.sroot_blockset.blockref[0];
3459                         count = HAMMER2_SET_COUNT;
3460                         break;
3461                 case HAMMER2_BREF_TYPE_FREEMAP:
3462                         base = &hmp->voldata.freemap_blockset.blockref[0];
3463                         count = HAMMER2_SET_COUNT;
3464                         break;
3465                 default:
3466                         panic("hammer2_chain_create_indirect: "
3467                               "unrecognized blockref type: %d",
3468                               parent->bref.type);
3469                         count = 0;
3470                         break;
3471                 }
3472         }
3473
3474         /*
3475          * dummy used in later chain allocation (no longer used for lookups).
3476          */
3477         bzero(&dummy, sizeof(dummy));
3478         dummy.delete_tid = HAMMER2_MAX_TID;
3479
3480         /*
3481          * When creating an indirect block for a freemap node or leaf
3482          * the key/keybits must be fitted to static radix levels because
3483          * particular radix levels use particular reserved blocks in the
3484          * related zone.
3485          *
3486          * This routine calculates the key/radix of the indirect block
3487          * we need to create, and whether it is on the high-side or the
3488          * low-side.
3489          */
3490         if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
3491             for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
3492                 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
3493                                                        base, count);
3494         } else {
3495                 keybits = hammer2_chain_indkey_normal(parent, &key, keybits,
3496                                                       base, count);
3497         }
3498
3499         /*
3500          * Normalize the key for the radix being represented, keeping the
3501          * high bits and throwing away the low bits.
3502          */
3503         key &= ~(((hammer2_key_t)1 << keybits) - 1);
3504
3505         /*
3506          * How big should our new indirect block be?  It has to be at least
3507          * as large as its parent.
3508          */
3509         if (parent->bref.type == HAMMER2_BREF_TYPE_INODE)
3510                 nbytes = HAMMER2_IND_BYTES_MIN;
3511         else
3512                 nbytes = HAMMER2_IND_BYTES_MAX;
3513         if (nbytes < count * sizeof(hammer2_blockref_t))
3514                 nbytes = count * sizeof(hammer2_blockref_t);
3515
3516         /*
3517          * Ok, create our new indirect block
3518          */
3519         if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
3520             for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
3521                 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
3522         } else {
3523                 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
3524         }
3525         dummy.bref.key = key;
3526         dummy.bref.keybits = keybits;
3527         dummy.bref.data_off = hammer2_getradix(nbytes);
3528         dummy.bref.methods = parent->bref.methods;
3529
3530         ichain = hammer2_chain_alloc(hmp, parent->pmp, trans, &dummy.bref);
3531         atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
3532         hammer2_chain_core_alloc(trans, ichain, NULL);
3533         icore = ichain->core;
3534         hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
3535         hammer2_chain_drop(ichain);     /* excess ref from alloc */
3536
3537         /*
3538          * We have to mark it modified to allocate its block, but use
3539          * OPTDATA to allow it to remain in the INITIAL state.  Otherwise
3540          * it won't be acted upon by the flush code.
3541          *
3542          * XXX leave the node unmodified, depend on the update_hi
3543          * flush to assign and modify parent blocks.
3544          */
3545         hammer2_chain_modify(trans, &ichain, HAMMER2_MODIFY_OPTDATA);
3546
3547         /*
3548          * Iterate the original parent and move the matching brefs into
3549          * the new indirect block.
3550          *
3551          * XXX handle flushes.
3552          */
3553         key_beg = 0;
3554         key_end = HAMMER2_MAX_KEY;
3555         cache_index = 0;
3556         spin_lock(&above->cst.spin);
3557         loops = 0;
3558         reason = 0;
3559         retry_same = 0;
3560
3561         for (;;) {
3562                 if (++loops > 100000) {
3563                     spin_unlock(&above->cst.spin);
3564                     panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
3565                           reason, parent, base, count, key_next);
3566                 }
3567
3568                 /*
3569                  * NOTE: spinlock stays intact, returned chain (if not NULL)
3570                  *       is not referenced or locked which means that we
3571                  *       cannot safely check its flagged / deletion status
3572                  *       until we lock it.
3573                  */
3574                 chain = hammer2_combined_find(parent, base, count,
3575                                               &cache_index, &key_next,
3576                                               key_beg, key_end,
3577                                               &bref);
3578                 generation = above->generation;
3579                 if (bref == NULL)
3580                         break;
3581                 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3582
3583                 /*
3584                  * Skip keys that are not within the key/radix of the new
3585                  * indirect block.  They stay in the parent.
3586                  */
3587                 if ((~(((hammer2_key_t)1 << keybits) - 1) &
3588                     (key ^ bref->key)) != 0) {
3589                         goto next_key_spinlocked;
3590                 }
3591
3592                 /*
3593                  * Load the new indirect block by acquiring the related
3594                  * chains (potentially from media as it might not be
3595                  * in-memory).  Then move it to the new parent (ichain)
3596                  * via DELETE-DUPLICATE.
3597                  *
3598                  * chain is referenced but not locked.  We must lock the
3599                  * chain to obtain definitive DUPLICATED/DELETED state
3600                  */
3601                 if (chain) {
3602                         /*
3603                          * Use chain already present in the RBTREE
3604                          */
3605                         hammer2_chain_ref(chain);
3606                         wasdup = ((chain->flags &
3607                                    HAMMER2_CHAIN_DUPLICATED) != 0);
3608                         spin_unlock(&above->cst.spin);
3609                         hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER |
3610                                                   HAMMER2_RESOLVE_NOREF);
3611                 } else {
3612                         /*
3613                          * Get chain for blockref element.  _get returns NULL
3614                          * on insertion race.
3615                          */
3616                         bcopy = *bref;
3617                         spin_unlock(&above->cst.spin);
3618                         chain = hammer2_chain_get(parent, &bcopy, generation);
3619                         if (chain == NULL) {
3620                                 reason = 1;
3621                                 spin_lock(&above->cst.spin);
3622                                 continue;
3623                         }
3624                         if (bcmp(&bcopy, bref, sizeof(bcopy))) {
3625                                 reason = 2;
3626                                 hammer2_chain_drop(chain);
3627                                 spin_lock(&above->cst.spin);
3628                                 continue;
3629                         }
3630                         hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER |
3631                                                   HAMMER2_RESOLVE_NOREF);
3632                         wasdup = 0;
3633                 }
3634
3635                 /*
3636                  * This is always live so if the chain has been delete-
3637                  * duplicated we raced someone and we have to retry.
3638                  *
3639                  * NOTE: Lookups can race delete-duplicate because
3640                  *       delete-duplicate does not lock the parent's core
3641                  *       (they just use the spinlock on the core).  We must
3642                  *       check for races by comparing the DUPLICATED flag before
3643                  *       releasing the spinlock with the flag after locking the
3644                  *       chain.
3645                  *
3646                  *       (note reversed logic for this one)
3647                  */
3648                 if (chain->flags & HAMMER2_CHAIN_DELETED) {
3649                         hammer2_chain_unlock(chain);
3650                         if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) &&
3651                             wasdup == 0) {
3652                                 retry_same = 1;
3653                                 xxchain = chain;
3654                         }
3655                         goto next_key;
3656                 }
3657
3658                 /*
3659                  * Shift the chain to the indirect block.
3660                  *
3661                  * WARNING! Can cause held-over chains to require a refactor.
3662                  *          Fortunately we have none (our locked chains are
3663                  *          passed into and modified by the call).
3664                  */
3665                 hammer2_chain_delete(trans, chain, 0);
3666                 hammer2_chain_duplicate(trans, &ichain, &chain, NULL, 0, 1);
3667                 hammer2_chain_unlock(chain);
3668                 KKASSERT(parent->refs > 0);
3669                 chain = NULL;
3670 next_key:
3671                 spin_lock(&above->cst.spin);
3672 next_key_spinlocked:
3673                 if (--maxloops == 0)
3674                         panic("hammer2_chain_create_indirect: maxloops");
3675                 reason = 4;
3676                 if (retry_same == 0) {
3677                         if (key_next == 0 || key_next > key_end)
3678                                 break;
3679                         key_beg = key_next;
3680                 }
3681                 /* loop */
3682         }
3683         spin_unlock(&above->cst.spin);
3684
3685         /*
3686          * Insert the new indirect block into the parent now that we've
3687          * cleared out some entries in the parent.  We calculated a good
3688          * insertion index in the loop above (ichain->index).
3689          *
3690          * We don't have to set MOVED here because we mark ichain modified
3691          * down below (so the normal modified -> flush -> set-moved sequence
3692          * applies).
3693          *
3694          * The insertion shouldn't race as this is a completely new block
3695          * and the parent is locked.
3696          */
3697         KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
3698         hammer2_chain_insert(above, NULL, ichain,
3699                              HAMMER2_CHAIN_INSERT_SPIN |
3700                              HAMMER2_CHAIN_INSERT_LIVE,
3701                              0);
3702
3703         /*
3704          * Mark the new indirect block modified after insertion, which
3705          * will propagate up through parent all the way to the root and
3706          * also allocate the physical block in ichain for our caller,
3707          * and assign ichain->data to a pre-zero'd space (because there
3708          * is not prior data to copy into it).
3709          *
3710          * We have to set update_hi in ichain's flags manually so the
3711          * flusher knows it has to recurse through it to get to all of
3712          * our moved blocks, then call setsubmod() to set the bit
3713          * recursively.
3714          */
3715         /*hammer2_chain_modify(trans, &ichain, HAMMER2_MODIFY_OPTDATA);*/
3716         if (ichain->core->update_hi < trans->sync_tid) {
3717                 spin_lock(&ichain->core->cst.spin);
3718                 if (ichain->core->update_hi < trans->sync_tid)
3719                         ichain->core->update_hi = trans->sync_tid;
3720                 spin_unlock(&ichain->core->cst.spin);
3721         }
3722         hammer2_chain_setsubmod(trans, ichain);
3723
3724         /*
3725          * Figure out what to return.
3726          */
3727         if (~(((hammer2_key_t)1 << keybits) - 1) &
3728                    (create_key ^ key)) {
3729                 /*
3730                  * Key being created is outside the key range,
3731                  * return the original parent.
3732                  */
3733                 hammer2_chain_unlock(ichain);
3734         } else {
3735                 /*
3736                  * Otherwise its in the range, return the new parent.
3737                  * (leave both the new and old parent locked).
3738                  */
3739                 parent = ichain;
3740         }
3741         XXChain = xxchain;
3742
3743         return(parent);
3744 }
3745
3746 /*
3747  * Calculate the keybits and highside/lowside of the freemap node the
3748  * caller is creating.
3749  *
3750  * This routine will specify the next higher-level freemap key/radix
3751  * representing the lowest-ordered set.  By doing so, eventually all
3752  * low-ordered sets will be moved one level down.
3753  *
3754  * We have to be careful here because the freemap reserves a limited
3755  * number of blocks for a limited number of levels.  So we can't just
3756  * push indiscriminately.
3757  */
3758 int
3759 hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp,
3760                              int keybits, hammer2_blockref_t *base, int count)
3761 {
3762         hammer2_chain_core_t *above;
3763         hammer2_chain_t *chain;
3764         hammer2_blockref_t *bref;
3765         hammer2_key_t key;
3766         hammer2_key_t key_beg;
3767         hammer2_key_t key_end;
3768         hammer2_key_t key_next;
3769         int cache_index;
3770         int locount;
3771         int hicount;
3772         int maxloops = 300000;
3773
3774         key = *keyp;
3775         above = parent->core;
3776         locount = 0;
3777         hicount = 0;
3778         keybits = 64;
3779
3780         /*
3781          * Calculate the range of keys in the array being careful to skip
3782          * slots which are overridden with a deletion.
3783          */
3784         key_beg = 0;
3785         key_end = HAMMER2_MAX_KEY;
3786         cache_index = 0;
3787         spin_lock(&above->cst.spin);
3788
3789         for (;;) {
3790                 if (--maxloops == 0) {
3791                         panic("indkey_freemap shit %p %p:%d\n",
3792                               parent, base, count);
3793                 }
3794                 chain = hammer2_combined_find(parent, base, count,
3795                                               &cache_index, &key_next,
3796                                               key_beg, key_end, &bref);
3797
3798                 /*
3799                  * Exhausted search
3800                  */
3801                 if (bref == NULL)
3802                         break;
3803
3804                 /*
3805                  * NOTE: No need to check DUPLICATED here because we do
3806                  *       not release the spinlock.
3807                  */
3808                 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3809                         if (key_next == 0 || key_next > key_end)
3810                                 break;
3811                         key_beg = key_next;
3812                         continue;
3813                 }
3814
3815                 /*
3816                  * Use the full live (not deleted) element for the scan
3817                  * iteration.  HAMMER2 does not allow partial replacements.
3818                  *
3819                  * XXX should be built into hammer2_combined_find().
3820                  */
3821                 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3822
3823                 if (keybits > bref->keybits) {
3824                         key = bref->key;
3825                         keybits = bref->keybits;
3826                 } else if (keybits == bref->keybits && bref->key < key) {
3827                         key = bref->key;
3828                 }
3829                 if (key_next == 0)
3830                         break;
3831                 key_beg = key_next;
3832         }
3833         spin_unlock(&above->cst.spin);
3834
3835         /*
3836          * Return the keybits for a higher-level FREEMAP_NODE covering
3837          * this node.
3838          */
3839         switch(keybits) {
3840         case HAMMER2_FREEMAP_LEVEL0_RADIX:
3841                 keybits = HAMMER2_FREEMAP_LEVEL1_RADIX;
3842                 break;
3843         case HAMMER2_FREEMAP_LEVEL1_RADIX:
3844                 keybits = HAMMER2_FREEMAP_LEVEL2_RADIX;
3845                 break;
3846         case HAMMER2_FREEMAP_LEVEL2_RADIX:
3847                 keybits = HAMMER2_FREEMAP_LEVEL3_RADIX;
3848                 break;
3849         case HAMMER2_FREEMAP_LEVEL3_RADIX:
3850                 keybits = HAMMER2_FREEMAP_LEVEL4_RADIX;
3851                 break;
3852         case HAMMER2_FREEMAP_LEVEL4_RADIX:
3853                 panic("hammer2_chain_indkey_freemap: level too high");
3854                 break;
3855         default:
3856                 panic("hammer2_chain_indkey_freemap: bad radix");
3857                 break;
3858         }
3859         *keyp = key;
3860
3861         return (keybits);
3862 }
3863
3864 /*
3865  * Calculate the keybits and highside/lowside of the indirect block the
3866  * caller is creating.
3867  */
3868 static int
3869 hammer2_chain_indkey_normal(hammer2_chain_t *parent, hammer2_key_t *keyp,
3870                             int keybits, hammer2_blockref_t *base, int count)
3871 {
3872         hammer2_chain_core_t *above;
3873         hammer2_blockref_t *bref;
3874         hammer2_chain_t *chain;
3875         hammer2_key_t key_beg;
3876         hammer2_key_t key_end;
3877         hammer2_key_t key_next;
3878         hammer2_key_t key;
3879         int nkeybits;
3880         int locount;
3881         int hicount;
3882         int cache_index;
3883         int maxloops = 300000;
3884
3885         key = *keyp;
3886         above = parent->core;
3887         locount = 0;
3888         hicount = 0;
3889
3890         /*
3891          * Calculate the range of keys in the array being careful to skip
3892          * slots which are overridden with a deletion.  Once the scan
3893          * completes we will cut the key range in half and shift half the
3894          * range into the new indirect block.
3895          */
3896         key_beg = 0;
3897         key_end = HAMMER2_MAX_KEY;
3898         cache_index = 0;
3899         spin_lock(&above->cst.spin);
3900
3901         for (;;) {
3902                 if (--maxloops == 0) {
3903                         panic("indkey_freemap shit %p %p:%d\n",
3904                               parent, base, count);
3905                 }
3906                 chain = hammer2_combined_find(parent, base, count,
3907                                               &cache_index, &key_next,
3908                                               key_beg, key_end, &bref);
3909
3910                 /*
3911                  * Exhausted search
3912                  */
3913                 if (bref == NULL)
3914                         break;
3915
3916                 /*
3917                  * NOTE: No need to check DUPLICATED here because we do
3918                  *       not release the spinlock.
3919                  */
3920                 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
3921                         if (key_next == 0 || key_next > key_end)
3922                                 break;
3923                         key_beg = key_next;
3924                         continue;
3925                 }
3926
3927                 /*
3928                  * Use the full live (not deleted) element for the scan
3929                  * iteration.  HAMMER2 does not allow partial replacements.
3930                  *
3931                  * XXX should be built into hammer2_combined_find().
3932                  */
3933                 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
3934
3935                 /*
3936                  * Expand our calculated key range (key, keybits) to fit
3937                  * the scanned key.  nkeybits represents the full range
3938                  * that we will later cut in half (two halves @ nkeybits - 1).
3939                  */
3940                 nkeybits = keybits;
3941                 if (nkeybits < bref->keybits) {
3942                         if (bref->keybits > 64) {
3943                                 kprintf("bad bref chain %p bref %p\n",
3944                                         chain, bref);
3945                                 Debugger("fubar");
3946                         }
3947                         nkeybits = bref->keybits;
3948                 }
3949                 while (nkeybits < 64 &&
3950                        (~(((hammer2_key_t)1 << nkeybits) - 1) &
3951                         (key ^ bref->key)) != 0) {
3952                         ++nkeybits;
3953                 }
3954
3955                 /*
3956                  * If the new key range is larger we have to determine
3957                  * which side of the new key range the existing keys fall
3958                  * under by checking the high bit, then collapsing the
3959                  * locount into the hicount or vise-versa.
3960                  */
3961                 if (keybits != nkeybits) {
3962                         if (((hammer2_key_t)1 << (nkeybits - 1)) & key) {
3963                                 hicount += locount;
3964                                 locount = 0;
3965                         } else {
3966                                 locount += hicount;
3967                                 hicount = 0;
3968                         }
3969                         keybits = nkeybits;
3970                 }
3971
3972                 /*
3973                  * The newly scanned key will be in the lower half or the
3974                  * upper half of the (new) key range.
3975                  */
3976                 if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key)
3977                         ++hicount;
3978                 else
3979                         ++locount;
3980
3981                 if (key_next == 0)
3982                         break;
3983                 key_beg = key_next;
3984         }
3985         spin_unlock(&above->cst.spin);
3986         bref = NULL;    /* now invalid (safety) */
3987
3988         /*
3989          * Adjust keybits to represent half of the full range calculated
3990          * above (radix 63 max)
3991          */
3992         --keybits;
3993
3994         /*
3995          * Select whichever half contains the most elements.  Theoretically
3996          * we can select either side as long as it contains at least one
3997          * element (in order to ensure that a free slot is present to hold
3998          * the indirect block).
3999          */
4000         if (hammer2_indirect_optimize) {
4001                 /*
4002                  * Insert node for least number of keys, this will arrange
4003                  * the first few blocks of a large file or the first few
4004                  * inodes in a directory with fewer indirect blocks when
4005                  * created linearly.
4006                  */
4007                 if (hicount < locount && hicount != 0)
4008                         key |= (hammer2_key_t)1 << keybits;
4009                 else
4010                         key &= ~(hammer2_key_t)1 << keybits;
4011         } else {
4012                 /*
4013                  * Insert node for most number of keys, best for heavily
4014                  * fragmented files.
4015                  */
4016                 if (hicount > locount)
4017                         key |= (hammer2_key_t)1 << keybits;
4018                 else
4019                         key &= ~(hammer2_key_t)1 << keybits;
4020         }
4021         *keyp = key;
4022
4023         return (keybits);
4024 }
4025
4026 /*
4027  * Sets CHAIN_DELETED and CHAIN_MOVED in the chain being deleted and
4028  * set chain->delete_tid.  The chain is not actually marked possibly-free
4029  * in the freemap until the deletion is completely flushed out (because
4030  * a flush which doesn't cover the entire deletion is flushing the deleted
4031  * chain as if it were live).
4032  *
4033  * This function does NOT generate a modification to the parent.  It
4034  * would be nearly impossible to figure out which parent to modify anyway.
4035  * Such modifications are handled top-down by the flush code and are
4036  * properly merged using the flush synchronization point.
4037  *
4038  * The find/get code will properly overload the RBTREE check on top of
4039  * the bref check to detect deleted entries.
4040  *
4041  * This function is NOT recursive.  Any entity already pushed into the
4042  * chain (such as an inode) may still need visibility into its contents,
4043  * as well as the ability to read and modify the contents.  For example,
4044  * for an unlinked file which is still open.
4045  *
4046  * NOTE: This function does NOT set chain->modify_tid, allowing future
4047  *       code to distinguish between live and deleted chains by testing
4048  *       trans->sync_tid vs chain->modify_tid and chain->delete_tid.
4049  *
4050  * NOTE: Deletions normally do not occur in the middle of a duplication
4051  *       chain but we use a trick for hardlink migration that refactors
4052  *       the originating inode without deleting it, so we make no assumptions
4053  *       here.
4054  */
4055 void
4056 hammer2_chain_delete(hammer2_trans_t *trans, hammer2_chain_t *chain, int flags)
4057 {
4058         KKASSERT(ccms_thread_lock_owned(&chain->core->cst));
4059
4060         /*
4061          * Nothing to do if already marked.
4062          */
4063         if (chain->flags & HAMMER2_CHAIN_DELETED)
4064                 return;
4065
4066         /*
4067          * The setting of DELETED causes finds, lookups, and _next iterations
4068          * to no longer recognize the chain.  RB_SCAN()s will still have
4069          * visibility (needed for flush serialization points).
4070          *
4071          * We need the spinlock on the core whos RBTREE contains chain
4072          * to protect against races.
4073          */
4074         KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
4075         spin_lock(&chain->above->cst.spin);
4076
4077         KKASSERT(trans->sync_tid >= chain->modify_tid);
4078         chain->delete_tid = trans->sync_tid;
4079         atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
4080         atomic_add_int(&chain->above->live_count, -1);
4081         ++chain->above->generation;
4082
4083         /*
4084          * We must set MOVED along with DELETED for the flush code to
4085          * recognize the operation and properly disconnect the chain
4086          * in-memory.
4087          */
4088         if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
4089                 hammer2_chain_ref(chain);
4090                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
4091         }
4092         spin_unlock(&chain->above->cst.spin);
4093
4094         hammer2_chain_setsubmod(trans, chain);
4095 }
4096
4097 /*
4098  * Called with the core spinlock held to check for freeable layers.
4099  * Used by the flush code.  Layers can wind up not being freed due
4100  * to the temporary layer->refs count.  This function frees up any
4101  * layers that were missed.
4102  */
4103 void
4104 hammer2_chain_layer_check_locked(hammer2_mount_t *hmp,
4105                                  hammer2_chain_core_t *core)
4106 {
4107         hammer2_chain_layer_t *layer;
4108         hammer2_chain_layer_t *tmp;
4109
4110         tmp = TAILQ_FIRST(&core->layerq);
4111         while ((layer = tmp) != NULL) {
4112                 tmp = TAILQ_NEXT(tmp, entry);
4113                 if (layer->refs == 0 && RB_EMPTY(&layer->rbtree)) {
4114                         TAILQ_REMOVE(&core->layerq, layer, entry);
4115                         if (tmp)
4116                                 ++tmp->refs;
4117                         spin_unlock(&core->cst.spin);
4118                         kfree(layer, hmp->mchain);
4119                         spin_lock(&core->cst.spin);
4120                         if (tmp)
4121                                 --tmp->refs;
4122                 }
4123         }
4124 }
4125
4126 /*
4127  * Returns the index of the nearest element in the blockref array >= elm.
4128  * Returns (count) if no element could be found.
4129  *
4130  * Sets *key_nextp to the next key for loop purposes but does not modify
4131  * it if the next key would be higher than the current value of *key_nextp.
4132  * Note that *key_nexp can overflow to 0, which should be tested by the
4133  * caller.
4134  *
4135  * (*cache_indexp) is a heuristic and can be any value without effecting
4136  * the result.
4137  *
4138  * The spin lock on the related chain must be held.
4139  */
4140 int
4141 hammer2_base_find(hammer2_chain_t *chain,
4142                   hammer2_blockref_t *base, int count,
4143                   int *cache_indexp, hammer2_key_t *key_nextp,
4144                   hammer2_key_t key_beg, hammer2_key_t key_end)
4145 {
4146         hammer2_chain_core_t *core = chain->core;
4147         hammer2_blockref_t *scan;
4148         hammer2_key_t scan_end;
4149         int i;
4150         int limit;
4151
4152         /*
4153          * Require the live chain's already have their core's counted
4154          * so we can optimize operations.
4155          */
4156         KKASSERT((chain->flags & HAMMER2_CHAIN_DUPLICATED) ||
4157                  core->flags & HAMMER2_CORE_COUNTEDBREFS);
4158
4159         /*
4160          * Degenerate case
4161          */
4162         if (count == 0 || base == NULL)
4163                 return(count);
4164
4165         /*
4166          * Sequential optimization using *cache_indexp.  This is the most
4167          * likely scenario.
4168          *
4169          * We can avoid trailing empty entries on live chains, otherwise
4170          * we might have to check the whole block array.
4171          */
4172         i = *cache_indexp;
4173         cpu_ccfence();
4174         if (chain->flags & HAMMER2_CHAIN_DUPLICATED)
4175                 limit = count;
4176         else
4177                 limit = core->live_zero;
4178         if (i >= limit)
4179                 i = limit - 1;
4180         if (i < 0)
4181                 i = 0;
4182         KKASSERT(i < count);
4183
4184         /*
4185          * Search backwards
4186          */
4187         scan = &base[i];
4188         while (i > 0 && (scan->type == 0 || scan->key > key_beg)) {
4189                 --scan;
4190                 --i;
4191         }
4192         *cache_indexp = i;
4193
4194         /*
4195          * Search forwards, stop when we find a scan element which
4196          * encloses the key or until we know that there are no further
4197          * elements.
4198          */
4199         while (i < count) {
4200                 if (scan->type != 0) {
4201                         if (scan->key > key_beg)
4202                                 break;
4203                         scan_end = scan->key +
4204                                    ((hammer2_key_t)1 << scan->keybits) - 1;
4205                         if (scan_end >= key_beg)
4206                                 break;
4207                 }
4208                 if (i >= limit)
4209                         return (count);
4210                 ++scan;
4211                 ++i;
4212         }
4213         if (i != count) {
4214                 *cache_indexp = i;
4215                 if (i >= limit) {
4216                         i = count;
4217                 } else {
4218                         scan_end = scan->key +
4219                                    ((hammer2_key_t)1 << scan->keybits);
4220                         if (scan_end && (*key_nextp > scan_end ||
4221                                          *key_nextp == 0)) {
4222                                 *key_nextp = scan_end;
4223                         }
4224                 }
4225         }
4226         return (i);
4227 }
4228
4229 /*
4230  * Do a combined search and return the next match either from the blockref
4231  * array or from the in-memory chain.  Sets *bresp to the returned bref in
4232  * both cases, or sets it to NULL if the search exhausted.  Only returns
4233  * a non-NULL chain if the search matched from the in-memory chain.
4234  *
4235  * Must be called with above's spinlock held.  Spinlock remains held
4236  * through the operation.
4237  *
4238  * The returned chain is not locked or referenced.  Use the returned bref
4239  * to determine if the search exhausted or not.
4240  */
4241 static hammer2_chain_t *
4242 hammer2_combined_find(hammer2_chain_t *parent,
4243                       hammer2_blockref_t *base, int count,
4244                       int *cache_indexp, hammer2_key_t *key_nextp,
4245                       hammer2_key_t key_beg, hammer2_key_t key_end,
4246                       hammer2_blockref_t **bresp)
4247 {
4248         hammer2_blockref_t *bref;
4249         hammer2_chain_t *chain;
4250         int i;
4251
4252         *key_nextp = key_end + 1;
4253         i = hammer2_base_find(parent, base, count, cache_indexp,
4254                               key_nextp, key_beg, key_end);
4255         chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end);
4256
4257         /*
4258          * Neither matched
4259          */
4260         if (i == count && chain == NULL) {
4261                 *bresp = NULL;
4262                 return(chain);  /* NULL */
4263         }
4264
4265         /*
4266          * Only chain matched
4267          */
4268         if (i == count) {
4269                 bref = &chain->bref;
4270                 goto found;
4271         }
4272
4273         /*
4274          * Only blockref matched.
4275          */
4276         if (chain == NULL) {
4277                 bref = &base[i];
4278                 goto found;
4279         }
4280
4281         /*
4282          * Both in-memory and blockref match.  Select the nearer element.
4283          * If both are flush with the left-hand side they are considered
4284          * to be the same distance.
4285          *
4286          * When both are the same distance away select the chain if it is
4287          * live or if it's delete_tid is greater than the parent's
4288          * synchronized bref.mirror_tid (a single test suffices for both
4289          * conditions), otherwise select the element.
4290          *
4291          * (It is possible for an old deletion to linger after a rename-over
4292          *  and flush, which would make the media copy the correct choice).
4293          */
4294
4295         /*
4296          * Either both are flush with the left-hand side or they are the
4297          * same distance away.  Select the chain if it is not deleted
4298          * or it has a higher delete_tid, else select the media.
4299          */
4300         if ((chain->bref.key <= key_beg && base[i].key <= key_beg) ||
4301             chain->bref.key == base[i].key) {
4302                 if (chain->delete_tid > base[i].mirror_tid) {
4303                         bref = &chain->bref;
4304                 } else {
4305                         KKASSERT(chain->flags & HAMMER2_CHAIN_DELETED);
4306                         bref = &base[i];
4307                         chain = NULL;
4308                 }
4309                 goto found;
4310         }
4311
4312         /*
4313          * Select the nearer key.
4314          */
4315         if (chain->bref.key < base[i].key) {
4316                 bref = &chain->bref;
4317         } else {
4318                 bref = &base[i];
4319                 chain = NULL;
4320         }
4321
4322         /*
4323          * If the bref is out of bounds we've exhausted our search.
4324          */
4325 found:
4326         if (bref->key > key_end) {
4327                 *bresp = NULL;
4328                 chain = NULL;
4329         } else {
4330                 *bresp = bref;
4331         }
4332         return(chain);
4333 }
4334
4335 /*
4336  * Locate the specified block array element and delete it.  The element
4337  * must exist.
4338  *
4339  * The spin lock on the related chain must be held.
4340  *
4341  * NOTE: live_count was adjusted when the chain was deleted, so it does not
4342  *       need to be adjusted when we commit the media change.
4343  */
4344 void
4345 hammer2_base_delete(hammer2_trans_t *trans, hammer2_chain_t *parent,
4346                     hammer2_blockref_t *base, int count,
4347                     int *cache_indexp, hammer2_chain_t *child)
4348 {
4349         hammer2_blockref_t *elm = &child->bref;
4350         hammer2_chain_core_t *core = parent->core;
4351         hammer2_key_t key_next;
4352         int i;
4353
4354         /*
4355          * Delete element.  Expect the element to exist.
4356          *
4357          * XXX see caller, flush code not yet sophisticated enough to prevent
4358          *     re-flushed in some cases.
4359          */
4360         key_next = 0; /* max range */
4361         i = hammer2_base_find(parent, base, count, cache_indexp,
4362                               &key_next, elm->key, elm->key);
4363         if (i == count || base[i].type == 0 ||
4364             base[i].key != elm->key || base[i].keybits != elm->keybits) {
4365                 panic("delete base %p element not found at %d/%d elm %p\n",
4366                       base, i, count, elm);
4367                 return;
4368         }
4369         bzero(&base[i], sizeof(*base));
4370         base[i].mirror_tid = (intptr_t)parent;          /* debug */
4371         base[i].modify_tid = (intptr_t)child;           /* debug */
4372         base[i].check.debug.sync_tid = trans->sync_tid; /* debug */
4373
4374         /*
4375          * We can only optimize core->live_zero for live chains.
4376          */
4377         if ((parent->flags & HAMMER2_CHAIN_DUPLICATED) == 0) {
4378                 if (core->live_zero == i + 1) {
4379                         while (--i >= 0 && base[i].type == 0)
4380                                 ;
4381                         core->live_zero = i + 1;
4382                 }
4383         }
4384 }
4385
4386 /*
4387  * Insert the specified element.  The block array must not already have the
4388  * element and must have space available for the insertion.
4389  *
4390  * The spin lock on the related chain must be held.
4391  *
4392  * NOTE: live_count was adjusted when the chain was deleted, so it does not
4393  *       need to be adjusted when we commit the media change.
4394  */
4395 void
4396 hammer2_base_insert(hammer2_trans_t *trans __unused, hammer2_chain_t *parent,
4397                     hammer2_blockref_t *base, int count,
4398                     int *cache_indexp, hammer2_chain_t *child)
4399 {
4400         hammer2_blockref_t *elm = &child->bref;
4401         hammer2_chain_core_t *core = parent->core;
4402         hammer2_key_t key_next;
4403         hammer2_key_t xkey;
4404         int i;
4405         int j;
4406         int k;
4407         int l;
4408         int u = 1;
4409
4410         /*
4411          * Insert new element.  Expect the element to not already exist
4412          * unless we are replacing it.
4413          *
4414          * XXX see caller, flush code not yet sophisticated enough to prevent
4415          *     re-flushed in some cases.
4416          */
4417         key_next = 0; /* max range */
4418         i = hammer2_base_find(parent, base, count, cache_indexp,
4419                               &key_next, elm->key, elm->key);
4420
4421         /*
4422          * Shortcut fill optimization, typical ordered insertion(s) may not
4423          * require a search.
4424          */
4425         KKASSERT(i >= 0 && i <= count);
4426
4427         /*
4428          * We can only optimize core->live_zero for live chains.
4429          */
4430         if (i == count && core->live_zero < count) {
4431                 if ((parent->flags & HAMMER2_CHAIN_DUPLICATED) == 0) {
4432                         i = core->live_zero++;
4433                         base[i] = *elm;
4434                         return;
4435                 }
4436         }
4437
4438         xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1;
4439         if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) {
4440                 panic("insert base %p overlapping elements at %d elm %p\n",
4441                       base, i, elm);
4442         }
4443
4444         /*
4445          * Try to find an empty slot before or after.
4446          */
4447         j = i;
4448         k = i;
4449         while (j > 0 || k < count) {
4450                 --j;
4451                 if (j >= 0 && base[j].type == 0) {
4452                         if (j == i - 1) {
4453                                 base[j] = *elm;
4454                         } else {
4455                                 bcopy(&base[j+1], &base[j],
4456                                       (i - j - 1) * sizeof(*base));
4457                                 base[i - 1] = *elm;
4458                         }
4459                         goto validate;
4460                 }
4461                 ++k;
4462                 if (k < count && base[k].type == 0) {
4463                         bcopy(&base[i], &base[i+1],
4464                               (k - i) * sizeof(hammer2_blockref_t));
4465                         base[i] = *elm;
4466
4467                         /*
4468                          * We can only update core->live_zero for live
4469                          * chains.
4470                          */
4471                         if ((parent->flags & HAMMER2_CHAIN_DUPLICATED) == 0) {
4472                                 if (core->live_zero <= k)
4473                                         core->live_zero = k + 1;
4474                         }
4475                         u = 2;
4476                         goto validate;
4477                 }
4478         }
4479         panic("hammer2_base_insert: no room!");
4480
4481         /*
4482          * Debugging
4483          */
4484 validate:
4485         key_next = 0;
4486         for (l = 0; l < count; ++l) {
4487                 if (base[l].type) {
4488                         key_next = base[l].key +
4489                                    ((hammer2_key_t)1 << base[l].keybits) - 1;
4490                         break;
4491                 }
4492         }
4493         while (++l < count) {
4494                 if (base[l].type) {
4495                         if (base[l].key <= key_next)
4496                                 panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l);
4497                         key_next = base[l].key +
4498                                    ((hammer2_key_t)1 << base[l].keybits) - 1;
4499
4500                 }
4501         }
4502
4503 }
4504
4505 #if 0
4506
4507 /*
4508  * Sort the blockref array for the chain.  Used by the flush code to
4509  * sort the blockref[] array.
4510  *
4511  * The chain must be exclusively locked AND spin-locked.
4512  */
4513 typedef hammer2_blockref_t *hammer2_blockref_p;
4514
4515 static
4516 int
4517 hammer2_base_sort_callback(const void *v1, const void *v2)
4518 {
4519         hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1;
4520         hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2;
4521
4522         /*
4523          * Make sure empty elements are placed at the end of the array
4524          */
4525         if (bref1->type == 0) {
4526                 if (bref2->type == 0)
4527                         return(0);
4528                 return(1);
4529         } else if (bref2->type == 0) {
4530                 return(-1);
4531         }
4532
4533         /*
4534          * Sort by key
4535          */
4536         if (bref1->key < bref2->key)
4537                 return(-1);
4538         if (bref1->key > bref2->key)
4539                 return(1);
4540         return(0);
4541 }
4542
4543 void
4544 hammer2_base_sort(hammer2_chain_t *chain)
4545 {
4546         hammer2_blockref_t *base;
4547         int count;
4548
4549         switch(chain->bref.type) {
4550         case HAMMER2_BREF_TYPE_INODE:
4551                 /*
4552                  * Special shortcut for embedded data returns the inode
4553                  * itself.  Callers must detect this condition and access
4554                  * the embedded data (the strategy code does this for us).
4555                  *
4556                  * This is only applicable to regular files and softlinks.
4557                  */
4558                 if (chain->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA)
4559                         return;
4560                 base = &chain->data->ipdata.u.blockset.blockref[0];
4561                 count = HAMMER2_SET_COUNT;
4562                 break;
4563         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
4564         case HAMMER2_BREF_TYPE_INDIRECT:
4565                 /*
4566                  * Optimize indirect blocks in the INITIAL state to avoid
4567                  * I/O.
4568                  */
4569                 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0);
4570                 base = &chain->data->npdata[0];
4571                 count = chain->bytes / sizeof(hammer2_blockref_t);
4572                 break;
4573         case HAMMER2_BREF_TYPE_VOLUME:
4574                 base = &chain->hmp->voldata.sroot_blockset.blockref[0];
4575                 count = HAMMER2_SET_COUNT;
4576                 break;
4577         case HAMMER2_BREF_TYPE_FREEMAP:
4578                 base = &chain->hmp->voldata.freemap_blockset.blockref[0];
4579                 count = HAMMER2_SET_COUNT;
4580                 break;
4581         default:
4582                 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
4583                       chain->bref.type);
4584                 base = NULL;    /* safety */
4585                 count = 0;      /* safety */
4586         }
4587         kqsort(base, count, sizeof(*base), hammer2_base_sort_callback);
4588 }
4589
4590 #endif
4591
4592 /*
4593  * Chain memory management
4594  */
4595 void
4596 hammer2_chain_wait(hammer2_chain_t *chain)
4597 {
4598         tsleep(chain, 0, "chnflw", 1);
4599 }
4600
4601 /*
4602  * Manage excessive memory resource use for chain and related
4603  * structures.
4604  */
4605 void
4606 hammer2_chain_memory_wait(hammer2_pfsmount_t *pmp)
4607 {
4608         long waiting;
4609         long count;
4610         long limit;
4611         static int zzticks;
4612
4613         /*
4614          * Atomic check condition and wait.  Also do an early speedup of
4615          * the syncer to try to avoid hitting the wait.
4616          */
4617         for (;;) {
4618                 waiting = pmp->inmem_dirty_chains;
4619                 cpu_ccfence();
4620                 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
4621
4622                 limit = pmp->mp->mnt_nvnodelistsize / 10;
4623                 if (limit < hammer2_limit_dirty_chains)
4624                         limit = hammer2_limit_dirty_chains;
4625                 if (limit < 1000)
4626                         limit = 1000;
4627
4628                 if ((int)(ticks - zzticks) > hz) {
4629                         zzticks = ticks;
4630                         kprintf("count %ld %ld\n", count, limit);
4631                 }
4632
4633                 /*
4634                  * Block if there are too many dirty chains present, wait
4635                  * for the flush to clean some out.
4636                  */
4637                 if (count > limit) {
4638                         tsleep_interlock(&pmp->inmem_dirty_chains, 0);
4639                         if (atomic_cmpset_long(&pmp->inmem_dirty_chains,
4640                                                waiting,
4641                                        waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
4642                                 speedup_syncer(pmp->mp);
4643                                 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
4644                                        "chnmem", hz);
4645                         }
4646                         continue;       /* loop on success or fail */
4647                 }
4648
4649                 /*
4650                  * Try to start an early flush before we are forced to block.
4651                  */
4652                 if (count > limit * 7 / 10)
4653                         speedup_syncer(pmp->mp);
4654                 break;
4655         }
4656 }
4657
4658 void
4659 hammer2_chain_memory_inc(hammer2_pfsmount_t *pmp)
4660 {
4661         if (pmp)
4662                 atomic_add_long(&pmp->inmem_dirty_chains, 1);
4663 }
4664
4665 void
4666 hammer2_chain_memory_wakeup(hammer2_pfsmount_t *pmp)
4667 {
4668         long waiting;
4669
4670         if (pmp == NULL)
4671                 return;
4672
4673         for (;;) {
4674                 waiting = pmp->inmem_dirty_chains;
4675                 cpu_ccfence();
4676                 if (atomic_cmpset_long(&pmp->inmem_dirty_chains,
4677                                        waiting,
4678                                        (waiting - 1) &
4679                                         ~HAMMER2_DIRTYCHAIN_WAITING)) {
4680                         break;
4681                 }
4682         }
4683         if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
4684                 wakeup(&pmp->inmem_dirty_chains);
4685 }
4686
4687 static
4688 void
4689 adjreadcounter(hammer2_blockref_t *bref, size_t bytes)
4690 {
4691         long *counterp;
4692
4693         switch(bref->type) {
4694         case HAMMER2_BREF_TYPE_DATA:
4695                 counterp = &hammer2_iod_file_read;
4696                 break;
4697         case HAMMER2_BREF_TYPE_INODE:
4698                 counterp = &hammer2_iod_meta_read;
4699                 break;
4700         case HAMMER2_BREF_TYPE_INDIRECT:
4701                 counterp = &hammer2_iod_indr_read;
4702                 break;
4703         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
4704         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
4705                 counterp = &hammer2_iod_fmap_read;
4706                 break;
4707         default:
4708                 counterp = &hammer2_iod_volu_read;
4709                 break;
4710         }
4711         *counterp += bytes;
4712 }