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