From nobody Sat Oct 11 04:07:54 2025 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 7C67025F7B7; Wed, 11 Jun 2025 11:29:41 +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=1749641383; cv=none; b=fVIw8YOqCnH8oGgvER1I3pTB2OltufwZ5gODvT3yTRPbXDVwIi9kWbsXwwgODYXhmzUHgKcWOASvb1YBGgN4VUAZ3xseox/08oHcf2VQO5Oe3FCCCPABJyQVtK7UOjzZojd2QhA0S2otr7s1aqWohyKQUPGdcRe62qhbKse0/Zw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749641383; c=relaxed/simple; bh=aWFl6Wl3JHoXR2aAcYYUwJU4szeoESNVnGFjDgiiTUo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T50ek3Q5DPiuQNr1od4QtbXICoHxQ3aCP4tpC2Hy62bc+FIVHQwWnrWVbqKU9tsGELlBPVzEP7w/b+MTU0g34hg2cNEw96ztxVbmbV8xSoSUwMjIZY+2BB+xxZUDjMshKIsagbEMHKi9HyeG/J+XU+3QYymddvjPKezCeVvTs1E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4bHNjc16LDzYQvvB; Wed, 11 Jun 2025 19:29:40 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 2678D1A0C6F; Wed, 11 Jun 2025 19:29:39 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.112.188]) by APP3 (Coremail) with SMTP id _Ch0CgAXacOXaElofvDPOw--.32023S5; Wed, 11 Jun 2025 19:29:38 +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, ojaswin@linux.ibm.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, libaokun1@huawei.com, yukuai3@huawei.com, yangerkun@huawei.com Subject: [PATCH v2 1/6] ext4: move the calculation of wbc->nr_to_write to mpage_folio_done() Date: Wed, 11 Jun 2025 19:16:20 +0800 Message-ID: <20250611111625.1668035-2-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20250611111625.1668035-1-yi.zhang@huaweicloud.com> References: <20250611111625.1668035-1-yi.zhang@huaweicloud.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: _Ch0CgAXacOXaElofvDPOw--.32023S5 X-Coremail-Antispam: 1UD129KBjvJXoW7Kw1UKr4xtFy3KFW5Gw1DGFg_yoW8Jw1UpF W5Ka4kWay8Xr9Igrn7WFsrZr1xtF95GF4UXFWfWw43WFy5ArykKF4jq34YvF43JrWkJ3yI qFs5JFy5uF17JFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUm014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67 kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY 6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0x vEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVj vjDU0xZFpf9x0JUIiiDUUUUU= X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" From: Zhang Yi mpage_folio_done() should be a more appropriate place than mpage_submit_folio() for updating the wbc->nr_to_write after we have submitted a fully mapped folio. Preparing to make mpage_submit_folio() allows to submit partially mapped folio that is still under processing. Signed-off-by: Zhang Yi Reviewed-by: Baokun Li Reviewed-by: Jan Kara --- fs/ext4/inode.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index be9a4cba35fd..3a086fee7989 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -2024,7 +2024,10 @@ int ext4_da_get_block_prep(struct inode *inode, sect= or_t iblock, =20 static void mpage_folio_done(struct mpage_da_data *mpd, struct folio *foli= o) { - mpd->first_page +=3D folio_nr_pages(folio); + unsigned long nr_pages =3D folio_nr_pages(folio); + + mpd->first_page +=3D nr_pages; + mpd->wbc->nr_to_write -=3D nr_pages; folio_unlock(folio); } =20 @@ -2055,8 +2058,6 @@ static int mpage_submit_folio(struct mpage_da_data *m= pd, struct folio *folio) !ext4_verity_in_progress(mpd->inode)) len =3D size & (len - 1); err =3D ext4_bio_write_folio(&mpd->io_submit, folio, len); - if (!err) - mpd->wbc->nr_to_write -=3D folio_nr_pages(folio); =20 return err; } --=20 2.46.1 From nobody Sat Oct 11 04:07:54 2025 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 7C60325BF12; Wed, 11 Jun 2025 11:29:41 +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=1749641383; cv=none; b=liRDE0VQZfCTxlAq/NiB+2z876bebB2IJGi7rytyE72ulSmgFrgNK8XIeAMpVPCOebl+rbDOhOIvno8bA8M5aas2jtu1PaTxx6vSuOX8ybxBFqzY9v6bGst4hnIiCfQTysaHHV7RIymHRPmrAp28ndnKCM0op59Q3PhtoxDMzx4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749641383; c=relaxed/simple; bh=lVChN0/oMfMsiVquqIy42WwZPKjVduH4hbJF3twoQ6s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YBL1WhJsblGB/cfhyMLtnATWE5VcKLn5Q64iJAnvdNqeKT77LDEzv+9tZd1Udjwos+koffOSRwW6bieeHzsvudyzCKWP7RRWEpQe1BfMMtJAFfleapWNlUlStfGxJQ8jLPXMhLQt8o7cxHU8pzugzERXEo1m5DEyUeBirbsOdsM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.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 ESMTPS id 4bHNjc4dsxzYQvsb; Wed, 11 Jun 2025 19:29:40 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 9F1661A1669; Wed, 11 Jun 2025 19:29:39 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.112.188]) by APP3 (Coremail) with SMTP id _Ch0CgAXacOXaElofvDPOw--.32023S6; Wed, 11 Jun 2025 19:29:39 +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, ojaswin@linux.ibm.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, libaokun1@huawei.com, yukuai3@huawei.com, yangerkun@huawei.com Subject: [PATCH v2 2/6] ext4: fix stale data if it bail out of the extents mapping loop Date: Wed, 11 Jun 2025 19:16:21 +0800 Message-ID: <20250611111625.1668035-3-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20250611111625.1668035-1-yi.zhang@huaweicloud.com> References: <20250611111625.1668035-1-yi.zhang@huaweicloud.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: _Ch0CgAXacOXaElofvDPOw--.32023S6 X-Coremail-Antispam: 1UD129KBjvJXoWxWryDXr47uFyDGw18GrWfKrg_yoWrGFykpF Wjkwn8Kw4kJaya9rZ3XayDZr1Sy3yrJrW7Jay7GFW2vFy5GryfKr48ta4FvFWrXrWDJFW0 qF45Kr45u3W7AFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUm014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67 kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY 6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0x vEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVj vjDU0xZFpf9x0JUl9a9UUUUU= X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" From: Zhang Yi During the process of writing back folios, if mpage_map_and_submit_extent() exits the extent mapping loop due to an ENOSPC or ENOMEM error, it may result in stale data or filesystem inconsistency in environments where the block size is smaller than the folio size. When mapping a discontinuous folio in mpage_map_and_submit_extent(), some buffers may have already be mapped. If we exit the mapping loop prematurely, the folio data within the mapped range will not be written back, and the file's disk size will not be updated. Once the transaction that includes this range of extents is committed, this can lead to stale data or filesystem inconsistency. Fix this by submitting the current processing partial mapped folio and update the disk size to the end of the mapped range. Suggested-by: Jan Kara Signed-off-by: Zhang Yi --- fs/ext4/inode.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 3a086fee7989..d0db6e3bf158 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -2362,6 +2362,42 @@ static int mpage_map_one_extent(handle_t *handle, st= ruct mpage_da_data *mpd) return 0; } =20 +/* + * This is used to submit mapped buffers in a single folio that is not ful= ly + * mapped for various reasons, such as insufficient space or journal credi= ts. + */ +static int mpage_submit_buffers(struct mpage_da_data *mpd, loff_t pos) +{ + struct inode *inode =3D mpd->inode; + struct folio *folio; + int ret; + + folio =3D filemap_get_folio(inode->i_mapping, mpd->first_page); + if (IS_ERR(folio)) + return PTR_ERR(folio); + + ret =3D mpage_submit_folio(mpd, folio); + if (ret) + goto out; + /* + * Update first_page to prevent this folio from being released in + * mpage_release_unused_pages(), it should not equal to the folio + * index. + * + * The first_page will be reset to the aligned folio index when this + * folio is written again in the next round. Additionally, do not + * update wbc->nr_to_write here, as it will be updated once the + * entire folio has finished processing. + */ + mpd->first_page =3D round_up(pos, PAGE_SIZE) >> PAGE_SHIFT; + WARN_ON_ONCE((folio->index =3D=3D mpd->first_page) || + !folio_contains(folio, pos >> PAGE_SHIFT)); +out: + folio_unlock(folio); + folio_put(folio); + return ret; +} + /* * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length * mpd->len and submit pages underlying it for IO @@ -2412,8 +2448,16 @@ static int mpage_map_and_submit_extent(handle_t *han= dle, */ if ((err =3D=3D -ENOMEM) || (err =3D=3D -ENOSPC && ext4_count_free_clusters(sb))) { - if (progress) + /* + * We may have already allocated extents for + * some bhs inside the folio, issue the + * corresponding data to prevent stale data. + */ + if (progress) { + if (mpage_submit_buffers(mpd, disksize)) + goto invalidate_dirty_pages; goto update_disksize; + } return err; } ext4_msg(sb, KERN_CRIT, @@ -2432,6 +2476,8 @@ static int mpage_map_and_submit_extent(handle_t *hand= le, *give_up_on_write =3D true; return err; } + disksize =3D ((loff_t)(map->m_lblk + map->m_len)) << + inode->i_blkbits; progress =3D 1; /* * Update buffer state, submit mapped pages, and get us new @@ -2442,12 +2488,12 @@ static int mpage_map_and_submit_extent(handle_t *ha= ndle, goto update_disksize; } while (map->m_len); =20 + disksize =3D ((loff_t)mpd->first_page) << PAGE_SHIFT; update_disksize: /* * Update on-disk size after IO is submitted. Races with * truncate are avoided by checking i_size under i_data_sem. */ - disksize =3D ((loff_t)mpd->first_page) << PAGE_SHIFT; if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { int err2; loff_t i_size; --=20 2.46.1 From nobody Sat Oct 11 04:07:54 2025 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 EF9212609C7; Wed, 11 Jun 2025 11:29:41 +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=1749641385; cv=none; b=IRm6Wde78xYIItcBqJ0GobOv+u8PkQXVp+FiRz7fAydVE0I/4ubrPw+Uwffm4U2nyNAhagxhkvKN7rq18XyOyMil/RYBERBrrSKg2Iw68TVkFtPPbf3NL01/inDOm92i7PQf55c1RkDj5gLArwu+kAq4eoa35+vzXlFedgKkXrs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749641385; c=relaxed/simple; bh=rzOMd1WZj1UyrCqbzZ7rp3KTElt1HVNQRYgATXSj3u0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LNnUU1++vmlfpIdqwwra5LiRtR4Dc2gVDSQspWMWmAiVXTmytDoK3CfK7wPTq+i/1q/Y7QFAI3dhhUlnq0j0qQyj6Lo1J2XVXSQOVtYGR9CUXRYzfF0+RTMrmATaZ3Nft8yZDyUMSevGz9719f/6vOjIa/3xaZyvQnTVXDDFYxI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.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 ESMTPS id 4bHNjd182rzYQvvR; Wed, 11 Jun 2025 19:29:41 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 288EE1A123A; Wed, 11 Jun 2025 19:29:40 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.112.188]) by APP3 (Coremail) with SMTP id _Ch0CgAXacOXaElofvDPOw--.32023S7; Wed, 11 Jun 2025 19:29:39 +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, ojaswin@linux.ibm.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, libaokun1@huawei.com, yukuai3@huawei.com, yangerkun@huawei.com Subject: [PATCH v2 3/6] ext4: restart handle if credits are insufficient during allocating blocks Date: Wed, 11 Jun 2025 19:16:22 +0800 Message-ID: <20250611111625.1668035-4-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20250611111625.1668035-1-yi.zhang@huaweicloud.com> References: <20250611111625.1668035-1-yi.zhang@huaweicloud.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: _Ch0CgAXacOXaElofvDPOw--.32023S7 X-Coremail-Antispam: 1UD129KBjvJXoWxCF17Zw4kWw1rCF4DGr4rXwb_yoWruryxpr W3CFy5Gr1jgryfWF4Sqw4DXF1a93W8trWUJF9xW3sYvayDJry3KF4rtFyYya9YkrW8W3W3 ZF4jkryjga1jyrJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUm014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67 kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY 6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0x vEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVj vjDU0xZFpf9x0JULBMNUUUUU= X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" From: Zhang Yi After large folios are supported on ext4, writing back a sufficiently large and discontinuous folio may consume a significant number of journal credits, placing considerable strain on the journal. For example, in a 20GB filesystem with 1K block size and 1MB journal size, writing back a 2MB folio could require thousands of credits in the worst-case scenario (when each block is discontinuous and distributed across different block groups), potentially exceeding the journal size. This issue can also occur in ext4_write_begin() and ext4_page_mkwrite() when delalloc is not enabled. Fix this by ensuring that there are sufficient journal credits before allocating an extent in mpage_map_one_extent() and _ext4_get_block(). If there are not enough credits, return -EAGAIN, exit the current mapping loop, restart a new handle and a new transaction, and allocating blocks on this folio again in the next iteration. Suggested-by: Jan Kara Signed-off-by: Zhang Yi --- fs/ext4/inode.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index d0db6e3bf158..b51de58518b2 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -877,20 +877,44 @@ static void ext4_update_bh_state(struct buffer_head *= bh, unsigned long flags) } while (unlikely(!try_cmpxchg(&bh->b_state, &old_state, new_state))); } =20 +/* + * Make sure that the current journal transaction has enough credits to map + * one extent. Return -EAGAIN if it cannot extend the current running + * transaction. + */ +static inline int ext4_journal_ensure_extent_credits(handle_t *handle, + struct inode *inode) +{ + int needed_credits; + int ret; + + needed_credits =3D ext4_chunk_trans_blocks(inode, 1); + ret =3D __ext4_journal_ensure_credits(handle, needed_credits, + needed_credits, 0); + return ret <=3D 0 ? ret : -EAGAIN; +} + static int _ext4_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh, int flags) { struct ext4_map_blocks map; + handle_t *handle =3D ext4_journal_current_handle(); int ret =3D 0; =20 if (ext4_has_inline_data(inode)) return -ERANGE; =20 + /* Make sure transaction has enough credits for this extent */ + if (flags & EXT4_GET_BLOCKS_CREATE) { + ret =3D ext4_journal_ensure_extent_credits(handle, inode); + if (ret) + return ret; + } + map.m_lblk =3D iblock; map.m_len =3D bh->b_size >> inode->i_blkbits; =20 - ret =3D ext4_map_blocks(ext4_journal_current_handle(), inode, &map, - flags); + ret =3D ext4_map_blocks(handle, inode, &map, flags); if (ret > 0) { map_bh(bh, inode->i_sb, map.m_pblk); ext4_update_bh_state(bh, map.m_flags); @@ -1374,8 +1398,9 @@ static int ext4_write_begin(struct file *file, struct= address_space *mapping, ext4_orphan_del(NULL, inode); } =20 - if (ret =3D=3D -ENOSPC && - ext4_should_retry_alloc(inode->i_sb, &retries)) + if (ret =3D=3D -EAGAIN || + (ret =3D=3D -ENOSPC && + ext4_should_retry_alloc(inode->i_sb, &retries))) goto retry_journal; folio_put(folio); return ret; @@ -2324,6 +2349,11 @@ static int mpage_map_one_extent(handle_t *handle, st= ruct mpage_da_data *mpd) int get_blocks_flags; int err, dioread_nolock; =20 + /* Make sure transaction has enough credits for this extent */ + err =3D ext4_journal_ensure_extent_credits(handle, inode); + if (err < 0) + return err; + trace_ext4_da_write_pages_extent(inode, map); /* * Call ext4_map_blocks() to allocate any delayed allocation blocks, or @@ -2446,7 +2476,7 @@ static int mpage_map_and_submit_extent(handle_t *hand= le, * In the case of ENOSPC, if ext4_count_free_blocks() * is non-zero, a commit should free up blocks. */ - if ((err =3D=3D -ENOMEM) || + if ((err =3D=3D -ENOMEM) || (err =3D=3D -EAGAIN) || (err =3D=3D -ENOSPC && ext4_count_free_clusters(sb))) { /* * We may have already allocated extents for @@ -2953,6 +2983,8 @@ static int ext4_do_writepages(struct mpage_da_data *m= pd) ret =3D 0; continue; } + if (ret =3D=3D -EAGAIN) + ret =3D 0; /* Fatal error - ENOMEM, EIO... */ if (ret) break; @@ -6722,7 +6754,8 @@ vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) } } ext4_journal_stop(handle); - if (err =3D=3D -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) + if (err =3D=3D -EAGAIN || + (err =3D=3D -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)= )) goto retry_alloc; out_ret: ret =3D vmf_fs_error(err); --=20 2.46.1 From nobody Sat Oct 11 04:07:54 2025 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 84F502609DC; Wed, 11 Jun 2025 11:29:42 +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=1749641384; cv=none; b=QfZVk1jKAhqxoRgh4i6daz78KgY6RhrT+mzV5SgIjDW66xK8C/v870YEO84nErc5h369X4B1iF3OLoi9z0N/FgyleZ8YzCtP67niRFELyB+2KEIYajqxPwPR3+SWWt/A51+oUnKwIBw3VdT6yU+ih2aCVAjR7sCy7wnxISaBlTU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749641384; c=relaxed/simple; bh=NtdCfMcf2r/v8qSftAZ4GJ0vPtaPTT6F5ZXD12ZoS0g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VChQA8cOTfuCBkio2MSHE2ihOZ0jlOPOdMSru8zMH57dItYVNeQsxda3h5agsZxGyqwVjSgm2OJIrOhxXe3i89xVz7RHlivEWdy+tpWV13OQwLXcFBL+DE4CK2Dno9ACaoH1TmPIuxv4q6cC+P3aT1IqDATBWj0jxuTeE4ioq/A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4bHNjd4klbzYQvvZ; Wed, 11 Jun 2025 19:29:41 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id A361B1A0C76; Wed, 11 Jun 2025 19:29:40 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.112.188]) by APP3 (Coremail) with SMTP id _Ch0CgAXacOXaElofvDPOw--.32023S8; Wed, 11 Jun 2025 19:29: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, ojaswin@linux.ibm.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, libaokun1@huawei.com, yukuai3@huawei.com, yangerkun@huawei.com Subject: [PATCH v2 4/6] ext4: correct the reserved credits for extent conversion Date: Wed, 11 Jun 2025 19:16:23 +0800 Message-ID: <20250611111625.1668035-5-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20250611111625.1668035-1-yi.zhang@huaweicloud.com> References: <20250611111625.1668035-1-yi.zhang@huaweicloud.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: _Ch0CgAXacOXaElofvDPOw--.32023S8 X-Coremail-Antispam: 1UD129KBjvJXoW7Ar4kAw4fJFyDJFyxZr45trb_yoW8GF4UpF nxGFykWF18ua4kua1S9anrZF1rCay8Cw4UJF4fCw4DXa98Grn7Kr1qqw1Fy3WUJrWxJrWr ZF47CryDu3W3ZrJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmI14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkE bVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCw CI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnI WIevJa73UjIFyTuYvjfUo73vUUUUU X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" From: Zhang Yi Now, we reserve journal credits for converting extents in only one page to written state when the I/O operation is complete. This is insufficient when large folio is enabled. Fix this by reserving credits for converting up to one extent per block in the largest 2MB folio, this calculation should only involve extents index and leaf blocks, so it should not estimate too many credits. Fixes: 7ac67301e82f ("ext4: enable large folio for regular file") Signed-off-by: Zhang Yi Reviewed-by: Jan Kara Reviewed-by: Baokun Li --- fs/ext4/inode.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index b51de58518b2..67e37dd546eb 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -2848,12 +2848,12 @@ static int ext4_do_writepages(struct mpage_da_data = *mpd) mpd->journalled_more_data =3D 0; =20 if (ext4_should_dioread_nolock(inode)) { + int bpf =3D ext4_journal_blocks_per_folio(inode); /* * We may need to convert up to one extent per block in - * the page and we may dirty the inode. + * the folio and we may dirty the inode. */ - rsv_blocks =3D 1 + ext4_chunk_trans_blocks(inode, - PAGE_SIZE >> inode->i_blkbits); + rsv_blocks =3D 1 + ext4_ext_index_trans_blocks(inode, bpf); } =20 if (wbc->range_start =3D=3D 0 && wbc->range_end =3D=3D LLONG_MAX) --=20 2.46.1 From nobody Sat Oct 11 04:07:54 2025 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 62F8825BF1F; Wed, 11 Jun 2025 11:29:43 +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=1749641385; cv=none; b=cchK0SvTAUIdNeBZt4+oZzApPzp4XZoxMZIxgKr3MAYRcDptYOfjBS78yrQnCjv+UkKu3C5wXdCUSfUDgnZw8h7y6CMzDltSy3uSUnlX7D5y9Ns8LN5iixjJfLWBMl3CVe0iInZ1HqWGY8Z1jBode4QaM2fWcNtE3oERmFel6Ok= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749641385; c=relaxed/simple; bh=IV36qXYR1r6Yzg131vx3tGM/NHJrjbq6W7QxVFzIZKY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=In0affXT82sz2jJok51YEtsT2/XdVyLiScMA64uJvtEM7vMZEA7Tx7JWeJkqAEth1LoiihXShxagQRRhW9pj04WG6CqZDCdursN/jHiUYC75Ey2CAJ9ydZcNE7VkPpTfxudYzlcGkAtJ22wySz5cC02sZAPqSURpVZYQdXZyhvI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4bHNjf5bxtzKHNPG; Wed, 11 Jun 2025 19:29:42 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 259831A1883; Wed, 11 Jun 2025 19:29:41 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.112.188]) by APP3 (Coremail) with SMTP id _Ch0CgAXacOXaElofvDPOw--.32023S9; Wed, 11 Jun 2025 19:29: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, ojaswin@linux.ibm.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, libaokun1@huawei.com, yukuai3@huawei.com, yangerkun@huawei.com Subject: [PATCH v2 5/6] ext4/jbd2: reintroduce jbd2_journal_blocks_per_page() Date: Wed, 11 Jun 2025 19:16:24 +0800 Message-ID: <20250611111625.1668035-6-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20250611111625.1668035-1-yi.zhang@huaweicloud.com> References: <20250611111625.1668035-1-yi.zhang@huaweicloud.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: _Ch0CgAXacOXaElofvDPOw--.32023S9 X-Coremail-Antispam: 1UD129KBjvJXoWxCFW3uFW3ZFy8Gry7KrWDXFb_yoWrXry7pF ZrCFyrCr95uryDuFs7Wr4UZry2ga40kFWUWr9a9FnYqa9Fq34xtFnrtw1ayFy5trWDGa10 vF45G3yDGw4Dt37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmI14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkE bVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCw CI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnI WIevJa73UjIFyTuYvjfUo73vUUUUU X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" From: Zhang Yi This partially reverts commit d6bf294773a4 ("ext4/jbd2: convert jbd2_journal_blocks_per_page() to support large folio"). This jbd2_journal_blocks_per_folio() will lead to a significant overestimation of journal credits. Since we still reserve credits for one page and attempt to extend and restart handles during large folio writebacks, so we should convert this helper back to ext4_journal_blocks_per_page(). Signed-off-by: Zhang Yi --- fs/ext4/ext4_jbd2.h | 7 +++++++ fs/ext4/inode.c | 6 +++--- fs/jbd2/journal.c | 6 ++++++ include/linux/jbd2.h | 1 + 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h index 63d17c5201b5..c0ee756cb34c 100644 --- a/fs/ext4/ext4_jbd2.h +++ b/fs/ext4/ext4_jbd2.h @@ -326,6 +326,13 @@ static inline int ext4_journal_blocks_per_folio(struct= inode *inode) return 0; } =20 +static inline int ext4_journal_blocks_per_page(struct inode *inode) +{ + if (EXT4_JOURNAL(inode) !=3D NULL) + return jbd2_journal_blocks_per_page(inode); + return 0; +} + static inline int ext4_journal_force_commit(journal_t *journal) { if (journal) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 67e37dd546eb..9835145b1b27 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -2556,7 +2556,7 @@ static int mpage_map_and_submit_extent(handle_t *hand= le, */ static int ext4_da_writepages_trans_blocks(struct inode *inode) { - int bpp =3D ext4_journal_blocks_per_folio(inode); + int bpp =3D ext4_journal_blocks_per_page(inode); =20 return ext4_meta_trans_blocks(inode, MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); @@ -2634,7 +2634,7 @@ static int mpage_prepare_extent_to_map(struct mpage_d= a_data *mpd) ext4_lblk_t lblk; struct buffer_head *head; handle_t *handle =3D NULL; - int bpp =3D ext4_journal_blocks_per_folio(mpd->inode); + int bpp =3D ext4_journal_blocks_per_page(mpd->inode); =20 if (mpd->wbc->sync_mode =3D=3D WB_SYNC_ALL || mpd->wbc->tagged_writepages) tag =3D PAGECACHE_TAG_TOWRITE; @@ -6255,7 +6255,7 @@ int ext4_meta_trans_blocks(struct inode *inode, int l= blocks, int pextents) */ int ext4_writepage_trans_blocks(struct inode *inode) { - int bpp =3D ext4_journal_blocks_per_folio(inode); + int bpp =3D ext4_journal_blocks_per_page(inode); int ret; =20 ret =3D ext4_meta_trans_blocks(inode, bpp, bpp); diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c index d480b94117cd..7fccb425907f 100644 --- a/fs/jbd2/journal.c +++ b/fs/jbd2/journal.c @@ -84,6 +84,7 @@ EXPORT_SYMBOL(jbd2_journal_start_commit); EXPORT_SYMBOL(jbd2_journal_force_commit_nested); EXPORT_SYMBOL(jbd2_journal_wipe); EXPORT_SYMBOL(jbd2_journal_blocks_per_folio); +EXPORT_SYMBOL(jbd2_journal_blocks_per_page); EXPORT_SYMBOL(jbd2_journal_invalidate_folio); EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); EXPORT_SYMBOL(jbd2_journal_force_commit); @@ -2661,6 +2662,11 @@ int jbd2_journal_blocks_per_folio(struct inode *inod= e) inode->i_sb->s_blocksize_bits); } =20 +int jbd2_journal_blocks_per_page(struct inode *inode) +{ + return 1 << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); +} + /* * helper functions to deal with 32 or 64bit block numbers. */ diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h index 43b9297fe8a7..f35369c104ba 100644 --- a/include/linux/jbd2.h +++ b/include/linux/jbd2.h @@ -1724,6 +1724,7 @@ static inline int tid_geq(tid_t x, tid_t y) } =20 extern int jbd2_journal_blocks_per_folio(struct inode *inode); +extern int jbd2_journal_blocks_per_page(struct inode *inode); extern size_t journal_tag_bytes(journal_t *journal); =20 static inline int jbd2_journal_has_csum_v2or3(journal_t *journal) --=20 2.46.1 From nobody Sat Oct 11 04:07:54 2025 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 E6C5325D8F5; Wed, 11 Jun 2025 11:29:43 +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=1749641386; cv=none; b=rXvo60JG27lzH3DxWAvIo0OYvALpG3mozuIc8/hFXilFPVHWevSEqy446IGo98hJ1zjj5mW7OTEeBrABJx9en33Wpnihhny6m/WC+3vrM3GH2foktu6rAPGJIz15wJR9F77ayD5UREFWacOMlpRkVmTkZtdxccE1J3CxnVPXC7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749641386; c=relaxed/simple; bh=PjZbjonEikl0ssZOOSWJaqyCJF5bAU8HJZK179JXyJg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D/vvBgQlHz7y5lblLXN2dIEoylMkRW+k3gSyTdE4CQhJ20835Id2Wz05HQIBON8T3Blgtykd9AKQKJiTPhQjyCvtueJKnOCYce/TcZMx8gZqL66FCPKSs7PLd8dmUYj9hHdJVUp13c3bongqg0yThlcg1CUkX2RdkyTSKgOV+FE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.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 ESMTPS id 4bHNjg2KxmzKHNRX; Wed, 11 Jun 2025 19:29:43 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id A7A241A0E76; Wed, 11 Jun 2025 19:29:41 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.112.188]) by APP3 (Coremail) with SMTP id _Ch0CgAXacOXaElofvDPOw--.32023S10; Wed, 11 Jun 2025 19:29: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, ojaswin@linux.ibm.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, libaokun1@huawei.com, yukuai3@huawei.com, yangerkun@huawei.com Subject: [PATCH v2 6/6] ext4: fix insufficient credits calculation in ext4_meta_trans_blocks() Date: Wed, 11 Jun 2025 19:16:25 +0800 Message-ID: <20250611111625.1668035-7-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20250611111625.1668035-1-yi.zhang@huaweicloud.com> References: <20250611111625.1668035-1-yi.zhang@huaweicloud.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: _Ch0CgAXacOXaElofvDPOw--.32023S10 X-Coremail-Antispam: 1UD129KBjvJXoW7uFy7Zw48KrW5GFWrCF1fCrg_yoW8XF4xp3 Z3CF1rJ34rWw4v9a18Ww42qr48Ka1kGa17uFWfAw15JFZxZr92krnFq34rAa45tFWfKw1q qF4Yyry3Gw1UArDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUma14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkE bVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCw CI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUvcSsG vfC2KfnxnUUI43ZEXa7VUbAnY7UUUUU== X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" From: Zhang Yi The calculation of journal credits in ext4_meta_trans_blocks() should include pextents, as each extent separately may be allocated from a different group and thus need to update different bitmap and group descriptor block. Fixes: 0e32d8617012 ("ext4: correct the journal credits calculations of all= ocating blocks") Reported-by: Jan Kara Closes: https://lore.kernel.org/linux-ext4/nhxfuu53wyacsrq7xqgxvgzcggyscu2t= babginahcygvmc45hy@t4fvmyeky33e/ Signed-off-by: Zhang Yi Reviewed-by: Jan Kara Reviewed-by: Baokun Li --- fs/ext4/inode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 9835145b1b27..9b6ebf823740 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -6218,7 +6218,7 @@ int ext4_meta_trans_blocks(struct inode *inode, int l= blocks, int pextents) int ret; =20 /* - * How many index and lead blocks need to touch to map @lblocks + * How many index and leaf blocks need to touch to map @lblocks * logical blocks to @pextents physical extents? */ idxblocks =3D ext4_index_trans_blocks(inode, lblocks, pextents); @@ -6227,7 +6227,7 @@ int ext4_meta_trans_blocks(struct inode *inode, int l= blocks, int pextents) * Now let's see how many group bitmaps and group descriptors need * to account */ - groups =3D idxblocks; + groups =3D idxblocks + pextents; gdpblocks =3D groups; if (groups > ngroups) groups =3D ngroups; --=20 2.46.1