Btrfs: compressed file size ioctl
PATCH 1/1 Btrfs: compressed file size ioctl
Returns a file's size on disk. Based on a patch by Chris Ball, improved following suggestions by Chris Mason and Miao Xie.
Minimal example:
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#define BTRFS_IOCTL_MAGIC 0x94
#define BTRFS_IOC_COMPR_SIZE _IOR(BTRFS_IOCTL_MAGIC, 21, uint64_t)
int main(int argc, char **argv)
{
uint64_t size = -1;
int d = open(argv[1], O_RDONLY);
ioctl(d, BTRFS_IOC_COMPR_SIZE, &size);
printf("%zd\n", size);
return 0;
}
--- fs/btrfs/ioctl.c | 41 +++++++++++++++++++++++++++++++++++++++++ fs/btrfs/ioctl.h | 1 + 2 files changed, 42 insertions(+), 0 deletions(-)
fs/btrfs/ioctl.c +41 -0
@@ -1750,6 +1750,45 @@ out_drop_write:return ret; } +static long btrfs_ioctl_compsize (struct file *file,void __user *argp) +{ +/* This ioctl returns the compressed size of an inode on disk +* by counting the on-disk space used by all of its extents. +*/ +struct inode *inode =fdentry (file)->d_inode; +struct extent_map *em; +u64 len = inode->i_size; +u64 compressed_size =0 ; +u64 offset =0 ; + +if (S_ISDIR (inode->i_mode)) +return -EISDIR ; + +mutex_lock (&inode->i_mutex); + +/* do any pending delalloc/csum calc on inode, one way or +another, and lock file content */ +btrfs_wait_ordered_range (inode,0 , (u64)-1 ); + +while (offset < len) { + em =btrfs_get_extent (inode,NULL ,0 , offset,1 ,0 ); +BUG_ON (IS_ERR (em) || !em); +if (em->block_len != (u64)-1 ) + compressed_size += em->block_len; + offset += em->len; +free_extent_map (em); + } +mutex_unlock (&inode->i_mutex); + +unlock_extent (&BTRFS_I (inode)->io_tree,0 , len,GFP_NOFS ); + +/* We've succeeded in going through all extents; set the final size. */ +if (copy_to_user (argp, &compressed_size,sizeof (compressed_size))) +return -EFAULT ; +else +return 0 ; +} +static long btrfs_ioctl_clone_range (struct file *file,void __user *argp) {struct btrfs_ioctl_clone_range_args args;
@@ -2034,6 +2073,8 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_SYNC :btrfs_sync_fs (file->f_dentry->d_sb,1 );return 0 ; +case BTRFS_IOC_COMPR_SIZE : +return btrfs_ioctl_compsize (file, argp); }return -ENOTTY ;
fs/btrfs/ioctl.h +1 -0
@@ -178,4 +178,5 @@ struct btrfs_ioctl_space_args {#define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)#define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \ struct btrfs_ioctl_space_args) +#define BTRFS_IOC_COMPR_SIZE _IOR(BTRFS_IOCTL_MAGIC, 21, u64)#endif
Signature · 1 line
1.7.1
Quoted 61 lines · Ulrich Hecht
Returns a file's size on disk. Based on a patch by Chris Ball, improved following suggestions by Chris Mason and Miao Xie.
Minimal example:
#include <
sys/ioctl.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#defineBTRFS_IOCTL_MAGIC0x94
#defineBTRFS_IOC_COMPR_SIZE_IOR(BTRFS_IOCTL_MAGIC, 21, uint64_t)
intmain(int argc, char **argv)
{
uint64_tsize = -1;
int d =open(argv[1], O_RDONLY);
ioctl(d, BTRFS_IOC_COMPR_SIZE, &size);
printf("%zd\n", size);
return 0;
}Signed-off-by Ulrich Hecht <[email protected]>--- fs/btrfs/ioctl.c | 41 +++++++++++++++++++++++++++++++++++++++++ fs/btrfs/ioctl.h | 1 + 2 files changed, 42 insertions(+), 0 deletions(-)fs/btrfs/ioctl.c +27
-0@@ -1750,6 +1750,45 @@ out_drop_write:return ret; } +static long btrfs_ioctl_compsize (struct file *file,void __user *argp) +{ +/* This ioctl returns the compressed size of an inode on disk +* by counting the on-disk space used by all of its extents. +*/ +struct inode *inode =fdentry (file)->d_inode; +struct extent_map *em; +u64 len = inode->i_size; +u64 compressed_size =0 ; +u64 offset =0 ; + +if (S_ISDIR (inode->i_mode)) +return -EISDIR ; + +mutex_lock (&inode->i_mutex); + +/* do any pending delalloc/csum calc on inode, one way or +another, and lock file content */ +btrfs_wait_ordered_range (inode,0 , (u64)-1 ); + +while (offset < len) { + em =btrfs_get_extent (inode,NULL ,0 , offset,1 ,0 ); +BUG_ON (IS_ERR (em) || !em); +if (em->block_len != (u64)-1 ) + compressed_size += em->block_len; + offset += em->len; +free_extent_map (em);
fs/btrfs/ioctl.c out_drop_write: · line 1789
Quoted 10 lines
fs/btrfs/ioctl.c +9
-0+ } +mutex_unlock (&inode->i_mutex); + +unlock_extent (&BTRFS_I (inode)->io_tree,0 , len,GFP_NOFS ); + +/* We've succeeded in going through all extents; set the final size. */ +if (copy_to_user (argp, &compressed_size,sizeof (compressed_size))) +return -EFAULT ; +else
BTW, this "else" can be dropped. :-)
Thanks,
liubo
Quoted 26 lines
fs/btrfs/ioctl.c +3
-0+return 0 ; +} +static long btrfs_ioctl_clone_range (struct file *file,void __user *argp) {fs/btrfs/ioctl.c +2
-0struct btrfs_ioctl_clone_range_args args;@@ -2034,6 +2073,8 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_SYNC :btrfs_sync_fs (file->f_dentry->d_sb,1 );return 0 ; +case BTRFS_IOC_COMPR_SIZE : +return btrfs_ioctl_compsize (file, argp); }return -ENOTTY ;fs/btrfs/ioctl.h +1
-0@@ -178,4 +178,5 @@ struct btrfs_ioctl_space_args {#define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)#define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \ struct btrfs_ioctl_space_args) +#define BTRFS_IOC_COMPR_SIZE _IOR(BTRFS_IOCTL_MAGIC, 21, u64)#endif
Quoted 12 lines · Ulrich Hecht
fs/btrfs/ioctl.c +12
-0+static long btrfs_ioctl_compsize (struct file *file,void __user *argp) +{ +/* This ioctl returns the compressed size of an inode on disk +* by counting the on-disk space used by all of its extents. +*/ +struct inode *inode =fdentry (file)->d_inode; +struct extent_map *em; +u64 len = inode->i_size; +u64 compressed_size =0 ; +u64 offset =0 ; + +if (S_ISDIR (inode->i_mode))
fs/btrfs/ioctl.c out_drop_write: · line 1774
Quoted 10 lines
fs/btrfs/ioctl.c +10
-0+return -EISDIR ; + +mutex_lock (&inode->i_mutex); + +/* do any pending delalloc/csum calc on inode, one way or +another, and lock file content */ +btrfs_wait_ordered_range (inode,0 , (u64)-1 ); + +while (offset< len) { + em =btrfs_get_extent (inode,NULL ,0 , offset,1 ,0 );
I think you need use lock_extent()/unlock_extent() to enclose btrfs_get_extent(), but I didn't find lock_extent().
Thanks
Miao
Quoted 39 lines
fs/btrfs/ioctl.c +19
-0+BUG_ON (IS_ERR (em) || !em); +if (em->block_len != (u64)-1 ) + compressed_size += em->block_len; + offset += em->len; +free_extent_map (em); + } +mutex_unlock (&inode->i_mutex); + +unlock_extent (&BTRFS_I (inode)->io_tree,0 , len,GFP_NOFS ); + +/* We've succeeded in going through all extents; set the final size. */ +if (copy_to_user (argp,&compressed_size,sizeof (compressed_size))) +return -EFAULT ; +else +return 0 ; +} +static long btrfs_ioctl_clone_range (struct file *file,void __user *argp) {struct btrfs_ioctl_clone_range_args args;@@ -2034,6 +2073,8 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_SYNC :btrfs_sync_fs (file->f_dentry->d_sb,1 );return 0 ; +case BTRFS_IOC_COMPR_SIZE : +return btrfs_ioctl_compsize (file, argp); }return -ENOTTY ;fs/btrfs/ioctl.h +1
-0@@ -178,4 +178,5 @@ struct btrfs_ioctl_space_args {#define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)#define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \ struct btrfs_ioctl_space_args) +#define BTRFS_IOC_COMPR_SIZE _IOR(BTRFS_IOCTL_MAGIC, 21, u64)#endif
Quoted 2 lines · Miao Xie
I think you need use lock_extent()/unlock_extent() to enclose
btrfs_get_extent(), but I didn't findlock_extent().
Yeah, I think that was missing in the original patch as well. Hope the=20 latest iteration is OK.
CU
Uli
--=20
SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG N=A8=B9rnberg) --
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html