| 1 | /* |
| 2 | * Copyright (c) 2011-2014 The DragonFly Project. All rights reserved. |
| 3 | * |
| 4 | * This code is derived from software contributed to The DragonFly Project |
| 5 | * by Matthew Dillon <dillon@dragonflybsd.org> |
| 6 | * and Venkatesh Srinivas <vsrinivas@dragonflybsd.org> |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * 3. Neither the name of The DragonFly Project nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific, prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 26 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 30 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 32 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 | * SUCH DAMAGE. |
| 34 | */ |
| 35 | /* |
| 36 | * This subsystem implements most of the core support functions for |
| 37 | * the hammer2_chain structure. |
| 38 | * |
| 39 | * Chains are the in-memory version on media objects (volume header, inodes, |
| 40 | * indirect blocks, data blocks, etc). Chains represent a portion of the |
| 41 | * HAMMER2 topology. |
| 42 | * |
| 43 | * Chains are no-longer delete-duplicated. Instead, the original in-memory |
| 44 | * chain will be moved along with its block reference (e.g. for things like |
| 45 | * renames, hardlink operations, modifications, etc), and will be indexed |
| 46 | * on a secondary list for flush handling instead of propagating a flag |
| 47 | * upward to the root. |
| 48 | * |
| 49 | * Concurrent front-end operations can still run against backend flushes |
| 50 | * as long as they do not cross the current flush boundary. An operation |
| 51 | * running above the current flush (in areas not yet flushed) can become |
| 52 | * part of the current flush while ano peration running below the current |
| 53 | * flush can become part of the next flush. |
| 54 | */ |
| 55 | #include <sys/cdefs.h> |
| 56 | #include <sys/param.h> |
| 57 | #include <sys/systm.h> |
| 58 | #include <sys/types.h> |
| 59 | #include <sys/lock.h> |
| 60 | #include <sys/kern_syscall.h> |
| 61 | #include <sys/uuid.h> |
| 62 | |
| 63 | #include <crypto/sha2/sha2.h> |
| 64 | |
| 65 | #include "hammer2.h" |
| 66 | |
| 67 | static int hammer2_indirect_optimize; /* XXX SYSCTL */ |
| 68 | |
| 69 | static hammer2_chain_t *hammer2_chain_create_indirect( |
| 70 | hammer2_chain_t *parent, |
| 71 | hammer2_key_t key, int keybits, int for_type, int *errorp); |
| 72 | static void hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop); |
| 73 | static 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); |
| 79 | |
| 80 | /* |
| 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. |
| 84 | * |
| 85 | * Chains in delete-duplicate sequences can always iterate through core_entry |
| 86 | * to locate the live version of the chain. |
| 87 | */ |
| 88 | RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp); |
| 89 | |
| 90 | int |
| 91 | hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2) |
| 92 | { |
| 93 | hammer2_key_t c1_beg; |
| 94 | hammer2_key_t c1_end; |
| 95 | hammer2_key_t c2_beg; |
| 96 | hammer2_key_t c2_end; |
| 97 | |
| 98 | /* |
| 99 | * Compare chains. Overlaps are not supposed to happen and catch |
| 100 | * any software issues early we count overlaps as a match. |
| 101 | */ |
| 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; |
| 106 | |
| 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) */ |
| 112 | } |
| 113 | |
| 114 | static __inline |
| 115 | int |
| 116 | hammer2_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 | |
| 128 | /* |
| 129 | * Make a chain visible to the flusher. The flusher needs to be able to |
| 130 | * do flushes of 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 volume root. |
| 133 | * |
| 134 | * This routine sets ONFLUSH upward until it hits the volume root. For |
| 135 | * simplicity we ignore PFSROOT boundaries whos rules can be complex. |
| 136 | * Extra ONFLUSH flagging doesn't hurt the filesystem. |
| 137 | */ |
| 138 | void |
| 139 | hammer2_chain_setflush(hammer2_chain_t *chain) |
| 140 | { |
| 141 | hammer2_chain_t *parent; |
| 142 | |
| 143 | if ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) { |
| 144 | hammer2_spin_sh(&chain->core.spin); |
| 145 | while ((chain->flags & HAMMER2_CHAIN_ONFLUSH) == 0) { |
| 146 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH); |
| 147 | if ((parent = chain->parent) == NULL) |
| 148 | break; |
| 149 | hammer2_spin_sh(&parent->core.spin); |
| 150 | hammer2_spin_unsh(&chain->core.spin); |
| 151 | chain = parent; |
| 152 | } |
| 153 | hammer2_spin_unsh(&chain->core.spin); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Allocate a new disconnected chain element representing the specified |
| 159 | * bref. chain->refs is set to 1 and the passed bref is copied to |
| 160 | * chain->bref. chain->bytes is derived from the bref. |
| 161 | * |
| 162 | * chain->pmp inherits pmp unless the chain is an inode (other than the |
| 163 | * super-root inode). |
| 164 | * |
| 165 | * NOTE: Returns a referenced but unlocked (because there is no core) chain. |
| 166 | */ |
| 167 | hammer2_chain_t * |
| 168 | hammer2_chain_alloc(hammer2_dev_t *hmp, hammer2_pfs_t *pmp, |
| 169 | hammer2_blockref_t *bref) |
| 170 | { |
| 171 | hammer2_chain_t *chain; |
| 172 | u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX); |
| 173 | |
| 174 | /* |
| 175 | * Construct the appropriate system structure. |
| 176 | */ |
| 177 | switch(bref->type) { |
| 178 | case HAMMER2_BREF_TYPE_INODE: |
| 179 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 180 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 181 | case HAMMER2_BREF_TYPE_DATA: |
| 182 | case HAMMER2_BREF_TYPE_FREEMAP_LEAF: |
| 183 | /* |
| 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. |
| 187 | */ |
| 188 | chain = kmalloc(sizeof(*chain), hmp->mchain, M_WAITOK | M_ZERO); |
| 189 | break; |
| 190 | case HAMMER2_BREF_TYPE_VOLUME: |
| 191 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 192 | chain = NULL; |
| 193 | panic("hammer2_chain_alloc volume type illegal for op"); |
| 194 | default: |
| 195 | chain = NULL; |
| 196 | panic("hammer2_chain_alloc: unrecognized blockref type: %d", |
| 197 | bref->type); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Initialize the new chain structure. pmp must be set to NULL for |
| 202 | * chains belonging to the super-root topology of a device mount. |
| 203 | */ |
| 204 | if (pmp == hmp->spmp) |
| 205 | chain->pmp = NULL; |
| 206 | else |
| 207 | chain->pmp = pmp; |
| 208 | chain->hmp = hmp; |
| 209 | chain->bref = *bref; |
| 210 | chain->bytes = bytes; |
| 211 | chain->refs = 1; |
| 212 | chain->flags = HAMMER2_CHAIN_ALLOCATED; |
| 213 | |
| 214 | /* |
| 215 | * Set the PFS boundary flag if this chain represents a PFS root. |
| 216 | */ |
| 217 | if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT) |
| 218 | chain->flags |= HAMMER2_CHAIN_PFSBOUNDARY; |
| 219 | hammer2_chain_core_init(chain); |
| 220 | |
| 221 | return (chain); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Initialize a chain's core structure. This structure used to be allocated |
| 226 | * but is now embedded. |
| 227 | * |
| 228 | * The core is not locked. No additional refs on the chain are made. |
| 229 | * (trans) must not be NULL if (core) is not NULL. |
| 230 | */ |
| 231 | void |
| 232 | hammer2_chain_core_init(hammer2_chain_t *chain) |
| 233 | { |
| 234 | /* |
| 235 | * Fresh core under nchain (no multi-homing of ochain's |
| 236 | * sub-tree). |
| 237 | */ |
| 238 | RB_INIT(&chain->core.rbtree); /* live chains */ |
| 239 | hammer2_mtx_init(&chain->lock, "h2chain"); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Add a reference to a chain element, preventing its destruction. |
| 244 | * |
| 245 | * (can be called with spinlock held) |
| 246 | */ |
| 247 | void |
| 248 | hammer2_chain_ref(hammer2_chain_t *chain) |
| 249 | { |
| 250 | atomic_add_int(&chain->refs, 1); |
| 251 | #if 0 |
| 252 | kprintf("REFC %p %d %08x\n", chain, chain->refs - 1, chain->flags); |
| 253 | print_backtrace(8); |
| 254 | #endif |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Insert the chain in the core rbtree. |
| 259 | * |
| 260 | * Normal insertions are placed in the live rbtree. Insertion of a deleted |
| 261 | * chain is a special case used by the flush code that is placed on the |
| 262 | * unstaged deleted list to avoid confusing the live view. |
| 263 | */ |
| 264 | #define HAMMER2_CHAIN_INSERT_SPIN 0x0001 |
| 265 | #define HAMMER2_CHAIN_INSERT_LIVE 0x0002 |
| 266 | #define HAMMER2_CHAIN_INSERT_RACE 0x0004 |
| 267 | |
| 268 | static |
| 269 | int |
| 270 | hammer2_chain_insert(hammer2_chain_t *parent, hammer2_chain_t *chain, |
| 271 | int flags, int generation) |
| 272 | { |
| 273 | hammer2_chain_t *xchain; |
| 274 | int error = 0; |
| 275 | |
| 276 | if (flags & HAMMER2_CHAIN_INSERT_SPIN) |
| 277 | hammer2_spin_ex(&parent->core.spin); |
| 278 | |
| 279 | /* |
| 280 | * Interlocked by spinlock, check for race |
| 281 | */ |
| 282 | if ((flags & HAMMER2_CHAIN_INSERT_RACE) && |
| 283 | parent->core.generation != generation) { |
| 284 | error = EAGAIN; |
| 285 | goto failed; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Insert chain |
| 290 | */ |
| 291 | xchain = RB_INSERT(hammer2_chain_tree, &parent->core.rbtree, chain); |
| 292 | KASSERT(xchain == NULL, |
| 293 | ("hammer2_chain_insert: collision %p %p", chain, xchain)); |
| 294 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE); |
| 295 | chain->parent = parent; |
| 296 | ++parent->core.chain_count; |
| 297 | ++parent->core.generation; /* XXX incs for _get() too, XXX */ |
| 298 | |
| 299 | /* |
| 300 | * We have to keep track of the effective live-view blockref count |
| 301 | * so the create code knows when to push an indirect block. |
| 302 | */ |
| 303 | if (flags & HAMMER2_CHAIN_INSERT_LIVE) |
| 304 | atomic_add_int(&parent->core.live_count, 1); |
| 305 | failed: |
| 306 | if (flags & HAMMER2_CHAIN_INSERT_SPIN) |
| 307 | hammer2_spin_unex(&parent->core.spin); |
| 308 | return error; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Drop the caller's reference to the chain. When the ref count drops to |
| 313 | * zero this function will try to disassociate the chain from its parent and |
| 314 | * deallocate it, then recursely drop the parent using the implied ref |
| 315 | * from the chain's chain->parent. |
| 316 | */ |
| 317 | static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_chain_t *chain); |
| 318 | |
| 319 | void |
| 320 | hammer2_chain_drop(hammer2_chain_t *chain) |
| 321 | { |
| 322 | u_int refs; |
| 323 | u_int need = 0; |
| 324 | |
| 325 | if (hammer2_debug & 0x200000) |
| 326 | Debugger("drop"); |
| 327 | #if 0 |
| 328 | kprintf("DROP %p %d %08x\n", chain, chain->refs - 1, chain->flags); |
| 329 | print_backtrace(8); |
| 330 | #endif |
| 331 | |
| 332 | if (chain->flags & HAMMER2_CHAIN_UPDATE) |
| 333 | ++need; |
| 334 | if (chain->flags & HAMMER2_CHAIN_MODIFIED) |
| 335 | ++need; |
| 336 | KKASSERT(chain->refs > need); |
| 337 | |
| 338 | while (chain) { |
| 339 | refs = chain->refs; |
| 340 | cpu_ccfence(); |
| 341 | KKASSERT(refs > 0); |
| 342 | |
| 343 | if (refs == 1) { |
| 344 | chain = hammer2_chain_lastdrop(chain); |
| 345 | } else { |
| 346 | if (atomic_cmpset_int(&chain->refs, refs, refs - 1)) |
| 347 | break; |
| 348 | /* retry the same chain */ |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Safe handling of the 1->0 transition on chain. Returns a chain for |
| 355 | * recursive drop or NULL, possibly returning the same chain if the atomic |
| 356 | * op fails. |
| 357 | * |
| 358 | * Whem two chains need to be recursively dropped we use the chain |
| 359 | * we would otherwise free to placehold the additional chain. It's a bit |
| 360 | * convoluted but we can't just recurse without potentially blowing out |
| 361 | * the kernel stack. |
| 362 | * |
| 363 | * The chain cannot be freed if it has any children. |
| 364 | * |
| 365 | * The core spinlock is allowed nest child-to-parent (not parent-to-child). |
| 366 | */ |
| 367 | static |
| 368 | hammer2_chain_t * |
| 369 | hammer2_chain_lastdrop(hammer2_chain_t *chain) |
| 370 | { |
| 371 | hammer2_pfs_t *pmp; |
| 372 | hammer2_dev_t *hmp; |
| 373 | hammer2_chain_t *parent; |
| 374 | hammer2_chain_t *rdrop; |
| 375 | |
| 376 | /* |
| 377 | * Spinlock the core and check to see if it is empty. If it is |
| 378 | * not empty we leave chain intact with refs == 0. The elements |
| 379 | * in core->rbtree are associated with other chains contemporary |
| 380 | * with ours but not with our chain directly. |
| 381 | */ |
| 382 | hammer2_spin_ex(&chain->core.spin); |
| 383 | |
| 384 | /* |
| 385 | * We can't free non-stale chains with children until we are |
| 386 | * able to free the children because there might be a flush |
| 387 | * dependency. Flushes of stale children (which should also |
| 388 | * have their deleted flag set) short-cut recursive flush |
| 389 | * dependencies and can be freed here. Any flushes which run |
| 390 | * through stale children due to the flush synchronization |
| 391 | * point should have a FLUSH_* bit set in the chain and not |
| 392 | * reach lastdrop at this time. |
| 393 | * |
| 394 | * NOTE: We return (chain) on failure to retry. |
| 395 | */ |
| 396 | if (chain->core.chain_count) { |
| 397 | if (atomic_cmpset_int(&chain->refs, 1, 0)) { |
| 398 | hammer2_spin_unex(&chain->core.spin); |
| 399 | chain = NULL; /* success */ |
| 400 | } else { |
| 401 | hammer2_spin_unex(&chain->core.spin); |
| 402 | } |
| 403 | return(chain); |
| 404 | } |
| 405 | /* no chains left under us */ |
| 406 | |
| 407 | /* |
| 408 | * chain->core has no children left so no accessors can get to our |
| 409 | * chain from there. Now we have to lock the parent core to interlock |
| 410 | * remaining possible accessors that might bump chain's refs before |
| 411 | * we can safely drop chain's refs with intent to free the chain. |
| 412 | */ |
| 413 | hmp = chain->hmp; |
| 414 | pmp = chain->pmp; /* can be NULL */ |
| 415 | rdrop = NULL; |
| 416 | |
| 417 | /* |
| 418 | * Spinlock the parent and try to drop the last ref on chain. |
| 419 | * On success remove chain from its parent, otherwise return NULL. |
| 420 | * |
| 421 | * (normal core locks are top-down recursive but we define core |
| 422 | * spinlocks as bottom-up recursive, so this is safe). |
| 423 | */ |
| 424 | if ((parent = chain->parent) != NULL) { |
| 425 | hammer2_spin_ex(&parent->core.spin); |
| 426 | if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) { |
| 427 | /* 1->0 transition failed */ |
| 428 | hammer2_spin_unex(&parent->core.spin); |
| 429 | hammer2_spin_unex(&chain->core.spin); |
| 430 | return(chain); /* retry */ |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * 1->0 transition successful, remove chain from its |
| 435 | * above core. |
| 436 | */ |
| 437 | if (chain->flags & HAMMER2_CHAIN_ONRBTREE) { |
| 438 | RB_REMOVE(hammer2_chain_tree, |
| 439 | &parent->core.rbtree, chain); |
| 440 | atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE); |
| 441 | --parent->core.chain_count; |
| 442 | chain->parent = NULL; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * If our chain was the last chain in the parent's core the |
| 447 | * core is now empty and its parent might have to be |
| 448 | * re-dropped if it has 0 refs. |
| 449 | */ |
| 450 | if (parent->core.chain_count == 0) { |
| 451 | rdrop = parent; |
| 452 | if (atomic_cmpset_int(&rdrop->refs, 0, 1) == 0) { |
| 453 | rdrop = NULL; |
| 454 | } |
| 455 | } |
| 456 | hammer2_spin_unex(&parent->core.spin); |
| 457 | parent = NULL; /* safety */ |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Successful 1->0 transition and the chain can be destroyed now. |
| 462 | * |
| 463 | * We still have the core spinlock, and core's chain_count is 0. |
| 464 | * Any parent spinlock is gone. |
| 465 | */ |
| 466 | hammer2_spin_unex(&chain->core.spin); |
| 467 | KKASSERT(RB_EMPTY(&chain->core.rbtree) && |
| 468 | chain->core.chain_count == 0); |
| 469 | |
| 470 | /* |
| 471 | * All spin locks are gone, finish freeing stuff. |
| 472 | */ |
| 473 | KKASSERT((chain->flags & (HAMMER2_CHAIN_UPDATE | |
| 474 | HAMMER2_CHAIN_MODIFIED)) == 0); |
| 475 | hammer2_chain_drop_data(chain, 1); |
| 476 | |
| 477 | KKASSERT(chain->dio == NULL); |
| 478 | |
| 479 | /* |
| 480 | * Once chain resources are gone we can use the now dead chain |
| 481 | * structure to placehold what might otherwise require a recursive |
| 482 | * drop, because we have potentially two things to drop and can only |
| 483 | * return one directly. |
| 484 | */ |
| 485 | if (chain->flags & HAMMER2_CHAIN_ALLOCATED) { |
| 486 | chain->flags &= ~HAMMER2_CHAIN_ALLOCATED; |
| 487 | chain->hmp = NULL; |
| 488 | kfree(chain, hmp->mchain); |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Possible chaining loop when parent re-drop needed. |
| 493 | */ |
| 494 | return(rdrop); |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * On either last lock release or last drop |
| 499 | */ |
| 500 | static void |
| 501 | hammer2_chain_drop_data(hammer2_chain_t *chain, int lastdrop) |
| 502 | { |
| 503 | /*hammer2_dev_t *hmp = chain->hmp;*/ |
| 504 | |
| 505 | switch(chain->bref.type) { |
| 506 | case HAMMER2_BREF_TYPE_VOLUME: |
| 507 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 508 | if (lastdrop) |
| 509 | chain->data = NULL; |
| 510 | break; |
| 511 | default: |
| 512 | KKASSERT(chain->data == NULL); |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Lock a referenced chain element, acquiring its data with I/O if necessary, |
| 519 | * and specify how you would like the data to be resolved. |
| 520 | * |
| 521 | * If an I/O or other fatal error occurs, chain->error will be set to non-zero. |
| 522 | * |
| 523 | * The lock is allowed to recurse, multiple locking ops will aggregate |
| 524 | * the requested resolve types. Once data is assigned it will not be |
| 525 | * removed until the last unlock. |
| 526 | * |
| 527 | * HAMMER2_RESOLVE_NEVER - Do not resolve the data element. |
| 528 | * (typically used to avoid device/logical buffer |
| 529 | * aliasing for data) |
| 530 | * |
| 531 | * HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in |
| 532 | * the INITIAL-create state (indirect blocks only). |
| 533 | * |
| 534 | * Do not resolve data elements for DATA chains. |
| 535 | * (typically used to avoid device/logical buffer |
| 536 | * aliasing for data) |
| 537 | * |
| 538 | * HAMMER2_RESOLVE_ALWAYS- Always resolve the data element. |
| 539 | * |
| 540 | * HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise |
| 541 | * it will be locked exclusive. |
| 542 | * |
| 543 | * NOTE: Embedded elements (volume header, inodes) are always resolved |
| 544 | * regardless. |
| 545 | * |
| 546 | * NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded |
| 547 | * element will instantiate and zero its buffer, and flush it on |
| 548 | * release. |
| 549 | * |
| 550 | * NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE |
| 551 | * so as not to instantiate a device buffer, which could alias against |
| 552 | * a logical file buffer. However, if ALWAYS is specified the |
| 553 | * device buffer will be instantiated anyway. |
| 554 | * |
| 555 | * WARNING! This function blocks on I/O if data needs to be fetched. This |
| 556 | * blocking can run concurrent with other compatible lock holders |
| 557 | * who do not need data returning. The lock is not upgraded to |
| 558 | * exclusive during a data fetch, a separate bit is used to |
| 559 | * interlock I/O. However, an exclusive lock holder can still count |
| 560 | * on being interlocked against an I/O fetch managed by a shared |
| 561 | * lock holder. |
| 562 | */ |
| 563 | void |
| 564 | hammer2_chain_lock(hammer2_chain_t *chain, int how) |
| 565 | { |
| 566 | /* |
| 567 | * Ref and lock the element. Recursive locks are allowed. |
| 568 | */ |
| 569 | KKASSERT(chain->refs > 0); |
| 570 | atomic_add_int(&chain->lockcnt, 1); |
| 571 | |
| 572 | /* |
| 573 | * Get the appropriate lock. |
| 574 | */ |
| 575 | if (how & HAMMER2_RESOLVE_SHARED) |
| 576 | hammer2_mtx_sh(&chain->lock); |
| 577 | else |
| 578 | hammer2_mtx_ex(&chain->lock); |
| 579 | |
| 580 | /* |
| 581 | * If we already have a valid data pointer no further action is |
| 582 | * necessary. |
| 583 | */ |
| 584 | if (chain->data) |
| 585 | return; |
| 586 | |
| 587 | /* |
| 588 | * Do we have to resolve the data? |
| 589 | */ |
| 590 | switch(how & HAMMER2_RESOLVE_MASK) { |
| 591 | case HAMMER2_RESOLVE_NEVER: |
| 592 | return; |
| 593 | case HAMMER2_RESOLVE_MAYBE: |
| 594 | if (chain->flags & HAMMER2_CHAIN_INITIAL) |
| 595 | return; |
| 596 | if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) |
| 597 | return; |
| 598 | #if 0 |
| 599 | if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) |
| 600 | return; |
| 601 | if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) |
| 602 | return; |
| 603 | #endif |
| 604 | /* fall through */ |
| 605 | case HAMMER2_RESOLVE_ALWAYS: |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * Caller requires data |
| 611 | */ |
| 612 | hammer2_chain_load_data(chain); |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * Issue I/O and install chain->data. Caller must hold a chain lock, lock |
| 617 | * may be of any type. |
| 618 | * |
| 619 | * Once chain->data is set it cannot be disposed of until all locks are |
| 620 | * released. |
| 621 | */ |
| 622 | void |
| 623 | hammer2_chain_load_data(hammer2_chain_t *chain) |
| 624 | { |
| 625 | hammer2_blockref_t *bref; |
| 626 | hammer2_dev_t *hmp; |
| 627 | char *bdata; |
| 628 | int error; |
| 629 | |
| 630 | /* |
| 631 | * Degenerate case, data already present. |
| 632 | */ |
| 633 | if (chain->data) |
| 634 | return; |
| 635 | |
| 636 | hmp = chain->hmp; |
| 637 | KKASSERT(hmp != NULL); |
| 638 | |
| 639 | /* |
| 640 | * Gain the IOINPROG bit, interlocked block. |
| 641 | */ |
| 642 | for (;;) { |
| 643 | u_int oflags; |
| 644 | u_int nflags; |
| 645 | |
| 646 | oflags = chain->flags; |
| 647 | cpu_ccfence(); |
| 648 | if (oflags & HAMMER2_CHAIN_IOINPROG) { |
| 649 | nflags = oflags | HAMMER2_CHAIN_IOSIGNAL; |
| 650 | tsleep_interlock(&chain->flags, 0); |
| 651 | if (atomic_cmpset_int(&chain->flags, oflags, nflags)) { |
| 652 | tsleep(&chain->flags, PINTERLOCKED, |
| 653 | "h2iocw", 0); |
| 654 | } |
| 655 | /* retry */ |
| 656 | } else { |
| 657 | nflags = oflags | HAMMER2_CHAIN_IOINPROG; |
| 658 | if (atomic_cmpset_int(&chain->flags, oflags, nflags)) { |
| 659 | break; |
| 660 | } |
| 661 | /* retry */ |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * We own CHAIN_IOINPROG |
| 667 | * |
| 668 | * Degenerate case if we raced another load. |
| 669 | */ |
| 670 | if (chain->data) |
| 671 | goto done; |
| 672 | |
| 673 | /* |
| 674 | * We must resolve to a device buffer, either by issuing I/O or |
| 675 | * by creating a zero-fill element. We do not mark the buffer |
| 676 | * dirty when creating a zero-fill element (the hammer2_chain_modify() |
| 677 | * API must still be used to do that). |
| 678 | * |
| 679 | * The device buffer is variable-sized in powers of 2 down |
| 680 | * to HAMMER2_MIN_ALLOC (typically 1K). A 64K physical storage |
| 681 | * chunk always contains buffers of the same size. (XXX) |
| 682 | * |
| 683 | * The minimum physical IO size may be larger than the variable |
| 684 | * block size. |
| 685 | */ |
| 686 | bref = &chain->bref; |
| 687 | |
| 688 | /* |
| 689 | * The getblk() optimization can only be used on newly created |
| 690 | * elements if the physical block size matches the request. |
| 691 | */ |
| 692 | if (chain->flags & HAMMER2_CHAIN_INITIAL) { |
| 693 | error = hammer2_io_new(hmp, bref->data_off, chain->bytes, |
| 694 | &chain->dio); |
| 695 | } else { |
| 696 | error = hammer2_io_bread(hmp, bref->data_off, chain->bytes, |
| 697 | &chain->dio); |
| 698 | hammer2_adjreadcounter(&chain->bref, chain->bytes); |
| 699 | } |
| 700 | if (error) { |
| 701 | chain->error = HAMMER2_ERROR_IO; |
| 702 | kprintf("hammer2_chain_lock: I/O error %016jx: %d\n", |
| 703 | (intmax_t)bref->data_off, error); |
| 704 | hammer2_io_bqrelse(&chain->dio); |
| 705 | goto done; |
| 706 | } |
| 707 | chain->error = 0; |
| 708 | |
| 709 | /* |
| 710 | * NOTE: A locked chain's data cannot be modified without first |
| 711 | * calling hammer2_chain_modify(). |
| 712 | */ |
| 713 | |
| 714 | /* |
| 715 | * Clear INITIAL. In this case we used io_new() and the buffer has |
| 716 | * been zero'd and marked dirty. |
| 717 | */ |
| 718 | bdata = hammer2_io_data(chain->dio, chain->bref.data_off); |
| 719 | if (chain->flags & HAMMER2_CHAIN_INITIAL) { |
| 720 | atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL); |
| 721 | chain->bref.flags |= HAMMER2_BREF_FLAG_ZERO; |
| 722 | } else if (chain->flags & HAMMER2_CHAIN_MODIFIED) { |
| 723 | /* |
| 724 | * check data not currently synchronized due to |
| 725 | * modification. XXX assumes data stays in the buffer |
| 726 | * cache, which might not be true (need biodep on flush |
| 727 | * to calculate crc? or simple crc?). |
| 728 | */ |
| 729 | } else { |
| 730 | if (hammer2_chain_testcheck(chain, bdata) == 0) { |
| 731 | kprintf("chain %016jx.%02x meth=%02x " |
| 732 | "CHECK FAIL %08x (flags=%08x)\n", |
| 733 | chain->bref.data_off, |
| 734 | chain->bref.type, |
| 735 | chain->bref.methods, |
| 736 | hammer2_icrc32(bdata, chain->bytes), |
| 737 | chain->flags); |
| 738 | chain->error = HAMMER2_ERROR_CHECK; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Setup the data pointer, either pointing it to an embedded data |
| 744 | * structure and copying the data from the buffer, or pointing it |
| 745 | * into the buffer. |
| 746 | * |
| 747 | * The buffer is not retained when copying to an embedded data |
| 748 | * structure in order to avoid potential deadlocks or recursions |
| 749 | * on the same physical buffer. |
| 750 | * |
| 751 | * WARNING! Other threads can start using the data the instant we |
| 752 | * set chain->data non-NULL. |
| 753 | */ |
| 754 | switch (bref->type) { |
| 755 | case HAMMER2_BREF_TYPE_VOLUME: |
| 756 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 757 | /* |
| 758 | * Copy data from bp to embedded buffer |
| 759 | */ |
| 760 | panic("hammer2_chain_lock: called on unresolved volume header"); |
| 761 | break; |
| 762 | case HAMMER2_BREF_TYPE_INODE: |
| 763 | case HAMMER2_BREF_TYPE_FREEMAP_LEAF: |
| 764 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 765 | case HAMMER2_BREF_TYPE_DATA: |
| 766 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 767 | default: |
| 768 | /* |
| 769 | * Point data at the device buffer and leave dio intact. |
| 770 | */ |
| 771 | chain->data = (void *)bdata; |
| 772 | break; |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Release HAMMER2_CHAIN_IOINPROG and signal waiters if requested. |
| 777 | */ |
| 778 | done: |
| 779 | for (;;) { |
| 780 | u_int oflags; |
| 781 | u_int nflags; |
| 782 | |
| 783 | oflags = chain->flags; |
| 784 | nflags = oflags & ~(HAMMER2_CHAIN_IOINPROG | |
| 785 | HAMMER2_CHAIN_IOSIGNAL); |
| 786 | KKASSERT(oflags & HAMMER2_CHAIN_IOINPROG); |
| 787 | if (atomic_cmpset_int(&chain->flags, oflags, nflags)) { |
| 788 | if (oflags & HAMMER2_CHAIN_IOSIGNAL) |
| 789 | wakeup(&chain->flags); |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * Unlock and deref a chain element. |
| 797 | * |
| 798 | * On the last lock release any non-embedded data (chain->dio) will be |
| 799 | * retired. |
| 800 | */ |
| 801 | void |
| 802 | hammer2_chain_unlock(hammer2_chain_t *chain) |
| 803 | { |
| 804 | hammer2_mtx_state_t ostate; |
| 805 | long *counterp; |
| 806 | u_int lockcnt; |
| 807 | |
| 808 | /* |
| 809 | * If multiple locks are present (or being attempted) on this |
| 810 | * particular chain we can just unlock, drop refs, and return. |
| 811 | * |
| 812 | * Otherwise fall-through on the 1->0 transition. |
| 813 | */ |
| 814 | for (;;) { |
| 815 | lockcnt = chain->lockcnt; |
| 816 | KKASSERT(lockcnt > 0); |
| 817 | cpu_ccfence(); |
| 818 | if (lockcnt > 1) { |
| 819 | if (atomic_cmpset_int(&chain->lockcnt, |
| 820 | lockcnt, lockcnt - 1)) { |
| 821 | hammer2_mtx_unlock(&chain->lock); |
| 822 | return; |
| 823 | } |
| 824 | } else { |
| 825 | if (atomic_cmpset_int(&chain->lockcnt, 1, 0)) |
| 826 | break; |
| 827 | } |
| 828 | /* retry */ |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * On the 1->0 transition we upgrade the core lock (if necessary) |
| 833 | * to exclusive for terminal processing. If after upgrading we find |
| 834 | * that lockcnt is non-zero, another thread is racing us and will |
| 835 | * handle the unload for us later on, so just cleanup and return |
| 836 | * leaving the data/io intact |
| 837 | * |
| 838 | * Otherwise if lockcnt is still 0 it is possible for it to become |
| 839 | * non-zero and race, but since we hold the core->lock exclusively |
| 840 | * all that will happen is that the chain will be reloaded after we |
| 841 | * unload it. |
| 842 | */ |
| 843 | ostate = hammer2_mtx_upgrade(&chain->lock); |
| 844 | if (chain->lockcnt) { |
| 845 | hammer2_mtx_unlock(&chain->lock); |
| 846 | return; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Shortcut the case if the data is embedded or not resolved. |
| 851 | * |
| 852 | * Do NOT NULL out chain->data (e.g. inode data), it might be |
| 853 | * dirty. |
| 854 | */ |
| 855 | if (chain->dio == NULL) { |
| 856 | if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) |
| 857 | hammer2_chain_drop_data(chain, 0); |
| 858 | hammer2_mtx_unlock(&chain->lock); |
| 859 | return; |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * Statistics |
| 864 | */ |
| 865 | if (hammer2_io_isdirty(chain->dio) == 0) { |
| 866 | ; |
| 867 | } else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) { |
| 868 | switch(chain->bref.type) { |
| 869 | case HAMMER2_BREF_TYPE_DATA: |
| 870 | counterp = &hammer2_ioa_file_write; |
| 871 | break; |
| 872 | case HAMMER2_BREF_TYPE_INODE: |
| 873 | counterp = &hammer2_ioa_meta_write; |
| 874 | break; |
| 875 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 876 | counterp = &hammer2_ioa_indr_write; |
| 877 | break; |
| 878 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 879 | case HAMMER2_BREF_TYPE_FREEMAP_LEAF: |
| 880 | counterp = &hammer2_ioa_fmap_write; |
| 881 | break; |
| 882 | default: |
| 883 | counterp = &hammer2_ioa_volu_write; |
| 884 | break; |
| 885 | } |
| 886 | *counterp += chain->bytes; |
| 887 | } else { |
| 888 | switch(chain->bref.type) { |
| 889 | case HAMMER2_BREF_TYPE_DATA: |
| 890 | counterp = &hammer2_iod_file_write; |
| 891 | break; |
| 892 | case HAMMER2_BREF_TYPE_INODE: |
| 893 | counterp = &hammer2_iod_meta_write; |
| 894 | break; |
| 895 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 896 | counterp = &hammer2_iod_indr_write; |
| 897 | break; |
| 898 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 899 | case HAMMER2_BREF_TYPE_FREEMAP_LEAF: |
| 900 | counterp = &hammer2_iod_fmap_write; |
| 901 | break; |
| 902 | default: |
| 903 | counterp = &hammer2_iod_volu_write; |
| 904 | break; |
| 905 | } |
| 906 | *counterp += chain->bytes; |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * Clean out the dio. |
| 911 | * |
| 912 | * If a device buffer was used for data be sure to destroy the |
| 913 | * buffer when we are done to avoid aliases (XXX what about the |
| 914 | * underlying VM pages?). |
| 915 | * |
| 916 | * NOTE: Freemap leaf's use reserved blocks and thus no aliasing |
| 917 | * is possible. |
| 918 | * |
| 919 | * NOTE: The isdirty check tracks whether we have to bdwrite() the |
| 920 | * buffer or not. The buffer might already be dirty. The |
| 921 | * flag is re-set when chain_modify() is called, even if |
| 922 | * MODIFIED is already set, allowing the OS to retire the |
| 923 | * buffer independent of a hammer2 flush. |
| 924 | */ |
| 925 | chain->data = NULL; |
| 926 | if ((chain->flags & HAMMER2_CHAIN_IOFLUSH) && |
| 927 | hammer2_io_isdirty(chain->dio)) { |
| 928 | hammer2_io_bawrite(&chain->dio); |
| 929 | } else { |
| 930 | hammer2_io_bqrelse(&chain->dio); |
| 931 | } |
| 932 | hammer2_mtx_unlock(&chain->lock); |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * This counts the number of live blockrefs in a block array and |
| 937 | * also calculates the point at which all remaining blockrefs are empty. |
| 938 | * This routine can only be called on a live chain (DUPLICATED flag not set). |
| 939 | * |
| 940 | * NOTE: Flag is not set until after the count is complete, allowing |
| 941 | * callers to test the flag without holding the spinlock. |
| 942 | * |
| 943 | * NOTE: If base is NULL the related chain is still in the INITIAL |
| 944 | * state and there are no blockrefs to count. |
| 945 | * |
| 946 | * NOTE: live_count may already have some counts accumulated due to |
| 947 | * creation and deletion and could even be initially negative. |
| 948 | */ |
| 949 | void |
| 950 | hammer2_chain_countbrefs(hammer2_chain_t *chain, |
| 951 | hammer2_blockref_t *base, int count) |
| 952 | { |
| 953 | hammer2_spin_ex(&chain->core.spin); |
| 954 | if ((chain->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) { |
| 955 | if (base) { |
| 956 | while (--count >= 0) { |
| 957 | if (base[count].type) |
| 958 | break; |
| 959 | } |
| 960 | chain->core.live_zero = count + 1; |
| 961 | while (count >= 0) { |
| 962 | if (base[count].type) |
| 963 | atomic_add_int(&chain->core.live_count, |
| 964 | 1); |
| 965 | --count; |
| 966 | } |
| 967 | } else { |
| 968 | chain->core.live_zero = 0; |
| 969 | } |
| 970 | /* else do not modify live_count */ |
| 971 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_COUNTEDBREFS); |
| 972 | } |
| 973 | hammer2_spin_unex(&chain->core.spin); |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * Resize the chain's physical storage allocation in-place. This function does |
| 978 | * not adjust the data pointer and must be followed by (typically) a |
| 979 | * hammer2_chain_modify() call to copy any old data over and adjust the |
| 980 | * data pointer. |
| 981 | * |
| 982 | * Chains can be resized smaller without reallocating the storage. Resizing |
| 983 | * larger will reallocate the storage. Excess or prior storage is reclaimed |
| 984 | * asynchronously at a later time. |
| 985 | * |
| 986 | * Must be passed an exclusively locked parent and chain. |
| 987 | * |
| 988 | * This function is mostly used with DATA blocks locked RESOLVE_NEVER in order |
| 989 | * to avoid instantiating a device buffer that conflicts with the vnode data |
| 990 | * buffer. However, because H2 can compress or encrypt data, the chain may |
| 991 | * have a dio assigned to it in those situations, and they do not conflict. |
| 992 | * |
| 993 | * XXX return error if cannot resize. |
| 994 | */ |
| 995 | void |
| 996 | hammer2_chain_resize(hammer2_inode_t *ip, |
| 997 | hammer2_chain_t *parent, hammer2_chain_t *chain, |
| 998 | int nradix, int flags) |
| 999 | { |
| 1000 | hammer2_dev_t *hmp; |
| 1001 | size_t obytes; |
| 1002 | size_t nbytes; |
| 1003 | |
| 1004 | hmp = chain->hmp; |
| 1005 | |
| 1006 | /* |
| 1007 | * Only data and indirect blocks can be resized for now. |
| 1008 | * (The volu root, inodes, and freemap elements use a fixed size). |
| 1009 | */ |
| 1010 | KKASSERT(chain != &hmp->vchain); |
| 1011 | KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA || |
| 1012 | chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT); |
| 1013 | |
| 1014 | /* |
| 1015 | * Nothing to do if the element is already the proper size |
| 1016 | */ |
| 1017 | obytes = chain->bytes; |
| 1018 | nbytes = 1U << nradix; |
| 1019 | if (obytes == nbytes) |
| 1020 | return; |
| 1021 | |
| 1022 | /* |
| 1023 | * Make sure the old data is instantiated so we can copy it. If this |
| 1024 | * is a data block, the device data may be superfluous since the data |
| 1025 | * might be in a logical block, but compressed or encrypted data is |
| 1026 | * another matter. |
| 1027 | * |
| 1028 | * NOTE: The modify will set BMAPUPD for us if BMAPPED is set. |
| 1029 | */ |
| 1030 | hammer2_chain_modify(chain, 0); |
| 1031 | |
| 1032 | /* |
| 1033 | * Relocate the block, even if making it smaller (because different |
| 1034 | * block sizes may be in different regions). |
| 1035 | * |
| 1036 | * (data blocks only, we aren't copying the storage here). |
| 1037 | */ |
| 1038 | hammer2_freemap_alloc(chain, nbytes); |
| 1039 | chain->bytes = nbytes; |
| 1040 | /*ip->delta_dcount += (ssize_t)(nbytes - obytes);*/ /* XXX atomic */ |
| 1041 | |
| 1042 | /* |
| 1043 | * We don't want the followup chain_modify() to try to copy data |
| 1044 | * from the old (wrong-sized) buffer. It won't know how much to |
| 1045 | * copy. This case should only occur during writes when the |
| 1046 | * originator already has the data to write in-hand. |
| 1047 | */ |
| 1048 | if (chain->dio) { |
| 1049 | KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA); |
| 1050 | hammer2_io_brelse(&chain->dio); |
| 1051 | chain->data = NULL; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | void |
| 1056 | hammer2_chain_modify(hammer2_chain_t *chain, int flags) |
| 1057 | { |
| 1058 | hammer2_blockref_t obref; |
| 1059 | hammer2_dev_t *hmp; |
| 1060 | hammer2_io_t *dio; |
| 1061 | int error; |
| 1062 | int wasinitial; |
| 1063 | int newmod; |
| 1064 | char *bdata; |
| 1065 | |
| 1066 | hmp = chain->hmp; |
| 1067 | obref = chain->bref; |
| 1068 | KKASSERT((chain->flags & HAMMER2_CHAIN_FICTITIOUS) == 0); |
| 1069 | |
| 1070 | /* |
| 1071 | * Data is not optional for freemap chains (we must always be sure |
| 1072 | * to copy the data on COW storage allocations). |
| 1073 | */ |
| 1074 | if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE || |
| 1075 | chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) { |
| 1076 | KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) || |
| 1077 | (flags & HAMMER2_MODIFY_OPTDATA) == 0); |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * Data must be resolved if already assigned, unless explicitly |
| 1082 | * flagged otherwise. |
| 1083 | */ |
| 1084 | if (chain->data == NULL && (flags & HAMMER2_MODIFY_OPTDATA) == 0 && |
| 1085 | (chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX)) { |
| 1086 | hammer2_chain_load_data(chain); |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * Set MODIFIED to indicate that the chain has been modified. |
| 1091 | * Set UPDATE to ensure that the blockref is updated in the parent. |
| 1092 | */ |
| 1093 | if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) { |
| 1094 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED); |
| 1095 | hammer2_chain_ref(chain); |
| 1096 | hammer2_pfs_memory_inc(chain->pmp); /* can be NULL */ |
| 1097 | newmod = 1; |
| 1098 | } else { |
| 1099 | newmod = 0; |
| 1100 | } |
| 1101 | if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) { |
| 1102 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE); |
| 1103 | hammer2_chain_ref(chain); |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * The modification or re-modification requires an allocation and |
| 1108 | * possible COW. |
| 1109 | * |
| 1110 | * We normally always allocate new storage here. If storage exists |
| 1111 | * and MODIFY_NOREALLOC is passed in, we do not allocate new storage. |
| 1112 | */ |
| 1113 | if (chain != &hmp->vchain && chain != &hmp->fchain) { |
| 1114 | if ((chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0 || |
| 1115 | ((flags & HAMMER2_MODIFY_NOREALLOC) == 0 && newmod) |
| 1116 | ) { |
| 1117 | hammer2_freemap_alloc(chain, chain->bytes); |
| 1118 | /* XXX failed allocation */ |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * Update mirror_tid and modify_tid. modify_tid is only updated |
| 1124 | * automatically by this function when used from the frontend. |
| 1125 | * Flushes and synchronization adjust the flag manually. |
| 1126 | * |
| 1127 | * NOTE: chain->pmp could be the device spmp. |
| 1128 | */ |
| 1129 | chain->bref.mirror_tid = hmp->voldata.mirror_tid + 1; |
| 1130 | if (chain->pmp && (flags & HAMMER2_MODIFY_KEEPMODIFY) == 0) { |
| 1131 | /* XXX HAMMER2_TRANS_ISFLUSH */ |
| 1132 | chain->bref.modify_tid = chain->pmp->modify_tid; |
| 1133 | } |
| 1134 | |
| 1135 | /* |
| 1136 | * Set BMAPUPD to tell the flush code that an existing blockmap entry |
| 1137 | * requires updating as well as to tell the delete code that the |
| 1138 | * chain's blockref might not exactly match (in terms of physical size |
| 1139 | * or block offset) the one in the parent's blocktable. The base key |
| 1140 | * of course will still match. |
| 1141 | */ |
| 1142 | if (chain->flags & HAMMER2_CHAIN_BMAPPED) |
| 1143 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPUPD); |
| 1144 | |
| 1145 | /* |
| 1146 | * Short-cut data blocks which the caller does not need an actual |
| 1147 | * data reference to (aka OPTDATA), as long as the chain does not |
| 1148 | * already have a data pointer to the data. This generally means |
| 1149 | * that the modifications are being done via the logical buffer cache. |
| 1150 | * The INITIAL flag relates only to the device data buffer and thus |
| 1151 | * remains unchange in this situation. |
| 1152 | */ |
| 1153 | if (chain->bref.type == HAMMER2_BREF_TYPE_DATA && |
| 1154 | (flags & HAMMER2_MODIFY_OPTDATA) && |
| 1155 | chain->data == NULL) { |
| 1156 | goto skip2; |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * Clearing the INITIAL flag (for indirect blocks) indicates that |
| 1161 | * we've processed the uninitialized storage allocation. |
| 1162 | * |
| 1163 | * If this flag is already clear we are likely in a copy-on-write |
| 1164 | * situation but we have to be sure NOT to bzero the storage if |
| 1165 | * no data is present. |
| 1166 | */ |
| 1167 | if (chain->flags & HAMMER2_CHAIN_INITIAL) { |
| 1168 | atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL); |
| 1169 | wasinitial = 1; |
| 1170 | } else { |
| 1171 | wasinitial = 0; |
| 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * Instantiate data buffer and possibly execute COW operation |
| 1176 | */ |
| 1177 | switch(chain->bref.type) { |
| 1178 | case HAMMER2_BREF_TYPE_VOLUME: |
| 1179 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 1180 | /* |
| 1181 | * The data is embedded, no copy-on-write operation is |
| 1182 | * needed. |
| 1183 | */ |
| 1184 | KKASSERT(chain->dio == NULL); |
| 1185 | break; |
| 1186 | case HAMMER2_BREF_TYPE_INODE: |
| 1187 | case HAMMER2_BREF_TYPE_FREEMAP_LEAF: |
| 1188 | case HAMMER2_BREF_TYPE_DATA: |
| 1189 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 1190 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 1191 | /* |
| 1192 | * Perform the copy-on-write operation |
| 1193 | * |
| 1194 | * zero-fill or copy-on-write depending on whether |
| 1195 | * chain->data exists or not and set the dirty state for |
| 1196 | * the new buffer. hammer2_io_new() will handle the |
| 1197 | * zero-fill. |
| 1198 | */ |
| 1199 | KKASSERT(chain != &hmp->vchain && chain != &hmp->fchain); |
| 1200 | |
| 1201 | if (wasinitial) { |
| 1202 | error = hammer2_io_new(hmp, chain->bref.data_off, |
| 1203 | chain->bytes, &dio); |
| 1204 | } else { |
| 1205 | error = hammer2_io_bread(hmp, chain->bref.data_off, |
| 1206 | chain->bytes, &dio); |
| 1207 | } |
| 1208 | hammer2_adjreadcounter(&chain->bref, chain->bytes); |
| 1209 | |
| 1210 | /* |
| 1211 | * If an I/O error occurs make sure callers cannot accidently |
| 1212 | * modify the old buffer's contents and corrupt the filesystem. |
| 1213 | */ |
| 1214 | if (error) { |
| 1215 | kprintf("hammer2_chain_modify: hmp=%p I/O error\n", |
| 1216 | hmp); |
| 1217 | chain->error = HAMMER2_ERROR_IO; |
| 1218 | hammer2_io_brelse(&dio); |
| 1219 | hammer2_io_brelse(&chain->dio); |
| 1220 | chain->data = NULL; |
| 1221 | break; |
| 1222 | } |
| 1223 | chain->error = 0; |
| 1224 | bdata = hammer2_io_data(dio, chain->bref.data_off); |
| 1225 | |
| 1226 | if (chain->data) { |
| 1227 | KKASSERT(chain->dio != NULL); |
| 1228 | if (chain->data != (void *)bdata) { |
| 1229 | bcopy(chain->data, bdata, chain->bytes); |
| 1230 | } |
| 1231 | } else if (wasinitial == 0) { |
| 1232 | /* |
| 1233 | * We have a problem. We were asked to COW but |
| 1234 | * we don't have any data to COW with! |
| 1235 | */ |
| 1236 | panic("hammer2_chain_modify: having a COW %p\n", |
| 1237 | chain); |
| 1238 | } |
| 1239 | |
| 1240 | /* |
| 1241 | * Retire the old buffer, replace with the new. Dirty or |
| 1242 | * redirty the new buffer. |
| 1243 | * |
| 1244 | * WARNING! The system buffer cache may have already flushed |
| 1245 | * the buffer, so we must be sure to [re]dirty it |
| 1246 | * for further modification. |
| 1247 | */ |
| 1248 | if (chain->dio) |
| 1249 | hammer2_io_brelse(&chain->dio); |
| 1250 | chain->data = (void *)bdata; |
| 1251 | chain->dio = dio; |
| 1252 | hammer2_io_setdirty(dio); /* modified by bcopy above */ |
| 1253 | break; |
| 1254 | default: |
| 1255 | panic("hammer2_chain_modify: illegal non-embedded type %d", |
| 1256 | chain->bref.type); |
| 1257 | break; |
| 1258 | |
| 1259 | } |
| 1260 | skip2: |
| 1261 | /* |
| 1262 | * setflush on parent indicating that the parent must recurse down |
| 1263 | * to us. Do not call on chain itself which might already have it |
| 1264 | * set. |
| 1265 | */ |
| 1266 | if (chain->parent) |
| 1267 | hammer2_chain_setflush(chain->parent); |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * Modify the chain associated with an inode. |
| 1272 | */ |
| 1273 | void |
| 1274 | hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain, int flags) |
| 1275 | { |
| 1276 | hammer2_inode_modify(ip); |
| 1277 | hammer2_chain_modify(chain, flags); |
| 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * Volume header data locks |
| 1282 | */ |
| 1283 | void |
| 1284 | hammer2_voldata_lock(hammer2_dev_t *hmp) |
| 1285 | { |
| 1286 | lockmgr(&hmp->vollk, LK_EXCLUSIVE); |
| 1287 | } |
| 1288 | |
| 1289 | void |
| 1290 | hammer2_voldata_unlock(hammer2_dev_t *hmp) |
| 1291 | { |
| 1292 | lockmgr(&hmp->vollk, LK_RELEASE); |
| 1293 | } |
| 1294 | |
| 1295 | void |
| 1296 | hammer2_voldata_modify(hammer2_dev_t *hmp) |
| 1297 | { |
| 1298 | if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) { |
| 1299 | atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED); |
| 1300 | hammer2_chain_ref(&hmp->vchain); |
| 1301 | hammer2_pfs_memory_inc(hmp->vchain.pmp); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | * This function returns the chain at the nearest key within the specified |
| 1307 | * range. The returned chain will be referenced but not locked. |
| 1308 | * |
| 1309 | * This function will recurse through chain->rbtree as necessary and will |
| 1310 | * return a *key_nextp suitable for iteration. *key_nextp is only set if |
| 1311 | * the iteration value is less than the current value of *key_nextp. |
| 1312 | * |
| 1313 | * The caller should use (*key_nextp) to calculate the actual range of |
| 1314 | * the returned element, which will be (key_beg to *key_nextp - 1), because |
| 1315 | * there might be another element which is superior to the returned element |
| 1316 | * and overlaps it. |
| 1317 | * |
| 1318 | * (*key_nextp) can be passed as key_beg in an iteration only while non-NULL |
| 1319 | * chains continue to be returned. On EOF (*key_nextp) may overflow since |
| 1320 | * it will wind up being (key_end + 1). |
| 1321 | * |
| 1322 | * WARNING! Must be called with child's spinlock held. Spinlock remains |
| 1323 | * held through the operation. |
| 1324 | */ |
| 1325 | struct hammer2_chain_find_info { |
| 1326 | hammer2_chain_t *best; |
| 1327 | hammer2_key_t key_beg; |
| 1328 | hammer2_key_t key_end; |
| 1329 | hammer2_key_t key_next; |
| 1330 | }; |
| 1331 | |
| 1332 | static int hammer2_chain_find_cmp(hammer2_chain_t *child, void *data); |
| 1333 | static int hammer2_chain_find_callback(hammer2_chain_t *child, void *data); |
| 1334 | |
| 1335 | static |
| 1336 | hammer2_chain_t * |
| 1337 | hammer2_chain_find(hammer2_chain_t *parent, hammer2_key_t *key_nextp, |
| 1338 | hammer2_key_t key_beg, hammer2_key_t key_end) |
| 1339 | { |
| 1340 | struct hammer2_chain_find_info info; |
| 1341 | |
| 1342 | info.best = NULL; |
| 1343 | info.key_beg = key_beg; |
| 1344 | info.key_end = key_end; |
| 1345 | info.key_next = *key_nextp; |
| 1346 | |
| 1347 | RB_SCAN(hammer2_chain_tree, &parent->core.rbtree, |
| 1348 | hammer2_chain_find_cmp, hammer2_chain_find_callback, |
| 1349 | &info); |
| 1350 | *key_nextp = info.key_next; |
| 1351 | #if 0 |
| 1352 | kprintf("chain_find %p %016jx:%016jx next=%016jx\n", |
| 1353 | parent, key_beg, key_end, *key_nextp); |
| 1354 | #endif |
| 1355 | |
| 1356 | return (info.best); |
| 1357 | } |
| 1358 | |
| 1359 | static |
| 1360 | int |
| 1361 | hammer2_chain_find_cmp(hammer2_chain_t *child, void *data) |
| 1362 | { |
| 1363 | struct hammer2_chain_find_info *info = data; |
| 1364 | hammer2_key_t child_beg; |
| 1365 | hammer2_key_t child_end; |
| 1366 | |
| 1367 | child_beg = child->bref.key; |
| 1368 | child_end = child_beg + ((hammer2_key_t)1 << child->bref.keybits) - 1; |
| 1369 | |
| 1370 | if (child_end < info->key_beg) |
| 1371 | return(-1); |
| 1372 | if (child_beg > info->key_end) |
| 1373 | return(1); |
| 1374 | return(0); |
| 1375 | } |
| 1376 | |
| 1377 | static |
| 1378 | int |
| 1379 | hammer2_chain_find_callback(hammer2_chain_t *child, void *data) |
| 1380 | { |
| 1381 | struct hammer2_chain_find_info *info = data; |
| 1382 | hammer2_chain_t *best; |
| 1383 | hammer2_key_t child_end; |
| 1384 | |
| 1385 | /* |
| 1386 | * WARNING! Do not discard DUPLICATED chains, it is possible that |
| 1387 | * we are catching an insertion half-way done. If a |
| 1388 | * duplicated chain turns out to be the best choice the |
| 1389 | * caller will re-check its flags after locking it. |
| 1390 | * |
| 1391 | * WARNING! Layerq is scanned forwards, exact matches should keep |
| 1392 | * the existing info->best. |
| 1393 | */ |
| 1394 | if ((best = info->best) == NULL) { |
| 1395 | /* |
| 1396 | * No previous best. Assign best |
| 1397 | */ |
| 1398 | info->best = child; |
| 1399 | } else if (best->bref.key <= info->key_beg && |
| 1400 | child->bref.key <= info->key_beg) { |
| 1401 | /* |
| 1402 | * Illegal overlap. |
| 1403 | */ |
| 1404 | KKASSERT(0); |
| 1405 | /*info->best = child;*/ |
| 1406 | } else if (child->bref.key < best->bref.key) { |
| 1407 | /* |
| 1408 | * Child has a nearer key and best is not flush with key_beg. |
| 1409 | * Set best to child. Truncate key_next to the old best key. |
| 1410 | */ |
| 1411 | info->best = child; |
| 1412 | if (info->key_next > best->bref.key || info->key_next == 0) |
| 1413 | info->key_next = best->bref.key; |
| 1414 | } else if (child->bref.key == best->bref.key) { |
| 1415 | /* |
| 1416 | * If our current best is flush with the child then this |
| 1417 | * is an illegal overlap. |
| 1418 | * |
| 1419 | * key_next will automatically be limited to the smaller of |
| 1420 | * the two end-points. |
| 1421 | */ |
| 1422 | KKASSERT(0); |
| 1423 | info->best = child; |
| 1424 | } else { |
| 1425 | /* |
| 1426 | * Keep the current best but truncate key_next to the child's |
| 1427 | * base. |
| 1428 | * |
| 1429 | * key_next will also automatically be limited to the smaller |
| 1430 | * of the two end-points (probably not necessary for this case |
| 1431 | * but we do it anyway). |
| 1432 | */ |
| 1433 | if (info->key_next > child->bref.key || info->key_next == 0) |
| 1434 | info->key_next = child->bref.key; |
| 1435 | } |
| 1436 | |
| 1437 | /* |
| 1438 | * Always truncate key_next based on child's end-of-range. |
| 1439 | */ |
| 1440 | child_end = child->bref.key + ((hammer2_key_t)1 << child->bref.keybits); |
| 1441 | if (child_end && (info->key_next > child_end || info->key_next == 0)) |
| 1442 | info->key_next = child_end; |
| 1443 | |
| 1444 | return(0); |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * Retrieve the specified chain from a media blockref, creating the |
| 1449 | * in-memory chain structure which reflects it. |
| 1450 | * |
| 1451 | * To handle insertion races pass the INSERT_RACE flag along with the |
| 1452 | * generation number of the core. NULL will be returned if the generation |
| 1453 | * number changes before we have a chance to insert the chain. Insert |
| 1454 | * races can occur because the parent might be held shared. |
| 1455 | * |
| 1456 | * Caller must hold the parent locked shared or exclusive since we may |
| 1457 | * need the parent's bref array to find our block. |
| 1458 | * |
| 1459 | * WARNING! chain->pmp is always set to NULL for any chain representing |
| 1460 | * part of the super-root topology. |
| 1461 | */ |
| 1462 | hammer2_chain_t * |
| 1463 | hammer2_chain_get(hammer2_chain_t *parent, int generation, |
| 1464 | hammer2_blockref_t *bref) |
| 1465 | { |
| 1466 | hammer2_dev_t *hmp = parent->hmp; |
| 1467 | hammer2_chain_t *chain; |
| 1468 | int error; |
| 1469 | |
| 1470 | /* |
| 1471 | * Allocate a chain structure representing the existing media |
| 1472 | * entry. Resulting chain has one ref and is not locked. |
| 1473 | */ |
| 1474 | if (bref->flags & HAMMER2_BREF_FLAG_PFSROOT) |
| 1475 | chain = hammer2_chain_alloc(hmp, NULL, bref); |
| 1476 | else |
| 1477 | chain = hammer2_chain_alloc(hmp, parent->pmp, bref); |
| 1478 | /* ref'd chain returned */ |
| 1479 | |
| 1480 | /* |
| 1481 | * Flag that the chain is in the parent's blockmap so delete/flush |
| 1482 | * knows what to do with it. |
| 1483 | */ |
| 1484 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED); |
| 1485 | |
| 1486 | /* |
| 1487 | * Link the chain into its parent. A spinlock is required to safely |
| 1488 | * access the RBTREE, and it is possible to collide with another |
| 1489 | * hammer2_chain_get() operation because the caller might only hold |
| 1490 | * a shared lock on the parent. |
| 1491 | */ |
| 1492 | KKASSERT(parent->refs > 0); |
| 1493 | error = hammer2_chain_insert(parent, chain, |
| 1494 | HAMMER2_CHAIN_INSERT_SPIN | |
| 1495 | HAMMER2_CHAIN_INSERT_RACE, |
| 1496 | generation); |
| 1497 | if (error) { |
| 1498 | KKASSERT((chain->flags & HAMMER2_CHAIN_ONRBTREE) == 0); |
| 1499 | kprintf("chain %p get race\n", chain); |
| 1500 | hammer2_chain_drop(chain); |
| 1501 | chain = NULL; |
| 1502 | } else { |
| 1503 | KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE); |
| 1504 | } |
| 1505 | |
| 1506 | /* |
| 1507 | * Return our new chain referenced but not locked, or NULL if |
| 1508 | * a race occurred. |
| 1509 | */ |
| 1510 | return (chain); |
| 1511 | } |
| 1512 | |
| 1513 | /* |
| 1514 | * Lookup initialization/completion API |
| 1515 | */ |
| 1516 | hammer2_chain_t * |
| 1517 | hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags) |
| 1518 | { |
| 1519 | hammer2_chain_ref(parent); |
| 1520 | if (flags & HAMMER2_LOOKUP_SHARED) { |
| 1521 | hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS | |
| 1522 | HAMMER2_RESOLVE_SHARED); |
| 1523 | } else { |
| 1524 | hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); |
| 1525 | } |
| 1526 | return (parent); |
| 1527 | } |
| 1528 | |
| 1529 | void |
| 1530 | hammer2_chain_lookup_done(hammer2_chain_t *parent) |
| 1531 | { |
| 1532 | if (parent) { |
| 1533 | hammer2_chain_unlock(parent); |
| 1534 | hammer2_chain_drop(parent); |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | hammer2_chain_t * |
| 1539 | hammer2_chain_getparent(hammer2_chain_t **parentp, int how) |
| 1540 | { |
| 1541 | hammer2_chain_t *oparent; |
| 1542 | hammer2_chain_t *nparent; |
| 1543 | |
| 1544 | /* |
| 1545 | * Be careful of order, oparent must be unlocked before nparent |
| 1546 | * is locked below to avoid a deadlock. |
| 1547 | */ |
| 1548 | oparent = *parentp; |
| 1549 | hammer2_spin_ex(&oparent->core.spin); |
| 1550 | nparent = oparent->parent; |
| 1551 | hammer2_chain_ref(nparent); |
| 1552 | hammer2_spin_unex(&oparent->core.spin); |
| 1553 | if (oparent) { |
| 1554 | hammer2_chain_unlock(oparent); |
| 1555 | hammer2_chain_drop(oparent); |
| 1556 | oparent = NULL; |
| 1557 | } |
| 1558 | |
| 1559 | hammer2_chain_lock(nparent, how); |
| 1560 | *parentp = nparent; |
| 1561 | |
| 1562 | return (nparent); |
| 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * Locate the first chain whos key range overlaps (key_beg, key_end) inclusive. |
| 1567 | * (*parentp) typically points to an inode but can also point to a related |
| 1568 | * indirect block and this function will recurse upwards and find the inode |
| 1569 | * again. |
| 1570 | * |
| 1571 | * (*parentp) must be exclusively locked and referenced and can be an inode |
| 1572 | * or an existing indirect block within the inode. |
| 1573 | * |
| 1574 | * On return (*parentp) will be modified to point at the deepest parent chain |
| 1575 | * element encountered during the search, as a helper for an insertion or |
| 1576 | * deletion. The new (*parentp) will be locked and referenced and the old |
| 1577 | * will be unlocked and dereferenced (no change if they are both the same). |
| 1578 | * |
| 1579 | * The matching chain will be returned exclusively locked. If NOLOCK is |
| 1580 | * requested the chain will be returned only referenced. Note that the |
| 1581 | * parent chain must always be locked shared or exclusive, matching the |
| 1582 | * HAMMER2_LOOKUP_SHARED flag. We can conceivably lock it SHARED temporarily |
| 1583 | * when NOLOCK is specified but that complicates matters if *parentp must |
| 1584 | * inherit the chain. |
| 1585 | * |
| 1586 | * NOLOCK also implies NODATA, since an unlocked chain usually has a NULL |
| 1587 | * data pointer or can otherwise be in flux. |
| 1588 | * |
| 1589 | * NULL is returned if no match was found, but (*parentp) will still |
| 1590 | * potentially be adjusted. |
| 1591 | * |
| 1592 | * If a fatal error occurs (typically an I/O error), a dummy chain is |
| 1593 | * returned with chain->error and error-identifying information set. This |
| 1594 | * chain will assert if you try to do anything fancy with it. |
| 1595 | * |
| 1596 | * XXX Depending on where the error occurs we should allow continued iteration. |
| 1597 | * |
| 1598 | * On return (*key_nextp) will point to an iterative value for key_beg. |
| 1599 | * (If NULL is returned (*key_nextp) is set to (key_end + 1)). |
| 1600 | * |
| 1601 | * This function will also recurse up the chain if the key is not within the |
| 1602 | * current parent's range. (*parentp) can never be set to NULL. An iteration |
| 1603 | * can simply allow (*parentp) to float inside the loop. |
| 1604 | * |
| 1605 | * NOTE! chain->data is not always resolved. By default it will not be |
| 1606 | * resolved for BREF_TYPE_DATA, FREEMAP_NODE, or FREEMAP_LEAF. Use |
| 1607 | * HAMMER2_LOOKUP_ALWAYS to force resolution (but be careful w/ |
| 1608 | * BREF_TYPE_DATA as the device buffer can alias the logical file |
| 1609 | * buffer). |
| 1610 | */ |
| 1611 | hammer2_chain_t * |
| 1612 | hammer2_chain_lookup(hammer2_chain_t **parentp, hammer2_key_t *key_nextp, |
| 1613 | hammer2_key_t key_beg, hammer2_key_t key_end, |
| 1614 | int *cache_indexp, int flags) |
| 1615 | { |
| 1616 | hammer2_dev_t *hmp; |
| 1617 | hammer2_chain_t *parent; |
| 1618 | hammer2_chain_t *chain; |
| 1619 | hammer2_blockref_t *base; |
| 1620 | hammer2_blockref_t *bref; |
| 1621 | hammer2_blockref_t bcopy; |
| 1622 | hammer2_key_t scan_beg; |
| 1623 | hammer2_key_t scan_end; |
| 1624 | int count = 0; |
| 1625 | int how_always = HAMMER2_RESOLVE_ALWAYS; |
| 1626 | int how_maybe = HAMMER2_RESOLVE_MAYBE; |
| 1627 | int how; |
| 1628 | int generation; |
| 1629 | int maxloops = 300000; |
| 1630 | |
| 1631 | if (flags & HAMMER2_LOOKUP_ALWAYS) { |
| 1632 | how_maybe = how_always; |
| 1633 | how = HAMMER2_RESOLVE_ALWAYS; |
| 1634 | } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) { |
| 1635 | how = HAMMER2_RESOLVE_NEVER; |
| 1636 | } else { |
| 1637 | how = HAMMER2_RESOLVE_MAYBE; |
| 1638 | } |
| 1639 | if (flags & HAMMER2_LOOKUP_SHARED) { |
| 1640 | how_maybe |= HAMMER2_RESOLVE_SHARED; |
| 1641 | how_always |= HAMMER2_RESOLVE_SHARED; |
| 1642 | how |= HAMMER2_RESOLVE_SHARED; |
| 1643 | } |
| 1644 | |
| 1645 | /* |
| 1646 | * Recurse (*parentp) upward if necessary until the parent completely |
| 1647 | * encloses the key range or we hit the inode. |
| 1648 | * |
| 1649 | * This function handles races against the flusher doing a delete- |
| 1650 | * duplicate above us and re-homes the parent to the duplicate in |
| 1651 | * that case, otherwise we'd wind up recursing down a stale chain. |
| 1652 | */ |
| 1653 | parent = *parentp; |
| 1654 | hmp = parent->hmp; |
| 1655 | |
| 1656 | while (parent->bref.type == HAMMER2_BREF_TYPE_INDIRECT || |
| 1657 | parent->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) { |
| 1658 | scan_beg = parent->bref.key; |
| 1659 | scan_end = scan_beg + |
| 1660 | ((hammer2_key_t)1 << parent->bref.keybits) - 1; |
| 1661 | if (key_beg >= scan_beg && key_end <= scan_end) |
| 1662 | break; |
| 1663 | parent = hammer2_chain_getparent(parentp, how_maybe); |
| 1664 | } |
| 1665 | |
| 1666 | again: |
| 1667 | if (--maxloops == 0) |
| 1668 | panic("hammer2_chain_lookup: maxloops"); |
| 1669 | /* |
| 1670 | * Locate the blockref array. Currently we do a fully associative |
| 1671 | * search through the array. |
| 1672 | */ |
| 1673 | switch(parent->bref.type) { |
| 1674 | case HAMMER2_BREF_TYPE_INODE: |
| 1675 | /* |
| 1676 | * Special shortcut for embedded data returns the inode |
| 1677 | * itself. Callers must detect this condition and access |
| 1678 | * the embedded data (the strategy code does this for us). |
| 1679 | * |
| 1680 | * This is only applicable to regular files and softlinks. |
| 1681 | */ |
| 1682 | if (parent->data->ipdata.meta.op_flags & |
| 1683 | HAMMER2_OPFLAG_DIRECTDATA) { |
| 1684 | if (flags & HAMMER2_LOOKUP_NODIRECT) { |
| 1685 | chain = NULL; |
| 1686 | *key_nextp = key_end + 1; |
| 1687 | goto done; |
| 1688 | } |
| 1689 | hammer2_chain_ref(parent); |
| 1690 | if ((flags & HAMMER2_LOOKUP_NOLOCK) == 0) |
| 1691 | hammer2_chain_lock(parent, how_always); |
| 1692 | *key_nextp = key_end + 1; |
| 1693 | return (parent); |
| 1694 | } |
| 1695 | base = &parent->data->ipdata.u.blockset.blockref[0]; |
| 1696 | count = HAMMER2_SET_COUNT; |
| 1697 | break; |
| 1698 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 1699 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 1700 | /* |
| 1701 | * Handle MATCHIND on the parent |
| 1702 | */ |
| 1703 | if (flags & HAMMER2_LOOKUP_MATCHIND) { |
| 1704 | scan_beg = parent->bref.key; |
| 1705 | scan_end = scan_beg + |
| 1706 | ((hammer2_key_t)1 << parent->bref.keybits) - 1; |
| 1707 | if (key_beg == scan_beg && key_end == scan_end) { |
| 1708 | chain = parent; |
| 1709 | hammer2_chain_ref(chain); |
| 1710 | hammer2_chain_lock(chain, how_maybe); |
| 1711 | *key_nextp = scan_end + 1; |
| 1712 | goto done; |
| 1713 | } |
| 1714 | } |
| 1715 | /* |
| 1716 | * Optimize indirect blocks in the INITIAL state to avoid |
| 1717 | * I/O. |
| 1718 | */ |
| 1719 | if (parent->flags & HAMMER2_CHAIN_INITIAL) { |
| 1720 | base = NULL; |
| 1721 | } else { |
| 1722 | if (parent->data == NULL) |
| 1723 | panic("parent->data is NULL"); |
| 1724 | base = &parent->data->npdata[0]; |
| 1725 | } |
| 1726 | count = parent->bytes / sizeof(hammer2_blockref_t); |
| 1727 | break; |
| 1728 | case HAMMER2_BREF_TYPE_VOLUME: |
| 1729 | base = &hmp->voldata.sroot_blockset.blockref[0]; |
| 1730 | count = HAMMER2_SET_COUNT; |
| 1731 | break; |
| 1732 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 1733 | base = &hmp->voldata.freemap_blockset.blockref[0]; |
| 1734 | count = HAMMER2_SET_COUNT; |
| 1735 | break; |
| 1736 | default: |
| 1737 | kprintf("hammer2_chain_lookup: unrecognized " |
| 1738 | "blockref(B) type: %d", |
| 1739 | parent->bref.type); |
| 1740 | while (1) |
| 1741 | tsleep(&base, 0, "dead", 0); |
| 1742 | panic("hammer2_chain_lookup: unrecognized " |
| 1743 | "blockref(B) type: %d", |
| 1744 | parent->bref.type); |
| 1745 | base = NULL; /* safety */ |
| 1746 | count = 0; /* safety */ |
| 1747 | } |
| 1748 | |
| 1749 | /* |
| 1750 | * Merged scan to find next candidate. |
| 1751 | * |
| 1752 | * hammer2_base_*() functions require the parent->core.live_* fields |
| 1753 | * to be synchronized. |
| 1754 | * |
| 1755 | * We need to hold the spinlock to access the block array and RB tree |
| 1756 | * and to interlock chain creation. |
| 1757 | */ |
| 1758 | if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) |
| 1759 | hammer2_chain_countbrefs(parent, base, count); |
| 1760 | |
| 1761 | /* |
| 1762 | * Combined search |
| 1763 | */ |
| 1764 | hammer2_spin_ex(&parent->core.spin); |
| 1765 | chain = hammer2_combined_find(parent, base, count, |
| 1766 | cache_indexp, key_nextp, |
| 1767 | key_beg, key_end, |
| 1768 | &bref); |
| 1769 | generation = parent->core.generation; |
| 1770 | |
| 1771 | /* |
| 1772 | * Exhausted parent chain, iterate. |
| 1773 | */ |
| 1774 | if (bref == NULL) { |
| 1775 | hammer2_spin_unex(&parent->core.spin); |
| 1776 | if (key_beg == key_end) /* short cut single-key case */ |
| 1777 | return (NULL); |
| 1778 | |
| 1779 | /* |
| 1780 | * Stop if we reached the end of the iteration. |
| 1781 | */ |
| 1782 | if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT && |
| 1783 | parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) { |
| 1784 | return (NULL); |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * Calculate next key, stop if we reached the end of the |
| 1789 | * iteration, otherwise go up one level and loop. |
| 1790 | */ |
| 1791 | key_beg = parent->bref.key + |
| 1792 | ((hammer2_key_t)1 << parent->bref.keybits); |
| 1793 | if (key_beg == 0 || key_beg > key_end) |
| 1794 | return (NULL); |
| 1795 | parent = hammer2_chain_getparent(parentp, how_maybe); |
| 1796 | goto again; |
| 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * Selected from blockref or in-memory chain. |
| 1801 | */ |
| 1802 | if (chain == NULL) { |
| 1803 | bcopy = *bref; |
| 1804 | hammer2_spin_unex(&parent->core.spin); |
| 1805 | chain = hammer2_chain_get(parent, generation, |
| 1806 | &bcopy); |
| 1807 | if (chain == NULL) { |
| 1808 | kprintf("retry lookup parent %p keys %016jx:%016jx\n", |
| 1809 | parent, key_beg, key_end); |
| 1810 | goto again; |
| 1811 | } |
| 1812 | if (bcmp(&bcopy, bref, sizeof(bcopy))) { |
| 1813 | hammer2_chain_drop(chain); |
| 1814 | goto again; |
| 1815 | } |
| 1816 | } else { |
| 1817 | hammer2_chain_ref(chain); |
| 1818 | hammer2_spin_unex(&parent->core.spin); |
| 1819 | } |
| 1820 | |
| 1821 | /* |
| 1822 | * chain is referenced but not locked. We must lock the chain |
| 1823 | * to obtain definitive DUPLICATED/DELETED state |
| 1824 | */ |
| 1825 | if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT || |
| 1826 | chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) { |
| 1827 | hammer2_chain_lock(chain, how_maybe); |
| 1828 | } else { |
| 1829 | hammer2_chain_lock(chain, how); |
| 1830 | } |
| 1831 | |
| 1832 | /* |
| 1833 | * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX) |
| 1834 | * |
| 1835 | * NOTE: Chain's key range is not relevant as there might be |
| 1836 | * one-offs within the range that are not deleted. |
| 1837 | * |
| 1838 | * NOTE: Lookups can race delete-duplicate because |
| 1839 | * delete-duplicate does not lock the parent's core |
| 1840 | * (they just use the spinlock on the core). We must |
| 1841 | * check for races by comparing the DUPLICATED flag before |
| 1842 | * releasing the spinlock with the flag after locking the |
| 1843 | * chain. |
| 1844 | */ |
| 1845 | if (chain->flags & HAMMER2_CHAIN_DELETED) { |
| 1846 | hammer2_chain_unlock(chain); |
| 1847 | hammer2_chain_drop(chain); |
| 1848 | key_beg = *key_nextp; |
| 1849 | if (key_beg == 0 || key_beg > key_end) |
| 1850 | return(NULL); |
| 1851 | goto again; |
| 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * If the chain element is an indirect block it becomes the new |
| 1856 | * parent and we loop on it. We must maintain our top-down locks |
| 1857 | * to prevent the flusher from interfering (i.e. doing a |
| 1858 | * delete-duplicate and leaving us recursing down a deleted chain). |
| 1859 | * |
| 1860 | * The parent always has to be locked with at least RESOLVE_MAYBE |
| 1861 | * so we can access its data. It might need a fixup if the caller |
| 1862 | * passed incompatible flags. Be careful not to cause a deadlock |
| 1863 | * as a data-load requires an exclusive lock. |
| 1864 | * |
| 1865 | * If HAMMER2_LOOKUP_MATCHIND is set and the indirect block's key |
| 1866 | * range is within the requested key range we return the indirect |
| 1867 | * block and do NOT loop. This is usually only used to acquire |
| 1868 | * freemap nodes. |
| 1869 | */ |
| 1870 | if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT || |
| 1871 | chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_NODE) { |
| 1872 | hammer2_chain_unlock(parent); |
| 1873 | hammer2_chain_drop(parent); |
| 1874 | *parentp = parent = chain; |
| 1875 | goto again; |
| 1876 | } |
| 1877 | done: |
| 1878 | /* |
| 1879 | * All done, return the chain. |
| 1880 | * |
| 1881 | * If the caller does not want a locked chain, replace the lock with |
| 1882 | * a ref. Perhaps this can eventually be optimized to not obtain the |
| 1883 | * lock in the first place for situations where the data does not |
| 1884 | * need to be resolved. |
| 1885 | */ |
| 1886 | if (chain) { |
| 1887 | if (flags & HAMMER2_LOOKUP_NOLOCK) |
| 1888 | hammer2_chain_unlock(chain); |
| 1889 | } |
| 1890 | |
| 1891 | return (chain); |
| 1892 | } |
| 1893 | |
| 1894 | /* |
| 1895 | * After having issued a lookup we can iterate all matching keys. |
| 1896 | * |
| 1897 | * If chain is non-NULL we continue the iteration from just after it's index. |
| 1898 | * |
| 1899 | * If chain is NULL we assume the parent was exhausted and continue the |
| 1900 | * iteration at the next parent. |
| 1901 | * |
| 1902 | * If a fatal error occurs (typically an I/O error), a dummy chain is |
| 1903 | * returned with chain->error and error-identifying information set. This |
| 1904 | * chain will assert if you try to do anything fancy with it. |
| 1905 | * |
| 1906 | * XXX Depending on where the error occurs we should allow continued iteration. |
| 1907 | * |
| 1908 | * parent must be locked on entry and remains locked throughout. chain's |
| 1909 | * lock status must match flags. Chain is always at least referenced. |
| 1910 | * |
| 1911 | * WARNING! The MATCHIND flag does not apply to this function. |
| 1912 | */ |
| 1913 | hammer2_chain_t * |
| 1914 | hammer2_chain_next(hammer2_chain_t **parentp, hammer2_chain_t *chain, |
| 1915 | hammer2_key_t *key_nextp, |
| 1916 | hammer2_key_t key_beg, hammer2_key_t key_end, |
| 1917 | int *cache_indexp, int flags) |
| 1918 | { |
| 1919 | hammer2_chain_t *parent; |
| 1920 | int how_maybe; |
| 1921 | |
| 1922 | /* |
| 1923 | * Calculate locking flags for upward recursion. |
| 1924 | */ |
| 1925 | how_maybe = HAMMER2_RESOLVE_MAYBE; |
| 1926 | if (flags & HAMMER2_LOOKUP_SHARED) |
| 1927 | how_maybe |= HAMMER2_RESOLVE_SHARED; |
| 1928 | |
| 1929 | parent = *parentp; |
| 1930 | |
| 1931 | /* |
| 1932 | * Calculate the next index and recalculate the parent if necessary. |
| 1933 | */ |
| 1934 | if (chain) { |
| 1935 | key_beg = chain->bref.key + |
| 1936 | ((hammer2_key_t)1 << chain->bref.keybits); |
| 1937 | if ((flags & (HAMMER2_LOOKUP_NOLOCK | |
| 1938 | HAMMER2_LOOKUP_NOUNLOCK)) == 0) { |
| 1939 | hammer2_chain_unlock(chain); |
| 1940 | } |
| 1941 | hammer2_chain_drop(chain); |
| 1942 | |
| 1943 | /* |
| 1944 | * chain invalid past this point, but we can still do a |
| 1945 | * pointer comparison w/parent. |
| 1946 | * |
| 1947 | * Any scan where the lookup returned degenerate data embedded |
| 1948 | * in the inode has an invalid index and must terminate. |
| 1949 | */ |
| 1950 | if (chain == parent) |
| 1951 | return(NULL); |
| 1952 | if (key_beg == 0 || key_beg > key_end) |
| 1953 | return(NULL); |
| 1954 | chain = NULL; |
| 1955 | } else if (parent->bref.type != HAMMER2_BREF_TYPE_INDIRECT && |
| 1956 | parent->bref.type != HAMMER2_BREF_TYPE_FREEMAP_NODE) { |
| 1957 | /* |
| 1958 | * We reached the end of the iteration. |
| 1959 | */ |
| 1960 | return (NULL); |
| 1961 | } else { |
| 1962 | /* |
| 1963 | * Continue iteration with next parent unless the current |
| 1964 | * parent covers the range. |
| 1965 | */ |
| 1966 | key_beg = parent->bref.key + |
| 1967 | ((hammer2_key_t)1 << parent->bref.keybits); |
| 1968 | if (key_beg == 0 || key_beg > key_end) |
| 1969 | return (NULL); |
| 1970 | parent = hammer2_chain_getparent(parentp, how_maybe); |
| 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * And execute |
| 1975 | */ |
| 1976 | return (hammer2_chain_lookup(parentp, key_nextp, |
| 1977 | key_beg, key_end, |
| 1978 | cache_indexp, flags)); |
| 1979 | } |
| 1980 | |
| 1981 | /* |
| 1982 | * The raw scan function is similar to lookup/next but does not seek to a key. |
| 1983 | * Blockrefs are iterated via first_chain = (parent, NULL) and |
| 1984 | * next_chain = (parent, chain). |
| 1985 | * |
| 1986 | * The passed-in parent must be locked and its data resolved. The returned |
| 1987 | * chain will be locked. Pass chain == NULL to acquire the first sub-chain |
| 1988 | * under parent and then iterate with the passed-in chain (which this |
| 1989 | * function will unlock). |
| 1990 | */ |
| 1991 | hammer2_chain_t * |
| 1992 | hammer2_chain_scan(hammer2_chain_t *parent, hammer2_chain_t *chain, |
| 1993 | int *cache_indexp, int flags) |
| 1994 | { |
| 1995 | hammer2_dev_t *hmp; |
| 1996 | hammer2_blockref_t *base; |
| 1997 | hammer2_blockref_t *bref; |
| 1998 | hammer2_blockref_t bcopy; |
| 1999 | hammer2_key_t key; |
| 2000 | hammer2_key_t next_key; |
| 2001 | int count = 0; |
| 2002 | int how_always = HAMMER2_RESOLVE_ALWAYS; |
| 2003 | int how_maybe = HAMMER2_RESOLVE_MAYBE; |
| 2004 | int how; |
| 2005 | int generation; |
| 2006 | int maxloops = 300000; |
| 2007 | |
| 2008 | hmp = parent->hmp; |
| 2009 | |
| 2010 | /* |
| 2011 | * Scan flags borrowed from lookup. |
| 2012 | */ |
| 2013 | if (flags & HAMMER2_LOOKUP_ALWAYS) { |
| 2014 | how_maybe = how_always; |
| 2015 | how = HAMMER2_RESOLVE_ALWAYS; |
| 2016 | } else if (flags & (HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NOLOCK)) { |
| 2017 | how = HAMMER2_RESOLVE_NEVER; |
| 2018 | } else { |
| 2019 | how = HAMMER2_RESOLVE_MAYBE; |
| 2020 | } |
| 2021 | if (flags & HAMMER2_LOOKUP_SHARED) { |
| 2022 | how_maybe |= HAMMER2_RESOLVE_SHARED; |
| 2023 | how_always |= HAMMER2_RESOLVE_SHARED; |
| 2024 | how |= HAMMER2_RESOLVE_SHARED; |
| 2025 | } |
| 2026 | |
| 2027 | /* |
| 2028 | * Calculate key to locate first/next element, unlocking the previous |
| 2029 | * element as we go. Be careful, the key calculation can overflow. |
| 2030 | */ |
| 2031 | if (chain) { |
| 2032 | key = chain->bref.key + |
| 2033 | ((hammer2_key_t)1 << chain->bref.keybits); |
| 2034 | hammer2_chain_unlock(chain); |
| 2035 | hammer2_chain_drop(chain); |
| 2036 | chain = NULL; |
| 2037 | if (key == 0) |
| 2038 | goto done; |
| 2039 | } else { |
| 2040 | key = 0; |
| 2041 | } |
| 2042 | |
| 2043 | again: |
| 2044 | KKASSERT(parent->error == 0); /* XXX case not handled yet */ |
| 2045 | if (--maxloops == 0) |
| 2046 | panic("hammer2_chain_scan: maxloops"); |
| 2047 | /* |
| 2048 | * Locate the blockref array. Currently we do a fully associative |
| 2049 | * search through the array. |
| 2050 | */ |
| 2051 | switch(parent->bref.type) { |
| 2052 | case HAMMER2_BREF_TYPE_INODE: |
| 2053 | /* |
| 2054 | * An inode with embedded data has no sub-chains. |
| 2055 | */ |
| 2056 | if (parent->data->ipdata.meta.op_flags & |
| 2057 | HAMMER2_OPFLAG_DIRECTDATA) { |
| 2058 | goto done; |
| 2059 | } |
| 2060 | base = &parent->data->ipdata.u.blockset.blockref[0]; |
| 2061 | count = HAMMER2_SET_COUNT; |
| 2062 | break; |
| 2063 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 2064 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 2065 | /* |
| 2066 | * Optimize indirect blocks in the INITIAL state to avoid |
| 2067 | * I/O. |
| 2068 | */ |
| 2069 | if (parent->flags & HAMMER2_CHAIN_INITIAL) { |
| 2070 | base = NULL; |
| 2071 | } else { |
| 2072 | if (parent->data == NULL) |
| 2073 | panic("parent->data is NULL"); |
| 2074 | base = &parent->data->npdata[0]; |
| 2075 | } |
| 2076 | count = parent->bytes / sizeof(hammer2_blockref_t); |
| 2077 | break; |
| 2078 | case HAMMER2_BREF_TYPE_VOLUME: |
| 2079 | base = &hmp->voldata.sroot_blockset.blockref[0]; |
| 2080 | count = HAMMER2_SET_COUNT; |
| 2081 | break; |
| 2082 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 2083 | base = &hmp->voldata.freemap_blockset.blockref[0]; |
| 2084 | count = HAMMER2_SET_COUNT; |
| 2085 | break; |
| 2086 | default: |
| 2087 | panic("hammer2_chain_lookup: unrecognized blockref type: %d", |
| 2088 | parent->bref.type); |
| 2089 | base = NULL; /* safety */ |
| 2090 | count = 0; /* safety */ |
| 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * Merged scan to find next candidate. |
| 2095 | * |
| 2096 | * hammer2_base_*() functions require the parent->core.live_* fields |
| 2097 | * to be synchronized. |
| 2098 | * |
| 2099 | * We need to hold the spinlock to access the block array and RB tree |
| 2100 | * and to interlock chain creation. |
| 2101 | */ |
| 2102 | if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) |
| 2103 | hammer2_chain_countbrefs(parent, base, count); |
| 2104 | |
| 2105 | next_key = 0; |
| 2106 | hammer2_spin_ex(&parent->core.spin); |
| 2107 | chain = hammer2_combined_find(parent, base, count, |
| 2108 | cache_indexp, &next_key, |
| 2109 | key, HAMMER2_KEY_MAX, |
| 2110 | &bref); |
| 2111 | generation = parent->core.generation; |
| 2112 | |
| 2113 | /* |
| 2114 | * Exhausted parent chain, we're done. |
| 2115 | */ |
| 2116 | if (bref == NULL) { |
| 2117 | hammer2_spin_unex(&parent->core.spin); |
| 2118 | KKASSERT(chain == NULL); |
| 2119 | goto done; |
| 2120 | } |
| 2121 | |
| 2122 | /* |
| 2123 | * Selected from blockref or in-memory chain. |
| 2124 | */ |
| 2125 | if (chain == NULL) { |
| 2126 | bcopy = *bref; |
| 2127 | hammer2_spin_unex(&parent->core.spin); |
| 2128 | chain = hammer2_chain_get(parent, generation, &bcopy); |
| 2129 | if (chain == NULL) { |
| 2130 | kprintf("retry scan parent %p keys %016jx\n", |
| 2131 | parent, key); |
| 2132 | goto again; |
| 2133 | } |
| 2134 | if (bcmp(&bcopy, bref, sizeof(bcopy))) { |
| 2135 | hammer2_chain_drop(chain); |
| 2136 | chain = NULL; |
| 2137 | goto again; |
| 2138 | } |
| 2139 | } else { |
| 2140 | hammer2_chain_ref(chain); |
| 2141 | hammer2_spin_unex(&parent->core.spin); |
| 2142 | } |
| 2143 | |
| 2144 | /* |
| 2145 | * chain is referenced but not locked. We must lock the chain |
| 2146 | * to obtain definitive DUPLICATED/DELETED state |
| 2147 | */ |
| 2148 | hammer2_chain_lock(chain, how); |
| 2149 | |
| 2150 | /* |
| 2151 | * Skip deleted chains (XXX cache 'i' end-of-block-array? XXX) |
| 2152 | * |
| 2153 | * NOTE: chain's key range is not relevant as there might be |
| 2154 | * one-offs within the range that are not deleted. |
| 2155 | * |
| 2156 | * NOTE: XXX this could create problems with scans used in |
| 2157 | * situations other than mount-time recovery. |
| 2158 | * |
| 2159 | * NOTE: Lookups can race delete-duplicate because |
| 2160 | * delete-duplicate does not lock the parent's core |
| 2161 | * (they just use the spinlock on the core). We must |
| 2162 | * check for races by comparing the DUPLICATED flag before |
| 2163 | * releasing the spinlock with the flag after locking the |
| 2164 | * chain. |
| 2165 | */ |
| 2166 | if (chain->flags & HAMMER2_CHAIN_DELETED) { |
| 2167 | hammer2_chain_unlock(chain); |
| 2168 | hammer2_chain_drop(chain); |
| 2169 | chain = NULL; |
| 2170 | |
| 2171 | key = next_key; |
| 2172 | if (key == 0) |
| 2173 | goto done; |
| 2174 | goto again; |
| 2175 | } |
| 2176 | |
| 2177 | done: |
| 2178 | /* |
| 2179 | * All done, return the chain or NULL |
| 2180 | */ |
| 2181 | return (chain); |
| 2182 | } |
| 2183 | |
| 2184 | /* |
| 2185 | * Create and return a new hammer2 system memory structure of the specified |
| 2186 | * key, type and size and insert it under (*parentp). This is a full |
| 2187 | * insertion, based on the supplied key/keybits, and may involve creating |
| 2188 | * indirect blocks and moving other chains around via delete/duplicate. |
| 2189 | * |
| 2190 | * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (*parentp) TO THE INSERTION |
| 2191 | * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING |
| 2192 | * FULL. This typically means that the caller is creating the chain after |
| 2193 | * doing a hammer2_chain_lookup(). |
| 2194 | * |
| 2195 | * (*parentp) must be exclusive locked and may be replaced on return |
| 2196 | * depending on how much work the function had to do. |
| 2197 | * |
| 2198 | * (*parentp) must not be errored or this function will assert. |
| 2199 | * |
| 2200 | * (*chainp) usually starts out NULL and returns the newly created chain, |
| 2201 | * but if the caller desires the caller may allocate a disconnected chain |
| 2202 | * and pass it in instead. |
| 2203 | * |
| 2204 | * This function should NOT be used to insert INDIRECT blocks. It is |
| 2205 | * typically used to create/insert inodes and data blocks. |
| 2206 | * |
| 2207 | * Caller must pass-in an exclusively locked parent the new chain is to |
| 2208 | * be inserted under, and optionally pass-in a disconnected, exclusively |
| 2209 | * locked chain to insert (else we create a new chain). The function will |
| 2210 | * adjust (*parentp) as necessary, create or connect the chain, and |
| 2211 | * return an exclusively locked chain in *chainp. |
| 2212 | * |
| 2213 | * When creating a PFSROOT inode under the super-root, pmp is typically NULL |
| 2214 | * and will be reassigned. |
| 2215 | */ |
| 2216 | int |
| 2217 | hammer2_chain_create(hammer2_chain_t **parentp, |
| 2218 | hammer2_chain_t **chainp, hammer2_pfs_t *pmp, |
| 2219 | hammer2_key_t key, int keybits, int type, size_t bytes, |
| 2220 | int flags) |
| 2221 | { |
| 2222 | hammer2_dev_t *hmp; |
| 2223 | hammer2_chain_t *chain; |
| 2224 | hammer2_chain_t *parent; |
| 2225 | hammer2_blockref_t *base; |
| 2226 | hammer2_blockref_t dummy; |
| 2227 | int allocated = 0; |
| 2228 | int error = 0; |
| 2229 | int count; |
| 2230 | int maxloops = 300000; |
| 2231 | |
| 2232 | /* |
| 2233 | * Topology may be crossing a PFS boundary. |
| 2234 | */ |
| 2235 | parent = *parentp; |
| 2236 | KKASSERT(hammer2_mtx_owned(&parent->lock)); |
| 2237 | KKASSERT(parent->error == 0); |
| 2238 | hmp = parent->hmp; |
| 2239 | chain = *chainp; |
| 2240 | |
| 2241 | if (chain == NULL) { |
| 2242 | /* |
| 2243 | * First allocate media space and construct the dummy bref, |
| 2244 | * then allocate the in-memory chain structure. Set the |
| 2245 | * INITIAL flag for fresh chains which do not have embedded |
| 2246 | * data. |
| 2247 | */ |
| 2248 | bzero(&dummy, sizeof(dummy)); |
| 2249 | dummy.type = type; |
| 2250 | dummy.key = key; |
| 2251 | dummy.keybits = keybits; |
| 2252 | dummy.data_off = hammer2_getradix(bytes); |
| 2253 | dummy.methods = parent->bref.methods; |
| 2254 | chain = hammer2_chain_alloc(hmp, pmp, &dummy); |
| 2255 | |
| 2256 | /* |
| 2257 | * Lock the chain manually, chain_lock will load the chain |
| 2258 | * which we do NOT want to do. (note: chain->refs is set |
| 2259 | * to 1 by chain_alloc() for us, but lockcnt is not). |
| 2260 | */ |
| 2261 | chain->lockcnt = 1; |
| 2262 | hammer2_mtx_ex(&chain->lock); |
| 2263 | allocated = 1; |
| 2264 | |
| 2265 | /* |
| 2266 | * Set INITIAL to optimize I/O. The flag will generally be |
| 2267 | * processed when we call hammer2_chain_modify(). |
| 2268 | * |
| 2269 | * Recalculate bytes to reflect the actual media block |
| 2270 | * allocation. |
| 2271 | */ |
| 2272 | bytes = (hammer2_off_t)1 << |
| 2273 | (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX); |
| 2274 | chain->bytes = bytes; |
| 2275 | |
| 2276 | switch(type) { |
| 2277 | case HAMMER2_BREF_TYPE_VOLUME: |
| 2278 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 2279 | panic("hammer2_chain_create: called with volume type"); |
| 2280 | break; |
| 2281 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 2282 | panic("hammer2_chain_create: cannot be used to" |
| 2283 | "create indirect block"); |
| 2284 | break; |
| 2285 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 2286 | panic("hammer2_chain_create: cannot be used to" |
| 2287 | "create freemap root or node"); |
| 2288 | break; |
| 2289 | case HAMMER2_BREF_TYPE_FREEMAP_LEAF: |
| 2290 | KKASSERT(bytes == sizeof(chain->data->bmdata)); |
| 2291 | /* fall through */ |
| 2292 | case HAMMER2_BREF_TYPE_INODE: |
| 2293 | case HAMMER2_BREF_TYPE_DATA: |
| 2294 | default: |
| 2295 | /* |
| 2296 | * leave chain->data NULL, set INITIAL |
| 2297 | */ |
| 2298 | KKASSERT(chain->data == NULL); |
| 2299 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_INITIAL); |
| 2300 | break; |
| 2301 | } |
| 2302 | } else { |
| 2303 | /* |
| 2304 | * We are reattaching a previously deleted chain, possibly |
| 2305 | * under a new parent and possibly with a new key/keybits. |
| 2306 | * The chain does not have to be in a modified state. The |
| 2307 | * UPDATE flag will be set later on in this routine. |
| 2308 | * |
| 2309 | * Do NOT mess with the current state of the INITIAL flag. |
| 2310 | */ |
| 2311 | chain->bref.key = key; |
| 2312 | chain->bref.keybits = keybits; |
| 2313 | if (chain->flags & HAMMER2_CHAIN_DELETED) |
| 2314 | atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DELETED); |
| 2315 | KKASSERT(chain->parent == NULL); |
| 2316 | } |
| 2317 | if (flags & HAMMER2_INSERT_PFSROOT) |
| 2318 | chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT; |
| 2319 | else |
| 2320 | chain->bref.flags &= ~HAMMER2_BREF_FLAG_PFSROOT; |
| 2321 | |
| 2322 | /* |
| 2323 | * Calculate how many entries we have in the blockref array and |
| 2324 | * determine if an indirect block is required. |
| 2325 | */ |
| 2326 | again: |
| 2327 | if (--maxloops == 0) |
| 2328 | panic("hammer2_chain_create: maxloops"); |
| 2329 | |
| 2330 | switch(parent->bref.type) { |
| 2331 | case HAMMER2_BREF_TYPE_INODE: |
| 2332 | KKASSERT((parent->data->ipdata.meta.op_flags & |
| 2333 | HAMMER2_OPFLAG_DIRECTDATA) == 0); |
| 2334 | KKASSERT(parent->data != NULL); |
| 2335 | base = &parent->data->ipdata.u.blockset.blockref[0]; |
| 2336 | count = HAMMER2_SET_COUNT; |
| 2337 | break; |
| 2338 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 2339 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 2340 | if (parent->flags & HAMMER2_CHAIN_INITIAL) |
| 2341 | base = NULL; |
| 2342 | else |
| 2343 | base = &parent->data->npdata[0]; |
| 2344 | count = parent->bytes / sizeof(hammer2_blockref_t); |
| 2345 | break; |
| 2346 | case HAMMER2_BREF_TYPE_VOLUME: |
| 2347 | KKASSERT(parent->data != NULL); |
| 2348 | base = &hmp->voldata.sroot_blockset.blockref[0]; |
| 2349 | count = HAMMER2_SET_COUNT; |
| 2350 | break; |
| 2351 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 2352 | KKASSERT(parent->data != NULL); |
| 2353 | base = &hmp->voldata.freemap_blockset.blockref[0]; |
| 2354 | count = HAMMER2_SET_COUNT; |
| 2355 | break; |
| 2356 | default: |
| 2357 | panic("hammer2_chain_create: unrecognized blockref type: %d", |
| 2358 | parent->bref.type); |
| 2359 | base = NULL; |
| 2360 | count = 0; |
| 2361 | break; |
| 2362 | } |
| 2363 | |
| 2364 | /* |
| 2365 | * Make sure we've counted the brefs |
| 2366 | */ |
| 2367 | if ((parent->flags & HAMMER2_CHAIN_COUNTEDBREFS) == 0) |
| 2368 | hammer2_chain_countbrefs(parent, base, count); |
| 2369 | |
| 2370 | KKASSERT(parent->core.live_count >= 0 && |
| 2371 | parent->core.live_count <= count); |
| 2372 | |
| 2373 | /* |
| 2374 | * If no free blockref could be found we must create an indirect |
| 2375 | * block and move a number of blockrefs into it. With the parent |
| 2376 | * locked we can safely lock each child in order to delete+duplicate |
| 2377 | * it without causing a deadlock. |
| 2378 | * |
| 2379 | * This may return the new indirect block or the old parent depending |
| 2380 | * on where the key falls. NULL is returned on error. |
| 2381 | */ |
| 2382 | if (parent->core.live_count == count) { |
| 2383 | hammer2_chain_t *nparent; |
| 2384 | |
| 2385 | nparent = hammer2_chain_create_indirect(parent, key, keybits, |
| 2386 | type, &error); |
| 2387 | if (nparent == NULL) { |
| 2388 | if (allocated) |
| 2389 | hammer2_chain_drop(chain); |
| 2390 | chain = NULL; |
| 2391 | goto done; |
| 2392 | } |
| 2393 | if (parent != nparent) { |
| 2394 | hammer2_chain_unlock(parent); |
| 2395 | hammer2_chain_drop(parent); |
| 2396 | parent = *parentp = nparent; |
| 2397 | } |
| 2398 | goto again; |
| 2399 | } |
| 2400 | |
| 2401 | /* |
| 2402 | * Link the chain into its parent. |
| 2403 | */ |
| 2404 | if (chain->parent != NULL) |
| 2405 | panic("hammer2: hammer2_chain_create: chain already connected"); |
| 2406 | KKASSERT(chain->parent == NULL); |
| 2407 | hammer2_chain_insert(parent, chain, |
| 2408 | HAMMER2_CHAIN_INSERT_SPIN | |
| 2409 | HAMMER2_CHAIN_INSERT_LIVE, |
| 2410 | 0); |
| 2411 | |
| 2412 | if (allocated) { |
| 2413 | /* |
| 2414 | * Mark the newly created chain modified. This will cause |
| 2415 | * UPDATE to be set and process the INITIAL flag. |
| 2416 | * |
| 2417 | * Device buffers are not instantiated for DATA elements |
| 2418 | * as these are handled by logical buffers. |
| 2419 | * |
| 2420 | * Indirect and freemap node indirect blocks are handled |
| 2421 | * by hammer2_chain_create_indirect() and not by this |
| 2422 | * function. |
| 2423 | * |
| 2424 | * Data for all other bref types is expected to be |
| 2425 | * instantiated (INODE, LEAF). |
| 2426 | */ |
| 2427 | switch(chain->bref.type) { |
| 2428 | case HAMMER2_BREF_TYPE_DATA: |
| 2429 | case HAMMER2_BREF_TYPE_FREEMAP_LEAF: |
| 2430 | case HAMMER2_BREF_TYPE_INODE: |
| 2431 | hammer2_chain_modify(chain, HAMMER2_MODIFY_OPTDATA); |
| 2432 | break; |
| 2433 | default: |
| 2434 | /* |
| 2435 | * Remaining types are not supported by this function. |
| 2436 | * In particular, INDIRECT and LEAF_NODE types are |
| 2437 | * handled by create_indirect(). |
| 2438 | */ |
| 2439 | panic("hammer2_chain_create: bad type: %d", |
| 2440 | chain->bref.type); |
| 2441 | /* NOT REACHED */ |
| 2442 | break; |
| 2443 | } |
| 2444 | } else { |
| 2445 | /* |
| 2446 | * When reconnecting a chain we must set UPDATE and |
| 2447 | * setflush so the flush recognizes that it must update |
| 2448 | * the bref in the parent. |
| 2449 | */ |
| 2450 | if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0) { |
| 2451 | hammer2_chain_ref(chain); |
| 2452 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE); |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | /* |
| 2457 | * We must setflush(parent) to ensure that it recurses through to |
| 2458 | * chain. setflush(chain) might not work because ONFLUSH is possibly |
| 2459 | * already set in the chain (so it won't recurse up to set it in the |
| 2460 | * parent). |
| 2461 | */ |
| 2462 | hammer2_chain_setflush(parent); |
| 2463 | |
| 2464 | done: |
| 2465 | *chainp = chain; |
| 2466 | |
| 2467 | return (error); |
| 2468 | } |
| 2469 | |
| 2470 | /* |
| 2471 | * Move the chain from its old parent to a new parent. The chain must have |
| 2472 | * already been deleted or already disconnected (or never associated) with |
| 2473 | * a parent. The chain is reassociated with the new parent and the deleted |
| 2474 | * flag will be cleared (no longer deleted). The chain's modification state |
| 2475 | * is not altered. |
| 2476 | * |
| 2477 | * THE CALLER MUST HAVE ALREADY PROPERLY SEEKED (parent) TO THE INSERTION |
| 2478 | * POINT SANS ANY REQUIRED INDIRECT BLOCK CREATIONS DUE TO THE ARRAY BEING |
| 2479 | * FULL. This typically means that the caller is creating the chain after |
| 2480 | * doing a hammer2_chain_lookup(). |
| 2481 | * |
| 2482 | * A non-NULL bref is typically passed when key and keybits must be overridden. |
| 2483 | * Note that hammer2_cluster_duplicate() *ONLY* uses the key and keybits fields |
| 2484 | * from a passed-in bref and uses the old chain's bref for everything else. |
| 2485 | * |
| 2486 | * Neither (parent) or (chain) can be errored. |
| 2487 | * |
| 2488 | * If (parent) is non-NULL then the new duplicated chain is inserted under |
| 2489 | * the parent. |
| 2490 | * |
| 2491 | * If (parent) is NULL then the newly duplicated chain is not inserted |
| 2492 | * anywhere, similar to if it had just been chain_alloc()'d (suitable for |
| 2493 | * passing into hammer2_chain_create() after this function returns). |
| 2494 | * |
| 2495 | * WARNING! This function calls create which means it can insert indirect |
| 2496 | * blocks. This can cause other unrelated chains in the parent to |
| 2497 | * be moved to a newly inserted indirect block in addition to the |
| 2498 | * specific chain. |
| 2499 | */ |
| 2500 | void |
| 2501 | hammer2_chain_rename(hammer2_blockref_t *bref, |
| 2502 | hammer2_chain_t **parentp, hammer2_chain_t *chain, |
| 2503 | int flags) |
| 2504 | { |
| 2505 | hammer2_dev_t *hmp; |
| 2506 | hammer2_chain_t *parent; |
| 2507 | size_t bytes; |
| 2508 | |
| 2509 | /* |
| 2510 | * WARNING! We should never resolve DATA to device buffers |
| 2511 | * (XXX allow it if the caller did?), and since |
| 2512 | * we currently do not have the logical buffer cache |
| 2513 | * buffer in-hand to fix its cached physical offset |
| 2514 | * we also force the modify code to not COW it. XXX |
| 2515 | */ |
| 2516 | hmp = chain->hmp; |
| 2517 | KKASSERT(chain->parent == NULL); |
| 2518 | KKASSERT(chain->error == 0); |
| 2519 | |
| 2520 | /* |
| 2521 | * Now create a duplicate of the chain structure, associating |
| 2522 | * it with the same core, making it the same size, pointing it |
| 2523 | * to the same bref (the same media block). |
| 2524 | */ |
| 2525 | if (bref == NULL) |
| 2526 | bref = &chain->bref; |
| 2527 | bytes = (hammer2_off_t)1 << |
| 2528 | (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX); |
| 2529 | |
| 2530 | /* |
| 2531 | * If parent is not NULL the duplicated chain will be entered under |
| 2532 | * the parent and the UPDATE bit set to tell flush to update |
| 2533 | * the blockref. |
| 2534 | * |
| 2535 | * We must setflush(parent) to ensure that it recurses through to |
| 2536 | * chain. setflush(chain) might not work because ONFLUSH is possibly |
| 2537 | * already set in the chain (so it won't recurse up to set it in the |
| 2538 | * parent). |
| 2539 | * |
| 2540 | * Having both chains locked is extremely important for atomicy. |
| 2541 | */ |
| 2542 | if (parentp && (parent = *parentp) != NULL) { |
| 2543 | KKASSERT(hammer2_mtx_owned(&parent->lock)); |
| 2544 | KKASSERT(parent->refs > 0); |
| 2545 | KKASSERT(parent->error == 0); |
| 2546 | |
| 2547 | hammer2_chain_create(parentp, &chain, chain->pmp, |
| 2548 | bref->key, bref->keybits, bref->type, |
| 2549 | chain->bytes, flags); |
| 2550 | KKASSERT(chain->flags & HAMMER2_CHAIN_UPDATE); |
| 2551 | hammer2_chain_setflush(*parentp); |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | /* |
| 2556 | * Helper function for deleting chains. |
| 2557 | * |
| 2558 | * The chain is removed from the live view (the RBTREE) as well as the parent's |
| 2559 | * blockmap. Both chain and its parent must be locked. |
| 2560 | * |
| 2561 | * parent may not be errored. chain can be errored. |
| 2562 | */ |
| 2563 | static void |
| 2564 | _hammer2_chain_delete_helper(hammer2_chain_t *parent, hammer2_chain_t *chain, |
| 2565 | int flags) |
| 2566 | { |
| 2567 | hammer2_dev_t *hmp; |
| 2568 | |
| 2569 | KKASSERT((chain->flags & (HAMMER2_CHAIN_DELETED | |
| 2570 | HAMMER2_CHAIN_FICTITIOUS)) == 0); |
| 2571 | hmp = chain->hmp; |
| 2572 | |
| 2573 | if (chain->flags & HAMMER2_CHAIN_BMAPPED) { |
| 2574 | /* |
| 2575 | * Chain is blockmapped, so there must be a parent. |
| 2576 | * Atomically remove the chain from the parent and remove |
| 2577 | * the blockmap entry. |
| 2578 | */ |
| 2579 | hammer2_blockref_t *base; |
| 2580 | int count; |
| 2581 | |
| 2582 | KKASSERT(parent != NULL); |
| 2583 | KKASSERT(parent->error == 0); |
| 2584 | KKASSERT((parent->flags & HAMMER2_CHAIN_INITIAL) == 0); |
| 2585 | hammer2_chain_modify(parent, HAMMER2_MODIFY_OPTDATA); |
| 2586 | |
| 2587 | /* |
| 2588 | * Calculate blockmap pointer |
| 2589 | */ |
| 2590 | KKASSERT(chain->flags & HAMMER2_CHAIN_ONRBTREE); |
| 2591 | hammer2_spin_ex(&parent->core.spin); |
| 2592 | |
| 2593 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED); |
| 2594 | atomic_add_int(&parent->core.live_count, -1); |
| 2595 | ++parent->core.generation; |
| 2596 | RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain); |
| 2597 | atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE); |
| 2598 | --parent->core.chain_count; |
| 2599 | chain->parent = NULL; |
| 2600 | |
| 2601 | switch(parent->bref.type) { |
| 2602 | case HAMMER2_BREF_TYPE_INODE: |
| 2603 | /* |
| 2604 | * Access the inode's block array. However, there |
| 2605 | * is no block array if the inode is flagged |
| 2606 | * DIRECTDATA. The DIRECTDATA case typicaly only |
| 2607 | * occurs when a hardlink has been shifted up the |
| 2608 | * tree and the original inode gets replaced with |
| 2609 | * an OBJTYPE_HARDLINK placeholding inode. |
| 2610 | */ |
| 2611 | if (parent->data && |
| 2612 | (parent->data->ipdata.meta.op_flags & |
| 2613 | HAMMER2_OPFLAG_DIRECTDATA) == 0) { |
| 2614 | base = |
| 2615 | &parent->data->ipdata.u.blockset.blockref[0]; |
| 2616 | } else { |
| 2617 | base = NULL; |
| 2618 | } |
| 2619 | count = HAMMER2_SET_COUNT; |
| 2620 | break; |
| 2621 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 2622 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 2623 | if (parent->data) |
| 2624 | base = &parent->data->npdata[0]; |
| 2625 | else |
| 2626 | base = NULL; |
| 2627 | count = parent->bytes / sizeof(hammer2_blockref_t); |
| 2628 | break; |
| 2629 | case HAMMER2_BREF_TYPE_VOLUME: |
| 2630 | base = &hmp->voldata.sroot_blockset.blockref[0]; |
| 2631 | count = HAMMER2_SET_COUNT; |
| 2632 | break; |
| 2633 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 2634 | base = &parent->data->npdata[0]; |
| 2635 | count = HAMMER2_SET_COUNT; |
| 2636 | break; |
| 2637 | default: |
| 2638 | base = NULL; |
| 2639 | count = 0; |
| 2640 | panic("hammer2_flush_pass2: " |
| 2641 | "unrecognized blockref type: %d", |
| 2642 | parent->bref.type); |
| 2643 | } |
| 2644 | |
| 2645 | /* |
| 2646 | * delete blockmapped chain from its parent. |
| 2647 | * |
| 2648 | * The parent is not affected by any statistics in chain |
| 2649 | * which are pending synchronization. That is, there is |
| 2650 | * nothing to undo in the parent since they have not yet |
| 2651 | * been incorporated into the parent. |
| 2652 | * |
| 2653 | * The parent is affected by statistics stored in inodes. |
| 2654 | * Those have already been synchronized, so they must be |
| 2655 | * undone. XXX split update possible w/delete in middle? |
| 2656 | */ |
| 2657 | if (base) { |
| 2658 | int cache_index = -1; |
| 2659 | hammer2_base_delete(parent, base, count, |
| 2660 | &cache_index, chain); |
| 2661 | } |
| 2662 | hammer2_spin_unex(&parent->core.spin); |
| 2663 | } else if (chain->flags & HAMMER2_CHAIN_ONRBTREE) { |
| 2664 | /* |
| 2665 | * Chain is not blockmapped but a parent is present. |
| 2666 | * Atomically remove the chain from the parent. There is |
| 2667 | * no blockmap entry to remove. |
| 2668 | * |
| 2669 | * Because chain was associated with a parent but not |
| 2670 | * synchronized, the chain's *_count_up fields contain |
| 2671 | * inode adjustment statistics which must be undone. |
| 2672 | */ |
| 2673 | hammer2_spin_ex(&parent->core.spin); |
| 2674 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED); |
| 2675 | atomic_add_int(&parent->core.live_count, -1); |
| 2676 | ++parent->core.generation; |
| 2677 | RB_REMOVE(hammer2_chain_tree, &parent->core.rbtree, chain); |
| 2678 | atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE); |
| 2679 | --parent->core.chain_count; |
| 2680 | chain->parent = NULL; |
| 2681 | hammer2_spin_unex(&parent->core.spin); |
| 2682 | } else { |
| 2683 | /* |
| 2684 | * Chain is not blockmapped and has no parent. This |
| 2685 | * is a degenerate case. |
| 2686 | */ |
| 2687 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELETED); |
| 2688 | } |
| 2689 | } |
| 2690 | |
| 2691 | /* |
| 2692 | * Create an indirect block that covers one or more of the elements in the |
| 2693 | * current parent. Either returns the existing parent with no locking or |
| 2694 | * ref changes or returns the new indirect block locked and referenced |
| 2695 | * and leaving the original parent lock/ref intact as well. |
| 2696 | * |
| 2697 | * If an error occurs, NULL is returned and *errorp is set to the error. |
| 2698 | * |
| 2699 | * The returned chain depends on where the specified key falls. |
| 2700 | * |
| 2701 | * The key/keybits for the indirect mode only needs to follow three rules: |
| 2702 | * |
| 2703 | * (1) That all elements underneath it fit within its key space and |
| 2704 | * |
| 2705 | * (2) That all elements outside it are outside its key space. |
| 2706 | * |
| 2707 | * (3) When creating the new indirect block any elements in the current |
| 2708 | * parent that fit within the new indirect block's keyspace must be |
| 2709 | * moved into the new indirect block. |
| 2710 | * |
| 2711 | * (4) The keyspace chosen for the inserted indirect block CAN cover a wider |
| 2712 | * keyspace the the current parent, but lookup/iteration rules will |
| 2713 | * ensure (and must ensure) that rule (2) for all parents leading up |
| 2714 | * to the nearest inode or the root volume header is adhered to. This |
| 2715 | * is accomplished by always recursing through matching keyspaces in |
| 2716 | * the hammer2_chain_lookup() and hammer2_chain_next() API. |
| 2717 | * |
| 2718 | * The current implementation calculates the current worst-case keyspace by |
| 2719 | * iterating the current parent and then divides it into two halves, choosing |
| 2720 | * whichever half has the most elements (not necessarily the half containing |
| 2721 | * the requested key). |
| 2722 | * |
| 2723 | * We can also opt to use the half with the least number of elements. This |
| 2724 | * causes lower-numbered keys (aka logical file offsets) to recurse through |
| 2725 | * fewer indirect blocks and higher-numbered keys to recurse through more. |
| 2726 | * This also has the risk of not moving enough elements to the new indirect |
| 2727 | * block and being forced to create several indirect blocks before the element |
| 2728 | * can be inserted. |
| 2729 | * |
| 2730 | * Must be called with an exclusively locked parent. |
| 2731 | */ |
| 2732 | static int hammer2_chain_indkey_freemap(hammer2_chain_t *parent, |
| 2733 | hammer2_key_t *keyp, int keybits, |
| 2734 | hammer2_blockref_t *base, int count); |
| 2735 | static int hammer2_chain_indkey_normal(hammer2_chain_t *parent, |
| 2736 | hammer2_key_t *keyp, int keybits, |
| 2737 | hammer2_blockref_t *base, int count); |
| 2738 | static |
| 2739 | hammer2_chain_t * |
| 2740 | hammer2_chain_create_indirect(hammer2_chain_t *parent, |
| 2741 | hammer2_key_t create_key, int create_bits, |
| 2742 | int for_type, int *errorp) |
| 2743 | { |
| 2744 | hammer2_dev_t *hmp; |
| 2745 | hammer2_blockref_t *base; |
| 2746 | hammer2_blockref_t *bref; |
| 2747 | hammer2_blockref_t bcopy; |
| 2748 | hammer2_chain_t *chain; |
| 2749 | hammer2_chain_t *ichain; |
| 2750 | hammer2_chain_t dummy; |
| 2751 | hammer2_key_t key = create_key; |
| 2752 | hammer2_key_t key_beg; |
| 2753 | hammer2_key_t key_end; |
| 2754 | hammer2_key_t key_next; |
| 2755 | int keybits = create_bits; |
| 2756 | int count; |
| 2757 | int nbytes; |
| 2758 | int cache_index; |
| 2759 | int loops; |
| 2760 | int reason; |
| 2761 | int generation; |
| 2762 | int maxloops = 300000; |
| 2763 | |
| 2764 | /* |
| 2765 | * Calculate the base blockref pointer or NULL if the chain |
| 2766 | * is known to be empty. We need to calculate the array count |
| 2767 | * for RB lookups either way. |
| 2768 | */ |
| 2769 | hmp = parent->hmp; |
| 2770 | *errorp = 0; |
| 2771 | KKASSERT(hammer2_mtx_owned(&parent->lock)); |
| 2772 | |
| 2773 | /*hammer2_chain_modify(&parent, HAMMER2_MODIFY_OPTDATA);*/ |
| 2774 | if (parent->flags & HAMMER2_CHAIN_INITIAL) { |
| 2775 | base = NULL; |
| 2776 | |
| 2777 | switch(parent->bref.type) { |
| 2778 | case HAMMER2_BREF_TYPE_INODE: |
| 2779 | count = HAMMER2_SET_COUNT; |
| 2780 | break; |
| 2781 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 2782 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 2783 | count = parent->bytes / sizeof(hammer2_blockref_t); |
| 2784 | break; |
| 2785 | case HAMMER2_BREF_TYPE_VOLUME: |
| 2786 | count = HAMMER2_SET_COUNT; |
| 2787 | break; |
| 2788 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 2789 | count = HAMMER2_SET_COUNT; |
| 2790 | break; |
| 2791 | default: |
| 2792 | panic("hammer2_chain_create_indirect: " |
| 2793 | "unrecognized blockref type: %d", |
| 2794 | parent->bref.type); |
| 2795 | count = 0; |
| 2796 | break; |
| 2797 | } |
| 2798 | } else { |
| 2799 | switch(parent->bref.type) { |
| 2800 | case HAMMER2_BREF_TYPE_INODE: |
| 2801 | base = &parent->data->ipdata.u.blockset.blockref[0]; |
| 2802 | count = HAMMER2_SET_COUNT; |
| 2803 | break; |
| 2804 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 2805 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 2806 | base = &parent->data->npdata[0]; |
| 2807 | count = parent->bytes / sizeof(hammer2_blockref_t); |
| 2808 | break; |
| 2809 | case HAMMER2_BREF_TYPE_VOLUME: |
| 2810 | base = &hmp->voldata.sroot_blockset.blockref[0]; |
| 2811 | count = HAMMER2_SET_COUNT; |
| 2812 | break; |
| 2813 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 2814 | base = &hmp->voldata.freemap_blockset.blockref[0]; |
| 2815 | count = HAMMER2_SET_COUNT; |
| 2816 | break; |
| 2817 | default: |
| 2818 | panic("hammer2_chain_create_indirect: " |
| 2819 | "unrecognized blockref type: %d", |
| 2820 | parent->bref.type); |
| 2821 | count = 0; |
| 2822 | break; |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | /* |
| 2827 | * dummy used in later chain allocation (no longer used for lookups). |
| 2828 | */ |
| 2829 | bzero(&dummy, sizeof(dummy)); |
| 2830 | |
| 2831 | /* |
| 2832 | * When creating an indirect block for a freemap node or leaf |
| 2833 | * the key/keybits must be fitted to static radix levels because |
| 2834 | * particular radix levels use particular reserved blocks in the |
| 2835 | * related zone. |
| 2836 | * |
| 2837 | * This routine calculates the key/radix of the indirect block |
| 2838 | * we need to create, and whether it is on the high-side or the |
| 2839 | * low-side. |
| 2840 | */ |
| 2841 | if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE || |
| 2842 | for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) { |
| 2843 | keybits = hammer2_chain_indkey_freemap(parent, &key, keybits, |
| 2844 | base, count); |
| 2845 | } else { |
| 2846 | keybits = hammer2_chain_indkey_normal(parent, &key, keybits, |
| 2847 | base, count); |
| 2848 | } |
| 2849 | |
| 2850 | /* |
| 2851 | * Normalize the key for the radix being represented, keeping the |
| 2852 | * high bits and throwing away the low bits. |
| 2853 | */ |
| 2854 | key &= ~(((hammer2_key_t)1 << keybits) - 1); |
| 2855 | |
| 2856 | /* |
| 2857 | * How big should our new indirect block be? It has to be at least |
| 2858 | * as large as its parent. |
| 2859 | * |
| 2860 | * The freemap uses a specific indirect block size. |
| 2861 | * |
| 2862 | * The first indirect block level down from an inode typically |
| 2863 | * uses LBUFSIZE (16384), else it uses PBUFSIZE (65536). |
| 2864 | */ |
| 2865 | if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE || |
| 2866 | for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) { |
| 2867 | nbytes = HAMMER2_FREEMAP_LEVELN_PSIZE; |
| 2868 | } else if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) { |
| 2869 | nbytes = HAMMER2_IND_BYTES_MIN; |
| 2870 | } else { |
| 2871 | nbytes = HAMMER2_IND_BYTES_MAX; |
| 2872 | } |
| 2873 | if (nbytes < count * sizeof(hammer2_blockref_t)) { |
| 2874 | KKASSERT(for_type != HAMMER2_BREF_TYPE_FREEMAP_NODE && |
| 2875 | for_type != HAMMER2_BREF_TYPE_FREEMAP_LEAF); |
| 2876 | nbytes = count * sizeof(hammer2_blockref_t); |
| 2877 | } |
| 2878 | |
| 2879 | /* |
| 2880 | * Ok, create our new indirect block |
| 2881 | */ |
| 2882 | if (for_type == HAMMER2_BREF_TYPE_FREEMAP_NODE || |
| 2883 | for_type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) { |
| 2884 | dummy.bref.type = HAMMER2_BREF_TYPE_FREEMAP_NODE; |
| 2885 | } else { |
| 2886 | dummy.bref.type = HAMMER2_BREF_TYPE_INDIRECT; |
| 2887 | } |
| 2888 | dummy.bref.key = key; |
| 2889 | dummy.bref.keybits = keybits; |
| 2890 | dummy.bref.data_off = hammer2_getradix(nbytes); |
| 2891 | dummy.bref.methods = parent->bref.methods; |
| 2892 | |
| 2893 | ichain = hammer2_chain_alloc(hmp, parent->pmp, &dummy.bref); |
| 2894 | atomic_set_int(&ichain->flags, HAMMER2_CHAIN_INITIAL); |
| 2895 | hammer2_chain_lock(ichain, HAMMER2_RESOLVE_MAYBE); |
| 2896 | /* ichain has one ref at this point */ |
| 2897 | |
| 2898 | /* |
| 2899 | * We have to mark it modified to allocate its block, but use |
| 2900 | * OPTDATA to allow it to remain in the INITIAL state. Otherwise |
| 2901 | * it won't be acted upon by the flush code. |
| 2902 | */ |
| 2903 | hammer2_chain_modify(ichain, HAMMER2_MODIFY_OPTDATA); |
| 2904 | |
| 2905 | /* |
| 2906 | * Iterate the original parent and move the matching brefs into |
| 2907 | * the new indirect block. |
| 2908 | * |
| 2909 | * XXX handle flushes. |
| 2910 | */ |
| 2911 | key_beg = 0; |
| 2912 | key_end = HAMMER2_KEY_MAX; |
| 2913 | cache_index = 0; |
| 2914 | hammer2_spin_ex(&parent->core.spin); |
| 2915 | loops = 0; |
| 2916 | reason = 0; |
| 2917 | |
| 2918 | for (;;) { |
| 2919 | if (++loops > 100000) { |
| 2920 | hammer2_spin_unex(&parent->core.spin); |
| 2921 | panic("excessive loops r=%d p=%p base/count %p:%d %016jx\n", |
| 2922 | reason, parent, base, count, key_next); |
| 2923 | } |
| 2924 | |
| 2925 | /* |
| 2926 | * NOTE: spinlock stays intact, returned chain (if not NULL) |
| 2927 | * is not referenced or locked which means that we |
| 2928 | * cannot safely check its flagged / deletion status |
| 2929 | * until we lock it. |
| 2930 | */ |
| 2931 | chain = hammer2_combined_find(parent, base, count, |
| 2932 | &cache_index, &key_next, |
| 2933 | key_beg, key_end, |
| 2934 | &bref); |
| 2935 | generation = parent->core.generation; |
| 2936 | if (bref == NULL) |
| 2937 | break; |
| 2938 | key_next = bref->key + ((hammer2_key_t)1 << bref->keybits); |
| 2939 | |
| 2940 | /* |
| 2941 | * Skip keys that are not within the key/radix of the new |
| 2942 | * indirect block. They stay in the parent. |
| 2943 | */ |
| 2944 | if ((~(((hammer2_key_t)1 << keybits) - 1) & |
| 2945 | (key ^ bref->key)) != 0) { |
| 2946 | goto next_key_spinlocked; |
| 2947 | } |
| 2948 | |
| 2949 | /* |
| 2950 | * Load the new indirect block by acquiring the related |
| 2951 | * chains (potentially from media as it might not be |
| 2952 | * in-memory). Then move it to the new parent (ichain) |
| 2953 | * via DELETE-DUPLICATE. |
| 2954 | * |
| 2955 | * chain is referenced but not locked. We must lock the |
| 2956 | * chain to obtain definitive DUPLICATED/DELETED state |
| 2957 | */ |
| 2958 | if (chain) { |
| 2959 | /* |
| 2960 | * Use chain already present in the RBTREE |
| 2961 | */ |
| 2962 | hammer2_chain_ref(chain); |
| 2963 | hammer2_spin_unex(&parent->core.spin); |
| 2964 | hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER); |
| 2965 | } else { |
| 2966 | /* |
| 2967 | * Get chain for blockref element. _get returns NULL |
| 2968 | * on insertion race. |
| 2969 | */ |
| 2970 | bcopy = *bref; |
| 2971 | hammer2_spin_unex(&parent->core.spin); |
| 2972 | chain = hammer2_chain_get(parent, generation, &bcopy); |
| 2973 | if (chain == NULL) { |
| 2974 | reason = 1; |
| 2975 | hammer2_spin_ex(&parent->core.spin); |
| 2976 | continue; |
| 2977 | } |
| 2978 | if (bcmp(&bcopy, bref, sizeof(bcopy))) { |
| 2979 | kprintf("REASON 2\n"); |
| 2980 | reason = 2; |
| 2981 | hammer2_chain_drop(chain); |
| 2982 | hammer2_spin_ex(&parent->core.spin); |
| 2983 | continue; |
| 2984 | } |
| 2985 | hammer2_chain_lock(chain, HAMMER2_RESOLVE_NEVER); |
| 2986 | } |
| 2987 | |
| 2988 | /* |
| 2989 | * This is always live so if the chain has been deleted |
| 2990 | * we raced someone and we have to retry. |
| 2991 | * |
| 2992 | * NOTE: Lookups can race delete-duplicate because |
| 2993 | * delete-duplicate does not lock the parent's core |
| 2994 | * (they just use the spinlock on the core). We must |
| 2995 | * check for races by comparing the DUPLICATED flag before |
| 2996 | * releasing the spinlock with the flag after locking the |
| 2997 | * chain. |
| 2998 | * |
| 2999 | * (note reversed logic for this one) |
| 3000 | */ |
| 3001 | if (chain->flags & HAMMER2_CHAIN_DELETED) { |
| 3002 | hammer2_chain_unlock(chain); |
| 3003 | hammer2_chain_drop(chain); |
| 3004 | goto next_key; |
| 3005 | } |
| 3006 | |
| 3007 | /* |
| 3008 | * Shift the chain to the indirect block. |
| 3009 | * |
| 3010 | * WARNING! No reason for us to load chain data, pass NOSTATS |
| 3011 | * to prevent delete/insert from trying to access |
| 3012 | * inode stats (and thus asserting if there is no |
| 3013 | * chain->data loaded). |
| 3014 | */ |
| 3015 | hammer2_chain_delete(parent, chain, |
| 3016 | HAMMER2_DELETE_NOSTATS); |
| 3017 | hammer2_chain_rename(NULL, &ichain, chain, |
| 3018 | HAMMER2_INSERT_NOSTATS); |
| 3019 | hammer2_chain_unlock(chain); |
| 3020 | hammer2_chain_drop(chain); |
| 3021 | KKASSERT(parent->refs > 0); |
| 3022 | chain = NULL; |
| 3023 | next_key: |
| 3024 | hammer2_spin_ex(&parent->core.spin); |
| 3025 | next_key_spinlocked: |
| 3026 | if (--maxloops == 0) |
| 3027 | panic("hammer2_chain_create_indirect: maxloops"); |
| 3028 | reason = 4; |
| 3029 | if (key_next == 0 || key_next > key_end) |
| 3030 | break; |
| 3031 | key_beg = key_next; |
| 3032 | /* loop */ |
| 3033 | } |
| 3034 | hammer2_spin_unex(&parent->core.spin); |
| 3035 | |
| 3036 | /* |
| 3037 | * Insert the new indirect block into the parent now that we've |
| 3038 | * cleared out some entries in the parent. We calculated a good |
| 3039 | * insertion index in the loop above (ichain->index). |
| 3040 | * |
| 3041 | * We don't have to set UPDATE here because we mark ichain |
| 3042 | * modified down below (so the normal modified -> flush -> set-moved |
| 3043 | * sequence applies). |
| 3044 | * |
| 3045 | * The insertion shouldn't race as this is a completely new block |
| 3046 | * and the parent is locked. |
| 3047 | */ |
| 3048 | KKASSERT((ichain->flags & HAMMER2_CHAIN_ONRBTREE) == 0); |
| 3049 | hammer2_chain_insert(parent, ichain, |
| 3050 | HAMMER2_CHAIN_INSERT_SPIN | |
| 3051 | HAMMER2_CHAIN_INSERT_LIVE, |
| 3052 | 0); |
| 3053 | |
| 3054 | /* |
| 3055 | * Make sure flushes propogate after our manual insertion. |
| 3056 | */ |
| 3057 | hammer2_chain_setflush(ichain); |
| 3058 | hammer2_chain_setflush(parent); |
| 3059 | |
| 3060 | /* |
| 3061 | * Figure out what to return. |
| 3062 | */ |
| 3063 | if (~(((hammer2_key_t)1 << keybits) - 1) & |
| 3064 | (create_key ^ key)) { |
| 3065 | /* |
| 3066 | * Key being created is outside the key range, |
| 3067 | * return the original parent. |
| 3068 | */ |
| 3069 | hammer2_chain_unlock(ichain); |
| 3070 | hammer2_chain_drop(ichain); |
| 3071 | } else { |
| 3072 | /* |
| 3073 | * Otherwise its in the range, return the new parent. |
| 3074 | * (leave both the new and old parent locked). |
| 3075 | */ |
| 3076 | parent = ichain; |
| 3077 | } |
| 3078 | |
| 3079 | return(parent); |
| 3080 | } |
| 3081 | |
| 3082 | /* |
| 3083 | * Calculate the keybits and highside/lowside of the freemap node the |
| 3084 | * caller is creating. |
| 3085 | * |
| 3086 | * This routine will specify the next higher-level freemap key/radix |
| 3087 | * representing the lowest-ordered set. By doing so, eventually all |
| 3088 | * low-ordered sets will be moved one level down. |
| 3089 | * |
| 3090 | * We have to be careful here because the freemap reserves a limited |
| 3091 | * number of blocks for a limited number of levels. So we can't just |
| 3092 | * push indiscriminately. |
| 3093 | */ |
| 3094 | int |
| 3095 | hammer2_chain_indkey_freemap(hammer2_chain_t *parent, hammer2_key_t *keyp, |
| 3096 | int keybits, hammer2_blockref_t *base, int count) |
| 3097 | { |
| 3098 | hammer2_chain_t *chain; |
| 3099 | hammer2_blockref_t *bref; |
| 3100 | hammer2_key_t key; |
| 3101 | hammer2_key_t key_beg; |
| 3102 | hammer2_key_t key_end; |
| 3103 | hammer2_key_t key_next; |
| 3104 | int cache_index; |
| 3105 | int locount; |
| 3106 | int hicount; |
| 3107 | int maxloops = 300000; |
| 3108 | |
| 3109 | key = *keyp; |
| 3110 | locount = 0; |
| 3111 | hicount = 0; |
| 3112 | keybits = 64; |
| 3113 | |
| 3114 | /* |
| 3115 | * Calculate the range of keys in the array being careful to skip |
| 3116 | * slots which are overridden with a deletion. |
| 3117 | */ |
| 3118 | key_beg = 0; |
| 3119 | key_end = HAMMER2_KEY_MAX; |
| 3120 | cache_index = 0; |
| 3121 | hammer2_spin_ex(&parent->core.spin); |
| 3122 | |
| 3123 | for (;;) { |
| 3124 | if (--maxloops == 0) { |
| 3125 | panic("indkey_freemap shit %p %p:%d\n", |
| 3126 | parent, base, count); |
| 3127 | } |
| 3128 | chain = hammer2_combined_find(parent, base, count, |
| 3129 | &cache_index, &key_next, |
| 3130 | key_beg, key_end, |
| 3131 | &bref); |
| 3132 | |
| 3133 | /* |
| 3134 | * Exhausted search |
| 3135 | */ |
| 3136 | if (bref == NULL) |
| 3137 | break; |
| 3138 | |
| 3139 | /* |
| 3140 | * Skip deleted chains. |
| 3141 | */ |
| 3142 | if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) { |
| 3143 | if (key_next == 0 || key_next > key_end) |
| 3144 | break; |
| 3145 | key_beg = key_next; |
| 3146 | continue; |
| 3147 | } |
| 3148 | |
| 3149 | /* |
| 3150 | * Use the full live (not deleted) element for the scan |
| 3151 | * iteration. HAMMER2 does not allow partial replacements. |
| 3152 | * |
| 3153 | * XXX should be built into hammer2_combined_find(). |
| 3154 | */ |
| 3155 | key_next = bref->key + ((hammer2_key_t)1 << bref->keybits); |
| 3156 | |
| 3157 | if (keybits > bref->keybits) { |
| 3158 | key = bref->key; |
| 3159 | keybits = bref->keybits; |
| 3160 | } else if (keybits == bref->keybits && bref->key < key) { |
| 3161 | key = bref->key; |
| 3162 | } |
| 3163 | if (key_next == 0) |
| 3164 | break; |
| 3165 | key_beg = key_next; |
| 3166 | } |
| 3167 | hammer2_spin_unex(&parent->core.spin); |
| 3168 | |
| 3169 | /* |
| 3170 | * Return the keybits for a higher-level FREEMAP_NODE covering |
| 3171 | * this node. |
| 3172 | */ |
| 3173 | switch(keybits) { |
| 3174 | case HAMMER2_FREEMAP_LEVEL0_RADIX: |
| 3175 | keybits = HAMMER2_FREEMAP_LEVEL1_RADIX; |
| 3176 | break; |
| 3177 | case HAMMER2_FREEMAP_LEVEL1_RADIX: |
| 3178 | keybits = HAMMER2_FREEMAP_LEVEL2_RADIX; |
| 3179 | break; |
| 3180 | case HAMMER2_FREEMAP_LEVEL2_RADIX: |
| 3181 | keybits = HAMMER2_FREEMAP_LEVEL3_RADIX; |
| 3182 | break; |
| 3183 | case HAMMER2_FREEMAP_LEVEL3_RADIX: |
| 3184 | keybits = HAMMER2_FREEMAP_LEVEL4_RADIX; |
| 3185 | break; |
| 3186 | case HAMMER2_FREEMAP_LEVEL4_RADIX: |
| 3187 | keybits = HAMMER2_FREEMAP_LEVEL5_RADIX; |
| 3188 | break; |
| 3189 | case HAMMER2_FREEMAP_LEVEL5_RADIX: |
| 3190 | panic("hammer2_chain_indkey_freemap: level too high"); |
| 3191 | break; |
| 3192 | default: |
| 3193 | panic("hammer2_chain_indkey_freemap: bad radix"); |
| 3194 | break; |
| 3195 | } |
| 3196 | *keyp = key; |
| 3197 | |
| 3198 | return (keybits); |
| 3199 | } |
| 3200 | |
| 3201 | /* |
| 3202 | * Calculate the keybits and highside/lowside of the indirect block the |
| 3203 | * caller is creating. |
| 3204 | */ |
| 3205 | static int |
| 3206 | hammer2_chain_indkey_normal(hammer2_chain_t *parent, hammer2_key_t *keyp, |
| 3207 | int keybits, hammer2_blockref_t *base, int count) |
| 3208 | { |
| 3209 | hammer2_blockref_t *bref; |
| 3210 | hammer2_chain_t *chain; |
| 3211 | hammer2_key_t key_beg; |
| 3212 | hammer2_key_t key_end; |
| 3213 | hammer2_key_t key_next; |
| 3214 | hammer2_key_t key; |
| 3215 | int nkeybits; |
| 3216 | int locount; |
| 3217 | int hicount; |
| 3218 | int cache_index; |
| 3219 | int maxloops = 300000; |
| 3220 | |
| 3221 | key = *keyp; |
| 3222 | locount = 0; |
| 3223 | hicount = 0; |
| 3224 | |
| 3225 | /* |
| 3226 | * Calculate the range of keys in the array being careful to skip |
| 3227 | * slots which are overridden with a deletion. Once the scan |
| 3228 | * completes we will cut the key range in half and shift half the |
| 3229 | * range into the new indirect block. |
| 3230 | */ |
| 3231 | key_beg = 0; |
| 3232 | key_end = HAMMER2_KEY_MAX; |
| 3233 | cache_index = 0; |
| 3234 | hammer2_spin_ex(&parent->core.spin); |
| 3235 | |
| 3236 | for (;;) { |
| 3237 | if (--maxloops == 0) { |
| 3238 | panic("indkey_freemap shit %p %p:%d\n", |
| 3239 | parent, base, count); |
| 3240 | } |
| 3241 | chain = hammer2_combined_find(parent, base, count, |
| 3242 | &cache_index, &key_next, |
| 3243 | key_beg, key_end, |
| 3244 | &bref); |
| 3245 | |
| 3246 | /* |
| 3247 | * Exhausted search |
| 3248 | */ |
| 3249 | if (bref == NULL) |
| 3250 | break; |
| 3251 | |
| 3252 | /* |
| 3253 | * NOTE: No need to check DUPLICATED here because we do |
| 3254 | * not release the spinlock. |
| 3255 | */ |
| 3256 | if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) { |
| 3257 | if (key_next == 0 || key_next > key_end) |
| 3258 | break; |
| 3259 | key_beg = key_next; |
| 3260 | continue; |
| 3261 | } |
| 3262 | |
| 3263 | /* |
| 3264 | * Use the full live (not deleted) element for the scan |
| 3265 | * iteration. HAMMER2 does not allow partial replacements. |
| 3266 | * |
| 3267 | * XXX should be built into hammer2_combined_find(). |
| 3268 | */ |
| 3269 | key_next = bref->key + ((hammer2_key_t)1 << bref->keybits); |
| 3270 | |
| 3271 | /* |
| 3272 | * Expand our calculated key range (key, keybits) to fit |
| 3273 | * the scanned key. nkeybits represents the full range |
| 3274 | * that we will later cut in half (two halves @ nkeybits - 1). |
| 3275 | */ |
| 3276 | nkeybits = keybits; |
| 3277 | if (nkeybits < bref->keybits) { |
| 3278 | if (bref->keybits > 64) { |
| 3279 | kprintf("bad bref chain %p bref %p\n", |
| 3280 | chain, bref); |
| 3281 | Debugger("fubar"); |
| 3282 | } |
| 3283 | nkeybits = bref->keybits; |
| 3284 | } |
| 3285 | while (nkeybits < 64 && |
| 3286 | (~(((hammer2_key_t)1 << nkeybits) - 1) & |
| 3287 | (key ^ bref->key)) != 0) { |
| 3288 | ++nkeybits; |
| 3289 | } |
| 3290 | |
| 3291 | /* |
| 3292 | * If the new key range is larger we have to determine |
| 3293 | * which side of the new key range the existing keys fall |
| 3294 | * under by checking the high bit, then collapsing the |
| 3295 | * locount into the hicount or vise-versa. |
| 3296 | */ |
| 3297 | if (keybits != nkeybits) { |
| 3298 | if (((hammer2_key_t)1 << (nkeybits - 1)) & key) { |
| 3299 | hicount += locount; |
| 3300 | locount = 0; |
| 3301 | } else { |
| 3302 | locount += hicount; |
| 3303 | hicount = 0; |
| 3304 | } |
| 3305 | keybits = nkeybits; |
| 3306 | } |
| 3307 | |
| 3308 | /* |
| 3309 | * The newly scanned key will be in the lower half or the |
| 3310 | * upper half of the (new) key range. |
| 3311 | */ |
| 3312 | if (((hammer2_key_t)1 << (nkeybits - 1)) & bref->key) |
| 3313 | ++hicount; |
| 3314 | else |
| 3315 | ++locount; |
| 3316 | |
| 3317 | if (key_next == 0) |
| 3318 | break; |
| 3319 | key_beg = key_next; |
| 3320 | } |
| 3321 | hammer2_spin_unex(&parent->core.spin); |
| 3322 | bref = NULL; /* now invalid (safety) */ |
| 3323 | |
| 3324 | /* |
| 3325 | * Adjust keybits to represent half of the full range calculated |
| 3326 | * above (radix 63 max) |
| 3327 | */ |
| 3328 | --keybits; |
| 3329 | |
| 3330 | /* |
| 3331 | * Select whichever half contains the most elements. Theoretically |
| 3332 | * we can select either side as long as it contains at least one |
| 3333 | * element (in order to ensure that a free slot is present to hold |
| 3334 | * the indirect block). |
| 3335 | */ |
| 3336 | if (hammer2_indirect_optimize) { |
| 3337 | /* |
| 3338 | * Insert node for least number of keys, this will arrange |
| 3339 | * the first few blocks of a large file or the first few |
| 3340 | * inodes in a directory with fewer indirect blocks when |
| 3341 | * created linearly. |
| 3342 | */ |
| 3343 | if (hicount < locount && hicount != 0) |
| 3344 | key |= (hammer2_key_t)1 << keybits; |
| 3345 | else |
| 3346 | key &= ~(hammer2_key_t)1 << keybits; |
| 3347 | } else { |
| 3348 | /* |
| 3349 | * Insert node for most number of keys, best for heavily |
| 3350 | * fragmented files. |
| 3351 | */ |
| 3352 | if (hicount > locount) |
| 3353 | key |= (hammer2_key_t)1 << keybits; |
| 3354 | else |
| 3355 | key &= ~(hammer2_key_t)1 << keybits; |
| 3356 | } |
| 3357 | *keyp = key; |
| 3358 | |
| 3359 | return (keybits); |
| 3360 | } |
| 3361 | |
| 3362 | /* |
| 3363 | * Sets CHAIN_DELETED and remove the chain's blockref from the parent if |
| 3364 | * it exists. |
| 3365 | * |
| 3366 | * Both parent and chain must be locked exclusively. |
| 3367 | * |
| 3368 | * This function will modify the parent if the blockref requires removal |
| 3369 | * from the parent's block table. |
| 3370 | * |
| 3371 | * This function is NOT recursive. Any entity already pushed into the |
| 3372 | * chain (such as an inode) may still need visibility into its contents, |
| 3373 | * as well as the ability to read and modify the contents. For example, |
| 3374 | * for an unlinked file which is still open. |
| 3375 | */ |
| 3376 | void |
| 3377 | hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain, |
| 3378 | int flags) |
| 3379 | { |
| 3380 | KKASSERT(hammer2_mtx_owned(&chain->lock)); |
| 3381 | |
| 3382 | /* |
| 3383 | * Nothing to do if already marked. |
| 3384 | * |
| 3385 | * We need the spinlock on the core whos RBTREE contains chain |
| 3386 | * to protect against races. |
| 3387 | */ |
| 3388 | if ((chain->flags & HAMMER2_CHAIN_DELETED) == 0) { |
| 3389 | KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0 && |
| 3390 | chain->parent == parent); |
| 3391 | _hammer2_chain_delete_helper(parent, chain, flags); |
| 3392 | } |
| 3393 | |
| 3394 | /* |
| 3395 | * To avoid losing track of a permanent deletion we add the chain |
| 3396 | * to the delayed flush queue. If were to flush it right now the |
| 3397 | * parent would end up in a modified state and generate I/O. |
| 3398 | * The delayed queue gives the parent a chance to be deleted to |
| 3399 | * (e.g. rm -rf). |
| 3400 | */ |
| 3401 | if (flags & HAMMER2_DELETE_PERMANENT) { |
| 3402 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY); |
| 3403 | hammer2_delayed_flush(chain); |
| 3404 | } else { |
| 3405 | /* XXX might not be needed */ |
| 3406 | hammer2_chain_setflush(chain); |
| 3407 | } |
| 3408 | } |
| 3409 | |
| 3410 | /* |
| 3411 | * Returns the index of the nearest element in the blockref array >= elm. |
| 3412 | * Returns (count) if no element could be found. |
| 3413 | * |
| 3414 | * Sets *key_nextp to the next key for loop purposes but does not modify |
| 3415 | * it if the next key would be higher than the current value of *key_nextp. |
| 3416 | * Note that *key_nexp can overflow to 0, which should be tested by the |
| 3417 | * caller. |
| 3418 | * |
| 3419 | * (*cache_indexp) is a heuristic and can be any value without effecting |
| 3420 | * the result. |
| 3421 | * |
| 3422 | * WARNING! Must be called with parent's spinlock held. Spinlock remains |
| 3423 | * held through the operation. |
| 3424 | */ |
| 3425 | static int |
| 3426 | hammer2_base_find(hammer2_chain_t *parent, |
| 3427 | hammer2_blockref_t *base, int count, |
| 3428 | int *cache_indexp, hammer2_key_t *key_nextp, |
| 3429 | hammer2_key_t key_beg, hammer2_key_t key_end) |
| 3430 | { |
| 3431 | hammer2_blockref_t *scan; |
| 3432 | hammer2_key_t scan_end; |
| 3433 | int i; |
| 3434 | int limit; |
| 3435 | |
| 3436 | /* |
| 3437 | * Require the live chain's already have their core's counted |
| 3438 | * so we can optimize operations. |
| 3439 | */ |
| 3440 | KKASSERT(parent->flags & HAMMER2_CHAIN_COUNTEDBREFS); |
| 3441 | |
| 3442 | /* |
| 3443 | * Degenerate case |
| 3444 | */ |
| 3445 | if (count == 0 || base == NULL) |
| 3446 | return(count); |
| 3447 | |
| 3448 | /* |
| 3449 | * Sequential optimization using *cache_indexp. This is the most |
| 3450 | * likely scenario. |
| 3451 | * |
| 3452 | * We can avoid trailing empty entries on live chains, otherwise |
| 3453 | * we might have to check the whole block array. |
| 3454 | */ |
| 3455 | i = *cache_indexp; |
| 3456 | cpu_ccfence(); |
| 3457 | limit = parent->core.live_zero; |
| 3458 | if (i >= limit) |
| 3459 | i = limit - 1; |
| 3460 | if (i < 0) |
| 3461 | i = 0; |
| 3462 | KKASSERT(i < count); |
| 3463 | |
| 3464 | /* |
| 3465 | * Search backwards |
| 3466 | */ |
| 3467 | scan = &base[i]; |
| 3468 | while (i > 0 && (scan->type == 0 || scan->key > key_beg)) { |
| 3469 | --scan; |
| 3470 | --i; |
| 3471 | } |
| 3472 | *cache_indexp = i; |
| 3473 | |
| 3474 | /* |
| 3475 | * Search forwards, stop when we find a scan element which |
| 3476 | * encloses the key or until we know that there are no further |
| 3477 | * elements. |
| 3478 | */ |
| 3479 | while (i < count) { |
| 3480 | if (scan->type != 0) { |
| 3481 | scan_end = scan->key + |
| 3482 | ((hammer2_key_t)1 << scan->keybits) - 1; |
| 3483 | if (scan->key > key_beg || scan_end >= key_beg) |
| 3484 | break; |
| 3485 | } |
| 3486 | if (i >= limit) |
| 3487 | return (count); |
| 3488 | ++scan; |
| 3489 | ++i; |
| 3490 | } |
| 3491 | if (i != count) { |
| 3492 | *cache_indexp = i; |
| 3493 | if (i >= limit) { |
| 3494 | i = count; |
| 3495 | } else { |
| 3496 | scan_end = scan->key + |
| 3497 | ((hammer2_key_t)1 << scan->keybits); |
| 3498 | if (scan_end && (*key_nextp > scan_end || |
| 3499 | *key_nextp == 0)) { |
| 3500 | *key_nextp = scan_end; |
| 3501 | } |
| 3502 | } |
| 3503 | } |
| 3504 | return (i); |
| 3505 | } |
| 3506 | |
| 3507 | /* |
| 3508 | * Do a combined search and return the next match either from the blockref |
| 3509 | * array or from the in-memory chain. Sets *bresp to the returned bref in |
| 3510 | * both cases, or sets it to NULL if the search exhausted. Only returns |
| 3511 | * a non-NULL chain if the search matched from the in-memory chain. |
| 3512 | * |
| 3513 | * When no in-memory chain has been found and a non-NULL bref is returned |
| 3514 | * in *bresp. |
| 3515 | * |
| 3516 | * |
| 3517 | * The returned chain is not locked or referenced. Use the returned bref |
| 3518 | * to determine if the search exhausted or not. Iterate if the base find |
| 3519 | * is chosen but matches a deleted chain. |
| 3520 | * |
| 3521 | * WARNING! Must be called with parent's spinlock held. Spinlock remains |
| 3522 | * held through the operation. |
| 3523 | */ |
| 3524 | static hammer2_chain_t * |
| 3525 | hammer2_combined_find(hammer2_chain_t *parent, |
| 3526 | hammer2_blockref_t *base, int count, |
| 3527 | int *cache_indexp, hammer2_key_t *key_nextp, |
| 3528 | hammer2_key_t key_beg, hammer2_key_t key_end, |
| 3529 | hammer2_blockref_t **bresp) |
| 3530 | { |
| 3531 | hammer2_blockref_t *bref; |
| 3532 | hammer2_chain_t *chain; |
| 3533 | int i; |
| 3534 | |
| 3535 | /* |
| 3536 | * Lookup in block array and in rbtree. |
| 3537 | */ |
| 3538 | *key_nextp = key_end + 1; |
| 3539 | i = hammer2_base_find(parent, base, count, cache_indexp, |
| 3540 | key_nextp, key_beg, key_end); |
| 3541 | chain = hammer2_chain_find(parent, key_nextp, key_beg, key_end); |
| 3542 | |
| 3543 | /* |
| 3544 | * Neither matched |
| 3545 | */ |
| 3546 | if (i == count && chain == NULL) { |
| 3547 | *bresp = NULL; |
| 3548 | return(NULL); |
| 3549 | } |
| 3550 | |
| 3551 | /* |
| 3552 | * Only chain matched. |
| 3553 | */ |
| 3554 | if (i == count) { |
| 3555 | bref = &chain->bref; |
| 3556 | goto found; |
| 3557 | } |
| 3558 | |
| 3559 | /* |
| 3560 | * Only blockref matched. |
| 3561 | */ |
| 3562 | if (chain == NULL) { |
| 3563 | bref = &base[i]; |
| 3564 | goto found; |
| 3565 | } |
| 3566 | |
| 3567 | /* |
| 3568 | * Both in-memory and blockref matched, select the nearer element. |
| 3569 | * |
| 3570 | * If both are flush with the left-hand side or both are the |
| 3571 | * same distance away, select the chain. In this situation the |
| 3572 | * chain must have been loaded from the matching blockmap. |
| 3573 | */ |
| 3574 | if ((chain->bref.key <= key_beg && base[i].key <= key_beg) || |
| 3575 | chain->bref.key == base[i].key) { |
| 3576 | KKASSERT(chain->bref.key == base[i].key); |
| 3577 | bref = &chain->bref; |
| 3578 | goto found; |
| 3579 | } |
| 3580 | |
| 3581 | /* |
| 3582 | * Select the nearer key |
| 3583 | */ |
| 3584 | if (chain->bref.key < base[i].key) { |
| 3585 | bref = &chain->bref; |
| 3586 | } else { |
| 3587 | bref = &base[i]; |
| 3588 | chain = NULL; |
| 3589 | } |
| 3590 | |
| 3591 | /* |
| 3592 | * If the bref is out of bounds we've exhausted our search. |
| 3593 | */ |
| 3594 | found: |
| 3595 | if (bref->key > key_end) { |
| 3596 | *bresp = NULL; |
| 3597 | chain = NULL; |
| 3598 | } else { |
| 3599 | *bresp = bref; |
| 3600 | } |
| 3601 | return(chain); |
| 3602 | } |
| 3603 | |
| 3604 | /* |
| 3605 | * Locate the specified block array element and delete it. The element |
| 3606 | * must exist. |
| 3607 | * |
| 3608 | * The spin lock on the related chain must be held. |
| 3609 | * |
| 3610 | * NOTE: live_count was adjusted when the chain was deleted, so it does not |
| 3611 | * need to be adjusted when we commit the media change. |
| 3612 | */ |
| 3613 | void |
| 3614 | hammer2_base_delete(hammer2_chain_t *parent, |
| 3615 | hammer2_blockref_t *base, int count, |
| 3616 | int *cache_indexp, hammer2_chain_t *chain) |
| 3617 | { |
| 3618 | hammer2_blockref_t *elm = &chain->bref; |
| 3619 | hammer2_key_t key_next; |
| 3620 | int i; |
| 3621 | |
| 3622 | /* |
| 3623 | * Delete element. Expect the element to exist. |
| 3624 | * |
| 3625 | * XXX see caller, flush code not yet sophisticated enough to prevent |
| 3626 | * re-flushed in some cases. |
| 3627 | */ |
| 3628 | key_next = 0; /* max range */ |
| 3629 | i = hammer2_base_find(parent, base, count, cache_indexp, |
| 3630 | &key_next, elm->key, elm->key); |
| 3631 | if (i == count || base[i].type == 0 || |
| 3632 | base[i].key != elm->key || |
| 3633 | ((chain->flags & HAMMER2_CHAIN_BMAPUPD) == 0 && |
| 3634 | base[i].keybits != elm->keybits)) { |
| 3635 | hammer2_spin_unex(&parent->core.spin); |
| 3636 | panic("delete base %p element not found at %d/%d elm %p\n", |
| 3637 | base, i, count, elm); |
| 3638 | return; |
| 3639 | } |
| 3640 | |
| 3641 | /* |
| 3642 | * Update stats and zero the entry |
| 3643 | */ |
| 3644 | parent->bref.data_count -= base[i].data_count; |
| 3645 | parent->bref.data_count -= (hammer2_off_t)1 << |
| 3646 | (int)(base[i].data_off & HAMMER2_OFF_MASK_RADIX); |
| 3647 | parent->bref.inode_count -= base[i].inode_count; |
| 3648 | if (base[i].type == HAMMER2_BREF_TYPE_INODE) |
| 3649 | parent->bref.inode_count -= 1; |
| 3650 | |
| 3651 | bzero(&base[i], sizeof(*base)); |
| 3652 | |
| 3653 | /* |
| 3654 | * We can only optimize parent->core.live_zero for live chains. |
| 3655 | */ |
| 3656 | if (parent->core.live_zero == i + 1) { |
| 3657 | while (--i >= 0 && base[i].type == 0) |
| 3658 | ; |
| 3659 | parent->core.live_zero = i + 1; |
| 3660 | } |
| 3661 | |
| 3662 | /* |
| 3663 | * Clear appropriate blockmap flags in chain. |
| 3664 | */ |
| 3665 | atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED | |
| 3666 | HAMMER2_CHAIN_BMAPUPD); |
| 3667 | } |
| 3668 | |
| 3669 | /* |
| 3670 | * Insert the specified element. The block array must not already have the |
| 3671 | * element and must have space available for the insertion. |
| 3672 | * |
| 3673 | * The spin lock on the related chain must be held. |
| 3674 | * |
| 3675 | * NOTE: live_count was adjusted when the chain was deleted, so it does not |
| 3676 | * need to be adjusted when we commit the media change. |
| 3677 | */ |
| 3678 | void |
| 3679 | hammer2_base_insert(hammer2_chain_t *parent, |
| 3680 | hammer2_blockref_t *base, int count, |
| 3681 | int *cache_indexp, hammer2_chain_t *chain) |
| 3682 | { |
| 3683 | hammer2_blockref_t *elm = &chain->bref; |
| 3684 | hammer2_key_t key_next; |
| 3685 | hammer2_key_t xkey; |
| 3686 | int i; |
| 3687 | int j; |
| 3688 | int k; |
| 3689 | int l; |
| 3690 | int u = 1; |
| 3691 | |
| 3692 | /* |
| 3693 | * Insert new element. Expect the element to not already exist |
| 3694 | * unless we are replacing it. |
| 3695 | * |
| 3696 | * XXX see caller, flush code not yet sophisticated enough to prevent |
| 3697 | * re-flushed in some cases. |
| 3698 | */ |
| 3699 | key_next = 0; /* max range */ |
| 3700 | i = hammer2_base_find(parent, base, count, cache_indexp, |
| 3701 | &key_next, elm->key, elm->key); |
| 3702 | |
| 3703 | /* |
| 3704 | * Shortcut fill optimization, typical ordered insertion(s) may not |
| 3705 | * require a search. |
| 3706 | */ |
| 3707 | KKASSERT(i >= 0 && i <= count); |
| 3708 | |
| 3709 | /* |
| 3710 | * Set appropriate blockmap flags in chain. |
| 3711 | */ |
| 3712 | atomic_set_int(&chain->flags, HAMMER2_CHAIN_BMAPPED); |
| 3713 | |
| 3714 | /* |
| 3715 | * Update stats and zero the entry |
| 3716 | */ |
| 3717 | parent->bref.data_count += elm->data_count; |
| 3718 | parent->bref.data_count += (hammer2_off_t)1 << |
| 3719 | (int)(elm->data_off & HAMMER2_OFF_MASK_RADIX); |
| 3720 | parent->bref.inode_count += elm->inode_count; |
| 3721 | if (elm->type == HAMMER2_BREF_TYPE_INODE) |
| 3722 | parent->bref.inode_count += 1; |
| 3723 | |
| 3724 | |
| 3725 | /* |
| 3726 | * We can only optimize parent->core.live_zero for live chains. |
| 3727 | */ |
| 3728 | if (i == count && parent->core.live_zero < count) { |
| 3729 | i = parent->core.live_zero++; |
| 3730 | base[i] = *elm; |
| 3731 | return; |
| 3732 | } |
| 3733 | |
| 3734 | xkey = elm->key + ((hammer2_key_t)1 << elm->keybits) - 1; |
| 3735 | if (i != count && (base[i].key < elm->key || xkey >= base[i].key)) { |
| 3736 | hammer2_spin_unex(&parent->core.spin); |
| 3737 | panic("insert base %p overlapping elements at %d elm %p\n", |
| 3738 | base, i, elm); |
| 3739 | } |
| 3740 | |
| 3741 | /* |
| 3742 | * Try to find an empty slot before or after. |
| 3743 | */ |
| 3744 | j = i; |
| 3745 | k = i; |
| 3746 | while (j > 0 || k < count) { |
| 3747 | --j; |
| 3748 | if (j >= 0 && base[j].type == 0) { |
| 3749 | if (j == i - 1) { |
| 3750 | base[j] = *elm; |
| 3751 | } else { |
| 3752 | bcopy(&base[j+1], &base[j], |
| 3753 | (i - j - 1) * sizeof(*base)); |
| 3754 | base[i - 1] = *elm; |
| 3755 | } |
| 3756 | goto validate; |
| 3757 | } |
| 3758 | ++k; |
| 3759 | if (k < count && base[k].type == 0) { |
| 3760 | bcopy(&base[i], &base[i+1], |
| 3761 | (k - i) * sizeof(hammer2_blockref_t)); |
| 3762 | base[i] = *elm; |
| 3763 | |
| 3764 | /* |
| 3765 | * We can only update parent->core.live_zero for live |
| 3766 | * chains. |
| 3767 | */ |
| 3768 | if (parent->core.live_zero <= k) |
| 3769 | parent->core.live_zero = k + 1; |
| 3770 | u = 2; |
| 3771 | goto validate; |
| 3772 | } |
| 3773 | } |
| 3774 | panic("hammer2_base_insert: no room!"); |
| 3775 | |
| 3776 | /* |
| 3777 | * Debugging |
| 3778 | */ |
| 3779 | validate: |
| 3780 | key_next = 0; |
| 3781 | for (l = 0; l < count; ++l) { |
| 3782 | if (base[l].type) { |
| 3783 | key_next = base[l].key + |
| 3784 | ((hammer2_key_t)1 << base[l].keybits) - 1; |
| 3785 | break; |
| 3786 | } |
| 3787 | } |
| 3788 | while (++l < count) { |
| 3789 | if (base[l].type) { |
| 3790 | if (base[l].key <= key_next) |
| 3791 | panic("base_insert %d %d,%d,%d fail %p:%d", u, i, j, k, base, l); |
| 3792 | key_next = base[l].key + |
| 3793 | ((hammer2_key_t)1 << base[l].keybits) - 1; |
| 3794 | |
| 3795 | } |
| 3796 | } |
| 3797 | |
| 3798 | } |
| 3799 | |
| 3800 | #if 0 |
| 3801 | |
| 3802 | /* |
| 3803 | * Sort the blockref array for the chain. Used by the flush code to |
| 3804 | * sort the blockref[] array. |
| 3805 | * |
| 3806 | * The chain must be exclusively locked AND spin-locked. |
| 3807 | */ |
| 3808 | typedef hammer2_blockref_t *hammer2_blockref_p; |
| 3809 | |
| 3810 | static |
| 3811 | int |
| 3812 | hammer2_base_sort_callback(const void *v1, const void *v2) |
| 3813 | { |
| 3814 | hammer2_blockref_p bref1 = *(const hammer2_blockref_p *)v1; |
| 3815 | hammer2_blockref_p bref2 = *(const hammer2_blockref_p *)v2; |
| 3816 | |
| 3817 | /* |
| 3818 | * Make sure empty elements are placed at the end of the array |
| 3819 | */ |
| 3820 | if (bref1->type == 0) { |
| 3821 | if (bref2->type == 0) |
| 3822 | return(0); |
| 3823 | return(1); |
| 3824 | } else if (bref2->type == 0) { |
| 3825 | return(-1); |
| 3826 | } |
| 3827 | |
| 3828 | /* |
| 3829 | * Sort by key |
| 3830 | */ |
| 3831 | if (bref1->key < bref2->key) |
| 3832 | return(-1); |
| 3833 | if (bref1->key > bref2->key) |
| 3834 | return(1); |
| 3835 | return(0); |
| 3836 | } |
| 3837 | |
| 3838 | void |
| 3839 | hammer2_base_sort(hammer2_chain_t *chain) |
| 3840 | { |
| 3841 | hammer2_blockref_t *base; |
| 3842 | int count; |
| 3843 | |
| 3844 | switch(chain->bref.type) { |
| 3845 | case HAMMER2_BREF_TYPE_INODE: |
| 3846 | /* |
| 3847 | * Special shortcut for embedded data returns the inode |
| 3848 | * itself. Callers must detect this condition and access |
| 3849 | * the embedded data (the strategy code does this for us). |
| 3850 | * |
| 3851 | * This is only applicable to regular files and softlinks. |
| 3852 | */ |
| 3853 | if (chain->data->ipdata.meta.op_flags & |
| 3854 | HAMMER2_OPFLAG_DIRECTDATA) { |
| 3855 | return; |
| 3856 | } |
| 3857 | base = &chain->data->ipdata.u.blockset.blockref[0]; |
| 3858 | count = HAMMER2_SET_COUNT; |
| 3859 | break; |
| 3860 | case HAMMER2_BREF_TYPE_FREEMAP_NODE: |
| 3861 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 3862 | /* |
| 3863 | * Optimize indirect blocks in the INITIAL state to avoid |
| 3864 | * I/O. |
| 3865 | */ |
| 3866 | KKASSERT((chain->flags & HAMMER2_CHAIN_INITIAL) == 0); |
| 3867 | base = &chain->data->npdata[0]; |
| 3868 | count = chain->bytes / sizeof(hammer2_blockref_t); |
| 3869 | break; |
| 3870 | case HAMMER2_BREF_TYPE_VOLUME: |
| 3871 | base = &chain->hmp->voldata.sroot_blockset.blockref[0]; |
| 3872 | count = HAMMER2_SET_COUNT; |
| 3873 | break; |
| 3874 | case HAMMER2_BREF_TYPE_FREEMAP: |
| 3875 | base = &chain->hmp->voldata.freemap_blockset.blockref[0]; |
| 3876 | count = HAMMER2_SET_COUNT; |
| 3877 | break; |
| 3878 | default: |
| 3879 | kprintf("hammer2_chain_lookup: unrecognized " |
| 3880 | "blockref(A) type: %d", |
| 3881 | chain->bref.type); |
| 3882 | while (1) |
| 3883 | tsleep(&base, 0, "dead", 0); |
| 3884 | panic("hammer2_chain_lookup: unrecognized " |
| 3885 | "blockref(A) type: %d", |
| 3886 | chain->bref.type); |
| 3887 | base = NULL; /* safety */ |
| 3888 | count = 0; /* safety */ |
| 3889 | } |
| 3890 | kqsort(base, count, sizeof(*base), hammer2_base_sort_callback); |
| 3891 | } |
| 3892 | |
| 3893 | #endif |
| 3894 | |
| 3895 | /* |
| 3896 | * Chain memory management |
| 3897 | */ |
| 3898 | void |
| 3899 | hammer2_chain_wait(hammer2_chain_t *chain) |
| 3900 | { |
| 3901 | tsleep(chain, 0, "chnflw", 1); |
| 3902 | } |
| 3903 | |
| 3904 | const hammer2_media_data_t * |
| 3905 | hammer2_chain_rdata(hammer2_chain_t *chain) |
| 3906 | { |
| 3907 | KKASSERT(chain->data != NULL); |
| 3908 | return (chain->data); |
| 3909 | } |
| 3910 | |
| 3911 | hammer2_media_data_t * |
| 3912 | hammer2_chain_wdata(hammer2_chain_t *chain) |
| 3913 | { |
| 3914 | KKASSERT(chain->data != NULL); |
| 3915 | return (chain->data); |
| 3916 | } |
| 3917 | |
| 3918 | /* |
| 3919 | * Set the check data for a chain. This can be a heavy-weight operation |
| 3920 | * and typically only runs on-flush. For file data check data is calculated |
| 3921 | * when the logical buffers are flushed. |
| 3922 | */ |
| 3923 | void |
| 3924 | hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata) |
| 3925 | { |
| 3926 | chain->bref.flags &= ~HAMMER2_BREF_FLAG_ZERO; |
| 3927 | |
| 3928 | switch(HAMMER2_DEC_CHECK(chain->bref.methods)) { |
| 3929 | case HAMMER2_CHECK_NONE: |
| 3930 | break; |
| 3931 | case HAMMER2_CHECK_DISABLED: |
| 3932 | break; |
| 3933 | case HAMMER2_CHECK_ISCSI32: |
| 3934 | chain->bref.check.iscsi32.value = |
| 3935 | hammer2_icrc32(bdata, chain->bytes); |
| 3936 | break; |
| 3937 | case HAMMER2_CHECK_CRC64: |
| 3938 | chain->bref.check.crc64.value = 0; |
| 3939 | /* XXX */ |
| 3940 | break; |
| 3941 | case HAMMER2_CHECK_SHA192: |
| 3942 | { |
| 3943 | SHA256_CTX hash_ctx; |
| 3944 | union { |
| 3945 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 3946 | uint64_t digest64[SHA256_DIGEST_LENGTH/8]; |
| 3947 | } u; |
| 3948 | |
| 3949 | SHA256_Init(&hash_ctx); |
| 3950 | SHA256_Update(&hash_ctx, bdata, chain->bytes); |
| 3951 | SHA256_Final(u.digest, &hash_ctx); |
| 3952 | u.digest64[2] ^= u.digest64[3]; |
| 3953 | bcopy(u.digest, |
| 3954 | chain->bref.check.sha192.data, |
| 3955 | sizeof(chain->bref.check.sha192.data)); |
| 3956 | } |
| 3957 | break; |
| 3958 | case HAMMER2_CHECK_FREEMAP: |
| 3959 | chain->bref.check.freemap.icrc32 = |
| 3960 | hammer2_icrc32(bdata, chain->bytes); |
| 3961 | break; |
| 3962 | default: |
| 3963 | kprintf("hammer2_chain_setcheck: unknown check type %02x\n", |
| 3964 | chain->bref.methods); |
| 3965 | break; |
| 3966 | } |
| 3967 | } |
| 3968 | |
| 3969 | int |
| 3970 | hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata) |
| 3971 | { |
| 3972 | int r; |
| 3973 | |
| 3974 | if (chain->bref.flags & HAMMER2_BREF_FLAG_ZERO) |
| 3975 | return 1; |
| 3976 | |
| 3977 | switch(HAMMER2_DEC_CHECK(chain->bref.methods)) { |
| 3978 | case HAMMER2_CHECK_NONE: |
| 3979 | r = 1; |
| 3980 | break; |
| 3981 | case HAMMER2_CHECK_DISABLED: |
| 3982 | r = 1; |
| 3983 | break; |
| 3984 | case HAMMER2_CHECK_ISCSI32: |
| 3985 | r = (chain->bref.check.iscsi32.value == |
| 3986 | hammer2_icrc32(bdata, chain->bytes)); |
| 3987 | break; |
| 3988 | case HAMMER2_CHECK_CRC64: |
| 3989 | r = (chain->bref.check.crc64.value == 0); |
| 3990 | /* XXX */ |
| 3991 | break; |
| 3992 | case HAMMER2_CHECK_SHA192: |
| 3993 | { |
| 3994 | SHA256_CTX hash_ctx; |
| 3995 | union { |
| 3996 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 3997 | uint64_t digest64[SHA256_DIGEST_LENGTH/8]; |
| 3998 | } u; |
| 3999 | |
| 4000 | SHA256_Init(&hash_ctx); |
| 4001 | SHA256_Update(&hash_ctx, bdata, chain->bytes); |
| 4002 | SHA256_Final(u.digest, &hash_ctx); |
| 4003 | u.digest64[2] ^= u.digest64[3]; |
| 4004 | if (bcmp(u.digest, |
| 4005 | chain->bref.check.sha192.data, |
| 4006 | sizeof(chain->bref.check.sha192.data)) == 0) { |
| 4007 | r = 1; |
| 4008 | } else { |
| 4009 | r = 0; |
| 4010 | } |
| 4011 | } |
| 4012 | break; |
| 4013 | case HAMMER2_CHECK_FREEMAP: |
| 4014 | r = (chain->bref.check.freemap.icrc32 == |
| 4015 | hammer2_icrc32(bdata, chain->bytes)); |
| 4016 | if (r == 0) { |
| 4017 | kprintf("freemap.icrc %08x icrc32 %08x (%d)\n", |
| 4018 | chain->bref.check.freemap.icrc32, |
| 4019 | hammer2_icrc32(bdata, chain->bytes), chain->bytes); |
| 4020 | if (chain->dio) |
| 4021 | kprintf("dio %p buf %016jx,%d bdata %p/%p\n", |
| 4022 | chain->dio, chain->dio->bp->b_loffset, chain->dio->bp->b_bufsize, bdata, chain->dio->bp->b_data); |
| 4023 | } |
| 4024 | |
| 4025 | break; |
| 4026 | default: |
| 4027 | kprintf("hammer2_chain_setcheck: unknown check type %02x\n", |
| 4028 | chain->bref.methods); |
| 4029 | r = 1; |
| 4030 | break; |
| 4031 | } |
| 4032 | return r; |
| 4033 | } |
| 4034 | |
| 4035 | /* |
| 4036 | * The caller presents a shared-locked (parent, chain) where the chain |
| 4037 | * is of type HAMMER2_OBJTYPE_HARDLINK. The caller must hold the ip |
| 4038 | * structure representing the inode locked to prevent |
| 4039 | * consolidation/deconsolidation races. |
| 4040 | * |
| 4041 | * We locate the hardlink in the current or a common parent directory. |
| 4042 | * |
| 4043 | * If we are unable to locate the hardlink, EIO is returned and |
| 4044 | * (*chainp) is unlocked and dropped. |
| 4045 | */ |
| 4046 | int |
| 4047 | hammer2_chain_hardlink_find(hammer2_inode_t *dip, |
| 4048 | hammer2_chain_t **parentp, |
| 4049 | hammer2_chain_t **chainp, |
| 4050 | int flags) |
| 4051 | { |
| 4052 | hammer2_chain_t *parent; |
| 4053 | hammer2_chain_t *rchain; |
| 4054 | hammer2_key_t key_dummy; |
| 4055 | hammer2_key_t lhc; |
| 4056 | int cache_index = -1; |
| 4057 | |
| 4058 | /* |
| 4059 | * Obtain the key for the hardlink from *chainp. |
| 4060 | */ |
| 4061 | rchain = *chainp; |
| 4062 | lhc = rchain->data->ipdata.meta.inum; |
| 4063 | hammer2_chain_unlock(rchain); |
| 4064 | hammer2_chain_drop(rchain); |
| 4065 | rchain = NULL; |
| 4066 | |
| 4067 | for (;;) { |
| 4068 | int nloops; |
| 4069 | rchain = hammer2_chain_lookup(parentp, &key_dummy, |
| 4070 | lhc, lhc, |
| 4071 | &cache_index, flags); |
| 4072 | if (rchain) |
| 4073 | break; |
| 4074 | |
| 4075 | /* |
| 4076 | * Iterate parents, handle parent rename races by retrying |
| 4077 | * the operation. |
| 4078 | */ |
| 4079 | nloops = -1; |
| 4080 | while (nloops) { |
| 4081 | --nloops; |
| 4082 | parent = *parentp; |
| 4083 | if (nloops < 0 && |
| 4084 | parent->bref.type == HAMMER2_BREF_TYPE_INODE) { |
| 4085 | nloops = 1; |
| 4086 | } |
| 4087 | if (parent->bref.flags & HAMMER2_BREF_FLAG_PFSROOT) |
| 4088 | goto done; |
| 4089 | if (parent->parent == NULL) |
| 4090 | goto done; |
| 4091 | parent = parent->parent; |
| 4092 | hammer2_chain_ref(parent); |
| 4093 | hammer2_chain_unlock(*parentp); |
| 4094 | hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS | |
| 4095 | flags); |
| 4096 | if ((*parentp)->parent == parent) { |
| 4097 | hammer2_chain_drop(*parentp); |
| 4098 | *parentp = parent; |
| 4099 | } else { |
| 4100 | hammer2_chain_unlock(parent); |
| 4101 | hammer2_chain_drop(parent); |
| 4102 | hammer2_chain_lock(*parentp, |
| 4103 | HAMMER2_RESOLVE_ALWAYS | |
| 4104 | flags); |
| 4105 | parent = NULL; /* safety */ |
| 4106 | /* retry */ |
| 4107 | } |
| 4108 | } |
| 4109 | } |
| 4110 | done: |
| 4111 | |
| 4112 | *chainp = rchain; |
| 4113 | return (rchain ? EINVAL : 0); |
| 4114 | } |