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