From patchwork Wed Jan 13 18:21:23 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 72686 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.2) with ESMTP id o0DI72f6000430 for ; Wed, 13 Jan 2010 18:07:02 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753018Ab0AMSHB (ORCPT ); Wed, 13 Jan 2010 13:07:01 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1751917Ab0AMSHB (ORCPT ); Wed, 13 Jan 2010 13:07:01 -0500 Received: from mx1.redhat.com ([209.132.183.28]:30713 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751809Ab0AMSHA (ORCPT ); Wed, 13 Jan 2010 13:07:00 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o0DI6x1L010641 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 13 Jan 2010 13:07:00 -0500 Received: from dhcp231-156.rdu.redhat.com (dhcp231-156.rdu.redhat.com [10.11.231.156]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o0DI6xM0023942; Wed, 13 Jan 2010 13:06:59 -0500 Date: Wed, 13 Jan 2010 13:21:23 -0500 From: Josef Bacik To: Josef Bacik Cc: linux-btrfs@vger.kernel.org Subject: Re: [PATCH] Btrfs-progs: add btrfsctl -i to print space info Message-ID: <20100113182123.GB2774@dhcp231-156.rdu.redhat.com> References: <20100112203157.GB13414@localhost.localdomain> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20100112203157.GB13414@localhost.localdomain> User-Agent: Mutt/1.5.19 (2009-01-05) X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org diff --git a/btrfsctl.c b/btrfsctl.c index 81d2a30..4a516fd 100644 --- a/btrfsctl.c +++ b/btrfsctl.c @@ -61,6 +61,8 @@ static void print_usage(void) printf("\t-l file: listing snapshot/subvolume under a subvolume\n"); printf("\t-m [tree id] directory: set the default mounted subvolume" " to the [tree id] or the directory\n"); + printf("\t-i dir: print out detailed space usage information on the" + " volume\n"); printf("%s\n", BTRFS_BUILD_VERSION); exit(1); } @@ -213,6 +215,88 @@ int btrfs_show_subvolume(struct list_head *list_top, char *path, return 0; } +int btrfs_print_space_info(int fd) +{ + struct btrfs_ioctl_space_args *sargs; + u64 count = 0, i; + int ret; + + 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); + if (ret) { + 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); + if (ret) { + 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) { + 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; +} int btrfs_list_subvolumes(int fd, unsigned long command) { @@ -394,6 +478,8 @@ int main(int ac, char **av) exit(1); } } + } else if (strcmp(av[i], "-i") == 0) { + command = BTRFS_IOC_SPACE_INFO; } } if (command == 0) { @@ -427,6 +513,8 @@ int main(int ac, char **av) } else if (command == BTRFS_IOC_DEFAULT_SUBVOL) { printf("objectid is %llu\n", objectid); ret = ioctl(fd, command, &objectid); + } else if (command == BTRFS_IOC_SPACE_INFO) { + ret = btrfs_print_space_info(fd); } else ret = ioctl(fd, command, &args); if (ret < 0) { diff --git a/ioctl.h b/ioctl.h index 70fc15d..ee369e2 100644 --- a/ioctl.h +++ b/ioctl.h @@ -57,6 +57,18 @@ struct btrfs_ioctl_subvol_leaf { struct btrfs_ioctl_subvol_items items[]; }; +struct btrfs_ioctl_space_info { + u64 flags; + u64 total_bytes; + u64 used_bytes; +}; + +struct btrfs_ioctl_space_args { + u64 space_slots; + u64 total_spaces; + struct btrfs_ioctl_space_info spaces[0]; +}; + #define BTRFS_SUBVOL_LEAF_SIZE_MIN sizeof(struct btrfs_ioctl_subvol_leaf) + \ sizeof(struct btrfs_ioctl_subvol_items) @@ -92,5 +104,6 @@ struct btrfs_ioctl_subvol_leaf { #define BTRFS_IOC_SNAP_LISTING _IOWR(BTRFS_IOCTL_MAGIC, 16, \ struct btrfs_ioctl_subvol_args) #define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 17, u64) - +#define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 18, \ + struct btrfs_ioctl_space_args) #endif