diff mbox

[2/2,v3] btrfs: obtain used_bytes in BTRFS_IOC_FS_INFO ioctl

Message ID 1371799948-14176-1-git-send-email-anand.jain@oracle.com (mailing list archive)
State New, archived
Headers show

Commit Message

Anand Jain June 21, 2013, 7:32 a.m. UTC
btrfs-progs has to read fs info from the kernel to
read the latest info instead of reading it from the disks,
which generally is a stale info after certain critical
operation.

getting used_bytes parameter will help to fix
	btrfs filesystem show --kernel
to show the current info of the fs

v2->v3:
	everything is changed dropped the plan to introduce
	the new ioctl, as of now filesystem show could 
	manage if we add the used_bytes parameter to the
	BTRFS_IOC_FS_INFO and
	Update the title from
	 add framework to read fs info and dev info from the kernel

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/ioctl.c           | 16 ++++++++++++++--
 include/uapi/linux/btrfs.h |  3 ++-
 2 files changed, 16 insertions(+), 3 deletions(-)

Comments

Josef Bacik June 24, 2013, 5:03 p.m. UTC | #1
On Fri, Jun 21, 2013 at 03:32:28PM +0800, Anand Jain wrote:
> btrfs-progs has to read fs info from the kernel to
> read the latest info instead of reading it from the disks,
> which generally is a stale info after certain critical
> operation.
> 
> getting used_bytes parameter will help to fix
> 	btrfs filesystem show --kernel
> to show the current info of the fs
> 
> v2->v3:
> 	everything is changed dropped the plan to introduce
> 	the new ioctl, as of now filesystem show could 
> 	manage if we add the used_bytes parameter to the
> 	BTRFS_IOC_FS_INFO and
> 	Update the title from
> 	 add framework to read fs info and dev info from the kernel
> 
> Signed-off-by: Anand Jain <anand.jain@oracle.com>
> ---
>  fs/btrfs/ioctl.c           | 16 ++++++++++++++--
>  include/uapi/linux/btrfs.h |  3 ++-
>  2 files changed, 16 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
> index 0e7bcc5..082c9fc 100644
> --- a/fs/btrfs/ioctl.c
> +++ b/fs/btrfs/ioctl.c
> @@ -2384,7 +2384,10 @@ static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
>  	struct btrfs_ioctl_fs_info_args *fi_args;
>  	struct btrfs_device *device;
>  	struct btrfs_device *next;
> -	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
> +	struct btrfs_fs_info *info = root->fs_info;
> +	struct btrfs_fs_devices *fs_devices = info->fs_devices;
> +	struct btrfs_space_info *si;
> +	u64 used_bytes = 0;
>  	int ret = 0;
>  
>  	if (!capable(CAP_SYS_ADMIN))
> @@ -2394,8 +2397,17 @@ static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
>  	if (!fi_args)
>  		return -ENOMEM;
>  
> +	rcu_read_lock();
> +	list_for_each_entry_rcu(si, &info->space_info, list) {
> +		used_bytes += si->bytes_used + si->bytes_reserved
> +				+ si->bytes_pinned + si->bytes_readonly
> +				+ si->bytes_may_use;
> +	}
> +	rcu_read_unlock();
> +	fi_args->used_bytes = used_bytes;
> +

So what exactly are we using this number for?  ->bytes_may_used is going to
change all the damned time because of reservations and it could even make
used_bytes to appear to be larger than total_bytes because of overcommitting.
So why exactly do you want this value?  Also how are you going to deal with
older kernels where this isn't populated?  Thanks,

Josef
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Anand Jain June 25, 2013, 3 a.m. UTC | #2
On 06/25/2013 01:03 AM, Josef Bacik wrote:
> On Fri, Jun 21, 2013 at 03:32:28PM +0800, Anand Jain wrote:
>> btrfs-progs has to read fs info from the kernel to
>> read the latest info instead of reading it from the disks,
>> which generally is a stale info after certain critical
>> operation.
>>
>> getting used_bytes parameter will help to fix
>> 	btrfs filesystem show --kernel
>> to show the current info of the fs
>>
>> v2->v3:
>> 	everything is changed dropped the plan to introduce
>> 	the new ioctl, as of now filesystem show could
>> 	manage if we add the used_bytes parameter to the
>> 	BTRFS_IOC_FS_INFO and
>> 	Update the title from
>> 	 add framework to read fs info and dev info from the kernel
>>
>> Signed-off-by: Anand Jain <anand.jain@oracle.com>
>> ---
>>   fs/btrfs/ioctl.c           | 16 ++++++++++++++--
>>   include/uapi/linux/btrfs.h |  3 ++-
>>   2 files changed, 16 insertions(+), 3 deletions(-)
>>
>> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
>> index 0e7bcc5..082c9fc 100644
>> --- a/fs/btrfs/ioctl.c
>> +++ b/fs/btrfs/ioctl.c
>> @@ -2384,7 +2384,10 @@ static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
>>   	struct btrfs_ioctl_fs_info_args *fi_args;
>>   	struct btrfs_device *device;
>>   	struct btrfs_device *next;
>> -	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
>> +	struct btrfs_fs_info *info = root->fs_info;
>> +	struct btrfs_fs_devices *fs_devices = info->fs_devices;
>> +	struct btrfs_space_info *si;
>> +	u64 used_bytes = 0;
>>   	int ret = 0;
>>
>>   	if (!capable(CAP_SYS_ADMIN))
>> @@ -2394,8 +2397,17 @@ static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
>>   	if (!fi_args)
>>   		return -ENOMEM;
>>
>> +	rcu_read_lock();
>> +	list_for_each_entry_rcu(si, &info->space_info, list) {
>> +		used_bytes += si->bytes_used + si->bytes_reserved
>> +				+ si->bytes_pinned + si->bytes_readonly
>> +				+ si->bytes_may_use;
>> +	}
>> +	rcu_read_unlock();
>> +	fi_args->used_bytes = used_bytes;
>> +
>
> So what exactly are we using this number for?  ->bytes_may_used is going to
> change all the damned time because of reservations and it could even make
> used_bytes to appear to be larger than total_bytes because of overcommitting.
 > So why exactly do you want this value?

  used for and to be consistent with what btrfs fi show does as of now,
  it shows the number of bytes used in the FS.

  just figured out that I could also use BTRFS_IOC_SPACE_INFO
  and do that math outside kernel. So pls. drop this kernel patch.
  I will be sending out a new progs patch which doesn't need kernel
  ioctl changes.

> Also how are you going to deal with
> older kernels where this isn't populated?  Thanks,

  Since the ioctl arg size didn't change here (as I was using the
  reserved space) it will just show 0.0 when old kernel is used.
  Not a best way (as it lets user to wonder) but a good trade-off
  to make the fix as simple as possible.

Thanks, Anand
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 0e7bcc5..082c9fc 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2384,7 +2384,10 @@  static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
 	struct btrfs_ioctl_fs_info_args *fi_args;
 	struct btrfs_device *device;
 	struct btrfs_device *next;
-	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
+	struct btrfs_fs_info *info = root->fs_info;
+	struct btrfs_fs_devices *fs_devices = info->fs_devices;
+	struct btrfs_space_info *si;
+	u64 used_bytes = 0;
 	int ret = 0;
 
 	if (!capable(CAP_SYS_ADMIN))
@@ -2394,8 +2397,17 @@  static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
 	if (!fi_args)
 		return -ENOMEM;
 
+	rcu_read_lock();
+	list_for_each_entry_rcu(si, &info->space_info, list) {
+		used_bytes += si->bytes_used + si->bytes_reserved
+				+ si->bytes_pinned + si->bytes_readonly
+				+ si->bytes_may_use;
+	}
+	rcu_read_unlock();
+	fi_args->used_bytes = used_bytes;
+
 	fi_args->num_devices = fs_devices->num_devices;
-	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
+	memcpy(&fi_args->fsid, info->fsid, sizeof(fi_args->fsid));
 
 	mutex_lock(&fs_devices->device_list_mutex);
 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h
index de3ec34..44bac0c 100644
--- a/include/uapi/linux/btrfs.h
+++ b/include/uapi/linux/btrfs.h
@@ -181,7 +181,8 @@  struct btrfs_ioctl_fs_info_args {
 	__u64 max_id;				/* out */
 	__u64 num_devices;			/* out */
 	__u8 fsid[BTRFS_FSID_SIZE];		/* out */
-	__u64 reserved[124];			/* pad to 1k */
+	__u64 used_bytes;                       /* out */
+	__u64 reserved[123];			/* pad to 1k */
 };
 
 /* balance control ioctl modes */