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