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"
33 #include "list_sort.h"
35 static const char * const filesystem_cmd_group_usage[] = {
36 "btrfs filesystem [<group>] <command> [<args>]",
40 static const char * const cmd_df_usage[] = {
41 "btrfs filesystem df <path>",
42 "Show space usage information for a mount point",
46 static int cmd_df(int argc, char **argv)
48 struct btrfs_ioctl_space_args *sargs, *sargs_orig;
54 DIR *dirstream = NULL;
56 if (check_argc_exact(argc, 2))
61 fd = open_file_or_dir(path, &dirstream);
63 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
67 sargs_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
73 sargs->space_slots = 0;
74 sargs->total_spaces = 0;
76 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
79 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
83 if (!sargs->total_spaces) {
88 count = sargs->total_spaces;
90 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
91 (count * sizeof(struct btrfs_ioctl_space_info)));
98 sargs->space_slots = count;
99 sargs->total_spaces = 0;
101 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
104 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
109 for (i = 0; i < sargs->total_spaces; i++) {
110 char description[80];
112 u64 flags = sargs->spaces[i].flags;
114 memset(description, 0, 80);
116 if (flags & BTRFS_BLOCK_GROUP_DATA) {
117 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
118 snprintf(description, 14, "%s",
122 snprintf(description, 5, "%s", "Data");
125 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
126 snprintf(description, 7, "%s", "System");
128 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
129 snprintf(description, 9, "%s", "Metadata");
133 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
134 snprintf(description+written, 8, "%s", ", RAID0");
136 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
137 snprintf(description+written, 8, "%s", ", RAID1");
139 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
140 snprintf(description+written, 6, "%s", ", DUP");
142 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
143 snprintf(description+written, 9, "%s", ", RAID10");
145 } else if (flags & BTRFS_BLOCK_GROUP_RAID5) {
146 snprintf(description+written, 9, "%s", ", RAID5");
148 } else if (flags & BTRFS_BLOCK_GROUP_RAID6) {
149 snprintf(description+written, 9, "%s", ", RAID6");
153 printf("%s: total=%s, used=%s\n", description,
154 pretty_size(sargs->spaces[i].total_bytes),
155 pretty_size(sargs->spaces[i].used_bytes));
158 close_file_or_dir(fd, dirstream);
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)
186 * Sort devices by devid, ascending
188 static int cmp_device_id(void *priv, struct list_head *a,
191 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
193 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
196 return da->devid < db->devid ? -1 :
197 da->devid > db->devid ? 1 : 0;
200 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
203 struct list_head *cur;
204 struct btrfs_device *device;
208 uuid_unparse(fs_devices->fsid, uuidbuf);
209 device = list_entry(fs_devices->devices.next, struct btrfs_device,
211 if (device->label && device->label[0])
212 printf("Label: '%s' ", device->label);
214 printf("Label: none ");
217 total = device->total_devs;
218 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
219 (unsigned long long)total,
220 pretty_size(device->super_bytes_used));
222 list_sort(NULL, &fs_devices->devices, cmp_device_id);
223 list_for_each(cur, &fs_devices->devices) {
224 device = list_entry(cur, struct btrfs_device, dev_list);
226 printf("\tdevid %4llu size %s used %s path %s\n",
227 (unsigned long long)device->devid,
228 pretty_size(device->total_bytes),
229 pretty_size(device->bytes_used), device->name);
233 if (devs_found < total) {
234 printf("\t*** Some devices missing\n");
239 static const char * const cmd_show_usage[] = {
240 "btrfs filesystem show [--all-devices|<uuid>]",
241 "Show the structure of a filesystem",
242 "If no argument is given, structure of all present filesystems is shown.",
246 static int cmd_show(int argc, char **argv)
248 struct list_head *all_uuids;
249 struct btrfs_fs_devices *fs_devices;
250 struct list_head *cur_uuid;
253 int where = BTRFS_SCAN_PROC;
256 if( argc > 1 && !strcmp(argv[1],"--all-devices")){
257 where = BTRFS_SCAN_DEV;
261 if (check_argc_max(argc, searchstart + 1))
262 usage(cmd_show_usage);
264 ret = scan_for_btrfs(where, 0);
267 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
271 if(searchstart < argc)
272 search = argv[searchstart];
274 all_uuids = btrfs_scanned_uuids();
275 list_for_each(cur_uuid, all_uuids) {
276 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
278 if (search && uuid_search(fs_devices, search) == 0)
280 print_one_uuid(fs_devices);
282 printf("%s\n", BTRFS_BUILD_VERSION);
286 static const char * const cmd_sync_usage[] = {
287 "btrfs filesystem sync <path>",
288 "Force a sync on a filesystem",
292 static int cmd_sync(int argc, char **argv)
296 DIR *dirstream = NULL;
298 if (check_argc_exact(argc, 2))
299 usage(cmd_sync_usage);
303 fd = open_file_or_dir(path, &dirstream);
305 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
309 printf("FSSync '%s'\n", path);
310 res = ioctl(fd, BTRFS_IOC_SYNC);
312 close_file_or_dir(fd, dirstream);
314 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
322 static int parse_compress_type(char *s)
324 if (strcmp(optarg, "zlib") == 0)
325 return BTRFS_COMPRESS_ZLIB;
326 else if (strcmp(optarg, "lzo") == 0)
327 return BTRFS_COMPRESS_LZO;
329 fprintf(stderr, "Unknown compress type %s\n", s);
334 static const char * const cmd_defrag_usage[] = {
335 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
336 "Defragment a file or a directory",
339 "-c[zlib,lzo] compress the file while defragmenting",
340 "-f flush data to disk immediately after defragmenting",
341 "-s start defragment only from byte onward",
342 "-l len defragment only up to len bytes",
343 "-t size minimal size of file to be considered for defragmenting",
347 static int cmd_defrag(int argc, char **argv)
359 struct btrfs_ioctl_defrag_range_args range;
361 int compress_type = BTRFS_COMPRESS_NONE;
362 DIR *dirstream = NULL;
366 int c = getopt(argc, argv, "vc::fs:l:t:");
372 compress_type = BTRFS_COMPRESS_ZLIB;
374 compress_type = parse_compress_type(optarg);
385 start = parse_size(optarg);
389 len = parse_size(optarg);
393 thresh = parse_size(optarg);
397 usage(cmd_defrag_usage);
401 if (check_argc_min(argc - optind, 1))
402 usage(cmd_defrag_usage);
404 memset(&range, 0, sizeof(range));
407 range.extent_thresh = thresh;
409 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
410 range.compress_type = compress_type;
413 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
415 for (i = optind; i < argc; i++) {
417 printf("%s\n", argv[i]);
418 fd = open_file_or_dir(argv[i], &dirstream);
420 fprintf(stderr, "failed to open %s\n", argv[i]);
426 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
429 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
430 if (ret && errno == ENOTTY) {
431 fprintf(stderr, "ERROR: defrag range ioctl not "
432 "supported in this kernel, please try "
433 "without any options.\n");
441 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
442 argv[i], strerror(e));
445 close_file_or_dir(fd, dirstream);
448 printf("%s\n", BTRFS_BUILD_VERSION);
450 fprintf(stderr, "total %d failures\n", errors);
455 static const char * const cmd_resize_usage[] = {
456 "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
457 "Resize a filesystem",
458 "If 'max' is passed, the filesystem will occupy all available space",
459 "on the device 'devid'.",
463 static int cmd_resize(int argc, char **argv)
465 struct btrfs_ioctl_vol_args args;
468 DIR *dirstream = NULL;
470 if (check_argc_exact(argc, 3))
471 usage(cmd_resize_usage);
476 len = strlen(amount);
477 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
478 fprintf(stderr, "ERROR: size value too long ('%s)\n",
483 fd = open_file_or_dir(path, &dirstream);
485 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
489 printf("Resize '%s' of '%s'\n", path, amount);
490 strncpy_null(args.name, amount);
491 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
493 close_file_or_dir(fd, dirstream);
495 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
502 static const char * const cmd_label_usage[] = {
503 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
504 "Get or change the label of a filesystem",
505 "With one argument, get the label of filesystem on <device>.",
506 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
510 static int cmd_label(int argc, char **argv)
512 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
513 usage(cmd_label_usage);
516 return set_label(argv[1], argv[2]);
518 return get_label(argv[1]);
521 const struct cmd_group filesystem_cmd_group = {
522 filesystem_cmd_group_usage, NULL, {
523 { "df", cmd_df, cmd_df_usage, NULL, 0 },
524 { "show", cmd_show, cmd_show_usage, NULL, 0 },
525 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
526 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
527 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
528 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
529 { "label", cmd_label, cmd_label_usage, NULL, 0 },
534 int cmd_filesystem(int argc, char **argv)
536 return handle_command_group(&filesystem_cmd_group, argc, argv);