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