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