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