btrfs filesystem show: skip duplicate fsids
[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 #define _XOPEN_SOURCE 500
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/ioctl.h>
23 #include <errno.h>
24 #include <uuid/uuid.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <ftw.h>
28 #include <mntent.h>
29 #include <linux/limits.h>
30 #include <getopt.h>
31
32 #include "kerncompat.h"
33 #include "ctree.h"
34 #include "ioctl.h"
35 #include "utils.h"
36 #include "volumes.h"
37 #include "version.h"
38 #include "commands.h"
39 #include "list_sort.h"
40
41
42 /*
43  * for btrfs fi show, we maintain a hash of fsids we've already printed.
44  * This way we don't print dups if a given FS is mounted more than once.
45  */
46 #define SEEN_FSID_HASH_SIZE 256
47
48 struct seen_fsid {
49         u8 fsid[BTRFS_FSID_SIZE];
50         struct seen_fsid *next;
51 };
52
53 static struct seen_fsid *seen_fsid_hash[SEEN_FSID_HASH_SIZE] = {NULL,};
54
55 static int add_seen_fsid(u8 *fsid)
56 {
57         u8 hash = fsid[0];
58         int slot = hash % SEEN_FSID_HASH_SIZE;
59         struct seen_fsid *seen = seen_fsid_hash[slot];
60         struct seen_fsid *alloc;
61
62         if (!seen)
63                 goto insert;
64
65         while (1) {
66                 if (memcmp(seen->fsid, fsid, BTRFS_FSID_SIZE) == 0)
67                         return -EEXIST;
68
69                 if (!seen->next)
70                         break;
71
72                 seen = seen->next;
73         }
74
75 insert:
76
77         alloc = malloc(sizeof(*alloc));
78         if (!alloc)
79                 return -ENOMEM;
80
81         alloc->next = NULL;
82         memcpy(alloc->fsid, fsid, BTRFS_FSID_SIZE);
83
84         if (seen)
85                 seen->next = alloc;
86         else
87                 seen_fsid_hash[slot] = alloc;
88
89         return 0;
90 }
91
92 static void free_seen_fsid(void)
93 {
94         int slot;
95         struct seen_fsid *seen;
96         struct seen_fsid *next;
97
98         for (slot = 0; slot < SEEN_FSID_HASH_SIZE; slot++) {
99                 seen = seen_fsid_hash[slot];
100                 while (seen) {
101                         next = seen->next;
102                         free(seen);
103                         seen = next;
104                 }
105                 seen_fsid_hash[slot] = NULL;
106         }
107 }
108
109 static const char * const filesystem_cmd_group_usage[] = {
110         "btrfs filesystem [<group>] <command> [<args>]",
111         NULL
112 };
113
114 static const char * const cmd_df_usage[] = {
115         "btrfs filesystem df <path>",
116         "Show space usage information for a mount point",
117         NULL
118 };
119
120 static char *group_type_str(u64 flag)
121 {
122         switch (flag & BTRFS_BLOCK_GROUP_TYPE_MASK) {
123         case BTRFS_BLOCK_GROUP_DATA:
124                 return "Data";
125         case BTRFS_BLOCK_GROUP_SYSTEM:
126                 return "System";
127         case BTRFS_BLOCK_GROUP_METADATA:
128                 return "Metadata";
129         case BTRFS_BLOCK_GROUP_DATA|BTRFS_BLOCK_GROUP_METADATA:
130                 return "Data+Metadata";
131         default:
132                 return "unknown";
133         }
134 }
135
136 static char *group_profile_str(u64 flag)
137 {
138         switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
139         case 0:
140                 return "single";
141         case BTRFS_BLOCK_GROUP_RAID0:
142                 return "RAID0";
143         case BTRFS_BLOCK_GROUP_RAID1:
144                 return "RAID1";
145         case BTRFS_BLOCK_GROUP_RAID5:
146                 return "RAID5";
147         case BTRFS_BLOCK_GROUP_RAID6:
148                 return "RAID6";
149         case BTRFS_BLOCK_GROUP_DUP:
150                 return "DUP";
151         case BTRFS_BLOCK_GROUP_RAID10:
152                 return "RAID10";
153         default:
154                 return "unknown";
155         }
156 }
157
158 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
159 {
160         u64 count = 0;
161         int ret, e;
162         struct btrfs_ioctl_space_args *sargs;
163
164         sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
165         if (!sargs)
166                 return -ENOMEM;
167
168         sargs->space_slots = 0;
169         sargs->total_spaces = 0;
170
171         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
172         e = errno;
173         if (ret) {
174                 fprintf(stderr, "ERROR: couldn't get space info - %s\n",
175                         strerror(e));
176                 free(sargs);
177                 return -e;
178         }
179         /* This really should never happen */
180         if (!sargs->total_spaces) {
181                 free(sargs);
182                 return -ENOENT;
183         }
184         count = sargs->total_spaces;
185         free(sargs);
186
187         sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
188                         (count * sizeof(struct btrfs_ioctl_space_info)));
189         if (!sargs)
190                 ret = -ENOMEM;
191
192         sargs->space_slots = count;
193         sargs->total_spaces = 0;
194         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
195         e = errno;
196         if (ret) {
197                 fprintf(stderr, "ERROR: get space info count %llu - %s\n",
198                                 count, strerror(e));
199                 free(sargs);
200                 return -e;
201         }
202         *sargs_ret = sargs;
203         return 0;
204 }
205
206 static void print_df(struct btrfs_ioctl_space_args *sargs)
207 {
208         u64 i;
209         struct btrfs_ioctl_space_info *sp = sargs->spaces;
210
211         for (i = 0; i < sargs->total_spaces; i++, sp++) {
212                 printf("%s, %s: total=%s, used=%s\n",
213                         group_type_str(sp->flags),
214                         group_profile_str(sp->flags),
215                         pretty_size(sp->total_bytes),
216                         pretty_size(sp->used_bytes));
217         }
218 }
219
220 static int cmd_df(int argc, char **argv)
221 {
222         struct btrfs_ioctl_space_args *sargs = NULL;
223         int ret;
224         int fd;
225         char *path;
226         DIR  *dirstream = NULL;
227
228         if (check_argc_exact(argc, 2))
229                 usage(cmd_df_usage);
230
231         path = argv[1];
232
233         fd = open_file_or_dir(path, &dirstream);
234         if (fd < 0) {
235                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
236                 return 1;
237         }
238         ret = get_df(fd, &sargs);
239
240         if (!ret && sargs) {
241                 print_df(sargs);
242                 free(sargs);
243         } else {
244                 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(-ret));
245         }
246
247         close_file_or_dir(fd, dirstream);
248         return !!ret;
249 }
250
251 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
252 {
253         char uuidbuf[37];
254         struct list_head *cur;
255         struct btrfs_device *device;
256         int search_len = strlen(search);
257
258         search_len = min(search_len, 37);
259         uuid_unparse(fs_devices->fsid, uuidbuf);
260         if (!strncmp(uuidbuf, search, search_len))
261                 return 1;
262
263         list_for_each(cur, &fs_devices->devices) {
264                 device = list_entry(cur, struct btrfs_device, dev_list);
265                 if ((device->label && strcmp(device->label, search) == 0) ||
266                     strcmp(device->name, search) == 0)
267                         return 1;
268         }
269         return 0;
270 }
271
272 /*
273  * Sort devices by devid, ascending
274  */
275 static int cmp_device_id(void *priv, struct list_head *a,
276                 struct list_head *b)
277 {
278         const struct btrfs_device *da = list_entry(a, struct btrfs_device,
279                         dev_list);
280         const struct btrfs_device *db = list_entry(b, struct btrfs_device,
281                         dev_list);
282
283         return da->devid < db->devid ? -1 :
284                 da->devid > db->devid ? 1 : 0;
285 }
286
287 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
288 {
289         char uuidbuf[37];
290         struct list_head *cur;
291         struct btrfs_device *device;
292         u64 devs_found = 0;
293         u64 total;
294
295         if (add_seen_fsid(fs_devices->fsid))
296                 return;
297
298         uuid_unparse(fs_devices->fsid, uuidbuf);
299         device = list_entry(fs_devices->devices.next, struct btrfs_device,
300                             dev_list);
301         if (device->label && device->label[0])
302                 printf("Label: '%s' ", device->label);
303         else
304                 printf("Label: none ");
305
306
307         total = device->total_devs;
308         printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
309                (unsigned long long)total,
310                pretty_size(device->super_bytes_used));
311
312         list_sort(NULL, &fs_devices->devices, cmp_device_id);
313         list_for_each(cur, &fs_devices->devices) {
314                 device = list_entry(cur, struct btrfs_device, dev_list);
315
316                 printf("\tdevid %4llu size %s used %s path %s\n",
317                        (unsigned long long)device->devid,
318                        pretty_size(device->total_bytes),
319                        pretty_size(device->bytes_used), device->name);
320
321                 devs_found++;
322         }
323         if (devs_found < total) {
324                 printf("\t*** Some devices missing\n");
325         }
326         printf("\n");
327 }
328
329 /* adds up all the used spaces as reported by the space info ioctl
330  */
331 static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
332 {
333         u64 ret = 0;
334         int i;
335         for (i = 0; i < si->total_spaces; i++)
336                 ret += si->spaces[i].used_bytes;
337         return ret;
338 }
339
340 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
341                 struct btrfs_ioctl_dev_info_args *dev_info,
342                 struct btrfs_ioctl_space_args *space_info,
343                 char *label, char *path)
344 {
345         int i;
346         char uuidbuf[37];
347         struct btrfs_ioctl_dev_info_args *tmp_dev_info;
348         int ret;
349
350         ret = add_seen_fsid(fs_info->fsid);
351         if (ret == -EEXIST)
352                 return 0;
353         else if (ret)
354                 return ret;
355
356         uuid_unparse(fs_info->fsid, uuidbuf);
357         printf("Label: %s  uuid: %s\n",
358                 strlen(label) ? label : "none", uuidbuf);
359
360         printf("\tTotal devices %llu FS bytes used %s\n",
361                                 fs_info->num_devices,
362                         pretty_size(calc_used_bytes(space_info)));
363
364         for (i = 0; i < fs_info->num_devices; i++) {
365                 tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
366                 printf("\tdevid    %llu size %s used %s path %s\n",
367                         tmp_dev_info->devid,
368                         pretty_size(tmp_dev_info->total_bytes),
369                         pretty_size(tmp_dev_info->bytes_used),
370                         tmp_dev_info->path);
371         }
372
373         printf("\n");
374         return 0;
375 }
376
377 /* This function checks if the given input parameter is
378  * an uuid or a path
379  * return -1: some error in the given input
380  * return 0: unknow input
381  * return 1: given input is uuid
382  * return 2: given input is path
383  */
384 static int check_arg_type(char *input)
385 {
386         uuid_t  out;
387         char path[PATH_MAX];
388
389         if (!input)
390                 return BTRFS_ARG_UNKNOWN;
391
392         if (realpath(input, path)) {
393                 if (is_block_device(input) == 1)
394                         return BTRFS_ARG_BLKDEV;
395
396                 if (is_mount_point(input) == 1)
397                         return BTRFS_ARG_MNTPOINT;
398
399                 return BTRFS_ARG_UNKNOWN;
400         }
401
402         if (!uuid_parse(input, out))
403                 return BTRFS_ARG_UUID;
404
405         return BTRFS_ARG_UNKNOWN;
406 }
407
408 static int btrfs_scan_kernel(void *search)
409 {
410         int ret = 0, fd, type;
411         FILE *f;
412         struct mntent *mnt;
413         struct btrfs_ioctl_fs_info_args fs_info_arg;
414         struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
415         struct btrfs_ioctl_space_args *space_info_arg;
416         char label[BTRFS_LABEL_SIZE];
417         uuid_t uuid;
418
419         f = setmntent("/proc/self/mounts", "r");
420         if (f == NULL)
421                 return 1;
422
423         type = check_arg_type(search);
424         if (type == BTRFS_ARG_BLKDEV)
425                 return 1;
426
427         while ((mnt = getmntent(f)) != NULL) {
428                 if (strcmp(mnt->mnt_type, "btrfs"))
429                         continue;
430                 ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
431                                 &dev_info_arg);
432                 if (ret)
433                         return ret;
434
435                 switch (type) {
436                 case BTRFS_ARG_UUID:
437                         ret = uuid_parse(search, uuid);
438                         if (ret)
439                                 return 1;
440                         if (uuid_compare(fs_info_arg.fsid, uuid))
441                                 continue;
442                         break;
443                 case BTRFS_ARG_MNTPOINT:
444                         if (strcmp(search, mnt->mnt_dir))
445                                 continue;
446                         break;
447                 case BTRFS_ARG_UNKNOWN:
448                         break;
449                 }
450
451                 fd = open(mnt->mnt_dir, O_RDONLY);
452                 if ((fd != -1) && !get_df(fd, &space_info_arg)) {
453                         get_label_mounted(mnt->mnt_dir, label);
454                         print_one_fs(&fs_info_arg, dev_info_arg,
455                                         space_info_arg, label, mnt->mnt_dir);
456                         free(space_info_arg);
457                 }
458                 if (fd != -1)
459                         close(fd);
460                 free(dev_info_arg);
461         }
462         return ret;
463 }
464
465 static const char * const cmd_show_usage[] = {
466         "btrfs filesystem show [options|<path>|<uuid>]",
467         "Show the structure of a filesystem",
468         "-d|--all-devices   show only disks under /dev containing btrfs filesystem",
469         "-m|--mounted       show only mounted btrfs",
470         "If no argument is given, structure of all present filesystems is shown.",
471         NULL
472 };
473
474 static int cmd_show(int argc, char **argv)
475 {
476         struct list_head *all_uuids;
477         struct btrfs_fs_devices *fs_devices;
478         struct list_head *cur_uuid;
479         char *search = NULL;
480         int ret;
481         int where = BTRFS_SCAN_LBLKID;
482         int type = 0;
483         char mp[BTRFS_PATH_NAME_MAX + 1];
484
485         while (1) {
486                 int long_index;
487                 static struct option long_options[] = {
488                         { "all-devices", no_argument, NULL, 'd'},
489                         { "mounted", no_argument, NULL, 'm'},
490                         { NULL, no_argument, NULL, 0 },
491                 };
492                 int c = getopt_long(argc, argv, "dm", long_options,
493                                         &long_index);
494                 if (c < 0)
495                         break;
496                 switch (c) {
497                 case 'd':
498                         where = BTRFS_SCAN_DEV;
499                         break;
500                 case 'm':
501                         where = BTRFS_SCAN_MOUNTED;
502                         break;
503                 default:
504                         usage(cmd_show_usage);
505                 }
506         }
507
508         if (where == BTRFS_SCAN_LBLKID) {
509                 if (check_argc_max(argc, optind + 1))
510                         usage(cmd_show_usage);
511         } else {
512                 if (check_argc_max(argc, optind))
513                         usage(cmd_show_usage);
514         }
515         if (argc > optind) {
516                 search = argv[optind];
517                 type = check_arg_type(search);
518                 if (type == BTRFS_ARG_UNKNOWN) {
519                         fprintf(stderr, "ERROR: arg type unknown\n");
520                         usage(cmd_show_usage);
521                 }
522                 if (type == BTRFS_ARG_BLKDEV) {
523                         ret = get_btrfs_mount(search, mp, sizeof(mp));
524                         if (ret == 0)
525                                 search = mp;
526                 }
527         }
528
529         if (where == BTRFS_SCAN_DEV)
530                 goto devs_only;
531
532         /* show mounted btrfs */
533         btrfs_scan_kernel(search);
534
535         /* shows mounted only */
536         if (where == BTRFS_SCAN_MOUNTED)
537                 goto out;
538
539 devs_only:
540         ret = scan_for_btrfs(where, !BTRFS_UPDATE_KERNEL);
541
542         if (ret) {
543                 fprintf(stderr, "ERROR: %d while scanning\n", ret);
544                 return 1;
545         }
546         
547         all_uuids = btrfs_scanned_uuids();
548         list_for_each(cur_uuid, all_uuids) {
549                 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
550                                         list);
551                 if (search && uuid_search(fs_devices, search) == 0)
552                         continue;
553
554                 print_one_uuid(fs_devices);
555         }
556
557 out:
558         printf("%s\n", BTRFS_BUILD_VERSION);
559         free_seen_fsid();
560         return 0;
561 }
562
563 static const char * const cmd_sync_usage[] = {
564         "btrfs filesystem sync <path>",
565         "Force a sync on a filesystem",
566         NULL
567 };
568
569 static int cmd_sync(int argc, char **argv)
570 {
571         int     fd, res, e;
572         char    *path;
573         DIR     *dirstream = NULL;
574
575         if (check_argc_exact(argc, 2))
576                 usage(cmd_sync_usage);
577
578         path = argv[1];
579
580         fd = open_file_or_dir(path, &dirstream);
581         if (fd < 0) {
582                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
583                 return 1;
584         }
585
586         printf("FSSync '%s'\n", path);
587         res = ioctl(fd, BTRFS_IOC_SYNC);
588         e = errno;
589         close_file_or_dir(fd, dirstream);
590         if( res < 0 ){
591                 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n", 
592                         path, strerror(e));
593                 return 1;
594         }
595
596         return 0;
597 }
598
599 static int parse_compress_type(char *s)
600 {
601         if (strcmp(optarg, "zlib") == 0)
602                 return BTRFS_COMPRESS_ZLIB;
603         else if (strcmp(optarg, "lzo") == 0)
604                 return BTRFS_COMPRESS_LZO;
605         else {
606                 fprintf(stderr, "Unknown compress type %s\n", s);
607                 exit(1);
608         };
609 }
610
611 static const char * const cmd_defrag_usage[] = {
612         "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
613         "Defragment a file or a directory",
614         "",
615         "-v             be verbose",
616         "-r             defragment files recursively",
617         "-c[zlib,lzo]   compress the file while defragmenting",
618         "-f             flush data to disk immediately after defragmenting",
619         "-s start       defragment only from byte onward",
620         "-l len         defragment only up to len bytes",
621         "-t size        minimal size of file to be considered for defragmenting",
622         NULL
623 };
624
625 static int do_defrag(int fd, int fancy_ioctl,
626                 struct btrfs_ioctl_defrag_range_args *range)
627 {
628         int ret;
629
630         if (!fancy_ioctl)
631                 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
632         else
633                 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
634
635         return ret;
636 }
637
638 static int defrag_global_fancy_ioctl;
639 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
640 static int defrag_global_verbose;
641 static int defrag_global_errors;
642 static int defrag_callback(const char *fpath, const struct stat *sb,
643                 int typeflag, struct FTW *ftwbuf)
644 {
645         int ret = 0;
646         int e = 0;
647         int fd = 0;
648
649         if (typeflag == FTW_F) {
650                 if (defrag_global_verbose)
651                         printf("%s\n", fpath);
652                 fd = open(fpath, O_RDWR);
653                 e = errno;
654                 if (fd < 0)
655                         goto error;
656                 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
657                 e = errno;
658                 close(fd);
659                 if (ret && e == ENOTTY) {
660                         fprintf(stderr, "ERROR: defrag range ioctl not "
661                                 "supported in this kernel, please try "
662                                 "without any options.\n");
663                         defrag_global_errors++;
664                         return ENOTTY;
665                 }
666                 if (ret)
667                         goto error;
668         }
669         return 0;
670
671 error:
672         fprintf(stderr, "ERROR: defrag failed on %s - %s\n", fpath, strerror(e));
673         defrag_global_errors++;
674         return 0;
675 }
676
677 static int cmd_defrag(int argc, char **argv)
678 {
679         int fd;
680         int flush = 0;
681         u64 start = 0;
682         u64 len = (u64)-1;
683         u32 thresh = 0;
684         int i;
685         int recursive = 0;
686         int ret = 0;
687         struct btrfs_ioctl_defrag_range_args range;
688         int e = 0;
689         int compress_type = BTRFS_COMPRESS_NONE;
690         DIR *dirstream;
691
692         defrag_global_errors = 0;
693         defrag_global_verbose = 0;
694         defrag_global_errors = 0;
695         defrag_global_fancy_ioctl = 0;
696         optind = 1;
697         while(1) {
698                 int c = getopt(argc, argv, "vrc::fs:l:t:");
699                 if (c < 0)
700                         break;
701
702                 switch(c) {
703                 case 'c':
704                         compress_type = BTRFS_COMPRESS_ZLIB;
705                         if (optarg)
706                                 compress_type = parse_compress_type(optarg);
707                         defrag_global_fancy_ioctl = 1;
708                         break;
709                 case 'f':
710                         flush = 1;
711                         defrag_global_fancy_ioctl = 1;
712                         break;
713                 case 'v':
714                         defrag_global_verbose = 1;
715                         break;
716                 case 's':
717                         start = parse_size(optarg);
718                         defrag_global_fancy_ioctl = 1;
719                         break;
720                 case 'l':
721                         len = parse_size(optarg);
722                         defrag_global_fancy_ioctl = 1;
723                         break;
724                 case 't':
725                         thresh = parse_size(optarg);
726                         defrag_global_fancy_ioctl = 1;
727                         break;
728                 case 'r':
729                         recursive = 1;
730                         break;
731                 default:
732                         usage(cmd_defrag_usage);
733                 }
734         }
735
736         if (check_argc_min(argc - optind, 1))
737                 usage(cmd_defrag_usage);
738
739         memset(&defrag_global_range, 0, sizeof(range));
740         defrag_global_range.start = start;
741         defrag_global_range.len = len;
742         defrag_global_range.extent_thresh = thresh;
743         if (compress_type) {
744                 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
745                 defrag_global_range.compress_type = compress_type;
746         }
747         if (flush)
748                 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
749
750         for (i = optind; i < argc; i++) {
751                 dirstream = NULL;
752                 fd = open_file_or_dir(argv[i], &dirstream);
753                 if (fd < 0) {
754                         fprintf(stderr, "ERROR: failed to open %s - %s\n", argv[i],
755                                         strerror(errno));
756                         defrag_global_errors++;
757                         close_file_or_dir(fd, dirstream);
758                         continue;
759                 }
760                 if (recursive) {
761                         struct stat st;
762
763                         if (fstat(fd, &st)) {
764                                 fprintf(stderr, "ERROR: failed to stat %s - %s\n",
765                                                 argv[i], strerror(errno));
766                                 defrag_global_errors++;
767                                 close_file_or_dir(fd, dirstream);
768                                 continue;
769                         }
770                         if (S_ISDIR(st.st_mode)) {
771                                 ret = nftw(argv[i], defrag_callback, 10,
772                                                 FTW_MOUNT | FTW_PHYS);
773                                 if (ret == ENOTTY)
774                                         exit(1);
775                                 /* errors are handled in the callback */
776                                 ret = 0;
777                         } else {
778                                 if (defrag_global_verbose)
779                                         printf("%s\n", argv[i]);
780                                 ret = do_defrag(fd, defrag_global_fancy_ioctl,
781                                                 &defrag_global_range);
782                                 e = errno;
783                         }
784                 } else {
785                         if (defrag_global_verbose)
786                                 printf("%s\n", argv[i]);
787                         ret = do_defrag(fd, defrag_global_fancy_ioctl,
788                                         &defrag_global_range);
789                         e = errno;
790                 }
791                 close_file_or_dir(fd, dirstream);
792                 if (ret && e == ENOTTY) {
793                         fprintf(stderr, "ERROR: defrag range ioctl not "
794                                 "supported in this kernel, please try "
795                                 "without any options.\n");
796                         defrag_global_errors++;
797                         break;
798                 }
799                 if (ret) {
800                         fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
801                                 argv[i], strerror(e));
802                         defrag_global_errors++;
803                 }
804         }
805         if (defrag_global_verbose)
806                 printf("%s\n", BTRFS_BUILD_VERSION);
807         if (defrag_global_errors)
808                 fprintf(stderr, "total %d failures\n", defrag_global_errors);
809
810         return !!defrag_global_errors;
811 }
812
813 static const char * const cmd_resize_usage[] = {
814         "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
815         "Resize a filesystem",
816         "If 'max' is passed, the filesystem will occupy all available space",
817         "on the device 'devid'.",
818         NULL
819 };
820
821 static int cmd_resize(int argc, char **argv)
822 {
823         struct btrfs_ioctl_vol_args     args;
824         int     fd, res, len, e;
825         char    *amount, *path;
826         DIR     *dirstream = NULL;
827
828         if (check_argc_exact(argc, 3))
829                 usage(cmd_resize_usage);
830
831         amount = argv[1];
832         path = argv[2];
833
834         len = strlen(amount);
835         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
836                 fprintf(stderr, "ERROR: size value too long ('%s)\n",
837                         amount);
838                 return 1;
839         }
840
841         fd = open_file_or_dir(path, &dirstream);
842         if (fd < 0) {
843                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
844                 return 1;
845         }
846
847         printf("Resize '%s' of '%s'\n", path, amount);
848         strncpy_null(args.name, amount);
849         res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
850         e = errno;
851         close_file_or_dir(fd, dirstream);
852         if( res < 0 ){
853                 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n", 
854                         path, strerror(e));
855                 return 1;
856         }
857         return 0;
858 }
859
860 static const char * const cmd_label_usage[] = {
861         "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
862         "Get or change the label of a filesystem",
863         "With one argument, get the label of filesystem on <device>.",
864         "If <newlabel> is passed, set the filesystem label to <newlabel>.",
865         NULL
866 };
867
868 static int cmd_label(int argc, char **argv)
869 {
870         if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
871                 usage(cmd_label_usage);
872
873         if (argc > 2)
874                 return set_label(argv[1], argv[2]);
875         else
876                 return get_label(argv[1]);
877 }
878
879 const struct cmd_group filesystem_cmd_group = {
880         filesystem_cmd_group_usage, NULL, {
881                 { "df", cmd_df, cmd_df_usage, NULL, 0 },
882                 { "show", cmd_show, cmd_show_usage, NULL, 0 },
883                 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
884                 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
885                 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
886                 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
887                 { "label", cmd_label, cmd_label_usage, NULL, 0 },
888                 NULL_CMD_STRUCT
889         }
890 };
891
892 int cmd_filesystem(int argc, char **argv)
893 {
894         return handle_command_group(&filesystem_cmd_group, argc, argv);
895 }