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