aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorJosef Bacik <jbacik@fb.com>2014-09-10 16:20:45 -0400
committerChris Mason <clm@fb.com>2014-09-17 16:38:24 -0400
commitdc046b10c8b7d4f40befe457acb82340bf8b0699 (patch)
tree2659e92649b19df4cd54a65eabd34f961f8d57f8 /fs
parent78a017a2c92df9b571db0a55a016280f9019c65e (diff)
Btrfs: make fiemap not blow when you have lots of snapshots
We have been iterating all references for each extent we have in a file when we do fiemap to see if it is shared. This is fine when you have a few clones or a few snapshots, but when you have 5k snapshots suddenly fiemap just sits there and stares at you. So add btrfs_check_shared which will use the backref walking code but will short circuit as soon as it finds a root or inode that doesn't match the one we currently have. This makes fiemap on my testbox go from looking at me blankly for a day to spitting out actual output in a reasonable amount of time. Thanks, Signed-off-by: Josef Bacik <jbacik@fb.com> Signed-off-by: Chris Mason <clm@fb.com>
Diffstat (limited to 'fs')
-rw-r--r--fs/btrfs/backref.c109
-rw-r--r--fs/btrfs/backref.h3
-rw-r--r--fs/btrfs/extent_io.c40
3 files changed, 116 insertions, 36 deletions
diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index 4de97926939e..6829dc5aa657 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -25,6 +25,9 @@
25#include "delayed-ref.h" 25#include "delayed-ref.h"
26#include "locking.h" 26#include "locking.h"
27 27
28/* Just an arbitrary number so we can be sure this happened */
29#define BACKREF_FOUND_SHARED 6
30
28struct extent_inode_elem { 31struct extent_inode_elem {
29 u64 inum; 32 u64 inum;
30 u64 offset; 33 u64 offset;
@@ -377,7 +380,8 @@ out:
377static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info, 380static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
378 struct btrfs_path *path, u64 time_seq, 381 struct btrfs_path *path, u64 time_seq,
379 struct list_head *head, 382 struct list_head *head,
380 const u64 *extent_item_pos, u64 total_refs) 383 const u64 *extent_item_pos, u64 total_refs,
384 u64 root_objectid)
381{ 385{
382 int err; 386 int err;
383 int ret = 0; 387 int ret = 0;
@@ -402,6 +406,10 @@ static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
402 continue; 406 continue;
403 if (ref->count == 0) 407 if (ref->count == 0)
404 continue; 408 continue;
409 if (root_objectid && ref->root_id != root_objectid) {
410 ret = BACKREF_FOUND_SHARED;
411 goto out;
412 }
405 err = __resolve_indirect_ref(fs_info, path, time_seq, ref, 413 err = __resolve_indirect_ref(fs_info, path, time_seq, ref,
406 parents, extent_item_pos, 414 parents, extent_item_pos,
407 total_refs); 415 total_refs);
@@ -561,7 +569,8 @@ static void __merge_refs(struct list_head *head, int mode)
561 * smaller or equal that seq to the list 569 * smaller or equal that seq to the list
562 */ 570 */
563static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq, 571static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
564 struct list_head *prefs, u64 *total_refs) 572 struct list_head *prefs, u64 *total_refs,
573 u64 inum)
565{ 574{
566 struct btrfs_delayed_extent_op *extent_op = head->extent_op; 575 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
567 struct rb_node *n = &head->node.rb_node; 576 struct rb_node *n = &head->node.rb_node;
@@ -625,6 +634,16 @@ static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
625 key.objectid = ref->objectid; 634 key.objectid = ref->objectid;
626 key.type = BTRFS_EXTENT_DATA_KEY; 635 key.type = BTRFS_EXTENT_DATA_KEY;
627 key.offset = ref->offset; 636 key.offset = ref->offset;
637
638 /*
639 * Found a inum that doesn't match our known inum, we
640 * know it's shared.
641 */
642 if (inum && ref->objectid != inum) {
643 ret = BACKREF_FOUND_SHARED;
644 break;
645 }
646
628 ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0, 647 ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0,
629 node->bytenr, 648 node->bytenr,
630 node->ref_mod * sgn, GFP_ATOMIC); 649 node->ref_mod * sgn, GFP_ATOMIC);
@@ -659,7 +678,7 @@ static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
659static int __add_inline_refs(struct btrfs_fs_info *fs_info, 678static int __add_inline_refs(struct btrfs_fs_info *fs_info,
660 struct btrfs_path *path, u64 bytenr, 679 struct btrfs_path *path, u64 bytenr,
661 int *info_level, struct list_head *prefs, 680 int *info_level, struct list_head *prefs,
662 u64 *total_refs) 681 u64 *total_refs, u64 inum)
663{ 682{
664 int ret = 0; 683 int ret = 0;
665 int slot; 684 int slot;
@@ -744,6 +763,12 @@ static int __add_inline_refs(struct btrfs_fs_info *fs_info,
744 dref); 763 dref);
745 key.type = BTRFS_EXTENT_DATA_KEY; 764 key.type = BTRFS_EXTENT_DATA_KEY;
746 key.offset = btrfs_extent_data_ref_offset(leaf, dref); 765 key.offset = btrfs_extent_data_ref_offset(leaf, dref);
766
767 if (inum && key.objectid != inum) {
768 ret = BACKREF_FOUND_SHARED;
769 break;
770 }
771
747 root = btrfs_extent_data_ref_root(leaf, dref); 772 root = btrfs_extent_data_ref_root(leaf, dref);
748 ret = __add_prelim_ref(prefs, root, &key, 0, 0, 773 ret = __add_prelim_ref(prefs, root, &key, 0, 0,
749 bytenr, count, GFP_NOFS); 774 bytenr, count, GFP_NOFS);
@@ -765,7 +790,7 @@ static int __add_inline_refs(struct btrfs_fs_info *fs_info,
765 */ 790 */
766static int __add_keyed_refs(struct btrfs_fs_info *fs_info, 791static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
767 struct btrfs_path *path, u64 bytenr, 792 struct btrfs_path *path, u64 bytenr,
768 int info_level, struct list_head *prefs) 793 int info_level, struct list_head *prefs, u64 inum)
769{ 794{
770 struct btrfs_root *extent_root = fs_info->extent_root; 795 struct btrfs_root *extent_root = fs_info->extent_root;
771 int ret; 796 int ret;
@@ -827,6 +852,12 @@ static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
827 dref); 852 dref);
828 key.type = BTRFS_EXTENT_DATA_KEY; 853 key.type = BTRFS_EXTENT_DATA_KEY;
829 key.offset = btrfs_extent_data_ref_offset(leaf, dref); 854 key.offset = btrfs_extent_data_ref_offset(leaf, dref);
855
856 if (inum && key.objectid != inum) {
857 ret = BACKREF_FOUND_SHARED;
858 break;
859 }
860
830 root = btrfs_extent_data_ref_root(leaf, dref); 861 root = btrfs_extent_data_ref_root(leaf, dref);
831 ret = __add_prelim_ref(prefs, root, &key, 0, 0, 862 ret = __add_prelim_ref(prefs, root, &key, 0, 0,
832 bytenr, count, GFP_NOFS); 863 bytenr, count, GFP_NOFS);
@@ -854,7 +885,8 @@ static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
854static int find_parent_nodes(struct btrfs_trans_handle *trans, 885static int find_parent_nodes(struct btrfs_trans_handle *trans,
855 struct btrfs_fs_info *fs_info, u64 bytenr, 886 struct btrfs_fs_info *fs_info, u64 bytenr,
856 u64 time_seq, struct ulist *refs, 887 u64 time_seq, struct ulist *refs,
857 struct ulist *roots, const u64 *extent_item_pos) 888 struct ulist *roots, const u64 *extent_item_pos,
889 u64 root_objectid, u64 inum)
858{ 890{
859 struct btrfs_key key; 891 struct btrfs_key key;
860 struct btrfs_path *path; 892 struct btrfs_path *path;
@@ -929,7 +961,8 @@ again:
929 } 961 }
930 spin_unlock(&delayed_refs->lock); 962 spin_unlock(&delayed_refs->lock);
931 ret = __add_delayed_refs(head, time_seq, 963 ret = __add_delayed_refs(head, time_seq,
932 &prefs_delayed, &total_refs); 964 &prefs_delayed, &total_refs,
965 inum);
933 mutex_unlock(&head->mutex); 966 mutex_unlock(&head->mutex);
934 if (ret) 967 if (ret)
935 goto out; 968 goto out;
@@ -951,11 +984,11 @@ again:
951 key.type == BTRFS_METADATA_ITEM_KEY)) { 984 key.type == BTRFS_METADATA_ITEM_KEY)) {
952 ret = __add_inline_refs(fs_info, path, bytenr, 985 ret = __add_inline_refs(fs_info, path, bytenr,
953 &info_level, &prefs, 986 &info_level, &prefs,
954 &total_refs); 987 &total_refs, inum);
955 if (ret) 988 if (ret)
956 goto out; 989 goto out;
957 ret = __add_keyed_refs(fs_info, path, bytenr, 990 ret = __add_keyed_refs(fs_info, path, bytenr,
958 info_level, &prefs); 991 info_level, &prefs, inum);
959 if (ret) 992 if (ret)
960 goto out; 993 goto out;
961 } 994 }
@@ -971,7 +1004,8 @@ again:
971 __merge_refs(&prefs, 1); 1004 __merge_refs(&prefs, 1);
972 1005
973 ret = __resolve_indirect_refs(fs_info, path, time_seq, &prefs, 1006 ret = __resolve_indirect_refs(fs_info, path, time_seq, &prefs,
974 extent_item_pos, total_refs); 1007 extent_item_pos, total_refs,
1008 root_objectid);
975 if (ret) 1009 if (ret)
976 goto out; 1010 goto out;
977 1011
@@ -981,6 +1015,11 @@ again:
981 ref = list_first_entry(&prefs, struct __prelim_ref, list); 1015 ref = list_first_entry(&prefs, struct __prelim_ref, list);
982 WARN_ON(ref->count < 0); 1016 WARN_ON(ref->count < 0);
983 if (roots && ref->count && ref->root_id && ref->parent == 0) { 1017 if (roots && ref->count && ref->root_id && ref->parent == 0) {
1018 if (root_objectid && ref->root_id != root_objectid) {
1019 ret = BACKREF_FOUND_SHARED;
1020 goto out;
1021 }
1022
984 /* no parent == root of tree */ 1023 /* no parent == root of tree */
985 ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 1024 ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
986 if (ret < 0) 1025 if (ret < 0)
@@ -1087,7 +1126,7 @@ static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
1087 return -ENOMEM; 1126 return -ENOMEM;
1088 1127
1089 ret = find_parent_nodes(trans, fs_info, bytenr, 1128 ret = find_parent_nodes(trans, fs_info, bytenr,
1090 time_seq, *leafs, NULL, extent_item_pos); 1129 time_seq, *leafs, NULL, extent_item_pos, 0, 0);
1091 if (ret < 0 && ret != -ENOENT) { 1130 if (ret < 0 && ret != -ENOENT) {
1092 free_leaf_list(*leafs); 1131 free_leaf_list(*leafs);
1093 return ret; 1132 return ret;
@@ -1130,7 +1169,7 @@ static int __btrfs_find_all_roots(struct btrfs_trans_handle *trans,
1130 ULIST_ITER_INIT(&uiter); 1169 ULIST_ITER_INIT(&uiter);
1131 while (1) { 1170 while (1) {
1132 ret = find_parent_nodes(trans, fs_info, bytenr, 1171 ret = find_parent_nodes(trans, fs_info, bytenr,
1133 time_seq, tmp, *roots, NULL); 1172 time_seq, tmp, *roots, NULL, 0, 0);
1134 if (ret < 0 && ret != -ENOENT) { 1173 if (ret < 0 && ret != -ENOENT) {
1135 ulist_free(tmp); 1174 ulist_free(tmp);
1136 ulist_free(*roots); 1175 ulist_free(*roots);
@@ -1161,6 +1200,54 @@ int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
1161 return ret; 1200 return ret;
1162} 1201}
1163 1202
1203int btrfs_check_shared(struct btrfs_trans_handle *trans,
1204 struct btrfs_fs_info *fs_info, u64 root_objectid,
1205 u64 inum, u64 bytenr)
1206{
1207 struct ulist *tmp = NULL;
1208 struct ulist *roots = NULL;
1209 struct ulist_iterator uiter;
1210 struct ulist_node *node;
1211 struct seq_list elem = {};
1212 int ret = 0;
1213
1214 tmp = ulist_alloc(GFP_NOFS);
1215 roots = ulist_alloc(GFP_NOFS);
1216 if (!tmp || !roots) {
1217 ulist_free(tmp);
1218 ulist_free(roots);
1219 return -ENOMEM;
1220 }
1221
1222 if (trans)
1223 btrfs_get_tree_mod_seq(fs_info, &elem);
1224 else
1225 down_read(&fs_info->commit_root_sem);
1226 ULIST_ITER_INIT(&uiter);
1227 while (1) {
1228 ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
1229 roots, NULL, root_objectid, inum);
1230 if (ret == BACKREF_FOUND_SHARED) {
1231 ret = 1;
1232 break;
1233 }
1234 if (ret < 0 && ret != -ENOENT)
1235 break;
1236 node = ulist_next(tmp, &uiter);
1237 if (!node)
1238 break;
1239 bytenr = node->val;
1240 cond_resched();
1241 }
1242 if (trans)
1243 btrfs_put_tree_mod_seq(fs_info, &elem);
1244 else
1245 up_read(&fs_info->commit_root_sem);
1246 ulist_free(tmp);
1247 ulist_free(roots);
1248 return ret;
1249}
1250
1164/* 1251/*
1165 * this makes the path point to (inum INODE_ITEM ioff) 1252 * this makes the path point to (inum INODE_ITEM ioff)
1166 */ 1253 */
diff --git a/fs/btrfs/backref.h b/fs/btrfs/backref.h
index 86fc20fec282..2a1ac6bfc724 100644
--- a/fs/btrfs/backref.h
+++ b/fs/btrfs/backref.h
@@ -71,6 +71,9 @@ int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
71 u64 start_off, struct btrfs_path *path, 71 u64 start_off, struct btrfs_path *path,
72 struct btrfs_inode_extref **ret_extref, 72 struct btrfs_inode_extref **ret_extref,
73 u64 *found_off); 73 u64 *found_off);
74int btrfs_check_shared(struct btrfs_trans_handle *trans,
75 struct btrfs_fs_info *fs_info, u64 root_objectid,
76 u64 inum, u64 bytenr);
74 77
75int __init btrfs_prelim_ref_init(void); 78int __init btrfs_prelim_ref_init(void);
76void btrfs_prelim_ref_exit(void); 79void btrfs_prelim_ref_exit(void);
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index d70feb545066..d5e71d4646dd 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -4169,19 +4169,6 @@ static struct extent_map *get_extent_skip_holes(struct inode *inode,
4169 return NULL; 4169 return NULL;
4170} 4170}
4171 4171
4172static noinline int count_ext_ref(u64 inum, u64 offset, u64 root_id, void *ctx)
4173{
4174 unsigned long cnt = *((unsigned long *)ctx);
4175
4176 cnt++;
4177 *((unsigned long *)ctx) = cnt;
4178
4179 /* Now we're sure that the extent is shared. */
4180 if (cnt > 1)
4181 return 1;
4182 return 0;
4183}
4184
4185int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 4172int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4186 __u64 start, __u64 len, get_extent_t *get_extent) 4173 __u64 start, __u64 len, get_extent_t *get_extent)
4187{ 4174{
@@ -4198,6 +4185,7 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4198 struct extent_map *em = NULL; 4185 struct extent_map *em = NULL;
4199 struct extent_state *cached_state = NULL; 4186 struct extent_state *cached_state = NULL;
4200 struct btrfs_path *path; 4187 struct btrfs_path *path;
4188 struct btrfs_root *root = BTRFS_I(inode)->root;
4201 int end = 0; 4189 int end = 0;
4202 u64 em_start = 0; 4190 u64 em_start = 0;
4203 u64 em_len = 0; 4191 u64 em_len = 0;
@@ -4218,8 +4206,8 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4218 * lookup the last file extent. We're not using i_size here 4206 * lookup the last file extent. We're not using i_size here
4219 * because there might be preallocation past i_size 4207 * because there might be preallocation past i_size
4220 */ 4208 */
4221 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root, 4209 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
4222 path, btrfs_ino(inode), -1, 0); 4210 0);
4223 if (ret < 0) { 4211 if (ret < 0) {
4224 btrfs_free_path(path); 4212 btrfs_free_path(path);
4225 return ret; 4213 return ret;
@@ -4312,25 +4300,27 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4312 } else if (em->block_start == EXTENT_MAP_DELALLOC) { 4300 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
4313 flags |= (FIEMAP_EXTENT_DELALLOC | 4301 flags |= (FIEMAP_EXTENT_DELALLOC |
4314 FIEMAP_EXTENT_UNKNOWN); 4302 FIEMAP_EXTENT_UNKNOWN);
4315 } else { 4303 } else if (fieinfo->fi_extents_max) {
4316 unsigned long ref_cnt = 0; 4304 u64 bytenr = em->block_start -
4305 (em->start - em->orig_start);
4317 4306
4318 disko = em->block_start + offset_in_extent; 4307 disko = em->block_start + offset_in_extent;
4319 4308
4320 /* 4309 /*
4321 * As btrfs supports shared space, this information 4310 * As btrfs supports shared space, this information
4322 * can be exported to userspace tools via 4311 * can be exported to userspace tools via
4323 * flag FIEMAP_EXTENT_SHARED. 4312 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4313 * then we're just getting a count and we can skip the
4314 * lookup stuff.
4324 */ 4315 */
4325 ret = iterate_inodes_from_logical( 4316 ret = btrfs_check_shared(NULL, root->fs_info,
4326 em->block_start, 4317 root->objectid,
4327 BTRFS_I(inode)->root->fs_info, 4318 btrfs_ino(inode), bytenr);
4328 path, count_ext_ref, &ref_cnt); 4319 if (ret < 0)
4329 if (ret < 0 && ret != -ENOENT)
4330 goto out_free; 4320 goto out_free;
4331 4321 if (ret)
4332 if (ref_cnt > 1)
4333 flags |= FIEMAP_EXTENT_SHARED; 4322 flags |= FIEMAP_EXTENT_SHARED;
4323 ret = 0;
4334 } 4324 }
4335 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) 4325 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4336 flags |= FIEMAP_EXTENT_ENCODED; 4326 flags |= FIEMAP_EXTENT_ENCODED;