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