On 10/12/2018 05:03 PM, Nikolay Borisov wrote:


On 12.10.2018 07:06, Anand Jain wrote:
This patch adds cli
   btrfs device forget [dev]
to remove the given device structure in the kernel if the device
is unmounted. If no argument is given it shall remove all stale
(device which are not mounted) from the kernel.

Signed-off-by: Anand Jain <anand.j...@oracle.com>
---
  cmds-device.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++---------
  ioctl.h       |  2 ++
  2 files changed, 56 insertions(+), 9 deletions(-)

diff --git a/cmds-device.c b/cmds-device.c
index 2a05f70a76a9..ecc391ea01d8 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -254,10 +254,32 @@ static int cmd_device_delete(int argc, char **argv)
        return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
  }
+static int btrfs_forget_devices(char *path)
+{
+       struct btrfs_ioctl_vol_args args;
+       int ret;
+       int fd;
+
+       fd = open("/dev/btrfs-control", O_RDWR);
+       if (fd < 0)
+               return -errno;
+
+       memset(&args, 0, sizeof(args));
+       if (path)
+               strncpy_null(args.name, path);
+       ret = ioctl(fd, BTRFS_IOC_FORGET_DEV, &args);
+       if (ret)
+               ret = -errno;
+       close(fd);
+       return ret;
+}
+
  static const char * const cmd_device_scan_usage[] = {
-       "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
-       "Scan devices for a btrfs filesystem",
+       "btrfs device scan [(-d|--all-devices)|-u|--forget|<device> "\
+                                                       "[<device>...]]",
+       "Scan or forget (deregister) devices for a btrfs filesystem",
        " -d|--all-devices (deprecated)",
+       " -u|--forget [<device> ..]",
        NULL
  };
@@ -267,32 +289,40 @@ static int cmd_device_scan(int argc, char **argv)
        int devstart;
        int all = 0;
        int ret = 0;
+       int forget = 0;
optind = 0;
        while (1) {
                int c;
                static const struct option long_options[] = {
                        { "all-devices", no_argument, NULL, 'd'},
+                       { "forget", no_argument, NULL, 'u'},
                        { NULL, 0, NULL, 0}
                };
- c = getopt_long(argc, argv, "d", long_options, NULL);
+               c = getopt_long(argc, argv, "du", long_options, NULL);
                if (c < 0)
                        break;
                switch (c) {
                case 'd':
                        all = 1;
                        break;
+               case 'u':
+                       forget = 1;
+                       break;
                default:
                        usage(cmd_device_scan_usage);
                }
        }
        devstart = optind;
+ if (all && forget)
+               usage(cmd_device_scan_usage);
+
        if (all && check_argc_max(argc - optind, 1))
                usage(cmd_device_scan_usage);
- if (all || argc - optind == 0) {
+       if (!forget && (all || argc - optind == 0)) {
                printf("Scanning for Btrfs filesystems\n");
                ret = btrfs_scan_devices();
                error_on(ret, "error %d while scanning", ret);
@@ -301,6 +331,13 @@ static int cmd_device_scan(int argc, char **argv)
                goto out;
        }
+ if (forget && (all || argc - optind == 0)) {

You cannot ever have (forget && all) since you specifically call usage()
above, just remove the 'all'. Furthermore, I think the code will be more
legible if it's part of an else if rather than a plain if construct. > In fact 
the whole function will be more readable. I.e you will have the
error conditions at the to, followed by "all device scan" case, followed
by "all device forget" and in the final else branch you will have the
specific device handling.

 Originally the -d | --all option is bit broken.
The -d | --all expects either no arguments OR only one device as an argument.

        if (all && check_argc_max(argc - optind, 1))
                usage(cmd_device_scan_usage);

 Now because of

    if (all || argc - optind == 0) {

   The cli 'btrfs device scan --all /dev/sdb' which should have scanned
    only one device, ends up scanning all the devices and I am not
    trying to fix this bug in this patch because..
  . -d|--all is marked as deprecated, I hope -d option would go away
  . For now some script might be using this bug as a feature, and fixing
    this bug might lead to mount failure.

However still there is some opportunity to make the my patch more readable, thanks for the comments pls see v10 if it is much better?

The increased indentation level won't be a
problem since the longest lines are string prints and we are not
wrapping those.


+               ret = btrfs_forget_devices(NULL);
+               if (ret)
+                       error("Can't forget: %s", strerror(-ret));
+               goto out;
+       }
+
        for( i = devstart ; i < argc ; i++ ){
                char *path;
@@ -315,11 +352,19 @@ static int cmd_device_scan(int argc, char **argv)
                        ret = 1;
                        goto out;
                }
-               printf("Scanning for Btrfs filesystems in '%s'\n", path);
-               if (btrfs_register_one_device(path) != 0) {
-                       ret = 1;
-                       free(path);
-                       goto out;
+               if (forget) {
+                       ret = btrfs_forget_devices(path);
+                       if (ret)
+                               error("Can't forget '%s': %s",
+                                                       path, strerror(-ret));

For consistency sake I think this error printout should be moved inside
btrfs_forget_devices() similarly to what btrfs_register_one_device does
on error.

 Where possible we are trying to move the printing to the parent
 functions, so that these helper functions can be used as a/part
 of the library function. As here we can successfully return the unique
 errno for the two possible errors, I would like to keep it as
 it is as of now.

Thanks, Anand

+               } else {
+                       printf("Scanning for Btrfs filesystems in '%s'\n",
+                                                                       path);
+                       if (btrfs_register_one_device(path) != 0) {
+                               ret = 1;
+                               free(path);
+                               goto out;
+                       }
                }
                free(path);
        }
diff --git a/ioctl.h b/ioctl.h
index 709e996f401c..e27d80e09392 100644
--- a/ioctl.h
+++ b/ioctl.h
@@ -721,6 +721,8 @@ static inline char *btrfs_err_str(enum btrfs_err_code 
err_code)
                                   struct btrfs_ioctl_vol_args)
  #define BTRFS_IOC_SCAN_DEV _IOW(BTRFS_IOCTL_MAGIC, 4, \
                                   struct btrfs_ioctl_vol_args)
+#define BTRFS_IOC_FORGET_DEV _IOW(BTRFS_IOCTL_MAGIC, 5, \
+                                  struct btrfs_ioctl_vol_args)
  /* trans start and trans end are dangerous, and only for
   * use by applications that know how to avoid the
   * resulting deadlocks

Reply via email to