btrfs-progs: check sscanf return code
[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
32 #include "commands.h"
33
34 static const char * const device_cmd_group_usage[] = {
35         "btrfs device <command> [<args>]",
36         NULL
37 };
38
39 static const char * const cmd_add_dev_usage[] = {
40         "btrfs device add [options] <device> [<device>...] <path>",
41         "Add a device to a filesystem",
42         "-K|--nodiscard    do not perform whole device TRIM",
43         "-f|--force        force overwrite existing filesystem on the disk",
44         NULL
45 };
46
47 static int cmd_add_dev(int argc, char **argv)
48 {
49         char    *mntpnt;
50         int     i, fdmnt, ret=0, e;
51         DIR     *dirstream = NULL;
52         int discard = 1;
53         int force = 0;
54         char estr[100];
55
56         while (1) {
57                 int long_index;
58                 static struct option long_options[] = {
59                         { "nodiscard", optional_argument, NULL, 'K'},
60                         { "force", no_argument, NULL, 'f'},
61                         { 0, 0, 0, 0 }
62                 };
63                 int c = getopt_long(argc, argv, "Kf", long_options,
64                                         &long_index);
65                 if (c < 0)
66                         break;
67                 switch (c) {
68                 case 'K':
69                         discard = 0;
70                         break;
71                 case 'f':
72                         force = 1;
73                         break;
74                 default:
75                         usage(cmd_add_dev_usage);
76                 }
77         }
78
79         argc = argc - optind;
80
81         if (check_argc_min(argc, 2))
82                 usage(cmd_add_dev_usage);
83
84         mntpnt = argv[optind + argc - 1];
85
86         fdmnt = open_file_or_dir(mntpnt, &dirstream);
87         if (fdmnt < 0) {
88                 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
89                 return 1;
90         }
91
92         for (i = optind; i < optind + argc - 1; i++){
93                 struct btrfs_ioctl_vol_args ioctl_args;
94                 int     devfd, res;
95                 u64 dev_block_count = 0;
96                 int mixed = 0;
97                 char *path;
98
99                 res = test_dev_for_mkfs(argv[i], force, estr);
100                 if (res) {
101                         fprintf(stderr, "%s", estr);
102                         ret++;
103                         continue;
104                 }
105
106                 devfd = open(argv[i], O_RDWR);
107                 if (devfd < 0) {
108                         fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
109                         ret++;
110                         continue;
111                 }
112
113                 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
114                                            0, &mixed, discard);
115                 close(devfd);
116                 if (res) {
117                         ret++;
118                         goto error_out;
119                 }
120
121                 path = canonicalize_path(argv[i]);
122                 if (!path) {
123                         fprintf(stderr,
124                                 "ERROR: Could not canonicalize pathname '%s': %s\n",
125                                 argv[i], strerror(errno));
126                         ret++;
127                         goto error_out;
128                 }
129
130                 strncpy_null(ioctl_args.name, path);
131                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
132                 e = errno;
133                 if (res < 0) {
134                         fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
135                                 path, strerror(e));
136                         ret++;
137                 }
138                 free(path);
139         }
140
141 error_out:
142         close_file_or_dir(fdmnt, dirstream);
143         return !!ret;
144 }
145
146 static const char * const cmd_rm_dev_usage[] = {
147         "btrfs device delete <device> [<device>...] <path>",
148         "Remove a device from a filesystem",
149         NULL
150 };
151
152 static int cmd_rm_dev(int argc, char **argv)
153 {
154         char    *mntpnt;
155         int     i, fdmnt, ret=0, e;
156         DIR     *dirstream = NULL;
157
158         if (check_argc_min(argc, 3))
159                 usage(cmd_rm_dev_usage);
160
161         mntpnt = argv[argc - 1];
162
163         fdmnt = open_file_or_dir(mntpnt, &dirstream);
164         if (fdmnt < 0) {
165                 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
166                 return 1;
167         }
168
169         for(i=1 ; i < argc - 1; i++ ){
170                 struct  btrfs_ioctl_vol_args arg;
171                 int     res;
172
173                 if (!is_block_device(argv[i])) {
174                         fprintf(stderr,
175                                 "ERROR: %s is not a block device\n", argv[i]);
176                         ret++;
177                         continue;
178                 }
179                 strncpy_null(arg.name, argv[i]);
180                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
181                 e = errno;
182                 if (res > 0) {
183                         fprintf(stderr,
184                                 "ERROR: error removing the device '%s' - %s\n",
185                                 argv[i], btrfs_err_str(res));
186                         ret++;
187                 } else if (res < 0) {
188                         fprintf(stderr,
189                                 "ERROR: error removing the device '%s' - %s\n",
190                                 argv[i], strerror(e));
191                         ret++;
192                 }
193         }
194
195         close_file_or_dir(fdmnt, dirstream);
196         return !!ret;
197 }
198
199 static const char * const cmd_scan_dev_usage[] = {
200         "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
201         "Scan devices for a btrfs filesystem",
202         " -d|--all-devices (deprecated)",
203         NULL
204 };
205
206 static int cmd_scan_dev(int argc, char **argv)
207 {
208         int i, fd, e;
209         int devstart = 1;
210         int all = 0;
211         int ret = 0;
212
213         optind = 1;
214         while (1) {
215                 int long_index;
216                 static struct option long_options[] = {
217                         { "all-devices", no_argument, NULL, 'd'},
218                         { 0, 0, 0, 0 },
219                 };
220                 int c = getopt_long(argc, argv, "d", long_options,
221                                     &long_index);
222                 if (c < 0)
223                         break;
224                 switch (c) {
225                 case 'd':
226                         all = 1;
227                         break;
228                 default:
229                         usage(cmd_scan_dev_usage);
230                 }
231         }
232
233         if (all && check_argc_max(argc, 2))
234                 usage(cmd_scan_dev_usage);
235
236         if (all || argc == 1) {
237                 printf("Scanning for Btrfs filesystems\n");
238                 ret = btrfs_scan_lblkid(BTRFS_UPDATE_KERNEL);
239                 if (ret)
240                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
241                 goto out;
242         }
243
244         fd = open("/dev/btrfs-control", O_RDWR);
245         if (fd < 0) {
246                 perror("failed to open /dev/btrfs-control");
247                 ret = 1;
248                 goto out;
249         }
250
251         for( i = devstart ; i < argc ; i++ ){
252                 struct btrfs_ioctl_vol_args args;
253                 char *path;
254
255                 if (!is_block_device(argv[i])) {
256                         fprintf(stderr,
257                                 "ERROR: %s is not a block device\n", argv[i]);
258                         ret = 1;
259                         goto close_out;
260                 }
261                 path = canonicalize_path(argv[i]);
262                 if (!path) {
263                         fprintf(stderr,
264                                 "ERROR: Could not canonicalize path '%s': %s\n",
265                                 argv[i], strerror(errno));
266                         ret = 1;
267                         goto close_out;
268                 }
269                 printf("Scanning for Btrfs filesystems in '%s'\n", path);
270
271                 strncpy_null(args.name, path);
272                 /*
273                  * FIXME: which are the error code returned by this ioctl ?
274                  * it seems that is impossible to understand if there no is
275                  * a btrfs filesystem from an I/O error !!!
276                  */
277                 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
278                 e = errno;
279
280                 if( ret < 0 ){
281                         fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
282                                 path, strerror(e));
283                         free(path);
284                         goto close_out;
285                 }
286                 free(path);
287         }
288
289 close_out:
290         close(fd);
291 out:
292         return !!ret;
293 }
294
295 static const char * const cmd_ready_dev_usage[] = {
296         "btrfs device ready <device>",
297         "Check device to see if it has all of its devices in cache for mounting",
298         NULL
299 };
300
301 static int cmd_ready_dev(int argc, char **argv)
302 {
303         struct  btrfs_ioctl_vol_args args;
304         int     fd;
305         int     ret;
306         char    *path;
307
308         if (check_argc_min(argc, 2))
309                 usage(cmd_ready_dev_usage);
310
311         fd = open("/dev/btrfs-control", O_RDWR);
312         if (fd < 0) {
313                 perror("failed to open /dev/btrfs-control");
314                 return 1;
315         }
316
317         path = canonicalize_path(argv[argc - 1]);
318         if (!path) {
319                 fprintf(stderr,
320                         "ERROR: Could not canonicalize pathname '%s': %s\n",
321                         argv[argc - 1], strerror(errno));
322                 ret = 1;
323                 goto out;
324         }
325
326         if (!is_block_device(path)) {
327                 fprintf(stderr,
328                         "ERROR: %s is not a block device\n", path);
329                 ret = 1;
330                 goto out;
331         }
332
333         strncpy(args.name, path, BTRFS_PATH_NAME_MAX);
334         ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
335         if (ret < 0) {
336                 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
337                         " is ready for mounting - %s\n", path,
338                         strerror(errno));
339                 ret = 1;
340         }
341
342 out:
343         free(path);
344         close(fd);
345         return ret;
346 }
347
348 static const char * const cmd_dev_stats_usage[] = {
349         "btrfs device stats [-z] <path>|<device>",
350         "Show current device IO stats. -z to reset stats afterwards.",
351         NULL
352 };
353
354 static int cmd_dev_stats(int argc, char **argv)
355 {
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;
366
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_dev_stats_usage);
376                 }
377         }
378
379         argc = argc - optind;
380         if (check_argc_exact(argc, 1))
381                 usage(cmd_dev_stats_usage);
382
383         dev_path = argv[optind];
384
385         fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
386
387         if (fdmnt < 0) {
388                 if (errno == EINVAL)
389                         fprintf(stderr,
390                                 "ERROR: '%s' is not a mounted btrfs device\n",
391                                 dev_path);
392                 else
393                         fprintf(stderr, "ERROR: can't access '%s': %s\n",
394                                 dev_path, strerror(errno));
395                 return 1;
396         }
397
398         ret = get_fs_info(dev_path, &fi_args, &di_args);
399         if (ret) {
400                 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
401                                 "%s\n", strerror(-ret));
402                 err = 1;
403                 goto out;
404         }
405         if (!fi_args.num_devices) {
406                 fprintf(stderr, "ERROR: no devices found\n");
407                 err = 1;
408                 goto out;
409         }
410
411         for (i = 0; i < fi_args.num_devices; i++) {
412                 struct btrfs_ioctl_get_dev_stats args = {0};
413                 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
414
415                 strncpy((char *)path, (char *)di_args[i].path,
416                         BTRFS_DEVICE_PATH_NAME_MAX);
417                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
418
419                 args.devid = di_args[i].devid;
420                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
421                 args.flags = flags;
422
423                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
424                         fprintf(stderr,
425                                 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
426                                 path, strerror(errno));
427                         err = 1;
428                 } else {
429                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
430                                 printf("[%s].write_io_errs   %llu\n",
431                                        path,
432                                        (unsigned long long) args.values[
433                                         BTRFS_DEV_STAT_WRITE_ERRS]);
434                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
435                                 printf("[%s].read_io_errs    %llu\n",
436                                        path,
437                                        (unsigned long long) args.values[
438                                         BTRFS_DEV_STAT_READ_ERRS]);
439                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
440                                 printf("[%s].flush_io_errs   %llu\n",
441                                        path,
442                                        (unsigned long long) args.values[
443                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
444                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
445                                 printf("[%s].corruption_errs %llu\n",
446                                        path,
447                                        (unsigned long long) args.values[
448                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
449                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
450                                 printf("[%s].generation_errs %llu\n",
451                                        path,
452                                        (unsigned long long) args.values[
453                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
454                 }
455         }
456
457 out:
458         free(di_args);
459         close_file_or_dir(fdmnt, dirstream);
460
461         return err;
462 }
463
464 const struct cmd_group device_cmd_group = {
465         device_cmd_group_usage, NULL, {
466                 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
467                 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
468                 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
469                 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
470                 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
471                 NULL_CMD_STRUCT
472         }
473 };
474
475 int cmd_device(int argc, char **argv)
476 {
477         return handle_command_group(&device_cmd_group, argc, argv);
478 }