Btrfs-progs: fix magic return value in cmds-filesystem.c
[platform/upstream/btrfs-progs.git] / cmds-filesystem.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 <sys/ioctl.h>
22 #include <errno.h>
23 #include <uuid/uuid.h>
24 #include <ctype.h>
25
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "ioctl.h"
29 #include "utils.h"
30 #include "volumes.h"
31
32 #include "version.h"
33
34 #include "commands.h"
35
36 static const char * const filesystem_cmd_group_usage[] = {
37         "btrfs filesystem [<group>] <command> [<args>]",
38         NULL
39 };
40
41 static const char * const cmd_df_usage[] = {
42         "btrfs filesystem df <path>",
43         "Show space usage information for a mount point",
44         NULL
45 };
46
47 static int cmd_df(int argc, char **argv)
48 {
49         struct btrfs_ioctl_space_args *sargs, *sargs_orig;
50         u64 count = 0, i;
51         int ret;
52         int fd;
53         int e;
54         char *path;
55         DIR  *dirstream = NULL;
56
57         if (check_argc_exact(argc, 2))
58                 usage(cmd_df_usage);
59
60         path = argv[1];
61
62         fd = open_file_or_dir(path, &dirstream);
63         if (fd < 0) {
64                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
65                 return 1;
66         }
67
68         sargs_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
69         if (!sargs) {
70                 ret = -ENOMEM;
71                 goto out;
72         }
73
74         sargs->space_slots = 0;
75         sargs->total_spaces = 0;
76
77         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
78         e = errno;
79         if (ret) {
80                 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
81                         path, strerror(e));
82                 goto out;
83         }
84         if (!sargs->total_spaces) {
85                 ret = 0;
86                 goto out;
87         }
88
89         count = sargs->total_spaces;
90
91         sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
92                         (count * sizeof(struct btrfs_ioctl_space_info)));
93         if (!sargs) {
94                 sargs = sargs_orig;
95                 ret = -ENOMEM;
96                 goto out;
97         }
98
99         sargs->space_slots = count;
100         sargs->total_spaces = 0;
101
102         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
103         e = errno;
104         if (ret) {
105                 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
106                         path, strerror(e));
107                 goto out;
108         }
109
110         for (i = 0; i < sargs->total_spaces; i++) {
111                 char description[80];
112                 int written = 0;
113                 u64 flags = sargs->spaces[i].flags;
114
115                 memset(description, 0, 80);
116
117                 if (flags & BTRFS_BLOCK_GROUP_DATA) {
118                         if (flags & BTRFS_BLOCK_GROUP_METADATA) {
119                                 snprintf(description, 14, "%s",
120                                          "Data+Metadata");
121                                 written += 13;
122                         } else {
123                                 snprintf(description, 5, "%s", "Data");
124                                 written += 4;
125                         }
126                 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
127                         snprintf(description, 7, "%s", "System");
128                         written += 6;
129                 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
130                         snprintf(description, 9, "%s", "Metadata");
131                         written += 8;
132                 }
133
134                 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
135                         snprintf(description+written, 8, "%s", ", RAID0");
136                         written += 7;
137                 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
138                         snprintf(description+written, 8, "%s", ", RAID1");
139                         written += 7;
140                 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
141                         snprintf(description+written, 6, "%s", ", DUP");
142                         written += 5;
143                 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
144                         snprintf(description+written, 9, "%s", ", RAID10");
145                         written += 8;
146                 } else if (flags & BTRFS_BLOCK_GROUP_RAID5) {
147                         snprintf(description+written, 9, "%s", ", RAID5");
148                         written += 7;
149                 } else if (flags & BTRFS_BLOCK_GROUP_RAID6) {
150                         snprintf(description+written, 9, "%s", ", RAID6");
151                         written += 7;
152                 }
153
154                 printf("%s: total=%s, used=%s\n", description,
155                         pretty_size(sargs->spaces[i].total_bytes),
156                         pretty_size(sargs->spaces[i].used_bytes));
157         }
158 out:
159         close_file_or_dir(fd, dirstream);
160         free(sargs);
161
162         return !!ret;
163 }
164
165 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
166 {
167         char uuidbuf[37];
168         struct list_head *cur;
169         struct btrfs_device *device;
170         int search_len = strlen(search);
171
172         search_len = min(search_len, 37);
173         uuid_unparse(fs_devices->fsid, uuidbuf);
174         if (!strncmp(uuidbuf, search, search_len))
175                 return 1;
176
177         list_for_each(cur, &fs_devices->devices) {
178                 device = list_entry(cur, struct btrfs_device, dev_list);
179                 if ((device->label && strcmp(device->label, search) == 0) ||
180                     strcmp(device->name, search) == 0)
181                         return 1;
182         }
183         return 0;
184 }
185
186 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
187 {
188         char uuidbuf[37];
189         struct list_head *cur;
190         struct btrfs_device *device;
191         u64 devs_found = 0;
192         u64 total;
193
194         uuid_unparse(fs_devices->fsid, uuidbuf);
195         device = list_entry(fs_devices->devices.next, struct btrfs_device,
196                             dev_list);
197         if (device->label && device->label[0])
198                 printf("Label: '%s' ", device->label);
199         else
200                 printf("Label: none ");
201
202
203         total = device->total_devs;
204         printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
205                (unsigned long long)total,
206                pretty_size(device->super_bytes_used));
207
208         list_for_each(cur, &fs_devices->devices) {
209                 device = list_entry(cur, struct btrfs_device, dev_list);
210
211                 printf("\tdevid %4llu size %s used %s path %s\n",
212                        (unsigned long long)device->devid,
213                        pretty_size(device->total_bytes),
214                        pretty_size(device->bytes_used), device->name);
215
216                 devs_found++;
217         }
218         if (devs_found < total) {
219                 printf("\t*** Some devices missing\n");
220         }
221         printf("\n");
222 }
223
224 static const char * const cmd_show_usage[] = {
225         "btrfs filesystem show [--all-devices|<uuid>]",
226         "Show the structure of a filesystem",
227         "If no argument is given, structure of all present filesystems is shown.",
228         NULL
229 };
230
231 static int cmd_show(int argc, char **argv)
232 {
233         struct list_head *all_uuids;
234         struct btrfs_fs_devices *fs_devices;
235         struct list_head *cur_uuid;
236         char *search = NULL;
237         int ret;
238         int where = BTRFS_SCAN_PROC;
239         int searchstart = 1;
240
241         if( argc > 1 && !strcmp(argv[1],"--all-devices")){
242                 where = BTRFS_SCAN_DEV;
243                 searchstart += 1;
244         }
245
246         if (check_argc_max(argc, searchstart + 1))
247                 usage(cmd_show_usage);
248
249         ret = scan_for_btrfs(where, 0);
250
251         if (ret){
252                 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
253                 return 1;
254         }
255         
256         if(searchstart < argc)
257                 search = argv[searchstart];
258
259         all_uuids = btrfs_scanned_uuids();
260         list_for_each(cur_uuid, all_uuids) {
261                 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
262                                         list);
263                 if (search && uuid_search(fs_devices, search) == 0)
264                         continue;
265                 print_one_uuid(fs_devices);
266         }
267         printf("%s\n", BTRFS_BUILD_VERSION);
268         return 0;
269 }
270
271 static const char * const cmd_sync_usage[] = {
272         "btrfs filesystem sync <path>",
273         "Force a sync on a filesystem",
274         NULL
275 };
276
277 static int cmd_sync(int argc, char **argv)
278 {
279         int     fd, res, e;
280         char    *path;
281         DIR     *dirstream = NULL;
282
283         if (check_argc_exact(argc, 2))
284                 usage(cmd_sync_usage);
285
286         path = argv[1];
287
288         fd = open_file_or_dir(path, &dirstream);
289         if (fd < 0) {
290                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
291                 return 1;
292         }
293
294         printf("FSSync '%s'\n", path);
295         res = ioctl(fd, BTRFS_IOC_SYNC);
296         e = errno;
297         close_file_or_dir(fd, dirstream);
298         if( res < 0 ){
299                 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n", 
300                         path, strerror(e));
301                 return 1;
302         }
303
304         return 0;
305 }
306
307 static int parse_compress_type(char *s)
308 {
309         if (strcmp(optarg, "zlib") == 0)
310                 return BTRFS_COMPRESS_ZLIB;
311         else if (strcmp(optarg, "lzo") == 0)
312                 return BTRFS_COMPRESS_LZO;
313         else {
314                 fprintf(stderr, "Unknown compress type %s\n", s);
315                 exit(1);
316         };
317 }
318
319 static const char * const cmd_defrag_usage[] = {
320         "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
321         "Defragment a file or a directory",
322         "",
323         "-v             be verbose",
324         "-c[zlib,lzo]   compress the file while defragmenting",
325         "-f             flush data to disk immediately after defragmenting",
326         "-s start       defragment only from byte onward",
327         "-l len         defragment only up to len bytes",
328         "-t size        minimal size of file to be considered for defragmenting",
329         NULL
330 };
331
332 static int cmd_defrag(int argc, char **argv)
333 {
334         int fd;
335         int flush = 0;
336         u64 start = 0;
337         u64 len = (u64)-1;
338         u32 thresh = 0;
339         int i;
340         int errors = 0;
341         int ret = 0;
342         int verbose = 0;
343         int fancy_ioctl = 0;
344         struct btrfs_ioctl_defrag_range_args range;
345         int e=0;
346         int compress_type = BTRFS_COMPRESS_NONE;
347         DIR *dirstream = NULL;
348
349         optind = 1;
350         while(1) {
351                 int c = getopt(argc, argv, "vc::fs:l:t:");
352                 if (c < 0)
353                         break;
354
355                 switch(c) {
356                 case 'c':
357                         compress_type = BTRFS_COMPRESS_ZLIB;
358                         if (optarg)
359                                 compress_type = parse_compress_type(optarg);
360                         fancy_ioctl = 1;
361                         break;
362                 case 'f':
363                         flush = 1;
364                         fancy_ioctl = 1;
365                         break;
366                 case 'v':
367                         verbose = 1;
368                         break;
369                 case 's':
370                         start = parse_size(optarg);
371                         fancy_ioctl = 1;
372                         break;
373                 case 'l':
374                         len = parse_size(optarg);
375                         fancy_ioctl = 1;
376                         break;
377                 case 't':
378                         thresh = parse_size(optarg);
379                         fancy_ioctl = 1;
380                         break;
381                 default:
382                         usage(cmd_defrag_usage);
383                 }
384         }
385
386         if (check_argc_min(argc - optind, 1))
387                 usage(cmd_defrag_usage);
388
389         memset(&range, 0, sizeof(range));
390         range.start = start;
391         range.len = len;
392         range.extent_thresh = thresh;
393         if (compress_type) {
394                 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
395                 range.compress_type = compress_type;
396         }
397         if (flush)
398                 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
399
400         for (i = optind; i < argc; i++) {
401                 if (verbose)
402                         printf("%s\n", argv[i]);
403                 fd = open_file_or_dir(argv[i], &dirstream);
404                 if (fd < 0) {
405                         fprintf(stderr, "failed to open %s\n", argv[i]);
406                         perror("open:");
407                         errors++;
408                         continue;
409                 }
410                 if (!fancy_ioctl) {
411                         ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
412                         e=errno;
413                 } else {
414                         ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
415                         if (ret && errno == ENOTTY) {
416                                 fprintf(stderr, "ERROR: defrag range ioctl not "
417                                         "supported in this kernel, please try "
418                                         "without any options.\n");
419                                 errors++;
420                                 close(fd);
421                                 break;
422                         }
423                         e = errno;
424                 }
425                 if (ret) {
426                         fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
427                                 argv[i], strerror(e));
428                         errors++;
429                 }
430                 close_file_or_dir(fd, dirstream);
431         }
432         if (verbose)
433                 printf("%s\n", BTRFS_BUILD_VERSION);
434         if (errors)
435                 fprintf(stderr, "total %d failures\n", errors);
436
437         return !!errors;
438 }
439
440 static const char * const cmd_resize_usage[] = {
441         "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
442         "Resize a filesystem",
443         "If 'max' is passed, the filesystem will occupy all available space",
444         "on the device 'devid'.",
445         NULL
446 };
447
448 static int cmd_resize(int argc, char **argv)
449 {
450         struct btrfs_ioctl_vol_args     args;
451         int     fd, res, len, e;
452         char    *amount, *path;
453         DIR     *dirstream = NULL;
454
455         if (check_argc_exact(argc, 3))
456                 usage(cmd_resize_usage);
457
458         amount = argv[1];
459         path = argv[2];
460
461         len = strlen(amount);
462         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
463                 fprintf(stderr, "ERROR: size value too long ('%s)\n",
464                         amount);
465                 return 1;
466         }
467
468         fd = open_file_or_dir(path, &dirstream);
469         if (fd < 0) {
470                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
471                 return 1;
472         }
473
474         printf("Resize '%s' of '%s'\n", path, amount);
475         strncpy_null(args.name, amount);
476         res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
477         e = errno;
478         close_file_or_dir(fd, dirstream);
479         if( res < 0 ){
480                 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n", 
481                         path, strerror(e));
482                 return 1;
483         }
484         return 0;
485 }
486
487 static const char * const cmd_label_usage[] = {
488         "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
489         "Get or change the label of a filesystem",
490         "With one argument, get the label of filesystem on <device>.",
491         "If <newlabel> is passed, set the filesystem label to <newlabel>.",
492         NULL
493 };
494
495 static int cmd_label(int argc, char **argv)
496 {
497         if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
498                 usage(cmd_label_usage);
499
500         if (argc > 2)
501                 return set_label(argv[1], argv[2]);
502         else
503                 return get_label(argv[1]);
504 }
505
506 const struct cmd_group filesystem_cmd_group = {
507         filesystem_cmd_group_usage, NULL, {
508                 { "df", cmd_df, cmd_df_usage, NULL, 0 },
509                 { "show", cmd_show, cmd_show_usage, NULL, 0 },
510                 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
511                 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
512                 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
513                 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
514                 { "label", cmd_label, cmd_label_usage, NULL, 0 },
515                 NULL_CMD_STRUCT
516         }
517 };
518
519 int cmd_filesystem(int argc, char **argv)
520 {
521         return handle_command_group(&filesystem_cmd_group, argc, argv);
522 }