btrfs-progs: defrag: warn when deframgenting directories without -r
authorDavid Sterba <dsterba@suse.com>
Mon, 12 Dec 2016 17:38:47 +0000 (18:38 +0100)
committerDavid Sterba <dsterba@suse.com>
Wed, 14 Dec 2016 14:06:36 +0000 (15:06 +0100)
The current implementaion of defrag ioctl on directoreis does not do
what users expect. The -r needs to be specified, but we should also
print a warning to avoid confusion.

Signed-off-by: David Sterba <dsterba@suse.com>
cmds-filesystem.c

index eb16875..c66709b 100644 (file)
@@ -1109,6 +1109,35 @@ static int cmd_filesystem_defrag(int argc, char **argv)
        if (flush)
                defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
 
+       /*
+        * Look for directory arguments and warn if the recursive mode is not
+        * requested, as this is not implemented as recursive defragmentation
+        * in kernel. The stat errors are silent here as we check them below.
+        */
+       if (!recursive) {
+               int found = 0;
+
+               for (i = optind; i < argc; i++) {
+                       struct stat st;
+
+                       if (stat(argv[i], &st))
+                               continue;
+
+                       if (S_ISDIR(st.st_mode)) {
+                               warning(
+                       "directory specified but recursive mode not requested: %s",
+                                       argv[i]);
+                               found = 1;
+                       }
+               }
+               if (found) {
+                       warning(
+"a directory passed to the defrag ioctl will not process the files\n"
+"recursively but will defragment the subvolume tree and the extent tree.\n"
+"If this is not intended, please use option -r .");
+               }
+       }
+
        for (i = optind; i < argc; i++) {
                struct stat st;
                int defrag_err = 0;