btrfs-progs: add discard support to mkfs
[btrfs-progs-unstable/devel.git] / btrfs_cmds.c
blob32f6b25c8d7f661df1a64dc470cd9e1a5b518980
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 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;
334 char *newname;
335 char *dstdir;
337 subvol = argv[1];
338 dst = argv[2];
339 struct btrfs_ioctl_vol_args args;
341 res = test_issubvolume(subvol);
342 if(res<0){
343 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
344 return 12;
346 if(!res){
347 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
348 return 13;
351 res = test_isdir(dst);
352 if(res == 0 ){
353 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
354 return 12;
357 if(res>0){
358 newname = strdup(subvol);
359 newname = basename(newname);
360 dstdir = dst;
361 }else{
362 newname = strdup(dst);
363 newname = basename(newname);
364 dstdir = strdup(dst);
365 dstdir = dirname(dstdir);
368 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
369 strchr(newname, '/') ){
370 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
371 newname);
372 return 14;
375 len = strlen(newname);
376 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
377 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
378 newname);
379 return 14;
382 fddst = open_file_or_dir(dstdir);
383 if (fddst < 0) {
384 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
385 return 12;
388 fd = open_file_or_dir(subvol);
389 if (fd < 0) {
390 close(fddst);
391 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
392 return 12;
395 printf("Create a snapshot of '%s' in '%s/%s'\n",
396 subvol, dstdir, newname);
397 args.fd = fd;
398 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
399 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE, &args);
400 e = errno;
402 close(fd);
403 close(fddst);
405 if(res < 0 ){
406 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
407 subvol, strerror(e));
408 return 11;
411 return 0;
415 int do_delete_subvolume(int argc, char **argv)
417 int res, fd, len, e;
418 struct btrfs_ioctl_vol_args args;
419 char *dname, *vname, *cpath;
420 char *path = argv[1];
422 res = test_issubvolume(path);
423 if(res<0){
424 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
425 return 12;
427 if(!res){
428 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
429 return 13;
432 cpath = realpath(path, 0);
433 dname = strdup(cpath);
434 dname = dirname(dname);
435 vname = strdup(cpath);
436 vname = basename(vname);
437 free(cpath);
439 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
440 strchr(vname, '/') ){
441 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
442 vname);
443 return 14;
446 len = strlen(vname);
447 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
448 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
449 vname);
450 return 14;
453 fd = open_file_or_dir(dname);
454 if (fd < 0) {
455 close(fd);
456 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
457 return 12;
460 printf("Delete subvolume '%s/%s'\n", dname, vname);
461 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
462 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
463 e = errno;
465 close(fd);
467 if(res < 0 ){
468 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
469 dname, vname, strerror(e));
470 return 11;
473 return 0;
477 int do_create_subvol(int argc, char **argv)
479 int res, fddst, len, e;
480 char *newname;
481 char *dstdir;
482 struct btrfs_ioctl_vol_args args;
483 char *dst = argv[1];
485 res = test_isdir(dst);
486 if(res >= 0 ){
487 fprintf(stderr, "ERROR: '%s' exists\n", dst);
488 return 12;
491 newname = strdup(dst);
492 newname = basename(newname);
493 dstdir = strdup(dst);
494 dstdir = dirname(dstdir);
496 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
497 strchr(newname, '/') ){
498 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
499 newname);
500 return 14;
503 len = strlen(newname);
504 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
505 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
506 newname);
507 return 14;
510 fddst = open_file_or_dir(dstdir);
511 if (fddst < 0) {
512 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
513 return 12;
516 printf("Create subvolume '%s/%s'\n", dstdir, newname);
517 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
518 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
519 e = errno;
521 close(fddst);
523 if(res < 0 ){
524 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
525 strerror(e));
526 return 11;
529 return 0;
533 int do_fssync(int argc, char **argv)
535 int fd, res, e;
536 char *path = argv[1];
538 fd = open_file_or_dir(path);
539 if (fd < 0) {
540 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
541 return 12;
544 printf("FSSync '%s'\n", path);
545 res = ioctl(fd, BTRFS_IOC_SYNC);
546 e = errno;
547 close(fd);
548 if( res < 0 ){
549 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
550 path, strerror(e));
551 return 16;
554 return 0;
557 int do_scan(int argc, char **argv)
559 int i, fd, e;
560 if(argc<=1){
561 int ret;
563 printf("Scanning for Btrfs filesystems\n");
564 ret = btrfs_scan_one_dir("/dev", 1);
565 if (ret){
566 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
567 return 18;
569 return 0;
572 fd = open("/dev/btrfs-control", O_RDWR);
573 if (fd < 0) {
574 perror("failed to open /dev/btrfs-control");
575 return 10;
578 for( i = 1 ; i < argc ; i++ ){
579 struct btrfs_ioctl_vol_args args;
580 int ret;
582 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
584 strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
586 * FIXME: which are the error code returned by this ioctl ?
587 * it seems that is impossible to understand if there no is
588 * a btrfs filesystem from an I/O error !!!
590 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
591 e = errno;
593 if( ret < 0 ){
594 close(fd);
595 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
596 argv[i], strerror(e));
597 return 11;
601 close(fd);
602 return 0;
606 int do_resize(int argc, char **argv)
609 struct btrfs_ioctl_vol_args args;
610 int fd, res, len, e;
611 char *amount=argv[1], *path=argv[2];
613 fd = open_file_or_dir(path);
614 if (fd < 0) {
615 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
616 return 12;
618 len = strlen(amount);
619 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
620 fprintf(stderr, "ERROR: size value too long ('%s)\n",
621 amount);
622 return 14;
625 printf("Resize '%s' of '%s'\n", path, amount);
626 strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
627 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
628 e = errno;
629 close(fd);
630 if( res < 0 ){
631 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
632 path, strerror(e));
633 return 30;
635 return 0;
638 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
640 struct list_head *cur;
641 struct btrfs_device *device;
643 list_for_each(cur, &fs_devices->devices) {
644 device = list_entry(cur, struct btrfs_device, dev_list);
645 if ((device->label && strcmp(device->label, search) == 0) ||
646 strcmp(device->name, search) == 0)
647 return 1;
649 return 0;
652 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
654 char uuidbuf[37];
655 struct list_head *cur;
656 struct btrfs_device *device;
657 char *super_bytes_used;
658 u64 devs_found = 0;
659 u64 total;
661 uuid_unparse(fs_devices->fsid, uuidbuf);
662 device = list_entry(fs_devices->devices.next, struct btrfs_device,
663 dev_list);
664 if (device->label && device->label[0])
665 printf("Label: '%s' ", device->label);
666 else
667 printf("Label: none ");
669 super_bytes_used = pretty_sizes(device->super_bytes_used);
671 total = device->total_devs;
672 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
673 (unsigned long long)total, super_bytes_used);
675 free(super_bytes_used);
677 list_for_each(cur, &fs_devices->devices) {
678 char *total_bytes;
679 char *bytes_used;
680 device = list_entry(cur, struct btrfs_device, dev_list);
681 total_bytes = pretty_sizes(device->total_bytes);
682 bytes_used = pretty_sizes(device->bytes_used);
683 printf("\tdevid %4llu size %s used %s path %s\n",
684 (unsigned long long)device->devid,
685 total_bytes, bytes_used, device->name);
686 free(total_bytes);
687 free(bytes_used);
688 devs_found++;
690 if (devs_found < total) {
691 printf("\t*** Some devices missing\n");
693 printf("\n");
696 int do_show_filesystem(int argc, char **argv)
698 struct list_head *all_uuids;
699 struct btrfs_fs_devices *fs_devices;
700 struct list_head *cur_uuid;
701 char *search = argv[1];
702 int ret;
704 ret = btrfs_scan_one_dir("/dev", 0);
705 if (ret){
706 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
707 return 18;
710 all_uuids = btrfs_scanned_uuids();
711 list_for_each(cur_uuid, all_uuids) {
712 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
713 list);
714 if (search && uuid_search(fs_devices, search) == 0)
715 continue;
716 print_one_uuid(fs_devices);
718 printf("%s\n", BTRFS_BUILD_VERSION);
719 return 0;
722 int do_add_volume(int nargs, char **args)
725 char *mntpnt = args[nargs-1];
726 int i, fdmnt, ret=0, e;
729 fdmnt = open_file_or_dir(mntpnt);
730 if (fdmnt < 0) {
731 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
732 return 12;
735 for (i = 1; i < (nargs-1); i++ ){
736 struct btrfs_ioctl_vol_args ioctl_args;
737 int devfd, res;
738 u64 dev_block_count = 0;
739 struct stat st;
740 int mixed = 0;
742 res = check_mounted(args[i]);
743 if (res < 0) {
744 fprintf(stderr, "error checking %s mount status\n",
745 args[i]);
746 ret++;
747 continue;
749 if (res == 1) {
750 fprintf(stderr, "%s is mounted\n", args[i]);
751 ret++;
752 continue;
755 devfd = open(args[i], O_RDWR);
756 if (!devfd) {
757 fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
758 close(devfd);
759 ret++;
760 continue;
762 res = fstat(devfd, &st);
763 if (res) {
764 fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
765 close(devfd);
766 ret++;
767 continue;
769 if (!S_ISBLK(st.st_mode)) {
770 fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
771 close(devfd);
772 ret++;
773 continue;
776 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed);
777 if (res) {
778 fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
779 close(devfd);
780 ret++;
781 continue;
783 close(devfd);
785 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
786 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
787 e = errno;
788 if(res<0){
789 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
790 args[i], strerror(e));
791 ret++;
796 close(fdmnt);
797 if (ret)
798 return ret+20;
799 else
800 return 0;
804 int do_balance(int argc, char **argv)
807 int fdmnt, ret=0, e;
808 struct btrfs_ioctl_vol_args args;
809 char *path = argv[1];
811 fdmnt = open_file_or_dir(path);
812 if (fdmnt < 0) {
813 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
814 return 12;
817 memset(&args, 0, sizeof(args));
818 ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
819 e = errno;
820 close(fdmnt);
821 if(ret<0){
822 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n",
823 path, strerror(e));
825 return 19;
827 return 0;
829 int do_remove_volume(int nargs, char **args)
832 char *mntpnt = args[nargs-1];
833 int i, fdmnt, ret=0, e;
835 fdmnt = open_file_or_dir(mntpnt);
836 if (fdmnt < 0) {
837 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
838 return 12;
841 for(i=1 ; i < (nargs-1) ; i++ ){
842 struct btrfs_ioctl_vol_args arg;
843 int res;
845 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
846 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
847 e = errno;
848 if(res<0){
849 fprintf(stderr, "ERROR: error removing the device '%s' - %s\n",
850 args[i], strerror(e));
851 ret++;
855 close(fdmnt);
856 if( ret)
857 return ret+20;
858 else
859 return 0;
862 int do_set_default_subvol(int nargs, char **argv)
864 int ret=0, fd, e;
865 u64 objectid;
866 char *path = argv[2];
867 char *subvolid = argv[1];
869 fd = open_file_or_dir(path);
870 if (fd < 0) {
871 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
872 return 12;
875 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
876 if (errno == ERANGE) {
877 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
878 return 30;
880 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
881 e = errno;
882 close(fd);
883 if( ret < 0 ){
884 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
885 strerror(e));
886 return 30;
888 return 0;
891 int do_change_label(int nargs, char **argv)
893 /* check the number of argument */
894 if ( nargs > 3 ){
895 fprintf(stderr, "ERROR: '%s' requires maximum 2 args\n",
896 argv[0]);
897 return -2;
898 }else if (nargs == 2){
899 return get_label(argv[1]);
900 } else { /* nargs == 0 */
901 return set_label(argv[1], argv[2]);
906 int do_df_filesystem(int nargs, char **argv)
908 struct btrfs_ioctl_space_args *sargs;
909 u64 count = 0, i;
910 int ret;
911 int fd;
912 int e;
913 char *path = argv[1];
915 fd = open_file_or_dir(path);
916 if (fd < 0) {
917 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
918 return 12;
921 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
922 if (!sargs)
923 return -ENOMEM;
925 sargs->space_slots = 0;
926 sargs->total_spaces = 0;
928 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
929 e = errno;
930 if (ret) {
931 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
932 path, strerror(e));
933 free(sargs);
934 return ret;
936 if (!sargs->total_spaces)
937 return 0;
939 count = sargs->total_spaces;
941 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
942 (count * sizeof(struct btrfs_ioctl_space_info)));
943 if (!sargs)
944 return -ENOMEM;
946 sargs->space_slots = count;
947 sargs->total_spaces = 0;
949 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
950 e = errno;
951 if (ret) {
952 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
953 path, strerror(e));
954 close(fd);
955 free(sargs);
956 return ret;
959 for (i = 0; i < sargs->total_spaces; i++) {
960 char description[80];
961 char *total_bytes;
962 char *used_bytes;
963 int written = 0;
964 u64 flags = sargs->spaces[i].flags;
966 memset(description, 0, 80);
968 if (flags & BTRFS_BLOCK_GROUP_DATA) {
969 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
970 snprintf(description, 15, "%s",
971 "Data+Metadata");
972 written += 14;
973 } else {
974 snprintf(description, 5, "%s", "Data");
975 written += 4;
977 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
978 snprintf(description, 7, "%s", "System");
979 written += 6;
980 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
981 snprintf(description, 9, "%s", "Metadata");
982 written += 8;
985 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
986 snprintf(description+written, 8, "%s", ", RAID0");
987 written += 7;
988 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
989 snprintf(description+written, 8, "%s", ", RAID1");
990 written += 7;
991 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
992 snprintf(description+written, 6, "%s", ", DUP");
993 written += 5;
994 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
995 snprintf(description+written, 9, "%s", ", RAID10");
996 written += 8;
999 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
1000 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
1001 printf("%s: total=%s, used=%s\n", description, total_bytes,
1002 used_bytes);
1004 free(sargs);
1006 return 0;