btrfs-progs: extend parse_range API to accept a relaxed range
[platform/upstream/btrfs-progs.git] / cmds-balance.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <getopt.h>
22 #include <sys/ioctl.h>
23 #include <errno.h>
24
25 #include "kerncompat.h"
26 #include "ctree.h"
27 #include "ioctl.h"
28 #include "volumes.h"
29
30 #include "commands.h"
31 #include "utils.h"
32
33 static const char * const balance_cmd_group_usage[] = {
34         "btrfs balance <command> [options] <path>",
35         "btrfs balance <path>",
36         NULL
37 };
38
39 static int parse_one_profile(const char *profile, u64 *flags)
40 {
41         if (!strcmp(profile, "raid0")) {
42                 *flags |= BTRFS_BLOCK_GROUP_RAID0;
43         } else if (!strcmp(profile, "raid1")) {
44                 *flags |= BTRFS_BLOCK_GROUP_RAID1;
45         } else if (!strcmp(profile, "raid10")) {
46                 *flags |= BTRFS_BLOCK_GROUP_RAID10;
47         } else if (!strcmp(profile, "raid5")) {
48                 *flags |= BTRFS_BLOCK_GROUP_RAID5;
49         } else if (!strcmp(profile, "raid6")) {
50                 *flags |= BTRFS_BLOCK_GROUP_RAID6;
51         } else if (!strcmp(profile, "dup")) {
52                 *flags |= BTRFS_BLOCK_GROUP_DUP;
53         } else if (!strcmp(profile, "single")) {
54                 *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
55         } else {
56                 fprintf(stderr, "Unknown profile '%s'\n", profile);
57                 return 1;
58         }
59
60         return 0;
61 }
62
63 static int parse_profiles(char *profiles, u64 *flags)
64 {
65         char *this_char;
66         char *save_ptr = NULL; /* Satisfy static checkers */
67
68         for (this_char = strtok_r(profiles, "|", &save_ptr);
69              this_char != NULL;
70              this_char = strtok_r(NULL, "|", &save_ptr)) {
71                 if (parse_one_profile(this_char, flags))
72                         return 1;
73         }
74
75         return 0;
76 }
77
78 static int parse_u64(const char *str, u64 *result)
79 {
80         char *endptr;
81         u64 val;
82
83         val = strtoull(str, &endptr, 10);
84         if (*endptr)
85                 return 1;
86
87         *result = val;
88         return 0;
89 }
90
91 /*
92  * Parse range that's missing some part that can be implicit:
93  * a..b - exact range, a can be equal to b
94  * a..  - implicitly unbounded maximum (end == (u64)-1)
95  * ..b  - implicitly starting at 0
96  * a    - invalid; unclear semantics, use parse_u64 instead
97  *
98  * Returned values are u64, value validation and interpretation should be done
99  * by the caller.
100  */
101 static int parse_range(const char *range, u64 *start, u64 *end)
102 {
103         char *dots;
104         char *endptr;
105         const char *rest;
106         int skipped = 0;
107
108         dots = strstr(range, "..");
109         if (!dots)
110                 return 1;
111
112         rest = dots + 2;
113
114         if (!*rest) {
115                 *end = (u64)-1;
116                 skipped++;
117         } else {
118                 *end = strtoull(rest, &endptr, 10);
119                 if (*endptr)
120                         return 1;
121         }
122         if (dots == range) {
123                 *start = 0;
124                 skipped++;
125         } else {
126                 *end = strtoull(range, &endptr, 10);
127                 if (*endptr != 0 && *endptr != '.')
128                         return 1;
129         }
130
131         if (*start > *end) {
132                 fprintf(stderr,
133                         "ERROR: range %llu..%llu doesn't make sense\n",
134                         (unsigned long long)*start,
135                         (unsigned long long)*end);
136                 return 1;
137         }
138
139         if (skipped <= 1)
140                 return 0;
141
142         return 1;
143 }
144
145 /*
146  * Parse range and check if start < end
147  */
148 static int parse_range_strict(const char *range, u64 *start, u64 *end)
149 {
150         if (parse_range(range, start, end) == 0) {
151                 if (*start >= *end) {
152                         fprintf(stderr,
153                                 "ERROR: range %llu..%llu not allowed\n",
154                                 (unsigned long long)*start,
155                                 (unsigned long long)*end);
156                         return 1;
157                 }
158                 return 0;
159         }
160
161         return 1;
162 }
163
164 static int parse_filters(char *filters, struct btrfs_balance_args *args)
165 {
166         char *this_char;
167         char *value;
168         char *save_ptr = NULL; /* Satisfy static checkers */
169
170         if (!filters)
171                 return 0;
172
173         for (this_char = strtok_r(filters, ",", &save_ptr);
174              this_char != NULL;
175              this_char = strtok_r(NULL, ",", &save_ptr)) {
176                 if ((value = strchr(this_char, '=')) != NULL)
177                         *value++ = 0;
178                 if (!strcmp(this_char, "profiles")) {
179                         if (!value || !*value) {
180                                 fprintf(stderr, "the profiles filter requires "
181                                        "an argument\n");
182                                 return 1;
183                         }
184                         if (parse_profiles(value, &args->profiles)) {
185                                 fprintf(stderr, "Invalid profiles argument\n");
186                                 return 1;
187                         }
188                         args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
189                 } else if (!strcmp(this_char, "usage")) {
190                         if (!value || !*value) {
191                                 fprintf(stderr, "the usage filter requires "
192                                        "an argument\n");
193                                 return 1;
194                         }
195                         if (parse_u64(value, &args->usage) ||
196                             args->usage > 100) {
197                                 fprintf(stderr, "Invalid usage argument: %s\n",
198                                        value);
199                                 return 1;
200                         }
201                         args->flags |= BTRFS_BALANCE_ARGS_USAGE;
202                 } else if (!strcmp(this_char, "devid")) {
203                         if (!value || !*value) {
204                                 fprintf(stderr, "the devid filter requires "
205                                        "an argument\n");
206                                 return 1;
207                         }
208                         if (parse_u64(value, &args->devid) ||
209                             args->devid == 0) {
210                                 fprintf(stderr, "Invalid devid argument: %s\n",
211                                        value);
212                                 return 1;
213                         }
214                         args->flags |= BTRFS_BALANCE_ARGS_DEVID;
215                 } else if (!strcmp(this_char, "drange")) {
216                         if (!value || !*value) {
217                                 fprintf(stderr, "the drange filter requires "
218                                        "an argument\n");
219                                 return 1;
220                         }
221                         if (parse_range_strict(value, &args->pstart, &args->pend)) {
222                                 fprintf(stderr, "Invalid drange argument\n");
223                                 return 1;
224                         }
225                         args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
226                 } else if (!strcmp(this_char, "vrange")) {
227                         if (!value || !*value) {
228                                 fprintf(stderr, "the vrange filter requires "
229                                        "an argument\n");
230                                 return 1;
231                         }
232                         if (parse_range_strict(value, &args->vstart, &args->vend)) {
233                                 fprintf(stderr, "Invalid vrange argument\n");
234                                 return 1;
235                         }
236                         args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
237                 } else if (!strcmp(this_char, "convert")) {
238                         if (!value || !*value) {
239                                 fprintf(stderr, "the convert option requires "
240                                        "an argument\n");
241                                 return 1;
242                         }
243                         if (parse_one_profile(value, &args->target)) {
244                                 fprintf(stderr, "Invalid convert argument\n");
245                                 return 1;
246                         }
247                         args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
248                 } else if (!strcmp(this_char, "soft")) {
249                         args->flags |= BTRFS_BALANCE_ARGS_SOFT;
250                 } else if (!strcmp(this_char, "limit")) {
251                         if (!value || !*value) {
252                                 fprintf(stderr,
253                                         "the limit filter requires an argument\n");
254                                 return 1;
255                         }
256                         if (parse_u64(value, &args->limit)) {
257                                 fprintf(stderr, "Invalid limit argument: %s\n",
258                                        value);
259                                 return 1;
260                         }
261                         args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
262                 } else {
263                         fprintf(stderr, "Unrecognized balance option '%s'\n",
264                                 this_char);
265                         return 1;
266                 }
267         }
268
269         return 0;
270 }
271
272 static void dump_balance_args(struct btrfs_balance_args *args)
273 {
274         if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
275                 printf("converting, target=%llu, soft is %s",
276                        (unsigned long long)args->target,
277                        (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
278         } else {
279                 printf("balancing");
280         }
281
282         if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
283                 printf(", profiles=%llu", (unsigned long long)args->profiles);
284         if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
285                 printf(", usage=%llu", (unsigned long long)args->usage);
286         if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
287                 printf(", devid=%llu", (unsigned long long)args->devid);
288         if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
289                 printf(", drange=%llu..%llu",
290                        (unsigned long long)args->pstart,
291                        (unsigned long long)args->pend);
292         if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
293                 printf(", vrange=%llu..%llu",
294                        (unsigned long long)args->vstart,
295                        (unsigned long long)args->vend);
296         if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
297                 printf(", limit=%llu", (unsigned long long)args->limit);
298
299         printf("\n");
300 }
301
302 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
303 {
304         printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
305                (unsigned long long)args->flags, (unsigned long long)args->state,
306                (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
307         if (args->flags & BTRFS_BALANCE_DATA) {
308                 printf("  DATA (flags 0x%llx): ",
309                        (unsigned long long)args->data.flags);
310                 dump_balance_args(&args->data);
311         }
312         if (args->flags & BTRFS_BALANCE_METADATA) {
313                 printf("  METADATA (flags 0x%llx): ",
314                        (unsigned long long)args->meta.flags);
315                 dump_balance_args(&args->meta);
316         }
317         if (args->flags & BTRFS_BALANCE_SYSTEM) {
318                 printf("  SYSTEM (flags 0x%llx): ",
319                        (unsigned long long)args->sys.flags);
320                 dump_balance_args(&args->sys);
321         }
322 }
323
324 static int do_balance_v1(int fd)
325 {
326         struct btrfs_ioctl_vol_args args;
327         int ret;
328
329         memset(&args, 0, sizeof(args));
330         ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
331         return ret;
332 }
333
334 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
335                       int nofilters)
336 {
337         int fd;
338         int ret;
339         int e;
340         DIR *dirstream = NULL;
341
342         fd = btrfs_open_dir(path, &dirstream, 1);
343         if (fd < 0)
344                 return 1;
345
346         ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
347         e = errno;
348
349         if (ret < 0) {
350                 /*
351                  * older kernels don't have the new balance ioctl, try the
352                  * old one.  But, the old one doesn't know any filters, so
353                  * don't fall back if they tried to use the fancy new things
354                  */
355                 if (e == ENOTTY && nofilters) {
356                         ret = do_balance_v1(fd);
357                         if (ret == 0)
358                                 goto out;
359                         e = errno;
360                 }
361
362                 if (e == ECANCELED) {
363                         if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
364                                 fprintf(stderr, "balance paused by user\n");
365                         if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
366                                 fprintf(stderr, "balance canceled by user\n");
367                         ret = 0;
368                 } else {
369                         fprintf(stderr, "ERROR: error during balancing '%s' "
370                                 "- %s\n", path, strerror(e));
371                         if (e != EINPROGRESS)
372                                 fprintf(stderr, "There may be more info in "
373                                         "syslog - try dmesg | tail\n");
374                         ret = 1;
375                 }
376         } else {
377                 printf("Done, had to relocate %llu out of %llu chunks\n",
378                        (unsigned long long)args->stat.completed,
379                        (unsigned long long)args->stat.considered);
380                 ret = 0;
381         }
382
383 out:
384         close_file_or_dir(fd, dirstream);
385         return ret;
386 }
387
388 static const char * const cmd_balance_start_usage[] = {
389         "btrfs balance start [options] <path>",
390         "Balance chunks across the devices",
391         "Balance and/or convert (change allocation profile of) chunks that",
392         "passed all filters in a comma-separated list of filters for a",
393         "particular chunk type.  If filter list is not given balance all",
394         "chunks of that type.  In case none of the -d, -m or -s options is",
395         "given balance all chunks in a filesystem.",
396         "",
397         "-d[filters]    act on data chunks",
398         "-m[filters]    act on metadata chunks",
399         "-s[filters]    act on system chunks (only under -f)",
400         "-v             be verbose",
401         "-f             force reducing of metadata integrity",
402         NULL
403 };
404
405 static int cmd_balance_start(int argc, char **argv)
406 {
407         struct btrfs_ioctl_balance_args args;
408         struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
409                                                 &args.meta, NULL };
410         int force = 0;
411         int verbose = 0;
412         int nofilters = 1;
413         int i;
414
415         memset(&args, 0, sizeof(args));
416
417         optind = 1;
418         while (1) {
419                 static const struct option longopts[] = {
420                         { "data", optional_argument, NULL, 'd'},
421                         { "metadata", optional_argument, NULL, 'm' },
422                         { "system", optional_argument, NULL, 's' },
423                         { "force", no_argument, NULL, 'f' },
424                         { "verbose", no_argument, NULL, 'v' },
425                         { NULL, 0, NULL, 0 }
426                 };
427
428                 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
429                 if (opt < 0)
430                         break;
431
432                 switch (opt) {
433                 case 'd':
434                         nofilters = 0;
435                         args.flags |= BTRFS_BALANCE_DATA;
436
437                         if (parse_filters(optarg, &args.data))
438                                 return 1;
439                         break;
440                 case 's':
441                         nofilters = 0;
442                         args.flags |= BTRFS_BALANCE_SYSTEM;
443
444                         if (parse_filters(optarg, &args.sys))
445                                 return 1;
446                         break;
447                 case 'm':
448                         nofilters = 0;
449                         args.flags |= BTRFS_BALANCE_METADATA;
450
451                         if (parse_filters(optarg, &args.meta))
452                                 return 1;
453                         break;
454                 case 'f':
455                         force = 1;
456                         break;
457                 case 'v':
458                         verbose = 1;
459                         break;
460                 default:
461                         usage(cmd_balance_start_usage);
462                 }
463         }
464
465         if (check_argc_exact(argc - optind, 1))
466                 usage(cmd_balance_start_usage);
467
468         /*
469          * allow -s only under --force, otherwise do with system chunks
470          * the same thing we were ordered to do with meta chunks
471          */
472         if (args.flags & BTRFS_BALANCE_SYSTEM) {
473                 if (!force) {
474                         fprintf(stderr,
475 "Refusing to explicitly operate on system chunks.\n"
476 "Pass --force if you really want to do that.\n");
477                         return 1;
478                 }
479         } else if (args.flags & BTRFS_BALANCE_METADATA) {
480                 args.flags |= BTRFS_BALANCE_SYSTEM;
481                 memcpy(&args.sys, &args.meta,
482                         sizeof(struct btrfs_balance_args));
483         }
484
485         if (nofilters) {
486                 /* relocate everything - no filters */
487                 args.flags |= BTRFS_BALANCE_TYPE_MASK;
488         }
489
490         /* drange makes sense only when devid is set */
491         for (i = 0; ptrs[i]; i++) {
492                 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
493                     !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
494                         fprintf(stderr, "drange filter can be used only if "
495                                 "devid filter is used\n");
496                         return 1;
497                 }
498         }
499
500         /* soft makes sense only when convert for corresponding type is set */
501         for (i = 0; ptrs[i]; i++) {
502                 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
503                     !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
504                         fprintf(stderr, "'soft' option can be used only if "
505                                 "changing profiles\n");
506                         return 1;
507                 }
508         }
509
510         if (force)
511                 args.flags |= BTRFS_BALANCE_FORCE;
512         if (verbose)
513                 dump_ioctl_balance_args(&args);
514
515         return do_balance(argv[optind], &args, nofilters);
516 }
517
518 static const char * const cmd_balance_pause_usage[] = {
519         "btrfs balance pause <path>",
520         "Pause running balance",
521         NULL
522 };
523
524 static int cmd_balance_pause(int argc, char **argv)
525 {
526         const char *path;
527         int fd;
528         int ret;
529         int e;
530         DIR *dirstream = NULL;
531
532         if (check_argc_exact(argc, 2))
533                 usage(cmd_balance_pause_usage);
534
535         path = argv[1];
536
537         fd = btrfs_open_dir(path, &dirstream, 1);
538         if (fd < 0)
539                 return 1;
540
541         ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
542         e = errno;
543         close_file_or_dir(fd, dirstream);
544
545         if (ret < 0) {
546                 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
547                         path, (e == ENOTCONN) ? "Not running" : strerror(e));
548                 if (e == ENOTCONN)
549                         return 2;
550                 else
551                         return 1;
552         }
553
554         return 0;
555 }
556
557 static const char * const cmd_balance_cancel_usage[] = {
558         "btrfs balance cancel <path>",
559         "Cancel running or paused balance",
560         NULL
561 };
562
563 static int cmd_balance_cancel(int argc, char **argv)
564 {
565         const char *path;
566         int fd;
567         int ret;
568         int e;
569         DIR *dirstream = NULL;
570
571         if (check_argc_exact(argc, 2))
572                 usage(cmd_balance_cancel_usage);
573
574         path = argv[1];
575
576         fd = btrfs_open_dir(path, &dirstream, 1);
577         if (fd < 0)
578                 return 1;
579
580         ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
581         e = errno;
582         close_file_or_dir(fd, dirstream);
583
584         if (ret < 0) {
585                 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
586                         path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
587                 if (e == ENOTCONN)
588                         return 2;
589                 else
590                         return 1;
591         }
592
593         return 0;
594 }
595
596 static const char * const cmd_balance_resume_usage[] = {
597         "btrfs balance resume <path>",
598         "Resume interrupted balance",
599         NULL
600 };
601
602 static int cmd_balance_resume(int argc, char **argv)
603 {
604         struct btrfs_ioctl_balance_args args;
605         const char *path;
606         DIR *dirstream = NULL;
607         int fd;
608         int ret;
609         int e;
610
611         if (check_argc_exact(argc, 2))
612                 usage(cmd_balance_resume_usage);
613
614         path = argv[1];
615
616         fd = btrfs_open_dir(path, &dirstream, 1);
617         if (fd < 0)
618                 return 1;
619
620         memset(&args, 0, sizeof(args));
621         args.flags |= BTRFS_BALANCE_RESUME;
622
623         ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
624         e = errno;
625         close_file_or_dir(fd, dirstream);
626
627         if (ret < 0) {
628                 if (e == ECANCELED) {
629                         if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
630                                 fprintf(stderr, "balance paused by user\n");
631                         if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
632                                 fprintf(stderr, "balance canceled by user\n");
633                 } else if (e == ENOTCONN || e == EINPROGRESS) {
634                         fprintf(stderr, "ERROR: balance resume on '%s' "
635                                 "failed - %s\n", path,
636                                 (e == ENOTCONN) ? "Not in progress" :
637                                                   "Already running");
638                         if (e == ENOTCONN)
639                                 return 2;
640                         else
641                                 return 1;
642                 } else {
643                         fprintf(stderr,
644 "ERROR: error during balancing '%s' - %s\n"
645 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
646                         return 1;
647                 }
648         } else {
649                 printf("Done, had to relocate %llu out of %llu chunks\n",
650                        (unsigned long long)args.stat.completed,
651                        (unsigned long long)args.stat.considered);
652         }
653
654         return 0;
655 }
656
657 static const char * const cmd_balance_status_usage[] = {
658         "btrfs balance status [-v] <path>",
659         "Show status of running or paused balance",
660         "",
661         "-v     be verbose",
662         NULL
663 };
664
665 /* Checks the status of the balance if any
666  * return codes:
667  *   2 : Error failed to know if there is any pending balance
668  *   1 : Successful to know status of a pending balance
669  *   0 : When there is no pending balance or completed
670  */
671 static int cmd_balance_status(int argc, char **argv)
672 {
673         struct btrfs_ioctl_balance_args args;
674         const char *path;
675         DIR *dirstream = NULL;
676         int fd;
677         int verbose = 0;
678         int ret;
679         int e;
680
681         optind = 1;
682         while (1) {
683                 int opt;
684                 static const struct option longopts[] = {
685                         { "verbose", no_argument, NULL, 'v' },
686                         { NULL, 0, NULL, 0 }
687                 };
688
689                 opt = getopt_long(argc, argv, "v", longopts, NULL);
690                 if (opt < 0)
691                         break;
692
693                 switch (opt) {
694                 case 'v':
695                         verbose = 1;
696                         break;
697                 default:
698                         usage(cmd_balance_status_usage);
699                 }
700         }
701
702         if (check_argc_exact(argc - optind, 1))
703                 usage(cmd_balance_status_usage);
704
705         path = argv[optind];
706
707         fd = btrfs_open_dir(path, &dirstream, 1);
708         if (fd < 0)
709                 return 2;
710
711         ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
712         e = errno;
713         close_file_or_dir(fd, dirstream);
714
715         if (ret < 0) {
716                 if (e == ENOTCONN) {
717                         printf("No balance found on '%s'\n", path);
718                         return 0;
719                 }
720                 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
721                         path, strerror(e));
722                 return 2;
723         }
724
725         if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
726                 printf("Balance on '%s' is running", path);
727                 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
728                         printf(", cancel requested\n");
729                 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
730                         printf(", pause requested\n");
731                 else
732                         printf("\n");
733         } else {
734                 printf("Balance on '%s' is paused\n", path);
735         }
736
737         printf("%llu out of about %llu chunks balanced (%llu considered), "
738                "%3.f%% left\n", (unsigned long long)args.stat.completed,
739                (unsigned long long)args.stat.expected,
740                (unsigned long long)args.stat.considered,
741                100 * (1 - (float)args.stat.completed/args.stat.expected));
742
743         if (verbose)
744                 dump_ioctl_balance_args(&args);
745
746         return 1;
747 }
748
749 static const char balance_cmd_group_info[] =
750 "balance data accross devices, or change block groups using filters";
751
752 const struct cmd_group balance_cmd_group = {
753         balance_cmd_group_usage, balance_cmd_group_info, {
754                 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
755                 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
756                 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
757                 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
758                 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
759                 NULL_CMD_STRUCT
760         }
761 };
762
763 int cmd_balance(int argc, char **argv)
764 {
765         if (argc == 2) {
766                 /* old 'btrfs filesystem balance <path>' syntax */
767                 struct btrfs_ioctl_balance_args args;
768
769                 memset(&args, 0, sizeof(args));
770                 args.flags |= BTRFS_BALANCE_TYPE_MASK;
771
772                 return do_balance(argv[1], &args, 1);
773         }
774
775         return handle_command_group(&balance_cmd_group, argc, argv);
776 }