btrfs: Get rid of the confusing btrfs_file_extent_inline_len
authorQu Wenruo <wqu@suse.com>
Wed, 6 Jun 2018 07:41:49 +0000 (15:41 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 6 Aug 2018 11:12:38 +0000 (13:12 +0200)
We used to call btrfs_file_extent_inline_len() to get the uncompressed
data size of an inlined extent.

However this function is hiding evil, for compressed extent, it has no
choice but to directly read out ram_bytes from btrfs_file_extent_item.
While for uncompressed extent, it uses item size to calculate the real
data size, and ignoring ram_bytes completely.

In fact, for corrupted ram_bytes, due to above behavior kernel
btrfs_print_leaf() can't even print correct ram_bytes to expose the bug.

Since we have the tree-checker to verify all EXTENT_DATA, such mismatch
can be detected pretty easily, thus we can trust ram_bytes without the
evil btrfs_file_extent_inline_len().

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ctree.h
fs/btrfs/file-item.c
fs/btrfs/file.c
fs/btrfs/inode.c
fs/btrfs/print-tree.c
fs/btrfs/send.c
fs/btrfs/tree-log.c
include/trace/events/btrfs.h

index 12cb327cd16e81f23924c0d159e2597650f09d27..41ba770b9db9ff23c56b655ced106fbb6ff850a9 100644 (file)
@@ -2428,32 +2428,6 @@ static inline u32 btrfs_file_extent_inline_item_len(
        return btrfs_item_size(eb, e) - BTRFS_FILE_EXTENT_INLINE_DATA_START;
 }
 
-/* this returns the number of file bytes represented by the inline item.
- * If an item is compressed, this is the uncompressed size
- */
-static inline u32 btrfs_file_extent_inline_len(const struct extent_buffer *eb,
-                                       int slot,
-                                       const struct btrfs_file_extent_item *fi)
-{
-       struct btrfs_map_token token;
-
-       btrfs_init_map_token(&token);
-       /*
-        * return the space used on disk if this item isn't
-        * compressed or encoded
-        */
-       if (btrfs_token_file_extent_compression(eb, fi, &token) == 0 &&
-           btrfs_token_file_extent_encryption(eb, fi, &token) == 0 &&
-           btrfs_token_file_extent_other_encoding(eb, fi, &token) == 0) {
-               return btrfs_file_extent_inline_item_len(eb,
-                                                        btrfs_item_nr(slot));
-       }
-
-       /* otherwise use the ram bytes field */
-       return btrfs_token_file_extent_ram_bytes(eb, fi, &token);
-}
-
-
 /* btrfs_dev_stats_item */
 static inline u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
                                        const struct btrfs_dev_stats_item *ptr,
index f9dd6d1836a3b6b343cca48cea5e294a0a4c1416..8c3cd7072caf3f5237ed2448c6ace49780bf35e8 100644 (file)
@@ -942,7 +942,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
                        btrfs_file_extent_num_bytes(leaf, fi);
        } else if (type == BTRFS_FILE_EXTENT_INLINE) {
                size_t size;
-               size = btrfs_file_extent_inline_len(leaf, slot, fi);
+               size = btrfs_file_extent_ram_bytes(leaf, fi);
                extent_end = ALIGN(extent_start + size,
                                   fs_info->sectorsize);
        }
index 975c590c50d8a6e0ae16382e5454ca63cbed3235..4cd8af14f915b53ea741b32e153b75af9555e0a1 100644 (file)
@@ -833,8 +833,7 @@ next_slot:
                                btrfs_file_extent_num_bytes(leaf, fi);
                } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
                        extent_end = key.offset +
-                               btrfs_file_extent_inline_len(leaf,
-                                                    path->slots[0], fi);
+                               btrfs_file_extent_ram_bytes(leaf, fi);
                } else {
                        /* can't happen */
                        BUG();
index eba61bcb9bb3cdd9759837b539c257aaaed1edef..1ade43c02b81102d08b229d12fb0566874026778 100644 (file)
@@ -1443,8 +1443,7 @@ next_slot:
                        nocow = 1;
                } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
                        extent_end = found_key.offset +
-                               btrfs_file_extent_inline_len(leaf,
-                                                    path->slots[0], fi);
+                               btrfs_file_extent_ram_bytes(leaf, fi);
                        extent_end = ALIGN(extent_end,
                                           fs_info->sectorsize);
                } else {
@@ -4643,8 +4642,8 @@ search_again:
                                        BTRFS_I(inode), leaf, fi,
                                        found_key.offset);
                        } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
-                               item_end += btrfs_file_extent_inline_len(leaf,
-                                                        path->slots[0], fi);
+                               item_end += btrfs_file_extent_ram_bytes(leaf,
+                                                                       fi);
 
                                trace_btrfs_truncate_show_fi_inline(
                                        BTRFS_I(inode), leaf, fi, path->slots[0],
@@ -6943,7 +6942,8 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
                                                       extent_start);
        } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
                size_t size;
-               size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
+
+               size = btrfs_file_extent_ram_bytes(leaf, item);
                extent_end = ALIGN(extent_start + size,
                                   fs_info->sectorsize);
 
@@ -6994,7 +6994,7 @@ next:
                if (new_inline)
                        goto out;
 
-               size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
+               size = btrfs_file_extent_ram_bytes(leaf, item);
                extent_offset = page_offset(page) + pg_offset - extent_start;
                copy_size = min_t(u64, PAGE_SIZE - pg_offset,
                                  size - extent_offset);
index a4e11cf04671e6777802922d460dec16007b8380..59efcf2e0de8eb007257c5f827456fd895a87e8f 100644 (file)
@@ -267,8 +267,8 @@ void btrfs_print_leaf(struct extent_buffer *l)
                                            struct btrfs_file_extent_item);
                        if (btrfs_file_extent_type(l, fi) ==
                            BTRFS_FILE_EXTENT_INLINE) {
-                               pr_info("\t\tinline extent data size %u\n",
-                                      btrfs_file_extent_inline_len(l, i, fi));
+                               pr_info("\t\tinline extent data size %llu\n",
+                                      btrfs_file_extent_ram_bytes(l, fi));
                                break;
                        }
                        pr_info("\t\textent data disk bytenr %llu nr %llu\n",
index c47f62b1922678752aa721cde6cfce8f23f20aac..6ff7a1315e5222801e9020682a46995b44a7fae0 100644 (file)
@@ -1500,7 +1500,7 @@ static int read_symlink(struct btrfs_root *root,
        BUG_ON(compression);
 
        off = btrfs_file_extent_inline_start(ei);
-       len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
+       len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
 
        ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
 
@@ -5160,7 +5160,7 @@ static int clone_range(struct send_ctx *sctx,
                ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
                type = btrfs_file_extent_type(leaf, ei);
                if (type == BTRFS_FILE_EXTENT_INLINE) {
-                       ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
+                       ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
                        ext_len = PAGE_ALIGN(ext_len);
                } else {
                        ext_len = btrfs_file_extent_num_bytes(leaf, ei);
@@ -5236,8 +5236,7 @@ static int send_write_or_clone(struct send_ctx *sctx,
                        struct btrfs_file_extent_item);
        type = btrfs_file_extent_type(path->nodes[0], ei);
        if (type == BTRFS_FILE_EXTENT_INLINE) {
-               len = btrfs_file_extent_inline_len(path->nodes[0],
-                                                  path->slots[0], ei);
+               len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
                /*
                 * it is possible the inline item won't cover the whole page,
                 * but there may be items after this page.  Make
@@ -5375,7 +5374,7 @@ static int is_extent_unchanged(struct send_ctx *sctx,
                }
 
                if (right_type == BTRFS_FILE_EXTENT_INLINE) {
-                       right_len = btrfs_file_extent_inline_len(eb, slot, ei);
+                       right_len = btrfs_file_extent_ram_bytes(eb, ei);
                        right_len = PAGE_ALIGN(right_len);
                } else {
                        right_len = btrfs_file_extent_num_bytes(eb, ei);
@@ -5496,8 +5495,7 @@ static int get_last_extent(struct send_ctx *sctx, u64 offset)
                            struct btrfs_file_extent_item);
        type = btrfs_file_extent_type(path->nodes[0], fi);
        if (type == BTRFS_FILE_EXTENT_INLINE) {
-               u64 size = btrfs_file_extent_inline_len(path->nodes[0],
-                                                       path->slots[0], fi);
+               u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
                extent_end = ALIGN(key.offset + size,
                                   sctx->send_root->fs_info->sectorsize);
        } else {
@@ -5560,7 +5558,7 @@ static int range_is_hole_in_parent(struct send_ctx *sctx,
                fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
                if (btrfs_file_extent_type(leaf, fi) ==
                    BTRFS_FILE_EXTENT_INLINE) {
-                       u64 size = btrfs_file_extent_inline_len(leaf, slot, fi);
+                       u64 size = btrfs_file_extent_ram_bytes(leaf, fi);
 
                        extent_end = ALIGN(key.offset + size,
                                           root->fs_info->sectorsize);
@@ -5606,8 +5604,7 @@ static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
                            struct btrfs_file_extent_item);
        type = btrfs_file_extent_type(path->nodes[0], fi);
        if (type == BTRFS_FILE_EXTENT_INLINE) {
-               u64 size = btrfs_file_extent_inline_len(path->nodes[0],
-                                                       path->slots[0], fi);
+               u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
                extent_end = ALIGN(key->offset + size,
                                   sctx->send_root->fs_info->sectorsize);
        } else {
index b3b1d424f2d8be3328c5a50afc464fc33777a20d..6bca8f88ade09ae08e536f2aca6f9d321eab7f7b 100644 (file)
@@ -597,7 +597,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
                if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
                        nbytes = 0;
        } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
-               size = btrfs_file_extent_inline_len(eb, slot, item);
+               size = btrfs_file_extent_ram_bytes(eb, item);
                nbytes = btrfs_file_extent_ram_bytes(eb, item);
                extent_end = ALIGN(start + size,
                                   fs_info->sectorsize);
@@ -3920,9 +3920,7 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
                                        struct btrfs_file_extent_item);
                if (btrfs_file_extent_type(src, extent) ==
                    BTRFS_FILE_EXTENT_INLINE) {
-                       len = btrfs_file_extent_inline_len(src,
-                                                          src_path->slots[0],
-                                                          extent);
+                       len = btrfs_file_extent_ram_bytes(src, extent);
                        *last_extent = ALIGN(key.offset + len,
                                             fs_info->sectorsize);
                } else {
@@ -3987,7 +3985,7 @@ fill_holes:
                extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
                if (btrfs_file_extent_type(src, extent) ==
                    BTRFS_FILE_EXTENT_INLINE) {
-                       len = btrfs_file_extent_inline_len(src, i, extent);
+                       len = btrfs_file_extent_ram_bytes(src, extent);
                        extent_end = ALIGN(key.offset + len,
                                           fs_info->sectorsize);
                } else {
@@ -4572,9 +4570,7 @@ static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
 
                if (btrfs_file_extent_type(leaf, extent) ==
                    BTRFS_FILE_EXTENT_INLINE) {
-                       len = btrfs_file_extent_inline_len(leaf,
-                                                          path->slots[0],
-                                                          extent);
+                       len = btrfs_file_extent_ram_bytes(leaf, extent);
                        ASSERT(len == i_size ||
                               (len == fs_info->sectorsize &&
                                btrfs_file_extent_compression(leaf, extent) !=
index 7057cc99d267739962d313a7e8f3b5731e0fb9b8..b401c4e3639459e6e4e876cd1fae17e42251fec5 100644 (file)
@@ -374,7 +374,7 @@ DECLARE_EVENT_CLASS(
                __entry->extent_type    = btrfs_file_extent_type(l, fi);
                __entry->compression    = btrfs_file_extent_compression(l, fi);
                __entry->extent_start   = start;
-               __entry->extent_end     = (start + btrfs_file_extent_inline_len(l, slot, fi));
+               __entry->extent_end     = (start + btrfs_file_extent_ram_bytes(l, fi));
        ),
 
        TP_printk_btrfs(