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