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