i386 removal, part 54/x: Remove perfmon(4) remains.
[dragonfly.git] / sbin / hammer / cmd_show.c
blob22b17af351c21705a12948e4efc79b1a04805f47
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,
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);
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;
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 */
112 #define INDENT _indents[opt.indent ? depth : 0]
114 void
115 hammer_cmd_show(const char *arg, int filter, int obfuscate, int indent)
117 struct volume_info *volume;
118 struct hammer_volume_ondisk *ondisk;
119 struct hammer_blockmap *blockmap;
120 struct zone_stat *stats = NULL;
121 int zone;
123 AssertOnFailure = (DebugOpt != 0);
125 if (VerboseOpt)
126 stats = hammer_init_zone_stat_bits();
128 volume = get_root_volume();
129 ondisk = volume->ondisk;
130 if (QuietOpt < 3) {
131 printf("Volume header\tnext_tid=%016jx\n",
132 (uintmax_t)ondisk->vol0_next_tid);
133 printf("\t\tbufoffset=%016jx\n",
134 (uintmax_t)ondisk->vol_buf_beg);
135 for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) {
136 blockmap = ondisk->vol0_blockmap + zone;
137 printf("\t\tzone %d\tnext_offset=%016jx\n",
138 zone, blockmap->next_offset);
142 bzero(&opt, sizeof(opt));
143 opt.filter = filter;
144 opt.obfuscate = obfuscate;
145 opt.indent = indent;
146 opt.stats = stats;
148 if (init_btree_search(arg) > 0) {
149 printf("arg=\"%s\"", arg);
150 if (opt.limit > 0)
151 printf(" lo=%08x", opt.base.localization);
152 if (opt.limit > 1)
153 printf(" obj=%016jx", (uintmax_t)opt.base.obj_id);
154 if (opt.limit > 2)
155 printf(" rt=%02x", opt.base.rec_type);
156 if (opt.limit > 3)
157 printf(" key=%016jx", (uintmax_t)opt.base.key);
158 if (opt.limit > 4)
159 printf(" tid=%016jx", (uintmax_t)opt.base.create_tid);
160 printf("\n");
162 print_btree(ondisk->vol0_btree_root);
164 if (VerboseOpt) {
165 hammer_print_zone_stat(stats);
166 hammer_cleanup_zone_stat(stats);
169 if (num_bad_node || VerboseOpt) {
170 printf("%d bad nodes\n", num_bad_node);
172 if (num_bad_elm || VerboseOpt) {
173 printf("%d bad elms\n", num_bad_elm);
175 if (num_bad_rec || VerboseOpt) {
176 printf("%d bad records\n", num_bad_rec);
180 static __inline
181 void
182 print_btree(hammer_off_t node_offset)
184 depth = -1;
185 print_btree_node(node_offset, HAMMER_MAX_TID, NULL, NULL);
186 assert(depth == -1);
189 static __inline
190 void
191 print_subtree(hammer_btree_elm_t elm)
193 print_btree_node(elm->internal.subtree_offset,
194 elm->internal.mirror_tid, &elm[0].base, &elm[1].base);
197 static void
198 print_btree_node(hammer_off_t node_offset,
199 hammer_tid_t mirror_tid,
200 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
202 struct buffer_info *buffer = NULL;
203 hammer_node_ondisk_t node;
204 hammer_btree_elm_t elm;
205 int i;
206 char badc = ' '; /* good */
207 char badm = ' '; /* good */
208 const char *ext;
210 depth++;
211 node = get_node(node_offset, &buffer);
213 if (node == NULL) {
214 badc = 'B';
215 badm = 'I';
216 } else {
217 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) != node->crc)
218 badc = 'B';
219 if (node->mirror_tid > mirror_tid) {
220 badc = 'B';
221 badm = 'M';
223 if (test_node_count(node, &badm) == -1) {
224 badc = 'B';
225 assert(badm != ' ');
229 if (badm != ' ' || badc != ' ') /* not good */
230 ++num_bad_node;
232 printf("%s%c%c NODE %016jx ",
233 INDENT, badc, badm, (uintmax_t)node_offset);
234 printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx",
235 node->count,
236 (uintmax_t)node->parent,
237 (node->type ? node->type : '?'),
238 depth,
239 (uintmax_t)node->mirror_tid);
240 if (QuietOpt < 3) {
241 printf(" fill=");
242 print_bigblock_fill(node_offset);
244 printf(" {\n");
246 if (VerboseOpt)
247 hammer_add_zone_stat(opt.stats, node_offset, sizeof(*node));
249 for (i = 0; i < node->count; ++i) {
250 elm = &node->elms[i];
251 ext = NULL;
252 if (opt.limit) {
253 switch (node->type) {
254 case HAMMER_BTREE_TYPE_INTERNAL:
255 if (!test_btree_out_of_range(elm))
256 ext = "*";
257 break;
258 case HAMMER_BTREE_TYPE_LEAF:
259 if (test_btree_match(elm))
260 ext = "*";
261 break;
264 print_btree_elm(node, node_offset,
265 elm, left_bound, right_bound, ext);
267 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
268 assert(i == node->count); /* boundary */
269 elm = &node->elms[i];
270 print_btree_elm(node, node_offset,
271 elm, left_bound, right_bound, NULL);
273 printf("%s }\n", INDENT);
275 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
276 for (i = 0; i < node->count; ++i) {
277 elm = &node->elms[i];
278 if (opt.limit && opt.filter) {
279 if (test_btree_out_of_range(elm))
280 continue;
282 if (elm->internal.subtree_offset) {
283 print_subtree(elm);
285 * Cause show to do normal iteration after
286 * seeking to the lo:objid:rectype:key:tid
287 * by default
289 if (opt.limit && opt.filter == -1) /* default */
290 opt.filter = 0;
294 rel_buffer(buffer);
295 depth--;
298 static int
299 test_node_count(hammer_node_ondisk_t node, char *badmp)
301 hammer_node_ondisk_t parent_node;
302 struct buffer_info *buffer = NULL;
303 int maxcount;
305 maxcount = hammer_node_max_elements(node->type);
307 if (maxcount == -1) {
308 *badmp = 'U';
309 return(-1);
310 } else if (node->count > maxcount) {
311 *badmp = 'C';
312 return(-1);
313 } else if (node->count == 0) {
314 parent_node = get_node(node->parent, &buffer);
315 if (parent_node->count != 1) {
316 *badmp = 'C';
317 rel_buffer(buffer);
318 return(-1);
320 rel_buffer(buffer);
323 return(0);
326 static __inline
328 is_root_btree_beg(uint8_t type, int i, hammer_btree_elm_t elm)
331 * elm->base.btype depends on what the original node had
332 * so it could be anything but HAMMER_BTREE_TYPE_NONE.
334 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
335 i == 0 &&
336 elm->base.localization == 0 &&
337 elm->base.obj_id == (int64_t)-0x8000000000000000LL &&
338 elm->base.key == (int64_t)-0x8000000000000000LL &&
339 elm->base.create_tid == 1 &&
340 elm->base.delete_tid == 1 &&
341 elm->base.rec_type == 0 &&
342 elm->base.obj_type == 0 &&
343 elm->base.btype != HAMMER_BTREE_TYPE_NONE);
346 static __inline
348 is_root_btree_end(uint8_t type, int i, hammer_btree_elm_t elm)
350 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
351 i != 0 &&
352 elm->base.localization == 0xFFFFFFFFU &&
353 elm->base.obj_id == 0x7FFFFFFFFFFFFFFFLL &&
354 elm->base.key == 0x7FFFFFFFFFFFFFFFLL &&
355 elm->base.create_tid == 0xFFFFFFFFFFFFFFFFULL &&
356 elm->base.delete_tid == 0 &&
357 elm->base.rec_type == 0xFFFFU &&
358 elm->base.obj_type == 0 &&
359 elm->base.btype == HAMMER_BTREE_TYPE_NONE);
362 static
363 void
364 print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
365 hammer_btree_elm_t elm,
366 hammer_base_elm_t left_bound,
367 hammer_base_elm_t right_bound,
368 const char *ext)
370 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
371 char deleted;
372 char rootelm;
373 const char *label;
374 int flags;
375 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
377 flags = get_elm_flags(node, node_offset, elm, left_bound, right_bound);
378 flagstr[0] = flags ? 'B' : 'G';
379 if (flags & FLAG_TOOFARLEFT)
380 flagstr[2] = 'L';
381 if (flags & FLAG_TOOFARRIGHT)
382 flagstr[3] = 'R';
383 if (flags & FLAG_BADTYPE)
384 flagstr[4] = 'T';
385 if (flags & FLAG_BADCHILDPARENT)
386 flagstr[5] = 'C';
387 if (flags & FLAG_BADMIRRORTID)
388 flagstr[6] = 'M';
389 if (flagstr[0] == 'B')
390 ++num_bad_elm;
393 * Check if elm is derived from root split
395 if (is_root_btree_beg(node->type, i, elm))
396 rootelm = '>';
397 else if (is_root_btree_end(node->type, i, elm))
398 rootelm = '<';
399 else
400 rootelm = ' ';
402 if (elm->base.delete_tid)
403 deleted = 'd';
404 else
405 deleted = ' ';
407 if (node->type == HAMMER_BTREE_TYPE_INTERNAL && node->count == i)
408 label = "RBN";
409 else
410 label = "ELM";
412 printf("%s%s %s %2d %c ",
413 INDENT, flagstr, label, i, hammer_elm_btype(elm));
414 printf("lo=%08x obj=%016jx rt=%02x key=%016jx tid=%016jx\n",
415 elm->base.localization,
416 (uintmax_t)elm->base.obj_id,
417 elm->base.rec_type,
418 (uintmax_t)elm->base.key,
419 (uintmax_t)elm->base.create_tid);
420 printf("%s %c del=%016jx ot=%02x",
421 INDENT,
422 (rootelm == ' ' ? deleted : rootelm),
423 (uintmax_t)elm->base.delete_tid,
424 elm->base.obj_type);
426 switch(node->type) {
427 case HAMMER_BTREE_TYPE_INTERNAL:
428 printf(" suboff=%016jx",
429 (uintmax_t)elm->internal.subtree_offset);
430 if (QuietOpt < 3) {
431 printf(" mirror=%016jx",
432 (uintmax_t)elm->internal.mirror_tid);
434 if (ext)
435 printf(" %s", ext);
436 break;
437 case HAMMER_BTREE_TYPE_LEAF:
438 switch(elm->base.btype) {
439 case HAMMER_BTREE_TYPE_RECORD:
440 printf(" dataoff=%016jx/%d",
441 (uintmax_t)elm->leaf.data_offset,
442 elm->leaf.data_len);
443 if (QuietOpt < 3) {
444 const char *p = check_data_crc(elm);
445 printf(" crc=%08x", elm->leaf.data_crc);
446 if (p) {
447 printf(" error=%s", p);
448 ++num_bad_rec;
450 printf(" fill=");
451 print_bigblock_fill(elm->leaf.data_offset);
453 if (QuietOpt < 2)
454 print_record(elm);
455 if (VerboseOpt)
456 hammer_add_zone_stat(opt.stats,
457 elm->leaf.data_offset,
458 elm->leaf.data_len);
459 break;
460 default:
461 printf(" badtype=%d", elm->base.btype);
462 break;
464 if (ext)
465 printf(" %s", ext);
466 break;
468 printf("\n");
471 static
473 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
474 hammer_btree_elm_t elm,
475 hammer_base_elm_t left_bound,
476 hammer_base_elm_t right_bound)
478 hammer_off_t child_offset;
479 int flags = 0;
480 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
482 switch(node->type) {
483 case HAMMER_BTREE_TYPE_INTERNAL:
484 child_offset = elm->internal.subtree_offset;
485 if (elm->internal.mirror_tid > node->mirror_tid)
486 flags |= FLAG_BADMIRRORTID;
488 if (i == node->count) {
489 if (child_offset != 0)
490 flags |= FLAG_BADCHILDPARENT;
491 switch(elm->base.btype) {
492 case HAMMER_BTREE_TYPE_NONE:
493 flags |= test_rbn_lr(elm, left_bound, right_bound);
494 break;
495 default:
496 flags |= FLAG_BADTYPE;
497 break;
499 } else {
500 if (child_offset == 0) {
501 flags |= FLAG_BADCHILDPARENT;
502 } else {
503 struct buffer_info *buffer = NULL;
504 hammer_node_ondisk_t subnode;
505 subnode = get_node(child_offset, &buffer);
506 if (subnode == NULL)
507 flags |= FLAG_BADCHILDPARENT;
508 else if (subnode->parent != node_offset)
509 flags |= FLAG_BADCHILDPARENT;
510 rel_buffer(buffer);
512 switch(elm->base.btype) {
513 case HAMMER_BTREE_TYPE_INTERNAL:
514 case HAMMER_BTREE_TYPE_LEAF:
515 flags |= test_lr(elm, left_bound, right_bound);
516 break;
517 default:
518 flags |= FLAG_BADTYPE;
519 break;
522 break;
523 case HAMMER_BTREE_TYPE_LEAF:
524 if (elm->leaf.data_offset == 0) {
525 flags |= FLAG_BADCHILDPARENT;
527 if (elm->leaf.data_len == 0) {
528 flags |= FLAG_BADCHILDPARENT;
531 if (node->mirror_tid == 0 &&
532 !(node->parent == 0 && node->count == 2)) {
533 flags |= FLAG_BADMIRRORTID;
535 if (elm->base.create_tid && node->mirror_tid &&
536 elm->base.create_tid > node->mirror_tid) {
537 flags |= FLAG_BADMIRRORTID;
539 if (elm->base.delete_tid && node->mirror_tid &&
540 elm->base.delete_tid > node->mirror_tid) {
541 flags |= FLAG_BADMIRRORTID;
543 switch(elm->base.btype) {
544 case HAMMER_BTREE_TYPE_RECORD:
545 flags |= test_lr(elm, left_bound, right_bound);
546 break;
547 default:
548 flags |= FLAG_BADTYPE;
549 break;
551 break;
552 default:
553 flags |= FLAG_BADTYPE;
554 break;
556 return(flags);
559 static
561 test_lr(hammer_btree_elm_t elm,
562 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
564 if (left_bound == NULL || right_bound == NULL)
565 return(0);
566 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
567 return(FLAG_TOOFARLEFT);
568 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
569 return(FLAG_TOOFARRIGHT);
570 return(0);
573 static
575 test_rbn_lr(hammer_btree_elm_t rbn,
576 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
578 if (left_bound == NULL || right_bound == NULL)
579 return(0);
580 if (hammer_btree_cmp(&rbn->base, left_bound) < 0)
581 return(FLAG_TOOFARLEFT);
582 if (hammer_btree_cmp(&rbn->base, right_bound) > 0)
583 return(FLAG_TOOFARRIGHT);
584 return(0);
587 static
588 void
589 print_bigblock_fill(hammer_off_t offset)
591 struct hammer_blockmap_layer1 layer1;
592 struct hammer_blockmap_layer2 layer2;
593 int fill;
594 int error;
596 blockmap_lookup(offset, &layer1, &layer2, &error);
597 printf("z%d:v%d:%d:%d:%lu=",
598 HAMMER_ZONE_DECODE(offset),
599 HAMMER_VOL_DECODE(offset),
600 HAMMER_BLOCKMAP_LAYER1_INDEX(offset),
601 HAMMER_BLOCKMAP_LAYER2_INDEX(offset),
602 offset & HAMMER_BIGBLOCK_MASK64);
604 if (error) {
605 printf("B%d", error);
606 } else {
607 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
608 fill = 100 - fill;
609 printf("%d%%", fill);
614 * Check the generic crc on a data element. Inodes record types are
615 * special in that some of their fields are not CRCed.
617 * Also check that the zone is valid.
619 static
620 const char *
621 check_data_crc(hammer_btree_elm_t elm)
623 struct buffer_info *data_buffer;
624 hammer_off_t data_offset;
625 hammer_off_t buf_offset;
626 int32_t data_len;
627 uint32_t crc;
628 int error;
629 char *ptr;
630 static char bo[5];
632 data_offset = elm->leaf.data_offset;
633 data_len = elm->leaf.data_len;
634 data_buffer = NULL;
635 if (data_offset == 0 || data_len == 0)
636 return("ZO"); /* zero offset or length */
638 error = 0;
639 buf_offset = blockmap_lookup(data_offset, NULL, NULL, &error);
640 if (error) {
641 bzero(bo, sizeof(bo));
642 snprintf(bo, sizeof(bo), "BO%d", -error);
643 return(bo);
646 crc = 0;
647 switch (elm->leaf.base.rec_type) {
648 case HAMMER_RECTYPE_INODE:
649 /* this should always match */
650 if (data_len == sizeof(struct hammer_inode_data)) {
651 ptr = get_buffer_data(buf_offset, &data_buffer, 0);
652 crc = crc32(ptr, HAMMER_INODE_CRCSIZE);
653 rel_buffer(data_buffer);
655 break;
656 default:
657 crc = get_buf_crc(buf_offset, data_len);
658 break;
661 if (crc == 0)
662 return("Bx"); /* bad crc */
663 if (crc != elm->leaf.data_crc)
664 return("BX"); /* bad crc */
665 return(NULL); /* success */
668 static
669 uint32_t
670 get_buf_crc(hammer_off_t buf_offset, int32_t buf_len)
672 struct buffer_info *data_buffer = NULL;
673 int32_t len;
674 uint32_t crc = 0;
675 char *ptr;
677 while (buf_len) {
678 ptr = get_buffer_data(buf_offset, &data_buffer, 0);
679 len = HAMMER_BUFSIZE - ((int)buf_offset & HAMMER_BUFMASK);
680 if (len > buf_len)
681 len = (int)buf_len;
682 assert(len <= HAMMER_BUFSIZE);
683 crc = crc32_ext(ptr, len, crc);
684 buf_len -= len;
685 buf_offset += len;
687 rel_buffer(data_buffer);
689 return(crc);
692 static
693 void
694 print_config(char *cfgtxt)
696 char *token;
698 printf("\n%s%17s", INDENT, "");
699 printf("config text=\"\n");
700 if (cfgtxt != NULL) {
701 while((token = strsep(&cfgtxt, "\r\n")) != NULL)
702 if (strlen(token))
703 printf("%s%17s %s\n",
704 INDENT, "", token);
706 printf("%s%17s \"", INDENT, "");
709 static
710 void
711 print_record(hammer_btree_elm_t elm)
713 struct buffer_info *data_buffer;
714 hammer_off_t data_offset;
715 int32_t data_len;
716 hammer_data_ondisk_t data;
717 uint32_t status;
718 char *str1 = NULL;
719 char *str2 = NULL;
721 data_offset = elm->leaf.data_offset;
722 data_len = elm->leaf.data_len;
723 assert(data_offset != 0);
724 assert(data_len != 0);
726 data_buffer = NULL;
727 data = get_buffer_data(data_offset, &data_buffer, 0);
728 assert(data != NULL);
730 switch(elm->leaf.base.rec_type) {
731 case HAMMER_RECTYPE_UNKNOWN:
732 printf("\n%s%17s", INDENT, "");
733 printf("unknown");
734 break;
735 case HAMMER_RECTYPE_INODE:
736 printf("\n%s%17s", INDENT, "");
737 printf("inode size=%jd nlinks=%jd",
738 (intmax_t)data->inode.size,
739 (intmax_t)data->inode.nlinks);
740 if (QuietOpt < 1) {
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);
757 break;
758 case HAMMER_RECTYPE_DIRENTRY:
759 data_len -= HAMMER_ENTRY_NAME_OFF;
760 printf("\n%s%17s", INDENT, "");
761 printf("dir-entry ino=%016jx lo=%08x",
762 (uintmax_t)data->entry.obj_id,
763 data->entry.localization);
764 if (!opt.obfuscate)
765 printf(" name=\"%*.*s\"",
766 data_len, data_len, data->entry.name);
767 break;
768 case HAMMER_RECTYPE_FIX:
769 switch(elm->leaf.base.key) {
770 case HAMMER_FIXKEY_SYMLINK:
771 data_len -= HAMMER_SYMLINK_NAME_OFF;
772 printf("\n%s%17s", INDENT, "");
773 printf("fix-symlink name=\"%*.*s\"",
774 data_len, data_len, data->symlink.name);
775 break;
777 break;
778 case HAMMER_RECTYPE_PFS:
779 printf("\n%s%17s", INDENT, "");
780 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
781 (intmax_t)data->pfsd.sync_beg_tid,
782 (intmax_t)data->pfsd.sync_end_tid);
783 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
784 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
785 printf("%17s", "");
786 printf(" shared_uuid=%s\n", str1);
787 printf("%17s", "");
788 printf(" unique_uuid=%s\n", str2);
789 printf("%17s", "");
790 printf(" mirror_flags=%08x label=\"%s\"",
791 data->pfsd.mirror_flags, data->pfsd.label);
792 if (data->pfsd.snapshots[0])
793 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
794 free(str1);
795 free(str2);
796 break;
797 case HAMMER_RECTYPE_SNAPSHOT:
798 printf("\n%s%17s", INDENT, "");
799 printf("snapshot tid=%016jx label=\"%s\"",
800 (intmax_t)data->snap.tid, data->snap.label);
801 break;
802 case HAMMER_RECTYPE_CONFIG:
803 if (VerboseOpt > 2) {
804 char *p = strdup(data->config.text);
805 print_config(p);
806 free(p);
808 break;
809 case HAMMER_RECTYPE_DATA:
810 if (VerboseOpt > 3) {
811 printf("\n");
812 hexdump_record(data, data_len, "\t\t ");
814 break;
815 case HAMMER_RECTYPE_EXT:
816 case HAMMER_RECTYPE_DB:
817 if (VerboseOpt > 2) {
818 printf("\n");
819 hexdump_record(data, data_len, "\t\t ");
821 break;
822 default:
823 assert(0);
824 break;
826 rel_buffer(data_buffer);
830 * HAMMER userspace only supports buffer size upto HAMMER_BUFSIZE
831 * which is 16KB. Passing record data length larger than 16KB to
832 * hexdump(3) is invalid even if the leaf node elm says >16KB data.
834 static void
835 hexdump_record(const void *ptr, int length, const char *hdr)
837 int data_len = length;
839 if (data_len > HAMMER_BUFSIZE) /* XXX */
840 data_len = HAMMER_BUFSIZE;
841 hexdump(ptr, data_len, hdr, 0);
843 if (length > data_len)
844 printf("%s....\n", hdr);
847 static __inline
848 unsigned long
849 _strtoul(const char *p, int base)
851 unsigned long retval;
853 errno = 0; /* clear */
854 retval = strtoul(p, NULL, base);
855 if (errno == ERANGE && retval == ULONG_MAX)
856 err(1, "strtoul");
857 return retval;
860 static __inline
861 unsigned long long
862 _strtoull(const char *p, int base)
864 unsigned long long retval;
866 errno = 0; /* clear */
867 retval = strtoull(p, NULL, base);
868 if (errno == ERANGE && retval == ULLONG_MAX)
869 err(1, "strtoull");
870 return retval;
873 static int
874 init_btree_search(const char *arg)
876 char *s, *p;
877 int i = 0;
879 bzero(&opt.base, sizeof(opt.base));
880 opt.limit = 0;
882 if (arg == NULL)
883 return(-1);
884 if (strcmp(arg, "none") == 0)
885 return(-1);
887 s = strdup(arg);
888 if (s == NULL)
889 return(-1);
891 while ((p = s) != NULL) {
892 if ((s = strchr(s, ':')) != NULL)
893 *s++ = 0;
894 if (++i == 1) {
895 opt.base.localization = _strtoul(p, 16);
896 } else if (i == 2) {
897 opt.base.obj_id = _strtoull(p, 16);
898 } else if (i == 3) {
899 opt.base.rec_type = _strtoul(p, 16);
900 } else if (i == 4) {
901 opt.base.key = _strtoull(p, 16);
902 } else if (i == 5) {
903 opt.base.create_tid = _strtoull(p, 16);
904 break;
907 opt.limit = i;
908 free(s);
910 return(i);
913 static int
914 test_btree_search(hammer_btree_elm_t elm)
916 hammer_base_elm_t base1 = &elm->base;
917 hammer_base_elm_t base2 = &opt.base;
918 int limit = opt.limit;
920 if (base1->localization < base2->localization)
921 return(-1);
922 if (base1->localization > base2->localization)
923 return(1);
924 if (limit == 1)
925 return(0); /* ignore below */
927 if (base1->obj_id < base2->obj_id)
928 return(-2);
929 if (base1->obj_id > base2->obj_id)
930 return(2);
931 if (limit == 2)
932 return(0); /* ignore below */
934 if (base1->rec_type < base2->rec_type)
935 return(-3);
936 if (base1->rec_type > base2->rec_type)
937 return(3);
938 if (limit == 3)
939 return(0); /* ignore below */
941 if (base1->key < base2->key)
942 return(-4);
943 if (base1->key > base2->key)
944 return(4);
945 if (limit == 4)
946 return(0); /* ignore below */
948 if (base1->create_tid == 0) {
949 if (base2->create_tid == 0)
950 return(0);
951 return(5);
953 if (base2->create_tid == 0)
954 return(-5);
955 if (base1->create_tid < base2->create_tid)
956 return(-5);
957 if (base1->create_tid > base2->create_tid)
958 return(5);
959 return(0);
962 static __inline
964 test_btree_match(hammer_btree_elm_t elm)
966 if (test_btree_search(elm) == 0)
967 return(1);
968 return(0);
971 static
973 test_btree_out_of_range(hammer_btree_elm_t elm)
975 if (test_btree_search(elm) > 0)
976 return(1); /* conditions < this elm */
978 if (opt.limit >= 5) {
979 if (test_btree_search(elm + 1) <= 0)
980 return(1); /* next elm <= conditions */
981 } else {
982 if (test_btree_search(elm + 1) < 0)
983 return(1); /* next elm < conditions */
985 return(0);
989 * Dump the UNDO FIFO
991 void
992 hammer_cmd_show_undo(void)
994 struct volume_info *volume;
995 hammer_blockmap_t rootmap;
996 hammer_off_t scan_offset;
997 hammer_fifo_any_t head;
998 struct buffer_info *data_buffer = NULL;
999 int64_t bytes;
1001 volume = get_root_volume();
1002 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
1003 if (rootmap->first_offset <= rootmap->next_offset)
1004 bytes = rootmap->next_offset - rootmap->first_offset;
1005 else
1006 bytes = rootmap->alloc_offset - rootmap->first_offset +
1007 (rootmap->next_offset & HAMMER_OFF_LONG_MASK);
1009 printf("Volume header UNDO %016jx-%016jx/%016jx\n",
1010 (intmax_t)rootmap->first_offset,
1011 (intmax_t)rootmap->next_offset,
1012 (intmax_t)rootmap->alloc_offset);
1013 printf("UNDO map is %jdMB\n",
1014 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
1015 (1024 * 1024)));
1016 printf("UNDO being used is %jdB\n", (intmax_t)bytes);
1018 scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
1019 while (scan_offset < rootmap->alloc_offset) {
1020 head = get_buffer_data(scan_offset, &data_buffer, 0);
1021 printf("%016jx ", scan_offset);
1023 switch(head->head.hdr_type) {
1024 case HAMMER_HEAD_TYPE_PAD:
1025 printf("PAD(%04x)", head->head.hdr_size);
1026 break;
1027 case HAMMER_HEAD_TYPE_DUMMY:
1028 printf("DUMMY(%04x) seq=%08x",
1029 head->head.hdr_size, head->head.hdr_seq);
1030 break;
1031 case HAMMER_HEAD_TYPE_UNDO:
1032 printf("UNDO(%04x) seq=%08x "
1033 "dataoff=%016jx bytes=%d",
1034 head->head.hdr_size, head->head.hdr_seq,
1035 (intmax_t)head->undo.undo_offset,
1036 head->undo.undo_data_bytes);
1037 break;
1038 case HAMMER_HEAD_TYPE_REDO:
1039 printf("REDO(%04x) seq=%08x flags=%08x "
1040 "objid=%016jx logoff=%016jx bytes=%d",
1041 head->head.hdr_size, head->head.hdr_seq,
1042 head->redo.redo_flags,
1043 (intmax_t)head->redo.redo_objid,
1044 (intmax_t)head->redo.redo_offset,
1045 head->redo.redo_data_bytes);
1046 break;
1047 default:
1048 printf("UNKNOWN(%04x,%04x) seq=%08x",
1049 head->head.hdr_type,
1050 head->head.hdr_size,
1051 head->head.hdr_seq);
1052 break;
1055 if (scan_offset == rootmap->first_offset)
1056 printf(" >");
1057 if (scan_offset == rootmap->next_offset)
1058 printf(" <");
1059 printf("\n");
1061 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
1062 head->head.hdr_size == 0 ||
1063 head->head.hdr_size > HAMMER_UNDO_ALIGN -
1064 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
1065 printf("Illegal size field, skipping to "
1066 "next boundary\n");
1067 scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
1068 ~HAMMER_UNDO_MASK64;
1069 } else {
1070 scan_offset += head->head.hdr_size;
1073 rel_buffer(data_buffer);