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