Skip to content

Commit e999376

Browse files
committed
Btrfs: avoid delayed metadata items during commits
Snapshot creation has two phases. One is the initial snapshot setup, and the second is done during commit, while nobody is allowed to modify the root we are snapshotting. The delayed metadata insertion code can break that rule, it does a delayed inode update on the inode of the parent of the snapshot, and delayed directory item insertion. This makes sure to run the pending delayed operations before we record the snapshot root, which avoids corruptions. Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 35a30d7 commit e999376

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎fs/btrfs/delayed-inode.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,13 @@ static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
12371237
return 0;
12381238
}
12391239

1240+
void btrfs_assert_delayed_root_empty(struct btrfs_root *root)
1241+
{
1242+
struct btrfs_delayed_root *delayed_root;
1243+
delayed_root = btrfs_get_delayed_root(root);
1244+
WARN_ON(btrfs_first_delayed_node(delayed_root));
1245+
}
1246+
12401247
void btrfs_balance_delayed_items(struct btrfs_root *root)
12411248
{
12421249
struct btrfs_delayed_root *delayed_root;

‎fs/btrfs/delayed-inode.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,8 @@ int btrfs_readdir_delayed_dir_index(struct file *filp, void *dirent,
137137
/* for init */
138138
int __init btrfs_delayed_inode_init(void);
139139
void btrfs_delayed_inode_exit(void);
140+
141+
/* for debugging */
142+
void btrfs_assert_delayed_root_empty(struct btrfs_root *root);
143+
140144
#endif

‎fs/btrfs/transaction.c‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,15 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
957957
ret = btrfs_update_inode(trans, parent_root, parent_inode);
958958
BUG_ON(ret);
959959

960+
/*
961+
* pull in the delayed directory update
962+
* and the delayed inode item
963+
* otherwise we corrupt the FS during
964+
* snapshot
965+
*/
966+
ret = btrfs_run_delayed_items(trans, root);
967+
BUG_ON(ret);
968+
960969
record_root_in_trans(trans, root);
961970
btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
962971
memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
@@ -1018,14 +1027,6 @@ static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
10181027
int ret;
10191028

10201029
list_for_each_entry(pending, head, list) {
1021-
/*
1022-
* We must deal with the delayed items before creating
1023-
* snapshots, or we will create a snapthot with inconsistent
1024-
* information.
1025-
*/
1026-
ret = btrfs_run_delayed_items(trans, fs_info->fs_root);
1027-
BUG_ON(ret);
1028-
10291030
ret = create_pending_snapshot(trans, fs_info, pending);
10301031
BUG_ON(ret);
10311032
}
@@ -1319,15 +1320,21 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
13191320
*/
13201321
mutex_lock(&root->fs_info->reloc_mutex);
13211322

1322-
ret = create_pending_snapshots(trans, root->fs_info);
1323+
ret = btrfs_run_delayed_items(trans, root);
13231324
BUG_ON(ret);
13241325

1325-
ret = btrfs_run_delayed_items(trans, root);
1326+
ret = create_pending_snapshots(trans, root->fs_info);
13261327
BUG_ON(ret);
13271328

13281329
ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
13291330
BUG_ON(ret);
13301331

1332+
/*
1333+
* make sure none of the code above managed to slip in a
1334+
* delayed item
1335+
*/
1336+
btrfs_assert_delayed_root_empty(root);
1337+
13311338
WARN_ON(cur_trans != trans->transaction);
13321339

13331340
btrfs_scrub_pause(root);

0 commit comments

Comments
 (0)