sbin/*hammer: Use typedef'd for struct buffer_info*
[dragonfly.git] / sbin / hammer / cmd_show.c
blob83246fde9bf116de431ee510afe29861419d3b49
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, const char **whichp);
67 static hammer_crc_t get_leaf_crc(uint32_t vol_version, void *data,
68 hammer_btree_leaf_elm_t leaf, const char **whichp);
69 static uint32_t get_buf_crc(hammer_off_t buf_offset, int32_t buf_len,
70 uint32_t leaf_crc, const char **whichp);
71 static void print_record(hammer_btree_elm_t elm);
72 static int init_btree_search(const char *arg);
73 static int test_btree_search(hammer_btree_elm_t elm);
74 static int test_btree_match(hammer_btree_elm_t elm);
75 static int test_btree_out_of_range(hammer_btree_elm_t elm);
76 static void hexdump_record(const void *ptr, int length, const char *hdr);
78 static int num_bad_node = 0;
79 static int num_bad_elm = 0;
80 static int num_bad_rec = 0;
81 static int depth;
83 #define _X "\t"
84 static const char* _indents[] = {
85 "",
86 _X,
87 _X _X,
88 _X _X _X,
89 _X _X _X _X,
90 _X _X _X _X _X,
91 _X _X _X _X _X _X,
92 _X _X _X _X _X _X _X,
93 _X _X _X _X _X _X _X _X,
94 _X _X _X _X _X _X _X _X _X,
95 _X _X _X _X _X _X _X _X _X _X,
96 _X _X _X _X _X _X _X _X _X _X _X,
97 _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,
99 _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
100 _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
101 _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
102 /* deep enough */
104 #define INDENT _indents[opt.indent ? depth : 0]
106 void
107 hammer_cmd_show(const char *arg, int filter, int obfuscate, int indent)
109 volume_info_t volume;
110 hammer_volume_ondisk_t ondisk;
111 struct zone_stat *stats = NULL;
113 volume = get_root_volume();
114 ondisk = volume->ondisk;
116 print_blockmap(volume);
118 if (VerboseOpt)
119 stats = hammer_init_zone_stat_bits();
121 bzero(&opt, sizeof(opt));
122 opt.filter = filter;
123 opt.obfuscate = obfuscate;
124 opt.indent = indent;
125 opt.stats = stats;
127 if (init_btree_search(arg) > 0) {
128 printf("arg=\"%s\"", arg);
129 if (opt.limit > 0)
130 printf(" lo=%08x", opt.base.localization);
131 if (opt.limit > 1)
132 printf(" objid=%016jx", (uintmax_t)opt.base.obj_id);
133 if (opt.limit > 2)
134 printf(" rt=%02x", opt.base.rec_type);
135 if (opt.limit > 3)
136 printf(" key=%016jx", (uintmax_t)opt.base.key);
137 if (opt.limit > 4)
138 printf(" tid=%016jx", (uintmax_t)opt.base.create_tid);
139 printf("\n");
141 print_btree(ondisk->vol0_btree_root);
143 if (stats) {
144 hammer_print_zone_stat(stats);
145 hammer_cleanup_zone_stat(stats);
148 if (num_bad_node || VerboseOpt)
149 printf("%d bad nodes\n", num_bad_node);
150 if (num_bad_elm || VerboseOpt)
151 printf("%d bad elms\n", num_bad_elm);
152 if (num_bad_rec || VerboseOpt)
153 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 buffer_info_t 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(HammerVersion, 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 buffer_info_t 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 const char *which;
344 int flags;
345 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
347 flags = get_elm_flags(node, node_offset, elm, lbe);
348 flagstr[0] = flags ? 'B' : 'G';
349 if (flags & FLAG_TOOFARLEFT)
350 flagstr[2] = 'L';
351 if (flags & FLAG_TOOFARRIGHT)
352 flagstr[3] = 'R';
353 if (flags & FLAG_BADTYPE)
354 flagstr[4] = 'T';
355 if (flags & FLAG_BADCHILDPARENT)
356 flagstr[5] = 'C';
357 if (flags & FLAG_BADMIRRORTID)
358 flagstr[6] = 'M';
359 if (flagstr[0] == 'B')
360 ++num_bad_elm;
363 * Check if elm is derived from root split
365 if (is_root_btree_beg(node->type, i, elm))
366 rootelm = '>';
367 else if (is_root_btree_end(node->type, i, elm))
368 rootelm = '<';
369 else
370 rootelm = ' ';
372 if (elm->base.delete_tid)
373 deleted = 'd';
374 else
375 deleted = ' ';
377 if (node->type == HAMMER_BTREE_TYPE_INTERNAL && node->count == i)
378 label = "RBN";
379 else
380 label = "ELM";
382 printf("%s%s %s %2d %c ",
383 INDENT, flagstr, label, i, hammer_elm_btype(elm));
384 printf("lo=%08x objid=%016jx rt=%02x key=%016jx tid=%016jx\n",
385 elm->base.localization,
386 (uintmax_t)elm->base.obj_id,
387 elm->base.rec_type,
388 (uintmax_t)elm->base.key,
389 (uintmax_t)elm->base.create_tid);
390 printf("%s %c del=%016jx ot=%02x",
391 INDENT,
392 (rootelm == ' ' ? deleted : rootelm),
393 (uintmax_t)elm->base.delete_tid,
394 elm->base.obj_type);
396 switch(node->type) {
397 case HAMMER_BTREE_TYPE_INTERNAL:
398 printf(" suboff=%016jx mirror=%016jx",
399 (uintmax_t)elm->internal.subtree_offset,
400 (uintmax_t)elm->internal.mirror_tid);
401 if (ext)
402 printf(" %s", ext);
403 break;
404 case HAMMER_BTREE_TYPE_LEAF:
405 switch(elm->base.btype) {
406 case HAMMER_BTREE_TYPE_RECORD:
407 printf(" dataoff=%016jx/%d",
408 (uintmax_t)elm->leaf.data_offset,
409 elm->leaf.data_len);
410 p = check_data_crc(elm, &which);
411 printf(" %scrc=%08x", which, elm->leaf.data_crc);
412 if (p) {
413 printf(" error=%s", p);
414 ++num_bad_rec;
416 printf(" fill=");
417 print_bigblock_fill(elm->leaf.data_offset);
418 if (QuietOpt < 2)
419 print_record(elm);
420 if (opt.stats)
421 hammer_add_zone_stat(opt.stats,
422 elm->leaf.data_offset,
423 elm->leaf.data_len);
424 break;
425 default:
426 printf(" badtype=%d", elm->base.btype);
427 break;
429 if (ext)
430 printf(" %s", ext);
431 break;
433 printf("\n");
436 static
438 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
439 hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
441 hammer_off_t child_offset;
442 int flags = 0;
443 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
445 switch(node->type) {
446 case HAMMER_BTREE_TYPE_INTERNAL:
447 child_offset = elm->internal.subtree_offset;
448 if (elm->internal.mirror_tid > node->mirror_tid)
449 flags |= FLAG_BADMIRRORTID;
451 if (i == node->count) {
452 if (child_offset != 0)
453 flags |= FLAG_BADCHILDPARENT;
454 switch(elm->base.btype) {
455 case HAMMER_BTREE_TYPE_NONE:
456 flags |= test_rbn_lr(elm, lbe);
457 break;
458 default:
459 flags |= FLAG_BADTYPE;
460 break;
462 } else {
463 if (child_offset == 0) {
464 flags |= FLAG_BADCHILDPARENT;
465 } else {
466 buffer_info_t buffer = NULL;
467 hammer_node_ondisk_t subnode;
468 subnode = get_buffer_data(child_offset, &buffer, 0);
469 if (subnode == NULL)
470 flags |= FLAG_BADCHILDPARENT;
471 else if (subnode->parent != node_offset)
472 flags |= FLAG_BADCHILDPARENT;
473 rel_buffer(buffer);
475 switch(elm->base.btype) {
476 case HAMMER_BTREE_TYPE_INTERNAL:
477 case HAMMER_BTREE_TYPE_LEAF:
478 flags |= test_lr(elm, lbe);
479 break;
480 default:
481 flags |= FLAG_BADTYPE;
482 break;
485 break;
486 case HAMMER_BTREE_TYPE_LEAF:
487 if (elm->leaf.data_offset == 0)
488 flags |= FLAG_BADCHILDPARENT;
489 if (elm->leaf.data_len == 0)
490 flags |= FLAG_BADCHILDPARENT;
492 if (node->mirror_tid == 0 &&
493 !(node->parent == 0 && node->count == 2)) {
494 flags |= FLAG_BADMIRRORTID;
496 if (elm->base.create_tid && node->mirror_tid &&
497 elm->base.create_tid > node->mirror_tid) {
498 flags |= FLAG_BADMIRRORTID;
500 if (elm->base.delete_tid && node->mirror_tid &&
501 elm->base.delete_tid > node->mirror_tid) {
502 flags |= FLAG_BADMIRRORTID;
504 switch(elm->base.btype) {
505 case HAMMER_BTREE_TYPE_RECORD:
506 flags |= test_lr(elm, lbe);
507 break;
508 default:
509 flags |= FLAG_BADTYPE;
510 break;
512 break;
513 default:
514 flags |= FLAG_BADTYPE;
515 break;
517 return(flags);
521 * Taken from /usr/src/sys/vfs/hammer/hammer_btree.c.
523 static
525 hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
527 if (key1->localization < key2->localization)
528 return(-5);
529 if (key1->localization > key2->localization)
530 return(5);
532 if (key1->obj_id < key2->obj_id)
533 return(-4);
534 if (key1->obj_id > key2->obj_id)
535 return(4);
537 if (key1->rec_type < key2->rec_type)
538 return(-3);
539 if (key1->rec_type > key2->rec_type)
540 return(3);
542 if (key1->key < key2->key)
543 return(-2);
544 if (key1->key > key2->key)
545 return(2);
547 if (key1->create_tid == 0) {
548 if (key2->create_tid == 0)
549 return(0);
550 return(1);
552 if (key2->create_tid == 0)
553 return(-1);
554 if (key1->create_tid < key2->create_tid)
555 return(-1);
556 if (key1->create_tid > key2->create_tid)
557 return(1);
558 return(0);
561 static
563 test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
565 if (lbe) {
566 hammer_btree_elm_t rbe = lbe + 1;
567 if (hammer_btree_cmp(&elm->base, &lbe->base) < 0)
568 return(FLAG_TOOFARLEFT);
569 if (hammer_btree_cmp(&elm->base, &rbe->base) >= 0)
570 return(FLAG_TOOFARRIGHT);
572 return(0);
575 static
577 test_rbn_lr(hammer_btree_elm_t rbn, hammer_btree_elm_t lbe)
579 if (lbe) {
580 hammer_btree_elm_t rbe = lbe + 1;
581 if (hammer_btree_cmp(&rbn->base, &lbe->base) < 0)
582 return(FLAG_TOOFARLEFT);
583 if (hammer_btree_cmp(&rbn->base, &rbe->base) > 0)
584 return(FLAG_TOOFARRIGHT);
586 return(0);
589 static
590 void
591 print_bigblock_fill(hammer_off_t offset)
593 struct hammer_blockmap_layer1 layer1;
594 struct hammer_blockmap_layer2 layer2;
595 int fill;
596 int error;
598 blockmap_lookup_save(offset, &layer1, &layer2, &error);
599 printf("z%d:v%d:%d:%d:%lu=",
600 HAMMER_ZONE_DECODE(offset),
601 HAMMER_VOL_DECODE(offset),
602 HAMMER_BLOCKMAP_LAYER1_INDEX(offset),
603 HAMMER_BLOCKMAP_LAYER2_INDEX(offset),
604 offset & HAMMER_BIGBLOCK_MASK64);
606 if (error) {
607 printf("B%d", error);
608 } else {
609 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
610 printf("%d%%", 100 - fill);
615 * Check the generic crc on a data element. Inodes record types are
616 * special in that some of their fields are not CRCed.
618 * Also check that the zone is valid.
620 static
621 const char *
622 check_data_crc(hammer_btree_elm_t elm, const char **whichp)
624 buffer_info_t data_buffer;
625 hammer_off_t data_offset;
626 int32_t data_len;
627 uint32_t crc;
628 char *ptr;
630 *whichp = "";
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 = get_leaf_crc(HammerVersion, ptr, &elm->leaf, whichp);
644 rel_buffer(data_buffer);
645 break;
646 default:
647 crc = get_buf_crc(data_offset, data_len, elm->leaf.data_crc,
648 whichp);
649 break;
652 if (crc == 0)
653 return("Bx"); /* bad crc */
654 if (crc != elm->leaf.data_crc)
655 return("BX"); /* bad crc */
656 return(NULL); /* success */
659 static
660 uint32_t
661 get_buf_crc(hammer_off_t buf_offset, int32_t buf_len, uint32_t leaf_crc,
662 const char **whichp)
664 buffer_info_t data_buffer = NULL;
665 int32_t len;
666 uint32_t crc = 0;
667 uint32_t ncrc = 0;
668 char *ptr;
670 while (buf_len) {
671 ptr = get_buffer_data(buf_offset, &data_buffer, 0);
672 len = HAMMER_BUFSIZE - ((int)buf_offset & HAMMER_BUFMASK);
673 if (len > buf_len)
674 len = (int)buf_len;
675 assert(len <= HAMMER_BUFSIZE);
676 crc = crc32_ext(ptr, len, crc);
677 ncrc = iscsi_crc32_ext(ptr, len, ncrc);
678 buf_len -= len;
679 buf_offset += len;
681 rel_buffer(data_buffer);
683 if (leaf_crc == crc) {
684 *whichp = "o";
685 return crc;
687 *whichp = "i";
688 return ncrc;
691 static hammer_crc_t
692 get_leaf_crc(uint32_t vol_version, void *data, hammer_btree_leaf_elm_t leaf,
693 const char **whichp)
695 hammer_crc_t crc;
697 *whichp = "";
698 if (leaf->data_len == 0)
699 return(0);
701 switch(leaf->base.rec_type) {
702 case HAMMER_RECTYPE_INODE:
703 if (leaf->data_len != sizeof(struct hammer_inode_data))
704 return(0); /* This shouldn't happen */
705 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
706 crc = iscsi_crc32(data, HAMMER_INODE_CRCSIZE);
707 if (crc == leaf->data_crc) {
708 *whichp = "i";
709 break;
712 crc = crc32(data, HAMMER_INODE_CRCSIZE);
713 if (crc == leaf->data_crc)
714 *whichp = "o";
715 break;
716 default:
717 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
718 crc = iscsi_crc32(data, leaf->data_len);
719 if (crc == leaf->data_crc) {
720 *whichp = "i";
721 break;
724 crc = crc32(data, leaf->data_len);
725 if (crc == leaf->data_crc)
726 *whichp = "o";
727 break;
729 return(crc);
732 static
733 void
734 print_config(char *cfgtxt)
736 char *token;
738 printf("\n%s%17s", INDENT, "");
739 printf("config text=\"\n");
740 if (cfgtxt != NULL) {
741 while((token = strsep(&cfgtxt, "\r\n")) != NULL)
742 if (strlen(token))
743 printf("%s%17s %s\n",
744 INDENT, "", token);
746 printf("%s%17s \"", INDENT, "");
749 static
750 void
751 print_record(hammer_btree_elm_t elm)
753 buffer_info_t data_buffer;
754 hammer_off_t data_offset;
755 int32_t data_len;
756 hammer_data_ondisk_t data;
757 uint32_t status;
758 char *str1 = NULL;
759 char *str2 = NULL;
761 data_offset = elm->leaf.data_offset;
762 data_len = elm->leaf.data_len;
763 assert(data_offset != 0);
764 assert(data_len != 0);
766 data_buffer = NULL;
767 data = get_buffer_data(data_offset, &data_buffer, 0);
768 assert(data != NULL);
770 switch(elm->leaf.base.rec_type) {
771 case HAMMER_RECTYPE_UNKNOWN:
772 printf("\n%s%17s", INDENT, "");
773 printf("unknown");
774 break;
775 case HAMMER_RECTYPE_INODE:
776 printf("\n%s%17s", INDENT, "");
777 printf("inode size=%jd nlinks=%jd",
778 (intmax_t)data->inode.size,
779 (intmax_t)data->inode.nlinks);
780 printf(" mode=%05o uflags=%08x caps=%02x",
781 data->inode.mode,
782 data->inode.uflags,
783 data->inode.cap_flags);
784 printf(" pobjid=%016jx ot=%02x\n",
785 (uintmax_t)data->inode.parent_obj_id,
786 data->inode.obj_type);
787 printf("%s%17s", INDENT, "");
788 printf(" ctime=%016jx mtime=%016jx atime=%016jx",
789 (uintmax_t)data->inode.ctime,
790 (uintmax_t)data->inode.mtime,
791 (uintmax_t)data->inode.atime);
792 if (data->inode.ext.symlink[0])
793 printf(" symlink=\"%s\"",
794 data->inode.ext.symlink);
795 break;
796 case HAMMER_RECTYPE_DIRENTRY:
797 data_len -= HAMMER_ENTRY_NAME_OFF;
798 printf("\n%s%17s", INDENT, "");
799 printf("dir-entry objid=%016jx lo=%08x",
800 (uintmax_t)data->entry.obj_id,
801 data->entry.localization);
802 if (!opt.obfuscate)
803 printf(" name=\"%*.*s\"",
804 data_len, data_len, data->entry.name);
805 break;
806 case HAMMER_RECTYPE_FIX:
807 switch(elm->leaf.base.key) {
808 case HAMMER_FIXKEY_SYMLINK:
809 data_len -= HAMMER_SYMLINK_NAME_OFF;
810 printf("\n%s%17s", INDENT, "");
811 printf("fix-symlink name=\"%*.*s\"",
812 data_len, data_len, data->symlink.name);
813 break;
815 break;
816 case HAMMER_RECTYPE_PFS:
817 printf("\n%s%17s", INDENT, "");
818 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
819 (intmax_t)data->pfsd.sync_beg_tid,
820 (intmax_t)data->pfsd.sync_end_tid);
821 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
822 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
823 printf("%17s", "");
824 printf(" shared_uuid=%s\n", str1);
825 printf("%17s", "");
826 printf(" unique_uuid=%s\n", str2);
827 printf("%17s", "");
828 printf(" mirror_flags=%08x label=\"%s\"",
829 data->pfsd.mirror_flags, data->pfsd.label);
830 if (data->pfsd.snapshots[0])
831 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
832 free(str1);
833 free(str2);
834 break;
835 case HAMMER_RECTYPE_SNAPSHOT:
836 printf("\n%s%17s", INDENT, "");
837 printf("snapshot tid=%016jx label=\"%s\"",
838 (intmax_t)data->snap.tid, data->snap.label);
839 break;
840 case HAMMER_RECTYPE_CONFIG:
841 if (VerboseOpt > 2) {
842 char *p = strdup(data->config.text);
843 print_config(p);
844 free(p);
846 break;
847 case HAMMER_RECTYPE_DATA:
848 if (VerboseOpt > 3) {
849 printf("\n");
850 hexdump_record(data, data_len, "\t\t ");
852 break;
853 case HAMMER_RECTYPE_EXT:
854 case HAMMER_RECTYPE_DB:
855 if (VerboseOpt > 2) {
856 printf("\n");
857 hexdump_record(data, data_len, "\t\t ");
859 break;
860 default:
861 assert(0);
862 break;
864 rel_buffer(data_buffer);
868 * HAMMER userspace only supports buffer size upto HAMMER_BUFSIZE
869 * which is 16KB. Passing record data length larger than 16KB to
870 * hexdump(3) is invalid even if the leaf node elm says >16KB data.
872 static void
873 hexdump_record(const void *ptr, int length, const char *hdr)
875 int data_len = length;
877 if (data_len > HAMMER_BUFSIZE) /* XXX */
878 data_len = HAMMER_BUFSIZE;
879 hexdump(ptr, data_len, hdr, 0);
881 if (length > data_len)
882 printf("%s....\n", hdr);
885 static __inline __always_inline
886 unsigned long
887 _strtoul(const char *p, int base)
889 unsigned long retval;
891 errno = 0; /* clear */
892 retval = strtoul(p, NULL, base);
893 if (errno == ERANGE && retval == ULONG_MAX) {
894 err(1, "strtoul");
895 /* not reached */
897 return retval;
900 static __inline __always_inline
901 unsigned long long
902 _strtoull(const char *p, int base)
904 unsigned long long retval;
906 errno = 0; /* clear */
907 retval = strtoull(p, NULL, base);
908 if (errno == ERANGE && retval == ULLONG_MAX) {
909 err(1, "strtoull");
910 /* not reached */
912 return retval;
915 static int
916 init_btree_search(const char *arg)
918 char *s, *p;
919 int i = 0;
921 bzero(&opt.base, sizeof(opt.base));
922 opt.limit = 0;
924 if (arg == NULL)
925 return(-1);
926 if (strcmp(arg, "none") == 0)
927 return(-1);
929 s = strdup(arg);
930 if (s == NULL)
931 return(-1);
933 while ((p = s) != NULL) {
934 if ((s = strchr(s, ':')) != NULL)
935 *s++ = 0;
936 if (++i == 1) {
937 opt.base.localization = _strtoul(p, 16);
938 } else if (i == 2) {
939 opt.base.obj_id = _strtoull(p, 16);
940 } else if (i == 3) {
941 opt.base.rec_type = _strtoul(p, 16);
942 } else if (i == 4) {
943 opt.base.key = _strtoull(p, 16);
944 } else if (i == 5) {
945 opt.base.create_tid = _strtoull(p, 16);
946 break;
949 opt.limit = i;
950 free(s);
952 return(i);
955 static int
956 test_btree_search(hammer_btree_elm_t elm)
958 hammer_base_elm_t base1 = &elm->base;
959 hammer_base_elm_t base2 = &opt.base;
960 int limit = opt.limit;
962 if (base1->localization < base2->localization)
963 return(-5);
964 if (base1->localization > base2->localization)
965 return(5);
966 if (limit == 1)
967 return(0); /* ignore below */
969 if (base1->obj_id < base2->obj_id)
970 return(-4);
971 if (base1->obj_id > base2->obj_id)
972 return(4);
973 if (limit == 2)
974 return(0); /* ignore below */
976 if (base1->rec_type < base2->rec_type)
977 return(-3);
978 if (base1->rec_type > base2->rec_type)
979 return(3);
980 if (limit == 3)
981 return(0); /* ignore below */
983 if (base1->key < base2->key)
984 return(-2);
985 if (base1->key > base2->key)
986 return(2);
987 if (limit == 4)
988 return(0); /* ignore below */
990 if (base1->create_tid == 0) {
991 if (base2->create_tid == 0)
992 return(0);
993 return(1);
995 if (base2->create_tid == 0)
996 return(-1);
997 if (base1->create_tid < base2->create_tid)
998 return(-1);
999 if (base1->create_tid > base2->create_tid)
1000 return(1);
1001 return(0);
1004 static __inline
1006 test_btree_match(hammer_btree_elm_t elm)
1008 if (test_btree_search(elm) == 0)
1009 return(1);
1010 return(0);
1013 static
1015 test_btree_out_of_range(hammer_btree_elm_t elm)
1017 if (test_btree_search(elm) > 0)
1018 return(1); /* conditions < this elm */
1020 if (opt.limit >= 5) {
1021 if (test_btree_search(elm + 1) <= 0)
1022 return(1); /* next elm <= conditions */
1023 } else {
1024 if (test_btree_search(elm + 1) < 0)
1025 return(1); /* next elm < conditions */
1027 return(0);
1031 * Dump the UNDO FIFO
1033 void
1034 hammer_cmd_show_undo(void)
1036 volume_info_t volume;
1037 hammer_blockmap_t rootmap;
1038 hammer_off_t scan_offset;
1039 hammer_fifo_any_t head;
1040 hammer_fifo_head_t hdr;
1041 buffer_info_t data_buffer = NULL;
1042 struct zone_stat *stats = NULL;
1044 volume = get_root_volume();
1045 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
1047 print_blockmap(volume);
1049 if (VerboseOpt)
1050 stats = hammer_init_zone_stat_bits();
1052 scan_offset = HAMMER_ENCODE_UNDO(0);
1053 while (scan_offset < rootmap->alloc_offset) {
1054 head = get_buffer_data(scan_offset, &data_buffer, 0);
1055 hdr = &head->head;
1056 printf("%016jx ", scan_offset);
1058 switch(hdr->hdr_type) {
1059 case HAMMER_HEAD_TYPE_PAD:
1060 printf("PAD(%d)", hdr->hdr_size);
1061 break;
1062 case HAMMER_HEAD_TYPE_DUMMY:
1063 printf("DUMMY(%d)\tseq=%08x",
1064 hdr->hdr_size, hdr->hdr_seq);
1065 break;
1066 case HAMMER_HEAD_TYPE_UNDO:
1067 printf("UNDO(%d)\tseq=%08x offset=%016jx bytes=%d",
1068 hdr->hdr_size, hdr->hdr_seq,
1069 (intmax_t)head->undo.undo_offset,
1070 head->undo.undo_data_bytes);
1071 break;
1072 case HAMMER_HEAD_TYPE_REDO:
1073 printf("REDO(%d)\tseq=%08x offset=%016jx bytes=%d "
1074 "objid=%016jx flags=%08x lo=%08x",
1075 hdr->hdr_size, hdr->hdr_seq,
1076 (intmax_t)head->redo.redo_offset,
1077 head->redo.redo_data_bytes,
1078 (intmax_t)head->redo.redo_objid,
1079 head->redo.redo_flags,
1080 head->redo.redo_localization);
1081 break;
1082 default:
1083 printf("%04x(%d)\tseq=%08x",
1084 hdr->hdr_type, hdr->hdr_size, hdr->hdr_seq);
1085 break;
1088 if (scan_offset == rootmap->first_offset)
1089 printf(" >");
1090 if (scan_offset == rootmap->next_offset)
1091 printf(" <");
1092 printf("\n");
1094 if (stats)
1095 hammer_add_zone_stat(stats, scan_offset, hdr->hdr_size);
1097 if ((hdr->hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
1098 hdr->hdr_size == 0 ||
1099 hdr->hdr_size > HAMMER_UNDO_ALIGN -
1100 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
1101 printf("Illegal size field, skipping to "
1102 "next boundary\n");
1103 scan_offset = HAMMER_UNDO_DOALIGN(scan_offset);
1104 } else {
1105 scan_offset += hdr->hdr_size;
1108 rel_buffer(data_buffer);
1110 if (stats) {
1111 hammer_print_zone_stat(stats);
1112 hammer_cleanup_zone_stat(stats);