btrfs-progs: cleanup unused assignment for chunk-recover
[platform/upstream/btrfs-progs.git] / print-tree.c
index 7c615dd..cb5c2e1 100644 (file)
 #include "ctree.h"
 #include "disk-io.h"
 #include "print-tree.h"
+#include "utils.h"
+
+
+static void print_dir_item_type(struct extent_buffer *eb,
+                                struct btrfs_dir_item *di)
+{
+       u8 type = btrfs_dir_type(eb, di);
+
+       switch (type) {
+       case BTRFS_FT_REG_FILE:
+               printf("FILE");
+               break;
+       case BTRFS_FT_DIR:
+               printf("DIR");
+               break;
+       case BTRFS_FT_CHRDEV:
+               printf("CHRDEV");
+               break;
+       case BTRFS_FT_BLKDEV:
+               printf("BLKDEV");
+               break;
+       case BTRFS_FT_FIFO:
+               printf("FIFO");
+               break;
+       case BTRFS_FT_SOCK:
+               printf("SOCK");
+               break;
+       case BTRFS_FT_SYMLINK:
+               printf("SYMLINK");
+               break;
+       case BTRFS_FT_XATTR:
+               printf("XATTR");
+               break;
+       default:
+               printf("%u", type);
+       }
+}
 
 static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
                          struct btrfs_dir_item *di)
@@ -41,7 +78,9 @@ static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
                btrfs_dir_item_key(eb, di, &location);
                printf("\t\tlocation ");
                btrfs_print_key(&location);
-               printf(" type %u\n", btrfs_dir_type(eb, di));
+               printf(" type ");
+               print_dir_item_type(eb, di);
+               printf("\n");
                name_len = btrfs_dir_name_len(eb, di);
                data_len = btrfs_dir_data_len(eb, di);
                len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
@@ -61,6 +100,42 @@ static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
        return 0;
 }
 
+static int print_inode_extref_item(struct extent_buffer *eb,
+                                  struct btrfs_item *item,
+                                  struct btrfs_inode_extref *extref)
+{
+       u32 total;
+       u32 cur = 0;
+       u32 len;
+       u32 name_len = 0;
+       u64 index = 0;
+       u64 parent_objid;
+       char namebuf[BTRFS_NAME_LEN];
+
+       total = btrfs_item_size(eb, item);
+
+       while (cur < total) {
+               index = btrfs_inode_extref_index(eb, extref);
+               name_len = btrfs_inode_extref_name_len(eb, extref);
+               parent_objid = btrfs_inode_extref_parent(eb, extref);
+
+               len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
+
+               read_extent_buffer(eb, namebuf, (unsigned long)(extref->name), len);
+
+               printf("\t\tinode extref index %llu parent %llu namelen %u "
+                      "name: %.*s\n",
+                      (unsigned long long)index,
+                      (unsigned long long)parent_objid,
+                      name_len, len, namebuf);
+
+               len = sizeof(*extref) + name_len;
+               extref = (struct btrfs_inode_extref *)((char *)extref + len);
+               cur += len;
+       }
+       return 0;
+}
+
 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
                                struct btrfs_inode_ref *ref)
 {
@@ -85,7 +160,7 @@ static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *ite
        return 0;
 }
 
-static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
+void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
 {
        int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
        int i;
@@ -104,36 +179,46 @@ static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
 static void print_dev_item(struct extent_buffer *eb,
                           struct btrfs_dev_item *dev_item)
 {
+       char disk_uuid_c[BTRFS_UUID_UNPARSED_SIZE];
+       u8 disk_uuid[BTRFS_UUID_SIZE];
+
+       read_extent_buffer(eb, disk_uuid,
+                          (unsigned long)btrfs_device_uuid(dev_item),
+                          BTRFS_UUID_SIZE);
+       uuid_unparse(disk_uuid, disk_uuid_c);
        printf("\t\tdev item devid %llu "
-              "total_bytes %llu bytes used %Lu\n",
+              "total_bytes %llu bytes used %Lu\n"
+              "\t\tdev uuid %s\n",
               (unsigned long long)btrfs_device_id(eb, dev_item),
               (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
-              (unsigned long long)btrfs_device_bytes_used(eb, dev_item));
+              (unsigned long long)btrfs_device_bytes_used(eb, dev_item),
+              disk_uuid_c);
 }
 
 static void print_uuids(struct extent_buffer *eb)
 {
-       char fs_uuid[37];
-       char chunk_uuid[37];
+       char fs_uuid[BTRFS_UUID_UNPARSED_SIZE];
+       char chunk_uuid[BTRFS_UUID_UNPARSED_SIZE];
        u8 disk_uuid[BTRFS_UUID_SIZE];
 
-       read_extent_buffer(eb, disk_uuid, (unsigned long)btrfs_header_fsid(eb),
+       read_extent_buffer(eb, disk_uuid, btrfs_header_fsid(),
                           BTRFS_FSID_SIZE);
 
-       fs_uuid[36] = '\0';
+       fs_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
        uuid_unparse(disk_uuid, fs_uuid);
 
        read_extent_buffer(eb, disk_uuid,
-                          (unsigned long)btrfs_header_chunk_tree_uuid(eb),
+                          btrfs_header_chunk_tree_uuid(eb),
                           BTRFS_UUID_SIZE);
 
-       chunk_uuid[36] = '\0';
+       chunk_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
        uuid_unparse(disk_uuid, chunk_uuid);
        printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
 }
 
 static void print_file_extent_item(struct extent_buffer *eb,
                                   struct btrfs_item *item,
+                                  int slot,
                                   struct btrfs_file_extent_item *fi)
 {
        int extent_type = btrfs_file_extent_type(eb, fi);
@@ -142,7 +227,7 @@ static void print_file_extent_item(struct extent_buffer *eb,
                printf("\t\tinline extent data size %u "
                       "ram %u compress %d\n",
                  btrfs_file_extent_inline_item_len(eb, item),
-                 btrfs_file_extent_inline_len(eb, fi),
+                 btrfs_file_extent_inline_len(eb, slot, fi),
                  btrfs_file_extent_compression(eb, fi));
                return;
        }
@@ -166,7 +251,7 @@ static void print_file_extent_item(struct extent_buffer *eb,
               btrfs_file_extent_compression(eb, fi));
 }
 
-static void print_extent_item(struct extent_buffer *eb, int slot)
+void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
 {
        struct btrfs_extent_item *ei;
        struct btrfs_extent_inline_ref *iref;
@@ -201,7 +286,7 @@ static void print_extent_item(struct extent_buffer *eb, int slot)
               (unsigned long long)btrfs_extent_generation(eb, ei),
               (unsigned long long)flags);
 
-       if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
+       if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !metadata) {
                struct btrfs_tree_block_info *info;
                info = (struct btrfs_tree_block_info *)(ei + 1);
                btrfs_tree_block_key(eb, info, &key);
@@ -209,7 +294,13 @@ static void print_extent_item(struct extent_buffer *eb, int slot)
                btrfs_print_key(&key);
                printf(" level %d\n", btrfs_tree_block_level(eb, info));
                iref = (struct btrfs_extent_inline_ref *)(info + 1);
-       } else {
+       } else if (metadata) {
+               struct btrfs_key tmp;
+
+               btrfs_item_key_to_cpu(eb, &tmp, slot);
+               printf("\t\ttree block skinny level %d\n", (int)tmp.offset);
+               iref = (struct btrfs_extent_inline_ref *)(ei + 1);
+       } else{
                iref = (struct btrfs_extent_inline_ref *)(ei + 1);
        }
 
@@ -297,7 +388,7 @@ static void print_root(struct extent_buffer *leaf, int slot)
        struct btrfs_root_item *ri;
        struct btrfs_root_item root_item;
        int len;
-       char uuid_str[128];
+       char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
 
        ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
        len = btrfs_item_size_nr(leaf, slot);
@@ -371,6 +462,9 @@ static void print_key_type(u64 objectid, u8 type)
        case BTRFS_INODE_REF_KEY:
                printf("INODE_REF");
                break;
+       case BTRFS_INODE_EXTREF_KEY:
+               printf("INODE_EXTREF");
+               break;
        case BTRFS_DIR_ITEM_KEY:
                printf("DIR_ITEM");
                break;
@@ -401,6 +495,9 @@ static void print_key_type(u64 objectid, u8 type)
        case BTRFS_EXTENT_ITEM_KEY:
                printf("EXTENT_ITEM");
                break;
+       case BTRFS_METADATA_ITEM_KEY:
+               printf("METADATA_ITEM");
+               break;
        case BTRFS_TREE_BLOCK_REF_KEY:
                printf("TREE_BLOCK_REF");
                break;
@@ -440,6 +537,9 @@ static void print_key_type(u64 objectid, u8 type)
        case BTRFS_BALANCE_ITEM_KEY:
                printf("BALANCE_ITEM");
                break;
+       case BTRFS_DEV_REPLACE_KEY:
+               printf("DEV_REPLACE_ITEM");
+               break;
        case BTRFS_STRING_ITEM_KEY:
                printf("STRING_ITEM");
                break;
@@ -455,6 +555,15 @@ static void print_key_type(u64 objectid, u8 type)
        case BTRFS_QGROUP_LIMIT_KEY:
                printf("BTRFS_QGROUP_LIMIT_KEY");
                break;
+       case BTRFS_DEV_STATS_KEY:
+               printf("DEV_STATS_ITEM");
+               break;
+       case BTRFS_UUID_KEY_SUBVOL:
+               printf("BTRFS_UUID_KEY_SUBVOL");
+               break;
+       case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
+               printf("BTRFS_UUID_KEY_RECEIVED_SUBVOL");
+               break;
        default:
                printf("UNKNOWN.%d", type);
        };
@@ -462,15 +571,18 @@ static void print_key_type(u64 objectid, u8 type)
 
 static void print_objectid(u64 objectid, u8 type)
 {
-       if (type == BTRFS_DEV_EXTENT_KEY) {
+       switch (type) {
+       case BTRFS_DEV_EXTENT_KEY:
                printf("%llu", (unsigned long long)objectid); /* device id */
                return;
-       }
-       switch (type) {
        case BTRFS_QGROUP_RELATION_KEY:
                printf("%llu/%llu", objectid >> 48,
                        objectid & ((1ll << 48) - 1));
                return;
+       case BTRFS_UUID_KEY_SUBVOL:
+       case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
+               printf("0x%016llx", (unsigned long long)objectid);
+               return;
        }
 
        switch (objectid) {
@@ -524,12 +636,19 @@ static void print_objectid(u64 objectid, u8 type)
                break;
        case BTRFS_FREE_INO_OBJECTID:
                printf("FREE_INO");
+               break;
        case BTRFS_QUOTA_TREE_OBJECTID:
                printf("QUOTA_TREE");
                break;
+       case BTRFS_UUID_TREE_OBJECTID:
+               printf("UUID_TREE");
+               break;
        case BTRFS_MULTIPLE_OBJECTIDS:
                printf("MULTIPLE");
                break;
+       case (u64)-1:
+               printf("-1");
+               break;
        case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
                if (type == BTRFS_CHUNK_ITEM_KEY) {
                        printf("FIRST_CHUNK_TREE");
@@ -558,12 +677,38 @@ void btrfs_print_key(struct btrfs_disk_key *disk_key)
                printf(" %llu/%llu)", (unsigned long long)(offset >> 48),
                        (unsigned long long)(offset & ((1ll << 48) - 1)));
                break;
+       case BTRFS_UUID_KEY_SUBVOL:
+       case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
+               printf(" 0x%016llx)", (unsigned long long)offset);
+               break;
        default:
-               printf(" %llu)", (unsigned long long)offset);
+               if (offset == (u64)-1)
+                       printf(" -1)");
+               else
+                       printf(" %llu)", (unsigned long long)offset);
                break;
        }
 }
 
+static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
+                           u32 item_size)
+{
+       if (item_size & (sizeof(u64) - 1)) {
+               printf("btrfs: uuid item with illegal size %lu!\n",
+                      (unsigned long)item_size);
+               return;
+       }
+       while (item_size) {
+               __le64 subvol_id;
+
+               read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
+               printf("\t\tsubvol_id %llu\n",
+                       (unsigned long long)le64_to_cpu(subvol_id));
+               item_size -= sizeof(u64);
+               offset += sizeof(u64);
+       }
+}
+
 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
 {
        int i;
@@ -576,6 +721,7 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
        struct btrfs_extent_data_ref *dref;
        struct btrfs_shared_data_ref *sref;
        struct btrfs_inode_ref *iref;
+       struct btrfs_inode_extref *iref2;
        struct btrfs_dev_extent *dev_extent;
        struct btrfs_disk_key disk_key;
        struct btrfs_block_group_item bg_item;
@@ -595,7 +741,7 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
        print_uuids(l);
        fflush(stdout);
        for (i = 0 ; i < nr ; i++) {
-               item = btrfs_item_nr(l, i);
+               item = btrfs_item_nr(i);
                btrfs_item_key(l, &disk_key, i);
                objectid = btrfs_disk_key_objectid(&disk_key);
                type = btrfs_disk_key_type(&disk_key);
@@ -623,6 +769,10 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
                        iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
                        print_inode_ref_item(l, item, iref);
                        break;
+               case BTRFS_INODE_EXTREF_KEY:
+                       iref2 = btrfs_item_ptr(l, i, struct btrfs_inode_extref);
+                       print_inode_extref_item(l, item, iref2);
+                       break;
                case BTRFS_DIR_ITEM_KEY:
                case BTRFS_DIR_INDEX_KEY:
                case BTRFS_XATTR_ITEM_KEY:
@@ -648,7 +798,10 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
                        print_root_ref(l, i, "backref");
                        break;
                case BTRFS_EXTENT_ITEM_KEY:
-                       print_extent_item(l, i);
+                       print_extent_item(l, i, 0);
+                       break;
+               case BTRFS_METADATA_ITEM_KEY:
+                       print_extent_item(l, i, 1);
                        break;
                case BTRFS_TREE_BLOCK_REF_KEY:
                        printf("\t\ttree block backref\n");
@@ -686,7 +839,7 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
                case BTRFS_EXTENT_DATA_KEY:
                        fi = btrfs_item_ptr(l, i,
                                            struct btrfs_file_extent_item);
-                       print_file_extent_item(l, item, fi);
+                       print_file_extent_item(l, item, i, fi);
                        break;
                case BTRFS_BLOCK_GROUP_ITEM_KEY:
                        bi = btrfs_item_ptr(l, i,
@@ -740,18 +893,18 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
                        qg_info = btrfs_item_ptr(l, i,
                                                 struct btrfs_qgroup_info_item);
                        printf("\t\tgeneration %llu\n"
-                            "\t\treferenced %lld referenced compressed %lld\n"
-                            "\t\texclusive %lld exclusive compressed %lld\n",
+                            "\t\treferenced %llu referenced compressed %llu\n"
+                            "\t\texclusive %llu exclusive compressed %llu\n",
                               (unsigned long long)
                               btrfs_qgroup_info_generation(l, qg_info),
-                              (long long)
+                              (unsigned long long)
                               btrfs_qgroup_info_referenced(l, qg_info),
-                              (long long)
+                              (unsigned long long)
                               btrfs_qgroup_info_referenced_compressed(l,
                                                                       qg_info),
-                              (long long)
+                              (unsigned long long)
                               btrfs_qgroup_info_exclusive(l, qg_info),
-                              (long long)
+                              (unsigned long long)
                               btrfs_qgroup_info_exclusive_compressed(l,
                                                                      qg_info));
                        break;
@@ -772,11 +925,19 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
                               (long long)
                               btrfs_qgroup_limit_rsv_exclusive(l, qg_limit));
                        break;
+               case BTRFS_UUID_KEY_SUBVOL:
+               case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
+                       print_uuid_item(l, btrfs_item_ptr_offset(l, i),
+                                       btrfs_item_size_nr(l, i));
+                       break;
                case BTRFS_STRING_ITEM_KEY:
                        /* dirty, but it's simple */
                        str = l->data + btrfs_item_ptr_offset(l, i);
                        printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
                        break;
+               case BTRFS_DEV_STATS_KEY:
+                       printf("\t\tdevice stats\n");
+                       break;
                };
                fflush(stdout);
        }