btrfs-progs: congregate dev scan
[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
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "ioctl.h"
29 #include "utils.h"
30
31 #include "commands.h"
32
33 /* FIXME - imported cruft, fix sparse errors and warnings */
34 #ifdef __CHECKER__
35 #define BLKGETSIZE64 0
36 #define BTRFS_IOC_SNAP_CREATE_V2 0
37 #define BTRFS_VOL_NAME_MAX 255
38 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
39 static inline int ioctl(int fd, int define, void *arg) { return 0; }
40 #endif
41
42 static const char * const device_cmd_group_usage[] = {
43         "btrfs device <command> [<args>]",
44         NULL
45 };
46
47 static const char * const cmd_add_dev_usage[] = {
48         "btrfs device add <device> [<device>...] <path>",
49         "Add a device to a filesystem",
50         NULL
51 };
52
53 static int cmd_add_dev(int argc, char **argv)
54 {
55         char    *mntpnt;
56         int     i, fdmnt, ret=0, e;
57         DIR     *dirstream = NULL;
58
59         if (check_argc_min(argc, 3))
60                 usage(cmd_add_dev_usage);
61
62         mntpnt = argv[argc - 1];
63
64         fdmnt = open_file_or_dir(mntpnt, &dirstream);
65         if (fdmnt < 0) {
66                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
67                 return 12;
68         }
69
70         for (i = 1; i < argc - 1; i++ ){
71                 struct btrfs_ioctl_vol_args ioctl_args;
72                 int     devfd, res;
73                 u64 dev_block_count = 0;
74                 struct stat st;
75                 int mixed = 0;
76
77                 res = check_mounted(argv[i]);
78                 if (res < 0) {
79                         fprintf(stderr, "error checking %s mount status\n",
80                                 argv[i]);
81                         ret++;
82                         continue;
83                 }
84                 if (res == 1) {
85                         fprintf(stderr, "%s is mounted\n", argv[i]);
86                         ret++;
87                         continue;
88                 }
89
90                 devfd = open(argv[i], O_RDWR);
91                 if (devfd < 0) {
92                         fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
93                         ret++;
94                         continue;
95                 }
96                 res = fstat(devfd, &st);
97                 if (res) {
98                         fprintf(stderr, "ERROR: Unable to stat '%s'\n", argv[i]);
99                         close(devfd);
100                         ret++;
101                         continue;
102                 }
103                 if (!S_ISBLK(st.st_mode)) {
104                         fprintf(stderr, "ERROR: '%s' is not a block device\n", argv[i]);
105                         close(devfd);
106                         ret++;
107                         continue;
108                 }
109
110                 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
111                                            0, &mixed, 0);
112                 if (res) {
113                         fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]);
114                         close(devfd);
115                         ret++;
116                         continue;
117                 }
118                 close(devfd);
119
120                 strncpy_null(ioctl_args.name, argv[i]);
121                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
122                 e = errno;
123                 if(res<0){
124                         fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
125                                 argv[i], strerror(e));
126                         ret++;
127                 }
128
129         }
130
131         close_file_or_dir(fdmnt, dirstream);
132         if (ret)
133                 return ret+20;
134         else
135                 return 0;
136 }
137
138 static const char * const cmd_rm_dev_usage[] = {
139         "btrfs device delete <device> [<device>...] <path>",
140         "Remove a device from a filesystem",
141         NULL
142 };
143
144 static int cmd_rm_dev(int argc, char **argv)
145 {
146         char    *mntpnt;
147         int     i, fdmnt, ret=0, e;
148         DIR     *dirstream = NULL;
149
150         if (check_argc_min(argc, 3))
151                 usage(cmd_rm_dev_usage);
152
153         mntpnt = argv[argc - 1];
154
155         fdmnt = open_file_or_dir(mntpnt, &dirstream);
156         if (fdmnt < 0) {
157                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
158                 return 12;
159         }
160
161         for(i=1 ; i < argc - 1; i++ ){
162                 struct  btrfs_ioctl_vol_args arg;
163                 int     res;
164
165                 strncpy_null(arg.name, argv[i]);
166                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
167                 e = errno;
168                 if(res<0){
169                         fprintf(stderr, "ERROR: error removing the device '%s' - %s\n",
170                                 argv[i], strerror(e));
171                         ret++;
172                 }
173         }
174
175         close_file_or_dir(fdmnt, dirstream);
176         if( ret)
177                 return ret+20;
178         else
179                 return 0;
180 }
181
182 static const char * const cmd_scan_dev_usage[] = {
183         "btrfs device scan [<--all-devices>|<device> [<device>...]]",
184         "Scan devices for a btrfs filesystem",
185         NULL
186 };
187
188 static int cmd_scan_dev(int argc, char **argv)
189 {
190         int     i, fd, e;
191         int     where = BTRFS_SCAN_PROC;
192         int     devstart = 1;
193
194         if( argc > 1 && !strcmp(argv[1],"--all-devices")){
195                 if (check_argc_max(argc, 2))
196                         usage(cmd_scan_dev_usage);
197
198                 where = BTRFS_SCAN_DEV;
199                 devstart += 1;
200         }
201
202         if(argc<=devstart){
203                 int ret;
204                 printf("Scanning for Btrfs filesystems\n");
205                 ret = scan_for_btrfs(where, 1);
206                 if (ret){
207                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
208                         return 18;
209                 }
210                 return 0;
211         }
212
213         fd = open("/dev/btrfs-control", O_RDWR);
214         if (fd < 0) {
215                 perror("failed to open /dev/btrfs-control");
216                 return 10;
217         }
218
219         for( i = devstart ; i < argc ; i++ ){
220                 struct btrfs_ioctl_vol_args args;
221                 int ret;
222
223                 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
224
225                 strncpy_null(args.name, argv[i]);
226                 /*
227                  * FIXME: which are the error code returned by this ioctl ?
228                  * it seems that is impossible to understand if there no is
229                  * a btrfs filesystem from an I/O error !!!
230                  */
231                 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
232                 e = errno;
233
234                 if( ret < 0 ){
235                         close(fd);
236                         fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
237                                 argv[i], strerror(e));
238                         return 11;
239                 }
240         }
241
242         close(fd);
243         return 0;
244 }
245
246 static const char * const cmd_ready_dev_usage[] = {
247         "btrfs device ready <device>",
248         "Check device to see if it has all of it's devices in cache for mounting",
249         NULL
250 };
251
252 static int cmd_ready_dev(int argc, char **argv)
253 {
254         struct  btrfs_ioctl_vol_args args;
255         int     fd;
256         int     ret;
257
258         if (check_argc_min(argc, 2))
259                 usage(cmd_ready_dev_usage);
260
261         fd = open("/dev/btrfs-control", O_RDWR);
262         if (fd < 0) {
263                 perror("failed to open /dev/btrfs-control");
264                 return 10;
265         }
266
267         strncpy(args.name, argv[argc - 1], BTRFS_PATH_NAME_MAX);
268         ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
269         if (ret < 0) {
270                 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
271                         " is ready for mounting - %s\n", argv[argc - 1],
272                         strerror(errno));
273                 ret = 1;
274         }
275
276         close(fd);
277         return ret;
278 }
279
280 static const char * const cmd_dev_stats_usage[] = {
281         "btrfs device stats [-z] <path>|<device>",
282         "Show current device IO stats. -z to reset stats afterwards.",
283         NULL
284 };
285
286 static int cmd_dev_stats(int argc, char **argv)
287 {
288         char *path;
289         struct btrfs_ioctl_fs_info_args fi_args;
290         struct btrfs_ioctl_dev_info_args *di_args = NULL;
291         int ret;
292         int fdmnt;
293         int i;
294         int c;
295         int err = 0;
296         __u64 flags = 0;
297         DIR *dirstream = NULL;
298
299         optind = 1;
300         while ((c = getopt(argc, argv, "z")) != -1) {
301                 switch (c) {
302                 case 'z':
303                         flags = BTRFS_DEV_STATS_RESET;
304                         break;
305                 case '?':
306                 default:
307                         fprintf(stderr, "ERROR: device stat args invalid.\n"
308                                         " device stat [-z] <path>|<device>\n"
309                                         " -z  to reset stats after reading.\n");
310                         return 1;
311                 }
312         }
313
314         if (optind + 1 != argc) {
315                 fprintf(stderr, "ERROR: device stat needs path|device as single"
316                         " argument\n");
317                 return 1;
318         }
319
320         path = argv[optind];
321
322         fdmnt = open_path_or_dev_mnt(path, &dirstream);
323
324         if (fdmnt < 0) {
325                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
326                 return 12;
327         }
328
329         ret = get_fs_info(path, &fi_args, &di_args);
330         if (ret) {
331                 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
332                                 "%s\n", strerror(-ret));
333                 err = 1;
334                 goto out;
335         }
336         if (!fi_args.num_devices) {
337                 fprintf(stderr, "ERROR: no devices found\n");
338                 err = 1;
339                 goto out;
340         }
341
342         for (i = 0; i < fi_args.num_devices; i++) {
343                 struct btrfs_ioctl_get_dev_stats args = {0};
344                 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
345
346                 strncpy((char *)path, (char *)di_args[i].path,
347                         BTRFS_DEVICE_PATH_NAME_MAX);
348                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
349
350                 args.devid = di_args[i].devid;
351                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
352                 args.flags = flags;
353
354                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
355                         fprintf(stderr,
356                                 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
357                                 path, strerror(errno));
358                         err = 1;
359                 } else {
360                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
361                                 printf("[%s].write_io_errs   %llu\n",
362                                        path,
363                                        (unsigned long long) args.values[
364                                         BTRFS_DEV_STAT_WRITE_ERRS]);
365                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
366                                 printf("[%s].read_io_errs    %llu\n",
367                                        path,
368                                        (unsigned long long) args.values[
369                                         BTRFS_DEV_STAT_READ_ERRS]);
370                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
371                                 printf("[%s].flush_io_errs   %llu\n",
372                                        path,
373                                        (unsigned long long) args.values[
374                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
375                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
376                                 printf("[%s].corruption_errs %llu\n",
377                                        path,
378                                        (unsigned long long) args.values[
379                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
380                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
381                                 printf("[%s].generation_errs %llu\n",
382                                        path,
383                                        (unsigned long long) args.values[
384                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
385                 }
386         }
387
388 out:
389         free(di_args);
390         close_file_or_dir(fdmnt, dirstream);
391
392         return err;
393 }
394
395 const struct cmd_group device_cmd_group = {
396         device_cmd_group_usage, NULL, {
397                 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
398                 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
399                 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
400                 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
401                 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
402                 { 0, 0, 0, 0, 0 }
403         }
404 };
405
406 int cmd_device(int argc, char **argv)
407 {
408         return handle_command_group(&device_cmd_group, argc, argv);
409 }