Btrfs-progs: add sanity checks for btrfs device operations
[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 to '%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
98                 res = test_dev_for_mkfs(argv[i], force, estr);
99                 if (res) {
100                         fprintf(stderr, "%s", estr);
101                         ret++;
102                         continue;
103                 }
104
105                 devfd = open(argv[i], O_RDWR);
106                 if (devfd < 0) {
107                         fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
108                         ret++;
109                         continue;
110                 }
111
112                 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
113                                            0, &mixed, discard);
114                 if (res) {
115                         fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]);
116                         close(devfd);
117                         ret++;
118                         continue;
119                 }
120                 close(devfd);
121
122                 strncpy_null(ioctl_args.name, argv[i]);
123                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
124                 e = errno;
125                 if(res<0){
126                         fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
127                                 argv[i], strerror(e));
128                         ret++;
129                 }
130
131         }
132
133         close_file_or_dir(fdmnt, dirstream);
134         return !!ret;
135 }
136
137 static const char * const cmd_rm_dev_usage[] = {
138         "btrfs device delete <device> [<device>...] <path>",
139         "Remove a device from a filesystem",
140         NULL
141 };
142
143 static int cmd_rm_dev(int argc, char **argv)
144 {
145         char    *mntpnt;
146         int     i, fdmnt, ret=0, e;
147         DIR     *dirstream = NULL;
148
149         if (check_argc_min(argc, 3))
150                 usage(cmd_rm_dev_usage);
151
152         mntpnt = argv[argc - 1];
153
154         fdmnt = open_file_or_dir(mntpnt, &dirstream);
155         if (fdmnt < 0) {
156                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
157                 return 1;
158         }
159
160         for(i=1 ; i < argc - 1; i++ ){
161                 struct  btrfs_ioctl_vol_args arg;
162                 int     res;
163
164                 if (!is_block_device(argv[i])) {
165                         fprintf(stderr,
166                                 "ERROR: %s is not a block device\n", argv[i]);
167                         ret++;
168                         continue;
169                 }
170                 strncpy_null(arg.name, argv[i]);
171                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
172                 e = errno;
173                 if (res > 0) {
174                         fprintf(stderr,
175                                 "ERROR: error removing the device '%s' - %s\n",
176                                 argv[i], btrfs_err_str(res));
177                         ret++;
178                 } else if (res < 0) {
179                         fprintf(stderr,
180                                 "ERROR: error removing the device '%s' - %s\n",
181                                 argv[i], strerror(e));
182                         ret++;
183                 }
184         }
185
186         close_file_or_dir(fdmnt, dirstream);
187         return !!ret;
188 }
189
190 static const char * const cmd_scan_dev_usage[] = {
191         "btrfs device scan [<--all-devices>|<device> [<device>...]]",
192         "Scan devices for a btrfs filesystem",
193         NULL
194 };
195
196 static int cmd_scan_dev(int argc, char **argv)
197 {
198         int     i, fd, e;
199         int     where = BTRFS_SCAN_LBLKID;
200         int     devstart = 1;
201
202         if( argc > 1 && !strcmp(argv[1],"--all-devices")){
203                 if (check_argc_max(argc, 2))
204                         usage(cmd_scan_dev_usage);
205
206                 where = BTRFS_SCAN_DEV;
207                 devstart += 1;
208         }
209
210         if(argc<=devstart){
211                 int ret;
212                 printf("Scanning for Btrfs filesystems\n");
213                 ret = scan_for_btrfs(where, BTRFS_UPDATE_KERNEL);
214                 if (ret){
215                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
216                         return 1;
217                 }
218                 return 0;
219         }
220
221         fd = open("/dev/btrfs-control", O_RDWR);
222         if (fd < 0) {
223                 perror("failed to open /dev/btrfs-control");
224                 return 1;
225         }
226
227         for( i = devstart ; i < argc ; i++ ){
228                 struct btrfs_ioctl_vol_args args;
229                 int ret;
230
231                 if (!is_block_device(argv[i])) {
232                         fprintf(stderr,
233                                 "ERROR: %s is not a block device\n", argv[i]);
234                         close(fd);
235                         return 1;
236                 }
237                 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
238
239                 strncpy_null(args.name, argv[i]);
240                 /*
241                  * FIXME: which are the error code returned by this ioctl ?
242                  * it seems that is impossible to understand if there no is
243                  * a btrfs filesystem from an I/O error !!!
244                  */
245                 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
246                 e = errno;
247
248                 if( ret < 0 ){
249                         close(fd);
250                         fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
251                                 argv[i], strerror(e));
252                         return 1;
253                 }
254         }
255
256         close(fd);
257         return 0;
258 }
259
260 static const char * const cmd_ready_dev_usage[] = {
261         "btrfs device ready <device>",
262         "Check device to see if it has all of it's devices in cache for mounting",
263         NULL
264 };
265
266 static int cmd_ready_dev(int argc, char **argv)
267 {
268         struct  btrfs_ioctl_vol_args args;
269         int     fd;
270         int     ret;
271
272         if (check_argc_min(argc, 2))
273                 usage(cmd_ready_dev_usage);
274
275         fd = open("/dev/btrfs-control", O_RDWR);
276         if (fd < 0) {
277                 perror("failed to open /dev/btrfs-control");
278                 return 1;
279         }
280         if (!is_block_device(argv[1])) {
281                 fprintf(stderr,
282                         "ERROR: %s is not a block device\n", argv[1]);
283                 close(fd);
284                 return 1;
285         }
286
287         strncpy(args.name, argv[argc - 1], BTRFS_PATH_NAME_MAX);
288         ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
289         if (ret < 0) {
290                 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
291                         " is ready for mounting - %s\n", argv[argc - 1],
292                         strerror(errno));
293                 ret = 1;
294         }
295
296         close(fd);
297         return ret;
298 }
299
300 static const char * const cmd_dev_stats_usage[] = {
301         "btrfs device stats [-z] <path>|<device>",
302         "Show current device IO stats. -z to reset stats afterwards.",
303         NULL
304 };
305
306 static int cmd_dev_stats(int argc, char **argv)
307 {
308         char *dev_path;
309         struct btrfs_ioctl_fs_info_args fi_args;
310         struct btrfs_ioctl_dev_info_args *di_args = NULL;
311         int ret;
312         int fdmnt;
313         int i;
314         int c;
315         int err = 0;
316         __u64 flags = 0;
317         DIR *dirstream = NULL;
318
319         optind = 1;
320         while ((c = getopt(argc, argv, "z")) != -1) {
321                 switch (c) {
322                 case 'z':
323                         flags = BTRFS_DEV_STATS_RESET;
324                         break;
325                 case '?':
326                 default:
327                         fprintf(stderr, "ERROR: device stat args invalid.\n"
328                                         " device stat [-z] <path>|<device>\n"
329                                         " -z  to reset stats after reading.\n");
330                         return 1;
331                 }
332         }
333
334         if (optind + 1 != argc) {
335                 fprintf(stderr, "ERROR: device stat needs path|device as single"
336                         " argument\n");
337                 return 1;
338         }
339
340         dev_path = argv[optind];
341
342         fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
343
344         if (fdmnt < 0) {
345                 fprintf(stderr, "ERROR: can't access '%s'\n", dev_path);
346                 return 1;
347         }
348
349         ret = get_fs_info(dev_path, &fi_args, &di_args);
350         if (ret) {
351                 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
352                                 "%s\n", strerror(-ret));
353                 err = 1;
354                 goto out;
355         }
356         if (!fi_args.num_devices) {
357                 fprintf(stderr, "ERROR: no devices found\n");
358                 err = 1;
359                 goto out;
360         }
361
362         for (i = 0; i < fi_args.num_devices; i++) {
363                 struct btrfs_ioctl_get_dev_stats args = {0};
364                 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
365
366                 strncpy((char *)path, (char *)di_args[i].path,
367                         BTRFS_DEVICE_PATH_NAME_MAX);
368                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
369
370                 args.devid = di_args[i].devid;
371                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
372                 args.flags = flags;
373
374                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
375                         fprintf(stderr,
376                                 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
377                                 path, strerror(errno));
378                         err = 1;
379                 } else {
380                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
381                                 printf("[%s].write_io_errs   %llu\n",
382                                        path,
383                                        (unsigned long long) args.values[
384                                         BTRFS_DEV_STAT_WRITE_ERRS]);
385                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
386                                 printf("[%s].read_io_errs    %llu\n",
387                                        path,
388                                        (unsigned long long) args.values[
389                                         BTRFS_DEV_STAT_READ_ERRS]);
390                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
391                                 printf("[%s].flush_io_errs   %llu\n",
392                                        path,
393                                        (unsigned long long) args.values[
394                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
395                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
396                                 printf("[%s].corruption_errs %llu\n",
397                                        path,
398                                        (unsigned long long) args.values[
399                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
400                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
401                                 printf("[%s].generation_errs %llu\n",
402                                        path,
403                                        (unsigned long long) args.values[
404                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
405                 }
406         }
407
408 out:
409         free(di_args);
410         close_file_or_dir(fdmnt, dirstream);
411
412         return err;
413 }
414
415 const struct cmd_group device_cmd_group = {
416         device_cmd_group_usage, NULL, {
417                 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
418                 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
419                 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
420                 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
421                 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
422                 NULL_CMD_STRUCT
423         }
424 };
425
426 int cmd_device(int argc, char **argv)
427 {
428         return handle_command_group(&device_cmd_group, argc, argv);
429 }