sbin/hammer: Fix hammer show-undo output format
[dragonfly.git] / sbin / hammer / cmd_show.c
blob7f21c0f5fe0138c58a5ef79c4466fa92c3097ee1
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 <libutil.h>
37 #include "hammer.h"
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
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;
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, hammer_tid_t mirror_tid,
57 hammer_btree_elm_t lbe);
58 static int test_node_count(hammer_node_ondisk_t node, char *badmp);
59 static void print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
60 hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext);
61 static int get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
62 hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
63 static int test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
64 static int test_rbn_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
65 static void print_bigblock_fill(hammer_off_t offset);
66 static const char *check_data_crc(hammer_btree_elm_t elm);
67 static uint32_t get_buf_crc(hammer_off_t buf_offset, int32_t buf_len);
68 static void print_record(hammer_btree_elm_t elm);
69 static int init_btree_search(const char *arg);
70 static int test_btree_search(hammer_btree_elm_t elm);
71 static int test_btree_match(hammer_btree_elm_t elm);
72 static int test_btree_out_of_range(hammer_btree_elm_t elm);
73 static void hexdump_record(const void *ptr, int length, const char *hdr);
75 static int num_bad_node = 0;
76 static int num_bad_elm = 0;
77 static int num_bad_rec = 0;
78 static int depth;
80 #define _X "\t"
81 static const char* _indents[] = {
82 "",
83 _X,
84 _X _X,
85 _X _X _X,
86 _X _X _X _X,
87 _X _X _X _X _X,
88 _X _X _X _X _X _X,
89 _X _X _X _X _X _X _X,
90 _X _X _X _X _X _X _X _X,
91 _X _X _X _X _X _X _X _X _X,
92 _X _X _X _X _X _X _X _X _X _X,
93 _X _X _X _X _X _X _X _X _X _X _X,
94 _X _X _X _X _X _X _X _X _X _X _X _X,
95 _X _X _X _X _X _X _X _X _X _X _X _X _X,
96 _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
97 _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
98 _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
99 /* deep enough */
101 #define INDENT _indents[opt.indent ? depth : 0]
103 void
104 hammer_cmd_show(const char *arg, int filter, int obfuscate, int indent)
106 struct volume_info *volume;
107 hammer_volume_ondisk_t ondisk;
108 struct zone_stat *stats = NULL;
110 volume = get_root_volume();
111 ondisk = volume->ondisk;
113 print_blockmap(volume);
115 if (VerboseOpt)
116 stats = hammer_init_zone_stat_bits();
118 bzero(&opt, sizeof(opt));
119 opt.filter = filter;
120 opt.obfuscate = obfuscate;
121 opt.indent = indent;
122 opt.stats = stats;
124 if (init_btree_search(arg) > 0) {
125 printf("arg=\"%s\"", arg);
126 if (opt.limit > 0)
127 printf(" lo=%08x", opt.base.localization);
128 if (opt.limit > 1)
129 printf(" objid=%016jx", (uintmax_t)opt.base.obj_id);
130 if (opt.limit > 2)
131 printf(" rt=%02x", opt.base.rec_type);
132 if (opt.limit > 3)
133 printf(" key=%016jx", (uintmax_t)opt.base.key);
134 if (opt.limit > 4)
135 printf(" tid=%016jx", (uintmax_t)opt.base.create_tid);
136 printf("\n");
138 print_btree(ondisk->vol0_btree_root);
140 if (stats) {
141 hammer_print_zone_stat(stats);
142 hammer_cleanup_zone_stat(stats);
145 if (num_bad_node || VerboseOpt) {
146 printf("%d bad nodes\n", num_bad_node);
148 if (num_bad_elm || VerboseOpt) {
149 printf("%d bad elms\n", num_bad_elm);
151 if (num_bad_rec || VerboseOpt) {
152 printf("%d bad records\n", num_bad_rec);
156 static __inline
157 void
158 print_btree(hammer_off_t node_offset)
160 depth = -1;
161 print_btree_node(node_offset, HAMMER_MAX_TID, NULL);
162 assert(depth == -1);
165 static __inline
166 void
167 print_subtree(hammer_btree_elm_t elm)
169 hammer_btree_internal_elm_t i = &elm->internal;
170 print_btree_node(i->subtree_offset, i->mirror_tid, elm);
173 static void
174 print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid,
175 hammer_btree_elm_t lbe)
177 struct buffer_info *buffer = NULL;
178 hammer_node_ondisk_t node;
179 hammer_btree_elm_t elm;
180 int i;
181 char badc = ' '; /* good */
182 char badm = ' '; /* good */
183 const char *ext;
185 depth++;
186 node = get_buffer_data(node_offset, &buffer, 0);
188 if (node == NULL) {
189 badc = 'B';
190 badm = 'I';
191 } else {
192 if (!hammer_crc_test_btree(node))
193 badc = 'B';
194 if (node->mirror_tid > mirror_tid) {
195 badc = 'B';
196 badm = 'M';
198 if (test_node_count(node, &badm) == -1) {
199 badc = 'B';
200 assert(badm != ' ');
204 if (badm != ' ' || badc != ' ') /* not good */
205 ++num_bad_node;
207 printf("%s%c%c NODE %016jx ",
208 INDENT, badc, badm, (uintmax_t)node_offset);
209 printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx",
210 node->count,
211 (uintmax_t)node->parent,
212 (node->type ? node->type : '?'),
213 depth,
214 (uintmax_t)node->mirror_tid);
215 printf(" fill=");
216 print_bigblock_fill(node_offset);
217 printf(" {\n");
219 if (opt.stats)
220 hammer_add_zone_stat(opt.stats, node_offset, sizeof(*node));
222 for (i = 0; i < node->count; ++i) {
223 elm = &node->elms[i];
224 ext = NULL;
225 if (opt.limit) {
226 switch (node->type) {
227 case HAMMER_BTREE_TYPE_INTERNAL:
228 if (!test_btree_out_of_range(elm))
229 ext = "*";
230 break;
231 case HAMMER_BTREE_TYPE_LEAF:
232 if (test_btree_match(elm))
233 ext = "*";
234 break;
237 print_btree_elm(node, node_offset, elm, lbe, ext);
239 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
240 assert(i == node->count); /* boundary */
241 elm = &node->elms[i];
242 print_btree_elm(node, node_offset, elm, lbe, NULL);
244 printf("%s }\n", INDENT);
246 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
247 for (i = 0; i < node->count; ++i) {
248 elm = &node->elms[i];
249 if (opt.limit && opt.filter) {
250 if (test_btree_out_of_range(elm))
251 continue;
253 if (elm->internal.subtree_offset) {
254 print_subtree(elm);
256 * Cause show to do normal iteration after
257 * seeking to the lo:objid:rt:key:tid
258 * by default
260 if (opt.limit && opt.filter == -1) /* default */
261 opt.filter = 0;
265 rel_buffer(buffer);
266 depth--;
269 static int
270 test_node_count(hammer_node_ondisk_t node, char *badmp)
272 hammer_node_ondisk_t parent_node;
273 struct buffer_info *buffer = NULL;
274 int maxcount;
276 maxcount = hammer_node_max_elements(node->type);
278 if (maxcount == -1) {
279 *badmp = 'U';
280 return(-1);
281 } else if (node->count > maxcount) {
282 *badmp = 'C';
283 return(-1);
284 } else if (node->count == 0) {
285 parent_node = get_buffer_data(node->parent, &buffer, 0);
286 if (parent_node->count != 1) {
287 *badmp = 'C';
288 rel_buffer(buffer);
289 return(-1);
291 rel_buffer(buffer);
294 return(0);
297 static __inline
299 is_root_btree_beg(uint8_t type, int i, hammer_btree_elm_t elm)
302 * elm->base.btype depends on what the original node had
303 * so it could be anything but HAMMER_BTREE_TYPE_NONE.
305 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
306 i == 0 &&
307 elm->base.localization == HAMMER_MIN_ONDISK_LOCALIZATION &&
308 elm->base.obj_id == (int64_t)HAMMER_MIN_OBJID &&
309 elm->base.key == (int64_t)HAMMER_MIN_KEY &&
310 elm->base.create_tid == 1 &&
311 elm->base.delete_tid == 1 &&
312 elm->base.rec_type == HAMMER_MIN_RECTYPE &&
313 elm->base.obj_type == 0 &&
314 elm->base.btype != HAMMER_BTREE_TYPE_NONE);
317 static __inline
319 is_root_btree_end(uint8_t type, int i, hammer_btree_elm_t elm)
321 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
322 i != 0 &&
323 elm->base.localization == HAMMER_MAX_ONDISK_LOCALIZATION &&
324 elm->base.obj_id == HAMMER_MAX_OBJID &&
325 elm->base.key == HAMMER_MAX_KEY &&
326 elm->base.create_tid == HAMMER_MAX_TID &&
327 elm->base.delete_tid == 0 &&
328 elm->base.rec_type == HAMMER_MAX_RECTYPE &&
329 elm->base.obj_type == 0 &&
330 elm->base.btype == HAMMER_BTREE_TYPE_NONE);
333 static
334 void
335 print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
336 hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext)
338 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
339 char deleted;
340 char rootelm;
341 const char *label;
342 const char *p;
343 int flags;
344 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
346 flags = get_elm_flags(node, node_offset, elm, lbe);
347 flagstr[0] = flags ? 'B' : 'G';
348 if (flags & FLAG_TOOFARLEFT)
349 flagstr[2] = 'L';
350 if (flags & FLAG_TOOFARRIGHT)
351 flagstr[3] = 'R';
352 if (flags & FLAG_BADTYPE)
353 flagstr[4] = 'T';
354 if (flags & FLAG_BADCHILDPARENT)
355 flagstr[5] = 'C';
356 if (flags & FLAG_BADMIRRORTID)
357 flagstr[6] = 'M';
358 if (flagstr[0] == 'B')
359 ++num_bad_elm;
362 * Check if elm is derived from root split
364 if (is_root_btree_beg(node->type, i, elm))
365 rootelm = '>';
366 else if (is_root_btree_end(node->type, i, elm))
367 rootelm = '<';
368 else
369 rootelm = ' ';
371 if (elm->base.delete_tid)
372 deleted = 'd';
373 else
374 deleted = ' ';
376 if (node->type == HAMMER_BTREE_TYPE_INTERNAL && node->count == i)
377 label = "RBN";
378 else
379 label = "ELM";
381 printf("%s%s %s %2d %c ",
382 INDENT, flagstr, label, i, hammer_elm_btype(elm));
383 printf("lo=%08x objid=%016jx rt=%02x key=%016jx tid=%016jx\n",
384 elm->base.localization,
385 (uintmax_t)elm->base.obj_id,
386 elm->base.rec_type,
387 (uintmax_t)elm->base.key,
388 (uintmax_t)elm->base.create_tid);
389 printf("%s %c del=%016jx ot=%02x",
390 INDENT,
391 (rootelm == ' ' ? deleted : rootelm),
392 (uintmax_t)elm->base.delete_tid,
393 elm->base.obj_type);
395 switch(node->type) {
396 case HAMMER_BTREE_TYPE_INTERNAL:
397 printf(" suboff=%016jx mirror=%016jx",
398 (uintmax_t)elm->internal.subtree_offset,
399 (uintmax_t)elm->internal.mirror_tid);
400 if (ext)
401 printf(" %s", ext);
402 break;
403 case HAMMER_BTREE_TYPE_LEAF:
404 switch(elm->base.btype) {
405 case HAMMER_BTREE_TYPE_RECORD:
406 printf(" dataoff=%016jx/%d",
407 (uintmax_t)elm->leaf.data_offset,
408 elm->leaf.data_len);
409 p = check_data_crc(elm);
410 printf(" crc=%08x", elm->leaf.data_crc);
411 if (p) {
412 printf(" error=%s", p);
413 ++num_bad_rec;
415 printf(" fill=");
416 print_bigblock_fill(elm->leaf.data_offset);
417 if (QuietOpt < 2)
418 print_record(elm);
419 if (opt.stats)
420 hammer_add_zone_stat(opt.stats,
421 elm->leaf.data_offset,
422 elm->leaf.data_len);
423 break;
424 default:
425 printf(" badtype=%d", elm->base.btype);
426 break;
428 if (ext)
429 printf(" %s", ext);
430 break;
432 printf("\n");
435 static
437 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
438 hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
440 hammer_off_t child_offset;
441 int flags = 0;
442 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
444 switch(node->type) {
445 case HAMMER_BTREE_TYPE_INTERNAL:
446 child_offset = elm->internal.subtree_offset;
447 if (elm->internal.mirror_tid > node->mirror_tid)
448 flags |= FLAG_BADMIRRORTID;
450 if (i == node->count) {
451 if (child_offset != 0)
452 flags |= FLAG_BADCHILDPARENT;
453 switch(elm->base.btype) {
454 case HAMMER_BTREE_TYPE_NONE:
455 flags |= test_rbn_lr(elm, lbe);
456 break;
457 default:
458 flags |= FLAG_BADTYPE;
459 break;
461 } else {
462 if (child_offset == 0) {
463 flags |= FLAG_BADCHILDPARENT;
464 } else {
465 struct buffer_info *buffer = NULL;
466 hammer_node_ondisk_t subnode;
467 subnode = get_buffer_data(child_offset, &buffer, 0);
468 if (subnode == NULL)
469 flags |= FLAG_BADCHILDPARENT;
470 else if (subnode->parent != node_offset)
471 flags |= FLAG_BADCHILDPARENT;
472 rel_buffer(buffer);
474 switch(elm->base.btype) {
475 case HAMMER_BTREE_TYPE_INTERNAL:
476 case HAMMER_BTREE_TYPE_LEAF:
477 flags |= test_lr(elm, lbe);
478 break;
479 default:
480 flags |= FLAG_BADTYPE;
481 break;
484 break;
485 case HAMMER_BTREE_TYPE_LEAF:
486 if (elm->leaf.data_offset == 0) {
487 flags |= FLAG_BADCHILDPARENT;
489 if (elm->leaf.data_len == 0) {
490 flags |= FLAG_BADCHILDPARENT;
493 if (node->mirror_tid == 0 &&
494 !(node->parent == 0 && node->count == 2)) {
495 flags |= FLAG_BADMIRRORTID;
497 if (elm->base.create_tid && node->mirror_tid &&
498 elm->base.create_tid > node->mirror_tid) {
499 flags |= FLAG_BADMIRRORTID;
501 if (elm->base.delete_tid && node->mirror_tid &&
502 elm->base.delete_tid > node->mirror_tid) {
503 flags |= FLAG_BADMIRRORTID;
505 switch(elm->base.btype) {
506 case HAMMER_BTREE_TYPE_RECORD:
507 flags |= test_lr(elm, lbe);
508 break;
509 default:
510 flags |= FLAG_BADTYPE;
511 break;
513 break;
514 default:
515 flags |= FLAG_BADTYPE;
516 break;
518 return(flags);
522 * Taken from /usr/src/sys/vfs/hammer/hammer_btree.c.
524 static
526 hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
528 if (key1->localization < key2->localization)
529 return(-5);
530 if (key1->localization > key2->localization)
531 return(5);
533 if (key1->obj_id < key2->obj_id)
534 return(-4);
535 if (key1->obj_id > key2->obj_id)
536 return(4);
538 if (key1->rec_type < key2->rec_type)
539 return(-3);
540 if (key1->rec_type > key2->rec_type)
541 return(3);
543 if (key1->key < key2->key)
544 return(-2);
545 if (key1->key > key2->key)
546 return(2);
548 if (key1->create_tid == 0) {
549 if (key2->create_tid == 0)
550 return(0);
551 return(1);
553 if (key2->create_tid == 0)
554 return(-1);
555 if (key1->create_tid < key2->create_tid)
556 return(-1);
557 if (key1->create_tid > key2->create_tid)
558 return(1);
559 return(0);
562 static
564 test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
566 if (lbe) {
567 hammer_btree_elm_t rbe = lbe + 1;
568 if (hammer_btree_cmp(&elm->base, &lbe->base) < 0)
569 return(FLAG_TOOFARLEFT);
570 if (hammer_btree_cmp(&elm->base, &rbe->base) >= 0)
571 return(FLAG_TOOFARRIGHT);
573 return(0);
576 static
578 test_rbn_lr(hammer_btree_elm_t rbn, hammer_btree_elm_t lbe)
580 if (lbe) {
581 hammer_btree_elm_t rbe = lbe + 1;
582 if (hammer_btree_cmp(&rbn->base, &lbe->base) < 0)
583 return(FLAG_TOOFARLEFT);
584 if (hammer_btree_cmp(&rbn->base, &rbe->base) > 0)
585 return(FLAG_TOOFARRIGHT);
587 return(0);
590 static
591 void
592 print_bigblock_fill(hammer_off_t offset)
594 struct hammer_blockmap_layer1 layer1;
595 struct hammer_blockmap_layer2 layer2;
596 int fill;
597 int error;
599 blockmap_lookup(offset, &layer1, &layer2, &error);
600 printf("z%d:v%d:%d:%d:%lu=",
601 HAMMER_ZONE_DECODE(offset),
602 HAMMER_VOL_DECODE(offset),
603 HAMMER_BLOCKMAP_LAYER1_INDEX(offset),
604 HAMMER_BLOCKMAP_LAYER2_INDEX(offset),
605 offset & HAMMER_BIGBLOCK_MASK64);
607 if (error) {
608 printf("B%d", error);
609 } else {
610 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
611 printf("%d%%", 100 - fill);
616 * Check the generic crc on a data element. Inodes record types are
617 * special in that some of their fields are not CRCed.
619 * Also check that the zone is valid.
621 static
622 const char *
623 check_data_crc(hammer_btree_elm_t elm)
625 struct buffer_info *data_buffer;
626 hammer_off_t data_offset;
627 hammer_off_t buf_offset;
628 int32_t data_len;
629 uint32_t crc;
630 int error;
631 char *ptr;
632 static char bo[5];
634 data_offset = elm->leaf.data_offset;
635 data_len = elm->leaf.data_len;
636 data_buffer = NULL;
637 if (data_offset == 0 || data_len == 0)
638 return("ZO"); /* zero offset or length */
640 error = 0;
641 buf_offset = blockmap_lookup(data_offset, NULL, NULL, &error);
642 if (error) {
643 bzero(bo, sizeof(bo));
644 snprintf(bo, sizeof(bo), "BO%d", -error);
645 return(bo);
648 crc = 0;
649 switch (elm->leaf.base.rec_type) {
650 case HAMMER_RECTYPE_INODE:
651 if (data_len != sizeof(struct hammer_inode_data))
652 return("BI"); /* bad inode size */
653 ptr = get_buffer_data(buf_offset, &data_buffer, 0);
654 crc = hammer_crc_get_leaf(ptr, &elm->leaf);
655 rel_buffer(data_buffer);
656 break;
657 default:
658 crc = get_buf_crc(buf_offset, data_len);
659 break;
662 if (crc == 0)
663 return("Bx"); /* bad crc */
664 if (crc != elm->leaf.data_crc)
665 return("BX"); /* bad crc */
666 return(NULL); /* success */
669 static
670 uint32_t
671 get_buf_crc(hammer_off_t buf_offset, int32_t buf_len)
673 struct buffer_info *data_buffer = NULL;
674 int32_t len;
675 uint32_t crc = 0;
676 char *ptr;
678 while (buf_len) {
679 ptr = get_buffer_data(buf_offset, &data_buffer, 0);
680 len = HAMMER_BUFSIZE - ((int)buf_offset & HAMMER_BUFMASK);
681 if (len > buf_len)
682 len = (int)buf_len;
683 assert(len <= HAMMER_BUFSIZE);
684 crc = crc32_ext(ptr, len, crc);
685 buf_len -= len;
686 buf_offset += len;
688 rel_buffer(data_buffer);
690 return(crc);
693 static
694 void
695 print_config(char *cfgtxt)
697 char *token;
699 printf("\n%s%17s", INDENT, "");
700 printf("config text=\"\n");
701 if (cfgtxt != NULL) {
702 while((token = strsep(&cfgtxt, "\r\n")) != NULL)
703 if (strlen(token))
704 printf("%s%17s %s\n",
705 INDENT, "", token);
707 printf("%s%17s \"", INDENT, "");
710 static
711 void
712 print_record(hammer_btree_elm_t elm)
714 struct buffer_info *data_buffer;
715 hammer_off_t data_offset;
716 int32_t data_len;
717 hammer_data_ondisk_t data;
718 uint32_t status;
719 char *str1 = NULL;
720 char *str2 = NULL;
722 data_offset = elm->leaf.data_offset;
723 data_len = elm->leaf.data_len;
724 assert(data_offset != 0);
725 assert(data_len != 0);
727 data_buffer = NULL;
728 data = get_buffer_data(data_offset, &data_buffer, 0);
729 assert(data != NULL);
731 switch(elm->leaf.base.rec_type) {
732 case HAMMER_RECTYPE_UNKNOWN:
733 printf("\n%s%17s", INDENT, "");
734 printf("unknown");
735 break;
736 case HAMMER_RECTYPE_INODE:
737 printf("\n%s%17s", INDENT, "");
738 printf("inode size=%jd nlinks=%jd",
739 (intmax_t)data->inode.size,
740 (intmax_t)data->inode.nlinks);
741 printf(" mode=%05o uflags=%08x caps=%02x",
742 data->inode.mode,
743 data->inode.uflags,
744 data->inode.cap_flags);
745 printf(" pobjid=%016jx ot=%02x\n",
746 (uintmax_t)data->inode.parent_obj_id,
747 data->inode.obj_type);
748 printf("%s%17s", INDENT, "");
749 printf(" ctime=%016jx mtime=%016jx atime=%016jx",
750 (uintmax_t)data->inode.ctime,
751 (uintmax_t)data->inode.mtime,
752 (uintmax_t)data->inode.atime);
753 if (data->inode.ext.symlink[0])
754 printf(" symlink=\"%s\"",
755 data->inode.ext.symlink);
756 break;
757 case HAMMER_RECTYPE_DIRENTRY:
758 data_len -= HAMMER_ENTRY_NAME_OFF;
759 printf("\n%s%17s", INDENT, "");
760 printf("dir-entry objid=%016jx lo=%08x",
761 (uintmax_t)data->entry.obj_id,
762 data->entry.localization);
763 if (!opt.obfuscate)
764 printf(" name=\"%*.*s\"",
765 data_len, data_len, data->entry.name);
766 break;
767 case HAMMER_RECTYPE_FIX:
768 switch(elm->leaf.base.key) {
769 case HAMMER_FIXKEY_SYMLINK:
770 data_len -= HAMMER_SYMLINK_NAME_OFF;
771 printf("\n%s%17s", INDENT, "");
772 printf("fix-symlink name=\"%*.*s\"",
773 data_len, data_len, data->symlink.name);
774 break;
776 break;
777 case HAMMER_RECTYPE_PFS:
778 printf("\n%s%17s", INDENT, "");
779 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
780 (intmax_t)data->pfsd.sync_beg_tid,
781 (intmax_t)data->pfsd.sync_end_tid);
782 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
783 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
784 printf("%17s", "");
785 printf(" shared_uuid=%s\n", str1);
786 printf("%17s", "");
787 printf(" unique_uuid=%s\n", str2);
788 printf("%17s", "");
789 printf(" mirror_flags=%08x label=\"%s\"",
790 data->pfsd.mirror_flags, data->pfsd.label);
791 if (data->pfsd.snapshots[0])
792 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
793 free(str1);
794 free(str2);
795 break;
796 case HAMMER_RECTYPE_SNAPSHOT:
797 printf("\n%s%17s", INDENT, "");
798 printf("snapshot tid=%016jx label=\"%s\"",
799 (intmax_t)data->snap.tid, data->snap.label);
800 break;
801 case HAMMER_RECTYPE_CONFIG:
802 if (VerboseOpt > 2) {
803 char *p = strdup(data->config.text);
804 print_config(p);
805 free(p);
807 break;
808 case HAMMER_RECTYPE_DATA:
809 if (VerboseOpt > 3) {
810 printf("\n");
811 hexdump_record(data, data_len, "\t\t ");
813 break;
814 case HAMMER_RECTYPE_EXT:
815 case HAMMER_RECTYPE_DB:
816 if (VerboseOpt > 2) {
817 printf("\n");
818 hexdump_record(data, data_len, "\t\t ");
820 break;
821 default:
822 assert(0);
823 break;
825 rel_buffer(data_buffer);
829 * HAMMER userspace only supports buffer size upto HAMMER_BUFSIZE
830 * which is 16KB. Passing record data length larger than 16KB to
831 * hexdump(3) is invalid even if the leaf node elm says >16KB data.
833 static void
834 hexdump_record(const void *ptr, int length, const char *hdr)
836 int data_len = length;
838 if (data_len > HAMMER_BUFSIZE) /* XXX */
839 data_len = HAMMER_BUFSIZE;
840 hexdump(ptr, data_len, hdr, 0);
842 if (length > data_len)
843 printf("%s....\n", hdr);
846 static __inline __always_inline
847 unsigned long
848 _strtoul(const char *p, int base)
850 unsigned long retval;
852 errno = 0; /* clear */
853 retval = strtoul(p, NULL, base);
854 if (errno == ERANGE && retval == ULONG_MAX)
855 err(1, "strtoul");
856 return retval;
859 static __inline __always_inline
860 unsigned long long
861 _strtoull(const char *p, int base)
863 unsigned long long retval;
865 errno = 0; /* clear */
866 retval = strtoull(p, NULL, base);
867 if (errno == ERANGE && retval == ULLONG_MAX)
868 err(1, "strtoull");
869 return retval;
872 static int
873 init_btree_search(const char *arg)
875 char *s, *p;
876 int i = 0;
878 bzero(&opt.base, sizeof(opt.base));
879 opt.limit = 0;
881 if (arg == NULL)
882 return(-1);
883 if (strcmp(arg, "none") == 0)
884 return(-1);
886 s = strdup(arg);
887 if (s == NULL)
888 return(-1);
890 while ((p = s) != NULL) {
891 if ((s = strchr(s, ':')) != NULL)
892 *s++ = 0;
893 if (++i == 1) {
894 opt.base.localization = _strtoul(p, 16);
895 } else if (i == 2) {
896 opt.base.obj_id = _strtoull(p, 16);
897 } else if (i == 3) {
898 opt.base.rec_type = _strtoul(p, 16);
899 } else if (i == 4) {
900 opt.base.key = _strtoull(p, 16);
901 } else if (i == 5) {
902 opt.base.create_tid = _strtoull(p, 16);
903 break;
906 opt.limit = i;
907 free(s);
909 return(i);
912 static int
913 test_btree_search(hammer_btree_elm_t elm)
915 hammer_base_elm_t base1 = &elm->base;
916 hammer_base_elm_t base2 = &opt.base;
917 int limit = opt.limit;
919 if (base1->localization < base2->localization)
920 return(-5);
921 if (base1->localization > base2->localization)
922 return(5);
923 if (limit == 1)
924 return(0); /* ignore below */
926 if (base1->obj_id < base2->obj_id)
927 return(-4);
928 if (base1->obj_id > base2->obj_id)
929 return(4);
930 if (limit == 2)
931 return(0); /* ignore below */
933 if (base1->rec_type < base2->rec_type)
934 return(-3);
935 if (base1->rec_type > base2->rec_type)
936 return(3);
937 if (limit == 3)
938 return(0); /* ignore below */
940 if (base1->key < base2->key)
941 return(-2);
942 if (base1->key > base2->key)
943 return(2);
944 if (limit == 4)
945 return(0); /* ignore below */
947 if (base1->create_tid == 0) {
948 if (base2->create_tid == 0)
949 return(0);
950 return(1);
952 if (base2->create_tid == 0)
953 return(-1);
954 if (base1->create_tid < base2->create_tid)
955 return(-1);
956 if (base1->create_tid > base2->create_tid)
957 return(1);
958 return(0);
961 static __inline
963 test_btree_match(hammer_btree_elm_t elm)
965 if (test_btree_search(elm) == 0)
966 return(1);
967 return(0);
970 static
972 test_btree_out_of_range(hammer_btree_elm_t elm)
974 if (test_btree_search(elm) > 0)
975 return(1); /* conditions < this elm */
977 if (opt.limit >= 5) {
978 if (test_btree_search(elm + 1) <= 0)
979 return(1); /* next elm <= conditions */
980 } else {
981 if (test_btree_search(elm + 1) < 0)
982 return(1); /* next elm < conditions */
984 return(0);
988 * Dump the UNDO FIFO
990 void
991 hammer_cmd_show_undo(void)
993 struct volume_info *volume;
994 hammer_blockmap_t rootmap;
995 hammer_off_t scan_offset;
996 hammer_fifo_any_t head;
997 hammer_fifo_head_t hdr;
998 struct buffer_info *data_buffer = NULL;
999 struct zone_stat *stats = NULL;
1001 volume = get_root_volume();
1002 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
1004 print_blockmap(volume);
1006 if (VerboseOpt)
1007 stats = hammer_init_zone_stat_bits();
1009 scan_offset = HAMMER_ENCODE_UNDO(0);
1010 while (scan_offset < rootmap->alloc_offset) {
1011 head = get_buffer_data(scan_offset, &data_buffer, 0);
1012 hdr = &head->head;
1013 printf("%016jx ", scan_offset);
1015 switch(hdr->hdr_type) {
1016 case HAMMER_HEAD_TYPE_PAD:
1017 printf("PAD(%d)", hdr->hdr_size);
1018 break;
1019 case HAMMER_HEAD_TYPE_DUMMY:
1020 printf("DUMMY(%d)\tseq=%08x",
1021 hdr->hdr_size, hdr->hdr_seq);
1022 break;
1023 case HAMMER_HEAD_TYPE_UNDO:
1024 printf("UNDO(%d)\tseq=%08x dataoff=%016jx bytes=%d",
1025 hdr->hdr_size, hdr->hdr_seq,
1026 (intmax_t)head->undo.undo_offset,
1027 head->undo.undo_data_bytes);
1028 break;
1029 case HAMMER_HEAD_TYPE_REDO:
1030 printf("REDO(%d)\tseq=%08x flags=%08x "
1031 "objid=%016jx logoff=%016jx bytes=%d",
1032 hdr->hdr_size, hdr->hdr_seq,
1033 head->redo.redo_flags,
1034 (intmax_t)head->redo.redo_objid,
1035 (intmax_t)head->redo.redo_offset,
1036 head->redo.redo_data_bytes);
1037 break;
1038 default:
1039 printf("%04x(%d)\tseq=%08x",
1040 hdr->hdr_type, hdr->hdr_size, hdr->hdr_seq);
1041 break;
1044 if (scan_offset == rootmap->first_offset)
1045 printf(" >");
1046 if (scan_offset == rootmap->next_offset)
1047 printf(" <");
1048 printf("\n");
1050 if (stats)
1051 hammer_add_zone_stat(stats, scan_offset, hdr->hdr_size);
1053 if ((hdr->hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
1054 hdr->hdr_size == 0 ||
1055 hdr->hdr_size > HAMMER_UNDO_ALIGN -
1056 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
1057 printf("Illegal size field, skipping to "
1058 "next boundary\n");
1059 scan_offset = HAMMER_UNDO_DOALIGN(scan_offset);
1060 } else {
1061 scan_offset += hdr->hdr_size;
1064 rel_buffer(data_buffer);
1066 if (stats) {
1067 hammer_print_zone_stat(stats);
1068 hammer_cleanup_zone_stat(stats);