btrfs-progs: Dont' stop scanning of devices at first failed device
[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], 1, &dev_block_count,
111                                            0, discard);
112                 close(devfd);
113                 if (res) {
114                         ret++;
115                         goto error_out;
116                 }
117
118                 path = canonicalize_path(argv[i]);
119                 if (!path) {
120                         error("could not canonicalize pathname '%s': %s",
121                                 argv[i], strerror(errno));
122                         ret++;
123                         goto error_out;
124                 }
125
126                 memset(&ioctl_args, 0, sizeof(ioctl_args));
127                 strncpy_null(ioctl_args.name, path);
128                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
129                 if (res < 0) {
130                         error("error adding device '%s': %s",
131                                 path, strerror(errno));
132                         ret++;
133                 }
134                 free(path);
135         }
136
137 error_out:
138         close_file_or_dir(fdmnt, dirstream);
139         return !!ret;
140 }
141
142 static int _cmd_device_remove(int argc, char **argv,
143                 const char * const *usagestr)
144 {
145         char    *mntpnt;
146         int i, fdmnt, ret = 0;
147         DIR     *dirstream = NULL;
148
149         clean_args_no_options(argc, argv, usagestr);
150
151         if (check_argc_min(argc - optind, 2))
152                 usage(usagestr);
153
154         mntpnt = argv[argc - 1];
155
156         fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
157         if (fdmnt < 0)
158                 return 1;
159
160         for(i = optind; i < argc - 1; i++) {
161                 struct  btrfs_ioctl_vol_args arg;
162                 int     res;
163
164                 if (is_block_device(argv[i]) != 1 && strcmp(argv[i], "missing")) {
165                         error("not a block device: %s", argv[i]);
166                         ret++;
167                         continue;
168                 }
169                 memset(&arg, 0, sizeof(arg));
170                 strncpy_null(arg.name, argv[i]);
171                 /*
172                  * Positive values are from BTRFS_ERROR_DEV_*,
173                  * otherwise it's a generic error, one of errnos
174                  */
175                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
176                 if (res) {
177                         const char *msg;
178
179                         if (res > 0)
180                                 msg = btrfs_err_str(res);
181                         else
182                                 msg = strerror(errno);
183                         error("error removing device '%s': %s",
184                                 argv[i], msg);
185                         ret++;
186                 }
187         }
188
189         close_file_or_dir(fdmnt, dirstream);
190         return !!ret;
191 }
192
193 static const char * const cmd_device_remove_usage[] = {
194         "btrfs device remove <device> [<device>...] <path>",
195         "Remove a device from a filesystem",
196         NULL
197 };
198
199 static int cmd_device_remove(int argc, char **argv)
200 {
201         return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
202 }
203
204 static const char * const cmd_device_delete_usage[] = {
205         "btrfs device delete <device> [<device>...] <path>",
206         "Remove a device from a filesystem",
207         NULL
208 };
209
210 static int cmd_device_delete(int argc, char **argv)
211 {
212         return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
213 }
214
215 static const char * const cmd_device_scan_usage[] = {
216         "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
217         "Scan devices for a btrfs filesystem",
218         " -d|--all-devices (deprecated)",
219         NULL
220 };
221
222 static int cmd_device_scan(int argc, char **argv)
223 {
224         int i;
225         int devstart = 1;
226         int all = 0;
227         int ret = 0;
228
229         optind = 1;
230         while (1) {
231                 int c;
232                 static const struct option long_options[] = {
233                         { "all-devices", no_argument, NULL, 'd'},
234                         { NULL, 0, NULL, 0}
235                 };
236
237                 c = getopt_long(argc, argv, "d", long_options, NULL);
238                 if (c < 0)
239                         break;
240                 switch (c) {
241                 case 'd':
242                         all = 1;
243                         break;
244                 default:
245                         usage(cmd_device_scan_usage);
246                 }
247         }
248
249         if (all && check_argc_max(argc - optind, 1))
250                 usage(cmd_device_scan_usage);
251
252         if (all || argc - optind == 0) {
253                 printf("Scanning for Btrfs filesystems\n");
254                 ret = btrfs_scan_lblkid();
255                 error_on(ret, "error %d while scanning", ret);
256                 ret = btrfs_register_all_devices();
257                 error_on(ret, "there are %d errors while registering devices", ret);
258                 goto out;
259         }
260
261         for( i = devstart ; i < argc ; i++ ){
262                 char *path;
263
264                 if (is_block_device(argv[i]) != 1) {
265                         error("not a block device: %s", argv[i]);
266                         ret = 1;
267                         goto out;
268                 }
269                 path = canonicalize_path(argv[i]);
270                 if (!path) {
271                         error("could not canonicalize path '%s': %s",
272                                 argv[i], strerror(errno));
273                         ret = 1;
274                         goto out;
275                 }
276                 printf("Scanning for Btrfs filesystems in '%s'\n", path);
277                 if (btrfs_register_one_device(path) != 0) {
278                         ret = 1;
279                         free(path);
280                         goto out;
281                 }
282                 free(path);
283         }
284
285 out:
286         return !!ret;
287 }
288
289 static const char * const cmd_device_ready_usage[] = {
290         "btrfs device ready <device>",
291         "Check device to see if it has all of its devices in cache for mounting",
292         NULL
293 };
294
295 static int cmd_device_ready(int argc, char **argv)
296 {
297         struct  btrfs_ioctl_vol_args args;
298         int     fd;
299         int     ret;
300         char    *path;
301
302         clean_args_no_options(argc, argv, cmd_device_ready_usage);
303
304         if (check_argc_min(argc - optind, 1))
305                 usage(cmd_device_ready_usage);
306
307         fd = open("/dev/btrfs-control", O_RDWR);
308         if (fd < 0) {
309                 perror("failed to open /dev/btrfs-control");
310                 return 1;
311         }
312
313         path = canonicalize_path(argv[optind]);
314         if (!path) {
315                 error("could not canonicalize pathname '%s': %s",
316                         argv[optind], strerror(errno));
317                 ret = 1;
318                 goto out;
319         }
320
321         if (is_block_device(path) != 1) {
322                 error("not a block device: %s", path);
323                 ret = 1;
324                 goto out;
325         }
326
327         memset(&args, 0, sizeof(args));
328         strncpy_null(args.name, path);
329         ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
330         if (ret < 0) {
331                 error("unable to determine if device '%s' is ready for mount: %s",
332                         path, strerror(errno));
333                 ret = 1;
334         }
335
336 out:
337         free(path);
338         close(fd);
339         return ret;
340 }
341
342 static const char * const cmd_device_stats_usage[] = {
343         "btrfs device stats [-z] <path>|<device>",
344         "Show current device IO stats.",
345         "",
346         "-z                     show current stats and reset values to zero",
347         NULL
348 };
349
350 static int cmd_device_stats(int argc, char **argv)
351 {
352         char *dev_path;
353         struct btrfs_ioctl_fs_info_args fi_args;
354         struct btrfs_ioctl_dev_info_args *di_args = NULL;
355         int ret;
356         int fdmnt;
357         int i;
358         int c;
359         int err = 0;
360         __u64 flags = 0;
361         DIR *dirstream = NULL;
362
363         optind = 1;
364         while ((c = getopt(argc, argv, "z")) != -1) {
365                 switch (c) {
366                 case 'z':
367                         flags = BTRFS_DEV_STATS_RESET;
368                         break;
369                 case '?':
370                 default:
371                         usage(cmd_device_stats_usage);
372                 }
373         }
374
375         if (check_argc_exact(argc - optind, 1))
376                 usage(cmd_device_stats_usage);
377
378         dev_path = argv[optind];
379
380         fdmnt = open_path_or_dev_mnt(dev_path, &dirstream, 1);
381         if (fdmnt < 0)
382                 return 1;
383
384         ret = get_fs_info(dev_path, &fi_args, &di_args);
385         if (ret) {
386                 error("getting dev info for devstats failed: %s",
387                         strerror(-ret));
388                 err = 1;
389                 goto out;
390         }
391         if (!fi_args.num_devices) {
392                 error("no devices found");
393                 err = 1;
394                 goto out;
395         }
396
397         for (i = 0; i < fi_args.num_devices; i++) {
398                 struct btrfs_ioctl_get_dev_stats args = {0};
399                 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
400
401                 strncpy((char *)path, (char *)di_args[i].path,
402                         BTRFS_DEVICE_PATH_NAME_MAX);
403                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
404
405                 args.devid = di_args[i].devid;
406                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
407                 args.flags = flags;
408
409                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
410                         error("DEV_STATS ioctl failed on %s: %s",
411                               path, strerror(errno));
412                         err = 1;
413                 } else {
414                         char *canonical_path;
415
416                         canonical_path = canonicalize_path((char *)path);
417
418                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
419                                 printf("[%s].write_io_errs   %llu\n",
420                                        canonical_path,
421                                        (unsigned long long) args.values[
422                                         BTRFS_DEV_STAT_WRITE_ERRS]);
423                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
424                                 printf("[%s].read_io_errs    %llu\n",
425                                        canonical_path,
426                                        (unsigned long long) args.values[
427                                         BTRFS_DEV_STAT_READ_ERRS]);
428                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
429                                 printf("[%s].flush_io_errs   %llu\n",
430                                        canonical_path,
431                                        (unsigned long long) args.values[
432                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
433                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
434                                 printf("[%s].corruption_errs %llu\n",
435                                        canonical_path,
436                                        (unsigned long long) args.values[
437                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
438                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
439                                 printf("[%s].generation_errs %llu\n",
440                                        canonical_path,
441                                        (unsigned long long) args.values[
442                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
443
444                         free(canonical_path);
445                 }
446         }
447
448 out:
449         free(di_args);
450         close_file_or_dir(fdmnt, dirstream);
451
452         return err;
453 }
454
455 static const char * const cmd_device_usage_usage[] = {
456         "btrfs device usage [options] <path> [<path>..]",
457         "Show detailed information about internal allocations in devices.",
458         HELPINFO_UNITS_SHORT_LONG,
459         NULL
460 };
461
462 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
463 {
464         int i;
465         int ret = 0;
466         struct chunk_info *chunkinfo = NULL;
467         struct device_info *devinfo = NULL;
468         int chunkcount = 0;
469         int devcount = 0;
470
471         ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
472                         &devcount);
473         if (ret)
474                 goto out;
475
476         for (i = 0; i < devcount; i++) {
477                 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
478                 print_device_sizes(fd, &devinfo[i], unit_mode);
479                 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
480                                 unit_mode);
481                 printf("\n");
482         }
483
484 out:
485         free(devinfo);
486         free(chunkinfo);
487
488         return ret;
489 }
490
491 static int cmd_device_usage(int argc, char **argv)
492 {
493         unsigned unit_mode;
494         int ret = 0;
495         int i;
496
497         unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
498
499         clean_args_no_options(argc, argv, cmd_device_usage_usage);
500
501         if (check_argc_min(argc - optind, 1))
502                 usage(cmd_device_usage_usage);
503
504         for (i = optind; i < argc; i++) {
505                 int fd;
506                 DIR *dirstream = NULL;
507
508                 if (i > 1)
509                         printf("\n");
510
511                 fd = btrfs_open_dir(argv[i], &dirstream, 1);
512                 if (fd < 0) {
513                         ret = 1;
514                         break;
515                 }
516
517                 ret = _cmd_device_usage(fd, argv[i], unit_mode);
518                 close_file_or_dir(fd, dirstream);
519
520                 if (ret)
521                         break;
522         }
523
524         return !!ret;
525 }
526
527 static const char device_cmd_group_info[] =
528 "manage and query devices in the filesystem";
529
530 const struct cmd_group device_cmd_group = {
531         device_cmd_group_usage, device_cmd_group_info, {
532                 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
533                 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
534                         CMD_ALIAS },
535                 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
536                 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
537                 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
538                 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
539                 { "usage", cmd_device_usage,
540                         cmd_device_usage_usage, NULL, 0 },
541                 NULL_CMD_STRUCT
542         }
543 };
544
545 int cmd_device(int argc, char **argv)
546 {
547         return handle_command_group(&device_cmd_group, argc, argv);
548 }