2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_blockmap.c,v 1.27 2008/07/31 22:30:33 dillon Exp $
42 static int hammer_res_rb_compare(hammer_reserve_t res1, hammer_reserve_t res2);
43 static void hammer_reserve_setdelay_offset(hammer_mount_t hmp,
44 hammer_off_t base_offset, int zone,
45 struct hammer_blockmap_layer2 *layer2);
46 static void hammer_reserve_setdelay(hammer_mount_t hmp, hammer_reserve_t resv);
49 * Reserved big-blocks red-black tree support
51 RB_GENERATE2(hammer_res_rb_tree, hammer_reserve, rb_node,
52 hammer_res_rb_compare, hammer_off_t, zone_offset);
55 hammer_res_rb_compare(hammer_reserve_t res1, hammer_reserve_t res2)
57 if (res1->zone_offset < res2->zone_offset)
59 if (res1->zone_offset > res2->zone_offset)
65 * Allocate bytes from a zone
68 hammer_blockmap_alloc(hammer_transaction_t trans, int zone, int bytes,
69 hammer_off_t hint, int *errorp)
72 hammer_volume_t root_volume;
73 hammer_blockmap_t blockmap;
74 hammer_blockmap_t freemap;
75 hammer_reserve_t resv;
76 struct hammer_blockmap_layer1 *layer1;
77 struct hammer_blockmap_layer2 *layer2;
78 hammer_buffer_t buffer1 = NULL;
79 hammer_buffer_t buffer2 = NULL;
80 hammer_buffer_t buffer3 = NULL;
81 hammer_off_t tmp_offset;
82 hammer_off_t next_offset;
83 hammer_off_t result_offset;
84 hammer_off_t layer1_offset;
85 hammer_off_t layer2_offset;
86 hammer_off_t base_off;
88 int offset; /* offset within big-block */
94 * Deal with alignment and buffer-boundary issues.
96 * Be careful, certain primary alignments are used below to allocate
97 * new blockmap blocks.
99 bytes = (bytes + 15) & ~15;
100 KKASSERT(bytes > 0 && bytes <= HAMMER_XBUFSIZE);
101 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
106 root_volume = trans->rootvol;
108 blockmap = &hmp->blockmap[zone];
109 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
110 KKASSERT(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
113 * Use the hint if we have one.
115 if (hint && HAMMER_ZONE_DECODE(hint) == zone) {
116 next_offset = (hint + 15) & ~(hammer_off_t)15;
119 next_offset = blockmap->next_offset;
125 * use_hint is turned off if we leave the hinted big-block.
127 if (use_hint && ((next_offset ^ hint) & ~HAMMER_HINTBLOCK_MASK64)) {
128 next_offset = blockmap->next_offset;
135 if (next_offset == HAMMER_ZONE_ENCODE(zone + 1, 0)) {
141 next_offset = HAMMER_ZONE_ENCODE(zone, 0);
145 * The allocation request may not cross a buffer boundary. Special
146 * large allocations must not cross a large-block boundary.
148 tmp_offset = next_offset + bytes - 1;
149 if (bytes <= HAMMER_BUFSIZE) {
150 if ((next_offset ^ tmp_offset) & ~HAMMER_BUFMASK64) {
151 next_offset = tmp_offset & ~HAMMER_BUFMASK64;
155 if ((next_offset ^ tmp_offset) & ~HAMMER_LARGEBLOCK_MASK64) {
156 next_offset = tmp_offset & ~HAMMER_LARGEBLOCK_MASK64;
160 offset = (int)next_offset & HAMMER_LARGEBLOCK_MASK;
165 layer1_offset = freemap->phys_offset +
166 HAMMER_BLOCKMAP_LAYER1_OFFSET(next_offset);
168 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer1);
177 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
178 hammer_lock_ex(&hmp->blkmap_lock);
179 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
180 panic("CRC FAILED: LAYER1");
181 hammer_unlock(&hmp->blkmap_lock);
185 * If we are at a big-block boundary and layer1 indicates no
186 * free big-blocks, then we cannot allocate a new bigblock in
187 * layer2, skip to the next layer1 entry.
189 if (offset == 0 && layer1->blocks_free == 0) {
190 next_offset = (next_offset + HAMMER_BLOCKMAP_LAYER2) &
191 ~HAMMER_BLOCKMAP_LAYER2_MASK;
194 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
197 * Skip this layer1 entry if it is pointing to a layer2 big-block
198 * on a volume that we are currently trying to remove from the
199 * file-system. This is used by the volume-del code together with
200 * the reblocker to free up a volume.
202 if ((int)HAMMER_VOL_DECODE(layer1->phys_offset) ==
203 hmp->volume_to_remove) {
204 next_offset = (next_offset + HAMMER_BLOCKMAP_LAYER2) &
205 ~HAMMER_BLOCKMAP_LAYER2_MASK;
210 * Dive layer 2, each entry represents a large-block.
212 layer2_offset = layer1->phys_offset +
213 HAMMER_BLOCKMAP_LAYER2_OFFSET(next_offset);
214 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer2);
221 * Check CRC. This can race another thread holding the lock
222 * and in the middle of modifying layer2.
224 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
225 hammer_lock_ex(&hmp->blkmap_lock);
226 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
227 panic("CRC FAILED: LAYER2");
228 hammer_unlock(&hmp->blkmap_lock);
232 * Skip the layer if the zone is owned by someone other then us.
234 if (layer2->zone && layer2->zone != zone) {
235 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
238 if (offset < layer2->append_off) {
239 next_offset += layer2->append_off - offset;
244 * If operating in the current non-hint blockmap block, do not
245 * allow it to get over-full. Also drop any active hinting so
246 * blockmap->next_offset is updated at the end.
248 * We do this for B-Tree and meta-data allocations to provide
249 * localization for updates.
251 if ((zone == HAMMER_ZONE_BTREE_INDEX ||
252 zone == HAMMER_ZONE_META_INDEX) &&
253 offset >= HAMMER_LARGEBLOCK_OVERFILL &&
254 !((next_offset ^ blockmap->next_offset) & ~HAMMER_LARGEBLOCK_MASK64)
256 if (offset >= HAMMER_LARGEBLOCK_OVERFILL) {
257 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
264 * We need the lock from this point on. We have to re-check zone
265 * ownership after acquiring the lock and also check for reservations.
267 hammer_lock_ex(&hmp->blkmap_lock);
269 if (layer2->zone && layer2->zone != zone) {
270 hammer_unlock(&hmp->blkmap_lock);
271 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
274 if (offset < layer2->append_off) {
275 hammer_unlock(&hmp->blkmap_lock);
276 next_offset += layer2->append_off - offset;
281 * The bigblock might be reserved by another zone. If it is reserved
282 * by our zone we may have to move next_offset past the append_off.
284 base_off = (next_offset &
285 (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) |
286 HAMMER_ZONE_RAW_BUFFER;
287 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root, base_off);
289 if (resv->zone != zone) {
290 hammer_unlock(&hmp->blkmap_lock);
291 next_offset = (next_offset + HAMMER_LARGEBLOCK_SIZE) &
292 ~HAMMER_LARGEBLOCK_MASK64;
295 if (offset < resv->append_off) {
296 hammer_unlock(&hmp->blkmap_lock);
297 next_offset += resv->append_off - offset;
304 * Ok, we can allocate out of this layer2 big-block. Assume ownership
305 * of the layer for real. At this point we've validated any
306 * reservation that might exist and can just ignore resv.
308 if (layer2->zone == 0) {
310 * Assign the bigblock to our zone
312 hammer_modify_buffer(trans, buffer1,
313 layer1, sizeof(*layer1));
314 --layer1->blocks_free;
315 layer1->layer1_crc = crc32(layer1,
316 HAMMER_LAYER1_CRCSIZE);
317 hammer_modify_buffer_done(buffer1);
318 hammer_modify_buffer(trans, buffer2,
319 layer2, sizeof(*layer2));
321 KKASSERT(layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE);
322 KKASSERT(layer2->append_off == 0);
323 hammer_modify_volume_field(trans, trans->rootvol,
324 vol0_stat_freebigblocks);
325 --root_volume->ondisk->vol0_stat_freebigblocks;
326 hmp->copy_stat_freebigblocks =
327 root_volume->ondisk->vol0_stat_freebigblocks;
328 hammer_modify_volume_done(trans->rootvol);
330 hammer_modify_buffer(trans, buffer2,
331 layer2, sizeof(*layer2));
333 KKASSERT(layer2->zone == zone);
336 * NOTE: bytes_free can legally go negative due to de-dup.
338 layer2->bytes_free -= bytes;
339 KKASSERT(layer2->append_off <= offset);
340 layer2->append_off = offset + bytes;
341 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
342 hammer_modify_buffer_done(buffer2);
345 * We hold the blockmap lock and should be the only ones
346 * capable of modifying resv->append_off. Track the allocation
349 KKASSERT(bytes != 0);
351 KKASSERT(resv->append_off <= offset);
352 resv->append_off = offset + bytes;
353 resv->flags &= ~HAMMER_RESF_LAYER2FREE;
354 hammer_blockmap_reserve_complete(hmp, resv);
358 * If we are allocating from the base of a new buffer we can avoid
359 * a disk read by calling hammer_bnew().
361 if ((next_offset & HAMMER_BUFMASK) == 0) {
362 hammer_bnew_ext(trans->hmp, next_offset, bytes,
365 result_offset = next_offset;
368 * If we weren't supplied with a hint or could not use the hint
369 * then we wound up using blockmap->next_offset as the hint and
373 hammer_modify_volume(NULL, root_volume, NULL, 0);
374 blockmap->next_offset = next_offset + bytes;
375 hammer_modify_volume_done(root_volume);
377 hammer_unlock(&hmp->blkmap_lock);
384 hammer_rel_buffer(buffer1, 0);
386 hammer_rel_buffer(buffer2, 0);
388 hammer_rel_buffer(buffer3, 0);
390 return(result_offset);
394 * Frontend function - Reserve bytes in a zone.
396 * This code reserves bytes out of a blockmap without committing to any
397 * meta-data modifications, allowing the front-end to directly issue disk
398 * write I/O for large blocks of data
400 * The backend later finalizes the reservation with hammer_blockmap_finalize()
401 * upon committing the related record.
404 hammer_blockmap_reserve(hammer_mount_t hmp, int zone, int bytes,
405 hammer_off_t *zone_offp, int *errorp)
407 hammer_volume_t root_volume;
408 hammer_blockmap_t blockmap;
409 hammer_blockmap_t freemap;
410 struct hammer_blockmap_layer1 *layer1;
411 struct hammer_blockmap_layer2 *layer2;
412 hammer_buffer_t buffer1 = NULL;
413 hammer_buffer_t buffer2 = NULL;
414 hammer_buffer_t buffer3 = NULL;
415 hammer_off_t tmp_offset;
416 hammer_off_t next_offset;
417 hammer_off_t layer1_offset;
418 hammer_off_t layer2_offset;
419 hammer_off_t base_off;
420 hammer_reserve_t resv;
421 hammer_reserve_t resx;
428 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
429 root_volume = hammer_get_root_volume(hmp, errorp);
432 blockmap = &hmp->blockmap[zone];
433 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
434 KKASSERT(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
437 * Deal with alignment and buffer-boundary issues.
439 * Be careful, certain primary alignments are used below to allocate
440 * new blockmap blocks.
442 bytes = (bytes + 15) & ~15;
443 KKASSERT(bytes > 0 && bytes <= HAMMER_XBUFSIZE);
445 next_offset = blockmap->next_offset;
451 if (next_offset == HAMMER_ZONE_ENCODE(zone + 1, 0)) {
456 next_offset = HAMMER_ZONE_ENCODE(zone, 0);
460 * The allocation request may not cross a buffer boundary. Special
461 * large allocations must not cross a large-block boundary.
463 tmp_offset = next_offset + bytes - 1;
464 if (bytes <= HAMMER_BUFSIZE) {
465 if ((next_offset ^ tmp_offset) & ~HAMMER_BUFMASK64) {
466 next_offset = tmp_offset & ~HAMMER_BUFMASK64;
470 if ((next_offset ^ tmp_offset) & ~HAMMER_LARGEBLOCK_MASK64) {
471 next_offset = tmp_offset & ~HAMMER_LARGEBLOCK_MASK64;
475 offset = (int)next_offset & HAMMER_LARGEBLOCK_MASK;
480 layer1_offset = freemap->phys_offset +
481 HAMMER_BLOCKMAP_LAYER1_OFFSET(next_offset);
482 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer1);
489 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
490 hammer_lock_ex(&hmp->blkmap_lock);
491 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
492 panic("CRC FAILED: LAYER1");
493 hammer_unlock(&hmp->blkmap_lock);
497 * If we are at a big-block boundary and layer1 indicates no
498 * free big-blocks, then we cannot allocate a new bigblock in
499 * layer2, skip to the next layer1 entry.
501 if ((next_offset & HAMMER_LARGEBLOCK_MASK) == 0 &&
502 layer1->blocks_free == 0) {
503 next_offset = (next_offset + HAMMER_BLOCKMAP_LAYER2) &
504 ~HAMMER_BLOCKMAP_LAYER2_MASK;
507 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
510 * Dive layer 2, each entry represents a large-block.
512 layer2_offset = layer1->phys_offset +
513 HAMMER_BLOCKMAP_LAYER2_OFFSET(next_offset);
514 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer2);
519 * Check CRC if not allocating into uninitialized space (which we
520 * aren't when reserving space).
522 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
523 hammer_lock_ex(&hmp->blkmap_lock);
524 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
525 panic("CRC FAILED: LAYER2");
526 hammer_unlock(&hmp->blkmap_lock);
530 * Skip the layer if the zone is owned by someone other then us.
532 if (layer2->zone && layer2->zone != zone) {
533 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
536 if (offset < layer2->append_off) {
537 next_offset += layer2->append_off - offset;
542 * We need the lock from this point on. We have to re-check zone
543 * ownership after acquiring the lock and also check for reservations.
545 hammer_lock_ex(&hmp->blkmap_lock);
547 if (layer2->zone && layer2->zone != zone) {
548 hammer_unlock(&hmp->blkmap_lock);
549 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
552 if (offset < layer2->append_off) {
553 hammer_unlock(&hmp->blkmap_lock);
554 next_offset += layer2->append_off - offset;
559 * The bigblock might be reserved by another zone. If it is reserved
560 * by our zone we may have to move next_offset past the append_off.
562 base_off = (next_offset &
563 (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) |
564 HAMMER_ZONE_RAW_BUFFER;
565 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root, base_off);
567 if (resv->zone != zone) {
568 hammer_unlock(&hmp->blkmap_lock);
569 next_offset = (next_offset + HAMMER_LARGEBLOCK_SIZE) &
570 ~HAMMER_LARGEBLOCK_MASK64;
573 if (offset < resv->append_off) {
574 hammer_unlock(&hmp->blkmap_lock);
575 next_offset += resv->append_off - offset;
581 resx = kmalloc(sizeof(*resv), hmp->m_misc,
582 M_WAITOK | M_ZERO | M_USE_RESERVE);
585 resx->zone_offset = base_off;
586 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE)
587 resx->flags |= HAMMER_RESF_LAYER2FREE;
588 resv = RB_INSERT(hammer_res_rb_tree, &hmp->rb_resv_root, resx);
589 KKASSERT(resv == NULL);
591 ++hammer_count_reservations;
593 resv->append_off = offset + bytes;
596 * If we are not reserving a whole buffer but are at the start of
597 * a new block, call hammer_bnew() to avoid a disk read.
599 * If we are reserving a whole buffer (or more), the caller will
600 * probably use a direct read, so do nothing.
602 if (bytes < HAMMER_BUFSIZE && (next_offset & HAMMER_BUFMASK) == 0) {
603 hammer_bnew(hmp, next_offset, errorp, &buffer3);
607 * Adjust our iterator and alloc_offset. The layer1 and layer2
608 * space beyond alloc_offset is uninitialized. alloc_offset must
609 * be big-block aligned.
611 blockmap->next_offset = next_offset + bytes;
612 hammer_unlock(&hmp->blkmap_lock);
616 hammer_rel_buffer(buffer1, 0);
618 hammer_rel_buffer(buffer2, 0);
620 hammer_rel_buffer(buffer3, 0);
621 hammer_rel_volume(root_volume, 0);
622 *zone_offp = next_offset;
628 * Dereference a reservation structure. Upon the final release the
629 * underlying big-block is checked and if it is entirely free we delete
630 * any related HAMMER buffers to avoid potential conflicts with future
631 * reuse of the big-block.
634 hammer_blockmap_reserve_complete(hammer_mount_t hmp, hammer_reserve_t resv)
636 hammer_off_t base_offset;
639 KKASSERT(resv->refs > 0);
640 KKASSERT((resv->zone_offset & HAMMER_OFF_ZONE_MASK) ==
641 HAMMER_ZONE_RAW_BUFFER);
644 * Setting append_off to the max prevents any new allocations
645 * from occuring while we are trying to dispose of the reservation,
646 * allowing us to safely delete any related HAMMER buffers.
648 * If we are unable to clean out all related HAMMER buffers we
651 if (resv->refs == 1 && (resv->flags & HAMMER_RESF_LAYER2FREE)) {
652 resv->append_off = HAMMER_LARGEBLOCK_SIZE;
653 base_offset = resv->zone_offset & ~HAMMER_OFF_ZONE_MASK;
654 base_offset = HAMMER_ZONE_ENCODE(resv->zone, base_offset);
655 error = hammer_del_buffers(hmp, base_offset,
657 HAMMER_LARGEBLOCK_SIZE,
660 hammer_reserve_setdelay(hmp, resv);
662 if (--resv->refs == 0) {
663 KKASSERT((resv->flags & HAMMER_RESF_ONDELAY) == 0);
664 RB_REMOVE(hammer_res_rb_tree, &hmp->rb_resv_root, resv);
665 kfree(resv, hmp->m_misc);
666 --hammer_count_reservations;
671 * Prevent a potentially free big-block from being reused until after
672 * the related flushes have completely cycled, otherwise crash recovery
673 * could resurrect a data block that was already reused and overwritten.
675 * The caller might reset the underlying layer2 entry's append_off to 0, so
676 * our covering append_off must be set to max to prevent any reallocation
677 * until after the flush delays complete, not to mention proper invalidation
678 * of any underlying cached blocks.
681 hammer_reserve_setdelay_offset(hammer_mount_t hmp, hammer_off_t base_offset,
682 int zone, struct hammer_blockmap_layer2 *layer2)
684 hammer_reserve_t resv;
687 * Allocate the reservation if necessary.
689 * NOTE: need lock in future around resv lookup/allocation and
690 * the setdelay call, currently refs is not bumped until the call.
693 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root, base_offset);
695 resv = kmalloc(sizeof(*resv), hmp->m_misc,
696 M_WAITOK | M_ZERO | M_USE_RESERVE);
698 resv->zone_offset = base_offset;
700 resv->append_off = HAMMER_LARGEBLOCK_SIZE;
702 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE)
703 resv->flags |= HAMMER_RESF_LAYER2FREE;
704 if (RB_INSERT(hammer_res_rb_tree, &hmp->rb_resv_root, resv)) {
705 kfree(resv, hmp->m_misc);
708 ++hammer_count_reservations;
710 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE)
711 resv->flags |= HAMMER_RESF_LAYER2FREE;
713 hammer_reserve_setdelay(hmp, resv);
717 * Enter the reservation on the on-delay list, or move it if it
718 * is already on the list.
721 hammer_reserve_setdelay(hammer_mount_t hmp, hammer_reserve_t resv)
723 if (resv->flags & HAMMER_RESF_ONDELAY) {
724 TAILQ_REMOVE(&hmp->delay_list, resv, delay_entry);
725 resv->flush_group = hmp->flusher.next + 1;
726 TAILQ_INSERT_TAIL(&hmp->delay_list, resv, delay_entry);
729 ++hmp->rsv_fromdelay;
730 resv->flags |= HAMMER_RESF_ONDELAY;
731 resv->flush_group = hmp->flusher.next + 1;
732 TAILQ_INSERT_TAIL(&hmp->delay_list, resv, delay_entry);
737 hammer_reserve_clrdelay(hammer_mount_t hmp, hammer_reserve_t resv)
739 KKASSERT(resv->flags & HAMMER_RESF_ONDELAY);
740 resv->flags &= ~HAMMER_RESF_ONDELAY;
741 TAILQ_REMOVE(&hmp->delay_list, resv, delay_entry);
742 --hmp->rsv_fromdelay;
743 hammer_blockmap_reserve_complete(hmp, resv);
747 * Backend function - free (offset, bytes) in a zone.
752 hammer_blockmap_free(hammer_transaction_t trans,
753 hammer_off_t zone_offset, int bytes)
756 hammer_volume_t root_volume;
757 hammer_blockmap_t blockmap;
758 hammer_blockmap_t freemap;
759 struct hammer_blockmap_layer1 *layer1;
760 struct hammer_blockmap_layer2 *layer2;
761 hammer_buffer_t buffer1 = NULL;
762 hammer_buffer_t buffer2 = NULL;
763 hammer_off_t layer1_offset;
764 hammer_off_t layer2_offset;
765 hammer_off_t base_off;
776 bytes = (bytes + 15) & ~15;
777 KKASSERT(bytes <= HAMMER_XBUFSIZE);
778 KKASSERT(((zone_offset ^ (zone_offset + (bytes - 1))) &
779 ~HAMMER_LARGEBLOCK_MASK64) == 0);
782 * Basic zone validation & locking
784 zone = HAMMER_ZONE_DECODE(zone_offset);
785 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
786 root_volume = trans->rootvol;
789 blockmap = &hmp->blockmap[zone];
790 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
795 layer1_offset = freemap->phys_offset +
796 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
797 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
800 KKASSERT(layer1->phys_offset &&
801 layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
802 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
803 hammer_lock_ex(&hmp->blkmap_lock);
804 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
805 panic("CRC FAILED: LAYER1");
806 hammer_unlock(&hmp->blkmap_lock);
810 * Dive layer 2, each entry represents a large-block.
812 layer2_offset = layer1->phys_offset +
813 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
814 layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
817 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
818 hammer_lock_ex(&hmp->blkmap_lock);
819 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
820 panic("CRC FAILED: LAYER2");
821 hammer_unlock(&hmp->blkmap_lock);
824 hammer_lock_ex(&hmp->blkmap_lock);
826 hammer_modify_buffer(trans, buffer2, layer2, sizeof(*layer2));
829 * Free space previously allocated via blockmap_alloc().
831 * NOTE: bytes_free can be and remain negative due to de-dup ops
832 * but can never become larger than HAMMER_LARGEBLOCK_SIZE.
834 KKASSERT(layer2->zone == zone);
835 layer2->bytes_free += bytes;
836 KKASSERT(layer2->bytes_free <= HAMMER_LARGEBLOCK_SIZE);
839 * If a big-block becomes entirely free we must create a covering
840 * reservation to prevent premature reuse. Note, however, that
841 * the big-block and/or reservation may still have an append_off
842 * that allows further (non-reused) allocations.
844 * Once the reservation has been made we re-check layer2 and if
845 * the big-block is still entirely free we reset the layer2 entry.
846 * The reservation will prevent premature reuse.
848 * NOTE: hammer_buffer's are only invalidated when the reservation
849 * is completed, if the layer2 entry is still completely free at
850 * that time. Any allocations from the reservation that may have
851 * occured in the mean time, or active references on the reservation
852 * from new pending allocations, will prevent the invalidation from
855 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE) {
856 base_off = (zone_offset & (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) | HAMMER_ZONE_RAW_BUFFER;
858 hammer_reserve_setdelay_offset(hmp, base_off, zone, layer2);
859 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE) {
861 layer2->append_off = 0;
862 hammer_modify_buffer(trans, buffer1,
863 layer1, sizeof(*layer1));
864 ++layer1->blocks_free;
865 layer1->layer1_crc = crc32(layer1,
866 HAMMER_LAYER1_CRCSIZE);
867 hammer_modify_buffer_done(buffer1);
868 hammer_modify_volume_field(trans,
870 vol0_stat_freebigblocks);
871 ++root_volume->ondisk->vol0_stat_freebigblocks;
872 hmp->copy_stat_freebigblocks =
873 root_volume->ondisk->vol0_stat_freebigblocks;
874 hammer_modify_volume_done(trans->rootvol);
877 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
878 hammer_modify_buffer_done(buffer2);
879 hammer_unlock(&hmp->blkmap_lock);
883 hammer_rel_buffer(buffer1, 0);
885 hammer_rel_buffer(buffer2, 0);
889 * Backend function - finalize (offset, bytes) in a zone.
891 * Allocate space that was previously reserved by the frontend.
894 hammer_blockmap_finalize(hammer_transaction_t trans,
895 hammer_reserve_t resv,
896 hammer_off_t zone_offset, int bytes)
899 hammer_volume_t root_volume;
900 hammer_blockmap_t blockmap;
901 hammer_blockmap_t freemap;
902 struct hammer_blockmap_layer1 *layer1;
903 struct hammer_blockmap_layer2 *layer2;
904 hammer_buffer_t buffer1 = NULL;
905 hammer_buffer_t buffer2 = NULL;
906 hammer_off_t layer1_offset;
907 hammer_off_t layer2_offset;
919 bytes = (bytes + 15) & ~15;
920 KKASSERT(bytes <= HAMMER_XBUFSIZE);
923 * Basic zone validation & locking
925 zone = HAMMER_ZONE_DECODE(zone_offset);
926 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
927 root_volume = trans->rootvol;
930 blockmap = &hmp->blockmap[zone];
931 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
936 layer1_offset = freemap->phys_offset +
937 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
938 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
941 KKASSERT(layer1->phys_offset &&
942 layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
943 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
944 hammer_lock_ex(&hmp->blkmap_lock);
945 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
946 panic("CRC FAILED: LAYER1");
947 hammer_unlock(&hmp->blkmap_lock);
951 * Dive layer 2, each entry represents a large-block.
953 layer2_offset = layer1->phys_offset +
954 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
955 layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
958 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
959 hammer_lock_ex(&hmp->blkmap_lock);
960 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
961 panic("CRC FAILED: LAYER2");
962 hammer_unlock(&hmp->blkmap_lock);
965 hammer_lock_ex(&hmp->blkmap_lock);
967 hammer_modify_buffer(trans, buffer2, layer2, sizeof(*layer2));
970 * Finalize some or all of the space covered by a current
971 * reservation. An allocation in the same layer may have
972 * already assigned ownership.
974 if (layer2->zone == 0) {
975 hammer_modify_buffer(trans, buffer1,
976 layer1, sizeof(*layer1));
977 --layer1->blocks_free;
978 layer1->layer1_crc = crc32(layer1,
979 HAMMER_LAYER1_CRCSIZE);
980 hammer_modify_buffer_done(buffer1);
982 KKASSERT(layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE);
983 KKASSERT(layer2->append_off == 0);
984 hammer_modify_volume_field(trans,
986 vol0_stat_freebigblocks);
987 --root_volume->ondisk->vol0_stat_freebigblocks;
988 hmp->copy_stat_freebigblocks =
989 root_volume->ondisk->vol0_stat_freebigblocks;
990 hammer_modify_volume_done(trans->rootvol);
992 if (layer2->zone != zone)
993 kprintf("layer2 zone mismatch %d %d\n", layer2->zone, zone);
994 KKASSERT(layer2->zone == zone);
995 KKASSERT(bytes != 0);
996 layer2->bytes_free -= bytes;
998 resv->flags &= ~HAMMER_RESF_LAYER2FREE;
1001 * Finalizations can occur out of order, or combined with allocations.
1002 * append_off must be set to the highest allocated offset.
1004 offset = ((int)zone_offset & HAMMER_LARGEBLOCK_MASK) + bytes;
1005 if (layer2->append_off < offset)
1006 layer2->append_off = offset;
1008 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
1009 hammer_modify_buffer_done(buffer2);
1010 hammer_unlock(&hmp->blkmap_lock);
1014 hammer_rel_buffer(buffer1, 0);
1016 hammer_rel_buffer(buffer2, 0);
1021 * Return the approximate number of free bytes in the big-block
1022 * containing the specified blockmap offset.
1024 * WARNING: A negative number can be returned if data de-dup exists,
1025 * and the result will also not represent he actual number
1026 * of free bytes in this case.
1028 * This code is used only by the reblocker.
1031 hammer_blockmap_getfree(hammer_mount_t hmp, hammer_off_t zone_offset,
1032 int *curp, int *errorp)
1034 hammer_volume_t root_volume;
1035 hammer_blockmap_t blockmap;
1036 hammer_blockmap_t freemap;
1037 struct hammer_blockmap_layer1 *layer1;
1038 struct hammer_blockmap_layer2 *layer2;
1039 hammer_buffer_t buffer = NULL;
1040 hammer_off_t layer1_offset;
1041 hammer_off_t layer2_offset;
1045 zone = HAMMER_ZONE_DECODE(zone_offset);
1046 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
1047 root_volume = hammer_get_root_volume(hmp, errorp);
1052 blockmap = &hmp->blockmap[zone];
1053 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
1058 layer1_offset = freemap->phys_offset +
1059 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
1060 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer);
1065 KKASSERT(layer1->phys_offset);
1066 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
1067 hammer_lock_ex(&hmp->blkmap_lock);
1068 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
1069 panic("CRC FAILED: LAYER1");
1070 hammer_unlock(&hmp->blkmap_lock);
1074 * Dive layer 2, each entry represents a large-block.
1076 * (reuse buffer, layer1 pointer becomes invalid)
1078 layer2_offset = layer1->phys_offset +
1079 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
1080 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer);
1085 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
1086 hammer_lock_ex(&hmp->blkmap_lock);
1087 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
1088 panic("CRC FAILED: LAYER2");
1089 hammer_unlock(&hmp->blkmap_lock);
1091 KKASSERT(layer2->zone == zone);
1093 bytes = layer2->bytes_free;
1095 if ((blockmap->next_offset ^ zone_offset) & ~HAMMER_LARGEBLOCK_MASK64)
1101 hammer_rel_buffer(buffer, 0);
1102 hammer_rel_volume(root_volume, 0);
1103 if (hammer_debug_general & 0x0800) {
1104 kprintf("hammer_blockmap_getfree: %016llx -> %d\n",
1105 (long long)zone_offset, bytes);
1112 * Lookup a blockmap offset.
1115 hammer_blockmap_lookup(hammer_mount_t hmp, hammer_off_t zone_offset,
1118 hammer_volume_t root_volume;
1119 hammer_blockmap_t freemap;
1120 struct hammer_blockmap_layer1 *layer1;
1121 struct hammer_blockmap_layer2 *layer2;
1122 hammer_buffer_t buffer = NULL;
1123 hammer_off_t layer1_offset;
1124 hammer_off_t layer2_offset;
1125 hammer_off_t result_offset;
1126 hammer_off_t base_off;
1127 hammer_reserve_t resv;
1131 * Calculate the zone-2 offset.
1133 zone = HAMMER_ZONE_DECODE(zone_offset);
1134 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
1136 result_offset = (zone_offset & ~HAMMER_OFF_ZONE_MASK) |
1137 HAMMER_ZONE_RAW_BUFFER;
1140 * We can actually stop here, normal blockmaps are now direct-mapped
1141 * onto the freemap and so represent zone-2 addresses.
1143 if (hammer_verify_zone == 0) {
1145 return(result_offset);
1149 * Validate the allocation zone
1151 root_volume = hammer_get_root_volume(hmp, errorp);
1154 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
1155 KKASSERT(freemap->phys_offset != 0);
1160 layer1_offset = freemap->phys_offset +
1161 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
1162 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer);
1165 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
1166 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
1167 hammer_lock_ex(&hmp->blkmap_lock);
1168 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
1169 panic("CRC FAILED: LAYER1");
1170 hammer_unlock(&hmp->blkmap_lock);
1174 * Dive layer 2, each entry represents a large-block.
1176 layer2_offset = layer1->phys_offset +
1177 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
1178 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer);
1182 if (layer2->zone == 0) {
1183 base_off = (zone_offset & (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) | HAMMER_ZONE_RAW_BUFFER;
1184 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root,
1186 KKASSERT(resv && resv->zone == zone);
1188 } else if (layer2->zone != zone) {
1189 panic("hammer_blockmap_lookup: bad zone %d/%d\n",
1190 layer2->zone, zone);
1192 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
1193 hammer_lock_ex(&hmp->blkmap_lock);
1194 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
1195 panic("CRC FAILED: LAYER2");
1196 hammer_unlock(&hmp->blkmap_lock);
1201 hammer_rel_buffer(buffer, 0);
1202 hammer_rel_volume(root_volume, 0);
1203 if (hammer_debug_general & 0x0800) {
1204 kprintf("hammer_blockmap_lookup: %016llx -> %016llx\n",
1205 (long long)zone_offset, (long long)result_offset);
1207 return(result_offset);
1212 * Check space availability
1215 _hammer_checkspace(hammer_mount_t hmp, int slop, int64_t *resp)
1217 const int in_size = sizeof(struct hammer_inode_data) +
1218 sizeof(union hammer_btree_elm);
1219 const int rec_size = (sizeof(union hammer_btree_elm) * 2);
1222 usedbytes = hmp->rsv_inodes * in_size +
1223 hmp->rsv_recs * rec_size +
1224 hmp->rsv_databytes +
1225 ((int64_t)hmp->rsv_fromdelay << HAMMER_LARGEBLOCK_BITS) +
1226 ((int64_t)hidirtybufspace << 2) +
1227 (slop << HAMMER_LARGEBLOCK_BITS);
1229 hammer_count_extra_space_used = usedbytes; /* debugging */
1233 if (hmp->copy_stat_freebigblocks >=
1234 (usedbytes >> HAMMER_LARGEBLOCK_BITS)) {