Commit 70c8a91c authored by Josef Bacik's avatar Josef Bacik Committed by Chris Mason

Btrfs: log changed inodes based on the extent map tree

We don't really need to copy extents from the source tree since we have all
of the information already available to us in the extent_map tree.  So
instead just write the extents straight to the log tree and don't bother to
copy the extent items from the source tree.
Signed-off-by: default avatarJosef Bacik <jbacik@fusionio.com>
Signed-off-by: default avatarChris Mason <chris.mason@fusionio.com>
parent d6393786
...@@ -5490,6 +5490,139 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) ...@@ -5490,6 +5490,139 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
return btrfs_next_old_leaf(root, path, 0); return btrfs_next_old_leaf(root, path, 0);
} }
/* Release the path up to but not including the given level */
static void btrfs_release_level(struct btrfs_path *path, int level)
{
int i;
for (i = 0; i < level; i++) {
path->slots[i] = 0;
if (!path->nodes[i])
continue;
if (path->locks[i]) {
btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
path->locks[i] = 0;
}
free_extent_buffer(path->nodes[i]);
path->nodes[i] = NULL;
}
}
/*
* This function assumes 2 things
*
* 1) You are using path->keep_locks
* 2) You are not inserting items.
*
* If either of these are not true do not use this function. If you need a next
* leaf with either of these not being true then this function can be easily
* adapted to do that, but at the moment these are the limitations.
*/
int btrfs_next_leaf_write(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_path *path,
int del)
{
struct extent_buffer *b;
struct btrfs_key key;
u32 nritems;
int level = 1;
int slot;
int ret = 1;
int write_lock_level = BTRFS_MAX_LEVEL;
int ins_len = del ? -1 : 0;
WARN_ON(!(path->keep_locks || path->really_keep_locks));
nritems = btrfs_header_nritems(path->nodes[0]);
btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
while (path->nodes[level]) {
nritems = btrfs_header_nritems(path->nodes[level]);
if (!(path->locks[level] & BTRFS_WRITE_LOCK)) {
search:
btrfs_release_path(path);
ret = btrfs_search_slot(trans, root, &key, path,
ins_len, 1);
if (ret < 0)
goto out;
level = 1;
continue;
}
if (path->slots[level] >= nritems - 1) {
level++;
continue;
}
btrfs_release_level(path, level);
break;
}
if (!path->nodes[level]) {
ret = 1;
goto out;
}
path->slots[level]++;
b = path->nodes[level];
while (b) {
level = btrfs_header_level(b);
if (!should_cow_block(trans, root, b))
goto cow_done;
btrfs_set_path_blocking(path);
ret = btrfs_cow_block(trans, root, b,
path->nodes[level + 1],
path->slots[level + 1], &b);
if (ret)
goto out;
cow_done:
path->nodes[level] = b;
btrfs_clear_path_blocking(path, NULL, 0);
if (level != 0) {
ret = setup_nodes_for_search(trans, root, path, b,
level, ins_len,
&write_lock_level);
if (ret == -EAGAIN)
goto search;
if (ret)
goto out;
b = path->nodes[level];
slot = path->slots[level];
ret = read_block_for_search(trans, root, path,
&b, level, slot, &key, 0);
if (ret == -EAGAIN)
goto search;
if (ret)
goto out;
level = btrfs_header_level(b);
if (!btrfs_try_tree_write_lock(b)) {
btrfs_set_path_blocking(path);
btrfs_tree_lock(b);
btrfs_clear_path_blocking(path, b,
BTRFS_WRITE_LOCK);
}
path->locks[level] = BTRFS_WRITE_LOCK;
path->nodes[level] = b;
path->slots[level] = 0;
} else {
path->slots[level] = 0;
ret = 0;
break;
}
}
out:
if (ret)
btrfs_release_path(path);
return ret;
}
int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
u64 time_seq) u64 time_seq)
{ {
......
...@@ -3187,6 +3187,9 @@ static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, ...@@ -3187,6 +3187,9 @@ static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
} }
int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path); int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
int btrfs_next_leaf_write(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_path *path,
int del);
int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
u64 time_seq); u64 time_seq);
static inline int btrfs_next_old_item(struct btrfs_root *root, static inline int btrfs_next_old_item(struct btrfs_root *root,
......
...@@ -49,7 +49,7 @@ void extent_map_tree_init(struct extent_map_tree *tree) ...@@ -49,7 +49,7 @@ void extent_map_tree_init(struct extent_map_tree *tree)
struct extent_map *alloc_extent_map(void) struct extent_map *alloc_extent_map(void)
{ {
struct extent_map *em; struct extent_map *em;
em = kmem_cache_alloc(extent_map_cache, GFP_NOFS); em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
if (!em) if (!em)
return NULL; return NULL;
em->in_tree = 0; em->in_tree = 0;
...@@ -198,16 +198,15 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) ...@@ -198,16 +198,15 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
merge = rb_entry(rb, struct extent_map, rb_node); merge = rb_entry(rb, struct extent_map, rb_node);
if (rb && mergable_maps(merge, em)) { if (rb && mergable_maps(merge, em)) {
em->start = merge->start; em->start = merge->start;
em->orig_start = merge->orig_start;
em->len += merge->len; em->len += merge->len;
em->block_len += merge->block_len; em->block_len += merge->block_len;
em->block_start = merge->block_start; em->block_start = merge->block_start;
merge->in_tree = 0; merge->in_tree = 0;
if (merge->generation > em->generation) { em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
em->mod_start = em->start; em->mod_start = merge->mod_start;
em->mod_len = em->len; em->generation = max(em->generation, merge->generation);
em->generation = merge->generation; list_move(&em->list, &tree->modified_extents);
list_move(&em->list, &tree->modified_extents);
}
list_del_init(&merge->list); list_del_init(&merge->list);
rb_erase(&merge->rb_node, &tree->map); rb_erase(&merge->rb_node, &tree->map);
...@@ -223,11 +222,8 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) ...@@ -223,11 +222,8 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
em->block_len += merge->len; em->block_len += merge->len;
rb_erase(&merge->rb_node, &tree->map); rb_erase(&merge->rb_node, &tree->map);
merge->in_tree = 0; merge->in_tree = 0;
if (merge->generation > em->generation) { em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
em->mod_len = em->len; em->generation = max(em->generation, merge->generation);
em->generation = merge->generation;
list_move(&em->list, &tree->modified_extents);
}
list_del_init(&merge->list); list_del_init(&merge->list);
free_extent_map(merge); free_extent_map(merge);
} }
......
...@@ -621,7 +621,7 @@ void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end, ...@@ -621,7 +621,7 @@ void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
} else { } else {
split->block_len = split->len; split->block_len = split->len;
split->block_start = em->block_start + diff; split->block_start = em->block_start + diff;
split->orig_start = split->start; split->orig_start = em->orig_start;
} }
ret = add_extent_mapping(em_tree, split); ret = add_extent_mapping(em_tree, split);
......
...@@ -95,6 +95,10 @@ static noinline int cow_file_range(struct inode *inode, ...@@ -95,6 +95,10 @@ static noinline int cow_file_range(struct inode *inode,
struct page *locked_page, struct page *locked_page,
u64 start, u64 end, int *page_started, u64 start, u64 end, int *page_started,
unsigned long *nr_written, int unlock); unsigned long *nr_written, int unlock);
static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
u64 len, u64 orig_start,
u64 block_start, u64 block_len,
u64 orig_block_len, int type);
static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
struct inode *inode, struct inode *dir, struct inode *inode, struct inode *dir,
...@@ -704,10 +708,14 @@ static noinline int submit_compressed_extents(struct inode *inode, ...@@ -704,10 +708,14 @@ static noinline int submit_compressed_extents(struct inode *inode,
em->compress_type = async_extent->compress_type; em->compress_type = async_extent->compress_type;
set_bit(EXTENT_FLAG_PINNED, &em->flags); set_bit(EXTENT_FLAG_PINNED, &em->flags);
set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
em->generation = -1;
while (1) { while (1) {
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = add_extent_mapping(em_tree, em); ret = add_extent_mapping(em_tree, em);
if (!ret)
list_move(&em->list,
&em_tree->modified_extents);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret != -EEXIST) { if (ret != -EEXIST) {
free_extent_map(em); free_extent_map(em);
...@@ -890,10 +898,14 @@ static noinline int __cow_file_range(struct btrfs_trans_handle *trans, ...@@ -890,10 +898,14 @@ static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
em->orig_block_len = ins.offset; em->orig_block_len = ins.offset;
em->bdev = root->fs_info->fs_devices->latest_bdev; em->bdev = root->fs_info->fs_devices->latest_bdev;
set_bit(EXTENT_FLAG_PINNED, &em->flags); set_bit(EXTENT_FLAG_PINNED, &em->flags);
em->generation = -1;
while (1) { while (1) {
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = add_extent_mapping(em_tree, em); ret = add_extent_mapping(em_tree, em);
if (!ret)
list_move(&em->list,
&em_tree->modified_extents);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret != -EEXIST) { if (ret != -EEXIST) {
free_extent_map(em); free_extent_map(em);
...@@ -1320,7 +1332,7 @@ static noinline int run_delalloc_nocow(struct inode *inode, ...@@ -1320,7 +1332,7 @@ static noinline int run_delalloc_nocow(struct inode *inode,
em = alloc_extent_map(); em = alloc_extent_map();
BUG_ON(!em); /* -ENOMEM */ BUG_ON(!em); /* -ENOMEM */
em->start = cur_offset; em->start = cur_offset;
em->orig_start = em->start; em->orig_start = found_key.offset - extent_offset;
em->len = num_bytes; em->len = num_bytes;
em->block_len = num_bytes; em->block_len = num_bytes;
em->block_start = disk_bytenr; em->block_start = disk_bytenr;
...@@ -1328,9 +1340,13 @@ static noinline int run_delalloc_nocow(struct inode *inode, ...@@ -1328,9 +1340,13 @@ static noinline int run_delalloc_nocow(struct inode *inode,
em->bdev = root->fs_info->fs_devices->latest_bdev; em->bdev = root->fs_info->fs_devices->latest_bdev;
set_bit(EXTENT_FLAG_PINNED, &em->flags); set_bit(EXTENT_FLAG_PINNED, &em->flags);
set_bit(EXTENT_FLAG_FILLING, &em->flags); set_bit(EXTENT_FLAG_FILLING, &em->flags);
em->generation = -1;
while (1) { while (1) {
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = add_extent_mapping(em_tree, em); ret = add_extent_mapping(em_tree, em);
if (!ret)
list_move(&em->list,
&em_tree->modified_extents);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret != -EEXIST) { if (ret != -EEXIST) {
free_extent_map(em); free_extent_map(em);
...@@ -5371,6 +5387,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -5371,6 +5387,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
if (start + len <= found_key.offset) if (start + len <= found_key.offset)
goto not_found; goto not_found;
em->start = start; em->start = start;
em->orig_start = start;
em->len = found_key.offset - start; em->len = found_key.offset - start;
goto not_found_em; goto not_found_em;
} }
...@@ -5423,7 +5440,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -5423,7 +5440,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
em->len = (copy_size + root->sectorsize - 1) & em->len = (copy_size + root->sectorsize - 1) &
~((u64)root->sectorsize - 1); ~((u64)root->sectorsize - 1);
em->orig_block_len = em->len; em->orig_block_len = em->len;
em->orig_start = EXTENT_MAP_INLINE; em->orig_start = em->start;
if (compress_type) { if (compress_type) {
set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
em->compress_type = compress_type; em->compress_type = compress_type;
...@@ -5476,6 +5493,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -5476,6 +5493,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
} }
not_found: not_found:
em->start = start; em->start = start;
em->orig_start = start;
em->len = len; em->len = len;
not_found_em: not_found_em:
em->block_start = EXTENT_MAP_HOLE; em->block_start = EXTENT_MAP_HOLE;
...@@ -5677,30 +5695,14 @@ struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *pag ...@@ -5677,30 +5695,14 @@ struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *pag
} }
static struct extent_map *btrfs_new_extent_direct(struct inode *inode, static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
struct extent_map *em,
u64 start, u64 len) u64 start, u64 len)
{ {
struct btrfs_root *root = BTRFS_I(inode)->root; struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; struct extent_map *em;
struct btrfs_key ins; struct btrfs_key ins;
u64 alloc_hint; u64 alloc_hint;
int ret; int ret;
bool insert = false;
/*
* Ok if the extent map we looked up is a hole and is for the exact
* range we want, there is no reason to allocate a new one, however if
* it is not right then we need to free this one and drop the cache for
* our range.
*/
if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
em->len != len) {
free_extent_map(em);
em = NULL;
insert = true;
btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
}
trans = btrfs_join_transaction(root); trans = btrfs_join_transaction(root);
if (IS_ERR(trans)) if (IS_ERR(trans))
...@@ -5716,38 +5718,10 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode, ...@@ -5716,38 +5718,10 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
goto out; goto out;
} }
if (!em) { em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
em = alloc_extent_map(); ins.offset, ins.offset, 0);
if (!em) { if (IS_ERR(em))
em = ERR_PTR(-ENOMEM); goto out;
goto out;
}
}
em->start = start;
em->orig_start = em->start;
em->len = ins.offset;
em->block_start = ins.objectid;
em->block_len = ins.offset;
em->orig_block_len = ins.offset;
em->bdev = root->fs_info->fs_devices->latest_bdev;
/*
* We need to do this because if we're using the original em we searched
* for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
*/
em->flags = 0;
set_bit(EXTENT_FLAG_PINNED, &em->flags);
while (insert) {
write_lock(&em_tree->lock);
ret = add_extent_mapping(em_tree, em);
write_unlock(&em_tree->lock);
if (ret != -EEXIST)
break;
btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
}
ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
ins.offset, ins.offset, 0); ins.offset, ins.offset, 0);
...@@ -5943,6 +5917,7 @@ static struct extent_map *create_pinned_em(struct inode *inode, u64 start, ...@@ -5943,6 +5917,7 @@ static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
em->block_start = block_start; em->block_start = block_start;
em->bdev = root->fs_info->fs_devices->latest_bdev; em->bdev = root->fs_info->fs_devices->latest_bdev;
em->orig_block_len = orig_block_len; em->orig_block_len = orig_block_len;
em->generation = -1;
set_bit(EXTENT_FLAG_PINNED, &em->flags); set_bit(EXTENT_FLAG_PINNED, &em->flags);
if (type == BTRFS_ORDERED_PREALLOC) if (type == BTRFS_ORDERED_PREALLOC)
set_bit(EXTENT_FLAG_FILLING, &em->flags); set_bit(EXTENT_FLAG_FILLING, &em->flags);
...@@ -5952,6 +5927,9 @@ static struct extent_map *create_pinned_em(struct inode *inode, u64 start, ...@@ -5952,6 +5927,9 @@ static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
em->start + em->len - 1, 0); em->start + em->len - 1, 0);
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = add_extent_mapping(em_tree, em); ret = add_extent_mapping(em_tree, em);
if (!ret)
list_move(&em->list,
&em_tree->modified_extents);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
} while (ret == -EEXIST); } while (ret == -EEXIST);
...@@ -6078,7 +6056,7 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, ...@@ -6078,7 +6056,7 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
goto must_cow; goto must_cow;
if (can_nocow_odirect(trans, inode, start, len) == 1) { if (can_nocow_odirect(trans, inode, start, len) == 1) {
u64 orig_start = em->start; u64 orig_start = em->orig_start;
u64 orig_block_len = em->orig_block_len; u64 orig_block_len = em->orig_block_len;
if (type == BTRFS_ORDERED_PREALLOC) { if (type == BTRFS_ORDERED_PREALLOC) {
...@@ -6110,7 +6088,8 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, ...@@ -6110,7 +6088,8 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
* it above * it above
*/ */
len = bh_result->b_size; len = bh_result->b_size;
em = btrfs_new_extent_direct(inode, em, start, len); free_extent_map(em);
em = btrfs_new_extent_direct(inode, start, len);
if (IS_ERR(em)) { if (IS_ERR(em)) {
ret = PTR_ERR(em); ret = PTR_ERR(em);
goto unlock_err; goto unlock_err;
......
This diff is collapsed.
...@@ -4983,6 +4983,7 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, ...@@ -4983,6 +4983,7 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
em->bdev = (struct block_device *)map; em->bdev = (struct block_device *)map;
em->start = logical; em->start = logical;
em->len = length; em->len = length;
em->orig_start = 0;
em->block_start = 0; em->block_start = 0;
em->block_len = em->len; em->block_len = em->len;
......
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