mkfs.btrfs: return some defined value instead of garbage when lookup checksum
[btrfs-progs-unstable/devel.git] / btrfs_cmds.c
blobda5bd91852931b45f63e51ed6c0fc34ff9110d61
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 static 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 char *subvol;
307 subvol = argv[1];
309 ret = test_issubvolume(subvol);
310 if (ret < 0) {
311 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
312 return 12;
314 if (!ret) {
315 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
316 return 13;
319 fd = open_file_or_dir(subvol);
320 if (fd < 0) {
321 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
322 return 12;
324 ret = list_subvols(fd);
325 if (ret)
326 return 19;
327 return 0;
330 int do_clone(int argc, char **argv)
332 char *subvol, *dst;
333 int res, fd, fddst, len, e, optind = 0, readonly = 0;
334 char *newname;
335 char *dstdir;
336 struct btrfs_ioctl_vol_args_v2 args;
338 memset(&args, 0, sizeof(args));
340 while (1) {
341 int c = getopt(argc, argv, "r");
343 if (c < 0)
344 break;
345 switch (c) {
346 case 'r':
347 optind++;
348 readonly = 1;
349 break;
350 default:
351 fprintf(stderr,
352 "Invalid arguments for subvolume snapshot\n");
353 free(argv);
354 return 1;
357 if (argc - optind < 2) {
358 fprintf(stderr, "Invalid arguments for subvolume snapshot\n");
359 free(argv);
360 return 1;
363 subvol = argv[optind+1];
364 dst = argv[optind+2];
366 res = test_issubvolume(subvol);
367 if(res<0){
368 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
369 return 12;
371 if(!res){
372 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
373 return 13;
376 res = test_isdir(dst);
377 if(res == 0 ){
378 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
379 return 12;
382 if(res>0){
383 newname = strdup(subvol);
384 newname = basename(newname);
385 dstdir = dst;
386 }else{
387 newname = strdup(dst);
388 newname = basename(newname);
389 dstdir = strdup(dst);
390 dstdir = dirname(dstdir);
393 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
394 strchr(newname, '/') ){
395 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
396 newname);
397 return 14;
400 len = strlen(newname);
401 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
402 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
403 newname);
404 return 14;
407 fddst = open_file_or_dir(dstdir);
408 if (fddst < 0) {
409 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
410 return 12;
413 fd = open_file_or_dir(subvol);
414 if (fd < 0) {
415 close(fddst);
416 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
417 return 12;
420 if (readonly) {
421 args.flags |= BTRFS_SUBVOL_RDONLY;
422 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
423 subvol, dstdir, newname);
424 } else {
425 printf("Create a snapshot of '%s' in '%s/%s'\n",
426 subvol, dstdir, newname);
429 args.fd = fd;
430 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
431 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
432 e = errno;
434 close(fd);
435 close(fddst);
437 if(res < 0 ){
438 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
439 subvol, strerror(e));
440 return 11;
443 return 0;
447 int do_delete_subvolume(int argc, char **argv)
449 int res, fd, len, e;
450 struct btrfs_ioctl_vol_args args;
451 char *dname, *vname, *cpath;
452 char *path = argv[1];
454 res = test_issubvolume(path);
455 if(res<0){
456 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
457 return 12;
459 if(!res){
460 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
461 return 13;
464 cpath = realpath(path, 0);
465 dname = strdup(cpath);
466 dname = dirname(dname);
467 vname = strdup(cpath);
468 vname = basename(vname);
469 free(cpath);
471 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
472 strchr(vname, '/') ){
473 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
474 vname);
475 return 14;
478 len = strlen(vname);
479 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
480 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
481 vname);
482 return 14;
485 fd = open_file_or_dir(dname);
486 if (fd < 0) {
487 close(fd);
488 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
489 return 12;
492 printf("Delete subvolume '%s/%s'\n", dname, vname);
493 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
494 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
495 e = errno;
497 close(fd);
499 if(res < 0 ){
500 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
501 dname, vname, strerror(e));
502 return 11;
505 return 0;
509 int do_create_subvol(int argc, char **argv)
511 int res, fddst, len, e;
512 char *newname;
513 char *dstdir;
514 struct btrfs_ioctl_vol_args args;
515 char *dst = argv[1];
517 res = test_isdir(dst);
518 if(res >= 0 ){
519 fprintf(stderr, "ERROR: '%s' exists\n", dst);
520 return 12;
523 newname = strdup(dst);
524 newname = basename(newname);
525 dstdir = strdup(dst);
526 dstdir = dirname(dstdir);
528 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
529 strchr(newname, '/') ){
530 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
531 newname);
532 return 14;
535 len = strlen(newname);
536 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
537 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
538 newname);
539 return 14;
542 fddst = open_file_or_dir(dstdir);
543 if (fddst < 0) {
544 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
545 return 12;
548 printf("Create subvolume '%s/%s'\n", dstdir, newname);
549 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
550 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
551 e = errno;
553 close(fddst);
555 if(res < 0 ){
556 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
557 strerror(e));
558 return 11;
561 return 0;
565 int do_fssync(int argc, char **argv)
567 int fd, res, e;
568 char *path = argv[1];
570 fd = open_file_or_dir(path);
571 if (fd < 0) {
572 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
573 return 12;
576 printf("FSSync '%s'\n", path);
577 res = ioctl(fd, BTRFS_IOC_SYNC);
578 e = errno;
579 close(fd);
580 if( res < 0 ){
581 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
582 path, strerror(e));
583 return 16;
586 return 0;
589 int do_scan(int argc, char **argv)
591 int i, fd, e;
592 if(argc<=1){
593 int ret;
595 printf("Scanning for Btrfs filesystems\n");
596 ret = btrfs_scan_one_dir("/dev", 1);
597 if (ret){
598 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
599 return 18;
601 return 0;
604 fd = open("/dev/btrfs-control", O_RDWR);
605 if (fd < 0) {
606 perror("failed to open /dev/btrfs-control");
607 return 10;
610 for( i = 1 ; i < argc ; i++ ){
611 struct btrfs_ioctl_vol_args args;
612 int ret;
614 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
616 strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
618 * FIXME: which are the error code returned by this ioctl ?
619 * it seems that is impossible to understand if there no is
620 * a btrfs filesystem from an I/O error !!!
622 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
623 e = errno;
625 if( ret < 0 ){
626 close(fd);
627 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
628 argv[i], strerror(e));
629 return 11;
633 close(fd);
634 return 0;
638 int do_resize(int argc, char **argv)
641 struct btrfs_ioctl_vol_args args;
642 int fd, res, len, e;
643 char *amount=argv[1], *path=argv[2];
645 fd = open_file_or_dir(path);
646 if (fd < 0) {
647 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
648 return 12;
650 len = strlen(amount);
651 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
652 fprintf(stderr, "ERROR: size value too long ('%s)\n",
653 amount);
654 return 14;
657 printf("Resize '%s' of '%s'\n", path, amount);
658 strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
659 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
660 e = errno;
661 close(fd);
662 if( res < 0 ){
663 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
664 path, strerror(e));
665 return 30;
667 return 0;
670 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
672 struct list_head *cur;
673 struct btrfs_device *device;
675 list_for_each(cur, &fs_devices->devices) {
676 device = list_entry(cur, struct btrfs_device, dev_list);
677 if ((device->label && strcmp(device->label, search) == 0) ||
678 strcmp(device->name, search) == 0)
679 return 1;
681 return 0;
684 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
686 char uuidbuf[37];
687 struct list_head *cur;
688 struct btrfs_device *device;
689 char *super_bytes_used;
690 u64 devs_found = 0;
691 u64 total;
693 uuid_unparse(fs_devices->fsid, uuidbuf);
694 device = list_entry(fs_devices->devices.next, struct btrfs_device,
695 dev_list);
696 if (device->label && device->label[0])
697 printf("Label: '%s' ", device->label);
698 else
699 printf("Label: none ");
701 super_bytes_used = pretty_sizes(device->super_bytes_used);
703 total = device->total_devs;
704 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
705 (unsigned long long)total, super_bytes_used);
707 free(super_bytes_used);
709 list_for_each(cur, &fs_devices->devices) {
710 char *total_bytes;
711 char *bytes_used;
712 device = list_entry(cur, struct btrfs_device, dev_list);
713 total_bytes = pretty_sizes(device->total_bytes);
714 bytes_used = pretty_sizes(device->bytes_used);
715 printf("\tdevid %4llu size %s used %s path %s\n",
716 (unsigned long long)device->devid,
717 total_bytes, bytes_used, device->name);
718 free(total_bytes);
719 free(bytes_used);
720 devs_found++;
722 if (devs_found < total) {
723 printf("\t*** Some devices missing\n");
725 printf("\n");
728 int do_show_filesystem(int argc, char **argv)
730 struct list_head *all_uuids;
731 struct btrfs_fs_devices *fs_devices;
732 struct list_head *cur_uuid;
733 char *search = argv[1];
734 int ret;
736 ret = btrfs_scan_one_dir("/dev", 0);
737 if (ret){
738 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
739 return 18;
742 all_uuids = btrfs_scanned_uuids();
743 list_for_each(cur_uuid, all_uuids) {
744 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
745 list);
746 if (search && uuid_search(fs_devices, search) == 0)
747 continue;
748 print_one_uuid(fs_devices);
750 printf("%s\n", BTRFS_BUILD_VERSION);
751 return 0;
754 int do_add_volume(int nargs, char **args)
757 char *mntpnt = args[nargs-1];
758 int i, fdmnt, ret=0, e;
761 fdmnt = open_file_or_dir(mntpnt);
762 if (fdmnt < 0) {
763 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
764 return 12;
767 for (i = 1; i < (nargs-1); i++ ){
768 struct btrfs_ioctl_vol_args ioctl_args;
769 int devfd, res;
770 u64 dev_block_count = 0;
771 struct stat st;
772 int mixed = 0;
774 res = check_mounted(args[i]);
775 if (res < 0) {
776 fprintf(stderr, "error checking %s mount status\n",
777 args[i]);
778 ret++;
779 continue;
781 if (res == 1) {
782 fprintf(stderr, "%s is mounted\n", args[i]);
783 ret++;
784 continue;
787 devfd = open(args[i], O_RDWR);
788 if (!devfd) {
789 fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
790 close(devfd);
791 ret++;
792 continue;
794 res = fstat(devfd, &st);
795 if (res) {
796 fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
797 close(devfd);
798 ret++;
799 continue;
801 if (!S_ISBLK(st.st_mode)) {
802 fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
803 close(devfd);
804 ret++;
805 continue;
808 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed);
809 if (res) {
810 fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
811 close(devfd);
812 ret++;
813 continue;
815 close(devfd);
817 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
818 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
819 e = errno;
820 if(res<0){
821 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
822 args[i], strerror(e));
823 ret++;
828 close(fdmnt);
829 if (ret)
830 return ret+20;
831 else
832 return 0;
836 int do_balance(int argc, char **argv)
839 int fdmnt, ret=0, e;
840 struct btrfs_ioctl_vol_args args;
841 char *path = argv[1];
843 fdmnt = open_file_or_dir(path);
844 if (fdmnt < 0) {
845 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
846 return 12;
849 memset(&args, 0, sizeof(args));
850 ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
851 e = errno;
852 close(fdmnt);
853 if(ret<0){
854 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n",
855 path, strerror(e));
857 return 19;
859 return 0;
861 int do_remove_volume(int nargs, char **args)
864 char *mntpnt = args[nargs-1];
865 int i, fdmnt, ret=0, e;
867 fdmnt = open_file_or_dir(mntpnt);
868 if (fdmnt < 0) {
869 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
870 return 12;
873 for(i=1 ; i < (nargs-1) ; i++ ){
874 struct btrfs_ioctl_vol_args arg;
875 int res;
877 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
878 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
879 e = errno;
880 if(res<0){
881 fprintf(stderr, "ERROR: error removing the device '%s' - %s\n",
882 args[i], strerror(e));
883 ret++;
887 close(fdmnt);
888 if( ret)
889 return ret+20;
890 else
891 return 0;
894 int do_set_default_subvol(int nargs, char **argv)
896 int ret=0, fd, e;
897 u64 objectid;
898 char *path = argv[2];
899 char *subvolid = argv[1];
901 fd = open_file_or_dir(path);
902 if (fd < 0) {
903 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
904 return 12;
907 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
908 if (errno == ERANGE) {
909 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
910 return 30;
912 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
913 e = errno;
914 close(fd);
915 if( ret < 0 ){
916 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
917 strerror(e));
918 return 30;
920 return 0;
923 int do_change_label(int nargs, char **argv)
925 /* check the number of argument */
926 if ( nargs > 3 ){
927 fprintf(stderr, "ERROR: '%s' requires maximum 2 args\n",
928 argv[0]);
929 return -2;
930 }else if (nargs == 2){
931 return get_label(argv[1]);
932 } else { /* nargs == 0 */
933 return set_label(argv[1], argv[2]);
938 int do_df_filesystem(int nargs, char **argv)
940 struct btrfs_ioctl_space_args *sargs;
941 u64 count = 0, i;
942 int ret;
943 int fd;
944 int e;
945 char *path = argv[1];
947 fd = open_file_or_dir(path);
948 if (fd < 0) {
949 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
950 return 12;
953 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
954 if (!sargs)
955 return -ENOMEM;
957 sargs->space_slots = 0;
958 sargs->total_spaces = 0;
960 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
961 e = errno;
962 if (ret) {
963 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
964 path, strerror(e));
965 free(sargs);
966 return ret;
968 if (!sargs->total_spaces)
969 return 0;
971 count = sargs->total_spaces;
973 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
974 (count * sizeof(struct btrfs_ioctl_space_info)));
975 if (!sargs)
976 return -ENOMEM;
978 sargs->space_slots = count;
979 sargs->total_spaces = 0;
981 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
982 e = errno;
983 if (ret) {
984 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
985 path, strerror(e));
986 close(fd);
987 free(sargs);
988 return ret;
991 for (i = 0; i < sargs->total_spaces; i++) {
992 char description[80];
993 char *total_bytes;
994 char *used_bytes;
995 int written = 0;
996 u64 flags = sargs->spaces[i].flags;
998 memset(description, 0, 80);
1000 if (flags & BTRFS_BLOCK_GROUP_DATA) {
1001 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1002 snprintf(description, 15, "%s",
1003 "Data+Metadata");
1004 written += 14;
1005 } else {
1006 snprintf(description, 5, "%s", "Data");
1007 written += 4;
1009 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
1010 snprintf(description, 7, "%s", "System");
1011 written += 6;
1012 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1013 snprintf(description, 9, "%s", "Metadata");
1014 written += 8;
1017 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
1018 snprintf(description+written, 8, "%s", ", RAID0");
1019 written += 7;
1020 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
1021 snprintf(description+written, 8, "%s", ", RAID1");
1022 written += 7;
1023 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
1024 snprintf(description+written, 6, "%s", ", DUP");
1025 written += 5;
1026 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
1027 snprintf(description+written, 9, "%s", ", RAID10");
1028 written += 8;
1031 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
1032 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
1033 printf("%s: total=%s, used=%s\n", description, total_bytes,
1034 used_bytes);
1036 free(sargs);
1038 return 0;