btrfs-progs: Fix typos in docs and user-facing strings
[platform/upstream/btrfs-progs.git] / print-tree.c
index 32d8a61..2dbed72 100644 (file)
@@ -198,9 +198,17 @@ void print_chunk_item(struct extent_buffer *eb, struct btrfs_chunk *chunk)
 {
        u16 num_stripes = btrfs_chunk_num_stripes(eb, chunk);
        int i;
-       u32 chunk_item_size = btrfs_chunk_item_size(num_stripes);
+       u32 chunk_item_size;
        char chunk_flags_str[32] = {0};
 
+       /* The chunk must contain at least one stripe */
+       if (num_stripes < 1) {
+               printf("invalid num_stripes: %u\n", num_stripes);
+               return;
+       }
+
+       chunk_item_size = btrfs_chunk_item_size(num_stripes);
+
        if ((unsigned long)chunk + chunk_item_size > eb->len) {
                printf("\t\tchunk item invalid\n");
                return;
@@ -315,6 +323,9 @@ static void compress_type_to_str(u8 compress_type, char *ret)
        case BTRFS_COMPRESS_LZO:
                strcpy(ret, "lzo");
                break;
+       case BTRFS_COMPRESS_ZSTD:
+               strcpy(ret, "zstd");
+               break;
        default:
                sprintf(ret, "UNKNOWN.%d", compress_type);
        }
@@ -540,6 +551,26 @@ static void root_flags_to_str(u64 flags, char *ret)
                strcat(ret, "none");
 }
 
+static void print_timespec(struct extent_buffer *eb,
+               struct btrfs_timespec *timespec, const char *prefix,
+               const char *suffix)
+{
+       struct tm tm;
+       u64 tmp_u64;
+       u32 tmp_u32;
+       time_t tmp_time;
+       char timestamp[256];
+
+       tmp_u64 = btrfs_timespec_sec(eb, timespec);
+       tmp_u32 = btrfs_timespec_nsec(eb, timespec);
+       tmp_time = tmp_u64;
+       localtime_r(&tmp_time, &tm);
+       strftime(timestamp, sizeof(timestamp),
+                       "%Y-%m-%d %H:%M:%S", &tm);
+       printf("%s%llu.%u (%s)%s", prefix, (unsigned long long)tmp_u64, tmp_u32,
+                       timestamp, suffix);
+}
+
 static void print_root_item(struct extent_buffer *leaf, int slot)
 {
        struct btrfs_root_item *ri;
@@ -587,6 +618,18 @@ static void print_root_item(struct extent_buffer *leaf, int slot)
                                btrfs_root_stransid(&root_item),
                                btrfs_root_rtransid(&root_item));
                }
+               if (btrfs_timespec_sec(leaf, btrfs_root_ctime(ri)))
+                       print_timespec(leaf, btrfs_root_ctime(ri),
+                                       "\t\tctime ", "\n");
+               if (btrfs_timespec_sec(leaf, btrfs_root_otime(ri)))
+                       print_timespec(leaf, btrfs_root_otime(ri),
+                                       "\t\totime ", "\n");
+               if (btrfs_timespec_sec(leaf, btrfs_root_stime(ri)))
+                       print_timespec(leaf, btrfs_root_stime(ri),
+                                       "\t\tstime ", "\n");
+               if (btrfs_timespec_sec(leaf, btrfs_root_rtime(ri)))
+                       print_timespec(leaf, btrfs_root_rtime(ri),
+                                       "\t\trtime ", "\n");
        }
 
        btrfs_disk_key_to_cpu(&drop_key, &root_item.drop_progress);
@@ -783,6 +826,26 @@ void btrfs_print_key(struct btrfs_disk_key *disk_key)
        case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
                printf(" 0x%016llx)", (unsigned long long)offset);
                break;
+
+       /*
+        * Key offsets of ROOT_ITEM point to tree root, print them in human
+        * readable format.  Especially useful for trees like data/tree reloc
+        * tree, whose tree id can be negative.
+        */
+       case BTRFS_ROOT_ITEM_KEY:
+               printf(" ");
+               /*
+                * Normally offset of ROOT_ITEM should present the generation
+                * of creation time of the root.
+                * However if this is reloc tree, offset is the subvolume
+                * id of its source. Here we do extra check on this.
+                */
+               if (objectid == BTRFS_TREE_RELOC_OBJECTID)
+                       print_objectid(stdout, offset, type);
+               else
+                       printf("%lld", offset);
+               printf(")");
+               break;
        default:
                if (offset == (u64)-1)
                        printf(" -1)");
@@ -845,26 +908,6 @@ static void inode_flags_to_str(u64 flags, char *ret)
                strcat(ret, "none");
 }
 
-static void print_timespec(struct extent_buffer *eb,
-               struct btrfs_timespec *timespec, const char *prefix,
-               const char *suffix)
-{
-       struct tm tm;
-       u64 tmp_u64;
-       u32 tmp_u32;
-       time_t tmp_time;
-       char timestamp[256];
-
-       tmp_u64 = btrfs_timespec_sec(eb, timespec);
-       tmp_u32 = btrfs_timespec_nsec(eb, timespec);
-       tmp_time = tmp_u64;
-       localtime_r(&tmp_time, &tm);
-       strftime(timestamp, sizeof(timestamp),
-                       "%Y-%m-%d %H:%M:%S", &tm);
-       printf("%s%llu.%u (%s)%s", prefix, (unsigned long long)tmp_u64, tmp_u32,
-                       timestamp, suffix);
-}
-
 static void print_inode_item(struct extent_buffer *eb,
                struct btrfs_inode_item *ii)
 {
@@ -885,8 +928,8 @@ static void print_inode_item(struct extent_buffer *eb,
               btrfs_inode_uid(eb, ii),
               btrfs_inode_gid(eb, ii),
               (unsigned long long)btrfs_inode_rdev(eb,ii),
-              (unsigned long long)btrfs_inode_flags(eb,ii),
               (unsigned long long)btrfs_inode_sequence(eb, ii),
+              (unsigned long long)btrfs_inode_flags(eb,ii),
               flags_str);
        print_timespec(eb, btrfs_inode_atime(ii), "\t\tatime ", "\n");
        print_timespec(eb, btrfs_inode_ctime(ii), "\t\tctime ", "\n");
@@ -970,6 +1013,150 @@ static void print_block_group_item(struct extent_buffer *eb,
                flags_str);
 }
 
+static void print_extent_data_ref(struct extent_buffer *eb, int slot)
+{
+       struct btrfs_extent_data_ref *dref;
+
+       dref = btrfs_item_ptr(eb, slot, struct btrfs_extent_data_ref);
+       printf("\t\textent data backref root %llu "
+               "objectid %llu offset %llu count %u\n",
+               (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
+               (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
+               (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
+               btrfs_extent_data_ref_count(eb, dref));
+}
+
+static void print_shared_data_ref(struct extent_buffer *eb, int slot)
+{
+       struct btrfs_shared_data_ref *sref;
+
+       sref = btrfs_item_ptr(eb, slot, struct btrfs_shared_data_ref);
+       printf("\t\tshared data backref count %u\n",
+               btrfs_shared_data_ref_count(eb, sref));
+}
+
+static void print_free_space_info(struct extent_buffer *eb, int slot)
+{
+       struct btrfs_free_space_info *free_info;
+
+       free_info = btrfs_item_ptr(eb, slot, struct btrfs_free_space_info);
+       printf("\t\tfree space info extent count %u flags %u\n",
+               (unsigned)btrfs_free_space_extent_count(eb, free_info),
+               (unsigned)btrfs_free_space_flags(eb, free_info));
+}
+
+static void print_dev_extent(struct extent_buffer *eb, int slot)
+{
+       struct btrfs_dev_extent *dev_extent;
+       u8 uuid[BTRFS_UUID_SIZE];
+       char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
+
+       dev_extent = btrfs_item_ptr(eb, slot, struct btrfs_dev_extent);
+       read_extent_buffer(eb, uuid,
+               (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
+               BTRFS_UUID_SIZE);
+       uuid_unparse(uuid, uuid_str);
+       printf("\t\tdev extent chunk_tree %llu\n"
+               "\t\tchunk_objectid %llu chunk_offset %llu "
+               "length %llu\n"
+               "\t\tchunk_tree_uuid %s\n",
+               (unsigned long long)btrfs_dev_extent_chunk_tree(eb, dev_extent),
+               (unsigned long long)btrfs_dev_extent_chunk_objectid(eb, dev_extent),
+               (unsigned long long)btrfs_dev_extent_chunk_offset(eb, dev_extent),
+               (unsigned long long)btrfs_dev_extent_length(eb, dev_extent),
+               uuid_str);
+}
+
+static void print_qgroup_status(struct extent_buffer *eb, int slot)
+{
+       struct btrfs_qgroup_status_item *qg_status;
+       char flags_str[256];
+
+       qg_status = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_status_item);
+       memset(flags_str, 0, sizeof(flags_str));
+       qgroup_flags_to_str(btrfs_qgroup_status_flags(eb, qg_status),
+                                       flags_str);
+       printf("\t\tversion %llu generation %llu flags %s scan %lld\n",
+               (unsigned long long)btrfs_qgroup_status_version(eb, qg_status),
+               (unsigned long long)btrfs_qgroup_status_generation(eb, qg_status),
+               flags_str,
+               (unsigned long long)btrfs_qgroup_status_rescan(eb, qg_status));
+}
+
+static void print_qgroup_info(struct extent_buffer *eb, int slot)
+{
+       struct btrfs_qgroup_info_item *qg_info;
+
+       qg_info = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_info_item);
+       printf("\t\tgeneration %llu\n"
+               "\t\treferenced %llu referenced_compressed %llu\n"
+               "\t\texclusive %llu exclusive_compressed %llu\n",
+               (unsigned long long)btrfs_qgroup_info_generation(eb, qg_info),
+               (unsigned long long)btrfs_qgroup_info_referenced(eb, qg_info),
+               (unsigned long long)btrfs_qgroup_info_referenced_compressed(eb,
+                                                                      qg_info),
+               (unsigned long long)btrfs_qgroup_info_exclusive(eb, qg_info),
+               (unsigned long long)btrfs_qgroup_info_exclusive_compressed(eb,
+                                                                     qg_info));
+}
+
+static void print_qgroup_limit(struct extent_buffer *eb, int slot)
+{
+       struct btrfs_qgroup_limit_item *qg_limit;
+
+       qg_limit = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_limit_item);
+       printf("\t\tflags %llx\n"
+               "\t\tmax_referenced %lld max_exclusive %lld\n"
+               "\t\trsv_referenced %lld rsv_exclusive %lld\n",
+               (unsigned long long)btrfs_qgroup_limit_flags(eb, qg_limit),
+               (long long)btrfs_qgroup_limit_max_referenced(eb, qg_limit),
+               (long long)btrfs_qgroup_limit_max_exclusive(eb, qg_limit),
+               (long long)btrfs_qgroup_limit_rsv_referenced(eb, qg_limit),
+               (long long)btrfs_qgroup_limit_rsv_exclusive(eb, qg_limit));
+}
+
+static void print_persistent_item(struct extent_buffer *eb, void *ptr,
+               u32 item_size, u64 objectid, u64 offset)
+{
+       printf("\t\tpersistent item objectid ");
+       print_objectid(stdout, objectid, BTRFS_PERSISTENT_ITEM_KEY);
+       printf(" offset %llu\n", (unsigned long long)offset);
+       switch (objectid) {
+       case BTRFS_DEV_STATS_OBJECTID:
+               print_dev_stats(eb, ptr, item_size);
+               break;
+       default:
+               printf("\t\tunknown persistent item objectid %llu\n", objectid);
+       }
+}
+
+static void print_temporary_item(struct extent_buffer *eb, void *ptr,
+               u64 objectid, u64 offset)
+{
+       printf("\t\ttemporary item objectid ");
+       print_objectid(stdout, objectid, BTRFS_TEMPORARY_ITEM_KEY);
+       printf(" offset %llu\n", (unsigned long long)offset);
+       switch (objectid) {
+       case BTRFS_BALANCE_OBJECTID:
+               print_balance_item(eb, ptr);
+               break;
+       default:
+               printf("\t\tunknown temporary item objectid %llu\n", objectid);
+       }
+}
+
+static void print_extent_csum(struct extent_buffer *eb,
+               struct btrfs_fs_info *fs_info, u32 item_size, u64 start)
+{
+       u32 size;
+
+       size = (item_size / btrfs_super_csum_size(fs_info->super_copy)) *
+                       fs_info->sectorsize;
+       printf("\t\trange start %llu end %llu length %u\n",
+                       (unsigned long long)start,
+                       (unsigned long long)start + size, size);
+}
+
 /* Caller must ensure sizeof(*ret) >= 14 "WRITTEN|RELOC" */
 static void header_flags_to_str(u64 flags, char *ret)
 {
@@ -1017,9 +1204,6 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *eb)
                u64 objectid;
                u32 type;
                u64 offset;
-               char flags_str[256];
-               char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
-               u8 uuid[BTRFS_UUID_SIZE];
 
                item = btrfs_item_nr(i);
                item_size = btrfs_item_size(eb, item);
@@ -1088,25 +1272,12 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *eb)
                case BTRFS_SHARED_BLOCK_REF_KEY:
                        printf("\t\tshared block backref\n");
                        break;
-               case BTRFS_EXTENT_DATA_REF_KEY: {
-                       struct btrfs_extent_data_ref *dref;
-
-                       dref = btrfs_item_ptr(eb, i, struct btrfs_extent_data_ref);
-                       printf("\t\textent data backref root %llu "
-                              "objectid %llu offset %llu count %u\n",
-                              (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
-                              (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
-                              (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
-                              btrfs_extent_data_ref_count(eb, dref));
+               case BTRFS_EXTENT_DATA_REF_KEY:
+                       print_extent_data_ref(eb, i);
                        break;
-                       }
-               case BTRFS_SHARED_DATA_REF_KEY: {
-                       struct btrfs_shared_data_ref *sref;
-                       sref = btrfs_item_ptr(eb, i, struct btrfs_shared_data_ref);
-                       printf("\t\tshared data backref count %u\n",
-                              btrfs_shared_data_ref_count(eb, sref));
+               case BTRFS_SHARED_DATA_REF_KEY:
+                       print_shared_data_ref(eb, i);
                        break;
-                       }
                case BTRFS_EXTENT_REF_V0_KEY:
 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
                        print_extent_ref_v0(eb, i);
@@ -1118,7 +1289,8 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *eb)
                        printf("\t\tcsum item\n");
                        break;
                case BTRFS_EXTENT_CSUM_KEY:
-                       printf("\t\textent csum item\n");
+                       print_extent_csum(eb, root->fs_info, item_size,
+                                       offset);
                        break;
                case BTRFS_EXTENT_DATA_KEY:
                        print_file_extent_item(eb, item, i, ptr);
@@ -1126,15 +1298,9 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *eb)
                case BTRFS_BLOCK_GROUP_ITEM_KEY:
                        print_block_group_item(eb, ptr);
                        break;
-               case BTRFS_FREE_SPACE_INFO_KEY: {
-                       struct btrfs_free_space_info *free_info;
-
-                       free_info = btrfs_item_ptr(eb, i, struct btrfs_free_space_info);
-                       printf("\t\tfree space info extent count %u flags %u\n",
-                              (unsigned)btrfs_free_space_extent_count(eb, free_info),
-                              (unsigned)btrfs_free_space_flags(eb, free_info));
+               case BTRFS_FREE_SPACE_INFO_KEY:
+                       print_free_space_info(eb, i);
                        break;
-                       }
                case BTRFS_FREE_SPACE_EXTENT_KEY:
                        printf("\t\tfree space extent\n");
                        break;
@@ -1147,93 +1313,20 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *eb)
                case BTRFS_DEV_ITEM_KEY:
                        print_dev_item(eb, ptr);
                        break;
-               case BTRFS_DEV_EXTENT_KEY: {
-                       struct btrfs_dev_extent *dev_extent;
-
-                       dev_extent = btrfs_item_ptr(eb, i,
-                                                   struct btrfs_dev_extent);
-                       read_extent_buffer(eb, uuid,
-                               (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
-                               BTRFS_UUID_SIZE);
-                       uuid_unparse(uuid, uuid_str);
-                       printf("\t\tdev extent chunk_tree %llu\n"
-                              "\t\tchunk_objectid %llu chunk_offset %llu "
-                              "length %llu\n"
-                              "\t\tchunk_tree_uuid %s\n",
-                              (unsigned long long)
-                              btrfs_dev_extent_chunk_tree(eb, dev_extent),
-                              (unsigned long long)
-                              btrfs_dev_extent_chunk_objectid(eb, dev_extent),
-                              (unsigned long long)
-                              btrfs_dev_extent_chunk_offset(eb, dev_extent),
-                              (unsigned long long)
-                              btrfs_dev_extent_length(eb, dev_extent),
-                              uuid_str);
+               case BTRFS_DEV_EXTENT_KEY:
+                       print_dev_extent(eb, i);
                        break;
-                       }
-               case BTRFS_QGROUP_STATUS_KEY: {
-                       struct btrfs_qgroup_status_item *qg_status;
-
-                       qg_status = btrfs_item_ptr(eb, i,
-                                       struct btrfs_qgroup_status_item);
-                       memset(flags_str, 0, sizeof(flags_str));
-                       qgroup_flags_to_str(btrfs_qgroup_status_flags(eb, qg_status),
-                                       flags_str);
-                       printf("\t\tversion %llu generation %llu flags %s "
-                               "scan %lld\n",
-                               (unsigned long long)
-                               btrfs_qgroup_status_version(eb, qg_status),
-                               (unsigned long long)
-                               btrfs_qgroup_status_generation(eb, qg_status),
-                               flags_str,
-                               (unsigned long long)
-                               btrfs_qgroup_status_rescan(eb, qg_status));
+               case BTRFS_QGROUP_STATUS_KEY:
+                       print_qgroup_status(eb, i);
                        break;
-                       }
                case BTRFS_QGROUP_RELATION_KEY:
                        break;
-               case BTRFS_QGROUP_INFO_KEY: {
-                       struct btrfs_qgroup_info_item *qg_info;
-
-                       qg_info = btrfs_item_ptr(eb, i,
-                                                struct btrfs_qgroup_info_item);
-                       printf("\t\tgeneration %llu\n"
-                            "\t\treferenced %llu referenced_compressed %llu\n"
-                            "\t\texclusive %llu exclusive_compressed %llu\n",
-                              (unsigned long long)
-                              btrfs_qgroup_info_generation(eb, qg_info),
-                              (unsigned long long)
-                              btrfs_qgroup_info_referenced(eb, qg_info),
-                              (unsigned long long)
-                              btrfs_qgroup_info_referenced_compressed(eb,
-                                                                      qg_info),
-                              (unsigned long long)
-                              btrfs_qgroup_info_exclusive(eb, qg_info),
-                              (unsigned long long)
-                              btrfs_qgroup_info_exclusive_compressed(eb,
-                                                                     qg_info));
+               case BTRFS_QGROUP_INFO_KEY:
+                       print_qgroup_info(eb, i);
                        break;
-                       }
-               case BTRFS_QGROUP_LIMIT_KEY: {
-                       struct btrfs_qgroup_limit_item *qg_limit;
-
-                       qg_limit = btrfs_item_ptr(eb, i,
-                                        struct btrfs_qgroup_limit_item);
-                       printf("\t\tflags %llx\n"
-                            "\t\tmax_referenced %lld max_exclusive %lld\n"
-                            "\t\trsv_referenced %lld rsv_exclusive %lld\n",
-                              (unsigned long long)
-                              btrfs_qgroup_limit_flags(eb, qg_limit),
-                              (long long)
-                              btrfs_qgroup_limit_max_referenced(eb, qg_limit),
-                              (long long)
-                              btrfs_qgroup_limit_max_exclusive(eb, qg_limit),
-                              (long long)
-                              btrfs_qgroup_limit_rsv_referenced(eb, qg_limit),
-                              (long long)
-                              btrfs_qgroup_limit_rsv_exclusive(eb, qg_limit));
+               case BTRFS_QGROUP_LIMIT_KEY:
+                       print_qgroup_limit(eb, i);
                        break;
-                       }
                case BTRFS_UUID_KEY_SUBVOL:
                case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
                        print_uuid_item(eb, btrfs_item_ptr_offset(eb, i),
@@ -1246,30 +1339,11 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *eb)
                        break;
                        }
                case BTRFS_PERSISTENT_ITEM_KEY:
-                       printf("\t\tpersistent item objectid ");
-                       print_objectid(stdout, objectid, BTRFS_PERSISTENT_ITEM_KEY);
-                       printf(" offset %llu\n", (unsigned long long)offset);
-                       switch (objectid) {
-                       case BTRFS_DEV_STATS_OBJECTID:
-                               print_dev_stats(eb, ptr, item_size);
-                               break;
-                       default:
-                               printf("\t\tunknown persistent item objectid %llu\n",
-                                               objectid);
-                       }
+                       print_persistent_item(eb, ptr, item_size, objectid,
+                                       offset);
                        break;
                case BTRFS_TEMPORARY_ITEM_KEY:
-                       printf("\t\ttemporary item objectid ");
-                       print_objectid(stdout, objectid, BTRFS_TEMPORARY_ITEM_KEY);
-                       printf(" offset %llu\n", (unsigned long long)offset);
-                       switch (objectid) {
-                       case BTRFS_BALANCE_OBJECTID:
-                               print_balance_item(eb, ptr);
-                               break;
-                       default:
-                               printf("\t\tunknown temporary item objectid %llu\n",
-                                               objectid);
-                       }
+                       print_temporary_item(eb, ptr, objectid, offset);
                        break;
                };
                fflush(stdout);
@@ -1294,7 +1368,7 @@ void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int fol
        printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
               (unsigned long long)eb->start,
                btrfs_header_level(eb), nr,
-               (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
+               (u32)BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - nr,
                (unsigned long long)btrfs_header_generation(eb),
                (unsigned long long)btrfs_header_owner(eb));
        print_uuids(eb);