btrfs-progs: do not modify the string in parse_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, "Range %llu..%llu doesn't make "
133                         "sense\n", (unsigned long long)*start,
134                         (unsigned long long)*end);
135                 return 1;
136         }
137
138         if (skipped <= 1)
139                 return 0;
140
141         return 1;
142 }
143
144 static int parse_filters(char *filters, struct btrfs_balance_args *args)
145 {
146         char *this_char;
147         char *value;
148         char *save_ptr = NULL; /* Satisfy static checkers */
149
150         if (!filters)
151                 return 0;
152
153         for (this_char = strtok_r(filters, ",", &save_ptr);
154              this_char != NULL;
155              this_char = strtok_r(NULL, ",", &save_ptr)) {
156                 if ((value = strchr(this_char, '=')) != NULL)
157                         *value++ = 0;
158                 if (!strcmp(this_char, "profiles")) {
159                         if (!value || !*value) {
160                                 fprintf(stderr, "the profiles filter requires "
161                                        "an argument\n");
162                                 return 1;
163                         }
164                         if (parse_profiles(value, &args->profiles)) {
165                                 fprintf(stderr, "Invalid profiles argument\n");
166                                 return 1;
167                         }
168                         args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
169                 } else if (!strcmp(this_char, "usage")) {
170                         if (!value || !*value) {
171                                 fprintf(stderr, "the usage filter requires "
172                                        "an argument\n");
173                                 return 1;
174                         }
175                         if (parse_u64(value, &args->usage) ||
176                             args->usage > 100) {
177                                 fprintf(stderr, "Invalid usage argument: %s\n",
178                                        value);
179                                 return 1;
180                         }
181                         args->flags |= BTRFS_BALANCE_ARGS_USAGE;
182                 } else if (!strcmp(this_char, "devid")) {
183                         if (!value || !*value) {
184                                 fprintf(stderr, "the devid filter requires "
185                                        "an argument\n");
186                                 return 1;
187                         }
188                         if (parse_u64(value, &args->devid) ||
189                             args->devid == 0) {
190                                 fprintf(stderr, "Invalid devid argument: %s\n",
191                                        value);
192                                 return 1;
193                         }
194                         args->flags |= BTRFS_BALANCE_ARGS_DEVID;
195                 } else if (!strcmp(this_char, "drange")) {
196                         if (!value || !*value) {
197                                 fprintf(stderr, "the drange filter requires "
198                                        "an argument\n");
199                                 return 1;
200                         }
201                         if (parse_range(value, &args->pstart, &args->pend)) {
202                                 fprintf(stderr, "Invalid drange argument\n");
203                                 return 1;
204                         }
205                         args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
206                 } else if (!strcmp(this_char, "vrange")) {
207                         if (!value || !*value) {
208                                 fprintf(stderr, "the vrange filter requires "
209                                        "an argument\n");
210                                 return 1;
211                         }
212                         if (parse_range(value, &args->vstart, &args->vend)) {
213                                 fprintf(stderr, "Invalid vrange argument\n");
214                                 return 1;
215                         }
216                         args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
217                 } else if (!strcmp(this_char, "convert")) {
218                         if (!value || !*value) {
219                                 fprintf(stderr, "the convert option requires "
220                                        "an argument\n");
221                                 return 1;
222                         }
223                         if (parse_one_profile(value, &args->target)) {
224                                 fprintf(stderr, "Invalid convert argument\n");
225                                 return 1;
226                         }
227                         args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
228                 } else if (!strcmp(this_char, "soft")) {
229                         args->flags |= BTRFS_BALANCE_ARGS_SOFT;
230                 } else if (!strcmp(this_char, "limit")) {
231                         if (!value || !*value) {
232                                 fprintf(stderr,
233                                         "the limit filter requires an argument\n");
234                                 return 1;
235                         }
236                         if (parse_u64(value, &args->limit)) {
237                                 fprintf(stderr, "Invalid limit argument: %s\n",
238                                        value);
239                                 return 1;
240                         }
241                         args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
242                 } else {
243                         fprintf(stderr, "Unrecognized balance option '%s'\n",
244                                 this_char);
245                         return 1;
246                 }
247         }
248
249         return 0;
250 }
251
252 static void dump_balance_args(struct btrfs_balance_args *args)
253 {
254         if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
255                 printf("converting, target=%llu, soft is %s",
256                        (unsigned long long)args->target,
257                        (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
258         } else {
259                 printf("balancing");
260         }
261
262         if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
263                 printf(", profiles=%llu", (unsigned long long)args->profiles);
264         if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
265                 printf(", usage=%llu", (unsigned long long)args->usage);
266         if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
267                 printf(", devid=%llu", (unsigned long long)args->devid);
268         if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
269                 printf(", drange=%llu..%llu",
270                        (unsigned long long)args->pstart,
271                        (unsigned long long)args->pend);
272         if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
273                 printf(", vrange=%llu..%llu",
274                        (unsigned long long)args->vstart,
275                        (unsigned long long)args->vend);
276         if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
277                 printf(", limit=%llu", (unsigned long long)args->limit);
278
279         printf("\n");
280 }
281
282 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
283 {
284         printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
285                (unsigned long long)args->flags, (unsigned long long)args->state,
286                (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
287         if (args->flags & BTRFS_BALANCE_DATA) {
288                 printf("  DATA (flags 0x%llx): ",
289                        (unsigned long long)args->data.flags);
290                 dump_balance_args(&args->data);
291         }
292         if (args->flags & BTRFS_BALANCE_METADATA) {
293                 printf("  METADATA (flags 0x%llx): ",
294                        (unsigned long long)args->meta.flags);
295                 dump_balance_args(&args->meta);
296         }
297         if (args->flags & BTRFS_BALANCE_SYSTEM) {
298                 printf("  SYSTEM (flags 0x%llx): ",
299                        (unsigned long long)args->sys.flags);
300                 dump_balance_args(&args->sys);
301         }
302 }
303
304 static int do_balance_v1(int fd)
305 {
306         struct btrfs_ioctl_vol_args args;
307         int ret;
308
309         memset(&args, 0, sizeof(args));
310         ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
311         return ret;
312 }
313
314 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
315                       int nofilters)
316 {
317         int fd;
318         int ret;
319         int e;
320         DIR *dirstream = NULL;
321
322         fd = btrfs_open_dir(path, &dirstream, 1);
323         if (fd < 0)
324                 return 1;
325
326         ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
327         e = errno;
328
329         if (ret < 0) {
330                 /*
331                  * older kernels don't have the new balance ioctl, try the
332                  * old one.  But, the old one doesn't know any filters, so
333                  * don't fall back if they tried to use the fancy new things
334                  */
335                 if (e == ENOTTY && nofilters) {
336                         ret = do_balance_v1(fd);
337                         if (ret == 0)
338                                 goto out;
339                         e = errno;
340                 }
341
342                 if (e == ECANCELED) {
343                         if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
344                                 fprintf(stderr, "balance paused by user\n");
345                         if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
346                                 fprintf(stderr, "balance canceled by user\n");
347                         ret = 0;
348                 } else {
349                         fprintf(stderr, "ERROR: error during balancing '%s' "
350                                 "- %s\n", path, strerror(e));
351                         if (e != EINPROGRESS)
352                                 fprintf(stderr, "There may be more info in "
353                                         "syslog - try dmesg | tail\n");
354                         ret = 1;
355                 }
356         } else {
357                 printf("Done, had to relocate %llu out of %llu chunks\n",
358                        (unsigned long long)args->stat.completed,
359                        (unsigned long long)args->stat.considered);
360                 ret = 0;
361         }
362
363 out:
364         close_file_or_dir(fd, dirstream);
365         return ret;
366 }
367
368 static const char * const cmd_balance_start_usage[] = {
369         "btrfs balance start [options] <path>",
370         "Balance chunks across the devices",
371         "Balance and/or convert (change allocation profile of) chunks that",
372         "passed all filters in a comma-separated list of filters for a",
373         "particular chunk type.  If filter list is not given balance all",
374         "chunks of that type.  In case none of the -d, -m or -s options is",
375         "given balance all chunks in a filesystem.",
376         "",
377         "-d[filters]    act on data chunks",
378         "-m[filters]    act on metadata chunks",
379         "-s[filters]    act on system chunks (only under -f)",
380         "-v             be verbose",
381         "-f             force reducing of metadata integrity",
382         NULL
383 };
384
385 static int cmd_balance_start(int argc, char **argv)
386 {
387         struct btrfs_ioctl_balance_args args;
388         struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
389                                                 &args.meta, NULL };
390         int force = 0;
391         int verbose = 0;
392         int nofilters = 1;
393         int i;
394
395         memset(&args, 0, sizeof(args));
396
397         optind = 1;
398         while (1) {
399                 static const struct option longopts[] = {
400                         { "data", optional_argument, NULL, 'd'},
401                         { "metadata", optional_argument, NULL, 'm' },
402                         { "system", optional_argument, NULL, 's' },
403                         { "force", no_argument, NULL, 'f' },
404                         { "verbose", no_argument, NULL, 'v' },
405                         { NULL, 0, NULL, 0 }
406                 };
407
408                 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
409                 if (opt < 0)
410                         break;
411
412                 switch (opt) {
413                 case 'd':
414                         nofilters = 0;
415                         args.flags |= BTRFS_BALANCE_DATA;
416
417                         if (parse_filters(optarg, &args.data))
418                                 return 1;
419                         break;
420                 case 's':
421                         nofilters = 0;
422                         args.flags |= BTRFS_BALANCE_SYSTEM;
423
424                         if (parse_filters(optarg, &args.sys))
425                                 return 1;
426                         break;
427                 case 'm':
428                         nofilters = 0;
429                         args.flags |= BTRFS_BALANCE_METADATA;
430
431                         if (parse_filters(optarg, &args.meta))
432                                 return 1;
433                         break;
434                 case 'f':
435                         force = 1;
436                         break;
437                 case 'v':
438                         verbose = 1;
439                         break;
440                 default:
441                         usage(cmd_balance_start_usage);
442                 }
443         }
444
445         if (check_argc_exact(argc - optind, 1))
446                 usage(cmd_balance_start_usage);
447
448         /*
449          * allow -s only under --force, otherwise do with system chunks
450          * the same thing we were ordered to do with meta chunks
451          */
452         if (args.flags & BTRFS_BALANCE_SYSTEM) {
453                 if (!force) {
454                         fprintf(stderr,
455 "Refusing to explicitly operate on system chunks.\n"
456 "Pass --force if you really want to do that.\n");
457                         return 1;
458                 }
459         } else if (args.flags & BTRFS_BALANCE_METADATA) {
460                 args.flags |= BTRFS_BALANCE_SYSTEM;
461                 memcpy(&args.sys, &args.meta,
462                         sizeof(struct btrfs_balance_args));
463         }
464
465         if (nofilters) {
466                 /* relocate everything - no filters */
467                 args.flags |= BTRFS_BALANCE_TYPE_MASK;
468         }
469
470         /* drange makes sense only when devid is set */
471         for (i = 0; ptrs[i]; i++) {
472                 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
473                     !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
474                         fprintf(stderr, "drange filter can be used only if "
475                                 "devid filter is used\n");
476                         return 1;
477                 }
478         }
479
480         /* soft makes sense only when convert for corresponding type is set */
481         for (i = 0; ptrs[i]; i++) {
482                 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
483                     !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
484                         fprintf(stderr, "'soft' option can be used only if "
485                                 "changing profiles\n");
486                         return 1;
487                 }
488         }
489
490         if (force)
491                 args.flags |= BTRFS_BALANCE_FORCE;
492         if (verbose)
493                 dump_ioctl_balance_args(&args);
494
495         return do_balance(argv[optind], &args, nofilters);
496 }
497
498 static const char * const cmd_balance_pause_usage[] = {
499         "btrfs balance pause <path>",
500         "Pause running balance",
501         NULL
502 };
503
504 static int cmd_balance_pause(int argc, char **argv)
505 {
506         const char *path;
507         int fd;
508         int ret;
509         int e;
510         DIR *dirstream = NULL;
511
512         if (check_argc_exact(argc, 2))
513                 usage(cmd_balance_pause_usage);
514
515         path = argv[1];
516
517         fd = btrfs_open_dir(path, &dirstream, 1);
518         if (fd < 0)
519                 return 1;
520
521         ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
522         e = errno;
523         close_file_or_dir(fd, dirstream);
524
525         if (ret < 0) {
526                 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
527                         path, (e == ENOTCONN) ? "Not running" : strerror(e));
528                 if (e == ENOTCONN)
529                         return 2;
530                 else
531                         return 1;
532         }
533
534         return 0;
535 }
536
537 static const char * const cmd_balance_cancel_usage[] = {
538         "btrfs balance cancel <path>",
539         "Cancel running or paused balance",
540         NULL
541 };
542
543 static int cmd_balance_cancel(int argc, char **argv)
544 {
545         const char *path;
546         int fd;
547         int ret;
548         int e;
549         DIR *dirstream = NULL;
550
551         if (check_argc_exact(argc, 2))
552                 usage(cmd_balance_cancel_usage);
553
554         path = argv[1];
555
556         fd = btrfs_open_dir(path, &dirstream, 1);
557         if (fd < 0)
558                 return 1;
559
560         ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
561         e = errno;
562         close_file_or_dir(fd, dirstream);
563
564         if (ret < 0) {
565                 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
566                         path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
567                 if (e == ENOTCONN)
568                         return 2;
569                 else
570                         return 1;
571         }
572
573         return 0;
574 }
575
576 static const char * const cmd_balance_resume_usage[] = {
577         "btrfs balance resume <path>",
578         "Resume interrupted balance",
579         NULL
580 };
581
582 static int cmd_balance_resume(int argc, char **argv)
583 {
584         struct btrfs_ioctl_balance_args args;
585         const char *path;
586         DIR *dirstream = NULL;
587         int fd;
588         int ret;
589         int e;
590
591         if (check_argc_exact(argc, 2))
592                 usage(cmd_balance_resume_usage);
593
594         path = argv[1];
595
596         fd = btrfs_open_dir(path, &dirstream, 1);
597         if (fd < 0)
598                 return 1;
599
600         memset(&args, 0, sizeof(args));
601         args.flags |= BTRFS_BALANCE_RESUME;
602
603         ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
604         e = errno;
605         close_file_or_dir(fd, dirstream);
606
607         if (ret < 0) {
608                 if (e == ECANCELED) {
609                         if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
610                                 fprintf(stderr, "balance paused by user\n");
611                         if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
612                                 fprintf(stderr, "balance canceled by user\n");
613                 } else if (e == ENOTCONN || e == EINPROGRESS) {
614                         fprintf(stderr, "ERROR: balance resume on '%s' "
615                                 "failed - %s\n", path,
616                                 (e == ENOTCONN) ? "Not in progress" :
617                                                   "Already running");
618                         if (e == ENOTCONN)
619                                 return 2;
620                         else
621                                 return 1;
622                 } else {
623                         fprintf(stderr,
624 "ERROR: error during balancing '%s' - %s\n"
625 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
626                         return 1;
627                 }
628         } else {
629                 printf("Done, had to relocate %llu out of %llu chunks\n",
630                        (unsigned long long)args.stat.completed,
631                        (unsigned long long)args.stat.considered);
632         }
633
634         return 0;
635 }
636
637 static const char * const cmd_balance_status_usage[] = {
638         "btrfs balance status [-v] <path>",
639         "Show status of running or paused balance",
640         "",
641         "-v     be verbose",
642         NULL
643 };
644
645 /* Checks the status of the balance if any
646  * return codes:
647  *   2 : Error failed to know if there is any pending balance
648  *   1 : Successful to know status of a pending balance
649  *   0 : When there is no pending balance or completed
650  */
651 static int cmd_balance_status(int argc, char **argv)
652 {
653         struct btrfs_ioctl_balance_args args;
654         const char *path;
655         DIR *dirstream = NULL;
656         int fd;
657         int verbose = 0;
658         int ret;
659         int e;
660
661         optind = 1;
662         while (1) {
663                 int opt;
664                 static const struct option longopts[] = {
665                         { "verbose", no_argument, NULL, 'v' },
666                         { NULL, 0, NULL, 0 }
667                 };
668
669                 opt = getopt_long(argc, argv, "v", longopts, NULL);
670                 if (opt < 0)
671                         break;
672
673                 switch (opt) {
674                 case 'v':
675                         verbose = 1;
676                         break;
677                 default:
678                         usage(cmd_balance_status_usage);
679                 }
680         }
681
682         if (check_argc_exact(argc - optind, 1))
683                 usage(cmd_balance_status_usage);
684
685         path = argv[optind];
686
687         fd = btrfs_open_dir(path, &dirstream, 1);
688         if (fd < 0)
689                 return 2;
690
691         ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
692         e = errno;
693         close_file_or_dir(fd, dirstream);
694
695         if (ret < 0) {
696                 if (e == ENOTCONN) {
697                         printf("No balance found on '%s'\n", path);
698                         return 0;
699                 }
700                 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
701                         path, strerror(e));
702                 return 2;
703         }
704
705         if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
706                 printf("Balance on '%s' is running", path);
707                 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
708                         printf(", cancel requested\n");
709                 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
710                         printf(", pause requested\n");
711                 else
712                         printf("\n");
713         } else {
714                 printf("Balance on '%s' is paused\n", path);
715         }
716
717         printf("%llu out of about %llu chunks balanced (%llu considered), "
718                "%3.f%% left\n", (unsigned long long)args.stat.completed,
719                (unsigned long long)args.stat.expected,
720                (unsigned long long)args.stat.considered,
721                100 * (1 - (float)args.stat.completed/args.stat.expected));
722
723         if (verbose)
724                 dump_ioctl_balance_args(&args);
725
726         return 1;
727 }
728
729 static const char balance_cmd_group_info[] =
730 "balance data accross devices, or change block groups using filters";
731
732 const struct cmd_group balance_cmd_group = {
733         balance_cmd_group_usage, balance_cmd_group_info, {
734                 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
735                 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
736                 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
737                 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
738                 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
739                 NULL_CMD_STRUCT
740         }
741 };
742
743 int cmd_balance(int argc, char **argv)
744 {
745         if (argc == 2) {
746                 /* old 'btrfs filesystem balance <path>' syntax */
747                 struct btrfs_ioctl_balance_args args;
748
749                 memset(&args, 0, sizeof(args));
750                 args.flags |= BTRFS_BALANCE_TYPE_MASK;
751
752                 return do_balance(argv[1], &args, 1);
753         }
754
755         return handle_command_group(&balance_cmd_group, argc, argv);
756 }