/* * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License v2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. */ #include #include #include #include #include #include #include #include #include "kerncompat.h" #include "ctree.h" #include "ioctl.h" #include "utils.h" #include "volumes.h" #include "version.h" #include "commands.h" #include "btrfslabel.h" static const char filesystem_cmd_group_usage[] = "btrfs filesystem [] []"; static const char * const cmd_df_usage[] = { "btrfs filesystem df ", "Show space usage information for a mount point", NULL }; static int cmd_df(int argc, char **argv) { struct btrfs_ioctl_space_args *sargs; u64 count = 0, i; int ret; int fd; int e; char *path; if (check_argc_exact(argc, 2)) usage(cmd_df_usage); path = argv[1]; fd = open_file_or_dir(path); if (fd < 0) { fprintf(stderr, "ERROR: can't access to '%s'\n", path); return 12; } sargs = malloc(sizeof(struct btrfs_ioctl_space_args)); if (!sargs) return -ENOMEM; sargs->space_slots = 0; sargs->total_spaces = 0; ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs); e = errno; if (ret) { fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n", path, strerror(e)); free(sargs); return ret; } if (!sargs->total_spaces) return 0; count = sargs->total_spaces; sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) + (count * sizeof(struct btrfs_ioctl_space_info))); if (!sargs) return -ENOMEM; sargs->space_slots = count; sargs->total_spaces = 0; ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs); e = errno; if (ret) { fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n", path, strerror(e)); close(fd); free(sargs); return ret; } for (i = 0; i < sargs->total_spaces; i++) { char description[80]; char *total_bytes; char *used_bytes; int written = 0; u64 flags = sargs->spaces[i].flags; memset(description, 0, 80); if (flags & BTRFS_BLOCK_GROUP_DATA) { if (flags & BTRFS_BLOCK_GROUP_METADATA) { snprintf(description, 14, "%s", "Data+Metadata"); written += 13; } else { snprintf(description, 5, "%s", "Data"); written += 4; } } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) { snprintf(description, 7, "%s", "System"); written += 6; } else if (flags & BTRFS_BLOCK_GROUP_METADATA) { snprintf(description, 9, "%s", "Metadata"); written += 8; } if (flags & BTRFS_BLOCK_GROUP_RAID0) { snprintf(description+written, 8, "%s", ", RAID0"); written += 7; } else if (flags & BTRFS_BLOCK_GROUP_RAID1) { snprintf(description+written, 8, "%s", ", RAID1"); written += 7; } else if (flags & BTRFS_BLOCK_GROUP_DUP) { snprintf(description+written, 6, "%s", ", DUP"); written += 5; } else if (flags & BTRFS_BLOCK_GROUP_RAID10) { snprintf(description+written, 9, "%s", ", RAID10"); written += 8; } total_bytes = pretty_sizes(sargs->spaces[i].total_bytes); used_bytes = pretty_sizes(sargs->spaces[i].used_bytes); printf("%s: total=%s, used=%s\n", description, total_bytes, used_bytes); } free(sargs); return 0; } static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search) { struct list_head *cur; struct btrfs_device *device; list_for_each(cur, &fs_devices->devices) { device = list_entry(cur, struct btrfs_device, dev_list); if ((device->label && strcmp(device->label, search) == 0) || strcmp(device->name, search) == 0) return 1; } return 0; } static void print_one_uuid(struct btrfs_fs_devices *fs_devices) { char uuidbuf[37]; struct list_head *cur; struct btrfs_device *device; char *super_bytes_used; u64 devs_found = 0; u64 total; uuid_unparse(fs_devices->fsid, uuidbuf); device = list_entry(fs_devices->devices.next, struct btrfs_device, dev_list); if (device->label && device->label[0]) printf("Label: '%s' ", device->label); else printf("Label: none "); super_bytes_used = pretty_sizes(device->super_bytes_used); total = device->total_devs; printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf, (unsigned long long)total, super_bytes_used); free(super_bytes_used); list_for_each(cur, &fs_devices->devices) { char *total_bytes; char *bytes_used; device = list_entry(cur, struct btrfs_device, dev_list); total_bytes = pretty_sizes(device->total_bytes); bytes_used = pretty_sizes(device->bytes_used); printf("\tdevid %4llu size %s used %s path %s\n", (unsigned long long)device->devid, total_bytes, bytes_used, device->name); free(total_bytes); free(bytes_used); devs_found++; } if (devs_found < total) { printf("\t*** Some devices missing\n"); } printf("\n"); } static const char * const cmd_show_usage[] = { "btrfs filesystem show [--all-devices] [|