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