linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: don't allocate blocks beyond EOF from __mpage_writepage
@ 2023-01-03 10:44 Jan Kara
  2023-01-04  0:02 ` Al Viro
  2023-01-08 17:25 ` Christoph Hellwig
  0 siblings, 2 replies; 8+ messages in thread
From: Jan Kara @ 2023-01-03 10:44 UTC (permalink / raw)
  To: linux-fsdevel; +Cc: Matthew Wilcox, Christoph Hellwig, Andrew Morton, Jan Kara

When __mpage_writepage() is called for a page beyond EOF, it will go and
allocate all blocks underlying the page. This is not only unnecessary
but this way blocks can get leaked (e.g. if a page beyond EOF is marked
dirty but in the end write fails and i_size is not extended).

Signed-off-by: Jan Kara <jack@suse.cz>
---
 fs/mpage.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/fs/mpage.c b/fs/mpage.c
index 0f8ae954a579..9f040c1d5912 100644
--- a/fs/mpage.c
+++ b/fs/mpage.c
@@ -524,6 +524,12 @@ static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
 	 */
 	BUG_ON(!PageUptodate(page));
 	block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
+	/*
+	 * Whole page beyond EOF? Skip allocating blocks to avoid leaking
+	 * space.
+	 */
+	if (block_in_file >= (i_size + (1 << blkbits) - 1) >> blkbits)
+		goto page_is_mapped;
 	last_block = (i_size - 1) >> blkbits;
 	map_bh.b_page = page;
 	for (page_block = 0; page_block < blocks_per_page; ) {
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-01-26  8:42 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-03 10:44 [PATCH] fs: don't allocate blocks beyond EOF from __mpage_writepage Jan Kara
2023-01-04  0:02 ` Al Viro
2023-01-04  8:41   ` Jan Kara
2023-01-08 17:25 ` Christoph Hellwig
2023-01-25 14:23   ` Jan Kara
2023-01-25 15:45     ` Matthew Wilcox
2023-01-26  0:52     ` Andrew Morton
2023-01-26  8:42       ` Jan Kara

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).