11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * truncate.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * PURPOSE 51da177e4SLinus Torvalds * Truncate handling routines for the OSTA-UDF(tm) filesystem. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * COPYRIGHT 81da177e4SLinus Torvalds * This file is distributed under the terms of the GNU General Public 91da177e4SLinus Torvalds * License (GPL). Copies of the GPL can be obtained from: 101da177e4SLinus Torvalds * ftp://prep.ai.mit.edu/pub/gnu/GPL 111da177e4SLinus Torvalds * Each contributing author retains all rights to their own work. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * (C) 1999-2004 Ben Fennema 141da177e4SLinus Torvalds * (C) 1999 Stelias Computing Inc 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * HISTORY 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * 02/24/99 blf Created. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds */ 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #include "udfdecl.h" 231da177e4SLinus Torvalds #include <linux/fs.h> 241da177e4SLinus Torvalds #include <linux/mm.h> 251da177e4SLinus Torvalds #include <linux/buffer_head.h> 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #include "udf_i.h" 281da177e4SLinus Torvalds #include "udf_sb.h" 291da177e4SLinus Torvalds 30ff116fc8SJan Kara static void extent_trunc(struct inode *inode, struct extent_position *epos, 3197e961fdSPekka Enberg struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen, 32cb00ea35SCyrill Gorcunov uint32_t nelen) 331da177e4SLinus Torvalds { 345ca4e4beSPekka Enberg struct kernel_lb_addr neloc = {}; 3528de7948SCyrill Gorcunov int last_block = (elen + inode->i_sb->s_blocksize - 1) >> 3628de7948SCyrill Gorcunov inode->i_sb->s_blocksize_bits; 3728de7948SCyrill Gorcunov int first_block = (nelen + inode->i_sb->s_blocksize - 1) >> 3828de7948SCyrill Gorcunov inode->i_sb->s_blocksize_bits; 391da177e4SLinus Torvalds 40cb00ea35SCyrill Gorcunov if (nelen) { 41cb00ea35SCyrill Gorcunov if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { 42cb00ea35SCyrill Gorcunov udf_free_blocks(inode->i_sb, inode, eloc, 0, 43cb00ea35SCyrill Gorcunov last_block); 441da177e4SLinus Torvalds etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30); 45cb00ea35SCyrill Gorcunov } else 4697e961fdSPekka Enberg neloc = *eloc; 471da177e4SLinus Torvalds nelen = (etype << 30) | nelen; 481da177e4SLinus Torvalds } 491da177e4SLinus Torvalds 50cb00ea35SCyrill Gorcunov if (elen != nelen) { 5197e961fdSPekka Enberg udf_write_aext(inode, epos, &neloc, nelen, 0); 52cb00ea35SCyrill Gorcunov if (last_block - first_block > 0) { 531da177e4SLinus Torvalds if (etype == (EXT_RECORDED_ALLOCATED >> 30)) 541da177e4SLinus Torvalds mark_inode_dirty(inode); 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) 57cb00ea35SCyrill Gorcunov udf_free_blocks(inode->i_sb, inode, eloc, 58cb00ea35SCyrill Gorcunov first_block, 59cb00ea35SCyrill Gorcunov last_block - first_block); 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds 6474584ae5SJan Kara /* 6574584ae5SJan Kara * Truncate the last extent to match i_size. This function assumes 6674584ae5SJan Kara * that preallocation extent is already truncated. 6774584ae5SJan Kara */ 6874584ae5SJan Kara void udf_truncate_tail_extent(struct inode *inode) 691da177e4SLinus Torvalds { 7028de7948SCyrill Gorcunov struct extent_position epos = {}; 715ca4e4beSPekka Enberg struct kernel_lb_addr eloc; 72ff116fc8SJan Kara uint32_t elen, nelen; 731da177e4SLinus Torvalds uint64_t lbcount = 0; 741da177e4SLinus Torvalds int8_t etype = -1, netype; 751da177e4SLinus Torvalds int adsize; 7648d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode); 771da177e4SLinus Torvalds 7848d6d8ffSMarcin Slusarz if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB || 7948d6d8ffSMarcin Slusarz inode->i_size == iinfo->i_lenExtents) 801da177e4SLinus Torvalds return; 8174584ae5SJan Kara /* Are we going to delete the file anyway? */ 8274584ae5SJan Kara if (inode->i_nlink == 0) 8374584ae5SJan Kara return; 8474584ae5SJan Kara 8548d6d8ffSMarcin Slusarz if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 865ca4e4beSPekka Enberg adsize = sizeof(struct short_ad); 8748d6d8ffSMarcin Slusarz else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 885ca4e4beSPekka Enberg adsize = sizeof(struct long_ad); 8974584ae5SJan Kara else 9074584ae5SJan Kara BUG(); 9174584ae5SJan Kara 9274584ae5SJan Kara /* Find the last extent in the file */ 93cb00ea35SCyrill Gorcunov while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { 9474584ae5SJan Kara etype = netype; 9574584ae5SJan Kara lbcount += elen; 9674584ae5SJan Kara if (lbcount > inode->i_size) { 9774584ae5SJan Kara if (lbcount - inode->i_size >= inode->i_sb->s_blocksize) 9878ace70cSJoe Perches udf_warn(inode->i_sb, 9978ace70cSJoe Perches "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n", 10074584ae5SJan Kara (unsigned)inode->i_ino, 10174584ae5SJan Kara (long long)inode->i_size, 10274584ae5SJan Kara (long long)lbcount, 10374584ae5SJan Kara (unsigned)eloc.logicalBlockNum, 10474584ae5SJan Kara (unsigned)elen); 10574584ae5SJan Kara nelen = elen - (lbcount - inode->i_size); 10674584ae5SJan Kara epos.offset -= adsize; 10797e961fdSPekka Enberg extent_trunc(inode, &epos, &eloc, etype, elen, nelen); 10874584ae5SJan Kara epos.offset += adsize; 10974584ae5SJan Kara if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1) 11078ace70cSJoe Perches udf_err(inode->i_sb, 11178ace70cSJoe Perches "Extent after EOF in inode %u\n", 11274584ae5SJan Kara (unsigned)inode->i_ino); 11374584ae5SJan Kara break; 11474584ae5SJan Kara } 11574584ae5SJan Kara } 11674584ae5SJan Kara /* This inode entry is in-memory only and thus we don't have to mark 11774584ae5SJan Kara * the inode dirty */ 11848d6d8ffSMarcin Slusarz iinfo->i_lenExtents = inode->i_size; 11974584ae5SJan Kara brelse(epos.bh); 12074584ae5SJan Kara } 12174584ae5SJan Kara 12274584ae5SJan Kara void udf_discard_prealloc(struct inode *inode) 12374584ae5SJan Kara { 12474584ae5SJan Kara struct extent_position epos = { NULL, 0, {0, 0} }; 1255ca4e4beSPekka Enberg struct kernel_lb_addr eloc; 12674584ae5SJan Kara uint32_t elen; 12774584ae5SJan Kara uint64_t lbcount = 0; 12874584ae5SJan Kara int8_t etype = -1, netype; 12974584ae5SJan Kara int adsize; 13048d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode); 13174584ae5SJan Kara 13248d6d8ffSMarcin Slusarz if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB || 13348d6d8ffSMarcin Slusarz inode->i_size == iinfo->i_lenExtents) 13474584ae5SJan Kara return; 1351da177e4SLinus Torvalds 13648d6d8ffSMarcin Slusarz if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 1375ca4e4beSPekka Enberg adsize = sizeof(struct short_ad); 13848d6d8ffSMarcin Slusarz else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 1395ca4e4beSPekka Enberg adsize = sizeof(struct long_ad); 1401da177e4SLinus Torvalds else 1411da177e4SLinus Torvalds adsize = 0; 1421da177e4SLinus Torvalds 14348d6d8ffSMarcin Slusarz epos.block = iinfo->i_location; 1441da177e4SLinus Torvalds 145ff116fc8SJan Kara /* Find the last extent in the file */ 14674584ae5SJan Kara while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { 1471da177e4SLinus Torvalds etype = netype; 1481da177e4SLinus Torvalds lbcount += elen; 1491da177e4SLinus Torvalds } 150ff116fc8SJan Kara if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { 151ff116fc8SJan Kara epos.offset -= adsize; 1521da177e4SLinus Torvalds lbcount -= elen; 15397e961fdSPekka Enberg extent_trunc(inode, &epos, &eloc, etype, elen, 0); 15474584ae5SJan Kara if (!epos.bh) { 15548d6d8ffSMarcin Slusarz iinfo->i_lenAlloc = 1564b11111aSMarcin Slusarz epos.offset - 1574b11111aSMarcin Slusarz udf_file_entry_alloc_offset(inode); 1581da177e4SLinus Torvalds mark_inode_dirty(inode); 15974584ae5SJan Kara } else { 160cb00ea35SCyrill Gorcunov struct allocExtDesc *aed = 161cb00ea35SCyrill Gorcunov (struct allocExtDesc *)(epos.bh->b_data); 162cb00ea35SCyrill Gorcunov aed->lengthAllocDescs = 163cb00ea35SCyrill Gorcunov cpu_to_le32(epos.offset - 164cb00ea35SCyrill Gorcunov sizeof(struct allocExtDesc)); 16528de7948SCyrill Gorcunov if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || 1666c79e987SMarcin Slusarz UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) 167ff116fc8SJan Kara udf_update_tag(epos.bh->b_data, epos.offset); 1681da177e4SLinus Torvalds else 169cb00ea35SCyrill Gorcunov udf_update_tag(epos.bh->b_data, 170cb00ea35SCyrill Gorcunov sizeof(struct allocExtDesc)); 171ff116fc8SJan Kara mark_buffer_dirty_inode(epos.bh, inode); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds } 17474584ae5SJan Kara /* This inode entry is in-memory only and thus we don't have to mark 17574584ae5SJan Kara * the inode dirty */ 17648d6d8ffSMarcin Slusarz iinfo->i_lenExtents = lbcount; 1773bf25cb4SJan Kara brelse(epos.bh); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 180456390deSmarcin.slusarz@gmail.com static void udf_update_alloc_ext_desc(struct inode *inode, 181456390deSmarcin.slusarz@gmail.com struct extent_position *epos, 182456390deSmarcin.slusarz@gmail.com u32 lenalloc) 183456390deSmarcin.slusarz@gmail.com { 184456390deSmarcin.slusarz@gmail.com struct super_block *sb = inode->i_sb; 185456390deSmarcin.slusarz@gmail.com struct udf_sb_info *sbi = UDF_SB(sb); 186456390deSmarcin.slusarz@gmail.com 187456390deSmarcin.slusarz@gmail.com struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data); 188456390deSmarcin.slusarz@gmail.com int len = sizeof(struct allocExtDesc); 189456390deSmarcin.slusarz@gmail.com 190456390deSmarcin.slusarz@gmail.com aed->lengthAllocDescs = cpu_to_le32(lenalloc); 191456390deSmarcin.slusarz@gmail.com if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201) 192456390deSmarcin.slusarz@gmail.com len += lenalloc; 193456390deSmarcin.slusarz@gmail.com 194456390deSmarcin.slusarz@gmail.com udf_update_tag(epos->bh->b_data, len); 195456390deSmarcin.slusarz@gmail.com mark_buffer_dirty_inode(epos->bh, inode); 196456390deSmarcin.slusarz@gmail.com } 197456390deSmarcin.slusarz@gmail.com 1987e49b6f2SJan Kara /* 1997e49b6f2SJan Kara * Truncate extents of inode to inode->i_size. This function can be used only 2007e49b6f2SJan Kara * for making file shorter. For making file longer, udf_extend_file() has to 2017e49b6f2SJan Kara * be used. 2027e49b6f2SJan Kara */ 2031da177e4SLinus Torvalds void udf_truncate_extents(struct inode *inode) 2041da177e4SLinus Torvalds { 205ff116fc8SJan Kara struct extent_position epos; 2065ca4e4beSPekka Enberg struct kernel_lb_addr eloc, neloc = {}; 207ff116fc8SJan Kara uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc; 2081da177e4SLinus Torvalds int8_t etype; 20931170b6aSJan Kara struct super_block *sb = inode->i_sb; 21031170b6aSJan Kara sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset; 21160448b1dSJan Kara loff_t byte_offset; 2121da177e4SLinus Torvalds int adsize; 21348d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode); 2141da177e4SLinus Torvalds 21548d6d8ffSMarcin Slusarz if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 2165ca4e4beSPekka Enberg adsize = sizeof(struct short_ad); 21748d6d8ffSMarcin Slusarz else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 2185ca4e4beSPekka Enberg adsize = sizeof(struct long_ad); 2191da177e4SLinus Torvalds else 220ff116fc8SJan Kara BUG(); 2211da177e4SLinus Torvalds 222ff116fc8SJan Kara etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset); 22328de7948SCyrill Gorcunov byte_offset = (offset << sb->s_blocksize_bits) + 224cb00ea35SCyrill Gorcunov (inode->i_size & (sb->s_blocksize - 1)); 2257e49b6f2SJan Kara if (etype == -1) { 2267e49b6f2SJan Kara /* We should extend the file? */ 2277e49b6f2SJan Kara WARN_ON(byte_offset); 2287e49b6f2SJan Kara return; 2297e49b6f2SJan Kara } 230ff116fc8SJan Kara epos.offset -= adsize; 23197e961fdSPekka Enberg extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset); 232ff116fc8SJan Kara epos.offset += adsize; 23360448b1dSJan Kara if (byte_offset) 234ff116fc8SJan Kara lenalloc = epos.offset; 2351da177e4SLinus Torvalds else 236ff116fc8SJan Kara lenalloc = epos.offset - adsize; 2371da177e4SLinus Torvalds 238ff116fc8SJan Kara if (!epos.bh) 2391da177e4SLinus Torvalds lenalloc -= udf_file_entry_alloc_offset(inode); 2401da177e4SLinus Torvalds else 2411da177e4SLinus Torvalds lenalloc -= sizeof(struct allocExtDesc); 2421da177e4SLinus Torvalds 2434b11111aSMarcin Slusarz while ((etype = udf_current_aext(inode, &epos, &eloc, 2444b11111aSMarcin Slusarz &elen, 0)) != -1) { 245cb00ea35SCyrill Gorcunov if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) { 24697e961fdSPekka Enberg udf_write_aext(inode, &epos, &neloc, nelen, 0); 247cb00ea35SCyrill Gorcunov if (indirect_ext_len) { 248ff116fc8SJan Kara /* We managed to free all extents in the 249ff116fc8SJan Kara * indirect extent - free it too */ 2509de90b76Smarcin.slusarz@gmail.com BUG_ON(!epos.bh); 25117dc59baSJan Kara udf_free_blocks(sb, NULL, &epos.block, 252cb00ea35SCyrill Gorcunov 0, indirect_ext_len); 2539de90b76Smarcin.slusarz@gmail.com } else if (!epos.bh) { 2549de90b76Smarcin.slusarz@gmail.com iinfo->i_lenAlloc = lenalloc; 2551da177e4SLinus Torvalds mark_inode_dirty(inode); 256456390deSmarcin.slusarz@gmail.com } else 257456390deSmarcin.slusarz@gmail.com udf_update_alloc_ext_desc(inode, 258456390deSmarcin.slusarz@gmail.com &epos, lenalloc); 259ff116fc8SJan Kara brelse(epos.bh); 260ff116fc8SJan Kara epos.offset = sizeof(struct allocExtDesc); 261ff116fc8SJan Kara epos.block = eloc; 2624b11111aSMarcin Slusarz epos.bh = udf_tread(sb, 26397e961fdSPekka Enberg udf_get_lb_pblock(sb, &eloc, 0)); 2641da177e4SLinus Torvalds if (elen) 2654b11111aSMarcin Slusarz indirect_ext_len = 2664b11111aSMarcin Slusarz (elen + sb->s_blocksize - 1) >> 26728de7948SCyrill Gorcunov sb->s_blocksize_bits; 2681da177e4SLinus Torvalds else 269ff116fc8SJan Kara indirect_ext_len = 1; 270cb00ea35SCyrill Gorcunov } else { 2717e49b6f2SJan Kara extent_trunc(inode, &epos, &eloc, etype, elen, 0); 272ff116fc8SJan Kara epos.offset += adsize; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 276cb00ea35SCyrill Gorcunov if (indirect_ext_len) { 2779de90b76Smarcin.slusarz@gmail.com BUG_ON(!epos.bh); 27817dc59baSJan Kara udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len); 2799de90b76Smarcin.slusarz@gmail.com } else if (!epos.bh) { 28048d6d8ffSMarcin Slusarz iinfo->i_lenAlloc = lenalloc; 2811da177e4SLinus Torvalds mark_inode_dirty(inode); 282456390deSmarcin.slusarz@gmail.com } else 283456390deSmarcin.slusarz@gmail.com udf_update_alloc_ext_desc(inode, &epos, lenalloc); 28448d6d8ffSMarcin Slusarz iinfo->i_lenExtents = inode->i_size; 2851da177e4SLinus Torvalds 2863bf25cb4SJan Kara brelse(epos.bh); 2871da177e4SLinus Torvalds } 288