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"
35 #include "btrfslabel.h"
37 static const char * const filesystem_cmd_group_usage[] = {
38 "btrfs filesystem [<group>] <command> [<args>]",
42 static const char * const cmd_df_usage[] = {
43 "btrfs filesystem df <path>",
44 "Show space usage information for a mount point",
48 static int cmd_df(int argc, char **argv)
50 struct btrfs_ioctl_space_args *sargs, *sargs_orig;
57 if (check_argc_exact(argc, 2))
62 fd = open_file_or_dir(path);
64 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
68 sargs_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
72 sargs->space_slots = 0;
73 sargs->total_spaces = 0;
75 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
78 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
84 if (!sargs->total_spaces) {
90 count = sargs->total_spaces;
92 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
93 (count * sizeof(struct btrfs_ioctl_space_info)));
100 sargs->space_slots = count;
101 sargs->total_spaces = 0;
103 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
106 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
113 for (i = 0; i < sargs->total_spaces; i++) {
114 char description[80];
118 u64 flags = sargs->spaces[i].flags;
120 memset(description, 0, 80);
122 if (flags & BTRFS_BLOCK_GROUP_DATA) {
123 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
124 snprintf(description, 14, "%s",
128 snprintf(description, 5, "%s", "Data");
131 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
132 snprintf(description, 7, "%s", "System");
134 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
135 snprintf(description, 9, "%s", "Metadata");
139 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
140 snprintf(description+written, 8, "%s", ", RAID0");
142 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
143 snprintf(description+written, 8, "%s", ", RAID1");
145 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
146 snprintf(description+written, 6, "%s", ", DUP");
148 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
149 snprintf(description+written, 9, "%s", ", RAID10");
153 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
154 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
155 printf("%s: total=%s, used=%s\n", description, total_bytes,
164 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
167 struct list_head *cur;
168 struct btrfs_device *device;
169 int search_len = strlen(search);
171 search_len = min(search_len, 37);
172 uuid_unparse(fs_devices->fsid, uuidbuf);
173 if (!strncmp(uuidbuf, search, search_len))
176 list_for_each(cur, &fs_devices->devices) {
177 device = list_entry(cur, struct btrfs_device, dev_list);
178 if ((device->label && strcmp(device->label, search) == 0) ||
179 strcmp(device->name, search) == 0)
185 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
188 struct list_head *cur;
189 struct btrfs_device *device;
190 char *super_bytes_used;
194 uuid_unparse(fs_devices->fsid, uuidbuf);
195 device = list_entry(fs_devices->devices.next, struct btrfs_device,
197 if (device->label && device->label[0])
198 printf("Label: '%s' ", device->label);
200 printf("Label: none ");
202 super_bytes_used = pretty_sizes(device->super_bytes_used);
204 total = device->total_devs;
205 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
206 (unsigned long long)total, super_bytes_used);
208 free(super_bytes_used);
210 list_for_each(cur, &fs_devices->devices) {
213 device = list_entry(cur, struct btrfs_device, dev_list);
214 total_bytes = pretty_sizes(device->total_bytes);
215 bytes_used = pretty_sizes(device->bytes_used);
216 printf("\tdevid %4llu size %s used %s path %s\n",
217 (unsigned long long)device->devid,
218 total_bytes, bytes_used, device->name);
223 if (devs_found < total) {
224 printf("\t*** Some devices missing\n");
229 static const char * const cmd_show_usage[] = {
230 "btrfs filesystem show [--all-devices] [<uuid>|<label>]",
231 "Show the structure of a filesystem",
232 "If no argument is given, structure of all present filesystems is shown.",
236 static int cmd_show(int argc, char **argv)
238 struct list_head *all_uuids;
239 struct btrfs_fs_devices *fs_devices;
240 struct list_head *cur_uuid;
246 if( argc > 1 && !strcmp(argv[1],"--all-devices")){
251 if (check_argc_max(argc, searchstart + 1))
252 usage(cmd_show_usage);
255 ret = btrfs_scan_block_devices(0);
257 ret = btrfs_scan_one_dir("/dev", 0);
260 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
264 if(searchstart < argc)
265 search = argv[searchstart];
267 all_uuids = btrfs_scanned_uuids();
268 list_for_each(cur_uuid, all_uuids) {
269 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
271 if (search && uuid_search(fs_devices, search) == 0)
273 print_one_uuid(fs_devices);
275 printf("%s\n", BTRFS_BUILD_VERSION);
279 static const char * const cmd_sync_usage[] = {
280 "btrfs filesystem sync <path>",
281 "Force a sync on a filesystem",
285 static int cmd_sync(int argc, char **argv)
290 if (check_argc_exact(argc, 2))
291 usage(cmd_sync_usage);
295 fd = open_file_or_dir(path);
297 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
301 printf("FSSync '%s'\n", path);
302 res = ioctl(fd, BTRFS_IOC_SYNC);
306 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
314 static int parse_compress_type(char *s)
316 if (strcmp(optarg, "zlib") == 0)
317 return BTRFS_COMPRESS_ZLIB;
318 else if (strcmp(optarg, "lzo") == 0)
319 return BTRFS_COMPRESS_LZO;
321 fprintf(stderr, "Unknown compress type %s\n", s);
326 static const char * const cmd_defrag_usage[] = {
327 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
328 "Defragment a file or a directory",
331 "-c[zlib,lzo] compress the file while defragmenting",
332 "-f flush data to disk immediately after defragmenting",
333 "-s start defragment only from byte onward",
334 "-l len defragment only up to len bytes",
335 "-t size minimal size of file to be considered for defragmenting",
339 static int cmd_defrag(int argc, char **argv)
351 struct btrfs_ioctl_defrag_range_args range;
353 int compress_type = BTRFS_COMPRESS_NONE;
357 int c = getopt(argc, argv, "vc::fs:l:t:");
363 compress_type = BTRFS_COMPRESS_ZLIB;
365 compress_type = parse_compress_type(optarg);
376 start = parse_size(optarg);
380 len = parse_size(optarg);
384 thresh = parse_size(optarg);
388 usage(cmd_defrag_usage);
392 if (check_argc_min(argc - optind, 1))
393 usage(cmd_defrag_usage);
395 memset(&range, 0, sizeof(range));
398 range.extent_thresh = thresh;
400 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
401 range.compress_type = compress_type;
404 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
406 for (i = optind; i < argc; i++) {
408 printf("%s\n", argv[i]);
409 fd = open_file_or_dir(argv[i]);
411 fprintf(stderr, "failed to open %s\n", argv[i]);
417 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
420 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
421 if (ret && errno == ENOTTY) {
422 fprintf(stderr, "ERROR: defrag range ioctl not "
423 "supported in this kernel, please try "
424 "without any options.\n");
431 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
432 argv[i], strerror(e));
438 printf("%s\n", BTRFS_BUILD_VERSION);
440 fprintf(stderr, "total %d failures\n", errors);
447 static const char * const cmd_resize_usage[] = {
448 "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
449 "Resize a filesystem",
450 "If 'max' is passed, the filesystem will occupy all available space",
451 "on the device 'devid'.",
455 static int cmd_resize(int argc, char **argv)
457 struct btrfs_ioctl_vol_args args;
461 if (check_argc_exact(argc, 3))
462 usage(cmd_resize_usage);
467 len = strlen(amount);
468 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
469 fprintf(stderr, "ERROR: size value too long ('%s)\n",
474 fd = open_file_or_dir(path);
476 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
480 printf("Resize '%s' of '%s'\n", path, amount);
481 strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
482 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
483 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
487 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
494 static const char * const cmd_label_usage[] = {
495 "btrfs filesystem label <device> [<newlabel>]",
496 "Get or change the label of an unmounted filesystem",
497 "With one argument, get the label of filesystem on <device>.",
498 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
502 static int cmd_label(int argc, char **argv)
504 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
505 usage(cmd_label_usage);
508 return set_label(argv[1], argv[2]);
510 return get_label(argv[1]);
513 const struct cmd_group filesystem_cmd_group = {
514 filesystem_cmd_group_usage, NULL, {
515 { "df", cmd_df, cmd_df_usage, NULL, 0 },
516 { "show", cmd_show, cmd_show_usage, NULL, 0 },
517 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
518 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
519 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
520 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
521 { "label", cmd_label, cmd_label_usage, NULL, 0 },
526 int cmd_filesystem(int argc, char **argv)
528 return handle_command_group(&filesystem_cmd_group, argc, argv);