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"
36 static const char * const filesystem_cmd_group_usage[] = {
37 "btrfs filesystem [<group>] <command> [<args>]",
41 static const char * const cmd_df_usage[] = {
42 "btrfs filesystem df <path>",
43 "Show space usage information for a mount point",
47 static int cmd_df(int argc, char **argv)
49 struct btrfs_ioctl_space_args *sargs, *sargs_orig;
56 if (check_argc_exact(argc, 2))
61 fd = open_file_or_dir(path);
63 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
67 sargs_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
71 sargs->space_slots = 0;
72 sargs->total_spaces = 0;
74 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
77 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
83 if (!sargs->total_spaces) {
89 count = sargs->total_spaces;
91 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
92 (count * sizeof(struct btrfs_ioctl_space_info)));
99 sargs->space_slots = count;
100 sargs->total_spaces = 0;
102 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
105 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
112 for (i = 0; i < sargs->total_spaces; i++) {
113 char description[80];
117 u64 flags = sargs->spaces[i].flags;
119 memset(description, 0, 80);
121 if (flags & BTRFS_BLOCK_GROUP_DATA) {
122 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
123 snprintf(description, 14, "%s",
127 snprintf(description, 5, "%s", "Data");
130 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
131 snprintf(description, 7, "%s", "System");
133 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
134 snprintf(description, 9, "%s", "Metadata");
138 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
139 snprintf(description+written, 8, "%s", ", RAID0");
141 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
142 snprintf(description+written, 8, "%s", ", RAID1");
144 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
145 snprintf(description+written, 6, "%s", ", DUP");
147 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
148 snprintf(description+written, 9, "%s", ", RAID10");
150 } else if (flags & BTRFS_BLOCK_GROUP_RAID5) {
151 snprintf(description+written, 9, "%s", ", RAID5");
153 } else if (flags & BTRFS_BLOCK_GROUP_RAID6) {
154 snprintf(description+written, 9, "%s", ", RAID6");
158 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
159 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
160 printf("%s: total=%s, used=%s\n", description, total_bytes,
169 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
172 struct list_head *cur;
173 struct btrfs_device *device;
174 int search_len = strlen(search);
176 search_len = min(search_len, 37);
177 uuid_unparse(fs_devices->fsid, uuidbuf);
178 if (!strncmp(uuidbuf, search, search_len))
181 list_for_each(cur, &fs_devices->devices) {
182 device = list_entry(cur, struct btrfs_device, dev_list);
183 if ((device->label && strcmp(device->label, search) == 0) ||
184 strcmp(device->name, search) == 0)
190 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
193 struct list_head *cur;
194 struct btrfs_device *device;
195 char *super_bytes_used;
199 uuid_unparse(fs_devices->fsid, uuidbuf);
200 device = list_entry(fs_devices->devices.next, struct btrfs_device,
202 if (device->label && device->label[0])
203 printf("Label: '%s' ", device->label);
205 printf("Label: none ");
207 super_bytes_used = pretty_sizes(device->super_bytes_used);
209 total = device->total_devs;
210 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
211 (unsigned long long)total, super_bytes_used);
213 free(super_bytes_used);
215 list_for_each(cur, &fs_devices->devices) {
218 device = list_entry(cur, struct btrfs_device, dev_list);
219 total_bytes = pretty_sizes(device->total_bytes);
220 bytes_used = pretty_sizes(device->bytes_used);
221 printf("\tdevid %4llu size %s used %s path %s\n",
222 (unsigned long long)device->devid,
223 total_bytes, bytes_used, device->name);
228 if (devs_found < total) {
229 printf("\t*** Some devices missing\n");
234 static const char * const cmd_show_usage[] = {
235 "btrfs filesystem show [--all-devices] [<uuid>|<label>]",
236 "Show the structure of a filesystem",
237 "If no argument is given, structure of all present filesystems is shown.",
241 static int cmd_show(int argc, char **argv)
243 struct list_head *all_uuids;
244 struct btrfs_fs_devices *fs_devices;
245 struct list_head *cur_uuid;
251 if( argc > 1 && !strcmp(argv[1],"--all-devices")){
256 if (check_argc_max(argc, searchstart + 1))
257 usage(cmd_show_usage);
260 ret = btrfs_scan_block_devices(0);
262 ret = btrfs_scan_one_dir("/dev", 0);
265 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
269 if(searchstart < argc)
270 search = argv[searchstart];
272 all_uuids = btrfs_scanned_uuids();
273 list_for_each(cur_uuid, all_uuids) {
274 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
276 if (search && uuid_search(fs_devices, search) == 0)
278 print_one_uuid(fs_devices);
280 printf("%s\n", BTRFS_BUILD_VERSION);
284 static const char * const cmd_sync_usage[] = {
285 "btrfs filesystem sync <path>",
286 "Force a sync on a filesystem",
290 static int cmd_sync(int argc, char **argv)
295 if (check_argc_exact(argc, 2))
296 usage(cmd_sync_usage);
300 fd = open_file_or_dir(path);
302 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
306 printf("FSSync '%s'\n", path);
307 res = ioctl(fd, BTRFS_IOC_SYNC);
311 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
319 static int parse_compress_type(char *s)
321 if (strcmp(optarg, "zlib") == 0)
322 return BTRFS_COMPRESS_ZLIB;
323 else if (strcmp(optarg, "lzo") == 0)
324 return BTRFS_COMPRESS_LZO;
326 fprintf(stderr, "Unknown compress type %s\n", s);
331 static const char * const cmd_defrag_usage[] = {
332 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
333 "Defragment a file or a directory",
336 "-c[zlib,lzo] compress the file while defragmenting",
337 "-f flush data to disk immediately after defragmenting",
338 "-s start defragment only from byte onward",
339 "-l len defragment only up to len bytes",
340 "-t size minimal size of file to be considered for defragmenting",
344 static int cmd_defrag(int argc, char **argv)
356 struct btrfs_ioctl_defrag_range_args range;
358 int compress_type = BTRFS_COMPRESS_NONE;
362 int c = getopt(argc, argv, "vc::fs:l:t:");
368 compress_type = BTRFS_COMPRESS_ZLIB;
370 compress_type = parse_compress_type(optarg);
381 start = parse_size(optarg);
385 len = parse_size(optarg);
389 thresh = parse_size(optarg);
393 usage(cmd_defrag_usage);
397 if (check_argc_min(argc - optind, 1))
398 usage(cmd_defrag_usage);
400 memset(&range, 0, sizeof(range));
403 range.extent_thresh = thresh;
405 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
406 range.compress_type = compress_type;
409 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
411 for (i = optind; i < argc; i++) {
413 printf("%s\n", argv[i]);
414 fd = open_file_or_dir(argv[i]);
416 fprintf(stderr, "failed to open %s\n", argv[i]);
422 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
425 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
426 if (ret && errno == ENOTTY) {
427 fprintf(stderr, "ERROR: defrag range ioctl not "
428 "supported in this kernel, please try "
429 "without any options.\n");
436 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
437 argv[i], strerror(e));
443 printf("%s\n", BTRFS_BUILD_VERSION);
445 fprintf(stderr, "total %d failures\n", errors);
452 static const char * const cmd_resize_usage[] = {
453 "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
454 "Resize a filesystem",
455 "If 'max' is passed, the filesystem will occupy all available space",
456 "on the device 'devid'.",
460 static int cmd_resize(int argc, char **argv)
462 struct btrfs_ioctl_vol_args args;
466 if (check_argc_exact(argc, 3))
467 usage(cmd_resize_usage);
472 len = strlen(amount);
473 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
474 fprintf(stderr, "ERROR: size value too long ('%s)\n",
479 fd = open_file_or_dir(path);
481 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
485 printf("Resize '%s' of '%s'\n", path, amount);
486 strncpy_null(args.name, amount);
487 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
491 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
498 static const char * const cmd_label_usage[] = {
499 "btrfs filesystem label [<device>|<mountpoint>] [<newlabel>]",
500 "Get or change the label of a filesystem",
501 "With one argument, get the label of filesystem on <device>.",
502 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
506 static int cmd_label(int argc, char **argv)
508 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
509 usage(cmd_label_usage);
512 return set_label(argv[1], argv[2]);
514 return get_label(argv[1]);
517 const struct cmd_group filesystem_cmd_group = {
518 filesystem_cmd_group_usage, NULL, {
519 { "df", cmd_df, cmd_df_usage, NULL, 0 },
520 { "show", cmd_show, cmd_show_usage, NULL, 0 },
521 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
522 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
523 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
524 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
525 { "label", cmd_label, cmd_label_usage, NULL, 0 },
530 int cmd_filesystem(int argc, char **argv)
532 return handle_command_group(&filesystem_cmd_group, argc, argv);