sbin/hammer2: Refactor show_bref()
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Wed, 14 Aug 2019 15:36:33 +0000 (00:36 +0900)
committerTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Thu, 15 Aug 2019 15:13:33 +0000 (00:13 +0900)
No functional difference.
Read media buf, get bscan pointer, and calculate bcount first.

sbin/hammer2/cmd_debug.c

index 4e6d648..a362895 100644 (file)
@@ -463,6 +463,43 @@ show_bref(hammer2_volume_data_t *voldata, int fd, int tab,
        uint32_t cv;
        uint64_t cv64;
 
+       bytes = (bref->data_off & HAMMER2_OFF_MASK_RADIX);
+       if (bytes)
+               bytes = (size_t)1 << bytes;
+       if (bytes) {
+               hammer2_off_t io_off;
+               hammer2_off_t io_base;
+               size_t io_bytes;
+               size_t boff;
+
+               io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
+               io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
+               boff = io_off - io_base;
+
+               io_bytes = HAMMER2_MINIOSIZE;
+               while (io_bytes + boff < bytes)
+                       io_bytes <<= 1;
+
+               if (io_bytes > sizeof(media)) {
+                       printf("(bad block size %zd)\n", bytes);
+                       return;
+               }
+               if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
+                       lseek(fd, io_base, 0);
+                       if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
+                               printf("(media read failed)\n");
+                               return;
+                       }
+                       if (boff)
+                               bcopy((char *)&media + boff, &media, bytes);
+               }
+       }
+
+       bscan = NULL;
+       bcount = 0;
+       namelen = 0;
+       failed = 0;
+
        switch(bref->type) {
        case HAMMER2_BREF_TYPE_EMPTY:
                type_str = "empty";
@@ -496,6 +533,31 @@ show_bref(hammer2_volume_data_t *voldata, int fd, int tab,
                break;
        }
 
+       switch(bref->type) {
+       case HAMMER2_BREF_TYPE_INODE:
+               if (!(media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA)) {
+                       bscan = &media.ipdata.u.blockset.blockref[0];
+                       bcount = HAMMER2_SET_COUNT;
+               }
+               break;
+       case HAMMER2_BREF_TYPE_INDIRECT:
+               bscan = &media.npdata[0];
+               bcount = bytes / sizeof(hammer2_blockref_t);
+               break;
+       case HAMMER2_BREF_TYPE_VOLUME:
+               bscan = &media.voldata.sroot_blockset.blockref[0];
+               bcount = HAMMER2_SET_COUNT;
+               break;
+       case HAMMER2_BREF_TYPE_FREEMAP:
+               bscan = &media.voldata.freemap_blockset.blockref[0];
+               bcount = HAMMER2_SET_COUNT;
+               break;
+       case HAMMER2_BREF_TYPE_FREEMAP_NODE:
+               bscan = &media.npdata[0];
+               bcount = bytes / sizeof(hammer2_blockref_t);
+               break;
+       }
+
        tabprintf(tab,
                  "%s.%-3d %016jx %016jx/%-2d "
                  "mir=%016jx mod=%016jx leafcnt=%d ",
@@ -512,43 +574,6 @@ show_bref(hammer2_volume_data_t *voldata, int fd, int tab,
                        bref->check.freemap.bigmask, bref->check.freemap.avail);
        }
 
-       bytes = (bref->data_off & HAMMER2_OFF_MASK_RADIX);
-       if (bytes)
-               bytes = (size_t)1 << bytes;
-       if (bytes) {
-               hammer2_off_t io_off;
-               hammer2_off_t io_base;
-               size_t io_bytes;
-               size_t boff;
-
-               io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
-               io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
-               boff = io_off - io_base;
-
-               io_bytes = HAMMER2_MINIOSIZE;
-               while (io_bytes + boff < bytes)
-                       io_bytes <<= 1;
-
-               if (io_bytes > sizeof(media)) {
-                       printf("(bad block size %zd)\n", bytes);
-                       return;
-               }
-               if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
-                       lseek(fd, io_base, 0);
-                       if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
-                               printf("(media read failed)\n");
-                               return;
-                       }
-                       if (boff)
-                               bcopy((char *)&media + boff, &media, bytes);
-               }
-       }
-
-       bscan = NULL;
-       bcount = 0;
-       namelen = 0;
-       failed = 0;
-
        /*
         * Check data integrity in verbose mode, otherwise we are just doing
         * a quick meta-data scan.  Meta-data integrity is always checked.
@@ -612,31 +637,6 @@ show_bref(hammer2_volume_data_t *voldata, int fd, int tab,
                }
        }
 
-       switch(bref->type) {
-       case HAMMER2_BREF_TYPE_INODE:
-               if (!(media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA)) {
-                       bscan = &media.ipdata.u.blockset.blockref[0];
-                       bcount = HAMMER2_SET_COUNT;
-               }
-               break;
-       case HAMMER2_BREF_TYPE_INDIRECT:
-               bscan = &media.npdata[0];
-               bcount = bytes / sizeof(hammer2_blockref_t);
-               break;
-       case HAMMER2_BREF_TYPE_VOLUME:
-               bscan = &media.voldata.sroot_blockset.blockref[0];
-               bcount = HAMMER2_SET_COUNT;
-               break;
-       case HAMMER2_BREF_TYPE_FREEMAP:
-               bscan = &media.voldata.freemap_blockset.blockref[0];
-               bcount = HAMMER2_SET_COUNT;
-               break;
-       case HAMMER2_BREF_TYPE_FREEMAP_NODE:
-               bscan = &media.npdata[0];
-               bcount = bytes / sizeof(hammer2_blockref_t);
-               break;
-       }
-
        if (QuietOpt > 0) {
                obrace = 0;
                printf("\n");