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(const char *path, struct btrfs_ioctl_balance_args *args)
274 {
275         int fd;
276         int ret;
277         int e;
278
279         fd = open_file_or_dir(path);
280         if (fd < 0) {
281                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
282                 return 12;
283         }
284
285         ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
286         e = errno;
287         close(fd);
288
289         if (ret < 0) {
290                 if (e == ECANCELED) {
291                         if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
292                                 fprintf(stderr, "balance paused by user\n");
293                         if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
294                                 fprintf(stderr, "balance canceled by user\n");
295                 } else {
296                         fprintf(stderr, "ERROR: error during balancing '%s' "
297                                 "- %s\n", path, strerror(e));
298                         if (e != EINPROGRESS)
299                                 fprintf(stderr, "There may be more info in "
300                                         "syslog - try dmesg | tail\n");
301                         return 19;
302                 }
303         } else {
304                 printf("Done, had to relocate %llu out of %llu chunks\n",
305                        (unsigned long long)args->stat.completed,
306                        (unsigned long long)args->stat.considered);
307         }
308
309         return 0;
310 }
311
312 static const char * const cmd_balance_start_usage[] = {
313         "btrfs [filesystem] balance start [options] <path>",
314         "Balance chunks across the devices",
315         "Balance and/or convert (change allocation profile of) chunks that",
316         "passed all filters in a comma-separated list of filters for a",
317         "particular chunk type.  If filter list is not given balance all",
318         "chunks of that type.  In case none of the -d, -m or -s options is",
319         "given balance all chunks in a filesystem.",
320         "",
321         "-d[filters]    act on data chunks",
322         "-m[filters]    act on metadata chunks",
323         "-s[filetrs]    act on system chunks (only under -f)",
324         "-v             be verbose",
325         "-f             force reducing of metadata integrity",
326         NULL
327 };
328
329 static int cmd_balance_start(int argc, char **argv)
330 {
331         struct btrfs_ioctl_balance_args args;
332         struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
333                                                 &args.meta, NULL };
334         int force = 0;
335         int verbose = 0;
336         int nofilters = 1;
337         int i;
338
339         memset(&args, 0, sizeof(args));
340
341         optind = 1;
342         while (1) {
343                 int longindex;
344                 static struct option longopts[] = {
345                         { "data", optional_argument, NULL, 'd'},
346                         { "metadata", optional_argument, NULL, 'm' },
347                         { "system", optional_argument, NULL, 's' },
348                         { "force", no_argument, NULL, 'f' },
349                         { "verbose", no_argument, NULL, 'v' },
350                         { 0, 0, 0, 0 }
351                 };
352
353                 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts,
354                                       &longindex);
355                 if (opt < 0)
356                         break;
357
358                 switch (opt) {
359                 case 'd':
360                         nofilters = 0;
361                         args.flags |= BTRFS_BALANCE_DATA;
362
363                         if (parse_filters(optarg, &args.data))
364                                 return 1;
365                         break;
366                 case 's':
367                         nofilters = 0;
368                         args.flags |= BTRFS_BALANCE_SYSTEM;
369
370                         if (parse_filters(optarg, &args.sys))
371                                 return 1;
372                         break;
373                 case 'm':
374                         nofilters = 0;
375                         args.flags |= BTRFS_BALANCE_METADATA;
376
377                         if (parse_filters(optarg, &args.meta))
378                                 return 1;
379                         break;
380                 case 'f':
381                         force = 1;
382                         break;
383                 case 'v':
384                         verbose = 1;
385                         break;
386                 default:
387                         usage(cmd_balance_start_usage);
388                 }
389         }
390
391         if (check_argc_exact(argc - optind, 1))
392                 usage(cmd_balance_start_usage);
393
394         /*
395          * allow -s only under --force, otherwise do with system chunks
396          * the same thing we were ordered to do with meta chunks
397          */
398         if (args.flags & BTRFS_BALANCE_SYSTEM) {
399                 if (!force) {
400                         fprintf(stderr,
401 "Refusing to explicitly operate on system chunks.\n"
402 "Pass --force if you really want to do that.\n");
403                         return 1;
404                 }
405         } else if (args.flags & BTRFS_BALANCE_METADATA) {
406                 args.flags |= BTRFS_BALANCE_SYSTEM;
407                 memcpy(&args.sys, &args.meta,
408                         sizeof(struct btrfs_balance_args));
409         }
410
411         if (nofilters) {
412                 /* relocate everything - no filters */
413                 args.flags |= BTRFS_BALANCE_TYPE_MASK;
414         }
415
416         /* drange makes sense only when devid is set */
417         for (i = 0; ptrs[i]; i++) {
418                 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
419                     !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
420                         fprintf(stderr, "drange filter can be used only if "
421                                 "devid filter is used\n");
422                         return 1;
423                 }
424         }
425
426         /* soft makes sense only when convert for corresponding type is set */
427         for (i = 0; ptrs[i]; i++) {
428                 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
429                     !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
430                         fprintf(stderr, "'soft' option can be used only if "
431                                 "changing profiles\n");
432                         return 1;
433                 }
434         }
435
436         if (force)
437                 args.flags |= BTRFS_BALANCE_FORCE;
438         if (verbose)
439                 dump_ioctl_balance_args(&args);
440
441         return do_balance(argv[optind], &args);
442 }
443
444 static const char * const cmd_balance_pause_usage[] = {
445         "btrfs [filesystem] balance pause <path>",
446         "Pause running balance",
447         NULL
448 };
449
450 static int cmd_balance_pause(int argc, char **argv)
451 {
452         const char *path;
453         int fd;
454         int ret;
455         int e;
456
457         if (check_argc_exact(argc, 2))
458                 usage(cmd_balance_pause_usage);
459
460         path = argv[1];
461
462         fd = open_file_or_dir(path);
463         if (fd < 0) {
464                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
465                 return 12;
466         }
467
468         ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
469         e = errno;
470         close(fd);
471
472         if (ret < 0) {
473                 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
474                         path, (e == ENOTCONN) ? "Not running" : strerror(e));
475                 return 19;
476         }
477
478         return 0;
479 }
480
481 static const char * const cmd_balance_cancel_usage[] = {
482         "btrfs [filesystem] balance cancel <path>",
483         "Cancel running or paused balance",
484         NULL
485 };
486
487 static int cmd_balance_cancel(int argc, char **argv)
488 {
489         const char *path;
490         int fd;
491         int ret;
492         int e;
493
494         if (check_argc_exact(argc, 2))
495                 usage(cmd_balance_cancel_usage);
496
497         path = argv[1];
498
499         fd = open_file_or_dir(path);
500         if (fd < 0) {
501                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
502                 return 12;
503         }
504
505         ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
506         e = errno;
507         close(fd);
508
509         if (ret < 0) {
510                 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
511                         path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
512                 return 19;
513         }
514
515         return 0;
516 }
517
518 static const char * const cmd_balance_resume_usage[] = {
519         "btrfs [filesystem] balance resume <path>",
520         "Resume interrupted balance",
521         NULL
522 };
523
524 static int cmd_balance_resume(int argc, char **argv)
525 {
526         struct btrfs_ioctl_balance_args args;
527         const char *path;
528         int fd;
529         int ret;
530         int e;
531
532         if (check_argc_exact(argc, 2))
533                 usage(cmd_balance_resume_usage);
534
535         path = argv[1];
536
537         fd = open_file_or_dir(path);
538         if (fd < 0) {
539                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
540                 return 12;
541         }
542
543         memset(&args, 0, sizeof(args));
544         args.flags |= BTRFS_BALANCE_RESUME;
545
546         ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
547         e = errno;
548         close(fd);
549
550         if (ret < 0) {
551                 if (e == ECANCELED) {
552                         if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
553                                 fprintf(stderr, "balance paused by user\n");
554                         if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
555                                 fprintf(stderr, "balance canceled by user\n");
556                 } else if (e == ENOTCONN || e == EINPROGRESS) {
557                         fprintf(stderr, "ERROR: balance resume on '%s' "
558                                 "failed - %s\n", path,
559                                 (e == ENOTCONN) ? "Not in progress" :
560                                                   "Already running");
561                         return 19;
562                 } else {
563                         fprintf(stderr,
564 "ERROR: error during balancing '%s' - %s\n"
565 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
566                         return 19;
567                 }
568         } else {
569                 printf("Done, had to relocate %llu out of %llu chunks\n",
570                        (unsigned long long)args.stat.completed,
571                        (unsigned long long)args.stat.considered);
572         }
573
574         return 0;
575 }
576
577 static const char * const cmd_balance_status_usage[] = {
578         "btrfs [filesystem] balance status [-v] <path>",
579         "Show status of running or paused balance",
580         "",
581         "-v     be verbose",
582         NULL
583 };
584
585 static int cmd_balance_status(int argc, char **argv)
586 {
587         struct btrfs_ioctl_balance_args args;
588         const char *path;
589         int fd;
590         int verbose = 0;
591         int ret;
592         int e;
593
594         optind = 1;
595         while (1) {
596                 int longindex;
597                 static struct option longopts[] = {
598                         { "verbose", no_argument, NULL, 'v' },
599                         { 0, 0, 0, 0}
600                 };
601
602                 int opt = getopt_long(argc, argv, "v", longopts, &longindex);
603                 if (opt < 0)
604                         break;
605
606                 switch (opt) {
607                 case 'v':
608                         verbose = 1;
609                         break;
610                 default:
611                         usage(cmd_balance_status_usage);
612                 }
613         }
614
615         if (check_argc_exact(argc - optind, 1))
616                 usage(cmd_balance_status_usage);
617
618         path = argv[optind];
619
620         fd = open_file_or_dir(path);
621         if (fd < 0) {
622                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
623                 return 12;
624         }
625
626         ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
627         e = errno;
628         close(fd);
629
630         if (ret < 0) {
631                 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
632                         path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
633                 return 19;
634         }
635
636         if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
637                 printf("Balance on '%s' is running", path);
638                 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
639                         printf(", cancel requested\n");
640                 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
641                         printf(", pause requested\n");
642                 else
643                         printf("\n");
644         } else {
645                 printf("Balance on '%s' is paused\n", path);
646         }
647
648         printf("%llu out of about %llu chunks balanced (%llu considered), "
649                "%3.f%% left\n", (unsigned long long)args.stat.completed,
650                (unsigned long long)args.stat.expected,
651                (unsigned long long)args.stat.considered,
652                100 * (1 - (float)args.stat.completed/args.stat.expected));
653
654         if (verbose)
655                 dump_ioctl_balance_args(&args);
656
657         return 0;
658 }
659
660 const struct cmd_group balance_cmd_group = {
661         balance_cmd_group_usage, balance_cmd_group_info, {
662                 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
663                 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
664                 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
665                 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
666                 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
667                 { 0, 0, 0, 0, 0 }
668         }
669 };
670
671 int cmd_balance(int argc, char **argv)
672 {
673         if (argc == 2) {
674                 /* old 'btrfs filesystem balance <path>' syntax */
675                 struct btrfs_ioctl_balance_args args;
676
677                 memset(&args, 0, sizeof(args));
678                 args.flags |= BTRFS_BALANCE_TYPE_MASK;
679
680                 return do_balance(argv[1], &args);
681         }
682
683         return handle_command_group(&balance_cmd_group, argc, argv);
684 }