hammer2 - Refactor frontend part 14/many
[dragonfly.git] / sys / vfs / hammer2 / hammer2_chain.c
CommitLineData
7cfa8da5 1/*
8138a154 2 * Copyright (c) 2011-2014 The DragonFly Project. All rights reserved.
7cfa8da5
MD
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
da6f36f4 6 * and Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7cfa8da5
MD
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 */
5c23d7f1 35/*
0dea3156 36 * This subsystem implements most of the core support functions for
1897c66e
MD
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 *
da6f36f4
MD
43 * Chains are no-longer delete-duplicated. Instead, the original in-memory
44 * chain will be moved along with its block reference (e.g. for things like
45 * renames, hardlink operations, modifications, etc), and will be indexed
46 * on a secondary list for flush handling instead of propagating a flag
47 * upward to the root.
48 *
49 * Concurrent front-end operations can still run against backend flushes
50 * as long as they do not cross the current flush boundary. An operation
51 * running above the current flush (in areas not yet flushed) can become
52 * part of the current flush while ano peration running below the current
53 * flush can become part of the next flush.
5c23d7f1 54 */
7cfa8da5
MD
55#include <sys/cdefs.h>
56#include <sys/param.h>
57#include <sys/systm.h>
58#include <sys/types.h>
59#include <sys/lock.h>
a864c5d9 60#include <sys/kern_syscall.h>
7cfa8da5
MD
61#include <sys/uuid.h>
62
a71db85d
MD
63#include <crypto/sha2/sha2.h>
64
7cfa8da5
MD
65#include "hammer2.h"
66
995e78dc
MD
67static int hammer2_indirect_optimize; /* XXX SYSCTL */
68
69static hammer2_chain_t *hammer2_chain_create_indirect(
c603b86b 70 hammer2_chain_t *parent,
1a7cfe5a 71 hammer2_key_t key, int keybits, int for_type, int *errorp);
355d67fc 72static void hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop);
51a0d27c
MD
73static hammer2_chain_t *hammer2_combined_find(
74 hammer2_chain_t *parent,
75 hammer2_blockref_t *base, int count,
76 int *cache_indexp, hammer2_key_t *key_nextp,
77 hammer2_key_t key_beg, hammer2_key_t key_end,
78 hammer2_blockref_t **bresp);
995e78dc 79
5c23d7f1 80/*
8138a154
MD
81 * Basic RBTree for chains (core->rbtree and core->dbtree). Chains cannot
82 * overlap in the RB trees. Deleted chains are moved from rbtree to either
83 * dbtree or to dbq.
1897c66e 84 *
8138a154
MD
85 * Chains in delete-duplicate sequences can always iterate through core_entry
86 * to locate the live version of the chain.
5c23d7f1 87 */
ecc33e71 88RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
01eabad4 89
5c23d7f1
MD
90int
91hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
92{
1897c66e
MD
93 hammer2_key_t c1_beg;
94 hammer2_key_t c1_end;
95 hammer2_key_t c2_beg;
96 hammer2_key_t c2_end;
355d67fc 97
8138a154
MD
98 /*
99 * Compare chains. Overlaps are not supposed to happen and catch
100 * any software issues early we count overlaps as a match.
101 */
1897c66e
MD
102 c1_beg = chain1->bref.key;
103 c1_end = c1_beg + ((hammer2_key_t)1 << chain1->bref.keybits) - 1;
104 c2_beg = chain2->bref.key;
105 c2_end = c2_beg + ((hammer2_key_t)1 << chain2->bref.keybits) - 1;
355d67fc 106
1897c66e
MD
107 if (c1_end < c2_beg) /* fully to the left */
108 return(-1);
109 if (c1_beg > c2_end) /* fully to the right */
110 return(1);
111 return(0); /* overlap (must not cross edge boundary) */
5c23d7f1
MD
112}
113
a98aa0b0
MD
114static __inline
115int
116hammer2_isclusterable(hammer2_chain_t *chain)
117{
118 if (hammer2_cluster_enable) {
119 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
120 chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
121 chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
122 return(1);
123 }
124 }
125 return(0);
126}
127
01eabad4 128/*
da6f36f4 129 * Make a chain visible to the flusher. The flusher needs to be able to
9450e866 130 * do flushes of subdirectory chains or single files so it does a top-down
da6f36f4 131 * recursion using the ONFLUSH flag for the recursion. It locates MODIFIED
9450e866
MD
132 * or UPDATE chains and flushes back up the chain to the volume root.
133 *
134 * This routine sets ONFLUSH upward until it hits the volume root. For
135 * simplicity we ignore PFSROOT boundaries whos rules can be complex.
136 * Extra ONFLUSH flagging doesn't hurt the filesystem.
01eabad4 137 */
32b800e6 138void
c603b86b 139hammer2_chain_setflush(hammer2_chain_t *chain)
01eabad4 140{
da6f36f4 141 hammer2_chain_t *parent;
8138a154 142
da6f36f4 143 if ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
94491fa0 144 hammer2_spin_sh(&chain->core.spin);
da6f36f4
MD
145 while ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) {
146 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH);
147 if ((parent = chain->parent) == NULL)
148 break;
94491fa0
MD
149 hammer2_spin_sh(&parent->core.spin);
150 hammer2_spin_unsh(&chain->core.spin);
da6f36f4
MD
151 chain = parent;
152 }
94491fa0 153 hammer2_spin_unsh(&chain->core.spin);
01eabad4
MD
154 }
155}
156
5c23d7f1
MD
157/*
158 * Allocate a new disconnected chain element representing the specified
0dea3156
MD
159 * bref. chain->refs is set to 1 and the passed bref is copied to
160 * chain->bref. chain->bytes is derived from the bref.
161 *
50456506
MD
162 * chain->pmp inherits pmp unless the chain is an inode (other than the
163 * super-root inode).
164 *
0dea3156 165 * NOTE: Returns a referenced but unlocked (because there is no core) chain.
5c23d7f1
MD
166 */
167hammer2_chain_t *
506bd6d1 168hammer2_chain_alloc(hammer2_dev_t *hmp, hammer2_pfs_t *pmp,
c603b86b 169 hammer2_blockref_t *bref)
5c23d7f1
MD
170{
171 hammer2_chain_t *chain;
6ba3b984 172 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
5c23d7f1
MD
173
174 /*
175 * Construct the appropriate system structure.
176 */
177 switch(bref->type) {
178 case HAMMER2_BREF_TYPE_INODE:
5c23d7f1 179 case HAMMER2_BREF_TYPE_INDIRECT:
9061bde5 180 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
5c23d7f1 181 case HAMMER2_BREF_TYPE_DATA:
9061bde5 182 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
355d67fc 183 /*
1897c66e
MD
184 * Chain's are really only associated with the hmp but we
185 * maintain a pmp association for per-mount memory tracking
186 * purposes. The pmp can be NULL.
355d67fc 187 */
476d2aad 188 chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO);
5c23d7f1
MD
189 break;
190 case HAMMER2_BREF_TYPE_VOLUME:
1a7cfe5a 191 case HAMMER2_BREF_TYPE_FREEMAP:
5c23d7f1 192 chain = NULL;
866d5273 193 panic("hammer2_chain_alloc volume type illegal for op");
5c23d7f1
MD
194 default:
195 chain = NULL;
866d5273 196 panic("hammer2_chain_alloc: unrecognized blockref type: %d",
5c23d7f1
MD
197 bref->type);
198 }
004f88b4 199
50456506 200 /*
23c7c7dd
MD
201 * Initialize the new chain structure. pmp must be set to NULL for
202 * chains belonging to the super-root topology of a device mount.
50456506 203 */
506bd6d1
MD
204 if (pmp == hmp->spmp)
205 chain->pmp = NULL;
206 else
207 chain->pmp = pmp;
0dea3156 208 chain->hmp = hmp;
5c23d7f1 209 chain->bref = *bref;
6ba3b984 210 chain->bytes = bytes;
0dea3156
MD
211 chain->refs = 1;
212 chain->flags = HAMMER2_CHAIN_ALLOCATED;
50456506
MD
213
214 /*
215 * Set the PFS boundary flag if this chain represents a PFS root.
216 */
217 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
218 chain->flags |= HAMMER2_CHAIN_PFSBOUNDARY;
23c7c7dd 219 hammer2_chain_core_init(chain);
a7720be7 220
5c23d7f1
MD
221 return (chain);
222}
223
a0ed3c24 224/*
23c7c7dd
MD
225 * Initialize a chain's core structure. This structure used to be allocated
226 * but is now embedded.
a0ed3c24 227 *
0dea3156 228 * The core is not locked. No additional refs on the chain are made.
1897c66e 229 * (trans) must not be NULL if (core) is not NULL.
0dea3156
MD
230 */
231void
23c7c7dd 232hammer2_chain_core_init(hammer2_chain_t *chain)
0dea3156 233{
da6f36f4
MD
234 /*
235 * Fresh core under nchain (no multi-homing of ochain's
236 * sub-tree).
237 */
f1c7c224
MD
238 RB_INIT(&chain->core.rbtree); /* live chains */
239 hammer2_mtx_init(&chain->lock, "h2chain");
5c23d7f1
MD
240}
241
7cfa8da5 242/*
a0ed3c24 243 * Add a reference to a chain element, preventing its destruction.
af791767
MD
244 *
245 * (can be called with spinlock held)
7cfa8da5
MD
246 */
247void
0dea3156 248hammer2_chain_ref(hammer2_chain_t *chain)
7cfa8da5 249{
0dea3156 250 atomic_add_int(&chain->refs, 1);
9450e866
MD
251#if 0
252 kprintf("REFC %p %d %08x\n", chain, chain->refs - 1, chain->flags);
253 print_backtrace(8);
254#endif
7cfa8da5
MD
255}
256
1897c66e 257/*
8138a154
MD
258 * Insert the chain in the core rbtree.
259 *
260 * Normal insertions are placed in the live rbtree. Insertion of a deleted
261 * chain is a special case used by the flush code that is placed on the
262 * unstaged deleted list to avoid confusing the live view.
1897c66e
MD
263 */
264#define HAMMER2_CHAIN_INSERT_SPIN 0x0001
265#define HAMMER2_CHAIN_INSERT_LIVE 0x0002
266#define HAMMER2_CHAIN_INSERT_RACE 0x0004
267
268static
623d43d4 269int
da6f36f4 270hammer2_chain_insert(hammer2_chain_t *parent, hammer2_chain_t *chain,
8138a154 271 int flags, int generation)
1897c66e 272{
1897c66e 273 hammer2_chain_t *xchain;
623d43d4 274 int error = 0;
1897c66e
MD
275
276 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
94491fa0 277 hammer2_spin_ex(&parent->core.spin);
925e4ad1 278
623d43d4
MD
279 /*
280 * Interlocked by spinlock, check for race
281 */
282 if ((flags & HAMMER2_CHAIN_INSERT_RACE) &&
da6f36f4 283 parent->core.generation != generation) {
623d43d4
MD
284 error = EAGAIN;
285 goto failed;
286 }
1897c66e
MD
287
288 /*
da6f36f4 289 * Insert chain
1897c66e 290 */
da6f36f4
MD
291 xchain = RB_INSERT(hammer2_chain_tree, &parent->core.rbtree, chain);
292 KASSERT(xchain == NULL,
293 ("hammer2_chain_insert: collision %p %p", chain, xchain));
294 atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
295 chain->parent = parent;
296 ++parent->core.chain_count;
297 ++parent->core.generation; /* XXX incs for _get() too, XXX */
1897c66e 298
0924b3f8
MD
299 /*
300 * We have to keep track of the effective live-view blockref count
301 * so the create code knows when to push an indirect block.
302 */
8138a154 303 if (flags & HAMMER2_CHAIN_INSERT_LIVE)
da6f36f4 304 atomic_add_int(&parent->core.live_count, 1);
2a8b1c40 305failed:
1897c66e 306 if (flags & HAMMER2_CHAIN_INSERT_SPIN)
94491fa0 307 hammer2_spin_unex(&parent->core.spin);
623d43d4 308 return error;
1897c66e
MD
309}
310
7cfa8da5 311/*
0dea3156 312 * Drop the caller's reference to the chain. When the ref count drops to
1fca819a 313 * zero this function will try to disassociate the chain from its parent and
0dea3156
MD
314 * deallocate it, then recursely drop the parent using the implied ref
315 * from the chain's chain->parent.
7cfa8da5 316 */
da6f36f4 317static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain);
a0ed3c24 318
7cfa8da5 319void
0dea3156 320hammer2_chain_drop(hammer2_chain_t *chain)
7cfa8da5 321{
5c23d7f1 322 u_int refs;
ea155208
MD
323 u_int need = 0;
324
925e4ad1
MD
325 if (hammer2_debug & 0x200000)
326 Debugger("drop");
9450e866
MD
327#if 0
328 kprintf("DROP %p %d %08x\n", chain, chain->refs - 1, chain->flags);
329 print_backtrace(8);
330#endif
925e4ad1 331
da6f36f4 332 if (chain->flags & HAMMER2_CHAIN_UPDATE)
ea155208
MD
333 ++need;
334 if (chain->flags & HAMMER2_CHAIN_MODIFIED)
335 ++need;
336 KKASSERT(chain->refs > need);
a7720be7 337
5c23d7f1
MD
338 while (chain) {
339 refs = chain->refs;
340 cpu_ccfence();
db0c2eb3 341 KKASSERT(refs > 0);
0dea3156 342
5c23d7f1 343 if (refs == 1) {
da6f36f4 344 chain = hammer2_chain_lastdrop(chain);
5c23d7f1 345 } else {
0dea3156 346 if (atomic_cmpset_int(&chain->refs, refs, refs - 1))
5c23d7f1 347 break;
6934ae32 348 /* retry the same chain */
7cfa8da5
MD
349 }
350 }
351}
352
a0ed3c24 353/*
731b2a84 354 * Safe handling of the 1->0 transition on chain. Returns a chain for
1897c66e 355 * recursive drop or NULL, possibly returning the same chain if the atomic
731b2a84 356 * op fails.
a0ed3c24 357 *
053e752c
MD
358 * Whem two chains need to be recursively dropped we use the chain
359 * we would otherwise free to placehold the additional chain. It's a bit
360 * convoluted but we can't just recurse without potentially blowing out
361 * the kernel stack.
362 *
23c7c7dd 363 * The chain cannot be freed if it has any children.
1fca819a 364 *
94491fa0 365 * The core spinlock is allowed nest child-to-parent (not parent-to-child).
a0ed3c24
MD
366 */
367static
368hammer2_chain_t *
da6f36f4 369hammer2_chain_lastdrop(hammer2_chain_t *chain)
a0ed3c24 370{
506bd6d1
MD
371 hammer2_pfs_t *pmp;
372 hammer2_dev_t *hmp;
da6f36f4
MD
373 hammer2_chain_t *parent;
374 hammer2_chain_t *rdrop;
a0ed3c24 375
731b2a84
MD
376 /*
377 * Spinlock the core and check to see if it is empty. If it is
1897c66e
MD
378 * not empty we leave chain intact with refs == 0. The elements
379 * in core->rbtree are associated with other chains contemporary
380 * with ours but not with our chain directly.
731b2a84 381 */
94491fa0 382 hammer2_spin_ex(&chain->core.spin);
1897c66e 383
da6f36f4
MD
384 /*
385 * We can't free non-stale chains with children until we are
386 * able to free the children because there might be a flush
387 * dependency. Flushes of stale children (which should also
388 * have their deleted flag set) short-cut recursive flush
389 * dependencies and can be freed here. Any flushes which run
390 * through stale children due to the flush synchronization
391 * point should have a FLUSH_* bit set in the chain and not
392 * reach lastdrop at this time.
393 *
394 * NOTE: We return (chain) on failure to retry.
395 */
396 if (chain->core.chain_count) {
397 if (atomic_cmpset_int(&chain->refs, 1, 0)) {
94491fa0 398 hammer2_spin_unex(&chain->core.spin);
da6f36f4
MD
399 chain = NULL; /* success */
400 } else {
94491fa0 401 hammer2_spin_unex(&chain->core.spin);
731b2a84 402 }
da6f36f4 403 return(chain);
731b2a84 404 }
da6f36f4 405 /* no chains left under us */
731b2a84 406
053e752c
MD
407 /*
408 * chain->core has no children left so no accessors can get to our
da6f36f4 409 * chain from there. Now we have to lock the parent core to interlock
053e752c
MD
410 * remaining possible accessors that might bump chain's refs before
411 * we can safely drop chain's refs with intent to free the chain.
412 */
731b2a84 413 hmp = chain->hmp;
355d67fc 414 pmp = chain->pmp; /* can be NULL */
da6f36f4 415 rdrop = NULL;
731b2a84
MD
416
417 /*
053e752c
MD
418 * Spinlock the parent and try to drop the last ref on chain.
419 * On success remove chain from its parent, otherwise return NULL.
1897c66e 420 *
053e752c
MD
421 * (normal core locks are top-down recursive but we define core
422 * spinlocks as bottom-up recursive, so this is safe).
731b2a84 423 */
da6f36f4 424 if ((parent = chain->parent) != NULL) {
94491fa0 425 hammer2_spin_ex(&parent->core.spin);
053e752c 426 if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
731b2a84 427 /* 1->0 transition failed */
94491fa0
MD
428 hammer2_spin_unex(&parent->core.spin);
429 hammer2_spin_unex(&chain->core.spin);
1897c66e 430 return(chain); /* retry */
731b2a84 431 }
a0ed3c24 432
731b2a84 433 /*
1897c66e 434 * 1->0 transition successful, remove chain from its
8138a154 435 * above core.
731b2a84 436 */
da6f36f4
MD
437 if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
438 RB_REMOVE(hammer2_chain_tree,
439 &parent->core.rbtree, chain);
8138a154 440 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
da6f36f4
MD
441 --parent->core.chain_count;
442 chain->parent = NULL;
8138a154
MD
443 }
444
731b2a84 445 /*
51a0d27c 446 * If our chain was the last chain in the parent's core the
da6f36f4
MD
447 * core is now empty and its parent might have to be
448 * re-dropped if it has 0 refs.
731b2a84 449 */
da6f36f4
MD
450 if (parent->core.chain_count == 0) {
451 rdrop = parent;
452 if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0) {
453 rdrop = NULL;
731b2a84 454 }
f3843dc2 455 }
94491fa0 456 hammer2_spin_unex(&parent->core.spin);
da6f36f4 457 parent = NULL; /* safety */
f3843dc2 458 }
731b2a84
MD
459
460 /*
053e752c
MD
461 * Successful 1->0 transition and the chain can be destroyed now.
462 *
da6f36f4
MD
463 * We still have the core spinlock, and core's chain_count is 0.
464 * Any parent spinlock is gone.
731b2a84 465 */
94491fa0 466 hammer2_spin_unex(&chain->core.spin);
da6f36f4
MD
467 KKASSERT(RB_EMPTY(&chain->core.rbtree) &&
468 chain->core.chain_count == 0);
731b2a84
MD
469
470 /*
471 * All spin locks are gone, finish freeing stuff.
472 */
da6f36f4 473 KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE |
731b2a84 474 HAMMER2_CHAIN_MODIFIED)) == 0);
355d67fc
MD
475 hammer2_chain_drop_data(chain, 1);
476
fdf62707 477 KKASSERT(chain->dio == NULL);
1897c66e 478
053e752c
MD
479 /*
480 * Once chain resources are gone we can use the now dead chain
481 * structure to placehold what might otherwise require a recursive
482 * drop, because we have potentially two things to drop and can only
483 * return one directly.
484 */
da6f36f4 485 if (chain->flags & HAMMER2_CHAIN_ALLOCATED) {
053e752c
MD
486 chain->flags &= ~HAMMER2_CHAIN_ALLOCATED;
487 chain->hmp = NULL;
488 kfree(chain, hmp->mchain);
489 }
925e4ad1
MD
490
491 /*
da6f36f4 492 * Possible chaining loop when parent re-drop needed.
925e4ad1 493 */
da6f36f4 494 return(rdrop);
355d67fc
MD
495}
496
497/*
498 * On either last lock release or last drop
499 */
500static void
501hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop)
502{
506bd6d1 503 /*hammer2_dev_t *hmp = chain->hmp;*/
355d67fc 504
731b2a84
MD
505 switch(chain->bref.type) {
506 case HAMMER2_BREF_TYPE_VOLUME:
1a7cfe5a 507 case HAMMER2_BREF_TYPE_FREEMAP:
355d67fc
MD
508 if (lastdrop)
509 chain->data = NULL;
731b2a84 510 break;
731b2a84
MD
511 default:
512 KKASSERT(chain->data == NULL);
513 break;
514 }
a0ed3c24
MD
515}
516
5c23d7f1 517/*
e513e77e 518 * Lock a referenced chain element, acquiring its data with I/O if necessary,
01eabad4 519 * and specify how you would like the data to be resolved.
5c23d7f1 520 *
b93cc2e0 521 * If an I/O or other fatal error occurs, chain->error will be set to non-zero.
db71f61f 522 *
01eabad4
MD
523 * The lock is allowed to recurse, multiple locking ops will aggregate
524 * the requested resolve types. Once data is assigned it will not be
525 * removed until the last unlock.
526 *
527 * HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
528 * (typically used to avoid device/logical buffer
529 * aliasing for data)
530 *
531 * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
532 * the INITIAL-create state (indirect blocks only).
533 *
534 * Do not resolve data elements for DATA chains.
535 * (typically used to avoid device/logical buffer
536 * aliasing for data)
537 *
538 * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
539 *
a0ed3c24
MD
540 * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
541 * it will be locked exclusive.
8cce658d 542 *
01eabad4
MD
543 * NOTE: Embedded elements (volume header, inodes) are always resolved
544 * regardless.
545 *
546 * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
547 * element will instantiate and zero its buffer, and flush it on
548 * release.
549 *
550 * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
551 * so as not to instantiate a device buffer, which could alias against
552 * a logical file buffer. However, if ALWAYS is specified the
553 * device buffer will be instantiated anyway.
0dea3156 554 *
f1c7c224
MD
555 * WARNING! This function blocks on I/O if data needs to be fetched. This
556 * blocking can run concurrent with other compatible lock holders
557 * who do not need data returning. The lock is not upgraded to
558 * exclusive during a data fetch, a separate bit is used to
559 * interlock I/O. However, an exclusive lock holder can still count
560 * on being interlocked against an I/O fetch managed by a shared
561 * lock holder.
5c23d7f1 562 */
23c7c7dd 563void
0dea3156 564hammer2_chain_lock(hammer2_chain_t *chain, int how)
232a50f9 565{
5c23d7f1 566 /*
a0ed3c24 567 * Ref and lock the element. Recursive locks are allowed.
5c23d7f1 568 */
e513e77e 569 KKASSERT(chain->refs > 0);
731b2a84
MD
570 atomic_add_int(&chain->lockcnt, 1);
571
0dea3156
MD
572 /*
573 * Get the appropriate lock.
574 */
5f6853df 575 if (how & HAMMER2_RESOLVE_SHARED)
f1c7c224 576 hammer2_mtx_sh(&chain->lock);
5f6853df 577 else
f1c7c224 578 hammer2_mtx_ex(&chain->lock);
5c23d7f1
MD
579
580 /*
01eabad4
MD
581 * If we already have a valid data pointer no further action is
582 * necessary.
5c23d7f1 583 */
db71f61f 584 if (chain->data)
23c7c7dd 585 return;
5c23d7f1 586
8cce658d 587 /*
01eabad4 588 * Do we have to resolve the data?
8cce658d 589 */
a0ed3c24 590 switch(how & HAMMER2_RESOLVE_MASK) {
01eabad4 591 case HAMMER2_RESOLVE_NEVER:
23c7c7dd 592 return;
01eabad4
MD
593 case HAMMER2_RESOLVE_MAYBE:
594 if (chain->flags & HAMMER2_CHAIN_INITIAL)
23c7c7dd 595 return;
01eabad4 596 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
23c7c7dd 597 return;
1a7cfe5a
MD
598#if 0
599 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE)
23c7c7dd 600 return;
9061bde5 601 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
23c7c7dd 602 return;
a71db85d 603#endif
01eabad4
MD
604 /* fall through */
605 case HAMMER2_RESOLVE_ALWAYS:
606 break;
607 }
8cce658d 608
a0ed3c24 609 /*
f1c7c224
MD
610 * Caller requires data
611 */
612 hammer2_chain_load_data(chain);
613}
614
615/*
616 * Issue I/O and install chain->data. Caller must hold a chain lock, lock
617 * may be of any type.
618 *
619 * Once chain->data is set it cannot be disposed of until all locks are
620 * released.
621 */
622void
623hammer2_chain_load_data(hammer2_chain_t *chain)
624{
625 hammer2_blockref_t *bref;
626 hammer2_dev_t *hmp;
627 char *bdata;
628 int error;
629
630 /*
631 * Degenerate case, data already present.
a0ed3c24 632 */
f1c7c224 633 if (chain->data)
23c7c7dd 634 return;
f1c7c224
MD
635
636 hmp = chain->hmp;
637 KKASSERT(hmp != NULL);
638
639 /*
640 * Gain the IOINPROG bit, interlocked block.
641 */
642 for (;;) {
643 u_int oflags;
644 u_int nflags;
645
646 oflags = chain->flags;
647 cpu_ccfence();
648 if (oflags & HAMMER2_CHAIN_IOINPROG) {
649 nflags = oflags | HAMMER2_CHAIN_IOSIGNAL;
650 tsleep_interlock(&chain->flags, 0);
651 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
652 tsleep(&chain->flags, PINTERLOCKED,
653 "h2iocw", 0);
654 }
655 /* retry */
656 } else {
657 nflags = oflags | HAMMER2_CHAIN_IOINPROG;
658 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
659 break;
660 }
661 /* retry */
662 }
a0ed3c24
MD
663 }
664
f1c7c224
MD
665 /*
666 * We own CHAIN_IOINPROG
667 *
668 * Degenerate case if we raced another load.
669 */
670 if (chain->data)
671 goto done;
672
5c23d7f1 673 /*
01eabad4
MD
674 * We must resolve to a device buffer, either by issuing I/O or
675 * by creating a zero-fill element. We do not mark the buffer
676 * dirty when creating a zero-fill element (the hammer2_chain_modify()
677 * API must still be used to do that).
db71f61f 678 *
01eabad4 679 * The device buffer is variable-sized in powers of 2 down
1a7cfe5a 680 * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage
01eabad4 681 * chunk always contains buffers of the same size. (XXX)
8cce658d 682 *
01eabad4
MD
683 * The minimum physical IO size may be larger than the variable
684 * block size.
5c23d7f1 685 */
5c23d7f1
MD
686 bref = &chain->bref;
687
01eabad4
MD
688 /*
689 * The getblk() optimization can only be used on newly created
690 * elements if the physical block size matches the request.
691 */
fdf62707
MD
692 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
693 error = hammer2_io_new(hmp, bref->data_off, chain->bytes,
694 &chain->dio);
01eabad4 695 } else {
fdf62707
MD
696 error = hammer2_io_bread(hmp, bref->data_off, chain->bytes,
697 &chain->dio);
278ab2b2 698 hammer2_adjreadcounter(&chain->bref, chain->bytes);
01eabad4 699 }
5c23d7f1 700 if (error) {
23c7c7dd 701 chain->error = HAMMER2_ERROR_IO;
1897c66e 702 kprintf("hammer2_chain_lock: I/O error %016jx: %d\n",
fdf62707
MD
703 (intmax_t)bref->data_off, error);
704 hammer2_io_bqrelse(&chain->dio);
f1c7c224 705 goto done;
5c23d7f1 706 }
23c7c7dd 707 chain->error = 0;
5c23d7f1 708
8138a154 709 /*
23c7c7dd
MD
710 * NOTE: A locked chain's data cannot be modified without first
711 * calling hammer2_chain_modify().
8138a154 712 */
8138a154 713
01eabad4 714 /*
da6f36f4
MD
715 * Clear INITIAL. In this case we used io_new() and the buffer has
716 * been zero'd and marked dirty.
01eabad4 717 */
fdf62707 718 bdata = hammer2_io_data(chain->dio, chain->bref.data_off);
a71db85d 719 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1a7cfe5a 720 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
a71db85d
MD
721 chain->bref.flags |= HAMMER2_BREF_FLAG_ZERO;
722 } else if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
723 /*
724 * check data not currently synchronized due to
725 * modification. XXX assumes data stays in the buffer
726 * cache, which might not be true (need biodep on flush
727 * to calculate crc? or simple crc?).
728 */
729 } else {
730 if (hammer2_chain_testcheck(chain, bdata) == 0) {
23c7c7dd
MD
731 kprintf("chain %016jx.%02x meth=%02x "
732 "CHECK FAIL %08x (flags=%08x)\n",
a71db85d
MD
733 chain->bref.data_off,
734 chain->bref.type,
735 chain->bref.methods,
736 hammer2_icrc32(bdata, chain->bytes),
737 chain->flags);
23c7c7dd 738 chain->error = HAMMER2_ERROR_CHECK;
a71db85d
MD
739 }
740 }
01eabad4 741
5c23d7f1 742 /*
db71f61f 743 * Setup the data pointer, either pointing it to an embedded data
8cce658d 744 * structure and copying the data from the buffer, or pointing it
db71f61f 745 * into the buffer.
5c23d7f1 746 *
db71f61f
MD
747 * The buffer is not retained when copying to an embedded data
748 * structure in order to avoid potential deadlocks or recursions
749 * on the same physical buffer.
f1c7c224
MD
750 *
751 * WARNING! Other threads can start using the data the instant we
752 * set chain->data non-NULL.
5c23d7f1
MD
753 */
754 switch (bref->type) {
b7926f31 755 case HAMMER2_BREF_TYPE_VOLUME:
1a7cfe5a 756 case HAMMER2_BREF_TYPE_FREEMAP:
b7926f31
MD
757 /*
758 * Copy data from bp to embedded buffer
759 */
01eabad4 760 panic("hammer2_chain_lock: called on unresolved volume header");
b7926f31 761 break;
5c23d7f1 762 case HAMMER2_BREF_TYPE_INODE:
512beabd 763 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
6ba3b984 764 case HAMMER2_BREF_TYPE_INDIRECT:
8cce658d 765 case HAMMER2_BREF_TYPE_DATA:
9061bde5 766 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
5c23d7f1
MD
767 default:
768 /*
91caa51c 769 * Point data at the device buffer and leave dio intact.
5c23d7f1 770 */
01eabad4 771 chain->data = (void *)bdata;
5c23d7f1
MD
772 break;
773 }
f1c7c224
MD
774
775 /*
776 * Release HAMMER2_CHAIN_IOINPROG and signal waiters if requested.
777 */
778done:
779 for (;;) {
780 u_int oflags;
781 u_int nflags;
782
783 oflags = chain->flags;
784 nflags = oflags & ~(HAMMER2_CHAIN_IOINPROG |
785 HAMMER2_CHAIN_IOSIGNAL);
786 KKASSERT(oflags & HAMMER2_CHAIN_IOINPROG);
787 if (atomic_cmpset_int(&chain->flags, oflags, nflags)) {
788 if (oflags & HAMMER2_CHAIN_IOSIGNAL)
789 wakeup(&chain->flags);
790 break;
791 }
792 }
232a50f9
MD
793}
794
222d9e22 795/*
01eabad4 796 * Unlock and deref a chain element.
8cce658d 797 *
fdf62707 798 * On the last lock release any non-embedded data (chain->dio) will be
01eabad4 799 * retired.
866d5273
MD
800 */
801void
0dea3156 802hammer2_chain_unlock(hammer2_chain_t *chain)
866d5273 803{
94491fa0 804 hammer2_mtx_state_t ostate;
01eabad4 805 long *counterp;
731b2a84 806 u_int lockcnt;
866d5273
MD
807
808 /*
731b2a84
MD
809 * If multiple locks are present (or being attempted) on this
810 * particular chain we can just unlock, drop refs, and return.
811 *
812 * Otherwise fall-through on the 1->0 transition.
866d5273 813 */
731b2a84
MD
814 for (;;) {
815 lockcnt = chain->lockcnt;
816 KKASSERT(lockcnt > 0);
817 cpu_ccfence();
818 if (lockcnt > 1) {
819 if (atomic_cmpset_int(&chain->lockcnt,
820 lockcnt, lockcnt - 1)) {
f1c7c224 821 hammer2_mtx_unlock(&chain->lock);
731b2a84
MD
822 return;
823 }
824 } else {
825 if (atomic_cmpset_int(&chain->lockcnt, 1, 0))
826 break;
827 }
828 /* retry */
829 }
830
831 /*
832 * On the 1->0 transition we upgrade the core lock (if necessary)
833 * to exclusive for terminal processing. If after upgrading we find
834 * that lockcnt is non-zero, another thread is racing us and will
835 * handle the unload for us later on, so just cleanup and return
fdf62707 836 * leaving the data/io intact
731b2a84
MD
837 *
838 * Otherwise if lockcnt is still 0 it is possible for it to become
94491fa0
MD
839 * non-zero and race, but since we hold the core->lock exclusively
840 * all that will happen is that the chain will be reloaded after we
841 * unload it.
731b2a84 842 */
f1c7c224 843 ostate = hammer2_mtx_upgrade(&chain->lock);
731b2a84 844 if (chain->lockcnt) {
f1c7c224 845 hammer2_mtx_unlock(&chain->lock);
01eabad4
MD
846 return;
847 }
866d5273
MD
848
849 /*
01eabad4 850 * Shortcut the case if the data is embedded or not resolved.
26b047fa 851 *
476d2aad
MD
852 * Do NOT NULL out chain->data (e.g. inode data), it might be
853 * dirty.
866d5273 854 */
fdf62707 855 if (chain->dio == NULL) {
355d67fc
MD
856 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0)
857 hammer2_chain_drop_data(chain, 0);
f1c7c224 858 hammer2_mtx_unlock(&chain->lock);
866d5273 859 return;
01eabad4 860 }
866d5273 861
866d5273 862 /*
01eabad4 863 * Statistics
866d5273 864 */
fdf62707 865 if (hammer2_io_isdirty(chain->dio) == 0) {
01eabad4
MD
866 ;
867 } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
866d5273 868 switch(chain->bref.type) {
01eabad4
MD
869 case HAMMER2_BREF_TYPE_DATA:
870 counterp = &hammer2_ioa_file_write;
871 break;
872 case HAMMER2_BREF_TYPE_INODE:
873 counterp = &hammer2_ioa_meta_write;
866d5273
MD
874 break;
875 case HAMMER2_BREF_TYPE_INDIRECT:
01eabad4
MD
876 counterp = &hammer2_ioa_indr_write;
877 break;
9061bde5
MD
878 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
879 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
880 counterp = &hammer2_ioa_fmap_write;
881 break;
01eabad4
MD
882 default:
883 counterp = &hammer2_ioa_volu_write;
6ba3b984 884 break;
01eabad4 885 }
a98aa0b0 886 *counterp += chain->bytes;
01eabad4
MD
887 } else {
888 switch(chain->bref.type) {
866d5273 889 case HAMMER2_BREF_TYPE_DATA:
01eabad4
MD
890 counterp = &hammer2_iod_file_write;
891 break;
892 case HAMMER2_BREF_TYPE_INODE:
893 counterp = &hammer2_iod_meta_write;
894 break;
895 case HAMMER2_BREF_TYPE_INDIRECT:
896 counterp = &hammer2_iod_indr_write;
866d5273 897 break;
9061bde5
MD
898 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
899 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
900 counterp = &hammer2_iod_fmap_write;
901 break;
866d5273 902 default:
01eabad4 903 counterp = &hammer2_iod_volu_write;
866d5273 904 break;
01eabad4 905 }
a98aa0b0 906 *counterp += chain->bytes;
01eabad4 907 }
866d5273 908
01eabad4 909 /*
fdf62707 910 * Clean out the dio.
01eabad4
MD
911 *
912 * If a device buffer was used for data be sure to destroy the
913 * buffer when we are done to avoid aliases (XXX what about the
914 * underlying VM pages?).
9061bde5
MD
915 *
916 * NOTE: Freemap leaf's use reserved blocks and thus no aliasing
917 * is possible.
fdf62707
MD
918 *
919 * NOTE: The isdirty check tracks whether we have to bdwrite() the
920 * buffer or not. The buffer might already be dirty. The
921 * flag is re-set when chain_modify() is called, even if
922 * MODIFIED is already set, allowing the OS to retire the
923 * buffer independent of a hammer2 flush.
26b047fa 924 */
01eabad4 925 chain->data = NULL;
fdf62707
MD
926 if ((chain->flags & HAMMER2_CHAIN_IOFLUSH) &&
927 hammer2_io_isdirty(chain->dio)) {
928 hammer2_io_bawrite(&chain->dio);
01eabad4 929 } else {
fdf62707 930 hammer2_io_bqrelse(&chain->dio);
866d5273 931 }
f1c7c224 932 hammer2_mtx_unlock(&chain->lock);
866d5273
MD
933}
934
1897c66e
MD
935/*
936 * This counts the number of live blockrefs in a block array and
937 * also calculates the point at which all remaining blockrefs are empty.
0238551e 938 * This routine can only be called on a live chain (DUPLICATED flag not set).
1897c66e
MD
939 *
940 * NOTE: Flag is not set until after the count is complete, allowing
941 * callers to test the flag without holding the spinlock.
942 *
943 * NOTE: If base is NULL the related chain is still in the INITIAL
944 * state and there are no blockrefs to count.
945 *
946 * NOTE: live_count may already have some counts accumulated due to
947 * creation and deletion and could even be initially negative.
948 */
949void
51a0d27c 950hammer2_chain_countbrefs(hammer2_chain_t *chain,
1897c66e
MD
951 hammer2_blockref_t *base, int count)
952{
94491fa0 953 hammer2_spin_ex(&chain->core.spin);
f1c7c224 954 if ((chain->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) {
1897c66e
MD
955 if (base) {
956 while (--count >= 0) {
957 if (base[count].type)
958 break;
959 }
da6f36f4 960 chain->core.live_zero = count + 1;
1897c66e
MD
961 while (count >= 0) {
962 if (base[count].type)
da6f36f4
MD
963 atomic_add_int(&chain->core.live_count,
964 1);
1897c66e
MD
965 --count;
966 }
51a0d27c 967 } else {
da6f36f4 968 chain->core.live_zero = 0;
1897c66e
MD
969 }
970 /* else do not modify live_count */
f1c7c224 971 atomic_set_int(&chain->flags, HAMMER2_CHAIN_COUNTEDBREFS);
1897c66e 972 }
94491fa0 973 hammer2_spin_unex(&chain->core.spin);
1897c66e
MD
974}
975
8cce658d 976/*
05dd26e4
MD
977 * Resize the chain's physical storage allocation in-place. This function does
978 * not adjust the data pointer and must be followed by (typically) a
979 * hammer2_chain_modify() call to copy any old data over and adjust the
980 * data pointer.
981 *
982 * Chains can be resized smaller without reallocating the storage. Resizing
983 * larger will reallocate the storage. Excess or prior storage is reclaimed
984 * asynchronously at a later time.
0dea3156 985 *
da6f36f4 986 * Must be passed an exclusively locked parent and chain.
004f88b4 987 *
01eabad4 988 * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
da6f36f4 989 * to avoid instantiating a device buffer that conflicts with the vnode data
05dd26e4
MD
990 * buffer. However, because H2 can compress or encrypt data, the chain may
991 * have a dio assigned to it in those situations, and they do not conflict.
01eabad4 992 *
0dea3156 993 * XXX return error if cannot resize.
8cce658d
MD
994 */
995void
c603b86b 996hammer2_chain_resize(hammer2_inode_t *ip,
da6f36f4 997 hammer2_chain_t *parent, hammer2_chain_t *chain,
01eabad4 998 int nradix, int flags)
8cce658d 999{
506bd6d1 1000 hammer2_dev_t *hmp;
8cce658d
MD
1001 size_t obytes;
1002 size_t nbytes;
1003
a5913bdf
MD
1004 hmp = chain->hmp;
1005
8cce658d 1006 /*
9061bde5
MD
1007 * Only data and indirect blocks can be resized for now.
1008 * (The volu root, inodes, and freemap elements use a fixed size).
8cce658d
MD
1009 */
1010 KKASSERT(chain != &hmp->vchain);
1011 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
1012 chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
1013
1014 /*
1015 * Nothing to do if the element is already the proper size
1016 */
1017 obytes = chain->bytes;
1c9f601e 1018 nbytes = 1U << nradix;
8cce658d
MD
1019 if (obytes == nbytes)
1020 return;
1021
0dea3156 1022 /*
05dd26e4
MD
1023 * Make sure the old data is instantiated so we can copy it. If this
1024 * is a data block, the device data may be superfluous since the data
1025 * might be in a logical block, but compressed or encrypted data is
1026 * another matter.
da6f36f4
MD
1027 *
1028 * NOTE: The modify will set BMAPUPD for us if BMAPPED is set.
0dea3156 1029 */
c603b86b 1030 hammer2_chain_modify(chain, 0);
0dea3156 1031
01eabad4
MD
1032 /*
1033 * Relocate the block, even if making it smaller (because different
1034 * block sizes may be in different regions).
8138a154
MD
1035 *
1036 * (data blocks only, we aren't copying the storage here).
01eabad4 1037 */
c603b86b 1038 hammer2_freemap_alloc(chain, nbytes);
01eabad4 1039 chain->bytes = nbytes;
476d2aad 1040 /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */
01eabad4 1041
01eabad4 1042 /*
05dd26e4
MD
1043 * We don't want the followup chain_modify() to try to copy data
1044 * from the old (wrong-sized) buffer. It won't know how much to
1045 * copy. This case should only occur during writes when the
1046 * originator already has the data to write in-hand.
01eabad4 1047 */
05dd26e4
MD
1048 if (chain->dio) {
1049 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA);
1050 hammer2_io_brelse(&chain->dio);
1051 chain->data = NULL;
1052 }
8cce658d
MD
1053}
1054
232a50f9 1055void
c603b86b 1056hammer2_chain_modify(hammer2_chain_t *chain, int flags)
232a50f9 1057{
bca9f8e6 1058 hammer2_blockref_t obref;
506bd6d1 1059 hammer2_dev_t *hmp;
fdf62707 1060 hammer2_io_t *dio;
db71f61f 1061 int error;
1a7cfe5a 1062 int wasinitial;
da6f36f4 1063 int newmod;
fdf62707 1064 char *bdata;
a92f82c4 1065
1897c66e 1066 hmp = chain->hmp;
bca9f8e6 1067 obref = chain->bref;
b93cc2e0 1068 KKASSERT((chain->flags & HAMMER2_CHAIN_FICTITIOUS) == 0);
1897c66e 1069
1a7cfe5a 1070 /*
bca9f8e6 1071 * Data is not optional for freemap chains (we must always be sure
1a7cfe5a
MD
1072 * to copy the data on COW storage allocations).
1073 */
1074 if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
1075 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
1076 KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) ||
1077 (flags & HAMMER2_MODIFY_OPTDATA) == 0);
1078 }
4d5318eb 1079
91caa51c 1080 /*
a849aee8 1081 * Data must be resolved if already assigned, unless explicitly
91caa51c
MD
1082 * flagged otherwise.
1083 */
1084 if (chain->data == NULL && (flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
1085 (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) {
a849aee8 1086 hammer2_chain_load_data(chain);
91caa51c
MD
1087 }
1088
1a7cfe5a 1089 /*
a849aee8
MD
1090 * Set MODIFIED to indicate that the chain has been modified.
1091 * Set UPDATE to ensure that the blockref is updated in the parent.
1a7cfe5a 1092 */
9596b8c4
MD
1093 if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1094 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
1095 hammer2_chain_ref(chain);
506bd6d1 1096 hammer2_pfs_memory_inc(chain->pmp); /* can be NULL */
da6f36f4
MD
1097 newmod = 1;
1098 } else {
1099 newmod = 0;
0924b3f8 1100 }
da6f36f4
MD
1101 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
1102 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
0924b3f8 1103 hammer2_chain_ref(chain);
9596b8c4 1104 }
ea155208 1105
73e441b9 1106 /*
1a7cfe5a
MD
1107 * The modification or re-modification requires an allocation and
1108 * possible COW.
c667909f 1109 *
1a7cfe5a
MD
1110 * We normally always allocate new storage here. If storage exists
1111 * and MODIFY_NOREALLOC is passed in, we do not allocate new storage.
1112 */
a4dc31e0
MD
1113 if (chain != &hmp->vchain && chain != &hmp->fchain) {
1114 if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 ||
da6f36f4 1115 ((flags & HAMMER2_MODIFY_NOREALLOC) == 0 && newmod)
a4dc31e0 1116 ) {
c603b86b 1117 hammer2_freemap_alloc(chain, chain->bytes);
a4dc31e0 1118 /* XXX failed allocation */
a4dc31e0 1119 }
db71f61f 1120 }
5c23d7f1 1121
e513e77e 1122 /*
a6cf1052
MD
1123 * Update mirror_tid and modify_tid. modify_tid is only updated
1124 * automatically by this function when used from the frontend.
1125 * Flushes and synchronization adjust the flag manually.
e513e77e
MD
1126 *
1127 * NOTE: chain->pmp could be the device spmp.
1128 */
1129 chain->bref.mirror_tid = hmp->voldata.mirror_tid + 1;
c603b86b
MD
1130 if (chain->pmp && (flags & HAMMER2_MODIFY_KEEPMODIFY) == 0) {
1131 /* XXX HAMMER2_TRANS_ISFLUSH */
1132 chain->bref.modify_tid = chain->pmp->modify_tid;
a6cf1052 1133 }
e513e77e 1134
10136ab6 1135 /*
da6f36f4
MD
1136 * Set BMAPUPD to tell the flush code that an existing blockmap entry
1137 * requires updating as well as to tell the delete code that the
1138 * chain's blockref might not exactly match (in terms of physical size
1139 * or block offset) the one in the parent's blocktable. The base key
1140 * of course will still match.
10136ab6 1141 */
da6f36f4
MD
1142 if (chain->flags & HAMMER2_CHAIN_BMAPPED)
1143 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD);
10136ab6 1144
01eabad4 1145 /*
a71db85d
MD
1146 * Short-cut data blocks which the caller does not need an actual
1147 * data reference to (aka OPTDATA), as long as the chain does not
1148 * already have a data pointer to the data. This generally means
1149 * that the modifications are being done via the logical buffer cache.
1150 * The INITIAL flag relates only to the device data buffer and thus
1151 * remains unchange in this situation.
01eabad4 1152 */
91caa51c 1153 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA &&
a71db85d
MD
1154 (flags & HAMMER2_MODIFY_OPTDATA) &&
1155 chain->data == NULL) {
01eabad4 1156 goto skip2;
91caa51c 1157 }
01eabad4 1158
01eabad4 1159 /*
01eabad4 1160 * Clearing the INITIAL flag (for indirect blocks) indicates that
1a7cfe5a
MD
1161 * we've processed the uninitialized storage allocation.
1162 *
1163 * If this flag is already clear we are likely in a copy-on-write
1164 * situation but we have to be sure NOT to bzero the storage if
1165 * no data is present.
01eabad4 1166 */
1a7cfe5a
MD
1167 if (chain->flags & HAMMER2_CHAIN_INITIAL) {
1168 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1169 wasinitial = 1;
1170 } else {
1171 wasinitial = 0;
1172 }
01eabad4 1173
01eabad4 1174 /*
1a7cfe5a 1175 * Instantiate data buffer and possibly execute COW operation
01eabad4 1176 */
db71f61f 1177 switch(chain->bref.type) {
01eabad4 1178 case HAMMER2_BREF_TYPE_VOLUME:
1a7cfe5a 1179 case HAMMER2_BREF_TYPE_FREEMAP:
5c23d7f1 1180 /*
01eabad4
MD
1181 * The data is embedded, no copy-on-write operation is
1182 * needed.
5c23d7f1 1183 */
fdf62707 1184 KKASSERT(chain->dio == NULL);
db71f61f 1185 break;
91caa51c
MD
1186 case HAMMER2_BREF_TYPE_INODE:
1187 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
db71f61f 1188 case HAMMER2_BREF_TYPE_DATA:
01eabad4 1189 case HAMMER2_BREF_TYPE_INDIRECT:
9061bde5 1190 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
db71f61f 1191 /*
01eabad4 1192 * Perform the copy-on-write operation
91caa51c
MD
1193 *
1194 * zero-fill or copy-on-write depending on whether
1195 * chain->data exists or not and set the dirty state for
1196 * the new buffer. hammer2_io_new() will handle the
1197 * zero-fill.
db71f61f 1198 */
1a7cfe5a
MD
1199 KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain);
1200
fdf62707
MD
1201 if (wasinitial) {
1202 error = hammer2_io_new(hmp, chain->bref.data_off,
1203 chain->bytes, &dio);
01eabad4 1204 } else {
fdf62707
MD
1205 error = hammer2_io_bread(hmp, chain->bref.data_off,
1206 chain->bytes, &dio);
01eabad4 1207 }
278ab2b2 1208 hammer2_adjreadcounter(&chain->bref, chain->bytes);
fdf62707 1209
23c7c7dd
MD
1210 /*
1211 * If an I/O error occurs make sure callers cannot accidently
1212 * modify the old buffer's contents and corrupt the filesystem.
1213 */
1214 if (error) {
1215 kprintf("hammer2_chain_modify: hmp=%p I/O error\n",
1216 hmp);
1217 chain->error = HAMMER2_ERROR_IO;
1218 hammer2_io_brelse(&dio);
1219 hammer2_io_brelse(&chain->dio);
1220 chain->data = NULL;
1221 break;
1222 }
1223 chain->error = 0;
fdf62707 1224 bdata = hammer2_io_data(dio, chain->bref.data_off);
866d5273 1225
db71f61f 1226 if (chain->data) {
fdf62707
MD
1227 KKASSERT(chain->dio != NULL);
1228 if (chain->data != (void *)bdata) {
1a7cfe5a
MD
1229 bcopy(chain->data, bdata, chain->bytes);
1230 }
fdf62707 1231 } else if (wasinitial == 0) {
1a7cfe5a
MD
1232 /*
1233 * We have a problem. We were asked to COW but
1234 * we don't have any data to COW with!
1235 */
1236 panic("hammer2_chain_modify: having a COW %p\n",
1237 chain);
db71f61f 1238 }
91caa51c
MD
1239
1240 /*
a71db85d
MD
1241 * Retire the old buffer, replace with the new. Dirty or
1242 * redirty the new buffer.
1243 *
1244 * WARNING! The system buffer cache may have already flushed
1245 * the buffer, so we must be sure to [re]dirty it
1246 * for further modification.
91caa51c
MD
1247 */
1248 if (chain->dio)
1249 hammer2_io_brelse(&chain->dio);
fdf62707
MD
1250 chain->data = (void *)bdata;
1251 chain->dio = dio;
1252 hammer2_io_setdirty(dio); /* modified by bcopy above */
db71f61f
MD
1253 break;
1254 default:
01eabad4
MD
1255 panic("hammer2_chain_modify: illegal non-embedded type %d",
1256 chain->bref.type);
db71f61f
MD
1257 break;
1258
5c23d7f1 1259 }
01eabad4 1260skip2:
da6f36f4
MD
1261 /*
1262 * setflush on parent indicating that the parent must recurse down
1263 * to us. Do not call on chain itself which might already have it
1264 * set.
1265 */
1266 if (chain->parent)
c603b86b 1267 hammer2_chain_setflush(chain->parent);
232a50f9
MD
1268}
1269
d34788ef
MD
1270/*
1271 * Modify the chain associated with an inode.
1272 */
1273void
1274hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain, int flags)
1275{
1276 hammer2_inode_modify(ip);
1277 hammer2_chain_modify(chain, flags);
1278}
1279
2910a90c 1280/*
50456506 1281 * Volume header data locks
2910a90c
MD
1282 */
1283void
506bd6d1 1284hammer2_voldata_lock(hammer2_dev_t *hmp)
2910a90c 1285{
50456506
MD
1286 lockmgr(&hmp->vollk, LK_EXCLUSIVE);
1287}
1288
1289void
506bd6d1 1290hammer2_voldata_unlock(hammer2_dev_t *hmp)
50456506
MD
1291{
1292 lockmgr(&hmp->vollk, LK_RELEASE);
1293}
1294
1295void
506bd6d1 1296hammer2_voldata_modify(hammer2_dev_t *hmp)
50456506
MD
1297{
1298 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
1299 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
1300 hammer2_chain_ref(&hmp->vchain);
da6f36f4 1301 hammer2_pfs_memory_inc(hmp->vchain.pmp);
50456506 1302 }
2910a90c
MD
1303}
1304
b7926f31 1305/*
1897c66e 1306 * This function returns the chain at the nearest key within the specified
af791767 1307 * range. The returned chain will be referenced but not locked.
1897c66e
MD
1308 *
1309 * This function will recurse through chain->rbtree as necessary and will
1310 * return a *key_nextp suitable for iteration. *key_nextp is only set if
1311 * the iteration value is less than the current value of *key_nextp.
1312 *
1313 * The caller should use (*key_nextp) to calculate the actual range of
a7720be7
MD
1314 * the returned element, which will be (key_beg to *key_nextp - 1), because
1315 * there might be another element which is superior to the returned element
1316 * and overlaps it.
1897c66e
MD
1317 *
1318 * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL
1319 * chains continue to be returned. On EOF (*key_nextp) may overflow since
1320 * it will wind up being (key_end + 1).
af791767
MD
1321 *
1322 * WARNING! Must be called with child's spinlock held. Spinlock remains
1323 * held through the operation.
b7926f31 1324 */
ea155208 1325struct hammer2_chain_find_info {
1897c66e
MD
1326 hammer2_chain_t *best;
1327 hammer2_key_t key_beg;
1328 hammer2_key_t key_end;
1329 hammer2_key_t key_next;
ea155208
MD
1330};
1331
1897c66e
MD
1332static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data);
1333static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data);
1334
1335static
1336hammer2_chain_t *
1337hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp,
1338 hammer2_key_t key_beg, hammer2_key_t key_end)
1339{
1340 struct hammer2_chain_find_info info;
1897c66e
MD
1341
1342 info.best = NULL;
1343 info.key_beg = key_beg;
1344 info.key_end = key_end;
51a0d27c 1345 info.key_next = *key_nextp;
1897c66e 1346
da6f36f4 1347 RB_SCAN(hammer2_chain_tree, &parent->core.rbtree,
8138a154
MD
1348 hammer2_chain_find_cmp, hammer2_chain_find_callback,
1349 &info);
51a0d27c
MD
1350 *key_nextp = info.key_next;
1351#if 0
1352 kprintf("chain_find %p %016jx:%016jx next=%016jx\n",
1353 parent, key_beg, key_end, *key_nextp);
1354#endif
1355
1897c66e
MD
1356 return (info.best);
1357}
1358
ea155208
MD
1359static
1360int
1361hammer2_chain_find_cmp(hammer2_chain_t *child, void *data)
1362{
1363 struct hammer2_chain_find_info *info = data;
1897c66e
MD
1364 hammer2_key_t child_beg;
1365 hammer2_key_t child_end;
ea155208 1366
1897c66e
MD
1367 child_beg = child->bref.key;
1368 child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1;
1369
1370 if (child_end < info->key_beg)
ea155208 1371 return(-1);
1897c66e 1372 if (child_beg > info->key_end)
ea155208
MD
1373 return(1);
1374 return(0);
1375}
1376
1377static
1378int
1379hammer2_chain_find_callback(hammer2_chain_t *child, void *data)
1380{
1381 struct hammer2_chain_find_info *info = data;
1897c66e
MD
1382 hammer2_chain_t *best;
1383 hammer2_key_t child_end;
1384
1897c66e 1385 /*
623d43d4
MD
1386 * WARNING! Do not discard DUPLICATED chains, it is possible that
1387 * we are catching an insertion half-way done. If a
1388 * duplicated chain turns out to be the best choice the
1389 * caller will re-check its flags after locking it.
1390 *
1391 * WARNING! Layerq is scanned forwards, exact matches should keep
1392 * the existing info->best.
1897c66e
MD
1393 */
1394 if ((best = info->best) == NULL) {
1395 /*
1396 * No previous best. Assign best
1397 */
51a0d27c
MD
1398 info->best = child;
1399 } else if (best->bref.key <= info->key_beg &&
1400 child->bref.key <= info->key_beg) {
355d67fc 1401 /*
da6f36f4 1402 * Illegal overlap.
355d67fc 1403 */
da6f36f4
MD
1404 KKASSERT(0);
1405 /*info->best = child;*/
51a0d27c 1406 } else if (child->bref.key < best->bref.key) {
1897c66e 1407 /*
51a0d27c 1408 * Child has a nearer key and best is not flush with key_beg.
da6f36f4 1409 * Set best to child. Truncate key_next to the old best key.
1897c66e 1410 */
51a0d27c 1411 info->best = child;
da6f36f4 1412 if (info->key_next > best->bref.key || info->key_next == 0)
51a0d27c 1413 info->key_next = best->bref.key;
a7720be7
MD
1414 } else if (child->bref.key == best->bref.key) {
1415 /*
da6f36f4
MD
1416 * If our current best is flush with the child then this
1417 * is an illegal overlap.
a7720be7
MD
1418 *
1419 * key_next will automatically be limited to the smaller of
1420 * the two end-points.
1421 */
da6f36f4
MD
1422 KKASSERT(0);
1423 info->best = child;
51a0d27c 1424 } else {
1897c66e 1425 /*
a7720be7 1426 * Keep the current best but truncate key_next to the child's
da6f36f4 1427 * base.
a7720be7
MD
1428 *
1429 * key_next will also automatically be limited to the smaller
1430 * of the two end-points (probably not necessary for this case
1431 * but we do it anyway).
1897c66e 1432 */
da6f36f4 1433 if (info->key_next > child->bref.key || info->key_next == 0)
a7720be7 1434 info->key_next = child->bref.key;
ea155208 1435 }
51a0d27c
MD
1436
1437 /*
1438 * Always truncate key_next based on child's end-of-range.
1439 */
1440 child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits);
1441 if (child_end && (info->key_next > child_end || info->key_next == 0))
1442 info->key_next = child_end;
1443
ea155208
MD
1444 return(0);
1445}
1446
5c23d7f1 1447/*
1897c66e 1448 * Retrieve the specified chain from a media blockref, creating the
da6f36f4 1449 * in-memory chain structure which reflects it.
1897c66e 1450 *
623d43d4
MD
1451 * To handle insertion races pass the INSERT_RACE flag along with the
1452 * generation number of the core. NULL will be returned if the generation
1453 * number changes before we have a chance to insert the chain. Insert
1454 * races can occur because the parent might be held shared.
5c23d7f1 1455 *
0dea3156
MD
1456 * Caller must hold the parent locked shared or exclusive since we may
1457 * need the parent's bref array to find our block.
50456506 1458 *
506bd6d1
MD
1459 * WARNING! chain->pmp is always set to NULL for any chain representing
1460 * part of the super-root topology.
232a50f9
MD
1461 */
1462hammer2_chain_t *
8138a154
MD
1463hammer2_chain_get(hammer2_chain_t *parent, int generation,
1464 hammer2_blockref_t *bref)
232a50f9 1465{
506bd6d1 1466 hammer2_dev_t *hmp = parent->hmp;
232a50f9 1467 hammer2_chain_t *chain;
623d43d4 1468 int error;
6934ae32
MD
1469
1470 /*
1471 * Allocate a chain structure representing the existing media
0dea3156 1472 * entry. Resulting chain has one ref and is not locked.
6934ae32 1473 */
50456506 1474 if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT)
c603b86b 1475 chain = hammer2_chain_alloc(hmp, NULL, bref);
50456506 1476 else
c603b86b 1477 chain = hammer2_chain_alloc(hmp, parent->pmp, bref);
1897c66e 1478 /* ref'd chain returned */
8138a154
MD
1479
1480 /*
da6f36f4
MD
1481 * Flag that the chain is in the parent's blockmap so delete/flush
1482 * knows what to do with it.
8138a154 1483 */
8138a154 1484 atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED);
5c23d7f1
MD
1485
1486 /*
0dea3156
MD
1487 * Link the chain into its parent. A spinlock is required to safely
1488 * access the RBTREE, and it is possible to collide with another
1489 * hammer2_chain_get() operation because the caller might only hold
1490 * a shared lock on the parent.
5c23d7f1 1491 */
0dea3156 1492 KKASSERT(parent->refs > 0);
da6f36f4 1493 error = hammer2_chain_insert(parent, chain,
623d43d4
MD
1494 HAMMER2_CHAIN_INSERT_SPIN |
1495 HAMMER2_CHAIN_INSERT_RACE,
1496 generation);
1497 if (error) {
da6f36f4 1498 KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0);
623d43d4 1499 kprintf("chain %p get race\n", chain);
0dea3156 1500 hammer2_chain_drop(chain);
1897c66e 1501 chain = NULL;
623d43d4
MD
1502 } else {
1503 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
0dea3156 1504 }
5c23d7f1
MD
1505
1506 /*
623d43d4
MD
1507 * Return our new chain referenced but not locked, or NULL if
1508 * a race occurred.
5c23d7f1 1509 */
232a50f9
MD
1510 return (chain);
1511}
1512
0dea3156
MD
1513/*
1514 * Lookup initialization/completion API
1515 */
1516hammer2_chain_t *
1517hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags)
1518{
e513e77e 1519 hammer2_chain_ref(parent);
0dea3156
MD
1520 if (flags & HAMMER2_LOOKUP_SHARED) {
1521 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1522 HAMMER2_RESOLVE_SHARED);
1523 } else {
1524 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1525 }
1526 return (parent);
1527}
1528
1529void
1530hammer2_chain_lookup_done(hammer2_chain_t *parent)
1531{
e513e77e 1532 if (parent) {
0dea3156 1533 hammer2_chain_unlock(parent);
e513e77e
MD
1534 hammer2_chain_drop(parent);
1535 }
0dea3156
MD
1536}
1537
731b2a84
MD
1538hammer2_chain_t *
1539hammer2_chain_getparent(hammer2_chain_t **parentp, int how)
1540{
1541 hammer2_chain_t *oparent;
1542 hammer2_chain_t *nparent;
51a0d27c 1543
2a8b1c40
MD
1544 /*
1545 * Be careful of order, oparent must be unlocked before nparent
da6f36f4 1546 * is locked below to avoid a deadlock.
2a8b1c40 1547 */
da6f36f4 1548 oparent = *parentp;
94491fa0 1549 hammer2_spin_ex(&oparent->core.spin);
da6f36f4
MD
1550 nparent = oparent->parent;
1551 hammer2_chain_ref(nparent);
94491fa0 1552 hammer2_spin_unex(&oparent->core.spin);
da6f36f4
MD
1553 if (oparent) {
1554 hammer2_chain_unlock(oparent);
e513e77e 1555 hammer2_chain_drop(oparent);
da6f36f4 1556 oparent = NULL;
51a0d27c 1557 }
da6f36f4 1558
e513e77e 1559 hammer2_chain_lock(nparent, how);
731b2a84
MD
1560 *parentp = nparent;
1561
1562 return (nparent);
1563}
0dea3156 1564
7cfa8da5 1565/*
1897c66e
MD
1566 * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive.
1567 * (*parentp) typically points to an inode but can also point to a related
1568 * indirect block and this function will recurse upwards and find the inode
1569 * again.
37aa19df 1570 *
5c23d7f1
MD
1571 * (*parentp) must be exclusively locked and referenced and can be an inode
1572 * or an existing indirect block within the inode.
1573 *
1574 * On return (*parentp) will be modified to point at the deepest parent chain
1575 * element encountered during the search, as a helper for an insertion or
1576 * deletion. The new (*parentp) will be locked and referenced and the old
1577 * will be unlocked and dereferenced (no change if they are both the same).
1578 *
0dea3156 1579 * The matching chain will be returned exclusively locked. If NOLOCK is
e513e77e
MD
1580 * requested the chain will be returned only referenced. Note that the
1581 * parent chain must always be locked shared or exclusive, matching the
1582 * HAMMER2_LOOKUP_SHARED flag. We can conceivably lock it SHARED temporarily
1583 * when NOLOCK is specified but that complicates matters if *parentp must
1584 * inherit the chain.
1585 *
1586 * NOLOCK also implies NODATA, since an unlocked chain usually has a NULL
1587 * data pointer or can otherwise be in flux.
5c23d7f1
MD
1588 *
1589 * NULL is returned if no match was found, but (*parentp) will still
1590 * potentially be adjusted.
1591 *
b93cc2e0
MD
1592 * If a fatal error occurs (typically an I/O error), a dummy chain is
1593 * returned with chain->error and error-identifying information set. This
1594 * chain will assert if you try to do anything fancy with it.
1595 *
1596 * XXX Depending on where the error occurs we should allow continued iteration.
1597 *
1897c66e 1598 * On return (*key_nextp) will point to an iterative value for key_beg.
f7712c43 1599 * (If NULL is returned (*key_nextp) is set to (key_end + 1)).
1897c66e 1600 *
5c23d7f1
MD
1601 * This function will also recurse up the chain if the key is not within the
1602 * current parent's range. (*parentp) can never be set to NULL. An iteration
1603 * can simply allow (*parentp) to float inside the loop.
1a7cfe5a
MD
1604 *
1605 * NOTE! chain->data is not always resolved. By default it will not be
1606 * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use
1607 * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/
1608 * BREF_TYPE_DATA as the device buffer can alias the logical file
1609 * buffer).
7cfa8da5
MD
1610 */
1611hammer2_chain_t *
1897c66e 1612hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp,
c667909f 1613 hammer2_key_t key_beg, hammer2_key_t key_end,
b8ba9690 1614 int *cache_indexp, int flags)
7cfa8da5 1615{
506bd6d1 1616 hammer2_dev_t *hmp;
5c23d7f1 1617 hammer2_chain_t *parent;
232a50f9 1618 hammer2_chain_t *chain;
5c23d7f1 1619 hammer2_blockref_t *base;
51a0d27c
MD
1620 hammer2_blockref_t *bref;
1621 hammer2_blockref_t bcopy;
e028fa74
MD
1622 hammer2_key_t scan_beg;
1623 hammer2_key_t scan_end;
232a50f9 1624 int count = 0;
a0ed3c24
MD
1625 int how_always = HAMMER2_RESOLVE_ALWAYS;
1626 int how_maybe = HAMMER2_RESOLVE_MAYBE;
1897c66e 1627 int how;
623d43d4
MD
1628 int generation;
1629 int maxloops = 300000;
a0ed3c24 1630
1897c66e 1631 if (flags & HAMMER2_LOOKUP_ALWAYS) {
1a7cfe5a 1632 how_maybe = how_always;
1897c66e
MD
1633 how = HAMMER2_RESOLVE_ALWAYS;
1634 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
1635 how = HAMMER2_RESOLVE_NEVER;
1636 } else {
1637 how = HAMMER2_RESOLVE_MAYBE;
1638 }
e513e77e 1639 if (flags & HAMMER2_LOOKUP_SHARED) {
a0ed3c24
MD
1640 how_maybe |= HAMMER2_RESOLVE_SHARED;
1641 how_always |= HAMMER2_RESOLVE_SHARED;
1897c66e 1642 how |= HAMMER2_RESOLVE_SHARED;
a0ed3c24 1643 }
232a50f9 1644
232a50f9 1645 /*
e028fa74
MD
1646 * Recurse (*parentp) upward if necessary until the parent completely
1647 * encloses the key range or we hit the inode.
1897c66e
MD
1648 *
1649 * This function handles races against the flusher doing a delete-
1650 * duplicate above us and re-homes the parent to the duplicate in
1651 * that case, otherwise we'd wind up recursing down a stale chain.
5c23d7f1
MD
1652 */
1653 parent = *parentp;
0dea3156
MD
1654 hmp = parent->hmp;
1655
9061bde5
MD
1656 while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1657 parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
e028fa74
MD
1658 scan_beg = parent->bref.key;
1659 scan_end = scan_beg +
1660 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1661 if (key_beg >= scan_beg && key_end <= scan_end)
5c23d7f1 1662 break;
731b2a84 1663 parent = hammer2_chain_getparent(parentp, how_maybe);
5c23d7f1
MD
1664 }
1665
1666again:
623d43d4
MD
1667 if (--maxloops == 0)
1668 panic("hammer2_chain_lookup: maxloops");
5c23d7f1
MD
1669 /*
1670 * Locate the blockref array. Currently we do a fully associative
1671 * search through the array.
232a50f9
MD
1672 */
1673 switch(parent->bref.type) {
1674 case HAMMER2_BREF_TYPE_INODE:
3ac6a319
MD
1675 /*
1676 * Special shortcut for embedded data returns the inode
1677 * itself. Callers must detect this condition and access
1678 * the embedded data (the strategy code does this for us).
1679 *
1680 * This is only applicable to regular files and softlinks.
1681 */
b0f58de8
MD
1682 if (parent->data->ipdata.meta.op_flags &
1683 HAMMER2_OPFLAG_DIRECTDATA) {
f7712c43
MD
1684 if (flags & HAMMER2_LOOKUP_NODIRECT) {
1685 chain = NULL;
1686 *key_nextp = key_end + 1;
1687 goto done;
1688 }
e513e77e
MD
1689 hammer2_chain_ref(parent);
1690 if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0)
0dea3156 1691 hammer2_chain_lock(parent, how_always);
1897c66e 1692 *key_nextp = key_end + 1;
3ac6a319
MD
1693 return (parent);
1694 }
5c23d7f1
MD
1695 base = &parent->data->ipdata.u.blockset.blockref[0];
1696 count = HAMMER2_SET_COUNT;
232a50f9 1697 break;
9061bde5 1698 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1a7cfe5a
MD
1699 case HAMMER2_BREF_TYPE_INDIRECT:
1700 /*
1701 * Handle MATCHIND on the parent
1702 */
1703 if (flags & HAMMER2_LOOKUP_MATCHIND) {
1704 scan_beg = parent->bref.key;
1705 scan_end = scan_beg +
1706 ((hammer2_key_t)1 << parent->bref.keybits) - 1;
1707 if (key_beg == scan_beg && key_end == scan_end) {
1708 chain = parent;
e513e77e 1709 hammer2_chain_ref(chain);
1a7cfe5a 1710 hammer2_chain_lock(chain, how_maybe);
1897c66e 1711 *key_nextp = scan_end + 1;
1a7cfe5a
MD
1712 goto done;
1713 }
1714 }
01eabad4
MD
1715 /*
1716 * Optimize indirect blocks in the INITIAL state to avoid
1717 * I/O.
1718 */
1719 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
1720 base = NULL;
1721 } else {
1722 if (parent->data == NULL)
1723 panic("parent->data is NULL");
93f3933a 1724 base = &parent->data->npdata[0];
01eabad4 1725 }
6ba3b984 1726 count = parent->bytes / sizeof(hammer2_blockref_t);
232a50f9
MD
1727 break;
1728 case HAMMER2_BREF_TYPE_VOLUME:
5c23d7f1
MD
1729 base = &hmp->voldata.sroot_blockset.blockref[0];
1730 count = HAMMER2_SET_COUNT;
232a50f9 1731 break;
1a7cfe5a
MD
1732 case HAMMER2_BREF_TYPE_FREEMAP:
1733 base = &hmp->voldata.freemap_blockset.blockref[0];
1734 count = HAMMER2_SET_COUNT;
1735 break;
232a50f9 1736 default:
a6cf1052
MD
1737 kprintf("hammer2_chain_lookup: unrecognized "
1738 "blockref(B) type: %d",
1739 parent->bref.type);
1740 while (1)
1741 tsleep(&base, 0, "dead", 0);
1742 panic("hammer2_chain_lookup: unrecognized "
1743 "blockref(B) type: %d",
232a50f9 1744 parent->bref.type);
5c23d7f1
MD
1745 base = NULL; /* safety */
1746 count = 0; /* safety */
232a50f9
MD
1747 }
1748
5c23d7f1 1749 /*
1897c66e 1750 * Merged scan to find next candidate.
5f6853df 1751 *
da6f36f4 1752 * hammer2_base_*() functions require the parent->core.live_* fields
1897c66e 1753 * to be synchronized.
0dea3156 1754 *
1897c66e
MD
1755 * We need to hold the spinlock to access the block array and RB tree
1756 * and to interlock chain creation.
5c23d7f1 1757 */
f1c7c224 1758 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
51a0d27c 1759 hammer2_chain_countbrefs(parent, base, count);
5c23d7f1
MD
1760
1761 /*
51a0d27c 1762 * Combined search
1897c66e 1763 */
94491fa0 1764 hammer2_spin_ex(&parent->core.spin);
51a0d27c
MD
1765 chain = hammer2_combined_find(parent, base, count,
1766 cache_indexp, key_nextp,
8138a154
MD
1767 key_beg, key_end,
1768 &bref);
da6f36f4 1769 generation = parent->core.generation;
1897c66e
MD
1770
1771 /*
1772 * Exhausted parent chain, iterate.
1897c66e 1773 */
51a0d27c 1774 if (bref == NULL) {
94491fa0 1775 hammer2_spin_unex(&parent->core.spin);
1897c66e
MD
1776 if (key_beg == key_end) /* short cut single-key case */
1777 return (NULL);
c9bda9e7
MD
1778
1779 /*
1780 * Stop if we reached the end of the iteration.
1781 */
1782 if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1783 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
1784 return (NULL);
1785 }
1786
1787 /*
1788 * Calculate next key, stop if we reached the end of the
1789 * iteration, otherwise go up one level and loop.
1790 */
1791 key_beg = parent->bref.key +
1792 ((hammer2_key_t)1 << parent->bref.keybits);
1793 if (key_beg == 0 || key_beg > key_end)
1794 return (NULL);
1795 parent = hammer2_chain_getparent(parentp, how_maybe);
1796 goto again;
1897c66e
MD
1797 }
1798
51a0d27c
MD
1799 /*
1800 * Selected from blockref or in-memory chain.
1801 */
1802 if (chain == NULL) {
1803 bcopy = *bref;
94491fa0 1804 hammer2_spin_unex(&parent->core.spin);
8138a154
MD
1805 chain = hammer2_chain_get(parent, generation,
1806 &bcopy);
51a0d27c
MD
1807 if (chain == NULL) {
1808 kprintf("retry lookup parent %p keys %016jx:%016jx\n",
1809 parent, key_beg, key_end);
1810 goto again;
1811 }
1812 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
1813 hammer2_chain_drop(chain);
1814 goto again;
1815 }
1816 } else {
1817 hammer2_chain_ref(chain);
94491fa0 1818 hammer2_spin_unex(&parent->core.spin);
51a0d27c 1819 }
623d43d4
MD
1820
1821 /*
1822 * chain is referenced but not locked. We must lock the chain
1823 * to obtain definitive DUPLICATED/DELETED state
1824 */
1825 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1826 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
e513e77e 1827 hammer2_chain_lock(chain, how_maybe);
623d43d4 1828 } else {
e513e77e 1829 hammer2_chain_lock(chain, how);
623d43d4 1830 }
51a0d27c 1831
1897c66e
MD
1832 /*
1833 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
1834 *
623d43d4 1835 * NOTE: Chain's key range is not relevant as there might be
1897c66e 1836 * one-offs within the range that are not deleted.
623d43d4 1837 *
0924b3f8
MD
1838 * NOTE: Lookups can race delete-duplicate because
1839 * delete-duplicate does not lock the parent's core
1840 * (they just use the spinlock on the core). We must
1841 * check for races by comparing the DUPLICATED flag before
1842 * releasing the spinlock with the flag after locking the
1843 * chain.
1897c66e
MD
1844 */
1845 if (chain->flags & HAMMER2_CHAIN_DELETED) {
623d43d4 1846 hammer2_chain_unlock(chain);
e513e77e 1847 hammer2_chain_drop(chain);
da6f36f4
MD
1848 key_beg = *key_nextp;
1849 if (key_beg == 0 || key_beg > key_end)
1850 return(NULL);
1897c66e
MD
1851 goto again;
1852 }
1853
1854 /*
1855 * If the chain element is an indirect block it becomes the new
1856 * parent and we loop on it. We must maintain our top-down locks
1857 * to prevent the flusher from interfering (i.e. doing a
1858 * delete-duplicate and leaving us recursing down a deleted chain).
1859 *
1860 * The parent always has to be locked with at least RESOLVE_MAYBE
1861 * so we can access its data. It might need a fixup if the caller
1862 * passed incompatible flags. Be careful not to cause a deadlock
1863 * as a data-load requires an exclusive lock.
1864 *
1865 * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key
1866 * range is within the requested key range we return the indirect
1867 * block and do NOT loop. This is usually only used to acquire
1a7cfe5a 1868 * freemap nodes.
5c23d7f1 1869 */
9061bde5
MD
1870 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
1871 chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) {
0dea3156 1872 hammer2_chain_unlock(parent);
e513e77e 1873 hammer2_chain_drop(parent);
5c23d7f1
MD
1874 *parentp = parent = chain;
1875 goto again;
1876 }
1a7cfe5a 1877done:
5c23d7f1 1878 /*
8db69c9f
MD
1879 * All done, return the chain.
1880 *
1881 * If the caller does not want a locked chain, replace the lock with
1882 * a ref. Perhaps this can eventually be optimized to not obtain the
1883 * lock in the first place for situations where the data does not
1884 * need to be resolved.
5c23d7f1 1885 */
8db69c9f 1886 if (chain) {
e513e77e 1887 if (flags & HAMMER2_LOOKUP_NOLOCK)
8db69c9f 1888 hammer2_chain_unlock(chain);
8db69c9f
MD
1889 }
1890
232a50f9 1891 return (chain);
7cfa8da5
MD
1892}
1893
1894/*
5c23d7f1
MD
1895 * After having issued a lookup we can iterate all matching keys.
1896 *
1897 * If chain is non-NULL we continue the iteration from just after it's index.
1898 *
1899 * If chain is NULL we assume the parent was exhausted and continue the
1900 * iteration at the next parent.
8e12e3c9 1901 *
b93cc2e0
MD
1902 * If a fatal error occurs (typically an I/O error), a dummy chain is
1903 * returned with chain->error and error-identifying information set. This
1904 * chain will assert if you try to do anything fancy with it.
1905 *
1906 * XXX Depending on where the error occurs we should allow continued iteration.
1907 *
8e12e3c9 1908 * parent must be locked on entry and remains locked throughout. chain's
0dea3156 1909 * lock status must match flags. Chain is always at least referenced.
1a7cfe5a
MD
1910 *
1911 * WARNING! The MATCHIND flag does not apply to this function.
7cfa8da5
MD
1912 */
1913hammer2_chain_t *
0dea3156 1914hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain,
1897c66e 1915 hammer2_key_t *key_nextp,
c667909f 1916 hammer2_key_t key_beg, hammer2_key_t key_end,
1897c66e 1917 int *cache_indexp, int flags)
7cfa8da5 1918{
5c23d7f1 1919 hammer2_chain_t *parent;
1897c66e 1920 int how_maybe;
5c23d7f1 1921
1897c66e
MD
1922 /*
1923 * Calculate locking flags for upward recursion.
1924 */
1925 how_maybe = HAMMER2_RESOLVE_MAYBE;
e513e77e 1926 if (flags & HAMMER2_LOOKUP_SHARED)
a0ed3c24
MD
1927 how_maybe |= HAMMER2_RESOLVE_SHARED;
1928
5c23d7f1 1929 parent = *parentp;
232a50f9 1930
5c23d7f1
MD
1931 /*
1932 * Calculate the next index and recalculate the parent if necessary.
1933 */
1934 if (chain) {
1897c66e
MD
1935 key_beg = chain->bref.key +
1936 ((hammer2_key_t)1 << chain->bref.keybits);
c847e838
MD
1937 if ((flags & (HAMMER2_LOOKUP_NOLOCK |
1938 HAMMER2_LOOKUP_NOUNLOCK)) == 0) {
0dea3156 1939 hammer2_chain_unlock(chain);
c847e838 1940 }
e513e77e 1941 hammer2_chain_drop(chain);
3ac6a319
MD
1942
1943 /*
b93cc2e0
MD
1944 * chain invalid past this point, but we can still do a
1945 * pointer comparison w/parent.
1946 *
3ac6a319
MD
1947 * Any scan where the lookup returned degenerate data embedded
1948 * in the inode has an invalid index and must terminate.
1949 */
1950 if (chain == parent)
1951 return(NULL);
1897c66e
MD
1952 if (key_beg == 0 || key_beg > key_end)
1953 return(NULL);
5c23d7f1 1954 chain = NULL;
9061bde5
MD
1955 } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT &&
1956 parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) {
5c23d7f1
MD
1957 /*
1958 * We reached the end of the iteration.
1959 */
1960 return (NULL);
1961 } else {
1962 /*
37aa19df
MD
1963 * Continue iteration with next parent unless the current
1964 * parent covers the range.
5c23d7f1 1965 */
1897c66e
MD
1966 key_beg = parent->bref.key +
1967 ((hammer2_key_t)1 << parent->bref.keybits);
1968 if (key_beg == 0 || key_beg > key_end)
37aa19df 1969 return (NULL);
731b2a84 1970 parent = hammer2_chain_getparent(parentp, how_maybe);
5c23d7f1 1971 }
232a50f9 1972
5c23d7f1 1973 /*
1897c66e 1974 * And execute
5c23d7f1 1975 */
1897c66e
MD
1976 return (hammer2_chain_lookup(parentp, key_nextp,
1977 key_beg, key_end,
b8ba9690 1978 cache_indexp, flags));
91abd410
MD
1979}
1980
10136ab6 1981/*
877eacb6
MD
1982 * The raw scan function is similar to lookup/next but does not seek to a key.
1983 * Blockrefs are iterated via first_chain = (parent, NULL) and
1984 * next_chain = (parent, chain).
1985 *
1986 * The passed-in parent must be locked and its data resolved. The returned
1987 * chain will be locked. Pass chain == NULL to acquire the first sub-chain
1988 * under parent and then iterate with the passed-in chain (which this
1989 * function will unlock).
10136ab6
MD
1990 */
1991hammer2_chain_t *
1992hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t *chain,
1993 int *cache_indexp, int flags)
1994{
506bd6d1 1995 hammer2_dev_t *hmp;
10136ab6
MD
1996 hammer2_blockref_t *base;
1997 hammer2_blockref_t *bref;
1998 hammer2_blockref_t bcopy;
10136ab6
MD
1999 hammer2_key_t key;
2000 hammer2_key_t next_key;
2001 int count = 0;
2002 int how_always = HAMMER2_RESOLVE_ALWAYS;
2003 int how_maybe = HAMMER2_RESOLVE_MAYBE;
2004 int how;
623d43d4
MD
2005 int generation;
2006 int maxloops = 300000;
10136ab6
MD
2007
2008 hmp = parent->hmp;
2009
2010 /*
e513e77e 2011 * Scan flags borrowed from lookup.
10136ab6
MD
2012 */
2013 if (flags & HAMMER2_LOOKUP_ALWAYS) {
2014 how_maybe = how_always;
2015 how = HAMMER2_RESOLVE_ALWAYS;
2016 } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) {
2017 how = HAMMER2_RESOLVE_NEVER;
2018 } else {
2019 how = HAMMER2_RESOLVE_MAYBE;
2020 }
e513e77e 2021 if (flags & HAMMER2_LOOKUP_SHARED) {
10136ab6
MD
2022 how_maybe |= HAMMER2_RESOLVE_SHARED;
2023 how_always |= HAMMER2_RESOLVE_SHARED;
2024 how |= HAMMER2_RESOLVE_SHARED;
2025 }
2026
2027 /*
2028 * Calculate key to locate first/next element, unlocking the previous
2029 * element as we go. Be careful, the key calculation can overflow.
2030 */
2031 if (chain) {
2032 key = chain->bref.key +
2033 ((hammer2_key_t)1 << chain->bref.keybits);
2034 hammer2_chain_unlock(chain);
e513e77e 2035 hammer2_chain_drop(chain);
10136ab6
MD
2036 chain = NULL;
2037 if (key == 0)
2038 goto done;
2039 } else {
2040 key = 0;
2041 }
2042
2043again:
b93cc2e0 2044 KKASSERT(parent->error == 0); /* XXX case not handled yet */
623d43d4
MD
2045 if (--maxloops == 0)
2046 panic("hammer2_chain_scan: maxloops");
10136ab6
MD
2047 /*
2048 * Locate the blockref array. Currently we do a fully associative
2049 * search through the array.
2050 */
2051 switch(parent->bref.type) {
2052 case HAMMER2_BREF_TYPE_INODE:
2053 /*
2054 * An inode with embedded data has no sub-chains.
2055 */
b0f58de8
MD
2056 if (parent->data->ipdata.meta.op_flags &
2057 HAMMER2_OPFLAG_DIRECTDATA) {
10136ab6 2058 goto done;
b0f58de8 2059 }
10136ab6
MD
2060 base = &parent->data->ipdata.u.blockset.blockref[0];
2061 count = HAMMER2_SET_COUNT;
2062 break;
2063 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2064 case HAMMER2_BREF_TYPE_INDIRECT:
2065 /*
2066 * Optimize indirect blocks in the INITIAL state to avoid
2067 * I/O.
2068 */
2069 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2070 base = NULL;
2071 } else {
2072 if (parent->data == NULL)
2073 panic("parent->data is NULL");
2074 base = &parent->data->npdata[0];
2075 }
2076 count = parent->bytes / sizeof(hammer2_blockref_t);
2077 break;
2078 case HAMMER2_BREF_TYPE_VOLUME:
2079 base = &hmp->voldata.sroot_blockset.blockref[0];
2080 count = HAMMER2_SET_COUNT;
2081 break;
2082 case HAMMER2_BREF_TYPE_FREEMAP:
2083 base = &hmp->voldata.freemap_blockset.blockref[0];
2084 count = HAMMER2_SET_COUNT;
2085 break;
2086 default:
2087 panic("hammer2_chain_lookup: unrecognized blockref type: %d",
2088 parent->bref.type);
2089 base = NULL; /* safety */
2090 count = 0; /* safety */
2091 }
2092
2093 /*
2094 * Merged scan to find next candidate.
2095 *
da6f36f4 2096 * hammer2_base_*() functions require the parent->core.live_* fields
10136ab6
MD
2097 * to be synchronized.
2098 *
2099 * We need to hold the spinlock to access the block array and RB tree
2100 * and to interlock chain creation.
2101 */
f1c7c224 2102 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
10136ab6
MD
2103 hammer2_chain_countbrefs(parent, base, count);
2104
10136ab6 2105 next_key = 0;
94491fa0 2106 hammer2_spin_ex(&parent->core.spin);
10136ab6
MD
2107 chain = hammer2_combined_find(parent, base, count,
2108 cache_indexp, &next_key,
50456506 2109 key, HAMMER2_KEY_MAX,
8138a154 2110 &bref);
da6f36f4 2111 generation = parent->core.generation;
10136ab6
MD
2112
2113 /*
2114 * Exhausted parent chain, we're done.
2115 */
2116 if (bref == NULL) {
94491fa0 2117 hammer2_spin_unex(&parent->core.spin);
10136ab6
MD
2118 KKASSERT(chain == NULL);
2119 goto done;
2120 }
2121
2122 /*
2123 * Selected from blockref or in-memory chain.
2124 */
2125 if (chain == NULL) {
2126 bcopy = *bref;
94491fa0 2127 hammer2_spin_unex(&parent->core.spin);
8138a154 2128 chain = hammer2_chain_get(parent, generation, &bcopy);
10136ab6
MD
2129 if (chain == NULL) {
2130 kprintf("retry scan parent %p keys %016jx\n",
2131 parent, key);
2132 goto again;
2133 }
2134 if (bcmp(&bcopy, bref, sizeof(bcopy))) {
2135 hammer2_chain_drop(chain);
2136 chain = NULL;
2137 goto again;
2138 }
2139 } else {
2140 hammer2_chain_ref(chain);
94491fa0 2141 hammer2_spin_unex(&parent->core.spin);
10136ab6 2142 }
623d43d4
MD
2143
2144 /*
2145 * chain is referenced but not locked. We must lock the chain
2146 * to obtain definitive DUPLICATED/DELETED state
2147 */
e513e77e 2148 hammer2_chain_lock(chain, how);
10136ab6
MD
2149
2150 /*
2151 * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX)
2152 *
2153 * NOTE: chain's key range is not relevant as there might be
2154 * one-offs within the range that are not deleted.
2155 *
2156 * NOTE: XXX this could create problems with scans used in
2157 * situations other than mount-time recovery.
623d43d4 2158 *
0924b3f8
MD
2159 * NOTE: Lookups can race delete-duplicate because
2160 * delete-duplicate does not lock the parent's core
2161 * (they just use the spinlock on the core). We must
2162 * check for races by comparing the DUPLICATED flag before
2163 * releasing the spinlock with the flag after locking the
2164 * chain.
10136ab6
MD
2165 */
2166 if (chain->flags & HAMMER2_CHAIN_DELETED) {
623d43d4 2167 hammer2_chain_unlock(chain);
e513e77e 2168 hammer2_chain_drop(chain);
10136ab6
MD
2169 chain = NULL;
2170
da6f36f4
MD
2171 key = next_key;
2172 if (key == 0)
2173 goto done;
10136ab6
MD
2174 goto again;
2175 }
2176
10136ab6
MD
2177done:
2178 /*
2179 * All done, return the chain or NULL
2180 */
2181 return (chain);
2182}
2183
7cfa8da5 2184/*
5c23d7f1 2185 * Create and return a new hammer2 system memory structure of the specified
d001f460
MD
2186 * key, type and size and insert it under (*parentp). This is a full
2187 * insertion, based on the supplied key/keybits, and may involve creating
2188 * indirect blocks and moving other chains around via delete/duplicate.
37aa19df 2189 *
59c5121a
MD
2190 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION
2191 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2192 * FULL. This typically means that the caller is creating the chain after
2193 * doing a hammer2_chain_lookup().
2194 *
d001f460
MD
2195 * (*parentp) must be exclusive locked and may be replaced on return
2196 * depending on how much work the function had to do.
5c23d7f1 2197 *
b93cc2e0
MD
2198 * (*parentp) must not be errored or this function will assert.
2199 *
d001f460
MD
2200 * (*chainp) usually starts out NULL and returns the newly created chain,
2201 * but if the caller desires the caller may allocate a disconnected chain
da6f36f4 2202 * and pass it in instead.
0dea3156 2203 *
d001f460
MD
2204 * This function should NOT be used to insert INDIRECT blocks. It is
2205 * typically used to create/insert inodes and data blocks.
5c23d7f1 2206 *
d001f460
MD
2207 * Caller must pass-in an exclusively locked parent the new chain is to
2208 * be inserted under, and optionally pass-in a disconnected, exclusively
2209 * locked chain to insert (else we create a new chain). The function will
1a7cfe5a
MD
2210 * adjust (*parentp) as necessary, create or connect the chain, and
2211 * return an exclusively locked chain in *chainp.
506bd6d1
MD
2212 *
2213 * When creating a PFSROOT inode under the super-root, pmp is typically NULL
2214 * and will be reassigned.
7cfa8da5 2215 */
0dea3156 2216int
c603b86b 2217hammer2_chain_create(hammer2_chain_t **parentp,
506bd6d1 2218 hammer2_chain_t **chainp, hammer2_pfs_t *pmp,
b3659de2
MD
2219 hammer2_key_t key, int keybits, int type, size_t bytes,
2220 int flags)
7cfa8da5 2221{
506bd6d1 2222 hammer2_dev_t *hmp;
0dea3156 2223 hammer2_chain_t *chain;
da6f36f4 2224 hammer2_chain_t *parent;
5c23d7f1 2225 hammer2_blockref_t *base;
1897c66e 2226 hammer2_blockref_t dummy;
6934ae32 2227 int allocated = 0;
0dea3156 2228 int error = 0;
5c23d7f1 2229 int count;
623d43d4 2230 int maxloops = 300000;
232a50f9 2231
50456506
MD
2232 /*
2233 * Topology may be crossing a PFS boundary.
2234 */
da6f36f4 2235 parent = *parentp;
f1c7c224 2236 KKASSERT(hammer2_mtx_owned(&parent->lock));
b93cc2e0 2237 KKASSERT(parent->error == 0);
0dea3156
MD
2238 hmp = parent->hmp;
2239 chain = *chainp;
a0ed3c24 2240
6934ae32
MD
2241 if (chain == NULL) {
2242 /*
2243 * First allocate media space and construct the dummy bref,
1a7cfe5a 2244 * then allocate the in-memory chain structure. Set the
925e4ad1
MD
2245 * INITIAL flag for fresh chains which do not have embedded
2246 * data.
6934ae32
MD
2247 */
2248 bzero(&dummy, sizeof(dummy));
2249 dummy.type = type;
2250 dummy.key = key;
2251 dummy.keybits = keybits;
1a7cfe5a 2252 dummy.data_off = hammer2_getradix(bytes);
9061bde5 2253 dummy.methods = parent->bref.methods;
c603b86b 2254 chain = hammer2_chain_alloc(hmp, pmp, &dummy);
731b2a84
MD
2255
2256 /*
2257 * Lock the chain manually, chain_lock will load the chain
2258 * which we do NOT want to do. (note: chain->refs is set
2259 * to 1 by chain_alloc() for us, but lockcnt is not).
2260 */
2261 chain->lockcnt = 1;
f1c7c224 2262 hammer2_mtx_ex(&chain->lock);
6934ae32 2263 allocated = 1;
232a50f9 2264
6934ae32 2265 /*
0cc33e20
MD
2266 * Set INITIAL to optimize I/O. The flag will generally be
2267 * processed when we call hammer2_chain_modify().
8e12e3c9 2268 *
6934ae32
MD
2269 * Recalculate bytes to reflect the actual media block
2270 * allocation.
2271 */
2272 bytes = (hammer2_off_t)1 <<
2273 (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
866d5273 2274 chain->bytes = bytes;
6934ae32
MD
2275
2276 switch(type) {
2277 case HAMMER2_BREF_TYPE_VOLUME:
1a7cfe5a 2278 case HAMMER2_BREF_TYPE_FREEMAP:
6934ae32
MD
2279 panic("hammer2_chain_create: called with volume type");
2280 break;
6ba3b984 2281 case HAMMER2_BREF_TYPE_INDIRECT:
01eabad4
MD
2282 panic("hammer2_chain_create: cannot be used to"
2283 "create indirect block");
2284 break;
9061bde5
MD
2285 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2286 panic("hammer2_chain_create: cannot be used to"
2287 "create freemap root or node");
2288 break;
2289 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
512beabd 2290 KKASSERT(bytes == sizeof(chain->data->bmdata));
91caa51c
MD
2291 /* fall through */
2292 case HAMMER2_BREF_TYPE_INODE:
8cce658d 2293 case HAMMER2_BREF_TYPE_DATA:
6934ae32 2294 default:
925e4ad1
MD
2295 /*
2296 * leave chain->data NULL, set INITIAL
2297 */
6934ae32 2298 KKASSERT(chain->data == NULL);
925e4ad1 2299 atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
6934ae32
MD
2300 break;
2301 }
2302 } else {
2303 /*
da6f36f4
MD
2304 * We are reattaching a previously deleted chain, possibly
2305 * under a new parent and possibly with a new key/keybits.
2306 * The chain does not have to be in a modified state. The
2307 * UPDATE flag will be set later on in this routine.
1a7cfe5a
MD
2308 *
2309 * Do NOT mess with the current state of the INITIAL flag.
6934ae32
MD
2310 */
2311 chain->bref.key = key;
2312 chain->bref.keybits = keybits;
da6f36f4
MD
2313 if (chain->flags & HAMMER2_CHAIN_DELETED)
2314 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2315 KKASSERT(chain->parent == NULL);
5c23d7f1 2316 }
506bd6d1
MD
2317 if (flags & HAMMER2_INSERT_PFSROOT)
2318 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
2319 else
2320 chain->bref.flags &= ~HAMMER2_BREF_FLAG_PFSROOT;
5c23d7f1 2321
1897c66e
MD
2322 /*
2323 * Calculate how many entries we have in the blockref array and
2324 * determine if an indirect block is required.
2325 */
995e78dc 2326again:
623d43d4
MD
2327 if (--maxloops == 0)
2328 panic("hammer2_chain_create: maxloops");
731b2a84 2329
232a50f9
MD
2330 switch(parent->bref.type) {
2331 case HAMMER2_BREF_TYPE_INODE:
b0f58de8 2332 KKASSERT((parent->data->ipdata.meta.op_flags &
28ee5f14 2333 HAMMER2_OPFLAG_DIRECTDATA) == 0);
995e78dc 2334 KKASSERT(parent->data != NULL);
5c23d7f1
MD
2335 base = &parent->data->ipdata.u.blockset.blockref[0];
2336 count = HAMMER2_SET_COUNT;
232a50f9
MD
2337 break;
2338 case HAMMER2_BREF_TYPE_INDIRECT:
9061bde5 2339 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1897c66e 2340 if (parent->flags & HAMMER2_CHAIN_INITIAL)
01eabad4 2341 base = NULL;
1897c66e 2342 else
93f3933a 2343 base = &parent->data->npdata[0];
6ba3b984 2344 count = parent->bytes / sizeof(hammer2_blockref_t);
232a50f9
MD
2345 break;
2346 case HAMMER2_BREF_TYPE_VOLUME:
995e78dc 2347 KKASSERT(parent->data != NULL);
5c23d7f1
MD
2348 base = &hmp->voldata.sroot_blockset.blockref[0];
2349 count = HAMMER2_SET_COUNT;
232a50f9 2350 break;
1a7cfe5a
MD
2351 case HAMMER2_BREF_TYPE_FREEMAP:
2352 KKASSERT(parent->data != NULL);
2353 base = &hmp->voldata.freemap_blockset.blockref[0];
2354 count = HAMMER2_SET_COUNT;
2355 break;
232a50f9 2356 default:
3ac6a319 2357 panic("hammer2_chain_create: unrecognized blockref type: %d",
232a50f9 2358 parent->bref.type);
1897c66e 2359 base = NULL;
5c23d7f1
MD
2360 count = 0;
2361 break;
232a50f9
MD
2362 }
2363
b7926f31 2364 /*
1897c66e 2365 * Make sure we've counted the brefs
b7926f31 2366 */
f1c7c224 2367 if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0)
51a0d27c 2368 hammer2_chain_countbrefs(parent, base, count);
1897c66e 2369
da6f36f4
MD
2370 KKASSERT(parent->core.live_count >= 0 &&
2371 parent->core.live_count <= count);
5c23d7f1
MD
2372
2373 /*
004f88b4 2374 * If no free blockref could be found we must create an indirect
995e78dc 2375 * block and move a number of blockrefs into it. With the parent
1897c66e
MD
2376 * locked we can safely lock each child in order to delete+duplicate
2377 * it without causing a deadlock.
995e78dc
MD
2378 *
2379 * This may return the new indirect block or the old parent depending
0dea3156 2380 * on where the key falls. NULL is returned on error.
5c23d7f1 2381 */
da6f36f4 2382 if (parent->core.live_count == count) {
995e78dc
MD
2383 hammer2_chain_t *nparent;
2384
c603b86b 2385 nparent = hammer2_chain_create_indirect(parent, key, keybits,
1a7cfe5a 2386 type, &error);
995e78dc 2387 if (nparent == NULL) {
6934ae32 2388 if (allocated)
731b2a84 2389 hammer2_chain_drop(chain);
995e78dc
MD
2390 chain = NULL;
2391 goto done;
2392 }
2393 if (parent != nparent) {
9797e933 2394 hammer2_chain_unlock(parent);
e513e77e 2395 hammer2_chain_drop(parent);
9797e933 2396 parent = *parentp = nparent;
995e78dc
MD
2397 }
2398 goto again;
5c23d7f1
MD
2399 }
2400
2401 /*
8138a154 2402 * Link the chain into its parent.
5c23d7f1 2403 */
da6f36f4 2404 if (chain->parent != NULL)
6934ae32 2405 panic("hammer2: hammer2_chain_create: chain already connected");
da6f36f4
MD
2406 KKASSERT(chain->parent == NULL);
2407 hammer2_chain_insert(parent, chain,
925e4ad1 2408 HAMMER2_CHAIN_INSERT_SPIN |
623d43d4
MD
2409 HAMMER2_CHAIN_INSERT_LIVE,
2410 0);
e028fa74 2411
8e12e3c9 2412 if (allocated) {
1a7cfe5a 2413 /*
8138a154 2414 * Mark the newly created chain modified. This will cause
0cc33e20 2415 * UPDATE to be set and process the INITIAL flag.
1a7cfe5a
MD
2416 *
2417 * Device buffers are not instantiated for DATA elements
2418 * as these are handled by logical buffers.
2419 *
2420 * Indirect and freemap node indirect blocks are handled
2421 * by hammer2_chain_create_indirect() and not by this
2422 * function.
2423 *
2424 * Data for all other bref types is expected to be
2425 * instantiated (INODE, LEAF).
2426 */
9061bde5
MD
2427 switch(chain->bref.type) {
2428 case HAMMER2_BREF_TYPE_DATA:
1a7cfe5a
MD
2429 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2430 case HAMMER2_BREF_TYPE_INODE:
c603b86b 2431 hammer2_chain_modify(chain, HAMMER2_MODIFY_OPTDATA);
9061bde5
MD
2432 break;
2433 default:
1a7cfe5a
MD
2434 /*
2435 * Remaining types are not supported by this function.
2436 * In particular, INDIRECT and LEAF_NODE types are
2437 * handled by create_indirect().
2438 */
2439 panic("hammer2_chain_create: bad type: %d",
2440 chain->bref.type);
2441 /* NOT REACHED */
9061bde5 2442 break;
01eabad4 2443 }
8e12e3c9
MD
2444 } else {
2445 /*
da6f36f4
MD
2446 * When reconnecting a chain we must set UPDATE and
2447 * setflush so the flush recognizes that it must update
8138a154 2448 * the bref in the parent.
8e12e3c9 2449 */
da6f36f4 2450 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) {
0dea3156 2451 hammer2_chain_ref(chain);
da6f36f4 2452 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
8e12e3c9 2453 }
6934ae32 2454 }
da6f36f4
MD
2455
2456 /*
2457 * We must setflush(parent) to ensure that it recurses through to
2458 * chain. setflush(chain) might not work because ONFLUSH is possibly
2459 * already set in the chain (so it won't recurse up to set it in the
2460 * parent).
2461 */
c603b86b 2462 hammer2_chain_setflush(parent);
37494cab 2463
995e78dc 2464done:
0dea3156 2465 *chainp = chain;
9797e933 2466
0dea3156
MD
2467 return (error);
2468}
2469
2470/*
da6f36f4
MD
2471 * Move the chain from its old parent to a new parent. The chain must have
2472 * already been deleted or already disconnected (or never associated) with
2473 * a parent. The chain is reassociated with the new parent and the deleted
2474 * flag will be cleared (no longer deleted). The chain's modification state
2475 * is not altered.
59c5121a 2476 *
da6f36f4 2477 * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION
59c5121a
MD
2478 * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING
2479 * FULL. This typically means that the caller is creating the chain after
2480 * doing a hammer2_chain_lookup().
a4dc31e0 2481 *
84e47819
MD
2482 * A non-NULL bref is typically passed when key and keybits must be overridden.
2483 * Note that hammer2_cluster_duplicate() *ONLY* uses the key and keybits fields
2484 * from a passed-in bref and uses the old chain's bref for everything else.
2485 *
b93cc2e0
MD
2486 * Neither (parent) or (chain) can be errored.
2487 *
1897c66e
MD
2488 * If (parent) is non-NULL then the new duplicated chain is inserted under
2489 * the parent.
0dea3156 2490 *
0924b3f8
MD
2491 * If (parent) is NULL then the newly duplicated chain is not inserted
2492 * anywhere, similar to if it had just been chain_alloc()'d (suitable for
2493 * passing into hammer2_chain_create() after this function returns).
2494 *
0924b3f8 2495 * WARNING! This function calls create which means it can insert indirect
da6f36f4
MD
2496 * blocks. This can cause other unrelated chains in the parent to
2497 * be moved to a newly inserted indirect block in addition to the
2498 * specific chain.
0dea3156
MD
2499 */
2500void
c603b86b 2501hammer2_chain_rename(hammer2_blockref_t *bref,
b3659de2
MD
2502 hammer2_chain_t **parentp, hammer2_chain_t *chain,
2503 int flags)
0dea3156 2504{
506bd6d1 2505 hammer2_dev_t *hmp;
41c34a6d 2506 hammer2_chain_t *parent;
0dea3156 2507 size_t bytes;
0dea3156
MD
2508
2509 /*
a4dc31e0
MD
2510 * WARNING! We should never resolve DATA to device buffers
2511 * (XXX allow it if the caller did?), and since
2512 * we currently do not have the logical buffer cache
2513 * buffer in-hand to fix its cached physical offset
2514 * we also force the modify code to not COW it. XXX
0dea3156 2515 */
da6f36f4
MD
2516 hmp = chain->hmp;
2517 KKASSERT(chain->parent == NULL);
b93cc2e0 2518 KKASSERT(chain->error == 0);
a4dc31e0 2519
a4dc31e0
MD
2520 /*
2521 * Now create a duplicate of the chain structure, associating
2522 * it with the same core, making it the same size, pointing it
2523 * to the same bref (the same media block).
a4dc31e0 2524 */
ea155208 2525 if (bref == NULL)
da6f36f4 2526 bref = &chain->bref;
0dea3156 2527 bytes = (hammer2_off_t)1 <<
ea155208 2528 (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
0dea3156
MD
2529
2530 /*
a4dc31e0 2531 * If parent is not NULL the duplicated chain will be entered under
da6f36f4 2532 * the parent and the UPDATE bit set to tell flush to update
8138a154 2533 * the blockref.
1a7cfe5a 2534 *
da6f36f4
MD
2535 * We must setflush(parent) to ensure that it recurses through to
2536 * chain. setflush(chain) might not work because ONFLUSH is possibly
2537 * already set in the chain (so it won't recurse up to set it in the
2538 * parent).
2539 *
1a7cfe5a 2540 * Having both chains locked is extremely important for atomicy.
0dea3156 2541 */
41c34a6d 2542 if (parentp && (parent = *parentp) != NULL) {
f1c7c224 2543 KKASSERT(hammer2_mtx_owned(&parent->lock));
0dea3156 2544 KKASSERT(parent->refs > 0);
b93cc2e0 2545 KKASSERT(parent->error == 0);
41c34a6d 2546
c603b86b 2547 hammer2_chain_create(parentp, &chain, chain->pmp,
da6f36f4 2548 bref->key, bref->keybits, bref->type,
b3659de2 2549 chain->bytes, flags);
da6f36f4 2550 KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE);
c603b86b 2551 hammer2_chain_setflush(*parentp);
0dea3156 2552 }
8138a154
MD
2553}
2554
2555/*
2556 * Helper function for deleting chains.
2557 *
da6f36f4
MD
2558 * The chain is removed from the live view (the RBTREE) as well as the parent's
2559 * blockmap. Both chain and its parent must be locked.
b93cc2e0
MD
2560 *
2561 * parent may not be errored. chain can be errored.
8138a154
MD
2562 */
2563static void
c603b86b 2564_hammer2_chain_delete_helper(hammer2_chain_t *parent, hammer2_chain_t *chain,
b3659de2 2565 int flags)
8138a154 2566{
506bd6d1 2567 hammer2_dev_t *hmp;
8138a154 2568
b93cc2e0
MD
2569 KKASSERT((chain->flags & (HAMMER2_CHAIN_DELETED |
2570 HAMMER2_CHAIN_FICTITIOUS)) == 0);
8138a154 2571 hmp = chain->hmp;
a4dc31e0 2572
8138a154
MD
2573 if (chain->flags & HAMMER2_CHAIN_BMAPPED) {
2574 /*
da6f36f4
MD
2575 * Chain is blockmapped, so there must be a parent.
2576 * Atomically remove the chain from the parent and remove
2577 * the blockmap entry.
8138a154 2578 */
da6f36f4
MD
2579 hammer2_blockref_t *base;
2580 int count;
2581
2582 KKASSERT(parent != NULL);
b93cc2e0 2583 KKASSERT(parent->error == 0);
da6f36f4 2584 KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0);
c603b86b 2585 hammer2_chain_modify(parent, HAMMER2_MODIFY_OPTDATA);
8138a154 2586
8138a154 2587 /*
da6f36f4 2588 * Calculate blockmap pointer
8138a154 2589 */
da6f36f4 2590 KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE);
94491fa0 2591 hammer2_spin_ex(&parent->core.spin);
355d67fc 2592
da6f36f4
MD
2593 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2594 atomic_add_int(&parent->core.live_count, -1);
2595 ++parent->core.generation;
2596 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2597 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2598 --parent->core.chain_count;
2599 chain->parent = NULL;
925e4ad1 2600
da6f36f4
MD
2601 switch(parent->bref.type) {
2602 case HAMMER2_BREF_TYPE_INODE:
2603 /*
2604 * Access the inode's block array. However, there
2605 * is no block array if the inode is flagged
2606 * DIRECTDATA. The DIRECTDATA case typicaly only
2607 * occurs when a hardlink has been shifted up the
2608 * tree and the original inode gets replaced with
2609 * an OBJTYPE_HARDLINK placeholding inode.
2610 */
2611 if (parent->data &&
b0f58de8 2612 (parent->data->ipdata.meta.op_flags &
da6f36f4
MD
2613 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
2614 base =
2615 &parent->data->ipdata.u.blockset.blockref[0];
2616 } else {
2617 base = NULL;
2618 }
2619 count = HAMMER2_SET_COUNT;
2620 break;
2621 case HAMMER2_BREF_TYPE_INDIRECT:
2622 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2623 if (parent->data)
2624 base = &parent->data->npdata[0];
2625 else
2626 base = NULL;
2627 count = parent->bytes / sizeof(hammer2_blockref_t);
2628 break;
2629 case HAMMER2_BREF_TYPE_VOLUME:
2630 base = &hmp->voldata.sroot_blockset.blockref[0];
2631 count = HAMMER2_SET_COUNT;
2632 break;
2633 case HAMMER2_BREF_TYPE_FREEMAP:
2634 base = &parent->data->npdata[0];
2635 count = HAMMER2_SET_COUNT;
2636 break;
2637 default:
2638 base = NULL;
2639 count = 0;
2640 panic("hammer2_flush_pass2: "
2641 "unrecognized blockref type: %d",
2642 parent->bref.type);
2643 }
b3659de2
MD
2644
2645 /*
2646 * delete blockmapped chain from its parent.
2647 *
2648 * The parent is not affected by any statistics in chain
2649 * which are pending synchronization. That is, there is
2650 * nothing to undo in the parent since they have not yet
2651 * been incorporated into the parent.
2652 *
2653 * The parent is affected by statistics stored in inodes.
2654 * Those have already been synchronized, so they must be
2655 * undone. XXX split update possible w/delete in middle?
2656 */
da6f36f4
MD
2657 if (base) {
2658 int cache_index = -1;
c603b86b 2659 hammer2_base_delete(parent, base, count,
da6f36f4 2660 &cache_index, chain);
925e4ad1 2661 }
94491fa0 2662 hammer2_spin_unex(&parent->core.spin);
da6f36f4 2663 } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
72ebfa75 2664 /*
da6f36f4
MD
2665 * Chain is not blockmapped but a parent is present.
2666 * Atomically remove the chain from the parent. There is
2667 * no blockmap entry to remove.
b3659de2
MD
2668 *
2669 * Because chain was associated with a parent but not
2670 * synchronized, the chain's *_count_up fields contain
2671 * inode adjustment statistics which must be undone.
72ebfa75 2672 */
94491fa0 2673 hammer2_spin_ex(&parent->core.spin);
da6f36f4
MD
2674 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
2675 atomic_add_int(&parent->core.live_count, -1);
2676 ++parent->core.generation;
2677 RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain);
2678 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
2679 --parent->core.chain_count;
2680 chain->parent = NULL;
94491fa0 2681 hammer2_spin_unex(&parent->core.spin);
355d67fc 2682 } else {
623d43d4 2683 /*
da6f36f4
MD
2684 * Chain is not blockmapped and has no parent. This
2685 * is a degenerate case.
623d43d4 2686 */
da6f36f4 2687 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED);
355d67fc 2688 }
1a7cfe5a
MD
2689}
2690
995e78dc
MD
2691/*
2692 * Create an indirect block that covers one or more of the elements in the
2693 * current parent. Either returns the existing parent with no locking or
004f88b4
MD
2694 * ref changes or returns the new indirect block locked and referenced
2695 * and leaving the original parent lock/ref intact as well.
2696 *
5f6853df 2697 * If an error occurs, NULL is returned and *errorp is set to the error.
5f6853df 2698 *
004f88b4 2699 * The returned chain depends on where the specified key falls.
995e78dc
MD
2700 *
2701 * The key/keybits for the indirect mode only needs to follow three rules:
2702 *
2703 * (1) That all elements underneath it fit within its key space and
2704 *
2705 * (2) That all elements outside it are outside its key space.
2706 *
2707 * (3) When creating the new indirect block any elements in the current
2708 * parent that fit within the new indirect block's keyspace must be
2709 * moved into the new indirect block.
2710 *
2711 * (4) The keyspace chosen for the inserted indirect block CAN cover a wider
2712 * keyspace the the current parent, but lookup/iteration rules will
2713 * ensure (and must ensure) that rule (2) for all parents leading up
2714 * to the nearest inode or the root volume header is adhered to. This
2715 * is accomplished by always recursing through matching keyspaces in
2716 * the hammer2_chain_lookup() and hammer2_chain_next() API.
2717 *
2718 * The current implementation calculates the current worst-case keyspace by
2719 * iterating the current parent and then divides it into two halves, choosing
2720 * whichever half has the most elements (not necessarily the half containing
2721 * the requested key).
2722 *
2723 * We can also opt to use the half with the least number of elements. This
2724 * causes lower-numbered keys (aka logical file offsets) to recurse through
2725 * fewer indirect blocks and higher-numbered keys to recurse through more.
2726 * This also has the risk of not moving enough elements to the new indirect
2727 * block and being forced to create several indirect blocks before the element
2728 * can be inserted.
a0ed3c24 2729 *
5f6853df 2730 * Must be called with an exclusively locked parent.
995e78dc 2731 */
1a7cfe5a
MD
2732static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent,
2733 hammer2_key_t *keyp, int keybits,
2734 hammer2_blockref_t *base, int count);
2735static int hammer2_chain_indkey_normal(hammer2_chain_t *parent,
2736 hammer2_key_t *keyp, int keybits,
2737 hammer2_blockref_t *base, int count);
995e78dc
MD
2738static
2739hammer2_chain_t *
c603b86b 2740hammer2_chain_create_indirect(hammer2_chain_t *parent,
5f6853df 2741 hammer2_key_t create_key, int create_bits,
1a7cfe5a 2742 int for_type, int *errorp)
995e78dc 2743{
506bd6d1 2744 hammer2_dev_t *hmp;
995e78dc
MD
2745 hammer2_blockref_t *base;
2746 hammer2_blockref_t *bref;
51a0d27c 2747 hammer2_blockref_t bcopy;
995e78dc
MD
2748 hammer2_chain_t *chain;
2749 hammer2_chain_t *ichain;
2750 hammer2_chain_t dummy;
2751 hammer2_key_t key = create_key;
1897c66e
MD
2752 hammer2_key_t key_beg;
2753 hammer2_key_t key_end;
2754 hammer2_key_t key_next;
995e78dc 2755 int keybits = create_bits;
995e78dc 2756 int count;
6ba3b984 2757 int nbytes;
1897c66e 2758 int cache_index;
51a0d27c 2759 int loops;
10136ab6 2760 int reason;
623d43d4
MD
2761 int generation;
2762 int maxloops = 300000;
995e78dc 2763
995e78dc 2764 /*
01eabad4 2765 * Calculate the base blockref pointer or NULL if the chain
004f88b4 2766 * is known to be empty. We need to calculate the array count
ecc33e71 2767 * for RB lookups either way.
995e78dc 2768 */
a5913bdf 2769 hmp = parent->hmp;
5f6853df 2770 *errorp = 0;
f1c7c224 2771 KKASSERT(hammer2_mtx_owned(&parent->lock));
a0ed3c24 2772
c603b86b 2773 /*hammer2_chain_modify(&parent, HAMMER2_MODIFY_OPTDATA);*/
01eabad4
MD
2774 if (parent->flags & HAMMER2_CHAIN_INITIAL) {
2775 base = NULL;
995e78dc 2776
01eabad4
MD
2777 switch(parent->bref.type) {
2778 case HAMMER2_BREF_TYPE_INODE:
2779 count = HAMMER2_SET_COUNT;
2780 break;
2781 case HAMMER2_BREF_TYPE_INDIRECT:
9061bde5 2782 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
01eabad4
MD
2783 count = parent->bytes / sizeof(hammer2_blockref_t);
2784 break;
2785 case HAMMER2_BREF_TYPE_VOLUME:
2786 count = HAMMER2_SET_COUNT;
2787 break;
1a7cfe5a
MD
2788 case HAMMER2_BREF_TYPE_FREEMAP:
2789 count = HAMMER2_SET_COUNT;
2790 break;
01eabad4
MD
2791 default:
2792 panic("hammer2_chain_create_indirect: "
2793 "unrecognized blockref type: %d",
2794 parent->bref.type);
2795 count = 0;
2796 break;
2797 }
2798 } else {
01eabad4
MD
2799 switch(parent->bref.type) {
2800 case HAMMER2_BREF_TYPE_INODE:
2801 base = &parent->data->ipdata.u.blockset.blockref[0];
2802 count = HAMMER2_SET_COUNT;
2803 break;
2804 case HAMMER2_BREF_TYPE_INDIRECT:
9061bde5 2805 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
93f3933a 2806 base = &parent->data->npdata[0];
01eabad4
MD
2807 count = parent->bytes / sizeof(hammer2_blockref_t);
2808 break;
2809 case HAMMER2_BREF_TYPE_VOLUME:
2810 base = &hmp->voldata.sroot_blockset.blockref[0];
2811 count = HAMMER2_SET_COUNT;
2812 break;
1a7cfe5a
MD
2813 case HAMMER2_BREF_TYPE_FREEMAP:
2814 base = &hmp->voldata.freemap_blockset.blockref[0];
2815 count = HAMMER2_SET_COUNT;
2816 break;
01eabad4
MD
2817 default:
2818 panic("hammer2_chain_create_indirect: "
2819 "unrecognized blockref type: %d",
2820 parent->bref.type);
2821 count = 0;
2822 break;
2823 }
995e78dc
MD
2824 }
2825
2826 /*
1a7cfe5a 2827 * dummy used in later chain allocation (no longer used for lookups).
995e78dc
MD
2828 */
2829 bzero(&dummy, sizeof(dummy));
0dea3156 2830
995e78dc 2831 /*
1a7cfe5a
MD
2832 * When creating an indirect block for a freemap node or leaf
2833 * the key/keybits must be fitted to static radix levels because
2834 * particular radix levels use particular reserved blocks in the
2835 * related zone.
2836 *
2837 * This routine calculates the key/radix of the indirect block
2838 * we need to create, and whether it is on the high-side or the
2839 * low-side.
2840 */
2841 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2842 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2843 keybits = hammer2_chain_indkey_freemap(parent, &key, keybits,
2844 base, count);
2845 } else {
2846 keybits = hammer2_chain_indkey_normal(parent, &key, keybits,
2847 base, count);
2848 }
37aa19df
MD
2849
2850 /*
1a7cfe5a
MD
2851 * Normalize the key for the radix being represented, keeping the
2852 * high bits and throwing away the low bits.
995e78dc
MD
2853 */
2854 key &= ~(((hammer2_key_t)1 << keybits) - 1);
995e78dc 2855
6ba3b984
MD
2856 /*
2857 * How big should our new indirect block be? It has to be at least
2858 * as large as its parent.
0057d3b8
MD
2859 *
2860 * The freemap uses a specific indirect block size.
2861 *
2862 * The first indirect block level down from an inode typically
2863 * uses LBUFSIZE (16384), else it uses PBUFSIZE (65536).
6ba3b984 2864 */
0057d3b8
MD
2865 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2866 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
2867 nbytes = HAMMER2_FREEMAP_LEVELN_PSIZE;
2868 } else if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
6ba3b984 2869 nbytes = HAMMER2_IND_BYTES_MIN;
0057d3b8 2870 } else {
6ba3b984 2871 nbytes = HAMMER2_IND_BYTES_MAX;
0057d3b8
MD
2872 }
2873 if (nbytes < count * sizeof(hammer2_blockref_t)) {
2874 KKASSERT(for_type != HAMMER2_BREF_TYPE_FREEMAP_NODE &&
2875 for_type != HAMMER2_BREF_TYPE_FREEMAP_LEAF);
6ba3b984 2876 nbytes = count * sizeof(hammer2_blockref_t);
0057d3b8 2877 }
6ba3b984 2878
995e78dc
MD
2879 /*
2880 * Ok, create our new indirect block
2881 */
1a7cfe5a
MD
2882 if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
2883 for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
9061bde5 2884 dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE;
1a7cfe5a 2885 } else {
9061bde5 2886 dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT;
9061bde5 2887 }
995e78dc 2888 dummy.bref.key = key;
37aa19df 2889 dummy.bref.keybits = keybits;
1a7cfe5a 2890 dummy.bref.data_off = hammer2_getradix(nbytes);
9061bde5 2891 dummy.bref.methods = parent->bref.methods;
0dea3156 2892
c603b86b 2893 ichain = hammer2_chain_alloc(hmp, parent->pmp, &dummy.bref);
01eabad4 2894 atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL);
0dea3156 2895 hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE);
e513e77e 2896 /* ichain has one ref at this point */
995e78dc 2897
ea155208
MD
2898 /*
2899 * We have to mark it modified to allocate its block, but use
2900 * OPTDATA to allow it to remain in the INITIAL state. Otherwise
2901 * it won't be acted upon by the flush code.
2902 */
c603b86b 2903 hammer2_chain_modify(ichain, HAMMER2_MODIFY_OPTDATA);
ea155208 2904
995e78dc
MD
2905 /*
2906 * Iterate the original parent and move the matching brefs into
37aa19df 2907 * the new indirect block.
0dea3156
MD
2908 *
2909 * XXX handle flushes.
995e78dc 2910 */
1897c66e 2911 key_beg = 0;
50456506 2912 key_end = HAMMER2_KEY_MAX;
1897c66e 2913 cache_index = 0;
94491fa0 2914 hammer2_spin_ex(&parent->core.spin);
51a0d27c 2915 loops = 0;
10136ab6 2916 reason = 0;
1897c66e
MD
2917
2918 for (;;) {
10136ab6 2919 if (++loops > 100000) {
94491fa0 2920 hammer2_spin_unex(&parent->core.spin);
10136ab6
MD
2921 panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n",
2922 reason, parent, base, count, key_next);
51a0d27c
MD
2923 }
2924
37aa19df 2925 /*
51a0d27c 2926 * NOTE: spinlock stays intact, returned chain (if not NULL)
521b4014
MD
2927 * is not referenced or locked which means that we
2928 * cannot safely check its flagged / deletion status
2929 * until we lock it.
37aa19df 2930 */
51a0d27c
MD
2931 chain = hammer2_combined_find(parent, base, count,
2932 &cache_index, &key_next,
2933 key_beg, key_end,
2934 &bref);
da6f36f4 2935 generation = parent->core.generation;
51a0d27c
MD
2936 if (bref == NULL)
2937 break;
51a0d27c 2938 key_next = bref->key + ((hammer2_key_t)1 << bref->keybits);
995e78dc
MD
2939
2940 /*
1a7cfe5a
MD
2941 * Skip keys that are not within the key/radix of the new
2942 * indirect block. They stay in the parent.
995e78dc 2943 */
37aa19df 2944 if ((~(((hammer2_key_t)1 << keybits) - 1) &
995e78dc 2945 (key ^ bref->key)) != 0) {
521b4014 2946 goto next_key_spinlocked;
995e78dc
MD
2947 }
2948
995e78dc 2949 /*
521b4014
MD
2950 * Load the new indirect block by acquiring the related
2951 * chains (potentially from media as it might not be
2952 * in-memory). Then move it to the new parent (ichain)
1897c66e 2953 * via DELETE-DUPLICATE.
623d43d4
MD
2954 *
2955 * chain is referenced but not locked. We must lock the
2956 * chain to obtain definitive DUPLICATED/DELETED state
995e78dc 2957 */
1897c66e
MD
2958 if (chain) {
2959 /*
2960 * Use chain already present in the RB