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