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