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