udf: Fix data corruption with fsx
Subsystems (3)
Jan Kara
reviewercover udf: Fix data corruption with fsx
Hello,
when running fsx on UDF I've spotted several data corruption issues mostly involving handling of holes and preallocation extents (which people usually don't trip on because if UDF is used in RW mode, it is usually on USB sticks to copy files in and out...). This series fixes them so fsx doesn't show any more issues. Unless someone objects, I'll queue these fixes to my tree.
Honza
Jan Kara
reviewerPATCH 1/4 udf: Fix preallocation discarding at indirect extent boundary
When preallocation extent is the first one in the extent block, the code would corrupt extent tree header instead. Fix the problem and use udf_delete_aext() for deleting extent to avoid some code duplication.
--- fs/udf/truncate.c | 45 +++++++++++++-------------------------------- 1 file changed, 13 insertions(+), 32 deletions(-)
fs/udf/truncate.c +13 -32
@@ -120,60 +120,41 @@ void udf_truncate_tail_extent(struct inode *inode)void udf_discard_prealloc (struct inode *inode) { -struct extent_position epos = {NULL ,0 , {0 ,0 } }; +struct extent_position epos = {}; +struct extent_position prev_epos = {};struct kernel_lb_addr eloc;uint32_t elen;uint64_t lbcount =0 ;int8_t etype =-1 , netype; -int adsize;struct udf_inode_info *iinfo =UDF_I (inode);if (iinfo->i_alloc_type ==ICBTAG_FLAG_AD_IN_ICB || inode->i_size == iinfo->i_lenExtents)return ; -if (iinfo->i_alloc_type ==ICBTAG_FLAG_AD_SHORT ) - adsize =sizeof (struct short_ad ); -else if (iinfo->i_alloc_type ==ICBTAG_FLAG_AD_LONG ) - adsize =sizeof (struct long_ad ); -else - adsize =0 ; - epos.block = iinfo->i_location;/* Find the last extent in the file */ -while ((netype =udf_next_aext (inode, &epos, &eloc, &elen,1 )) !=-1 ) { - etype = netype; +while ((netype =udf_next_aext (inode, &epos, &eloc, &elen,0 )) !=-1 ) { +brelse (prev_epos.bh); + prev_epos = epos; +if (prev_epos.bh) +get_bh (prev_epos.bh); + + etype =udf_next_aext (inode, &epos, &eloc, &elen,1 ); lbcount += elen; }if (etype == (EXT_NOT_RECORDED_ALLOCATED >>30 )) { - epos.offset -= adsize; lbcount -= elen; -extent_trunc (inode, &epos, &eloc, etype, elen,0 ); -if (!epos.bh) { - iinfo->i_lenAlloc = - epos.offset - -udf_file_entry_alloc_offset (inode); -mark_inode_dirty (inode); - }else { -struct allocExtDesc *aed = - (struct allocExtDesc *)(epos.bh->b_data); - aed->lengthAllocDescs = -cpu_to_le32 (epos.offset - -sizeof (struct allocExtDesc )); -if (!UDF_QUERY_FLAG (inode->i_sb,UDF_FLAG_STRICT ) || -UDF_SB (inode->i_sb)->s_udfrev >=0x0201 ) -udf_update_tag (epos.bh->b_data, epos.offset); -else -udf_update_tag (epos.bh->b_data, -sizeof (struct allocExtDesc )); -mark_buffer_dirty_inode (epos.bh, inode); - } +udf_delete_aext (inode, prev_epos); +udf_free_blocks (inode->i_sb, inode, &eloc,0 , +DIV_ROUND_UP (elen,1 << inode->i_blkbits)); }/* This inode entry is in-memory only and thus we don't have to mark * the inode dirty */ iinfo->i_lenExtents = lbcount;brelse (epos.bh); +brelse (prev_epos.bh); } static void udf_update_alloc_ext_desc(struct inode *inode,
Signature · 1 line
2.35.3
Jan Kara
reviewerPATCH 2/4 udf: Do not bother looking for prealloc extents if i_lenExtents matches i_size
In reply to Jan KaraIf rounded block-rounded i_lenExtents matches block rounded i_size, there are no preallocation extents. Do not bother walking extent linked list.
--- fs/udf/truncate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/udf/truncate.c +2 -1
@@ -127,9 +127,10 @@ void udf_discard_prealloc(struct inode *inode)uint64_t lbcount =0 ;int8_t etype =-1 , netype;struct udf_inode_info *iinfo =UDF_I (inode); +int bsize =1 << inode->i_blkbits;if (iinfo->i_alloc_type ==ICBTAG_FLAG_AD_IN_ICB || - inode->i_size == iinfo->i_lenExtents) +ALIGN (inode->i_size, bsize) ==ALIGN (iinfo->i_lenExtents, bsize))return ; epos.block = iinfo->i_location;
Signature · 1 line
2.35.3
Jan Kara
reviewerPATCH 3/4 udf: Discard preallocation before extending file with a hole
In reply to Jan KaraWhen extending file with a hole, we tried to preserve existing preallocation for the file. However that is not very useful and complicates code because the previous extent may need to be rounded to block boundary as well (which we forgot to do thus causing data corruption for sequence like:
xfs_io -f -c "pwrite 0x75e63 11008" -c "truncate 0x7b24b" \
-c "truncate 0xabaa3" -c "pwrite 0xac70b 22954" \
-c "pwrite 0x93a43 11358" -c "pwrite 0xb8e65 52211" file
with 512-byte block size. Just discard preallocation before extending file to simplify things and also fix this data corruption.
--- fs/udf/inode.c | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-)
fs/udf/inode.c +18 -28
@@ -349,6 +349,12 @@ static int udf_get_block(struct inode *inode, sector_t block, iinfo->i_next_alloc_goal++; } +/* +* Block beyond EOF and prealloc extents? Just discard preallocation +* as it is not useful and complicates things. +*/ +if (((loff_t )block) << inode->i_blkbits > iinfo->i_lenExtents) +udf_discard_prealloc (inode);udf_clear_extent_cache (inode); phys =inode_getblk (inode, block, &err, &new);if (!phys)
@@ -398,8 +404,6 @@ static int udf_do_extend_file(struct inode *inode,uint32_t add;int count =0 , fake = !(last_ext->extLength &UDF_EXTENT_LENGTH_MASK );struct super_block *sb = inode->i_sb; -struct kernel_lb_addr prealloc_loc = {}; -uint32_t prealloc_len =0 ;struct udf_inode_info *iinfo;int err;
@@ -420,19 +424,6 @@ static int udf_do_extend_file(struct inode *inode, ~(sb->s_blocksize -1 ); } -/* Last extent are just preallocated blocks? */ -if ((last_ext->extLength &UDF_EXTENT_FLAG_MASK ) == -EXT_NOT_RECORDED_ALLOCATED ) { -/* Save the extent so that we can reattach it to the end */ - prealloc_loc = last_ext->extLocation; - prealloc_len = last_ext->extLength; -/* Mark the extent as a hole */ - last_ext->extLength =EXT_NOT_RECORDED_NOT_ALLOCATED | - (last_ext->extLength &UDF_EXTENT_LENGTH_MASK ); - last_ext->extLocation.logicalBlockNum =0 ; - last_ext->extLocation.partitionReferenceNum =0 ; - } -/* Can we merge with the previous extent? */ if ((last_ext->extLength &UDF_EXTENT_FLAG_MASK ) ==EXT_NOT_RECORDED_NOT_ALLOCATED ) {
@@ -460,7 +451,7 @@ static int udf_do_extend_file(struct inode *inode,* more extents, we may need to enter possible following * empty indirect extent. */ -if (new_block_bytes || prealloc_len) +if (new_block_bytes)udf_next_aext (inode, last_pos, &tmploc, &tmplen,0 ); }
@@ -494,17 +485,6 @@ static int udf_do_extend_file(struct inode *inode, } out: -/* Do we have some preallocated blocks saved? */ -if (prealloc_len) { - err =udf_add_aext (inode, last_pos, &prealloc_loc, - prealloc_len,1 ); -if (err) -return err; - last_ext->extLocation = prealloc_loc; - last_ext->extLength = prealloc_len; - count++; - } -/* last_pos should point to the last written extent... */ if (iinfo->i_alloc_type ==ICBTAG_FLAG_AD_SHORT ) last_pos->offset -=sizeof (struct short_ad );
@@ -557,8 +537,17 @@ static int udf_extend_file(struct inode *inode, loff_t newsize)else BUG (); +/* +* When creating hole in file, just don't bother with preserving +* preallocation. It likely won't be very useful anyway. +*/ +udf_discard_prealloc (inode); + etype =inode_bmap (inode, first_block, &epos, &eloc, &elen, &offset); within_final_block = (etype !=-1 ); +/* We don't expect extents past EOF... */ +WARN_ON_ONCE (etype !=-1 && + elen > ((loff_t )offset +1 ) << inode->i_blkbits);if ((!epos.bh && epos.offset ==udf_file_entry_alloc_offset (inode)) || (epos.bh && epos.offset ==sizeof (struct allocExtDesc ))) {
@@ -687,10 +676,11 @@ static sector_t inode_getblk(struct inode *inode, sector_t block, goto out_free; } -/* Are we beyond EOF? */ +/* Are we beyond EOF and preallocated extent? */ if (etype ==-1 ) {int ret;loff_t hole_len; + isBeyondEOF = true;if (count) {if (c)
Signature · 1 line
2.35.3
Jan Kara
reviewerPATCH 4/4 udf: Fix extending file within last block
In reply to Jan KaraWhen extending file within last block it can happen that the extent is already rounded to the blocksize and thus contains the offset we want to grow up to. In such case we would mistakenly expand the last extent and make it one block longer than it should be, exposing unallocated block in a file and causing data corruption. Fix the problem by properly detecting this case and bailing out.
--- fs/udf/inode.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-)
fs/udf/inode.c +17 -15
@@ -500,13 +500,17 @@ static int udf_do_extend_file(struct inode *inode,static void udf_do_extend_final_block (struct inode *inode,struct extent_position *last_pos,struct kernel_long_ad *last_ext, -uint32_t final_block_len) +uint32_t new_elen) { -struct super_block *sb = inode->i_sb;uint32_t added_bytes; - added_bytes = final_block_len - - (last_ext->extLength & (sb->s_blocksize -1 )); +/* +* Extent already large enough? It may be already rounded up to block +* size... +*/ +if (new_elen <= (last_ext->extLength &UDF_EXTENT_LENGTH_MASK )) +return ; + added_bytes = (last_ext->extLength &UDF_EXTENT_LENGTH_MASK ) - new_elen; last_ext->extLength += added_bytes;UDF_I (inode)->i_lenExtents += added_bytes;
@@ -523,12 +527,12 @@ static int udf_extend_file(struct inode *inode, loff_t newsize)int8_t etype;struct super_block *sb = inode->i_sb;sector_t first_block = newsize >> sb->s_blocksize_bits, offset; -unsigned long partial_final_block; +loff_t new_elen;int adsize;struct udf_inode_info *iinfo =UDF_I (inode);struct kernel_long_ad extent;int err =0 ; -int within_final_block; +bool within_last_ext;if (iinfo->i_alloc_type ==ICBTAG_FLAG_AD_SHORT ) adsize =sizeof (struct short_ad );
@@ -544,9 +548,9 @@ static int udf_extend_file(struct inode *inode, loff_t newsize)udf_discard_prealloc (inode); etype =inode_bmap (inode, first_block, &epos, &eloc, &elen, &offset); - within_final_block = (etype !=-1 ); + within_last_ext = (etype !=-1 );/* We don't expect extents past EOF... */ -WARN_ON_ONCE (etype !=-1 && +WARN_ON_ONCE (within_last_ext && elen > ((loff_t )offset +1 ) << inode->i_blkbits); if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
@@ -563,19 +567,17 @@ static int udf_extend_file(struct inode *inode, loff_t newsize) extent.extLength |= etype <<30 ; } - partial_final_block = newsize & (sb->s_blocksize -1 ); + new_elen = ((loff_t )offset << inode->i_blkbits) | + (newsize & (sb->s_blocksize -1 ));/* File has extent covering the new size (could happen when extending * inside a block)? */ -if (within_final_block) { +if (within_last_ext) {/* Extending file within the last file block */ -udf_do_extend_final_block (inode, &epos, &extent, - partial_final_block); +udf_do_extend_final_block (inode, &epos, &extent, new_elen); }else { -loff_t add = ((loff_t )offset << sb->s_blocksize_bits) | - partial_final_block; - err =udf_do_extend_file (inode, &epos, &extent, add); + err =udf_do_extend_file (inode, &epos, &extent, new_elen); }if (err <0 )
Signature · 1 line
2.35.3