mirror of
https://github.com/kdave/btrfs-progs
synced 2025-04-27 05:27:56 +00:00
btrfs-progs: cmd_start_replace() to use test_dev_for_mkfs()
test_dev_for_mkfs() is a common place where we check if a device is fit for the btrfs use. cmd_start_replace() should make use of test_dev_for_mkfs(), and here the test_dev_for_mkfs() is further enhanced to fit the cmd_start_replace() needs. Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
This commit is contained in:
parent
bbe9df154b
commit
689ad3e362
@ -137,13 +137,12 @@ static int cmd_start_replace(int argc, char **argv)
|
|||||||
char *dstdev;
|
char *dstdev;
|
||||||
int avoid_reading_from_srcdev = 0;
|
int avoid_reading_from_srcdev = 0;
|
||||||
int force_using_targetdev = 0;
|
int force_using_targetdev = 0;
|
||||||
u64 total_devs = 1;
|
|
||||||
struct btrfs_fs_devices *fs_devices_mnt = NULL;
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
u64 dstdev_block_count;
|
u64 dstdev_block_count;
|
||||||
int do_not_background = 0;
|
int do_not_background = 0;
|
||||||
int mixed = 0;
|
int mixed = 0;
|
||||||
DIR *dirstream = NULL;
|
DIR *dirstream = NULL;
|
||||||
|
char estr[100]; /* check test_dev_for_mkfs() for error string size*/
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "Brf")) != -1) {
|
while ((c = getopt(argc, argv, "Brf")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
@ -265,15 +264,9 @@ static int cmd_start_replace(int argc, char **argv)
|
|||||||
start_args.start.srcdevid = 0;
|
start_args.start.srcdevid = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = check_mounted(dstdev);
|
ret = test_dev_for_mkfs(dstdev, force_using_targetdev, estr);
|
||||||
if (ret < 0) {
|
if (ret) {
|
||||||
fprintf(stderr, "Error checking %s mount status\n", dstdev);
|
fprintf(stderr, "%s", estr);
|
||||||
goto leave_with_error;
|
|
||||||
}
|
|
||||||
if (ret == 1) {
|
|
||||||
fprintf(stderr,
|
|
||||||
"Error, target device %s is in use and currently mounted!\n",
|
|
||||||
dstdev);
|
|
||||||
goto leave_with_error;
|
goto leave_with_error;
|
||||||
}
|
}
|
||||||
fddstdev = open(dstdev, O_RDWR);
|
fddstdev = open(dstdev, O_RDWR);
|
||||||
@ -281,23 +274,6 @@ static int cmd_start_replace(int argc, char **argv)
|
|||||||
fprintf(stderr, "Unable to open %s\n", dstdev);
|
fprintf(stderr, "Unable to open %s\n", dstdev);
|
||||||
goto leave_with_error;
|
goto leave_with_error;
|
||||||
}
|
}
|
||||||
ret = btrfs_scan_one_device(fddstdev, dstdev, &fs_devices_mnt,
|
|
||||||
&total_devs, BTRFS_SUPER_INFO_OFFSET);
|
|
||||||
if (ret >= 0 && !force_using_targetdev) {
|
|
||||||
fprintf(stderr,
|
|
||||||
"Error, target device %s contains filesystem, use '-f' to force overwriting.\n",
|
|
||||||
dstdev);
|
|
||||||
goto leave_with_error;
|
|
||||||
}
|
|
||||||
ret = fstat(fddstdev, &st);
|
|
||||||
if (ret) {
|
|
||||||
fprintf(stderr, "Error: Unable to stat '%s'\n", dstdev);
|
|
||||||
goto leave_with_error;
|
|
||||||
}
|
|
||||||
if (!S_ISBLK(st.st_mode)) {
|
|
||||||
fprintf(stderr, "Error: '%s' is not a block device\n", dstdev);
|
|
||||||
goto leave_with_error;
|
|
||||||
}
|
|
||||||
strncpy((char *)start_args.start.tgtdev_name, dstdev,
|
strncpy((char *)start_args.start.tgtdev_name, dstdev,
|
||||||
BTRFS_DEVICE_PATH_NAME_MAX);
|
BTRFS_DEVICE_PATH_NAME_MAX);
|
||||||
if (btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0,
|
if (btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0,
|
||||||
|
10
utils.c
10
utils.c
@ -1807,6 +1807,7 @@ int test_dev_for_mkfs(char *file, int force_overwrite, char *estr)
|
|||||||
{
|
{
|
||||||
int ret, fd;
|
int ret, fd;
|
||||||
size_t sz = 100;
|
size_t sz = 100;
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
ret = is_swap_device(file);
|
ret = is_swap_device(file);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@ -1841,6 +1842,15 @@ int test_dev_for_mkfs(char *file, int force_overwrite, char *estr)
|
|||||||
strerror(errno));
|
strerror(errno));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
if (fstat(fd, &st)) {
|
||||||
|
snprintf(estr, sz, "unable to stat %s: %s\n", file,
|
||||||
|
strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (!S_ISBLK(st.st_mode)) {
|
||||||
|
fprintf(stderr, "'%s' is not a block device\n", file);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user