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