Print the root generation in btrfs-debug-tree
[btrfs-progs-unstable/devel.git] / btrfs_cmds.c
blobf2b6355122c68806f12e690fdf0bfcc69cd90630
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.
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.
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.
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>
32 #undef ULONG_MAX
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"
42 #include "btrfs_cmds.h"
43 #include "btrfslabel.h"
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
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
60 static int test_issubvolume(char *path)
63 struct stat st;
64 int res;
66 res = stat(path, &st);
67 if(res < 0 )
68 return -1;
70 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
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
81 static int test_isdir(char *path)
83 struct stat st;
84 int res;
86 res = stat(path, &st);
87 if(res < 0 )
88 return -1;
90 return S_ISDIR(st.st_mode);
94 int open_file_or_dir(const char *fname)
96 int ret;
97 struct stat st;
98 DIR *dirstream;
99 int fd;
101 ret = stat(fname, &st);
102 if (ret < 0) {
103 return -1;
105 if (S_ISDIR(st.st_mode)) {
106 dirstream = opendir(fname);
107 if (!dirstream) {
108 return -2;
110 fd = dirfd(dirstream);
111 } else {
112 fd = open(fname, O_RDWR);
114 if (fd < 0) {
115 return -3;
117 return fd;
120 static u64 parse_size(char *s)
122 int len = strlen(s);
123 char c;
124 u64 mult = 1;
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);
141 s[len - 1] = '\0';
143 return atoll(s) * mult;
146 static int parse_compress_type(char *s)
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);
158 int do_defrag(int ac, char **av)
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;
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;
211 if (ac - optind == 0) {
212 fprintf(stderr, "Invalid arguments for defragment\n");
213 free(av);
214 return 1;
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;
225 if (flush)
226 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
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;
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;
252 if (ret) {
253 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
254 av[i], strerror(e));
255 errors++;
257 close(fd);
259 if (verbose)
260 printf("%s\n", BTRFS_BUILD_VERSION);
261 if (errors) {
262 fprintf(stderr, "total %d failures\n", errors);
263 exit(1);
266 free(av);
267 return errors + 20;
270 int do_find_newer(int argc, char **argv)
272 int fd;
273 int ret;
274 char *subvol;
275 u64 last_gen;
277 subvol = argv[1];
278 last_gen = atoll(argv[2]);
280 ret = test_issubvolume(subvol);
281 if (ret < 0) {
282 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
283 return 12;
285 if (!ret) {
286 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
287 return 13;
290 fd = open_file_or_dir(subvol);
291 if (fd < 0) {
292 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
293 return 12;
295 ret = find_updated_files(fd, 0, last_gen);
296 if (ret)
297 return 19;
298 return 0;
301 int do_subvol_list(int argc, char **argv)
303 int fd;
304 int ret;
305 int print_parent = 0;
306 char *subvol;
307 int optind = 1;
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;
320 if (argc - optind != 1) {
321 fprintf(stderr, "ERROR: invalid arguments for subvolume list\n");
322 return 1;
325 subvol = argv[optind];
327 ret = test_issubvolume(subvol);
328 if (ret < 0) {
329 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
330 return 12;
332 if (!ret) {
333 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
334 return 13;
337 fd = open_file_or_dir(subvol);
338 if (fd < 0) {
339 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
340 return 12;
342 ret = list_subvols(fd, print_parent, 0);
343 if (ret)
344 return 19;
345 return 0;
348 int do_clone(int argc, char **argv)
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;
356 memset(&args, 0, sizeof(args));
358 while (1) {
359 int c = getopt(argc, argv, "r");
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;
375 if (argc - optind != 3) {
376 fprintf(stderr, "Invalid arguments for subvolume snapshot\n");
377 free(argv);
378 return 1;
381 subvol = argv[optind+1];
382 dst = argv[optind+2];
384 res = test_issubvolume(subvol);
385 if(res<0){
386 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
387 return 12;
389 if(!res){
390 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
391 return 13;
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;
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);
411 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
412 strchr(newname, '/') ){
413 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
414 newname);
415 return 14;
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;
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;
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;
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);
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;
452 close(fd);
453 close(fddst);
455 if(res < 0 ){
456 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
457 subvol, strerror(e));
458 return 11;
461 return 0;
465 int do_delete_subvolume(int argc, char **argv)
467 int res, fd, len, e;
468 struct btrfs_ioctl_vol_args args;
469 char *dname, *vname, *cpath;
470 char *path = argv[1];
472 res = test_issubvolume(path);
473 if(res<0){
474 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
475 return 12;
477 if(!res){
478 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
479 return 13;
482 cpath = realpath(path, 0);
483 dname = strdup(cpath);
484 dname = dirname(dname);
485 vname = strdup(cpath);
486 vname = basename(vname);
487 free(cpath);
489 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
490 strchr(vname, '/') ){
491 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
492 vname);
493 return 14;
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;
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;
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;
515 close(fd);
517 if(res < 0 ){
518 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
519 dname, vname, strerror(e));
520 return 11;
523 return 0;
527 int do_create_subvol(int argc, char **argv)
529 int res, fddst, len, e;
530 char *newname;
531 char *dstdir;
532 struct btrfs_ioctl_vol_args args;
533 char *dst = argv[1];
535 res = test_isdir(dst);
536 if(res >= 0 ){
537 fprintf(stderr, "ERROR: '%s' exists\n", dst);
538 return 12;
541 newname = strdup(dst);
542 newname = basename(newname);
543 dstdir = strdup(dst);
544 dstdir = dirname(dstdir);
546 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
547 strchr(newname, '/') ){
548 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
549 newname);
550 return 14;
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;
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;
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;
571 close(fddst);
573 if(res < 0 ){
574 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
575 strerror(e));
576 return 11;
579 return 0;
583 int do_fssync(int argc, char **argv)
585 int fd, res, e;
586 char *path = argv[1];
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;
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;
604 return 0;
607 int do_scan(int argc, char **argv)
609 int i, fd, e;
610 int checklist = 1;
611 int devstart = 1;
613 if( argc >= 2 && !strcmp(argv[1],"--all-devices")){
615 if( argc >2 ){
616 fprintf(stderr, "ERROR: too may arguments\n");
617 return 22;
620 checklist = 0;
621 devstart += 1;
624 if(argc<=devstart){
626 int ret;
628 printf("Scanning for Btrfs filesystems\n");
629 if(checklist)
630 ret = btrfs_scan_block_devices(1);
631 else
632 ret = btrfs_scan_one_dir("/dev", 1);
633 if (ret){
634 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
635 return 18;
637 return 0;
640 fd = open("/dev/btrfs-control", O_RDWR);
641 if (fd < 0) {
642 perror("failed to open /dev/btrfs-control");
643 return 10;
646 for( i = devstart ; i < argc ; i++ ){
647 struct btrfs_ioctl_vol_args args;
648 int ret;
650 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
652 strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
654 * FIXME: which are the error code returned by this ioctl ?
655 * it seems that is impossible to understand if there no is
656 * a btrfs filesystem from an I/O error !!!
658 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
659 e = errno;
661 if( ret < 0 ){
662 close(fd);
663 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
664 argv[i], strerror(e));
665 return 11;
669 close(fd);
670 return 0;
674 int do_resize(int argc, char **argv)
677 struct btrfs_ioctl_vol_args args;
678 int fd, res, len, e;
679 char *amount=argv[1], *path=argv[2];
681 fd = open_file_or_dir(path);
682 if (fd < 0) {
683 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
684 return 12;
686 len = strlen(amount);
687 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
688 fprintf(stderr, "ERROR: size value too long ('%s)\n",
689 amount);
690 return 14;
693 printf("Resize '%s' of '%s'\n", path, amount);
694 strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
695 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
696 e = errno;
697 close(fd);
698 if( res < 0 ){
699 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
700 path, strerror(e));
701 return 30;
703 return 0;
706 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
708 struct list_head *cur;
709 struct btrfs_device *device;
711 list_for_each(cur, &fs_devices->devices) {
712 device = list_entry(cur, struct btrfs_device, dev_list);
713 if ((device->label && strcmp(device->label, search) == 0) ||
714 strcmp(device->name, search) == 0)
715 return 1;
717 return 0;
720 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
722 char uuidbuf[37];
723 struct list_head *cur;
724 struct btrfs_device *device;
725 char *super_bytes_used;
726 u64 devs_found = 0;
727 u64 total;
729 uuid_unparse(fs_devices->fsid, uuidbuf);
730 device = list_entry(fs_devices->devices.next, struct btrfs_device,
731 dev_list);
732 if (device->label && device->label[0])
733 printf("Label: '%s' ", device->label);
734 else
735 printf("Label: none ");
737 super_bytes_used = pretty_sizes(device->super_bytes_used);
739 total = device->total_devs;
740 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
741 (unsigned long long)total, super_bytes_used);
743 free(super_bytes_used);
745 list_for_each(cur, &fs_devices->devices) {
746 char *total_bytes;
747 char *bytes_used;
748 device = list_entry(cur, struct btrfs_device, dev_list);
749 total_bytes = pretty_sizes(device->total_bytes);
750 bytes_used = pretty_sizes(device->bytes_used);
751 printf("\tdevid %4llu size %s used %s path %s\n",
752 (unsigned long long)device->devid,
753 total_bytes, bytes_used, device->name);
754 free(total_bytes);
755 free(bytes_used);
756 devs_found++;
758 if (devs_found < total) {
759 printf("\t*** Some devices missing\n");
761 printf("\n");
764 int do_show_filesystem(int argc, char **argv)
766 struct list_head *all_uuids;
767 struct btrfs_fs_devices *fs_devices;
768 struct list_head *cur_uuid;
769 char *search = 0;
770 int ret;
771 int checklist = 1;
772 int searchstart = 1;
774 if( argc >= 2 && !strcmp(argv[1],"--all-devices")){
775 checklist = 0;
776 searchstart += 1;
779 if( argc > searchstart+1 ){
780 fprintf(stderr, "ERROR: too many arguments\n");
781 return 22;
784 if(checklist)
785 ret = btrfs_scan_block_devices(0);
786 else
787 ret = btrfs_scan_one_dir("/dev", 0);
789 if (ret){
790 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
791 return 18;
794 if(searchstart < argc)
795 search = argv[searchstart];
797 all_uuids = btrfs_scanned_uuids();
798 list_for_each(cur_uuid, all_uuids) {
799 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
800 list);
801 if (search && uuid_search(fs_devices, search) == 0)
802 continue;
803 print_one_uuid(fs_devices);
805 printf("%s\n", BTRFS_BUILD_VERSION);
806 return 0;
809 int do_add_volume(int nargs, char **args)
812 char *mntpnt = args[nargs-1];
813 int i, fdmnt, ret=0, e;
816 fdmnt = open_file_or_dir(mntpnt);
817 if (fdmnt < 0) {
818 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
819 return 12;
822 for (i = 1; i < (nargs-1); i++ ){
823 struct btrfs_ioctl_vol_args ioctl_args;
824 int devfd, res;
825 u64 dev_block_count = 0;
826 struct stat st;
827 int mixed = 0;
829 res = check_mounted(args[i]);
830 if (res < 0) {
831 fprintf(stderr, "error checking %s mount status\n",
832 args[i]);
833 ret++;
834 continue;
836 if (res == 1) {
837 fprintf(stderr, "%s is mounted\n", args[i]);
838 ret++;
839 continue;
842 devfd = open(args[i], O_RDWR);
843 if (!devfd) {
844 fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
845 close(devfd);
846 ret++;
847 continue;
849 res = fstat(devfd, &st);
850 if (res) {
851 fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
852 close(devfd);
853 ret++;
854 continue;
856 if (!S_ISBLK(st.st_mode)) {
857 fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
858 close(devfd);
859 ret++;
860 continue;
863 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed);
864 if (res) {
865 fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
866 close(devfd);
867 ret++;
868 continue;
870 close(devfd);
872 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
873 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
874 e = errno;
875 if(res<0){
876 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
877 args[i], strerror(e));
878 ret++;
883 close(fdmnt);
884 if (ret)
885 return ret+20;
886 else
887 return 0;
891 int do_balance(int argc, char **argv)
894 int fdmnt, ret=0, e;
895 struct btrfs_ioctl_vol_args args;
896 char *path = argv[1];
898 fdmnt = open_file_or_dir(path);
899 if (fdmnt < 0) {
900 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
901 return 12;
904 memset(&args, 0, sizeof(args));
905 ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
906 e = errno;
907 close(fdmnt);
908 if(ret<0){
909 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n",
910 path, strerror(e));
912 return 19;
914 return 0;
916 int do_remove_volume(int nargs, char **args)
919 char *mntpnt = args[nargs-1];
920 int i, fdmnt, ret=0, e;
922 fdmnt = open_file_or_dir(mntpnt);
923 if (fdmnt < 0) {
924 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
925 return 12;
928 for(i=1 ; i < (nargs-1) ; i++ ){
929 struct btrfs_ioctl_vol_args arg;
930 int res;
932 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
933 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
934 e = errno;
935 if(res<0){
936 fprintf(stderr, "ERROR: error removing the device '%s' - %s\n",
937 args[i], strerror(e));
938 ret++;
942 close(fdmnt);
943 if( ret)
944 return ret+20;
945 else
946 return 0;
949 int do_set_default_subvol(int nargs, char **argv)
951 int ret=0, fd, e;
952 u64 objectid;
953 char *path = argv[2];
954 char *subvolid = argv[1];
956 fd = open_file_or_dir(path);
957 if (fd < 0) {
958 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
959 return 12;
962 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
963 if (errno == ERANGE) {
964 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
965 return 30;
967 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
968 e = errno;
969 close(fd);
970 if( ret < 0 ){
971 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
972 strerror(e));
973 return 30;
975 return 0;
978 int do_change_label(int nargs, char **argv)
980 /* check the number of argument */
981 if ( nargs > 3 ){
982 fprintf(stderr, "ERROR: '%s' requires maximum 2 args\n",
983 argv[0]);
984 return -2;
985 }else if (nargs == 2){
986 return get_label(argv[1]);
987 } else { /* nargs == 0 */
988 return set_label(argv[1], argv[2]);
993 int do_get_default_subvol(int nargs, char **argv)
995 int fd;
996 int ret;
997 char *subvol;
999 subvol = argv[1];
1001 ret = test_issubvolume(subvol);
1002 if (ret < 0) {
1003 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
1004 return 12;
1006 if (!ret) {
1007 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
1008 return 13;
1011 fd = open_file_or_dir(subvol);
1012 if (fd < 0) {
1013 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
1014 return 12;
1016 ret = list_subvols(fd, 0, 1);
1017 if (ret)
1018 return 19;
1019 return 0;
1022 int do_df_filesystem(int nargs, char **argv)
1024 struct btrfs_ioctl_space_args *sargs;
1025 u64 count = 0, i;
1026 int ret;
1027 int fd;
1028 int e;
1029 char *path = argv[1];
1031 fd = open_file_or_dir(path);
1032 if (fd < 0) {
1033 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
1034 return 12;
1037 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
1038 if (!sargs)
1039 return -ENOMEM;
1041 sargs->space_slots = 0;
1042 sargs->total_spaces = 0;
1044 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
1045 e = errno;
1046 if (ret) {
1047 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
1048 path, strerror(e));
1049 free(sargs);
1050 return ret;
1052 if (!sargs->total_spaces)
1053 return 0;
1055 count = sargs->total_spaces;
1057 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
1058 (count * sizeof(struct btrfs_ioctl_space_info)));
1059 if (!sargs)
1060 return -ENOMEM;
1062 sargs->space_slots = count;
1063 sargs->total_spaces = 0;
1065 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
1066 e = errno;
1067 if (ret) {
1068 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
1069 path, strerror(e));
1070 close(fd);
1071 free(sargs);
1072 return ret;
1075 for (i = 0; i < sargs->total_spaces; i++) {
1076 char description[80];
1077 char *total_bytes;
1078 char *used_bytes;
1079 int written = 0;
1080 u64 flags = sargs->spaces[i].flags;
1082 memset(description, 0, 80);
1084 if (flags & BTRFS_BLOCK_GROUP_DATA) {
1085 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1086 snprintf(description, 14, "%s",
1087 "Data+Metadata");
1088 written += 13;
1089 } else {
1090 snprintf(description, 5, "%s", "Data");
1091 written += 4;
1093 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
1094 snprintf(description, 7, "%s", "System");
1095 written += 6;
1096 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1097 snprintf(description, 9, "%s", "Metadata");
1098 written += 8;
1101 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
1102 snprintf(description+written, 8, "%s", ", RAID0");
1103 written += 7;
1104 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
1105 snprintf(description+written, 8, "%s", ", RAID1");
1106 written += 7;
1107 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
1108 snprintf(description+written, 6, "%s", ", DUP");
1109 written += 5;
1110 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
1111 snprintf(description+written, 9, "%s", ", RAID10");
1112 written += 8;
1115 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
1116 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
1117 printf("%s: total=%s, used=%s\n", description, total_bytes,
1118 used_bytes);
1120 free(sargs);
1122 return 0;