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