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 <linux/limits.h>
32 #include "kerncompat.h"
39 #include "cmds-fi-disk_usage.h"
40 #include "list_sort.h"
45 * for btrfs fi show, we maintain a hash of fsids we've already printed.
46 * This way we don't print dups if a given FS is mounted more than once.
48 #define SEEN_FSID_HASH_SIZE 256
51 u8 fsid
[BTRFS_FSID_SIZE
];
52 struct seen_fsid
*next
;
55 static struct seen_fsid
*seen_fsid_hash
[SEEN_FSID_HASH_SIZE
] = {NULL
,};
57 static int is_seen_fsid(u8
*fsid
)
60 int slot
= hash
% SEEN_FSID_HASH_SIZE
;
61 struct seen_fsid
*seen
= seen_fsid_hash
[slot
];
66 static int add_seen_fsid(u8
*fsid
)
69 int slot
= hash
% SEEN_FSID_HASH_SIZE
;
70 struct seen_fsid
*seen
= seen_fsid_hash
[slot
];
71 struct seen_fsid
*alloc
;
77 if (memcmp(seen
->fsid
, fsid
, BTRFS_FSID_SIZE
) == 0)
88 alloc
= malloc(sizeof(*alloc
));
93 memcpy(alloc
->fsid
, fsid
, BTRFS_FSID_SIZE
);
98 seen_fsid_hash
[slot
] = alloc
;
103 static void free_seen_fsid(void)
106 struct seen_fsid
*seen
;
107 struct seen_fsid
*next
;
109 for (slot
= 0; slot
< SEEN_FSID_HASH_SIZE
; slot
++) {
110 seen
= seen_fsid_hash
[slot
];
116 seen_fsid_hash
[slot
] = NULL
;
120 static const char * const filesystem_cmd_group_usage
[] = {
121 "btrfs filesystem [<group>] <command> [<args>]",
125 static const char * const cmd_filesystem_df_usage
[] = {
126 "btrfs filesystem df [options] <path>",
127 "Show space usage information for a mount point",
128 "-b|--raw raw numbers in bytes",
129 "-h human friendly numbers, base 1024 (default)",
130 "-H human friendly numbers, base 1000",
131 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
132 "--si use 1000 as a base (kB, MB, GB, TB)",
133 "-k|--kbytes show sizes in KiB, or kB with --si",
134 "-m|--mbytes show sizes in MiB, or MB with --si",
135 "-g|--gbytes show sizes in GiB, or GB with --si",
136 "-t|--tbytes show sizes in TiB, or TB with --si",
140 static int get_df(int fd
, struct btrfs_ioctl_space_args
**sargs_ret
)
144 struct btrfs_ioctl_space_args
*sargs
;
146 sargs
= malloc(sizeof(struct btrfs_ioctl_space_args
));
150 sargs
->space_slots
= 0;
151 sargs
->total_spaces
= 0;
153 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
156 fprintf(stderr
, "ERROR: couldn't get space info - %s\n",
161 /* This really should never happen */
162 if (!sargs
->total_spaces
) {
166 count
= sargs
->total_spaces
;
169 sargs
= malloc(sizeof(struct btrfs_ioctl_space_args
) +
170 (count
* sizeof(struct btrfs_ioctl_space_info
)));
174 sargs
->space_slots
= count
;
175 sargs
->total_spaces
= 0;
176 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
179 fprintf(stderr
, "ERROR: get space info count %llu - %s\n",
188 static void print_df(struct btrfs_ioctl_space_args
*sargs
, unsigned unit_mode
)
191 struct btrfs_ioctl_space_info
*sp
= sargs
->spaces
;
193 for (i
= 0; i
< sargs
->total_spaces
; i
++, sp
++) {
194 printf("%s, %s: total=%s, used=%s\n",
195 btrfs_group_type_str(sp
->flags
),
196 btrfs_group_profile_str(sp
->flags
),
197 pretty_size_mode(sp
->total_bytes
, unit_mode
),
198 pretty_size_mode(sp
->used_bytes
, unit_mode
));
202 static int cmd_filesystem_df(int argc
, char **argv
)
204 struct btrfs_ioctl_space_args
*sargs
= NULL
;
208 DIR *dirstream
= NULL
;
209 unsigned unit_mode
= UNITS_DEFAULT
;
213 static const struct option long_options
[] = {
214 { "raw", no_argument
, NULL
, 'b'},
215 { "kbytes", no_argument
, NULL
, 'k'},
216 { "mbytes", no_argument
, NULL
, 'm'},
217 { "gbytes", no_argument
, NULL
, 'g'},
218 { "tbytes", no_argument
, NULL
, 't'},
219 { "si", no_argument
, NULL
, 256},
220 { "iec", no_argument
, NULL
, 257},
222 int c
= getopt_long(argc
, argv
, "bhHkmgt", long_options
,
228 unit_mode
= UNITS_RAW
;
231 units_set_base(&unit_mode
, UNITS_KBYTES
);
234 units_set_base(&unit_mode
, UNITS_MBYTES
);
237 units_set_base(&unit_mode
, UNITS_GBYTES
);
240 units_set_base(&unit_mode
, UNITS_TBYTES
);
243 unit_mode
= UNITS_HUMAN_BINARY
;
246 unit_mode
= UNITS_HUMAN_DECIMAL
;
249 units_set_mode(&unit_mode
, UNITS_DECIMAL
);
252 units_set_mode(&unit_mode
, UNITS_BINARY
);
255 usage(cmd_filesystem_df_usage
);
259 if (check_argc_exact(argc
, optind
+ 1))
260 usage(cmd_filesystem_df_usage
);
264 fd
= open_file_or_dir(path
, &dirstream
);
266 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
269 ret
= get_df(fd
, &sargs
);
272 print_df(sargs
, unit_mode
);
275 fprintf(stderr
, "ERROR: get_df failed %s\n", strerror(-ret
));
278 close_file_or_dir(fd
, dirstream
);
282 static int match_search_item_kernel(__u8
*fsid
, char *mnt
, char *label
,
285 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
286 int search_len
= strlen(search
);
288 search_len
= min(search_len
, BTRFS_UUID_UNPARSED_SIZE
);
289 uuid_unparse(fsid
, uuidbuf
);
290 if (!strncmp(uuidbuf
, search
, search_len
))
293 if (strlen(label
) && strcmp(label
, search
) == 0)
296 if (strcmp(mnt
, search
) == 0)
302 static int uuid_search(struct btrfs_fs_devices
*fs_devices
, char *search
)
304 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
305 struct list_head
*cur
;
306 struct btrfs_device
*device
;
307 int search_len
= strlen(search
);
309 search_len
= min(search_len
, BTRFS_UUID_UNPARSED_SIZE
);
310 uuid_unparse(fs_devices
->fsid
, uuidbuf
);
311 if (!strncmp(uuidbuf
, search
, search_len
))
314 list_for_each(cur
, &fs_devices
->devices
) {
315 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
316 if ((device
->label
&& strcmp(device
->label
, search
) == 0) ||
317 strcmp(device
->name
, search
) == 0)
324 * Sort devices by devid, ascending
326 static int cmp_device_id(void *priv
, struct list_head
*a
,
329 const struct btrfs_device
*da
= list_entry(a
, struct btrfs_device
,
331 const struct btrfs_device
*db
= list_entry(b
, struct btrfs_device
,
334 return da
->devid
< db
->devid
? -1 :
335 da
->devid
> db
->devid
? 1 : 0;
338 static void splice_device_list(struct list_head
*seed_devices
,
339 struct list_head
*all_devices
)
341 struct btrfs_device
*in_all
, *next_all
;
342 struct btrfs_device
*in_seed
, *next_seed
;
344 list_for_each_entry_safe(in_all
, next_all
, all_devices
, dev_list
) {
345 list_for_each_entry_safe(in_seed
, next_seed
, seed_devices
,
347 if (in_all
->devid
== in_seed
->devid
) {
349 * When do dev replace in a sprout fs
350 * to a dev in its seed fs, the replacing
351 * dev will reside in the sprout fs and
352 * the replaced dev will still exist
354 * So pick the latest one when showing
357 if (in_all
->generation
358 < in_seed
->generation
) {
359 list_del(&in_all
->dev_list
);
361 } else if (in_all
->generation
362 > in_seed
->generation
) {
363 list_del(&in_seed
->dev_list
);
371 list_splice(seed_devices
, all_devices
);
374 static void print_devices(struct btrfs_fs_devices
*fs_devices
,
377 struct btrfs_device
*device
;
378 struct btrfs_fs_devices
*cur_fs
;
379 struct list_head
*all_devices
;
381 all_devices
= &fs_devices
->devices
;
382 cur_fs
= fs_devices
->seed
;
383 /* add all devices of seed fs to the fs to be printed */
385 splice_device_list(&cur_fs
->devices
, all_devices
);
386 cur_fs
= cur_fs
->seed
;
389 list_sort(NULL
, all_devices
, cmp_device_id
);
390 list_for_each_entry(device
, all_devices
, dev_list
) {
391 printf("\tdevid %4llu size %s used %s path %s\n",
392 (unsigned long long)device
->devid
,
393 pretty_size(device
->total_bytes
),
394 pretty_size(device
->bytes_used
), device
->name
);
400 static void print_one_uuid(struct btrfs_fs_devices
*fs_devices
)
402 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
403 struct btrfs_device
*device
;
407 if (add_seen_fsid(fs_devices
->fsid
))
410 uuid_unparse(fs_devices
->fsid
, uuidbuf
);
411 device
= list_entry(fs_devices
->devices
.next
, struct btrfs_device
,
413 if (device
->label
&& device
->label
[0])
414 printf("Label: '%s' ", device
->label
);
416 printf("Label: none ");
418 total
= device
->total_devs
;
419 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf
,
420 (unsigned long long)total
,
421 pretty_size(device
->super_bytes_used
));
423 print_devices(fs_devices
, &devs_found
);
425 if (devs_found
< total
) {
426 printf("\t*** Some devices missing\n");
431 /* adds up all the used spaces as reported by the space info ioctl
433 static u64
calc_used_bytes(struct btrfs_ioctl_space_args
*si
)
437 for (i
= 0; i
< si
->total_spaces
; i
++)
438 ret
+= si
->spaces
[i
].used_bytes
;
442 static int print_one_fs(struct btrfs_ioctl_fs_info_args
*fs_info
,
443 struct btrfs_ioctl_dev_info_args
*dev_info
,
444 struct btrfs_ioctl_space_args
*space_info
,
445 char *label
, char *path
)
450 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
451 struct btrfs_ioctl_dev_info_args
*tmp_dev_info
;
454 ret
= add_seen_fsid(fs_info
->fsid
);
460 uuid_unparse(fs_info
->fsid
, uuidbuf
);
461 if (label
&& strlen(label
))
462 printf("Label: '%s' ", label
);
464 printf("Label: none ");
466 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf
,
467 fs_info
->num_devices
,
468 pretty_size(calc_used_bytes(space_info
)));
470 for (i
= 0; i
< fs_info
->num_devices
; i
++) {
471 char *canonical_path
;
473 tmp_dev_info
= (struct btrfs_ioctl_dev_info_args
*)&dev_info
[i
];
474 canonical_path
= canonicalize_path((char *)tmp_dev_info
->path
);
476 /* Add check for missing devices even mounted */
477 fd
= open((char *)tmp_dev_info
->path
, O_RDONLY
);
483 printf("\tdevid %4llu size %s used %s path %s\n",
485 pretty_size(tmp_dev_info
->total_bytes
),
486 pretty_size(tmp_dev_info
->bytes_used
),
489 free(canonical_path
);
493 printf("\t*** Some devices missing\n");
498 /* This function checks if the given input parameter is
500 * return -1: some error in the given input
501 * return 0: unknow input
502 * return 1: given input is uuid
503 * return 2: given input is path
505 static int check_arg_type(char *input
)
513 if (realpath(input
, path
)) {
514 if (is_block_device(path
) == 1)
515 return BTRFS_ARG_BLKDEV
;
517 if (is_mount_point(path
) == 1)
518 return BTRFS_ARG_MNTPOINT
;
520 return BTRFS_ARG_UNKNOWN
;
523 if (strlen(input
) == (BTRFS_UUID_UNPARSED_SIZE
- 1) &&
524 !uuid_parse(input
, out
))
525 return BTRFS_ARG_UUID
;
527 return BTRFS_ARG_UNKNOWN
;
530 static int btrfs_scan_kernel(void *search
)
536 struct btrfs_ioctl_fs_info_args fs_info_arg
;
537 struct btrfs_ioctl_dev_info_args
*dev_info_arg
= NULL
;
538 struct btrfs_ioctl_space_args
*space_info_arg
= NULL
;
539 char label
[BTRFS_LABEL_SIZE
];
541 f
= setmntent("/proc/self/mounts", "r");
545 memset(label
, 0, sizeof(label
));
546 while ((mnt
= getmntent(f
)) != NULL
) {
547 if (strcmp(mnt
->mnt_type
, "btrfs"))
549 ret
= get_fs_info(mnt
->mnt_dir
, &fs_info_arg
,
554 if (get_label_mounted(mnt
->mnt_dir
, label
)) {
558 if (search
&& !match_search_item_kernel(fs_info_arg
.fsid
,
559 mnt
->mnt_dir
, label
, search
)) {
564 fd
= open(mnt
->mnt_dir
, O_RDONLY
);
565 if ((fd
!= -1) && !get_df(fd
, &space_info_arg
)) {
566 print_one_fs(&fs_info_arg
, dev_info_arg
,
567 space_info_arg
, label
, mnt
->mnt_dir
);
568 kfree(space_info_arg
);
569 memset(label
, 0, sizeof(label
));
582 static int dev_to_fsid(char *dev
, __u8
*fsid
)
584 struct btrfs_super_block
*disk_super
;
593 fd
= open(dev
, O_RDONLY
);
600 disk_super
= (struct btrfs_super_block
*)buf
;
601 ret
= btrfs_read_dev_super(fd
, disk_super
,
602 BTRFS_SUPER_INFO_OFFSET
, 0);
606 memcpy(fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
615 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
617 struct btrfs_fs_devices
*cur_seed
, *next_seed
;
618 struct btrfs_device
*device
;
620 while (!list_empty(&fs_devices
->devices
)) {
621 device
= list_entry(fs_devices
->devices
.next
,
622 struct btrfs_device
, dev_list
);
623 list_del(&device
->dev_list
);
630 /* free seed fs chain */
631 cur_seed
= fs_devices
->seed
;
632 fs_devices
->seed
= NULL
;
634 next_seed
= cur_seed
->seed
;
637 cur_seed
= next_seed
;
640 list_del(&fs_devices
->list
);
644 static int copy_device(struct btrfs_device
*dst
,
645 struct btrfs_device
*src
)
647 dst
->devid
= src
->devid
;
648 memcpy(dst
->uuid
, src
->uuid
, BTRFS_UUID_SIZE
);
649 if (src
->name
== NULL
)
652 dst
->name
= strdup(src
->name
);
656 if (src
->label
== NULL
)
659 dst
->label
= strdup(src
->label
);
665 dst
->total_devs
= src
->total_devs
;
666 dst
->super_bytes_used
= src
->super_bytes_used
;
667 dst
->total_bytes
= src
->total_bytes
;
668 dst
->bytes_used
= src
->bytes_used
;
669 dst
->generation
= src
->generation
;
674 static int copy_fs_devices(struct btrfs_fs_devices
*dst
,
675 struct btrfs_fs_devices
*src
)
677 struct btrfs_device
*cur_dev
, *dev_copy
;
680 memcpy(dst
->fsid
, src
->fsid
, BTRFS_FSID_SIZE
);
681 INIT_LIST_HEAD(&dst
->devices
);
684 list_for_each_entry(cur_dev
, &src
->devices
, dev_list
) {
685 dev_copy
= malloc(sizeof(*dev_copy
));
691 ret
= copy_device(dev_copy
, cur_dev
);
697 list_add(&dev_copy
->dev_list
, &dst
->devices
);
698 dev_copy
->fs_devices
= dst
;
704 static int find_and_copy_seed(struct btrfs_fs_devices
*seed
,
705 struct btrfs_fs_devices
*copy
,
706 struct list_head
*fs_uuids
) {
707 struct btrfs_fs_devices
*cur_fs
;
709 list_for_each_entry(cur_fs
, fs_uuids
, list
)
710 if (!memcmp(seed
->fsid
, cur_fs
->fsid
, BTRFS_FSID_SIZE
))
711 return copy_fs_devices(copy
, cur_fs
);
716 static int map_seed_devices(struct list_head
*all_uuids
,
717 char *search
, int *found
)
719 struct btrfs_fs_devices
*cur_fs
, *cur_seed
;
720 struct btrfs_fs_devices
*fs_copy
, *seed_copy
;
721 struct btrfs_fs_devices
*opened_fs
;
722 struct btrfs_device
*device
;
723 struct btrfs_fs_info
*fs_info
;
724 struct list_head
*fs_uuids
;
727 fs_uuids
= btrfs_scanned_uuids();
730 * The fs_uuids list is global, and open_ctree_* will
731 * modify it, make a private copy here
733 list_for_each_entry(cur_fs
, fs_uuids
, list
) {
734 /* don't bother handle all fs, if search target specified */
736 if (uuid_search(cur_fs
, search
) == 0)
741 /* skip all fs already shown as mounted fs */
742 if (is_seen_fsid(cur_fs
->fsid
))
745 fs_copy
= malloc(sizeof(*fs_copy
));
751 ret
= copy_fs_devices(fs_copy
, cur_fs
);
757 list_add(&fs_copy
->list
, all_uuids
);
760 list_for_each_entry(cur_fs
, all_uuids
, list
) {
761 device
= list_first_entry(&cur_fs
->devices
,
762 struct btrfs_device
, dev_list
);
766 * open_ctree_* detects seed/sprout mapping
768 fs_info
= open_ctree_fs_info(device
->name
, 0, 0,
774 * copy the seed chain under the opened fs
776 opened_fs
= fs_info
->fs_devices
;
778 while (opened_fs
->seed
) {
779 seed_copy
= malloc(sizeof(*seed_copy
));
784 ret
= find_and_copy_seed(opened_fs
->seed
, seed_copy
,
791 cur_seed
->seed
= seed_copy
;
793 opened_fs
= opened_fs
->seed
;
794 cur_seed
= cur_seed
->seed
;
797 close_ctree(fs_info
->chunk_root
);
803 close_ctree(fs_info
->chunk_root
);
807 static const char * const cmd_show_usage
[] = {
808 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
809 "Show the structure of a filesystem",
810 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
811 "-m|--mounted show only mounted btrfs",
812 "If no argument is given, structure of all present filesystems is shown.",
816 static int cmd_show(int argc
, char **argv
)
818 LIST_HEAD(all_uuids
);
819 struct btrfs_fs_devices
*fs_devices
;
822 /* default, search both kernel and udev */
825 char mp
[BTRFS_PATH_NAME_MAX
+ 1];
827 __u8 fsid
[BTRFS_FSID_SIZE
];
828 char uuid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
833 static struct option long_options
[] = {
834 { "all-devices", no_argument
, NULL
, 'd'},
835 { "mounted", no_argument
, NULL
, 'm'},
836 { NULL
, no_argument
, NULL
, 0 },
838 int c
= getopt_long(argc
, argv
, "dm", long_options
,
844 where
= BTRFS_SCAN_LBLKID
;
847 where
= BTRFS_SCAN_MOUNTED
;
850 usage(cmd_show_usage
);
854 if (check_argc_max(argc
, optind
+ 1))
855 usage(cmd_show_usage
);
858 search
= argv
[optind
];
859 if (strlen(search
) == 0)
860 usage(cmd_show_usage
);
861 type
= check_arg_type(search
);
864 * For search is a device:
865 * realpath do /dev/mapper/XX => /dev/dm-X
866 * which is required by BTRFS_SCAN_DEV
867 * For search is a mountpoint:
868 * realpath do /mnt/btrfs/ => /mnt/btrfs
869 * which shall be recognized by btrfs_scan_kernel()
871 if (!realpath(search
, path
)) {
872 fprintf(stderr
, "ERROR: Could not show %s: %s\n",
873 search
, strerror(errno
));
880 * Needs special handling if input arg is block dev And if
881 * input arg is mount-point just print it right away
883 if (type
== BTRFS_ARG_BLKDEV
&& where
!= BTRFS_SCAN_LBLKID
) {
884 ret
= get_btrfs_mount(search
, mp
, sizeof(mp
));
886 /* given block dev is mounted */
888 type
= BTRFS_ARG_MNTPOINT
;
890 ret
= dev_to_fsid(search
, fsid
);
893 "ERROR: No btrfs on %s\n",
897 uuid_unparse(fsid
, uuid_buf
);
899 type
= BTRFS_ARG_UUID
;
905 if (where
== BTRFS_SCAN_LBLKID
)
908 /* show mounted btrfs */
909 ret
= btrfs_scan_kernel(search
);
910 if (search
&& !ret
) {
911 /* since search is found we are done */
915 /* shows mounted only */
916 if (where
== BTRFS_SCAN_MOUNTED
)
920 ret
= btrfs_scan_lblkid();
923 fprintf(stderr
, "ERROR: %d while scanning\n", ret
);
928 * scan_for_btrfs() don't build seed/sprout mapping,
929 * do mapping build for each scanned fs here
931 ret
= map_seed_devices(&all_uuids
, search
, &found
);
934 "ERROR: %d while mapping seed devices\n", ret
);
938 list_for_each_entry(fs_devices
, &all_uuids
, list
)
939 print_one_uuid(fs_devices
);
941 if (search
&& !found
)
944 while (!list_empty(&all_uuids
)) {
945 fs_devices
= list_entry(all_uuids
.next
,
946 struct btrfs_fs_devices
, list
);
947 free_fs_devices(fs_devices
);
950 printf("%s\n", BTRFS_BUILD_VERSION
);
955 static const char * const cmd_sync_usage
[] = {
956 "btrfs filesystem sync <path>",
957 "Force a sync on a filesystem",
961 static int cmd_sync(int argc
, char **argv
)
965 DIR *dirstream
= NULL
;
967 if (check_argc_exact(argc
, 2))
968 usage(cmd_sync_usage
);
972 fd
= open_file_or_dir(path
, &dirstream
);
974 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
978 printf("FSSync '%s'\n", path
);
979 res
= ioctl(fd
, BTRFS_IOC_SYNC
);
981 close_file_or_dir(fd
, dirstream
);
983 fprintf(stderr
, "ERROR: unable to fs-syncing '%s' - %s\n",
991 static int parse_compress_type(char *s
)
993 if (strcmp(optarg
, "zlib") == 0)
994 return BTRFS_COMPRESS_ZLIB
;
995 else if (strcmp(optarg
, "lzo") == 0)
996 return BTRFS_COMPRESS_LZO
;
998 fprintf(stderr
, "Unknown compress type %s\n", s
);
1003 static const char * const cmd_defrag_usage
[] = {
1004 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
1005 "Defragment a file or a directory",
1008 "-r defragment files recursively",
1009 "-c[zlib,lzo] compress the file while defragmenting",
1010 "-f flush data to disk immediately after defragmenting",
1011 "-s start defragment only from byte onward",
1012 "-l len defragment only up to len bytes",
1013 "-t size minimal size of file to be considered for defragmenting",
1017 static int do_defrag(int fd
, int fancy_ioctl
,
1018 struct btrfs_ioctl_defrag_range_args
*range
)
1023 ret
= ioctl(fd
, BTRFS_IOC_DEFRAG
, NULL
);
1025 ret
= ioctl(fd
, BTRFS_IOC_DEFRAG_RANGE
, range
);
1030 static int defrag_global_fancy_ioctl
;
1031 static struct btrfs_ioctl_defrag_range_args defrag_global_range
;
1032 static int defrag_global_verbose
;
1033 static int defrag_global_errors
;
1034 static int defrag_callback(const char *fpath
, const struct stat
*sb
,
1035 int typeflag
, struct FTW
*ftwbuf
)
1041 if ((typeflag
== FTW_F
) && S_ISREG(sb
->st_mode
)) {
1042 if (defrag_global_verbose
)
1043 printf("%s\n", fpath
);
1044 fd
= open(fpath
, O_RDWR
);
1048 ret
= do_defrag(fd
, defrag_global_fancy_ioctl
, &defrag_global_range
);
1051 if (ret
&& e
== ENOTTY
&& defrag_global_fancy_ioctl
) {
1052 fprintf(stderr
, "ERROR: defrag range ioctl not "
1053 "supported in this kernel, please try "
1054 "without any options.\n");
1055 defrag_global_errors
++;
1064 fprintf(stderr
, "ERROR: defrag failed on %s - %s\n", fpath
, strerror(e
));
1065 defrag_global_errors
++;
1069 static int cmd_defrag(int argc
, char **argv
)
1079 struct btrfs_ioctl_defrag_range_args range
;
1081 int compress_type
= BTRFS_COMPRESS_NONE
;
1084 defrag_global_errors
= 0;
1085 defrag_global_verbose
= 0;
1086 defrag_global_errors
= 0;
1087 defrag_global_fancy_ioctl
= 0;
1090 int c
= getopt(argc
, argv
, "vrc::fs:l:t:");
1096 compress_type
= BTRFS_COMPRESS_ZLIB
;
1098 compress_type
= parse_compress_type(optarg
);
1099 defrag_global_fancy_ioctl
= 1;
1103 defrag_global_fancy_ioctl
= 1;
1106 defrag_global_verbose
= 1;
1109 start
= parse_size(optarg
);
1110 defrag_global_fancy_ioctl
= 1;
1113 len
= parse_size(optarg
);
1114 defrag_global_fancy_ioctl
= 1;
1117 thresh
= parse_size(optarg
);
1118 defrag_global_fancy_ioctl
= 1;
1124 usage(cmd_defrag_usage
);
1128 if (check_argc_min(argc
- optind
, 1))
1129 usage(cmd_defrag_usage
);
1131 memset(&defrag_global_range
, 0, sizeof(range
));
1132 defrag_global_range
.start
= start
;
1133 defrag_global_range
.len
= len
;
1134 defrag_global_range
.extent_thresh
= thresh
;
1135 if (compress_type
) {
1136 defrag_global_range
.flags
|= BTRFS_DEFRAG_RANGE_COMPRESS
;
1137 defrag_global_range
.compress_type
= compress_type
;
1140 defrag_global_range
.flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1142 for (i
= optind
; i
< argc
; i
++) {
1146 fd
= open_file_or_dir(argv
[i
], &dirstream
);
1148 fprintf(stderr
, "ERROR: failed to open %s - %s\n", argv
[i
],
1150 defrag_global_errors
++;
1151 close_file_or_dir(fd
, dirstream
);
1154 if (fstat(fd
, &st
)) {
1155 fprintf(stderr
, "ERROR: failed to stat %s - %s\n",
1156 argv
[i
], strerror(errno
));
1157 defrag_global_errors
++;
1158 close_file_or_dir(fd
, dirstream
);
1161 if (!(S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))) {
1163 "ERROR: %s is not a directory or a regular file\n",
1165 defrag_global_errors
++;
1166 close_file_or_dir(fd
, dirstream
);
1170 if (S_ISDIR(st
.st_mode
)) {
1171 ret
= nftw(argv
[i
], defrag_callback
, 10,
1172 FTW_MOUNT
| FTW_PHYS
);
1175 /* errors are handled in the callback */
1178 if (defrag_global_verbose
)
1179 printf("%s\n", argv
[i
]);
1180 ret
= do_defrag(fd
, defrag_global_fancy_ioctl
,
1181 &defrag_global_range
);
1185 if (defrag_global_verbose
)
1186 printf("%s\n", argv
[i
]);
1187 ret
= do_defrag(fd
, defrag_global_fancy_ioctl
,
1188 &defrag_global_range
);
1191 close_file_or_dir(fd
, dirstream
);
1192 if (ret
&& e
== ENOTTY
&& defrag_global_fancy_ioctl
) {
1193 fprintf(stderr
, "ERROR: defrag range ioctl not "
1194 "supported in this kernel, please try "
1195 "without any options.\n");
1196 defrag_global_errors
++;
1200 fprintf(stderr
, "ERROR: defrag failed on %s - %s\n",
1201 argv
[i
], strerror(e
));
1202 defrag_global_errors
++;
1205 if (defrag_global_verbose
)
1206 printf("%s\n", BTRFS_BUILD_VERSION
);
1207 if (defrag_global_errors
)
1208 fprintf(stderr
, "total %d failures\n", defrag_global_errors
);
1210 return !!defrag_global_errors
;
1213 static const char * const cmd_resize_usage
[] = {
1214 "btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
1215 "Resize a filesystem",
1216 "If 'max' is passed, the filesystem will occupy all available space",
1217 "on the device 'devid'.",
1218 "[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
1222 static int cmd_resize(int argc
, char **argv
)
1224 struct btrfs_ioctl_vol_args args
;
1225 int fd
, res
, len
, e
;
1226 char *amount
, *path
;
1227 DIR *dirstream
= NULL
;
1229 if (check_argc_exact(argc
, 3))
1230 usage(cmd_resize_usage
);
1235 len
= strlen(amount
);
1236 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
1237 fprintf(stderr
, "ERROR: size value too long ('%s)\n",
1242 fd
= open_file_or_dir(path
, &dirstream
);
1244 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
1248 printf("Resize '%s' of '%s'\n", path
, amount
);
1249 strncpy_null(args
.name
, amount
);
1250 res
= ioctl(fd
, BTRFS_IOC_RESIZE
, &args
);
1252 close_file_or_dir(fd
, dirstream
);
1254 fprintf(stderr
, "ERROR: unable to resize '%s' - %s\n",
1261 static const char * const cmd_label_usage
[] = {
1262 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
1263 "Get or change the label of a filesystem",
1264 "With one argument, get the label of filesystem on <device>.",
1265 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
1269 static int cmd_label(int argc
, char **argv
)
1271 if (check_argc_min(argc
, 2) || check_argc_max(argc
, 3))
1272 usage(cmd_label_usage
);
1275 return set_label(argv
[1], argv
[2]);
1277 char label
[BTRFS_LABEL_SIZE
];
1280 ret
= get_label(argv
[1], label
);
1282 fprintf(stdout
, "%s\n", label
);
1288 const struct cmd_group filesystem_cmd_group
= {
1289 filesystem_cmd_group_usage
, NULL
, {
1290 { "df", cmd_filesystem_df
, cmd_filesystem_df_usage
, NULL
, 0 },
1291 { "show", cmd_show
, cmd_show_usage
, NULL
, 0 },
1292 { "sync", cmd_sync
, cmd_sync_usage
, NULL
, 0 },
1293 { "defragment", cmd_defrag
, cmd_defrag_usage
, NULL
, 0 },
1294 { "balance", cmd_balance
, NULL
, &balance_cmd_group
, 1 },
1295 { "resize", cmd_resize
, cmd_resize_usage
, NULL
, 0 },
1296 { "label", cmd_label
, cmd_label_usage
, NULL
, 0 },
1297 { "usage", cmd_filesystem_usage
,
1298 cmd_filesystem_usage_usage
, NULL
, 0 },
1304 int cmd_filesystem(int argc
, char **argv
)
1306 return handle_command_group(&filesystem_cmd_group
, argc
, argv
);