Commit 07d400a6 authored by Yan Zheng's avatar Yan Zheng Committed by Chris Mason

Btrfs: tree logging checksum fixes

This patch contains following things.

1) Limit the max size of btrfs_ordered_sum structure to PAGE_SIZE.  This
struct is kmalloced so we want to keep it reasonable.

2) Replace copy_extent_csums by btrfs_lookup_csums_range.  This was
duplicated code in tree-log.c

3) Remove replay_one_csum. csum items are replayed at the same time as
   replaying file extents. This guarantees we only replay useful csums.

4) nbytes accounting fix.
Signed-off-by: default avatarYan Zheng <zheng.yan@oracle.com>
parent 1ba12553
...@@ -5579,7 +5579,7 @@ int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) ...@@ -5579,7 +5579,7 @@ int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
BUG_ON(ordered->file_offset != file_pos || ordered->len != len); BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
ret = btrfs_lookup_csums_range(root, disk_bytenr, ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
disk_bytenr + len - 1, &list); disk_bytenr + len - 1, &list);
while (!list_empty(&list)) { while (!list_empty(&list)) {
......
...@@ -27,6 +27,12 @@ ...@@ -27,6 +27,12 @@
#define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r) - \ #define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r) - \
sizeof(struct btrfs_item) * 2) / \ sizeof(struct btrfs_item) * 2) / \
size) - 1)) size) - 1))
#define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \
sizeof(struct btrfs_ordered_sum)) / \
sizeof(struct btrfs_sector_sum) * \
(r)->sectorsize - (r)->sectorsize)
int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_root *root,
u64 objectid, u64 pos, u64 objectid, u64 pos,
...@@ -259,8 +265,7 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, ...@@ -259,8 +265,7 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
key.offset = start; key.offset = start;
key.type = BTRFS_EXTENT_CSUM_KEY; key.type = BTRFS_EXTENT_CSUM_KEY;
ret = btrfs_search_slot(NULL, root->fs_info->csum_root, ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
&key, path, 0, 0);
if (ret < 0) if (ret < 0)
goto fail; goto fail;
if (ret > 0 && path->slots[0] > 0) { if (ret > 0 && path->slots[0] > 0) {
...@@ -279,7 +284,7 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, ...@@ -279,7 +284,7 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
while (start <= end) { while (start <= end) {
leaf = path->nodes[0]; leaf = path->nodes[0];
if (path->slots[0] >= btrfs_header_nritems(leaf)) { if (path->slots[0] >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(root->fs_info->csum_root, path); ret = btrfs_next_leaf(root, path);
if (ret < 0) if (ret < 0)
goto fail; goto fail;
if (ret > 0) if (ret > 0)
...@@ -306,33 +311,38 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, ...@@ -306,33 +311,38 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
continue; continue;
} }
size = min(csum_end, end + 1) - start; csum_end = min(csum_end, end + 1);
sums = kzalloc(btrfs_ordered_sum_size(root, size), GFP_NOFS); item = btrfs_item_ptr(path->nodes[0], path->slots[0],
BUG_ON(!sums); struct btrfs_csum_item);
while (start < csum_end) {
size = min_t(size_t, csum_end - start,
MAX_ORDERED_SUM_BYTES(root));
sums = kzalloc(btrfs_ordered_sum_size(root, size),
GFP_NOFS);
BUG_ON(!sums);
sector_sum = sums->sums; sector_sum = sums->sums;
sums->bytenr = start; sums->bytenr = start;
sums->len = size; sums->len = size;
offset = (start - key.offset) >> offset = (start - key.offset) >>
root->fs_info->sb->s_blocksize_bits; root->fs_info->sb->s_blocksize_bits;
offset *= csum_size; offset *= csum_size;
item = btrfs_item_ptr(path->nodes[0], path->slots[0], while (size > 0) {
struct btrfs_csum_item); read_extent_buffer(path->nodes[0],
while (size > 0) { &sector_sum->sum,
read_extent_buffer(path->nodes[0], &sector_sum->sum, ((unsigned long)item) +
((unsigned long)item) + offset, offset, csum_size);
csum_size); sector_sum->bytenr = start;
sector_sum->bytenr = start;
size -= root->sectorsize;
size -= root->sectorsize; start += root->sectorsize;
start += root->sectorsize; offset += csum_size;
offset += csum_size; sector_sum++;
sector_sum++; }
list_add_tail(&sums->list, list);
} }
list_add_tail(&sums->list, list);
path->slots[0]++; path->slots[0]++;
} }
ret = 0; ret = 0;
......
...@@ -157,7 +157,6 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, ...@@ -157,7 +157,6 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
key.objectid = inode->i_ino; key.objectid = inode->i_ino;
key.offset = start; key.offset = start;
btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
inode_add_bytes(inode, size);
datasize = btrfs_file_extent_calc_inline_size(cur_size); datasize = btrfs_file_extent_calc_inline_size(cur_size);
inode_add_bytes(inode, size); inode_add_bytes(inode, size);
...@@ -920,8 +919,8 @@ static noinline int csum_exist_in_range(struct btrfs_root *root, ...@@ -920,8 +919,8 @@ static noinline int csum_exist_in_range(struct btrfs_root *root,
struct btrfs_ordered_sum *sums; struct btrfs_ordered_sum *sums;
LIST_HEAD(list); LIST_HEAD(list);
ret = btrfs_lookup_csums_range(root, bytenr, bytenr + num_bytes - 1, ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
&list); bytenr + num_bytes - 1, &list);
if (ret == 0 && list_empty(&list)) if (ret == 0 && list_empty(&list))
return 0; return 0;
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment