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