Commit d5f37527 authored by Filipe David Borba Manana's avatar Filipe David Borba Manana Committed by Josef Bacik

Btrfs: faster/more efficient insertion of file extent items

This is an extension to my previous commit titled:

  "Btrfs: faster file extent item replace operations"
  (hash 1acae57b)

Instead of inserting the new file extent item if we deleted existing
file extent items covering our target file range, also allow to insert
the new file extent item if we didn't find any existing items to delete
and replace_extent != 0, since in this case our caller would do another
tree search to insert the new file extent item anyway, therefore just
combine the two tree searches into a single one, saving cpu time, reducing
lock contention and reducing btree node/leaf COW operations.

This covers the case where applications keep doing tail append writes to
files, which for example is the case of Apache CouchDB (its database and
view index files are always open with O_APPEND).
Signed-off-by: default avatarFilipe David Borba Manana <fdmanana@gmail.com>
Signed-off-by: default avatarJosef Bacik <jbacik@fb.com>
parent 51b98eff
...@@ -720,7 +720,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -720,7 +720,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
if (drop_cache) if (drop_cache)
btrfs_drop_extent_cache(inode, start, end - 1, 0); btrfs_drop_extent_cache(inode, start, end - 1, 0);
if (start >= BTRFS_I(inode)->disk_i_size) if (start >= BTRFS_I(inode)->disk_i_size && !replace_extent)
modify_tree = 0; modify_tree = 0;
while (1) { while (1) {
...@@ -938,27 +938,36 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -938,27 +938,36 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
* Set path->slots[0] to first slot, so that after the delete * Set path->slots[0] to first slot, so that after the delete
* if items are move off from our leaf to its immediate left or * if items are move off from our leaf to its immediate left or
* right neighbor leafs, we end up with a correct and adjusted * right neighbor leafs, we end up with a correct and adjusted
* path->slots[0] for our insertion. * path->slots[0] for our insertion (if replace_extent != 0).
*/ */
path->slots[0] = del_slot; path->slots[0] = del_slot;
ret = btrfs_del_items(trans, root, path, del_slot, del_nr); ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
if (ret) if (ret)
btrfs_abort_transaction(trans, root, ret); btrfs_abort_transaction(trans, root, ret);
}
leaf = path->nodes[0]; leaf = path->nodes[0];
/* /*
* leaf eb has flag EXTENT_BUFFER_STALE if it was deleted (that * If btrfs_del_items() was called, it might have deleted a leaf, in
* is, its contents got pushed to its neighbors), in which case * which case it unlocked our path, so check path->locks[0] matches a
* it means path->locks[0] == 0 * write lock.
*/ */
if (!ret && replace_extent && leafs_visited == 1 && if (!ret && replace_extent && leafs_visited == 1 &&
path->locks[0] && (path->locks[0] == BTRFS_WRITE_LOCK_BLOCKING ||
path->locks[0] == BTRFS_WRITE_LOCK) &&
btrfs_leaf_free_space(root, leaf) >= btrfs_leaf_free_space(root, leaf) >=
sizeof(struct btrfs_item) + extent_item_size) { sizeof(struct btrfs_item) + extent_item_size) {
key.objectid = ino; key.objectid = ino;
key.type = BTRFS_EXTENT_DATA_KEY; key.type = BTRFS_EXTENT_DATA_KEY;
key.offset = start; key.offset = start;
if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
struct btrfs_key slot_key;
btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
path->slots[0]++;
}
setup_items_for_insert(root, path, &key, setup_items_for_insert(root, path, &key,
&extent_item_size, &extent_item_size,
extent_item_size, extent_item_size,
...@@ -966,7 +975,6 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -966,7 +975,6 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
extent_item_size, 1); extent_item_size, 1);
*key_inserted = 1; *key_inserted = 1;
} }
}
if (!replace_extent || !(*key_inserted)) if (!replace_extent || !(*key_inserted))
btrfs_release_path(path); btrfs_release_path(path);
......
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