btrfs-progs: free-space-cache: Enhance free space cache free space check
[platform/upstream/btrfs-progs.git] / cmds-balance.c
index 7181673..0c91bdf 100644 (file)
@@ -20,6 +20,9 @@
 #include <unistd.h>
 #include <getopt.h>
 #include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <errno.h>
 
 #include "kerncompat.h"
@@ -29,6 +32,7 @@
 
 #include "commands.h"
 #include "utils.h"
+#include "help.h"
 
 static const char * const balance_cmd_group_usage[] = {
        "btrfs balance <command> [options] <path>",
@@ -439,7 +443,7 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
                printf("WARNING:\n\n");
                printf("\tFull balance without filters requested. This operation is very\n");
                printf("\tintense and takes potentially very long. It is recommended to\n");
-               printf("\tuse the balance filters to narrow down the balanced data.\n");
+               printf("\tuse the balance filters to narrow down the scope of balance.\n");
                printf("\tUse 'btrfs balance start --full-balance' option to skip this\n");
                printf("\twarning. The operation will start in %d seconds.\n", delay);
                printf("\tUse Ctrl-C to stop it.\n");
@@ -471,8 +475,7 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
                                fprintf(stderr, "balance canceled by user\n");
                        ret = 0;
                } else {
-                       error("error during balancing '%s': %s", path,
-                                       strerror(errno));
+                       error("error during balancing '%s': %m", path);
                        if (errno != EINPROGRESS)
                                fprintf(stderr,
                        "There may be more info in syslog - try dmesg | tail\n");
@@ -507,6 +510,8 @@ static const char * const cmd_balance_start_usage[] = {
        "-v             be verbose",
        "-f             force reducing of metadata integrity",
        "--full-balance do not print warning and do not delay start",
+       "--background|--bg",
+       "               run the balance as a background process",
        NULL
 };
 
@@ -517,14 +522,15 @@ static int cmd_balance_start(int argc, char **argv)
                                                &args.meta, NULL };
        int force = 0;
        int verbose = 0;
+       int background = 0;
        unsigned start_flags = 0;
        int i;
 
        memset(&args, 0, sizeof(args));
 
-       optind = 1;
        while (1) {
-               enum { GETOPT_VAL_FULL_BALANCE = 256 };
+               enum { GETOPT_VAL_FULL_BALANCE = 256,
+                       GETOPT_VAL_BACKGROUND = 257 };
                static const struct option longopts[] = {
                        { "data", optional_argument, NULL, 'd'},
                        { "metadata", optional_argument, NULL, 'm' },
@@ -533,6 +539,9 @@ static int cmd_balance_start(int argc, char **argv)
                        { "verbose", no_argument, NULL, 'v' },
                        { "full-balance", no_argument, NULL,
                                GETOPT_VAL_FULL_BALANCE },
+                       { "background", no_argument, NULL,
+                               GETOPT_VAL_BACKGROUND },
+                       { "bg", no_argument, NULL, GETOPT_VAL_BACKGROUND },
                        { NULL, 0, NULL, 0 }
                };
 
@@ -571,6 +580,9 @@ static int cmd_balance_start(int argc, char **argv)
                case GETOPT_VAL_FULL_BALANCE:
                        start_flags |= BALANCE_START_NOWARN;
                        break;
+               case GETOPT_VAL_BACKGROUND:
+                       background = 1;
+                       break;
                default:
                        usage(cmd_balance_start_usage);
                }
@@ -623,6 +635,40 @@ static int cmd_balance_start(int argc, char **argv)
                args.flags |= BTRFS_BALANCE_FORCE;
        if (verbose)
                dump_ioctl_balance_args(&args);
+       if (background) {
+               switch (fork()) {
+               case (-1):
+                       error("unable to fork to run balance in background");
+                       return 1;
+               case (0):
+                       setsid();
+                       switch(fork()) {
+                       case (-1):
+                               error(
+                               "unable to fork to run balance in background");
+                               exit(1);
+                       case (0):
+                               /*
+                                * Read the return value to silence compiler
+                                * warning. Change to / should succeed and
+                                * we're not in a security-sensitive context.
+                                */
+                               i = chdir("/");
+                               close(0);
+                               close(1);
+                               close(2);
+                               open("/dev/null", O_RDONLY);
+                               open("/dev/null", O_WRONLY);
+                               open("/dev/null", O_WRONLY);
+                               break;
+                       default:
+                               exit(0);
+                       }
+                       break;
+               default:
+                       exit(0);
+               }
+       }
 
        return do_balance(argv[optind], &args, start_flags);
 }
@@ -747,9 +793,9 @@ static int cmd_balance_resume(int argc, char **argv)
                        else
                                ret = 1;
                } else {
-                       error("error during balancing '%s': %s\n"
+                       error("error during balancing '%s': %m\n"
                          "There may be more info in syslog - try dmesg | tail",
-                               path, strerror(errno));
+                               path);
                        ret = 1;
                }
        } else {
@@ -785,7 +831,6 @@ static int cmd_balance_status(int argc, char **argv)
        int verbose = 0;
        int ret;
 
-       optind = 1;
        while (1) {
                int opt;
                static const struct option longopts[] = {
@@ -822,7 +867,7 @@ static int cmd_balance_status(int argc, char **argv)
                        ret = 0;
                        goto out;
                }
-               error("balance status on '%s' failed: %s", path, strerror(errno));
+               error("balance status on '%s' failed: %m", path);
                ret = 2;
                goto out;
        }