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