xref: /dragonfly/sbin/hammer/cmd_show.c (revision f2a91d31)
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  */
34 
35 #include <libutil.h>
36 
37 #include "hammer.h"
38 
39 #define FLAG_TOOFARLEFT		0x0001
40 #define FLAG_TOOFARRIGHT	0x0002
41 #define FLAG_BADTYPE		0x0004
42 #define FLAG_BADCHILDPARENT	0x0008
43 #define FLAG_BADMIRRORTID	0x0010
44 
45 struct {
46 	struct hammer_base_elm base;
47 	int limit;	/* # of fields to test */
48 	int filter;	/* filter type (default -1) */
49 	int obfuscate;	/* obfuscate direntry name */
50 	int indent;	/* use depth indentation */
51 	struct zone_stat *stats;
52 } opt;
53 
54 static __inline void print_btree(hammer_off_t node_offset);
55 static __inline void print_subtree(hammer_btree_elm_t elm);
56 static void print_btree_node(hammer_off_t node_offset,
57 			hammer_tid_t mirror_tid,
58 			hammer_base_elm_t left_bound,
59 			hammer_base_elm_t right_bound);
60 static int test_node_count(hammer_node_ondisk_t node, char *badmp);
61 static void print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
62 			hammer_btree_elm_t elm,
63 			hammer_base_elm_t left_bound,
64 			hammer_base_elm_t right_bound,
65 			const char *ext);
66 static int get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
67 			hammer_btree_elm_t elm,
68 			hammer_base_elm_t left_bound,
69 			hammer_base_elm_t right_bound);
70 static int test_lr(hammer_btree_elm_t elm,
71 			hammer_base_elm_t left_bound,
72 			hammer_base_elm_t right_bound);
73 static int test_rbn_lr(hammer_btree_elm_t elm,
74 			hammer_base_elm_t left_bound,
75 			hammer_base_elm_t right_bound);
76 static void print_bigblock_fill(hammer_off_t offset);
77 static const char *check_data_crc(hammer_btree_elm_t elm);
78 static uint32_t get_buf_crc(hammer_off_t buf_offset, int32_t buf_len);
79 static void print_record(hammer_btree_elm_t elm);
80 static int init_btree_search(const char *arg);
81 static int test_btree_search(hammer_btree_elm_t elm);
82 static int test_btree_match(hammer_btree_elm_t elm);
83 static int test_btree_out_of_range(hammer_btree_elm_t elm);
84 static void hexdump_record(const void *ptr, int length, const char *hdr);
85 
86 static int num_bad_node = 0;
87 static int num_bad_elm = 0;
88 static int num_bad_rec = 0;
89 static int depth;
90 
91 #define _X	"\t"
92 static const char* _indents[] = {
93 	"",
94 	_X,
95 	_X _X,
96 	_X _X _X,
97 	_X _X _X _X,
98 	_X _X _X _X _X,
99 	_X _X _X _X _X _X,
100 	_X _X _X _X _X _X _X,
101 	_X _X _X _X _X _X _X _X,
102 	_X _X _X _X _X _X _X _X _X,
103 	_X _X _X _X _X _X _X _X _X _X,
104 	_X _X _X _X _X _X _X _X _X _X _X,
105 	_X _X _X _X _X _X _X _X _X _X _X _X,
106 	_X _X _X _X _X _X _X _X _X _X _X _X _X,
107 	_X _X _X _X _X _X _X _X _X _X _X _X _X _X,
108 	_X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
109 	_X _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
110 	/* deep enough */
111 };
112 #define INDENT _indents[opt.indent ? depth : 0]
113 
114 void
115 hammer_cmd_show(const char *arg, int filter, int obfuscate, int indent)
116 {
117 	struct volume_info *volume;
118 	hammer_volume_ondisk_t ondisk;
119 	struct zone_stat *stats = NULL;
120 
121 	if (VerboseOpt)
122 		stats = hammer_init_zone_stat_bits();
123 
124 	volume = get_root_volume();
125 	ondisk = volume->ondisk;
126 
127 	print_blockmap(volume);
128 
129 	bzero(&opt, sizeof(opt));
130 	opt.filter = filter;
131 	opt.obfuscate = obfuscate;
132 	opt.indent = indent;
133 	opt.stats = stats;
134 
135 	if (init_btree_search(arg) > 0) {
136 		printf("arg=\"%s\"", arg);
137 		if (opt.limit > 0)
138 			printf(" lo=%08x", opt.base.localization);
139 		if (opt.limit > 1)
140 			printf(" objid=%016jx", (uintmax_t)opt.base.obj_id);
141 		if (opt.limit > 2)
142 			printf(" rt=%02x", opt.base.rec_type);
143 		if (opt.limit > 3)
144 			printf(" key=%016jx", (uintmax_t)opt.base.key);
145 		if (opt.limit > 4)
146 			printf(" tid=%016jx", (uintmax_t)opt.base.create_tid);
147 		printf("\n");
148 	}
149 	print_btree(ondisk->vol0_btree_root);
150 
151 	if (VerboseOpt) {
152 		hammer_print_zone_stat(stats);
153 		hammer_cleanup_zone_stat(stats);
154 	}
155 
156 	if (num_bad_node || VerboseOpt) {
157 		printf("%d bad nodes\n", num_bad_node);
158 	}
159 	if (num_bad_elm || VerboseOpt) {
160 		printf("%d bad elms\n", num_bad_elm);
161 	}
162 	if (num_bad_rec || VerboseOpt) {
163 		printf("%d bad records\n", num_bad_rec);
164 	}
165 }
166 
167 static __inline
168 void
169 print_btree(hammer_off_t node_offset)
170 {
171 	depth = -1;
172 	print_btree_node(node_offset, HAMMER_MAX_TID, NULL, NULL);
173 	assert(depth == -1);
174 }
175 
176 static __inline
177 void
178 print_subtree(hammer_btree_elm_t elm)
179 {
180 	print_btree_node(elm->internal.subtree_offset,
181 			 elm->internal.mirror_tid, &elm[0].base, &elm[1].base);
182 }
183 
184 static void
185 print_btree_node(hammer_off_t node_offset,
186 		 hammer_tid_t mirror_tid,
187 		 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
188 {
189 	struct buffer_info *buffer = NULL;
190 	hammer_node_ondisk_t node;
191 	hammer_btree_elm_t elm;
192 	int i;
193 	char badc = ' ';  /* good */
194 	char badm = ' ';  /* good */
195 	const char *ext;
196 
197 	depth++;
198 	node = get_node(node_offset, &buffer);
199 
200 	if (node == NULL) {
201 		badc = 'B';
202 		badm = 'I';
203 	} else {
204 		if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) != node->crc)
205 			badc = 'B';
206 		if (node->mirror_tid > mirror_tid) {
207 			badc = 'B';
208 			badm = 'M';
209 		}
210 		if (test_node_count(node, &badm) == -1) {
211 			badc = 'B';
212 			assert(badm != ' ');
213 		}
214 	}
215 
216 	if (badm != ' ' || badc != ' ')  /* not good */
217 		++num_bad_node;
218 
219 	printf("%s%c%c   NODE %016jx ",
220 	       INDENT, badc, badm, (uintmax_t)node_offset);
221 	printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx",
222 	       node->count,
223 	       (uintmax_t)node->parent,
224 	       (node->type ? node->type : '?'),
225 	       depth,
226 	       (uintmax_t)node->mirror_tid);
227 	printf(" fill=");
228 	print_bigblock_fill(node_offset);
229 	printf(" {\n");
230 
231 	if (VerboseOpt)
232 		hammer_add_zone_stat(opt.stats, node_offset, sizeof(*node));
233 
234 	for (i = 0; i < node->count; ++i) {
235 		elm = &node->elms[i];
236 		ext = NULL;
237 		if (opt.limit) {
238 			switch (node->type) {
239 			case HAMMER_BTREE_TYPE_INTERNAL:
240 				if (!test_btree_out_of_range(elm))
241 					ext = "*";
242 				break;
243 			case HAMMER_BTREE_TYPE_LEAF:
244 				if (test_btree_match(elm))
245 					ext = "*";
246 				break;
247 			}
248 		}
249 		print_btree_elm(node, node_offset,
250 				elm, left_bound, right_bound, ext);
251 	}
252 	if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
253 		assert(i == node->count);  /* boundary */
254 		elm = &node->elms[i];
255 		print_btree_elm(node, node_offset,
256 				elm, left_bound, right_bound, NULL);
257 	}
258 	printf("%s     }\n", INDENT);
259 
260 	if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
261 		for (i = 0; i < node->count; ++i) {
262 			elm = &node->elms[i];
263 			if (opt.limit && opt.filter) {
264 				if (test_btree_out_of_range(elm))
265 					continue;
266 			}
267 			if (elm->internal.subtree_offset) {
268 				print_subtree(elm);
269 				/*
270 				 * Cause show to do normal iteration after
271 				 * seeking to the lo:objid:rt:key:tid
272 				 * by default
273 				 */
274 				if (opt.limit && opt.filter == -1)  /* default */
275 					opt.filter = 0;
276 			}
277 		}
278 	}
279 	rel_buffer(buffer);
280 	depth--;
281 }
282 
283 static int
284 test_node_count(hammer_node_ondisk_t node, char *badmp)
285 {
286 	hammer_node_ondisk_t parent_node;
287 	struct buffer_info *buffer = NULL;
288 	int maxcount;
289 
290 	maxcount = hammer_node_max_elements(node->type);
291 
292 	if (maxcount == -1) {
293 		*badmp = 'U';
294 		return(-1);
295 	} else if (node->count > maxcount) {
296 		*badmp = 'C';
297 		return(-1);
298 	} else if (node->count == 0) {
299 		parent_node = get_node(node->parent, &buffer);
300 		if (parent_node->count != 1) {
301 			*badmp = 'C';
302 			rel_buffer(buffer);
303 			return(-1);
304 		}
305 		rel_buffer(buffer);
306 	}
307 
308 	return(0);
309 }
310 
311 static __inline
312 int
313 is_root_btree_beg(uint8_t type, int i, hammer_btree_elm_t elm)
314 {
315 	/*
316 	 * elm->base.btype depends on what the original node had
317 	 * so it could be anything but HAMMER_BTREE_TYPE_NONE.
318 	 */
319 	return (type == HAMMER_BTREE_TYPE_INTERNAL &&
320 		i == 0 &&
321 		elm->base.localization == 0 &&
322 		elm->base.obj_id == (int64_t)-0x8000000000000000LL &&
323 		elm->base.key == (int64_t)-0x8000000000000000LL &&
324 		elm->base.create_tid == 1 &&
325 		elm->base.delete_tid == 1 &&
326 		elm->base.rec_type == 0 &&
327 		elm->base.obj_type == 0 &&
328 		elm->base.btype != HAMMER_BTREE_TYPE_NONE);
329 }
330 
331 static __inline
332 int
333 is_root_btree_end(uint8_t type, int i, hammer_btree_elm_t elm)
334 {
335 	return (type == HAMMER_BTREE_TYPE_INTERNAL &&
336 		i != 0 &&
337 		elm->base.localization == 0xFFFFFFFFU &&
338 		elm->base.obj_id == 0x7FFFFFFFFFFFFFFFLL &&
339 		elm->base.key == 0x7FFFFFFFFFFFFFFFLL &&
340 		elm->base.create_tid == 0xFFFFFFFFFFFFFFFFULL &&
341 		elm->base.delete_tid == 0 &&
342 		elm->base.rec_type == 0xFFFFU &&
343 		elm->base.obj_type == 0 &&
344 		elm->base.btype == HAMMER_BTREE_TYPE_NONE);
345 }
346 
347 static
348 void
349 print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
350 		hammer_btree_elm_t elm,
351 		hammer_base_elm_t left_bound,
352 		hammer_base_elm_t right_bound,
353 		const char *ext)
354 {
355 	char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
356 	char deleted;
357 	char rootelm;
358 	const char *label;
359 	const char *p;
360 	int flags;
361 	int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
362 
363 	flags = get_elm_flags(node, node_offset, elm, left_bound, right_bound);
364 	flagstr[0] = flags ? 'B' : 'G';
365 	if (flags & FLAG_TOOFARLEFT)
366 		flagstr[2] = 'L';
367 	if (flags & FLAG_TOOFARRIGHT)
368 		flagstr[3] = 'R';
369 	if (flags & FLAG_BADTYPE)
370 		flagstr[4] = 'T';
371 	if (flags & FLAG_BADCHILDPARENT)
372 		flagstr[5] = 'C';
373 	if (flags & FLAG_BADMIRRORTID)
374 		flagstr[6] = 'M';
375 	if (flagstr[0] == 'B')
376 		++num_bad_elm;
377 
378 	/*
379 	 * Check if elm is derived from root split
380 	 */
381 	if (is_root_btree_beg(node->type, i, elm))
382 		rootelm = '>';
383 	else if (is_root_btree_end(node->type, i, elm))
384 		rootelm = '<';
385 	else
386 		rootelm = ' ';
387 
388 	if (elm->base.delete_tid)
389 		deleted = 'd';
390 	else
391 		deleted = ' ';
392 
393 	if (node->type == HAMMER_BTREE_TYPE_INTERNAL && node->count == i)
394 		label = "RBN";
395 	else
396 		label = "ELM";
397 
398 	printf("%s%s %s %2d %c ",
399 	       INDENT, flagstr, label, i, hammer_elm_btype(elm));
400 	printf("lo=%08x objid=%016jx rt=%02x key=%016jx tid=%016jx\n",
401 	       elm->base.localization,
402 	       (uintmax_t)elm->base.obj_id,
403 	       elm->base.rec_type,
404 	       (uintmax_t)elm->base.key,
405 	       (uintmax_t)elm->base.create_tid);
406 	printf("%s               %c del=%016jx ot=%02x",
407 	       INDENT,
408 	       (rootelm == ' ' ? deleted : rootelm),
409 	       (uintmax_t)elm->base.delete_tid,
410 	       elm->base.obj_type);
411 
412 	switch(node->type) {
413 	case HAMMER_BTREE_TYPE_INTERNAL:
414 		printf(" suboff=%016jx mirror=%016jx",
415 		       (uintmax_t)elm->internal.subtree_offset,
416 		       (uintmax_t)elm->internal.mirror_tid);
417 		if (ext)
418 			printf(" %s", ext);
419 		break;
420 	case HAMMER_BTREE_TYPE_LEAF:
421 		switch(elm->base.btype) {
422 		case HAMMER_BTREE_TYPE_RECORD:
423 			printf(" dataoff=%016jx/%d",
424 			       (uintmax_t)elm->leaf.data_offset,
425 			       elm->leaf.data_len);
426 			p = check_data_crc(elm);
427 			printf(" crc=%08x", elm->leaf.data_crc);
428 			if (p) {
429 				printf(" error=%s", p);
430 				++num_bad_rec;
431 			}
432 			printf(" fill=");
433 			print_bigblock_fill(elm->leaf.data_offset);
434 			if (QuietOpt < 2)
435 				print_record(elm);
436 			if (VerboseOpt)
437 				hammer_add_zone_stat(opt.stats,
438 					elm->leaf.data_offset,
439 					elm->leaf.data_len);
440 			break;
441 		default:
442 			printf(" badtype=%d", elm->base.btype);
443 			break;
444 		}
445 		if (ext)
446 			printf(" %s", ext);
447 		break;
448 	}
449 	printf("\n");
450 }
451 
452 static
453 int
454 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
455 		hammer_btree_elm_t elm,
456 		hammer_base_elm_t left_bound,
457 		hammer_base_elm_t right_bound)
458 {
459 	hammer_off_t child_offset;
460 	int flags = 0;
461 	int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
462 
463 	switch(node->type) {
464 	case HAMMER_BTREE_TYPE_INTERNAL:
465 		child_offset = elm->internal.subtree_offset;
466 		if (elm->internal.mirror_tid > node->mirror_tid)
467 			flags |= FLAG_BADMIRRORTID;
468 
469 		if (i == node->count) {
470 			if (child_offset != 0)
471 				flags |= FLAG_BADCHILDPARENT;
472 			switch(elm->base.btype) {
473 			case HAMMER_BTREE_TYPE_NONE:
474 				flags |= test_rbn_lr(elm, left_bound, right_bound);
475 				break;
476 			default:
477 				flags |= FLAG_BADTYPE;
478 				break;
479 			}
480 		} else {
481 			if (child_offset == 0) {
482 				flags |= FLAG_BADCHILDPARENT;
483 			} else {
484 				struct buffer_info *buffer = NULL;
485 				hammer_node_ondisk_t subnode;
486 				subnode = get_node(child_offset, &buffer);
487 				if (subnode == NULL)
488 					flags |= FLAG_BADCHILDPARENT;
489 				else if (subnode->parent != node_offset)
490 					flags |= FLAG_BADCHILDPARENT;
491 				rel_buffer(buffer);
492 			}
493 			switch(elm->base.btype) {
494 			case HAMMER_BTREE_TYPE_INTERNAL:
495 			case HAMMER_BTREE_TYPE_LEAF:
496 				flags |= test_lr(elm, left_bound, right_bound);
497 				break;
498 			default:
499 				flags |= FLAG_BADTYPE;
500 				break;
501 			}
502 		}
503 		break;
504 	case HAMMER_BTREE_TYPE_LEAF:
505 		if (elm->leaf.data_offset == 0) {
506 			flags |= FLAG_BADCHILDPARENT;
507 		}
508 		if (elm->leaf.data_len == 0) {
509 			flags |= FLAG_BADCHILDPARENT;
510 		}
511 
512 		if (node->mirror_tid == 0 &&
513 		    !(node->parent == 0 && node->count == 2)) {
514 			flags |= FLAG_BADMIRRORTID;
515 		}
516 		if (elm->base.create_tid && node->mirror_tid &&
517 		    elm->base.create_tid > node->mirror_tid) {
518 			flags |= FLAG_BADMIRRORTID;
519 		}
520 		if (elm->base.delete_tid && node->mirror_tid &&
521 		    elm->base.delete_tid > node->mirror_tid) {
522 			flags |= FLAG_BADMIRRORTID;
523 		}
524 		switch(elm->base.btype) {
525 		case HAMMER_BTREE_TYPE_RECORD:
526 			flags |= test_lr(elm, left_bound, right_bound);
527 			break;
528 		default:
529 			flags |= FLAG_BADTYPE;
530 			break;
531 		}
532 		break;
533 	default:
534 		flags |= FLAG_BADTYPE;
535 		break;
536 	}
537 	return(flags);
538 }
539 
540 static
541 int
542 test_lr(hammer_btree_elm_t elm,
543 	hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
544 {
545 	if (left_bound == NULL || right_bound == NULL)
546 		return(0);
547 	if (hammer_btree_cmp(&elm->base, left_bound) < 0)
548 		return(FLAG_TOOFARLEFT);
549 	if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
550 		return(FLAG_TOOFARRIGHT);
551 	return(0);
552 }
553 
554 static
555 int
556 test_rbn_lr(hammer_btree_elm_t rbn,
557 	hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
558 {
559 	if (left_bound == NULL || right_bound == NULL)
560 		return(0);
561 	if (hammer_btree_cmp(&rbn->base, left_bound) < 0)
562 		return(FLAG_TOOFARLEFT);
563 	if (hammer_btree_cmp(&rbn->base, right_bound) > 0)
564 		return(FLAG_TOOFARRIGHT);
565 	return(0);
566 }
567 
568 static
569 void
570 print_bigblock_fill(hammer_off_t offset)
571 {
572 	struct hammer_blockmap_layer1 layer1;
573 	struct hammer_blockmap_layer2 layer2;
574 	int fill;
575 	int error;
576 
577 	blockmap_lookup(offset, &layer1, &layer2, &error);
578 	printf("z%d:v%d:%d:%d:%lu=",
579 		HAMMER_ZONE_DECODE(offset),
580 		HAMMER_VOL_DECODE(offset),
581 		HAMMER_BLOCKMAP_LAYER1_INDEX(offset),
582 		HAMMER_BLOCKMAP_LAYER2_INDEX(offset),
583 		offset & HAMMER_BIGBLOCK_MASK64);
584 
585 	if (error) {
586 		printf("B%d", error);
587 	} else {
588 		fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
589 		printf("%d%%", 100 - fill);
590 	}
591 }
592 
593 /*
594  * Check the generic crc on a data element.  Inodes record types are
595  * special in that some of their fields are not CRCed.
596  *
597  * Also check that the zone is valid.
598  */
599 static
600 const char *
601 check_data_crc(hammer_btree_elm_t elm)
602 {
603 	struct buffer_info *data_buffer;
604 	hammer_off_t data_offset;
605 	hammer_off_t buf_offset;
606 	int32_t data_len;
607 	uint32_t crc;
608 	int error;
609 	char *ptr;
610 	static char bo[5];
611 
612 	data_offset = elm->leaf.data_offset;
613 	data_len = elm->leaf.data_len;
614 	data_buffer = NULL;
615 	if (data_offset == 0 || data_len == 0)
616 		return("ZO");  /* zero offset or length */
617 
618 	error = 0;
619 	buf_offset = blockmap_lookup(data_offset, NULL, NULL, &error);
620 	if (error) {
621 		bzero(bo, sizeof(bo));
622 		snprintf(bo, sizeof(bo), "BO%d", -error);
623 		return(bo);
624 	}
625 
626 	crc = 0;
627 	switch (elm->leaf.base.rec_type) {
628 	case HAMMER_RECTYPE_INODE:
629 		if (data_len != sizeof(struct hammer_inode_data))
630 			return("BI");  /* bad inode size */
631 		ptr = get_buffer_data(buf_offset, &data_buffer, 0);
632 		crc = crc32(ptr, HAMMER_INODE_CRCSIZE);
633 		rel_buffer(data_buffer);
634 		break;
635 	default:
636 		crc = get_buf_crc(buf_offset, data_len);
637 		break;
638 	}
639 
640 	if (crc == 0)
641 		return("Bx");  /* bad crc */
642 	if (crc != elm->leaf.data_crc)
643 		return("BX");  /* bad crc */
644 	return(NULL);  /* success */
645 }
646 
647 static
648 uint32_t
649 get_buf_crc(hammer_off_t buf_offset, int32_t buf_len)
650 {
651 	struct buffer_info *data_buffer = NULL;
652 	int32_t len;
653 	uint32_t crc = 0;
654 	char *ptr;
655 
656 	while (buf_len) {
657 		ptr = get_buffer_data(buf_offset, &data_buffer, 0);
658 		len = HAMMER_BUFSIZE - ((int)buf_offset & HAMMER_BUFMASK);
659 		if (len > buf_len)
660 			len = (int)buf_len;
661 		assert(len <= HAMMER_BUFSIZE);
662 		crc = crc32_ext(ptr, len, crc);
663 		buf_len -= len;
664 		buf_offset += len;
665 	}
666 	rel_buffer(data_buffer);
667 
668 	return(crc);
669 }
670 
671 static
672 void
673 print_config(char *cfgtxt)
674 {
675 	char *token;
676 
677 	printf("\n%s%17s", INDENT, "");
678 	printf("config text=\"\n");
679 	if (cfgtxt != NULL) {
680 		while((token = strsep(&cfgtxt, "\r\n")) != NULL)
681 			if (strlen(token))
682 				printf("%s%17s            %s\n",
683 					INDENT, "", token);
684 	}
685 	printf("%s%17s            \"", INDENT, "");
686 }
687 
688 static
689 void
690 print_record(hammer_btree_elm_t elm)
691 {
692 	struct buffer_info *data_buffer;
693 	hammer_off_t data_offset;
694 	int32_t data_len;
695 	hammer_data_ondisk_t data;
696 	uint32_t status;
697 	char *str1 = NULL;
698 	char *str2 = NULL;
699 
700 	data_offset = elm->leaf.data_offset;
701 	data_len = elm->leaf.data_len;
702 	assert(data_offset != 0);
703 	assert(data_len != 0);
704 
705 	data_buffer = NULL;
706 	data = get_buffer_data(data_offset, &data_buffer, 0);
707 	assert(data != NULL);
708 
709 	switch(elm->leaf.base.rec_type) {
710 	case HAMMER_RECTYPE_UNKNOWN:
711 		printf("\n%s%17s", INDENT, "");
712 		printf("unknown");
713 		break;
714 	case HAMMER_RECTYPE_INODE:
715 		printf("\n%s%17s", INDENT, "");
716 		printf("inode size=%jd nlinks=%jd",
717 		       (intmax_t)data->inode.size,
718 		       (intmax_t)data->inode.nlinks);
719 		printf(" mode=%05o uflags=%08x caps=%02x",
720 			data->inode.mode,
721 			data->inode.uflags,
722 			data->inode.cap_flags);
723 		printf(" pobjid=%016jx ot=%02x\n",
724 			(uintmax_t)data->inode.parent_obj_id,
725 			data->inode.obj_type);
726 		printf("%s%17s", INDENT, "");
727 		printf("      ctime=%016jx mtime=%016jx atime=%016jx",
728 			(uintmax_t)data->inode.ctime,
729 			(uintmax_t)data->inode.mtime,
730 			(uintmax_t)data->inode.atime);
731 		if (data->inode.ext.symlink[0])
732 			printf(" symlink=\"%s\"",
733 				data->inode.ext.symlink);
734 		break;
735 	case HAMMER_RECTYPE_DIRENTRY:
736 		data_len -= HAMMER_ENTRY_NAME_OFF;
737 		printf("\n%s%17s", INDENT, "");
738 		printf("dir-entry objid=%016jx lo=%08x",
739 		       (uintmax_t)data->entry.obj_id,
740 		       data->entry.localization);
741 		if (!opt.obfuscate)
742 			printf(" name=\"%*.*s\"",
743 			       data_len, data_len, data->entry.name);
744 		break;
745 	case HAMMER_RECTYPE_FIX:
746 		switch(elm->leaf.base.key) {
747 		case HAMMER_FIXKEY_SYMLINK:
748 			data_len -= HAMMER_SYMLINK_NAME_OFF;
749 			printf("\n%s%17s", INDENT, "");
750 			printf("fix-symlink name=\"%*.*s\"",
751 				data_len, data_len, data->symlink.name);
752 			break;
753 		}
754 		break;
755 	case HAMMER_RECTYPE_PFS:
756 		printf("\n%s%17s", INDENT, "");
757 		printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
758 			(intmax_t)data->pfsd.sync_beg_tid,
759 			(intmax_t)data->pfsd.sync_end_tid);
760 		uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
761 		uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
762 		printf("%17s", "");
763 		printf("    shared_uuid=%s\n", str1);
764 		printf("%17s", "");
765 		printf("    unique_uuid=%s\n", str2);
766 		printf("%17s", "");
767 		printf("    mirror_flags=%08x label=\"%s\"",
768 			data->pfsd.mirror_flags, data->pfsd.label);
769 		if (data->pfsd.snapshots[0])
770 			printf(" snapshots=\"%s\"", data->pfsd.snapshots);
771 		free(str1);
772 		free(str2);
773 		break;
774 	case HAMMER_RECTYPE_SNAPSHOT:
775 		printf("\n%s%17s", INDENT, "");
776 		printf("snapshot tid=%016jx label=\"%s\"",
777 			(intmax_t)data->snap.tid, data->snap.label);
778 		break;
779 	case HAMMER_RECTYPE_CONFIG:
780 		if (VerboseOpt > 2) {
781 			char *p = strdup(data->config.text);
782 			print_config(p);
783 			free(p);
784 		}
785 		break;
786 	case HAMMER_RECTYPE_DATA:
787 		if (VerboseOpt > 3) {
788 			printf("\n");
789 			hexdump_record(data, data_len, "\t\t  ");
790 		}
791 		break;
792 	case HAMMER_RECTYPE_EXT:
793 	case HAMMER_RECTYPE_DB:
794 		if (VerboseOpt > 2) {
795 			printf("\n");
796 			hexdump_record(data, data_len, "\t\t  ");
797 		}
798 		break;
799 	default:
800 		assert(0);
801 		break;
802 	}
803 	rel_buffer(data_buffer);
804 }
805 
806 /*
807  * HAMMER userspace only supports buffer size upto HAMMER_BUFSIZE
808  * which is 16KB.  Passing record data length larger than 16KB to
809  * hexdump(3) is invalid even if the leaf node elm says >16KB data.
810  */
811 static void
812 hexdump_record(const void *ptr, int length, const char *hdr)
813 {
814 	int data_len = length;
815 
816 	if (data_len > HAMMER_BUFSIZE)  /* XXX */
817 		data_len = HAMMER_BUFSIZE;
818 	hexdump(ptr, data_len, hdr, 0);
819 
820 	if (length > data_len)
821 		printf("%s....\n", hdr);
822 }
823 
824 static __inline
825 unsigned long
826 _strtoul(const char *p, int base)
827 {
828 	unsigned long retval;
829 
830 	errno = 0;  /* clear */
831 	retval = strtoul(p, NULL, base);
832 	if (errno == ERANGE && retval == ULONG_MAX)
833 		err(1, "strtoul");
834 	return retval;
835 }
836 
837 static __inline
838 unsigned long long
839 _strtoull(const char *p, int base)
840 {
841 	unsigned long long retval;
842 
843 	errno = 0;  /* clear */
844 	retval = strtoull(p, NULL, base);
845 	if (errno == ERANGE && retval == ULLONG_MAX)
846 		err(1, "strtoull");
847 	return retval;
848 }
849 
850 static int
851 init_btree_search(const char *arg)
852 {
853 	char *s, *p;
854 	int i = 0;
855 
856 	bzero(&opt.base, sizeof(opt.base));
857 	opt.limit = 0;
858 
859 	if (arg == NULL)
860 		return(-1);
861 	if (strcmp(arg, "none") == 0)
862 		return(-1);
863 
864 	s = strdup(arg);
865 	if (s == NULL)
866 		return(-1);
867 
868 	while ((p = s) != NULL) {
869 		if ((s = strchr(s, ':')) != NULL)
870 			*s++ = 0;
871 		if (++i == 1) {
872 			opt.base.localization = _strtoul(p, 16);
873 		} else if (i == 2) {
874 			opt.base.obj_id = _strtoull(p, 16);
875 		} else if (i == 3) {
876 			opt.base.rec_type = _strtoul(p, 16);
877 		} else if (i == 4) {
878 			opt.base.key = _strtoull(p, 16);
879 		} else if (i == 5) {
880 			opt.base.create_tid = _strtoull(p, 16);
881 			break;
882 		}
883 	}
884 	opt.limit = i;
885 	free(s);
886 
887 	return(i);
888 }
889 
890 static int
891 test_btree_search(hammer_btree_elm_t elm)
892 {
893 	hammer_base_elm_t base1 = &elm->base;
894 	hammer_base_elm_t base2 = &opt.base;
895 	int limit = opt.limit;
896 
897 	if (base1->localization < base2->localization)
898 		return(-1);
899 	if (base1->localization > base2->localization)
900 		return(1);
901 	if (limit == 1)
902 		return(0);  /* ignore below */
903 
904 	if (base1->obj_id < base2->obj_id)
905 		return(-2);
906 	if (base1->obj_id > base2->obj_id)
907 		return(2);
908 	if (limit == 2)
909 		return(0);  /* ignore below */
910 
911 	if (base1->rec_type < base2->rec_type)
912 		return(-3);
913 	if (base1->rec_type > base2->rec_type)
914 		return(3);
915 	if (limit == 3)
916 		return(0);  /* ignore below */
917 
918 	if (base1->key < base2->key)
919 		return(-4);
920 	if (base1->key > base2->key)
921 		return(4);
922 	if (limit == 4)
923 		return(0);  /* ignore below */
924 
925 	if (base1->create_tid == 0) {
926 		if (base2->create_tid == 0)
927 			return(0);
928 		return(5);
929 	}
930 	if (base2->create_tid == 0)
931 		return(-5);
932 	if (base1->create_tid < base2->create_tid)
933 		return(-5);
934 	if (base1->create_tid > base2->create_tid)
935 		return(5);
936 	return(0);
937 }
938 
939 static __inline
940 int
941 test_btree_match(hammer_btree_elm_t elm)
942 {
943 	if (test_btree_search(elm) == 0)
944 		return(1);
945 	return(0);
946 }
947 
948 static
949 int
950 test_btree_out_of_range(hammer_btree_elm_t elm)
951 {
952 	if (test_btree_search(elm) > 0)
953 		return(1);  /* conditions < this elm */
954 
955 	if (opt.limit >= 5) {
956 		if (test_btree_search(elm + 1) <= 0)
957 			return(1);  /* next elm <= conditions */
958 	} else {
959 		if (test_btree_search(elm + 1) < 0)
960 			return(1);  /* next elm < conditions */
961 	}
962 	return(0);
963 }
964 
965 /*
966  * Dump the UNDO FIFO
967  */
968 void
969 hammer_cmd_show_undo(void)
970 {
971 	struct volume_info *volume;
972 	hammer_blockmap_t rootmap;
973 	hammer_off_t scan_offset;
974 	hammer_fifo_any_t head;
975 	struct buffer_info *data_buffer = NULL;
976 
977 	volume = get_root_volume();
978 	rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
979 
980 	print_blockmap(volume);
981 
982 	scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
983 	while (scan_offset < rootmap->alloc_offset) {
984 		head = get_buffer_data(scan_offset, &data_buffer, 0);
985 		printf("%016jx ", scan_offset);
986 
987 		switch(head->head.hdr_type) {
988 		case HAMMER_HEAD_TYPE_PAD:
989 			printf("PAD(%04x)", head->head.hdr_size);
990 			break;
991 		case HAMMER_HEAD_TYPE_DUMMY:
992 			printf("DUMMY(%04x) seq=%08x",
993 				head->head.hdr_size, head->head.hdr_seq);
994 			break;
995 		case HAMMER_HEAD_TYPE_UNDO:
996 			printf("UNDO(%04x) seq=%08x "
997 			       "dataoff=%016jx bytes=%d",
998 				head->head.hdr_size, head->head.hdr_seq,
999 				(intmax_t)head->undo.undo_offset,
1000 				head->undo.undo_data_bytes);
1001 			break;
1002 		case HAMMER_HEAD_TYPE_REDO:
1003 			printf("REDO(%04x) seq=%08x flags=%08x "
1004 			       "objid=%016jx logoff=%016jx bytes=%d",
1005 				head->head.hdr_size, head->head.hdr_seq,
1006 				head->redo.redo_flags,
1007 				(intmax_t)head->redo.redo_objid,
1008 				(intmax_t)head->redo.redo_offset,
1009 				head->redo.redo_data_bytes);
1010 			break;
1011 		default:
1012 			printf("UNKNOWN(%04x,%04x) seq=%08x",
1013 				head->head.hdr_type,
1014 				head->head.hdr_size,
1015 				head->head.hdr_seq);
1016 			break;
1017 		}
1018 
1019 		if (scan_offset == rootmap->first_offset)
1020 			printf(" >");
1021 		if (scan_offset == rootmap->next_offset)
1022 			printf(" <");
1023 		printf("\n");
1024 
1025 		if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
1026 		    head->head.hdr_size == 0 ||
1027 		    head->head.hdr_size > HAMMER_UNDO_ALIGN -
1028 				    ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
1029 			printf("Illegal size field, skipping to "
1030 			       "next boundary\n");
1031 			scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
1032 					~HAMMER_UNDO_MASK64;
1033 		} else {
1034 			scan_offset += head->head.hdr_size;
1035 		}
1036 	}
1037 	rel_buffer(data_buffer);
1038 }
1039