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