Skip to content

Commit 80ff385

Browse files
Yan Zhengchrismason-xx
authored andcommitted
Btrfs: update nodatacow code v2
This patch simplifies the nodatacow checker. If all references were created after the latest snapshot, then we can avoid COW safely. This patch also updates run_delalloc_nocow to do more fine-grained checking. Signed-off-by: Yan Zheng <zheng.yan@oracle.com>
1 parent 6643558 commit 80ff385

7 files changed

Lines changed: 154 additions & 213 deletions

File tree

‎fs/btrfs/ctree.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ struct btrfs_root_item {
454454
__le64 bytenr;
455455
__le64 byte_limit;
456456
__le64 bytes_used;
457+
__le64 last_snapshot;
457458
__le32 flags;
458459
__le32 refs;
459460
struct btrfs_disk_key drop_progress;
@@ -1413,6 +1414,8 @@ BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
14131414
BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 32);
14141415
BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
14151416
BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
1417+
BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
1418+
last_snapshot, 64);
14161419

14171420
/* struct btrfs_super_block */
14181421
BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
@@ -1564,9 +1567,8 @@ int btrfs_update_pinned_extents(struct btrfs_root *root,
15641567
u64 bytenr, u64 num, int pin);
15651568
int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
15661569
struct btrfs_root *root, struct extent_buffer *leaf);
1567-
int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
1568-
struct btrfs_root *root,
1569-
struct btrfs_key *key, u64 bytenr);
1570+
int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1571+
struct btrfs_root *root, u64 bytenr);
15701572
int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
15711573
struct btrfs_root *root);
15721574
int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy);

‎fs/btrfs/extent-tree.c‎

Lines changed: 18 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -848,18 +848,17 @@ int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
848848
return 0;
849849
}
850850

851-
static int get_reference_status(struct btrfs_root *root, u64 bytenr,
852-
u64 parent_gen, u64 ref_objectid,
853-
u64 *min_generation, u32 *ref_count)
851+
int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
852+
struct btrfs_root *root, u64 bytenr)
854853
{
855854
struct btrfs_root *extent_root = root->fs_info->extent_root;
856855
struct btrfs_path *path;
857856
struct extent_buffer *leaf;
858857
struct btrfs_extent_ref *ref_item;
859858
struct btrfs_key key;
860859
struct btrfs_key found_key;
861-
u64 root_objectid = root->root_key.objectid;
862-
u64 ref_generation;
860+
u64 ref_root;
861+
u64 last_snapshot;
863862
u32 nritems;
864863
int ret;
865864

@@ -872,22 +871,20 @@ static int get_reference_status(struct btrfs_root *root, u64 bytenr,
872871
if (ret < 0)
873872
goto out;
874873
BUG_ON(ret == 0);
875-
if (ret < 0 || path->slots[0] == 0)
874+
875+
ret = -ENOENT;
876+
if (path->slots[0] == 0)
876877
goto out;
877878

878879
path->slots[0]--;
879880
leaf = path->nodes[0];
880881
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
881882

882883
if (found_key.objectid != bytenr ||
883-
found_key.type != BTRFS_EXTENT_ITEM_KEY) {
884-
ret = 1;
884+
found_key.type != BTRFS_EXTENT_ITEM_KEY)
885885
goto out;
886-
}
887-
888-
*ref_count = 0;
889-
*min_generation = (u64)-1;
890886

887+
last_snapshot = btrfs_root_last_snapshot(&root->root_item);
891888
while (1) {
892889
leaf = path->nodes[0];
893890
nritems = btrfs_header_nritems(leaf);
@@ -910,114 +907,22 @@ static int get_reference_status(struct btrfs_root *root, u64 bytenr,
910907

911908
ref_item = btrfs_item_ptr(leaf, path->slots[0],
912909
struct btrfs_extent_ref);
913-
ref_generation = btrfs_ref_generation(leaf, ref_item);
914-
/*
915-
* For (parent_gen > 0 && parent_gen > ref_generation):
916-
*
917-
* we reach here through the oldest root, therefore
918-
* all other reference from same snapshot should have
919-
* a larger generation.
920-
*/
921-
if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
922-
(parent_gen > 0 && parent_gen > ref_generation) ||
923-
(ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
924-
ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
925-
*ref_count = 2;
926-
break;
927-
}
928-
929-
*ref_count = 1;
930-
if (*min_generation > ref_generation)
931-
*min_generation = ref_generation;
932-
933-
path->slots[0]++;
934-
}
935-
ret = 0;
936-
out:
937-
btrfs_free_path(path);
938-
return ret;
939-
}
940-
941-
int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
942-
struct btrfs_root *root,
943-
struct btrfs_key *key, u64 bytenr)
944-
{
945-
struct btrfs_root *old_root;
946-
struct btrfs_path *path = NULL;
947-
struct extent_buffer *eb;
948-
struct btrfs_file_extent_item *item;
949-
u64 ref_generation;
950-
u64 min_generation;
951-
u64 extent_start;
952-
u32 ref_count;
953-
int level;
954-
int ret;
955-
956-
BUG_ON(trans == NULL);
957-
BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
958-
ret = get_reference_status(root, bytenr, 0, key->objectid,
959-
&min_generation, &ref_count);
960-
if (ret)
961-
return ret;
962-
963-
if (ref_count != 1)
964-
return 1;
965-
966-
old_root = root->dirty_root->root;
967-
ref_generation = old_root->root_key.offset;
968-
969-
/* all references are created in running transaction */
970-
if (min_generation > ref_generation) {
971-
ret = 0;
972-
goto out;
973-
}
974-
975-
path = btrfs_alloc_path();
976-
if (!path) {
977-
ret = -ENOMEM;
978-
goto out;
979-
}
980-
981-
path->skip_locking = 1;
982-
/* if no item found, the extent is referenced by other snapshot */
983-
ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
984-
if (ret)
985-
goto out;
986-
987-
eb = path->nodes[0];
988-
item = btrfs_item_ptr(eb, path->slots[0],
989-
struct btrfs_file_extent_item);
990-
if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
991-
btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
992-
ret = 1;
993-
goto out;
994-
}
995-
996-
for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
997-
if (level >= 0) {
998-
eb = path->nodes[level];
999-
if (!eb)
1000-
continue;
1001-
extent_start = eb->start;
1002-
} else
1003-
extent_start = bytenr;
1004-
1005-
ret = get_reference_status(root, extent_start, ref_generation,
1006-
0, &min_generation, &ref_count);
1007-
if (ret)
910+
ref_root = btrfs_ref_root(leaf, ref_item);
911+
if (ref_root != root->root_key.objectid &&
912+
ref_root != BTRFS_TREE_LOG_OBJECTID) {
913+
ret = 1;
1008914
goto out;
1009-
1010-
if (ref_count != 1) {
915+
}
916+
if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1011917
ret = 1;
1012918
goto out;
1013919
}
1014-
if (level >= 0)
1015-
ref_generation = btrfs_header_generation(eb);
920+
921+
path->slots[0]++;
1016922
}
1017923
ret = 0;
1018924
out:
1019-
if (path)
1020-
btrfs_free_path(path);
925+
btrfs_free_path(path);
1021926
return ret;
1022927
}
1023928

0 commit comments

Comments
 (0)