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.
21 #include <sys/ioctl.h>
23 #include <uuid/uuid.h>
26 #include "kerncompat.h"
34 #include "btrfs_cmds.h"
35 #include "btrfslabel.h"
37 int do_df_filesystem(int nargs, char **argv)
39 struct btrfs_ioctl_space_args *sargs;
46 fd = open_file_or_dir(path);
48 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
52 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
56 sargs->space_slots = 0;
57 sargs->total_spaces = 0;
59 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
62 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
67 if (!sargs->total_spaces)
70 count = sargs->total_spaces;
72 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
73 (count * sizeof(struct btrfs_ioctl_space_info)));
77 sargs->space_slots = count;
78 sargs->total_spaces = 0;
80 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
83 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
90 for (i = 0; i < sargs->total_spaces; i++) {
95 u64 flags = sargs->spaces[i].flags;
97 memset(description, 0, 80);
99 if (flags & BTRFS_BLOCK_GROUP_DATA) {
100 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
101 snprintf(description, 14, "%s",
105 snprintf(description, 5, "%s", "Data");
108 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
109 snprintf(description, 7, "%s", "System");
111 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
112 snprintf(description, 9, "%s", "Metadata");
116 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
117 snprintf(description+written, 8, "%s", ", RAID0");
119 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
120 snprintf(description+written, 8, "%s", ", RAID1");
122 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
123 snprintf(description+written, 6, "%s", ", DUP");
125 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
126 snprintf(description+written, 9, "%s", ", RAID10");
130 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
131 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
132 printf("%s: total=%s, used=%s\n", description, total_bytes,
140 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
142 struct list_head *cur;
143 struct btrfs_device *device;
145 list_for_each(cur, &fs_devices->devices) {
146 device = list_entry(cur, struct btrfs_device, dev_list);
147 if ((device->label && strcmp(device->label, search) == 0) ||
148 strcmp(device->name, search) == 0)
154 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
157 struct list_head *cur;
158 struct btrfs_device *device;
159 char *super_bytes_used;
163 uuid_unparse(fs_devices->fsid, uuidbuf);
164 device = list_entry(fs_devices->devices.next, struct btrfs_device,
166 if (device->label && device->label[0])
167 printf("Label: '%s' ", device->label);
169 printf("Label: none ");
171 super_bytes_used = pretty_sizes(device->super_bytes_used);
173 total = device->total_devs;
174 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
175 (unsigned long long)total, super_bytes_used);
177 free(super_bytes_used);
179 list_for_each(cur, &fs_devices->devices) {
182 device = list_entry(cur, struct btrfs_device, dev_list);
183 total_bytes = pretty_sizes(device->total_bytes);
184 bytes_used = pretty_sizes(device->bytes_used);
185 printf("\tdevid %4llu size %s used %s path %s\n",
186 (unsigned long long)device->devid,
187 total_bytes, bytes_used, device->name);
192 if (devs_found < total) {
193 printf("\t*** Some devices missing\n");
198 int do_show_filesystem(int argc, char **argv)
200 struct list_head *all_uuids;
201 struct btrfs_fs_devices *fs_devices;
202 struct list_head *cur_uuid;
208 if( argc >= 2 && !strcmp(argv[1],"--all-devices")){
213 if( argc > searchstart+1 ){
214 fprintf(stderr, "ERROR: too many arguments\n");
219 ret = btrfs_scan_block_devices(0);
221 ret = btrfs_scan_one_dir("/dev", 0);
224 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
228 if(searchstart < argc)
229 search = argv[searchstart];
231 all_uuids = btrfs_scanned_uuids();
232 list_for_each(cur_uuid, all_uuids) {
233 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
235 if (search && uuid_search(fs_devices, search) == 0)
237 print_one_uuid(fs_devices);
239 printf("%s\n", BTRFS_BUILD_VERSION);
243 int do_fssync(int argc, char **argv)
246 char *path = argv[1];
248 fd = open_file_or_dir(path);
250 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
254 printf("FSSync '%s'\n", path);
255 res = ioctl(fd, BTRFS_IOC_SYNC);
259 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
267 static u64 parse_size(char *s)
273 if (!isdigit(s[len - 1])) {
274 c = tolower(s[len - 1]);
285 fprintf(stderr, "Unknown size descriptor %c\n", c);
290 return atoll(s) * mult;
293 static int parse_compress_type(char *s)
295 if (strcmp(optarg, "zlib") == 0)
296 return BTRFS_COMPRESS_ZLIB;
297 else if (strcmp(optarg, "lzo") == 0)
298 return BTRFS_COMPRESS_LZO;
300 fprintf(stderr, "Unknown compress type %s\n", s);
305 int do_defrag(int ac, char **av)
317 struct btrfs_ioctl_defrag_range_args range;
319 int compress_type = BTRFS_COMPRESS_NONE;
323 int c = getopt(ac, av, "vc::fs:l:t:");
328 compress_type = BTRFS_COMPRESS_ZLIB;
330 compress_type = parse_compress_type(optarg);
341 start = parse_size(optarg);
345 len = parse_size(optarg);
349 thresh = parse_size(optarg);
353 fprintf(stderr, "Invalid arguments for defragment\n");
358 if (ac - optind == 0) {
359 fprintf(stderr, "Invalid arguments for defragment\n");
364 memset(&range, 0, sizeof(range));
367 range.extent_thresh = thresh;
369 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
370 range.compress_type = compress_type;
373 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
375 for (i = optind; i < ac; i++) {
377 printf("%s\n", av[i]);
378 fd = open_file_or_dir(av[i]);
380 fprintf(stderr, "failed to open %s\n", av[i]);
386 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
389 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
390 if (ret && errno == ENOTTY) {
391 fprintf(stderr, "ERROR: defrag range ioctl not "
392 "supported in this kernel, please try "
393 "without any options.\n");
400 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
407 printf("%s\n", BTRFS_BUILD_VERSION);
409 fprintf(stderr, "total %d failures\n", errors);
417 int do_balance(int argc, char **argv)
421 struct btrfs_ioctl_vol_args args;
422 char *path = argv[1];
424 fdmnt = open_file_or_dir(path);
426 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
430 memset(&args, 0, sizeof(args));
431 ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
435 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n",
443 int do_resize(int argc, char **argv)
446 struct btrfs_ioctl_vol_args args;
448 char *amount=argv[1], *path=argv[2];
450 fd = open_file_or_dir(path);
452 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
455 len = strlen(amount);
456 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
457 fprintf(stderr, "ERROR: size value too long ('%s)\n",
462 printf("Resize '%s' of '%s'\n", path, amount);
463 strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
464 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
468 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
475 int do_change_label(int nargs, char **argv)
477 /* check the number of argument */
479 fprintf(stderr, "ERROR: '%s' requires maximum 2 args\n",
482 }else if (nargs == 2){
483 return get_label(argv[1]);
484 } else { /* nargs == 0 */
485 return set_label(argv[1], argv[2]);