btrfs-progs: fix wrong data ratio for raid56 in btrfs-file-usage
[btrfs-progs-unstable/devel.git] / cmds-device.c
blob33f1311a546c26570d70bb5f08e8990d9ba86932
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-disk_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 long_index;
59 static struct option long_options[] = {
60 { "nodiscard", optional_argument, NULL, 'K'},
61 { "force", no_argument, NULL, 'f'},
62 { 0, 0, 0, 0 }
64 int c = getopt_long(argc, argv, "Kf", long_options,
65 &long_index);
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 > 0) {
184 fprintf(stderr,
185 "ERROR: error removing the device '%s' - %s\n",
186 argv[i], btrfs_err_str(res));
187 ret++;
188 } else if (res < 0) {
189 fprintf(stderr,
190 "ERROR: error removing the device '%s' - %s\n",
191 argv[i], strerror(e));
192 ret++;
196 close_file_or_dir(fdmnt, dirstream);
197 return !!ret;
200 static const char * const cmd_scan_dev_usage[] = {
201 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
202 "Scan devices for a btrfs filesystem",
203 " -d|--all-devices (deprecated)",
204 NULL
207 static int cmd_scan_dev(int argc, char **argv)
209 int i;
210 int devstart = 1;
211 int all = 0;
212 int ret = 0;
214 optind = 1;
215 while (1) {
216 int long_index;
217 static struct option long_options[] = {
218 { "all-devices", no_argument, NULL, 'd'},
219 { 0, 0, 0, 0 },
221 int c = getopt_long(argc, argv, "d", long_options,
222 &long_index);
223 if (c < 0)
224 break;
225 switch (c) {
226 case 'd':
227 all = 1;
228 break;
229 default:
230 usage(cmd_scan_dev_usage);
234 if (all && check_argc_max(argc, 2))
235 usage(cmd_scan_dev_usage);
237 if (all || argc == 1) {
238 printf("Scanning for Btrfs filesystems\n");
239 ret = btrfs_scan_lblkid();
240 if (ret)
241 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
242 ret = btrfs_register_all_devices();
243 if (ret)
244 fprintf(stderr, "ERROR: error %d while registering\n", ret);
245 goto out;
248 for( i = devstart ; i < argc ; i++ ){
249 char *path;
251 if (!is_block_device(argv[i])) {
252 fprintf(stderr,
253 "ERROR: %s is not a block device\n", argv[i]);
254 ret = 1;
255 goto out;
257 path = canonicalize_path(argv[i]);
258 if (!path) {
259 fprintf(stderr,
260 "ERROR: Could not canonicalize path '%s': %s\n",
261 argv[i], strerror(errno));
262 ret = 1;
263 goto out;
265 printf("Scanning for Btrfs filesystems in '%s'\n", path);
266 if (btrfs_register_one_device(path) != 0) {
267 ret = 1;
268 free(path);
269 goto out;
271 free(path);
274 out:
275 return !!ret;
278 static const char * const cmd_ready_dev_usage[] = {
279 "btrfs device ready <device>",
280 "Check device to see if it has all of its devices in cache for mounting",
281 NULL
284 static int cmd_ready_dev(int argc, char **argv)
286 struct btrfs_ioctl_vol_args args;
287 int fd;
288 int ret;
289 char *path;
291 if (check_argc_min(argc, 2))
292 usage(cmd_ready_dev_usage);
294 fd = open("/dev/btrfs-control", O_RDWR);
295 if (fd < 0) {
296 perror("failed to open /dev/btrfs-control");
297 return 1;
300 path = canonicalize_path(argv[argc - 1]);
301 if (!path) {
302 fprintf(stderr,
303 "ERROR: Could not canonicalize pathname '%s': %s\n",
304 argv[argc - 1], strerror(errno));
305 ret = 1;
306 goto out;
309 if (!is_block_device(path)) {
310 fprintf(stderr,
311 "ERROR: %s is not a block device\n", path);
312 ret = 1;
313 goto out;
316 strncpy(args.name, path, BTRFS_PATH_NAME_MAX);
317 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
318 if (ret < 0) {
319 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
320 " is ready for mounting - %s\n", path,
321 strerror(errno));
322 ret = 1;
325 out:
326 free(path);
327 close(fd);
328 return ret;
331 static const char * const cmd_dev_stats_usage[] = {
332 "btrfs device stats [-z] <path>|<device>",
333 "Show current device IO stats. -z to reset stats afterwards.",
334 NULL
337 static int cmd_dev_stats(int argc, char **argv)
339 char *dev_path;
340 struct btrfs_ioctl_fs_info_args fi_args;
341 struct btrfs_ioctl_dev_info_args *di_args = NULL;
342 int ret;
343 int fdmnt;
344 int i;
345 int c;
346 int err = 0;
347 __u64 flags = 0;
348 DIR *dirstream = NULL;
350 optind = 1;
351 while ((c = getopt(argc, argv, "z")) != -1) {
352 switch (c) {
353 case 'z':
354 flags = BTRFS_DEV_STATS_RESET;
355 break;
356 case '?':
357 default:
358 usage(cmd_dev_stats_usage);
362 argc = argc - optind;
363 if (check_argc_exact(argc, 1))
364 usage(cmd_dev_stats_usage);
366 dev_path = argv[optind];
368 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
370 if (fdmnt < 0) {
371 if (errno == EINVAL)
372 fprintf(stderr,
373 "ERROR: '%s' is not a mounted btrfs device\n",
374 dev_path);
375 else
376 fprintf(stderr, "ERROR: can't access '%s': %s\n",
377 dev_path, strerror(errno));
378 return 1;
381 ret = get_fs_info(dev_path, &fi_args, &di_args);
382 if (ret) {
383 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
384 "%s\n", strerror(-ret));
385 err = 1;
386 goto out;
388 if (!fi_args.num_devices) {
389 fprintf(stderr, "ERROR: no devices found\n");
390 err = 1;
391 goto out;
394 for (i = 0; i < fi_args.num_devices; i++) {
395 struct btrfs_ioctl_get_dev_stats args = {0};
396 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
398 strncpy((char *)path, (char *)di_args[i].path,
399 BTRFS_DEVICE_PATH_NAME_MAX);
400 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
402 args.devid = di_args[i].devid;
403 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
404 args.flags = flags;
406 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
407 fprintf(stderr,
408 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
409 path, strerror(errno));
410 err = 1;
411 } else {
412 char *canonical_path;
414 canonical_path = canonicalize_path((char *)path);
416 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
417 printf("[%s].write_io_errs %llu\n",
418 canonical_path,
419 (unsigned long long) args.values[
420 BTRFS_DEV_STAT_WRITE_ERRS]);
421 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
422 printf("[%s].read_io_errs %llu\n",
423 canonical_path,
424 (unsigned long long) args.values[
425 BTRFS_DEV_STAT_READ_ERRS]);
426 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
427 printf("[%s].flush_io_errs %llu\n",
428 canonical_path,
429 (unsigned long long) args.values[
430 BTRFS_DEV_STAT_FLUSH_ERRS]);
431 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
432 printf("[%s].corruption_errs %llu\n",
433 canonical_path,
434 (unsigned long long) args.values[
435 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
436 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
437 printf("[%s].generation_errs %llu\n",
438 canonical_path,
439 (unsigned long long) args.values[
440 BTRFS_DEV_STAT_GENERATION_ERRS]);
442 free(canonical_path);
446 out:
447 free(di_args);
448 close_file_or_dir(fdmnt, dirstream);
450 return err;
453 const char * const cmd_device_usage_usage[] = {
454 "btrfs device usage [-b] <path> [<path>..]",
455 "Show which chunks are in a device.",
457 "-b\tSet byte as unit",
458 NULL
461 static int _cmd_device_usage(int fd, char *path, int mode)
463 int i;
464 int ret = 0;
465 struct chunk_info *chunkinfo = NULL;
466 struct device_info *devinfo = NULL;
467 int chunkcount = 0;
468 int devcount = 0;
470 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
471 &devcount);
472 if (ret)
473 goto out;
475 for (i = 0; i < devcount; i++) {
476 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
477 print_device_sizes(fd, &devinfo[i], mode);
478 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
479 mode);
480 printf("\n");
483 out:
484 free(devinfo);
485 free(chunkinfo);
487 return ret;
490 int cmd_device_usage(int argc, char **argv)
493 int mode = UNITS_HUMAN;
494 int ret = 0;
495 int i, more_than_one = 0;
497 optind = 1;
498 while (1) {
499 int c = getopt(argc, argv, "b");
501 if (c < 0)
502 break;
504 switch (c) {
505 case 'b':
506 mode = UNITS_RAW;
507 break;
508 default:
509 usage(cmd_device_usage_usage);
513 if (check_argc_min(argc - optind, 1))
514 usage(cmd_device_usage_usage);
516 for (i = optind; i < argc ; i++) {
517 int fd;
518 DIR *dirstream = NULL;
519 if (more_than_one)
520 printf("\n");
522 fd = open_file_or_dir(argv[i], &dirstream);
523 if (fd < 0) {
524 fprintf(stderr, "ERROR: can't access '%s'\n",
525 argv[1]);
526 ret = 1;
527 goto out;
530 ret = _cmd_device_usage(fd, argv[i], mode);
531 close_file_or_dir(fd, dirstream);
533 if (ret)
534 goto out;
535 more_than_one = 1;
537 out:
538 return !!ret;
541 const struct cmd_group device_cmd_group = {
542 device_cmd_group_usage, NULL, {
543 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
544 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
545 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
546 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
547 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
548 { "usage", cmd_device_usage,
549 cmd_device_usage_usage, NULL, 0 },
550 NULL_CMD_STRUCT
554 int cmd_device(int argc, char **argv)
556 return handle_command_group(&device_cmd_group, argc, argv);