btrfs-progs: balance: enhance the limit fiter with 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         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                                 if (parse_range_u32(value, &args->limit_min,
310                                                         &args->limit_max)) {
311                                         fprintf(stderr,
312                                                 "Invalid limit argument: %s\n",
313                                                value);
314                                         return 1;
315                                 }
316                                 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT;
317                                 args->flags |= BTRFS_BALANCE_ARGS_LIMIT_RANGE;
318                         } else {
319                                 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT_RANGE;
320                                 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
321                         }
322                 } else {
323                         fprintf(stderr, "Unrecognized balance option '%s'\n",
324                                 this_char);
325                         return 1;
326                 }
327         }
328
329         return 0;
330 }
331
332 static void dump_balance_args(struct btrfs_balance_args *args)
333 {
334         if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
335                 printf("converting, target=%llu, soft is %s",
336                        (unsigned long long)args->target,
337                        (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
338         } else {
339                 printf("balancing");
340         }
341
342         if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
343                 printf(", profiles=%llu", (unsigned long long)args->profiles);
344         if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
345                 printf(", usage=%llu", (unsigned long long)args->usage);
346         if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
347                 printf(", devid=%llu", (unsigned long long)args->devid);
348         if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
349                 printf(", drange=%llu..%llu",
350                        (unsigned long long)args->pstart,
351                        (unsigned long long)args->pend);
352         if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
353                 printf(", vrange=%llu..%llu",
354                        (unsigned long long)args->vstart,
355                        (unsigned long long)args->vend);
356         if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
357                 printf(", limit=%llu", (unsigned long long)args->limit);
358         if (args->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) {
359                 printf(", limit=");
360                 print_range_u32(args->limit_min, args->limit_max);
361         }
362
363         printf("\n");
364 }
365
366 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
367 {
368         printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
369                (unsigned long long)args->flags, (unsigned long long)args->state,
370                (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
371         if (args->flags & BTRFS_BALANCE_DATA) {
372                 printf("  DATA (flags 0x%llx): ",
373                        (unsigned long long)args->data.flags);
374                 dump_balance_args(&args->data);
375         }
376         if (args->flags & BTRFS_BALANCE_METADATA) {
377                 printf("  METADATA (flags 0x%llx): ",
378                        (unsigned long long)args->meta.flags);
379                 dump_balance_args(&args->meta);
380         }
381         if (args->flags & BTRFS_BALANCE_SYSTEM) {
382                 printf("  SYSTEM (flags 0x%llx): ",
383                        (unsigned long long)args->sys.flags);
384                 dump_balance_args(&args->sys);
385         }
386 }
387
388 static int do_balance_v1(int fd)
389 {
390         struct btrfs_ioctl_vol_args args;
391         int ret;
392
393         memset(&args, 0, sizeof(args));
394         ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
395         return ret;
396 }
397
398 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
399                       int nofilters)
400 {
401         int fd;
402         int ret;
403         int e;
404         DIR *dirstream = NULL;
405
406         fd = btrfs_open_dir(path, &dirstream, 1);
407         if (fd < 0)
408                 return 1;
409
410         ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
411         e = errno;
412
413         if (ret < 0) {
414                 /*
415                  * older kernels don't have the new balance ioctl, try the
416                  * old one.  But, the old one doesn't know any filters, so
417                  * don't fall back if they tried to use the fancy new things
418                  */
419                 if (e == ENOTTY && nofilters) {
420                         ret = do_balance_v1(fd);
421                         if (ret == 0)
422                                 goto out;
423                         e = errno;
424                 }
425
426                 if (e == ECANCELED) {
427                         if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
428                                 fprintf(stderr, "balance paused by user\n");
429                         if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
430                                 fprintf(stderr, "balance canceled by user\n");
431                         ret = 0;
432                 } else {
433                         fprintf(stderr, "ERROR: error during balancing '%s' "
434                                 "- %s\n", path, strerror(e));
435                         if (e != EINPROGRESS)
436                                 fprintf(stderr, "There may be more info in "
437                                         "syslog - try dmesg | tail\n");
438                         ret = 1;
439                 }
440         } else {
441                 printf("Done, had to relocate %llu out of %llu chunks\n",
442                        (unsigned long long)args->stat.completed,
443                        (unsigned long long)args->stat.considered);
444                 ret = 0;
445         }
446
447 out:
448         close_file_or_dir(fd, dirstream);
449         return ret;
450 }
451
452 static const char * const cmd_balance_start_usage[] = {
453         "btrfs balance start [options] <path>",
454         "Balance chunks across the devices",
455         "Balance and/or convert (change allocation profile of) chunks that",
456         "passed all filters in a comma-separated list of filters for a",
457         "particular chunk type.  If filter list is not given balance all",
458         "chunks of that type.  In case none of the -d, -m or -s options is",
459         "given balance all chunks in a filesystem.",
460         "",
461         "-d[filters]    act on data chunks",
462         "-m[filters]    act on metadata chunks",
463         "-s[filters]    act on system chunks (only under -f)",
464         "-v             be verbose",
465         "-f             force reducing of metadata integrity",
466         NULL
467 };
468
469 static int cmd_balance_start(int argc, char **argv)
470 {
471         struct btrfs_ioctl_balance_args args;
472         struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
473                                                 &args.meta, NULL };
474         int force = 0;
475         int verbose = 0;
476         int nofilters = 1;
477         int i;
478
479         memset(&args, 0, sizeof(args));
480
481         optind = 1;
482         while (1) {
483                 static const struct option longopts[] = {
484                         { "data", optional_argument, NULL, 'd'},
485                         { "metadata", optional_argument, NULL, 'm' },
486                         { "system", optional_argument, NULL, 's' },
487                         { "force", no_argument, NULL, 'f' },
488                         { "verbose", no_argument, NULL, 'v' },
489                         { NULL, 0, NULL, 0 }
490                 };
491
492                 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
493                 if (opt < 0)
494                         break;
495
496                 switch (opt) {
497                 case 'd':
498                         nofilters = 0;
499                         args.flags |= BTRFS_BALANCE_DATA;
500
501                         if (parse_filters(optarg, &args.data))
502                                 return 1;
503                         break;
504                 case 's':
505                         nofilters = 0;
506                         args.flags |= BTRFS_BALANCE_SYSTEM;
507
508                         if (parse_filters(optarg, &args.sys))
509                                 return 1;
510                         break;
511                 case 'm':
512                         nofilters = 0;
513                         args.flags |= BTRFS_BALANCE_METADATA;
514
515                         if (parse_filters(optarg, &args.meta))
516                                 return 1;
517                         break;
518                 case 'f':
519                         force = 1;
520                         break;
521                 case 'v':
522                         verbose = 1;
523                         break;
524                 default:
525                         usage(cmd_balance_start_usage);
526                 }
527         }
528
529         if (check_argc_exact(argc - optind, 1))
530                 usage(cmd_balance_start_usage);
531
532         /*
533          * allow -s only under --force, otherwise do with system chunks
534          * the same thing we were ordered to do with meta chunks
535          */
536         if (args.flags & BTRFS_BALANCE_SYSTEM) {
537                 if (!force) {
538                         fprintf(stderr,
539 "Refusing to explicitly operate on system chunks.\n"
540 "Pass --force if you really want to do that.\n");
541                         return 1;
542                 }
543         } else if (args.flags & BTRFS_BALANCE_METADATA) {
544                 args.flags |= BTRFS_BALANCE_SYSTEM;
545                 memcpy(&args.sys, &args.meta,
546                         sizeof(struct btrfs_balance_args));
547         }
548
549         if (nofilters) {
550                 /* relocate everything - no filters */
551                 args.flags |= BTRFS_BALANCE_TYPE_MASK;
552         }
553
554         /* drange makes sense only when devid is set */
555         for (i = 0; ptrs[i]; i++) {
556                 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
557                     !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
558                         fprintf(stderr, "drange filter can be used only if "
559                                 "devid filter is used\n");
560                         return 1;
561                 }
562         }
563
564         /* soft makes sense only when convert for corresponding type is set */
565         for (i = 0; ptrs[i]; i++) {
566                 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
567                     !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
568                         fprintf(stderr, "'soft' option can be used only if "
569                                 "changing profiles\n");
570                         return 1;
571                 }
572         }
573
574         if (force)
575                 args.flags |= BTRFS_BALANCE_FORCE;
576         if (verbose)
577                 dump_ioctl_balance_args(&args);
578
579         return do_balance(argv[optind], &args, nofilters);
580 }
581
582 static const char * const cmd_balance_pause_usage[] = {
583         "btrfs balance pause <path>",
584         "Pause running balance",
585         NULL
586 };
587
588 static int cmd_balance_pause(int argc, char **argv)
589 {
590         const char *path;
591         int fd;
592         int ret;
593         int e;
594         DIR *dirstream = NULL;
595
596         if (check_argc_exact(argc, 2))
597                 usage(cmd_balance_pause_usage);
598
599         path = argv[1];
600
601         fd = btrfs_open_dir(path, &dirstream, 1);
602         if (fd < 0)
603                 return 1;
604
605         ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
606         e = errno;
607         close_file_or_dir(fd, dirstream);
608
609         if (ret < 0) {
610                 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
611                         path, (e == ENOTCONN) ? "Not running" : strerror(e));
612                 if (e == ENOTCONN)
613                         return 2;
614                 else
615                         return 1;
616         }
617
618         return 0;
619 }
620
621 static const char * const cmd_balance_cancel_usage[] = {
622         "btrfs balance cancel <path>",
623         "Cancel running or paused balance",
624         NULL
625 };
626
627 static int cmd_balance_cancel(int argc, char **argv)
628 {
629         const char *path;
630         int fd;
631         int ret;
632         int e;
633         DIR *dirstream = NULL;
634
635         if (check_argc_exact(argc, 2))
636                 usage(cmd_balance_cancel_usage);
637
638         path = argv[1];
639
640         fd = btrfs_open_dir(path, &dirstream, 1);
641         if (fd < 0)
642                 return 1;
643
644         ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
645         e = errno;
646         close_file_or_dir(fd, dirstream);
647
648         if (ret < 0) {
649                 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
650                         path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
651                 if (e == ENOTCONN)
652                         return 2;
653                 else
654                         return 1;
655         }
656
657         return 0;
658 }
659
660 static const char * const cmd_balance_resume_usage[] = {
661         "btrfs balance resume <path>",
662         "Resume interrupted balance",
663         NULL
664 };
665
666 static int cmd_balance_resume(int argc, char **argv)
667 {
668         struct btrfs_ioctl_balance_args args;
669         const char *path;
670         DIR *dirstream = NULL;
671         int fd;
672         int ret;
673         int e;
674
675         if (check_argc_exact(argc, 2))
676                 usage(cmd_balance_resume_usage);
677
678         path = argv[1];
679
680         fd = btrfs_open_dir(path, &dirstream, 1);
681         if (fd < 0)
682                 return 1;
683
684         memset(&args, 0, sizeof(args));
685         args.flags |= BTRFS_BALANCE_RESUME;
686
687         ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
688         e = errno;
689         close_file_or_dir(fd, dirstream);
690
691         if (ret < 0) {
692                 if (e == ECANCELED) {
693                         if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
694                                 fprintf(stderr, "balance paused by user\n");
695                         if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
696                                 fprintf(stderr, "balance canceled by user\n");
697                 } else if (e == ENOTCONN || e == EINPROGRESS) {
698                         fprintf(stderr, "ERROR: balance resume on '%s' "
699                                 "failed - %s\n", path,
700                                 (e == ENOTCONN) ? "Not in progress" :
701                                                   "Already running");
702                         if (e == ENOTCONN)
703                                 return 2;
704                         else
705                                 return 1;
706                 } else {
707                         fprintf(stderr,
708 "ERROR: error during balancing '%s' - %s\n"
709 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
710                         return 1;
711                 }
712         } else {
713                 printf("Done, had to relocate %llu out of %llu chunks\n",
714                        (unsigned long long)args.stat.completed,
715                        (unsigned long long)args.stat.considered);
716         }
717
718         return 0;
719 }
720
721 static const char * const cmd_balance_status_usage[] = {
722         "btrfs balance status [-v] <path>",
723         "Show status of running or paused balance",
724         "",
725         "-v     be verbose",
726         NULL
727 };
728
729 /* Checks the status of the balance if any
730  * return codes:
731  *   2 : Error failed to know if there is any pending balance
732  *   1 : Successful to know status of a pending balance
733  *   0 : When there is no pending balance or completed
734  */
735 static int cmd_balance_status(int argc, char **argv)
736 {
737         struct btrfs_ioctl_balance_args args;
738         const char *path;
739         DIR *dirstream = NULL;
740         int fd;
741         int verbose = 0;
742         int ret;
743         int e;
744
745         optind = 1;
746         while (1) {
747                 int opt;
748                 static const struct option longopts[] = {
749                         { "verbose", no_argument, NULL, 'v' },
750                         { NULL, 0, NULL, 0 }
751                 };
752
753                 opt = getopt_long(argc, argv, "v", longopts, NULL);
754                 if (opt < 0)
755                         break;
756
757                 switch (opt) {
758                 case 'v':
759                         verbose = 1;
760                         break;
761                 default:
762                         usage(cmd_balance_status_usage);
763                 }
764         }
765
766         if (check_argc_exact(argc - optind, 1))
767                 usage(cmd_balance_status_usage);
768
769         path = argv[optind];
770
771         fd = btrfs_open_dir(path, &dirstream, 1);
772         if (fd < 0)
773                 return 2;
774
775         ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
776         e = errno;
777         close_file_or_dir(fd, dirstream);
778
779         if (ret < 0) {
780                 if (e == ENOTCONN) {
781                         printf("No balance found on '%s'\n", path);
782                         return 0;
783                 }
784                 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
785                         path, strerror(e));
786                 return 2;
787         }
788
789         if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
790                 printf("Balance on '%s' is running", path);
791                 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
792                         printf(", cancel requested\n");
793                 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
794                         printf(", pause requested\n");
795                 else
796                         printf("\n");
797         } else {
798                 printf("Balance on '%s' is paused\n", path);
799         }
800
801         printf("%llu out of about %llu chunks balanced (%llu considered), "
802                "%3.f%% left\n", (unsigned long long)args.stat.completed,
803                (unsigned long long)args.stat.expected,
804                (unsigned long long)args.stat.considered,
805                100 * (1 - (float)args.stat.completed/args.stat.expected));
806
807         if (verbose)
808                 dump_ioctl_balance_args(&args);
809
810         return 1;
811 }
812
813 static const char balance_cmd_group_info[] =
814 "balance data accross devices, or change block groups using filters";
815
816 const struct cmd_group balance_cmd_group = {
817         balance_cmd_group_usage, balance_cmd_group_info, {
818                 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
819                 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
820                 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
821                 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
822                 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
823                 NULL_CMD_STRUCT
824         }
825 };
826
827 int cmd_balance(int argc, char **argv)
828 {
829         if (argc == 2) {
830                 /* old 'btrfs filesystem balance <path>' syntax */
831                 struct btrfs_ioctl_balance_args args;
832
833                 memset(&args, 0, sizeof(args));
834                 args.flags |= BTRFS_BALANCE_TYPE_MASK;
835
836                 return do_balance(argv[1], &args, 1);
837         }
838
839         return handle_command_group(&balance_cmd_group, argc, argv);
840 }