Commit 5ce14bbc authored by Chris Mason's avatar Chris Mason Committed by David Woodhouse

Btrfs: Find and remove dead roots the first time a root is loaded.

Dead roots are trees left over after a crash, and they were either in the
process of being removed or were waiting to be removed when the box crashed.
Before, a search of the entire tree of root pointers was done on mount
looking for dead roots.  Now, the search is done the first time we load
a root.

This makes mount faster when there are a large number of snapshots, and it
enables the block accounting code to properly update the block counts on
the latest root as old versions of the root are reaped after a crash.
Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 0e2752a7
...@@ -1148,7 +1148,8 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1148,7 +1148,8 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
*item); *item);
int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
btrfs_root_item *item, struct btrfs_key *key); btrfs_root_item *item, struct btrfs_key *key);
int btrfs_find_dead_roots(struct btrfs_root *root); int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
struct btrfs_root *latest_root);
/* dir-item.c */ /* dir-item.c */
int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, const char *name, int name_len, u64 dir, *root, const char *name, int name_len, u64 dir,
......
...@@ -426,6 +426,10 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, ...@@ -426,6 +426,10 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
return ERR_PTR(ret); return ERR_PTR(ret);
} }
ret = btrfs_find_dead_roots(fs_info->tree_root,
root->root_key.objectid, root);
BUG_ON(ret);
return root; return root;
} }
...@@ -522,11 +526,6 @@ struct btrfs_root *open_ctree(struct super_block *sb) ...@@ -522,11 +526,6 @@ struct btrfs_root *open_ctree(struct super_block *sb)
btrfs_read_block_groups(extent_root); btrfs_read_block_groups(extent_root);
fs_info->generation = btrfs_super_generation(disk_super) + 1; fs_info->generation = btrfs_super_generation(disk_super) + 1;
ret = btrfs_find_dead_roots(tree_root);
if (ret) {
mutex_unlock(&fs_info->fs_mutex);
goto fail_tree_root;
}
mutex_unlock(&fs_info->fs_mutex); mutex_unlock(&fs_info->fs_mutex);
return tree_root; return tree_root;
......
...@@ -93,7 +93,8 @@ int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -93,7 +93,8 @@ int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
return ret; return ret;
} }
int btrfs_find_dead_roots(struct btrfs_root *root) int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
struct btrfs_root *latest)
{ {
struct btrfs_root *dead_root; struct btrfs_root *dead_root;
struct btrfs_item *item; struct btrfs_item *item;
...@@ -105,7 +106,7 @@ int btrfs_find_dead_roots(struct btrfs_root *root) ...@@ -105,7 +106,7 @@ int btrfs_find_dead_roots(struct btrfs_root *root)
struct btrfs_leaf *leaf; struct btrfs_leaf *leaf;
int slot; int slot;
key.objectid = 0; key.objectid = objectid;
key.flags = 0; key.flags = 0;
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
key.offset = 0; key.offset = 0;
...@@ -131,15 +132,24 @@ int btrfs_find_dead_roots(struct btrfs_root *root) ...@@ -131,15 +132,24 @@ int btrfs_find_dead_roots(struct btrfs_root *root)
btrfs_disk_key_to_cpu(&key, &item->key); btrfs_disk_key_to_cpu(&key, &item->key);
if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY) if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
goto next; goto next;
if (key.objectid < objectid)
goto next;
if (key.objectid > objectid)
break;
ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item); ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
if (btrfs_root_refs(ri) != 0) if (btrfs_root_refs(ri) != 0)
goto next; goto next;
dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key); dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
if (IS_ERR(dead_root)) { if (IS_ERR(dead_root)) {
ret = PTR_ERR(dead_root); ret = PTR_ERR(dead_root);
goto err; goto err;
} }
ret = btrfs_add_dead_root(dead_root,
ret = btrfs_add_dead_root(dead_root, latest,
&root->fs_info->dead_roots); &root->fs_info->dead_roots);
if (ret) if (ret)
goto err; goto err;
......
...@@ -239,7 +239,9 @@ struct dirty_root { ...@@ -239,7 +239,9 @@ struct dirty_root {
struct btrfs_root *latest_root; struct btrfs_root *latest_root;
}; };
int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list) int btrfs_add_dead_root(struct btrfs_root *root,
struct btrfs_root *latest,
struct list_head *dead_list)
{ {
struct dirty_root *dirty; struct dirty_root *dirty;
...@@ -247,6 +249,7 @@ int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list) ...@@ -247,6 +249,7 @@ int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
if (!dirty) if (!dirty)
return -ENOMEM; return -ENOMEM;
dirty->root = root; dirty->root = root;
dirty->latest_root = latest;
list_add(&dirty->list, dead_list); list_add(&dirty->list, dead_list);
return 0; return 0;
} }
...@@ -412,7 +415,6 @@ static int drop_dirty_roots(struct btrfs_root *tree_root, ...@@ -412,7 +415,6 @@ static int drop_dirty_roots(struct btrfs_root *tree_root,
while(1) { while(1) {
trans = btrfs_start_transaction(tree_root, 1); trans = btrfs_start_transaction(tree_root, 1);
ret = btrfs_drop_snapshot(trans, dirty->root); ret = btrfs_drop_snapshot(trans, dirty->root);
if (ret != -EAGAIN) { if (ret != -EAGAIN) {
break; break;
......
...@@ -78,7 +78,8 @@ void btrfs_transaction_flush_work(struct btrfs_root *root); ...@@ -78,7 +78,8 @@ void btrfs_transaction_flush_work(struct btrfs_root *root);
void btrfs_transaction_queue_work(struct btrfs_root *root, int delay); void btrfs_transaction_queue_work(struct btrfs_root *root, int delay);
void btrfs_init_transaction_sys(void); void btrfs_init_transaction_sys(void);
void btrfs_exit_transaction_sys(void); void btrfs_exit_transaction_sys(void);
int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list); int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest,
struct list_head *dead_list);
int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info); int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info);
int btrfs_defrag_root(struct btrfs_root *root, int cacheonly); int btrfs_defrag_root(struct btrfs_root *root, int cacheonly);
int btrfs_clean_old_snapshots(struct btrfs_root *root); int btrfs_clean_old_snapshots(struct btrfs_root *root);
......
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