btrfs-progs: Improvement for making btrfs image from source directory.
[platform/upstream/btrfs-progs.git] / btrfs_cmds.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
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <dirent.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <libgen.h>
28 #include <limits.h>
29 #include <uuid/uuid.h>
30 #include <ctype.h>
31
32 #undef ULONG_MAX
33
34 #include "kerncompat.h"
35 #include "ctree.h"
36 #include "transaction.h"
37 #include "utils.h"
38 #include "version.h"
39 #include "ioctl.h"
40 #include "volumes.h"
41
42 #include "btrfs_cmds.h"
43 #include "btrfslabel.h"
44
45 #ifdef __CHECKER__
46 #define BLKGETSIZE64 0
47 #define BTRFS_IOC_SNAP_CREATE_V2 0
48 #define BTRFS_VOL_NAME_MAX 255
49 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
50 static inline int ioctl(int fd, int define, void *arg) { return 0; }
51 #endif
52
53 /*
54  * test if path is a subvolume:
55  * this function return
56  * 0-> path exists but it is not a subvolume
57  * 1-> path exists and it is  a subvolume
58  * -1 -> path is unaccessible
59  */
60 static int test_issubvolume(char *path)
61 {
62
63         struct stat     st;
64         int             res;
65
66         res = stat(path, &st);
67         if(res < 0 )
68                 return -1;
69
70         return (st.st_ino == 256) && S_ISDIR(st.st_mode);
71
72 }
73
74 /*
75  * test if path is a directory
76  * this function return
77  * 0-> path exists but it is not a directory
78  * 1-> path exists and it is  a directory
79  * -1 -> path is unaccessible
80  */
81 static int test_isdir(char *path)
82 {
83         struct stat     st;
84         int             res;
85
86         res = stat(path, &st);
87         if(res < 0 )
88                 return -1;
89
90         return S_ISDIR(st.st_mode);
91
92 }
93
94 int open_file_or_dir(const char *fname)
95 {
96         int ret;
97         struct stat st;
98         DIR *dirstream;
99         int fd;
100
101         ret = stat(fname, &st);
102         if (ret < 0) {
103                 return -1;
104         }
105         if (S_ISDIR(st.st_mode)) {
106                 dirstream = opendir(fname);
107                 if (!dirstream) {
108                         return -2;
109                 }
110                 fd = dirfd(dirstream);
111         } else {
112                 fd = open(fname, O_RDWR);
113         }
114         if (fd < 0) {
115                 return -3;
116         }
117         return fd;
118 }
119
120 static u64 parse_size(char *s)
121 {
122         int len = strlen(s);
123         char c;
124         u64 mult = 1;
125
126         if (!isdigit(s[len - 1])) {
127                 c = tolower(s[len - 1]);
128                 switch (c) {
129                 case 'g':
130                         mult *= 1024;
131                 case 'm':
132                         mult *= 1024;
133                 case 'k':
134                         mult *= 1024;
135                 case 'b':
136                         break;
137                 default:
138                         fprintf(stderr, "Unknown size descriptor %c\n", c);
139                         exit(1);
140                 }
141                 s[len - 1] = '\0';
142         }
143         return atoll(s) * mult;
144 }
145
146 static int parse_compress_type(char *s)
147 {
148         if (strcmp(optarg, "zlib") == 0)
149                 return BTRFS_COMPRESS_ZLIB;
150         else if (strcmp(optarg, "lzo") == 0)
151                 return BTRFS_COMPRESS_LZO;
152         else {
153                 fprintf(stderr, "Unknown compress type %s\n", s);
154                 exit(1);
155         };
156 }
157
158 int do_defrag(int ac, char **av)
159 {
160         int fd;
161         int flush = 0;
162         u64 start = 0;
163         u64 len = (u64)-1;
164         u32 thresh = 0;
165         int i;
166         int errors = 0;
167         int ret = 0;
168         int verbose = 0;
169         int fancy_ioctl = 0;
170         struct btrfs_ioctl_defrag_range_args range;
171         int e=0;
172         int compress_type = BTRFS_COMPRESS_NONE;
173
174         optind = 1;
175         while(1) {
176                 int c = getopt(ac, av, "vc::fs:l:t:");
177                 if (c < 0)
178                         break;
179                 switch(c) {
180                 case 'c':
181                         compress_type = BTRFS_COMPRESS_ZLIB;
182                         if (optarg)
183                                 compress_type = parse_compress_type(optarg);
184                         fancy_ioctl = 1;
185                         break;
186                 case 'f':
187                         flush = 1;
188                         fancy_ioctl = 1;
189                         break;
190                 case 'v':
191                         verbose = 1;
192                         break;
193                 case 's':
194                         start = parse_size(optarg);
195                         fancy_ioctl = 1;
196                         break;
197                 case 'l':
198                         len = parse_size(optarg);
199                         fancy_ioctl = 1;
200                         break;
201                 case 't':
202                         thresh = parse_size(optarg);
203                         fancy_ioctl = 1;
204                         break;
205                 default:
206                         fprintf(stderr, "Invalid arguments for defragment\n");
207                         free(av);
208                         return 1;
209                 }
210         }
211         if (ac - optind == 0) {
212                 fprintf(stderr, "Invalid arguments for defragment\n");
213                 free(av);
214                 return 1;
215         }
216
217         memset(&range, 0, sizeof(range));
218         range.start = start;
219         range.len = len;
220         range.extent_thresh = thresh;
221         if (compress_type) {
222                 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
223                 range.compress_type = compress_type;
224         }
225         if (flush)
226                 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
227
228         for (i = optind; i < ac; i++) {
229                 if (verbose)
230                         printf("%s\n", av[i]);
231                 fd = open_file_or_dir(av[i]);
232                 if (fd < 0) {
233                         fprintf(stderr, "failed to open %s\n", av[i]);
234                         perror("open:");
235                         errors++;
236                         continue;
237                 }
238                 if (!fancy_ioctl) {
239                         ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
240                         e=errno;
241                 } else {
242                         ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
243                         if (ret && errno == ENOTTY) {
244                                 fprintf(stderr, "ERROR: defrag range ioctl not "
245                                         "supported in this kernel, please try "
246                                         "without any options.\n");
247                                 errors++;
248                                 close(fd);
249                                 break;
250                         }
251                 }
252                 if (ret) {
253                         fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
254                                 av[i], strerror(e));
255                         errors++;
256                 }
257                 close(fd);
258         }
259         if (verbose)
260                 printf("%s\n", BTRFS_BUILD_VERSION);
261         if (errors) {
262                 fprintf(stderr, "total %d failures\n", errors);
263                 exit(1);
264         }
265
266         free(av);
267         return errors + 20;
268 }
269
270 int do_find_newer(int argc, char **argv)
271 {
272         int fd;
273         int ret;
274         char *subvol;
275         u64 last_gen;
276
277         subvol = argv[1];
278         last_gen = atoll(argv[2]);
279
280         ret = test_issubvolume(subvol);
281         if (ret < 0) {
282                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
283                 return 12;
284         }
285         if (!ret) {
286                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
287                 return 13;
288         }
289
290         fd = open_file_or_dir(subvol);
291         if (fd < 0) {
292                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
293                 return 12;
294         }
295         ret = find_updated_files(fd, 0, last_gen);
296         if (ret)
297                 return 19;
298         return 0;
299 }
300
301 int do_subvol_list(int argc, char **argv)
302 {
303         int fd;
304         int ret;
305         int print_parent = 0;
306         char *subvol;
307         int optind = 1;
308
309         while(1) {
310                 int c = getopt(argc, argv, "p");
311                 if (c < 0) break;
312                 switch(c) {
313                 case 'p':
314                         print_parent = 1;
315                         optind++;
316                         break;
317                 }
318         }
319         
320         if (argc - optind != 1) {
321                 fprintf(stderr, "ERROR: invalid arguments for subvolume list\n");
322                 return 1;
323         }
324
325         subvol = argv[optind];
326
327         ret = test_issubvolume(subvol);
328         if (ret < 0) {
329                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
330                 return 12;
331         }
332         if (!ret) {
333                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
334                 return 13;
335         }
336
337         fd = open_file_or_dir(subvol);
338         if (fd < 0) {
339                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
340                 return 12;
341         }
342         ret = list_subvols(fd, print_parent, 0);
343         if (ret)
344                 return 19;
345         return 0;
346 }
347
348 int do_clone(int argc, char **argv)
349 {
350         char    *subvol, *dst;
351         int     res, fd, fddst, len, e, optind = 0, readonly = 0;
352         char    *newname;
353         char    *dstdir;
354         struct btrfs_ioctl_vol_args_v2  args;
355
356         memset(&args, 0, sizeof(args));
357
358         while (1) {
359                 int c = getopt(argc, argv, "r");
360
361                 if (c < 0)
362                         break;
363                 switch (c) {
364                 case 'r':
365                         optind++;
366                         readonly = 1;
367                         break;
368                 default:
369                         fprintf(stderr,
370                                 "Invalid arguments for subvolume snapshot\n");
371                         free(argv);
372                         return 1;
373                 }
374         }
375         if (argc - optind != 3) {
376                 fprintf(stderr, "Invalid arguments for subvolume snapshot\n");
377                 free(argv);
378                 return 1;
379         }
380
381         subvol = argv[optind+1];
382         dst = argv[optind+2];
383
384         res = test_issubvolume(subvol);
385         if(res<0){
386                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
387                 return 12;
388         }
389         if(!res){
390                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
391                 return 13;
392         }
393
394         res = test_isdir(dst);
395         if(res == 0 ){
396                 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
397                 return 12;
398         }
399
400         if(res>0){
401                 newname = strdup(subvol);
402                 newname = basename(newname);
403                 dstdir = dst;
404         }else{
405                 newname = strdup(dst);
406                 newname = basename(newname);
407                 dstdir = strdup(dst);
408                 dstdir = dirname(dstdir);
409         }
410
411         if( !strcmp(newname,".") || !strcmp(newname,"..") ||
412              strchr(newname, '/') ){
413                 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
414                         newname);
415                 return 14;
416         }
417
418         len = strlen(newname);
419         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
420                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
421                         newname);
422                 return 14;
423         }
424
425         fddst = open_file_or_dir(dstdir);
426         if (fddst < 0) {
427                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
428                 return 12;
429         }
430
431         fd = open_file_or_dir(subvol);
432         if (fd < 0) {
433                 close(fddst);
434                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
435                 return 12;
436         }
437
438         if (readonly) {
439                 args.flags |= BTRFS_SUBVOL_RDONLY;
440                 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
441                        subvol, dstdir, newname);
442         } else {
443                 printf("Create a snapshot of '%s' in '%s/%s'\n",
444                        subvol, dstdir, newname);
445         }
446
447         args.fd = fd;
448         strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
449         res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
450         e = errno;
451
452         close(fd);
453         close(fddst);
454
455         if(res < 0 ){
456                 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
457                         subvol, strerror(e));
458                 return 11;
459         }
460
461         return 0;
462
463 }
464
465 int do_delete_subvolume(int argc, char **argv)
466 {
467         int     res, fd, len, e;
468         struct btrfs_ioctl_vol_args     args;
469         char    *dname, *vname, *cpath;
470         char    *path = argv[1];
471
472         res = test_issubvolume(path);
473         if(res<0){
474                 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
475                 return 12;
476         }
477         if(!res){
478                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
479                 return 13;
480         }
481
482         cpath = realpath(path, 0);
483         dname = strdup(cpath);
484         dname = dirname(dname);
485         vname = strdup(cpath);
486         vname = basename(vname);
487         free(cpath);
488
489         if( !strcmp(vname,".") || !strcmp(vname,"..") ||
490              strchr(vname, '/') ){
491                 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
492                         vname);
493                 return 14;
494         }
495
496         len = strlen(vname);
497         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
498                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
499                         vname);
500                 return 14;
501         }
502
503         fd = open_file_or_dir(dname);
504         if (fd < 0) {
505                 close(fd);
506                 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
507                 return 12;
508         }
509
510         printf("Delete subvolume '%s/%s'\n", dname, vname);
511         strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
512         res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
513         e = errno;
514
515         close(fd);
516
517         if(res < 0 ){
518                 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
519                         dname, vname, strerror(e));
520                 return 11;
521         }
522
523         return 0;
524
525 }
526
527 int do_create_subvol(int argc, char **argv)
528 {
529         int     res, fddst, len, e;
530         char    *newname;
531         char    *dstdir;
532         struct btrfs_ioctl_vol_args     args;
533         char    *dst = argv[1];
534
535         res = test_isdir(dst);
536         if(res >= 0 ){
537                 fprintf(stderr, "ERROR: '%s' exists\n", dst);
538                 return 12;
539         }
540
541         newname = strdup(dst);
542         newname = basename(newname);
543         dstdir = strdup(dst);
544         dstdir = dirname(dstdir);
545
546         if( !strcmp(newname,".") || !strcmp(newname,"..") ||
547              strchr(newname, '/') ){
548                 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
549                         newname);
550                 return 14;
551         }
552
553         len = strlen(newname);
554         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
555                 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
556                         newname);
557                 return 14;
558         }
559
560         fddst = open_file_or_dir(dstdir);
561         if (fddst < 0) {
562                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
563                 return 12;
564         }
565
566         printf("Create subvolume '%s/%s'\n", dstdir, newname);
567         strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
568         res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
569         e = errno;
570
571         close(fddst);
572
573         if(res < 0 ){
574                 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
575                         strerror(e));
576                 return 11;
577         }
578
579         return 0;
580
581 }
582
583 int do_fssync(int argc, char **argv)
584 {
585         int     fd, res, e;
586         char    *path = argv[1];
587
588         fd = open_file_or_dir(path);
589         if (fd < 0) {
590                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
591                 return 12;
592         }
593
594         printf("FSSync '%s'\n", path);
595         res = ioctl(fd, BTRFS_IOC_SYNC);
596         e = errno;
597         close(fd);
598         if( res < 0 ){
599                 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n", 
600                         path, strerror(e));
601                 return 16;
602         }
603
604         return 0;
605 }
606
607 int do_scan(int argc, char **argv)
608 {
609         int     i, fd, e;
610         int     checklist = 1;
611         int     devstart = 1;
612
613         if( argc >= 2 && !strcmp(argv[1],"--all-devices")){
614
615                 if( argc >2 ){
616                         fprintf(stderr, "ERROR: too may arguments\n");
617                         return 22;
618                 }
619
620                 checklist = 0;
621                 devstart += 1;
622         }
623
624         if(argc<=devstart){
625
626                 int ret;
627
628                 printf("Scanning for Btrfs filesystems\n");
629                 if(checklist)
630                         ret = btrfs_scan_block_devices(1);
631                 else
632                         ret = btrfs_scan_one_dir("/dev", 1);
633                 if (ret){
634                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
635                         return 18;
636                 }
637                 return 0;
638         }
639
640         fd = open("/dev/btrfs-control", O_RDWR);
641         if (fd < 0) {
642                 perror("failed to open /dev/btrfs-control");
643                 return 10;
644         }
645
646         for( i = devstart ; i < argc ; i++ ){
647                 struct btrfs_ioctl_vol_args args;
648                 int ret;
649
650                 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
651
652                 strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
653                 /*
654                  * FIXME: which are the error code returned by this ioctl ?
655                  * it seems that is impossible to understand if there no is
656                  * a btrfs filesystem from an I/O error !!!
657                  */
658                 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
659                 e = errno;
660
661                 if( ret < 0 ){
662                         close(fd);
663                         fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n", 
664                                 argv[i], strerror(e));
665                         return 11;
666                 }
667         }
668
669         close(fd);
670         return 0;
671
672 }
673
674 int do_resize(int argc, char **argv)
675 {
676
677         struct btrfs_ioctl_vol_args     args;
678         int     fd, res, len, e;
679         char    *amount=argv[1], *path=argv[2];
680
681         fd = open_file_or_dir(path);
682         if (fd < 0) {
683                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
684                 return 12;
685         }
686         len = strlen(amount);
687         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
688                 fprintf(stderr, "ERROR: size value too long ('%s)\n",
689                         amount);
690                 return 14;
691         }
692
693         printf("Resize '%s' of '%s'\n", path, amount);
694         strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
695         res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
696         e = errno;
697         close(fd);
698         if( res < 0 ){
699                 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n", 
700                         path, strerror(e));
701                 return 30;
702         }
703         return 0;
704 }
705
706 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
707 {
708         struct list_head *cur;
709         struct btrfs_device *device;
710
711         list_for_each(cur, &fs_devices->devices) {
712                 device = list_entry(cur, struct btrfs_device, dev_list);
713                 if ((device->label && strcmp(device->label, search) == 0) ||
714                     strcmp(device->name, search) == 0)
715                         return 1;
716         }
717         return 0;
718 }
719
720 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
721 {
722         char uuidbuf[37];
723         struct list_head *cur;
724         struct btrfs_device *device;
725         char *super_bytes_used;
726         u64 devs_found = 0;
727         u64 total;
728
729         uuid_unparse(fs_devices->fsid, uuidbuf);
730         device = list_entry(fs_devices->devices.next, struct btrfs_device,
731                             dev_list);
732         if (device->label && device->label[0])
733                 printf("Label: '%s' ", device->label);
734         else
735                 printf("Label: none ");
736
737         super_bytes_used = pretty_sizes(device->super_bytes_used);
738
739         total = device->total_devs;
740         printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
741                (unsigned long long)total, super_bytes_used);
742
743         free(super_bytes_used);
744
745         list_for_each(cur, &fs_devices->devices) {
746                 char *total_bytes;
747                 char *bytes_used;
748                 device = list_entry(cur, struct btrfs_device, dev_list);
749                 total_bytes = pretty_sizes(device->total_bytes);
750                 bytes_used = pretty_sizes(device->bytes_used);
751                 printf("\tdevid %4llu size %s used %s path %s\n",
752                        (unsigned long long)device->devid,
753                        total_bytes, bytes_used, device->name);
754                 free(total_bytes);
755                 free(bytes_used);
756                 devs_found++;
757         }
758         if (devs_found < total) {
759                 printf("\t*** Some devices missing\n");
760         }
761         printf("\n");
762 }
763
764 int do_show_filesystem(int argc, char **argv)
765 {
766         struct list_head *all_uuids;
767         struct btrfs_fs_devices *fs_devices;
768         struct list_head *cur_uuid;
769         char *search = 0;
770         int ret;
771         int checklist = 1;
772         int searchstart = 1;
773
774         if( argc >= 2 && !strcmp(argv[1],"--all-devices")){
775                 checklist = 0;
776                 searchstart += 1;
777         }
778
779         if( argc > searchstart+1 ){
780                 fprintf(stderr, "ERROR: too many arguments\n");
781                 return 22;
782         }       
783
784         if(checklist)
785                 ret = btrfs_scan_block_devices(0);
786         else
787                 ret = btrfs_scan_one_dir("/dev", 0);
788
789         if (ret){
790                 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
791                 return 18;
792         }
793         
794         if(searchstart < argc)
795                 search = argv[searchstart];
796
797         all_uuids = btrfs_scanned_uuids();
798         list_for_each(cur_uuid, all_uuids) {
799                 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
800                                         list);
801                 if (search && uuid_search(fs_devices, search) == 0)
802                         continue;
803                 print_one_uuid(fs_devices);
804         }
805         printf("%s\n", BTRFS_BUILD_VERSION);
806         return 0;
807 }
808
809 int do_add_volume(int nargs, char **args)
810 {
811
812         char    *mntpnt = args[nargs-1];
813         int     i, fdmnt, ret=0, e;
814
815
816         fdmnt = open_file_or_dir(mntpnt);
817         if (fdmnt < 0) {
818                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
819                 return 12;
820         }
821
822         for (i = 1; i < (nargs-1); i++ ){
823                 struct btrfs_ioctl_vol_args ioctl_args;
824                 int     devfd, res;
825                 u64 dev_block_count = 0;
826                 struct stat st;
827                 int mixed = 0;
828
829                 res = check_mounted(args[i]);
830                 if (res < 0) {
831                         fprintf(stderr, "error checking %s mount status\n",
832                                 args[i]);
833                         ret++;
834                         continue;
835                 }
836                 if (res == 1) {
837                         fprintf(stderr, "%s is mounted\n", args[i]);
838                         ret++;
839                         continue;
840                 }
841
842                 devfd = open(args[i], O_RDWR);
843                 if (!devfd) {
844                         fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
845                         close(devfd);
846                         ret++;
847                         continue;
848                 }
849                 res = fstat(devfd, &st);
850                 if (res) {
851                         fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
852                         close(devfd);
853                         ret++;
854                         continue;
855                 }
856                 if (!S_ISBLK(st.st_mode)) {
857                         fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
858                         close(devfd);
859                         ret++;
860                         continue;
861                 }
862
863                 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed);
864                 if (res) {
865                         fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
866                         close(devfd);
867                         ret++;
868                         continue;
869                 }
870                 close(devfd);
871
872                 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
873                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
874                 e = errno;
875                 if(res<0){
876                         fprintf(stderr, "ERROR: error adding the device '%s' - %s\n", 
877                                 args[i], strerror(e));
878                         ret++;
879                 }
880
881         }
882
883         close(fdmnt);
884         if (ret)
885                 return ret+20;
886         else
887                 return 0;
888
889 }
890
891 int do_balance(int argc, char **argv)
892 {
893
894         int     fdmnt, ret=0, e;
895         struct btrfs_ioctl_vol_args args;
896         char    *path = argv[1];
897
898         fdmnt = open_file_or_dir(path);
899         if (fdmnt < 0) {
900                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
901                 return 12;
902         }
903
904         memset(&args, 0, sizeof(args));
905         ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
906         e = errno;
907         close(fdmnt);
908         if(ret<0){
909                 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n", 
910                         path, strerror(e));
911
912                 return 19;
913         }
914         return 0;
915 }
916 int do_remove_volume(int nargs, char **args)
917 {
918
919         char    *mntpnt = args[nargs-1];
920         int     i, fdmnt, ret=0, e;
921
922         fdmnt = open_file_or_dir(mntpnt);
923         if (fdmnt < 0) {
924                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
925                 return 12;
926         }
927
928         for(i=1 ; i < (nargs-1) ; i++ ){
929                 struct  btrfs_ioctl_vol_args arg;
930                 int     res;
931
932                 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
933                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
934                 e = errno;
935                 if(res<0){
936                         fprintf(stderr, "ERROR: error removing the device '%s' - %s\n", 
937                                 args[i], strerror(e));
938                         ret++;
939                 }
940         }
941
942         close(fdmnt);
943         if( ret)
944                 return ret+20;
945         else
946                 return 0;
947 }
948
949 int do_set_default_subvol(int nargs, char **argv)
950 {
951         int     ret=0, fd, e;
952         u64     objectid;
953         char    *path = argv[2];
954         char    *subvolid = argv[1];
955
956         fd = open_file_or_dir(path);
957         if (fd < 0) {
958                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
959                 return 12;
960         }
961
962         objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
963         if (errno == ERANGE) {
964                 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
965                 return 30;
966         }
967         ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
968         e = errno;
969         close(fd);
970         if( ret < 0 ){
971                 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
972                         strerror(e));
973                 return 30;
974         }
975         return 0;
976 }
977
978 int do_change_label(int nargs, char **argv)
979 {
980         /* check the number of argument */
981         if ( nargs > 3 ){
982                 fprintf(stderr, "ERROR: '%s' requires maximum 2 args\n",
983                         argv[0]);
984                 return -2;
985         }else if (nargs == 2){
986                 return get_label(argv[1]);
987         } else {        /* nargs == 0 */
988                 return set_label(argv[1], argv[2]);
989         }
990 }
991
992
993 int do_get_default_subvol(int nargs, char **argv)
994 {
995         int fd;
996         int ret;
997         char *subvol;
998
999         subvol = argv[1];
1000
1001         ret = test_issubvolume(subvol);
1002         if (ret < 0) {
1003                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
1004                 return 12;
1005         }
1006         if (!ret) {
1007                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
1008                 return 13;
1009         }
1010
1011         fd = open_file_or_dir(subvol);
1012         if (fd < 0) {
1013                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
1014                 return 12;
1015         }
1016         ret = list_subvols(fd, 0, 1);
1017         if (ret)
1018                 return 19;
1019         return 0;
1020 }
1021
1022 int do_df_filesystem(int nargs, char **argv)
1023 {
1024         struct btrfs_ioctl_space_args *sargs;
1025         u64 count = 0, i;
1026         int ret;
1027         int fd;
1028         int e;
1029         char *path = argv[1];
1030
1031         fd = open_file_or_dir(path);
1032         if (fd < 0) {
1033                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
1034                 return 12;
1035         }
1036
1037         sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
1038         if (!sargs)
1039                 return -ENOMEM;
1040
1041         sargs->space_slots = 0;
1042         sargs->total_spaces = 0;
1043
1044         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
1045         e = errno;
1046         if (ret) {
1047                 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
1048                         path, strerror(e));
1049                 free(sargs);
1050                 return ret;
1051         }
1052         if (!sargs->total_spaces)
1053                 return 0;
1054
1055         count = sargs->total_spaces;
1056
1057         sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
1058                         (count * sizeof(struct btrfs_ioctl_space_info)));
1059         if (!sargs)
1060                 return -ENOMEM;
1061
1062         sargs->space_slots = count;
1063         sargs->total_spaces = 0;
1064
1065         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
1066         e = errno;
1067         if (ret) {
1068                 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
1069                         path, strerror(e));
1070                 close(fd);
1071                 free(sargs);
1072                 return ret;
1073         }
1074
1075         for (i = 0; i < sargs->total_spaces; i++) {
1076                 char description[80];
1077                 char *total_bytes;
1078                 char *used_bytes;
1079                 int written = 0;
1080                 u64 flags = sargs->spaces[i].flags;
1081
1082                 memset(description, 0, 80);
1083
1084                 if (flags & BTRFS_BLOCK_GROUP_DATA) {
1085                         if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1086                                 snprintf(description, 15, "%s",
1087                                          "Data+Metadata");
1088                                 written += 14;
1089                         } else {
1090                                 snprintf(description, 5, "%s", "Data");
1091                                 written += 4;
1092                         }
1093                 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
1094                         snprintf(description, 7, "%s", "System");
1095                         written += 6;
1096                 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1097                         snprintf(description, 9, "%s", "Metadata");
1098                         written += 8;
1099                 }
1100
1101                 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
1102                         snprintf(description+written, 8, "%s", ", RAID0");
1103                         written += 7;
1104                 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
1105                         snprintf(description+written, 8, "%s", ", RAID1");
1106                         written += 7;
1107                 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
1108                         snprintf(description+written, 6, "%s", ", DUP");
1109                         written += 5;
1110                 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
1111                         snprintf(description+written, 9, "%s", ", RAID10");
1112                         written += 8;
1113                 }
1114
1115                 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
1116                 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
1117                 printf("%s: total=%s, used=%s\n", description, total_bytes,
1118                        used_bytes);
1119         }
1120         free(sargs);
1121
1122         return 0;
1123 }