From patchwork Sun Jun 23 05:34:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13708488 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D832717C77 for ; Sun, 23 Jun 2024 05:35:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120941; cv=none; b=oLXkXUe3fixooL0gMBG5mFupZJJHnCfTGfjBH+xAZVOhUnozo4ZP/xDBb7BvrOY/tKVidUgmBUvgvC/EfvDZ558AdnYx2jqn4rgnPuPPfHJqgCdKjP0S6pN+PBr5G7ra//e+LCbEyupgmRPn0Cw8y7irLG/iq3PCw9UBUph5raE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120941; c=relaxed/simple; bh=DXxsVXIbVosYCl6BpQdDlVwNks8lt5EkSN1fg9rrGog=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pwCKeHflJAjzBtOxT5BYn9ZpTOraQ1DEfo18wbvhfofE6le1+U18PSCMCdtMhsmKaGmPOchTh2FGKB8PrGdnmIjGgT8U+kL8yneTFI2i2zpw0APKBD1BF6MPlLSkaNSLt9GWtSL1ggwEoPCKCMnPyf7+WnStFau0vSuF6mN7nVs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=g0XcGyRO; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="g0XcGyRO" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=FZh9xx0esYtfCcXPZ4cwTXmTytLJ80FpYkK0BOtlmbM=; b=g0XcGyROQHYn781XonSGbPxvVw vGdWvbUtYIENdUFdU93kLPuI/xavLO0dGroLzxf725HaSP8bq2LxMQdM9NLUInsGGV5Oov3pySDrh 1KEQriu+BHmJ4E+QGeGE7Nnk4CwyQRzZMxytk0FCNLGv2+aFOjkyqqBbvxSvgq69MFyIEE2lbcahI E5wi9vK+S48f+BCASMPEQ9Jwg7dQb+v3ZgnS59/+u+8aI39dSzVKuJymlGVqWdt0uzMMPhy6LRXFb odMP7fC/7W7Z7X+S2Dd0HARCWWMnjbuMNijeQbPNrwxn+SwZp0kRVsvgd/MVH1yosuYqUM+SOWl73 wHchc+Aw==; Received: from 2a02-8389-2341-5b80-9456-578d-194f-dacd.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:9456:578d:194f:dacd] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLFt4-0000000DOE7-0mTd; Sun, 23 Jun 2024 05:35:38 +0000 From: Christoph Hellwig To: Chandan Babu R Cc: "Darrick J. Wong" , Dave Chinner , linux-xfs@vger.kernel.org Subject: [PATCH 01/10] xfs: fix freeing speculative preallocations for preallocated files Date: Sun, 23 Jun 2024 07:34:46 +0200 Message-ID: <20240623053532.857496-2-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240623053532.857496-1-hch@lst.de> References: <20240623053532.857496-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html xfs_can_free_eofblocks returns false for files that have persistent preallocations unless the force flag is passed and there are delayed blocks. This means it won't free delalloc reservations for files with persistent preallocations unless the force flag is set, and it will also free the persistent preallocations if the force flag is set and the file happens to have delayed allocations. Both of these are bad, so do away with the force flag and always free post-EOF delayed allocations only for files with the XFS_DIFLAG_PREALLOC flag set. Signed-off-by: Christoph Hellwig Reviewed-by: Darrick J. Wong --- fs/xfs/xfs_bmap_util.c | 30 ++++++++++++++++++++++-------- fs/xfs/xfs_bmap_util.h | 2 +- fs/xfs/xfs_icache.c | 2 +- fs/xfs/xfs_inode.c | 14 ++++---------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/fs/xfs/xfs_bmap_util.c b/fs/xfs/xfs_bmap_util.c index ac2e77ebb54c73..a4d9fbc21b8343 100644 --- a/fs/xfs/xfs_bmap_util.c +++ b/fs/xfs/xfs_bmap_util.c @@ -486,13 +486,11 @@ xfs_bmap_punch_delalloc_range( /* * Test whether it is appropriate to check an inode for and free post EOF - * blocks. The 'force' parameter determines whether we should also consider - * regular files that are marked preallocated or append-only. + * blocks. */ bool xfs_can_free_eofblocks( - struct xfs_inode *ip, - bool force) + struct xfs_inode *ip) { struct xfs_bmbt_irec imap; struct xfs_mount *mp = ip->i_mount; @@ -526,11 +524,11 @@ xfs_can_free_eofblocks( return false; /* - * Do not free real preallocated or append-only files unless the file - * has delalloc blocks and we are forced to remove them. + * Only free real extents for inodes with persistent preallocations or + * the append-only flag. */ if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) - if (!force || ip->i_delayed_blks == 0) + if (ip->i_delayed_blks == 0) return false; /* @@ -584,6 +582,22 @@ xfs_free_eofblocks( /* Wait on dio to ensure i_size has settled. */ inode_dio_wait(VFS_I(ip)); + /* + * For preallocated files only free delayed allocations. + * + * Note that this means we also leave speculative preallocations in + * place for preallocated files. + */ + if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) { + if (ip->i_delayed_blks) { + xfs_bmap_punch_delalloc_range(ip, + round_up(XFS_ISIZE(ip), mp->m_sb.sb_blocksize), + LLONG_MAX); + } + xfs_inode_clear_eofblocks_tag(ip); + return 0; + } + error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); if (error) { ASSERT(xfs_is_shutdown(mp)); @@ -891,7 +905,7 @@ xfs_prepare_shift( * Trim eofblocks to avoid shifting uninitialized post-eof preallocation * into the accessible region of the file. */ - if (xfs_can_free_eofblocks(ip, true)) { + if (xfs_can_free_eofblocks(ip)) { error = xfs_free_eofblocks(ip); if (error) return error; diff --git a/fs/xfs/xfs_bmap_util.h b/fs/xfs/xfs_bmap_util.h index 51f84d8ff372fa..eb0895bfb9dae4 100644 --- a/fs/xfs/xfs_bmap_util.h +++ b/fs/xfs/xfs_bmap_util.h @@ -63,7 +63,7 @@ int xfs_insert_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); +bool xfs_can_free_eofblocks(struct xfs_inode *ip); int xfs_free_eofblocks(struct xfs_inode *ip); int xfs_swap_extents(struct xfs_inode *ip, struct xfs_inode *tip, diff --git a/fs/xfs/xfs_icache.c b/fs/xfs/xfs_icache.c index 0953163a2d8492..9967334ea99f1a 100644 --- a/fs/xfs/xfs_icache.c +++ b/fs/xfs/xfs_icache.c @@ -1155,7 +1155,7 @@ xfs_inode_free_eofblocks( } *lockflags |= XFS_IOLOCK_EXCL; - if (xfs_can_free_eofblocks(ip, false)) + if (xfs_can_free_eofblocks(ip)) return xfs_free_eofblocks(ip); /* inode could be preallocated or append-only */ diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c index f36091e1e7f50b..38f946e3be2da3 100644 --- a/fs/xfs/xfs_inode.c +++ b/fs/xfs/xfs_inode.c @@ -1595,7 +1595,7 @@ xfs_release( if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) return 0; - if (xfs_can_free_eofblocks(ip, false)) { + if (xfs_can_free_eofblocks(ip)) { /* * Check if the inode is being opened, written and closed * frequently and we have delayed allocation blocks outstanding @@ -1856,15 +1856,13 @@ xfs_inode_needs_inactive( /* * This file isn't being freed, so check if there are post-eof blocks - * to free. @force is true because we are evicting an inode from the - * cache. Post-eof blocks must be freed, lest we end up with broken - * free space accounting. + * to free. * * Note: don't bother with iolock here since lockdep complains about * acquiring it in reclaim context. We have the only reference to the * inode at this point anyways. */ - return xfs_can_free_eofblocks(ip, true); + return xfs_can_free_eofblocks(ip); } /* @@ -1947,15 +1945,11 @@ xfs_inactive( if (VFS_I(ip)->i_nlink != 0) { /* - * force is true because we are evicting an inode from the - * cache. Post-eof blocks must be freed, lest we end up with - * broken free space accounting. - * * Note: don't bother with iolock here since lockdep complains * about acquiring it in reclaim context. We have the only * reference to the inode at this point anyways. */ - if (xfs_can_free_eofblocks(ip, true)) + if (xfs_can_free_eofblocks(ip)) error = xfs_free_eofblocks(ip); goto out; From patchwork Sun Jun 23 05:34:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13708489 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52B5F17C77 for ; Sun, 23 Jun 2024 05:35:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120944; cv=none; b=AvHbLqf0SEzpGerVbzvmpnkKzSEDcXTopJh/kwUvFPgUpJPZqF1cxvDXzhB5zB0sTuZpJEl/wODTJl5spQHa04JKC6XZPizFBBM4gr+vA0uiTa7GQIHrFsVntpKJUaEOZzYfzjw6sNqn/IqyuDIDWOubLZw58LFZ2raFRS2C3zM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120944; c=relaxed/simple; bh=7ImR1cJrLGuYRIdiUrc57pJNv7JRdI8kFzjEElUY9bs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=guVfSRWRnrGfyBKQ/Q9RaPyoc9/AxcaZakhuVA70GEMGjvWkDePFiPgcg6pmg9uYH/AtEvsMX3tY0ZQKiFCRHmcwFk4nA0Ye8RCZOM5aoWTvaqocmZamNaX476BeN5CUJrZeIRf42cZkCJafUbBcjKHqjqzCSqsRtwW7/abOrX4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=2oz9l0mq; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="2oz9l0mq" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=g30x6+i89UtGLI9kG86blJP0PV9XoyqjE8wfLs+XJxQ=; b=2oz9l0mq33YmZXKGKCpCmkpgid YVH0pr4D0wIQb5vutI70n85nWDYb+MPfPxOMPiY4SyWY5Vt+LqTiTsxObCMS4JJtQCX9A7rzRRj93 mgJ//uIJf+5dkZFgyAo+IFBInEwn4HOGiTkbsJvS8L44LCG0O6JL3k7o4Bu+RF7InziG5ta0Tyz2o /aqmhQvCRoA2EMqsS2Ez830Lt25RhzhzO0+L9xYZuSFxQSzV8IqiO0Jv+8xpS0M8jUENG3v5GC/a5 WCknS7dqg8fWxDIge9QPbOi9K6erRIWA5cOGsEW+8yyBJ/9rXkhiDXhc417qUwXws/4SWn/axttDZ odBeKvOQ==; Received: from 2a02-8389-2341-5b80-9456-578d-194f-dacd.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:9456:578d:194f:dacd] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLFt8-0000000DOEU-1CSg; Sun, 23 Jun 2024 05:35:42 +0000 From: Christoph Hellwig To: Chandan Babu R Cc: "Darrick J. Wong" , Dave Chinner , linux-xfs@vger.kernel.org Subject: [PATCH 02/10] xfs: remove the i_mode check in xfs_release Date: Sun, 23 Jun 2024 07:34:47 +0200 Message-ID: <20240623053532.857496-3-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240623053532.857496-1-hch@lst.de> References: <20240623053532.857496-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html xfs_release is only called from xfs_file_release, which is wired up as the f_op->release handler for regular files only. Signed-off-by: Christoph Hellwig Reviewed-by: Darrick J. Wong --- fs/xfs/xfs_inode.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c index 38f946e3be2da3..9a9340aebe9d8a 100644 --- a/fs/xfs/xfs_inode.c +++ b/fs/xfs/xfs_inode.c @@ -1552,9 +1552,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; From patchwork Sun Jun 23 05:34:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13708490 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0902520E3 for ; Sun, 23 Jun 2024 05:35:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120948; cv=none; b=sY2onL4p6stCH6SYRWM7dK+eU8u830wsFABDsxzPxu5DGCA5F92uvlNVURys4GQupZ5jKUkvBbjkWoc5tq8aA6f820QrHLm+Y3O5LZAJIfg9/kwa8JMUDp5cCvxOTH1ESML63wlHDOH2gY/jTZLIN3CI89wR3dQtep/PQMEiG9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120948; c=relaxed/simple; bh=bBA/HgGKIY6hoX+xK8LIKcok/rtW1IPjYFfVJ0egc+Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ArGlNHbk2lzUy8Q3J7x9kCk1JXFShDrjhbU9Q7MbUY7bGMv8Ss27tlcbY8KP62aRdp1+vmmZgamtflO+usqtpy32CT8IUAKEEbqnr8DtwszM/PwnSVlAm0QevDh9oGjgs5ulGAnUgdctmZpy2PSVT7pL2o582vbbrYL5w5reX9U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=kCRt+IlZ; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="kCRt+IlZ" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=LJMlhVtRmAlI2kjRyD+N0FRr1lbhrqC8Pf9zZAMUOog=; b=kCRt+IlZWb+pZV3x4zhPqxMsS9 qPcEApkBGaQRKnYGu3lpuFB3kGPKrnncOwYXFoHxsBSQZ8wSeX/ikiTkmwkdTWgK7rqKhPGnXU7jC ZCJkQW6XojM5tZHjn93iU946YYOw5YZJ743Uj7gGTct/vy3or627FKOxQtQKbqL/d0nbs2zbEdKly TWT111TlM7odzSSY1EvoANEWYz/YhxkSiCrL0avSIqclbwIj9SHnK+hIudmZwjwPQICTpDkgOZd/J 0xmQJ6+HcuawGge7kGhKs6ZVfQ19iVaDfnuV3+vZUf9BHiECKqFlg5ubhGvsQzy6OPGOPwnBi94KX OWIg/Oow==; Received: from 2a02-8389-2341-5b80-9456-578d-194f-dacd.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:9456:578d:194f:dacd] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLFtC-0000000DOEm-0W3U; Sun, 23 Jun 2024 05:35:46 +0000 From: Christoph Hellwig To: Chandan Babu R Cc: "Darrick J. Wong" , Dave Chinner , linux-xfs@vger.kernel.org Subject: [PATCH 03/10] xfs: refactor f_op->release handling Date: Sun, 23 Jun 2024 07:34:48 +0200 Message-ID: <20240623053532.857496-4-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240623053532.857496-1-hch@lst.de> References: <20240623053532.857496-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Currently f_op->release is split in not very obvious ways. Fix that by folding xfs_release into xfs_file_release. Signed-off-by: Christoph Hellwig Reviewed-by: Darrick J. Wong --- 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(-) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index b240ea5241dc9d..d39d0ea522d1c2 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1188,10 +1188,75 @@ xfs_dir_open( STATIC int xfs_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 diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c index 9a9340aebe9d8a..fe4906a08665ee 100644 --- a/fs/xfs/xfs_inode.c +++ b/fs/xfs/xfs_inode.c @@ -1545,85 +1545,6 @@ xfs_itruncate_extents_flags( return error; } -int -xfs_release( - xfs_inode_t *ip) -{ - xfs_mount_t *mp = ip->i_mount; - int error = 0; - - /* If this is a read-only mount, don't do this (would generate I/O) */ - if (xfs_is_readonly(mp)) - return 0; - - if (!xfs_is_shutdown(mp)) { - int truncated; - - /* - * 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. - */ - truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); - if (truncated) { - xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE); - if (ip->i_delayed_blks > 0) { - error = filemap_flush(VFS_I(ip)->i_mapping); - if (error) - return error; - } - } - } - - if (VFS_I(ip)->i_nlink == 0) - return 0; - - /* - * 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 (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) - return 0; - - if (xfs_can_free_eofblocks(ip)) { - /* - * 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. - */ - if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE)) - goto out_unlock; - - error = xfs_free_eofblocks(ip); - if (error) - goto out_unlock; - - /* delalloc blocks after truncation means it really is dirty */ - if (ip->i_delayed_blks) - xfs_iflags_set(ip, XFS_IDIRTY_RELEASE); - } - -out_unlock: - xfs_iunlock(ip, XFS_IOLOCK_EXCL); - return error; -} - /* * Mark all the buffers attached to this directory stale. In theory we should * never be freeing a directory with any blocks at all, but this covers the diff --git a/fs/xfs/xfs_inode.h b/fs/xfs/xfs_inode.h index 292b90b5f2ac84..ae9851226f9913 100644 --- a/fs/xfs/xfs_inode.h +++ b/fs/xfs/xfs_inode.h @@ -513,7 +513,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); From patchwork Sun Jun 23 05:34:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13708491 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE96B20E3 for ; Sun, 23 Jun 2024 05:35:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120952; cv=none; b=gQg5Ll+zy9BiUEJUphLS8fvfgPuN4AtZR+SVd3PjouqdkR/Asntjs+4v5NsJCdcL5oHFuoNAZQIV+7FLLWoRCYphtYC8ToVR7Bq65JtP/MBnzY1DrvgBUk9AIrL61pZ4XWSxVuLU32jFjXK10ZLIDOlDcLNss0Neyo114ATfNvQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120952; c=relaxed/simple; bh=i9n168gM0dDFdc3s9KafIGIjC05KkC8o36NgBEE7FiM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hZ9c5mkVqEt2+YjGB1dJUi6BrBI01WiZQ7gPGXCquVQRgiw3G5m4yqjmsMkfwQCVVzZbqYw4VF59LL7EepDxkrWYOKywXZcnXOh9xE9guTFm54eGRwZmNlQqOMr42c2yDL6gmVISgM6luSBV+k7dORjqHpYFiGh5z55WC8TGZ0g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=x/GqvJvw; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="x/GqvJvw" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=Q9gmdjiWxsfAIqVzBM7T0E2hm79GGb2nIZ8r6MpscLI=; b=x/GqvJvwIyWqPTQuU6ViBNVnks iHibOJQzGJYk9d6GrKekuP1X4ensz4wbCOWst4WozS2x0NqsAY1MlLpJBl5Imgzi0/BssBeLOS4rR Gb5jZg7HfK3+eQ27vdEEMmVEHhAIhiOzEBrUlSNY/uWALL6EZr5PRy8i5kOtrjvY/JiWUbvKgodcG z2P+Xwbix8rSyXa5bpmw7SlU8KK6O+VS7DPXERB/ub8pzziZvt5jb/QyI5lBAmDrfks8UaNGzNPuM k/jTBAHn8KII5R1v8JFecseeaZbreynjI2kvYRW8jF/7Ig8DqVOSaWKcJ3O8eGwqLwqesd1+QmYL/ k4O2bROw==; Received: from 2a02-8389-2341-5b80-9456-578d-194f-dacd.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:9456:578d:194f:dacd] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLFtF-0000000DOFm-3bEU; Sun, 23 Jun 2024 05:35:50 +0000 From: Christoph Hellwig To: Chandan Babu R Cc: "Darrick J. Wong" , Dave Chinner , linux-xfs@vger.kernel.org Subject: [PATCH 04/10] xfs: don't bother returning errors from xfs_file_release Date: Sun, 23 Jun 2024 07:34:49 +0200 Message-ID: <20240623053532.857496-5-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240623053532.857496-1-hch@lst.de> References: <20240623053532.857496-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html While ->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. Signed-off-by: Christoph Hellwig Reviewed-by: Darrick J. Wong --- fs/xfs/xfs_file.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index d39d0ea522d1c2..7b91cbab80da55 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1186,6 +1186,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, @@ -1193,7 +1197,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)) @@ -1211,11 +1214,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); } /* @@ -1249,14 +1249,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 From patchwork Sun Jun 23 05:34:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13708492 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E369320E3 for ; Sun, 23 Jun 2024 05:35:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120956; cv=none; b=XazB0yydJwj0EzFRIOwVZZk3Mi3rCbvYBDHfESVFa7NYlFExykmStwA92jDh7WvlWAjPF00jRCj+oGsQ9ePb26qlb9ptxSbpKIPRCjInpy5qUQj+5Pa4VVVSaXAAdwqHkQ0VZZ5XXyzCil9xSEQUc1B9UunreTwsIyN298vGNgs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120956; c=relaxed/simple; bh=HWabCiX3GTPhrfxrrsppQSueQUNNqBGS0vFCkyD5X9Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ro8CgjCLooW2cYKElvresBHNM90rW/wBO+WCpW8poduWDEuDl5+3Of7tyGOZB2qQvxbG2uOXKdIXElTBcn5dFbZ+gi46lBAslTbkHWlzjsEUUQOP6A5plbCDZDkwQSQ6KRgnj8RWvGlqVI+ZxfcG4hKg0cRhg42uvnzio4CVDx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=sxLv7GS+; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="sxLv7GS+" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=/Wa+SZievx5M4xESTm6kE3zxysjRLotQPJd2n797pJ0=; b=sxLv7GS+H33P7Q0CgwMU+Am/hf JEOv/su0Pynm3s9Gmf57qKK1NjCWLRRlvqtbnPZGbIpCkUj1PSns4jrWR25XYrIG0AUiJqHmYpUrL S5BbkiZVAODUlONcSPK6mQAtKwD92rIcN0ipgvR1SDf73lkuYT8ifkSL9oRVEG69mH/gdzw11xkgO d3nmPur6Dh/D8fIhkszNiKUtH6lrIJdQP7GxLDNsyvapojamSLUK0jmFpVUKf0QXpajHo22XkU2bP 6aEXBiInG5AEFyoisYx00Td79qtRL8/Urka4Nf0yD/5dDfC/5ZTBg8EGIO/UV23kcvDcBMrEKMleA hUQrhY6w==; Received: from 2a02-8389-2341-5b80-9456-578d-194f-dacd.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:9456:578d:194f:dacd] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLFtJ-0000000DOGc-3kLt; Sun, 23 Jun 2024 05:35:54 +0000 From: Christoph Hellwig To: Chandan Babu R Cc: "Darrick J. Wong" , Dave Chinner , linux-xfs@vger.kernel.org Subject: [PATCH 05/10] xfs: skip all of xfs_file_release when shut down Date: Sun, 23 Jun 2024 07:34:50 +0200 Message-ID: <20240623053532.857496-6-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240623053532.857496-1-hch@lst.de> References: <20240623053532.857496-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html There 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_shutdown is called on a shut down file system. Signed-off-by: Christoph Hellwig Reviewed-by: Darrick J. Wong --- fs/xfs/xfs_file.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 7b91cbab80da55..0380e0b1d9c6c7 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1198,8 +1198,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; /* @@ -1211,8 +1214,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); From patchwork Sun Jun 23 05:34:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13708493 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F37720E3 for ; Sun, 23 Jun 2024 05:35:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120960; cv=none; b=DkSX39qbiMsbgt6AKZWY9429a8kZnc2MnQr6llyHebMJC284hWCQOBiTv4B/vngODoxMVwvkjAbgU6tSMoKqqNPf9IbiT8nVF8eKMTrULKCdMXKnigqJo5oKjOzAWSUVE/YcXE8xLHDM6Ku5pWLQIFOwq2GwyMcOApoQc1+PDJs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120960; c=relaxed/simple; bh=FjthGtxobfOfKC1lXtoGcz2HsyNzP4KVwehkR3dVjuY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qCtjY2yydUiKZNyawZsDpQKCxyIUcQdIScfhqX92+CJQmgbNCW5rixYY8aUp+bJW/A8LpdlCpllWhMEdsk7IKn5KRYEb8Vhwc38SgQBkzcDL+Nw2q9OnW9qMoUUtQ+Fx1xJXUcLixWb6H91IMrF3B5LBZlpMkOmPPfVceTx0Uz4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=PL1HP8AU; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="PL1HP8AU" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=kE7LVIEANSoFbS2XZcfOuC0Wpv4C7XA/HLyg/AVpzxU=; b=PL1HP8AUbk8ViQN1FsKOqPDO2W fp2LkKDwLkq++aXCQPrtiluzNAYgl9myZ4Y/eTjdKWIbKq6vLigHSoZVSh6+FW+Ffx2/0atoWl2Bs 4p8QMeilVpzeWxSQGEUrFcLlI2H8pws2FAiUgFWZIO2AVPLCkIr1y6bEVXxGu7oWy52xQeBRwYq+P ilPShF1kSByv+tYQgSercyUS6bHCQhtKpR+MOGTtTidXc95gASBWXZ8M/NOd7hejqsnKvJW+G1Z39 UBIu2/x3nLE1iuXBm2cUK77Vl9teKaXzDT06b8iUMBzdE/+ZzWWNJpoR/BAVFgeU+/XqNaZIoMegp K34cZK/Q==; Received: from 2a02-8389-2341-5b80-9456-578d-194f-dacd.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:9456:578d:194f:dacd] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLFtN-0000000DOHI-3yaV; Sun, 23 Jun 2024 05:35:58 +0000 From: Christoph Hellwig To: Chandan Babu R Cc: "Darrick J. Wong" , Dave Chinner , linux-xfs@vger.kernel.org Subject: [PATCH 06/10] xfs: don't free post-EOF blocks on read close Date: Sun, 23 Jun 2024 07:34:51 +0200 Message-ID: <20240623053532.857496-7-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240623053532.857496-1-hch@lst.de> References: <20240623053532.857496-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html From: Dave Chinner 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 Signed-off-by: Dave Chinner [darrick: wordsmithing, fix commit message] Signed-off-by: Darrick J. Wong [hch: ported to the new ->release code structure] Signed-off-by: Christoph Hellwig Reviewed-by: Darrick J. Wong --- fs/xfs/xfs_file.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 0380e0b1d9c6c7..8d70171678fe24 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1228,12 +1228,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)) { /* From patchwork Sun Jun 23 05:34:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13708494 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65A3520E3 for ; Sun, 23 Jun 2024 05:36:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120964; cv=none; b=pmzL3fXO3f7SqvePq+HKj4Qw0WnSTmfe++Med2FrSFaoq6wua8hP9GDY9QBJjvulOdxCXKEUM/7WOCZbDsAX61c6NQ41Jd58KBoaIdwH7RGZ1kqlyYc1q6s3JQplGH37EMyJs/z7DYDOuYXbfMaEVDsX3SKEDn9eXkD6OT35zho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120964; c=relaxed/simple; bh=Xa8X7DmNmvvBI+3BLcw4+/MYBQrq9Y8mwu6MOtxLbGI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NNJ2OjaI8JH2T9aJZtTvPs1FrsiXHklH1LmSFGQYd4Mke4X2L3plQuI3lJ5K51y0xsd2ADfOIui331kQ5RhqBAt9ug5c0mle+LpgcXMl8jaC2kteCp8gHBcWcRJ9Swkz+jXz2DuJLovIVB9mdjM5jEDv4ZeB5eQ9Nm8N/Vd/NG4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=BhqY2ePQ; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="BhqY2ePQ" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=ciLyMUR+tqJf1kco8npPGSIjJuVU20k7ydH1YSrLfXg=; b=BhqY2ePQG3YgjVkgwJKZvkdlTc 89g35St5v/7YykGDFkllstUSUVgEFa1x8rAhsreDckn3VzzR5Um1cth9Cq70oeVEhW+Pr3csbx4zA DzREYVYHRKpd1PXqmP4uIliES3x/PApaD8E7NXv3s3RGKU7bPE7ZaXKuFg11cG2Nv8x6fwsOcq94u 1ejBoDUAAG3f4DP4tGx+8py4j8HV2hqNV1xXI2t8Bt5JdJz+HiSHNnOlLAWmhx1+mYhAq0sd/Aicg 5E4GuTRxV6jEOg5PkIwCH3mU4/OHsTX+R9GIFa92cQxl5npfPQI+ixKFuzLQdawak47jMvYMg7x7c /s4yJgAQ==; Received: from 2a02-8389-2341-5b80-9456-578d-194f-dacd.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:9456:578d:194f:dacd] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLFtS-0000000DOIP-12sH; Sun, 23 Jun 2024 05:36:03 +0000 From: Christoph Hellwig To: Chandan Babu R Cc: "Darrick J. Wong" , Dave Chinner , linux-xfs@vger.kernel.org Subject: [PATCH 07/10] xfs: only free posteof blocks on first close Date: Sun, 23 Jun 2024 07:34:52 +0200 Message-ID: <20240623053532.857496-8-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240623053532.857496-1-hch@lst.de> References: <20240623053532.857496-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html From: "Darrick J. Wong" 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 [hch: rebased, updated comment, renamed the flag] Signed-off-by: Christoph Hellwig Reviewed-by: Darrick J. Wong --- fs/xfs/xfs_file.c | 32 +++++++++++--------------------- fs/xfs/xfs_inode.h | 4 ++-- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 8d70171678fe24..de52aceabebc27 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1215,15 +1215,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. @@ -1241,25 +1247,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); } diff --git a/fs/xfs/xfs_inode.h b/fs/xfs/xfs_inode.h index ae9851226f9913..548a4f00bcae1b 100644 --- a/fs/xfs/xfs_inode.h +++ b/fs/xfs/xfs_inode.h @@ -336,7 +336,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) @@ -383,7 +383,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) /* From patchwork Sun Jun 23 05:34:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13708495 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E3B120E3 for ; Sun, 23 Jun 2024 05:36:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120968; cv=none; b=XFqfJqQTDOSyE/6W7qKxU1lJ3tB1+luXzLovV6l1OGDn8L2Esn8SNjSBuJ7510b+Zgcxw0Anxhw+Ma6MlIeNI/RC5Z6Di4OUEDi2uLKLgsrb19TRZ+fcITO/MunRb0Qz3WKUUdUjxHja+mefoIB5FDlD1nPCPaPFi7qKOqPB+KI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120968; c=relaxed/simple; bh=xRwmoBrlTg/slmWpuDbPbxHOgFDrU5aR+P4B/7aRSCk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F1zRVDbw1v+NGZvpQtp7V+R5zkGcS1kbq6m5wrVFM7q/4NHAyGqO0Z+UYok2caroV63aNb1v5gMMBZ3SNHy8GbEKrYLpoCX34p+7TBGYVAgNsdH3+Gu874DV/z7Ak0r5WRYL8Ehl5aFJJc6YFleCTOzL4vQCmRc1Vajh+SxqMH8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=iv4RBopK; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="iv4RBopK" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=kn/Xqb36AvnmWyyN1R6nV4nu5NbV3uIfPpwFkKsFJws=; b=iv4RBopKsNlyuYpmT3rnpWe2BA AvaUPG4+Q9JaKktN983Vanab56TlERhQV55AeYuM4NLrA/L2KjC8WCDMnOdJBygSxAaGFfW9ldHsr lkDQTBRr/2WscDIpKB3RX0+XxGrUkPTtzxdztNGiYVwvEI//QIErE4VeuKuMsD6RTcwaqwE5Jah2M v/HUqTCvsd+uLohrFgRuFwSTAICvj32kE8rXvIDBFUzCb2Lw1DZgD/lZ2OZNXRD7kqd0zP4qf9Zl6 ZFaP3sisAozmNwp1O23lV/I1MVzgAZEWjavML96/FQg/a/69vUKarbCHjPyQS9C4N6FG10QD9x7RI ZBCq7okA==; Received: from 2a02-8389-2341-5b80-9456-578d-194f-dacd.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:9456:578d:194f:dacd] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLFtW-0000000DOJa-1iVD; Sun, 23 Jun 2024 05:36:06 +0000 From: Christoph Hellwig To: Chandan Babu R Cc: "Darrick J. Wong" , Dave Chinner , linux-xfs@vger.kernel.org Subject: [PATCH 08/10] xfs: check XFS_IDIRTY_RELEASE earlier in xfs_release_eofblocks Date: Sun, 23 Jun 2024 07:34:53 +0200 Message-ID: <20240623053532.857496-9-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240623053532.857496-1-hch@lst.de> References: <20240623053532.857496-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html If the XFS_IDIRTY_RELEASE 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. Signed-off-by: Christoph Hellwig Reviewed-by: Darrick J. Wong --- fs/xfs/xfs_file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index de52aceabebc27..1903fa5568a37d 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1245,9 +1245,9 @@ 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)) { + if (xfs_can_free_eofblocks(ip)) { xfs_free_eofblocks(ip); xfs_iflags_set(ip, XFS_EOFBLOCKS_RELEASED); } From patchwork Sun Jun 23 05:34:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13708496 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1CB9120E3 for ; Sun, 23 Jun 2024 05:36:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120972; cv=none; b=OqERdCae2Bk8QW0pCXdl7JJFvzcWfOrgDoe819hfMYII5sGl/FC0OQfs2IlLhECbIq2k0p0Mko3LrpS8zS81YnbdBICzrVCofrKT+yOpVN5gnyXwasm8P1Dhy7e8dkqKIEkfWN0t2yRbOqyT2+InnxkgQtoHqR9oMjjBau443CA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120972; c=relaxed/simple; bh=Xl7ZhTdSH0rx5ES25zn63cZGzL6lLalKtCJB0Xp8yBE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gTEfBWeMnOR3f+6Lwh2igm5rHdn5MFwlflPF8j5/+MRyz38UBq3cWucgDoejxVFQJl2kc9C9UstIMGs0S+EAl/L976FbNwi12VC1QAEe9bm1ijEb+pxTLvYqP8+Nn9K5anCiJiFkoiWskmd0zUC3sB7II861h17FtkuBpaxFXmQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=mIl+XF/y; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="mIl+XF/y" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=B7G2rHwczb0XBn9nGkvhOKsTeqaDrTu+fOpTyinDvHs=; b=mIl+XF/y/wax1SkNhmjVCTFt9t O5Gdv8+mKwzNorrNTbZZyRVdi6QaVC/uiuBnJgA2/hrKSF2XfPQYvpHEZ0oeeobllSFnjTdJbg9/X ZbRvG98K4z5Y4CAC0edsSWvln4JU4ngfsKO29MNBIwPdU4BRsPQIs61CrcfliPtGcFLjxCrN8yzoE snjs4+Zsk8zNCxAvy4B41z+LSm7lyObU/qS8vyGr5YG7vOrSHZL6we9h1KNGcqmKgP5I+0ZmBGIJ1 /IgREoU37kgHcc7QuNyuS7Atcl7xBt+/dBw+A/2Nxm/qavGw1a0dST8v+Oyz99v/Tr4O3P+sMFMc0 Zjh42+kg==; Received: from 2a02-8389-2341-5b80-9456-578d-194f-dacd.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:9456:578d:194f:dacd] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLFta-0000000DOJv-0za1; Sun, 23 Jun 2024 05:36:10 +0000 From: Christoph Hellwig To: Chandan Babu R Cc: "Darrick J. Wong" , Dave Chinner , linux-xfs@vger.kernel.org Subject: [PATCH 09/10] xfs: simplify extent lookup in xfs_can_free_eofblocks Date: Sun, 23 Jun 2024 07:34:54 +0200 Message-ID: <20240623053532.857496-10-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240623053532.857496-1-hch@lst.de> References: <20240623053532.857496-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html xfs_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. Signed-off-by: Christoph Hellwig Reviewed-by: Darrick J. Wong --- fs/xfs/xfs_bmap_util.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/fs/xfs/xfs_bmap_util.c b/fs/xfs/xfs_bmap_util.c index a4d9fbc21b8343..52863b784b023f 100644 --- a/fs/xfs/xfs_bmap_util.c +++ b/fs/xfs/xfs_bmap_util.c @@ -492,12 +492,12 @@ bool xfs_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; } /* From patchwork Sun Jun 23 05:34:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13708497 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B7A420E3 for ; Sun, 23 Jun 2024 05:36:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120975; cv=none; b=b1HQmeHwD5l1KipyKaACtE4nWz87tvONVPZqVi8iHzgEl04LiRedm8m+VH8cpBXUYPoDTQuNfYnrhLtHxHGw2ODI4wOltXl/43Svzqv313zm3IHvB+9vJPqhy7yhaTrj6arkfBApe47To0W9Tp+Ryfz5O1VIFy6dEOM1lB/Mgo4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719120975; c=relaxed/simple; bh=WqjiBkyJ/60e0YSpL9HzdpggI73cWZ10IYoqiICgLfI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MGTd1eJ+EAUpuO2IBMcV8tmc9Tf3qDkqTDDEnfQxWOAlsITk06g21qZgRYDL49xaqXbnv+ATljCuxyCdcEAkhXFzdr81JznCXd/LVCbZAMo/anXA7n3nWIm54fk8TJtlCayBZ9N/0KwV3zWJDhMitayJkuNkEfJ8AXYNt2K/E/8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=yyOW0WEf; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="yyOW0WEf" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=oOXV51C19r2ZeIRrSLS8NeZk0+Avh6JI8o9XXm0djkM=; b=yyOW0WEfe8TciTo4kyPiEABfVA L9iT627F7EtNIyflfIvxZ8VcPx7fMCCWL0ly4OwGVk1+da1vUT1pXbUML531xkCp7qggmQHb80qR5 wI+ywbsMb5lbb7rgGIhsGRJAOW9xObdx+gYhrRcCypZUrlmcadEptUzllgUXcu5NNjzRiy9JtyUY9 0Zv1FHDM1YcUQISXXTVzfZLyWUKdBF+yCp/ZOczDXp2LodG9VJl6fVXWyDBzLIKJtSewRrfk+MhQc vrbrXjaknqmktwB0GsIKoBA35+JJ81vG2OxXZMSrvU5+j+K4HqPxA9+exQr/ucxqo5Zl2iZ0UJmSs bAcJr8rg==; Received: from 2a02-8389-2341-5b80-9456-578d-194f-dacd.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:9456:578d:194f:dacd] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLFtd-0000000DOKq-1j94; Sun, 23 Jun 2024 05:36:13 +0000 From: Christoph Hellwig To: Chandan Babu R Cc: "Darrick J. Wong" , Dave Chinner , linux-xfs@vger.kernel.org Subject: [PATCH 10/10] xfs: reclaim speculative preallocations for append only files Date: Sun, 23 Jun 2024 07:34:55 +0200 Message-ID: <20240623053532.857496-11-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240623053532.857496-1-hch@lst.de> References: <20240623053532.857496-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html The 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) late 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 file aren't expected to be written to only once. Signed-off-by: Christoph Hellwig Reviewed-by: Darrick J. Wong --- 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(-) diff --git a/fs/xfs/xfs_bmap_util.c b/fs/xfs/xfs_bmap_util.c index 52863b784b023f..aa924d7cd32abd 100644 --- a/fs/xfs/xfs_bmap_util.c +++ b/fs/xfs/xfs_bmap_util.c @@ -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 diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 1903fa5568a37d..b05822a70ea680 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1231,6 +1231,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 flags is rather pointless for inodes written oly 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. * @@ -1245,6 +1248,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)) { diff --git a/fs/xfs/xfs_icache.c b/fs/xfs/xfs_icache.c index 9967334ea99f1a..0f07ec842b7023 100644 --- a/fs/xfs/xfs_icache.c +++ b/fs/xfs/xfs_icache.c @@ -1158,7 +1158,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;