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