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 char *group_type_str(u64 flag)
48 switch (flag & BTRFS_BLOCK_GROUP_TYPE_MASK) {
49 case BTRFS_BLOCK_GROUP_DATA:
51 case BTRFS_BLOCK_GROUP_SYSTEM:
53 case BTRFS_BLOCK_GROUP_METADATA:
55 case BTRFS_BLOCK_GROUP_DATA|BTRFS_BLOCK_GROUP_METADATA:
56 return "Data+Metadata";
62 static char *group_profile_str(u64 flag)
64 switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
67 case BTRFS_BLOCK_GROUP_RAID0:
69 case BTRFS_BLOCK_GROUP_RAID1:
71 case BTRFS_BLOCK_GROUP_RAID5:
73 case BTRFS_BLOCK_GROUP_RAID6:
75 case BTRFS_BLOCK_GROUP_DUP:
77 case BTRFS_BLOCK_GROUP_RAID10:
84 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
88 struct btrfs_ioctl_space_args *sargs;
90 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
94 sargs->space_slots = 0;
95 sargs->total_spaces = 0;
97 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
100 fprintf(stderr, "ERROR: couldn't get space info - %s\n",
105 if (!sargs->total_spaces) {
109 count = sargs->total_spaces;
112 sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
113 (count * sizeof(struct btrfs_ioctl_space_info)));
117 sargs->space_slots = count;
118 sargs->total_spaces = 0;
119 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
122 fprintf(stderr, "ERROR: get space info count %llu - %s\n",
131 static void print_df(struct btrfs_ioctl_space_args *sargs)
134 struct btrfs_ioctl_space_info *sp = sargs->spaces;
136 for (i = 0; i < sargs->total_spaces; i++, sp++) {
137 printf("%s, %s: total=%s, used=%s\n",
138 group_type_str(sp->flags),
139 group_profile_str(sp->flags),
140 pretty_size(sp->total_bytes),
141 pretty_size(sp->used_bytes));
145 static int cmd_df(int argc, char **argv)
147 struct btrfs_ioctl_space_args *sargs = NULL;
151 DIR *dirstream = NULL;
153 if (check_argc_exact(argc, 2))
158 fd = open_file_or_dir(path, &dirstream);
160 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
163 ret = get_df(fd, &sargs);
169 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(ret));
172 close_file_or_dir(fd, dirstream);
176 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
179 struct list_head *cur;
180 struct btrfs_device *device;
181 int search_len = strlen(search);
183 search_len = min(search_len, 37);
184 uuid_unparse(fs_devices->fsid, uuidbuf);
185 if (!strncmp(uuidbuf, search, search_len))
188 list_for_each(cur, &fs_devices->devices) {
189 device = list_entry(cur, struct btrfs_device, dev_list);
190 if ((device->label && strcmp(device->label, search) == 0) ||
191 strcmp(device->name, search) == 0)
198 * Sort devices by devid, ascending
200 static int cmp_device_id(void *priv, struct list_head *a,
203 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
205 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
208 return da->devid < db->devid ? -1 :
209 da->devid > db->devid ? 1 : 0;
212 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
215 struct list_head *cur;
216 struct btrfs_device *device;
220 uuid_unparse(fs_devices->fsid, uuidbuf);
221 device = list_entry(fs_devices->devices.next, struct btrfs_device,
223 if (device->label && device->label[0])
224 printf("Label: '%s' ", device->label);
226 printf("Label: none ");
229 total = device->total_devs;
230 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
231 (unsigned long long)total,
232 pretty_size(device->super_bytes_used));
234 list_sort(NULL, &fs_devices->devices, cmp_device_id);
235 list_for_each(cur, &fs_devices->devices) {
236 device = list_entry(cur, struct btrfs_device, dev_list);
238 printf("\tdevid %4llu size %s used %s path %s\n",
239 (unsigned long long)device->devid,
240 pretty_size(device->total_bytes),
241 pretty_size(device->bytes_used), device->name);
245 if (devs_found < total) {
246 printf("\t*** Some devices missing\n");
251 static const char * const cmd_show_usage[] = {
252 "btrfs filesystem show [--all-devices|<uuid>]",
253 "Show the structure of a filesystem",
254 "If no argument is given, structure of all present filesystems is shown.",
258 static int cmd_show(int argc, char **argv)
260 struct list_head *all_uuids;
261 struct btrfs_fs_devices *fs_devices;
262 struct list_head *cur_uuid;
265 int where = BTRFS_SCAN_PROC;
268 if( argc > 1 && !strcmp(argv[1],"--all-devices")){
269 where = BTRFS_SCAN_DEV;
273 if (check_argc_max(argc, searchstart + 1))
274 usage(cmd_show_usage);
276 ret = scan_for_btrfs(where, 0);
279 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
283 if(searchstart < argc)
284 search = argv[searchstart];
286 all_uuids = btrfs_scanned_uuids();
287 list_for_each(cur_uuid, all_uuids) {
288 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
290 if (search && uuid_search(fs_devices, search) == 0)
292 print_one_uuid(fs_devices);
294 printf("%s\n", BTRFS_BUILD_VERSION);
298 static const char * const cmd_sync_usage[] = {
299 "btrfs filesystem sync <path>",
300 "Force a sync on a filesystem",
304 static int cmd_sync(int argc, char **argv)
308 DIR *dirstream = NULL;
310 if (check_argc_exact(argc, 2))
311 usage(cmd_sync_usage);
315 fd = open_file_or_dir(path, &dirstream);
317 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
321 printf("FSSync '%s'\n", path);
322 res = ioctl(fd, BTRFS_IOC_SYNC);
324 close_file_or_dir(fd, dirstream);
326 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
334 static int parse_compress_type(char *s)
336 if (strcmp(optarg, "zlib") == 0)
337 return BTRFS_COMPRESS_ZLIB;
338 else if (strcmp(optarg, "lzo") == 0)
339 return BTRFS_COMPRESS_LZO;
341 fprintf(stderr, "Unknown compress type %s\n", s);
346 static const char * const cmd_defrag_usage[] = {
347 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
348 "Defragment a file or a directory",
351 "-c[zlib,lzo] compress the file while defragmenting",
352 "-f flush data to disk immediately after defragmenting",
353 "-s start defragment only from byte onward",
354 "-l len defragment only up to len bytes",
355 "-t size minimal size of file to be considered for defragmenting",
359 static int cmd_defrag(int argc, char **argv)
371 struct btrfs_ioctl_defrag_range_args range;
373 int compress_type = BTRFS_COMPRESS_NONE;
374 DIR *dirstream = NULL;
378 int c = getopt(argc, argv, "vc::fs:l:t:");
384 compress_type = BTRFS_COMPRESS_ZLIB;
386 compress_type = parse_compress_type(optarg);
397 start = parse_size(optarg);
401 len = parse_size(optarg);
405 thresh = parse_size(optarg);
409 usage(cmd_defrag_usage);
413 if (check_argc_min(argc - optind, 1))
414 usage(cmd_defrag_usage);
416 memset(&range, 0, sizeof(range));
419 range.extent_thresh = thresh;
421 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
422 range.compress_type = compress_type;
425 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
427 for (i = optind; i < argc; i++) {
429 printf("%s\n", argv[i]);
430 fd = open_file_or_dir(argv[i], &dirstream);
432 fprintf(stderr, "failed to open %s\n", argv[i]);
438 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
441 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
442 if (ret && errno == ENOTTY) {
443 fprintf(stderr, "ERROR: defrag range ioctl not "
444 "supported in this kernel, please try "
445 "without any options.\n");
453 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
454 argv[i], strerror(e));
457 close_file_or_dir(fd, dirstream);
460 printf("%s\n", BTRFS_BUILD_VERSION);
462 fprintf(stderr, "total %d failures\n", errors);
467 static const char * const cmd_resize_usage[] = {
468 "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
469 "Resize a filesystem",
470 "If 'max' is passed, the filesystem will occupy all available space",
471 "on the device 'devid'.",
475 static int cmd_resize(int argc, char **argv)
477 struct btrfs_ioctl_vol_args args;
480 DIR *dirstream = NULL;
482 if (check_argc_exact(argc, 3))
483 usage(cmd_resize_usage);
488 len = strlen(amount);
489 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
490 fprintf(stderr, "ERROR: size value too long ('%s)\n",
495 fd = open_file_or_dir(path, &dirstream);
497 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
501 printf("Resize '%s' of '%s'\n", path, amount);
502 strncpy_null(args.name, amount);
503 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
505 close_file_or_dir(fd, dirstream);
507 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
514 static const char * const cmd_label_usage[] = {
515 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
516 "Get or change the label of a filesystem",
517 "With one argument, get the label of filesystem on <device>.",
518 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
522 static int cmd_label(int argc, char **argv)
524 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
525 usage(cmd_label_usage);
528 return set_label(argv[1], argv[2]);
530 return get_label(argv[1]);
533 const struct cmd_group filesystem_cmd_group = {
534 filesystem_cmd_group_usage, NULL, {
535 { "df", cmd_df, cmd_df_usage, NULL, 0 },
536 { "show", cmd_show, cmd_show_usage, NULL, 0 },
537 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
538 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
539 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
540 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
541 { "label", cmd_label, cmd_label_usage, NULL, 0 },
546 int cmd_filesystem(int argc, char **argv)
548 return handle_command_group(&filesystem_cmd_group, argc, argv);