btrfs-progs: Add missing close_ctree to btrfs-select-super.c
[btrfs-progs-unstable/devel.git] / cmds-device.c
blobc2f3a408a861e88f45760bd2fce2f0fb3cae9d36
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.
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.
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.
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>
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"
34 #include "commands.h"
36 static const char * const device_cmd_group_usage[] = {
37 "btrfs device <command> [<args>]",
38 NULL
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
49 static int cmd_device_add(int argc, char **argv)
51 char *mntpnt;
52 int i, fdmnt, ret=0, e;
53 DIR *dirstream = NULL;
54 int discard = 1;
55 int force = 0;
56 int last_dev;
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}
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);
81 if (check_argc_min(argc - optind, 2))
82 usage(cmd_device_add_usage);
84 last_dev = argc - 1;
85 mntpnt = argv[last_dev];
87 fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
88 if (fdmnt < 0)
89 return 1;
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;
97 res = test_dev_for_mkfs(argv[i], force);
98 if (res) {
99 ret++;
100 continue;
103 devfd = open(argv[i], O_RDWR);
104 if (devfd < 0) {
105 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
106 ret++;
107 continue;
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;
118 path = canonicalize_path(argv[i]);
119 if (!path) {
120 fprintf(stderr,
121 "ERROR: Could not canonicalize pathname '%s': %s\n",
122 argv[i], strerror(errno));
123 ret++;
124 goto error_out;
127 memset(&ioctl_args, 0, sizeof(ioctl_args));
128 strncpy_null(ioctl_args.name, path);
129 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
130 e = errno;
131 if (res < 0) {
132 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
133 path, strerror(e));
134 ret++;
136 free(path);
139 error_out:
140 close_file_or_dir(fdmnt, dirstream);
141 return !!ret;
144 static int _cmd_device_remove(int argc, char **argv,
145 const char * const *usagestr)
147 char *mntpnt;
148 int i, fdmnt, ret=0, e;
149 DIR *dirstream = NULL;
151 if (check_argc_min(argc, 3))
152 usage(usagestr);
154 mntpnt = argv[argc - 1];
156 fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
157 if (fdmnt < 0)
158 return 1;
160 for(i=1 ; i < argc - 1; i++ ){
161 struct btrfs_ioctl_vol_args arg;
162 int res;
164 if (is_block_device(argv[i]) != 1) {
165 fprintf(stderr,
166 "ERROR: %s is not a block device\n", argv[i]);
167 ret++;
168 continue;
170 memset(&arg, 0, sizeof(arg));
171 strncpy_null(arg.name, argv[i]);
172 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
173 e = errno;
174 if (res) {
175 const char *msg;
177 if (res > 0)
178 msg = btrfs_err_str(res);
179 else
180 msg = strerror(e);
181 fprintf(stderr,
182 "ERROR: error removing the device '%s' - %s\n",
183 argv[i], msg);
184 ret++;
188 close_file_or_dir(fdmnt, dirstream);
189 return !!ret;
192 static const char * const cmd_device_remove_usage[] = {
193 "btrfs device remove <device> [<device>...] <path>",
194 "Remove a device from a filesystem",
195 NULL
198 static int cmd_device_remove(int argc, char **argv)
200 return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
203 static const char * const cmd_device_delete_usage[] = {
204 "btrfs device delete <device> [<device>...] <path>",
205 "Remove a device from a filesystem",
206 NULL
209 static int cmd_device_delete(int argc, char **argv)
211 return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
214 static const char * const cmd_device_scan_usage[] = {
215 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
216 "Scan devices for a btrfs filesystem",
217 " -d|--all-devices (deprecated)",
218 NULL
221 static int cmd_device_scan(int argc, char **argv)
223 int i;
224 int devstart = 1;
225 int all = 0;
226 int ret = 0;
228 optind = 1;
229 while (1) {
230 int c;
231 static const struct option long_options[] = {
232 { "all-devices", no_argument, NULL, 'd'},
233 { NULL, 0, NULL, 0}
236 c = getopt_long(argc, argv, "d", long_options, NULL);
237 if (c < 0)
238 break;
239 switch (c) {
240 case 'd':
241 all = 1;
242 break;
243 default:
244 usage(cmd_device_scan_usage);
248 if (all && check_argc_max(argc, 2))
249 usage(cmd_device_scan_usage);
251 if (all || argc == 1) {
252 printf("Scanning for Btrfs filesystems\n");
253 ret = btrfs_scan_lblkid();
254 if (ret)
255 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
256 ret = btrfs_register_all_devices();
257 if (ret)
258 fprintf(stderr, "ERROR: error %d while registering\n", ret);
259 goto out;
262 for( i = devstart ; i < argc ; i++ ){
263 char *path;
265 if (is_block_device(argv[i]) != 1) {
266 fprintf(stderr,
267 "ERROR: %s is not a block device\n", argv[i]);
268 ret = 1;
269 goto out;
271 path = canonicalize_path(argv[i]);
272 if (!path) {
273 fprintf(stderr,
274 "ERROR: Could not canonicalize path '%s': %s\n",
275 argv[i], strerror(errno));
276 ret = 1;
277 goto out;
279 printf("Scanning for Btrfs filesystems in '%s'\n", path);
280 if (btrfs_register_one_device(path) != 0) {
281 ret = 1;
282 free(path);
283 goto out;
285 free(path);
288 out:
289 return !!ret;
292 static const char * const cmd_device_ready_usage[] = {
293 "btrfs device ready <device>",
294 "Check device to see if it has all of its devices in cache for mounting",
295 NULL
298 static int cmd_device_ready(int argc, char **argv)
300 struct btrfs_ioctl_vol_args args;
301 int fd;
302 int ret;
303 char *path;
305 if (check_argc_min(argc, 2))
306 usage(cmd_device_ready_usage);
308 fd = open("/dev/btrfs-control", O_RDWR);
309 if (fd < 0) {
310 perror("failed to open /dev/btrfs-control");
311 return 1;
314 path = canonicalize_path(argv[argc - 1]);
315 if (!path) {
316 fprintf(stderr,
317 "ERROR: Could not canonicalize pathname '%s': %s\n",
318 argv[argc - 1], strerror(errno));
319 ret = 1;
320 goto out;
323 if (is_block_device(path) != 1) {
324 fprintf(stderr,
325 "ERROR: %s is not a block device\n", path);
326 ret = 1;
327 goto out;
330 memset(&args, 0, sizeof(args));
331 strncpy_null(args.name, path);
332 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
333 if (ret < 0) {
334 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
335 " is ready for mounting - %s\n", path,
336 strerror(errno));
337 ret = 1;
340 out:
341 free(path);
342 close(fd);
343 return ret;
346 static const char * const cmd_device_stats_usage[] = {
347 "btrfs device stats [-z] <path>|<device>",
348 "Show current device IO stats.",
350 "-z show current stats and reset values to zero",
351 NULL
354 static int cmd_device_stats(int argc, char **argv)
356 char *dev_path;
357 struct btrfs_ioctl_fs_info_args fi_args;
358 struct btrfs_ioctl_dev_info_args *di_args = NULL;
359 int ret;
360 int fdmnt;
361 int i;
362 int c;
363 int err = 0;
364 __u64 flags = 0;
365 DIR *dirstream = NULL;
367 optind = 1;
368 while ((c = getopt(argc, argv, "z")) != -1) {
369 switch (c) {
370 case 'z':
371 flags = BTRFS_DEV_STATS_RESET;
372 break;
373 case '?':
374 default:
375 usage(cmd_device_stats_usage);
379 argc = argc - optind;
380 if (check_argc_exact(argc, 1))
381 usage(cmd_device_stats_usage);
383 dev_path = argv[optind];
385 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream, 1);
386 if (fdmnt < 0)
387 return 1;
389 ret = get_fs_info(dev_path, &fi_args, &di_args);
390 if (ret) {
391 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
392 "%s\n", strerror(-ret));
393 err = 1;
394 goto out;
396 if (!fi_args.num_devices) {
397 fprintf(stderr, "ERROR: no devices found\n");
398 err = 1;
399 goto out;
402 for (i = 0; i < fi_args.num_devices; i++) {
403 struct btrfs_ioctl_get_dev_stats args = {0};
404 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
406 strncpy((char *)path, (char *)di_args[i].path,
407 BTRFS_DEVICE_PATH_NAME_MAX);
408 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
410 args.devid = di_args[i].devid;
411 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
412 args.flags = flags;
414 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
415 fprintf(stderr,
416 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
417 path, strerror(errno));
418 err = 1;
419 } else {
420 char *canonical_path;
422 canonical_path = canonicalize_path((char *)path);
424 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
425 printf("[%s].write_io_errs %llu\n",
426 canonical_path,
427 (unsigned long long) args.values[
428 BTRFS_DEV_STAT_WRITE_ERRS]);
429 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
430 printf("[%s].read_io_errs %llu\n",
431 canonical_path,
432 (unsigned long long) args.values[
433 BTRFS_DEV_STAT_READ_ERRS]);
434 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
435 printf("[%s].flush_io_errs %llu\n",
436 canonical_path,
437 (unsigned long long) args.values[
438 BTRFS_DEV_STAT_FLUSH_ERRS]);
439 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
440 printf("[%s].corruption_errs %llu\n",
441 canonical_path,
442 (unsigned long long) args.values[
443 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
444 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
445 printf("[%s].generation_errs %llu\n",
446 canonical_path,
447 (unsigned long long) args.values[
448 BTRFS_DEV_STAT_GENERATION_ERRS]);
450 free(canonical_path);
454 out:
455 free(di_args);
456 close_file_or_dir(fdmnt, dirstream);
458 return err;
461 static const char * const cmd_device_usage_usage[] = {
462 "btrfs device usage [options] <path> [<path>..]",
463 "Show detailed information about internal allocations in devices.",
464 HELPINFO_OUTPUT_UNIT_DF,
465 NULL
468 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
470 int i;
471 int ret = 0;
472 struct chunk_info *chunkinfo = NULL;
473 struct device_info *devinfo = NULL;
474 int chunkcount = 0;
475 int devcount = 0;
477 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
478 &devcount);
479 if (ret)
480 goto out;
482 for (i = 0; i < devcount; i++) {
483 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
484 print_device_sizes(fd, &devinfo[i], unit_mode);
485 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
486 unit_mode);
487 printf("\n");
490 out:
491 free(devinfo);
492 free(chunkinfo);
494 return ret;
497 static int cmd_device_usage(int argc, char **argv)
499 unsigned unit_mode;
500 int ret = 0;
501 int more_than_one = 0;
502 int i;
504 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
506 if (check_argc_min(argc, 2) || argv[1][0] == '-')
507 usage(cmd_device_usage_usage);
509 for (i = 1; i < argc; i++) {
510 int fd;
511 DIR *dirstream = NULL;
513 if (more_than_one)
514 printf("\n");
516 fd = btrfs_open_dir(argv[i], &dirstream, 1);
517 if (fd < 0) {
518 ret = 1;
519 goto out;
522 ret = _cmd_device_usage(fd, argv[i], unit_mode);
523 close_file_or_dir(fd, dirstream);
525 if (ret)
526 goto out;
527 more_than_one = 1;
529 out:
530 return !!ret;
533 static const char device_cmd_group_info[] =
534 "manage and query devices in the filesystem";
536 const struct cmd_group device_cmd_group = {
537 device_cmd_group_usage, device_cmd_group_info, {
538 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
539 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
540 CMD_ALIAS },
541 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
542 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
543 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
544 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
545 { "usage", cmd_device_usage,
546 cmd_device_usage_usage, NULL, 0 },
547 NULL_CMD_STRUCT
551 int cmd_device(int argc, char **argv)
553 return handle_command_group(&device_cmd_group, argc, argv);