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