Skip to contentSkip to conversation

udf: Fix handling of in-ICB files

PATCH In review Jan Kara 2 participants 6 messages Mar 1, 2023

Subsystems (2)
lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

cover udf: Fix handling of in-ICB files

Hello,

conversion of UDF to use the same address_space operations for normal an inline (in-ICB) files had two bugs resulting in buffered reads / writes not really working properly for in-ICB files. Interestingly none of the xfstests discovered this (likely because files have to be small to fit within ICB and you have to evict the first file page from the page cache to notice). I've now verified with manual testing that read & writes to in-ICB files work again after these fixes. I plan to push these fixes to Linus through my tree soon.

Honza

author reviewer
#1 lore raw

PATCH 1/3 udf: Fix lost writes in udf_adinicb_writepage()

The patch converting udf_adinicb_writepage() to avoid manually kmapping the page used memcpy_to_page() however that copies in the wrong direction (effectively overwriting file data with the old contents). What we should be using is memcpy_from_page() to copy data from the page into the inode and then mark inode dirty to store the data.

Fixes 5cfc45321a6d ("udf: Convert udf_adinicb_writepage() to memcpy_to_page()")
---
 fs/udf/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/udf/inode.c +1 -1
index f7a9607c2b95..facaf3a20625 100644
@@ -193,7 +193,7 @@ static int udf_adinicb_writepage(struct folio *folio,
 	struct udf_inode_info *iinfo = UDF_I(inode);
 
 	BUG_ON(!PageLocked(page));
-	memcpy_to_page(page, 0, iinfo->i_data + iinfo->i_lenEAttr,
+	memcpy_from_page(iinfo->i_data + iinfo->i_lenEAttr, page, 0,
 		       i_size_read(inode));
 	unlock_page(page);
 	mark_inode_dirty(inode);
Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH 1/3] udf: Fix lost writes in udf_adinicb_writepage()

Quoted 8 lines · Jan Kara

The patch converting udf_adinicb_writepage() to avoid manually kmapping the page used memcpy_to_page() however that copies in the wrong direction (effectively overwriting file data with the old contents). What we should be using is memcpy_from_page() to copy data from the page into the inode and then mark inode dirty to store the data.

Fixes 5cfc45321a6d ("udf: Convert udf_adinicb_writepage() to memcpy_to_page()")

Oops. Now you're copying in the right direction, we have a folio function for that, so we could just folio-ise the entire function? Maybe you'd rather keep the fix minimal and apply this later.

fs/udf/inode.c +5 -5
index f7a9607c2b95..890be63ddd02 100644
@@ -188,14 +188,14 @@ static void udf_write_failed(struct address_space *mapping, loff_t to)
 static int udf_adinicb_writepage(struct folio *folio,
 				 struct writeback_control *wbc, void *data)
 {
-	struct page *page = &folio->page;
-	struct inode *inode = page->mapping->host;
+	struct inode *inode = folio->mapping->host;
 	struct udf_inode_info *iinfo = UDF_I(inode);
 
-	BUG_ON(!PageLocked(page));
-	memcpy_to_page(page, 0, iinfo->i_data + iinfo->i_lenEAttr,
+	BUG_ON(!folio_test_locked(folio));
+	BUG_ON(folio->index != 0);
+	memcpy_from_file_folio(iinfo->i_data + iinfo->i_lenEAttr, folio, 0,
 		       i_size_read(inode));
-	unlock_page(page);
+	folio_unlock(folio);
 	mark_inode_dirty(inode);
 
 	return 0;
author reviewer
#3 lore raw

Re: [PATCH 1/3] udf: Fix lost writes in udf_adinicb_writepage()

Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · Jan Kara

The patch converting udf_adinicb_writepage() to avoid manually kmapping the page used memcpy_to_page() however that copies in the wrong direction (effectively overwriting file data with the old contents). What we should be using is memcpy_from_page() to copy data from the page into the inode and then mark inode dirty to store the data.

Quoted 8 lines
Quoted 2 lines
Fixes 5cfc45321a6d ("udf: Convert udf_adinicb_writepage() to memcpy_to_page()")

Oops. Now you're copying in the right direction, we have a folio function for that, so we could just folio-ise the entire function? Maybe you'd rather keep the fix minimal and apply this later.

Thanks! Yes, I had this in mind but wanted to fix the corruption first so that I can push it quickly to rc2... But I'll queue your patch for later.

Honza

Quoted 25 lines
fs/udf/inode.c +5 -5
index f7a9607c2b95..890be63ddd02 100644
@@ -188,14 +188,14 @@ static void udf_write_failed(struct address_space *mapping, loff_t to)
 static int udf_adinicb_writepage(struct folio *folio,
 				 struct writeback_control *wbc, void *data)
 {
-	struct page *page = &folio->page;
-	struct inode *inode = page->mapping->host;
+	struct inode *inode = folio->mapping->host;
 	struct udf_inode_info *iinfo = UDF_I(inode);
 
-	BUG_ON(!PageLocked(page));
-	memcpy_to_page(page, 0, iinfo->i_data + iinfo->i_lenEAttr,
+	BUG_ON(!folio_test_locked(folio));
+	BUG_ON(folio->index != 0);
+	memcpy_from_file_folio(iinfo->i_data + iinfo->i_lenEAttr, folio, 0,
 		       i_size_read(inode));
-	unlock_page(page);
+	folio_unlock(folio);
 	mark_inode_dirty(inode);
 
 	return 0;
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#4 lore raw

PATCH 2/3 udf: Fix reading of in-ICB files

In reply to Jan Kara

After merging address space operations of normal and in-ICB files, readahead could get called for in-ICB files which resulted in udf_get_block() being called for these files. udf_get_block() is not prepared to be called for in-ICB files and ends up returning garbage results as it interprets file data as extent list. Fix the problem by skipping readahead for in-ICB files.

Fixes 37a8a39f7ad3 ("udf: Switch to single address_space_operations")
---
 fs/udf/inode.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/udf/inode.c +9 -0
index facaf3a20625..0cb7d8fba2c8 100644
@@ -241,6 +241,15 @@ static int udf_read_folio(struct file *file, struct folio *folio)
 
 static void udf_readahead(struct readahead_control *rac)
 {
+	struct udf_inode_info *iinfo = UDF_I(rac->mapping->host);
+
+	/*
+	 * No readahead needed for in-ICB files and udf_get_block() would get
+	 * confused for such file anyway.
+	 */
+	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
+		return;
+
 	mpage_readahead(rac, udf_get_block);
 }
 
Signature · 1 line
2.35.3
author reviewer
#5 lore raw

PATCH 3/3 udf: Warn if block mapping is done for in-ICB files

In reply to Jan Kara

Now that address space operations are merge dfor in-ICB and normal files, it is more likely some code mistakenly tries to map blocks for in-ICB files. WARN and return error instead of silently returning garbage.

---
 fs/udf/inode.c | 3 +++
 1 file changed, 3 insertions(+)
fs/udf/inode.c +3 -0
index 0cb7d8fba2c8..2210e5eb1ea0 100644
@@ -416,6 +416,9 @@ static int udf_map_block(struct inode *inode, struct udf_map_rq *map)
 	int err;
 	struct udf_inode_info *iinfo = UDF_I(inode);
 
+	if (WARN_ON_ONCE(iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB))
+		return -EFSCORRUPTED;
+
 	map->oflags = 0;
 	if (!(map->iflags & UDF_MAP_CREATE)) {
 		struct kernel_lb_addr eloc;
Signature · 1 line
2.35.3