From nobody Wed Nov 27 10:26:47 2024 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 1A83680035; Thu, 10 Oct 2024 13:35:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567346; cv=none; b=bRK3nFUb7aBVtkWnbVeaPRN7UTbxvbOq3w31KnCS7my4WCq0bUjSFqYVzNEATFa/B8/s3RimRXlCt8igcUgnslonI/o2F1f+osaBG310rPSnpLbrb0ilhBwiTlbUQ/Vnyt2IIbfgZ1SInSLg/QHO+avgEIwZVZSV7tYGCr5Yjzw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567346; c=relaxed/simple; bh=VdJ9IMIjtQwr5Ztqa838ADogQtEwKITr1AZqEGXblPA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=A7xgkALRFEviMb/LR3VSsL9jRt/Du8ylnvrKBHSiSzc1sDQ+VYwjiXQaU8ElCDTLuZZqxgET2P3IIbhCozUDztDzcfHKpKhuyUl9TOU904MyGb/m1uPKOzBRn4bnc5mmh0xLp7MBEgnt+HUfzwiMIhp1QzSyQ4KMpNoqa+nfxWA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=none smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4XPW3P1dxHz4f3jtS; Thu, 10 Oct 2024 21:35:29 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 094761A0568; Thu, 10 Oct 2024 21:35:41 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCXysYc2AdnDRnZDg--.21356S5; Thu, 10 Oct 2024 21:35:40 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: [PATCH v3 01/10] ext4: remove writable userspace mappings before truncating page cache Date: Thu, 10 Oct 2024 21:33:24 +0800 Message-Id: <20241010133333.146793-2-yi.zhang@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241010133333.146793-1-yi.zhang@huawei.com> References: <20241010133333.146793-1-yi.zhang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCXysYc2AdnDRnZDg--.21356S5 X-Coremail-Antispam: 1UD129KBjvJXoWxXFyrJF43Xw18CFy5Gry5XFb_yoWrAr43pr 9rGFyfCrWrZasrWa1Sg3WUZw1rK3WkCF4UJ34fWr1UXFy8X3WkKF1Dtw1jyF4UtrW8Jr4j vF45trWjgF45A3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUHYb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUGw A2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7Iv64x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YV CY1x02628vn2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCF04k2 0xvEw4C26cxK6c8Ij28IcwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r 1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij 64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr 0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF 0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07jeg4hUUUUU= Sender: yi.zhang@huaweicloud.com X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" When zeroing a range of folios on the filesystem which block size is less than the page size, the file's mapped partial blocks within one page will be marked as unwritten, we should remove writable userspace mappings to ensure that ext4_page_mkwrite() can be called during subsequent write access to these folios. Otherwise, data written by subsequent mmap writes may not be saved to disk. $mkfs.ext4 -b 1024 /dev/vdb $mount /dev/vdb /mnt $xfs_io -t -f -c "pwrite -S 0x58 0 4096" -c "mmap -rw 0 4096" \ -c "mwrite -S 0x5a 2048 2048" -c "fzero 2048 2048" \ -c "mwrite -S 0x59 2048 2048" -c "close" /mnt/foo $od -Ax -t x1z /mnt/foo 000000 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 * 000800 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 * 001000 $umount /mnt && mount /dev/vdb /mnt $od -Ax -t x1z /mnt/foo 000000 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 * 000800 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 * 001000 Signed-off-by: Zhang Yi --- fs/ext4/ext4.h | 2 ++ fs/ext4/extents.c | 1 + fs/ext4/inode.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 44b0d418143c..6d0267afd4c1 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -3020,6 +3020,8 @@ extern int ext4_inode_attach_jinode(struct inode *ino= de); extern int ext4_can_truncate(struct inode *inode); extern int ext4_truncate(struct inode *); extern int ext4_break_layouts(struct inode *); +extern void ext4_truncate_folios_range(struct inode *inode, loff_t start, + loff_t end); extern int ext4_punch_hole(struct file *file, loff_t offset, loff_t length= ); extern void ext4_set_inode_flags(struct inode *, bool init); extern int ext4_alloc_da_blocks(struct inode *inode); diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 34e25eee6521..2a054c3689f0 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -4677,6 +4677,7 @@ static long ext4_zero_range(struct file *file, loff_t= offset, } =20 /* Now release the pages and zero block aligned part of pages */ + ext4_truncate_folios_range(inode, start, end); truncate_pagecache_range(inode, start, end - 1); inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); =20 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 54bdd4884fe6..8b34e79112d5 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -3870,6 +3871,46 @@ int ext4_update_disksize_before_punch(struct inode *= inode, loff_t offset, return ret; } =20 +static inline void ext4_truncate_folio(struct inode *inode, + loff_t start, loff_t end) +{ + unsigned long blocksize =3D i_blocksize(inode); + struct folio *folio; + + if (round_up(start, blocksize) >=3D round_down(end, blocksize)) + return; + + folio =3D filemap_lock_folio(inode->i_mapping, start >> PAGE_SHIFT); + if (IS_ERR(folio)) + return; + + if (folio_mkclean(folio)) + folio_mark_dirty(folio); + folio_unlock(folio); + folio_put(folio); +} + +/* + * When truncating a range of folios, if the block size is less than the + * page size, the file's mapped partial blocks within one page could be + * freed or converted to unwritten. We should call this function to remove + * writable userspace mappings so that ext4_page_mkwrite() can be called + * during subsequent write access to these folios. + */ +void ext4_truncate_folios_range(struct inode *inode, loff_t start, loff_t = end) +{ + unsigned long blocksize =3D i_blocksize(inode); + + if (end > inode->i_size) + end =3D inode->i_size; + if (start >=3D end || blocksize >=3D PAGE_SIZE) + return; + + ext4_truncate_folio(inode, start, min(round_up(start, PAGE_SIZE), end)); + if (end > round_up(start, PAGE_SIZE)) + ext4_truncate_folio(inode, round_down(end, PAGE_SIZE), end); +} + static void ext4_wait_dax_page(struct inode *inode) { filemap_invalidate_unlock(inode->i_mapping); --=20 2.39.2 From nobody Wed Nov 27 10:26:47 2024 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 54C311C7B77; Thu, 10 Oct 2024 13:35:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567348; cv=none; b=RnUYp5MWkZO9bLYsF4hgqJGFqihWvJ8NJMdcaze54E2tRUN/kfPC1e44hb+0f1EHFRUY49BQJi97/LO75P+PL1DStWNiQ2BNi8HBQgDxQuZYm/qx/cxZqj3LTZpjPeVp8+9LPu7/ul3+HyIKqBTjrjOLLhthn5zJGf2OT+6Rbks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567348; c=relaxed/simple; bh=4IZNTRA8M7DbveMxDNcfOFDsl0aJRwoDO3R5+zHPszs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NdEQhkwdZDdlq19s92lmp0CZIlFAUs/LkAQhbB/954QEaFmhvZ3HBzPFBnJnriWOgTRfqTl/wZSG2tm0JOUsX+Emkpnb+IBz48CDHWffZBJvECK4XcQTqxoH6mXMt4tlz+IKwcKlh504vyJu6IWCvA7MfgT+u4mJOgpgtcXguw8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4XPW3H62Nhz4f3lDN; Thu, 10 Oct 2024 21:35:23 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 788381A0A22; Thu, 10 Oct 2024 21:35:41 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCXysYc2AdnDRnZDg--.21356S6; Thu, 10 Oct 2024 21:35:41 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: [PATCH v3 02/10] ext4: don't explicit update times in ext4_fallocate() Date: Thu, 10 Oct 2024 21:33:25 +0800 Message-Id: <20241010133333.146793-3-yi.zhang@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241010133333.146793-1-yi.zhang@huawei.com> References: <20241010133333.146793-1-yi.zhang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCXysYc2AdnDRnZDg--.21356S6 X-Coremail-Antispam: 1UD129KBjvJXoWxZFyDZr13KFy8urW5KF48JFb_yoW5GrWUpr WrGa4rG3W8WFyq9rWFkr4UZrn7K3W7Gr48XrZ5u3yxua4DJw1vgF1YyFySyF4rtrW8Zr4j vFWUKw1UWw1Uu37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUHYb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7Iv64x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YV CY1x02628vn2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCF04k2 0xvEw4C26cxK6c8Ij28IcwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r 1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij 64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr 0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF 0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07UCiihUUUUU= Sender: yi.zhang@huaweicloud.com X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" After commit 'ad5cd4f4ee4d ("ext4: fix fallocate to use file_modified to update permissions consistently"), we can update mtime and ctime appropriately through file_modified() when doing zero range, collapse rage, insert range and punch hole, hence there is no need to explicit update times in those paths, just drop them. Signed-off-by: Zhang Yi Reviewed-by: Jan Kara --- fs/ext4/extents.c | 4 ---- fs/ext4/inode.c | 1 - 2 files changed, 5 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 2a054c3689f0..aa07b5ddaff8 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -4679,7 +4679,6 @@ static long ext4_zero_range(struct file *file, loff_t= offset, /* Now release the pages and zero block aligned part of pages */ ext4_truncate_folios_range(inode, start, end); truncate_pagecache_range(inode, start, end - 1); - inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); =20 ret =3D ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags); @@ -4704,7 +4703,6 @@ static long ext4_zero_range(struct file *file, loff_t= offset, goto out_mutex; } =20 - inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); if (new_size) ext4_update_inode_size(inode, new_size); ret =3D ext4_mark_inode_dirty(handle, inode); @@ -5440,7 +5438,6 @@ static int ext4_collapse_range(struct file *file, lof= f_t offset, loff_t len) up_write(&EXT4_I(inode)->i_data_sem); if (IS_SYNC(inode)) ext4_handle_sync(handle); - inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); ret =3D ext4_mark_inode_dirty(handle, inode); ext4_update_inode_fsync_trans(handle, inode, 1); =20 @@ -5550,7 +5547,6 @@ static int ext4_insert_range(struct file *file, loff_= t offset, loff_t len) /* Expand file to avoid data loss if there is error while shifting */ inode->i_size +=3D len; EXT4_I(inode)->i_disksize +=3D len; - inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); ret =3D ext4_mark_inode_dirty(handle, inode); if (ret) goto out_stop; diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 8b34e79112d5..f8796f7b0f94 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -4085,7 +4085,6 @@ int ext4_punch_hole(struct file *file, loff_t offset,= loff_t length) if (IS_SYNC(inode)) ext4_handle_sync(handle); =20 - inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); ret2 =3D ext4_mark_inode_dirty(handle, inode); if (unlikely(ret2)) ret =3D ret2; --=20 2.39.2 From nobody Wed Nov 27 10:26:47 2024 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 D97C51C3F34; Thu, 10 Oct 2024 13:35:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567347; cv=none; b=niI+tup7fuWo4BTQPHwf12LGamCq0UhafonDW7/fKSN+K67BzBB9S5NBKGjw4durcAv5ydcfyqxArLyaAWAK6nF4cSNAfkRyk4D8tzUM2OIwTmyTP4rSaE0GjH+vtoP7F8PbElxbzOPnOF9EP2oIKvtchKgR+A/6jsLDLD5lNOY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567347; c=relaxed/simple; bh=MxLOjZGAOSC9szCGPQl/dXlDGFC5Y8rm8OWUupTbMK4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=srNcCrKEOW45bLuMNfEDxzfM0PxYISk6B/EnX5uly26sscWV5VPKEU8iaXFI722yZxC20JMVr1QqyJmWuB3NQFzEVeMax0ft+GrdnTjhpUj51sBCTYp1aetlAl05puPW5ls1yoGBDw8ri376rdI2bfqLL/h4vfV8XwxaSyxNgcA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4XPW3Q13fqz4f3k6h; Thu, 10 Oct 2024 21:35:30 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id E9BD31A0359; Thu, 10 Oct 2024 21:35:41 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCXysYc2AdnDRnZDg--.21356S7; Thu, 10 Oct 2024 21:35:41 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: [PATCH v3 03/10] ext4: don't write back data before punch hole in nojournal mode Date: Thu, 10 Oct 2024 21:33:26 +0800 Message-Id: <20241010133333.146793-4-yi.zhang@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241010133333.146793-1-yi.zhang@huawei.com> References: <20241010133333.146793-1-yi.zhang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCXysYc2AdnDRnZDg--.21356S7 X-Coremail-Antispam: 1UD129KBjvJXoW7WF1DWr4UAry8Kr1rCF13twb_yoW8Cw1kpr 9IkrW5tF48WFZFkr4SqFsrXF1FgaykCrW8WFyxG3s7WayUAwn7KF4q9F18Ga1UtrZxArWj qF40qa4xWFyUZFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUHYb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUWw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7Iv64x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YV CY1x02628vn2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCF04k2 0xvEw4C26cxK6c8Ij28IcwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r 1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij 64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr 0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF 0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07UAEfOUUUUU= Sender: yi.zhang@huaweicloud.com X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" There is no need to write back all data before punching a hole in data=3Dordered|writeback mode since it will be dropped soon after removing space, so just remove the filemap_write_and_wait_range() in these modes. However, in data=3Djournal mode, we need to write dirty pages out before discarding page cache in case of crash before committing the freeing data transaction, which could expose old, stale data. Signed-off-by: Zhang Yi --- fs/ext4/inode.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index f8796f7b0f94..94b923afcd9c 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -3965,17 +3965,6 @@ int ext4_punch_hole(struct file *file, loff_t offset= , loff_t length) =20 trace_ext4_punch_hole(inode, offset, length, 0); =20 - /* - * Write out all dirty pages to avoid race conditions - * Then release them. - */ - if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { - ret =3D filemap_write_and_wait_range(mapping, offset, - offset + length - 1); - if (ret) - return ret; - } - inode_lock(inode); =20 /* No need to punch hole beyond i_size */ @@ -4037,6 +4026,21 @@ int ext4_punch_hole(struct file *file, loff_t offset= , loff_t length) ret =3D ext4_update_disksize_before_punch(inode, offset, length); if (ret) goto out_dio; + + /* + * For journalled data we need to write (and checkpoint) pages + * before discarding page cache to avoid inconsitent data on + * disk in case of crash before punching trans is committed. + */ + if (ext4_should_journal_data(inode)) { + ret =3D filemap_write_and_wait_range(mapping, + first_block_offset, last_block_offset); + if (ret) + goto out_dio; + } + + ext4_truncate_folios_range(inode, first_block_offset, + last_block_offset + 1); truncate_pagecache_range(inode, first_block_offset, last_block_offset); } --=20 2.39.2 From nobody Wed Nov 27 10:26:47 2024 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 54BE11C4625; Thu, 10 Oct 2024 13:35:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567347; cv=none; b=eoJnE8tKMWrNE8ZsjWYk7q89BQ5G4vSivL+GEsPmSzNdh7VUQHHph+ddITEHYrEuM0VlKbC6zD5Jk6kZdFfH4Dgg25Mau06kQhiiNkpKwW9kA1KXUWrCIWQupSXm3d+jY3cv5afcrpsxzr6NOi9tMbNab7wkxUEAkarDN/+zxT0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567347; c=relaxed/simple; bh=stsrva27qYMVu8ViiEei3hO+iSfuJcFVSbZW4duvV7I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AlsAdP5oIFBAOMdCEww1iLLMHiZ225Eb05ciSdq/HXQdLZJ0DYQ0G1rVlJIq3aZRiCZheq/LJcBJeozhhs7DOcYyo6Q4+4lTkieMOjds8oI2zS7zpVRxArKS3p73Ha5ZoRdFzVhv7r0BPdymQJPBRCsdq4iwHZuLfgHej7EynNs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=none smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4XPW3J5QrYz4f3kw2; Thu, 10 Oct 2024 21:35:24 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 644171A06D7; Thu, 10 Oct 2024 21:35:42 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCXysYc2AdnDRnZDg--.21356S8; Thu, 10 Oct 2024 21:35:42 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: [PATCH v3 04/10] ext4: refactor ext4_punch_hole() Date: Thu, 10 Oct 2024 21:33:27 +0800 Message-Id: <20241010133333.146793-5-yi.zhang@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241010133333.146793-1-yi.zhang@huawei.com> References: <20241010133333.146793-1-yi.zhang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCXysYc2AdnDRnZDg--.21356S8 X-Coremail-Antispam: 1UD129KBjvJXoW3GF48tFWrGF1fAF17GFWfKrg_yoW3GrW8p3 y3Ary5Kr48WFyq9F4Iqr4DXF1Ik3WDKrWUWryxGr1fX34qyw1IgFs0kF1Fgay5trZrZr4j vF45t347WryUuFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUHab4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7Iv64x0x7Aq67IIx4CEVc8vx2IErcIFxwACI4 02YVCY1x02628vn2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCF 04k20xvEw4C26cxK6c8Ij28IcwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14 v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkG c2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8 JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUFPETDU UUU Sender: yi.zhang@huaweicloud.com X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" The current implementation of ext4_punch_hole() contains complex position calculations and stale error tags. To improve the code's clarity and maintainability, it is essential to clean up the code and improve its readability, this can be achieved by: a) simplifying and renaming variables; b) eliminating unnecessary position calculations; c) writing back all data in data=3Djournal mode, and drop page cache from the original offset to the end, rather than using aligned blocks, d) renaming the stale error tags. Signed-off-by: Zhang Yi --- fs/ext4/inode.c | 140 +++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 78 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 94b923afcd9c..1d128333bd06 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -3955,13 +3955,14 @@ int ext4_punch_hole(struct file *file, loff_t offse= t, loff_t length) { struct inode *inode =3D file_inode(file); struct super_block *sb =3D inode->i_sb; - ext4_lblk_t first_block, stop_block; + ext4_lblk_t start_lblk, end_lblk; struct address_space *mapping =3D inode->i_mapping; - loff_t first_block_offset, last_block_offset, max_length; - struct ext4_sb_info *sbi =3D EXT4_SB(inode->i_sb); + loff_t max_end =3D EXT4_SB(sb)->s_bitmap_maxbytes - sb->s_blocksize; + loff_t end =3D offset + length; + unsigned long blocksize =3D i_blocksize(inode); handle_t *handle; unsigned int credits; - int ret =3D 0, ret2 =3D 0; + int ret =3D 0; =20 trace_ext4_punch_hole(inode, offset, length, 0); =20 @@ -3969,36 +3970,27 @@ int ext4_punch_hole(struct file *file, loff_t offse= t, loff_t length) =20 /* No need to punch hole beyond i_size */ if (offset >=3D inode->i_size) - goto out_mutex; + goto out; =20 /* - * If the hole extends beyond i_size, set the hole - * to end after the page that contains i_size + * If the hole extends beyond i_size, set the hole to end after + * the page that contains i_size, and also make sure that the hole + * within one block before last range. */ - if (offset + length > inode->i_size) { - length =3D inode->i_size + - PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - - offset; - } + if (end > inode->i_size) + end =3D round_up(inode->i_size, PAGE_SIZE); + if (end > max_end) + end =3D max_end; + length =3D end - offset; =20 /* - * For punch hole the length + offset needs to be within one block - * before last range. Adjust the length if it goes beyond that limit. + * Attach jinode to inode for jbd2 if we do any zeroing of partial + * block. */ - max_length =3D sbi->s_bitmap_maxbytes - inode->i_sb->s_blocksize; - if (offset + length > max_length) - length =3D max_length - offset; - - if (offset & (sb->s_blocksize - 1) || - (offset + length) & (sb->s_blocksize - 1)) { - /* - * Attach jinode to inode for jbd2 if we do any zeroing of - * partial block - */ + if (offset & (blocksize - 1) || end & (blocksize - 1)) { ret =3D ext4_inode_attach_jinode(inode); if (ret < 0) - goto out_mutex; - + goto out; } =20 /* Wait all existing dio workers, newcomers will block on i_rwsem */ @@ -4006,7 +3998,7 @@ int ext4_punch_hole(struct file *file, loff_t offset,= loff_t length) =20 ret =3D file_modified(file); if (ret) - goto out_mutex; + goto out; =20 /* * Prevent page faults from reinstantiating pages we have released from @@ -4016,34 +4008,24 @@ int ext4_punch_hole(struct file *file, loff_t offse= t, loff_t length) =20 ret =3D ext4_break_layouts(inode); if (ret) - goto out_dio; - - first_block_offset =3D round_up(offset, sb->s_blocksize); - last_block_offset =3D round_down((offset + length), sb->s_blocksize) - 1; + goto out_invalidate_lock; =20 - /* Now release the pages and zero block aligned part of pages*/ - if (last_block_offset > first_block_offset) { + /* + * For journalled data we need to write (and checkpoint) pages + * before discarding page cache to avoid inconsitent data on + * disk in case of crash before punching trans is committed. + */ + if (ext4_should_journal_data(inode)) { + ret =3D filemap_write_and_wait_range(mapping, offset, end - 1); + } else { ret =3D ext4_update_disksize_before_punch(inode, offset, length); - if (ret) - goto out_dio; - - /* - * For journalled data we need to write (and checkpoint) pages - * before discarding page cache to avoid inconsitent data on - * disk in case of crash before punching trans is committed. - */ - if (ext4_should_journal_data(inode)) { - ret =3D filemap_write_and_wait_range(mapping, - first_block_offset, last_block_offset); - if (ret) - goto out_dio; - } - - ext4_truncate_folios_range(inode, first_block_offset, - last_block_offset + 1); - truncate_pagecache_range(inode, first_block_offset, - last_block_offset); + ext4_truncate_folios_range(inode, offset, end); } + if (ret) + goto out_invalidate_lock; + + /* Now release the pages and zero block aligned part of pages*/ + truncate_pagecache_range(inode, offset, end - 1); =20 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) credits =3D ext4_writepage_trans_blocks(inode); @@ -4053,52 +4035,54 @@ int ext4_punch_hole(struct file *file, loff_t offse= t, loff_t length) if (IS_ERR(handle)) { ret =3D PTR_ERR(handle); ext4_std_error(sb, ret); - goto out_dio; + goto out_invalidate_lock; } =20 - ret =3D ext4_zero_partial_blocks(handle, inode, offset, - length); + ret =3D ext4_zero_partial_blocks(handle, inode, offset, length); if (ret) - goto out_stop; - - first_block =3D (offset + sb->s_blocksize - 1) >> - EXT4_BLOCK_SIZE_BITS(sb); - stop_block =3D (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); + goto out_handle; =20 /* If there are blocks to remove, do it */ - if (stop_block > first_block) { - ext4_lblk_t hole_len =3D stop_block - first_block; + start_lblk =3D round_up(offset, blocksize) >> inode->i_blkbits; + end_lblk =3D end >> inode->i_blkbits; + + if (end_lblk > start_lblk) { + ext4_lblk_t hole_len =3D end_lblk - start_lblk; =20 down_write(&EXT4_I(inode)->i_data_sem); ext4_discard_preallocations(inode); =20 - ext4_es_remove_extent(inode, first_block, hole_len); + ext4_es_remove_extent(inode, start_lblk, hole_len); =20 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) - ret =3D ext4_ext_remove_space(inode, first_block, - stop_block - 1); + ret =3D ext4_ext_remove_space(inode, start_lblk, + end_lblk - 1); else - ret =3D ext4_ind_remove_space(handle, inode, first_block, - stop_block); + ret =3D ext4_ind_remove_space(handle, inode, start_lblk, + end_lblk); + if (ret) { + up_write(&EXT4_I(inode)->i_data_sem); + goto out_handle; + } =20 - ext4_es_insert_extent(inode, first_block, hole_len, ~0, + ext4_es_insert_extent(inode, start_lblk, hole_len, ~0, EXTENT_STATUS_HOLE, 0); up_write(&EXT4_I(inode)->i_data_sem); } - ext4_fc_track_range(handle, inode, first_block, stop_block); + ext4_fc_track_range(handle, inode, start_lblk, end_lblk); + + ret =3D ext4_mark_inode_dirty(handle, inode); + if (unlikely(ret)) + goto out_handle; + + ext4_update_inode_fsync_trans(handle, inode, 1); if (IS_SYNC(inode)) ext4_handle_sync(handle); - - ret2 =3D ext4_mark_inode_dirty(handle, inode); - if (unlikely(ret2)) - ret =3D ret2; - if (ret >=3D 0) - ext4_update_inode_fsync_trans(handle, inode, 1); -out_stop: +out_handle: ext4_journal_stop(handle); -out_dio: +out_invalidate_lock: filemap_invalidate_unlock(mapping); -out_mutex: +out: inode_unlock(inode); return ret; } --=20 2.39.2 From nobody Wed Nov 27 10:26:47 2024 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (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 2BA741C7B8D; Thu, 10 Oct 2024 13:35:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567349; cv=none; b=cCNDEWDIjmuV7mua52g2Yx9opqP8v6RB3CLvDmCfwvFd6QRzBrLwqFqdTajA5M0s+c/Yo6sMS1c2vdTDYtdXfe+1h8tPvCcSwRJRwHQnXdhIdD945DSnuiwnJXYhSh31PNF2fx74zisq5llySwkx7Y6lPYQimaXGjpzzprc+WMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567349; c=relaxed/simple; bh=MMTmuru0uSXetyI199H8UvzbW3ycYcsIf8+FqX5Rajk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sC+eyzgRzyOoJ36dkZZUBPJt5Vxtrb8Ryp8xK21I49c1FYUj8kUx10aU1gzDNtucfSELDJjHXWvLYBfuO38xkDLUHtkNRK5SAMfB1aEXJUBJNu3AKU6lkpJMiVmQmMuhL37NygJVxnXeFLeRa9Zfoc2V5NENQA3bLihbLGjqzAs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=none smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4XPW3K6yC1z4f3jsL; Thu, 10 Oct 2024 21:35:25 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id D3FA91A058E; Thu, 10 Oct 2024 21:35:42 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCXysYc2AdnDRnZDg--.21356S9; Thu, 10 Oct 2024 21:35:42 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: [PATCH v3 05/10] ext4: refactor ext4_zero_range() Date: Thu, 10 Oct 2024 21:33:28 +0800 Message-Id: <20241010133333.146793-6-yi.zhang@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241010133333.146793-1-yi.zhang@huawei.com> References: <20241010133333.146793-1-yi.zhang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCXysYc2AdnDRnZDg--.21356S9 X-Coremail-Antispam: 1UD129KBjvJXoW3JFWrGr4DWF1UGr13Jr1fJFb_yoW3Gr1DpF ZIqrW5Kr4xWFyq9r48KFs7ZF40k3WkKrWUCFyxWrn5X3srtwn7Kan0kF95WFWIqrZ7Zw4Y vF4Yyry7GFWUWFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUHab4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7Iv64x0x7Aq67IIx4CEVc8vx2IErcIFxwACI4 02YVCY1x02628vn2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCF 04k20xvEw4C26cxK6c8Ij28IcwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14 v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkG c2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI42IY6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8 JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUFPETDU UUU Sender: yi.zhang@huaweicloud.com X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" The current implementation of ext4_zero_range() contains complex position calculations and stale error tags. To improve the code's clarity and maintainability, it is essential to clean up the code and improve its readability, this can be achieved by: a) simplifying and renaming variables, making the style the same as ext4_punch_hole(); b) eliminating unnecessary position calculations, writing back all data in data=3Djournal mode, and drop page cache from the original offset to the end, rather than using aligned blocks; c) renaming the stale out_mutex tags. Signed-off-by: Zhang Yi --- fs/ext4/extents.c | 161 +++++++++++++++++++--------------------------- 1 file changed, 65 insertions(+), 96 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index aa07b5ddaff8..f843342e5164 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -4565,40 +4565,15 @@ static long ext4_zero_range(struct file *file, loff= _t offset, struct inode *inode =3D file_inode(file); struct address_space *mapping =3D file->f_mapping; handle_t *handle =3D NULL; - unsigned int max_blocks; loff_t new_size =3D 0; - int ret =3D 0; - int flags; - int credits; - int partial_begin, partial_end; - loff_t start, end; - ext4_lblk_t lblk; + loff_t end =3D offset + len; + ext4_lblk_t start_lblk, end_lblk; + unsigned int blocksize =3D i_blocksize(inode); unsigned int blkbits =3D inode->i_blkbits; + int ret, flags, credits; =20 trace_ext4_zero_range(inode, offset, len, mode); =20 - /* - * Round up offset. This is not fallocate, we need to zero out - * blocks, so convert interior block aligned part of the range to - * unwritten and possibly manually zero out unaligned parts of the - * range. Here, start and partial_begin are inclusive, end and - * partial_end are exclusive. - */ - start =3D round_up(offset, 1 << blkbits); - end =3D round_down((offset + len), 1 << blkbits); - - if (start < offset || end > offset + len) - return -EINVAL; - partial_begin =3D offset & ((1 << blkbits) - 1); - partial_end =3D (offset + len) & ((1 << blkbits) - 1); - - lblk =3D start >> blkbits; - max_blocks =3D (end >> blkbits); - if (max_blocks < lblk) - max_blocks =3D 0; - else - max_blocks -=3D lblk; - inode_lock(inode); =20 /* @@ -4606,88 +4581,78 @@ static long ext4_zero_range(struct file *file, loff= _t offset, */ if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { ret =3D -EOPNOTSUPP; - goto out_mutex; + goto out; } =20 if (!(mode & FALLOC_FL_KEEP_SIZE) && - (offset + len > inode->i_size || - offset + len > EXT4_I(inode)->i_disksize)) { - new_size =3D offset + len; + (end > inode->i_size || end > EXT4_I(inode)->i_disksize)) { + new_size =3D end; ret =3D inode_newsize_ok(inode, new_size); if (ret) - goto out_mutex; + goto out; } =20 - flags =3D EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; - /* Wait all existing dio workers, newcomers will block on i_rwsem */ inode_dio_wait(inode); =20 ret =3D file_modified(file); if (ret) - goto out_mutex; - - /* Preallocate the range including the unaligned edges */ - if (partial_begin || partial_end) { - ret =3D ext4_alloc_file_blocks(file, - round_down(offset, 1 << blkbits) >> blkbits, - (round_up((offset + len), 1 << blkbits) - - round_down(offset, 1 << blkbits)) >> blkbits, - new_size, flags); - if (ret) - goto out_mutex; - - } - - /* Zero range excluding the unaligned edges */ - if (max_blocks > 0) { - flags |=3D (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | - EXT4_EX_NOCACHE); + goto out; =20 - /* - * Prevent page faults from reinstantiating pages we have - * released from page cache. - */ - filemap_invalidate_lock(mapping); + /* + * Prevent page faults from reinstantiating pages we have released + * from page cache. + */ + filemap_invalidate_lock(mapping); =20 - ret =3D ext4_break_layouts(inode); - if (ret) { - filemap_invalidate_unlock(mapping); - goto out_mutex; - } + ret =3D ext4_break_layouts(inode); + if (ret) + goto out_invalidate_lock; =20 + /* + * For journalled data we need to write (and checkpoint) pages before + * discarding page cache to avoid inconsitent data on disk in case of + * crash before zeroing trans is committed. + */ + if (ext4_should_journal_data(inode)) { + ret =3D filemap_write_and_wait_range(mapping, offset, end - 1); + } else { ret =3D ext4_update_disksize_before_punch(inode, offset, len); - if (ret) { - filemap_invalidate_unlock(mapping); - goto out_mutex; - } + ext4_truncate_folios_range(inode, offset, end); + } + if (ret) + goto out_invalidate_lock; =20 - /* - * For journalled data we need to write (and checkpoint) pages - * before discarding page cache to avoid inconsitent data on - * disk in case of crash before zeroing trans is committed. - */ - if (ext4_should_journal_data(inode)) { - ret =3D filemap_write_and_wait_range(mapping, start, - end - 1); - if (ret) { - filemap_invalidate_unlock(mapping); - goto out_mutex; - } - } + /* Now release the pages and zero block aligned part of pages */ + truncate_pagecache_range(inode, offset, end - 1); =20 - /* Now release the pages and zero block aligned part of pages */ - ext4_truncate_folios_range(inode, start, end); - truncate_pagecache_range(inode, start, end - 1); + flags =3D EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; + /* Preallocate the range including the unaligned edges */ + if (offset & (blocksize - 1) || end & (blocksize - 1)) { + ext4_lblk_t alloc_lblk =3D offset >> blkbits; + ext4_lblk_t len_lblk =3D EXT4_MAX_BLOCKS(len, offset, blkbits); =20 - ret =3D ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, - flags); - filemap_invalidate_unlock(mapping); + ret =3D ext4_alloc_file_blocks(file, alloc_lblk, len_lblk, + new_size, flags); if (ret) - goto out_mutex; + goto out_invalidate_lock; } - if (!partial_begin && !partial_end) - goto out_mutex; + + /* Zero range excluding the unaligned edges */ + start_lblk =3D round_up(offset, blocksize) >> blkbits; + end_lblk =3D end >> blkbits; + if (end_lblk > start_lblk) { + ext4_lblk_t zero_blks =3D end_lblk - start_lblk; + + flags |=3D (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | EXT4_EX_NOCACHE); + ret =3D ext4_alloc_file_blocks(file, start_lblk, zero_blks, + new_size, flags); + if (ret) + goto out_invalidate_lock; + } + /* Finish zeroing out if it doesn't contain partial block */ + if (!(offset & (blocksize - 1)) && !(end & (blocksize - 1))) + goto out_invalidate_lock; =20 /* * In worst case we have to writeout two nonadjacent unwritten @@ -4700,25 +4665,29 @@ static long ext4_zero_range(struct file *file, loff= _t offset, if (IS_ERR(handle)) { ret =3D PTR_ERR(handle); ext4_std_error(inode->i_sb, ret); - goto out_mutex; + goto out_invalidate_lock; } =20 + /* Zero out partial block at the edges of the range */ + ret =3D ext4_zero_partial_blocks(handle, inode, offset, len); + if (ret) + goto out_handle; + if (new_size) ext4_update_inode_size(inode, new_size); ret =3D ext4_mark_inode_dirty(handle, inode); if (unlikely(ret)) goto out_handle; - /* Zero out partial block at the edges of the range */ - ret =3D ext4_zero_partial_blocks(handle, inode, offset, len); - if (ret >=3D 0) - ext4_update_inode_fsync_trans(handle, inode, 1); =20 + ext4_update_inode_fsync_trans(handle, inode, 1); if (file->f_flags & O_SYNC) ext4_handle_sync(handle); =20 out_handle: ext4_journal_stop(handle); -out_mutex: +out_invalidate_lock: + filemap_invalidate_unlock(mapping); +out: inode_unlock(inode); return ret; } --=20 2.39.2 From nobody Wed Nov 27 10:26:47 2024 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 73FA41C9B79; Thu, 10 Oct 2024 13:35:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567348; cv=none; b=IjvCc0meypV8Dl10yuI7QKZJ2j470TJxuu2xa+w/Tx46UX8Fa1sLo57jmBvRjEyP3rhWjSi/Y0C6AEfFZHwipJix/VcPDQXt4n7K3ZQiFIyHHuIXylu6+wrslD6b8Xn/CXxQVzSjVbEPZGYvQBB/LlH/SsthY5hAkv+5dkJBkV0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567348; c=relaxed/simple; bh=yHmuL1Nv422R7dxZXueyIXmIHEgHIv1hVuyrn6vveIY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=S9F9b73GYV8H1YlefsQEYUMB7kLm/9JbfKS+aUyFfaBhLld0L4tVMaiPx58HaYVC3tWdEZHOgeX1pBHr9WQ0eMFBrUYRHru3iU973z0k2lrTKEuIhzVuImpH5dcKikUnRThbyWKVpeDF0RkLg8hGk13mICyr2h7ujBr1jTX550A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4XPW3K4lC4z4f3jHg; Thu, 10 Oct 2024 21:35:25 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 4CC2F1A0359; Thu, 10 Oct 2024 21:35:43 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCXysYc2AdnDRnZDg--.21356S10; Thu, 10 Oct 2024 21:35:43 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: [PATCH v3 06/10] ext4: refactor ext4_collapse_range() Date: Thu, 10 Oct 2024 21:33:29 +0800 Message-Id: <20241010133333.146793-7-yi.zhang@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241010133333.146793-1-yi.zhang@huawei.com> References: <20241010133333.146793-1-yi.zhang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCXysYc2AdnDRnZDg--.21356S10 X-Coremail-Antispam: 1UD129KBjvJXoW3Gry8KryxGr4fWrW5Kw18uFg_yoW7Kw17pr ZxWry5Kr40ga4kWr48tF4DZF10y3W0g3yUW3yxGr9aqa4qyrnrKa4YyFWF9FW8trWkZFWj qF40v34UWrW7Za7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUHab4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7Iv64x0x7Aq67IIx4CEVc8vx2IErcIFxwACI4 02YVCY1x02628vn2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCF 04k20xvEw4C26cxK6c8Ij28IcwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14 v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkG c2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI42IY6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8 JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUFPETDU UUU Sender: yi.zhang@huaweicloud.com X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" Simplify ext4_collapse_range() and align its code style with that of ext4_zero_range() and ext4_punch_hole(). Refactor it by: a) renaming variables, b) removing redundant input parameter checks and moving the remaining checks under i_rwsem in preparation for future refactoring, and c) renaming the three stale error tags. Signed-off-by: Zhang Yi --- fs/ext4/extents.c | 103 +++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 55 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index f843342e5164..a4e95f3b5f09 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -5295,43 +5295,36 @@ static int ext4_collapse_range(struct file *file, l= off_t offset, loff_t len) struct inode *inode =3D file_inode(file); struct super_block *sb =3D inode->i_sb; struct address_space *mapping =3D inode->i_mapping; - ext4_lblk_t punch_start, punch_stop; + loff_t end =3D offset + len; + ext4_lblk_t start_lblk, end_lblk; handle_t *handle; unsigned int credits; - loff_t new_size, ioffset; + loff_t start, new_size; int ret; =20 - /* - * We need to test this early because xfstests assumes that a - * collapse range of (0, 1) will return EOPNOTSUPP if the file - * system does not support collapse range. - */ - if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) - return -EOPNOTSUPP; + trace_ext4_collapse_range(inode, offset, len); =20 - /* Collapse range works only on fs cluster size aligned regions. */ - if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) - return -EINVAL; + inode_lock(inode); =20 - trace_ext4_collapse_range(inode, offset, len); + /* Currently just for extent based files */ + if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { + ret =3D -EOPNOTSUPP; + goto out; + } =20 - punch_start =3D offset >> EXT4_BLOCK_SIZE_BITS(sb); - punch_stop =3D (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb); + /* Collapse range works only on fs cluster size aligned regions. */ + if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) { + ret =3D -EINVAL; + goto out; + } =20 - inode_lock(inode); /* * There is no need to overlap collapse range with EOF, in which case * it is effectively a truncate operation */ - if (offset + len >=3D inode->i_size) { + if (end >=3D inode->i_size) { ret =3D -EINVAL; - goto out_mutex; - } - - /* Currently just for extent based files */ - if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { - ret =3D -EOPNOTSUPP; - goto out_mutex; + goto out; } =20 /* Wait for existing dio to complete */ @@ -5339,7 +5332,7 @@ static int ext4_collapse_range(struct file *file, lof= f_t offset, loff_t len) =20 ret =3D file_modified(file); if (ret) - goto out_mutex; + goto out; =20 /* * Prevent page faults from reinstantiating pages we have released from @@ -5349,55 +5342,52 @@ static int ext4_collapse_range(struct file *file, l= off_t offset, loff_t len) =20 ret =3D ext4_break_layouts(inode); if (ret) - goto out_mmap; + goto out_invalidate_lock; =20 /* + * Write tail of the last page before removed range and data that + * will be shifted since they will get removed from the page cache + * below. We are also protected from pages becoming dirty by + * i_rwsem and invalidate_lock. * Need to round down offset to be aligned with page size boundary * for page size > block size. */ - ioffset =3D round_down(offset, PAGE_SIZE); - /* - * Write tail of the last page before removed range since it will get - * removed from the page cache below. - */ - ret =3D filemap_write_and_wait_range(mapping, ioffset, offset); - if (ret) - goto out_mmap; - /* - * Write data that will be shifted to preserve them when discarding - * page cache below. We are also protected from pages becoming dirty - * by i_rwsem and invalidate_lock. - */ - ret =3D filemap_write_and_wait_range(mapping, offset + len, - LLONG_MAX); + start =3D round_down(offset, PAGE_SIZE); + ret =3D filemap_write_and_wait_range(mapping, start, offset); + if (!ret) + ret =3D filemap_write_and_wait_range(mapping, end, LLONG_MAX); if (ret) - goto out_mmap; - truncate_pagecache(inode, ioffset); + goto out_invalidate_lock; + + truncate_pagecache(inode, start); =20 credits =3D ext4_writepage_trans_blocks(inode); handle =3D ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); if (IS_ERR(handle)) { ret =3D PTR_ERR(handle); - goto out_mmap; + goto out_invalidate_lock; } ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle); =20 + start_lblk =3D offset >> inode->i_blkbits; + end_lblk =3D (offset + len) >> inode->i_blkbits; + down_write(&EXT4_I(inode)->i_data_sem); ext4_discard_preallocations(inode); - ext4_es_remove_extent(inode, punch_start, EXT_MAX_BLOCKS - punch_start); + ext4_es_remove_extent(inode, start_lblk, EXT_MAX_BLOCKS - start_lblk); =20 - ret =3D ext4_ext_remove_space(inode, punch_start, punch_stop - 1); + ret =3D ext4_ext_remove_space(inode, start_lblk, end_lblk - 1); if (ret) { up_write(&EXT4_I(inode)->i_data_sem); - goto out_stop; + goto out_handle; } ext4_discard_preallocations(inode); =20 - ret =3D ext4_ext_shift_extents(inode, handle, punch_stop, - punch_stop - punch_start, SHIFT_LEFT); + ret =3D ext4_ext_shift_extents(inode, handle, end_lblk, + end_lblk - start_lblk, SHIFT_LEFT); if (ret) { up_write(&EXT4_I(inode)->i_data_sem); - goto out_stop; + goto out_handle; } =20 new_size =3D inode->i_size - len; @@ -5405,16 +5395,19 @@ static int ext4_collapse_range(struct file *file, l= off_t offset, loff_t len) EXT4_I(inode)->i_disksize =3D new_size; =20 up_write(&EXT4_I(inode)->i_data_sem); - if (IS_SYNC(inode)) - ext4_handle_sync(handle); ret =3D ext4_mark_inode_dirty(handle, inode); + if (ret) + goto out_handle; + ext4_update_inode_fsync_trans(handle, inode, 1); + if (IS_SYNC(inode)) + ext4_handle_sync(handle); =20 -out_stop: +out_handle: ext4_journal_stop(handle); -out_mmap: +out_invalidate_lock: filemap_invalidate_unlock(mapping); -out_mutex: +out: inode_unlock(inode); return ret; } --=20 2.39.2 From nobody Wed Nov 27 10:26:47 2024 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 C345B1C9DFC; Thu, 10 Oct 2024 13:35:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567349; cv=none; b=iMALPA8bLSLrsmhmUeiqtIbjtA1tqprh/p1PvSENMxUE9Xnd/JeClvlxz1dd/13DJ+DCeex8ZU0yHgXR+ow6SqXyc2BLx9AgouT5m93m54VuyLbkc+uyzsVa7S2DGfhrLhjjpSAgwinjlF4ZONOLrCKVtRdqHAGuGWWq438eSjM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567349; c=relaxed/simple; bh=5IaHDXgJLDZhDi83JRwKcYQNxc6DDUZ84WOhlrhjdhk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RIiyf02/Kf3O2vfi2GZn/Y7vm3x9MWrhKTee/1/CmE3DLCb9oseThwr/qZDW53LEgcvIlmimVjlQIsDER0dIe+pM1K4GyUJn0B/nFO28fhZ/Srr4IAxtgY2V9wAsckKqk4VDqAem1tVhQFOybekLPQLlaaJ9bQXnWsudTzQAIqY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4XPW3L0ypxz4f3kw2; Thu, 10 Oct 2024 21:35:26 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id BE9D01A0359; Thu, 10 Oct 2024 21:35:43 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCXysYc2AdnDRnZDg--.21356S11; Thu, 10 Oct 2024 21:35:43 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: [PATCH v3 07/10] ext4: refactor ext4_insert_range() Date: Thu, 10 Oct 2024 21:33:30 +0800 Message-Id: <20241010133333.146793-8-yi.zhang@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241010133333.146793-1-yi.zhang@huawei.com> References: <20241010133333.146793-1-yi.zhang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCXysYc2AdnDRnZDg--.21356S11 X-Coremail-Antispam: 1UD129KBjvJXoW3GryUCF13Wr1UJw45uryDWrg_yoWxWFWDpr ZxWry5KrWFqa4v9rW8KF4DZF18K3WkG3y7GryxGrn3Xa4jvr9rK3WYkFyYgFy8KrWkZrWY vF4Fk345Way2ka7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUHab4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7Iv64x0x7Aq67IIx4CEVc8vx2IErcIFxwACI4 02YVCY1x02628vn2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCF 04k20xvEw4C26cxK6c8Ij28IcwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14 v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkG c2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI42IY6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8 JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUFPETDU UUU Sender: yi.zhang@huaweicloud.com X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" Simplify ext4_insert_range() and align its code style with that of ext4_collapse_range(). Refactor it by: a) renaming variables, b) removing redundant input parameter checks and moving the remaining checks under i_rwsem in preparation for future refactoring, and c) renaming the three stale error tags. Signed-off-by: Zhang Yi --- fs/ext4/extents.c | 101 ++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 53 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index a4e95f3b5f09..4e35c2415e9b 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -5428,45 +5428,37 @@ static int ext4_insert_range(struct file *file, lof= f_t offset, loff_t len) handle_t *handle; struct ext4_ext_path *path; struct ext4_extent *extent; - ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk =3D 0; + ext4_lblk_t start_lblk, len_lblk, ee_start_lblk =3D 0; unsigned int credits, ee_len; - int ret =3D 0, depth, split_flag =3D 0; - loff_t ioffset; - - /* - * We need to test this early because xfstests assumes that an - * insert range of (0, 1) will return EOPNOTSUPP if the file - * system does not support insert range. - */ - if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) - return -EOPNOTSUPP; - - /* Insert range works only on fs cluster size aligned regions. */ - if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) - return -EINVAL; + int ret, depth, split_flag =3D 0; + loff_t start; =20 trace_ext4_insert_range(inode, offset, len); =20 - offset_lblk =3D offset >> EXT4_BLOCK_SIZE_BITS(sb); - len_lblk =3D len >> EXT4_BLOCK_SIZE_BITS(sb); - inode_lock(inode); + /* Currently just for extent based files */ if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { ret =3D -EOPNOTSUPP; - goto out_mutex; + goto out; } =20 - /* Check whether the maximum file size would be exceeded */ - if (len > inode->i_sb->s_maxbytes - inode->i_size) { - ret =3D -EFBIG; - goto out_mutex; + /* Insert range works only on fs cluster size aligned regions. */ + if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) { + ret =3D -EINVAL; + goto out; } =20 /* Offset must be less than i_size */ if (offset >=3D inode->i_size) { ret =3D -EINVAL; - goto out_mutex; + goto out; + } + + /* Check whether the maximum file size would be exceeded */ + if (len > inode->i_sb->s_maxbytes - inode->i_size) { + ret =3D -EFBIG; + goto out; } =20 /* Wait for existing dio to complete */ @@ -5474,7 +5466,7 @@ static int ext4_insert_range(struct file *file, loff_= t offset, loff_t len) =20 ret =3D file_modified(file); if (ret) - goto out_mutex; + goto out; =20 /* * Prevent page faults from reinstantiating pages we have released from @@ -5484,25 +5476,24 @@ static int ext4_insert_range(struct file *file, lof= f_t offset, loff_t len) =20 ret =3D ext4_break_layouts(inode); if (ret) - goto out_mmap; + goto out_invalidate_lock; =20 /* - * Need to round down to align start offset to page size boundary - * for page size > block size. + * Write out all dirty pages. Need to round down to align start offset + * to page size boundary for page size > block size. */ - ioffset =3D round_down(offset, PAGE_SIZE); - /* Write out all dirty pages */ - ret =3D filemap_write_and_wait_range(inode->i_mapping, ioffset, - LLONG_MAX); + start =3D round_down(offset, PAGE_SIZE); + ret =3D filemap_write_and_wait_range(mapping, start, LLONG_MAX); if (ret) - goto out_mmap; - truncate_pagecache(inode, ioffset); + goto out_invalidate_lock; + + truncate_pagecache(inode, start); =20 credits =3D ext4_writepage_trans_blocks(inode); handle =3D ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); if (IS_ERR(handle)) { ret =3D PTR_ERR(handle); - goto out_mmap; + goto out_invalidate_lock; } ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle); =20 @@ -5511,16 +5502,19 @@ static int ext4_insert_range(struct file *file, lof= f_t offset, loff_t len) EXT4_I(inode)->i_disksize +=3D len; ret =3D ext4_mark_inode_dirty(handle, inode); if (ret) - goto out_stop; + goto out_handle; + + start_lblk =3D offset >> inode->i_blkbits; + len_lblk =3D len >> inode->i_blkbits; =20 down_write(&EXT4_I(inode)->i_data_sem); ext4_discard_preallocations(inode); =20 - path =3D ext4_find_extent(inode, offset_lblk, NULL, 0); + path =3D ext4_find_extent(inode, start_lblk, NULL, 0); if (IS_ERR(path)) { up_write(&EXT4_I(inode)->i_data_sem); ret =3D PTR_ERR(path); - goto out_stop; + goto out_handle; } =20 depth =3D ext_depth(inode); @@ -5530,16 +5524,16 @@ static int ext4_insert_range(struct file *file, lof= f_t offset, loff_t len) ee_len =3D ext4_ext_get_actual_len(extent); =20 /* - * If offset_lblk is not the starting block of extent, split - * the extent @offset_lblk + * If start_lblk is not the starting block of extent, split + * the extent @start_lblk */ - if ((offset_lblk > ee_start_lblk) && - (offset_lblk < (ee_start_lblk + ee_len))) { + if ((start_lblk > ee_start_lblk) && + (start_lblk < (ee_start_lblk + ee_len))) { if (ext4_ext_is_unwritten(extent)) split_flag =3D EXT4_EXT_MARK_UNWRIT1 | EXT4_EXT_MARK_UNWRIT2; path =3D ext4_split_extent_at(handle, inode, path, - offset_lblk, split_flag, + start_lblk, split_flag, EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO | EXT4_GET_BLOCKS_METADATA_NOFAIL); @@ -5548,31 +5542,32 @@ static int ext4_insert_range(struct file *file, lof= f_t offset, loff_t len) if (IS_ERR(path)) { up_write(&EXT4_I(inode)->i_data_sem); ret =3D PTR_ERR(path); - goto out_stop; + goto out_handle; } } =20 ext4_free_ext_path(path); - ext4_es_remove_extent(inode, offset_lblk, EXT_MAX_BLOCKS - offset_lblk); + ext4_es_remove_extent(inode, start_lblk, EXT_MAX_BLOCKS - start_lblk); =20 /* - * if offset_lblk lies in a hole which is at start of file, use + * if start_lblk lies in a hole which is at start of file, use * ee_start_lblk to shift extents */ ret =3D ext4_ext_shift_extents(inode, handle, - max(ee_start_lblk, offset_lblk), len_lblk, SHIFT_RIGHT); - + max(ee_start_lblk, start_lblk), len_lblk, SHIFT_RIGHT); up_write(&EXT4_I(inode)->i_data_sem); + if (ret) + goto out_handle; + + ext4_update_inode_fsync_trans(handle, inode, 1); if (IS_SYNC(inode)) ext4_handle_sync(handle); - if (ret >=3D 0) - ext4_update_inode_fsync_trans(handle, inode, 1); =20 -out_stop: +out_handle: ext4_journal_stop(handle); -out_mmap: +out_invalidate_lock: filemap_invalidate_unlock(mapping); -out_mutex: +out: inode_unlock(inode); return ret; } --=20 2.39.2 From nobody Wed Nov 27 10:26:47 2024 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (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 270241C9ED8; Thu, 10 Oct 2024 13:35:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567349; cv=none; b=HGt7qG0AO8OZwdiAfUl8JOsKUS7Gu+kDImY3Aua4xG1sfpJnLgf6FprN0fQdzT2H5Aj5kSouDVJ7AL5cmfadDpHWpZetHr4qZtdzSC7eEmrF8azm7RDxHD6tOHVIial7MdnfKgR+hvne1GoVoI9uEtyOTbfc5tv7wndYU9XKkN4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567349; c=relaxed/simple; bh=g5kmM5f1wHVl2nIsl9Z2qIXRjXjdgwEqBL6l9b83P0I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FhaBwCiqEdvjWkIQYC/bOxl+R8jT72fhVIiG6o6x+1zUwiyQkSKG6e5rM1Z2W7A/z0lo6hHSbGjQsVAwgtIu3/xYQH6JWSWj69HQC+dmL2RbNmnZQt5Uh8owVdCQISOL2GZ5WtsJ0AXCBI+mUDZnfUSBvAH3tBZfI/feiYD04rg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4XPW3M2Nthz4f3jXl; Thu, 10 Oct 2024 21:35:27 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 372741A0568; Thu, 10 Oct 2024 21:35:44 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCXysYc2AdnDRnZDg--.21356S12; Thu, 10 Oct 2024 21:35:44 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: [PATCH v3 08/10] ext4: factor out ext4_do_fallocate() Date: Thu, 10 Oct 2024 21:33:31 +0800 Message-Id: <20241010133333.146793-9-yi.zhang@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241010133333.146793-1-yi.zhang@huawei.com> References: <20241010133333.146793-1-yi.zhang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCXysYc2AdnDRnZDg--.21356S12 X-Coremail-Antispam: 1UD129KBjvJXoWxuFy8JFW7Kr48tw1fJFyrJFb_yoWrtw17pF Z8JryUGF4xXa4DWrW0qw4UXFn8ta1kKrWUWrWI9rnaq3s0y3sxKF1YkFyFgFWxtrW8Ar4j vF4Yyry7CF17A3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUHab4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7Iv64x0x7Aq67IIx4CEVc8vx2IErcIFxwACI4 02YVCY1x02628vn2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCF 04k20xvEw4C26cxK6c8Ij28IcwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14 v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkG c2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI42IY6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8 JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUFPETDU UUU Sender: yi.zhang@huaweicloud.com X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" Now the real job of normal fallocate are open coded in ext4_fallocate(), factor out a new helper ext4_do_fallocate() to do the real job, like others functions (e.g. ext4_zero_range()) in ext4_fallocate() do, this can make the code more clear, no functional changes. Signed-off-by: Zhang Yi Reviewed-by: Jan Kara --- fs/ext4/extents.c | 125 ++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 65 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 4e35c2415e9b..2f727104f53d 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -4692,6 +4692,58 @@ static long ext4_zero_range(struct file *file, loff_= t offset, return ret; } =20 +static long ext4_do_fallocate(struct file *file, loff_t offset, + loff_t len, int mode) +{ + struct inode *inode =3D file_inode(file); + loff_t end =3D offset + len; + loff_t new_size =3D 0; + ext4_lblk_t start_lblk, len_lblk; + int ret; + + trace_ext4_fallocate_enter(inode, offset, len, mode); + + start_lblk =3D offset >> inode->i_blkbits; + len_lblk =3D EXT4_MAX_BLOCKS(len, offset, inode->i_blkbits); + + inode_lock(inode); + + /* We only support preallocation for extent-based files only. */ + if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { + ret =3D -EOPNOTSUPP; + goto out; + } + + if (!(mode & FALLOC_FL_KEEP_SIZE) && + (end > inode->i_size || end > EXT4_I(inode)->i_disksize)) { + new_size =3D end; + ret =3D inode_newsize_ok(inode, new_size); + if (ret) + goto out; + } + + /* Wait all existing dio workers, newcomers will block on i_rwsem */ + inode_dio_wait(inode); + + ret =3D file_modified(file); + if (ret) + goto out; + + ret =3D ext4_alloc_file_blocks(file, start_lblk, len_lblk, new_size, + EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT); + if (ret) + goto out; + + if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) { + ret =3D ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, + EXT4_I(inode)->i_sync_tid); + } +out: + inode_unlock(inode); + trace_ext4_fallocate_exit(inode, offset, len_lblk, ret); + return ret; +} + /* * preallocate space for a file. This implements ext4's fallocate file * operation, which gets called from sys_fallocate system call. @@ -4702,12 +4754,7 @@ static long ext4_zero_range(struct file *file, loff_= t offset, long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) { struct inode *inode =3D file_inode(file); - loff_t new_size =3D 0; - unsigned int max_blocks; - int ret =3D 0; - int flags; - ext4_lblk_t lblk; - unsigned int blkbits =3D inode->i_blkbits; + int ret; =20 /* * Encrypted inodes can't handle collapse range or insert @@ -4729,71 +4776,19 @@ long ext4_fallocate(struct file *file, int mode, lo= ff_t offset, loff_t len) ret =3D ext4_convert_inline_data(inode); inode_unlock(inode); if (ret) - goto exit; + return ret; =20 - if (mode & FALLOC_FL_PUNCH_HOLE) { + if (mode & FALLOC_FL_PUNCH_HOLE) ret =3D ext4_punch_hole(file, offset, len); - goto exit; - } - - if (mode & FALLOC_FL_COLLAPSE_RANGE) { + else if (mode & FALLOC_FL_COLLAPSE_RANGE) ret =3D ext4_collapse_range(file, offset, len); - goto exit; - } - - if (mode & FALLOC_FL_INSERT_RANGE) { + else if (mode & FALLOC_FL_INSERT_RANGE) ret =3D ext4_insert_range(file, offset, len); - goto exit; - } - - if (mode & FALLOC_FL_ZERO_RANGE) { + else if (mode & FALLOC_FL_ZERO_RANGE) ret =3D ext4_zero_range(file, offset, len, mode); - goto exit; - } - trace_ext4_fallocate_enter(inode, offset, len, mode); - lblk =3D offset >> blkbits; - - max_blocks =3D EXT4_MAX_BLOCKS(len, offset, blkbits); - flags =3D EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; - - inode_lock(inode); - - /* - * We only support preallocation for extent-based files only - */ - if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { - ret =3D -EOPNOTSUPP; - goto out; - } - - if (!(mode & FALLOC_FL_KEEP_SIZE) && - (offset + len > inode->i_size || - offset + len > EXT4_I(inode)->i_disksize)) { - new_size =3D offset + len; - ret =3D inode_newsize_ok(inode, new_size); - if (ret) - goto out; - } - - /* Wait all existing dio workers, newcomers will block on i_rwsem */ - inode_dio_wait(inode); - - ret =3D file_modified(file); - if (ret) - goto out; - - ret =3D ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags); - if (ret) - goto out; + else + ret =3D ext4_do_fallocate(file, offset, len, mode); =20 - if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) { - ret =3D ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, - EXT4_I(inode)->i_sync_tid); - } -out: - inode_unlock(inode); - trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); -exit: return ret; } =20 --=20 2.39.2 From nobody Wed Nov 27 10:26:47 2024 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 E46311CB506; Thu, 10 Oct 2024 13:35:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567350; cv=none; b=YEcX/Cv3C69xptCGaEnO/ilOcZOYocxzwF8M7eil7Z/C9AG8Olnprph1YPk28xPu1zFH899mk/pdh1GjQf9w7ZE2rUCDxB4UNY5906F/HDEu7ItnV8UElW5s4TOWryfN/MzMM6/uWPLRfiTf7gCU5GZqGG9tb3RZz/dlUIYYmrY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567350; c=relaxed/simple; bh=4d/eifYjYq40EpqmeGsaUkJLh+iOaNzJz0DlPkZ0OD0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SX3/ah5EgMMXkMr/WUPuZukOy9axnpO7iQPpZXMGT46x+kkCXT1JFTR3I3/w1cdAly7J4iEOeLCANfRp7H/bv/jlH5LDSkbIP+WcNhud+Top4S/amJKPzg29z3qFRRc5BeSLxrzJPuFvRTtc46dG1TbIWbZZ8MqrP5KkIpjcYk8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4XPW3M09Bvz4f3lV7; Thu, 10 Oct 2024 21:35:27 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id A49D61A08FC; Thu, 10 Oct 2024 21:35:44 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCXysYc2AdnDRnZDg--.21356S13; Thu, 10 Oct 2024 21:35:44 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: [PATCH v3 09/10] ext4: move out inode_lock into ext4_fallocate() Date: Thu, 10 Oct 2024 21:33:32 +0800 Message-Id: <20241010133333.146793-10-yi.zhang@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241010133333.146793-1-yi.zhang@huawei.com> References: <20241010133333.146793-1-yi.zhang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCXysYc2AdnDRnZDg--.21356S13 X-Coremail-Antispam: 1UD129KBjvJXoW3Ar1DAFyUKr13Kw1rXrykAFb_yoW3Gw1Upr Z8G3y5Jr4rXFykWrWvqa1DZF1jy3Z7KrWUWrW8urnFyasFy34fKF4YyFyF9FWrtrW8ZrWY vF4Utry7CFy7C3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUHqb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7Iv64x0x7Aq67IIx4CEVc8vx2IErcIFxwACI4 02YVCY1x02628vn2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCF 04k20xvEw4C26cxK6c8Ij28IcwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14 v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkG c2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI42IY6xIIjxv20xvEc7CjxVAFwI 0_Gr1j6F4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_ Gr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x07UZyC LUUUUU= Sender: yi.zhang@huaweicloud.com X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" Currently, all five sub-functions of ext4_fallocate() acquire the inode's i_rwsem at the beginning and release it before exiting. This process can be simplified by factoring out the management of i_rwsem into the ext4_fallocate() function. Signed-off-by: Zhang Yi --- fs/ext4/extents.c | 90 +++++++++++++++-------------------------------- fs/ext4/inode.c | 13 +++---- 2 files changed, 33 insertions(+), 70 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 2f727104f53d..a2db4e85790f 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -4573,23 +4573,18 @@ static long ext4_zero_range(struct file *file, loff= _t offset, int ret, flags, credits; =20 trace_ext4_zero_range(inode, offset, len, mode); + WARN_ON_ONCE(!inode_is_locked(inode)); =20 - inode_lock(inode); - - /* - * Indirect files do not support unwritten extents - */ - if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { - ret =3D -EOPNOTSUPP; - goto out; - } + /* Indirect files do not support unwritten extents */ + if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) + return -EOPNOTSUPP; =20 if (!(mode & FALLOC_FL_KEEP_SIZE) && (end > inode->i_size || end > EXT4_I(inode)->i_disksize)) { new_size =3D end; ret =3D inode_newsize_ok(inode, new_size); if (ret) - goto out; + return ret; } =20 /* Wait all existing dio workers, newcomers will block on i_rwsem */ @@ -4597,7 +4592,7 @@ static long ext4_zero_range(struct file *file, loff_t= offset, =20 ret =3D file_modified(file); if (ret) - goto out; + return ret; =20 /* * Prevent page faults from reinstantiating pages we have released @@ -4687,8 +4682,6 @@ static long ext4_zero_range(struct file *file, loff_t= offset, ext4_journal_stop(handle); out_invalidate_lock: filemap_invalidate_unlock(mapping); -out: - inode_unlock(inode); return ret; } =20 @@ -4702,12 +4695,11 @@ static long ext4_do_fallocate(struct file *file, lo= ff_t offset, int ret; =20 trace_ext4_fallocate_enter(inode, offset, len, mode); + WARN_ON_ONCE(!inode_is_locked(inode)); =20 start_lblk =3D offset >> inode->i_blkbits; len_lblk =3D EXT4_MAX_BLOCKS(len, offset, inode->i_blkbits); =20 - inode_lock(inode); - /* We only support preallocation for extent-based files only. */ if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { ret =3D -EOPNOTSUPP; @@ -4739,7 +4731,6 @@ static long ext4_do_fallocate(struct file *file, loff= _t offset, EXT4_I(inode)->i_sync_tid); } out: - inode_unlock(inode); trace_ext4_fallocate_exit(inode, offset, len_lblk, ret); return ret; } @@ -4774,9 +4765,8 @@ long ext4_fallocate(struct file *file, int mode, loff= _t offset, loff_t len) =20 inode_lock(inode); ret =3D ext4_convert_inline_data(inode); - inode_unlock(inode); if (ret) - return ret; + goto out; =20 if (mode & FALLOC_FL_PUNCH_HOLE) ret =3D ext4_punch_hole(file, offset, len); @@ -4788,7 +4778,8 @@ long ext4_fallocate(struct file *file, int mode, loff= _t offset, loff_t len) ret =3D ext4_zero_range(file, offset, len, mode); else ret =3D ext4_do_fallocate(file, offset, len, mode); - +out: + inode_unlock(inode); return ret; } =20 @@ -5298,36 +5289,27 @@ static int ext4_collapse_range(struct file *file, l= off_t offset, loff_t len) int ret; =20 trace_ext4_collapse_range(inode, offset, len); - - inode_lock(inode); + WARN_ON_ONCE(!inode_is_locked(inode)); =20 /* Currently just for extent based files */ - if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { - ret =3D -EOPNOTSUPP; - goto out; - } - + if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) + return -EOPNOTSUPP; /* Collapse range works only on fs cluster size aligned regions. */ - if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) { - ret =3D -EINVAL; - goto out; - } - + if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) + return -EINVAL; /* * There is no need to overlap collapse range with EOF, in which case * it is effectively a truncate operation */ - if (end >=3D inode->i_size) { - ret =3D -EINVAL; - goto out; - } + if (end >=3D inode->i_size) + return -EINVAL; =20 /* Wait for existing dio to complete */ inode_dio_wait(inode); =20 ret =3D file_modified(file); if (ret) - goto out; + return ret; =20 /* * Prevent page faults from reinstantiating pages we have released from @@ -5402,8 +5384,6 @@ static int ext4_collapse_range(struct file *file, lof= f_t offset, loff_t len) ext4_journal_stop(handle); out_invalidate_lock: filemap_invalidate_unlock(mapping); -out: - inode_unlock(inode); return ret; } =20 @@ -5429,39 +5409,27 @@ static int ext4_insert_range(struct file *file, lof= f_t offset, loff_t len) loff_t start; =20 trace_ext4_insert_range(inode, offset, len); - - inode_lock(inode); + WARN_ON_ONCE(!inode_is_locked(inode)); =20 /* Currently just for extent based files */ - if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { - ret =3D -EOPNOTSUPP; - goto out; - } - + if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) + return -EOPNOTSUPP; /* Insert range works only on fs cluster size aligned regions. */ - if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) { - ret =3D -EINVAL; - goto out; - } - + if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) + return -EINVAL; /* Offset must be less than i_size */ - if (offset >=3D inode->i_size) { - ret =3D -EINVAL; - goto out; - } - + if (offset >=3D inode->i_size) + return -EINVAL; /* Check whether the maximum file size would be exceeded */ - if (len > inode->i_sb->s_maxbytes - inode->i_size) { - ret =3D -EFBIG; - goto out; - } + if (len > inode->i_sb->s_maxbytes - inode->i_size) + return -EFBIG; =20 /* Wait for existing dio to complete */ inode_dio_wait(inode); =20 ret =3D file_modified(file); if (ret) - goto out; + return ret; =20 /* * Prevent page faults from reinstantiating pages we have released from @@ -5562,8 +5530,6 @@ static int ext4_insert_range(struct file *file, loff_= t offset, loff_t len) ext4_journal_stop(handle); out_invalidate_lock: filemap_invalidate_unlock(mapping); -out: - inode_unlock(inode); return ret; } =20 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 1d128333bd06..bea19cd6e676 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -3962,15 +3962,14 @@ int ext4_punch_hole(struct file *file, loff_t offse= t, loff_t length) unsigned long blocksize =3D i_blocksize(inode); handle_t *handle; unsigned int credits; - int ret =3D 0; + int ret; =20 trace_ext4_punch_hole(inode, offset, length, 0); - - inode_lock(inode); + WARN_ON_ONCE(!inode_is_locked(inode)); =20 /* No need to punch hole beyond i_size */ if (offset >=3D inode->i_size) - goto out; + return 0; =20 /* * If the hole extends beyond i_size, set the hole to end after @@ -3990,7 +3989,7 @@ int ext4_punch_hole(struct file *file, loff_t offset,= loff_t length) if (offset & (blocksize - 1) || end & (blocksize - 1)) { ret =3D ext4_inode_attach_jinode(inode); if (ret < 0) - goto out; + return ret; } =20 /* Wait all existing dio workers, newcomers will block on i_rwsem */ @@ -3998,7 +3997,7 @@ int ext4_punch_hole(struct file *file, loff_t offset,= loff_t length) =20 ret =3D file_modified(file); if (ret) - goto out; + return ret; =20 /* * Prevent page faults from reinstantiating pages we have released from @@ -4082,8 +4081,6 @@ int ext4_punch_hole(struct file *file, loff_t offset,= loff_t length) ext4_journal_stop(handle); out_invalidate_lock: filemap_invalidate_unlock(mapping); -out: - inode_unlock(inode); return ret; } =20 --=20 2.39.2 From nobody Wed Nov 27 10:26:47 2024 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 AF37E1CC8B3; Thu, 10 Oct 2024 13:35:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567352; cv=none; b=jE7RKKB3N56ZqClmPSTbG0z7L1iUSQ0id0tcOLqPysqZRuwKdiAJPwr9edACAh123VXDWoUEGHhib753ijtawAF76uFHK8/2+WwaCJ7UvWwPHk+4Zv7q8PNmnlFyWmIZMbFdxJfAVarazvszThz8m9GCenNAtQa+PBRADcR2VVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728567352; c=relaxed/simple; bh=RKxUSbx478VgenYvihB5XbO2huMG84DHYLr6jtnYvTg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=i6wzPNVuK4j1BqQxXkWnGxuT8x6aRCFc7UeyaVCEYMeelTuUBlBvjuic+hWNa5wOegTQvdpZHpZoAbATxHjDPXIR8X6wI9xbPeEWzJgO4ynsQWOelf5BftO8nOWzVMsOnQfr63dTCyOLmT+IZBWRZYQL24cxMWH6S1rJOw0wK9E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=none smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4XPW3M3Pghz4f3kw1; Thu, 10 Oct 2024 21:35:27 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 1EF141A08FC; Thu, 10 Oct 2024 21:35:45 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCXysYc2AdnDRnZDg--.21356S14; Thu, 10 Oct 2024 21:35:44 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: [PATCH v3 10/10] ext4: move out common parts into ext4_fallocate() Date: Thu, 10 Oct 2024 21:33:33 +0800 Message-Id: <20241010133333.146793-11-yi.zhang@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241010133333.146793-1-yi.zhang@huawei.com> References: <20241010133333.146793-1-yi.zhang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCXysYc2AdnDRnZDg--.21356S14 X-Coremail-Antispam: 1UD129KBjvJXoW3Jw1DCF1rGrWfKF4kZry8Grg_yoWfuw47pF W5JrW5tFyxWFykWr4rAanrZF13twnFgrWUWrWxu34vvasIywnFka1YkFyFqFW3trW8Zr4j vF4jvr9rGFW7Z3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUHqb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7Iv64x0x7Aq67IIx4CEVc8vx2IErcIFxwACI4 02YVCY1x02628vn2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCF 04k20xvEw4C26cxK6c8Ij28IcwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14 v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkG c2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVW8JVW5JwCI42IY6xIIjxv20xvEc7CjxVAFwI 0_Gr1j6F4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_ Cr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x07UZyC LUUUUU= Sender: yi.zhang@huaweicloud.com X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" Currently, all zeroing ranges, punch holes, collapse ranges, and insert ranges first wait for all existing direct I/O workers to complete, and then they acquire the mapping's invalidate lock before performing the actual work. These common components are nearly identical, so we can simplify the code by factoring them out into the ext4_fallocate(). Signed-off-by: Zhang Yi --- fs/ext4/extents.c | 121 ++++++++++++++++------------------------------ fs/ext4/inode.c | 23 +-------- 2 files changed, 43 insertions(+), 101 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index a2db4e85790f..d5067d5aa449 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -4587,23 +4587,6 @@ static long ext4_zero_range(struct file *file, loff_= t offset, return ret; } =20 - /* Wait all existing dio workers, newcomers will block on i_rwsem */ - inode_dio_wait(inode); - - ret =3D file_modified(file); - if (ret) - return ret; - - /* - * Prevent page faults from reinstantiating pages we have released - * from page cache. - */ - filemap_invalidate_lock(mapping); - - ret =3D ext4_break_layouts(inode); - if (ret) - goto out_invalidate_lock; - /* * For journalled data we need to write (and checkpoint) pages before * discarding page cache to avoid inconsitent data on disk in case of @@ -4616,7 +4599,7 @@ static long ext4_zero_range(struct file *file, loff_t= offset, ext4_truncate_folios_range(inode, offset, end); } if (ret) - goto out_invalidate_lock; + return ret; =20 /* Now release the pages and zero block aligned part of pages */ truncate_pagecache_range(inode, offset, end - 1); @@ -4630,7 +4613,7 @@ static long ext4_zero_range(struct file *file, loff_t= offset, ret =3D ext4_alloc_file_blocks(file, alloc_lblk, len_lblk, new_size, flags); if (ret) - goto out_invalidate_lock; + return ret; } =20 /* Zero range excluding the unaligned edges */ @@ -4643,11 +4626,11 @@ static long ext4_zero_range(struct file *file, loff= _t offset, ret =3D ext4_alloc_file_blocks(file, start_lblk, zero_blks, new_size, flags); if (ret) - goto out_invalidate_lock; + return ret; } /* Finish zeroing out if it doesn't contain partial block */ if (!(offset & (blocksize - 1)) && !(end & (blocksize - 1))) - goto out_invalidate_lock; + return ret; =20 /* * In worst case we have to writeout two nonadjacent unwritten @@ -4660,7 +4643,7 @@ static long ext4_zero_range(struct file *file, loff_t= offset, if (IS_ERR(handle)) { ret =3D PTR_ERR(handle); ext4_std_error(inode->i_sb, ret); - goto out_invalidate_lock; + return ret; } =20 /* Zero out partial block at the edges of the range */ @@ -4680,8 +4663,6 @@ static long ext4_zero_range(struct file *file, loff_t= offset, =20 out_handle: ext4_journal_stop(handle); -out_invalidate_lock: - filemap_invalidate_unlock(mapping); return ret; } =20 @@ -4714,13 +4695,6 @@ static long ext4_do_fallocate(struct file *file, lof= f_t offset, goto out; } =20 - /* Wait all existing dio workers, newcomers will block on i_rwsem */ - inode_dio_wait(inode); - - ret =3D file_modified(file); - if (ret) - goto out; - ret =3D ext4_alloc_file_blocks(file, start_lblk, len_lblk, new_size, EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT); if (ret) @@ -4745,6 +4719,7 @@ static long ext4_do_fallocate(struct file *file, loff= _t offset, long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) { struct inode *inode =3D file_inode(file); + struct address_space *mapping =3D file->f_mapping; int ret; =20 /* @@ -4768,6 +4743,29 @@ long ext4_fallocate(struct file *file, int mode, lof= f_t offset, loff_t len) if (ret) goto out; =20 + /* Wait all existing dio workers, newcomers will block on i_rwsem */ + inode_dio_wait(inode); + + ret =3D file_modified(file); + if (ret) + return ret; + + if ((mode & FALLOC_FL_MODE_MASK) =3D=3D FALLOC_FL_ALLOCATE_RANGE) { + ret =3D ext4_do_fallocate(file, offset, len, mode); + goto out; + } + + /* + * Follow-up operations will drop page cache, hold invalidate lock + * to prevent page faults from reinstantiating pages we have + * released from page cache. + */ + filemap_invalidate_lock(mapping); + + ret =3D ext4_break_layouts(inode); + if (ret) + goto out_invalidate_lock; + if (mode & FALLOC_FL_PUNCH_HOLE) ret =3D ext4_punch_hole(file, offset, len); else if (mode & FALLOC_FL_COLLAPSE_RANGE) @@ -4777,7 +4775,10 @@ long ext4_fallocate(struct file *file, int mode, lof= f_t offset, loff_t len) else if (mode & FALLOC_FL_ZERO_RANGE) ret =3D ext4_zero_range(file, offset, len, mode); else - ret =3D ext4_do_fallocate(file, offset, len, mode); + ret =3D -EOPNOTSUPP; + +out_invalidate_lock: + filemap_invalidate_unlock(mapping); out: inode_unlock(inode); return ret; @@ -5304,23 +5305,6 @@ static int ext4_collapse_range(struct file *file, lo= ff_t offset, loff_t len) if (end >=3D inode->i_size) return -EINVAL; =20 - /* Wait for existing dio to complete */ - inode_dio_wait(inode); - - ret =3D file_modified(file); - if (ret) - return ret; - - /* - * Prevent page faults from reinstantiating pages we have released from - * page cache. - */ - filemap_invalidate_lock(mapping); - - ret =3D ext4_break_layouts(inode); - if (ret) - goto out_invalidate_lock; - /* * Write tail of the last page before removed range and data that * will be shifted since they will get removed from the page cache @@ -5334,16 +5318,15 @@ static int ext4_collapse_range(struct file *file, l= off_t offset, loff_t len) if (!ret) ret =3D filemap_write_and_wait_range(mapping, end, LLONG_MAX); if (ret) - goto out_invalidate_lock; + return ret; =20 truncate_pagecache(inode, start); =20 credits =3D ext4_writepage_trans_blocks(inode); handle =3D ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); - if (IS_ERR(handle)) { - ret =3D PTR_ERR(handle); - goto out_invalidate_lock; - } + if (IS_ERR(handle)) + return PTR_ERR(handle); + ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle); =20 start_lblk =3D offset >> inode->i_blkbits; @@ -5382,8 +5365,6 @@ static int ext4_collapse_range(struct file *file, lof= f_t offset, loff_t len) =20 out_handle: ext4_journal_stop(handle); -out_invalidate_lock: - filemap_invalidate_unlock(mapping); return ret; } =20 @@ -5424,23 +5405,6 @@ static int ext4_insert_range(struct file *file, loff= _t offset, loff_t len) if (len > inode->i_sb->s_maxbytes - inode->i_size) return -EFBIG; =20 - /* Wait for existing dio to complete */ - inode_dio_wait(inode); - - ret =3D file_modified(file); - if (ret) - return ret; - - /* - * Prevent page faults from reinstantiating pages we have released from - * page cache. - */ - filemap_invalidate_lock(mapping); - - ret =3D ext4_break_layouts(inode); - if (ret) - goto out_invalidate_lock; - /* * Write out all dirty pages. Need to round down to align start offset * to page size boundary for page size > block size. @@ -5448,16 +5412,15 @@ static int ext4_insert_range(struct file *file, lof= f_t offset, loff_t len) start =3D round_down(offset, PAGE_SIZE); ret =3D filemap_write_and_wait_range(mapping, start, LLONG_MAX); if (ret) - goto out_invalidate_lock; + return ret; =20 truncate_pagecache(inode, start); =20 credits =3D ext4_writepage_trans_blocks(inode); handle =3D ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); - if (IS_ERR(handle)) { - ret =3D PTR_ERR(handle); - goto out_invalidate_lock; - } + if (IS_ERR(handle)) + return PTR_ERR(handle); + ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle); =20 /* Expand file to avoid data loss if there is error while shifting */ @@ -5528,8 +5491,6 @@ static int ext4_insert_range(struct file *file, loff_= t offset, loff_t len) =20 out_handle: ext4_journal_stop(handle); -out_invalidate_lock: - filemap_invalidate_unlock(mapping); return ret; } =20 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index bea19cd6e676..1ccf84a64b7b 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -3992,23 +3992,6 @@ int ext4_punch_hole(struct file *file, loff_t offset= , loff_t length) return ret; } =20 - /* Wait all existing dio workers, newcomers will block on i_rwsem */ - inode_dio_wait(inode); - - ret =3D file_modified(file); - if (ret) - return ret; - - /* - * Prevent page faults from reinstantiating pages we have released from - * page cache. - */ - filemap_invalidate_lock(mapping); - - ret =3D ext4_break_layouts(inode); - if (ret) - goto out_invalidate_lock; - /* * For journalled data we need to write (and checkpoint) pages * before discarding page cache to avoid inconsitent data on @@ -4021,7 +4004,7 @@ int ext4_punch_hole(struct file *file, loff_t offset,= loff_t length) ext4_truncate_folios_range(inode, offset, end); } if (ret) - goto out_invalidate_lock; + return ret; =20 /* Now release the pages and zero block aligned part of pages*/ truncate_pagecache_range(inode, offset, end - 1); @@ -4034,7 +4017,7 @@ int ext4_punch_hole(struct file *file, loff_t offset,= loff_t length) if (IS_ERR(handle)) { ret =3D PTR_ERR(handle); ext4_std_error(sb, ret); - goto out_invalidate_lock; + return ret; } =20 ret =3D ext4_zero_partial_blocks(handle, inode, offset, length); @@ -4079,8 +4062,6 @@ int ext4_punch_hole(struct file *file, loff_t offset,= loff_t length) ext4_handle_sync(handle); out_handle: ext4_journal_stop(handle); -out_invalidate_lock: - filemap_invalidate_unlock(mapping); return ret; } =20 --=20 2.39.2