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