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