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(hammer_mount_t hmp,
44 hammer_off_t base_offset,
45 struct hammer_blockmap_layer2 *layer2);
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,
69 int bytes, 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 */
93 * Deal with alignment and buffer-boundary issues.
95 * Be careful, certain primary alignments are used below to allocate
96 * new blockmap blocks.
98 bytes = (bytes + 15) & ~15;
99 KKASSERT(bytes > 0 && bytes <= HAMMER_XBUFSIZE);
100 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
105 root_volume = trans->rootvol;
107 blockmap = &hmp->blockmap[zone];
108 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
109 KKASSERT(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
111 next_offset = blockmap->next_offset;
116 if (next_offset == HAMMER_ZONE_ENCODE(zone + 1, 0)) {
122 next_offset = HAMMER_ZONE_ENCODE(zone, 0);
126 * The allocation request may not cross a buffer boundary. Special
127 * large allocations must not cross a large-block boundary.
129 tmp_offset = next_offset + bytes - 1;
130 if (bytes <= HAMMER_BUFSIZE) {
131 if ((next_offset ^ tmp_offset) & ~HAMMER_BUFMASK64) {
132 next_offset = tmp_offset & ~HAMMER_BUFMASK64;
136 if ((next_offset ^ tmp_offset) & ~HAMMER_LARGEBLOCK_MASK64) {
137 next_offset = tmp_offset & ~HAMMER_LARGEBLOCK_MASK64;
141 offset = (int)next_offset & HAMMER_LARGEBLOCK_MASK;
146 layer1_offset = freemap->phys_offset +
147 HAMMER_BLOCKMAP_LAYER1_OFFSET(next_offset);
148 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer1);
157 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
158 Debugger("CRC FAILED: LAYER1");
162 * If we are at a big-block boundary and layer1 indicates no
163 * free big-blocks, then we cannot allocate a new bigblock in
164 * layer2, skip to the next layer1 entry.
166 if (offset == 0 && layer1->blocks_free == 0) {
167 next_offset = (next_offset + HAMMER_BLOCKMAP_LAYER2) &
168 ~HAMMER_BLOCKMAP_LAYER2_MASK;
171 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
174 * Dive layer 2, each entry represents a large-block.
176 layer2_offset = layer1->phys_offset +
177 HAMMER_BLOCKMAP_LAYER2_OFFSET(next_offset);
178 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer2);
187 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
188 Debugger("CRC FAILED: LAYER2");
192 * Skip the layer if the zone is owned by someone other then us.
194 if (layer2->zone && layer2->zone != zone) {
195 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
198 if (offset < layer2->append_off) {
199 next_offset += layer2->append_off - offset;
204 * We need the lock from this point on. We have to re-check zone
205 * ownership after acquiring the lock and also check for reservations.
207 hammer_lock_ex(&hmp->blkmap_lock);
209 if (layer2->zone && layer2->zone != zone) {
210 hammer_unlock(&hmp->blkmap_lock);
211 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
214 if (offset < layer2->append_off) {
215 hammer_unlock(&hmp->blkmap_lock);
216 next_offset += layer2->append_off - offset;
221 * The bigblock might be reserved by another zone. If it is reserved
222 * by our zone we may have to move next_offset past the append_off.
224 base_off = (next_offset &
225 (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) |
226 HAMMER_ZONE_RAW_BUFFER;
227 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root, base_off);
229 if (resv->zone != zone) {
230 hammer_unlock(&hmp->blkmap_lock);
231 next_offset = (next_offset + HAMMER_LARGEBLOCK_SIZE) &
232 ~HAMMER_LARGEBLOCK_MASK64;
235 if (offset < resv->append_off) {
236 hammer_unlock(&hmp->blkmap_lock);
237 next_offset += resv->append_off - offset;
243 * Ok, we can allocate out of this layer2 big-block. Assume ownership
244 * of the layer for real. At this point we've validated any
245 * reservation that might exist and can just ignore resv.
247 if (layer2->zone == 0) {
249 * Assign the bigblock to our zone
251 hammer_modify_buffer(trans, buffer1,
252 layer1, sizeof(*layer1));
253 --layer1->blocks_free;
254 layer1->layer1_crc = crc32(layer1,
255 HAMMER_LAYER1_CRCSIZE);
256 hammer_modify_buffer_done(buffer1);
257 hammer_modify_buffer(trans, buffer2,
258 layer2, sizeof(*layer2));
260 KKASSERT(layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE);
261 KKASSERT(layer2->append_off == 0);
262 hammer_modify_volume_field(trans, trans->rootvol,
263 vol0_stat_freebigblocks);
264 --root_volume->ondisk->vol0_stat_freebigblocks;
265 hmp->copy_stat_freebigblocks =
266 root_volume->ondisk->vol0_stat_freebigblocks;
267 hammer_modify_volume_done(trans->rootvol);
269 hammer_modify_buffer(trans, buffer2,
270 layer2, sizeof(*layer2));
272 KKASSERT(layer2->zone == zone);
274 layer2->bytes_free -= bytes;
275 KKASSERT(layer2->append_off <= offset);
276 layer2->append_off = offset + bytes;
277 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
278 hammer_modify_buffer_done(buffer2);
279 KKASSERT(layer2->bytes_free >= 0);
282 KKASSERT(resv->append_off <= offset);
283 resv->append_off = offset + bytes;
284 resv->flags &= ~HAMMER_RESF_LAYER2FREE;
288 * If we are allocating from the base of a new buffer we can avoid
289 * a disk read by calling hammer_bnew().
291 if ((next_offset & HAMMER_BUFMASK) == 0) {
292 hammer_bnew_ext(trans->hmp, next_offset, bytes,
295 result_offset = next_offset;
298 * Process allocated result_offset
300 hammer_modify_volume(NULL, root_volume, NULL, 0);
301 blockmap->next_offset = next_offset + bytes;
302 hammer_modify_volume_done(root_volume);
303 hammer_unlock(&hmp->blkmap_lock);
310 hammer_rel_buffer(buffer1, 0);
312 hammer_rel_buffer(buffer2, 0);
314 hammer_rel_buffer(buffer3, 0);
316 return(result_offset);
320 * Frontend function - Reserve bytes in a zone.
322 * This code reserves bytes out of a blockmap without committing to any
323 * meta-data modifications, allowing the front-end to directly issue disk
324 * write I/O for large blocks of data
326 * The backend later finalizes the reservation with hammer_blockmap_finalize()
327 * upon committing the related record.
330 hammer_blockmap_reserve(hammer_mount_t hmp, int zone, int bytes,
331 hammer_off_t *zone_offp, int *errorp)
333 hammer_volume_t root_volume;
334 hammer_blockmap_t blockmap;
335 hammer_blockmap_t freemap;
336 struct hammer_blockmap_layer1 *layer1;
337 struct hammer_blockmap_layer2 *layer2;
338 hammer_buffer_t buffer1 = NULL;
339 hammer_buffer_t buffer2 = NULL;
340 hammer_buffer_t buffer3 = NULL;
341 hammer_off_t tmp_offset;
342 hammer_off_t next_offset;
343 hammer_off_t layer1_offset;
344 hammer_off_t layer2_offset;
345 hammer_off_t base_off;
346 hammer_reserve_t resv;
347 hammer_reserve_t resx;
354 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
355 root_volume = hammer_get_root_volume(hmp, errorp);
358 blockmap = &hmp->blockmap[zone];
359 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
360 KKASSERT(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
363 * Deal with alignment and buffer-boundary issues.
365 * Be careful, certain primary alignments are used below to allocate
366 * new blockmap blocks.
368 bytes = (bytes + 15) & ~15;
369 KKASSERT(bytes > 0 && bytes <= HAMMER_XBUFSIZE);
371 next_offset = blockmap->next_offset;
377 if (next_offset == HAMMER_ZONE_ENCODE(zone + 1, 0)) {
382 next_offset = HAMMER_ZONE_ENCODE(zone, 0);
386 * The allocation request may not cross a buffer boundary. Special
387 * large allocations must not cross a large-block boundary.
389 tmp_offset = next_offset + bytes - 1;
390 if (bytes <= HAMMER_BUFSIZE) {
391 if ((next_offset ^ tmp_offset) & ~HAMMER_BUFMASK64) {
392 next_offset = tmp_offset & ~HAMMER_BUFMASK64;
396 if ((next_offset ^ tmp_offset) & ~HAMMER_LARGEBLOCK_MASK64) {
397 next_offset = tmp_offset & ~HAMMER_LARGEBLOCK_MASK64;
401 offset = (int)next_offset & HAMMER_LARGEBLOCK_MASK;
406 layer1_offset = freemap->phys_offset +
407 HAMMER_BLOCKMAP_LAYER1_OFFSET(next_offset);
408 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer1);
415 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
416 Debugger("CRC FAILED: LAYER1");
420 * If we are at a big-block boundary and layer1 indicates no
421 * free big-blocks, then we cannot allocate a new bigblock in
422 * layer2, skip to the next layer1 entry.
424 if ((next_offset & HAMMER_LARGEBLOCK_MASK) == 0 &&
425 layer1->blocks_free == 0) {
426 next_offset = (next_offset + HAMMER_BLOCKMAP_LAYER2) &
427 ~HAMMER_BLOCKMAP_LAYER2_MASK;
430 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
433 * Dive layer 2, each entry represents a large-block.
435 layer2_offset = layer1->phys_offset +
436 HAMMER_BLOCKMAP_LAYER2_OFFSET(next_offset);
437 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer2);
442 * Check CRC if not allocating into uninitialized space (which we
443 * aren't when reserving space).
445 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
446 Debugger("CRC FAILED: LAYER2");
450 * Skip the layer if the zone is owned by someone other then us.
452 if (layer2->zone && layer2->zone != zone) {
453 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
456 if (offset < layer2->append_off) {
457 next_offset += layer2->append_off - offset;
462 * We need the lock from this point on. We have to re-check zone
463 * ownership after acquiring the lock and also check for reservations.
465 hammer_lock_ex(&hmp->blkmap_lock);
467 if (layer2->zone && layer2->zone != zone) {
468 hammer_unlock(&hmp->blkmap_lock);
469 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
472 if (offset < layer2->append_off) {
473 hammer_unlock(&hmp->blkmap_lock);
474 next_offset += layer2->append_off - offset;
479 * The bigblock might be reserved by another zone. If it is reserved
480 * by our zone we may have to move next_offset past the append_off.
482 base_off = (next_offset &
483 (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) |
484 HAMMER_ZONE_RAW_BUFFER;
485 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root, base_off);
487 if (resv->zone != zone) {
488 hammer_unlock(&hmp->blkmap_lock);
489 next_offset = (next_offset + HAMMER_LARGEBLOCK_SIZE) &
490 ~HAMMER_LARGEBLOCK_MASK64;
493 if (offset < resv->append_off) {
494 hammer_unlock(&hmp->blkmap_lock);
495 next_offset += resv->append_off - offset;
501 resx = kmalloc(sizeof(*resv), hmp->m_misc,
502 M_WAITOK | M_ZERO | M_USE_RESERVE);
505 resx->zone_offset = base_off;
506 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE)
507 resx->flags |= HAMMER_RESF_LAYER2FREE;
508 resv = RB_INSERT(hammer_res_rb_tree, &hmp->rb_resv_root, resx);
509 KKASSERT(resv == NULL);
511 ++hammer_count_reservations;
513 resv->append_off = offset + bytes;
516 * If we are not reserving a whole buffer but are at the start of
517 * a new block, call hammer_bnew() to avoid a disk read.
519 * If we are reserving a whole buffer (or more), the caller will
520 * probably use a direct read, so do nothing.
522 if (bytes < HAMMER_BUFSIZE && (next_offset & HAMMER_BUFMASK) == 0) {
523 hammer_bnew(hmp, next_offset, errorp, &buffer3);
527 * Adjust our iterator and alloc_offset. The layer1 and layer2
528 * space beyond alloc_offset is uninitialized. alloc_offset must
529 * be big-block aligned.
531 blockmap->next_offset = next_offset + bytes;
532 hammer_unlock(&hmp->blkmap_lock);
536 hammer_rel_buffer(buffer1, 0);
538 hammer_rel_buffer(buffer2, 0);
540 hammer_rel_buffer(buffer3, 0);
541 hammer_rel_volume(root_volume, 0);
542 *zone_offp = next_offset;
549 * Backend function - undo a portion of a reservation.
552 hammer_blockmap_reserve_undo(hammer_mount_t hmp, hammer_reserve_t resv,
553 hammer_off_t zone_offset, int bytes)
555 resv->bytes_freed += bytes;
561 * Dereference a reservation structure. Upon the final release the
562 * underlying big-block is checked and if it is entirely free we delete
563 * any related HAMMER buffers to avoid potential conflicts with future
564 * reuse of the big-block.
567 hammer_blockmap_reserve_complete(hammer_mount_t hmp, hammer_reserve_t resv)
569 hammer_off_t base_offset;
571 KKASSERT(resv->refs > 0);
572 KKASSERT((resv->zone_offset & HAMMER_OFF_ZONE_MASK) ==
573 HAMMER_ZONE_RAW_BUFFER);
576 * Setting append_off to the max prevents any new allocations
577 * from occuring while we are trying to dispose of the reservation,
578 * allowing us to safely delete any related HAMMER buffers.
580 if (resv->refs == 1 && (resv->flags & HAMMER_RESF_LAYER2FREE)) {
581 resv->append_off = HAMMER_LARGEBLOCK_SIZE;
582 resv->flags &= ~HAMMER_RESF_LAYER2FREE;
583 base_offset = resv->zone_offset & ~HAMMER_ZONE_RAW_BUFFER;
584 base_offset = HAMMER_ZONE_ENCODE(base_offset, resv->zone);
585 hammer_del_buffers(hmp, base_offset, resv->zone_offset,
586 HAMMER_LARGEBLOCK_SIZE);
588 if (--resv->refs == 0) {
589 KKASSERT((resv->flags & HAMMER_RESF_ONDELAY) == 0);
590 RB_REMOVE(hammer_res_rb_tree, &hmp->rb_resv_root, resv);
591 kfree(resv, hmp->m_misc);
592 --hammer_count_reservations;
597 * Prevent a potentially free big-block from being reused until after
598 * the related flushes have completely cycled, otherwise crash recovery
599 * could resurrect a data block that was already reused and overwritten.
601 * Return 0 if the layer2 entry is still completely free after the
602 * reservation has been allocated.
605 hammer_reserve_setdelay(hammer_mount_t hmp, hammer_off_t base_offset,
606 struct hammer_blockmap_layer2 *layer2)
608 hammer_reserve_t resv;
611 * Allocate the reservation if necessary.
614 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root, base_offset);
616 resv = kmalloc(sizeof(*resv), hmp->m_misc,
617 M_WAITOK | M_ZERO | M_USE_RESERVE);
618 resv->zone_offset = base_offset;
620 /* XXX inherent lock until refs bumped later on */
621 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE)
622 resv->flags |= HAMMER_RESF_LAYER2FREE;
623 if (RB_INSERT(hammer_res_rb_tree, &hmp->rb_resv_root, resv)) {
624 kfree(resv, hmp->m_misc);
627 ++hammer_count_reservations;
631 * Enter the reservation on the on-delay list, or move it if it
632 * is already on the list.
634 if (resv->flags & HAMMER_RESF_ONDELAY) {
635 TAILQ_REMOVE(&hmp->delay_list, resv, delay_entry);
636 resv->flush_group = hmp->flusher.next + 1;
637 TAILQ_INSERT_TAIL(&hmp->delay_list, resv, delay_entry);
640 ++hmp->rsv_fromdelay;
641 resv->flags |= HAMMER_RESF_ONDELAY;
642 resv->flush_group = hmp->flusher.next + 1;
643 TAILQ_INSERT_TAIL(&hmp->delay_list, resv, delay_entry);
648 hammer_reserve_clrdelay(hammer_mount_t hmp, hammer_reserve_t resv)
650 KKASSERT(resv->flags & HAMMER_RESF_ONDELAY);
651 resv->flags &= ~HAMMER_RESF_ONDELAY;
652 TAILQ_REMOVE(&hmp->delay_list, resv, delay_entry);
653 --hmp->rsv_fromdelay;
654 hammer_blockmap_reserve_complete(hmp, resv);
658 * Backend function - free (offset, bytes) in a zone.
663 hammer_blockmap_free(hammer_transaction_t trans,
664 hammer_off_t zone_offset, int bytes)
667 hammer_volume_t root_volume;
668 hammer_blockmap_t blockmap;
669 hammer_blockmap_t freemap;
670 struct hammer_blockmap_layer1 *layer1;
671 struct hammer_blockmap_layer2 *layer2;
672 hammer_buffer_t buffer1 = NULL;
673 hammer_buffer_t buffer2 = NULL;
674 hammer_off_t layer1_offset;
675 hammer_off_t layer2_offset;
676 hammer_off_t base_off;
687 bytes = (bytes + 15) & ~15;
688 KKASSERT(bytes <= HAMMER_XBUFSIZE);
689 KKASSERT(((zone_offset ^ (zone_offset + (bytes - 1))) &
690 ~HAMMER_LARGEBLOCK_MASK64) == 0);
693 * Basic zone validation & locking
695 zone = HAMMER_ZONE_DECODE(zone_offset);
696 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
697 root_volume = trans->rootvol;
700 blockmap = &hmp->blockmap[zone];
701 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
706 layer1_offset = freemap->phys_offset +
707 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
708 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
711 KKASSERT(layer1->phys_offset &&
712 layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
713 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
714 Debugger("CRC FAILED: LAYER1");
718 * Dive layer 2, each entry represents a large-block.
720 layer2_offset = layer1->phys_offset +
721 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
722 layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
725 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
726 Debugger("CRC FAILED: LAYER2");
729 hammer_lock_ex(&hmp->blkmap_lock);
731 hammer_modify_buffer(trans, buffer2, layer2, sizeof(*layer2));
734 * Free space previously allocated via blockmap_alloc().
736 KKASSERT(layer2->zone == zone);
737 layer2->bytes_free += bytes;
738 KKASSERT(layer2->bytes_free <= HAMMER_LARGEBLOCK_SIZE);
741 * If a big-block becomes entirely free we must create a covering
742 * reservation to prevent premature reuse. Note, however, that
743 * the big-block and/or reservation may still have an append_off
744 * that allows further (non-reused) allocations.
746 * Once the reservation has been made we re-check layer2 and if
747 * the big-block is still entirely free we reset the layer2 entry.
748 * The reservation will prevent premature reuse.
750 * NOTE: hammer_buffer's are only invalidated when the reservation
751 * is completed, if the layer2 entry is still completely free at
752 * that time. Any allocations from the reservation that may have
753 * occured in the mean time, or active references on the reservation
754 * from new pending allocations, will prevent the invalidation from
757 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE) {
758 base_off = (zone_offset & (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) | HAMMER_ZONE_RAW_BUFFER;
760 hammer_reserve_setdelay(hmp, base_off, layer2);
761 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE) {
763 layer2->append_off = 0;
764 hammer_modify_buffer(trans, buffer1,
765 layer1, sizeof(*layer1));
766 ++layer1->blocks_free;
767 layer1->layer1_crc = crc32(layer1,
768 HAMMER_LAYER1_CRCSIZE);
769 hammer_modify_buffer_done(buffer1);
770 hammer_modify_volume_field(trans,
772 vol0_stat_freebigblocks);
773 ++root_volume->ondisk->vol0_stat_freebigblocks;
774 hmp->copy_stat_freebigblocks =
775 root_volume->ondisk->vol0_stat_freebigblocks;
776 hammer_modify_volume_done(trans->rootvol);
779 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
780 hammer_modify_buffer_done(buffer2);
781 hammer_unlock(&hmp->blkmap_lock);
785 hammer_rel_buffer(buffer1, 0);
787 hammer_rel_buffer(buffer2, 0);
791 * Backend function - finalize (offset, bytes) in a zone.
793 * Allocate space that was previously reserved by the frontend.
796 hammer_blockmap_finalize(hammer_transaction_t trans,
797 hammer_reserve_t resv,
798 hammer_off_t zone_offset, int bytes)
801 hammer_volume_t root_volume;
802 hammer_blockmap_t blockmap;
803 hammer_blockmap_t freemap;
804 struct hammer_blockmap_layer1 *layer1;
805 struct hammer_blockmap_layer2 *layer2;
806 hammer_buffer_t buffer1 = NULL;
807 hammer_buffer_t buffer2 = NULL;
808 hammer_off_t layer1_offset;
809 hammer_off_t layer2_offset;
821 bytes = (bytes + 15) & ~15;
822 KKASSERT(bytes <= HAMMER_XBUFSIZE);
825 * Basic zone validation & locking
827 zone = HAMMER_ZONE_DECODE(zone_offset);
828 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
829 root_volume = trans->rootvol;
832 blockmap = &hmp->blockmap[zone];
833 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
838 layer1_offset = freemap->phys_offset +
839 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
840 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
843 KKASSERT(layer1->phys_offset &&
844 layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
845 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
846 Debugger("CRC FAILED: LAYER1");
850 * Dive layer 2, each entry represents a large-block.
852 layer2_offset = layer1->phys_offset +
853 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
854 layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
857 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
858 Debugger("CRC FAILED: LAYER2");
861 hammer_lock_ex(&hmp->blkmap_lock);
863 hammer_modify_buffer(trans, buffer2, layer2, sizeof(*layer2));
866 * Finalize some or all of the space covered by a current
867 * reservation. An allocation in the same layer may have
868 * already assigned ownership.
870 if (layer2->zone == 0) {
871 hammer_modify_buffer(trans, buffer1,
872 layer1, sizeof(*layer1));
873 --layer1->blocks_free;
874 layer1->layer1_crc = crc32(layer1,
875 HAMMER_LAYER1_CRCSIZE);
876 hammer_modify_buffer_done(buffer1);
878 KKASSERT(layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE);
879 KKASSERT(layer2->append_off == 0);
880 hammer_modify_volume_field(trans,
882 vol0_stat_freebigblocks);
883 --root_volume->ondisk->vol0_stat_freebigblocks;
884 hmp->copy_stat_freebigblocks =
885 root_volume->ondisk->vol0_stat_freebigblocks;
886 hammer_modify_volume_done(trans->rootvol);
888 if (layer2->zone != zone)
889 kprintf("layer2 zone mismatch %d %d\n", layer2->zone, zone);
890 KKASSERT(layer2->zone == zone);
891 layer2->bytes_free -= bytes;
893 resv->flags &= ~HAMMER_RESF_LAYER2FREE;
896 * Finalizations can occur out of order, or combined with allocations.
897 * append_off must be set to the highest allocated offset.
899 offset = ((int)zone_offset & HAMMER_LARGEBLOCK_MASK) + bytes;
900 if (layer2->append_off < offset)
901 layer2->append_off = offset;
903 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
904 hammer_modify_buffer_done(buffer2);
905 hammer_unlock(&hmp->blkmap_lock);
909 hammer_rel_buffer(buffer1, 0);
911 hammer_rel_buffer(buffer2, 0);
916 * Return the number of free bytes in the big-block containing the
917 * specified blockmap offset.
920 hammer_blockmap_getfree(hammer_mount_t hmp, hammer_off_t zone_offset,
921 int *curp, int *errorp)
923 hammer_volume_t root_volume;
924 hammer_blockmap_t blockmap;
925 hammer_blockmap_t freemap;
926 struct hammer_blockmap_layer1 *layer1;
927 struct hammer_blockmap_layer2 *layer2;
928 hammer_buffer_t buffer = NULL;
929 hammer_off_t layer1_offset;
930 hammer_off_t layer2_offset;
934 zone = HAMMER_ZONE_DECODE(zone_offset);
935 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
936 root_volume = hammer_get_root_volume(hmp, errorp);
941 blockmap = &hmp->blockmap[zone];
942 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
947 layer1_offset = freemap->phys_offset +
948 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
949 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer);
954 KKASSERT(layer1->phys_offset);
955 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
956 Debugger("CRC FAILED: LAYER1");
960 * Dive layer 2, each entry represents a large-block.
962 * (reuse buffer, layer1 pointer becomes invalid)
964 layer2_offset = layer1->phys_offset +
965 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
966 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer);
971 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
972 Debugger("CRC FAILED: LAYER2");
974 KKASSERT(layer2->zone == zone);
976 bytes = layer2->bytes_free;
978 if ((blockmap->next_offset ^ zone_offset) & ~HAMMER_LARGEBLOCK_MASK64)
984 hammer_rel_buffer(buffer, 0);
985 hammer_rel_volume(root_volume, 0);
986 if (hammer_debug_general & 0x0800) {
987 kprintf("hammer_blockmap_getfree: %016llx -> %d\n",
995 * Lookup a blockmap offset.
998 hammer_blockmap_lookup(hammer_mount_t hmp, hammer_off_t zone_offset,
1001 hammer_volume_t root_volume;
1002 hammer_blockmap_t freemap;
1003 struct hammer_blockmap_layer1 *layer1;
1004 struct hammer_blockmap_layer2 *layer2;
1005 hammer_buffer_t buffer = NULL;
1006 hammer_off_t layer1_offset;
1007 hammer_off_t layer2_offset;
1008 hammer_off_t result_offset;
1009 hammer_off_t base_off;
1010 hammer_reserve_t resv;
1014 * Calculate the zone-2 offset.
1016 zone = HAMMER_ZONE_DECODE(zone_offset);
1017 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
1019 result_offset = (zone_offset & ~HAMMER_OFF_ZONE_MASK) |
1020 HAMMER_ZONE_RAW_BUFFER;
1023 * We can actually stop here, normal blockmaps are now direct-mapped
1024 * onto the freemap and so represent zone-2 addresses.
1026 if (hammer_verify_zone == 0) {
1028 return(result_offset);
1032 * Validate the allocation zone
1034 root_volume = hammer_get_root_volume(hmp, errorp);
1037 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
1038 KKASSERT(freemap->phys_offset != 0);
1043 layer1_offset = freemap->phys_offset +
1044 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
1045 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer);
1048 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
1049 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
1050 Debugger("CRC FAILED: LAYER1");
1054 * Dive layer 2, each entry represents a large-block.
1056 layer2_offset = layer1->phys_offset +
1057 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
1058 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer);
1062 if (layer2->zone == 0) {
1063 base_off = (zone_offset & (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) | HAMMER_ZONE_RAW_BUFFER;
1064 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root,
1066 KKASSERT(resv && resv->zone == zone);
1068 } else if (layer2->zone != zone) {
1069 panic("hammer_blockmap_lookup: bad zone %d/%d\n",
1070 layer2->zone, zone);
1072 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
1073 Debugger("CRC FAILED: LAYER2");
1078 hammer_rel_buffer(buffer, 0);
1079 hammer_rel_volume(root_volume, 0);
1080 if (hammer_debug_general & 0x0800) {
1081 kprintf("hammer_blockmap_lookup: %016llx -> %016llx\n",
1082 zone_offset, result_offset);
1084 return(result_offset);
1089 * Check space availability
1092 hammer_checkspace(hammer_mount_t hmp, int slop)
1094 const int in_size = sizeof(struct hammer_inode_data) +
1095 sizeof(union hammer_btree_elm);
1096 const int rec_size = (sizeof(union hammer_btree_elm) * 2);
1099 usedbytes = hmp->rsv_inodes * in_size +
1100 hmp->rsv_recs * rec_size +
1101 hmp->rsv_databytes +
1102 ((int64_t)hmp->rsv_fromdelay << HAMMER_LARGEBLOCK_BITS) +
1103 ((int64_t)hidirtybufspace << 2) +
1104 (slop << HAMMER_LARGEBLOCK_BITS);
1106 hammer_count_extra_space_used = usedbytes; /* debugging */
1108 if (hmp->copy_stat_freebigblocks >=
1109 (usedbytes >> HAMMER_LARGEBLOCK_BITS)) {