Skip to content

Commit 9a2e024

Browse files
adam900710kdave
authored andcommitted
btrfs-progs: open the devices exclusively for writes
There is an internal report that, during btrfs-convert to block-group tree, by accident some systemd events triggered the mount of the target fs. This leads to double mount (one by kernel and one by the btrfs-progs), which seems to cause quite some problems. To avoid such accident, exclusively opens all devices if btrfs-progs is doing write operations. Pull-request: #888 Reported-by: pandada8 <[email protected]> Signed-off-by: Qu Wenruo <[email protected]>
1 parent 17e53fd commit 9a2e024

File tree

6 files changed

+13
-8
lines changed

6 files changed

+13
-8
lines changed

cmds/rescue.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static int cmd_rescue_zero_log(const struct cmd_struct *cmd,
203203
}
204204

205205
root = open_ctree(devname, 0, OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL |
206-
OPEN_CTREE_NO_BLOCK_GROUPS);
206+
OPEN_CTREE_NO_BLOCK_GROUPS | OPEN_CTREE_EXCLUSIVE);
207207
if (!root) {
208208
error("could not open ctree");
209209
return 1;
@@ -258,7 +258,7 @@ static int cmd_rescue_fix_device_size(const struct cmd_struct *cmd,
258258
}
259259

260260
oca.filename = devname;
261-
oca.flags = OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL;
261+
oca.flags = OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL | OPEN_CTREE_EXCLUSIVE;
262262
fs_info = open_ctree_fs_info(&oca);
263263
if (!fs_info) {
264264
error("could not open btrfs");
@@ -437,7 +437,7 @@ static int cmd_rescue_clear_ino_cache(const struct cmd_struct *cmd,
437437
goto out;
438438
}
439439
oca.filename = devname;
440-
oca.flags = OPEN_CTREE_WRITES;
440+
oca.flags = OPEN_CTREE_WRITES | OPEN_CTREE_EXCLUSIVE;
441441
fs_info = open_ctree_fs_info(&oca);
442442
if (!fs_info) {
443443
error("could not open btrfs");

common/filesystem-utils.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ static int set_label_unmounted(const char *dev, const char *label)
9494
/* Open the super_block at the default location
9595
* and as read-write.
9696
*/
97-
root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
97+
root = open_ctree(dev, 0, OPEN_CTREE_WRITES |
98+
OPEN_CTREE_EXCLUSIVE);
9899
if (!root) /* errors are printed by open_ctree() */
99100
return -1;
100101

convert/main.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,8 @@ static int do_convert(const char *devname, u32 convert_flags, u32 nodesize,
13741374
btrfs_sb_committed = true;
13751375

13761376
root = open_ctree_fd(fd, devname, 0,
1377-
OPEN_CTREE_WRITES | OPEN_CTREE_TEMPORARY_SUPER);
1377+
OPEN_CTREE_WRITES | OPEN_CTREE_TEMPORARY_SUPER |
1378+
OPEN_CTREE_EXCLUSIVE);
13781379
if (!root) {
13791380
error("unable to open ctree for finalization");
13801381
goto fail;

image/image-restore.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,8 @@ int restore_metadump(const char *input, FILE *out, int old_restore,
17901790

17911791
oca.filename = target;
17921792
oca.flags = OPEN_CTREE_WRITES | OPEN_CTREE_RESTORE |
1793-
OPEN_CTREE_PARTIAL | OPEN_CTREE_SKIP_LEAF_ITEM_CHECKS;
1793+
OPEN_CTREE_PARTIAL | OPEN_CTREE_SKIP_LEAF_ITEM_CHECKS |
1794+
OPEN_CTREE_EXCLUSIVE;
17941795
info = open_ctree_fs_info(&oca);
17951796
if (!info) {
17961797
error("open ctree failed");
@@ -1855,6 +1856,7 @@ int restore_metadump(const char *input, FILE *out, int old_restore,
18551856
root = open_ctree_fd(fileno(out), target, 0,
18561857
OPEN_CTREE_PARTIAL |
18571858
OPEN_CTREE_WRITES |
1859+
OPEN_CTREE_EXCLUSIVE |
18581860
OPEN_CTREE_NO_DEVICES |
18591861
OPEN_CTREE_ALLOW_TRANSID_MISMATCH |
18601862
OPEN_CTREE_SKIP_LEAF_ITEM_CHECKS);

mkfs/main.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,8 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
18461846
}
18471847

18481848
oca.filename = file;
1849-
oca.flags = OPEN_CTREE_WRITES | OPEN_CTREE_TEMPORARY_SUPER;
1849+
oca.flags = OPEN_CTREE_WRITES | OPEN_CTREE_TEMPORARY_SUPER |
1850+
OPEN_CTREE_EXCLUSIVE;
18501851
fs_info = open_ctree_fs_info(&oca);
18511852
if (!fs_info) {
18521853
error("open ctree failed");

tune/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[])
185185
{
186186
struct btrfs_root *root;
187187
struct btrfs_fs_info *fs_info;
188-
unsigned ctree_flags = OPEN_CTREE_WRITES;
188+
unsigned ctree_flags = OPEN_CTREE_WRITES | OPEN_CTREE_EXCLUSIVE;
189189
int seeding_flag = 0;
190190
u64 seeding_value = 0;
191191
int random_fsid = 0;

0 commit comments

Comments
 (0)