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.25 2008/07/16 18:30:59 dillon Exp $
42 static int hammer_res_rb_compare(hammer_reserve_t res1, hammer_reserve_t res2);
43 static int hammer_reserve_setdelay(hammer_mount_t hmp, hammer_reserve_t resv,
44 hammer_off_t zone2_offset);
48 * Reserved big-blocks red-black tree support
50 RB_GENERATE2(hammer_res_rb_tree, hammer_reserve, rb_node,
51 hammer_res_rb_compare, hammer_off_t, zone_offset);
54 hammer_res_rb_compare(hammer_reserve_t res1, hammer_reserve_t res2)
56 if (res1->zone_offset < res2->zone_offset)
58 if (res1->zone_offset > res2->zone_offset)
64 * Allocate bytes from a zone
67 hammer_blockmap_alloc(hammer_transaction_t trans, int zone,
68 int bytes, int *errorp)
71 hammer_volume_t root_volume;
72 hammer_blockmap_t blockmap;
73 hammer_blockmap_t freemap;
74 hammer_reserve_t resv;
75 struct hammer_blockmap_layer1 *layer1;
76 struct hammer_blockmap_layer2 *layer2;
77 hammer_buffer_t buffer1 = NULL;
78 hammer_buffer_t buffer2 = NULL;
79 hammer_buffer_t buffer3 = NULL;
80 hammer_off_t tmp_offset;
81 hammer_off_t next_offset;
82 hammer_off_t result_offset;
83 hammer_off_t layer1_offset;
84 hammer_off_t layer2_offset;
85 hammer_off_t base_off;
87 int offset; /* offset within big-block */
92 * Deal with alignment and buffer-boundary issues.
94 * Be careful, certain primary alignments are used below to allocate
95 * new blockmap blocks.
97 bytes = (bytes + 15) & ~15;
98 KKASSERT(bytes > 0 && bytes <= HAMMER_XBUFSIZE);
99 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
104 root_volume = trans->rootvol;
106 blockmap = &hmp->blockmap[zone];
107 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
108 KKASSERT(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
110 next_offset = blockmap->next_offset;
115 if (next_offset == HAMMER_ZONE_ENCODE(zone + 1, 0)) {
121 next_offset = HAMMER_ZONE_ENCODE(zone, 0);
125 * The allocation request may not cross a buffer boundary. Special
126 * large allocations must not cross a large-block boundary.
128 tmp_offset = next_offset + bytes - 1;
129 if (bytes <= HAMMER_BUFSIZE) {
130 if ((next_offset ^ tmp_offset) & ~HAMMER_BUFMASK64) {
131 next_offset = tmp_offset & ~HAMMER_BUFMASK64;
135 if ((next_offset ^ tmp_offset) & ~HAMMER_LARGEBLOCK_MASK64) {
136 next_offset = tmp_offset & ~HAMMER_LARGEBLOCK_MASK64;
140 offset = (int)next_offset & HAMMER_LARGEBLOCK_MASK;
145 layer1_offset = freemap->phys_offset +
146 HAMMER_BLOCKMAP_LAYER1_OFFSET(next_offset);
147 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer1);
148 KKASSERT(*errorp == 0);
153 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
154 Debugger("CRC FAILED: LAYER1");
158 * If we are at a big-block boundary and layer1 indicates no
159 * free big-blocks, then we cannot allocate a new bigblock in
160 * layer2, skip to the next layer1 entry.
162 if (offset == 0 && layer1->blocks_free == 0) {
163 next_offset = (next_offset + HAMMER_BLOCKMAP_LAYER2) &
164 ~HAMMER_BLOCKMAP_LAYER2_MASK;
167 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
170 * Dive layer 2, each entry represents a large-block.
172 layer2_offset = layer1->phys_offset +
173 HAMMER_BLOCKMAP_LAYER2_OFFSET(next_offset);
174 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer2);
175 KKASSERT(*errorp == 0);
180 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
181 Debugger("CRC FAILED: LAYER2");
185 * Skip the layer if the zone is owned by someone other then us.
187 if (layer2->zone && layer2->zone != zone) {
188 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
191 if (offset < layer2->append_off) {
192 next_offset += layer2->append_off - offset;
197 * We need the lock from this point on. We have to re-check zone
198 * ownership after acquiring the lock and also check for reservations.
200 hammer_lock_ex(&hmp->blkmap_lock);
202 if (layer2->zone && layer2->zone != zone) {
203 hammer_unlock(&hmp->blkmap_lock);
204 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
207 if (offset < layer2->append_off) {
208 hammer_unlock(&hmp->blkmap_lock);
209 next_offset += layer2->append_off - offset;
214 * The bigblock might be reserved by another zone. If it is reserved
215 * by our zone we may have to move next_offset past the append_off.
217 base_off = (next_offset &
218 (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) |
219 HAMMER_ZONE_RAW_BUFFER;
220 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root, base_off);
222 if (resv->zone != zone) {
223 hammer_unlock(&hmp->blkmap_lock);
224 next_offset = (next_offset + HAMMER_LARGEBLOCK_SIZE) &
225 ~HAMMER_LARGEBLOCK_MASK64;
228 if (offset < resv->append_off) {
229 hammer_unlock(&hmp->blkmap_lock);
230 next_offset += resv->append_off - offset;
236 * Ok, we can allocate out of this layer2 big-block. Assume ownership
237 * of the layer for real. At this point we've validated any
238 * reservation that might exist and can just ignore resv.
240 if (layer2->zone == 0) {
242 * Assign the bigblock to our zone
244 hammer_modify_buffer(trans, buffer1,
245 layer1, sizeof(*layer1));
246 --layer1->blocks_free;
247 layer1->layer1_crc = crc32(layer1,
248 HAMMER_LAYER1_CRCSIZE);
249 hammer_modify_buffer_done(buffer1);
250 hammer_modify_buffer(trans, buffer2,
251 layer2, sizeof(*layer2));
253 KKASSERT(layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE);
254 KKASSERT(layer2->append_off == 0);
255 hammer_modify_volume_field(trans, trans->rootvol,
256 vol0_stat_freebigblocks);
257 --root_volume->ondisk->vol0_stat_freebigblocks;
258 hmp->copy_stat_freebigblocks =
259 root_volume->ondisk->vol0_stat_freebigblocks;
260 hammer_modify_volume_done(trans->rootvol);
262 hammer_modify_buffer(trans, buffer2,
263 layer2, sizeof(*layer2));
265 KKASSERT(layer2->zone == zone);
267 layer2->bytes_free -= bytes;
268 KKASSERT(layer2->append_off <= offset);
269 layer2->append_off = offset + bytes;
270 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
271 hammer_modify_buffer_done(buffer2);
272 KKASSERT(layer2->bytes_free >= 0);
275 KKASSERT(resv->append_off <= offset);
276 resv->append_off = offset + bytes;
280 * If we are allocating from the base of a new buffer we can avoid
281 * a disk read by calling hammer_bnew().
283 if ((next_offset & HAMMER_BUFMASK) == 0) {
284 hammer_bnew_ext(trans->hmp, next_offset, bytes,
287 result_offset = next_offset;
290 * Process allocated result_offset
292 hammer_modify_volume(NULL, root_volume, NULL, 0);
293 blockmap->next_offset = next_offset + bytes;
294 hammer_modify_volume_done(root_volume);
295 hammer_unlock(&hmp->blkmap_lock);
302 hammer_rel_buffer(buffer1, 0);
304 hammer_rel_buffer(buffer2, 0);
306 hammer_rel_buffer(buffer3, 0);
308 return(result_offset);
312 * Frontend function - Reserve bytes in a zone.
314 * This code reserves bytes out of a blockmap without committing to any
315 * meta-data modifications, allowing the front-end to directly issue disk
316 * write I/O for large blocks of data
318 * The backend later finalizes the reservation with hammer_blockmap_finalize()
319 * upon committing the related record.
322 hammer_blockmap_reserve(hammer_mount_t hmp, int zone, int bytes,
323 hammer_off_t *zone_offp, int *errorp)
325 hammer_volume_t root_volume;
326 hammer_blockmap_t blockmap;
327 hammer_blockmap_t freemap;
328 struct hammer_blockmap_layer1 *layer1;
329 struct hammer_blockmap_layer2 *layer2;
330 hammer_buffer_t buffer1 = NULL;
331 hammer_buffer_t buffer2 = NULL;
332 hammer_buffer_t buffer3 = NULL;
333 hammer_off_t tmp_offset;
334 hammer_off_t next_offset;
335 hammer_off_t layer1_offset;
336 hammer_off_t layer2_offset;
337 hammer_off_t base_off;
338 hammer_reserve_t resv;
339 hammer_reserve_t resx;
346 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
347 root_volume = hammer_get_root_volume(hmp, errorp);
350 blockmap = &hmp->blockmap[zone];
351 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
352 KKASSERT(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
355 * Deal with alignment and buffer-boundary issues.
357 * Be careful, certain primary alignments are used below to allocate
358 * new blockmap blocks.
360 bytes = (bytes + 15) & ~15;
361 KKASSERT(bytes > 0 && bytes <= HAMMER_XBUFSIZE);
363 next_offset = blockmap->next_offset;
369 if (next_offset == HAMMER_ZONE_ENCODE(zone + 1, 0)) {
374 next_offset = HAMMER_ZONE_ENCODE(zone, 0);
378 * The allocation request may not cross a buffer boundary. Special
379 * large allocations must not cross a large-block boundary.
381 tmp_offset = next_offset + bytes - 1;
382 if (bytes <= HAMMER_BUFSIZE) {
383 if ((next_offset ^ tmp_offset) & ~HAMMER_BUFMASK64) {
384 next_offset = tmp_offset & ~HAMMER_BUFMASK64;
388 if ((next_offset ^ tmp_offset) & ~HAMMER_LARGEBLOCK_MASK64) {
389 next_offset = tmp_offset & ~HAMMER_LARGEBLOCK_MASK64;
393 offset = (int)next_offset & HAMMER_LARGEBLOCK_MASK;
398 layer1_offset = freemap->phys_offset +
399 HAMMER_BLOCKMAP_LAYER1_OFFSET(next_offset);
400 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer1);
401 KKASSERT(*errorp == 0);
406 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
407 Debugger("CRC FAILED: LAYER1");
411 * If we are at a big-block boundary and layer1 indicates no
412 * free big-blocks, then we cannot allocate a new bigblock in
413 * layer2, skip to the next layer1 entry.
415 if ((next_offset & HAMMER_LARGEBLOCK_MASK) == 0 &&
416 layer1->blocks_free == 0) {
417 next_offset = (next_offset + HAMMER_BLOCKMAP_LAYER2) &
418 ~HAMMER_BLOCKMAP_LAYER2_MASK;
421 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
424 * Dive layer 2, each entry represents a large-block.
426 layer2_offset = layer1->phys_offset +
427 HAMMER_BLOCKMAP_LAYER2_OFFSET(next_offset);
428 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer2);
429 KKASSERT(*errorp == 0);
432 * Check CRC if not allocating into uninitialized space (which we
433 * aren't when reserving space).
435 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
436 Debugger("CRC FAILED: LAYER2");
440 * Skip the layer if the zone is owned by someone other then us.
442 if (layer2->zone && layer2->zone != zone) {
443 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
446 if (offset < layer2->append_off) {
447 next_offset += layer2->append_off - offset;
452 * We need the lock from this point on. We have to re-check zone
453 * ownership after acquiring the lock and also check for reservations.
455 hammer_lock_ex(&hmp->blkmap_lock);
457 if (layer2->zone && layer2->zone != zone) {
458 hammer_unlock(&hmp->blkmap_lock);
459 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
462 if (offset < layer2->append_off) {
463 hammer_unlock(&hmp->blkmap_lock);
464 next_offset += layer2->append_off - offset;
469 * The bigblock might be reserved by another zone. If it is reserved
470 * by our zone we may have to move next_offset past the append_off.
472 base_off = (next_offset &
473 (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) |
474 HAMMER_ZONE_RAW_BUFFER;
475 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root, base_off);
477 if (resv->zone != zone) {
478 hammer_unlock(&hmp->blkmap_lock);
479 next_offset = (next_offset + HAMMER_LARGEBLOCK_SIZE) &
480 ~HAMMER_LARGEBLOCK_MASK64;
483 if (offset < resv->append_off) {
484 hammer_unlock(&hmp->blkmap_lock);
485 next_offset += resv->append_off - offset;
491 resx = kmalloc(sizeof(*resv), M_HAMMER,
492 M_WAITOK | M_ZERO | M_USE_RESERVE);
495 resx->zone_offset = base_off;
496 resv = RB_INSERT(hammer_res_rb_tree, &hmp->rb_resv_root, resx);
497 KKASSERT(resv == NULL);
499 ++hammer_count_reservations;
501 resv->append_off = offset + bytes;
504 * If we are not reserving a whole buffer but are at the start of
505 * a new block, call hammer_bnew() to avoid a disk read.
507 * If we are reserving a whole buffer (or more), the caller will
508 * probably use a direct read, so do nothing.
510 if (bytes < HAMMER_BUFSIZE && (next_offset & HAMMER_BUFMASK) == 0) {
511 hammer_bnew(hmp, next_offset, errorp, &buffer3);
515 * Adjust our iterator and alloc_offset. The layer1 and layer2
516 * space beyond alloc_offset is uninitialized. alloc_offset must
517 * be big-block aligned.
519 blockmap->next_offset = next_offset + bytes;
520 hammer_unlock(&hmp->blkmap_lock);
524 hammer_rel_buffer(buffer1, 0);
526 hammer_rel_buffer(buffer2, 0);
528 hammer_rel_buffer(buffer3, 0);
529 hammer_rel_volume(root_volume, 0);
530 *zone_offp = next_offset;
536 * Backend function - undo a portion of a reservation.
539 hammer_blockmap_reserve_undo(hammer_reserve_t resv,
540 hammer_off_t zone_offset, int bytes)
542 resv->bytes_freed += bytes;
547 * A record with a storage reservation calls this function when it is
548 * being freed. The storage may or may not have actually been allocated.
550 * This function removes the lock that prevented other entities from
551 * allocating out of the storage or removing the zone assignment.
554 hammer_blockmap_reserve_complete(hammer_mount_t hmp, hammer_reserve_t resv)
556 hammer_off_t zone2_offset;
558 KKASSERT(resv->refs > 0);
559 if (--resv->refs == 0) {
560 KKASSERT((resv->flags & HAMMER_RESF_ONDELAY) == 0);
562 zone2_offset = (resv->zone_offset & ~HAMMER_OFF_ZONE_MASK) |
563 HAMMER_ZONE_RAW_BUFFER;
566 * If we are releasing a zone and all of its reservations
567 * were undone we have to clean out all hammer and device
568 * buffers associated with the big block.
570 * Any direct allocations will cause this test to fail
571 * (bytes_freed will never reach append_off), which is
572 * the behavior we desire. Once the zone has been assigned
573 * to the big-block the only way to allocate from it in the
574 * future is if the reblocker can completely clean it out,
575 * and that will also properly call hammer_del_buffers().
577 * If we don't we risk all sorts of buffer cache aliasing
578 * effects, including overlapping buffers with different
581 if (resv->bytes_freed == resv->append_off) {
583 hammer_del_buffers(hmp, resv->zone_offset,
585 HAMMER_LARGEBLOCK_SIZE);
587 RB_REMOVE(hammer_res_rb_tree, &hmp->rb_resv_root, resv);
588 kfree(resv, M_HAMMER);
589 --hammer_count_reservations;
594 * This ensures that no data reallocations will take place at the specified
595 * zone2_offset (pointing to the base of a bigblock) for 2 flush cycles,
596 * preventing deleted data space, which has no UNDO, from being reallocated
600 hammer_reserve_setdelay(hammer_mount_t hmp, hammer_reserve_t resv,
601 hammer_off_t zone2_offset)
606 resv = kmalloc(sizeof(*resv), M_HAMMER,
607 M_WAITOK | M_ZERO | M_USE_RESERVE);
608 resv->refs = 1; /* ref for on-delay list */
609 resv->zone_offset = zone2_offset;
610 resv->append_off = HAMMER_LARGEBLOCK_SIZE;
611 if (RB_INSERT(hammer_res_rb_tree, &hmp->rb_resv_root, resv)) {
613 kfree(resv, M_HAMMER);
616 ++hammer_count_reservations;
618 } else if (resv->flags & HAMMER_RESF_ONDELAY) {
619 --hmp->rsv_fromdelay;
620 resv->flags &= ~HAMMER_RESF_ONDELAY;
621 TAILQ_REMOVE(&hmp->delay_list, resv, delay_entry);
622 resv->flush_group = hmp->flusher.next + 1;
625 ++resv->refs; /* ref for on-delay list */
629 ++hmp->rsv_fromdelay;
630 resv->flags |= HAMMER_RESF_ONDELAY;
631 resv->flush_group = hmp->flusher.next + 1;
632 TAILQ_INSERT_TAIL(&hmp->delay_list, resv, delay_entry);
638 hammer_reserve_clrdelay(hammer_mount_t hmp, hammer_reserve_t resv)
640 KKASSERT(resv->flags & HAMMER_RESF_ONDELAY);
641 resv->flags &= ~HAMMER_RESF_ONDELAY;
642 TAILQ_REMOVE(&hmp->delay_list, resv, delay_entry);
643 --hmp->rsv_fromdelay;
644 hammer_blockmap_reserve_complete(hmp, resv);
648 * Backend function - free (offset, bytes) in a zone.
651 hammer_blockmap_free(hammer_transaction_t trans,
652 hammer_off_t zone_offset, int bytes)
655 hammer_volume_t root_volume;
656 hammer_reserve_t resv;
657 hammer_blockmap_t blockmap;
658 hammer_blockmap_t freemap;
659 struct hammer_blockmap_layer1 *layer1;
660 struct hammer_blockmap_layer2 *layer2;
661 hammer_buffer_t buffer1 = NULL;
662 hammer_buffer_t buffer2 = NULL;
663 hammer_off_t layer1_offset;
664 hammer_off_t layer2_offset;
665 hammer_off_t base_off;
676 bytes = (bytes + 15) & ~15;
677 KKASSERT(bytes <= HAMMER_XBUFSIZE);
678 KKASSERT(((zone_offset ^ (zone_offset + (bytes - 1))) &
679 ~HAMMER_LARGEBLOCK_MASK64) == 0);
682 * Basic zone validation & locking
684 zone = HAMMER_ZONE_DECODE(zone_offset);
685 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
686 root_volume = trans->rootvol;
689 blockmap = &hmp->blockmap[zone];
690 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
695 layer1_offset = freemap->phys_offset +
696 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
697 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
698 KKASSERT(error == 0);
699 KKASSERT(layer1->phys_offset &&
700 layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
701 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
702 Debugger("CRC FAILED: LAYER1");
706 * Dive layer 2, each entry represents a large-block.
708 layer2_offset = layer1->phys_offset +
709 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
710 layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
711 KKASSERT(error == 0);
712 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
713 Debugger("CRC FAILED: LAYER2");
716 hammer_lock_ex(&hmp->blkmap_lock);
718 hammer_modify_buffer(trans, buffer2, layer2, sizeof(*layer2));
721 * Freeing previously allocated space
723 KKASSERT(layer2->zone == zone);
724 layer2->bytes_free += bytes;
725 KKASSERT(layer2->bytes_free <= HAMMER_LARGEBLOCK_SIZE);
726 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE) {
727 base_off = (zone_offset & (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) | HAMMER_ZONE_RAW_BUFFER;
729 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root,
733 * Portions of this block have been reserved, do
736 * Make sure the reservation remains through
737 * the next flush cycle so potentially undoable
738 * data is not overwritten.
740 KKASSERT(resv->zone == zone);
741 hammer_reserve_setdelay(hmp, resv, base_off);
742 } else if ((blockmap->next_offset ^ zone_offset) &
743 ~HAMMER_LARGEBLOCK_MASK64) {
745 * Our iterator is not in the now-free big-block
746 * and we can release it.
748 * Make sure the reservation remains through
749 * the next flush cycle so potentially undoable
750 * data is not overwritten.
752 if (hammer_reserve_setdelay(hmp, NULL, base_off))
754 KKASSERT(layer2->zone == zone);
756 * XXX maybe incorporate this del call in the
757 * release code by setting base_offset, bytes_freed,
760 hammer_del_buffers(hmp,
762 ~HAMMER_LARGEBLOCK_MASK64,
764 HAMMER_LARGEBLOCK_SIZE);
766 layer2->append_off = 0;
767 hammer_modify_buffer(trans, buffer1,
768 layer1, sizeof(*layer1));
769 ++layer1->blocks_free;
770 layer1->layer1_crc = crc32(layer1,
771 HAMMER_LAYER1_CRCSIZE);
772 hammer_modify_buffer_done(buffer1);
773 hammer_modify_volume_field(trans,
775 vol0_stat_freebigblocks);
776 ++root_volume->ondisk->vol0_stat_freebigblocks;
777 hmp->copy_stat_freebigblocks =
778 root_volume->ondisk->vol0_stat_freebigblocks;
779 hammer_modify_volume_done(trans->rootvol);
783 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
784 hammer_modify_buffer_done(buffer2);
785 hammer_unlock(&hmp->blkmap_lock);
788 hammer_rel_buffer(buffer1, 0);
790 hammer_rel_buffer(buffer2, 0);
794 * Backend function - finalize (offset, bytes) in a zone.
796 * Allocate space that was previously reserved by the frontend.
799 hammer_blockmap_finalize(hammer_transaction_t trans,
800 hammer_off_t zone_offset, int bytes)
803 hammer_volume_t root_volume;
804 hammer_blockmap_t blockmap;
805 hammer_blockmap_t freemap;
806 struct hammer_blockmap_layer1 *layer1;
807 struct hammer_blockmap_layer2 *layer2;
808 hammer_buffer_t buffer1 = NULL;
809 hammer_buffer_t buffer2 = NULL;
810 hammer_off_t layer1_offset;
811 hammer_off_t layer2_offset;
823 bytes = (bytes + 15) & ~15;
824 KKASSERT(bytes <= HAMMER_XBUFSIZE);
827 * Basic zone validation & locking
829 zone = HAMMER_ZONE_DECODE(zone_offset);
830 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
831 root_volume = trans->rootvol;
834 blockmap = &hmp->blockmap[zone];
835 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
840 layer1_offset = freemap->phys_offset +
841 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
842 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
843 KKASSERT(error == 0);
844 KKASSERT(layer1->phys_offset &&
845 layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
846 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
847 Debugger("CRC FAILED: LAYER1");
851 * Dive layer 2, each entry represents a large-block.
853 layer2_offset = layer1->phys_offset +
854 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
855 layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
856 KKASSERT(error == 0);
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;
894 * Finalizations can occur out of order, or combined with allocations.
895 * append_off must be set to the highest allocated offset.
897 offset = ((int)zone_offset & HAMMER_LARGEBLOCK_MASK) + bytes;
898 if (layer2->append_off < offset)
899 layer2->append_off = offset;
901 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
902 hammer_modify_buffer_done(buffer2);
903 hammer_unlock(&hmp->blkmap_lock);
906 hammer_rel_buffer(buffer1, 0);
908 hammer_rel_buffer(buffer2, 0);
912 * Return the number of free bytes in the big-block containing the
913 * specified blockmap offset.
916 hammer_blockmap_getfree(hammer_mount_t hmp, hammer_off_t zone_offset,
917 int *curp, int *errorp)
919 hammer_volume_t root_volume;
920 hammer_blockmap_t blockmap;
921 hammer_blockmap_t freemap;
922 struct hammer_blockmap_layer1 *layer1;
923 struct hammer_blockmap_layer2 *layer2;
924 hammer_buffer_t buffer = NULL;
925 hammer_off_t layer1_offset;
926 hammer_off_t layer2_offset;
930 zone = HAMMER_ZONE_DECODE(zone_offset);
931 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
932 root_volume = hammer_get_root_volume(hmp, errorp);
937 blockmap = &hmp->blockmap[zone];
938 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
943 layer1_offset = freemap->phys_offset +
944 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
945 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer);
946 KKASSERT(*errorp == 0);
947 KKASSERT(layer1->phys_offset);
948 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
949 Debugger("CRC FAILED: LAYER1");
953 * Dive layer 2, each entry represents a large-block.
955 layer2_offset = layer1->phys_offset +
956 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
957 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer);
958 KKASSERT(*errorp == 0);
959 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
960 Debugger("CRC FAILED: LAYER2");
962 KKASSERT(layer2->zone == zone);
964 bytes = layer2->bytes_free;
966 if ((blockmap->next_offset ^ zone_offset) & ~HAMMER_LARGEBLOCK_MASK64)
971 hammer_rel_buffer(buffer, 0);
972 hammer_rel_volume(root_volume, 0);
973 if (hammer_debug_general & 0x0800) {
974 kprintf("hammer_blockmap_getfree: %016llx -> %d\n",
982 * Lookup a blockmap offset.
985 hammer_blockmap_lookup(hammer_mount_t hmp, hammer_off_t zone_offset,
988 hammer_volume_t root_volume;
989 hammer_blockmap_t freemap;
990 struct hammer_blockmap_layer1 *layer1;
991 struct hammer_blockmap_layer2 *layer2;
992 hammer_buffer_t buffer = NULL;
993 hammer_off_t layer1_offset;
994 hammer_off_t layer2_offset;
995 hammer_off_t result_offset;
996 hammer_off_t base_off;
997 hammer_reserve_t resv;
1001 * Calculate the zone-2 offset.
1003 zone = HAMMER_ZONE_DECODE(zone_offset);
1004 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
1006 result_offset = (zone_offset & ~HAMMER_OFF_ZONE_MASK) |
1007 HAMMER_ZONE_RAW_BUFFER;
1010 * We can actually stop here, normal blockmaps are now direct-mapped
1011 * onto the freemap and so represent zone-2 addresses.
1013 if (hammer_verify_zone == 0) {
1015 return(result_offset);
1019 * Validate the allocation zone
1021 root_volume = hammer_get_root_volume(hmp, errorp);
1024 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
1025 KKASSERT(freemap->phys_offset != 0);
1030 layer1_offset = freemap->phys_offset +
1031 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
1032 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer);
1033 KKASSERT(*errorp == 0);
1034 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
1035 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
1036 Debugger("CRC FAILED: LAYER1");
1040 * Dive layer 2, each entry represents a large-block.
1042 layer2_offset = layer1->phys_offset +
1043 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
1044 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer);
1046 KKASSERT(*errorp == 0);
1047 if (layer2->zone == 0) {
1048 base_off = (zone_offset & (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) | HAMMER_ZONE_RAW_BUFFER;
1049 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root,
1051 KKASSERT(resv && resv->zone == zone);
1053 } else if (layer2->zone != zone) {
1054 panic("hammer_blockmap_lookup: bad zone %d/%d\n",
1055 layer2->zone, zone);
1057 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
1058 Debugger("CRC FAILED: LAYER2");
1062 hammer_rel_buffer(buffer, 0);
1063 hammer_rel_volume(root_volume, 0);
1064 if (hammer_debug_general & 0x0800) {
1065 kprintf("hammer_blockmap_lookup: %016llx -> %016llx\n",
1066 zone_offset, result_offset);
1068 return(result_offset);
1073 * Check space availability
1076 hammer_checkspace(hammer_mount_t hmp, int slop)
1078 const int in_size = sizeof(struct hammer_inode_data) +
1079 sizeof(union hammer_btree_elm);
1080 const int rec_size = (sizeof(union hammer_btree_elm) * 2);
1083 usedbytes = hmp->rsv_inodes * in_size +
1084 hmp->rsv_recs * rec_size +
1085 hmp->rsv_databytes +
1086 ((int64_t)hmp->rsv_fromdelay << HAMMER_LARGEBLOCK_BITS) +
1087 ((int64_t)hidirtybufspace << 2) +
1088 (slop << HAMMER_LARGEBLOCK_BITS);
1090 hammer_count_extra_space_used = usedbytes; /* debugging */
1092 if (hmp->copy_stat_freebigblocks >=
1093 (usedbytes >> HAMMER_LARGEBLOCK_BITS)) {