Btrfs: implement memory reclaim for leaf reference cache
The memory reclaiming issue happens when snapshot exists. In that
case, some cache entries may not be used during old snapshot dropping,
so they will remain in the cache until umount.
The patch adds a field to struct btrfs_leaf_ref to record create time. Besides,
the patch makes all dead roots of a given snapshot linked together in order of
create time. After a old snapshot was completely dropped, we check the dead
root list and remove all cache entries created before the oldest dead root in
the list.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index fe1ddbd..37ca8df 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -867,8 +867,8 @@
/*
* For (parent_gen > 0 && parent_gen > ref_gen):
*
- * we reach here through the oldest root, therefore
- * all other reference from same snapshot should have
+ * we reach here through the oldest root, therefore
+ * all other reference from same snapshot should have
* a larger generation.
*/
if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
@@ -954,7 +954,7 @@
if (!eb)
continue;
extent_start = eb->start;
- } else
+ } else
extent_start = bytenr;
ret = get_reference_status(root, extent_start, ref_generation,
@@ -1048,7 +1048,7 @@
struct btrfs_leaf_ref *ref;
struct btrfs_extent_info *info;
- ref = btrfs_alloc_leaf_ref(nr_file_extents);
+ ref = btrfs_alloc_leaf_ref(root, nr_file_extents);
if (!ref) {
WARN_ON(1);
goto out;
@@ -1059,7 +1059,7 @@
ref->generation = btrfs_header_generation(buf);
ref->nritems = nr_file_extents;
info = ref->extents;
-
+
for (i = 0; nr_file_extents > 0 && i < nritems; i++) {
u64 disk_bytenr;
btrfs_item_key_to_cpu(buf, &key, i);
@@ -1085,7 +1085,7 @@
BUG_ON(!root->ref_tree);
ret = btrfs_add_leaf_ref(root, ref);
WARN_ON(ret);
- btrfs_free_leaf_ref(ref);
+ btrfs_free_leaf_ref(root, ref);
}
out:
return 0;
@@ -2316,7 +2316,7 @@
}
static int noinline drop_leaf_ref_no_cache(struct btrfs_trans_handle *trans,
- struct btrfs_root *root,
+ struct btrfs_root *root,
struct extent_buffer *leaf)
{
u64 leaf_owner;
@@ -2367,7 +2367,7 @@
}
static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
- struct btrfs_root *root,
+ struct btrfs_root *root,
struct btrfs_leaf_ref *ref)
{
int i;
@@ -2521,7 +2521,7 @@
ret = drop_leaf_ref(trans, root, ref);
BUG_ON(ret);
btrfs_remove_leaf_ref(root, ref);
- btrfs_free_leaf_ref(ref);
+ btrfs_free_leaf_ref(root, ref);
*level = 0;
break;
}