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