btrfs-progs: docs: update balance manual page
[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;
53         DIR     *dirstream = NULL;
54         int discard = 1;
55         int force = 0;
56         int last_dev;
57
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}
64                 };
65
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);
78                 }
79         }
80
81         if (check_argc_min(argc - optind, 2))
82                 usage(cmd_device_add_usage);
83
84         last_dev = argc - 1;
85         mntpnt = argv[last_dev];
86
87         fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
88         if (fdmnt < 0)
89                 return 1;
90
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;
96
97                 res = test_dev_for_mkfs(argv[i], force);
98                 if (res) {
99                         ret++;
100                         continue;
101                 }
102
103                 devfd = open(argv[i], O_RDWR);
104                 if (devfd < 0) {
105                         error("unable to open device '%s'", argv[i]);
106                         ret++;
107                         continue;
108                 }
109
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;
116                 }
117
118                 path = canonicalize_path(argv[i]);
119                 if (!path) {
120                         error("could not canonicalize pathname '%s': %s",
121                                 argv[i], strerror(errno));
122                         ret++;
123                         goto error_out;
124                 }
125
126                 memset(&ioctl_args, 0, sizeof(ioctl_args));
127                 strncpy_null(ioctl_args.name, path);
128                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
129                 if (res < 0) {
130                         error("error adding device '%s': %s",
131                                 path, strerror(errno));
132                         ret++;
133                 }
134                 free(path);
135         }
136
137 error_out:
138         close_file_or_dir(fdmnt, dirstream);
139         return !!ret;
140 }
141
142 static int _cmd_device_remove(int argc, char **argv,
143                 const char * const *usagestr)
144 {
145         char    *mntpnt;
146         int i, fdmnt, ret = 0;
147         DIR     *dirstream = NULL;
148
149         if (check_argc_min(argc, 3))
150                 usage(usagestr);
151
152         mntpnt = argv[argc - 1];
153
154         fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
155         if (fdmnt < 0)
156                 return 1;
157
158         for(i=1 ; i < argc - 1; i++ ){
159                 struct  btrfs_ioctl_vol_args arg;
160                 int     res;
161
162                 if (is_block_device(argv[i]) != 1 && strcmp(argv[i], "missing")) {
163                         error("not a block device: %s", argv[i]);
164                         ret++;
165                         continue;
166                 }
167                 memset(&arg, 0, sizeof(arg));
168                 strncpy_null(arg.name, argv[i]);
169                 /*
170                  * Positive values are from BTRFS_ERROR_DEV_*,
171                  * otherwise it's a generic error, one of errnos
172                  */
173                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
174                 if (res) {
175                         const char *msg;
176
177                         if (res > 0)
178                                 msg = btrfs_err_str(res);
179                         else
180                                 msg = strerror(errno);
181                         error("error removing device '%s': %s",
182                                 argv[i], msg);
183                         ret++;
184                 }
185         }
186
187         close_file_or_dir(fdmnt, dirstream);
188         return !!ret;
189 }
190
191 static const char * const cmd_device_remove_usage[] = {
192         "btrfs device remove <device> [<device>...] <path>",
193         "Remove a device from a filesystem",
194         NULL
195 };
196
197 static int cmd_device_remove(int argc, char **argv)
198 {
199         return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
200 }
201
202 static const char * const cmd_device_delete_usage[] = {
203         "btrfs device delete <device> [<device>...] <path>",
204         "Remove a device from a filesystem",
205         NULL
206 };
207
208 static int cmd_device_delete(int argc, char **argv)
209 {
210         return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
211 }
212
213 static const char * const cmd_device_scan_usage[] = {
214         "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
215         "Scan devices for a btrfs filesystem",
216         " -d|--all-devices (deprecated)",
217         NULL
218 };
219
220 static int cmd_device_scan(int argc, char **argv)
221 {
222         int i;
223         int devstart = 1;
224         int all = 0;
225         int ret = 0;
226
227         optind = 1;
228         while (1) {
229                 int c;
230                 static const struct option long_options[] = {
231                         { "all-devices", no_argument, NULL, 'd'},
232                         { NULL, 0, NULL, 0}
233                 };
234
235                 c = getopt_long(argc, argv, "d", long_options, NULL);
236                 if (c < 0)
237                         break;
238                 switch (c) {
239                 case 'd':
240                         all = 1;
241                         break;
242                 default:
243                         usage(cmd_device_scan_usage);
244                 }
245         }
246
247         if (all && check_argc_max(argc, 2))
248                 usage(cmd_device_scan_usage);
249
250         if (all || argc == 1) {
251                 printf("Scanning for Btrfs filesystems\n");
252                 ret = btrfs_scan_lblkid();
253                 error_on(ret, "error %d while scanning", ret);
254                 ret = btrfs_register_all_devices();
255                 error_on(ret, "error %d while registering devices", ret);
256                 goto out;
257         }
258
259         for( i = devstart ; i < argc ; i++ ){
260                 char *path;
261
262                 if (is_block_device(argv[i]) != 1) {
263                         error("not a block device: %s", argv[i]);
264                         ret = 1;
265                         goto out;
266                 }
267                 path = canonicalize_path(argv[i]);
268                 if (!path) {
269                         error("could not canonicalize path '%s': %s",
270                                 argv[i], strerror(errno));
271                         ret = 1;
272                         goto out;
273                 }
274                 printf("Scanning for Btrfs filesystems in '%s'\n", path);
275                 if (btrfs_register_one_device(path) != 0) {
276                         ret = 1;
277                         free(path);
278                         goto out;
279                 }
280                 free(path);
281         }
282
283 out:
284         return !!ret;
285 }
286
287 static const char * const cmd_device_ready_usage[] = {
288         "btrfs device ready <device>",
289         "Check device to see if it has all of its devices in cache for mounting",
290         NULL
291 };
292
293 static int cmd_device_ready(int argc, char **argv)
294 {
295         struct  btrfs_ioctl_vol_args args;
296         int     fd;
297         int     ret;
298         char    *path;
299
300         if (check_argc_min(argc, 2))
301                 usage(cmd_device_ready_usage);
302
303         fd = open("/dev/btrfs-control", O_RDWR);
304         if (fd < 0) {
305                 perror("failed to open /dev/btrfs-control");
306                 return 1;
307         }
308
309         path = canonicalize_path(argv[argc - 1]);
310         if (!path) {
311                 error("could not canonicalize pathname '%s': %s",
312                         argv[argc - 1], strerror(errno));
313                 ret = 1;
314                 goto out;
315         }
316
317         if (is_block_device(path) != 1) {
318                 error("not a block device: %s", path);
319                 ret = 1;
320                 goto out;
321         }
322
323         memset(&args, 0, sizeof(args));
324         strncpy_null(args.name, path);
325         ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
326         if (ret < 0) {
327                 error("unable to determine if device '%s' is ready for mount: %s",
328                         path, strerror(errno));
329                 ret = 1;
330         }
331
332 out:
333         free(path);
334         close(fd);
335         return ret;
336 }
337
338 static const char * const cmd_device_stats_usage[] = {
339         "btrfs device stats [-z] <path>|<device>",
340         "Show current device IO stats.",
341         "",
342         "-z                     show current stats and reset values to zero",
343         NULL
344 };
345
346 static int cmd_device_stats(int argc, char **argv)
347 {
348         char *dev_path;
349         struct btrfs_ioctl_fs_info_args fi_args;
350         struct btrfs_ioctl_dev_info_args *di_args = NULL;
351         int ret;
352         int fdmnt;
353         int i;
354         int c;
355         int err = 0;
356         __u64 flags = 0;
357         DIR *dirstream = NULL;
358
359         optind = 1;
360         while ((c = getopt(argc, argv, "z")) != -1) {
361                 switch (c) {
362                 case 'z':
363                         flags = BTRFS_DEV_STATS_RESET;
364                         break;
365                 case '?':
366                 default:
367                         usage(cmd_device_stats_usage);
368                 }
369         }
370
371         argc = argc - optind;
372         if (check_argc_exact(argc, 1))
373                 usage(cmd_device_stats_usage);
374
375         dev_path = argv[optind];
376
377         fdmnt = open_path_or_dev_mnt(dev_path, &dirstream, 1);
378         if (fdmnt < 0)
379                 return 1;
380
381         ret = get_fs_info(dev_path, &fi_args, &di_args);
382         if (ret) {
383                 error("getting dev info for devstats failed: %s",
384                         strerror(-ret));
385                 err = 1;
386                 goto out;
387         }
388         if (!fi_args.num_devices) {
389                 error("no devices found");
390                 err = 1;
391                 goto out;
392         }
393
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];
397
398                 strncpy((char *)path, (char *)di_args[i].path,
399                         BTRFS_DEVICE_PATH_NAME_MAX);
400                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
401
402                 args.devid = di_args[i].devid;
403                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
404                 args.flags = flags;
405
406                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
407                         error("DEV_STATS ioctl failed on %s: %s",
408                               path, strerror(errno));
409                         err = 1;
410                 } else {
411                         char *canonical_path;
412
413                         canonical_path = canonicalize_path((char *)path);
414
415                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
416                                 printf("[%s].write_io_errs   %llu\n",
417                                        canonical_path,
418                                        (unsigned long long) args.values[
419                                         BTRFS_DEV_STAT_WRITE_ERRS]);
420                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
421                                 printf("[%s].read_io_errs    %llu\n",
422                                        canonical_path,
423                                        (unsigned long long) args.values[
424                                         BTRFS_DEV_STAT_READ_ERRS]);
425                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
426                                 printf("[%s].flush_io_errs   %llu\n",
427                                        canonical_path,
428                                        (unsigned long long) args.values[
429                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
430                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
431                                 printf("[%s].corruption_errs %llu\n",
432                                        canonical_path,
433                                        (unsigned long long) args.values[
434                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
435                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
436                                 printf("[%s].generation_errs %llu\n",
437                                        canonical_path,
438                                        (unsigned long long) args.values[
439                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
440
441                         free(canonical_path);
442                 }
443         }
444
445 out:
446         free(di_args);
447         close_file_or_dir(fdmnt, dirstream);
448
449         return err;
450 }
451
452 static const char * const cmd_device_usage_usage[] = {
453         "btrfs device usage [options] <path> [<path>..]",
454         "Show detailed information about internal allocations in devices.",
455         HELPINFO_UNITS_SHORT_LONG,
456         NULL
457 };
458
459 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
460 {
461         int i;
462         int ret = 0;
463         struct chunk_info *chunkinfo = NULL;
464         struct device_info *devinfo = NULL;
465         int chunkcount = 0;
466         int devcount = 0;
467
468         ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
469                         &devcount);
470         if (ret)
471                 goto out;
472
473         for (i = 0; i < devcount; i++) {
474                 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
475                 print_device_sizes(fd, &devinfo[i], unit_mode);
476                 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
477                                 unit_mode);
478                 printf("\n");
479         }
480
481 out:
482         free(devinfo);
483         free(chunkinfo);
484
485         return ret;
486 }
487
488 static int cmd_device_usage(int argc, char **argv)
489 {
490         unsigned unit_mode;
491         int ret = 0;
492         int i;
493
494         unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
495
496         if (check_argc_min(argc, 2) || argv[1][0] == '-')
497                 usage(cmd_device_usage_usage);
498
499         for (i = 1; i < argc; i++) {
500                 int fd;
501                 DIR *dirstream = NULL;
502
503                 if (i > 1)
504                         printf("\n");
505
506                 fd = btrfs_open_dir(argv[i], &dirstream, 1);
507                 if (fd < 0) {
508                         ret = 1;
509                         break;
510                 }
511
512                 ret = _cmd_device_usage(fd, argv[i], unit_mode);
513                 close_file_or_dir(fd, dirstream);
514
515                 if (ret)
516                         break;
517         }
518
519         return !!ret;
520 }
521
522 static const char device_cmd_group_info[] =
523 "manage and query devices in the filesystem";
524
525 const struct cmd_group device_cmd_group = {
526         device_cmd_group_usage, device_cmd_group_info, {
527                 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
528                 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
529                         CMD_ALIAS },
530                 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
531                 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
532                 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
533                 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
534                 { "usage", cmd_device_usage,
535                         cmd_device_usage_usage, NULL, 0 },
536                 NULL_CMD_STRUCT
537         }
538 };
539
540 int cmd_device(int argc, char **argv)
541 {
542         return handle_command_group(&device_cmd_group, argc, argv);
543 }