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