HAMMER - Remove unused variable
[dragonfly.git] / sys / vfs / hammer / hammer_blockmap.c
CommitLineData
40043e7f
MD
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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
32 * SUCH DAMAGE.
33 *
e469566b 34 * $DragonFly: src/sys/vfs/hammer/hammer_blockmap.c,v 1.27 2008/07/31 22:30:33 dillon Exp $
40043e7f
MD
35 */
36
37/*
38 * HAMMER blockmap
39 */
40#include "hammer.h"
41
0832c9bb 42static int hammer_res_rb_compare(hammer_reserve_t res1, hammer_reserve_t res2);
362ec2dc 43static void hammer_reserve_setdelay_offset(hammer_mount_t hmp,
1ce12d35 44 hammer_off_t base_offset, int zone,
5e435c92 45 struct hammer_blockmap_layer2 *layer2);
362ec2dc 46static void hammer_reserve_setdelay(hammer_mount_t hmp, hammer_reserve_t resv);
0832c9bb
MD
47
48/*
49 * Reserved big-blocks red-black tree support
50 */
51RB_GENERATE2(hammer_res_rb_tree, hammer_reserve, rb_node,
52 hammer_res_rb_compare, hammer_off_t, zone_offset);
53
54static int
55hammer_res_rb_compare(hammer_reserve_t res1, hammer_reserve_t res2)
56{
57 if (res1->zone_offset < res2->zone_offset)
58 return(-1);
59 if (res1->zone_offset > res2->zone_offset)
60 return(1);
61 return(0);
62}
bf686dbe 63
40043e7f
MD
64/*
65 * Allocate bytes from a zone
66 */
67hammer_off_t
df2ccbac
MD
68hammer_blockmap_alloc(hammer_transaction_t trans, int zone, int bytes,
69 hammer_off_t hint, int *errorp)
40043e7f 70{
0832c9bb 71 hammer_mount_t hmp;
40043e7f 72 hammer_volume_t root_volume;
cb51be26
MD
73 hammer_blockmap_t blockmap;
74 hammer_blockmap_t freemap;
0832c9bb 75 hammer_reserve_t resv;
c3be93f2
MD
76 struct hammer_blockmap_layer1 *layer1;
77 struct hammer_blockmap_layer2 *layer2;
f03c9cf4
MD
78 hammer_buffer_t buffer1 = NULL;
79 hammer_buffer_t buffer2 = NULL;
80 hammer_buffer_t buffer3 = NULL;
c3be93f2 81 hammer_off_t tmp_offset;
f03c9cf4 82 hammer_off_t next_offset;
0832c9bb 83 hammer_off_t result_offset;
c3be93f2
MD
84 hammer_off_t layer1_offset;
85 hammer_off_t layer2_offset;
cb51be26 86 hammer_off_t base_off;
f03c9cf4 87 int loops = 0;
df301614 88 int offset; /* offset within big-block */
df2ccbac 89 int use_hint;
40043e7f 90
0832c9bb 91 hmp = trans->hmp;
40043e7f
MD
92
93 /*
94 * Deal with alignment and buffer-boundary issues.
95 *
96 * Be careful, certain primary alignments are used below to allocate
97 * new blockmap blocks.
98 */
0832c9bb 99 bytes = (bytes + 15) & ~15;
4a2796f3 100 KKASSERT(bytes > 0 && bytes <= HAMMER_XBUFSIZE);
0832c9bb 101 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
bf686dbe
MD
102
103 /*
cb51be26 104 * Setup
bf686dbe 105 */
cb51be26
MD
106 root_volume = trans->rootvol;
107 *errorp = 0;
108 blockmap = &hmp->blockmap[zone];
109 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
110 KKASSERT(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
111
df2ccbac
MD
112 /*
113 * Use the hint if we have one.
114 */
115 if (hint && HAMMER_ZONE_DECODE(hint) == zone) {
116 next_offset = (hint + 15) & ~(hammer_off_t)15;
117 use_hint = 1;
118 } else {
119 next_offset = blockmap->next_offset;
120 use_hint = 0;
121 }
cb51be26 122again:
df2ccbac
MD
123
124 /*
125 * use_hint is turned off if we leave the hinted big-block.
126 */
127 if (use_hint && ((next_offset ^ hint) & ~HAMMER_HINTBLOCK_MASK64)) {
128 next_offset = blockmap->next_offset;
129 use_hint = 0;
130 }
131
0832c9bb 132 /*
cb51be26 133 * Check for wrap
0832c9bb 134 */
4a2796f3 135 if (next_offset == HAMMER_ZONE_ENCODE(zone + 1, 0)) {
cb51be26
MD
136 if (++loops == 2) {
137 result_offset = 0;
138 *errorp = ENOSPC;
df301614 139 goto failed;
cb51be26
MD
140 }
141 next_offset = HAMMER_ZONE_ENCODE(zone, 0);
142 }
0832c9bb 143
f03c9cf4 144 /*
4a2796f3
MD
145 * The allocation request may not cross a buffer boundary. Special
146 * large allocations must not cross a large-block boundary.
f03c9cf4 147 */
bf686dbe 148 tmp_offset = next_offset + bytes - 1;
4a2796f3
MD
149 if (bytes <= HAMMER_BUFSIZE) {
150 if ((next_offset ^ tmp_offset) & ~HAMMER_BUFMASK64) {
151 next_offset = tmp_offset & ~HAMMER_BUFMASK64;
152 goto again;
153 }
154 } else {
155 if ((next_offset ^ tmp_offset) & ~HAMMER_LARGEBLOCK_MASK64) {
156 next_offset = tmp_offset & ~HAMMER_LARGEBLOCK_MASK64;
157 goto again;
158 }
bf686dbe 159 }
df301614 160 offset = (int)next_offset & HAMMER_LARGEBLOCK_MASK;
40043e7f
MD
161
162 /*
cb51be26 163 * Dive layer 1.
40043e7f 164 */
cb51be26 165 layer1_offset = freemap->phys_offset +
f03c9cf4 166 HAMMER_BLOCKMAP_LAYER1_OFFSET(next_offset);
865c9609 167
0832c9bb 168 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer1);
cdb6e4e6
MD
169 if (*errorp) {
170 result_offset = 0;
171 goto failed;
172 }
19619882 173
c3be93f2 174 /*
cb51be26 175 * Check CRC.
c3be93f2 176 */
cb51be26 177 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
db9f9d7f
MD
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);
40043e7f 182 }
40043e7f
MD
183
184 /*
cb51be26
MD
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.
40043e7f 188 */
df301614 189 if (offset == 0 && layer1->blocks_free == 0) {
cb51be26 190 next_offset = (next_offset + HAMMER_BLOCKMAP_LAYER2) &
f03c9cf4 191 ~HAMMER_BLOCKMAP_LAYER2_MASK;
f03c9cf4
MD
192 goto again;
193 }
cb51be26 194 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
40043e7f 195
c47d84e8
MN
196 /*
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.
201 */
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;
206 goto again;
207 }
208
c3be93f2 209 /*
f03c9cf4 210 * Dive layer 2, each entry represents a large-block.
c3be93f2 211 */
f03c9cf4
MD
212 layer2_offset = layer1->phys_offset +
213 HAMMER_BLOCKMAP_LAYER2_OFFSET(next_offset);
0832c9bb 214 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer2);
cdb6e4e6
MD
215 if (*errorp) {
216 result_offset = 0;
217 goto failed;
218 }
f03c9cf4 219
19619882 220 /*
db9f9d7f
MD
221 * Check CRC. This can race another thread holding the lock
222 * and in the middle of modifying layer2.
19619882 223 */
cb51be26 224 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
db9f9d7f
MD
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);
19619882
MD
229 }
230
cb51be26 231 /*
df301614 232 * Skip the layer if the zone is owned by someone other then us.
cb51be26 233 */
df301614
MD
234 if (layer2->zone && layer2->zone != zone) {
235 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
236 goto again;
237 }
238 if (offset < layer2->append_off) {
239 next_offset += layer2->append_off - offset;
4a2796f3
MD
240 goto again;
241 }
242
df2ccbac
MD
243 /*
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.
247 *
248 * We do this for B-Tree and meta-data allocations to provide
249 * localization for updates.
250 */
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)
255 ) {
256 if (offset >= HAMMER_LARGEBLOCK_OVERFILL) {
257 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
258 use_hint = 0;
259 goto again;
260 }
261 }
262
4a2796f3 263 /*
df301614
MD
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.
266 */
267 hammer_lock_ex(&hmp->blkmap_lock);
268
269 if (layer2->zone && layer2->zone != zone) {
270 hammer_unlock(&hmp->blkmap_lock);
271 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
272 goto again;
273 }
274 if (offset < layer2->append_off) {
275 hammer_unlock(&hmp->blkmap_lock);
276 next_offset += layer2->append_off - offset;
277 goto again;
278 }
279
280 /*
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.
4a2796f3
MD
283 */
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);
288 if (resv) {
4a2796f3 289 if (resv->zone != zone) {
df301614 290 hammer_unlock(&hmp->blkmap_lock);
cb51be26
MD
291 next_offset = (next_offset + HAMMER_LARGEBLOCK_SIZE) &
292 ~HAMMER_LARGEBLOCK_MASK64;
f03c9cf4
MD
293 goto again;
294 }
df301614
MD
295 if (offset < resv->append_off) {
296 hammer_unlock(&hmp->blkmap_lock);
297 next_offset += resv->append_off - offset;
298 goto again;
299 }
1ce12d35 300 ++resv->refs;
cb51be26
MD
301 }
302
4a2796f3
MD
303 /*
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.
307 */
cb51be26 308 if (layer2->zone == 0) {
f03c9cf4 309 /*
cb51be26 310 * Assign the bigblock to our zone
f03c9cf4 311 */
cb51be26
MD
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));
320 layer2->zone = zone;
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);
cb51be26
MD
329 } else {
330 hammer_modify_buffer(trans, buffer2,
331 layer2, sizeof(*layer2));
40043e7f 332 }
cb51be26 333 KKASSERT(layer2->zone == zone);
40043e7f 334
320a5c59
MD
335 /*
336 * NOTE: bytes_free can legally go negative due to de-dup.
337 */
c3be93f2 338 layer2->bytes_free -= bytes;
df301614
MD
339 KKASSERT(layer2->append_off <= offset);
340 layer2->append_off = offset + bytes;
19619882 341 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
10a5d1ba 342 hammer_modify_buffer_done(buffer2);
40043e7f 343
1ce12d35
MD
344 /*
345 * We hold the blockmap lock and should be the only ones
346 * capable of modifying resv->append_off. Track the allocation
347 * as appropriate.
348 */
349 KKASSERT(bytes != 0);
df301614
MD
350 if (resv) {
351 KKASSERT(resv->append_off <= offset);
352 resv->append_off = offset + bytes;
5e435c92 353 resv->flags &= ~HAMMER_RESF_LAYER2FREE;
1ce12d35 354 hammer_blockmap_reserve_complete(hmp, resv);
df301614
MD
355 }
356
40043e7f 357 /*
0832c9bb
MD
358 * If we are allocating from the base of a new buffer we can avoid
359 * a disk read by calling hammer_bnew().
40043e7f 360 */
f03c9cf4 361 if ((next_offset & HAMMER_BUFMASK) == 0) {
4a2796f3
MD
362 hammer_bnew_ext(trans->hmp, next_offset, bytes,
363 errorp, &buffer3);
40043e7f 364 }
0832c9bb 365 result_offset = next_offset;
40043e7f 366
c3be93f2 367 /*
df2ccbac
MD
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
370 * need to save it.
c3be93f2 371 */
df2ccbac
MD
372 if (use_hint == 0) {
373 hammer_modify_volume(NULL, root_volume, NULL, 0);
374 blockmap->next_offset = next_offset + bytes;
375 hammer_modify_volume_done(root_volume);
376 }
d99d6bf5 377 hammer_unlock(&hmp->blkmap_lock);
df301614 378failed:
0832c9bb
MD
379
380 /*
381 * Cleanup
382 */
f03c9cf4
MD
383 if (buffer1)
384 hammer_rel_buffer(buffer1, 0);
385 if (buffer2)
386 hammer_rel_buffer(buffer2, 0);
387 if (buffer3)
388 hammer_rel_buffer(buffer3, 0);
0832c9bb
MD
389
390 return(result_offset);
40043e7f
MD
391}
392
393/*
4a2796f3 394 * Frontend function - Reserve bytes in a zone.
47637bff
MD
395 *
396 * This code reserves bytes out of a blockmap without committing to any
cb51be26
MD
397 * meta-data modifications, allowing the front-end to directly issue disk
398 * write I/O for large blocks of data
4a2796f3
MD
399 *
400 * The backend later finalizes the reservation with hammer_blockmap_finalize()
401 * upon committing the related record.
47637bff 402 */
0832c9bb
MD
403hammer_reserve_t
404hammer_blockmap_reserve(hammer_mount_t hmp, int zone, int bytes,
405 hammer_off_t *zone_offp, int *errorp)
47637bff
MD
406{
407 hammer_volume_t root_volume;
cb51be26
MD
408 hammer_blockmap_t blockmap;
409 hammer_blockmap_t freemap;
47637bff
MD
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;
cb51be26 419 hammer_off_t base_off;
0832c9bb 420 hammer_reserve_t resv;
cb51be26 421 hammer_reserve_t resx;
47637bff 422 int loops = 0;
df301614 423 int offset;
47637bff 424
0832c9bb
MD
425 /*
426 * Setup
427 */
47637bff
MD
428 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
429 root_volume = hammer_get_root_volume(hmp, errorp);
430 if (*errorp)
0832c9bb 431 return(NULL);
cb51be26
MD
432 blockmap = &hmp->blockmap[zone];
433 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
434 KKASSERT(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
47637bff
MD
435
436 /*
437 * Deal with alignment and buffer-boundary issues.
438 *
439 * Be careful, certain primary alignments are used below to allocate
440 * new blockmap blocks.
441 */
0832c9bb 442 bytes = (bytes + 15) & ~15;
4a2796f3 443 KKASSERT(bytes > 0 && bytes <= HAMMER_XBUFSIZE);
47637bff 444
cb51be26 445 next_offset = blockmap->next_offset;
df301614 446again:
fce862c7 447 resv = NULL;
47637bff 448 /*
cb51be26 449 * Check for wrap
47637bff 450 */
4a2796f3 451 if (next_offset == HAMMER_ZONE_ENCODE(zone + 1, 0)) {
cb51be26 452 if (++loops == 2) {
47637bff 453 *errorp = ENOSPC;
df301614 454 goto failed;
47637bff
MD
455 }
456 next_offset = HAMMER_ZONE_ENCODE(zone, 0);
457 }
458
459 /*
4a2796f3
MD
460 * The allocation request may not cross a buffer boundary. Special
461 * large allocations must not cross a large-block boundary.
47637bff
MD
462 */
463 tmp_offset = next_offset + bytes - 1;
4a2796f3
MD
464 if (bytes <= HAMMER_BUFSIZE) {
465 if ((next_offset ^ tmp_offset) & ~HAMMER_BUFMASK64) {
466 next_offset = tmp_offset & ~HAMMER_BUFMASK64;
467 goto again;
468 }
469 } else {
470 if ((next_offset ^ tmp_offset) & ~HAMMER_LARGEBLOCK_MASK64) {
471 next_offset = tmp_offset & ~HAMMER_LARGEBLOCK_MASK64;
472 goto again;
473 }
47637bff 474 }
df301614 475 offset = (int)next_offset & HAMMER_LARGEBLOCK_MASK;
47637bff
MD
476
477 /*
478 * Dive layer 1.
479 */
cb51be26 480 layer1_offset = freemap->phys_offset +
47637bff
MD
481 HAMMER_BLOCKMAP_LAYER1_OFFSET(next_offset);
482 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer1);
cdb6e4e6
MD
483 if (*errorp)
484 goto failed;
47637bff
MD
485
486 /*
cb51be26 487 * Check CRC.
47637bff
MD
488 */
489 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
db9f9d7f
MD
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);
47637bff 494 }
47637bff
MD
495
496 /*
cb51be26
MD
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.
47637bff 500 */
cb51be26
MD
501 if ((next_offset & HAMMER_LARGEBLOCK_MASK) == 0 &&
502 layer1->blocks_free == 0) {
503 next_offset = (next_offset + HAMMER_BLOCKMAP_LAYER2) &
47637bff
MD
504 ~HAMMER_BLOCKMAP_LAYER2_MASK;
505 goto again;
506 }
cb51be26 507 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
47637bff
MD
508
509 /*
510 * Dive layer 2, each entry represents a large-block.
511 */
512 layer2_offset = layer1->phys_offset +
513 HAMMER_BLOCKMAP_LAYER2_OFFSET(next_offset);
514 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer2);
cdb6e4e6
MD
515 if (*errorp)
516 goto failed;
47637bff
MD
517
518 /*
0832c9bb
MD
519 * Check CRC if not allocating into uninitialized space (which we
520 * aren't when reserving space).
47637bff
MD
521 */
522 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
db9f9d7f
MD
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);
47637bff
MD
527 }
528
0832c9bb 529 /*
df301614 530 * Skip the layer if the zone is owned by someone other then us.
0832c9bb 531 */
df301614
MD
532 if (layer2->zone && layer2->zone != zone) {
533 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
534 goto again;
535 }
536 if (offset < layer2->append_off) {
537 next_offset += layer2->append_off - offset;
4a2796f3
MD
538 goto again;
539 }
540
541 /*
df301614
MD
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.
544 */
545 hammer_lock_ex(&hmp->blkmap_lock);
546
547 if (layer2->zone && layer2->zone != zone) {
548 hammer_unlock(&hmp->blkmap_lock);
549 next_offset += (HAMMER_LARGEBLOCK_SIZE - offset);
550 goto again;
551 }
552 if (offset < layer2->append_off) {
553 hammer_unlock(&hmp->blkmap_lock);
554 next_offset += layer2->append_off - offset;
555 goto again;
556 }
557
558 /*
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.
4a2796f3
MD
561 */
562 base_off = (next_offset &
df301614 563 (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) |
4a2796f3
MD
564 HAMMER_ZONE_RAW_BUFFER;
565 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root, base_off);
566 if (resv) {
4a2796f3 567 if (resv->zone != zone) {
df301614 568 hammer_unlock(&hmp->blkmap_lock);
4a2796f3
MD
569 next_offset = (next_offset + HAMMER_LARGEBLOCK_SIZE) &
570 ~HAMMER_LARGEBLOCK_MASK64;
571 goto again;
572 }
df301614
MD
573 if (offset < resv->append_off) {
574 hammer_unlock(&hmp->blkmap_lock);
575 next_offset += resv->append_off - offset;
576 goto again;
577 }
cb51be26 578 ++resv->refs;
df301614 579 resx = NULL;
cb51be26 580 } else {
bac808fe 581 resx = kmalloc(sizeof(*resv), hmp->m_misc,
df301614
MD
582 M_WAITOK | M_ZERO | M_USE_RESERVE);
583 resx->refs = 1;
584 resx->zone = zone;
585 resx->zone_offset = base_off;
5e435c92
MD
586 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE)
587 resx->flags |= HAMMER_RESF_LAYER2FREE;
df301614
MD
588 resv = RB_INSERT(hammer_res_rb_tree, &hmp->rb_resv_root, resx);
589 KKASSERT(resv == NULL);
590 resv = resx;
a7e9bef1 591 ++hammer_count_reservations;
cb51be26 592 }
df301614 593 resv->append_off = offset + bytes;
cb51be26 594
47637bff 595 /*
0832c9bb
MD
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.
598 *
4a2796f3
MD
599 * If we are reserving a whole buffer (or more), the caller will
600 * probably use a direct read, so do nothing.
47637bff 601 */
0832c9bb
MD
602 if (bytes < HAMMER_BUFSIZE && (next_offset & HAMMER_BUFMASK) == 0) {
603 hammer_bnew(hmp, next_offset, errorp, &buffer3);
604 }
605
47637bff
MD
606 /*
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.
610 */
df301614
MD
611 blockmap->next_offset = next_offset + bytes;
612 hammer_unlock(&hmp->blkmap_lock);
0832c9bb 613
df301614 614failed:
47637bff
MD
615 if (buffer1)
616 hammer_rel_buffer(buffer1, 0);
617 if (buffer2)
618 hammer_rel_buffer(buffer2, 0);
619 if (buffer3)
620 hammer_rel_buffer(buffer3, 0);
621 hammer_rel_volume(root_volume, 0);
0832c9bb
MD
622 *zone_offp = next_offset;
623
624 return(resv);
625}
626
627/*
5e435c92
MD
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.
0832c9bb
MD
632 */
633void
634hammer_blockmap_reserve_complete(hammer_mount_t hmp, hammer_reserve_t resv)
635{
5e435c92 636 hammer_off_t base_offset;
362ec2dc 637 int error;
1b0ab2c3 638
0832c9bb 639 KKASSERT(resv->refs > 0);
5e435c92
MD
640 KKASSERT((resv->zone_offset & HAMMER_OFF_ZONE_MASK) ==
641 HAMMER_ZONE_RAW_BUFFER);
642
643 /*
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.
362ec2dc
MD
647 *
648 * If we are unable to clean out all related HAMMER buffers we
649 * requeue the delay.
5e435c92
MD
650 */
651 if (resv->refs == 1 && (resv->flags & HAMMER_RESF_LAYER2FREE)) {
652 resv->append_off = HAMMER_LARGEBLOCK_SIZE;
1ce12d35
MD
653 base_offset = resv->zone_offset & ~HAMMER_OFF_ZONE_MASK;
654 base_offset = HAMMER_ZONE_ENCODE(resv->zone, base_offset);
362ec2dc
MD
655 error = hammer_del_buffers(hmp, base_offset,
656 resv->zone_offset,
657 HAMMER_LARGEBLOCK_SIZE,
f7d0505a
MD
658 1);
659 if (hammer_debug_general & 0x20000) {
660 kprintf("hammer: dellgblk %016jx error %d\n",
661 (intmax_t)base_offset, error);
662 }
362ec2dc
MD
663 if (error)
664 hammer_reserve_setdelay(hmp, resv);
5e435c92 665 }
0832c9bb 666 if (--resv->refs == 0) {
f7d0505a
MD
667 if (hammer_debug_general & 0x20000) {
668 kprintf("hammer: delresvr %016jx zone %02x\n",
669 (intmax_t)resv->zone_offset, resv->zone);
670 }
cb51be26 671 KKASSERT((resv->flags & HAMMER_RESF_ONDELAY) == 0);
0832c9bb 672 RB_REMOVE(hammer_res_rb_tree, &hmp->rb_resv_root, resv);
bac808fe 673 kfree(resv, hmp->m_misc);
0832c9bb
MD
674 --hammer_count_reservations;
675 }
47637bff
MD
676}
677
cb51be26 678/*
5e435c92
MD
679 * Prevent a potentially free big-block from being reused until after
680 * the related flushes have completely cycled, otherwise crash recovery
681 * could resurrect a data block that was already reused and overwritten.
682 *
1ce12d35
MD
683 * The caller might reset the underlying layer2 entry's append_off to 0, so
684 * our covering append_off must be set to max to prevent any reallocation
685 * until after the flush delays complete, not to mention proper invalidation
686 * of any underlying cached blocks.
cb51be26 687 */
5e435c92 688static void
362ec2dc 689hammer_reserve_setdelay_offset(hammer_mount_t hmp, hammer_off_t base_offset,
1ce12d35 690 int zone, struct hammer_blockmap_layer2 *layer2)
cb51be26 691{
5e435c92 692 hammer_reserve_t resv;
df301614 693
5e435c92
MD
694 /*
695 * Allocate the reservation if necessary.
1ce12d35
MD
696 *
697 * NOTE: need lock in future around resv lookup/allocation and
698 * the setdelay call, currently refs is not bumped until the call.
5e435c92
MD
699 */
700again:
701 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root, base_offset);
cb51be26 702 if (resv == NULL) {
bac808fe 703 resv = kmalloc(sizeof(*resv), hmp->m_misc,
df301614 704 M_WAITOK | M_ZERO | M_USE_RESERVE);
1ce12d35 705 resv->zone = zone;
5e435c92
MD
706 resv->zone_offset = base_offset;
707 resv->refs = 0;
1ce12d35
MD
708 resv->append_off = HAMMER_LARGEBLOCK_SIZE;
709
5e435c92
MD
710 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE)
711 resv->flags |= HAMMER_RESF_LAYER2FREE;
df301614 712 if (RB_INSERT(hammer_res_rb_tree, &hmp->rb_resv_root, resv)) {
bac808fe 713 kfree(resv, hmp->m_misc);
5e435c92 714 goto again;
df301614 715 }
5e435c92 716 ++hammer_count_reservations;
1ce12d35
MD
717 } else {
718 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE)
719 resv->flags |= HAMMER_RESF_LAYER2FREE;
5e435c92 720 }
1ce12d35 721 hammer_reserve_setdelay(hmp, resv);
362ec2dc 722}
5e435c92 723
362ec2dc
MD
724/*
725 * Enter the reservation on the on-delay list, or move it if it
726 * is already on the list.
727 */
728static void
729hammer_reserve_setdelay(hammer_mount_t hmp, hammer_reserve_t resv)
730{
5e435c92 731 if (resv->flags & HAMMER_RESF_ONDELAY) {
cb51be26
MD
732 TAILQ_REMOVE(&hmp->delay_list, resv, delay_entry);
733 resv->flush_group = hmp->flusher.next + 1;
5e435c92 734 TAILQ_INSERT_TAIL(&hmp->delay_list, resv, delay_entry);
cb51be26 735 } else {
5e435c92 736 ++resv->refs;
a7e9bef1 737 ++hmp->rsv_fromdelay;
df301614
MD
738 resv->flags |= HAMMER_RESF_ONDELAY;
739 resv->flush_group = hmp->flusher.next + 1;
740 TAILQ_INSERT_TAIL(&hmp->delay_list, resv, delay_entry);
741 }
cb51be26
MD
742}
743
f7d0505a
MD
744/*
745 * Reserve has reached its flush point, remove it from the delay list
746 * and finish it off. hammer_blockmap_reserve_complete() inherits
747 * the ondelay reference.
748 */
cb51be26
MD
749void
750hammer_reserve_clrdelay(hammer_mount_t hmp, hammer_reserve_t resv)
751{
752 KKASSERT(resv->flags & HAMMER_RESF_ONDELAY);
753 resv->flags &= ~HAMMER_RESF_ONDELAY;
754 TAILQ_REMOVE(&hmp->delay_list, resv, delay_entry);
a7e9bef1 755 --hmp->rsv_fromdelay;
cb51be26
MD
756 hammer_blockmap_reserve_complete(hmp, resv);
757}
758
47637bff 759/*
4a2796f3 760 * Backend function - free (offset, bytes) in a zone.
cdb6e4e6
MD
761 *
762 * XXX error return
40043e7f 763 */
c3be93f2 764void
36f82b23 765hammer_blockmap_free(hammer_transaction_t trans,
cb51be26 766 hammer_off_t zone_offset, int bytes)
40043e7f 767{
0832c9bb 768 hammer_mount_t hmp;
c3be93f2 769 hammer_volume_t root_volume;
cb51be26 770 hammer_blockmap_t freemap;
c3be93f2
MD
771 struct hammer_blockmap_layer1 *layer1;
772 struct hammer_blockmap_layer2 *layer2;
f03c9cf4
MD
773 hammer_buffer_t buffer1 = NULL;
774 hammer_buffer_t buffer2 = NULL;
c3be93f2
MD
775 hammer_off_t layer1_offset;
776 hammer_off_t layer2_offset;
cb51be26 777 hammer_off_t base_off;
c3be93f2
MD
778 int error;
779 int zone;
780
cb51be26
MD
781 if (bytes == 0)
782 return;
0832c9bb
MD
783 hmp = trans->hmp;
784
cb51be26
MD
785 /*
786 * Alignment
787 */
4a2796f3
MD
788 bytes = (bytes + 15) & ~15;
789 KKASSERT(bytes <= HAMMER_XBUFSIZE);
790 KKASSERT(((zone_offset ^ (zone_offset + (bytes - 1))) &
791 ~HAMMER_LARGEBLOCK_MASK64) == 0);
f03c9cf4 792
cb51be26
MD
793 /*
794 * Basic zone validation & locking
795 */
796 zone = HAMMER_ZONE_DECODE(zone_offset);
797 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
798 root_volume = trans->rootvol;
799 error = 0;
f03c9cf4 800
cb51be26 801 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
c3be93f2
MD
802
803 /*
804 * Dive layer 1.
805 */
cb51be26
MD
806 layer1_offset = freemap->phys_offset +
807 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
0832c9bb 808 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
cdb6e4e6
MD
809 if (error)
810 goto failed;
cb51be26
MD
811 KKASSERT(layer1->phys_offset &&
812 layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
19619882 813 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
db9f9d7f
MD
814 hammer_lock_ex(&hmp->blkmap_lock);
815 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
816 panic("CRC FAILED: LAYER1");
817 hammer_unlock(&hmp->blkmap_lock);
19619882 818 }
c3be93f2
MD
819
820 /*
821 * Dive layer 2, each entry represents a large-block.
822 */
823 layer2_offset = layer1->phys_offset +
cb51be26 824 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
0832c9bb 825 layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
cdb6e4e6
MD
826 if (error)
827 goto failed;
19619882 828 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
db9f9d7f
MD
829 hammer_lock_ex(&hmp->blkmap_lock);
830 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
831 panic("CRC FAILED: LAYER2");
832 hammer_unlock(&hmp->blkmap_lock);
19619882
MD
833 }
834
df301614
MD
835 hammer_lock_ex(&hmp->blkmap_lock);
836
36f82b23 837 hammer_modify_buffer(trans, buffer2, layer2, sizeof(*layer2));
4a2796f3
MD
838
839 /*
5e435c92 840 * Free space previously allocated via blockmap_alloc().
320a5c59
MD
841 *
842 * NOTE: bytes_free can be and remain negative due to de-dup ops
843 * but can never become larger than HAMMER_LARGEBLOCK_SIZE.
4a2796f3
MD
844 */
845 KKASSERT(layer2->zone == zone);
846 layer2->bytes_free += bytes;
847 KKASSERT(layer2->bytes_free <= HAMMER_LARGEBLOCK_SIZE);
5e435c92
MD
848
849 /*
850 * If a big-block becomes entirely free we must create a covering
851 * reservation to prevent premature reuse. Note, however, that
852 * the big-block and/or reservation may still have an append_off
853 * that allows further (non-reused) allocations.
854 *
855 * Once the reservation has been made we re-check layer2 and if
856 * the big-block is still entirely free we reset the layer2 entry.
857 * The reservation will prevent premature reuse.
858 *
859 * NOTE: hammer_buffer's are only invalidated when the reservation
860 * is completed, if the layer2 entry is still completely free at
861 * that time. Any allocations from the reservation that may have
862 * occured in the mean time, or active references on the reservation
863 * from new pending allocations, will prevent the invalidation from
864 * occuring.
865 */
4a2796f3
MD
866 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE) {
867 base_off = (zone_offset & (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) | HAMMER_ZONE_RAW_BUFFER;
5e435c92 868
1ce12d35 869 hammer_reserve_setdelay_offset(hmp, base_off, zone, layer2);
5e435c92 870 if (layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE) {
4a2796f3
MD
871 layer2->zone = 0;
872 layer2->append_off = 0;
36f82b23
MD
873 hammer_modify_buffer(trans, buffer1,
874 layer1, sizeof(*layer1));
4a2796f3 875 ++layer1->blocks_free;
19619882
MD
876 layer1->layer1_crc = crc32(layer1,
877 HAMMER_LAYER1_CRCSIZE);
10a5d1ba 878 hammer_modify_buffer_done(buffer1);
cb51be26
MD
879 hammer_modify_volume_field(trans,
880 trans->rootvol,
881 vol0_stat_freebigblocks);
4a2796f3 882 ++root_volume->ondisk->vol0_stat_freebigblocks;
cb51be26
MD
883 hmp->copy_stat_freebigblocks =
884 root_volume->ondisk->vol0_stat_freebigblocks;
885 hammer_modify_volume_done(trans->rootvol);
c3be93f2
MD
886 }
887 }
4a2796f3
MD
888 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
889 hammer_modify_buffer_done(buffer2);
890 hammer_unlock(&hmp->blkmap_lock);
891
cdb6e4e6 892failed:
4a2796f3
MD
893 if (buffer1)
894 hammer_rel_buffer(buffer1, 0);
895 if (buffer2)
896 hammer_rel_buffer(buffer2, 0);
897}
898
bb29b5d8
MD
899int
900hammer_blockmap_dedup(hammer_transaction_t trans,
901 hammer_off_t zone_offset, int bytes)
902{
903 hammer_mount_t hmp;
904 hammer_volume_t root_volume;
bb29b5d8
MD
905 hammer_blockmap_t freemap;
906 struct hammer_blockmap_layer1 *layer1;
907 struct hammer_blockmap_layer2 *layer2;
908 hammer_buffer_t buffer1 = NULL;
909 hammer_buffer_t buffer2 = NULL;
910 hammer_off_t layer1_offset;
911 hammer_off_t layer2_offset;
912 int32_t temp;
913 int error;
914 int zone;
915
916 if (bytes == 0)
917 return (0);
918 hmp = trans->hmp;
919
920 /*
921 * Alignment
922 */
923 bytes = (bytes + 15) & ~15;
924 KKASSERT(bytes <= HAMMER_LARGEBLOCK_SIZE);
925 KKASSERT(((zone_offset ^ (zone_offset + (bytes - 1))) &
926 ~HAMMER_LARGEBLOCK_MASK64) == 0);
927
928 /*
929 * Basic zone validation & locking
930 */
931 zone = HAMMER_ZONE_DECODE(zone_offset);
932 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
933 root_volume = trans->rootvol;
934 error = 0;
935
bb29b5d8
MD
936 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
937
938 /*
939 * Dive layer 1.
940 */
941 layer1_offset = freemap->phys_offset +
942 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
943 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
944 if (error)
945 goto failed;
946 KKASSERT(layer1->phys_offset &&
947 layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
948 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
949 hammer_lock_ex(&hmp->blkmap_lock);
950 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
951 panic("CRC FAILED: LAYER1");
952 hammer_unlock(&hmp->blkmap_lock);
953 }
954
955 /*
956 * Dive layer 2, each entry represents a large-block.
957 */
958 layer2_offset = layer1->phys_offset +
959 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
960 layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
961 if (error)
962 goto failed;
963 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
964 hammer_lock_ex(&hmp->blkmap_lock);
965 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
966 panic("CRC FAILED: LAYER2");
967 hammer_unlock(&hmp->blkmap_lock);
968 }
969
970 hammer_lock_ex(&hmp->blkmap_lock);
971
972 hammer_modify_buffer(trans, buffer2, layer2, sizeof(*layer2));
973
974 /*
975 * Free space previously allocated via blockmap_alloc().
976 *
977 * NOTE: bytes_free can be and remain negative due to de-dup ops
978 * but can never become larger than HAMMER_LARGEBLOCK_SIZE.
979 */
980 KKASSERT(layer2->zone == zone);
981 temp = layer2->bytes_free - HAMMER_LARGEBLOCK_SIZE * 2;
982 cpu_ccfence(); /* prevent gcc from optimizing temp out */
983 if (temp > layer2->bytes_free) {
984 error = ERANGE;
985 goto underflow;
986 }
987 layer2->bytes_free -= bytes;
988
989 KKASSERT(layer2->bytes_free <= HAMMER_LARGEBLOCK_SIZE);
990
991 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
992underflow:
993 hammer_modify_buffer_done(buffer2);
994 hammer_unlock(&hmp->blkmap_lock);
995
996failed:
997 if (buffer1)
998 hammer_rel_buffer(buffer1, 0);
999 if (buffer2)
1000 hammer_rel_buffer(buffer2, 0);
1001 return (error);
1002}
1003
4a2796f3
MD
1004/*
1005 * Backend function - finalize (offset, bytes) in a zone.
1006 *
1007 * Allocate space that was previously reserved by the frontend.
1008 */
cdb6e4e6 1009int
4a2796f3 1010hammer_blockmap_finalize(hammer_transaction_t trans,
5e435c92 1011 hammer_reserve_t resv,
4a2796f3
MD
1012 hammer_off_t zone_offset, int bytes)
1013{
1014 hammer_mount_t hmp;
1015 hammer_volume_t root_volume;
4a2796f3
MD
1016 hammer_blockmap_t freemap;
1017 struct hammer_blockmap_layer1 *layer1;
1018 struct hammer_blockmap_layer2 *layer2;
1019 hammer_buffer_t buffer1 = NULL;
1020 hammer_buffer_t buffer2 = NULL;
1021 hammer_off_t layer1_offset;
1022 hammer_off_t layer2_offset;
1023 int error;
1024 int zone;
df301614 1025 int offset;
4a2796f3
MD
1026
1027 if (bytes == 0)
cdb6e4e6 1028 return(0);
4a2796f3
MD
1029 hmp = trans->hmp;
1030
1031 /*
1032 * Alignment
1033 */
1034 bytes = (bytes + 15) & ~15;
1035 KKASSERT(bytes <= HAMMER_XBUFSIZE);
1036
1037 /*
1038 * Basic zone validation & locking
1039 */
1040 zone = HAMMER_ZONE_DECODE(zone_offset);
1041 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
1042 root_volume = trans->rootvol;
1043 error = 0;
4a2796f3 1044
4a2796f3
MD
1045 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
1046
1047 /*
1048 * Dive layer 1.
1049 */
1050 layer1_offset = freemap->phys_offset +
1051 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
1052 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
cdb6e4e6
MD
1053 if (error)
1054 goto failed;
4a2796f3
MD
1055 KKASSERT(layer1->phys_offset &&
1056 layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
1057 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
db9f9d7f
MD
1058 hammer_lock_ex(&hmp->blkmap_lock);
1059 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
1060 panic("CRC FAILED: LAYER1");
1061 hammer_unlock(&hmp->blkmap_lock);
4a2796f3
MD
1062 }
1063
1064 /*
1065 * Dive layer 2, each entry represents a large-block.
1066 */
1067 layer2_offset = layer1->phys_offset +
1068 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
1069 layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
cdb6e4e6
MD
1070 if (error)
1071 goto failed;
4a2796f3 1072 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
db9f9d7f
MD
1073 hammer_lock_ex(&hmp->blkmap_lock);
1074 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
1075 panic("CRC FAILED: LAYER2");
1076 hammer_unlock(&hmp->blkmap_lock);
4a2796f3
MD
1077 }
1078
df301614
MD
1079 hammer_lock_ex(&hmp->blkmap_lock);
1080
4a2796f3
MD
1081 hammer_modify_buffer(trans, buffer2, layer2, sizeof(*layer2));
1082
1083 /*
1084 * Finalize some or all of the space covered by a current
1085 * reservation. An allocation in the same layer may have
1086 * already assigned ownership.
1087 */
1088 if (layer2->zone == 0) {
1089 hammer_modify_buffer(trans, buffer1,
1090 layer1, sizeof(*layer1));
1091 --layer1->blocks_free;
1092 layer1->layer1_crc = crc32(layer1,
1093 HAMMER_LAYER1_CRCSIZE);
1094 hammer_modify_buffer_done(buffer1);
1095 layer2->zone = zone;
1096 KKASSERT(layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE);
1097 KKASSERT(layer2->append_off == 0);
1098 hammer_modify_volume_field(trans,
1099 trans->rootvol,
1100 vol0_stat_freebigblocks);
1101 --root_volume->ondisk->vol0_stat_freebigblocks;
1102 hmp->copy_stat_freebigblocks =
1103 root_volume->ondisk->vol0_stat_freebigblocks;
1104 hammer_modify_volume_done(trans->rootvol);
1105 }
1106 if (layer2->zone != zone)
1107 kprintf("layer2 zone mismatch %d %d\n", layer2->zone, zone);
1108 KKASSERT(layer2->zone == zone);
1ce12d35 1109 KKASSERT(bytes != 0);
4a2796f3 1110 layer2->bytes_free -= bytes;
5e435c92
MD
1111 if (resv)
1112 resv->flags &= ~HAMMER_RESF_LAYER2FREE;
4a2796f3
MD
1113
1114 /*
1115 * Finalizations can occur out of order, or combined with allocations.
1116 * append_off must be set to the highest allocated offset.
1117 */
df301614
MD
1118 offset = ((int)zone_offset & HAMMER_LARGEBLOCK_MASK) + bytes;
1119 if (layer2->append_off < offset)
1120 layer2->append_off = offset;
4a2796f3 1121
19619882 1122 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
10a5d1ba 1123 hammer_modify_buffer_done(buffer2);
d99d6bf5 1124 hammer_unlock(&hmp->blkmap_lock);
f03c9cf4 1125
cdb6e4e6 1126failed:
f03c9cf4
MD
1127 if (buffer1)
1128 hammer_rel_buffer(buffer1, 0);
1129 if (buffer2)
1130 hammer_rel_buffer(buffer2, 0);
cdb6e4e6 1131 return(error);
40043e7f
MD
1132}
1133
bf686dbe 1134/*
320a5c59
MD
1135 * Return the approximate number of free bytes in the big-block
1136 * containing the specified blockmap offset.
1137 *
1138 * WARNING: A negative number can be returned if data de-dup exists,
1139 * and the result will also not represent he actual number
1140 * of free bytes in this case.
1141 *
1142 * This code is used only by the reblocker.
bf686dbe
MD
1143 */
1144int
cb51be26 1145hammer_blockmap_getfree(hammer_mount_t hmp, hammer_off_t zone_offset,
bf686dbe
MD
1146 int *curp, int *errorp)
1147{
1148 hammer_volume_t root_volume;
cb51be26
MD
1149 hammer_blockmap_t blockmap;
1150 hammer_blockmap_t freemap;
bf686dbe
MD
1151 struct hammer_blockmap_layer1 *layer1;
1152 struct hammer_blockmap_layer2 *layer2;
1153 hammer_buffer_t buffer = NULL;
1154 hammer_off_t layer1_offset;
1155 hammer_off_t layer2_offset;
320a5c59 1156 int32_t bytes;
bf686dbe
MD
1157 int zone;
1158
cb51be26 1159 zone = HAMMER_ZONE_DECODE(zone_offset);
bf686dbe
MD
1160 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
1161 root_volume = hammer_get_root_volume(hmp, errorp);
1162 if (*errorp) {
1163 *curp = 0;
1164 return(0);
1165 }
cb51be26
MD
1166 blockmap = &hmp->blockmap[zone];
1167 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
bf686dbe
MD
1168
1169 /*
1170 * Dive layer 1.
1171 */
cb51be26
MD
1172 layer1_offset = freemap->phys_offset +
1173 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
bf686dbe 1174 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer);
cdb6e4e6
MD
1175 if (*errorp) {
1176 bytes = 0;
1177 goto failed;
1178 }
bf686dbe 1179 KKASSERT(layer1->phys_offset);
19619882 1180 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
db9f9d7f
MD
1181 hammer_lock_ex(&hmp->blkmap_lock);
1182 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
1183 panic("CRC FAILED: LAYER1");
1184 hammer_unlock(&hmp->blkmap_lock);
19619882 1185 }
bf686dbe
MD
1186
1187 /*
1188 * Dive layer 2, each entry represents a large-block.
cdb6e4e6
MD
1189 *
1190 * (reuse buffer, layer1 pointer becomes invalid)
bf686dbe
MD
1191 */
1192 layer2_offset = layer1->phys_offset +
cb51be26 1193 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
bf686dbe 1194 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer);
cdb6e4e6
MD
1195 if (*errorp) {
1196 bytes = 0;
1197 goto failed;
1198 }
19619882 1199 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
db9f9d7f
MD
1200 hammer_lock_ex(&hmp->blkmap_lock);
1201 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
1202 panic("CRC FAILED: LAYER2");
1203 hammer_unlock(&hmp->blkmap_lock);
19619882 1204 }
cb51be26 1205 KKASSERT(layer2->zone == zone);
bf686dbe
MD
1206
1207 bytes = layer2->bytes_free;
1208
cb51be26 1209 if ((blockmap->next_offset ^ zone_offset) & ~HAMMER_LARGEBLOCK_MASK64)
bf686dbe
MD
1210 *curp = 0;
1211 else
1212 *curp = 1;
cdb6e4e6 1213failed:
bf686dbe
MD
1214 if (buffer)
1215 hammer_rel_buffer(buffer, 0);
1216 hammer_rel_volume(root_volume, 0);
1217 if (hammer_debug_general & 0x0800) {
1218 kprintf("hammer_blockmap_getfree: %016llx -> %d\n",
973c11b9 1219 (long long)zone_offset, bytes);
bf686dbe
MD
1220 }
1221 return(bytes);
1222}
1223
1224
40043e7f
MD
1225/*
1226 * Lookup a blockmap offset.
1227 */
1228hammer_off_t
cb51be26
MD
1229hammer_blockmap_lookup(hammer_mount_t hmp, hammer_off_t zone_offset,
1230 int *errorp)
40043e7f
MD
1231{
1232 hammer_volume_t root_volume;
cb51be26 1233 hammer_blockmap_t freemap;
c3be93f2
MD
1234 struct hammer_blockmap_layer1 *layer1;
1235 struct hammer_blockmap_layer2 *layer2;
40043e7f 1236 hammer_buffer_t buffer = NULL;
c3be93f2
MD
1237 hammer_off_t layer1_offset;
1238 hammer_off_t layer2_offset;
40043e7f 1239 hammer_off_t result_offset;
cb51be26
MD
1240 hammer_off_t base_off;
1241 hammer_reserve_t resv;
40043e7f 1242 int zone;
40043e7f 1243
cb51be26
MD
1244 /*
1245 * Calculate the zone-2 offset.
1246 */
1247 zone = HAMMER_ZONE_DECODE(zone_offset);
40043e7f 1248 KKASSERT(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
cb51be26
MD
1249
1250 result_offset = (zone_offset & ~HAMMER_OFF_ZONE_MASK) |
1251 HAMMER_ZONE_RAW_BUFFER;
1252
1253 /*
1254 * We can actually stop here, normal blockmaps are now direct-mapped
1255 * onto the freemap and so represent zone-2 addresses.
1256 */
1257 if (hammer_verify_zone == 0) {
1258 *errorp = 0;
1259 return(result_offset);
1260 }
1261
1262 /*
1263 * Validate the allocation zone
1264 */
40043e7f
MD
1265 root_volume = hammer_get_root_volume(hmp, errorp);
1266 if (*errorp)
1267 return(0);
cb51be26
MD
1268 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
1269 KKASSERT(freemap->phys_offset != 0);
40043e7f
MD
1270
1271 /*
c3be93f2 1272 * Dive layer 1.
40043e7f 1273 */
cb51be26
MD
1274 layer1_offset = freemap->phys_offset +
1275 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset);
c3be93f2 1276 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer);
cdb6e4e6
MD
1277 if (*errorp)
1278 goto failed;
cb51be26 1279 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
19619882 1280 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
db9f9d7f
MD
1281 hammer_lock_ex(&hmp->blkmap_lock);
1282 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
1283 panic("CRC FAILED: LAYER1");
1284 hammer_unlock(&hmp->blkmap_lock);
19619882 1285 }
40043e7f
MD
1286
1287 /*
c3be93f2 1288 * Dive layer 2, each entry represents a large-block.
40043e7f 1289 */
c3be93f2 1290 layer2_offset = layer1->phys_offset +
cb51be26 1291 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset);
c3be93f2 1292 layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer);
40043e7f 1293
cdb6e4e6
MD
1294 if (*errorp)
1295 goto failed;
cb51be26
MD
1296 if (layer2->zone == 0) {
1297 base_off = (zone_offset & (~HAMMER_LARGEBLOCK_MASK64 & ~HAMMER_OFF_ZONE_MASK)) | HAMMER_ZONE_RAW_BUFFER;
1298 resv = RB_LOOKUP(hammer_res_rb_tree, &hmp->rb_resv_root,
1299 base_off);
1300 KKASSERT(resv && resv->zone == zone);
1301
1302 } else if (layer2->zone != zone) {
1303 panic("hammer_blockmap_lookup: bad zone %d/%d\n",
1304 layer2->zone, zone);
1305 }
19619882 1306 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
db9f9d7f
MD
1307 hammer_lock_ex(&hmp->blkmap_lock);
1308 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
1309 panic("CRC FAILED: LAYER2");
1310 hammer_unlock(&hmp->blkmap_lock);
19619882 1311 }
c3be93f2 1312
cdb6e4e6 1313failed:
40043e7f
MD
1314 if (buffer)
1315 hammer_rel_buffer(buffer, 0);
1316 hammer_rel_volume(root_volume, 0);
1317 if (hammer_debug_general & 0x0800) {
1318 kprintf("hammer_blockmap_lookup: %016llx -> %016llx\n",
973c11b9 1319 (long long)zone_offset, (long long)result_offset);
40043e7f
MD
1320 }
1321 return(result_offset);
1322}
1323
bf686dbe
MD
1324
1325/*
cb51be26 1326 * Check space availability
b0aab9b9
MD
1327 *
1328 * MPSAFE - does not require fs_token
bf686dbe 1329 */
cb51be26 1330int
0f65be10 1331_hammer_checkspace(hammer_mount_t hmp, int slop, int64_t *resp)
bf686dbe 1332{
cb51be26
MD
1333 const int in_size = sizeof(struct hammer_inode_data) +
1334 sizeof(union hammer_btree_elm);
1335 const int rec_size = (sizeof(union hammer_btree_elm) * 2);
a7e9bef1 1336 int64_t usedbytes;
cb51be26 1337
a7e9bef1
MD
1338 usedbytes = hmp->rsv_inodes * in_size +
1339 hmp->rsv_recs * rec_size +
1340 hmp->rsv_databytes +
7b6ccb11
MD
1341 ((int64_t)hmp->rsv_fromdelay << HAMMER_LARGEBLOCK_BITS) +
1342 ((int64_t)hidirtybufspace << 2) +
1343 (slop << HAMMER_LARGEBLOCK_BITS);
a7e9bef1 1344
7b6ccb11 1345 hammer_count_extra_space_used = usedbytes; /* debugging */
0f65be10
MD
1346 if (resp)
1347 *resp = usedbytes;
a7e9bef1 1348
7b6ccb11
MD
1349 if (hmp->copy_stat_freebigblocks >=
1350 (usedbytes >> HAMMER_LARGEBLOCK_BITS)) {
cb51be26 1351 return(0);
7b6ccb11 1352 }
cb51be26 1353 return (ENOSPC);
6f97fce3
MD
1354}
1355