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