btrfs-progs: Improve the errno string about open_path_or_dev_mnt()
[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 '%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                         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
112                 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
113                                            0, &mixed, discard);
114                 close(devfd);
115                 if (res) {
116                         ret++;
117                         goto error_out;
118                 }
119
120                 strncpy_null(ioctl_args.name, argv[i]);
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 error_out:
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 '%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                 if (!is_block_device(argv[i])) {
164                         fprintf(stderr,
165                                 "ERROR: %s is not a block device\n", argv[i]);
166                         ret++;
167                         continue;
168                 }
169                 strncpy_null(arg.name, argv[i]);
170                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
171                 e = errno;
172                 if (res > 0) {
173                         fprintf(stderr,
174                                 "ERROR: error removing the device '%s' - %s\n",
175                                 argv[i], btrfs_err_str(res));
176                         ret++;
177                 } else if (res < 0) {
178                         fprintf(stderr,
179                                 "ERROR: error removing the device '%s' - %s\n",
180                                 argv[i], strerror(e));
181                         ret++;
182                 }
183         }
184
185         close_file_or_dir(fdmnt, dirstream);
186         return !!ret;
187 }
188
189 static const char * const cmd_scan_dev_usage[] = {
190         "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
191         "Scan devices for a btrfs filesystem",
192         NULL
193 };
194
195 static int cmd_scan_dev(int argc, char **argv)
196 {
197         int i, fd, e;
198         int where = BTRFS_SCAN_LBLKID;
199         int devstart = 1;
200         int all = 0;
201         int ret = 0;
202
203         optind = 1;
204         while (1) {
205                 int long_index;
206                 static struct option long_options[] = {
207                         { "all-devices", no_argument, NULL, 'd'},
208                         { 0, 0, 0, 0 },
209                 };
210                 int c = getopt_long(argc, argv, "d", long_options,
211                                     &long_index);
212                 if (c < 0)
213                         break;
214                 switch (c) {
215                 case 'd':
216                         where = BTRFS_SCAN_DEV;
217                         all = 1;
218                         break;
219                 default:
220                         usage(cmd_scan_dev_usage);
221                 }
222         }
223
224         if (all && check_argc_max(argc, 2))
225                 usage(cmd_scan_dev_usage);
226
227         if (all || argc == 1) {
228                 printf("Scanning for Btrfs filesystems\n");
229                 ret = scan_for_btrfs(where, BTRFS_UPDATE_KERNEL);
230                 if (ret)
231                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
232                 goto out;
233         }
234
235         fd = open("/dev/btrfs-control", O_RDWR);
236         if (fd < 0) {
237                 perror("failed to open /dev/btrfs-control");
238                 ret = 1;
239                 goto out;
240         }
241
242         for( i = devstart ; i < argc ; i++ ){
243                 struct btrfs_ioctl_vol_args args;
244
245                 if (!is_block_device(argv[i])) {
246                         fprintf(stderr,
247                                 "ERROR: %s is not a block device\n", argv[i]);
248                         ret = 1;
249                         goto close_out;
250                 }
251                 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
252
253                 strncpy_null(args.name, argv[i]);
254                 /*
255                  * FIXME: which are the error code returned by this ioctl ?
256                  * it seems that is impossible to understand if there no is
257                  * a btrfs filesystem from an I/O error !!!
258                  */
259                 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
260                 e = errno;
261
262                 if( ret < 0 ){
263                         fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
264                                 argv[i], strerror(e));
265                         goto close_out;
266                 }
267         }
268
269 close_out:
270         close(fd);
271 out:
272         return !!ret;
273 }
274
275 static const char * const cmd_ready_dev_usage[] = {
276         "btrfs device ready <device>",
277         "Check device to see if it has all of it's devices in cache for mounting",
278         NULL
279 };
280
281 static int cmd_ready_dev(int argc, char **argv)
282 {
283         struct  btrfs_ioctl_vol_args args;
284         int     fd;
285         int     ret;
286
287         if (check_argc_min(argc, 2))
288                 usage(cmd_ready_dev_usage);
289
290         fd = open("/dev/btrfs-control", O_RDWR);
291         if (fd < 0) {
292                 perror("failed to open /dev/btrfs-control");
293                 return 1;
294         }
295         if (!is_block_device(argv[1])) {
296                 fprintf(stderr,
297                         "ERROR: %s is not a block device\n", argv[1]);
298                 close(fd);
299                 return 1;
300         }
301
302         strncpy(args.name, argv[argc - 1], BTRFS_PATH_NAME_MAX);
303         ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
304         if (ret < 0) {
305                 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
306                         " is ready for mounting - %s\n", argv[argc - 1],
307                         strerror(errno));
308                 ret = 1;
309         }
310
311         close(fd);
312         return ret;
313 }
314
315 static const char * const cmd_dev_stats_usage[] = {
316         "btrfs device stats [-z] <path>|<device>",
317         "Show current device IO stats. -z to reset stats afterwards.",
318         NULL
319 };
320
321 static int cmd_dev_stats(int argc, char **argv)
322 {
323         char *dev_path;
324         struct btrfs_ioctl_fs_info_args fi_args;
325         struct btrfs_ioctl_dev_info_args *di_args = NULL;
326         int ret;
327         int fdmnt;
328         int i;
329         int c;
330         int err = 0;
331         __u64 flags = 0;
332         DIR *dirstream = NULL;
333
334         optind = 1;
335         while ((c = getopt(argc, argv, "z")) != -1) {
336                 switch (c) {
337                 case 'z':
338                         flags = BTRFS_DEV_STATS_RESET;
339                         break;
340                 case '?':
341                 default:
342                         usage(cmd_dev_stats_usage);
343                 }
344         }
345
346         argc = argc - optind;
347         if (check_argc_exact(argc, 1))
348                 usage(cmd_dev_stats_usage);
349
350         dev_path = argv[optind];
351
352         fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
353
354         if (fdmnt < 0) {
355                 if (errno == EINVAL)
356                         fprintf(stderr,
357                                 "ERROR: '%s' is not a mounted btrfs device\n",
358                                 dev_path);
359                 else
360                         fprintf(stderr, "ERROR: can't access '%s': %s\n",
361                                 dev_path, strerror(errno));
362                 return 1;
363         }
364
365         ret = get_fs_info(dev_path, &fi_args, &di_args);
366         if (ret) {
367                 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
368                                 "%s\n", strerror(-ret));
369                 err = 1;
370                 goto out;
371         }
372         if (!fi_args.num_devices) {
373                 fprintf(stderr, "ERROR: no devices found\n");
374                 err = 1;
375                 goto out;
376         }
377
378         for (i = 0; i < fi_args.num_devices; i++) {
379                 struct btrfs_ioctl_get_dev_stats args = {0};
380                 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
381
382                 strncpy((char *)path, (char *)di_args[i].path,
383                         BTRFS_DEVICE_PATH_NAME_MAX);
384                 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
385
386                 args.devid = di_args[i].devid;
387                 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
388                 args.flags = flags;
389
390                 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
391                         fprintf(stderr,
392                                 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
393                                 path, strerror(errno));
394                         err = 1;
395                 } else {
396                         if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
397                                 printf("[%s].write_io_errs   %llu\n",
398                                        path,
399                                        (unsigned long long) args.values[
400                                         BTRFS_DEV_STAT_WRITE_ERRS]);
401                         if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
402                                 printf("[%s].read_io_errs    %llu\n",
403                                        path,
404                                        (unsigned long long) args.values[
405                                         BTRFS_DEV_STAT_READ_ERRS]);
406                         if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
407                                 printf("[%s].flush_io_errs   %llu\n",
408                                        path,
409                                        (unsigned long long) args.values[
410                                         BTRFS_DEV_STAT_FLUSH_ERRS]);
411                         if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
412                                 printf("[%s].corruption_errs %llu\n",
413                                        path,
414                                        (unsigned long long) args.values[
415                                         BTRFS_DEV_STAT_CORRUPTION_ERRS]);
416                         if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
417                                 printf("[%s].generation_errs %llu\n",
418                                        path,
419                                        (unsigned long long) args.values[
420                                         BTRFS_DEV_STAT_GENERATION_ERRS]);
421                 }
422         }
423
424 out:
425         free(di_args);
426         close_file_or_dir(fdmnt, dirstream);
427
428         return err;
429 }
430
431 const struct cmd_group device_cmd_group = {
432         device_cmd_group_usage, NULL, {
433                 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
434                 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
435                 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
436                 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
437                 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
438                 NULL_CMD_STRUCT
439         }
440 };
441
442 int cmd_device(int argc, char **argv)
443 {
444         return handle_command_group(&device_cmd_group, argc, argv);
445 }