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;
55 DIR *dirstream = NULL;
57 if (check_argc_exact(argc, 2))
62 fd = open_file_or_dir(path, &dirstream);
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",
82 if (!sargs->total_spaces) {
87 count = sargs->total_spaces;
89 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
90 (count * sizeof(struct btrfs_ioctl_space_info)));
97 sargs->space_slots = count;
98 sargs->total_spaces = 0;
100 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
103 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
108 for (i = 0; i < sargs->total_spaces; i++) {
109 char description[80];
111 u64 flags = sargs->spaces[i].flags;
113 memset(description, 0, 80);
115 if (flags & BTRFS_BLOCK_GROUP_DATA) {
116 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
117 snprintf(description, 14, "%s",
121 snprintf(description, 5, "%s", "Data");
124 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
125 snprintf(description, 7, "%s", "System");
127 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
128 snprintf(description, 9, "%s", "Metadata");
132 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
133 snprintf(description+written, 8, "%s", ", RAID0");
135 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
136 snprintf(description+written, 8, "%s", ", RAID1");
138 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
139 snprintf(description+written, 6, "%s", ", DUP");
141 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
142 snprintf(description+written, 9, "%s", ", RAID10");
144 } else if (flags & BTRFS_BLOCK_GROUP_RAID5) {
145 snprintf(description+written, 9, "%s", ", RAID5");
147 } else if (flags & BTRFS_BLOCK_GROUP_RAID6) {
148 snprintf(description+written, 9, "%s", ", RAID6");
152 printf("%s: total=%s, used=%s\n", description,
153 pretty_size(sargs->spaces[i].total_bytes),
154 pretty_size(sargs->spaces[i].used_bytes));
157 close_file_or_dir(fd, dirstream);
163 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
166 struct list_head *cur;
167 struct btrfs_device *device;
168 int search_len = strlen(search);
170 search_len = min(search_len, 37);
171 uuid_unparse(fs_devices->fsid, uuidbuf);
172 if (!strncmp(uuidbuf, search, search_len))
175 list_for_each(cur, &fs_devices->devices) {
176 device = list_entry(cur, struct btrfs_device, dev_list);
177 if ((device->label && strcmp(device->label, search) == 0) ||
178 strcmp(device->name, search) == 0)
184 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
187 struct list_head *cur;
188 struct btrfs_device *device;
192 uuid_unparse(fs_devices->fsid, uuidbuf);
193 device = list_entry(fs_devices->devices.next, struct btrfs_device,
195 if (device->label && device->label[0])
196 printf("Label: '%s' ", device->label);
198 printf("Label: none ");
201 total = device->total_devs;
202 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
203 (unsigned long long)total,
204 pretty_size(device->super_bytes_used));
206 list_for_each(cur, &fs_devices->devices) {
207 device = list_entry(cur, struct btrfs_device, dev_list);
209 printf("\tdevid %4llu size %s used %s path %s\n",
210 (unsigned long long)device->devid,
211 pretty_size(device->total_bytes),
212 pretty_size(device->bytes_used), device->name);
216 if (devs_found < total) {
217 printf("\t*** Some devices missing\n");
222 static const char * const cmd_show_usage[] = {
223 "btrfs filesystem show [--all-devices|<uuid>]",
224 "Show the structure of a filesystem",
225 "If no argument is given, structure of all present filesystems is shown.",
229 static int cmd_show(int argc, char **argv)
231 struct list_head *all_uuids;
232 struct btrfs_fs_devices *fs_devices;
233 struct list_head *cur_uuid;
236 int where = BTRFS_SCAN_PROC;
239 if( argc > 1 && !strcmp(argv[1],"--all-devices")){
240 where = BTRFS_SCAN_DEV;
244 if (check_argc_max(argc, searchstart + 1))
245 usage(cmd_show_usage);
247 ret = scan_for_btrfs(where, 0);
250 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
254 if(searchstart < argc)
255 search = argv[searchstart];
257 all_uuids = btrfs_scanned_uuids();
258 list_for_each(cur_uuid, all_uuids) {
259 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
261 if (search && uuid_search(fs_devices, search) == 0)
263 print_one_uuid(fs_devices);
265 printf("%s\n", BTRFS_BUILD_VERSION);
269 static const char * const cmd_sync_usage[] = {
270 "btrfs filesystem sync <path>",
271 "Force a sync on a filesystem",
275 static int cmd_sync(int argc, char **argv)
279 DIR *dirstream = NULL;
281 if (check_argc_exact(argc, 2))
282 usage(cmd_sync_usage);
286 fd = open_file_or_dir(path, &dirstream);
288 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
292 printf("FSSync '%s'\n", path);
293 res = ioctl(fd, BTRFS_IOC_SYNC);
295 close_file_or_dir(fd, dirstream);
297 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
305 static int parse_compress_type(char *s)
307 if (strcmp(optarg, "zlib") == 0)
308 return BTRFS_COMPRESS_ZLIB;
309 else if (strcmp(optarg, "lzo") == 0)
310 return BTRFS_COMPRESS_LZO;
312 fprintf(stderr, "Unknown compress type %s\n", s);
317 static const char * const cmd_defrag_usage[] = {
318 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
319 "Defragment a file or a directory",
322 "-c[zlib,lzo] compress the file while defragmenting",
323 "-f flush data to disk immediately after defragmenting",
324 "-s start defragment only from byte onward",
325 "-l len defragment only up to len bytes",
326 "-t size minimal size of file to be considered for defragmenting",
330 static int cmd_defrag(int argc, char **argv)
342 struct btrfs_ioctl_defrag_range_args range;
344 int compress_type = BTRFS_COMPRESS_NONE;
345 DIR *dirstream = NULL;
349 int c = getopt(argc, argv, "vc::fs:l:t:");
355 compress_type = BTRFS_COMPRESS_ZLIB;
357 compress_type = parse_compress_type(optarg);
368 start = parse_size(optarg);
372 len = parse_size(optarg);
376 thresh = parse_size(optarg);
380 usage(cmd_defrag_usage);
384 if (check_argc_min(argc - optind, 1))
385 usage(cmd_defrag_usage);
387 memset(&range, 0, sizeof(range));
390 range.extent_thresh = thresh;
392 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
393 range.compress_type = compress_type;
396 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
398 for (i = optind; i < argc; i++) {
400 printf("%s\n", argv[i]);
401 fd = open_file_or_dir(argv[i], &dirstream);
403 fprintf(stderr, "failed to open %s\n", argv[i]);
409 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
412 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
413 if (ret && errno == ENOTTY) {
414 fprintf(stderr, "ERROR: defrag range ioctl not "
415 "supported in this kernel, please try "
416 "without any options.\n");
424 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
425 argv[i], strerror(e));
428 close_file_or_dir(fd, dirstream);
431 printf("%s\n", BTRFS_BUILD_VERSION);
433 fprintf(stderr, "total %d failures\n", errors);
440 static const char * const cmd_resize_usage[] = {
441 "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
442 "Resize a filesystem",
443 "If 'max' is passed, the filesystem will occupy all available space",
444 "on the device 'devid'.",
448 static int cmd_resize(int argc, char **argv)
450 struct btrfs_ioctl_vol_args args;
453 DIR *dirstream = NULL;
455 if (check_argc_exact(argc, 3))
456 usage(cmd_resize_usage);
461 len = strlen(amount);
462 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
463 fprintf(stderr, "ERROR: size value too long ('%s)\n",
468 fd = open_file_or_dir(path, &dirstream);
470 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
474 printf("Resize '%s' of '%s'\n", path, amount);
475 strncpy_null(args.name, amount);
476 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
478 close_file_or_dir(fd, dirstream);
480 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
487 static const char * const cmd_label_usage[] = {
488 "btrfs filesystem label [<device>|<mountpoint>] [<newlabel>]",
489 "Get or change the label of a filesystem",
490 "With one argument, get the label of filesystem on <device>.",
491 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
495 static int cmd_label(int argc, char **argv)
497 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
498 usage(cmd_label_usage);
501 return set_label(argv[1], argv[2]);
503 return get_label(argv[1]);
506 const struct cmd_group filesystem_cmd_group = {
507 filesystem_cmd_group_usage, NULL, {
508 { "df", cmd_df, cmd_df_usage, NULL, 0 },
509 { "show", cmd_show, cmd_show_usage, NULL, 0 },
510 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
511 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
512 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
513 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
514 { "label", cmd_label, cmd_label_usage, NULL, 0 },
519 int cmd_filesystem(int argc, char **argv)
521 return handle_command_group(&filesystem_cmd_group, argc, argv);