Skip to content

Commit f1204d9

Browse files
Darrick J. WongChandan Babu R
authored andcommitted
xfs: only free posteof blocks on first close
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. Signed-off-by: Darrick J. Wong <[email protected]> [hch: rebased, updated comment, renamed the flag] Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Signed-off-by: Chandan Babu R <[email protected]>
1 parent 816e359 commit f1204d9

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

fs/xfs/xfs_file.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,15 +1204,21 @@ xfs_file_release(
12041204
* exposed to that problem.
12051205
*/
12061206
if (xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED)) {
1207-
xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1207+
xfs_iflags_clear(ip, XFS_EOFBLOCKS_RELEASED);
12081208
if (ip->i_delayed_blks > 0)
12091209
filemap_flush(inode->i_mapping);
12101210
}
12111211

12121212
/*
12131213
* XFS aggressively preallocates post-EOF space to generate contiguous
1214-
* allocations for writers that append to the end of the file and we
1215-
* try to free these when an open file context is released.
1214+
* allocations for writers that append to the end of the file.
1215+
*
1216+
* To support workloads that close and reopen the file frequently, these
1217+
* preallocations usually persist after a close unless it is the first
1218+
* close for the inode. This is a tradeoff to generate tightly packed
1219+
* data layouts for unpacking tarballs or similar archives that write
1220+
* one file after another without going back to it while keeping the
1221+
* preallocation for files that have recurring open/write/close cycles.
12161222
*
12171223
* There is no point in freeing blocks here for open but unlinked files
12181224
* as they will be taken care of by the inactivation path soon.
@@ -1230,25 +1236,9 @@ xfs_file_release(
12301236
(file->f_mode & FMODE_WRITE) &&
12311237
xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
12321238
if (xfs_can_free_eofblocks(ip) &&
1233-
!xfs_iflags_test(ip, XFS_IDIRTY_RELEASE)) {
1234-
/*
1235-
* Check if the inode is being opened, written and
1236-
* closed frequently and we have delayed allocation
1237-
* blocks outstanding (e.g. streaming writes from the
1238-
* NFS server), truncating the blocks past EOF will
1239-
* cause fragmentation to occur.
1240-
*
1241-
* In this case don't do the truncation, but we have to
1242-
* be careful how we detect this case. Blocks beyond EOF
1243-
* show up as i_delayed_blks even when the inode is
1244-
* clean, so we need to truncate them away first before
1245-
* checking for a dirty release. Hence on the first
1246-
* dirty close we will still remove the speculative
1247-
* allocation, but after that we will leave it in place.
1248-
*/
1239+
!xfs_iflags_test(ip, XFS_EOFBLOCKS_RELEASED)) {
12491240
xfs_free_eofblocks(ip);
1250-
if (ip->i_delayed_blks)
1251-
xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1241+
xfs_iflags_set(ip, XFS_EOFBLOCKS_RELEASED);
12521242
}
12531243
xfs_iunlock(ip, XFS_IOLOCK_EXCL);
12541244
}

fs/xfs/xfs_inode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ static inline bool xfs_inode_has_bigrtalloc(struct xfs_inode *ip)
336336
#define XFS_INEW (1 << 3) /* inode has just been allocated */
337337
#define XFS_IPRESERVE_DM_FIELDS (1 << 4) /* has legacy DMAPI fields set */
338338
#define XFS_ITRUNCATED (1 << 5) /* truncated down so flush-on-close */
339-
#define XFS_IDIRTY_RELEASE (1 << 6) /* dirty release already seen */
339+
#define XFS_EOFBLOCKS_RELEASED (1 << 6) /* eofblocks were freed in ->release */
340340
#define XFS_IFLUSHING (1 << 7) /* inode is being flushed */
341341
#define __XFS_IPINNED_BIT 8 /* wakeup key for zero pin count */
342342
#define XFS_IPINNED (1 << __XFS_IPINNED_BIT)
@@ -383,7 +383,7 @@ static inline bool xfs_inode_has_bigrtalloc(struct xfs_inode *ip)
383383
*/
384384
#define XFS_IRECLAIM_RESET_FLAGS \
385385
(XFS_IRECLAIMABLE | XFS_IRECLAIM | \
386-
XFS_IDIRTY_RELEASE | XFS_ITRUNCATED | XFS_NEED_INACTIVE | \
386+
XFS_EOFBLOCKS_RELEASED | XFS_ITRUNCATED | XFS_NEED_INACTIVE | \
387387
XFS_INACTIVATING | XFS_IQUOTAUNCHECKED)
388388

389389
/*

0 commit comments

Comments
 (0)