1 /* 2 * Copyright (c) 2011-2013 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@dragonflybsd.org> 6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/fcntl.h> 39 #include <sys/buf.h> 40 #include <sys/proc.h> 41 #include <sys/namei.h> 42 #include <sys/mount.h> 43 #include <sys/vnode.h> 44 #include <sys/mountctl.h> 45 46 #include "hammer2.h" 47 48 struct hammer2_fiterate { 49 hammer2_off_t bpref; 50 hammer2_off_t bnext; 51 int loops; 52 }; 53 54 typedef struct hammer2_fiterate hammer2_fiterate_t; 55 56 static int hammer2_freemap_try_alloc(hammer2_trans_t *trans, 57 hammer2_chain_t **parentp, hammer2_blockref_t *bref, 58 int radix, hammer2_fiterate_t *iter); 59 static void hammer2_freemap_init(hammer2_trans_t *trans, hammer2_mount_t *hmp, 60 hammer2_key_t key, hammer2_chain_t *chain); 61 static int hammer2_bmap_alloc(hammer2_trans_t *trans, hammer2_mount_t *hmp, 62 hammer2_bmap_data_t *bmap, uint16_t class, 63 int n, int radix, hammer2_key_t *basep); 64 static int hammer2_freemap_iterate(hammer2_trans_t *trans, 65 hammer2_chain_t **parentp, hammer2_chain_t **chainp, 66 hammer2_fiterate_t *iter); 67 68 static __inline 69 int 70 hammer2_freemapradix(int radix) 71 { 72 return(radix); 73 } 74 75 /* 76 * Calculate the device offset for the specified FREEMAP_NODE or FREEMAP_LEAF 77 * bref. Return a combined media offset and physical size radix. Freemap 78 * chains use fixed storage offsets in the 4MB reserved area at the 79 * beginning of each 2GB zone 80 * 81 * Rotate between four possibilities. Theoretically this means we have three 82 * good freemaps in case of a crash which we can use as a base for the fixup 83 * scan at mount-time. 84 */ 85 #define H2FMBASE(key, radix) ((key) & ~(((hammer2_off_t)1 << (radix)) - 1)) 86 #define H2FMSHIFT(radix) ((hammer2_off_t)1 << (radix)) 87 88 static 89 int 90 hammer2_freemap_reserve(hammer2_mount_t *hmp, hammer2_blockref_t *bref, 91 int radix) 92 { 93 hammer2_off_t off; 94 size_t bytes; 95 96 /* 97 * Physical allocation size -> radix. Typically either 256 for 98 * a level 0 freemap leaf or 65536 for a level N freemap node. 99 * 100 * NOTE: A 256 byte bitmap represents 256 x 8 x 1024 = 2MB of storage. 101 * Do not use hammer2_allocsize() here as it has a min cap. 102 */ 103 bytes = 1 << radix; 104 105 /* 106 * Adjust by HAMMER2_ZONE_FREEMAP_{A,B,C,D} using the existing 107 * offset as a basis. Start in zone A if previously unallocated. 108 */ 109 if ((bref->data_off & ~HAMMER2_OFF_MASK_RADIX) == 0) { 110 off = HAMMER2_ZONE_FREEMAP_A; 111 } else { 112 off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX & 113 (((hammer2_off_t)1 << HAMMER2_FREEMAP_LEVEL1_RADIX) - 1); 114 off = off / HAMMER2_PBUFSIZE; 115 KKASSERT(off >= HAMMER2_ZONE_FREEMAP_A); 116 KKASSERT(off < HAMMER2_ZONE_FREEMAP_D + 4); 117 118 if (off >= HAMMER2_ZONE_FREEMAP_D) 119 off = HAMMER2_ZONE_FREEMAP_A; 120 else if (off >= HAMMER2_ZONE_FREEMAP_C) 121 off = HAMMER2_ZONE_FREEMAP_D; 122 else if (off >= HAMMER2_ZONE_FREEMAP_B) 123 off = HAMMER2_ZONE_FREEMAP_C; 124 else 125 off = HAMMER2_ZONE_FREEMAP_B; 126 } 127 off = off * HAMMER2_PBUFSIZE; 128 129 /* 130 * Calculate the block offset of the reserved block. This will 131 * point into the 4MB reserved area at the base of the appropriate 132 * 2GB zone, once added to the FREEMAP_x selection above. 133 */ 134 switch(bref->keybits) { 135 /* case HAMMER2_FREEMAP_LEVEL5_RADIX: not applicable */ 136 case HAMMER2_FREEMAP_LEVEL4_RADIX: /* 2EB */ 137 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE); 138 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE); 139 off += H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL4_RADIX) + 140 HAMMER2_ZONEFM_LEVEL4 * HAMMER2_PBUFSIZE; 141 break; 142 case HAMMER2_FREEMAP_LEVEL3_RADIX: /* 2PB */ 143 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE); 144 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE); 145 off += H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL3_RADIX) + 146 HAMMER2_ZONEFM_LEVEL3 * HAMMER2_PBUFSIZE; 147 break; 148 case HAMMER2_FREEMAP_LEVEL2_RADIX: /* 2TB */ 149 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE); 150 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE); 151 off += H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL2_RADIX) + 152 HAMMER2_ZONEFM_LEVEL2 * HAMMER2_PBUFSIZE; 153 break; 154 case HAMMER2_FREEMAP_LEVEL1_RADIX: /* 2GB */ 155 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF); 156 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE); 157 off += H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL1_RADIX) + 158 HAMMER2_ZONEFM_LEVEL1 * HAMMER2_PBUFSIZE; 159 break; 160 default: 161 panic("freemap: bad radix(2) %p %d\n", bref, bref->keybits); 162 /* NOT REACHED */ 163 break; 164 } 165 bref->data_off = off | radix; 166 return (0); 167 } 168 169 /* 170 * Normal freemap allocator 171 * 172 * Use available hints to allocate space using the freemap. Create missing 173 * freemap infrastructure on-the-fly as needed (including marking initial 174 * allocations using the iterator as allocated, instantiating new 2GB zones, 175 * and dealing with the end-of-media edge case). 176 * 177 * ip and bpref are only used as a heuristic to determine locality of 178 * reference. bref->key may also be used heuristically. 179 */ 180 int 181 hammer2_freemap_alloc(hammer2_trans_t *trans, hammer2_mount_t *hmp, 182 hammer2_blockref_t *bref, size_t bytes) 183 { 184 hammer2_chain_t *parent; 185 int radix; 186 int error; 187 unsigned int hindex; 188 hammer2_fiterate_t iter; 189 190 /* 191 * Validate the allocation size. It must be a power of 2. 192 * 193 * For now require that the caller be aware of the minimum 194 * allocation (1K). 195 */ 196 radix = hammer2_getradix(bytes); 197 KKASSERT((size_t)1 << radix == bytes); 198 199 /* 200 * Freemap blocks themselves are simply assigned from the reserve 201 * area, not allocated from the freemap. 202 */ 203 if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE || 204 bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) { 205 return(hammer2_freemap_reserve(hmp, bref, radix)); 206 } 207 208 if (bref->data_off & ~HAMMER2_OFF_MASK_RADIX) 209 hammer2_freemap_free(trans, hmp, bref, 0); 210 211 /* 212 * Normal allocations 213 */ 214 KKASSERT(bytes >= HAMMER2_MIN_ALLOC && bytes <= HAMMER2_MAX_ALLOC); 215 216 /* 217 * Calculate the starting point for our allocation search. 218 * 219 * Each freemap leaf is dedicated to a specific freemap_radix. 220 * The freemap_radix can be more fine-grained than the device buffer 221 * radix which results in inodes being grouped together in their 222 * own segment, terminal-data (16K or less) and initial indirect 223 * block being grouped together, and then full-indirect and full-data 224 * blocks (64K) being grouped together. 225 * 226 * The single most important aspect of this is the inode grouping 227 * because that is what allows 'find' and 'ls' and other filesystem 228 * topology operations to run fast. 229 */ 230 #if 0 231 if (bref->data_off & ~HAMMER2_OFF_MASK_RADIX) 232 bpref = bref->data_off & ~HAMMER2_OFF_MASK_RADIX; 233 else if (trans->tmp_bpref) 234 bpref = trans->tmp_bpref; 235 else if (trans->tmp_ip) 236 bpref = trans->tmp_ip->chain->bref.data_off; 237 else 238 #endif 239 /* 240 * Heuristic tracking index. We would like one for each distinct 241 * bref type if possible. heur_freemap[] has room for two classes 242 * for each type. At a minimum we have to break-up our heuristic 243 * by device block sizes. 244 */ 245 hindex = hammer2_devblkradix(radix) - HAMMER2_MINIORADIX; 246 KKASSERT(hindex < HAMMER2_FREEMAP_HEUR_NRADIX); 247 hindex += bref->type * HAMMER2_FREEMAP_HEUR_NRADIX; 248 hindex &= HAMMER2_FREEMAP_HEUR_TYPES * HAMMER2_FREEMAP_HEUR_NRADIX - 1; 249 KKASSERT(hindex < HAMMER2_FREEMAP_HEUR); 250 251 iter.bpref = hmp->heur_freemap[hindex]; 252 253 /* 254 * Make sure bpref is in-bounds. It's ok if bpref covers a zone's 255 * reserved area, the try code will iterate past it. 256 */ 257 if (iter.bpref > hmp->voldata.volu_size) 258 iter.bpref = hmp->voldata.volu_size - 1; 259 260 /* 261 * Iterate the freemap looking for free space before and after. 262 */ 263 parent = &hmp->fchain; 264 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 265 error = EAGAIN; 266 iter.bnext = iter.bpref; 267 iter.loops = 0; 268 269 while (error == EAGAIN) { 270 error = hammer2_freemap_try_alloc(trans, &parent, bref, 271 radix, &iter); 272 } 273 hmp->heur_freemap[hindex] = iter.bnext; 274 hammer2_chain_unlock(parent); 275 276 return (error); 277 } 278 279 static int 280 hammer2_freemap_try_alloc(hammer2_trans_t *trans, hammer2_chain_t **parentp, 281 hammer2_blockref_t *bref, int radix, 282 hammer2_fiterate_t *iter) 283 { 284 hammer2_mount_t *hmp = (*parentp)->hmp; 285 hammer2_off_t l0size; 286 hammer2_off_t l1size; 287 hammer2_off_t l1mask; 288 hammer2_chain_t *chain; 289 hammer2_off_t key; 290 size_t bytes; 291 uint16_t class; 292 int error = 0; 293 294 295 /* 296 * Calculate the number of bytes being allocated, the number 297 * of contiguous bits of bitmap being allocated, and the bitmap 298 * mask. 299 * 300 * WARNING! cpu hardware may mask bits == 64 -> 0 and blow up the 301 * mask calculation. 302 */ 303 bytes = (size_t)1 << radix; 304 class = (bref->type << 8) | hammer2_devblkradix(radix); 305 306 /* 307 * Lookup the level1 freemap chain, creating and initializing one 308 * if necessary. Intermediate levels will be created automatically 309 * when necessary by hammer2_chain_create(). 310 */ 311 key = H2FMBASE(iter->bnext, HAMMER2_FREEMAP_LEVEL1_RADIX); 312 l0size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX); 313 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX); 314 l1mask = l1size - 1; 315 316 chain = hammer2_chain_lookup(parentp, key, key + l1mask, 317 HAMMER2_LOOKUP_FREEMAP | 318 HAMMER2_LOOKUP_ALWAYS | 319 HAMMER2_LOOKUP_MATCHIND/*XXX*/); 320 if (chain == NULL) { 321 /* 322 * Create the missing leaf, be sure to initialize 323 * the auxillary freemap tracking information in 324 * the bref.check.freemap structure. 325 */ 326 #if 0 327 kprintf("freemap create L1 @ %016jx bpref %016jx\n", 328 key, iter->bpref); 329 #endif 330 error = hammer2_chain_create(trans, parentp, &chain, 331 key, HAMMER2_FREEMAP_LEVEL1_RADIX, 332 HAMMER2_BREF_TYPE_FREEMAP_LEAF, 333 HAMMER2_FREEMAP_LEVELN_PSIZE); 334 if (error == 0) { 335 hammer2_chain_modify(trans, &chain, 0); 336 bzero(&chain->data->bmdata[0], 337 HAMMER2_FREEMAP_LEVELN_PSIZE); 338 chain->bref.check.freemap.bigmask = (uint32_t)-1; 339 chain->bref.check.freemap.avail = l1size; 340 /* bref.methods should already be inherited */ 341 342 hammer2_freemap_init(trans, hmp, key, chain); 343 } 344 } else if ((chain->bref.check.freemap.bigmask & (1 << radix)) == 0) { 345 /* 346 * Already flagged as not having enough space 347 */ 348 error = ENOSPC; 349 } else { 350 /* 351 * Modify existing chain to setup for adjustment. 352 */ 353 hammer2_chain_modify(trans, &chain, 0); 354 } 355 356 /* 357 * Scan 2MB entries. 358 */ 359 if (error == 0) { 360 hammer2_bmap_data_t *bmap; 361 hammer2_key_t base_key; 362 int count; 363 int start; 364 int n; 365 366 start = (int)((iter->bnext - key) >> 367 HAMMER2_FREEMAP_LEVEL0_RADIX); 368 KKASSERT(start >= 0 && start < HAMMER2_FREEMAP_COUNT); 369 hammer2_chain_modify(trans, &chain, 0); 370 371 error = ENOSPC; 372 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) { 373 if (start + count >= HAMMER2_FREEMAP_COUNT && 374 start - count < 0) { 375 break; 376 } 377 n = start + count; 378 bmap = &chain->data->bmdata[n]; 379 if (n < HAMMER2_FREEMAP_COUNT && bmap->avail && 380 (bmap->class == 0 || bmap->class == class)) { 381 base_key = key + n * l0size; 382 error = hammer2_bmap_alloc(trans, hmp, bmap, 383 class, n, radix, 384 &base_key); 385 if (error != ENOSPC) { 386 key = base_key; 387 break; 388 } 389 } 390 n = start - count; 391 bmap = &chain->data->bmdata[n]; 392 if (n >= 0 && bmap->avail && 393 (bmap->class == 0 || bmap->class == class)) { 394 base_key = key + n * l0size; 395 error = hammer2_bmap_alloc(trans, hmp, bmap, 396 class, n, radix, 397 &base_key); 398 if (error != ENOSPC) { 399 key = base_key; 400 break; 401 } 402 } 403 } 404 if (error == ENOSPC) 405 chain->bref.check.freemap.bigmask &= ~(1 << radix); 406 /* XXX also scan down from original count */ 407 } 408 409 if (error == 0) { 410 /* 411 * Assert validity. Must be beyond the static allocator used 412 * by newfs_hammer2 (and thus also beyond the aux area), 413 * not go past the volume size, and must not be in the 414 * reserved segment area for a zone. 415 */ 416 KKASSERT(key >= hmp->voldata.allocator_beg && 417 key + bytes <= hmp->voldata.volu_size); 418 KKASSERT((key & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG); 419 bref->data_off = key | radix; 420 421 #if 0 422 kprintf("alloc cp=%p %016jx %016jx using %016jx\n", 423 chain, 424 bref->key, bref->data_off, chain->bref.data_off); 425 #endif 426 } else if (error == ENOSPC) { 427 /* 428 * Return EAGAIN with next iteration in iter->bnext, or 429 * return ENOSPC if the allocation map has been exhausted. 430 */ 431 error = hammer2_freemap_iterate(trans, parentp, &chain, iter); 432 } 433 434 /* 435 * Cleanup 436 */ 437 if (chain) 438 hammer2_chain_unlock(chain); 439 return (error); 440 } 441 442 /* 443 * Allocate (1<<radix) bytes from the bmap whos base data offset is (*basep). 444 * 445 * If the linear iterator is mid-block we use it directly (the bitmap should 446 * already be marked allocated), otherwise we search for a block in the bitmap 447 * that fits the allocation request. 448 * 449 * A partial bitmap allocation sets the minimum bitmap granularity (16KB) 450 * to fully allocated and adjusts the linear allocator to allow the 451 * remaining space to be allocated. 452 */ 453 static 454 int 455 hammer2_bmap_alloc(hammer2_trans_t *trans, hammer2_mount_t *hmp, 456 hammer2_bmap_data_t *bmap, 457 uint16_t class, int n, int radix, hammer2_key_t *basep) 458 { 459 struct buf *bp; 460 size_t size; 461 size_t bsize; 462 int bmradix; 463 uint32_t bmmask; 464 int offset; 465 int i; 466 int j; 467 468 /* 469 * Take into account 2-bits per block when calculating bmradix. 470 */ 471 size = (size_t)1 << radix; 472 473 if (radix <= HAMMER2_FREEMAP_BLOCK_RADIX) { 474 bmradix = 2; 475 bsize = HAMMER2_FREEMAP_BLOCK_SIZE; 476 /* (16K) 2 bits per allocation block */ 477 } else { 478 bmradix = 2 << (radix - HAMMER2_FREEMAP_BLOCK_RADIX); 479 bsize = size; 480 /* (32K-256K) 4, 8, 16, 32 bits per allocation block */ 481 } 482 483 /* 484 * Use the linear iterator to pack small allocations, otherwise 485 * fall-back to finding a free 16KB chunk. The linear iterator 486 * is only valid when *NOT* on a freemap chunking boundary (16KB). 487 * If it is the bitmap must be scanned. It can become invalid 488 * once we pack to the boundary. We adjust it after a bitmap 489 * allocation only for sub-16KB allocations (so the perfectly good 490 * previous value can still be used for fragments when 16KB+ 491 * allocations are made). 492 * 493 * Beware of hardware artifacts when bmradix == 32 (intermediate 494 * result can wind up being '1' instead of '0' if hardware masks 495 * bit-count & 31). 496 * 497 * NOTE: j needs to be even in the j= calculation. As an artifact 498 * of the /2 division, our bitmask has to clear bit 0. 499 * 500 * NOTE: TODO this can leave little unallocatable fragments lying 501 * around. 502 */ 503 if (((uint32_t)bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) + size <= 504 HAMMER2_FREEMAP_BLOCK_SIZE && 505 (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) && 506 bmap->linear < HAMMER2_SEGSIZE) { 507 KKASSERT(bmap->linear >= 0 && 508 bmap->linear + size <= HAMMER2_SEGSIZE && 509 (bmap->linear & (HAMMER2_MIN_ALLOC - 1)) == 0); 510 offset = bmap->linear; 511 i = offset / (HAMMER2_SEGSIZE / 8); 512 j = (offset / (HAMMER2_FREEMAP_BLOCK_SIZE / 2)) & 30; 513 bmmask = (bmradix == 32) ? 514 0xFFFFFFFFU : (1 << bmradix) - 1; 515 bmmask <<= j; 516 bmap->linear = offset + size; 517 } else { 518 for (i = 0; i < 8; ++i) { 519 bmmask = (bmradix == 32) ? 520 0xFFFFFFFFU : (1 << bmradix) - 1; 521 for (j = 0; j < 32; j += bmradix) { 522 if ((bmap->bitmap[i] & bmmask) == 0) 523 goto success; 524 bmmask <<= bmradix; 525 } 526 } 527 /*fragments might remain*/ 528 /*KKASSERT(bmap->avail == 0);*/ 529 return (ENOSPC); 530 success: 531 offset = i * (HAMMER2_SEGSIZE / 8) + 532 (j * (HAMMER2_FREEMAP_BLOCK_SIZE / 2)); 533 if (size & HAMMER2_FREEMAP_BLOCK_MASK) 534 bmap->linear = offset + size; 535 } 536 537 KKASSERT(i >= 0 && i < 8); /* 8 x 16 -> 128 x 16K -> 2MB */ 538 539 /* 540 * Optimize the buffer cache to avoid unnecessary read-before-write 541 * operations. 542 * 543 * The device block size could be larger than the allocation size 544 * so the actual bitmap test is somewhat more involved. We have 545 * to use a compatible buffer size for this operation. 546 */ 547 if ((bmap->bitmap[i] & bmmask) == 0 && 548 hammer2_devblksize(size) != size) { 549 size_t psize = hammer2_devblksize(size); 550 hammer2_off_t pmask = (hammer2_off_t)psize - 1; 551 int pbmradix = 2 << (hammer2_devblkradix(radix) - 552 HAMMER2_FREEMAP_BLOCK_RADIX); 553 uint32_t pbmmask; 554 555 pbmmask = (pbmradix == 32) ? 0xFFFFFFFFU : (1 << pbmradix) - 1; 556 while ((pbmmask & bmmask) == 0) 557 pbmmask <<= pbmradix; 558 559 #if 0 560 kprintf("%016jx mask %08x %08x %08x (%zd/%zd)\n", 561 *basep + offset, bmap->bitmap[i], 562 pbmmask, bmmask, size, psize); 563 #endif 564 565 if ((bmap->bitmap[i] & pbmmask) == 0) { 566 bp = getblk(hmp->devvp, *basep + (offset & ~pmask), 567 psize, GETBLK_NOWAIT, 0); 568 if (bp) { 569 if ((bp->b_flags & B_CACHE) == 0) 570 vfs_bio_clrbuf(bp); 571 bp->b_flags |= B_CACHE; 572 bqrelse(bp); 573 } 574 } 575 } 576 577 #if 0 578 /* 579 * When initializing a new inode segment also attempt to initialize 580 * an adjacent segment. Be careful not to index beyond the array 581 * bounds. 582 * 583 * We do this to try to localize inode accesses to improve 584 * directory scan rates. XXX doesn't improve scan rates. 585 */ 586 if (size == HAMMER2_INODE_BYTES) { 587 if (n & 1) { 588 if (bmap[-1].radix == 0 && bmap[-1].avail) 589 bmap[-1].radix = radix; 590 } else { 591 if (bmap[1].radix == 0 && bmap[1].avail) 592 bmap[1].radix = radix; 593 } 594 } 595 #endif 596 597 /* 598 * Adjust the linear iterator, set the radix if necessary (might as 599 * well just set it unconditionally), adjust *basep to return the 600 * allocated data offset. 601 */ 602 bmap->bitmap[i] |= bmmask; 603 bmap->class = class; 604 bmap->avail -= size; 605 *basep += offset; 606 607 hammer2_voldata_lock(hmp); 608 hmp->voldata.allocator_free -= size; /* XXX */ 609 hammer2_voldata_unlock(hmp, 1); 610 611 return(0); 612 } 613 614 static 615 void 616 hammer2_freemap_init(hammer2_trans_t *trans, hammer2_mount_t *hmp, 617 hammer2_key_t key, hammer2_chain_t *chain) 618 { 619 hammer2_off_t l1size; 620 hammer2_off_t lokey; 621 hammer2_off_t hikey; 622 hammer2_bmap_data_t *bmap; 623 int count; 624 625 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX); 626 627 /* 628 * Calculate the portion of the 2GB map that should be initialized 629 * as free. Portions below or after will be initialized as allocated. 630 * SEGMASK-align the areas so we don't have to worry about sub-scans 631 * or endianess when using memset. 632 * 633 * (1) Ensure that all statically allocated space from newfs_hammer2 634 * is marked allocated. 635 * 636 * (2) Ensure that the reserved area is marked allocated (typically 637 * the first 4MB of the 2GB area being represented). 638 * 639 * (3) Ensure that any trailing space at the end-of-volume is marked 640 * allocated. 641 * 642 * WARNING! It is possible for lokey to be larger than hikey if the 643 * entire 2GB segment is within the static allocation. 644 */ 645 lokey = (hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) & 646 ~HAMMER2_SEGMASK64; 647 648 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) + 649 HAMMER2_ZONE_SEG64) { 650 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) + 651 HAMMER2_ZONE_SEG64; 652 } 653 654 hikey = key + H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX); 655 if (hikey > hmp->voldata.volu_size) { 656 hikey = hmp->voldata.volu_size & ~HAMMER2_SEGMASK64; 657 } 658 659 chain->bref.check.freemap.avail = 660 H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX); 661 bmap = &chain->data->bmdata[0]; 662 663 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) { 664 if (key < lokey || key >= hikey) { 665 memset(bmap->bitmap, -1, 666 sizeof(bmap->bitmap)); 667 bmap->avail = 0; 668 bmap->linear = HAMMER2_SEGSIZE; 669 chain->bref.check.freemap.avail -= 670 H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX); 671 } else { 672 bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX); 673 } 674 key += H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX); 675 ++bmap; 676 } 677 } 678 679 /* 680 * The current Level 1 freemap has been exhausted, iterate to the next 681 * one, return ENOSPC if no freemaps remain. 682 * 683 * XXX this should rotate back to the beginning to handle freed-up space 684 * XXX or use intermediate entries to locate free space. TODO 685 */ 686 static int 687 hammer2_freemap_iterate(hammer2_trans_t *trans, hammer2_chain_t **parentp, 688 hammer2_chain_t **chainp, hammer2_fiterate_t *iter) 689 { 690 hammer2_mount_t *hmp = (*parentp)->hmp; 691 692 iter->bnext &= ~(H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX) - 1); 693 iter->bnext += H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX); 694 if (iter->bnext >= hmp->voldata.volu_size) { 695 iter->bnext = 0; 696 if (++iter->loops == 2) 697 return (ENOSPC); 698 } 699 return(EAGAIN); 700 } 701 702 /* 703 * Free the specified blockref. This code is only able to fully free 704 * blocks when (how) is non-zero, otherwise the block is marked for 705 * the bulk freeing pass to check. 706 * 707 * Normal use is to only mark inodes as possibly being free. The underlying 708 * file blocks are not necessarily marked. The bulk freescan can 709 * theoretically handle the case. 710 * 711 * XXX currently disabled when how == 0 (the normal real-time case). At 712 * the moment we depend on the bulk freescan to actually free blocks. It 713 * will still call this routine with a non-zero how to stage possible frees 714 * and to do the actual free. 715 */ 716 void 717 hammer2_freemap_free(hammer2_trans_t *trans, hammer2_mount_t *hmp, 718 hammer2_blockref_t *bref, int how) 719 { 720 hammer2_off_t data_off = bref->data_off; 721 hammer2_chain_t *chain; 722 hammer2_chain_t *parent; 723 hammer2_bmap_data_t *bmap; 724 hammer2_key_t key; 725 hammer2_off_t l0size; 726 hammer2_off_t l1size; 727 hammer2_off_t l1mask; 728 uint32_t *bitmap; 729 const uint32_t bmmask00 = 0; 730 uint32_t bmmask01; 731 uint32_t bmmask10; 732 uint32_t bmmask11; 733 size_t bytes; 734 uint16_t class; 735 int radix; 736 int start; 737 int count; 738 int modified = 0; 739 740 radix = (int)data_off & HAMMER2_OFF_MASK_RADIX; 741 data_off &= ~HAMMER2_OFF_MASK_RADIX; 742 KKASSERT(radix <= HAMMER2_MAX_RADIX); 743 744 bytes = (size_t)1 << radix; 745 class = (bref->type << 8) | hammer2_devblkradix(radix); 746 747 /* 748 * Lookup the level1 freemap chain. The chain must exist. 749 */ 750 key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL1_RADIX); 751 l0size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX); 752 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX); 753 l1mask = l1size - 1; 754 755 parent = &hmp->fchain; 756 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 757 758 chain = hammer2_chain_lookup(&parent, key, key + l1mask, 759 HAMMER2_LOOKUP_FREEMAP | 760 HAMMER2_LOOKUP_ALWAYS | 761 HAMMER2_LOOKUP_MATCHIND/*XXX*/); 762 if (chain == NULL) { 763 kprintf("hammer2_freemap_free: %016jx: no chain\n", 764 (intmax_t)bref->data_off); 765 hammer2_chain_unlock(parent); 766 return; 767 } 768 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF); 769 770 /* 771 * Find the bmap entry (covering a 2MB swath) 772 * Find the bitmap array index 773 * Find the bitmap bit index (runs in 2-bit pairs) 774 */ 775 bmap = &chain->data->bmdata[(int)(data_off >> HAMMER2_SEGRADIX) & 776 (HAMMER2_FREEMAP_COUNT - 1)]; 777 bitmap = &bmap->bitmap[(int)(data_off >> (HAMMER2_SEGRADIX - 3)) & 7]; 778 779 start = ((int)(data_off >> HAMMER2_FREEMAP_BLOCK_RADIX) & 15) * 2; 780 bmmask01 = 1 << start; 781 bmmask10 = 2 << start; 782 bmmask11 = 3 << start; 783 784 /* 785 * Fixup the bitmap 786 */ 787 if (radix < HAMMER2_FREEMAP_BLOCK_RADIX) { 788 count = 1; 789 how = 0; /* partial block, cannot set to 00 */ 790 } else { 791 count = 1 << (radix - HAMMER2_FREEMAP_BLOCK_RADIX); 792 } 793 794 while (count) { 795 KKASSERT(bmmask11); 796 KKASSERT((*bitmap & bmmask11) != bmmask00); 797 if ((*bitmap & bmmask11) == bmmask11) { 798 if (!modified) { 799 hammer2_chain_modify(trans, &chain, 0); 800 modified = 1; 801 bmap = &chain->data->bmdata[(int)(data_off >> HAMMER2_SEGRADIX) & 802 (HAMMER2_FREEMAP_COUNT - 1)]; 803 bitmap = &bmap->bitmap[(int)(data_off >> (HAMMER2_SEGRADIX - 3)) & 7]; 804 } 805 if (how) 806 *bitmap &= ~bmmask11; 807 else 808 *bitmap = (*bitmap & ~bmmask11) | bmmask10; 809 } else if ((*bitmap & bmmask11) == bmmask10) { 810 if (how) { 811 if (!modified) { 812 hammer2_chain_modify(trans, &chain, 0); 813 modified = 1; 814 bmap = &chain->data->bmdata[(int)(data_off >> HAMMER2_SEGRADIX) & 815 (HAMMER2_FREEMAP_COUNT - 1)]; 816 bitmap = &bmap->bitmap[(int)(data_off >> (HAMMER2_SEGRADIX - 3)) & 7]; 817 } 818 *bitmap &= ~bmmask11; 819 } 820 } else if ((*bitmap & bmmask11) == bmmask01) { 821 KKASSERT(0); 822 } 823 --count; 824 bmmask01 <<= 2; 825 bmmask10 <<= 2; 826 bmmask11 <<= 2; 827 } 828 if (how && modified) { 829 bmap->avail += 1 << radix; 830 KKASSERT(bmap->avail <= HAMMER2_SEGSIZE); 831 if (bmap->avail == HAMMER2_SEGSIZE && 832 bmap->bitmap[0] == 0 && 833 bmap->bitmap[1] == 0 && 834 bmap->bitmap[2] == 0 && 835 bmap->bitmap[3] == 0 && 836 bmap->bitmap[4] == 0 && 837 bmap->bitmap[5] == 0 && 838 bmap->bitmap[6] == 0 && 839 bmap->bitmap[7] == 0) { 840 key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL0_RADIX); 841 kprintf("Freeseg %016jx\n", (intmax_t)key); 842 bmap->class = 0; 843 } 844 } 845 846 /* 847 * chain->bref.check.freemap.bigmask (XXX) 848 */ 849 if (modified) 850 chain->bref.check.freemap.bigmask |= 1 << radix; 851 852 hammer2_chain_unlock(chain); 853 hammer2_chain_unlock(parent); 854 } 855