Merge branch 'restriper' of git://github.com/idryomov/btrfs-progs
[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
32 static const char balance_cmd_group_usage[] =
33         "btrfs [filesystem] balance [<command>] [options] <path>";
34
35 static const char balance_cmd_group_info[] =
36         "'btrfs filesystem balance' command is deprecated, please use\n"
37         "'btrfs balance start' command instead.";
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, "dup")) {
48                 *flags |= BTRFS_BLOCK_GROUP_DUP;
49         } else if (!strcmp(profile, "single")) {
50                 *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
51         } else {
52                 fprintf(stderr, "Unknown profile '%s'\n", profile);
53                 return 1;
54         }
55
56         return 0;
57 }
58
59 static int parse_profiles(char *profiles, u64 *flags)
60 {
61         char *this_char;
62         char *save_ptr;
63
64         for (this_char = strtok_r(profiles, "|", &save_ptr);
65              this_char != NULL;
66              this_char = strtok_r(NULL, "|", &save_ptr)) {
67                 if (parse_one_profile(this_char, flags))
68                         return 1;
69         }
70
71         return 0;
72 }
73
74 static int parse_u64(const char *str, u64 *result)
75 {
76         char *endptr;
77         u64 val;
78
79         val = strtoull(str, &endptr, 10);
80         if (*endptr)
81                 return 1;
82
83         *result = val;
84         return 0;
85 }
86
87 static int parse_range(const char *range, u64 *start, u64 *end)
88 {
89         char *dots;
90
91         dots = strstr(range, "..");
92         if (dots) {
93                 const char *rest = dots + 2;
94                 int skipped = 0;
95
96                 *dots = 0;
97
98                 if (!*rest) {
99                         *end = (u64)-1;
100                         skipped++;
101                 } else {
102                         if (parse_u64(rest, end))
103                                 return 1;
104                 }
105                 if (dots == range) {
106                         *start = 0;
107                         skipped++;
108                 } else {
109                         if (parse_u64(range, start))
110                                 return 1;
111                 }
112
113                 if (*start >= *end) {
114                         fprintf(stderr, "Range %llu..%llu doesn't make "
115                                 "sense\n", (unsigned long long)*start,
116                                 (unsigned long long)*end);
117                         return 1;
118                 }
119
120                 if (skipped <= 1)
121                         return 0;
122         }
123
124         return 1;
125 }
126
127 static int parse_filters(char *filters, struct btrfs_balance_args *args)
128 {
129         char *this_char;
130         char *value;
131         char *save_ptr;
132
133         if (!filters)
134                 return 0;
135
136         for (this_char = strtok_r(filters, ",", &save_ptr);
137              this_char != NULL;
138              this_char = strtok_r(NULL, ",", &save_ptr)) {
139                 if ((value = strchr(this_char, '=')) != NULL)
140                         *value++ = 0;
141                 if (!strcmp(this_char, "profiles")) {
142                         if (!value || !*value) {
143                                 fprintf(stderr, "the profiles filter requires "
144                                        "an argument\n");
145                                 return 1;
146                         }
147                         if (parse_profiles(value, &args->profiles)) {
148                                 fprintf(stderr, "Invalid profiles argument\n");
149                                 return 1;
150                         }
151                         args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
152                 } else if (!strcmp(this_char, "usage")) {
153                         if (!value || !*value) {
154                                 fprintf(stderr, "the usage filter requires "
155                                        "an argument\n");
156                                 return 1;
157                         }
158                         if (parse_u64(value, &args->usage) ||
159                             args->usage < 1 || args->usage > 100) {
160                                 fprintf(stderr, "Invalid usage argument: %s\n",
161                                        value);
162                                 return 1;
163                         }
164                         args->flags |= BTRFS_BALANCE_ARGS_USAGE;
165                 } else if (!strcmp(this_char, "devid")) {
166                         if (!value || !*value) {
167                                 fprintf(stderr, "the devid filter requires "
168                                        "an argument\n");
169                                 return 1;
170                         }
171                         if (parse_u64(value, &args->devid) ||
172                             args->devid == 0) {
173                                 fprintf(stderr, "Invalid devid argument: %s\n",
174                                        value);
175                                 return 1;
176                         }
177                         args->flags |= BTRFS_BALANCE_ARGS_DEVID;
178                 } else if (!strcmp(this_char, "drange")) {
179                         if (!value || !*value) {
180                                 fprintf(stderr, "the drange filter requires "
181                                        "an argument\n");
182                                 return 1;
183                         }
184                         if (parse_range(value, &args->pstart, &args->pend)) {
185                                 fprintf(stderr, "Invalid drange argument\n");
186                                 return 1;
187                         }
188                         args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
189                 } else if (!strcmp(this_char, "vrange")) {
190                         if (!value || !*value) {
191                                 fprintf(stderr, "the vrange filter requires "
192                                        "an argument\n");
193                                 return 1;
194                         }
195                         if (parse_range(value, &args->vstart, &args->vend)) {
196                                 fprintf(stderr, "Invalid vrange argument\n");
197                                 return 1;
198                         }
199                         args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
200                 } else if (!strcmp(this_char, "convert")) {
201                         if (!value || !*value) {
202                                 fprintf(stderr, "the convert option requires "
203                                        "an argument\n");
204                                 return 1;
205                         }
206                         if (parse_one_profile(value, &args->target)) {
207                                 fprintf(stderr, "Invalid convert argument\n");
208                                 return 1;
209                         }
210                         args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
211                 } else if (!strcmp(this_char, "soft")) {
212                         args->flags |= BTRFS_BALANCE_ARGS_SOFT;
213                 } else {
214                         fprintf(stderr, "Unrecognized balance option '%s'\n",
215                                 this_char);
216                         return 1;
217                 }
218         }
219
220         return 0;
221 }
222
223 static void dump_balance_args(struct btrfs_balance_args *args)
224 {
225         if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
226                 printf("converting, target=%llu, soft is %s",
227                        (unsigned long long)args->target,
228                        (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
229         } else {
230                 printf("balancing");
231         }
232
233         if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
234                 printf(", profiles=%llu", (unsigned long long)args->profiles);
235         if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
236                 printf(", usage=%llu", (unsigned long long)args->usage);
237         if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
238                 printf(", devid=%llu", (unsigned long long)args->devid);
239         if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
240                 printf(", drange=%llu..%llu",
241                        (unsigned long long)args->pstart,
242                        (unsigned long long)args->pend);
243         if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
244                 printf(", vrange=%llu..%llu",
245                        (unsigned long long)args->vstart,
246                        (unsigned long long)args->vend);
247
248         printf("\n");
249 }
250
251 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
252 {
253         printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
254                (unsigned long long)args->flags, (unsigned long long)args->state,
255                (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
256         if (args->flags & BTRFS_BALANCE_DATA) {
257                 printf("  DATA (flags 0x%llx): ",
258                        (unsigned long long)args->data.flags);
259                 dump_balance_args(&args->data);
260         }
261         if (args->flags & BTRFS_BALANCE_METADATA) {
262                 printf("  METADATA (flags 0x%llx): ",
263                        (unsigned long long)args->meta.flags);
264                 dump_balance_args(&args->meta);
265         }
266         if (args->flags & BTRFS_BALANCE_SYSTEM) {
267                 printf("  SYSTEM (flags 0x%llx): ",
268                        (unsigned long long)args->sys.flags);
269                 dump_balance_args(&args->sys);
270         }
271 }
272
273 static int do_balance_v1(int fd)
274 {
275         struct btrfs_ioctl_vol_args args;
276         int ret;
277
278         memset(&args, 0, sizeof(args));
279         ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
280         return ret;
281 }
282
283 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
284                       int nofilters)
285 {
286         int fd;
287         int ret;
288         int e;
289
290         fd = open_file_or_dir(path);
291         if (fd < 0) {
292                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
293                 return 12;
294         }
295
296         ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
297         e = errno;
298
299         if (ret < 0) {
300                 /*
301                  * older kernels don't have the new balance ioctl, try the
302                  * old one.  But, the old one doesn't know any filters, so
303                  * don't fall back if they tried to use the fancy new things
304                  */
305                 if (e == ENOTTY && nofilters) {
306                         ret = do_balance_v1(fd);
307                         if (ret == 0)
308                                 goto out;
309                         e = errno;
310                 }
311
312                 if (e == ECANCELED) {
313                         if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
314                                 fprintf(stderr, "balance paused by user\n");
315                         if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
316                                 fprintf(stderr, "balance canceled by user\n");
317                         ret = 0;
318                 } else {
319                         fprintf(stderr, "ERROR: error during balancing '%s' "
320                                 "- %s\n", path, strerror(e));
321                         if (e != EINPROGRESS)
322                                 fprintf(stderr, "There may be more info in "
323                                         "syslog - try dmesg | tail\n");
324                         ret = 19;
325                 }
326         } else {
327                 printf("Done, had to relocate %llu out of %llu chunks\n",
328                        (unsigned long long)args->stat.completed,
329                        (unsigned long long)args->stat.considered);
330                 ret = 0;
331         }
332
333 out:
334         close(fd);
335         return ret;
336 }
337
338 static const char * const cmd_balance_start_usage[] = {
339         "btrfs [filesystem] balance start [options] <path>",
340         "Balance chunks across the devices",
341         "Balance and/or convert (change allocation profile of) chunks that",
342         "passed all filters in a comma-separated list of filters for a",
343         "particular chunk type.  If filter list is not given balance all",
344         "chunks of that type.  In case none of the -d, -m or -s options is",
345         "given balance all chunks in a filesystem.",
346         "",
347         "-d[filters]    act on data chunks",
348         "-m[filters]    act on metadata chunks",
349         "-s[filetrs]    act on system chunks (only under -f)",
350         "-v             be verbose",
351         "-f             force reducing of metadata integrity",
352         NULL
353 };
354
355 static int cmd_balance_start(int argc, char **argv)
356 {
357         struct btrfs_ioctl_balance_args args;
358         struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
359                                                 &args.meta, NULL };
360         int force = 0;
361         int verbose = 0;
362         int nofilters = 1;
363         int i;
364
365         memset(&args, 0, sizeof(args));
366
367         optind = 1;
368         while (1) {
369                 int longindex;
370                 static struct option longopts[] = {
371                         { "data", optional_argument, NULL, 'd'},
372                         { "metadata", optional_argument, NULL, 'm' },
373                         { "system", optional_argument, NULL, 's' },
374                         { "force", no_argument, NULL, 'f' },
375                         { "verbose", no_argument, NULL, 'v' },
376                         { 0, 0, 0, 0 }
377                 };
378
379                 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts,
380                                       &longindex);
381                 if (opt < 0)
382                         break;
383
384                 switch (opt) {
385                 case 'd':
386                         nofilters = 0;
387                         args.flags |= BTRFS_BALANCE_DATA;
388
389                         if (parse_filters(optarg, &args.data))
390                                 return 1;
391                         break;
392                 case 's':
393                         nofilters = 0;
394                         args.flags |= BTRFS_BALANCE_SYSTEM;
395
396                         if (parse_filters(optarg, &args.sys))
397                                 return 1;
398                         break;
399                 case 'm':
400                         nofilters = 0;
401                         args.flags |= BTRFS_BALANCE_METADATA;
402
403                         if (parse_filters(optarg, &args.meta))
404                                 return 1;
405                         break;
406                 case 'f':
407                         force = 1;
408                         break;
409                 case 'v':
410                         verbose = 1;
411                         break;
412                 default:
413                         usage(cmd_balance_start_usage);
414                 }
415         }
416
417         if (check_argc_exact(argc - optind, 1))
418                 usage(cmd_balance_start_usage);
419
420         /*
421          * allow -s only under --force, otherwise do with system chunks
422          * the same thing we were ordered to do with meta chunks
423          */
424         if (args.flags & BTRFS_BALANCE_SYSTEM) {
425                 if (!force) {
426                         fprintf(stderr,
427 "Refusing to explicitly operate on system chunks.\n"
428 "Pass --force if you really want to do that.\n");
429                         return 1;
430                 }
431         } else if (args.flags & BTRFS_BALANCE_METADATA) {
432                 args.flags |= BTRFS_BALANCE_SYSTEM;
433                 memcpy(&args.sys, &args.meta,
434                         sizeof(struct btrfs_balance_args));
435         }
436
437         if (nofilters) {
438                 /* relocate everything - no filters */
439                 args.flags |= BTRFS_BALANCE_TYPE_MASK;
440         }
441
442         /* drange makes sense only when devid is set */
443         for (i = 0; ptrs[i]; i++) {
444                 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
445                     !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
446                         fprintf(stderr, "drange filter can be used only if "
447                                 "devid filter is used\n");
448                         return 1;
449                 }
450         }
451
452         /* soft makes sense only when convert for corresponding type is set */
453         for (i = 0; ptrs[i]; i++) {
454                 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
455                     !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
456                         fprintf(stderr, "'soft' option can be used only if "
457                                 "changing profiles\n");
458                         return 1;
459                 }
460         }
461
462         if (force)
463                 args.flags |= BTRFS_BALANCE_FORCE;
464         if (verbose)
465                 dump_ioctl_balance_args(&args);
466
467         return do_balance(argv[optind], &args, nofilters);
468 }
469
470 static const char * const cmd_balance_pause_usage[] = {
471         "btrfs [filesystem] balance pause <path>",
472         "Pause running balance",
473         NULL
474 };
475
476 static int cmd_balance_pause(int argc, char **argv)
477 {
478         const char *path;
479         int fd;
480         int ret;
481         int e;
482
483         if (check_argc_exact(argc, 2))
484                 usage(cmd_balance_pause_usage);
485
486         path = argv[1];
487
488         fd = open_file_or_dir(path);
489         if (fd < 0) {
490                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
491                 return 12;
492         }
493
494         ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
495         e = errno;
496         close(fd);
497
498         if (ret < 0) {
499                 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
500                         path, (e == ENOTCONN) ? "Not running" : strerror(e));
501                 return 19;
502         }
503
504         return 0;
505 }
506
507 static const char * const cmd_balance_cancel_usage[] = {
508         "btrfs [filesystem] balance cancel <path>",
509         "Cancel running or paused balance",
510         NULL
511 };
512
513 static int cmd_balance_cancel(int argc, char **argv)
514 {
515         const char *path;
516         int fd;
517         int ret;
518         int e;
519
520         if (check_argc_exact(argc, 2))
521                 usage(cmd_balance_cancel_usage);
522
523         path = argv[1];
524
525         fd = open_file_or_dir(path);
526         if (fd < 0) {
527                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
528                 return 12;
529         }
530
531         ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
532         e = errno;
533         close(fd);
534
535         if (ret < 0) {
536                 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
537                         path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
538                 return 19;
539         }
540
541         return 0;
542 }
543
544 static const char * const cmd_balance_resume_usage[] = {
545         "btrfs [filesystem] balance resume <path>",
546         "Resume interrupted balance",
547         NULL
548 };
549
550 static int cmd_balance_resume(int argc, char **argv)
551 {
552         struct btrfs_ioctl_balance_args args;
553         const char *path;
554         int fd;
555         int ret;
556         int e;
557
558         if (check_argc_exact(argc, 2))
559                 usage(cmd_balance_resume_usage);
560
561         path = argv[1];
562
563         fd = open_file_or_dir(path);
564         if (fd < 0) {
565                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
566                 return 12;
567         }
568
569         memset(&args, 0, sizeof(args));
570         args.flags |= BTRFS_BALANCE_RESUME;
571
572         ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
573         e = errno;
574         close(fd);
575
576         if (ret < 0) {
577                 if (e == ECANCELED) {
578                         if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
579                                 fprintf(stderr, "balance paused by user\n");
580                         if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
581                                 fprintf(stderr, "balance canceled by user\n");
582                 } else if (e == ENOTCONN || e == EINPROGRESS) {
583                         fprintf(stderr, "ERROR: balance resume on '%s' "
584                                 "failed - %s\n", path,
585                                 (e == ENOTCONN) ? "Not in progress" :
586                                                   "Already running");
587                         return 19;
588                 } else {
589                         fprintf(stderr,
590 "ERROR: error during balancing '%s' - %s\n"
591 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
592                         return 19;
593                 }
594         } else {
595                 printf("Done, had to relocate %llu out of %llu chunks\n",
596                        (unsigned long long)args.stat.completed,
597                        (unsigned long long)args.stat.considered);
598         }
599
600         return 0;
601 }
602
603 static const char * const cmd_balance_status_usage[] = {
604         "btrfs [filesystem] balance status [-v] <path>",
605         "Show status of running or paused balance",
606         "",
607         "-v     be verbose",
608         NULL
609 };
610
611 static int cmd_balance_status(int argc, char **argv)
612 {
613         struct btrfs_ioctl_balance_args args;
614         const char *path;
615         int fd;
616         int verbose = 0;
617         int ret;
618         int e;
619
620         optind = 1;
621         while (1) {
622                 int longindex;
623                 static struct option longopts[] = {
624                         { "verbose", no_argument, NULL, 'v' },
625                         { 0, 0, 0, 0}
626                 };
627
628                 int opt = getopt_long(argc, argv, "v", longopts, &longindex);
629                 if (opt < 0)
630                         break;
631
632                 switch (opt) {
633                 case 'v':
634                         verbose = 1;
635                         break;
636                 default:
637                         usage(cmd_balance_status_usage);
638                 }
639         }
640
641         if (check_argc_exact(argc - optind, 1))
642                 usage(cmd_balance_status_usage);
643
644         path = argv[optind];
645
646         fd = open_file_or_dir(path);
647         if (fd < 0) {
648                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
649                 return 12;
650         }
651
652         ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
653         e = errno;
654         close(fd);
655
656         if (ret < 0) {
657                 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
658                         path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
659                 return 19;
660         }
661
662         if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
663                 printf("Balance on '%s' is running", path);
664                 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
665                         printf(", cancel requested\n");
666                 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
667                         printf(", pause requested\n");
668                 else
669                         printf("\n");
670         } else {
671                 printf("Balance on '%s' is paused\n", path);
672         }
673
674         printf("%llu out of about %llu chunks balanced (%llu considered), "
675                "%3.f%% left\n", (unsigned long long)args.stat.completed,
676                (unsigned long long)args.stat.expected,
677                (unsigned long long)args.stat.considered,
678                100 * (1 - (float)args.stat.completed/args.stat.expected));
679
680         if (verbose)
681                 dump_ioctl_balance_args(&args);
682
683         return 0;
684 }
685
686 const struct cmd_group balance_cmd_group = {
687         balance_cmd_group_usage, balance_cmd_group_info, {
688                 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
689                 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
690                 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
691                 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
692                 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
693                 { 0, 0, 0, 0, 0 }
694         }
695 };
696
697 int cmd_balance(int argc, char **argv)
698 {
699         if (argc == 2) {
700                 /* old 'btrfs filesystem balance <path>' syntax */
701                 struct btrfs_ioctl_balance_args args;
702
703                 memset(&args, 0, sizeof(args));
704                 args.flags |= BTRFS_BALANCE_TYPE_MASK;
705
706                 return do_balance(argv[1], &args, 1);
707         }
708
709         return handle_command_group(&balance_cmd_group, argc, argv);
710 }