diff options
Diffstat (limited to 'fs/udf/truncate.c')
| -rw-r--r-- | fs/udf/truncate.c | 411 |
1 files changed, 207 insertions, 204 deletions
diff --git a/fs/udf/truncate.c b/fs/udf/truncate.c index 7dc8a5572ca..8a9657d7f7c 100644 --- a/fs/udf/truncate.c +++ b/fs/udf/truncate.c @@ -4,11 +4,6 @@ * PURPOSE * Truncate handling routines for the OSTA-UDF(tm) filesystem. * - * CONTACTS - * E-mail regarding any portion of the Linux UDF file system should be - * directed to the development team mailing list (run by majordomo): - * linux_udf@hpesjro.fc.hp.com - * * COPYRIGHT * This file is distributed under the terms of the GNU General Public * License (GPL). Copies of the GPL can be obtained from: @@ -27,258 +22,266 @@ #include "udfdecl.h" #include <linux/fs.h> #include <linux/mm.h> -#include <linux/udf_fs.h> #include <linux/buffer_head.h> #include "udf_i.h" #include "udf_sb.h" -static void extent_trunc(struct inode * inode, kernel_lb_addr bloc, int extoffset, - kernel_lb_addr eloc, int8_t etype, uint32_t elen, struct buffer_head *bh, uint32_t nelen) +static void extent_trunc(struct inode *inode, struct extent_position *epos, + struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen, + uint32_t nelen) { - kernel_lb_addr neloc = { 0, 0 }; - int last_block = (elen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits; - int first_block = (nelen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits; + struct kernel_lb_addr neloc = {}; + int last_block = (elen + inode->i_sb->s_blocksize - 1) >> + inode->i_sb->s_blocksize_bits; + int first_block = (nelen + inode->i_sb->s_blocksize - 1) >> + inode->i_sb->s_blocksize_bits; - if (nelen) - { - if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) - { - udf_free_blocks(inode->i_sb, inode, eloc, 0, last_block); + if (nelen) { + if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { + udf_free_blocks(inode->i_sb, inode, eloc, 0, + last_block); etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30); - } - else - neloc = eloc; + } else + neloc = *eloc; nelen = (etype << 30) | nelen; } - if (elen != nelen) - { - udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 0); - if (last_block - first_block > 0) - { + if (elen != nelen) { + udf_write_aext(inode, epos, &neloc, nelen, 0); + if (last_block - first_block > 0) { if (etype == (EXT_RECORDED_ALLOCATED >> 30)) mark_inode_dirty(inode); if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) - udf_free_blocks(inode->i_sb, inode, eloc, first_block, last_block - first_block); + udf_free_blocks(inode->i_sb, inode, eloc, + first_block, + last_block - first_block); } } } -void udf_discard_prealloc(struct inode * inode) +/* + * Truncate the last extent to match i_size. This function assumes + * that preallocation extent is already truncated. + */ +void udf_truncate_tail_extent(struct inode *inode) { - kernel_lb_addr bloc, eloc; - uint32_t extoffset = 0, elen, nelen; + struct extent_position epos = {}; + struct kernel_lb_addr eloc; + uint32_t elen, nelen; uint64_t lbcount = 0; int8_t etype = -1, netype; - struct buffer_head *bh = NULL; int adsize; + struct udf_inode_info *iinfo = UDF_I(inode); - if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB || - inode->i_size == UDF_I_LENEXTENTS(inode)) - { + if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB || + inode->i_size == iinfo->i_lenExtents) + return; + /* Are we going to delete the file anyway? */ + if (inode->i_nlink == 0) 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 + BUG(); + + /* Find the last extent in the file */ + while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { + etype = netype; + lbcount += elen; + if (lbcount > inode->i_size) { + if (lbcount - inode->i_size >= inode->i_sb->s_blocksize) + udf_warn(inode->i_sb, + "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n", + (unsigned)inode->i_ino, + (long long)inode->i_size, + (long long)lbcount, + (unsigned)eloc.logicalBlockNum, + (unsigned)elen); + nelen = elen - (lbcount - inode->i_size); + epos.offset -= adsize; + extent_trunc(inode, &epos, &eloc, etype, elen, nelen); + epos.offset += adsize; + if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1) + udf_err(inode->i_sb, + "Extent after EOF in inode %u\n", + (unsigned)inode->i_ino); + break; + } } + /* This inode entry is in-memory only and thus we don't have to mark + * the inode dirty */ + iinfo->i_lenExtents = inode->i_size; + brelse(epos.bh); +} + +void udf_discard_prealloc(struct inode *inode) +{ + struct extent_position epos = { NULL, 0, {0, 0} }; + 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 (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT) - adsize = sizeof(short_ad); - else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG) - adsize = sizeof(long_ad); + 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; - bloc = UDF_I_LOCATION(inode); + epos.block = iinfo->i_location; - while ((netype = udf_next_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1) - { + /* Find the last extent in the file */ + while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { etype = netype; lbcount += elen; - if (lbcount > inode->i_size && lbcount - inode->i_size < inode->i_sb->s_blocksize) - { - nelen = elen - (lbcount - inode->i_size); - extent_trunc(inode, bloc, extoffset-adsize, eloc, etype, elen, bh, nelen); - lbcount = inode->i_size; - } } - if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) - { - extoffset -= adsize; + if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { + epos.offset -= adsize; lbcount -= elen; - extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, 0); - if (!bh) - { - UDF_I_LENALLOC(inode) = extoffset - udf_file_entry_alloc_offset(inode); + 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 *)(bh->b_data); - aed->lengthAllocDescs = cpu_to_le32(extoffset - sizeof(struct allocExtDesc)); - if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201) - udf_update_tag(bh->b_data, extoffset); + } 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(bh->b_data, sizeof(struct allocExtDesc)); - mark_buffer_dirty_inode(bh, inode); + udf_update_tag(epos.bh->b_data, + sizeof(struct allocExtDesc)); + mark_buffer_dirty_inode(epos.bh, inode); } } - UDF_I_LENEXTENTS(inode) = lbcount; + /* 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); +} + +static void udf_update_alloc_ext_desc(struct inode *inode, + struct extent_position *epos, + u32 lenalloc) +{ + struct super_block *sb = inode->i_sb; + struct udf_sb_info *sbi = UDF_SB(sb); - udf_release_data(bh); + struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data); + int len = sizeof(struct allocExtDesc); + + aed->lengthAllocDescs = cpu_to_le32(lenalloc); + if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201) + len += lenalloc; + + udf_update_tag(epos->bh->b_data, len); + mark_buffer_dirty_inode(epos->bh, inode); } -void udf_truncate_extents(struct inode * inode) +/* + * Truncate extents of inode to inode->i_size. This function can be used only + * for making file shorter. For making file longer, udf_extend_file() has to + * be used. + */ +void udf_truncate_extents(struct inode *inode) { - kernel_lb_addr bloc, eloc, neloc = { 0, 0 }; - uint32_t extoffset, elen, offset, nelen = 0, lelen = 0, lenalloc; + struct extent_position epos; + struct kernel_lb_addr eloc, neloc = {}; + uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc; int8_t etype; - int first_block = inode->i_size >> inode->i_sb->s_blocksize_bits; - struct buffer_head *bh = NULL; + struct super_block *sb = inode->i_sb; + sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset; + loff_t byte_offset; int adsize; + struct udf_inode_info *iinfo = UDF_I(inode); - if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT) - adsize = sizeof(short_ad); - else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG) - adsize = sizeof(long_ad); + 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; - - etype = inode_bmap(inode, first_block, &bloc, &extoffset, &eloc, &elen, &offset, &bh); - offset += (inode->i_size & (inode->i_sb->s_blocksize - 1)); - if (etype != -1) - { - extoffset -= adsize; - extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, offset); - extoffset += adsize; + BUG(); - if (offset) - lenalloc = extoffset; - else - lenalloc = extoffset - adsize; - - if (!bh) - lenalloc -= udf_file_entry_alloc_offset(inode); - else - lenalloc -= sizeof(struct allocExtDesc); - - while ((etype = udf_current_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 0)) != -1) - { - if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) - { - udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 0); - extoffset = 0; - if (lelen) - { - if (!bh) - BUG(); - else - memset(bh->b_data, 0x00, sizeof(struct allocExtDesc)); - udf_free_blocks(inode->i_sb, inode, bloc, 0, lelen); - } - else - { - if (!bh) - { - UDF_I_LENALLOC(inode) = lenalloc; - mark_inode_dirty(inode); - } - else - { - struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data); - aed->lengthAllocDescs = cpu_to_le32(lenalloc); - if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201) - udf_update_tag(bh->b_data, lenalloc + - sizeof(struct allocExtDesc)); - else - udf_update_tag(bh->b_data, sizeof(struct allocExtDesc)); - mark_buffer_dirty_inode(bh, inode); - } - } + etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset); + byte_offset = (offset << sb->s_blocksize_bits) + + (inode->i_size & (sb->s_blocksize - 1)); + if (etype == -1) { + /* We should extend the file? */ + WARN_ON(byte_offset); + return; + } + epos.offset -= adsize; + extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset); + epos.offset += adsize; + if (byte_offset) + lenalloc = epos.offset; + else + lenalloc = epos.offset - adsize; - udf_release_data(bh); - extoffset = sizeof(struct allocExtDesc); - bloc = eloc; - bh = udf_tread(inode->i_sb, udf_get_lb_pblock(inode->i_sb, bloc, 0)); - if (elen) - lelen = (elen + inode->i_sb->s_blocksize - 1) >> - inode->i_sb->s_blocksize_bits; - else - lelen = 1; - } - else - { - extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, 0); - extoffset += adsize; - } - } + if (!epos.bh) + lenalloc -= udf_file_entry_alloc_offset(inode); + else + lenalloc -= sizeof(struct allocExtDesc); - if (lelen) - { - if (!bh) - BUG(); - else - memset(bh->b_data, 0x00, sizeof(struct allocExtDesc)); - udf_free_blocks(inode->i_sb, inode, bloc, 0, lelen); - } - else - { - if (!bh) - { - UDF_I_LENALLOC(inode) = lenalloc; + while ((etype = udf_current_aext(inode, &epos, &eloc, + &elen, 0)) != -1) { + if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) { + udf_write_aext(inode, &epos, &neloc, nelen, 0); + if (indirect_ext_len) { + /* We managed to free all extents in the + * indirect extent - free it too */ + BUG_ON(!epos.bh); + udf_free_blocks(sb, NULL, &epos.block, + 0, indirect_ext_len); + } else if (!epos.bh) { + iinfo->i_lenAlloc = lenalloc; mark_inode_dirty(inode); - } + } else + udf_update_alloc_ext_desc(inode, + &epos, lenalloc); + brelse(epos.bh); + epos.offset = sizeof(struct allocExtDesc); + epos.block = eloc; + epos.bh = udf_tread(sb, + udf_get_lb_pblock(sb, &eloc, 0)); + if (elen) + indirect_ext_len = + (elen + sb->s_blocksize - 1) >> + sb->s_blocksize_bits; else - { - struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data); - aed->lengthAllocDescs = cpu_to_le32(lenalloc); - if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201) - udf_update_tag(bh->b_data, lenalloc + - sizeof(struct allocExtDesc)); - else - udf_update_tag(bh->b_data, sizeof(struct allocExtDesc)); - mark_buffer_dirty_inode(bh, inode); - } + indirect_ext_len = 1; + } else { + extent_trunc(inode, &epos, &eloc, etype, elen, 0); + epos.offset += adsize; } } - else if (inode->i_size) - { - if (offset) - { - extoffset -= adsize; - etype = udf_next_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 1); - if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) - { - extoffset -= adsize; - elen = EXT_NOT_RECORDED_NOT_ALLOCATED | (elen + offset); - udf_write_aext(inode, bloc, &extoffset, eloc, elen, bh, 0); - } - else if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) - { - kernel_lb_addr neloc = { 0, 0 }; - extoffset -= adsize; - nelen = EXT_NOT_RECORDED_NOT_ALLOCATED | - ((elen + offset + inode->i_sb->s_blocksize - 1) & - ~(inode->i_sb->s_blocksize - 1)); - udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 1); - udf_add_aext(inode, &bloc, &extoffset, eloc, (etype << 30) | elen, &bh, 1); - } - else - { - if (elen & (inode->i_sb->s_blocksize - 1)) - { - extoffset -= adsize; - elen = EXT_RECORDED_ALLOCATED | - ((elen + inode->i_sb->s_blocksize - 1) & - ~(inode->i_sb->s_blocksize - 1)); - udf_write_aext(inode, bloc, &extoffset, eloc, elen, bh, 1); - } - memset(&eloc, 0x00, sizeof(kernel_lb_addr)); - elen = EXT_NOT_RECORDED_NOT_ALLOCATED | offset; - udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &bh, 1); - } - } - } - UDF_I_LENEXTENTS(inode) = inode->i_size; - udf_release_data(bh); + if (indirect_ext_len) { + BUG_ON(!epos.bh); + udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len); + } else if (!epos.bh) { + iinfo->i_lenAlloc = lenalloc; + mark_inode_dirty(inode); + } else + udf_update_alloc_ext_desc(inode, &epos, lenalloc); + iinfo->i_lenExtents = inode->i_size; + + brelse(epos.bh); } |
