Btrfs-progs: add support for mixed data+metadata block groups
[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                 int mixed = 0;
724
725                 devfd = open(args[i], O_RDWR);
726                 if (!devfd) {
727                         fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
728                         close(devfd);
729                         ret++;
730                         continue;
731                 }
732                 ret = fstat(devfd, &st);
733                 if (ret) {
734                         fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
735                         close(devfd);
736                         ret++;
737                         continue;
738                 }
739                 if (!S_ISBLK(st.st_mode)) {
740                         fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
741                         close(devfd);
742                         ret++;
743                         continue;
744                 }
745
746                 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed);
747                 if (res) {
748                         fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
749                         close(devfd);
750                         ret++;
751                         continue;
752                 }
753                 close(devfd);
754
755                 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
756                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
757                 e = errno;
758                 if(res<0){
759                         fprintf(stderr, "ERROR: error adding the device '%s' - %s\n", 
760                                 args[i], strerror(e));
761                         ret++;
762                 }
763
764         }
765
766         close(fdmnt);
767         if( ret)
768                 return ret+20;
769         else
770                 return 0;
771
772 }
773
774 int do_balance(int argc, char **argv)
775 {
776
777         int     fdmnt, ret=0, e;
778         struct btrfs_ioctl_vol_args args;
779         char    *path = argv[1];
780
781         fdmnt = open_file_or_dir(path);
782         if (fdmnt < 0) {
783                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
784                 return 12;
785         }
786
787         memset(&args, 0, sizeof(args));
788         ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
789         e = errno;
790         close(fdmnt);
791         if(ret<0){
792                 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n", 
793                         path, strerror(e));
794
795                 return 19;
796         }
797         return 0;
798 }
799 int do_remove_volume(int nargs, char **args)
800 {
801
802         char    *mntpnt = args[nargs-1];
803         int     i, fdmnt, ret=0, e;
804
805         fdmnt = open_file_or_dir(mntpnt);
806         if (fdmnt < 0) {
807                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
808                 return 12;
809         }
810
811         for(i=1 ; i < (nargs-1) ; i++ ){
812                 struct  btrfs_ioctl_vol_args arg;
813                 int     res;
814
815                 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
816                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
817                 e = errno;
818                 if(res<0){
819                         fprintf(stderr, "ERROR: error removing the device '%s' - %s\n", 
820                                 args[i], strerror(e));
821                         ret++;
822                 }
823         }
824
825         close(fdmnt);
826         if( ret)
827                 return ret+20;
828         else
829                 return 0;
830 }
831
832 int do_set_default_subvol(int nargs, char **argv)
833 {
834         int     ret=0, fd, e;
835         u64     objectid;
836         char    *path = argv[2];
837         char    *subvolid = argv[1];
838
839         fd = open_file_or_dir(path);
840         if (fd < 0) {
841                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
842                 return 12;
843         }
844
845         objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
846         if (errno == ERANGE) {
847                 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
848                 return 30;
849         }
850         ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
851         e = errno;
852         close(fd);
853         if( ret < 0 ){
854                 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
855                         strerror(e));
856                 return 30;
857         }
858         return 0;
859 }
860
861 int do_df_filesystem(int nargs, char **argv)
862 {
863         struct btrfs_ioctl_space_args *sargs;
864         u64 count = 0, i;
865         int ret;
866         int fd;
867         int e;
868         char *path = argv[1];
869
870         fd = open_file_or_dir(path);
871         if (fd < 0) {
872                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
873                 return 12;
874         }
875
876         sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
877         if (!sargs)
878                 return -ENOMEM;
879
880         sargs->space_slots = 0;
881         sargs->total_spaces = 0;
882
883         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
884         e = errno;
885         if (ret) {
886                 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
887                         path, strerror(e));
888                 free(sargs);
889                 return ret;
890         }
891         if (!sargs->total_spaces)
892                 return 0;
893
894         count = sargs->total_spaces;
895
896         sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
897                         (count * sizeof(struct btrfs_ioctl_space_info)));
898         if (!sargs)
899                 return -ENOMEM;
900
901         sargs->space_slots = count;
902         sargs->total_spaces = 0;
903
904         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
905         e = errno;
906         if (ret) {
907                 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
908                         path, strerror(e));
909                 close(fd);
910                 free(sargs);
911                 return ret;
912         }
913
914         for (i = 0; i < sargs->total_spaces; i++) {
915                 char description[80];
916                 char *total_bytes;
917                 char *used_bytes;
918                 int written = 0;
919                 u64 flags = sargs->spaces[i].flags;
920
921                 memset(description, 0, 80);
922
923                 if (flags & BTRFS_BLOCK_GROUP_DATA) {
924                         if (flags & BTRFS_BLOCK_GROUP_METADATA) {
925                                 snprintf(description, 15, "%s",
926                                          "Data+Metadata");
927                                 written += 14;
928                         } else {
929                                 snprintf(description, 5, "%s", "Data");
930                                 written += 4;
931                         }
932                 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
933                         snprintf(description, 7, "%s", "System");
934                         written += 6;
935                 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
936                         snprintf(description, 9, "%s", "Metadata");
937                         written += 8;
938                 }
939
940                 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
941                         snprintf(description+written, 8, "%s", ", RAID0");
942                         written += 7;
943                 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
944                         snprintf(description+written, 8, "%s", ", RAID1");
945                         written += 7;
946                 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
947                         snprintf(description+written, 6, "%s", ", DUP");
948                         written += 5;
949                 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
950                         snprintf(description+written, 9, "%s", ", RAID10");
951                         written += 8;
952                 }
953
954                 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
955                 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
956                 printf("%s: total=%s, used=%s\n", description, total_bytes,
957                        used_bytes);
958         }
959         free(sargs);
960
961         return 0;
962 }