sbin/hammer: fix counter-intuitive output of hammer show command
[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
37 #define FLAG_TOOFARLEFT         0x0001
38 #define FLAG_TOOFARRIGHT        0x0002
39 #define FLAG_BADTYPE            0x0004
40 #define FLAG_BADCHILDPARENT     0x0008
41 #define FLAG_BADMIRRORTID       0x0010
42
43 typedef struct btree_search {
44         u_int32_t       lo;
45         int64_t         obj_id;
46 } *btree_search_t;
47
48 static void print_btree_node(hammer_off_t node_offset, btree_search_t search,
49                         int depth, int spike, hammer_tid_t mirror_tid,
50                         hammer_base_elm_t left_bound,
51                         hammer_base_elm_t right_bound);
52 static const char *check_data_crc(hammer_btree_elm_t elm);
53 static void print_record(hammer_btree_elm_t elm);
54 static void print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
55                         int flags, const char *label, const char *ext);
56 static int print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
57                         hammer_btree_elm_t elm, u_int8_t btype,
58                         hammer_base_elm_t left_bound,
59                         hammer_base_elm_t right_bound);
60 static void print_bigblock_fill(hammer_off_t offset);
61
62 void
63 hammer_cmd_show(hammer_off_t node_offset, u_int32_t lo, int64_t obj_id,
64                 int depth,
65                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
66 {
67         struct volume_info *volume;
68         struct btree_search search;
69         btree_search_t searchp;
70         int zone;
71
72         AssertOnFailure = 0;
73
74         if (node_offset == (hammer_off_t)-1) {
75                 volume = get_volume(RootVolNo);
76                 node_offset = volume->ondisk->vol0_btree_root;
77                 if (QuietOpt < 3) {
78                         printf("Volume header\trecords=%jd next_tid=%016jx\n",
79                                (intmax_t)volume->ondisk->vol0_stat_records,
80                                (uintmax_t)volume->ondisk->vol0_next_tid);
81                         printf("\t\tbufoffset=%016jx\n",
82                                (uintmax_t)volume->ondisk->vol_buf_beg);
83                         for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) {
84                                 printf("\t\tzone %d\tnext_offset=%016jx\n",
85                                         zone,
86                                         (uintmax_t)volume->ondisk->vol0_blockmap[zone].next_offset
87                                 );
88                         }
89                 }
90                 rel_volume(volume);
91         }
92
93         if (lo == 0 && obj_id == (int64_t)HAMMER_MIN_OBJID) {
94                 searchp = NULL;
95                 printf("show %016jx depth %d\n", (uintmax_t)node_offset, depth);
96         } else {
97                 search.lo = lo;
98                 search.obj_id = obj_id;
99                 searchp = &search;
100                 printf("show %016jx lo %08x obj_id %016jx depth %d\n",
101                         (uintmax_t)node_offset, lo, (uintmax_t)obj_id, depth);
102         }
103         print_btree_node(node_offset, searchp, depth, 0, HAMMER_MAX_TID,
104                          left_bound, right_bound);
105         print_btree_node(node_offset, searchp, depth, 1, HAMMER_MAX_TID,
106                          left_bound, right_bound);
107
108         AssertOnFailure = 1;
109 }
110
111 static void
112 print_btree_node(hammer_off_t node_offset, btree_search_t search,
113                 int depth, int spike, hammer_tid_t mirror_tid,
114                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
115 {
116         struct buffer_info *buffer = NULL;
117         hammer_node_ondisk_t node;
118         hammer_btree_elm_t elm;
119         int i;
120         int flags;
121         int maxcount;
122         char badc;
123         char badm;
124         const char *ext;
125
126         node = get_node(node_offset, &buffer);
127
128         if (node == NULL) {
129                 printf("BI   NODE %016jx (IO ERROR)\n",
130                        (uintmax_t)node_offset);
131                 return;
132         }
133
134         if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
135                 badc = ' ';
136         else
137                 badc = 'B';
138
139         if (node->mirror_tid <= mirror_tid) {
140                 badm = ' ';
141         } else {
142                 badm = 'M';
143                 badc = 'B';
144         }
145
146         if (spike == 0) {
147                 printf("%c%c   NODE %016jx cnt=%02d p=%016jx "
148                        "type=%c depth=%d",
149                        badc,
150                        badm,
151                        (uintmax_t)node_offset, node->count,
152                        (uintmax_t)node->parent,
153                        (node->type ? node->type : '?'), depth);
154                 printf(" mirror %016jx", (uintmax_t)node->mirror_tid);
155                 if (QuietOpt < 3) {
156                         printf(" fill=");
157                         print_bigblock_fill(node_offset);
158                 }
159                 printf(" {\n");
160
161                 maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
162                            HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
163
164                 for (i = 0; i < node->count && i < maxcount; ++i) {
165                         elm = &node->elms[i];
166
167                         if (node->type != HAMMER_BTREE_TYPE_INTERNAL) {
168                                 ext = NULL;
169                                 if (search &&
170                                     elm->base.localization == search->lo &&
171                                      elm->base.obj_id == search->obj_id) {
172                                         ext = " *";
173                                 }
174                         } else if (search) {
175                                 ext = " *";
176                                 if (elm->base.localization > search->lo ||
177                                     (elm->base.localization == search->lo &&
178                                      elm->base.obj_id > search->obj_id)) {
179                                         ext = NULL;
180                                 }
181                                 if (elm[1].base.localization < search->lo ||
182                                     (elm[1].base.localization == search->lo &&
183                                      elm[1].base.obj_id < search->obj_id)) {
184                                         ext = NULL;
185                                 }
186                         } else {
187                                 ext = NULL;
188                         }
189
190                         flags = print_elm_flags(node, node_offset,
191                                                 elm, elm->base.btype,
192                                                 left_bound, right_bound);
193                         print_btree_elm(elm, i, node->type, flags, "ELM", ext);
194                 }
195                 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
196                         elm = &node->elms[i];
197
198                         flags = print_elm_flags(node, node_offset,
199                                                 elm, 'I',
200                                                 left_bound, right_bound);
201                         print_btree_elm(elm, i, node->type, flags, "RBN", NULL);
202                 }
203                 printf("     }\n");
204         }
205
206         for (i = 0; i < node->count; ++i) {
207                 elm = &node->elms[i];
208
209                 switch(node->type) {
210                 case HAMMER_BTREE_TYPE_INTERNAL:
211                         if (search) {
212                                 if (elm->base.localization > search->lo ||
213                                     (elm->base.localization == search->lo &&
214                                      elm->base.obj_id > search->obj_id)) {
215                                         break;
216                                 }
217                                 if (elm[1].base.localization < search->lo ||
218                                     (elm[1].base.localization == search->lo &&
219                                      elm[1].base.obj_id < search->obj_id)) {
220                                         break;
221                                 }
222                         }
223                         if (elm->internal.subtree_offset) {
224                                 print_btree_node(elm->internal.subtree_offset,
225                                                  search, depth + 1, spike,
226                                                  elm->internal.mirror_tid,
227                                                  &elm[0].base, &elm[1].base);
228                                 /*
229                                  * Cause show to iterate after seeking to
230                                  * the lo:objid
231                                  */
232                                 search = NULL;
233                         }
234                         break;
235                 default:
236                         break;
237                 }
238         }
239         rel_buffer(buffer);
240 }
241
242 static
243 void
244 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
245                 int flags, const char *label, const char *ext)
246 {
247         char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
248
249         flagstr[0] = flags ? 'B' : 'G';
250         if (flags & FLAG_TOOFARLEFT)
251                 flagstr[2] = 'L';
252         if (flags & FLAG_TOOFARRIGHT)
253                 flagstr[3] = 'R';
254         if (flags & FLAG_BADTYPE)
255                 flagstr[4] = 'T';
256         if (flags & FLAG_BADCHILDPARENT)
257                 flagstr[5] = 'C';
258         if (flags & FLAG_BADMIRRORTID)
259                 flagstr[6] = 'M';
260
261         printf("%s\t%s %2d %c ",
262                flagstr, label, i,
263                (elm->base.btype ? elm->base.btype : '?'));
264         printf("lo=%08x obj=%016jx rt=%02x key=%016jx ot=%02x\n",
265                elm->base.localization,
266                (uintmax_t)elm->base.obj_id,
267                elm->base.rec_type,
268                (uintmax_t)elm->base.key,
269                elm->base.obj_type);
270         printf("\t       %c tids %016jx:%016jx ",
271                 (elm->base.delete_tid ? 'd' : ' '),
272                (uintmax_t)elm->base.create_tid,
273                (uintmax_t)elm->base.delete_tid);
274
275         switch(type) {
276         case HAMMER_BTREE_TYPE_INTERNAL:
277                 printf("suboff=%016jx",
278                        (uintmax_t)elm->internal.subtree_offset);
279                 if (QuietOpt < 3) {
280                         printf(" mirror %016jx",
281                                (uintmax_t)elm->internal.mirror_tid);
282                 }
283                 if (ext)
284                         printf(" %s", ext);
285                 break;
286         case HAMMER_BTREE_TYPE_LEAF:
287                 if (ext)
288                         printf(" %s", ext);
289                 switch(elm->base.btype) {
290                 case HAMMER_BTREE_TYPE_RECORD:
291                         if (QuietOpt < 3)
292                                 printf("\n%s\t         ", check_data_crc(elm));
293                         else
294                                 printf("\n\t         ");
295                         printf("dataoff=%016jx/%d",
296                                (uintmax_t)elm->leaf.data_offset,
297                                elm->leaf.data_len);
298                         if (QuietOpt < 3) {
299                                 printf(" crc=%04x", elm->leaf.data_crc);
300                                 printf("\n\t         fills=");
301                                 print_bigblock_fill(elm->leaf.data_offset);
302                         }
303                         if (QuietOpt < 2)
304                                 print_record(elm);
305                         break;
306                 }
307                 break;
308         default:
309                 break;
310         }
311         printf("\n");
312 }
313
314 static
315 int
316 print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
317                 hammer_btree_elm_t elm, u_int8_t btype,
318                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
319 {
320         int flags = 0;
321
322         switch(node->type) {
323         case HAMMER_BTREE_TYPE_INTERNAL:
324                 if (elm->internal.subtree_offset) {
325                         struct buffer_info *buffer = NULL;
326                         hammer_node_ondisk_t subnode;
327
328                         subnode = get_node(elm->internal.subtree_offset,
329                                            &buffer);
330                         if (subnode == NULL)
331                                 flags |= FLAG_BADCHILDPARENT;
332                         else if (subnode->parent != node_offset)
333                                 flags |= FLAG_BADCHILDPARENT;
334                         rel_buffer(buffer);
335                 }
336                 if (elm->internal.mirror_tid > node->mirror_tid)
337                         flags |= FLAG_BADMIRRORTID;
338
339                 switch(btype) {
340                 case HAMMER_BTREE_TYPE_INTERNAL:
341                         if (left_bound == NULL || right_bound == NULL)
342                                 break;
343                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
344                                 flags |= FLAG_TOOFARLEFT;
345                         if (hammer_btree_cmp(&elm->base, right_bound) > 0)
346                                 flags |= FLAG_TOOFARRIGHT;
347                         break;
348                 case HAMMER_BTREE_TYPE_LEAF:
349                         if (left_bound == NULL || right_bound == NULL)
350                                 break;
351                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
352                                 flags |= FLAG_TOOFARLEFT;
353                         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
354                                 flags |= FLAG_TOOFARRIGHT;
355                         break;
356                 default:
357                         flags |= FLAG_BADTYPE;
358                         break;
359                 }
360                 break;
361         case HAMMER_BTREE_TYPE_LEAF:
362                 if (elm->base.create_tid &&
363                     elm->base.create_tid > node->mirror_tid) {
364                         flags |= FLAG_BADMIRRORTID;
365                 }
366                 if (elm->base.delete_tid &&
367                     elm->base.delete_tid > node->mirror_tid) {
368                         flags |= FLAG_BADMIRRORTID;
369                 }
370                 switch(btype) {
371                 case HAMMER_BTREE_TYPE_RECORD:
372                         if (left_bound == NULL || right_bound == NULL)
373                                 break;
374                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
375                                 flags |= FLAG_TOOFARLEFT;
376                         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
377                                 flags |= FLAG_TOOFARRIGHT;
378                         break;
379                 default:
380                         flags |= FLAG_BADTYPE;
381                         break;
382                 }
383                 break;
384         default:
385                 flags |= FLAG_BADTYPE;
386                 break;
387         }
388         return(flags);
389 }
390
391 static
392 void
393 print_bigblock_fill(hammer_off_t offset)
394 {
395         struct hammer_blockmap_layer1 layer1;
396         struct hammer_blockmap_layer2 layer2;
397         int fill;
398         int error;
399
400         blockmap_lookup(offset, &layer1, &layer2, &error);
401         if (error) {
402                 printf("z%d:%lld=BADZ",
403                         HAMMER_ZONE_DECODE(offset),
404                         (offset & ~HAMMER_OFF_ZONE_MASK) /
405                             HAMMER_LARGEBLOCK_SIZE
406                 );
407         } else {
408                 fill = layer2.bytes_free * 100 / HAMMER_LARGEBLOCK_SIZE;
409                 fill = 100 - fill;
410
411                 printf("z%d:%lld=%d%%",
412                         HAMMER_ZONE_DECODE(offset),
413                         (offset & ~HAMMER_OFF_ZONE_MASK) /
414                             HAMMER_LARGEBLOCK_SIZE,
415                         fill
416                 );
417         }
418 }
419
420 /*
421  * Check the generic crc on a data element.  Inodes record types are
422  * special in that some of their fields are not CRCed.
423  *
424  * Also check that the zone is valid.
425  */
426 static
427 const char *
428 check_data_crc(hammer_btree_elm_t elm)
429 {
430         struct buffer_info *data_buffer;
431         hammer_off_t data_offset;
432         int32_t data_len;
433         int32_t len;
434         u_int32_t crc;
435         int error;
436         char *ptr;
437
438         data_offset = elm->leaf.data_offset;
439         data_len = elm->leaf.data_len;
440         data_buffer = NULL;
441         if (data_offset == 0 || data_len == 0)
442                 return("Z");
443
444         crc = 0;
445         error = 0;
446         while (data_len) {
447                 blockmap_lookup(data_offset, NULL, NULL, &error);
448                 if (error)
449                         break;
450
451                 ptr = get_buffer_data(data_offset, &data_buffer, 0);
452                 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
453                 if (len > data_len)
454                         len = (int)data_len;
455                 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
456                     data_len == sizeof(struct hammer_inode_data)) {
457                         crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
458                 } else {
459                         crc = crc32_ext(ptr, len, crc);
460                 }
461                 data_len -= len;
462                 data_offset += len;
463         }
464         if (data_buffer)
465                 rel_buffer(data_buffer);
466         if (error)
467                 return("BO");           /* bad offset */
468         if (crc == elm->leaf.data_crc)
469                 return("");
470         return("BX");                   /* bad crc */
471 }
472
473 static
474 void
475 print_record(hammer_btree_elm_t elm)
476 {
477         struct buffer_info *data_buffer;
478         hammer_off_t data_offset;
479         int32_t data_len;
480         hammer_data_ondisk_t data;
481
482         data_offset = elm->leaf.data_offset;
483         data_len = elm->leaf.data_len;
484         data_buffer = NULL;
485
486         if (data_offset)
487                 data = get_buffer_data(data_offset, &data_buffer, 0);
488         else
489                 data = NULL;
490
491         switch(elm->leaf.base.rec_type) {
492         case HAMMER_RECTYPE_INODE:
493                 printf("\n%17s", "");
494                 printf("size=%jd nlinks=%jd",
495                        (intmax_t)data->inode.size,
496                        (intmax_t)data->inode.nlinks);
497                 if (QuietOpt < 1) {
498                         printf(" mode=%05o uflags=%08x\n",
499                                 data->inode.mode,
500                                 data->inode.uflags);
501                         printf("%17s", "");
502                         printf("ctime=%016jx pobjid=%016jx obj_type=%d\n",
503                                 (uintmax_t)data->inode.ctime,
504                                 (uintmax_t)data->inode.parent_obj_id,
505                                 data->inode.obj_type);
506                         printf("%17s", "");
507                         printf("mtime=%016jx", (uintmax_t)data->inode.mtime);
508                         printf(" caps=%02x", data->inode.cap_flags);
509                 }
510                 break;
511         case HAMMER_RECTYPE_DIRENTRY:
512                 printf("\n%17s", "");
513                 data_len -= HAMMER_ENTRY_NAME_OFF;
514                 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"",
515                        (uintmax_t)data->entry.obj_id,
516                        data->entry.localization,
517                        data_len, data_len, data->entry.name);
518                 break;
519         case HAMMER_RECTYPE_FIX:
520                 switch(elm->leaf.base.key) {
521                 case HAMMER_FIXKEY_SYMLINK:
522                         data_len -= HAMMER_SYMLINK_NAME_OFF;
523                         printf("\n%17s", "");
524                         printf("symlink=\"%*.*s\"", data_len, data_len,
525                                 data->symlink.name);
526                         break;
527                 default:
528                         break;
529                 }
530                 break;
531         default:
532                 break;
533         }
534         if (data_buffer)
535                 rel_buffer(data_buffer);
536 }
537
538 /*
539  * Dump the UNDO FIFO
540  */
541 void
542 hammer_cmd_show_undo(void)
543 {
544         struct volume_info *volume;
545         hammer_blockmap_t rootmap;
546         hammer_off_t scan_offset;
547         hammer_fifo_any_t head;
548         struct buffer_info *data_buffer = NULL;
549
550         volume = get_volume(RootVolNo);
551         rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
552         printf("Volume header UNDO %016jx-%016jx/%016jx\n",
553                 (intmax_t)rootmap->first_offset,
554                 (intmax_t)rootmap->next_offset,
555                 (intmax_t)rootmap->alloc_offset);
556         printf("Undo map is %jdMB\n",
557                 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
558                            (1024 * 1024)));
559         scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
560         while (scan_offset < rootmap->alloc_offset) {
561                 head = get_buffer_data(scan_offset, &data_buffer, 0);
562                 printf("%016jx ", scan_offset);
563
564                 switch(head->head.hdr_type) {
565                 case HAMMER_HEAD_TYPE_PAD:
566                         printf("PAD(%04x)\n", head->head.hdr_size);
567                         break;
568                 case HAMMER_HEAD_TYPE_DUMMY:
569                         printf("DUMMY(%04x) seq=%08x\n",
570                                 head->head.hdr_size, head->head.hdr_seq);
571                         break;
572                 case HAMMER_HEAD_TYPE_UNDO:
573                         printf("UNDO(%04x) seq=%08x "
574                                "dataoff=%016jx bytes=%d\n",
575                                 head->head.hdr_size, head->head.hdr_seq,
576                                 (intmax_t)head->undo.undo_offset,
577                                 head->undo.undo_data_bytes);
578                         break;
579                 case HAMMER_HEAD_TYPE_REDO:
580                         printf("REDO(%04x) seq=%08x flags=%08x "
581                                "objid=%016jx logoff=%016jx bytes=%d\n",
582                                 head->head.hdr_size, head->head.hdr_seq,
583                                 head->redo.redo_flags,
584                                 (intmax_t)head->redo.redo_objid,
585                                 (intmax_t)head->redo.redo_offset,
586                                 head->redo.redo_data_bytes);
587                         break;
588                 default:
589                         printf("UNKNOWN(%04x,%04x) seq=%08x\n",
590                                 head->head.hdr_type,
591                                 head->head.hdr_size,
592                                 head->head.hdr_seq);
593                         break;
594                 }
595                 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
596                     head->head.hdr_size == 0 ||
597                     head->head.hdr_size > HAMMER_UNDO_ALIGN -
598                                     ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
599                         printf("Illegal size field, skipping to "
600                                "next boundary\n");
601                         scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
602                                         ~HAMMER_UNDO_MASK64;
603                 } else {
604                         scan_offset += head->head.hdr_size;
605                 }
606         }
607         if (data_buffer)
608                 rel_buffer(data_buffer);
609 }