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