<sys/param.h>: Bump __DragonFly_version for binutils update.
[dragonfly.git] / sbin / hammer / cmd_show.c
blob0fadf28648e70f7248040765b68fbab3fdaa5e97
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_save(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 int32_t data_len;
628 uint32_t crc;
629 char *ptr;
631 data_offset = elm->leaf.data_offset;
632 data_len = elm->leaf.data_len;
633 data_buffer = NULL;
634 if (data_offset == 0 || data_len == 0)
635 return("ZO"); /* zero offset or length */
637 crc = 0;
638 switch (elm->leaf.base.rec_type) {
639 case HAMMER_RECTYPE_INODE:
640 if (data_len != sizeof(struct hammer_inode_data))
641 return("BI"); /* bad inode size */
642 ptr = get_buffer_data(data_offset, &data_buffer, 0);
643 crc = hammer_crc_get_leaf(ptr, &elm->leaf);
644 rel_buffer(data_buffer);
645 break;
646 default:
647 crc = get_buf_crc(data_offset, data_len);
648 break;
651 if (crc == 0)
652 return("Bx"); /* bad crc */
653 if (crc != elm->leaf.data_crc)
654 return("BX"); /* bad crc */
655 return(NULL); /* success */
658 static
659 uint32_t
660 get_buf_crc(hammer_off_t buf_offset, int32_t buf_len)
662 struct buffer_info *data_buffer = NULL;
663 int32_t len;
664 uint32_t crc = 0;
665 char *ptr;
667 while (buf_len) {
668 ptr = get_buffer_data(buf_offset, &data_buffer, 0);
669 len = HAMMER_BUFSIZE - ((int)buf_offset & HAMMER_BUFMASK);
670 if (len > buf_len)
671 len = (int)buf_len;
672 assert(len <= HAMMER_BUFSIZE);
673 crc = crc32_ext(ptr, len, crc);
674 buf_len -= len;
675 buf_offset += len;
677 rel_buffer(data_buffer);
679 return(crc);
682 static
683 void
684 print_config(char *cfgtxt)
686 char *token;
688 printf("\n%s%17s", INDENT, "");
689 printf("config text=\"\n");
690 if (cfgtxt != NULL) {
691 while((token = strsep(&cfgtxt, "\r\n")) != NULL)
692 if (strlen(token))
693 printf("%s%17s %s\n",
694 INDENT, "", token);
696 printf("%s%17s \"", INDENT, "");
699 static
700 void
701 print_record(hammer_btree_elm_t elm)
703 struct buffer_info *data_buffer;
704 hammer_off_t data_offset;
705 int32_t data_len;
706 hammer_data_ondisk_t data;
707 uint32_t status;
708 char *str1 = NULL;
709 char *str2 = NULL;
711 data_offset = elm->leaf.data_offset;
712 data_len = elm->leaf.data_len;
713 assert(data_offset != 0);
714 assert(data_len != 0);
716 data_buffer = NULL;
717 data = get_buffer_data(data_offset, &data_buffer, 0);
718 assert(data != NULL);
720 switch(elm->leaf.base.rec_type) {
721 case HAMMER_RECTYPE_UNKNOWN:
722 printf("\n%s%17s", INDENT, "");
723 printf("unknown");
724 break;
725 case HAMMER_RECTYPE_INODE:
726 printf("\n%s%17s", INDENT, "");
727 printf("inode size=%jd nlinks=%jd",
728 (intmax_t)data->inode.size,
729 (intmax_t)data->inode.nlinks);
730 printf(" mode=%05o uflags=%08x caps=%02x",
731 data->inode.mode,
732 data->inode.uflags,
733 data->inode.cap_flags);
734 printf(" pobjid=%016jx ot=%02x\n",
735 (uintmax_t)data->inode.parent_obj_id,
736 data->inode.obj_type);
737 printf("%s%17s", INDENT, "");
738 printf(" ctime=%016jx mtime=%016jx atime=%016jx",
739 (uintmax_t)data->inode.ctime,
740 (uintmax_t)data->inode.mtime,
741 (uintmax_t)data->inode.atime);
742 if (data->inode.ext.symlink[0])
743 printf(" symlink=\"%s\"",
744 data->inode.ext.symlink);
745 break;
746 case HAMMER_RECTYPE_DIRENTRY:
747 data_len -= HAMMER_ENTRY_NAME_OFF;
748 printf("\n%s%17s", INDENT, "");
749 printf("dir-entry objid=%016jx lo=%08x",
750 (uintmax_t)data->entry.obj_id,
751 data->entry.localization);
752 if (!opt.obfuscate)
753 printf(" name=\"%*.*s\"",
754 data_len, data_len, data->entry.name);
755 break;
756 case HAMMER_RECTYPE_FIX:
757 switch(elm->leaf.base.key) {
758 case HAMMER_FIXKEY_SYMLINK:
759 data_len -= HAMMER_SYMLINK_NAME_OFF;
760 printf("\n%s%17s", INDENT, "");
761 printf("fix-symlink name=\"%*.*s\"",
762 data_len, data_len, data->symlink.name);
763 break;
765 break;
766 case HAMMER_RECTYPE_PFS:
767 printf("\n%s%17s", INDENT, "");
768 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
769 (intmax_t)data->pfsd.sync_beg_tid,
770 (intmax_t)data->pfsd.sync_end_tid);
771 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
772 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
773 printf("%17s", "");
774 printf(" shared_uuid=%s\n", str1);
775 printf("%17s", "");
776 printf(" unique_uuid=%s\n", str2);
777 printf("%17s", "");
778 printf(" mirror_flags=%08x label=\"%s\"",
779 data->pfsd.mirror_flags, data->pfsd.label);
780 if (data->pfsd.snapshots[0])
781 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
782 free(str1);
783 free(str2);
784 break;
785 case HAMMER_RECTYPE_SNAPSHOT:
786 printf("\n%s%17s", INDENT, "");
787 printf("snapshot tid=%016jx label=\"%s\"",
788 (intmax_t)data->snap.tid, data->snap.label);
789 break;
790 case HAMMER_RECTYPE_CONFIG:
791 if (VerboseOpt > 2) {
792 char *p = strdup(data->config.text);
793 print_config(p);
794 free(p);
796 break;
797 case HAMMER_RECTYPE_DATA:
798 if (VerboseOpt > 3) {
799 printf("\n");
800 hexdump_record(data, data_len, "\t\t ");
802 break;
803 case HAMMER_RECTYPE_EXT:
804 case HAMMER_RECTYPE_DB:
805 if (VerboseOpt > 2) {
806 printf("\n");
807 hexdump_record(data, data_len, "\t\t ");
809 break;
810 default:
811 assert(0);
812 break;
814 rel_buffer(data_buffer);
818 * HAMMER userspace only supports buffer size upto HAMMER_BUFSIZE
819 * which is 16KB. Passing record data length larger than 16KB to
820 * hexdump(3) is invalid even if the leaf node elm says >16KB data.
822 static void
823 hexdump_record(const void *ptr, int length, const char *hdr)
825 int data_len = length;
827 if (data_len > HAMMER_BUFSIZE) /* XXX */
828 data_len = HAMMER_BUFSIZE;
829 hexdump(ptr, data_len, hdr, 0);
831 if (length > data_len)
832 printf("%s....\n", hdr);
835 static __inline __always_inline
836 unsigned long
837 _strtoul(const char *p, int base)
839 unsigned long retval;
841 errno = 0; /* clear */
842 retval = strtoul(p, NULL, base);
843 if (errno == ERANGE && retval == ULONG_MAX)
844 err(1, "strtoul");
845 return retval;
848 static __inline __always_inline
849 unsigned long long
850 _strtoull(const char *p, int base)
852 unsigned long long retval;
854 errno = 0; /* clear */
855 retval = strtoull(p, NULL, base);
856 if (errno == ERANGE && retval == ULLONG_MAX)
857 err(1, "strtoull");
858 return retval;
861 static int
862 init_btree_search(const char *arg)
864 char *s, *p;
865 int i = 0;
867 bzero(&opt.base, sizeof(opt.base));
868 opt.limit = 0;
870 if (arg == NULL)
871 return(-1);
872 if (strcmp(arg, "none") == 0)
873 return(-1);
875 s = strdup(arg);
876 if (s == NULL)
877 return(-1);
879 while ((p = s) != NULL) {
880 if ((s = strchr(s, ':')) != NULL)
881 *s++ = 0;
882 if (++i == 1) {
883 opt.base.localization = _strtoul(p, 16);
884 } else if (i == 2) {
885 opt.base.obj_id = _strtoull(p, 16);
886 } else if (i == 3) {
887 opt.base.rec_type = _strtoul(p, 16);
888 } else if (i == 4) {
889 opt.base.key = _strtoull(p, 16);
890 } else if (i == 5) {
891 opt.base.create_tid = _strtoull(p, 16);
892 break;
895 opt.limit = i;
896 free(s);
898 return(i);
901 static int
902 test_btree_search(hammer_btree_elm_t elm)
904 hammer_base_elm_t base1 = &elm->base;
905 hammer_base_elm_t base2 = &opt.base;
906 int limit = opt.limit;
908 if (base1->localization < base2->localization)
909 return(-5);
910 if (base1->localization > base2->localization)
911 return(5);
912 if (limit == 1)
913 return(0); /* ignore below */
915 if (base1->obj_id < base2->obj_id)
916 return(-4);
917 if (base1->obj_id > base2->obj_id)
918 return(4);
919 if (limit == 2)
920 return(0); /* ignore below */
922 if (base1->rec_type < base2->rec_type)
923 return(-3);
924 if (base1->rec_type > base2->rec_type)
925 return(3);
926 if (limit == 3)
927 return(0); /* ignore below */
929 if (base1->key < base2->key)
930 return(-2);
931 if (base1->key > base2->key)
932 return(2);
933 if (limit == 4)
934 return(0); /* ignore below */
936 if (base1->create_tid == 0) {
937 if (base2->create_tid == 0)
938 return(0);
939 return(1);
941 if (base2->create_tid == 0)
942 return(-1);
943 if (base1->create_tid < base2->create_tid)
944 return(-1);
945 if (base1->create_tid > base2->create_tid)
946 return(1);
947 return(0);
950 static __inline
952 test_btree_match(hammer_btree_elm_t elm)
954 if (test_btree_search(elm) == 0)
955 return(1);
956 return(0);
959 static
961 test_btree_out_of_range(hammer_btree_elm_t elm)
963 if (test_btree_search(elm) > 0)
964 return(1); /* conditions < this elm */
966 if (opt.limit >= 5) {
967 if (test_btree_search(elm + 1) <= 0)
968 return(1); /* next elm <= conditions */
969 } else {
970 if (test_btree_search(elm + 1) < 0)
971 return(1); /* next elm < conditions */
973 return(0);
977 * Dump the UNDO FIFO
979 void
980 hammer_cmd_show_undo(void)
982 struct volume_info *volume;
983 hammer_blockmap_t rootmap;
984 hammer_off_t scan_offset;
985 hammer_fifo_any_t head;
986 hammer_fifo_head_t hdr;
987 struct buffer_info *data_buffer = NULL;
988 struct zone_stat *stats = NULL;
990 volume = get_root_volume();
991 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
993 print_blockmap(volume);
995 if (VerboseOpt)
996 stats = hammer_init_zone_stat_bits();
998 scan_offset = HAMMER_ENCODE_UNDO(0);
999 while (scan_offset < rootmap->alloc_offset) {
1000 head = get_buffer_data(scan_offset, &data_buffer, 0);
1001 hdr = &head->head;
1002 printf("%016jx ", scan_offset);
1004 switch(hdr->hdr_type) {
1005 case HAMMER_HEAD_TYPE_PAD:
1006 printf("PAD(%d)", hdr->hdr_size);
1007 break;
1008 case HAMMER_HEAD_TYPE_DUMMY:
1009 printf("DUMMY(%d)\tseq=%08x",
1010 hdr->hdr_size, hdr->hdr_seq);
1011 break;
1012 case HAMMER_HEAD_TYPE_UNDO:
1013 printf("UNDO(%d)\tseq=%08x offset=%016jx bytes=%d",
1014 hdr->hdr_size, hdr->hdr_seq,
1015 (intmax_t)head->undo.undo_offset,
1016 head->undo.undo_data_bytes);
1017 break;
1018 case HAMMER_HEAD_TYPE_REDO:
1019 printf("REDO(%d)\tseq=%08x offset=%016jx bytes=%d "
1020 "objid=%016jx flags=%08x lo=%08x",
1021 hdr->hdr_size, hdr->hdr_seq,
1022 (intmax_t)head->redo.redo_offset,
1023 head->redo.redo_data_bytes,
1024 (intmax_t)head->redo.redo_objid,
1025 head->redo.redo_flags,
1026 head->redo.redo_localization);
1027 break;
1028 default:
1029 printf("%04x(%d)\tseq=%08x",
1030 hdr->hdr_type, hdr->hdr_size, hdr->hdr_seq);
1031 break;
1034 if (scan_offset == rootmap->first_offset)
1035 printf(" >");
1036 if (scan_offset == rootmap->next_offset)
1037 printf(" <");
1038 printf("\n");
1040 if (stats)
1041 hammer_add_zone_stat(stats, scan_offset, hdr->hdr_size);
1043 if ((hdr->hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
1044 hdr->hdr_size == 0 ||
1045 hdr->hdr_size > HAMMER_UNDO_ALIGN -
1046 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
1047 printf("Illegal size field, skipping to "
1048 "next boundary\n");
1049 scan_offset = HAMMER_UNDO_DOALIGN(scan_offset);
1050 } else {
1051 scan_offset += hdr->hdr_size;
1054 rel_buffer(data_buffer);
1056 if (stats) {
1057 hammer_print_zone_stat(stats);
1058 hammer_cleanup_zone_stat(stats);