Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * truncate.c |
| 3 | * |
| 4 | * PURPOSE |
| 5 | * Truncate handling routines for the OSTA-UDF(tm) filesystem. |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * COPYRIGHT |
| 8 | * This file is distributed under the terms of the GNU General Public |
| 9 | * License (GPL). Copies of the GPL can be obtained from: |
| 10 | * ftp://prep.ai.mit.edu/pub/gnu/GPL |
| 11 | * Each contributing author retains all rights to their own work. |
| 12 | * |
| 13 | * (C) 1999-2004 Ben Fennema |
| 14 | * (C) 1999 Stelias Computing Inc |
| 15 | * |
| 16 | * HISTORY |
| 17 | * |
| 18 | * 02/24/99 blf Created. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "udfdecl.h" |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/buffer_head.h> |
| 26 | |
| 27 | #include "udf_i.h" |
| 28 | #include "udf_sb.h" |
| 29 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 30 | static void extent_trunc(struct inode *inode, struct extent_position *epos, |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 31 | struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen, |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 32 | uint32_t nelen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 34 | struct kernel_lb_addr neloc = {}; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 35 | int last_block = (elen + inode->i_sb->s_blocksize - 1) >> |
| 36 | inode->i_sb->s_blocksize_bits; |
| 37 | int first_block = (nelen + inode->i_sb->s_blocksize - 1) >> |
| 38 | inode->i_sb->s_blocksize_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 40 | if (nelen) { |
| 41 | if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { |
| 42 | udf_free_blocks(inode->i_sb, inode, eloc, 0, |
| 43 | last_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 45 | } else |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 46 | neloc = *eloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | nelen = (etype << 30) | nelen; |
| 48 | } |
| 49 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 50 | if (elen != nelen) { |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 51 | udf_write_aext(inode, epos, &neloc, nelen, 0); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 52 | if (last_block - first_block > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | if (etype == (EXT_RECORDED_ALLOCATED >> 30)) |
| 54 | mark_inode_dirty(inode); |
| 55 | |
| 56 | if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 57 | udf_free_blocks(inode->i_sb, inode, eloc, |
| 58 | first_block, |
| 59 | last_block - first_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 64 | /* |
| 65 | * Truncate the last extent to match i_size. This function assumes |
| 66 | * that preallocation extent is already truncated. |
| 67 | */ |
| 68 | void udf_truncate_tail_extent(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 70 | struct extent_position epos = {}; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 71 | struct kernel_lb_addr eloc; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 72 | uint32_t elen, nelen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | uint64_t lbcount = 0; |
| 74 | int8_t etype = -1, netype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | int adsize; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 76 | struct udf_inode_info *iinfo = UDF_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 78 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB || |
| 79 | inode->i_size == iinfo->i_lenExtents) |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 80 | return; |
| 81 | /* Are we going to delete the file anyway? */ |
| 82 | if (inode->i_nlink == 0) |
| 83 | return; |
| 84 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 85 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 86 | adsize = sizeof(struct short_ad); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 87 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 88 | adsize = sizeof(struct long_ad); |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 89 | else |
| 90 | BUG(); |
| 91 | |
| 92 | /* Find the last extent in the file */ |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 93 | while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 94 | etype = netype; |
| 95 | lbcount += elen; |
| 96 | if (lbcount > inode->i_size) { |
| 97 | if (lbcount - inode->i_size >= inode->i_sb->s_blocksize) |
| 98 | printk(KERN_WARNING |
| 99 | "udf_truncate_tail_extent(): Too long " |
| 100 | "extent after EOF in inode %u: i_size: " |
| 101 | "%Ld lbcount: %Ld extent %u+%u\n", |
| 102 | (unsigned)inode->i_ino, |
| 103 | (long long)inode->i_size, |
| 104 | (long long)lbcount, |
| 105 | (unsigned)eloc.logicalBlockNum, |
| 106 | (unsigned)elen); |
| 107 | nelen = elen - (lbcount - inode->i_size); |
| 108 | epos.offset -= adsize; |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 109 | extent_trunc(inode, &epos, &eloc, etype, elen, nelen); |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 110 | epos.offset += adsize; |
| 111 | if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1) |
| 112 | printk(KERN_ERR "udf_truncate_tail_extent(): " |
| 113 | "Extent after EOF in inode %u.\n", |
| 114 | (unsigned)inode->i_ino); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | /* This inode entry is in-memory only and thus we don't have to mark |
| 119 | * the inode dirty */ |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 120 | iinfo->i_lenExtents = inode->i_size; |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 121 | brelse(epos.bh); |
| 122 | } |
| 123 | |
| 124 | void udf_discard_prealloc(struct inode *inode) |
| 125 | { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 126 | struct extent_position epos = { NULL, 0, {0, 0} }; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 127 | struct kernel_lb_addr eloc; |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 128 | uint32_t elen; |
| 129 | uint64_t lbcount = 0; |
| 130 | int8_t etype = -1, netype; |
| 131 | int adsize; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 132 | struct udf_inode_info *iinfo = UDF_I(inode); |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 133 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 134 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB || |
| 135 | inode->i_size == iinfo->i_lenExtents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 138 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 139 | adsize = sizeof(struct short_ad); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 140 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 141 | adsize = sizeof(struct long_ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | else |
| 143 | adsize = 0; |
| 144 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 145 | epos.block = iinfo->i_location; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 147 | /* Find the last extent in the file */ |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 148 | while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | etype = netype; |
| 150 | lbcount += elen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 152 | if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { |
| 153 | epos.offset -= adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | lbcount -= elen; |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 155 | extent_trunc(inode, &epos, &eloc, etype, elen, 0); |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 156 | if (!epos.bh) { |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 157 | iinfo->i_lenAlloc = |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 158 | epos.offset - |
| 159 | udf_file_entry_alloc_offset(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | mark_inode_dirty(inode); |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 161 | } else { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 162 | struct allocExtDesc *aed = |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 163 | (struct allocExtDesc *)(epos.bh->b_data); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 164 | aed->lengthAllocDescs = |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 165 | cpu_to_le32(epos.offset - |
| 166 | sizeof(struct allocExtDesc)); |
| 167 | if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 168 | UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 169 | udf_update_tag(epos.bh->b_data, epos.offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | else |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 171 | udf_update_tag(epos.bh->b_data, |
| 172 | sizeof(struct allocExtDesc)); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 173 | mark_buffer_dirty_inode(epos.bh, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 176 | /* This inode entry is in-memory only and thus we don't have to mark |
| 177 | * the inode dirty */ |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 178 | iinfo->i_lenExtents = lbcount; |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 179 | brelse(epos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
marcin.slusarz@gmail.com | 456390d | 2008-01-30 22:03:56 +0100 | [diff] [blame] | 182 | static void udf_update_alloc_ext_desc(struct inode *inode, |
| 183 | struct extent_position *epos, |
| 184 | u32 lenalloc) |
| 185 | { |
| 186 | struct super_block *sb = inode->i_sb; |
| 187 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 188 | |
| 189 | struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data); |
| 190 | int len = sizeof(struct allocExtDesc); |
| 191 | |
| 192 | aed->lengthAllocDescs = cpu_to_le32(lenalloc); |
| 193 | if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201) |
| 194 | len += lenalloc; |
| 195 | |
| 196 | udf_update_tag(epos->bh->b_data, len); |
| 197 | mark_buffer_dirty_inode(epos->bh, inode); |
| 198 | } |
| 199 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 200 | void udf_truncate_extents(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 202 | struct extent_position epos; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 203 | struct kernel_lb_addr eloc, neloc = {}; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 204 | uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | int8_t etype; |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 206 | struct super_block *sb = inode->i_sb; |
| 207 | sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset; |
Jan Kara | 60448b1 | 2007-05-08 00:35:13 -0700 | [diff] [blame] | 208 | loff_t byte_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | int adsize; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 210 | struct udf_inode_info *iinfo = UDF_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 212 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 213 | adsize = sizeof(struct short_ad); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 214 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 215 | adsize = sizeof(struct long_ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | else |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 217 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 219 | etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 220 | byte_offset = (offset << sb->s_blocksize_bits) + |
| 221 | (inode->i_size & (sb->s_blocksize - 1)); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 222 | if (etype != -1) { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 223 | epos.offset -= adsize; |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 224 | extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 225 | epos.offset += adsize; |
Jan Kara | 60448b1 | 2007-05-08 00:35:13 -0700 | [diff] [blame] | 226 | if (byte_offset) |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 227 | lenalloc = epos.offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | else |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 229 | lenalloc = epos.offset - adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 231 | if (!epos.bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | lenalloc -= udf_file_entry_alloc_offset(inode); |
| 233 | else |
| 234 | lenalloc -= sizeof(struct allocExtDesc); |
| 235 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 236 | while ((etype = udf_current_aext(inode, &epos, &eloc, |
| 237 | &elen, 0)) != -1) { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 238 | if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) { |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 239 | udf_write_aext(inode, &epos, &neloc, nelen, 0); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 240 | if (indirect_ext_len) { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 241 | /* We managed to free all extents in the |
| 242 | * indirect extent - free it too */ |
marcin.slusarz@gmail.com | 9de90b7 | 2008-01-30 22:03:55 +0100 | [diff] [blame] | 243 | BUG_ON(!epos.bh); |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 244 | udf_free_blocks(sb, inode, &epos.block, |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 245 | 0, indirect_ext_len); |
marcin.slusarz@gmail.com | 9de90b7 | 2008-01-30 22:03:55 +0100 | [diff] [blame] | 246 | } else if (!epos.bh) { |
| 247 | iinfo->i_lenAlloc = lenalloc; |
| 248 | mark_inode_dirty(inode); |
marcin.slusarz@gmail.com | 456390d | 2008-01-30 22:03:56 +0100 | [diff] [blame] | 249 | } else |
| 250 | udf_update_alloc_ext_desc(inode, |
| 251 | &epos, lenalloc); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 252 | brelse(epos.bh); |
| 253 | epos.offset = sizeof(struct allocExtDesc); |
| 254 | epos.block = eloc; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 255 | epos.bh = udf_tread(sb, |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 256 | udf_get_lb_pblock(sb, &eloc, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (elen) |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 258 | indirect_ext_len = |
| 259 | (elen + sb->s_blocksize - 1) >> |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 260 | sb->s_blocksize_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | else |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 262 | indirect_ext_len = 1; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 263 | } else { |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 264 | extent_trunc(inode, &epos, &eloc, etype, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 265 | elen, 0); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 266 | epos.offset += adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 270 | if (indirect_ext_len) { |
marcin.slusarz@gmail.com | 9de90b7 | 2008-01-30 22:03:55 +0100 | [diff] [blame] | 271 | BUG_ON(!epos.bh); |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 272 | udf_free_blocks(sb, inode, &epos.block, 0, |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 273 | indirect_ext_len); |
marcin.slusarz@gmail.com | 9de90b7 | 2008-01-30 22:03:55 +0100 | [diff] [blame] | 274 | } else if (!epos.bh) { |
| 275 | iinfo->i_lenAlloc = lenalloc; |
| 276 | mark_inode_dirty(inode); |
marcin.slusarz@gmail.com | 456390d | 2008-01-30 22:03:56 +0100 | [diff] [blame] | 277 | } else |
| 278 | udf_update_alloc_ext_desc(inode, &epos, lenalloc); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 279 | } else if (inode->i_size) { |
| 280 | if (byte_offset) { |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 281 | struct kernel_long_ad extent; |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 282 | |
Jan Kara | 00a2b0f | 2006-08-15 13:56:26 +0200 | [diff] [blame] | 283 | /* |
| 284 | * OK, there is not extent covering inode->i_size and |
| 285 | * no extent above inode->i_size => truncate is |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 286 | * extending the file by 'offset' blocks. |
Jan Kara | 00a2b0f | 2006-08-15 13:56:26 +0200 | [diff] [blame] | 287 | */ |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 288 | if ((!epos.bh && |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 289 | epos.offset == |
| 290 | udf_file_entry_alloc_offset(inode)) || |
| 291 | (epos.bh && epos.offset == |
| 292 | sizeof(struct allocExtDesc))) { |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 293 | /* File has no extents at all or has empty last |
| 294 | * indirect extent! Create a fake extent... */ |
| 295 | extent.extLocation.logicalBlockNum = 0; |
| 296 | extent.extLocation.partitionReferenceNum = 0; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 297 | extent.extLength = |
| 298 | EXT_NOT_RECORDED_NOT_ALLOCATED; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 299 | } else { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 300 | epos.offset -= adsize; |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 301 | etype = udf_next_aext(inode, &epos, |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 302 | &extent.extLocation, |
| 303 | &extent.extLength, 0); |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 304 | extent.extLength |= etype << 30; |
Jan Kara | 00a2b0f | 2006-08-15 13:56:26 +0200 | [diff] [blame] | 305 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 306 | udf_extend_file(inode, &epos, &extent, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 307 | offset + |
| 308 | ((inode->i_size & |
| 309 | (sb->s_blocksize - 1)) != 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 312 | iinfo->i_lenExtents = inode->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 314 | brelse(epos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |