print parent ID in btrfs suvolume list
[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 static 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);
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 < 2) {
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         if(argc<=1){
611                 int ret;
612
613                 printf("Scanning for Btrfs filesystems\n");
614                 ret = btrfs_scan_one_dir("/dev", 1);
615                 if (ret){
616                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
617                         return 18;
618                 }
619                 return 0;
620         }
621
622         fd = open("/dev/btrfs-control", O_RDWR);
623         if (fd < 0) {
624                 perror("failed to open /dev/btrfs-control");
625                 return 10;
626         }
627
628         for( i = 1 ; i < argc ; i++ ){
629                 struct btrfs_ioctl_vol_args args;
630                 int ret;
631
632                 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
633
634                 strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
635                 /*
636                  * FIXME: which are the error code returned by this ioctl ?
637                  * it seems that is impossible to understand if there no is
638                  * a btrfs filesystem from an I/O error !!!
639                  */
640                 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
641                 e = errno;
642
643                 if( ret < 0 ){
644                         close(fd);
645                         fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n", 
646                                 argv[i], strerror(e));
647                         return 11;
648                 }
649         }
650
651         close(fd);
652         return 0;
653
654 }
655
656 int do_resize(int argc, char **argv)
657 {
658
659         struct btrfs_ioctl_vol_args     args;
660         int     fd, res, len, e;
661         char    *amount=argv[1], *path=argv[2];
662
663         fd = open_file_or_dir(path);
664         if (fd < 0) {
665                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
666                 return 12;
667         }
668         len = strlen(amount);
669         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
670                 fprintf(stderr, "ERROR: size value too long ('%s)\n",
671                         amount);
672                 return 14;
673         }
674
675         printf("Resize '%s' of '%s'\n", path, amount);
676         strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
677         res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
678         e = errno;
679         close(fd);
680         if( res < 0 ){
681                 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n", 
682                         path, strerror(e));
683                 return 30;
684         }
685         return 0;
686 }
687
688 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
689 {
690         struct list_head *cur;
691         struct btrfs_device *device;
692
693         list_for_each(cur, &fs_devices->devices) {
694                 device = list_entry(cur, struct btrfs_device, dev_list);
695                 if ((device->label && strcmp(device->label, search) == 0) ||
696                     strcmp(device->name, search) == 0)
697                         return 1;
698         }
699         return 0;
700 }
701
702 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
703 {
704         char uuidbuf[37];
705         struct list_head *cur;
706         struct btrfs_device *device;
707         char *super_bytes_used;
708         u64 devs_found = 0;
709         u64 total;
710
711         uuid_unparse(fs_devices->fsid, uuidbuf);
712         device = list_entry(fs_devices->devices.next, struct btrfs_device,
713                             dev_list);
714         if (device->label && device->label[0])
715                 printf("Label: '%s' ", device->label);
716         else
717                 printf("Label: none ");
718
719         super_bytes_used = pretty_sizes(device->super_bytes_used);
720
721         total = device->total_devs;
722         printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
723                (unsigned long long)total, super_bytes_used);
724
725         free(super_bytes_used);
726
727         list_for_each(cur, &fs_devices->devices) {
728                 char *total_bytes;
729                 char *bytes_used;
730                 device = list_entry(cur, struct btrfs_device, dev_list);
731                 total_bytes = pretty_sizes(device->total_bytes);
732                 bytes_used = pretty_sizes(device->bytes_used);
733                 printf("\tdevid %4llu size %s used %s path %s\n",
734                        (unsigned long long)device->devid,
735                        total_bytes, bytes_used, device->name);
736                 free(total_bytes);
737                 free(bytes_used);
738                 devs_found++;
739         }
740         if (devs_found < total) {
741                 printf("\t*** Some devices missing\n");
742         }
743         printf("\n");
744 }
745
746 int do_show_filesystem(int argc, char **argv)
747 {
748         struct list_head *all_uuids;
749         struct btrfs_fs_devices *fs_devices;
750         struct list_head *cur_uuid;
751         char *search = argv[1];
752         int ret;
753
754         ret = btrfs_scan_one_dir("/dev", 0);
755         if (ret){
756                 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
757                 return 18;
758         }
759
760         all_uuids = btrfs_scanned_uuids();
761         list_for_each(cur_uuid, all_uuids) {
762                 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
763                                         list);
764                 if (search && uuid_search(fs_devices, search) == 0)
765                         continue;
766                 print_one_uuid(fs_devices);
767         }
768         printf("%s\n", BTRFS_BUILD_VERSION);
769         return 0;
770 }
771
772 int do_add_volume(int nargs, char **args)
773 {
774
775         char    *mntpnt = args[nargs-1];
776         int     i, fdmnt, ret=0, e;
777
778
779         fdmnt = open_file_or_dir(mntpnt);
780         if (fdmnt < 0) {
781                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
782                 return 12;
783         }
784
785         for (i = 1; i < (nargs-1); i++ ){
786                 struct btrfs_ioctl_vol_args ioctl_args;
787                 int     devfd, res;
788                 u64 dev_block_count = 0;
789                 struct stat st;
790                 int mixed = 0;
791
792                 res = check_mounted(args[i]);
793                 if (res < 0) {
794                         fprintf(stderr, "error checking %s mount status\n",
795                                 args[i]);
796                         ret++;
797                         continue;
798                 }
799                 if (res == 1) {
800                         fprintf(stderr, "%s is mounted\n", args[i]);
801                         ret++;
802                         continue;
803                 }
804
805                 devfd = open(args[i], O_RDWR);
806                 if (!devfd) {
807                         fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
808                         close(devfd);
809                         ret++;
810                         continue;
811                 }
812                 res = fstat(devfd, &st);
813                 if (res) {
814                         fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
815                         close(devfd);
816                         ret++;
817                         continue;
818                 }
819                 if (!S_ISBLK(st.st_mode)) {
820                         fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
821                         close(devfd);
822                         ret++;
823                         continue;
824                 }
825
826                 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed);
827                 if (res) {
828                         fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
829                         close(devfd);
830                         ret++;
831                         continue;
832                 }
833                 close(devfd);
834
835                 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
836                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
837                 e = errno;
838                 if(res<0){
839                         fprintf(stderr, "ERROR: error adding the device '%s' - %s\n", 
840                                 args[i], strerror(e));
841                         ret++;
842                 }
843
844         }
845
846         close(fdmnt);
847         if (ret)
848                 return ret+20;
849         else
850                 return 0;
851
852 }
853
854 int do_balance(int argc, char **argv)
855 {
856
857         int     fdmnt, ret=0, e;
858         struct btrfs_ioctl_vol_args args;
859         char    *path = argv[1];
860
861         fdmnt = open_file_or_dir(path);
862         if (fdmnt < 0) {
863                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
864                 return 12;
865         }
866
867         memset(&args, 0, sizeof(args));
868         ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
869         e = errno;
870         close(fdmnt);
871         if(ret<0){
872                 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n", 
873                         path, strerror(e));
874
875                 return 19;
876         }
877         return 0;
878 }
879 int do_remove_volume(int nargs, char **args)
880 {
881
882         char    *mntpnt = args[nargs-1];
883         int     i, fdmnt, ret=0, e;
884
885         fdmnt = open_file_or_dir(mntpnt);
886         if (fdmnt < 0) {
887                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
888                 return 12;
889         }
890
891         for(i=1 ; i < (nargs-1) ; i++ ){
892                 struct  btrfs_ioctl_vol_args arg;
893                 int     res;
894
895                 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
896                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
897                 e = errno;
898                 if(res<0){
899                         fprintf(stderr, "ERROR: error removing the device '%s' - %s\n", 
900                                 args[i], strerror(e));
901                         ret++;
902                 }
903         }
904
905         close(fdmnt);
906         if( ret)
907                 return ret+20;
908         else
909                 return 0;
910 }
911
912 int do_set_default_subvol(int nargs, char **argv)
913 {
914         int     ret=0, fd, e;
915         u64     objectid;
916         char    *path = argv[2];
917         char    *subvolid = argv[1];
918
919         fd = open_file_or_dir(path);
920         if (fd < 0) {
921                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
922                 return 12;
923         }
924
925         objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
926         if (errno == ERANGE) {
927                 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
928                 return 30;
929         }
930         ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
931         e = errno;
932         close(fd);
933         if( ret < 0 ){
934                 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
935                         strerror(e));
936                 return 30;
937         }
938         return 0;
939 }
940
941 int do_change_label(int nargs, char **argv)
942 {
943         /* check the number of argument */
944         if ( nargs > 3 ){
945                 fprintf(stderr, "ERROR: '%s' requires maximum 2 args\n",
946                         argv[0]);
947                 return -2;
948         }else if (nargs == 2){
949                 return get_label(argv[1]);
950         } else {        /* nargs == 0 */
951                 return set_label(argv[1], argv[2]);
952         }
953 }
954
955
956 int do_df_filesystem(int nargs, char **argv)
957 {
958         struct btrfs_ioctl_space_args *sargs;
959         u64 count = 0, i;
960         int ret;
961         int fd;
962         int e;
963         char *path = argv[1];
964
965         fd = open_file_or_dir(path);
966         if (fd < 0) {
967                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
968                 return 12;
969         }
970
971         sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
972         if (!sargs)
973                 return -ENOMEM;
974
975         sargs->space_slots = 0;
976         sargs->total_spaces = 0;
977
978         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
979         e = errno;
980         if (ret) {
981                 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
982                         path, strerror(e));
983                 free(sargs);
984                 return ret;
985         }
986         if (!sargs->total_spaces)
987                 return 0;
988
989         count = sargs->total_spaces;
990
991         sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
992                         (count * sizeof(struct btrfs_ioctl_space_info)));
993         if (!sargs)
994                 return -ENOMEM;
995
996         sargs->space_slots = count;
997         sargs->total_spaces = 0;
998
999         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
1000         e = errno;
1001         if (ret) {
1002                 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
1003                         path, strerror(e));
1004                 close(fd);
1005                 free(sargs);
1006                 return ret;
1007         }
1008
1009         for (i = 0; i < sargs->total_spaces; i++) {
1010                 char description[80];
1011                 char *total_bytes;
1012                 char *used_bytes;
1013                 int written = 0;
1014                 u64 flags = sargs->spaces[i].flags;
1015
1016                 memset(description, 0, 80);
1017
1018                 if (flags & BTRFS_BLOCK_GROUP_DATA) {
1019                         if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1020                                 snprintf(description, 15, "%s",
1021                                          "Data+Metadata");
1022                                 written += 14;
1023                         } else {
1024                                 snprintf(description, 5, "%s", "Data");
1025                                 written += 4;
1026                         }
1027                 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
1028                         snprintf(description, 7, "%s", "System");
1029                         written += 6;
1030                 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1031                         snprintf(description, 9, "%s", "Metadata");
1032                         written += 8;
1033                 }
1034
1035                 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
1036                         snprintf(description+written, 8, "%s", ", RAID0");
1037                         written += 7;
1038                 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
1039                         snprintf(description+written, 8, "%s", ", RAID1");
1040                         written += 7;
1041                 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
1042                         snprintf(description+written, 6, "%s", ", DUP");
1043                         written += 5;
1044                 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
1045                         snprintf(description+written, 9, "%s", ", RAID10");
1046                         written += 8;
1047                 }
1048
1049                 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
1050                 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
1051                 printf("%s: total=%s, used=%s\n", description, total_bytes,
1052                        used_bytes);
1053         }
1054         free(sargs);
1055
1056         return 0;
1057 }