Commit 8407aa46 authored by Miao Xie's avatar Miao Xie Committed by Chris Mason

Btrfs: fix corrupted metadata in the snapshot

When we delete a inode, we will remove all the delayed items including delayed
inode update, and then truncate all the relative metadata. If there is lots of
metadata, we will end the current transaction, and start a new transaction to
truncate the left metadata. In this way, we will leave a inode item that its
link counter is > 0, and also may leave some directory index items in fs/file tree
after the current transaction ends. In other words, the metadata in this fs/file tree
is inconsistent. If we create a snapshot for this tree now, we will find a inode with
corrupted metadata in the new snapshot, and we won't continue to drop the left metadata,
because its link counter is not 0.

We fix this problem by updating the inode item before the current transaction ends.
Signed-off-by: default avatarMiao Xie <miaox@cn.fujitsu.com>
parent 837e1972
...@@ -3834,15 +3834,10 @@ void btrfs_evict_inode(struct inode *inode) ...@@ -3834,15 +3834,10 @@ void btrfs_evict_inode(struct inode *inode)
btrfs_i_size_write(inode, 0); btrfs_i_size_write(inode, 0);
/* /*
* This is a bit simpler than btrfs_truncate since * This is a bit simpler than btrfs_truncate since we've already
* * reserved our space for our orphan item in the unlink, so we just
* 1) We've already reserved our space for our orphan item in the * need to reserve some slack space in case we add bytes and update
* unlink. * inode item when doing the truncate.
* 2) We're going to delete the inode item, so we don't need to update
* it at all.
*
* So we just need to reserve some slack space in case we add bytes when
* doing the truncate.
*/ */
while (1) { while (1) {
ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size); ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
...@@ -3863,7 +3858,7 @@ void btrfs_evict_inode(struct inode *inode) ...@@ -3863,7 +3858,7 @@ void btrfs_evict_inode(struct inode *inode)
goto no_delete; goto no_delete;
} }
trans = btrfs_start_transaction(root, 0); trans = btrfs_start_transaction_noflush(root, 1);
if (IS_ERR(trans)) { if (IS_ERR(trans)) {
btrfs_orphan_del(NULL, inode); btrfs_orphan_del(NULL, inode);
btrfs_free_block_rsv(root, rsv); btrfs_free_block_rsv(root, rsv);
...@@ -3876,6 +3871,10 @@ void btrfs_evict_inode(struct inode *inode) ...@@ -3876,6 +3871,10 @@ void btrfs_evict_inode(struct inode *inode)
if (ret != -ENOSPC) if (ret != -ENOSPC)
break; break;
trans->block_rsv = &root->fs_info->trans_block_rsv;
ret = btrfs_update_inode(trans, root, inode);
BUG_ON(ret);
nr = trans->blocks_used; nr = trans->blocks_used;
btrfs_end_transaction(trans, root); btrfs_end_transaction(trans, root);
trans = NULL; trans = NULL;
......
...@@ -290,7 +290,8 @@ static int may_wait_transaction(struct btrfs_root *root, int type) ...@@ -290,7 +290,8 @@ static int may_wait_transaction(struct btrfs_root *root, int type)
} }
static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
u64 num_items, int type) u64 num_items, int type,
int noflush)
{ {
struct btrfs_trans_handle *h; struct btrfs_trans_handle *h;
struct btrfs_transaction *cur_trans; struct btrfs_transaction *cur_trans;
...@@ -324,6 +325,11 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, ...@@ -324,6 +325,11 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
} }
num_bytes = btrfs_calc_trans_metadata_size(root, num_items); num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
if (noflush)
ret = btrfs_block_rsv_add_noflush(root,
&root->fs_info->trans_block_rsv,
num_bytes);
else
ret = btrfs_block_rsv_add(root, ret = btrfs_block_rsv_add(root,
&root->fs_info->trans_block_rsv, &root->fs_info->trans_block_rsv,
num_bytes); num_bytes);
...@@ -393,21 +399,28 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, ...@@ -393,21 +399,28 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
int num_items) int num_items)
{ {
return start_transaction(root, num_items, TRANS_START); return start_transaction(root, num_items, TRANS_START, 0);
} }
struct btrfs_trans_handle *btrfs_start_transaction_noflush(
struct btrfs_root *root, int num_items)
{
return start_transaction(root, num_items, TRANS_START, 1);
}
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
{ {
return start_transaction(root, 0, TRANS_JOIN); return start_transaction(root, 0, TRANS_JOIN, 0);
} }
struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
{ {
return start_transaction(root, 0, TRANS_JOIN_NOLOCK); return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
} }
struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
{ {
return start_transaction(root, 0, TRANS_USERSPACE); return start_transaction(root, 0, TRANS_USERSPACE, 0);
} }
/* wait for a transaction commit to be fully complete */ /* wait for a transaction commit to be fully complete */
......
...@@ -97,6 +97,8 @@ int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans, ...@@ -97,6 +97,8 @@ int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
struct btrfs_root *root); struct btrfs_root *root);
struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
int num_items); int num_items);
struct btrfs_trans_handle *btrfs_start_transaction_noflush(
struct btrfs_root *root, int num_items);
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root); struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root); struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root);
struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root); struct btrfs_trans_handle *btrfs_start_ioctl_transaction(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