btrfs-progs: print error within test_dev_for_mkfs
[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_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
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_add_dev_usage);
76                 }
77         }
78
79         argc = argc - optind;
80
81         if (check_argc_min(argc, 2))
82                 usage(cmd_add_dev_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                 strncpy_null(ioctl_args.name, path);
130                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
131                 e = errno;
132                 if (res < 0) {
133                         fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
134                                 path, strerror(e));
135                         ret++;
136                 }
137                 free(path);
138         }
139
140 error_out:
141         close_file_or_dir(fdmnt, dirstream);
142         return !!ret;
143 }
144
145 static const char * const cmd_rm_dev_usage[] = {
146         "btrfs device delete <device> [<device>...] <path>",
147         "Remove a device from a filesystem",
148         NULL
149 };
150
151 static int cmd_rm_dev(int argc, char **argv)
152 {
153         char    *mntpnt;
154         int     i, fdmnt, ret=0, e;
155         DIR     *dirstream = NULL;
156
157         if (check_argc_min(argc, 3))
158                 usage(cmd_rm_dev_usage);
159
160         mntpnt = argv[argc - 1];
161
162         fdmnt = open_file_or_dir(mntpnt, &dirstream);
163         if (fdmnt < 0) {
164                 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
165                 return 1;
166         }
167
168         for(i=1 ; i < argc - 1; i++ ){
169                 struct  btrfs_ioctl_vol_args arg;
170                 int     res;
171
172                 if (!is_block_device(argv[i])) {
173                         fprintf(stderr,
174                                 "ERROR: %s is not a block device\n", argv[i]);
175                         ret++;
176                         continue;
177                 }
178                 strncpy_null(arg.name, argv[i]);
179                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
180                 e = errno;
181                 if (res) {
182                         const char *msg;
183
184                         if (res > 0)
185                                 msg = btrfs_err_str(res);
186                         else
187                                 msg = strerror(e);
188                         fprintf(stderr,
189                                 "ERROR: error removing the device '%s' - %s\n",
190                                 argv[i], msg);
191                         ret++;
192                 }
193         }
194
195         close_file_or_dir(fdmnt, dirstream);
196         return !!ret;
197 }
198
199 static const char * const cmd_scan_dev_usage[] = {
200         "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
201         "Scan devices for a btrfs filesystem",
202         " -d|--all-devices (deprecated)",
203         NULL
204 };
205
206 static int cmd_scan_dev(int argc, char **argv)
207 {
208         int i;
209         int devstart = 1;
210         int all = 0;
211         int ret = 0;
212
213         optind = 1;
214         while (1) {
215                 int c;
216                 static const struct option long_options[] = {
217                         { "all-devices", no_argument, NULL, 'd'},
218                         { NULL, 0, NULL, 0}
219                 };
220
221                 c = getopt_long(argc, argv, "d", long_options, NULL);
222                 if (c < 0)
223                         break;
224                 switch (c) {
225                 case 'd':
226                         all = 1;
227                         break;
228                 default:
229                         usage(cmd_scan_dev_usage);
230                 }
231         }
232
233         if (all && check_argc_max(argc, 2))
234                 usage(cmd_scan_dev_usage);
235
236         if (all || argc == 1) {
237                 printf("Scanning for Btrfs filesystems\n");
238                 ret = btrfs_scan_lblkid();
239                 if (ret)
240                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
241                 ret = btrfs_register_all_devices();
242                 if (ret)
243                         fprintf(stderr, "ERROR: error %d while registering\n", ret);
244                 goto out;
245         }
246
247         for( i = devstart ; i < argc ; i++ ){
248                 char *path;
249
250                 if (!is_block_device(argv[i])) {
251                         fprintf(stderr,
252                                 "ERROR: %s is not a block device\n", argv[i]);
253                         ret = 1;
254                         goto out;
255                 }
256                 path = canonicalize_path(argv[i]);
257                 if (!path) {
258                         fprintf(stderr,
259                                 "ERROR: Could not canonicalize path '%s': %s\n",
260                                 argv[i], strerror(errno));
261                         ret = 1;
262                         goto out;
263                 }
264                 printf("Scanning for Btrfs filesystems in '%s'\n", path);
265                 if (btrfs_register_one_device(path) != 0) {
266                         ret = 1;
267                         free(path);
268                         goto out;
269                 }
270                 free(path);
271         }
272
273 out:
274         return !!ret;
275 }
276
277 static const char * const cmd_ready_dev_usage[] = {
278         "btrfs device ready <device>",
279         "Check device to see if it has all of its devices in cache for mounting",
280         NULL
281 };
282
283 static int cmd_ready_dev(int argc, char **argv)
284 {
285         struct  btrfs_ioctl_vol_args args;
286         int     fd;
287         int     ret;
288         char    *path;
289
290         if (check_argc_min(argc, 2))
291                 usage(cmd_ready_dev_usage);
292
293         fd = open("/dev/btrfs-control", O_RDWR);
294         if (fd < 0) {
295                 perror("failed to open /dev/btrfs-control");
296                 return 1;
297         }
298
299         path = canonicalize_path(argv[argc - 1]);
300         if (!path) {
301                 fprintf(stderr,
302                         "ERROR: Could not canonicalize pathname '%s': %s\n",
303                         argv[argc - 1], strerror(errno));
304                 ret = 1;
305                 goto out;
306         }
307
308         if (!is_block_device(path)) {
309                 fprintf(stderr,
310                         "ERROR: %s is not a block device\n", path);
311                 ret = 1;
312                 goto out;
313         }
314
315         strncpy(args.name, path, BTRFS_PATH_NAME_MAX);
316         ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
317         if (ret < 0) {
318                 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
319                         " is ready for mounting - %s\n", path,
320                         strerror(errno));
321                 ret = 1;
322         }
323
324 out:
325         free(path);
326         close(fd);
327         return ret;
328 }
329
330 static const char * const cmd_dev_stats_usage[] = {
331         "btrfs device stats [-z] <path>|<device>",
332         "Show current device IO stats. -z to reset stats afterwards.",
333         NULL
334 };
335
336 static int cmd_dev_stats(int argc, char **argv)
337 {
338         char *dev_path;
339         struct btrfs_ioctl_fs_info_args fi_args;
340         struct btrfs_ioctl_dev_info_args *di_args = NULL;
341         int ret;
342         int fdmnt;
343         int i;
344         int c;
345         int err = 0;
346         __u64 flags = 0;
347         DIR *dirstream = NULL;
348
349         optind = 1;
350         while ((c = getopt(argc, argv, "z")) != -1) {
351                 switch (c) {
352                 case 'z':
353                         flags = BTRFS_DEV_STATS_RESET;
354                         break;
355                 case '?':
356                 default:
357                         usage(cmd_dev_stats_usage);
358                 }
359         }
360
361         argc = argc - optind;
362         if (check_argc_exact(argc, 1))
363                 usage(cmd_dev_stats_usage);
364
365         dev_path = argv[optind];
366
367         fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
368
369         if (fdmnt < 0) {
370                 if (errno == EINVAL)
371                         fprintf(stderr,
372                                 "ERROR: '%s' is not a mounted btrfs device\n",
373                                 dev_path);
374                 else
375                         fprintf(stderr, "ERROR: can't access '%s': %s\n",
376                                 dev_path, strerror(errno));
377                 return 1;
378         }
379
380         ret = get_fs_info(dev_path, &fi_args, &di_args);
381         if (ret) {
382                 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
383                                 "%s\n", strerror(-ret));
384                 err = 1;
385                 goto out;
386         }
387         if (!fi_args.num_devices) {
388                 fprintf(stderr, "ERROR: no devices found\n");
389                 err = 1;
390                 goto out;
391         }
392
393         for (i = 0; i < fi_args.num_devices; i++) {
394                 struct btrfs_ioctl_get_dev_stats args = {0};
395                 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
396
397                 strncpy((char *)path, (char *)di_args[i].path,
398                         BTRFS_DEVICE_PATH_NAME_MAX);
399                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
400
401                 args.devid = di_args[i].devid;
402                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
403                 args.flags = flags;
404
405                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
406                         fprintf(stderr,
407                                 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
408                                 path, strerror(errno));
409                         err = 1;
410                 } else {
411                         char *canonical_path;
412
413                         canonical_path = canonicalize_path((char *)path);
414
415                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
416                                 printf("[%s].write_io_errs   %llu\n",
417                                        canonical_path,
418                                        (unsigned long long) args.values[
419                                         BTRFS_DEV_STAT_WRITE_ERRS]);
420                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
421                                 printf("[%s].read_io_errs    %llu\n",
422                                        canonical_path,
423                                        (unsigned long long) args.values[
424                                         BTRFS_DEV_STAT_READ_ERRS]);
425                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
426                                 printf("[%s].flush_io_errs   %llu\n",
427                                        canonical_path,
428                                        (unsigned long long) args.values[
429                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
430                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
431                                 printf("[%s].corruption_errs %llu\n",
432                                        canonical_path,
433                                        (unsigned long long) args.values[
434                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
435                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
436                                 printf("[%s].generation_errs %llu\n",
437                                        canonical_path,
438                                        (unsigned long long) args.values[
439                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
440
441                         free(canonical_path);
442                 }
443         }
444
445 out:
446         free(di_args);
447         close_file_or_dir(fdmnt, dirstream);
448
449         return err;
450 }
451
452 const char * const cmd_device_usage_usage[] = {
453         "btrfs device usage [options] <path> [<path>..]",
454         "Show detailed information about internal allocations in devices.",
455         "-b|--raw           raw numbers in bytes",
456         "-h|--human-readable",
457         "                   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 c;
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, GETOPT_VAL_SI},
513                         { "iec", no_argument, NULL, GETOPT_VAL_IEC},
514                         { "human-readable", no_argument, NULL,
515                                 GETOPT_VAL_HUMAN_READABLE},
516                         { NULL, 0, NULL, 0 }
517                 };
518
519                 c = getopt_long(argc, argv, "bhHkmgt", long_options, NULL);
520                 if (c < 0)
521                         break;
522                 switch (c) {
523                 case 'b':
524                         unit_mode = UNITS_RAW;
525                         break;
526                 case 'k':
527                         units_set_base(&unit_mode, UNITS_KBYTES);
528                         break;
529                 case 'm':
530                         units_set_base(&unit_mode, UNITS_MBYTES);
531                         break;
532                 case 'g':
533                         units_set_base(&unit_mode, UNITS_GBYTES);
534                         break;
535                 case 't':
536                         units_set_base(&unit_mode, UNITS_TBYTES);
537                         break;
538                 case GETOPT_VAL_HUMAN_READABLE:
539                 case 'h':
540                         unit_mode = UNITS_HUMAN_BINARY;
541                         break;
542                 case 'H':
543                         unit_mode = UNITS_HUMAN_DECIMAL;
544                         break;
545                 case GETOPT_VAL_SI:
546                         units_set_mode(&unit_mode, UNITS_DECIMAL);
547                         break;
548                 case GETOPT_VAL_IEC:
549                         units_set_mode(&unit_mode, UNITS_BINARY);
550                         break;
551                 default:
552                         usage(cmd_device_usage_usage);
553                 }
554         }
555
556         if (check_argc_min(argc - optind, 1))
557                 usage(cmd_device_usage_usage);
558
559         for (i = optind; i < argc ; i++) {
560                 int fd;
561                 DIR     *dirstream = NULL;
562                 if (more_than_one)
563                         printf("\n");
564
565                 fd = open_file_or_dir(argv[i], &dirstream);
566                 if (fd < 0) {
567                         fprintf(stderr, "ERROR: can't access '%s'\n",
568                                 argv[1]);
569                         ret = 1;
570                         goto out;
571                 }
572
573                 ret = _cmd_device_usage(fd, argv[i], unit_mode);
574                 close_file_or_dir(fd, dirstream);
575
576                 if (ret)
577                         goto out;
578                 more_than_one = 1;
579         }
580 out:
581         return !!ret;
582 }
583
584 static const char device_cmd_group_info[] =
585 "manage and query devices in the filesystem";
586
587 const struct cmd_group device_cmd_group = {
588         device_cmd_group_usage, device_cmd_group_info, {
589                 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
590                 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
591                 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
592                 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
593                 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
594                 { "usage", cmd_device_usage,
595                         cmd_device_usage_usage, NULL, 0 },
596                 NULL_CMD_STRUCT
597         }
598 };
599
600 int cmd_device(int argc, char **argv)
601 {
602         return handle_command_group(&device_cmd_group, argc, argv);
603 }