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