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