btrfs-progs: convert: clean up blk_iterate_data handling wrt record_file_blocks
[btrfs-progs-unstable/devel.git] / cmds-device.c
blob1022656988c2e82e763f3490a830b5b278a39417
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 "cmds-fi-usage.h"
33 #include "commands.h"
35 static const char * const device_cmd_group_usage[] = {
36 "btrfs device <command> [<args>]",
37 NULL
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
48 static int cmd_add_dev(int argc, char **argv)
50 char *mntpnt;
51 int i, fdmnt, ret=0, e;
52 DIR *dirstream = NULL;
53 int discard = 1;
54 int force = 0;
55 char estr[100];
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}
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_add_dev_usage);
80 argc = argc - optind;
82 if (check_argc_min(argc, 2))
83 usage(cmd_add_dev_usage);
85 mntpnt = argv[optind + argc - 1];
87 fdmnt = open_file_or_dir(mntpnt, &dirstream);
88 if (fdmnt < 0) {
89 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
90 return 1;
93 for (i = optind; i < optind + argc - 1; i++){
94 struct btrfs_ioctl_vol_args ioctl_args;
95 int devfd, res;
96 u64 dev_block_count = 0;
97 int mixed = 0;
98 char *path;
100 res = test_dev_for_mkfs(argv[i], force, estr);
101 if (res) {
102 fprintf(stderr, "%s", estr);
103 ret++;
104 continue;
107 devfd = open(argv[i], O_RDWR);
108 if (devfd < 0) {
109 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
110 ret++;
111 continue;
114 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
115 0, &mixed, discard);
116 close(devfd);
117 if (res) {
118 ret++;
119 goto error_out;
122 path = canonicalize_path(argv[i]);
123 if (!path) {
124 fprintf(stderr,
125 "ERROR: Could not canonicalize pathname '%s': %s\n",
126 argv[i], strerror(errno));
127 ret++;
128 goto error_out;
131 strncpy_null(ioctl_args.name, path);
132 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
133 e = errno;
134 if (res < 0) {
135 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
136 path, strerror(e));
137 ret++;
139 free(path);
142 error_out:
143 close_file_or_dir(fdmnt, dirstream);
144 return !!ret;
147 static const char * const cmd_rm_dev_usage[] = {
148 "btrfs device delete <device> [<device>...] <path>",
149 "Remove a device from a filesystem",
150 NULL
153 static int cmd_rm_dev(int argc, char **argv)
155 char *mntpnt;
156 int i, fdmnt, ret=0, e;
157 DIR *dirstream = NULL;
159 if (check_argc_min(argc, 3))
160 usage(cmd_rm_dev_usage);
162 mntpnt = argv[argc - 1];
164 fdmnt = open_file_or_dir(mntpnt, &dirstream);
165 if (fdmnt < 0) {
166 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
167 return 1;
170 for(i=1 ; i < argc - 1; i++ ){
171 struct btrfs_ioctl_vol_args arg;
172 int res;
174 if (!is_block_device(argv[i])) {
175 fprintf(stderr,
176 "ERROR: %s is not a block device\n", argv[i]);
177 ret++;
178 continue;
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;
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++;
197 close_file_or_dir(fdmnt, dirstream);
198 return !!ret;
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
208 static int cmd_scan_dev(int argc, char **argv)
210 int i;
211 int devstart = 1;
212 int all = 0;
213 int ret = 0;
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}
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);
235 if (all && check_argc_max(argc, 2))
236 usage(cmd_scan_dev_usage);
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;
249 for( i = devstart ; i < argc ; i++ ){
250 char *path;
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;
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;
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;
272 free(path);
275 out:
276 return !!ret;
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
285 static int cmd_ready_dev(int argc, char **argv)
287 struct btrfs_ioctl_vol_args args;
288 int fd;
289 int ret;
290 char *path;
292 if (check_argc_min(argc, 2))
293 usage(cmd_ready_dev_usage);
295 fd = open("/dev/btrfs-control", O_RDWR);
296 if (fd < 0) {
297 perror("failed to open /dev/btrfs-control");
298 return 1;
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;
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;
317 strncpy(args.name, path, BTRFS_PATH_NAME_MAX);
318 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
319 if (ret < 0) {
320 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
321 " is ready for mounting - %s\n", path,
322 strerror(errno));
323 ret = 1;
326 out:
327 free(path);
328 close(fd);
329 return ret;
332 static const char * const cmd_dev_stats_usage[] = {
333 "btrfs device stats [-z] <path>|<device>",
334 "Show current device IO stats. -z to reset stats afterwards.",
335 NULL
338 static int cmd_dev_stats(int argc, char **argv)
340 char *dev_path;
341 struct btrfs_ioctl_fs_info_args fi_args;
342 struct btrfs_ioctl_dev_info_args *di_args = NULL;
343 int ret;
344 int fdmnt;
345 int i;
346 int c;
347 int err = 0;
348 __u64 flags = 0;
349 DIR *dirstream = NULL;
351 optind = 1;
352 while ((c = getopt(argc, argv, "z")) != -1) {
353 switch (c) {
354 case 'z':
355 flags = BTRFS_DEV_STATS_RESET;
356 break;
357 case '?':
358 default:
359 usage(cmd_dev_stats_usage);
363 argc = argc - optind;
364 if (check_argc_exact(argc, 1))
365 usage(cmd_dev_stats_usage);
367 dev_path = argv[optind];
369 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
371 if (fdmnt < 0) {
372 if (errno == EINVAL)
373 fprintf(stderr,
374 "ERROR: '%s' is not a mounted btrfs device\n",
375 dev_path);
376 else
377 fprintf(stderr, "ERROR: can't access '%s': %s\n",
378 dev_path, strerror(errno));
379 return 1;
382 ret = get_fs_info(dev_path, &fi_args, &di_args);
383 if (ret) {
384 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
385 "%s\n", strerror(-ret));
386 err = 1;
387 goto out;
389 if (!fi_args.num_devices) {
390 fprintf(stderr, "ERROR: no devices found\n");
391 err = 1;
392 goto out;
395 for (i = 0; i < fi_args.num_devices; i++) {
396 struct btrfs_ioctl_get_dev_stats args = {0};
397 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
399 strncpy((char *)path, (char *)di_args[i].path,
400 BTRFS_DEVICE_PATH_NAME_MAX);
401 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
403 args.devid = di_args[i].devid;
404 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
405 args.flags = flags;
407 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
408 fprintf(stderr,
409 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
410 path, strerror(errno));
411 err = 1;
412 } else {
413 char *canonical_path;
415 canonical_path = canonicalize_path((char *)path);
417 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
418 printf("[%s].write_io_errs %llu\n",
419 canonical_path,
420 (unsigned long long) args.values[
421 BTRFS_DEV_STAT_WRITE_ERRS]);
422 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
423 printf("[%s].read_io_errs %llu\n",
424 canonical_path,
425 (unsigned long long) args.values[
426 BTRFS_DEV_STAT_READ_ERRS]);
427 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
428 printf("[%s].flush_io_errs %llu\n",
429 canonical_path,
430 (unsigned long long) args.values[
431 BTRFS_DEV_STAT_FLUSH_ERRS]);
432 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
433 printf("[%s].corruption_errs %llu\n",
434 canonical_path,
435 (unsigned long long) args.values[
436 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
437 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
438 printf("[%s].generation_errs %llu\n",
439 canonical_path,
440 (unsigned long long) args.values[
441 BTRFS_DEV_STAT_GENERATION_ERRS]);
443 free(canonical_path);
447 out:
448 free(di_args);
449 close_file_or_dir(fdmnt, dirstream);
451 return err;
454 const char * const cmd_device_usage_usage[] = {
455 "btrfs device usage [options] <path> [<path>..]",
456 "Show detailed information about internal allocations in devices.",
457 "-b|--raw raw numbers in bytes",
458 "-h|--human-readable",
459 " human friendly numbers, base 1024 (default)",
460 "-H human friendly numbers, base 1000",
461 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
462 "--si use 1000 as a base (kB, MB, GB, TB)",
463 "-k|--kbytes show sizes in KiB, or kB with --si",
464 "-m|--mbytes show sizes in MiB, or MB with --si",
465 "-g|--gbytes show sizes in GiB, or GB with --si",
466 "-t|--tbytes show sizes in TiB, or TB with --si",
467 NULL
470 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
472 int i;
473 int ret = 0;
474 struct chunk_info *chunkinfo = NULL;
475 struct device_info *devinfo = NULL;
476 int chunkcount = 0;
477 int devcount = 0;
479 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
480 &devcount);
481 if (ret)
482 goto out;
484 for (i = 0; i < devcount; i++) {
485 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
486 print_device_sizes(fd, &devinfo[i], unit_mode);
487 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
488 unit_mode);
489 printf("\n");
492 out:
493 free(devinfo);
494 free(chunkinfo);
496 return ret;
499 int cmd_device_usage(int argc, char **argv)
501 unsigned unit_mode = UNITS_DEFAULT;
502 int ret = 0;
503 int i, more_than_one = 0;
505 optind = 1;
506 while (1) {
507 int c;
508 static const struct option long_options[] = {
509 { "raw", no_argument, NULL, 'b'},
510 { "kbytes", no_argument, NULL, 'k'},
511 { "mbytes", no_argument, NULL, 'm'},
512 { "gbytes", no_argument, NULL, 'g'},
513 { "tbytes", no_argument, NULL, 't'},
514 { "si", no_argument, NULL, GETOPT_VAL_SI},
515 { "iec", no_argument, NULL, GETOPT_VAL_IEC},
516 { "human-readable", no_argument, NULL,
517 GETOPT_VAL_HUMAN_READABLE},
518 { NULL, 0, NULL, 0 }
521 c = getopt_long(argc, argv, "bhHkmgt", long_options, NULL);
522 if (c < 0)
523 break;
524 switch (c) {
525 case 'b':
526 unit_mode = UNITS_RAW;
527 break;
528 case 'k':
529 units_set_base(&unit_mode, UNITS_KBYTES);
530 break;
531 case 'm':
532 units_set_base(&unit_mode, UNITS_MBYTES);
533 break;
534 case 'g':
535 units_set_base(&unit_mode, UNITS_GBYTES);
536 break;
537 case 't':
538 units_set_base(&unit_mode, UNITS_TBYTES);
539 break;
540 case GETOPT_VAL_HUMAN_READABLE:
541 case 'h':
542 unit_mode = UNITS_HUMAN_BINARY;
543 break;
544 case 'H':
545 unit_mode = UNITS_HUMAN_DECIMAL;
546 break;
547 case GETOPT_VAL_SI:
548 units_set_mode(&unit_mode, UNITS_DECIMAL);
549 break;
550 case GETOPT_VAL_IEC:
551 units_set_mode(&unit_mode, UNITS_BINARY);
552 break;
553 default:
554 usage(cmd_device_usage_usage);
558 if (check_argc_min(argc - optind, 1))
559 usage(cmd_device_usage_usage);
561 for (i = optind; i < argc ; i++) {
562 int fd;
563 DIR *dirstream = NULL;
564 if (more_than_one)
565 printf("\n");
567 fd = open_file_or_dir(argv[i], &dirstream);
568 if (fd < 0) {
569 fprintf(stderr, "ERROR: can't access '%s'\n",
570 argv[1]);
571 ret = 1;
572 goto out;
575 ret = _cmd_device_usage(fd, argv[i], unit_mode);
576 close_file_or_dir(fd, dirstream);
578 if (ret)
579 goto out;
580 more_than_one = 1;
582 out:
583 return !!ret;
586 const struct cmd_group device_cmd_group = {
587 device_cmd_group_usage, NULL, {
588 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
589 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
590 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
591 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
592 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
593 { "usage", cmd_device_usage,
594 cmd_device_usage_usage, NULL, 0 },
595 NULL_CMD_STRUCT
599 int cmd_device(int argc, char **argv)
601 return handle_command_group(&device_cmd_group, argc, argv);