xfs: Add support FALLOC_FL_COLLAPSE_RANGE for fallocate
Subsystems (2)
PATCH v5 2/10 xfs: Add support FALLOC_FL_COLLAPSE_RANGE for fallocate
From: Namjae Jeon <[email protected]>
This patch implements fallocate's FALLOC_FL_COLLAPSE_RANGE for XFS.
The semantics of this flag are following:
1) It collapses the range lying between offset and length by removing any data blocks which are present in this range and than updates all the logical offsets of extents beyond "offset + len" to nullify the hole created by removing blocks. In short, it does not leave a hole.
2) It should be used exclusively. No other fallocate flag in combination.
3) Offset and length supplied to fallocate should be fs block size aligned in case of xfs and ext4.
4) Collaspe range does not work beyond i_size.
---
Changelog
v5:
- reduce the number of extents to be shifted under write transactions from 2 to 1
- Simplified a check for the case when shifting first extent of the file, the hole is at the begining not large enough to contain the shift
v4:
- move block size aligned check from VFS layer to FS specific layer.
- update comments for xfs_bmap_shift_extents and change variable name to more reasonable name.
- add ASSERTs for pointers in XFS patch.
- drop all the xfs_bmbt_get*() wrappers.
- change return errno from EFSCORRUPTED to EINVAL,
when hole is not large enough to contain the shift.
- xfs: remove extents from on-disk btree also in case of merge.
v3:
- fix checkpatch.pl warning
v2:
- Added check to make sure before shifting that the hole is big enough to contian the shift.
- Added code to make merging of extent where possible.
- Under write transaction we are only allowed for 2 bmbt updated. Reduce the number of extents to be shifted in 1 transaction from 3 to 2.
- Change semantics of collapse range and contain it within EOF.
fs/xfs/xfs_bmap.c | 193 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/xfs/xfs_bmap.h | 15 ++++ fs/xfs/xfs_bmap_util.c | 96 ++++++++++++++++++++++++ fs/xfs/xfs_bmap_util.h | 2 + fs/xfs/xfs_file.c | 19 ++++- fs/xfs/xfs_trace.h | 1 + 6 files changed, 324 insertions(+), 2 deletions(-)
fs/xfs/xfs_bmap.c +193 -0
@@ -5378,3 +5378,196 @@ error0: }return error; } + +/* +* Shift extent records to the left to cover a hole. +* +* The maximum number of extents to be shifted in a single operation +* is @num_exts, and @current_ext keeps track of the current extent +* index we have shifted. @offset_shift_fsb is the length by which each +* extent is shifted. If there is no hole to shift the extents +* into, this will be considered invalid operation and we abort immediately. +*/ +int +xfs_bmap_shift_extents ( +struct xfs_trans *tp, +struct xfs_inode *ip, +int *done, +xfs_fileoff_t start_fsb, +xfs_fileoff_t offset_shift_fsb, +xfs_extnum_t *current_ext, +xfs_fsblock_t *firstblock, +struct xfs_bmap_free *flist, +int num_exts) +{ +struct xfs_btree_cur *cur; +struct xfs_bmbt_rec_host *gotp; +struct xfs_bmbt_irec got; +struct xfs_bmbt_irec left; +struct xfs_mount *mp = ip->i_mount; +struct xfs_ifork *ifp; +xfs_extnum_t nexts =0 ; +xfs_fileoff_t startoff; +int error =0 ; +int i; +int whichfork =XFS_DATA_FORK ; +int logflags; +xfs_filblks_t blockcount =0 ; + +if (unlikely (XFS_TEST_ERROR ( + (XFS_IFORK_FORMAT (ip, whichfork) !=XFS_DINODE_FMT_EXTENTS && +XFS_IFORK_FORMAT (ip, whichfork) !=XFS_DINODE_FMT_BTREE ), + mp,XFS_ERRTAG_BMAPIFORMAT ,XFS_RANDOM_BMAPIFORMAT ))) { +XFS_ERROR_REPORT ("xfs_bmap_shift_extents" , +XFS_ERRLEVEL_LOW , mp); +return XFS_ERROR (EFSCORRUPTED ); + } + +if (XFS_FORCED_SHUTDOWN (mp)) +return XFS_ERROR (EIO ); + +ASSERT (current_ext !=NULL ); + + ifp =XFS_IFORK_PTR (ip, whichfork); + +if (!(ifp->if_flags &XFS_IFEXTENTS )) { +/* Read in all the extents */ + error =xfs_iread_extents (tp, ip, whichfork); +if (error) +return error; + } + +/* +* If *current_ext is 0, we would need to lookup the extent +* from where we would start shifting and store it in gotp. +*/ +if (!*current_ext) { + gotp =xfs_iext_bno_to_ext (ifp, start_fsb, current_ext); +/* +* gotp can be null in 2 cases: 1) if there are no extents +* or 2) start_fsb lies in a hole beyond which there are +* no extents. Either way, we are done. +*/ +if (!gotp) { + *done =1 ; +return 0 ; + } + } + +/* We are going to change core inode */ + logflags =XFS_ILOG_CORE ; + +if (ifp->if_flags &XFS_IFBROOT ) { + cur =xfs_bmbt_init_cursor (mp, tp, ip, whichfork); + cur->bc_private.b.firstblock = *firstblock; + cur->bc_private.b.flist = flist; + cur->bc_private.b.flags =0 ; + }else { + cur =NULL ; + logflags |=XFS_ILOG_DEXT ; + } + +while (nexts++ < num_exts && + *current_ext <XFS_IFORK_NEXTENTS (ip, whichfork)) { + + gotp =xfs_iext_get_ext (ifp, *current_ext); +xfs_bmbt_get_all (gotp, &got); + startoff = got.br_startoff - offset_shift_fsb; + +/* +* Before shifting extent into hole, make sure that the hole +* is large enough to accomodate the shift. +*/ +if (*current_ext) { +xfs_bmbt_get_all (xfs_iext_get_ext (ifp, + *current_ext -1 ), &left); + +if (startoff < left.br_startoff + left.br_blockcount) + error =XFS_ERROR (EINVAL ); + }else if (offset_shift_fsb > got.br_startoff) { +/* +* When first extent is shifted, offset_shift_fsb +* should be less than the stating offset of +* the first extent. +*/ + error =XFS_ERROR (EINVAL ); + } + +if (error) + goto del_cursor; + +if (cur) { + error =xfs_bmbt_lookup_eq (cur, got.br_startoff, + got.br_startblock, + got.br_blockcount, + &i); +if (error) + goto del_cursor; +XFS_WANT_CORRUPTED_GOTO (i ==1 , del_cursor); + } + +/* Check if we can merge 2 adjacent extents */ +if (*current_ext && + left.br_startoff + left.br_blockcount == startoff && + left.br_startblock + left.br_blockcount == + got.br_startblock && + left.br_state == got.br_state && + left.br_blockcount + got.br_blockcount <=MAXEXTLEN ) { + blockcount = left.br_blockcount + + got.br_blockcount; +xfs_iext_remove (ip, *current_ext,1 ,0 ); +if (cur) { + error =xfs_btree_delete (cur, &i); +if (error) + goto del_cursor; +XFS_WANT_CORRUPTED_GOTO (i ==1 , del_cursor); + } +XFS_IFORK_NEXT_SET (ip, whichfork, +XFS_IFORK_NEXTENTS (ip, whichfork) -1 ); + gotp =xfs_iext_get_ext (ifp, --*current_ext); +xfs_bmbt_get_all (gotp, &got); + +/* Make cursor point to the extent we will update */ +if (cur) { + error =xfs_bmbt_lookup_eq (cur, got.br_startoff, + got.br_startblock, + got.br_blockcount, + &i); +if (error) + goto del_cursor; +XFS_WANT_CORRUPTED_GOTO (i ==1 , del_cursor); + } + +xfs_bmbt_set_blockcount (gotp, blockcount); + got.br_blockcount = blockcount; + }else { +/* We have to update the startoff */ +xfs_bmbt_set_startoff (gotp, startoff); + got.br_startoff = startoff; + } + +if (cur) { + error =xfs_bmbt_update (cur, got.br_startoff, + got.br_startblock, + got.br_blockcount, + got.br_state); +if (error) + goto del_cursor; + } + + (*current_ext)++; + } + +/* Check if we are done */ +if (*current_ext ==XFS_IFORK_NEXTENTS (ip, whichfork)) + *done =1 ; + +del_cursor: +if (cur) +xfs_btree_del_cursor (cur, + error ?XFS_BTREE_ERROR :XFS_BTREE_NOERROR ); + +xfs_trans_log_inode (tp, ip, logflags); + +return error; +}
fs/xfs/xfs_bmap.h +15 -0
@@ -127,6 +127,16 @@ static inline void xfs_bmap_init(xfs_bmap_free_t *flp, xfs_fsblock_t *fbp) { BMAP_RIGHT_FILLING, "RF" }, \ { BMAP_ATTRFORK, "ATTR" } + +/* +* This macro is used to determine how many extents will be shifted +* in one write transaction. We could require two splits, +* an extent move on the first and an extent merge on the second, +* So it is proper that one extent is shifted inside write transaction +* at a time. +*/ +#define XFS_BMAP_MAX_SHIFT_EXTENTS 1 +#ifdef DEBUG void xfs_bmap_trace_exlist (struct xfs_inode *ip,xfs_extnum_t cnt,int whichfork,unsigned long caller_ip);
@@ -169,5 +179,10 @@ int xfs_bunmapi(struct xfs_trans *tp, struct xfs_inode *ip,int xfs_check_nostate_extents (struct xfs_ifork *ifp,xfs_extnum_t idx,xfs_extnum_t num);uint xfs_default_attroffset (struct xfs_inode *ip); +int xfs_bmap_shift_extents (struct xfs_trans *tp,struct xfs_inode *ip, +int *done,xfs_fileoff_t start_fsb, +xfs_fileoff_t offset_shift_fsb,xfs_extnum_t *current_ext, +xfs_fsblock_t *firstblock,struct xfs_bmap_free *flist, +int num_exts);#endif /* __XFS_BMAP_H__ */
fs/xfs/xfs_bmap_util.c +96 -0
102 lines not on this page · raw message
fs/xfs/xfs_bmap_util.h +2 -0
@@ -99,6 +99,8 @@ int xfs_free_file_space(struct xfs_inode *ip, xfs_off_t offset, xfs_off_t len);int xfs_zero_file_space (struct xfs_inode *ip,xfs_off_t offset,xfs_off_t len); +int xfs_collapse_file_space (struct xfs_inode *,xfs_off_t offset, +xfs_off_t len);/* EOF block manipulation functions */ bool xfs_can_free_eofblocks (struct xfs_inode *ip,bool force);
fs/xfs/xfs_file.c +17 -2
@@ -823,7 +823,8 @@ xfs_file_fallocate(if (!S_ISREG (inode->i_mode))return -EINVAL ; -if (mode & ~(FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE )) +if (mode & ~(FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE | +FALLOC_FL_COLLAPSE_RANGE ))return -EOPNOTSUPP ;xfs_ilock (ip,XFS_IOLOCK_EXCL );
@@ -831,6 +832,20 @@ xfs_file_fallocate( error =xfs_free_file_space (ip, offset, len);if (error) goto out_unlock; + }else if (mode &FALLOC_FL_COLLAPSE_RANGE ) { +unsigned blksize_mask = (1 << inode->i_blkbits) -1 ; + +if (offset & blksize_mask || len & blksize_mask) { + error = -EINVAL ; + goto out_unlock; + } + +ASSERT (offset + len <i_size_read (inode)); + new_size =i_size_read (inode) - len; + + error =xfs_collapse_file_space (ip, offset, len); +if (error) + goto out_unlock; }else {if (!(mode &FALLOC_FL_KEEP_SIZE ) && offset + len >i_size_read (inode)) {
@@ -859,7 +874,7 @@ xfs_file_fallocate(if (ip->i_d.di_mode &S_IXGRP ) ip->i_d.di_mode &= ~S_ISGID ; -if (!(mode &FALLOC_FL_PUNCH_HOLE )) +if (!(mode & (FALLOC_FL_PUNCH_HOLE |FALLOC_FL_COLLAPSE_RANGE ))) ip->i_d.di_flags |=XFS_DIFLAG_PREALLOC ;xfs_trans_ichgtime (tp, ip,XFS_ICHGTIME_MOD |XFS_ICHGTIME_CHG );
fs/xfs/xfs_trace.h +1 -0
@@ -603,6 +603,7 @@ DEFINE_INODE_EVENT(xfs_readlink);DEFINE_INODE_EVENT (xfs_inactive_symlink);DEFINE_INODE_EVENT (xfs_alloc_file_space);DEFINE_INODE_EVENT (xfs_free_file_space); +DEFINE_INODE_EVENT (xfs_collapse_file_space);DEFINE_INODE_EVENT (xfs_readdir);#ifdef CONFIG_XFS_POSIX_ACL DEFINE_INODE_EVENT (xfs_get_acl);
Signature · 7 lines
1.7.11-rc0 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 16 lines · Namjae Jeon
From: Namjae Jeon <[email protected]>
This patch implements fallocate's
FALLOC_FL_COLLAPSE_RANGEfor XFS.The semantics of this flag are following:
1) It collapses the range lying between offset and length by removing any data blocks which are present in this range and than updates all the logical offsets of extents beyond "offset + len" to nullify the hole created by removing blocks. In short, it does not leave a hole.
2) It should be used exclusively. No other fallocate flag in combination.
3) Offset and length supplied to fallocate should be fs block size aligned in case of xfs and ext4.
4) Collaspe range does not work beyondi_size.Signed-off-by Namjae Jeon <[email protected]>Signed-off-by Ashish Sangwan <[email protected]>
.....
fs/xfs/xfs_bmap_util.c out: · line 1512
Quoted 3 lines
fs/xfs/xfs_bmap_util.c +3
-0+while (!error && !done) { + tp =xfs_trans_alloc (mp,XFS_TRANS_DIOSTRAT ); + tp->t_flags |=XFS_TRANS_RESERVE ;
This probably shouldn't use XFS_TRANS_RESERVE. If we are at ENOSPC, then the operation simply fails. Yes, we've already punched the hole, so we shouldn't get ENOSPC here, but I don't think it's worth dipping into the reserve pool as it has much more important uses...
You don' tneed to resent the entire patch for this - I can remove it directly myself....
Otherwise it looks good, so consider it
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
2014-02-24 6:22 GMT+09:00, Dave Chinner <[email protected]>:
Quoted 19 lines · Namjae Jeon
Quoted 18 lines
From: Namjae Jeon <[email protected]>
This patch implements fallocate's
FALLOC_FL_COLLAPSE_RANGEfor XFS.The semantics of this flag are following:
1) It collapses the range lying between offset and length by removing any data
blocks which are present in this range and than updates all the
logical
offsets of extents beyond "offset + len" to nullify the hole created
by
removing blocks. In short, it does not leave a hole.
2) It should be used exclusively. No other fallocate flag in combination.
3) Offset and length supplied to fallocate should be fs block size aligned
in case of xfs and ext4.
4) Collaspe range does not work beyondi_size.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Namjae Jeon <[email protected]>Signed-off-by Ashish Sangwan <[email protected]>.....
fs/xfs/xfs_bmap_util.c out: · line 1512
Quoted 3 lines
fs/xfs/xfs_bmap_util.c +3
-0+while (!error && !done) { + tp =xfs_trans_alloc (mp,XFS_TRANS_DIOSTRAT ); + tp->t_flags |=XFS_TRANS_RESERVE ;
Hi Dave.
Quoted 4 lines
This probably shouldn't use
XFS_TRANS_RESERVE. If we are at ENOSPC, then the operation simply fails. Yes, we've already punched the hole, so we shouldn't get ENOSPC here, but I don't think it's worth dipping into the reserve pool as it has much more important uses...
Okay.
Quoted 3 lines
You don' tneed to resent the entire patch for this - I can remove it directly myself....
Thanks very much for your help!!
Quoted 12 lines
Otherwise it looks good, so consider it
Reviewed-by Dave Chinner <[email protected]>Cheers,
Dave.
--
Dave Chinner
[email protected]