7a0f7a40cf3a204c8233decde07717bbfa85b18e
[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(ioctl_args.name, argv[i], BTRFS_PATH_NAME_MAX);
120                 ioctl_args.name[BTRFS_PATH_NAME_MAX-1] = 0;
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(fdmnt);
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
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);
155         if (fdmnt < 0) {
156                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
157                 return 12;
158         }
159
160         for(i=1 ; i < argc - 1; i++ ){
161                 struct  btrfs_ioctl_vol_args arg;
162                 int     res;
163
164                 strncpy(arg.name, argv[i], BTRFS_PATH_NAME_MAX);
165                 arg.name[BTRFS_PATH_NAME_MAX-1] = 0;
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(fdmnt);
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 [<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     checklist = 1;
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                 checklist = 0;
199                 devstart += 1;
200         }
201
202         if(argc<=devstart){
203
204                 int ret;
205
206                 printf("Scanning for Btrfs filesystems\n");
207                 if(checklist)
208                         ret = btrfs_scan_block_devices(1);
209                 else
210                         ret = btrfs_scan_one_dir("/dev", 1);
211                 if (ret){
212                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
213                         return 18;
214                 }
215                 return 0;
216         }
217
218         fd = open("/dev/btrfs-control", O_RDWR);
219         if (fd < 0) {
220                 perror("failed to open /dev/btrfs-control");
221                 return 10;
222         }
223
224         for( i = devstart ; i < argc ; i++ ){
225                 struct btrfs_ioctl_vol_args args;
226                 int ret;
227
228                 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
229
230                 strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
231                 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
232                 /*
233                  * FIXME: which are the error code returned by this ioctl ?
234                  * it seems that is impossible to understand if there no is
235                  * a btrfs filesystem from an I/O error !!!
236                  */
237                 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
238                 e = errno;
239
240                 if( ret < 0 ){
241                         close(fd);
242                         fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
243                                 argv[i], strerror(e));
244                         return 11;
245                 }
246         }
247
248         close(fd);
249         return 0;
250 }
251
252 static const char * const cmd_ready_dev_usage[] = {
253         "btrfs device ready <device>",
254         "Check device to see if it has all of it's devices in cache for mounting",
255         NULL
256 };
257
258 static int cmd_ready_dev(int argc, char **argv)
259 {
260         struct  btrfs_ioctl_vol_args args;
261         int     fd;
262         int     ret;
263
264         if (check_argc_min(argc, 2))
265                 usage(cmd_ready_dev_usage);
266
267         fd = open("/dev/btrfs-control", O_RDWR);
268         if (fd < 0) {
269                 perror("failed to open /dev/btrfs-control");
270                 return 10;
271         }
272
273         strncpy(args.name, argv[argc - 1], BTRFS_PATH_NAME_MAX);
274         ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
275         if (ret < 0) {
276                 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
277                         " is ready for mounting - %s\n", argv[argc - 1],
278                         strerror(errno));
279                 ret = 1;
280         }
281
282         close(fd);
283         return ret;
284 }
285
286 static const char * const cmd_dev_stats_usage[] = {
287         "btrfs device stats [-z] <path>|<device>",
288         "Show current device IO stats. -z to reset stats afterwards.",
289         NULL
290 };
291
292 static int cmd_dev_stats(int argc, char **argv)
293 {
294         char *path;
295         struct btrfs_ioctl_fs_info_args fi_args;
296         struct btrfs_ioctl_dev_info_args *di_args = NULL;
297         int ret;
298         int fdmnt;
299         int i;
300         char c;
301         int fdres = -1;
302         int err = 0;
303         __u64 flags = 0;
304
305         optind = 1;
306         while ((c = getopt(argc, argv, "z")) != -1) {
307                 switch (c) {
308                 case 'z':
309                         flags = BTRFS_DEV_STATS_RESET;
310                         break;
311                 case '?':
312                 default:
313                         fprintf(stderr, "ERROR: device stat args invalid.\n"
314                                         " device stat [-z] <path>|<device>\n"
315                                         " -z  to reset stats after reading.\n");
316                         return 1;
317                 }
318         }
319
320         if (optind + 1 != argc) {
321                 fprintf(stderr, "ERROR: device stat needs path|device as single"
322                         " argument\n");
323                 return 1;
324         }
325
326         path = argv[optind];
327
328         fdmnt = open_file_or_dir(path);
329         if (fdmnt < 0) {
330                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
331                 return 12;
332         }
333
334         ret = get_fs_info(fdmnt, path, &fi_args, &di_args);
335         if (ret) {
336                 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
337                                 "%s\n", strerror(-ret));
338                 err = 1;
339                 goto out;
340         }
341         if (!fi_args.num_devices) {
342                 fprintf(stderr, "ERROR: no devices found\n");
343                 err = 1;
344                 goto out;
345         }
346
347         for (i = 0; i < fi_args.num_devices; i++) {
348                 struct btrfs_ioctl_get_dev_stats args = {0};
349                 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
350
351                 strncpy((char *)path, (char *)di_args[i].path,
352                         BTRFS_DEVICE_PATH_NAME_MAX);
353                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
354
355                 args.devid = di_args[i].devid;
356                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
357                 args.flags = flags;
358
359                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
360                         fprintf(stderr,
361                                 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
362                                 path, strerror(errno));
363                         err = 1;
364                 } else {
365                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
366                                 printf("[%s].write_io_errs   %llu\n",
367                                        path,
368                                        (unsigned long long) args.values[
369                                         BTRFS_DEV_STAT_WRITE_ERRS]);
370                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
371                                 printf("[%s].read_io_errs    %llu\n",
372                                        path,
373                                        (unsigned long long) args.values[
374                                         BTRFS_DEV_STAT_READ_ERRS]);
375                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
376                                 printf("[%s].flush_io_errs   %llu\n",
377                                        path,
378                                        (unsigned long long) args.values[
379                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
380                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
381                                 printf("[%s].corruption_errs %llu\n",
382                                        path,
383                                        (unsigned long long) args.values[
384                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
385                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
386                                 printf("[%s].generation_errs %llu\n",
387                                        path,
388                                        (unsigned long long) args.values[
389                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
390                 }
391         }
392
393 out:
394         free(di_args);
395         close(fdmnt);
396         if (fdres > -1)
397                 close(fdres);
398
399         return err;
400 }
401
402 const struct cmd_group device_cmd_group = {
403         device_cmd_group_usage, NULL, {
404                 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
405                 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
406                 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
407                 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
408                 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
409                 { 0, 0, 0, 0, 0 }
410         }
411 };
412
413 int cmd_device(int argc, char **argv)
414 {
415         return handle_command_group(&device_cmd_group, argc, argv);
416 }