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