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