btrfs-progs: move device usage to cmds-device, more cleanups
[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-disk_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         char estr[100];
56
57         while (1) {
58                 int long_index;
59                 static struct option long_options[] = {
60                         { "nodiscard", optional_argument, NULL, 'K'},
61                         { "force", no_argument, NULL, 'f'},
62                         { 0, 0, 0, 0 }
63                 };
64                 int c = getopt_long(argc, argv, "Kf", long_options,
65                                         &long_index);
66                 if (c < 0)
67                         break;
68                 switch (c) {
69                 case 'K':
70                         discard = 0;
71                         break;
72                 case 'f':
73                         force = 1;
74                         break;
75                 default:
76                         usage(cmd_add_dev_usage);
77                 }
78         }
79
80         argc = argc - optind;
81
82         if (check_argc_min(argc, 2))
83                 usage(cmd_add_dev_usage);
84
85         mntpnt = argv[optind + argc - 1];
86
87         fdmnt = open_file_or_dir(mntpnt, &dirstream);
88         if (fdmnt < 0) {
89                 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
90                 return 1;
91         }
92
93         for (i = optind; i < optind + argc - 1; i++){
94                 struct btrfs_ioctl_vol_args ioctl_args;
95                 int     devfd, res;
96                 u64 dev_block_count = 0;
97                 int mixed = 0;
98                 char *path;
99
100                 res = test_dev_for_mkfs(argv[i], force, estr);
101                 if (res) {
102                         fprintf(stderr, "%s", estr);
103                         ret++;
104                         continue;
105                 }
106
107                 devfd = open(argv[i], O_RDWR);
108                 if (devfd < 0) {
109                         fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
110                         ret++;
111                         continue;
112                 }
113
114                 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
115                                            0, &mixed, discard);
116                 close(devfd);
117                 if (res) {
118                         ret++;
119                         goto error_out;
120                 }
121
122                 path = canonicalize_path(argv[i]);
123                 if (!path) {
124                         fprintf(stderr,
125                                 "ERROR: Could not canonicalize pathname '%s': %s\n",
126                                 argv[i], strerror(errno));
127                         ret++;
128                         goto error_out;
129                 }
130
131                 strncpy_null(ioctl_args.name, path);
132                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
133                 e = errno;
134                 if (res < 0) {
135                         fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
136                                 path, strerror(e));
137                         ret++;
138                 }
139                 free(path);
140         }
141
142 error_out:
143         close_file_or_dir(fdmnt, dirstream);
144         return !!ret;
145 }
146
147 static const char * const cmd_rm_dev_usage[] = {
148         "btrfs device delete <device> [<device>...] <path>",
149         "Remove a device from a filesystem",
150         NULL
151 };
152
153 static int cmd_rm_dev(int argc, char **argv)
154 {
155         char    *mntpnt;
156         int     i, fdmnt, ret=0, e;
157         DIR     *dirstream = NULL;
158
159         if (check_argc_min(argc, 3))
160                 usage(cmd_rm_dev_usage);
161
162         mntpnt = argv[argc - 1];
163
164         fdmnt = open_file_or_dir(mntpnt, &dirstream);
165         if (fdmnt < 0) {
166                 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
167                 return 1;
168         }
169
170         for(i=1 ; i < argc - 1; i++ ){
171                 struct  btrfs_ioctl_vol_args arg;
172                 int     res;
173
174                 if (!is_block_device(argv[i])) {
175                         fprintf(stderr,
176                                 "ERROR: %s is not a block device\n", argv[i]);
177                         ret++;
178                         continue;
179                 }
180                 strncpy_null(arg.name, argv[i]);
181                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
182                 e = errno;
183                 if (res > 0) {
184                         fprintf(stderr,
185                                 "ERROR: error removing the device '%s' - %s\n",
186                                 argv[i], btrfs_err_str(res));
187                         ret++;
188                 } else if (res < 0) {
189                         fprintf(stderr,
190                                 "ERROR: error removing the device '%s' - %s\n",
191                                 argv[i], strerror(e));
192                         ret++;
193                 }
194         }
195
196         close_file_or_dir(fdmnt, dirstream);
197         return !!ret;
198 }
199
200 static const char * const cmd_scan_dev_usage[] = {
201         "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
202         "Scan devices for a btrfs filesystem",
203         " -d|--all-devices (deprecated)",
204         NULL
205 };
206
207 static int cmd_scan_dev(int argc, char **argv)
208 {
209         int i;
210         int devstart = 1;
211         int all = 0;
212         int ret = 0;
213
214         optind = 1;
215         while (1) {
216                 int long_index;
217                 static struct option long_options[] = {
218                         { "all-devices", no_argument, NULL, 'd'},
219                         { 0, 0, 0, 0 },
220                 };
221                 int c = getopt_long(argc, argv, "d", long_options,
222                                     &long_index);
223                 if (c < 0)
224                         break;
225                 switch (c) {
226                 case 'd':
227                         all = 1;
228                         break;
229                 default:
230                         usage(cmd_scan_dev_usage);
231                 }
232         }
233
234         if (all && check_argc_max(argc, 2))
235                 usage(cmd_scan_dev_usage);
236
237         if (all || argc == 1) {
238                 printf("Scanning for Btrfs filesystems\n");
239                 ret = btrfs_scan_lblkid();
240                 if (ret)
241                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
242                 ret = btrfs_register_all_devices();
243                 if (ret)
244                         fprintf(stderr, "ERROR: error %d while registering\n", ret);
245                 goto out;
246         }
247
248         for( i = devstart ; i < argc ; i++ ){
249                 char *path;
250
251                 if (!is_block_device(argv[i])) {
252                         fprintf(stderr,
253                                 "ERROR: %s is not a block device\n", argv[i]);
254                         ret = 1;
255                         goto out;
256                 }
257                 path = canonicalize_path(argv[i]);
258                 if (!path) {
259                         fprintf(stderr,
260                                 "ERROR: Could not canonicalize path '%s': %s\n",
261                                 argv[i], strerror(errno));
262                         ret = 1;
263                         goto out;
264                 }
265                 printf("Scanning for Btrfs filesystems in '%s'\n", path);
266                 if (btrfs_register_one_device(path) != 0) {
267                         ret = 1;
268                         free(path);
269                         goto out;
270                 }
271                 free(path);
272         }
273
274 out:
275         return !!ret;
276 }
277
278 static const char * const cmd_ready_dev_usage[] = {
279         "btrfs device ready <device>",
280         "Check device to see if it has all of its devices in cache for mounting",
281         NULL
282 };
283
284 static int cmd_ready_dev(int argc, char **argv)
285 {
286         struct  btrfs_ioctl_vol_args args;
287         int     fd;
288         int     ret;
289         char    *path;
290
291         if (check_argc_min(argc, 2))
292                 usage(cmd_ready_dev_usage);
293
294         fd = open("/dev/btrfs-control", O_RDWR);
295         if (fd < 0) {
296                 perror("failed to open /dev/btrfs-control");
297                 return 1;
298         }
299
300         path = canonicalize_path(argv[argc - 1]);
301         if (!path) {
302                 fprintf(stderr,
303                         "ERROR: Could not canonicalize pathname '%s': %s\n",
304                         argv[argc - 1], strerror(errno));
305                 ret = 1;
306                 goto out;
307         }
308
309         if (!is_block_device(path)) {
310                 fprintf(stderr,
311                         "ERROR: %s is not a block device\n", path);
312                 ret = 1;
313                 goto out;
314         }
315
316         strncpy(args.name, path, BTRFS_PATH_NAME_MAX);
317         ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
318         if (ret < 0) {
319                 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
320                         " is ready for mounting - %s\n", path,
321                         strerror(errno));
322                 ret = 1;
323         }
324
325 out:
326         free(path);
327         close(fd);
328         return ret;
329 }
330
331 static const char * const cmd_dev_stats_usage[] = {
332         "btrfs device stats [-z] <path>|<device>",
333         "Show current device IO stats. -z to reset stats afterwards.",
334         NULL
335 };
336
337 static int cmd_dev_stats(int argc, char **argv)
338 {
339         char *dev_path;
340         struct btrfs_ioctl_fs_info_args fi_args;
341         struct btrfs_ioctl_dev_info_args *di_args = NULL;
342         int ret;
343         int fdmnt;
344         int i;
345         int c;
346         int err = 0;
347         __u64 flags = 0;
348         DIR *dirstream = NULL;
349
350         optind = 1;
351         while ((c = getopt(argc, argv, "z")) != -1) {
352                 switch (c) {
353                 case 'z':
354                         flags = BTRFS_DEV_STATS_RESET;
355                         break;
356                 case '?':
357                 default:
358                         usage(cmd_dev_stats_usage);
359                 }
360         }
361
362         argc = argc - optind;
363         if (check_argc_exact(argc, 1))
364                 usage(cmd_dev_stats_usage);
365
366         dev_path = argv[optind];
367
368         fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
369
370         if (fdmnt < 0) {
371                 if (errno == EINVAL)
372                         fprintf(stderr,
373                                 "ERROR: '%s' is not a mounted btrfs device\n",
374                                 dev_path);
375                 else
376                         fprintf(stderr, "ERROR: can't access '%s': %s\n",
377                                 dev_path, strerror(errno));
378                 return 1;
379         }
380
381         ret = get_fs_info(dev_path, &fi_args, &di_args);
382         if (ret) {
383                 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
384                                 "%s\n", strerror(-ret));
385                 err = 1;
386                 goto out;
387         }
388         if (!fi_args.num_devices) {
389                 fprintf(stderr, "ERROR: no devices found\n");
390                 err = 1;
391                 goto out;
392         }
393
394         for (i = 0; i < fi_args.num_devices; i++) {
395                 struct btrfs_ioctl_get_dev_stats args = {0};
396                 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
397
398                 strncpy((char *)path, (char *)di_args[i].path,
399                         BTRFS_DEVICE_PATH_NAME_MAX);
400                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
401
402                 args.devid = di_args[i].devid;
403                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
404                 args.flags = flags;
405
406                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
407                         fprintf(stderr,
408                                 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
409                                 path, strerror(errno));
410                         err = 1;
411                 } else {
412                         char *canonical_path;
413
414                         canonical_path = canonicalize_path((char *)path);
415
416                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
417                                 printf("[%s].write_io_errs   %llu\n",
418                                        canonical_path,
419                                        (unsigned long long) args.values[
420                                         BTRFS_DEV_STAT_WRITE_ERRS]);
421                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
422                                 printf("[%s].read_io_errs    %llu\n",
423                                        canonical_path,
424                                        (unsigned long long) args.values[
425                                         BTRFS_DEV_STAT_READ_ERRS]);
426                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
427                                 printf("[%s].flush_io_errs   %llu\n",
428                                        canonical_path,
429                                        (unsigned long long) args.values[
430                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
431                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
432                                 printf("[%s].corruption_errs %llu\n",
433                                        canonical_path,
434                                        (unsigned long long) args.values[
435                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
436                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
437                                 printf("[%s].generation_errs %llu\n",
438                                        canonical_path,
439                                        (unsigned long long) args.values[
440                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
441
442                         free(canonical_path);
443                 }
444         }
445
446 out:
447         free(di_args);
448         close_file_or_dir(fdmnt, dirstream);
449
450         return err;
451 }
452
453 const char * const cmd_device_usage_usage[] = {
454         "btrfs device usage [-b] <path> [<path>..]",
455         "Show which chunks are in a device.",
456         "",
457         "-b\tSet byte as unit",
458         NULL
459 };
460
461 static int _cmd_device_usage(int fd, char *path, int mode)
462 {
463         int i;
464         int ret = 0;
465         int info_count = 0;
466         struct chunk_info *info_ptr = 0;
467         struct device_info *device_info_ptr = 0;
468         int device_info_count = 0;
469
470         if (load_chunk_info(fd, &info_ptr, &info_count) ||
471             load_device_info(fd, &device_info_ptr, &device_info_count)) {
472                 ret = -1;
473                 goto exit;
474         }
475
476         for (i = 0; i < device_info_count; i++) {
477                 printf("%s\t%10s\n", device_info_ptr[i].path,
478                         df_pretty_sizes(device_info_ptr[i].size, mode));
479
480                 print_device_chunks(fd, device_info_ptr[i].devid,
481                                 device_info_ptr[i].size,
482                                 info_ptr, info_count,
483                                 mode);
484                 printf("\n");
485         }
486
487 exit:
488         if (device_info_ptr)
489                 free(device_info_ptr);
490         if (info_ptr)
491                 free(info_ptr);
492
493         return ret;
494 }
495
496 int cmd_device_usage(int argc, char **argv)
497 {
498
499         int     flags = DF_HUMAN_UNIT;
500         int     i, more_than_one = 0;
501
502         optind = 1;
503         while (1) {
504                 char    c = getopt(argc, argv, "b");
505
506                 if (c < 0)
507                         break;
508
509                 switch (c) {
510                 case 'b':
511                         flags &= ~DF_HUMAN_UNIT;
512                         break;
513                 default:
514                         usage(cmd_device_usage_usage);
515                 }
516         }
517
518         if (check_argc_min(argc - optind, 1))
519                 usage(cmd_device_usage_usage);
520
521         for (i = optind; i < argc ; i++) {
522                 int r, fd;
523                 DIR     *dirstream = NULL;
524                 if (more_than_one)
525                         printf("\n");
526
527                 fd = open_file_or_dir(argv[i], &dirstream);
528                 if (fd < 0) {
529                         fprintf(stderr, "ERROR: can't access to '%s'\n",
530                                 argv[1]);
531                         return 12;
532                 }
533                 r = _cmd_device_usage(fd, argv[i], flags);
534                 close_file_or_dir(fd, dirstream);
535
536                 if (r)
537                         return r;
538                 more_than_one = 1;
539
540         }
541
542         return 0;
543 }
544
545 const struct cmd_group device_cmd_group = {
546         device_cmd_group_usage, NULL, {
547                 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
548                 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
549                 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
550                 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
551                 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
552                 { "usage", cmd_device_usage,
553                         cmd_device_usage_usage, NULL, 0 },
554                 NULL_CMD_STRUCT
555         }
556 };
557
558 int cmd_device(int argc, char **argv)
559 {
560         return handle_command_group(&device_cmd_group, argc, argv);
561 }