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