btrfs-progs: mkfs: keep file descriptors open during whole time

[BUG]
There is an internal bug report that, after mkfs.btrfs there is a chance
that no /dev/disk/by-uuid/<uuid> symlink is not created at all.

[CAUSE]
That uuid symlink is created by udev, which listens to inotify
IN_CLOSE_WRITE events from all block devices.

After such IN_CLOSE_WRITE event is triggered, udev would *disable*
inotify for that block device, and do a blkid scan on it.
After the blkid scan is done, re-enables the inotify listening.

This means normally mkfs tools should open the fd, do all the writes,
and close the fd after everything is done.

But unfortunately for mkfs.btrfs, it's not the case, we have a lot of
phases separated by different close() calls:

  open_ctree() would open fds of each involved device
  and close them at close_ctree()
  Only after close_ctree() we have a valid superblock -\
                                                       |
|<------- A -------->|<--------- B --------->|<------- C ------->|
          |                      |
          |                      `- open a new fd for make_btrfs()
          |                         and close it before open_ctree()
          |                         The device contains invalid sb.
          |
          `- open a new fd for each device, then call
             btrfs_prepare_device(), then close the fd.
             The device would contain no valid superblock.

If at the close() of phase A udev event is triggered, while doing udev
scan we go into phase C (but before the new valid super blocks written),
udev would only see no superblock or invalid superblock.

Then phase C finished, udev resumes its inotify listening, but at this
time mkfs is finished, while udev only sees the premature data from
phase A, and misses the IN_CLOSE_WRITE events from phase C.

[FIX]
Instead of opening and closing a new fd for each device, re-use the fd
opened during prepare_one_device(), and close all the fds until
close_ctree() is called.

By this, although we may still have race between close_ctree() and
explicit close() calls, at least udev can always see the properly
written super blocks.

To compensate the change, some extra cleanups are made:

- Do not touch @device_count
  Which makes later prepare_ctx iteration much easier.

- Remove top-level @fd variable
  Instead go with prepare_ctx[i].fd.

- Do not open with O_RDWR in test_dev_for_mkfs()
  as test_dev_for_mkfs() would close the fd, if we go O_RDWR, it can
  cause the udev race.

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Qu Wenruo 2023-03-15 14:06:54 +08:00 committed by David Sterba
parent 0e59430167
commit b2a1be83b8
2 changed files with 33 additions and 40 deletions

View File

@ -1084,8 +1084,11 @@ int test_dev_for_mkfs(const char *file, int force_overwrite)
ret = test_status_for_mkfs(file, force_overwrite); ret = test_status_for_mkfs(file, force_overwrite);
if (ret) if (ret)
return 1; return 1;
/* check if the device is busy */ /*
fd = open(file, O_RDWR|O_EXCL); * Check if the device is busy. Open it in read-only mode to avoid triggering
* udev events.
*/
fd = open(file, O_RDONLY | O_EXCL);
if (fd < 0) { if (fd < 0) {
error("unable to open %s: %m", file); error("unable to open %s: %m", file);
return 1; return 1;

View File

@ -72,6 +72,7 @@ static bool opt_zoned = true;
static int opt_oflags = O_RDWR; static int opt_oflags = O_RDWR;
struct prepare_device_progress { struct prepare_device_progress {
int fd;
char *file; char *file;
u64 dev_block_count; u64 dev_block_count;
u64 block_count; u64 block_count;
@ -965,23 +966,21 @@ fail:
static void *prepare_one_device(void *ctx) static void *prepare_one_device(void *ctx)
{ {
struct prepare_device_progress *prepare_ctx = ctx; struct prepare_device_progress *prepare_ctx = ctx;
int fd;
fd = open(prepare_ctx->file, opt_oflags); prepare_ctx->fd = open(prepare_ctx->file, opt_oflags);
if (fd < 0) { if (prepare_ctx->fd < 0) {
error("unable to open %s: %m", prepare_ctx->file); error("unable to open %s: %m", prepare_ctx->file);
prepare_ctx->ret = -errno; prepare_ctx->ret = -errno;
return NULL; return NULL;
} }
prepare_ctx->ret = btrfs_prepare_device(fd, prepare_ctx->file, prepare_ctx->ret = btrfs_prepare_device(prepare_ctx->fd,
prepare_ctx->file,
&prepare_ctx->dev_block_count, &prepare_ctx->dev_block_count,
prepare_ctx->block_count, prepare_ctx->block_count,
(bconf.verbose ? PREP_DEVICE_VERBOSE : 0) | (bconf.verbose ? PREP_DEVICE_VERBOSE : 0) |
(opt_zero_end ? PREP_DEVICE_ZERO_END : 0) | (opt_zero_end ? PREP_DEVICE_ZERO_END : 0) |
(opt_discard ? PREP_DEVICE_DISCARD : 0) | (opt_discard ? PREP_DEVICE_DISCARD : 0) |
(opt_zoned ? PREP_DEVICE_ZONED : 0)); (opt_zoned ? PREP_DEVICE_ZONED : 0));
close(fd);
return NULL; return NULL;
} }
@ -992,7 +991,6 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
struct btrfs_fs_info *fs_info; struct btrfs_fs_info *fs_info;
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
struct open_ctree_flags ocf = { 0 }; struct open_ctree_flags ocf = { 0 };
int fd = -1;
int ret = 0; int ret = 0;
int close_ret; int close_ret;
int i; int i;
@ -1245,8 +1243,9 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
} }
} }
while (device_count-- > 0) { for (i = 0; i < device_count; i++) {
file = argv[optind++]; file = argv[optind++];
if (source_dir_set && path_exists(file) == 0) if (source_dir_set && path_exists(file) == 0)
ret = 0; ret = 0;
else if (path_is_block_device(file) == 1) else if (path_is_block_device(file) == 1)
@ -1392,6 +1391,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
if (source_dir_set) { if (source_dir_set) {
int oflags = O_RDWR; int oflags = O_RDWR;
struct stat statbuf; struct stat statbuf;
int fd;
if (path_exists(file) == 0) if (path_exists(file) == 0)
oflags |= O_CREAT; oflags |= O_CREAT;
@ -1522,12 +1522,6 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
goto error; goto error;
} }
device_count--;
fd = open(file, opt_oflags);
if (fd < 0) {
error("unable to open %s: %m", file);
goto error;
}
dev_block_count = prepare_ctx[0].dev_block_count; dev_block_count = prepare_ctx[0].dev_block_count;
if (block_count && block_count > dev_block_count) { if (block_count && block_count > dev_block_count) {
error("%s is smaller than requested size, expected %llu, found %llu", error("%s is smaller than requested size, expected %llu, found %llu",
@ -1568,7 +1562,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
else else
mkfs_cfg.zone_size = 0; mkfs_cfg.zone_size = 0;
ret = make_btrfs(fd, &mkfs_cfg); ret = make_btrfs(prepare_ctx[0].fd, &mkfs_cfg);
if (ret) { if (ret) {
errno = -ret; errno = -ret;
error("error during mkfs: %m"); error("error during mkfs: %m");
@ -1582,8 +1576,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
error("open ctree failed"); error("open ctree failed");
goto error; goto error;
} }
close(fd);
fd = -1;
root = fs_info->fs_root; root = fs_info->fs_root;
ret = create_metadata_block_groups(root, mixed, &allocation); ret = create_metadata_block_groups(root, mixed, &allocation);
@ -1636,36 +1629,28 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
if (device_count == 0) if (device_count == 0)
goto raid_groups; goto raid_groups;
while (device_count-- > 0) { for (i = 1; i < device_count; i++) {
int dev_index = argc - saved_optind - device_count - 1; ret = btrfs_device_already_in_root(root, prepare_ctx[i].fd,
file = argv[optind++];
fd = open(file, opt_oflags);
if (fd < 0) {
error("unable to open %s: %m", file);
goto error;
}
ret = btrfs_device_already_in_root(root, fd,
BTRFS_SUPER_INFO_OFFSET); BTRFS_SUPER_INFO_OFFSET);
if (ret) { if (ret) {
error("skipping duplicate device %s in the filesystem", error("skipping duplicate device %s in the filesystem",
file); file);
close(fd);
continue; continue;
} }
dev_block_count = prepare_ctx[dev_index].dev_block_count; dev_block_count = prepare_ctx[i].dev_block_count;
if (prepare_ctx[dev_index].ret) { if (prepare_ctx[i].ret) {
errno = -prepare_ctx[dev_index].ret; errno = -prepare_ctx[i].ret;
error("unable to prepare device %s: %m", error("unable to prepare device %s: %m", prepare_ctx[i].file);
prepare_ctx[dev_index].file);
goto error; goto error;
} }
ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count, ret = btrfs_add_to_fsid(trans, root, prepare_ctx[i].fd,
prepare_ctx[i].file, dev_block_count,
sectorsize, sectorsize, sectorsize); sectorsize, sectorsize, sectorsize);
if (ret) { if (ret) {
error("unable to add %s to filesystem: %d", file, ret); error("unable to add %s to filesystem: %d",
prepare_ctx[i].file, ret);
goto error; goto error;
} }
if (bconf.verbose >= 2) { if (bconf.verbose >= 2) {
@ -1839,6 +1824,10 @@ out:
} }
btrfs_close_all_devices(); btrfs_close_all_devices();
if (prepare_ctx) {
for (i = 0; i < device_count; i++)
close(prepare_ctx[i].fd);
}
free(t_prepare); free(t_prepare);
free(prepare_ctx); free(prepare_ctx);
free(label); free(label);
@ -1847,9 +1836,10 @@ out:
return !!ret; return !!ret;
error: error:
if (fd > 0) if (prepare_ctx) {
close(fd); for (i = 0; i < device_count; i++)
close(prepare_ctx[i].fd);
}
free(t_prepare); free(t_prepare);
free(prepare_ctx); free(prepare_ctx);
free(label); free(label);