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.
17 #define _XOPEN_SOURCE 500
22 #include <sys/ioctl.h>
24 #include <uuid/uuid.h>
29 #include "kerncompat.h"
36 #include "list_sort.h"
38 static const char * const filesystem_cmd_group_usage[] = {
39 "btrfs filesystem [<group>] <command> [<args>]",
43 static const char * const cmd_df_usage[] = {
44 "btrfs filesystem df <path>",
45 "Show space usage information for a mount point",
49 static char *group_type_str(u64 flag)
51 switch (flag & BTRFS_BLOCK_GROUP_TYPE_MASK) {
52 case BTRFS_BLOCK_GROUP_DATA:
54 case BTRFS_BLOCK_GROUP_SYSTEM:
56 case BTRFS_BLOCK_GROUP_METADATA:
58 case BTRFS_BLOCK_GROUP_DATA|BTRFS_BLOCK_GROUP_METADATA:
59 return "Data+Metadata";
65 static char *group_profile_str(u64 flag)
67 switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
70 case BTRFS_BLOCK_GROUP_RAID0:
72 case BTRFS_BLOCK_GROUP_RAID1:
74 case BTRFS_BLOCK_GROUP_RAID5:
76 case BTRFS_BLOCK_GROUP_RAID6:
78 case BTRFS_BLOCK_GROUP_DUP:
80 case BTRFS_BLOCK_GROUP_RAID10:
87 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
91 struct btrfs_ioctl_space_args *sargs;
93 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
97 sargs->space_slots = 0;
98 sargs->total_spaces = 0;
100 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
103 fprintf(stderr, "ERROR: couldn't get space info - %s\n",
108 if (!sargs->total_spaces) {
112 count = sargs->total_spaces;
115 sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
116 (count * sizeof(struct btrfs_ioctl_space_info)));
120 sargs->space_slots = count;
121 sargs->total_spaces = 0;
122 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
125 fprintf(stderr, "ERROR: get space info count %llu - %s\n",
134 static void print_df(struct btrfs_ioctl_space_args *sargs)
137 struct btrfs_ioctl_space_info *sp = sargs->spaces;
139 for (i = 0; i < sargs->total_spaces; i++, sp++) {
140 printf("%s, %s: total=%s, used=%s\n",
141 group_type_str(sp->flags),
142 group_profile_str(sp->flags),
143 pretty_size(sp->total_bytes),
144 pretty_size(sp->used_bytes));
148 static int cmd_df(int argc, char **argv)
150 struct btrfs_ioctl_space_args *sargs = NULL;
154 DIR *dirstream = NULL;
156 if (check_argc_exact(argc, 2))
161 fd = open_file_or_dir(path, &dirstream);
163 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
166 ret = get_df(fd, &sargs);
172 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(ret));
175 close_file_or_dir(fd, dirstream);
179 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
182 struct list_head *cur;
183 struct btrfs_device *device;
184 int search_len = strlen(search);
186 search_len = min(search_len, 37);
187 uuid_unparse(fs_devices->fsid, uuidbuf);
188 if (!strncmp(uuidbuf, search, search_len))
191 list_for_each(cur, &fs_devices->devices) {
192 device = list_entry(cur, struct btrfs_device, dev_list);
193 if ((device->label && strcmp(device->label, search) == 0) ||
194 strcmp(device->name, search) == 0)
201 * Sort devices by devid, ascending
203 static int cmp_device_id(void *priv, struct list_head *a,
206 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
208 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
211 return da->devid < db->devid ? -1 :
212 da->devid > db->devid ? 1 : 0;
215 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
218 struct list_head *cur;
219 struct btrfs_device *device;
223 uuid_unparse(fs_devices->fsid, uuidbuf);
224 device = list_entry(fs_devices->devices.next, struct btrfs_device,
226 if (device->label && device->label[0])
227 printf("Label: '%s' ", device->label);
229 printf("Label: none ");
232 total = device->total_devs;
233 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
234 (unsigned long long)total,
235 pretty_size(device->super_bytes_used));
237 list_sort(NULL, &fs_devices->devices, cmp_device_id);
238 list_for_each(cur, &fs_devices->devices) {
239 device = list_entry(cur, struct btrfs_device, dev_list);
241 printf("\tdevid %4llu size %s used %s path %s\n",
242 (unsigned long long)device->devid,
243 pretty_size(device->total_bytes),
244 pretty_size(device->bytes_used), device->name);
248 if (devs_found < total) {
249 printf("\t*** Some devices missing\n");
254 static const char * const cmd_show_usage[] = {
255 "btrfs filesystem show [--all-devices|<uuid>]",
256 "Show the structure of a filesystem",
257 "If no argument is given, structure of all present filesystems is shown.",
261 static int cmd_show(int argc, char **argv)
263 struct list_head *all_uuids;
264 struct btrfs_fs_devices *fs_devices;
265 struct list_head *cur_uuid;
268 int where = BTRFS_SCAN_PROC;
271 if( argc > 1 && !strcmp(argv[1],"--all-devices")){
272 where = BTRFS_SCAN_DEV;
276 if (check_argc_max(argc, searchstart + 1))
277 usage(cmd_show_usage);
279 ret = scan_for_btrfs(where, 0);
282 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
286 if(searchstart < argc)
287 search = argv[searchstart];
289 all_uuids = btrfs_scanned_uuids();
290 list_for_each(cur_uuid, all_uuids) {
291 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
293 if (search && uuid_search(fs_devices, search) == 0)
295 print_one_uuid(fs_devices);
297 printf("%s\n", BTRFS_BUILD_VERSION);
301 static const char * const cmd_sync_usage[] = {
302 "btrfs filesystem sync <path>",
303 "Force a sync on a filesystem",
307 static int cmd_sync(int argc, char **argv)
311 DIR *dirstream = NULL;
313 if (check_argc_exact(argc, 2))
314 usage(cmd_sync_usage);
318 fd = open_file_or_dir(path, &dirstream);
320 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
324 printf("FSSync '%s'\n", path);
325 res = ioctl(fd, BTRFS_IOC_SYNC);
327 close_file_or_dir(fd, dirstream);
329 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
337 static int parse_compress_type(char *s)
339 if (strcmp(optarg, "zlib") == 0)
340 return BTRFS_COMPRESS_ZLIB;
341 else if (strcmp(optarg, "lzo") == 0)
342 return BTRFS_COMPRESS_LZO;
344 fprintf(stderr, "Unknown compress type %s\n", s);
349 static const char * const cmd_defrag_usage[] = {
350 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
351 "Defragment a file or a directory",
354 "-r defragment files recursively",
355 "-c[zlib,lzo] compress the file while defragmenting",
356 "-f flush data to disk immediately after defragmenting",
357 "-s start defragment only from byte onward",
358 "-l len defragment only up to len bytes",
359 "-t size minimal size of file to be considered for defragmenting",
363 static int do_defrag(int fd, int fancy_ioctl,
364 struct btrfs_ioctl_defrag_range_args *range)
369 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
371 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
376 static int defrag_global_fancy_ioctl;
377 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
378 static int defrag_global_verbose;
379 static int defrag_global_errors;
380 static int defrag_callback(const char *fpath, const struct stat *sb,
381 int typeflag, struct FTW *ftwbuf)
387 if (typeflag == FTW_F) {
388 if (defrag_global_verbose)
389 printf("%s\n", fpath);
390 fd = open(fpath, O_RDWR);
394 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
397 if (ret && e == ENOTTY) {
398 fprintf(stderr, "ERROR: defrag range ioctl not "
399 "supported in this kernel, please try "
400 "without any options.\n");
401 defrag_global_errors++;
410 fprintf(stderr, "ERROR: defrag failed on %s - %s\n", fpath, strerror(e));
411 defrag_global_errors++;
415 static int cmd_defrag(int argc, char **argv)
425 struct btrfs_ioctl_defrag_range_args range;
427 int compress_type = BTRFS_COMPRESS_NONE;
430 defrag_global_errors = 0;
431 defrag_global_verbose = 0;
432 defrag_global_errors = 0;
433 defrag_global_fancy_ioctl = 0;
436 int c = getopt(argc, argv, "vrc::fs:l:t:");
442 compress_type = BTRFS_COMPRESS_ZLIB;
444 compress_type = parse_compress_type(optarg);
445 defrag_global_fancy_ioctl = 1;
449 defrag_global_fancy_ioctl = 1;
452 defrag_global_verbose = 1;
455 start = parse_size(optarg);
456 defrag_global_fancy_ioctl = 1;
459 len = parse_size(optarg);
460 defrag_global_fancy_ioctl = 1;
463 thresh = parse_size(optarg);
464 defrag_global_fancy_ioctl = 1;
470 usage(cmd_defrag_usage);
474 if (check_argc_min(argc - optind, 1))
475 usage(cmd_defrag_usage);
477 memset(&defrag_global_range, 0, sizeof(range));
478 defrag_global_range.start = start;
479 defrag_global_range.len = len;
480 defrag_global_range.extent_thresh = thresh;
482 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
483 defrag_global_range.compress_type = compress_type;
486 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
488 for (i = optind; i < argc; i++) {
490 fd = open_file_or_dir(argv[i], &dirstream);
492 fprintf(stderr, "ERROR: failed to open %s - %s\n", argv[i],
494 defrag_global_errors++;
495 close_file_or_dir(fd, dirstream);
502 if (S_ISDIR(st.st_mode)) {
503 ret = nftw(argv[i], defrag_callback, 10,
504 FTW_MOUNT | FTW_PHYS);
507 /* errors are handled in the callback */
510 if (defrag_global_verbose)
511 printf("%s\n", argv[i]);
512 ret = do_defrag(fd, defrag_global_fancy_ioctl,
513 &defrag_global_range);
517 if (defrag_global_verbose)
518 printf("%s\n", argv[i]);
519 ret = do_defrag(fd, defrag_global_fancy_ioctl,
520 &defrag_global_range);
523 close_file_or_dir(fd, dirstream);
524 if (ret && e == ENOTTY) {
525 fprintf(stderr, "ERROR: defrag range ioctl not "
526 "supported in this kernel, please try "
527 "without any options.\n");
528 defrag_global_errors++;
532 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
533 argv[i], strerror(e));
534 defrag_global_errors++;
537 if (defrag_global_verbose)
538 printf("%s\n", BTRFS_BUILD_VERSION);
539 if (defrag_global_errors)
540 fprintf(stderr, "total %d failures\n", defrag_global_errors);
542 return !!defrag_global_errors;
545 static const char * const cmd_resize_usage[] = {
546 "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
547 "Resize a filesystem",
548 "If 'max' is passed, the filesystem will occupy all available space",
549 "on the device 'devid'.",
553 static int cmd_resize(int argc, char **argv)
555 struct btrfs_ioctl_vol_args args;
558 DIR *dirstream = NULL;
560 if (check_argc_exact(argc, 3))
561 usage(cmd_resize_usage);
566 len = strlen(amount);
567 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
568 fprintf(stderr, "ERROR: size value too long ('%s)\n",
573 fd = open_file_or_dir(path, &dirstream);
575 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
579 printf("Resize '%s' of '%s'\n", path, amount);
580 strncpy_null(args.name, amount);
581 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
583 close_file_or_dir(fd, dirstream);
585 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
592 static const char * const cmd_label_usage[] = {
593 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
594 "Get or change the label of a filesystem",
595 "With one argument, get the label of filesystem on <device>.",
596 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
600 static int cmd_label(int argc, char **argv)
602 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
603 usage(cmd_label_usage);
606 return set_label(argv[1], argv[2]);
608 return get_label(argv[1]);
611 const struct cmd_group filesystem_cmd_group = {
612 filesystem_cmd_group_usage, NULL, {
613 { "df", cmd_df, cmd_df_usage, NULL, 0 },
614 { "show", cmd_show, cmd_show_usage, NULL, 0 },
615 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
616 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
617 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
618 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
619 { "label", cmd_label, cmd_label_usage, NULL, 0 },
624 int cmd_filesystem(int argc, char **argv)
626 return handle_command_group(&filesystem_cmd_group, argc, argv);