@@ -5880,7 +5880,7 @@ xfs_getbmap(
58805880 void * arg ) /* formatter arg */
58815881{
58825882 __int64_t bmvend ; /* last block requested */
5883- int error ; /* return value */
5883+ int error = 0 ; /* return value */
58845884 __int64_t fixlen ; /* length for -1 case */
58855885 int i ; /* extent number */
58865886 int lock ; /* lock state */
@@ -5890,39 +5890,18 @@ xfs_getbmap(
58905890 int nexleft ; /* # of user extents left */
58915891 int subnex ; /* # of bmapi's can do */
58925892 int nmap ; /* number of map entries */
5893- struct getbmapx out ; /* output structure */
5893+ struct getbmapx * out ; /* output structure */
58945894 int whichfork ; /* data or attr fork */
58955895 int prealloced ; /* this is a file with
58965896 * preallocated data space */
58975897 int iflags ; /* interface flags */
58985898 int bmapi_flags ; /* flags for xfs_bmapi */
5899+ int cur_ext = 0 ;
58995900
59005901 mp = ip -> i_mount ;
59015902 iflags = bmv -> bmv_iflags ;
5902-
59035903 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK ;
59045904
5905- /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5906- * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5907- * bit is set for the file, generate a read event in order
5908- * that the DMAPI application may do its thing before we return
5909- * the extents. Usually this means restoring user file data to
5910- * regions of the file that look like holes.
5911- *
5912- * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5913- * BMV_IF_NO_DMAPI_READ so that read events are generated.
5914- * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5915- * could misinterpret holes in a DMAPI file as true holes,
5916- * when in fact they may represent offline user data.
5917- */
5918- if ((iflags & BMV_IF_NO_DMAPI_READ ) == 0 &&
5919- DM_EVENT_ENABLED (ip , DM_EVENT_READ ) &&
5920- whichfork == XFS_DATA_FORK ) {
5921- error = XFS_SEND_DATA (mp , DM_EVENT_READ , ip , 0 , 0 , 0 , NULL );
5922- if (error )
5923- return XFS_ERROR (error );
5924- }
5925-
59265905 if (whichfork == XFS_ATTR_FORK ) {
59275906 if (XFS_IFORK_Q (ip )) {
59285907 if (ip -> i_d .di_aformat != XFS_DINODE_FMT_EXTENTS &&
@@ -5936,11 +5915,37 @@ xfs_getbmap(
59365915 ip -> i_mount );
59375916 return XFS_ERROR (EFSCORRUPTED );
59385917 }
5939- } else if (ip -> i_d .di_format != XFS_DINODE_FMT_EXTENTS &&
5940- ip -> i_d .di_format != XFS_DINODE_FMT_BTREE &&
5941- ip -> i_d .di_format != XFS_DINODE_FMT_LOCAL )
5942- return XFS_ERROR (EINVAL );
5943- if (whichfork == XFS_DATA_FORK ) {
5918+
5919+ prealloced = 0 ;
5920+ fixlen = 1LL << 32 ;
5921+ } else {
5922+ /*
5923+ * If the BMV_IF_NO_DMAPI_READ interface bit specified, do
5924+ * not generate a DMAPI read event. Otherwise, if the
5925+ * DM_EVENT_READ bit is set for the file, generate a read
5926+ * event in order that the DMAPI application may do its thing
5927+ * before we return the extents. Usually this means restoring
5928+ * user file data to regions of the file that look like holes.
5929+ *
5930+ * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5931+ * BMV_IF_NO_DMAPI_READ so that read events are generated.
5932+ * If this were not true, callers of ioctl(XFS_IOC_GETBMAP)
5933+ * could misinterpret holes in a DMAPI file as true holes,
5934+ * when in fact they may represent offline user data.
5935+ */
5936+ if (DM_EVENT_ENABLED (ip , DM_EVENT_READ ) &&
5937+ !(iflags & BMV_IF_NO_DMAPI_READ )) {
5938+ error = XFS_SEND_DATA (mp , DM_EVENT_READ , ip ,
5939+ 0 , 0 , 0 , NULL );
5940+ if (error )
5941+ return XFS_ERROR (error );
5942+ }
5943+
5944+ if (ip -> i_d .di_format != XFS_DINODE_FMT_EXTENTS &&
5945+ ip -> i_d .di_format != XFS_DINODE_FMT_BTREE &&
5946+ ip -> i_d .di_format != XFS_DINODE_FMT_LOCAL )
5947+ return XFS_ERROR (EINVAL );
5948+
59445949 if (xfs_get_extsz_hint (ip ) ||
59455950 ip -> i_d .di_flags & (XFS_DIFLAG_PREALLOC |XFS_DIFLAG_APPEND )){
59465951 prealloced = 1 ;
@@ -5949,42 +5954,41 @@ xfs_getbmap(
59495954 prealloced = 0 ;
59505955 fixlen = ip -> i_size ;
59515956 }
5952- } else {
5953- prealloced = 0 ;
5954- fixlen = 1LL << 32 ;
59555957 }
59565958
59575959 if (bmv -> bmv_length == -1 ) {
59585960 fixlen = XFS_FSB_TO_BB (mp , XFS_B_TO_FSB (mp , fixlen ));
5959- bmv -> bmv_length = MAX ( (__int64_t )(fixlen - bmv -> bmv_offset ),
5960- (__int64_t )0 );
5961- } else if (bmv -> bmv_length < 0 )
5962- return XFS_ERROR (EINVAL );
5963- if (bmv -> bmv_length == 0 ) {
5961+ bmv -> bmv_length =
5962+ max_t (__int64_t , fixlen - bmv -> bmv_offset , 0 );
5963+ } else if (bmv -> bmv_length == 0 ) {
59645964 bmv -> bmv_entries = 0 ;
59655965 return 0 ;
5966+ } else if (bmv -> bmv_length < 0 ) {
5967+ return XFS_ERROR (EINVAL );
59665968 }
5969+
59675970 nex = bmv -> bmv_count - 1 ;
59685971 if (nex <= 0 )
59695972 return XFS_ERROR (EINVAL );
59705973 bmvend = bmv -> bmv_offset + bmv -> bmv_length ;
59715974
5972- xfs_ilock (ip , XFS_IOLOCK_SHARED );
59735975
5974- if (((iflags & BMV_IF_DELALLOC ) == 0 ) &&
5975- (whichfork == XFS_DATA_FORK ) &&
5976- (ip -> i_delayed_blks || ip -> i_size > ip -> i_d .di_size )) {
5977- /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5978- error = xfs_flush_pages (ip , (xfs_off_t )0 ,
5979- -1 , 0 , FI_REMAPF );
5980- if (error ) {
5981- xfs_iunlock (ip , XFS_IOLOCK_SHARED );
5982- return error ;
5976+ if (bmv -> bmv_count > ULONG_MAX / sizeof (struct getbmapx ))
5977+ return XFS_ERROR (ENOMEM );
5978+ out = kmem_zalloc (bmv -> bmv_count * sizeof (struct getbmapx ), KM_MAYFAIL );
5979+ if (!out )
5980+ return XFS_ERROR (ENOMEM );
5981+
5982+ xfs_ilock (ip , XFS_IOLOCK_SHARED );
5983+ if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC )) {
5984+ if (ip -> i_delayed_blks || ip -> i_size > ip -> i_d .di_size ) {
5985+ error = xfs_flush_pages (ip , 0 , -1 , 0 , FI_REMAPF );
5986+ if (error )
5987+ goto out_unlock_iolock ;
59835988 }
5984- }
59855989
5986- ASSERT (whichfork == XFS_ATTR_FORK || ( iflags & BMV_IF_DELALLOC ) ||
5987- ip -> i_delayed_blks == 0 );
5990+ ASSERT (ip -> i_delayed_blks == 0 );
5991+ }
59885992
59895993 lock = xfs_ilock_map_shared (ip );
59905994
@@ -5995,23 +5999,25 @@ xfs_getbmap(
59955999 if (nex > XFS_IFORK_NEXTENTS (ip , whichfork ) * 2 + 1 )
59966000 nex = XFS_IFORK_NEXTENTS (ip , whichfork ) * 2 + 1 ;
59976001
5998- bmapi_flags = xfs_bmapi_aflag (whichfork ) |
5999- ((iflags & BMV_IF_PREALLOC ) ? 0 : XFS_BMAPI_IGSTATE );
6002+ bmapi_flags = xfs_bmapi_aflag (whichfork );
6003+ if (!(iflags & BMV_IF_PREALLOC ))
6004+ bmapi_flags |= XFS_BMAPI_IGSTATE ;
60006005
60016006 /*
60026007 * Allocate enough space to handle "subnex" maps at a time.
60036008 */
6009+ error = ENOMEM ;
60046010 subnex = 16 ;
6005- map = kmem_alloc (subnex * sizeof (* map ), KM_SLEEP );
6011+ map = kmem_alloc (subnex * sizeof (* map ), KM_MAYFAIL );
6012+ if (!map )
6013+ goto out_unlock_ilock ;
60066014
60076015 bmv -> bmv_entries = 0 ;
60086016
6009- if ((XFS_IFORK_NEXTENTS (ip , whichfork ) == 0 )) {
6010- if (((iflags & BMV_IF_DELALLOC ) == 0 ) ||
6011- whichfork == XFS_ATTR_FORK ) {
6012- error = 0 ;
6013- goto unlock_and_return ;
6014- }
6017+ if (XFS_IFORK_NEXTENTS (ip , whichfork ) == 0 &&
6018+ (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC ))) {
6019+ error = 0 ;
6020+ goto out_free_map ;
60156021 }
60166022
60176023 nexleft = nex ;
@@ -6023,53 +6029,61 @@ xfs_getbmap(
60236029 bmapi_flags , NULL , 0 , map , & nmap ,
60246030 NULL , NULL );
60256031 if (error )
6026- goto unlock_and_return ;
6032+ goto out_free_map ;
60276033 ASSERT (nmap <= subnex );
60286034
60296035 for (i = 0 ; i < nmap && nexleft && bmv -> bmv_length ; i ++ ) {
6030- out .bmv_oflags = 0 ;
6036+ out [ cur_ext ] .bmv_oflags = 0 ;
60316037 if (map [i ].br_state == XFS_EXT_UNWRITTEN )
6032- out .bmv_oflags |= BMV_OF_PREALLOC ;
6038+ out [ cur_ext ] .bmv_oflags |= BMV_OF_PREALLOC ;
60336039 else if (map [i ].br_startblock == DELAYSTARTBLOCK )
6034- out .bmv_oflags |= BMV_OF_DELALLOC ;
6035- out .bmv_offset = XFS_FSB_TO_BB (mp , map [i ].br_startoff );
6036- out .bmv_length = XFS_FSB_TO_BB (mp , map [i ].br_blockcount );
6037- out .bmv_unused1 = out .bmv_unused2 = 0 ;
6040+ out [cur_ext ].bmv_oflags |= BMV_OF_DELALLOC ;
6041+ out [cur_ext ].bmv_offset =
6042+ XFS_FSB_TO_BB (mp , map [i ].br_startoff );
6043+ out [cur_ext ].bmv_length =
6044+ XFS_FSB_TO_BB (mp , map [i ].br_blockcount );
6045+ out [cur_ext ].bmv_unused1 = 0 ;
6046+ out [cur_ext ].bmv_unused2 = 0 ;
60386047 ASSERT (((iflags & BMV_IF_DELALLOC ) != 0 ) ||
60396048 (map [i ].br_startblock != DELAYSTARTBLOCK ));
60406049 if (map [i ].br_startblock == HOLESTARTBLOCK &&
60416050 whichfork == XFS_ATTR_FORK ) {
60426051 /* came to the end of attribute fork */
6043- out .bmv_oflags |= BMV_OF_LAST ;
6044- goto unlock_and_return ;
6045- } else {
6046- int full = 0 ; /* user array is full */
6047-
6048- if (!xfs_getbmapx_fix_eof_hole (ip , & out ,
6049- prealloced , bmvend ,
6050- map [i ].br_startblock )) {
6051- goto unlock_and_return ;
6052- }
6053-
6054- /* format results & advance arg */
6055- error = formatter (& arg , & out , & full );
6056- if (error || full )
6057- goto unlock_and_return ;
6058- nexleft -- ;
6059- bmv -> bmv_offset =
6060- out .bmv_offset + out .bmv_length ;
6061- bmv -> bmv_length = MAX ((__int64_t )0 ,
6062- (__int64_t )(bmvend - bmv -> bmv_offset ));
6063- bmv -> bmv_entries ++ ;
6052+ out [cur_ext ].bmv_oflags |= BMV_OF_LAST ;
6053+ goto out_free_map ;
60646054 }
6055+
6056+ if (!xfs_getbmapx_fix_eof_hole (ip , & out [cur_ext ],
6057+ prealloced , bmvend ,
6058+ map [i ].br_startblock ))
6059+ goto out_free_map ;
6060+
6061+ nexleft -- ;
6062+ bmv -> bmv_offset =
6063+ out [cur_ext ].bmv_offset +
6064+ out [cur_ext ].bmv_length ;
6065+ bmv -> bmv_length =
6066+ max_t (__int64_t , 0 , bmvend - bmv -> bmv_offset );
6067+ bmv -> bmv_entries ++ ;
6068+ cur_ext ++ ;
60656069 }
60666070 } while (nmap && nexleft && bmv -> bmv_length );
60676071
6068- unlock_and_return :
6072+ out_free_map :
6073+ kmem_free (map );
6074+ out_unlock_ilock :
60696075 xfs_iunlock_map_shared (ip , lock );
6076+ out_unlock_iolock :
60706077 xfs_iunlock (ip , XFS_IOLOCK_SHARED );
60716078
6072- kmem_free (map );
6079+ for (i = 0 ; i < cur_ext ; i ++ ) {
6080+ int full = 0 ; /* user array is full */
6081+
6082+ /* format results & advance arg */
6083+ error = formatter (& arg , & out [i ], & full );
6084+ if (error || full )
6085+ break ;
6086+ }
60736087
60746088 return error ;
60756089}
0 commit comments