btrfs-progs: check: switch to iterating over the backref_tree
[btrfs-progs-unstable/devel.git] / cmds-balance.c
blob708bbf457889046c50b8c572044497a6d0a3b601
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.
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.
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.
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>
25 #include "kerncompat.h"
26 #include "ctree.h"
27 #include "ioctl.h"
28 #include "volumes.h"
30 #include "commands.h"
31 #include "utils.h"
33 static const char * const balance_cmd_group_usage[] = {
34 "btrfs balance <command> [options] <path>",
35 "btrfs balance <path>",
36 NULL
39 static int parse_one_profile(const char *profile, u64 *flags)
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 error("unknown profile: %s", profile);
57 return 1;
60 return 0;
63 static int parse_profiles(char *profiles, u64 *flags)
65 char *this_char;
66 char *save_ptr = NULL; /* Satisfy static checkers */
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;
75 return 0;
78 static int parse_u64(const char *str, u64 *result)
80 char *endptr;
81 u64 val;
83 val = strtoull(str, &endptr, 10);
84 if (*endptr)
85 return 1;
87 *result = val;
88 return 0;
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
98 * Returned values are u64, value validation and interpretation should be done
99 * by the caller.
101 static int parse_range(const char *range, u64 *start, u64 *end)
103 char *dots;
104 char *endptr;
105 const char *rest;
106 int skipped = 0;
108 dots = strstr(range, "..");
109 if (!dots)
110 return 1;
112 rest = dots + 2;
114 if (!*rest) {
115 *end = (u64)-1;
116 skipped++;
117 } else {
118 *end = strtoull(rest, &endptr, 10);
119 if (*endptr)
120 return 1;
122 if (dots == range) {
123 *start = 0;
124 skipped++;
125 } else {
126 *start = strtoull(range, &endptr, 10);
127 if (*endptr != 0 && *endptr != '.')
128 return 1;
131 if (*start > *end) {
132 error("range %llu..%llu doesn't make sense",
133 (unsigned long long)*start,
134 (unsigned long long)*end);
135 return 1;
138 if (skipped <= 1)
139 return 0;
141 return 1;
145 * Parse range and check if start < end
147 static int parse_range_strict(const char *range, u64 *start, u64 *end)
149 if (parse_range(range, start, end) == 0) {
150 if (*start >= *end) {
151 error("range %llu..%llu not allowed",
152 (unsigned long long)*start,
153 (unsigned long long)*end);
154 return 1;
156 return 0;
159 return 1;
163 * Convert 64bit range to 32bit with boundary checks
165 static int range_to_u32(u64 start, u64 end, u32 *start32, u32 *end32)
167 if (start > (u32)-1)
168 return 1;
170 if (end != (u64)-1 && end > (u32)-1)
171 return 1;
173 *start32 = (u32)start;
174 *end32 = (u32)end;
176 return 0;
179 __attribute__ ((unused))
180 static int parse_range_u32(const char *range, u32 *start, u32 *end)
182 u64 tmp_start;
183 u64 tmp_end;
185 if (parse_range(range, &tmp_start, &tmp_end))
186 return 1;
188 if (range_to_u32(tmp_start, tmp_end, start, end))
189 return 1;
191 return 0;
194 __attribute__ ((unused))
195 static void print_range(u64 start, u64 end)
197 if (start)
198 printf("%llu", (unsigned long long)start);
199 printf("..");
200 if (end != (u64)-1)
201 printf("%llu", (unsigned long long)end);
204 __attribute__ ((unused))
205 static void print_range_u32(u32 start, u32 end)
207 if (start)
208 printf("%u", start);
209 printf("..");
210 if (end != (u32)-1)
211 printf("%u", end);
214 static int parse_filters(char *filters, struct btrfs_balance_args *args)
216 char *this_char;
217 char *value;
218 char *save_ptr = NULL; /* Satisfy static checkers */
220 if (!filters)
221 return 0;
223 for (this_char = strtok_r(filters, ",", &save_ptr);
224 this_char != NULL;
225 this_char = strtok_r(NULL, ",", &save_ptr)) {
226 if ((value = strchr(this_char, '=')) != NULL)
227 *value++ = 0;
228 if (!strcmp(this_char, "profiles")) {
229 if (!value || !*value) {
230 error("the profiles filter requires an argument");
231 return 1;
233 if (parse_profiles(value, &args->profiles)) {
234 error("invalid profiles argument");
235 return 1;
237 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
238 } else if (!strcmp(this_char, "usage")) {
239 if (!value || !*value) {
240 error("the usage filter requires an argument");
241 return 1;
243 if (parse_u64(value, &args->usage)) {
244 if (parse_range_u32(value, &args->usage_min,
245 &args->usage_max)) {
246 error("invalid usage argument: %s",
247 value);
248 return 1;
250 if (args->usage_max > 100) {
251 error("invalid usage argument: %s",
252 value);
254 args->flags &= ~BTRFS_BALANCE_ARGS_USAGE;
255 args->flags |= BTRFS_BALANCE_ARGS_USAGE_RANGE;
256 } else {
257 if (args->usage > 100) {
258 error("invalid usage argument: %s",
259 value);
260 return 1;
262 args->flags &= ~BTRFS_BALANCE_ARGS_USAGE_RANGE;
263 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
265 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
266 } else if (!strcmp(this_char, "devid")) {
267 if (!value || !*value) {
268 error("the devid filter requires an argument");
269 return 1;
271 if (parse_u64(value, &args->devid) || args->devid == 0) {
272 error("invalid devid argument: %s", value);
273 return 1;
275 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
276 } else if (!strcmp(this_char, "drange")) {
277 if (!value || !*value) {
278 error("the drange filter requires an argument");
279 return 1;
281 if (parse_range_strict(value, &args->pstart, &args->pend)) {
282 error("invalid drange argument");
283 return 1;
285 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
286 } else if (!strcmp(this_char, "vrange")) {
287 if (!value || !*value) {
288 error("the vrange filter requires an argument");
289 return 1;
291 if (parse_range_strict(value, &args->vstart, &args->vend)) {
292 error("invalid vrange argument");
293 return 1;
295 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
296 } else if (!strcmp(this_char, "convert")) {
297 if (!value || !*value) {
298 error("the convert option requires an argument");
299 return 1;
301 if (parse_one_profile(value, &args->target)) {
302 error("invalid convert argument");
303 return 1;
305 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
306 } else if (!strcmp(this_char, "soft")) {
307 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
308 } else if (!strcmp(this_char, "limit")) {
309 if (!value || !*value) {
310 error("the limit filter requires an argument");
311 return 1;
313 if (parse_u64(value, &args->limit)) {
314 if (parse_range_u32(value, &args->limit_min,
315 &args->limit_max)) {
316 error("Invalid limit argument: %s",
317 value);
318 return 1;
320 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT;
321 args->flags |= BTRFS_BALANCE_ARGS_LIMIT_RANGE;
322 } else {
323 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT_RANGE;
324 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
326 } else if (!strcmp(this_char, "stripes")) {
327 if (!value || !*value) {
328 error("the stripes filter requires an argument");
329 return 1;
331 if (parse_range_u32(value, &args->stripes_min,
332 &args->stripes_max)) {
333 error("invalid stripes argument");
334 return 1;
336 args->flags |= BTRFS_BALANCE_ARGS_STRIPES_RANGE;
337 } else {
338 error("unrecognized balance option: %s", this_char);
339 return 1;
343 return 0;
346 static void dump_balance_args(struct btrfs_balance_args *args)
348 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
349 printf("converting, target=%llu, soft is %s",
350 (unsigned long long)args->target,
351 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
352 } else {
353 printf("balancing");
356 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
357 printf(", profiles=%llu", (unsigned long long)args->profiles);
358 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
359 printf(", usage=%llu", (unsigned long long)args->usage);
360 if (args->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) {
361 printf(", usage=");
362 print_range_u32(args->usage_min, args->usage_max);
364 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
365 printf(", devid=%llu", (unsigned long long)args->devid);
366 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
367 printf(", drange=%llu..%llu",
368 (unsigned long long)args->pstart,
369 (unsigned long long)args->pend);
370 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
371 printf(", vrange=%llu..%llu",
372 (unsigned long long)args->vstart,
373 (unsigned long long)args->vend);
374 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
375 printf(", limit=%llu", (unsigned long long)args->limit);
376 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) {
377 printf(", limit=");
378 print_range_u32(args->limit_min, args->limit_max);
380 if (args->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) {
381 printf(", stripes=");
382 print_range_u32(args->stripes_min, args->stripes_max);
385 printf("\n");
388 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
390 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
391 (unsigned long long)args->flags, (unsigned long long)args->state,
392 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
393 if (args->flags & BTRFS_BALANCE_DATA) {
394 printf(" DATA (flags 0x%llx): ",
395 (unsigned long long)args->data.flags);
396 dump_balance_args(&args->data);
398 if (args->flags & BTRFS_BALANCE_METADATA) {
399 printf(" METADATA (flags 0x%llx): ",
400 (unsigned long long)args->meta.flags);
401 dump_balance_args(&args->meta);
403 if (args->flags & BTRFS_BALANCE_SYSTEM) {
404 printf(" SYSTEM (flags 0x%llx): ",
405 (unsigned long long)args->sys.flags);
406 dump_balance_args(&args->sys);
410 static int do_balance_v1(int fd)
412 struct btrfs_ioctl_vol_args args;
413 int ret;
415 memset(&args, 0, sizeof(args));
416 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
417 return ret;
420 enum {
421 BALANCE_START_FILTERS = 1 << 0,
422 BALANCE_START_NOWARN = 1 << 1
425 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
426 unsigned flags)
428 int fd;
429 int ret;
430 int e;
431 DIR *dirstream = NULL;
433 fd = btrfs_open_dir(path, &dirstream, 1);
434 if (fd < 0)
435 return 1;
437 if (!(flags & BALANCE_START_FILTERS) && !(flags & BALANCE_START_NOWARN)) {
438 int delay = 10;
440 printf("WARNING:\n\n");
441 printf("\tFull balance without filters requested. This operation is very\n");
442 printf("\tintense and takes potentially very long. It is recommended to\n");
443 printf("\tuse the balance filters to narrow down the balanced data.\n");
444 printf("\tUse 'btrfs balance start --full-balance' option to skip this\n");
445 printf("\twarning. The operation will start in %d seconds.\n", delay);
446 printf("\tUse Ctrl-C to stop it.\n");
447 while (delay) {
448 printf("%2d", delay--);
449 fflush(stdout);
450 sleep(1);
452 printf("\nStarting balance without any filters.\n");
455 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
456 e = errno;
458 if (ret < 0) {
460 * older kernels don't have the new balance ioctl, try the
461 * old one. But, the old one doesn't know any filters, so
462 * don't fall back if they tried to use the fancy new things
464 if (e == ENOTTY && !(flags & BALANCE_START_FILTERS)) {
465 ret = do_balance_v1(fd);
466 if (ret == 0)
467 goto out;
468 e = errno;
471 if (e == ECANCELED) {
472 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
473 fprintf(stderr, "balance paused by user\n");
474 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
475 fprintf(stderr, "balance canceled by user\n");
476 ret = 0;
477 } else {
478 error("error during balancing '%s': %s", path, strerror(e));
479 if (e != EINPROGRESS)
480 fprintf(stderr,
481 "There may be more info in syslog - try dmesg | tail\n");
482 ret = 1;
484 } else {
485 printf("Done, had to relocate %llu out of %llu chunks\n",
486 (unsigned long long)args->stat.completed,
487 (unsigned long long)args->stat.considered);
488 ret = 0;
491 out:
492 close_file_or_dir(fd, dirstream);
493 return ret;
496 static const char * const cmd_balance_start_usage[] = {
497 "btrfs balance start [options] <path>",
498 "Balance chunks across the devices",
499 "Balance and/or convert (change allocation profile of) chunks that",
500 "passed all filters in a comma-separated list of filters for a",
501 "particular chunk type. If filter list is not given balance all",
502 "chunks of that type. In case none of the -d, -m or -s options is",
503 "given balance all chunks in a filesystem. This is potentially",
504 "long operation and the user is warned before this start, with",
505 "a delay to stop it.",
507 "-d[filters] act on data chunks",
508 "-m[filters] act on metadata chunks",
509 "-s[filters] act on system chunks (only under -f)",
510 "-v be verbose",
511 "-f force reducing of metadata integrity",
512 "--full-balance do not print warning and do not delay start",
513 NULL
516 static int cmd_balance_start(int argc, char **argv)
518 struct btrfs_ioctl_balance_args args;
519 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
520 &args.meta, NULL };
521 int force = 0;
522 int verbose = 0;
523 unsigned start_flags = 0;
524 int i;
526 memset(&args, 0, sizeof(args));
528 optind = 1;
529 while (1) {
530 enum { GETOPT_VAL_FULL_BALANCE = 256 };
531 static const struct option longopts[] = {
532 { "data", optional_argument, NULL, 'd'},
533 { "metadata", optional_argument, NULL, 'm' },
534 { "system", optional_argument, NULL, 's' },
535 { "force", no_argument, NULL, 'f' },
536 { "verbose", no_argument, NULL, 'v' },
537 { "full-balance", no_argument, NULL,
538 GETOPT_VAL_FULL_BALANCE },
539 { NULL, 0, NULL, 0 }
542 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
543 if (opt < 0)
544 break;
546 switch (opt) {
547 case 'd':
548 start_flags |= BALANCE_START_FILTERS;
549 args.flags |= BTRFS_BALANCE_DATA;
551 if (parse_filters(optarg, &args.data))
552 return 1;
553 break;
554 case 's':
555 start_flags |= BALANCE_START_FILTERS;
556 args.flags |= BTRFS_BALANCE_SYSTEM;
558 if (parse_filters(optarg, &args.sys))
559 return 1;
560 break;
561 case 'm':
562 start_flags |= BALANCE_START_FILTERS;
563 args.flags |= BTRFS_BALANCE_METADATA;
565 if (parse_filters(optarg, &args.meta))
566 return 1;
567 break;
568 case 'f':
569 force = 1;
570 break;
571 case 'v':
572 verbose = 1;
573 break;
574 case GETOPT_VAL_FULL_BALANCE:
575 start_flags |= BALANCE_START_NOWARN;
576 break;
577 default:
578 usage(cmd_balance_start_usage);
582 if (check_argc_exact(argc - optind, 1))
583 usage(cmd_balance_start_usage);
586 * allow -s only under --force, otherwise do with system chunks
587 * the same thing we were ordered to do with meta chunks
589 if (args.flags & BTRFS_BALANCE_SYSTEM) {
590 if (!force) {
591 error(
592 "Refusing to explicitly operate on system chunks.\n"
593 "Pass --force if you really want to do that.");
594 return 1;
596 } else if (args.flags & BTRFS_BALANCE_METADATA) {
597 args.flags |= BTRFS_BALANCE_SYSTEM;
598 memcpy(&args.sys, &args.meta,
599 sizeof(struct btrfs_balance_args));
602 if (!(start_flags & BALANCE_START_FILTERS)) {
603 /* relocate everything - no filters */
604 args.flags |= BTRFS_BALANCE_TYPE_MASK;
607 /* drange makes sense only when devid is set */
608 for (i = 0; ptrs[i]; i++) {
609 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
610 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
611 error("drange filter must be used with devid filter");
612 return 1;
616 /* soft makes sense only when convert for corresponding type is set */
617 for (i = 0; ptrs[i]; i++) {
618 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
619 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
620 error("'soft' option can be used only when converting profiles");
621 return 1;
625 if (force)
626 args.flags |= BTRFS_BALANCE_FORCE;
627 if (verbose)
628 dump_ioctl_balance_args(&args);
630 return do_balance(argv[optind], &args, start_flags);
633 static const char * const cmd_balance_pause_usage[] = {
634 "btrfs balance pause <path>",
635 "Pause running balance",
636 NULL
639 static int cmd_balance_pause(int argc, char **argv)
641 const char *path;
642 int fd;
643 int ret;
644 int e;
645 DIR *dirstream = NULL;
647 clean_args_no_options(argc, argv, cmd_balance_pause_usage);
649 if (check_argc_exact(argc - optind, 1))
650 usage(cmd_balance_pause_usage);
652 path = argv[optind];
654 fd = btrfs_open_dir(path, &dirstream, 1);
655 if (fd < 0)
656 return 1;
658 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
659 e = errno;
660 close_file_or_dir(fd, dirstream);
662 if (ret < 0) {
663 error("balance pause on '%s' failed: %s", path,
664 (e == ENOTCONN) ? "Not running" : strerror(e));
665 if (e == ENOTCONN)
666 return 2;
667 else
668 return 1;
671 return 0;
674 static const char * const cmd_balance_cancel_usage[] = {
675 "btrfs balance cancel <path>",
676 "Cancel running or paused balance",
677 NULL
680 static int cmd_balance_cancel(int argc, char **argv)
682 const char *path;
683 int fd;
684 int ret;
685 int e;
686 DIR *dirstream = NULL;
688 clean_args_no_options(argc, argv, cmd_balance_cancel_usage);
690 if (check_argc_exact(argc - optind, 1))
691 usage(cmd_balance_cancel_usage);
693 path = argv[optind];
695 fd = btrfs_open_dir(path, &dirstream, 1);
696 if (fd < 0)
697 return 1;
699 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
700 e = errno;
701 close_file_or_dir(fd, dirstream);
703 if (ret < 0) {
704 error("balance cancel on '%s' failed: %s", path,
705 (e == ENOTCONN) ? "Not in progress" : strerror(e));
706 if (e == ENOTCONN)
707 return 2;
708 else
709 return 1;
712 return 0;
715 static const char * const cmd_balance_resume_usage[] = {
716 "btrfs balance resume <path>",
717 "Resume interrupted balance",
718 NULL
721 static int cmd_balance_resume(int argc, char **argv)
723 struct btrfs_ioctl_balance_args args;
724 const char *path;
725 DIR *dirstream = NULL;
726 int fd;
727 int ret;
728 int e;
730 clean_args_no_options(argc, argv, cmd_balance_resume_usage);
732 if (check_argc_exact(argc - optind, 1))
733 usage(cmd_balance_resume_usage);
735 path = argv[optind];
737 fd = btrfs_open_dir(path, &dirstream, 1);
738 if (fd < 0)
739 return 1;
741 memset(&args, 0, sizeof(args));
742 args.flags |= BTRFS_BALANCE_RESUME;
744 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
745 e = errno;
746 close_file_or_dir(fd, dirstream);
748 if (ret < 0) {
749 if (e == ECANCELED) {
750 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
751 fprintf(stderr, "balance paused by user\n");
752 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
753 fprintf(stderr, "balance canceled by user\n");
754 } else if (e == ENOTCONN || e == EINPROGRESS) {
755 error("balance resume on '%s' failed: %s", path,
756 (e == ENOTCONN) ? "Not in progress" :
757 "Already running");
758 if (e == ENOTCONN)
759 return 2;
760 else
761 return 1;
762 } else {
763 error("error during balancing '%s': %s\n"
764 "There may be more info in syslog - try dmesg | tail",
765 path, strerror(e));
766 return 1;
768 } else {
769 printf("Done, had to relocate %llu out of %llu chunks\n",
770 (unsigned long long)args.stat.completed,
771 (unsigned long long)args.stat.considered);
774 return 0;
777 static const char * const cmd_balance_status_usage[] = {
778 "btrfs balance status [-v] <path>",
779 "Show status of running or paused balance",
781 "-v be verbose",
782 NULL
785 /* Checks the status of the balance if any
786 * return codes:
787 * 2 : Error failed to know if there is any pending balance
788 * 1 : Successful to know status of a pending balance
789 * 0 : When there is no pending balance or completed
791 static int cmd_balance_status(int argc, char **argv)
793 struct btrfs_ioctl_balance_args args;
794 const char *path;
795 DIR *dirstream = NULL;
796 int fd;
797 int verbose = 0;
798 int ret;
799 int e;
801 optind = 1;
802 while (1) {
803 int opt;
804 static const struct option longopts[] = {
805 { "verbose", no_argument, NULL, 'v' },
806 { NULL, 0, NULL, 0 }
809 opt = getopt_long(argc, argv, "v", longopts, NULL);
810 if (opt < 0)
811 break;
813 switch (opt) {
814 case 'v':
815 verbose = 1;
816 break;
817 default:
818 usage(cmd_balance_status_usage);
822 if (check_argc_exact(argc - optind, 1))
823 usage(cmd_balance_status_usage);
825 path = argv[optind];
827 fd = btrfs_open_dir(path, &dirstream, 1);
828 if (fd < 0)
829 return 2;
831 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
832 e = errno;
833 close_file_or_dir(fd, dirstream);
835 if (ret < 0) {
836 if (e == ENOTCONN) {
837 printf("No balance found on '%s'\n", path);
838 return 0;
840 error("balance status on '%s' failed: %s", path, strerror(e));
841 return 2;
844 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
845 printf("Balance on '%s' is running", path);
846 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
847 printf(", cancel requested\n");
848 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
849 printf(", pause requested\n");
850 else
851 printf("\n");
852 } else {
853 printf("Balance on '%s' is paused\n", path);
856 printf("%llu out of about %llu chunks balanced (%llu considered), "
857 "%3.f%% left\n", (unsigned long long)args.stat.completed,
858 (unsigned long long)args.stat.expected,
859 (unsigned long long)args.stat.considered,
860 100 * (1 - (float)args.stat.completed/args.stat.expected));
862 if (verbose)
863 dump_ioctl_balance_args(&args);
865 return 1;
868 static int cmd_balance_full(int argc, char **argv)
870 struct btrfs_ioctl_balance_args args;
872 memset(&args, 0, sizeof(args));
873 args.flags |= BTRFS_BALANCE_TYPE_MASK;
875 return do_balance(argv[1], &args, BALANCE_START_NOWARN);
878 static const char balance_cmd_group_info[] =
879 "balance data across devices, or change block groups using filters";
881 const struct cmd_group balance_cmd_group = {
882 balance_cmd_group_usage, balance_cmd_group_info, {
883 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
884 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
885 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
886 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
887 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
888 { "--full-balance", cmd_balance_full, NULL, NULL, 1 },
889 NULL_CMD_STRUCT
893 int cmd_balance(int argc, char **argv)
895 if (argc == 2 && strcmp("start", argv[1]) != 0) {
896 /* old 'btrfs filesystem balance <path>' syntax */
897 struct btrfs_ioctl_balance_args args;
899 memset(&args, 0, sizeof(args));
900 args.flags |= BTRFS_BALANCE_TYPE_MASK;
902 return do_balance(argv[1], &args, 0);
905 return handle_command_group(&balance_cmd_group, argc, argv);