Xfsprogs: add fiemap command to xfs_io V2
PATCH 1/1 Xfsprogs: add fiemap command to xfs_io V2
When trying to add a test for hole punching I noticed that the "bmap" command only works on XFS, which makes testing hole punching on other fs's kind of difficult. To fix this I've added an fiemap command that works almost exactly like bmap. It is formatted similarly and takes similar flags, the only thing thats different is obviously it doesn't spit out AG info and it doesn't make finding prealloc space optional. This is my first foray into all of this stuff so a good hard look would be appreciated. I tested it with a few different files to make sure bmap and fiemap both looked the same. Thanks,
---V1->V2: add checks to make sure the system supports fiemap so xfsprogs builds on things other than linux :).
configure.in | 1 + include/builddefs.in | 1 + io/Makefile | 7 + io/fiemap.c | 342 +++++++++++++++++++++++++++++++++++++++++++++++++ io/init.c | 1 + io/io.h | 6 + m4/package_libcdev.m4 | 8 + 7 files changed, 366 insertions(+), 0 deletions(-) create mode 100644 io/fiemap.c
configure.in +1 -0
@@ -103,6 +103,7 @@ AC_HAVE_SENDFILE AC_HAVE_GETMNTENT AC_HAVE_GETMNTINFO AC_HAVE_FALLOCATE +AC_HAVE_FIEMAP AC_HAVE_BLKID_TOPO($enable_blkid) AC_TYPE_PSINT
include/builddefs.in +1 -0
@@ -98,6 +98,7 @@ HAVE_SENDFILE = @have_sendfile@ HAVE_GETMNTENT = @have_getmntent@ HAVE_GETMNTINFO = @have_getmntinfo@ HAVE_FALLOCATE = @have_fallocate@ +HAVE_FIEMAP = @have_fiemap@ GCCFLAGS = -funsigned-char -fno-strict-aliasing -Wall # -Wbitwise -Wno-transparent-union -Wno-old-initializer -Wno-decl
io/Makefile +7 -0
@@ -44,6 +44,13 @@ else LSRCFILES += sendfile.c endif +ifeq ($(HAVE_FIEMAP),yes) +CFILES += fiemap.c +LCFLAGS += -DHAVE_FIEMAP +else +LSRCFILES += fiemap.c +endif + ifeq ($(PKG_PLATFORM),irix) LSRCFILES += inject.c resblks.c else
io/fiemap.c new file +342 -0
342 lines not on this page · raw message
io/init.c +1 -0
@@ -71,6 +71,7 @@ init_commands(void)parent_init ();pread_init ();prealloc_init (); +fiemap_init ();pwrite_init ();quit_init ();resblks_init ();
io/io.h +6 -0
@@ -135,3 +135,9 @@ extern void mincore_init(void);#else #define mincore_init () do { } while (0)#endif + +#ifdef HAVE_FIEMAP +extern void fiemap_init (void ); +#else +#define fiemap_init () do { } while (0) +#endif
m4/package_libcdev.m4 +8 -0
@@ -116,3 +116,11 @@ AC_DEFUN([AC_HAVE_FALLOCATE], AC_MSG_RESULT(no)) AC_SUBST(have_fallocate) ]) + +# +# Check if we have the fiemap ioctl (Linux) +# +AC_DEFUN([AC_HAVE_FIEMAP], + [ AC_CHECK_HEADERS([linux/fiemap.h], [ have_fiemap=yes ], [ have_fiemap=no ]) + AC_SUBST(have_fiemap) + ])
Signature · 6 lines
1.6.6.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 13 lines · Josef Bacik
When trying to add a test for hole punching I noticed that the "bmap" command only works on XFS, which makes testing hole punching on other fs's kind of difficult. To fix this I've added an fiemap command that works almost exactly like bmap. It is formatted similarly and takes similar flags, the only thing thats different is obviously it doesn't spit out AG info and it doesn't make finding prealloc space optional. This is my first foray into all of this stuff so a good hard look would be appreciated. I tested it with a few different files to make sure bmap and fiemap both looked the same. Thanks,
Signed-off-by Josef Bacik <[email protected]>---
V1->V2: add checks to make sure the system supports fiemap so xfsprogs builds on things other than linux :).
io/fiemap.c line 36
Quoted 9 lines
io/fiemap.c new file +9
-0+static void +fiemap_help (void ) +{ + printf(_( +"\n" +" prints the block mapping for a file's data or attribute forks" +"\n" +" Example:\n" +" 'bmap -vp' - tabular format verbose map, including unwritten extents\n"
'fiemap -vp' ?
Quoted 2 lines
+"\n"
+" bmap prints the map of disk blocks used by the current file.\n"
fiemap prints...
Quoted 5 lines
+" The map lists each extent used by the file, as well as regions in the\n" +" file that do not have any corresponding blocks (holes).\n" +" By default, each line of the listing takes the following form:\n"
+" extent: [startoffset..endoffset]: startblock..endblock\n"
+" Holes are marked by replacing the startblock..endblock with 'hole'.\n"
Quoted 8 lines
+" All the file offsets and disk blocks are in units of 512-byte blocks.\n" +" -a -- prints the attribute fork map instead of the data fork.\n" +" -l -- also displays the length of each extent in 512-byte blocks.\n" +" -n -- query n extents.\n"
+" -v -- Verbose information\n"
+" Note: the bmap for non-regular files can be obtained provided the file\n" +" was opened appropriately (in particular, must be opened read-only).\n" +"\n"));
This last note about non-regular files is not true for fiemap, right?
Quoted 21 lines
io/fiemap.c new file +21
-0+} + +static int +numlen ( +__u64 val, +int base) +{ +__u64 tmp; +int len; + +for (len =0 , tmp = val; tmp >0 ; tmp = tmp/base) + len++; +return (len ==0 ?1 : len); +} + +static void +print_verbose( + struct fiemap_extent *extent, + int blocksize, + int foff_w, + int boff_w,
io/fiemap.c line 82
Quoted 10 lines
io/fiemap.c new file +10
-0+int tot_w, + int flg_w, + int *cur_extent, + __u64 *last_logical) +{ + __u64 lstart; +__u64 len; +__u64 block; +char lbuf[32 ]; +char bbuf[32 ];
I don't think these two buffers are large enough to hold 2 64 bit integers as strings.
Quoted 20 lines
io/fiemap.c new file +20
-0+static void +print_plain ( +struct fiemap_extent *extent, +int lflag, +int blocksize, +int *cur_extent, +__u64 *last_logical) +{ +__u64 lstart; +__u64 block; +__u64 len; + + lstart = extent->fe_logical / blocksize; + len = extent->fe_length / blocksize; + block = extent->fe_physical / blocksize; + +if (lstart != *last_logical) { +printf ("\t%d: [%llu..%llu]: hole" , *cur_extent, + *last_logical, lstart -1LL ); +if (lflag)
io/fiemap.c line 140
Quoted 10 lines
io/fiemap.c new file +10
-0+printf (_ (" %llu blocks\n" ), + lstart - *last_logical); + else + printf("\n"); + (*cur_extent)++; + } + +printf ("\t%d: [%llu..%llu]: %llu..%llu" , *cur_extent, + lstart, lstart + len -1LL , block, + block + len -1 );
Why the "1LL" here and not anywhere else?
Quoted 45 lines
io/fiemap.c new file +45
-0+ +if (lflag) +printf (_ (" %llu blocks\n" ), len); +else +printf ("\n" ); + (*cur_extent)++; + *last_logical = lstart + len; +} + +int +fiemap_f ( +int argc, +char **argv) +{ +struct fiemap *fiemap; +int num_extents =32 ; +int nflag =0 ; +int lflag =0 ; +int vflag =0 ; +int fiemap_flags =FIEMAP_FLAG_SYNC ; +int c; +int i; +int map_size; +int ret; +int cur_extent =0 ; +int foff_w =16 ;/* 16 just for a good minimum range */ +int boff_w =16 ; +int tot_w =5 ;/* 5 since its just one number */ +int flg_w =5 ; +__u64 blocksize =512 ; +__u64 last_logical =0 ; +struct stat st; + +while ((c =getopt (argc, argv,"aln:v" )) !=EOF ) { +switch (c) { +case 'a' : + fiemap_flags |=FIEMAP_FLAG_XATTR ; +break ; +case 'l' : + lflag =1 ; +break ; +case 'n' : + num_extents =atoi (optarg); + nflag =1 ; +break ;
io/fiemap.c line 195
Quoted 10 lines
io/fiemap.c new file +10
-0+case 'v' : + vflag++; +break ; +default : +return command_usage (&fiemap_cmd); + } + } + + map_size =sizeof (struct fiemap ) + + (num_extents *sizeof (struct fiemap_extent ));
Need to validate num_extents before using it.
Quoted 40 lines
io/fiemap.c new file +40
-0+ fiemap =malloc (map_size); +if (!fiemap) { +fprintf (stderr,_ ("%s: malloc of %d bytes failed.\n" ), + progname, map_size); + exitcode =1 ; +return 0 ; + } + +memset (fiemap,0 , map_size); + fiemap->fm_flags = fiemap_flags; + fiemap->fm_length =-1 ; + fiemap->fm_extent_count = nflag ?0 : num_extents; + + ret =ioctl (file->fd,FS_IOC_FIEMAP , (unsigned long )fiemap); +if (ret <0 ) { +fprintf (stderr,"%s: ioctl(FS_IOC_FIEMAP) [\"%s\"]: " +"%s\n" , progname, file->name,strerror (errno)); +free (fiemap); + exitcode =1 ; +return 0 ; + } + +if (!nflag) { +if (fiemap->fm_mapped_extents > num_extents) { + num_extents = fiemap->fm_mapped_extents; + map_size =sizeof (struct fiemap ) + + (num_extents *sizeof (struct fiemap_extent )); + fiemap =realloc (fiemap, map_size); +if (!fiemap) { +fprintf (stderr,_ ("%s: realloc of %d bytes " +"failed.\n" ), progname, + map_size); + exitcode =1 ; +return 0 ; + } + } +memset (fiemap,0 , map_size); + fiemap->fm_flags = fiemap_flags; + fiemap->fm_length =-1 ; + fiemap->fm_extent_count = num_extents;
io/fiemap.c line 245
Quoted 10 lines
io/fiemap.c new file +10
-0+ + ret =ioctl (file->fd,FS_IOC_FIEMAP , (unsigned long )fiemap); +if (ret <0 ) { +fprintf (stderr,"%s: ioctl(FS_IOC_FIEMAP) [\"%s\"]: " +"%s\n" , progname, file->name,strerror (errno)); +free (fiemap); + exitcode =1 ; +return 0 ; + } + }
Hmmm. I'd prefer to see a loop mapping and printing N extents at a time rather than one massive allocation and hoping it fits in memory (think of a file with hundreds of thousands of extents). That's a problem with the existing xfs_bmap code - should we be duplicating that problem here?
Otherwise seems fine.
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 222 lines · Dave Chinner
Quoted 13 lines · Josef Bacik
When trying to add a test for hole punching I noticed that the "bmap" command only works on XFS, which makes testing hole punching on other fs's kind of difficult. To fix this I've added an fiemap command that works almost exactly like bmap. It is formatted similarly and takes similar flags, the only thing thats different is obviously it doesn't spit out AG info and it doesn't make finding prealloc space optional. This is my first foray into all of this stuff so a good hard look would be appreciated. I tested it with a few different files to make sure bmap and fiemap both looked the same. Thanks,
Signed-off-by Josef Bacik <[email protected]>---
V1->V2: add checks to make sure the system supports fiemap so xfsprogs builds on things other than linux :).io/fiemap.c line 36
Quoted 9 lines
io/fiemap.c new file +9
-0+static void +fiemap_help (void ) +{ + printf(_( +"\n" +" prints the block mapping for a file's data or attribute forks" +"\n" +" Example:\n" +" 'bmap -vp' - tabular format verbose map, including unwritten extents\n"'fiemap -vp' ?
Quoted 2 lines
+"\n"
+" bmap prints the map of disk blocks used by the current file.\n"fiemap prints...
Quoted 5 lines
+" The map lists each extent used by the file, as well as regions in the\n" +" file that do not have any corresponding blocks (holes).\n" +" By default, each line of the listing takes the following form:\n"
+" extent: [startoffset..endoffset]: startblock..endblock\n"
+" Holes are marked by replacing the startblock..endblock with 'hole'.\n"Quoted 8 lines
+" All the file offsets and disk blocks are in units of 512-byte blocks.\n" +" -a -- prints the attribute fork map instead of the data fork.\n" +" -l -- also displays the length of each extent in 512-byte blocks.\n" +" -n -- query n extents.\n"
+" -v -- Verbose information\n"
+" Note: the bmap for non-regular files can be obtained provided the file\n" +" was opened appropriately (in particular, must be opened read-only).\n" +"\n"));This last note about non-regular files is not true for fiemap, right?
Quoted 21 lines
io/fiemap.c new file +21
-0+} + +static int +numlen ( +__u64 val, +int base) +{ +__u64 tmp; +int len; + +for (len =0 , tmp = val; tmp >0 ; tmp = tmp/base) + len++; +return (len ==0 ?1 : len); +} + +static void +print_verbose( + struct fiemap_extent *extent, + int blocksize, + int foff_w, + int boff_w,io/fiemap.c line 82
Quoted 10 lines
io/fiemap.c new file +10
-0+int tot_w, + int flg_w, + int *cur_extent, + __u64 *last_logical) +{ + __u64 lstart; +__u64 len; +__u64 block; +char lbuf[32 ]; +char bbuf[32 ];I don't think these two buffers are large enough to hold 2 64 bit integers as strings.
Quoted 20 lines
io/fiemap.c new file +20
-0+static void +print_plain ( +struct fiemap_extent *extent, +int lflag, +int blocksize, +int *cur_extent, +__u64 *last_logical) +{ +__u64 lstart; +__u64 block; +__u64 len; + + lstart = extent->fe_logical / blocksize; + len = extent->fe_length / blocksize; + block = extent->fe_physical / blocksize; + +if (lstart != *last_logical) { +printf ("\t%d: [%llu..%llu]: hole" , *cur_extent, + *last_logical, lstart -1LL ); +if (lflag)io/fiemap.c line 140
Quoted 10 lines
io/fiemap.c new file +10
-0+printf (_ (" %llu blocks\n" ), + lstart - *last_logical); + else + printf("\n"); + (*cur_extent)++; + } + +printf ("\t%d: [%llu..%llu]: %llu..%llu" , *cur_extent, + lstart, lstart + len -1LL , block, + block + len -1 );Why the "1LL" here and not anywhere else?
Quoted 45 lines
io/fiemap.c new file +45
-0+ +if (lflag) +printf (_ (" %llu blocks\n" ), len); +else +printf ("\n" ); + (*cur_extent)++; + *last_logical = lstart + len; +} + +int +fiemap_f ( +int argc, +char **argv) +{ +struct fiemap *fiemap; +int num_extents =32 ; +int nflag =0 ; +int lflag =0 ; +int vflag =0 ; +int fiemap_flags =FIEMAP_FLAG_SYNC ; +int c; +int i; +int map_size; +int ret; +int cur_extent =0 ; +int foff_w =16 ;/* 16 just for a good minimum range */ +int boff_w =16 ; +int tot_w =5 ;/* 5 since its just one number */ +int flg_w =5 ; +__u64 blocksize =512 ; +__u64 last_logical =0 ; +struct stat st; + +while ((c =getopt (argc, argv,"aln:v" )) !=EOF ) { +switch (c) { +case 'a' : + fiemap_flags |=FIEMAP_FLAG_XATTR ; +break ; +case 'l' : + lflag =1 ; +break ; +case 'n' : + num_extents =atoi (optarg); + nflag =1 ; +break ;io/fiemap.c line 195
Quoted 10 lines
io/fiemap.c new file +10
-0+case 'v' : + vflag++; +break ; +default : +return command_usage (&fiemap_cmd); + } + } + + map_size =sizeof (struct fiemap ) + + (num_extents *sizeof (struct fiemap_extent ));Need to validate
num_extentsbefore using it.Quoted 48 lines
io/fiemap.c new file +48
-0+ fiemap =malloc (map_size); +if (!fiemap) { +fprintf (stderr,_ ("%s: malloc of %d bytes failed.\n" ), + progname, map_size); + exitcode =1 ; +return 0 ; + } + +memset (fiemap,0 , map_size); + fiemap->fm_flags = fiemap_flags; + fiemap->fm_length =-1 ; + fiemap->fm_extent_count = nflag ?0 : num_extents; + + ret =ioctl (file->fd,FS_IOC_FIEMAP , (unsigned long )fiemap); +if (ret <0 ) { +fprintf (stderr,"%s: ioctl(FS_IOC_FIEMAP) [\"%s\"]: " +"%s\n" , progname, file->name,strerror (errno)); +free (fiemap); + exitcode =1 ; +return 0 ; + } + +if (!nflag) { +if (fiemap->fm_mapped_extents > num_extents) { + num_extents = fiemap->fm_mapped_extents; + map_size =sizeof (struct fiemap ) + + (num_extents *sizeof (struct fiemap_extent )); + fiemap =realloc (fiemap, map_size); +if (!fiemap) { +fprintf (stderr,_ ("%s: realloc of %d bytes " +"failed.\n" ), progname, + map_size); + exitcode =1 ; +return 0 ; + } + } +memset (fiemap,0 , map_size); + fiemap->fm_flags = fiemap_flags; + fiemap->fm_length =-1 ; + fiemap->fm_extent_count = num_extents; + + ret =ioctl (file->fd,FS_IOC_FIEMAP , (unsigned long )fiemap); +if (ret <0 ) { +fprintf (stderr,"%s: ioctl(FS_IOC_FIEMAP) [\"%s\"]: " +"%s\n" , progname, file->name,strerror (errno)); +free (fiemap); + exitcode =1 ; +return 0 ;
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ } + }Hmmm. I'd prefer to see a loop mapping and printing N extents at a time rather than one massive allocation and hoping it fits in memory (think of a file with hundreds of thousands of extents). That's a problem with the existing
xfs_bmapcode - should we be duplicating that problem here?
Heh you know thats what I did before but decided it was better to do it the way bmap did it. Thanks for the review, I'll fix all these things up,
Josef
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs