btrfs-progs: add getopt stubs where needed
[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, 2))
250                 usage(cmd_device_scan_usage);
251
252         if (all || argc == 1) {
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, "error %d 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         argc = argc - optind;
376         if (check_argc_exact(argc, 1))
377                 usage(cmd_device_stats_usage);
378
379         dev_path = argv[optind];
380
381         fdmnt = open_path_or_dev_mnt(dev_path, &dirstream, 1);
382         if (fdmnt < 0)
383                 return 1;
384
385         ret = get_fs_info(dev_path, &fi_args, &di_args);
386         if (ret) {
387                 error("getting dev info for devstats failed: %s",
388                         strerror(-ret));
389                 err = 1;
390                 goto out;
391         }
392         if (!fi_args.num_devices) {
393                 error("no devices found");
394                 err = 1;
395                 goto out;
396         }
397
398         for (i = 0; i < fi_args.num_devices; i++) {
399                 struct btrfs_ioctl_get_dev_stats args = {0};
400                 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
401
402                 strncpy((char *)path, (char *)di_args[i].path,
403                         BTRFS_DEVICE_PATH_NAME_MAX);
404                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
405
406                 args.devid = di_args[i].devid;
407                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
408                 args.flags = flags;
409
410                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
411                         error("DEV_STATS ioctl failed on %s: %s",
412                               path, strerror(errno));
413                         err = 1;
414                 } else {
415                         char *canonical_path;
416
417                         canonical_path = canonicalize_path((char *)path);
418
419                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
420                                 printf("[%s].write_io_errs   %llu\n",
421                                        canonical_path,
422                                        (unsigned long long) args.values[
423                                         BTRFS_DEV_STAT_WRITE_ERRS]);
424                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
425                                 printf("[%s].read_io_errs    %llu\n",
426                                        canonical_path,
427                                        (unsigned long long) args.values[
428                                         BTRFS_DEV_STAT_READ_ERRS]);
429                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
430                                 printf("[%s].flush_io_errs   %llu\n",
431                                        canonical_path,
432                                        (unsigned long long) args.values[
433                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
434                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
435                                 printf("[%s].corruption_errs %llu\n",
436                                        canonical_path,
437                                        (unsigned long long) args.values[
438                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
439                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
440                                 printf("[%s].generation_errs %llu\n",
441                                        canonical_path,
442                                        (unsigned long long) args.values[
443                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
444
445                         free(canonical_path);
446                 }
447         }
448
449 out:
450         free(di_args);
451         close_file_or_dir(fdmnt, dirstream);
452
453         return err;
454 }
455
456 static const char * const cmd_device_usage_usage[] = {
457         "btrfs device usage [options] <path> [<path>..]",
458         "Show detailed information about internal allocations in devices.",
459         HELPINFO_UNITS_SHORT_LONG,
460         NULL
461 };
462
463 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
464 {
465         int i;
466         int ret = 0;
467         struct chunk_info *chunkinfo = NULL;
468         struct device_info *devinfo = NULL;
469         int chunkcount = 0;
470         int devcount = 0;
471
472         ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
473                         &devcount);
474         if (ret)
475                 goto out;
476
477         for (i = 0; i < devcount; i++) {
478                 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
479                 print_device_sizes(fd, &devinfo[i], unit_mode);
480                 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
481                                 unit_mode);
482                 printf("\n");
483         }
484
485 out:
486         free(devinfo);
487         free(chunkinfo);
488
489         return ret;
490 }
491
492 static int cmd_device_usage(int argc, char **argv)
493 {
494         unsigned unit_mode;
495         int ret = 0;
496         int i;
497
498         unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
499
500         clean_args_no_options(argc, argv, cmd_device_usage_usage);
501
502         if (check_argc_min(argc - optind, 1))
503                 usage(cmd_device_usage_usage);
504
505         for (i = optind; i < argc; i++) {
506                 int fd;
507                 DIR *dirstream = NULL;
508
509                 if (i > 1)
510                         printf("\n");
511
512                 fd = btrfs_open_dir(argv[i], &dirstream, 1);
513                 if (fd < 0) {
514                         ret = 1;
515                         break;
516                 }
517
518                 ret = _cmd_device_usage(fd, argv[i], unit_mode);
519                 close_file_or_dir(fd, dirstream);
520
521                 if (ret)
522                         break;
523         }
524
525         return !!ret;
526 }
527
528 static const char device_cmd_group_info[] =
529 "manage and query devices in the filesystem";
530
531 const struct cmd_group device_cmd_group = {
532         device_cmd_group_usage, device_cmd_group_info, {
533                 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
534                 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
535                         CMD_ALIAS },
536                 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
537                 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
538                 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
539                 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
540                 { "usage", cmd_device_usage,
541                         cmd_device_usage_usage, NULL, 0 },
542                 NULL_CMD_STRUCT
543         }
544 };
545
546 int cmd_device(int argc, char **argv)
547 {
548         return handle_command_group(&device_cmd_group, argc, argv);
549 }