sbin/hammer: Use big-block append offset to limit recovery scan range
[dragonfly.git] / sbin / hammer / cmd_recover.c
blob6b39f51b6f028c2a40e4ebe8ed3afb4a692c228b
1 /*
2 * Copyright (c) 2010 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
9 * are met:
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.
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.
35 #include "hammer.h"
37 struct recover_dict {
38 struct recover_dict *next;
39 struct recover_dict *parent;
40 int64_t obj_id;
41 uint8_t obj_type;
42 uint8_t flags;
43 uint16_t pfs_id;
44 int64_t size;
45 char *name;
48 #define DICTF_MADEDIR 0x01
49 #define DICTF_MADEFILE 0x02
50 #define DICTF_PARENT 0x04 /* parent attached for real */
51 #define DICTF_TRAVERSED 0x80
53 typedef struct bigblock *bigblock_t;
55 static void recover_top(char *ptr, hammer_off_t offset);
56 static void recover_elm(hammer_btree_leaf_elm_t leaf);
57 static struct recover_dict *get_dict(int64_t obj_id, uint16_t pfs_id);
58 static char *recover_path(struct recover_dict *dict);
59 static void sanitize_string(char *str);
60 static hammer_off_t scan_raw_limit(void);
61 static void scan_bigblocks(int target_zone);
62 static void free_bigblocks(void);
63 static void add_bigblock_entry(hammer_off_t offset,
64 hammer_blockmap_layer1_t layer1, hammer_blockmap_layer2_t layer2);
65 static bigblock_t get_bigblock_entry(hammer_off_t offset);
67 static const char *TargetDir;
68 static int CachedFd = -1;
69 static char *CachedPath;
71 typedef struct bigblock {
72 RB_ENTRY(bigblock) entry;
73 hammer_off_t phys_offset; /* zone-2 */
74 struct hammer_blockmap_layer1 layer1;
75 struct hammer_blockmap_layer2 layer2;
76 } *bigblock_t;
78 static int
79 bigblock_cmp(bigblock_t b1, bigblock_t b2)
81 if (b1->phys_offset < b2->phys_offset)
82 return(-1);
83 if (b1->phys_offset > b2->phys_offset)
84 return(1);
85 return(0);
88 RB_HEAD(bigblock_rb_tree, bigblock) ZoneTree = RB_INITIALIZER(&ZoneTree);
89 RB_PROTOTYPE2(bigblock_rb_tree, bigblock, entry, bigblock_cmp, hammer_off_t);
90 RB_GENERATE2(bigblock_rb_tree, bigblock, entry, bigblock_cmp, hammer_off_t,
91 phys_offset);
94 * There was a hidden bug here while iterating zone-2 offset as
95 * shown in an example below.
97 * If a volume was once used as HAMMER filesystem which consists of
98 * multiple volumes whose usage has reached beyond the first volume,
99 * and then later re-formatted only using 1 volume, hammer recover is
100 * likely to hit assertion in get_buffer() due to having access to
101 * invalid volume (vol1,2,...) from old filesystem data.
103 * To avoid this, now the command only scans upto the last big-block
104 * that's actually used for filesystem data or meta-data at the moment,
105 * if all layer1/2 entries have correct CRC values. This also avoids
106 * recovery of irrelevant files from old filesystem.
108 * It also doesn't scan beyond append offset of big-blocks in B-Tree
109 * zone to avoid recovery of irrelevant files from old filesystem,
110 * if layer1/2 entries for those big-blocks have correct CRC values.
112 * |-----vol0-----|-----vol1-----|-----vol2-----| old filesystem
113 * <-----------------------> used by old filesystem
115 * |-----vol0-----| new filesystem
116 * <-----> used by new filesystem
117 * <-------> unused, invalid data from old filesystem
118 * <-> B-Tree nodes likely to point to vol1
121 void
122 hammer_cmd_recover(char **av, int ac)
124 struct buffer_info *data_buffer;
125 struct volume_info *volume;
126 bigblock_t b = NULL;
127 hammer_off_t off;
128 hammer_off_t off_end;
129 hammer_off_t off_blk;
130 hammer_off_t raw_limit = 0;
131 hammer_off_t zone_limit = 0;
132 char *ptr;
133 int i;
134 int target_zone = HAMMER_ZONE_BTREE_INDEX;
135 int full = 0;
136 int quick = 0;
138 if (ac < 1) {
139 fprintf(stderr, "hammer recover <target_dir> [full|quick]\n");
140 exit(1);
143 TargetDir = av[0];
144 if (ac > 1) {
145 if (!strcmp(av[1], "full"))
146 full = 1;
147 if (!strcmp(av[1], "quick"))
148 quick = 1;
150 assert(!full || !quick);
152 if (mkdir(TargetDir, 0777) == -1) {
153 if (errno != EEXIST) {
154 perror("mkdir");
155 exit(1);
159 printf("Running %sraw scan of HAMMER image, recovering to %s\n",
160 full ? "full " : quick ? "quick " : "",
161 TargetDir);
163 if (!full) {
164 scan_bigblocks(target_zone);
165 raw_limit = scan_raw_limit();
166 if (raw_limit) {
167 raw_limit += HAMMER_BIGBLOCK_SIZE;
168 assert(hammer_is_zone_raw_buffer(raw_limit));
172 if (quick) {
173 assert(!full);
174 if (!RB_EMPTY(&ZoneTree)) {
175 printf("Found zone-%d big-blocks at\n", target_zone);
176 RB_FOREACH(b, bigblock_rb_tree, &ZoneTree)
177 printf("%016jx\n", b->phys_offset);
179 b = RB_MAX(bigblock_rb_tree, &ZoneTree);
180 zone_limit = b->phys_offset + HAMMER_BIGBLOCK_SIZE;
181 assert(hammer_is_zone_raw_buffer(zone_limit));
185 if (raw_limit || zone_limit) {
186 #define _fmt "Scanning zone-%d big-blocks till %016jx"
187 if (!raw_limit) /* unlikely */
188 printf(_fmt" ???", target_zone, zone_limit);
189 else if (!zone_limit)
190 printf(_fmt, HAMMER_ZONE_RAW_BUFFER_INDEX, raw_limit);
191 else if (raw_limit >= zone_limit)
192 printf(_fmt, target_zone, zone_limit);
193 else /* unlikely */
194 printf(_fmt" ???", HAMMER_ZONE_RAW_BUFFER_INDEX, raw_limit);
195 printf("\n");
198 data_buffer = NULL;
199 for (i = 0; i < HAMMER_MAX_VOLUMES; i++) {
200 volume = get_volume(i);
201 if (volume == NULL)
202 continue;
204 printf("Scanning volume %d size %s\n",
205 volume->vol_no, sizetostr(volume->size));
206 off = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
207 off_end = off + HAMMER_VOL_BUF_SIZE(volume->ondisk);
209 while (off < off_end) {
210 off_blk = off & HAMMER_BIGBLOCK_MASK64;
211 if (off_blk == 0)
212 b = get_bigblock_entry(off);
214 if (raw_limit) {
215 if (off >= raw_limit) {
216 printf("Done %016jx\n", (uintmax_t)off);
217 goto end;
220 if (zone_limit) {
221 if (off >= zone_limit) {
222 printf("Done %016jx\n", (uintmax_t)off);
223 goto end;
225 if (b == NULL) {
226 off = HAMMER_ZONE_LAYER2_NEXT_OFFSET(off);
227 continue;
231 if (b) {
232 if (hammer_crc_test_layer1(&b->layer1) &&
233 hammer_crc_test_layer2(&b->layer2) &&
234 off_blk >= b->layer2.append_off) {
235 off = HAMMER_ZONE_LAYER2_NEXT_OFFSET(off);
236 continue;
240 ptr = get_buffer_data(off, &data_buffer, 0);
241 if (ptr)
242 recover_top(ptr, off);
243 off += HAMMER_BUFSIZE;
246 end:
247 rel_buffer(data_buffer);
248 free_bigblocks();
250 if (CachedPath) {
251 free(CachedPath);
252 close(CachedFd);
253 CachedPath = NULL;
254 CachedFd = -1;
258 static __inline
259 void
260 print_node(hammer_node_ondisk_t node, hammer_off_t offset)
262 char buf[HAMMER_BTREE_LEAF_ELMS + 1];
263 int maxcount = hammer_node_max_elements(node->type);
264 int i;
266 for (i = 0; i < node->count && i < maxcount; ++i)
267 buf[i] = hammer_elm_btype(&node->elms[i]);
268 buf[i] = '\0';
270 printf("%016jx %c %d %s\n", offset, node->type, node->count, buf);
274 * Top level recovery processor. Assume the data is a B-Tree node.
275 * If the CRC is good we attempt to process the node, building the
276 * object space and creating the dictionary as we go.
278 static void
279 recover_top(char *ptr, hammer_off_t offset)
281 hammer_node_ondisk_t node;
282 hammer_btree_elm_t elm;
283 int maxcount;
284 int i;
285 int isnode;
287 for (node = (void *)ptr; (char *)node < ptr + HAMMER_BUFSIZE; ++node) {
288 isnode = hammer_crc_test_btree(node);
289 maxcount = hammer_node_max_elements(node->type);
291 if (DebugOpt) {
292 if (isnode)
293 print_node(node, offset);
294 else if (DebugOpt > 1)
295 printf("%016jx -\n", offset);
297 offset += sizeof(*node);
299 if (isnode && node->type == HAMMER_BTREE_TYPE_LEAF) {
300 for (i = 0; i < node->count && i < maxcount; ++i) {
301 elm = &node->elms[i];
302 if (elm->base.btype == HAMMER_BTREE_TYPE_RECORD)
303 recover_elm(&elm->leaf);
309 static void
310 recover_elm(hammer_btree_leaf_elm_t leaf)
312 struct buffer_info *data_buffer = NULL;
313 struct recover_dict *dict;
314 struct recover_dict *dict2;
315 hammer_data_ondisk_t ondisk;
316 hammer_off_t data_offset;
317 struct stat st;
318 int chunk;
319 int len;
320 int zfill;
321 int64_t file_offset;
322 uint16_t pfs_id;
323 size_t nlen;
324 int fd;
325 char *name;
326 char *path1;
327 char *path2;
330 * Ignore deleted records
332 if (leaf->delete_ts)
333 return;
334 if ((data_offset = leaf->data_offset) != 0)
335 ondisk = get_buffer_data(data_offset, &data_buffer, 0);
336 else
337 ondisk = NULL;
338 if (ondisk == NULL)
339 goto done;
341 len = leaf->data_len;
342 chunk = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
343 if (chunk > len)
344 chunk = len;
346 if (len < 0 || len > HAMMER_XBUFSIZE || len > chunk)
347 goto done;
349 pfs_id = lo_to_pfs(leaf->base.localization);
352 * Note that meaning of leaf->base.obj_id differs depending
353 * on record type. For a direntry, leaf->base.obj_id points
354 * to its parent inode that this entry is a part of, but not
355 * its corresponding inode.
357 dict = get_dict(leaf->base.obj_id, pfs_id);
359 switch(leaf->base.rec_type) {
360 case HAMMER_RECTYPE_INODE:
362 * We found an inode which also tells us where the file
363 * or directory is in the directory hierarchy.
365 if (VerboseOpt) {
366 printf("inode %016jx:%05d found\n",
367 (uintmax_t)leaf->base.obj_id, pfs_id);
369 path1 = recover_path(dict);
372 * Attach the inode to its parent. This isn't strictly
373 * necessary because the information is also in the
374 * directory entries, but if we do not find the directory
375 * entry this ensures that the files will still be
376 * reasonably well organized in their proper directories.
378 if ((dict->flags & DICTF_PARENT) == 0 &&
379 dict->obj_id != HAMMER_OBJID_ROOT &&
380 ondisk->inode.parent_obj_id != 0) {
381 dict->flags |= DICTF_PARENT;
382 dict->parent = get_dict(ondisk->inode.parent_obj_id,
383 pfs_id);
384 if (dict->parent &&
385 (dict->parent->flags & DICTF_MADEDIR) == 0) {
386 dict->parent->flags |= DICTF_MADEDIR;
387 path2 = recover_path(dict->parent);
388 printf("mkdir %s\n", path2);
389 mkdir(path2, 0777);
390 free(path2);
391 path2 = NULL;
394 if (dict->obj_type == 0)
395 dict->obj_type = ondisk->inode.obj_type;
396 dict->size = ondisk->inode.size;
397 path2 = recover_path(dict);
399 if (lstat(path1, &st) == 0) {
400 if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
401 truncate(path1, dict->size);
402 /* chmod(path1, 0666); */
404 if (strcmp(path1, path2)) {
405 printf("Rename %s -> %s\n", path1, path2);
406 rename(path1, path2);
408 } else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
409 printf("mkinode (file) %s\n", path2);
410 fd = open(path2, O_RDWR|O_CREAT, 0666);
411 if (fd > 0)
412 close(fd);
413 } else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
414 printf("mkinode (dir) %s\n", path2);
415 mkdir(path2, 0777);
416 dict->flags |= DICTF_MADEDIR;
418 free(path1);
419 free(path2);
420 break;
421 case HAMMER_RECTYPE_DATA:
423 * File record data
425 if (leaf->base.obj_id == 0)
426 break;
427 if (VerboseOpt) {
428 printf("inode %016jx:%05d data %016jx,%d\n",
429 (uintmax_t)leaf->base.obj_id,
430 pfs_id,
431 (uintmax_t)leaf->base.key - len,
432 len);
436 * Update the dictionary entry
438 if (dict->obj_type == 0)
439 dict->obj_type = HAMMER_OBJTYPE_REGFILE;
442 * If the parent directory has not been created we
443 * have to create it (typically a PFS%05d)
445 if (dict->parent &&
446 (dict->parent->flags & DICTF_MADEDIR) == 0) {
447 dict->parent->flags |= DICTF_MADEDIR;
448 path2 = recover_path(dict->parent);
449 printf("mkdir %s\n", path2);
450 mkdir(path2, 0777);
451 free(path2);
452 path2 = NULL;
456 * Create the file if necessary, report file creations
458 path1 = recover_path(dict);
459 if (CachedPath && strcmp(CachedPath, path1) == 0) {
460 fd = CachedFd;
461 } else {
462 fd = open(path1, O_CREAT|O_RDWR, 0666);
464 if (fd < 0) {
465 printf("Unable to create %s: %s\n",
466 path1, strerror(errno));
467 free(path1);
468 break;
470 if ((dict->flags & DICTF_MADEFILE) == 0) {
471 dict->flags |= DICTF_MADEFILE;
472 printf("mkfile %s\n", path1);
476 * And write the record. A HAMMER data block is aligned
477 * and may contain trailing zeros after the file EOF. The
478 * inode record is required to get the actual file size.
480 * However, when the inode record is not available
481 * we can do a sparse write and that will get it right
482 * most of the time even if the inode record is never
483 * found.
485 file_offset = (int64_t)leaf->base.key - len;
486 lseek(fd, (off_t)file_offset, SEEK_SET);
487 while (len) {
488 if (dict->size == -1) {
489 for (zfill = chunk - 1; zfill >= 0; --zfill) {
490 if (((char *)ondisk)[zfill])
491 break;
493 ++zfill;
494 } else {
495 zfill = chunk;
498 if (zfill)
499 write(fd, ondisk, zfill);
500 if (zfill < chunk)
501 lseek(fd, chunk - zfill, SEEK_CUR);
503 len -= chunk;
504 data_offset += chunk;
505 file_offset += chunk;
506 ondisk = get_buffer_data(data_offset, &data_buffer, 0);
507 if (ondisk == NULL)
508 break;
509 chunk = HAMMER_BUFSIZE -
510 ((int)data_offset & HAMMER_BUFMASK);
511 if (chunk > len)
512 chunk = len;
514 if (dict->size >= 0 && file_offset > dict->size) {
515 ftruncate(fd, dict->size);
516 /* fchmod(fd, 0666); */
519 if (fd == CachedFd) {
520 free(path1);
521 } else if (CachedPath) {
522 free(CachedPath);
523 close(CachedFd);
524 CachedPath = path1;
525 CachedFd = fd;
526 } else {
527 CachedPath = path1;
528 CachedFd = fd;
530 break;
531 case HAMMER_RECTYPE_DIRENTRY:
532 nlen = len - HAMMER_ENTRY_NAME_OFF;
533 if ((int)nlen < 0) /* illegal length */
534 break;
535 if (ondisk->entry.obj_id == 0 ||
536 ondisk->entry.obj_id == HAMMER_OBJID_ROOT)
537 break;
538 name = malloc(nlen + 1);
539 bcopy(ondisk->entry.name, name, nlen);
540 name[nlen] = 0;
541 sanitize_string(name);
543 if (VerboseOpt) {
544 printf("dir %016jx:%05d entry %016jx \"%s\"\n",
545 (uintmax_t)leaf->base.obj_id,
546 pfs_id,
547 (uintmax_t)ondisk->entry.obj_id,
548 name);
552 * We can't deal with hardlinks so if the object already
553 * has a name assigned to it we just keep using that name.
555 dict2 = get_dict(ondisk->entry.obj_id, pfs_id);
556 path1 = recover_path(dict2);
558 if (dict2->name == NULL)
559 dict2->name = name;
560 else
561 free(name);
564 * Attach dict2 to its directory (dict), create the
565 * directory (dict) if necessary. We must ensure
566 * that the directory entry exists in order to be
567 * able to properly rename() the file without creating
568 * a namespace conflict.
570 if ((dict2->flags & DICTF_PARENT) == 0) {
571 dict2->flags |= DICTF_PARENT;
572 dict2->parent = dict;
573 if ((dict->flags & DICTF_MADEDIR) == 0) {
574 dict->flags |= DICTF_MADEDIR;
575 path2 = recover_path(dict);
576 printf("mkdir %s\n", path2);
577 mkdir(path2, 0777);
578 free(path2);
579 path2 = NULL;
582 path2 = recover_path(dict2);
583 if (strcmp(path1, path2) != 0 && lstat(path1, &st) == 0) {
584 printf("Rename %s -> %s\n", path1, path2);
585 rename(path1, path2);
587 free(path1);
588 free(path2);
589 break;
590 default:
592 * Ignore any other record types
594 break;
596 done:
597 rel_buffer(data_buffer);
600 #define RD_HSIZE 32768
601 #define RD_HMASK (RD_HSIZE - 1)
603 struct recover_dict *RDHash[RD_HSIZE];
605 static
606 struct recover_dict *
607 get_dict(int64_t obj_id, uint16_t pfs_id)
609 struct recover_dict *dict;
610 int i;
612 if (obj_id == 0)
613 return(NULL);
615 i = crc32(&obj_id, sizeof(obj_id)) & RD_HMASK;
616 for (dict = RDHash[i]; dict; dict = dict->next) {
617 if (dict->obj_id == obj_id &&
618 dict->pfs_id == pfs_id) {
619 break;
622 if (dict == NULL) {
623 dict = malloc(sizeof(*dict));
624 bzero(dict, sizeof(*dict));
625 dict->obj_id = obj_id;
626 dict->pfs_id = pfs_id;
627 dict->next = RDHash[i];
628 dict->size = -1;
629 RDHash[i] = dict;
632 * Always connect dangling dictionary entries to object 1
633 * (the root of the PFS).
635 * DICTF_PARENT will not be set until we know what the
636 * real parent directory object is.
638 if (dict->obj_id != HAMMER_OBJID_ROOT)
639 dict->parent = get_dict(HAMMER_OBJID_ROOT, pfs_id);
641 return(dict);
644 struct path_info {
645 enum { PI_FIGURE, PI_LOAD } state;
646 uint16_t pfs_id;
647 char *base;
648 char *next;
649 int len;
652 static void recover_path_helper(struct recover_dict *, struct path_info *);
654 static
655 char *
656 recover_path(struct recover_dict *dict)
658 struct path_info info;
660 /* Find info.len first */
661 bzero(&info, sizeof(info));
662 info.state = PI_FIGURE;
663 recover_path_helper(dict, &info);
665 /* Fill in the path */
666 info.pfs_id = dict->pfs_id;
667 info.base = malloc(info.len);
668 info.next = info.base;
669 info.state = PI_LOAD;
670 recover_path_helper(dict, &info);
672 /* Return the path */
673 return(info.base);
676 #define STRLEN_OBJID 22 /* "obj_0x%016jx" */
677 #define STRLEN_PFSID 8 /* "PFS%05d" */
679 static
680 void
681 recover_path_helper(struct recover_dict *dict, struct path_info *info)
684 * Calculate path element length
686 dict->flags |= DICTF_TRAVERSED;
688 switch(info->state) {
689 case PI_FIGURE:
690 if (dict->obj_id == HAMMER_OBJID_ROOT)
691 info->len += STRLEN_PFSID;
692 else if (dict->name)
693 info->len += strlen(dict->name);
694 else
695 info->len += STRLEN_OBJID;
696 ++info->len;
698 if (dict->parent &&
699 (dict->parent->flags & DICTF_TRAVERSED) == 0) {
700 recover_path_helper(dict->parent, info);
701 } else {
702 info->len += strlen(TargetDir) + 1;
704 break;
705 case PI_LOAD:
706 if (dict->parent &&
707 (dict->parent->flags & DICTF_TRAVERSED) == 0) {
708 recover_path_helper(dict->parent, info);
709 } else {
710 strcpy(info->next, TargetDir);
711 info->next += strlen(info->next);
714 *info->next++ = '/';
715 if (dict->obj_id == HAMMER_OBJID_ROOT) {
716 snprintf(info->next, STRLEN_PFSID + 1,
717 "PFS%05d", info->pfs_id);
718 } else if (dict->name) {
719 strcpy(info->next, dict->name);
720 } else {
721 snprintf(info->next, STRLEN_OBJID + 1,
722 "obj_0x%016jx", (uintmax_t)dict->obj_id);
724 info->next += strlen(info->next);
725 break;
727 dict->flags &= ~DICTF_TRAVERSED;
730 static
731 void
732 sanitize_string(char *str)
734 while (*str) {
735 if (!isprint(*str))
736 *str = 'x';
737 ++str;
741 static
742 hammer_off_t
743 scan_raw_limit(void)
745 struct volume_info *vol;
746 hammer_blockmap_t rootmap;
747 hammer_blockmap_layer1_t layer1;
748 hammer_blockmap_layer2_t layer2;
749 struct buffer_info *buffer1 = NULL;
750 struct buffer_info *buffer2 = NULL;
751 hammer_off_t layer1_offset;
752 hammer_off_t layer2_offset;
753 hammer_off_t phys_offset;
754 hammer_off_t block_offset;
755 hammer_off_t offset = 0;
756 int zone = HAMMER_ZONE_FREEMAP_INDEX;
758 vol = get_root_volume();
759 rootmap = &vol->ondisk->vol0_blockmap[zone];
760 assert(rootmap->phys_offset != 0);
762 for (phys_offset = HAMMER_ZONE_ENCODE(zone, 0);
763 phys_offset < HAMMER_ZONE_ENCODE(zone, HAMMER_OFF_LONG_MASK);
764 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
766 * Dive layer 1.
768 layer1_offset = rootmap->phys_offset +
769 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
770 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
772 if (!hammer_crc_test_layer1(layer1)) {
773 offset = 0; /* failed */
774 goto end;
776 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL)
777 continue;
779 for (block_offset = 0;
780 block_offset < HAMMER_BLOCKMAP_LAYER2;
781 block_offset += HAMMER_BIGBLOCK_SIZE) {
783 * Dive layer 2, each entry represents a big-block.
785 layer2_offset = layer1->phys_offset +
786 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
787 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
789 if (!hammer_crc_test_layer2(layer2)) {
790 offset = 0; /* failed */
791 goto end;
793 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
794 break;
795 } else if (layer2->zone && layer2->zone != zone) {
796 offset = phys_offset + block_offset;
800 end:
801 rel_buffer(buffer1);
802 rel_buffer(buffer2);
804 return(hammer_xlate_to_zone2(offset));
807 static
808 void
809 scan_bigblocks(int target_zone)
811 struct volume_info *vol;
812 hammer_blockmap_t rootmap;
813 hammer_blockmap_layer1_t layer1;
814 hammer_blockmap_layer2_t layer2;
815 struct buffer_info *buffer1 = NULL;
816 struct buffer_info *buffer2 = NULL;
817 hammer_off_t layer1_offset;
818 hammer_off_t layer2_offset;
819 hammer_off_t phys_offset;
820 hammer_off_t block_offset;
821 hammer_off_t offset = 0;
822 int zone = HAMMER_ZONE_FREEMAP_INDEX;
824 vol = get_root_volume();
825 rootmap = &vol->ondisk->vol0_blockmap[zone];
826 assert(rootmap->phys_offset != 0);
828 for (phys_offset = HAMMER_ZONE_ENCODE(zone, 0);
829 phys_offset < HAMMER_ZONE_ENCODE(zone, HAMMER_OFF_LONG_MASK);
830 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
832 * Dive layer 1.
834 layer1_offset = rootmap->phys_offset +
835 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
836 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
839 if (!hammer_crc_test_layer1(layer1)) {
842 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL)
843 continue;
845 for (block_offset = 0;
846 block_offset < HAMMER_BLOCKMAP_LAYER2;
847 block_offset += HAMMER_BIGBLOCK_SIZE) {
848 offset = phys_offset + block_offset;
850 * Dive layer 2, each entry represents a big-block.
852 layer2_offset = layer1->phys_offset +
853 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
854 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
857 if (!hammer_crc_test_layer2(layer2)) {
860 if (layer2->zone == target_zone) {
861 add_bigblock_entry(offset, layer1, layer2);
862 } else if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
863 break;
867 rel_buffer(buffer1);
868 rel_buffer(buffer2);
871 static
872 void
873 free_bigblocks(void)
875 bigblock_t b;
877 while ((b = RB_ROOT(&ZoneTree)) != NULL) {
878 RB_REMOVE(bigblock_rb_tree, &ZoneTree, b);
879 free(b);
881 assert(RB_EMPTY(&ZoneTree));
884 static
885 void
886 add_bigblock_entry(hammer_off_t offset,
887 hammer_blockmap_layer1_t layer1, hammer_blockmap_layer2_t layer2)
889 bigblock_t b;
891 b = calloc(sizeof(*b), 1);
892 b->phys_offset = hammer_xlate_to_zone2(offset);
893 assert((b->phys_offset & HAMMER_BIGBLOCK_MASK64) == 0);
894 bcopy(layer1, &b->layer1, sizeof(*layer1));
895 bcopy(layer2, &b->layer2, sizeof(*layer2));
897 RB_INSERT(bigblock_rb_tree, &ZoneTree, b);
900 static
901 bigblock_t
902 get_bigblock_entry(hammer_off_t offset)
904 bigblock_t b;
906 offset = hammer_xlate_to_zone2(offset);
907 offset &= ~HAMMER_BIGBLOCK_MASK64;
909 b = RB_LOOKUP(bigblock_rb_tree, &ZoneTree, offset);
910 if (b)
911 return(b);
912 return(NULL);