post-EOF block handling revamp v3
Hi all,
this series reworks handling of post-EOF blocks, primarily in ->release. This takes over the work originally started by Dave in:
https://marc.info/?l=linux-xfs&m=154951612101291&w=2
and lingering in Darricks's tree:
for years to ensure ->release doesn't too eagerly kill post-EOF block speculative preallocation and then goes on to not let preallocation on inodes with the append only flag set linger forever.
Note that this does not include any of the suggested improvements for i_flags_lock contention - that's left for a follow-on series.
Changes since v2:
- fix another typo
- use test and set semantics for XFS_EOFBLOCKS_RELEASED
Changes since v1:
- fix a few commit log and comment typos
Diffstat:
xfs_bmap_util.c | 34 ++++++++--------------- xfs_file.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++-- xfs_icache.c | 2 - xfs_inode.c | 82 -------------------------------------------------------- xfs_inode.h | 5 +-- 5 files changed, 84 insertions(+), 111 deletions(-)
PATCH 1/9 xfs: remove the i_mode check in xfs_release
xfs_release is only called from xfs_file_release, which is wired up as the f_op->release handler for regular files only.
--- fs/xfs/xfs_inode.c | 3 --- 1 file changed, 3 deletions(-)
fs/xfs/xfs_inode.c +0 -3
@@ -1086,9 +1086,6 @@ xfs_release(xfs_mount_t *mp = ip->i_mount;int error =0 ; -if (!S_ISREG (VFS_I (ip)->i_mode) || (VFS_I (ip)->i_mode ==0 )) -return 0 ; -/* If this is a read-only mount, don't do this (would generate I/O) */ if (xfs_is_readonly (mp))return 0 ;
Signature · 1 line
2.43.0
PATCH 2/9 xfs: refactor f_op->release handling
In reply to Christoph HellwigCurrently f_op->release is split in not very obvious ways. Fix that by folding xfs_release into xfs_file_release.
--- fs/xfs/xfs_file.c | 71 +++++++++++++++++++++++++++++++++++++++-- fs/xfs/xfs_inode.c | 79 ---------------------------------------------- fs/xfs/xfs_inode.h | 1 - 3 files changed, 68 insertions(+), 83 deletions(-)
fs/xfs/xfs_file.c +68 -3
@@ -1177,10 +1177,75 @@ xfs_dir_open( STATIC intxfs_file_release ( -struct inode *inode, -struct file *filp) +struct inode *inode, +struct file *file) { -return xfs_release (XFS_I (inode)); +struct xfs_inode *ip =XFS_I (inode); +struct xfs_mount *mp = ip->i_mount; +int error; + +/* If this is a read-only mount, don't generate I/O */ +if (xfs_is_readonly (mp)) +return 0 ; + +/* +* If we previously truncated this file and removed old data in the +* process, we want to initiate "early" writeout on the last close. +* This is an attempt to combat the notorious NULL files problem which +* is particularly noticeable from a truncate down, buffered (re-)write +* (delalloc), followed by a crash. What we are effectively doing here +* is significantly reducing the time window where we'd otherwise be +* exposed to that problem. +*/ +if (!xfs_is_shutdown (mp) && +xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) { +xfs_iflags_clear (ip,XFS_IDIRTY_RELEASE ); +if (ip->i_delayed_blks >0 ) { + error =filemap_flush (inode->i_mapping); +if (error) +return error; + } + } + +/* +* XFS aggressively preallocates post-EOF space to generate contiguous +* allocations for writers that append to the end of the file and we +* try to free these when an open file context is released. +* +* There is no point in freeing blocks here for open but unlinked files +* as they will be taken care of by the inactivation path soon. +* +* If we can't get the iolock just skip truncating the blocks past EOF +* because we could deadlock with the mmap_lock otherwise. We'll get +* another chance to drop them once the last reference to the inode is +* dropped, so we'll never leak blocks permanently. +*/ +if (inode->i_nlink &&xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) { +if (xfs_can_free_eofblocks (ip) && + !xfs_iflags_test (ip,XFS_IDIRTY_RELEASE )) { +/* +* Check if the inode is being opened, written and +* closed frequently and we have delayed allocation +* blocks outstanding (e.g. streaming writes from the +* NFS server), truncating the blocks past EOF will +* cause fragmentation to occur. +* +* In this case don't do the truncation, but we have to +* be careful how we detect this case. Blocks beyond EOF +* show up as i_delayed_blks even when the inode is +* clean, so we need to truncate them away first before +* checking for a dirty release. Hence on the first +* dirty close we will still remove the speculative +* allocation, but after that we will leave it in place. +*/ + error =xfs_free_eofblocks (ip); +if (!error && ip->i_delayed_blks) +xfs_iflags_set (ip,XFS_IDIRTY_RELEASE ); + } +xfs_iunlock (ip,XFS_IOLOCK_EXCL ); + } + +return error; }STATIC int
fs/xfs/xfs_inode.c +0 -79
85 lines not on this page · raw message
fs/xfs/xfs_inode.h +0 -1
@@ -512,7 +512,6 @@ enum layout_break_reason {#define XFS_INHERIT_GID (pip) \ (xfs_has_grpid((pip)->i_mount) || (VFS_I(pip)->i_mode & S_ISGID)) -int xfs_release (struct xfs_inode *ip);int xfs_inactive (struct xfs_inode *ip);int xfs_lookup (struct xfs_inode *dp,const struct xfs_name *name,struct xfs_inode **ipp,struct xfs_name *ci_name);
Signature · 1 line
2.43.0
PATCH 3/9 xfs: don't bother returning errors from xfs_file_release
In reply to Christoph HellwigWhile ->release returns int, the only caller ignores the return value. As we're only doing cleanup work there isn't much of a point in return a value to start with, so just document the situation instead.
--- fs/xfs/xfs_file.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
fs/xfs/xfs_file.c +9 -9
@@ -1175,6 +1175,10 @@ xfs_dir_open(return error; } +/* +* Don't bother propagating errors. We're just doing cleanup, and the caller +* ignores the return value anyway. +*/ STATIC int xfs_file_release( struct inode *inode,
@@ -1182,7 +1186,6 @@ xfs_file_release( {struct xfs_inode *ip =XFS_I (inode);struct xfs_mount *mp = ip->i_mount; -int error;/* If this is a read-only mount, don't generate I/O */ if (xfs_is_readonly (mp))
@@ -1200,11 +1203,8 @@ xfs_file_release(if (!xfs_is_shutdown (mp) &&xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) {xfs_iflags_clear (ip,XFS_IDIRTY_RELEASE ); -if (ip->i_delayed_blks >0 ) { - error =filemap_flush (inode->i_mapping); -if (error) -return error; - } +if (ip->i_delayed_blks >0 ) +filemap_flush (inode->i_mapping); }/*
@@ -1238,14 +1238,14 @@ xfs_file_release(* dirty close we will still remove the speculative * allocation, but after that we will leave it in place. */ - error =xfs_free_eofblocks (ip); -if (!error && ip->i_delayed_blks) +xfs_free_eofblocks (ip); +if (ip->i_delayed_blks)xfs_iflags_set (ip,XFS_IDIRTY_RELEASE ); }xfs_iunlock (ip,XFS_IOLOCK_EXCL ); } -return error; +return 0 ; }STATIC int
Signature · 1 line
2.43.0
PATCH 4/9 xfs: skip all of xfs_file_release when shut down
In reply to Christoph HellwigThere is no point in trying to free post-EOF blocks when the file system is shutdown, as it will just error out ASAP. Instead return instantly when xfs_file_release is called on a shut down file system.
--- fs/xfs/xfs_file.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
fs/xfs/xfs_file.c +6 -4
@@ -1187,8 +1187,11 @@ xfs_file_release(struct xfs_inode *ip =XFS_I (inode);struct xfs_mount *mp = ip->i_mount; -/* If this is a read-only mount, don't generate I/O */ -if (xfs_is_readonly (mp)) +/* +* If this is a read-only mount or the file system has been shut down, +* don't generate I/O. +*/ +if (xfs_is_readonly (mp) ||xfs_is_shutdown (mp))return 0 ;/*
@@ -1200,8 +1203,7 @@ xfs_file_release(* is significantly reducing the time window where we'd otherwise be * exposed to that problem. */ -if (!xfs_is_shutdown (mp) && -xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) { +if (xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) {xfs_iflags_clear (ip,XFS_IDIRTY_RELEASE );if (ip->i_delayed_blks >0 )filemap_flush (inode->i_mapping);
Signature · 1 line
2.43.0
PATCH 5/9 xfs: don't free post-EOF blocks on read close
In reply to Christoph HellwigFrom: Dave Chinner <[email protected]>
When we have a workload that does open/read/close in parallel with other allocation, the file becomes rapidly fragmented. This is due to close() calling xfs_file_release() and removing the speculative preallocation beyond EOF.
Add a check for a writable context to xfs_file_release to skip the post-EOF block freeing (an the similarly pointless flushing on truncate down).
Before:
Test 1: sync write fragmentation counts
/mnt/scratch/file.0: 919/mnt/scratch/file.1: 916/mnt/scratch/file.2: 919/mnt/scratch/file.3: 920/mnt/scratch/file.4: 920/mnt/scratch/file.5: 921/mnt/scratch/file.6: 916/mnt/scratch/file.7: 918
After:
Test 1: sync write fragmentation counts
/mnt/scratch/file.0: 24/mnt/scratch/file.1: 24/mnt/scratch/file.2: 11/mnt/scratch/file.3: 24/mnt/scratch/file.4: 3/mnt/scratch/file.5: 24/mnt/scratch/file.6: 24/mnt/scratch/file.7: 23
--- fs/xfs/xfs_file.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
fs/xfs/xfs_file.c +7 -1
@@ -1217,12 +1217,18 @@ xfs_file_release(* There is no point in freeing blocks here for open but unlinked files * as they will be taken care of by the inactivation path soon. * +* When releasing a read-only context, don't flush data or trim post-EOF +* blocks. This avoids open/read/close workloads from removing EOF +* blocks that other writers depend upon to reduce fragmentation. +* * If we can't get the iolock just skip truncating the blocks past EOF * because we could deadlock with the mmap_lock otherwise. We'll get * another chance to drop them once the last reference to the inode is * dropped, so we'll never leak blocks permanently. */ -if (inode->i_nlink &&xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) { +if (inode->i_nlink && + (file->f_mode &FMODE_WRITE ) && +xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) {if (xfs_can_free_eofblocks (ip) && !xfs_iflags_test (ip,XFS_IDIRTY_RELEASE )) {/*
Signature · 1 line
2.43.0
PATCH 6/9 xfs: only free posteof blocks on first close
In reply to Christoph HellwigFrom: "Darrick J. Wong" <[email protected]>
Certain workloads fragment files on XFS very badly, such as a software package that creates a number of threads, each of which repeatedly run the sequence: open a file, perform a synchronous write, and close the file, which defeats the speculative preallocation mechanism. We work around this problem by only deleting posteof blocks the /first/ time a file is closed to preserve the behavior that unpacking a tarball lays out files one after the other with no gaps.
--- fs/xfs/xfs_file.c | 32 +++++++++++--------------------- fs/xfs/xfs_inode.h | 4 ++-- 2 files changed, 13 insertions(+), 23 deletions(-)
fs/xfs/xfs_file.c +11 -21
@@ -1204,15 +1204,21 @@ xfs_file_release(* exposed to that problem. */ if (xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) { -xfs_iflags_clear (ip,XFS_IDIRTY_RELEASE ); +xfs_iflags_clear (ip,XFS_EOFBLOCKS_RELEASED );if (ip->i_delayed_blks >0 )filemap_flush (inode->i_mapping); }/* * XFS aggressively preallocates post-EOF space to generate contiguous -* allocations for writers that append to the end of the file and we -* try to free these when an open file context is released. +* allocations for writers that append to the end of the file. +* +* To support workloads that close and reopen the file frequently, these +* preallocations usually persist after a close unless it is the first +* close for the inode. This is a tradeoff to generate tightly packed +* data layouts for unpacking tarballs or similar archives that write +* one file after another without going back to it while keeping the +* preallocation for files that have recurring open/write/close cycles. * * There is no point in freeing blocks here for open but unlinked files * as they will be taken care of by the inactivation path soon.
@@ -1230,25 +1236,9 @@ xfs_file_release( (file->f_mode & FMODE_WRITE) && xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {if (xfs_can_free_eofblocks (ip) && - !xfs_iflags_test (ip,XFS_IDIRTY_RELEASE )) { -/* -* Check if the inode is being opened, written and -* closed frequently and we have delayed allocation -* blocks outstanding (e.g. streaming writes from the -* NFS server), truncating the blocks past EOF will -* cause fragmentation to occur. -* -* In this case don't do the truncation, but we have to -* be careful how we detect this case. Blocks beyond EOF -* show up as i_delayed_blks even when the inode is -* clean, so we need to truncate them away first before -* checking for a dirty release. Hence on the first -* dirty close we will still remove the speculative -* allocation, but after that we will leave it in place. -*/ + !xfs_iflags_test (ip,XFS_EOFBLOCKS_RELEASED )) {xfs_free_eofblocks (ip); -if (ip->i_delayed_blks) -xfs_iflags_set (ip,XFS_IDIRTY_RELEASE ); +xfs_iflags_set (ip,XFS_EOFBLOCKS_RELEASED ); }xfs_iunlock (ip,XFS_IOLOCK_EXCL ); }
fs/xfs/xfs_inode.h +2 -2
@@ -335,7 +335,7 @@ static inline bool xfs_inode_has_bigrtalloc(struct xfs_inode *ip)#define XFS_INEW (1 << 3)/* inode has just been allocated */ #define XFS_IPRESERVE_DM_FIELDS (1 << 4)/* has legacy DMAPI fields set */ #define XFS_ITRUNCATED (1 << 5)/* truncated down so flush-on-close */ -#define XFS_IDIRTY_RELEASE (1 << 6)/* dirty release already seen */ +#define XFS_EOFBLOCKS_RELEASED (1 << 6)/* eofblocks were freed in ->release */ #define XFS_IFLUSHING (1 << 7)/* inode is being flushed */ #define __XFS_IPINNED_BIT 8/* wakeup key for zero pin count */ #define XFS_IPINNED (1 << __XFS_IPINNED_BIT)
@@ -382,7 +382,7 @@ static inline bool xfs_inode_has_bigrtalloc(struct xfs_inode *ip)*/ #define XFS_IRECLAIM_RESET_FLAGS \ (XFS_IRECLAIMABLE | XFS_IRECLAIM | \ - XFS_IDIRTY_RELEASE | XFS_ITRUNCATED | XFS_NEED_INACTIVE | \ + XFS_EOFBLOCKS_RELEASED | XFS_ITRUNCATED | XFS_NEED_INACTIVE | \ XFS_INACTIVATING | XFS_IQUOTAUNCHECKED)/*
Signature · 1 line
2.43.0
PATCH 7/9 xfs: check XFS_EOFBLOCKS_RELEASED earlier in xfs_release_eofblocks
In reply to Christoph HellwigIf the XFS_EOFBLOCKS_RELEASED flag is set, we are not going to free the eofblocks, so don't bother locking the inode or performing the checks in xfs_can_free_eofblocks. Also switch to a test_and_set operation once the iolock has been acquire so that only the caller that sets it actually frees the post-EOF blocks.
--- fs/xfs/xfs_file.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
fs/xfs/xfs_file.c +2 -3
@@ -1234,12 +1234,11 @@ xfs_file_release(*/ if (inode->i_nlink && (file->f_mode &FMODE_WRITE ) && + !xfs_iflags_test (ip,XFS_EOFBLOCKS_RELEASED ) &&xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) {if (xfs_can_free_eofblocks (ip) && - !xfs_iflags_test (ip,XFS_EOFBLOCKS_RELEASED )) { + !xfs_iflags_test_and_set (ip,XFS_EOFBLOCKS_RELEASED ))xfs_free_eofblocks (ip); -xfs_iflags_set (ip,XFS_EOFBLOCKS_RELEASED ); - }xfs_iunlock (ip,XFS_IOLOCK_EXCL ); }
Signature · 1 line
2.43.0
PATCH 8/9 xfs: simplify extent lookup in xfs_can_free_eofblocks
In reply to Christoph Hellwigxfs_can_free_eofblocks just cares if there is an extent beyond EOF. Replace the call to xfs_bmapi_read with a xfs_iext_lookup_extent as we've already checked that extents are read in earlier.
--- fs/xfs/xfs_bmap_util.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-)
fs/xfs/xfs_bmap_util.c +7 -15
@@ -492,12 +492,12 @@ boolxfs_can_free_eofblocks ( struct xfs_inode *ip) { -struct xfs_bmbt_irec imap;struct xfs_mount *mp = ip->i_mount; +bool found_blocks = false;xfs_fileoff_t end_fsb;xfs_fileoff_t last_fsb; -int nimaps =1 ; -int error; +struct xfs_bmbt_irec imap; +struct xfs_iext_cursor icur;/* * Caller must either hold the exclusive io lock; or be inactivating
@@ -544,21 +544,13 @@ xfs_can_free_eofblocks(return false;/* -* Look up the mapping for the first block past EOF. If we can't find -* it, there's nothing to free. +* Check if there is an post-EOF extent to free. */ xfs_ilock (ip,XFS_ILOCK_SHARED ); - error =xfs_bmapi_read (ip, end_fsb, last_fsb - end_fsb, &imap, &nimaps, -0 ); +if (xfs_iext_lookup_extent (ip, &ip->i_df, end_fsb, &icur, &imap)) + found_blocks = true;xfs_iunlock (ip,XFS_ILOCK_SHARED ); -if (error || nimaps ==0 ) -return false; - -/* -* If there's a real mapping there or there are delayed allocation -* reservations, then we have post-EOF blocks to try to free. -*/ -return imap.br_startblock !=HOLESTARTBLOCK || ip->i_delayed_blks; +return found_blocks; }/*
Signature · 1 line
2.43.0
PATCH 9/9 xfs: reclaim speculative preallocations for append only files
In reply to Christoph HellwigThe XFS XFS_DIFLAG_APPEND maps to the VFS S_APPEND flag, which forbids writes that don't append at the current EOF.
But the commit originally adding XFS_DIFLAG_APPEND support (commit a23321e766d in xfs xfs-import repository) also checked it to skip releasing speculative preallocations, which doesn't make any sense.
Another commit (dd9f438e3290 in the xfs-import repository) later extended that flag to also report these speculation preallocations which should not exist in getbmap.
Remove these checks as nothing XFS_DIFLAG_APPEND implies that preallocations beyond EOF should exist, but explicitly check for XFS_DIFLAG_APPEND in xfs_file_release to bypass the algorithm that discard preallocations on the first close as append only files aren't expected to be written to only once.
--- fs/xfs/xfs_bmap_util.c | 12 +++++------- fs/xfs/xfs_file.c | 4 ++++ fs/xfs/xfs_icache.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-)
fs/xfs/xfs_bmap_util.c +5 -7
@@ -331,8 +331,7 @@ xfs_getbmap( }if (xfs_get_extsz_hint (ip) || - (ip->i_diflags & - (XFS_DIFLAG_PREALLOC |XFS_DIFLAG_APPEND ))) + (ip->i_diflags &XFS_DIFLAG_PREALLOC )) max_len = mp->m_super->s_maxbytes;else max_len =XFS_ISIZE (ip);
@@ -524,12 +523,11 @@ xfs_can_free_eofblocks(return false;/* -* Only free real extents for inodes with persistent preallocations or -* the append-only flag. +* Do not free real extents in preallocated files unless the file has +* delalloc blocks and we are forced to remove them. */ -if (ip->i_diflags & (XFS_DIFLAG_PREALLOC |XFS_DIFLAG_APPEND )) -if (ip->i_delayed_blks ==0 ) -return false; +if ((ip->i_diflags &XFS_DIFLAG_PREALLOC ) && !ip->i_delayed_blks) +return false;/* * Do not try to free post-EOF blocks if EOF is beyond the end of the
fs/xfs/xfs_file.c +4 -0
@@ -1220,6 +1220,9 @@ xfs_file_release(* one file after another without going back to it while keeping the * preallocation for files that have recurring open/write/close cycles. * +* This heuristic is skipped for inodes with the append-only flag as +* that flag is rather pointless for inodes written only once. +* * There is no point in freeing blocks here for open but unlinked files * as they will be taken care of by the inactivation path soon. *
@@ -1234,6 +1237,7 @@ xfs_file_release(*/ if (inode->i_nlink && (file->f_mode &FMODE_WRITE ) && + !(ip->i_diflags &XFS_DIFLAG_APPEND ) && !xfs_iflags_test (ip,XFS_EOFBLOCKS_RELEASED ) &&xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) { if (xfs_can_free_eofblocks(ip) &&
fs/xfs/xfs_icache.c +1 -1
@@ -1159,7 +1159,7 @@ xfs_inode_free_eofblocks(if (xfs_can_free_eofblocks (ip))return xfs_free_eofblocks (ip); -/* inode could be preallocated or append-only */ +/* inode could be preallocated */ trace_xfs_inode_free_eofblocks_invalid (ip);xfs_inode_clear_eofblocks_tag (ip);return 0 ;
Signature · 1 line
2.43.0