btrfs-progs: unify unit mode parameters and variables
[platform/upstream/btrfs-progs.git] / cmds-device.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 <fcntl.h>
22 #include <sys/ioctl.h>
23 #include <errno.h>
24 #include <sys/stat.h>
25 #include <getopt.h>
26
27 #include "kerncompat.h"
28 #include "ctree.h"
29 #include "ioctl.h"
30 #include "utils.h"
31 #include "cmds-fi-disk_usage.h"
32
33 #include "commands.h"
34
35 static const char * const device_cmd_group_usage[] = {
36         "btrfs device <command> [<args>]",
37         NULL
38 };
39
40 static const char * const cmd_add_dev_usage[] = {
41         "btrfs device add [options] <device> [<device>...] <path>",
42         "Add a device to a filesystem",
43         "-K|--nodiscard    do not perform whole device TRIM",
44         "-f|--force        force overwrite existing filesystem on the disk",
45         NULL
46 };
47
48 static int cmd_add_dev(int argc, char **argv)
49 {
50         char    *mntpnt;
51         int     i, fdmnt, ret=0, e;
52         DIR     *dirstream = NULL;
53         int discard = 1;
54         int force = 0;
55         char estr[100];
56
57         while (1) {
58                 int long_index;
59                 static struct option long_options[] = {
60                         { "nodiscard", optional_argument, NULL, 'K'},
61                         { "force", no_argument, NULL, 'f'},
62                         { 0, 0, 0, 0 }
63                 };
64                 int c = getopt_long(argc, argv, "Kf", long_options,
65                                         &long_index);
66                 if (c < 0)
67                         break;
68                 switch (c) {
69                 case 'K':
70                         discard = 0;
71                         break;
72                 case 'f':
73                         force = 1;
74                         break;
75                 default:
76                         usage(cmd_add_dev_usage);
77                 }
78         }
79
80         argc = argc - optind;
81
82         if (check_argc_min(argc, 2))
83                 usage(cmd_add_dev_usage);
84
85         mntpnt = argv[optind + argc - 1];
86
87         fdmnt = open_file_or_dir(mntpnt, &dirstream);
88         if (fdmnt < 0) {
89                 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
90                 return 1;
91         }
92
93         for (i = optind; i < optind + argc - 1; i++){
94                 struct btrfs_ioctl_vol_args ioctl_args;
95                 int     devfd, res;
96                 u64 dev_block_count = 0;
97                 int mixed = 0;
98                 char *path;
99
100                 res = test_dev_for_mkfs(argv[i], force, estr);
101                 if (res) {
102                         fprintf(stderr, "%s", estr);
103                         ret++;
104                         continue;
105                 }
106
107                 devfd = open(argv[i], O_RDWR);
108                 if (devfd < 0) {
109                         fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
110                         ret++;
111                         continue;
112                 }
113
114                 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
115                                            0, &mixed, discard);
116                 close(devfd);
117                 if (res) {
118                         ret++;
119                         goto error_out;
120                 }
121
122                 path = canonicalize_path(argv[i]);
123                 if (!path) {
124                         fprintf(stderr,
125                                 "ERROR: Could not canonicalize pathname '%s': %s\n",
126                                 argv[i], strerror(errno));
127                         ret++;
128                         goto error_out;
129                 }
130
131                 strncpy_null(ioctl_args.name, path);
132                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
133                 e = errno;
134                 if (res < 0) {
135                         fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
136                                 path, strerror(e));
137                         ret++;
138                 }
139                 free(path);
140         }
141
142 error_out:
143         close_file_or_dir(fdmnt, dirstream);
144         return !!ret;
145 }
146
147 static const char * const cmd_rm_dev_usage[] = {
148         "btrfs device delete <device> [<device>...] <path>",
149         "Remove a device from a filesystem",
150         NULL
151 };
152
153 static int cmd_rm_dev(int argc, char **argv)
154 {
155         char    *mntpnt;
156         int     i, fdmnt, ret=0, e;
157         DIR     *dirstream = NULL;
158
159         if (check_argc_min(argc, 3))
160                 usage(cmd_rm_dev_usage);
161
162         mntpnt = argv[argc - 1];
163
164         fdmnt = open_file_or_dir(mntpnt, &dirstream);
165         if (fdmnt < 0) {
166                 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
167                 return 1;
168         }
169
170         for(i=1 ; i < argc - 1; i++ ){
171                 struct  btrfs_ioctl_vol_args arg;
172                 int     res;
173
174                 if (!is_block_device(argv[i])) {
175                         fprintf(stderr,
176                                 "ERROR: %s is not a block device\n", argv[i]);
177                         ret++;
178                         continue;
179                 }
180                 strncpy_null(arg.name, argv[i]);
181                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
182                 e = errno;
183                 if (res > 0) {
184                         fprintf(stderr,
185                                 "ERROR: error removing the device '%s' - %s\n",
186                                 argv[i], btrfs_err_str(res));
187                         ret++;
188                 } else if (res < 0) {
189                         fprintf(stderr,
190                                 "ERROR: error removing the device '%s' - %s\n",
191                                 argv[i], strerror(e));
192                         ret++;
193                 }
194         }
195
196         close_file_or_dir(fdmnt, dirstream);
197         return !!ret;
198 }
199
200 static const char * const cmd_scan_dev_usage[] = {
201         "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
202         "Scan devices for a btrfs filesystem",
203         " -d|--all-devices (deprecated)",
204         NULL
205 };
206
207 static int cmd_scan_dev(int argc, char **argv)
208 {
209         int i;
210         int devstart = 1;
211         int all = 0;
212         int ret = 0;
213
214         optind = 1;
215         while (1) {
216                 int long_index;
217                 static struct option long_options[] = {
218                         { "all-devices", no_argument, NULL, 'd'},
219                         { 0, 0, 0, 0 },
220                 };
221                 int c = getopt_long(argc, argv, "d", long_options,
222                                     &long_index);
223                 if (c < 0)
224                         break;
225                 switch (c) {
226                 case 'd':
227                         all = 1;
228                         break;
229                 default:
230                         usage(cmd_scan_dev_usage);
231                 }
232         }
233
234         if (all && check_argc_max(argc, 2))
235                 usage(cmd_scan_dev_usage);
236
237         if (all || argc == 1) {
238                 printf("Scanning for Btrfs filesystems\n");
239                 ret = btrfs_scan_lblkid();
240                 if (ret)
241                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
242                 ret = btrfs_register_all_devices();
243                 if (ret)
244                         fprintf(stderr, "ERROR: error %d while registering\n", ret);
245                 goto out;
246         }
247
248         for( i = devstart ; i < argc ; i++ ){
249                 char *path;
250
251                 if (!is_block_device(argv[i])) {
252                         fprintf(stderr,
253                                 "ERROR: %s is not a block device\n", argv[i]);
254                         ret = 1;
255                         goto out;
256                 }
257                 path = canonicalize_path(argv[i]);
258                 if (!path) {
259                         fprintf(stderr,
260                                 "ERROR: Could not canonicalize path '%s': %s\n",
261                                 argv[i], strerror(errno));
262                         ret = 1;
263                         goto out;
264                 }
265                 printf("Scanning for Btrfs filesystems in '%s'\n", path);
266                 if (btrfs_register_one_device(path) != 0) {
267                         ret = 1;
268                         free(path);
269                         goto out;
270                 }
271                 free(path);
272         }
273
274 out:
275         return !!ret;
276 }
277
278 static const char * const cmd_ready_dev_usage[] = {
279         "btrfs device ready <device>",
280         "Check device to see if it has all of its devices in cache for mounting",
281         NULL
282 };
283
284 static int cmd_ready_dev(int argc, char **argv)
285 {
286         struct  btrfs_ioctl_vol_args args;
287         int     fd;
288         int     ret;
289         char    *path;
290
291         if (check_argc_min(argc, 2))
292                 usage(cmd_ready_dev_usage);
293
294         fd = open("/dev/btrfs-control", O_RDWR);
295         if (fd < 0) {
296                 perror("failed to open /dev/btrfs-control");
297                 return 1;
298         }
299
300         path = canonicalize_path(argv[argc - 1]);
301         if (!path) {
302                 fprintf(stderr,
303                         "ERROR: Could not canonicalize pathname '%s': %s\n",
304                         argv[argc - 1], strerror(errno));
305                 ret = 1;
306                 goto out;
307         }
308
309         if (!is_block_device(path)) {
310                 fprintf(stderr,
311                         "ERROR: %s is not a block device\n", path);
312                 ret = 1;
313                 goto out;
314         }
315
316         strncpy(args.name, path, BTRFS_PATH_NAME_MAX);
317         ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
318         if (ret < 0) {
319                 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
320                         " is ready for mounting - %s\n", path,
321                         strerror(errno));
322                 ret = 1;
323         }
324
325 out:
326         free(path);
327         close(fd);
328         return ret;
329 }
330
331 static const char * const cmd_dev_stats_usage[] = {
332         "btrfs device stats [-z] <path>|<device>",
333         "Show current device IO stats. -z to reset stats afterwards.",
334         NULL
335 };
336
337 static int cmd_dev_stats(int argc, char **argv)
338 {
339         char *dev_path;
340         struct btrfs_ioctl_fs_info_args fi_args;
341         struct btrfs_ioctl_dev_info_args *di_args = NULL;
342         int ret;
343         int fdmnt;
344         int i;
345         int c;
346         int err = 0;
347         __u64 flags = 0;
348         DIR *dirstream = NULL;
349
350         optind = 1;
351         while ((c = getopt(argc, argv, "z")) != -1) {
352                 switch (c) {
353                 case 'z':
354                         flags = BTRFS_DEV_STATS_RESET;
355                         break;
356                 case '?':
357                 default:
358                         usage(cmd_dev_stats_usage);
359                 }
360         }
361
362         argc = argc - optind;
363         if (check_argc_exact(argc, 1))
364                 usage(cmd_dev_stats_usage);
365
366         dev_path = argv[optind];
367
368         fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
369
370         if (fdmnt < 0) {
371                 if (errno == EINVAL)
372                         fprintf(stderr,
373                                 "ERROR: '%s' is not a mounted btrfs device\n",
374                                 dev_path);
375                 else
376                         fprintf(stderr, "ERROR: can't access '%s': %s\n",
377                                 dev_path, strerror(errno));
378                 return 1;
379         }
380
381         ret = get_fs_info(dev_path, &fi_args, &di_args);
382         if (ret) {
383                 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
384                                 "%s\n", strerror(-ret));
385                 err = 1;
386                 goto out;
387         }
388         if (!fi_args.num_devices) {
389                 fprintf(stderr, "ERROR: no devices found\n");
390                 err = 1;
391                 goto out;
392         }
393
394         for (i = 0; i < fi_args.num_devices; i++) {
395                 struct btrfs_ioctl_get_dev_stats args = {0};
396                 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
397
398                 strncpy((char *)path, (char *)di_args[i].path,
399                         BTRFS_DEVICE_PATH_NAME_MAX);
400                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
401
402                 args.devid = di_args[i].devid;
403                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
404                 args.flags = flags;
405
406                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
407                         fprintf(stderr,
408                                 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
409                                 path, strerror(errno));
410                         err = 1;
411                 } else {
412                         char *canonical_path;
413
414                         canonical_path = canonicalize_path((char *)path);
415
416                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
417                                 printf("[%s].write_io_errs   %llu\n",
418                                        canonical_path,
419                                        (unsigned long long) args.values[
420                                         BTRFS_DEV_STAT_WRITE_ERRS]);
421                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
422                                 printf("[%s].read_io_errs    %llu\n",
423                                        canonical_path,
424                                        (unsigned long long) args.values[
425                                         BTRFS_DEV_STAT_READ_ERRS]);
426                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
427                                 printf("[%s].flush_io_errs   %llu\n",
428                                        canonical_path,
429                                        (unsigned long long) args.values[
430                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
431                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
432                                 printf("[%s].corruption_errs %llu\n",
433                                        canonical_path,
434                                        (unsigned long long) args.values[
435                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
436                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
437                                 printf("[%s].generation_errs %llu\n",
438                                        canonical_path,
439                                        (unsigned long long) args.values[
440                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
441
442                         free(canonical_path);
443                 }
444         }
445
446 out:
447         free(di_args);
448         close_file_or_dir(fdmnt, dirstream);
449
450         return err;
451 }
452
453 const char * const cmd_device_usage_usage[] = {
454         "btrfs device usage [options] <path> [<path>..]",
455         "Show detailed information about internal allocations in devices.",
456         "-b|--raw           raw numbers in bytes",
457         "-h                 human friendly numbers, base 1024 (default)",
458         "-H                 human friendly numbers, base 1000",
459         "--iec              use 1024 as a base (KiB, MiB, GiB, TiB)",
460         "--si               use 1000 as a base (kB, MB, GB, TB)",
461         "-k|--kbytes        show sizes in KiB, or kB with --si",
462         "-m|--mbytes        show sizes in MiB, or MB with --si",
463         "-g|--gbytes        show sizes in GiB, or GB with --si",
464         "-t|--tbytes        show sizes in TiB, or TB with --si",
465         NULL
466 };
467
468 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
469 {
470         int i;
471         int ret = 0;
472         struct chunk_info *chunkinfo = NULL;
473         struct device_info *devinfo = NULL;
474         int chunkcount = 0;
475         int devcount = 0;
476
477         ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
478                         &devcount);
479         if (ret)
480                 goto out;
481
482         for (i = 0; i < devcount; i++) {
483                 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
484                 print_device_sizes(fd, &devinfo[i], unit_mode);
485                 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
486                                 unit_mode);
487                 printf("\n");
488         }
489
490 out:
491         free(devinfo);
492         free(chunkinfo);
493
494         return ret;
495 }
496
497 int cmd_device_usage(int argc, char **argv)
498 {
499         unsigned unit_mode = UNITS_DEFAULT;
500         int ret = 0;
501         int     i, more_than_one = 0;
502
503         optind = 1;
504         while (1) {
505                 int long_index;
506                 static const struct option long_options[] = {
507                         { "raw", no_argument, NULL, 'b'},
508                         { "kbytes", no_argument, NULL, 'k'},
509                         { "mbytes", no_argument, NULL, 'm'},
510                         { "gbytes", no_argument, NULL, 'g'},
511                         { "tbytes", no_argument, NULL, 't'},
512                         { "si", no_argument, NULL, 256},
513                         { "iec", no_argument, NULL, 257},
514                 };
515                 int c = getopt_long(argc, argv, "bhHkmgt", long_options,
516                                 &long_index);
517
518                 if (c < 0)
519                         break;
520                 switch (c) {
521                 case 'b':
522                         unit_mode = UNITS_RAW;
523                         break;
524                 case 'k':
525                         units_set_base(&unit_mode, UNITS_KBYTES);
526                         break;
527                 case 'm':
528                         units_set_base(&unit_mode, UNITS_MBYTES);
529                         break;
530                 case 'g':
531                         units_set_base(&unit_mode, UNITS_GBYTES);
532                         break;
533                 case 't':
534                         units_set_base(&unit_mode, UNITS_TBYTES);
535                         break;
536                 case 'h':
537                         unit_mode = UNITS_HUMAN_BINARY;
538                         break;
539                 case 'H':
540                         unit_mode = UNITS_HUMAN_DECIMAL;
541                         break;
542                 case 256:
543                         units_set_mode(&unit_mode, UNITS_DECIMAL);
544                         break;
545                 case 257:
546                         units_set_mode(&unit_mode, UNITS_BINARY);
547                         break;
548                 default:
549                         usage(cmd_device_usage_usage);
550                 }
551         }
552
553         if (check_argc_min(argc - optind, 1))
554                 usage(cmd_device_usage_usage);
555
556         for (i = optind; i < argc ; i++) {
557                 int fd;
558                 DIR     *dirstream = NULL;
559                 if (more_than_one)
560                         printf("\n");
561
562                 fd = open_file_or_dir(argv[i], &dirstream);
563                 if (fd < 0) {
564                         fprintf(stderr, "ERROR: can't access '%s'\n",
565                                 argv[1]);
566                         ret = 1;
567                         goto out;
568                 }
569
570                 ret = _cmd_device_usage(fd, argv[i], unit_mode);
571                 close_file_or_dir(fd, dirstream);
572
573                 if (ret)
574                         goto out;
575                 more_than_one = 1;
576         }
577 out:
578         return !!ret;
579 }
580
581 const struct cmd_group device_cmd_group = {
582         device_cmd_group_usage, NULL, {
583                 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
584                 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
585                 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
586                 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
587                 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
588                 { "usage", cmd_device_usage,
589                         cmd_device_usage_usage, NULL, 0 },
590                 NULL_CMD_STRUCT
591         }
592 };
593
594 int cmd_device(int argc, char **argv)
595 {
596         return handle_command_group(&device_cmd_group, argc, argv);
597 }