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