xref: /dflybsd-src/sbin/hammer/cmd_recover.c (revision 35ca622fb0297a1330ee59317515a6f113512ca0)
1 /*
2  * Copyright (c) 2010 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  */
34 
35 #include "hammer.h"
36 
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;
46 };
47 
48 #define DICTF_MADEDIR	0x01
49 #define DICTF_MADEFILE	0x02
50 #define DICTF_PARENT	0x04	/* parent attached for real */
51 #define DICTF_TRAVERSED	0x80
52 
53 typedef struct bigblock *bigblock_t;
54 
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);
66 
67 static const char *TargetDir;
68 static int CachedFd = -1;
69 static char *CachedPath;
70 
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;
77 
78 static int
79 bigblock_cmp(bigblock_t b1, bigblock_t b2)
80 {
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);
86 }
87 
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);
92 
93 /*
94  * There was a hidden bug here while iterating zone-2 offset as
95  * shown in an example below.
96  *
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.
102  *
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.
107  *
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.
111  *
112  * |-----vol0-----|-----vol1-----|-----vol2-----| old filesystem
113  * <-----------------------> used by old filesystem
114  *
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
119  */
120 
121 void
122 hammer_cmd_recover(char **av, int ac)
123 {
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;
137 
138 	if (ac < 1) {
139 		errx(1, "hammer recover <target_dir> [full|quick]");
140 		/* not reached */
141 	}
142 
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;
149 	}
150 	assert(!full || !quick);
151 
152 	if (mkdir(TargetDir, 0777) == -1) {
153 		if (errno != EEXIST) {
154 			err(1, "mkdir");
155 			/* not reached */
156 		}
157 	}
158 
159 	printf("Running %sraw scan of HAMMER image, recovering to %s\n",
160 		full ? "full " : quick ? "quick " : "",
161 		TargetDir);
162 
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));
169 		}
170 	}
171 
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);
178 
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));
182 		}
183 	}
184 
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");
196 	}
197 
198 	data_buffer = NULL;
199 	for (i = 0; i < HAMMER_MAX_VOLUMES; i++) {
200 		volume = get_volume(i);
201 		if (volume == NULL)
202 			continue;
203 
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);
208 
209 		while (off < off_end) {
210 			off_blk = off & HAMMER_BIGBLOCK_MASK64;
211 			if (off_blk == 0)
212 				b = get_bigblock_entry(off);
213 
214 			if (raw_limit) {
215 				if (off >= raw_limit) {
216 					printf("Done %016jx\n", (uintmax_t)off);
217 					goto end;
218 				}
219 			}
220 			if (zone_limit) {
221 				if (off >= zone_limit) {
222 					printf("Done %016jx\n", (uintmax_t)off);
223 					goto end;
224 				}
225 				if (b == NULL) {
226 					off = HAMMER_ZONE_LAYER2_NEXT_OFFSET(off);
227 					continue;
228 				}
229 			}
230 
231 			if (b) {
232 				if (hammer_crc_test_layer1(HammerVersion,
233 							   &b->layer1) &&
234 				    hammer_crc_test_layer2(HammerVersion,
235 							   &b->layer2) &&
236 				    off_blk >= b->layer2.append_off) {
237 					off = HAMMER_ZONE_LAYER2_NEXT_OFFSET(off);
238 					continue;
239 				}
240 			}
241 
242 			ptr = get_buffer_data(off, &data_buffer, 0);
243 			if (ptr)
244 				recover_top(ptr, off);
245 			off += HAMMER_BUFSIZE;
246 		}
247 	}
248 end:
249 	rel_buffer(data_buffer);
250 	free_bigblocks();
251 
252 	if (CachedPath) {
253 		free(CachedPath);
254 		close(CachedFd);
255 		CachedPath = NULL;
256 		CachedFd = -1;
257 	}
258 }
259 
260 static __inline
261 void
262 print_node(hammer_node_ondisk_t node, hammer_off_t offset)
263 {
264 	char buf[HAMMER_BTREE_LEAF_ELMS + 1];
265 	int maxcount = hammer_node_max_elements(node->type);
266 	int i;
267 
268 	for (i = 0; i < node->count && i < maxcount; ++i)
269 		buf[i] = hammer_elm_btype(&node->elms[i]);
270 	buf[i] = '\0';
271 
272 	printf("%016jx %c %d %s\n", offset, node->type, node->count, buf);
273 }
274 
275 /*
276  * Top level recovery processor.  Assume the data is a B-Tree node.
277  * If the CRC is good we attempt to process the node, building the
278  * object space and creating the dictionary as we go.
279  */
280 static void
281 recover_top(char *ptr, hammer_off_t offset)
282 {
283 	hammer_node_ondisk_t node;
284 	hammer_btree_elm_t elm;
285 	int maxcount;
286 	int i;
287 	int isnode;
288 
289 	for (node = (void *)ptr; (char *)node < ptr + HAMMER_BUFSIZE; ++node) {
290 		isnode = hammer_crc_test_btree(HammerVersion, node);
291 		maxcount = hammer_node_max_elements(node->type);
292 
293 		if (DebugOpt) {
294 			if (isnode)
295 				print_node(node, offset);
296 			else if (DebugOpt > 1)
297 				printf("%016jx -\n", offset);
298 		}
299 		offset += sizeof(*node);
300 
301 		if (isnode && node->type == HAMMER_BTREE_TYPE_LEAF) {
302 			for (i = 0; i < node->count && i < maxcount; ++i) {
303 				elm = &node->elms[i];
304 				if (elm->base.btype == HAMMER_BTREE_TYPE_RECORD)
305 					recover_elm(&elm->leaf);
306 			}
307 		}
308 	}
309 }
310 
311 static void
312 recover_elm(hammer_btree_leaf_elm_t leaf)
313 {
314 	struct buffer_info *data_buffer = NULL;
315 	struct recover_dict *dict;
316 	struct recover_dict *dict2;
317 	hammer_data_ondisk_t ondisk;
318 	hammer_off_t data_offset;
319 	struct stat st;
320 	int chunk;
321 	int len;
322 	int zfill;
323 	int64_t file_offset;
324 	uint16_t pfs_id;
325 	size_t nlen;
326 	int fd;
327 	char *name;
328 	char *path1;
329 	char *path2;
330 
331 	/*
332 	 * Ignore deleted records
333 	 */
334 	if (leaf->delete_ts)
335 		return;
336 
337 	/*
338 	 * If we're running full scan, it's possible that data_offset
339 	 * refers to old filesystem data that we can't physically access.
340 	 */
341 	data_offset = leaf->data_offset;
342 	if (get_volume(HAMMER_VOL_DECODE(data_offset)) == NULL)
343 		return;
344 
345 	if (data_offset != 0)
346 		ondisk = get_buffer_data(data_offset, &data_buffer, 0);
347 	else
348 		ondisk = NULL;
349 	if (ondisk == NULL)
350 		goto done;
351 
352 	len = leaf->data_len;
353 	chunk = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
354 	if (chunk > len)
355 		chunk = len;
356 
357 	if (len < 0 || len > HAMMER_XBUFSIZE || len > chunk)
358 		goto done;
359 
360 	pfs_id = lo_to_pfs(leaf->base.localization);
361 
362 	/*
363 	 * Note that meaning of leaf->base.obj_id differs depending
364 	 * on record type.  For a direntry, leaf->base.obj_id points
365 	 * to its parent inode that this entry is a part of, but not
366 	 * its corresponding inode.
367 	 */
368 	dict = get_dict(leaf->base.obj_id, pfs_id);
369 
370 	switch(leaf->base.rec_type) {
371 	case HAMMER_RECTYPE_INODE:
372 		/*
373 		 * We found an inode which also tells us where the file
374 		 * or directory is in the directory hierarchy.
375 		 */
376 		if (VerboseOpt) {
377 			printf("inode %016jx:%05d found\n",
378 				(uintmax_t)leaf->base.obj_id, pfs_id);
379 		}
380 		path1 = recover_path(dict);
381 
382 		/*
383 		 * Attach the inode to its parent.  This isn't strictly
384 		 * necessary because the information is also in the
385 		 * directory entries, but if we do not find the directory
386 		 * entry this ensures that the files will still be
387 		 * reasonably well organized in their proper directories.
388 		 */
389 		if ((dict->flags & DICTF_PARENT) == 0 &&
390 		    dict->obj_id != HAMMER_OBJID_ROOT &&
391 		    ondisk->inode.parent_obj_id != 0) {
392 			dict->flags |= DICTF_PARENT;
393 			dict->parent = get_dict(ondisk->inode.parent_obj_id,
394 						pfs_id);
395 			if (dict->parent &&
396 			    (dict->parent->flags & DICTF_MADEDIR) == 0) {
397 				dict->parent->flags |= DICTF_MADEDIR;
398 				path2 = recover_path(dict->parent);
399 				printf("mkdir %s\n", path2);
400 				mkdir(path2, 0777);
401 				free(path2);
402 				path2 = NULL;
403 			}
404 		}
405 		if (dict->obj_type == 0)
406 			dict->obj_type = ondisk->inode.obj_type;
407 		dict->size = ondisk->inode.size;
408 		path2 = recover_path(dict);
409 
410 		if (lstat(path1, &st) == 0) {
411 			if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
412 				truncate(path1, dict->size);
413 				/* chmod(path1, 0666); */
414 			}
415 			if (strcmp(path1, path2)) {
416 				printf("Rename (inode) %s -> %s\n", path1, path2);
417 				rename(path1, path2);
418 			}
419 		} else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
420 			printf("mkinode (file) %s\n", path2);
421 			fd = open(path2, O_RDWR|O_CREAT, 0666);
422 			if (fd > 0)
423 				close(fd);
424 		} else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
425 			printf("mkinode (dir) %s\n", path2);
426 			mkdir(path2, 0777);
427 			dict->flags |= DICTF_MADEDIR;
428 		}
429 		free(path1);
430 		free(path2);
431 		break;
432 	case HAMMER_RECTYPE_DATA:
433 		/*
434 		 * File record data
435 		 */
436 		if (leaf->base.obj_id == 0)
437 			break;
438 		if (VerboseOpt) {
439 			printf("inode %016jx:%05d data %016jx,%d\n",
440 				(uintmax_t)leaf->base.obj_id,
441 				pfs_id,
442 				(uintmax_t)leaf->base.key - len,
443 				len);
444 		}
445 
446 		/*
447 		 * Update the dictionary entry
448 		 */
449 		if (dict->obj_type == 0)
450 			dict->obj_type = HAMMER_OBJTYPE_REGFILE;
451 
452 		/*
453 		 * If the parent directory has not been created we
454 		 * have to create it (typically a PFS%05d)
455 		 */
456 		if (dict->parent &&
457 		    (dict->parent->flags & DICTF_MADEDIR) == 0) {
458 			dict->parent->flags |= DICTF_MADEDIR;
459 			path2 = recover_path(dict->parent);
460 			printf("mkdir %s\n", path2);
461 			mkdir(path2, 0777);
462 			free(path2);
463 			path2 = NULL;
464 		}
465 
466 		/*
467 		 * Create the file if necessary, report file creations
468 		 */
469 		path1 = recover_path(dict);
470 		if (CachedPath && strcmp(CachedPath, path1) == 0)
471 			fd = CachedFd;
472 		else
473 			fd = open(path1, O_CREAT|O_RDWR, 0666);
474 		if (fd < 0) {
475 			printf("Unable to create %s: %s\n",
476 				path1, strerror(errno));
477 			free(path1);
478 			break;
479 		}
480 		if ((dict->flags & DICTF_MADEFILE) == 0) {
481 			dict->flags |= DICTF_MADEFILE;
482 			printf("mkfile %s\n", path1);
483 		}
484 
485 		/*
486 		 * And write the record.  A HAMMER data block is aligned
487 		 * and may contain trailing zeros after the file EOF.  The
488 		 * inode record is required to get the actual file size.
489 		 *
490 		 * However, when the inode record is not available
491 		 * we can do a sparse write and that will get it right
492 		 * most of the time even if the inode record is never
493 		 * found.
494 		 */
495 		file_offset = (int64_t)leaf->base.key - len;
496 		lseek(fd, (off_t)file_offset, SEEK_SET);
497 		while (len) {
498 			if (dict->size == -1) {
499 				for (zfill = chunk - 1; zfill >= 0; --zfill) {
500 					if (((char *)ondisk)[zfill])
501 						break;
502 				}
503 				++zfill;
504 			} else {
505 				zfill = chunk;
506 			}
507 
508 			if (zfill)
509 				write(fd, ondisk, zfill);
510 			if (zfill < chunk)
511 				lseek(fd, chunk - zfill, SEEK_CUR);
512 
513 			len -= chunk;
514 			data_offset += chunk;
515 			file_offset += chunk;
516 			ondisk = get_buffer_data(data_offset, &data_buffer, 0);
517 			if (ondisk == NULL)
518 				break;
519 			chunk = HAMMER_BUFSIZE -
520 				((int)data_offset & HAMMER_BUFMASK);
521 			if (chunk > len)
522 				chunk = len;
523 		}
524 		if (dict->size >= 0 && file_offset > dict->size) {
525 			ftruncate(fd, dict->size);
526 			/* fchmod(fd, 0666); */
527 		}
528 
529 		if (fd == CachedFd) {
530 			free(path1);
531 		} else if (CachedPath) {
532 			free(CachedPath);
533 			close(CachedFd);
534 			CachedPath = path1;
535 			CachedFd = fd;
536 		} else {
537 			CachedPath = path1;
538 			CachedFd = fd;
539 		}
540 		break;
541 	case HAMMER_RECTYPE_DIRENTRY:
542 		nlen = len - HAMMER_ENTRY_NAME_OFF;
543 		if ((int)nlen < 0)	/* illegal length */
544 			break;
545 		if (ondisk->entry.obj_id == 0 ||
546 		    ondisk->entry.obj_id == HAMMER_OBJID_ROOT)
547 			break;
548 		name = malloc(nlen + 1);
549 		bcopy(ondisk->entry.name, name, nlen);
550 		name[nlen] = 0;
551 		sanitize_string(name);
552 
553 		if (VerboseOpt) {
554 			printf("dir %016jx:%05d entry %016jx \"%s\"\n",
555 				(uintmax_t)leaf->base.obj_id,
556 				pfs_id,
557 				(uintmax_t)ondisk->entry.obj_id,
558 				name);
559 		}
560 
561 		/*
562 		 * We can't deal with hardlinks so if the object already
563 		 * has a name assigned to it we just keep using that name.
564 		 */
565 		dict2 = get_dict(ondisk->entry.obj_id, pfs_id);
566 		path1 = recover_path(dict2);
567 
568 		if (dict2->name == NULL)
569 			dict2->name = name;
570 		else
571 			free(name);
572 
573 		/*
574 		 * Attach dict2 to its directory (dict), create the
575 		 * directory (dict) if necessary.  We must ensure
576 		 * that the directory entry exists in order to be
577 		 * able to properly rename() the file without creating
578 		 * a namespace conflict.
579 		 */
580 		if ((dict2->flags & DICTF_PARENT) == 0) {
581 			dict2->flags |= DICTF_PARENT;
582 			dict2->parent = dict;
583 			if ((dict->flags & DICTF_MADEDIR) == 0) {
584 				dict->flags |= DICTF_MADEDIR;
585 				path2 = recover_path(dict);
586 				printf("mkdir %s\n", path2);
587 				mkdir(path2, 0777);
588 				free(path2);
589 				path2 = NULL;
590 			}
591 		}
592 		path2 = recover_path(dict2);
593 		if (strcmp(path1, path2) != 0 && lstat(path1, &st) == 0) {
594 			printf("Rename (entry) %s -> %s\n", path1, path2);
595 			rename(path1, path2);
596 		}
597 		free(path1);
598 		free(path2);
599 		break;
600 	default:
601 		/*
602 		 * Ignore any other record types
603 		 */
604 		break;
605 	}
606 done:
607 	rel_buffer(data_buffer);
608 }
609 
610 #define RD_HSIZE	32768
611 #define RD_HMASK	(RD_HSIZE - 1)
612 
613 struct recover_dict *RDHash[RD_HSIZE];
614 
615 static
616 struct recover_dict *
617 get_dict(int64_t obj_id, uint16_t pfs_id)
618 {
619 	struct recover_dict *dict;
620 	int i;
621 
622 	if (obj_id == 0)
623 		return(NULL);
624 
625 	i = crc32(&obj_id, sizeof(obj_id)) & RD_HMASK;
626 	for (dict = RDHash[i]; dict; dict = dict->next) {
627 		if (dict->obj_id == obj_id && dict->pfs_id == pfs_id)
628 			break;
629 	}
630 
631 	if (dict == NULL) {
632 		dict = malloc(sizeof(*dict));
633 		bzero(dict, sizeof(*dict));
634 		dict->obj_id = obj_id;
635 		dict->pfs_id = pfs_id;
636 		dict->next = RDHash[i];
637 		dict->size = -1;
638 		RDHash[i] = dict;
639 
640 		/*
641 		 * Always connect dangling dictionary entries to object 1
642 		 * (the root of the PFS).
643 		 *
644 		 * DICTF_PARENT will not be set until we know what the
645 		 * real parent directory object is.
646 		 */
647 		if (dict->obj_id != HAMMER_OBJID_ROOT)
648 			dict->parent = get_dict(HAMMER_OBJID_ROOT, pfs_id);
649 	}
650 	return(dict);
651 }
652 
653 struct path_info {
654 	enum { PI_FIGURE, PI_LOAD } state;
655 	uint16_t pfs_id;
656 	char *base;
657 	char *next;
658 	int len;
659 };
660 
661 static void recover_path_helper(struct recover_dict *, struct path_info *);
662 
663 static
664 char *
665 recover_path(struct recover_dict *dict)
666 {
667 	struct path_info info;
668 
669 	/* Find info.len first */
670 	bzero(&info, sizeof(info));
671 	info.state = PI_FIGURE;
672 	recover_path_helper(dict, &info);
673 
674 	/* Fill in the path */
675 	info.pfs_id = dict->pfs_id;
676 	info.base = malloc(info.len);
677 	info.next = info.base;
678 	info.state = PI_LOAD;
679 	recover_path_helper(dict, &info);
680 
681 	/* Return the path */
682 	return(info.base);
683 }
684 
685 #define STRLEN_OBJID	22	/* "obj_0x%016jx" */
686 #define STRLEN_PFSID	8	/* "PFS%05d" */
687 
688 static
689 void
690 recover_path_helper(struct recover_dict *dict, struct path_info *info)
691 {
692 	/*
693 	 * Calculate path element length
694 	 */
695 	dict->flags |= DICTF_TRAVERSED;
696 
697 	switch(info->state) {
698 	case PI_FIGURE:
699 		if (dict->obj_id == HAMMER_OBJID_ROOT)
700 			info->len += STRLEN_PFSID;
701 		else if (dict->name)
702 			info->len += strlen(dict->name);
703 		else
704 			info->len += STRLEN_OBJID;
705 		++info->len;
706 
707 		if (dict->parent &&
708 		    (dict->parent->flags & DICTF_TRAVERSED) == 0) {
709 			recover_path_helper(dict->parent, info);
710 		} else {
711 			info->len += strlen(TargetDir) + 1;
712 		}
713 		break;
714 	case PI_LOAD:
715 		if (dict->parent &&
716 		    (dict->parent->flags & DICTF_TRAVERSED) == 0) {
717 			recover_path_helper(dict->parent, info);
718 		} else {
719 			strcpy(info->next, TargetDir);
720 			info->next += strlen(info->next);
721 		}
722 
723 		*info->next++ = '/';
724 		if (dict->obj_id == HAMMER_OBJID_ROOT) {
725 			snprintf(info->next, STRLEN_PFSID + 1,
726 				"PFS%05d", info->pfs_id);
727 		} else if (dict->name) {
728 			strcpy(info->next, dict->name);
729 		} else {
730 			snprintf(info->next, STRLEN_OBJID + 1,
731 				"obj_0x%016jx", (uintmax_t)dict->obj_id);
732 		}
733 		info->next += strlen(info->next);
734 		break;
735 	}
736 	dict->flags &= ~DICTF_TRAVERSED;
737 }
738 
739 static
740 void
741 sanitize_string(char *str)
742 {
743 	while (*str) {
744 		if (!isprint(*str))
745 			*str = 'x';
746 		++str;
747 	}
748 }
749 
750 static
751 hammer_off_t
752 scan_raw_limit(void)
753 {
754 	struct volume_info *volume;
755 	hammer_blockmap_t rootmap;
756 	hammer_blockmap_layer1_t layer1;
757 	hammer_blockmap_layer2_t layer2;
758 	struct buffer_info *buffer1 = NULL;
759 	struct buffer_info *buffer2 = NULL;
760 	hammer_off_t layer1_offset;
761 	hammer_off_t layer2_offset;
762 	hammer_off_t phys_offset;
763 	hammer_off_t block_offset;
764 	hammer_off_t offset = 0;
765 	int zone = HAMMER_ZONE_FREEMAP_INDEX;
766 
767 	volume = get_root_volume();
768 	rootmap = &volume->ondisk->vol0_blockmap[zone];
769 	assert(rootmap->phys_offset != 0);
770 
771 	for (phys_offset = HAMMER_ZONE_ENCODE(zone, 0);
772 	     phys_offset < HAMMER_ZONE_ENCODE(zone, HAMMER_OFF_LONG_MASK);
773 	     phys_offset += HAMMER_BLOCKMAP_LAYER2) {
774 		/*
775 		 * Dive layer 1.
776 		 */
777 		layer1_offset = rootmap->phys_offset +
778 				HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
779 		layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
780 
781 		if (!hammer_crc_test_layer1(HammerVersion, layer1)) {
782 			offset = 0; /* failed */
783 			goto end;
784 		}
785 		if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL)
786 			continue;
787 
788 		for (block_offset = 0;
789 		     block_offset < HAMMER_BLOCKMAP_LAYER2;
790 		     block_offset += HAMMER_BIGBLOCK_SIZE) {
791 			/*
792 			 * Dive layer 2, each entry represents a big-block.
793 			 */
794 			layer2_offset = layer1->phys_offset +
795 					HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
796 			layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
797 
798 			if (!hammer_crc_test_layer2(HammerVersion, layer2)) {
799 				offset = 0; /* failed */
800 				goto end;
801 			}
802 			if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
803 				break;
804 			} else if (layer2->zone && layer2->zone != zone) {
805 				offset = phys_offset + block_offset;
806 			}
807 		}
808 	}
809 end:
810 	rel_buffer(buffer1);
811 	rel_buffer(buffer2);
812 
813 	return(hammer_xlate_to_zone2(offset));
814 }
815 
816 static
817 void
818 scan_bigblocks(int target_zone)
819 {
820 	struct volume_info *volume;
821 	hammer_blockmap_t rootmap;
822 	hammer_blockmap_layer1_t layer1;
823 	hammer_blockmap_layer2_t layer2;
824 	struct buffer_info *buffer1 = NULL;
825 	struct buffer_info *buffer2 = NULL;
826 	hammer_off_t layer1_offset;
827 	hammer_off_t layer2_offset;
828 	hammer_off_t phys_offset;
829 	hammer_off_t block_offset;
830 	hammer_off_t offset = 0;
831 	int zone = HAMMER_ZONE_FREEMAP_INDEX;
832 
833 	volume = get_root_volume();
834 	rootmap = &volume->ondisk->vol0_blockmap[zone];
835 	assert(rootmap->phys_offset != 0);
836 
837 	for (phys_offset = HAMMER_ZONE_ENCODE(zone, 0);
838 	     phys_offset < HAMMER_ZONE_ENCODE(zone, HAMMER_OFF_LONG_MASK);
839 	     phys_offset += HAMMER_BLOCKMAP_LAYER2) {
840 		/*
841 		 * Dive layer 1.
842 		 */
843 		layer1_offset = rootmap->phys_offset +
844 				HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
845 		layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
846 
847 		/*
848 		if (!hammer_crc_test_layer1(HammerVersion, layer1)) {
849 		}
850 		*/
851 		if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL)
852 			continue;
853 
854 		for (block_offset = 0;
855 		     block_offset < HAMMER_BLOCKMAP_LAYER2;
856 		     block_offset += HAMMER_BIGBLOCK_SIZE) {
857 			offset = phys_offset + block_offset;
858 			/*
859 			 * Dive layer 2, each entry represents a big-block.
860 			 */
861 			layer2_offset = layer1->phys_offset +
862 					HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
863 			layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
864 
865 			/*
866 			if (!hammer_crc_test_layer2(HammerVersion, layer2)) {
867 			}
868 			*/
869 			if (layer2->zone == target_zone) {
870 				add_bigblock_entry(offset, layer1, layer2);
871 			} else if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
872 				break;
873 			}
874 		}
875 	}
876 	rel_buffer(buffer1);
877 	rel_buffer(buffer2);
878 }
879 
880 static
881 void
882 free_bigblocks(void)
883 {
884 	bigblock_t b;
885 
886 	while ((b = RB_ROOT(&ZoneTree)) != NULL) {
887 		RB_REMOVE(bigblock_rb_tree, &ZoneTree, b);
888 		free(b);
889 	}
890 	assert(RB_EMPTY(&ZoneTree));
891 }
892 
893 static
894 void
895 add_bigblock_entry(hammer_off_t offset,
896 	hammer_blockmap_layer1_t layer1, hammer_blockmap_layer2_t layer2)
897 {
898 	bigblock_t b;
899 
900 	b = calloc(1, sizeof(*b));
901 	b->phys_offset = hammer_xlate_to_zone2(offset);
902 	assert((b->phys_offset & HAMMER_BIGBLOCK_MASK64) == 0);
903 	bcopy(layer1, &b->layer1, sizeof(*layer1));
904 	bcopy(layer2, &b->layer2, sizeof(*layer2));
905 
906 	RB_INSERT(bigblock_rb_tree, &ZoneTree, b);
907 }
908 
909 static
910 bigblock_t
911 get_bigblock_entry(hammer_off_t offset)
912 {
913 	bigblock_t b;
914 
915 	offset = hammer_xlate_to_zone2(offset);
916 	offset &= ~HAMMER_BIGBLOCK_MASK64;
917 
918 	b = RB_LOOKUP(bigblock_rb_tree, &ZoneTree, offset);
919 	if (b)
920 		return(b);
921 	return(NULL);
922 }
923