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> |
| 25 | #include <linux/udf_fs.h> |
| 26 | #include <linux/buffer_head.h> |
| 27 | |
| 28 | #include "udf_i.h" |
| 29 | #include "udf_sb.h" |
| 30 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 31 | static void extent_trunc(struct inode *inode, struct extent_position *epos, |
| 32 | kernel_lb_addr eloc, int8_t etype, uint32_t elen, |
| 33 | uint32_t nelen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 35 | kernel_lb_addr neloc = {}; |
| 36 | int last_block = (elen + inode->i_sb->s_blocksize - 1) >> |
| 37 | inode->i_sb->s_blocksize_bits; |
| 38 | int first_block = (nelen + inode->i_sb->s_blocksize - 1) >> |
| 39 | inode->i_sb->s_blocksize_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 41 | if (nelen) { |
| 42 | if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { |
| 43 | udf_free_blocks(inode->i_sb, inode, eloc, 0, |
| 44 | last_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 46 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | neloc = eloc; |
| 48 | nelen = (etype << 30) | nelen; |
| 49 | } |
| 50 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 51 | if (elen != nelen) { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 52 | udf_write_aext(inode, epos, neloc, nelen, 0); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 53 | if (last_block - first_block > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | if (etype == (EXT_RECORDED_ALLOCATED >> 30)) |
| 55 | mark_inode_dirty(inode); |
| 56 | |
| 57 | if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 58 | udf_free_blocks(inode->i_sb, inode, eloc, |
| 59 | first_block, |
| 60 | last_block - first_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Truncate the last extent to match i_size. This function assumes |
| 67 | * that preallocation extent is already truncated. |
| 68 | */ |
| 69 | void udf_truncate_tail_extent(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 71 | struct extent_position epos = {}; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 72 | kernel_lb_addr eloc; |
| 73 | uint32_t elen, nelen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | uint64_t lbcount = 0; |
| 75 | int8_t etype = -1, netype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | int adsize; |
| 77 | |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 78 | if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB || |
| 79 | inode->i_size == UDF_I(inode)->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 | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 85 | if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 86 | adsize = sizeof(short_ad); |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 87 | else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 88 | adsize = sizeof(long_ad); |
| 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; |
| 109 | extent_trunc(inode, &epos, eloc, etype, elen, nelen); |
| 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 | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 120 | UDF_I(inode)->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} }; |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 127 | kernel_lb_addr eloc; |
| 128 | uint32_t elen; |
| 129 | uint64_t lbcount = 0; |
| 130 | int8_t etype = -1, netype; |
| 131 | int adsize; |
| 132 | |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 133 | if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB || |
| 134 | inode->i_size == UDF_I(inode)->i_lenExtents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 137 | if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | adsize = sizeof(short_ad); |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 139 | else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | adsize = sizeof(long_ad); |
| 141 | else |
| 142 | adsize = 0; |
| 143 | |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 144 | epos.block = UDF_I(inode)->i_location; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 146 | /* Find the last extent in the file */ |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 147 | while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | etype = netype; |
| 149 | lbcount += elen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 151 | if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { |
| 152 | epos.offset -= adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | lbcount -= elen; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 154 | extent_trunc(inode, &epos, eloc, etype, elen, 0); |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 155 | if (!epos.bh) { |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 156 | UDF_I(inode)->i_lenAlloc = |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 157 | epos.offset - |
| 158 | udf_file_entry_alloc_offset(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | mark_inode_dirty(inode); |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 160 | } else { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 161 | struct allocExtDesc *aed = |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 162 | (struct allocExtDesc *)(epos.bh->b_data); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 163 | aed->lengthAllocDescs = |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 164 | cpu_to_le32(epos.offset - |
| 165 | sizeof(struct allocExtDesc)); |
| 166 | if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 167 | UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 168 | udf_update_tag(epos.bh->b_data, epos.offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | else |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 170 | udf_update_tag(epos.bh->b_data, |
| 171 | sizeof(struct allocExtDesc)); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 172 | mark_buffer_dirty_inode(epos.bh, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
Jan Kara | 74584ae | 2007-06-16 10:16:14 -0700 | [diff] [blame] | 175 | /* This inode entry is in-memory only and thus we don't have to mark |
| 176 | * the inode dirty */ |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 177 | UDF_I(inode)->i_lenExtents = lbcount; |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 178 | brelse(epos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 181 | void udf_truncate_extents(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 183 | struct extent_position epos; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 184 | kernel_lb_addr eloc, neloc = {}; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 185 | uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | int8_t etype; |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 187 | struct super_block *sb = inode->i_sb; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 188 | struct udf_sb_info *sbi = UDF_SB(sb); |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 189 | sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset; |
Jan Kara | 60448b1 | 2007-05-08 00:35:13 -0700 | [diff] [blame] | 190 | loff_t byte_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | int adsize; |
| 192 | |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 193 | if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | adsize = sizeof(short_ad); |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 195 | else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | adsize = sizeof(long_ad); |
| 197 | else |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 198 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 200 | etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 201 | byte_offset = (offset << sb->s_blocksize_bits) + |
| 202 | (inode->i_size & (sb->s_blocksize - 1)); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 203 | if (etype != -1) { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 204 | epos.offset -= adsize; |
| 205 | extent_trunc(inode, &epos, eloc, etype, elen, byte_offset); |
| 206 | epos.offset += adsize; |
Jan Kara | 60448b1 | 2007-05-08 00:35:13 -0700 | [diff] [blame] | 207 | if (byte_offset) |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 208 | lenalloc = epos.offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | else |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 210 | lenalloc = epos.offset - adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 212 | if (!epos.bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | lenalloc -= udf_file_entry_alloc_offset(inode); |
| 214 | else |
| 215 | lenalloc -= sizeof(struct allocExtDesc); |
| 216 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 217 | while ((etype = udf_current_aext(inode, &epos, &eloc, |
| 218 | &elen, 0)) != -1) { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 219 | if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 220 | udf_write_aext(inode, &epos, neloc, nelen, 0); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 221 | if (indirect_ext_len) { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 222 | /* We managed to free all extents in the |
| 223 | * indirect extent - free it too */ |
| 224 | if (!epos.bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | BUG(); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 226 | udf_free_blocks(sb, inode, epos.block, |
| 227 | 0, indirect_ext_len); |
| 228 | } else { |
| 229 | if (!epos.bh) { |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 230 | UDF_I(inode)->i_lenAlloc = |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 231 | lenalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | mark_inode_dirty(inode); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 233 | } else { |
| 234 | struct allocExtDesc *aed = |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 235 | (struct allocExtDesc *) |
| 236 | (epos.bh->b_data); |
| 237 | int len = |
| 238 | sizeof(struct allocExtDesc); |
| 239 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 240 | aed->lengthAllocDescs = |
| 241 | cpu_to_le32(lenalloc); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 242 | if (!UDF_QUERY_FLAG(sb, |
| 243 | UDF_FLAG_STRICT) || |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 244 | sbi->s_udfrev >= 0x0201) |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 245 | len += lenalloc; |
| 246 | |
| 247 | udf_update_tag(epos.bh->b_data, |
| 248 | len); |
| 249 | mark_buffer_dirty_inode( |
| 250 | epos.bh, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 253 | brelse(epos.bh); |
| 254 | epos.offset = sizeof(struct allocExtDesc); |
| 255 | epos.block = eloc; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 256 | epos.bh = udf_tread(sb, |
| 257 | udf_get_lb_pblock(sb, eloc, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (elen) |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 259 | indirect_ext_len = |
| 260 | (elen + sb->s_blocksize - 1) >> |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 261 | sb->s_blocksize_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | else |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 263 | indirect_ext_len = 1; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 264 | } else { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 265 | extent_trunc(inode, &epos, eloc, etype, |
| 266 | elen, 0); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 267 | epos.offset += adsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 271 | if (indirect_ext_len) { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 272 | if (!epos.bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | BUG(); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 274 | udf_free_blocks(sb, inode, epos.block, 0, |
| 275 | indirect_ext_len); |
| 276 | } else { |
| 277 | if (!epos.bh) { |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 278 | UDF_I(inode)->i_lenAlloc = lenalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | mark_inode_dirty(inode); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 280 | } else { |
| 281 | struct allocExtDesc *aed = |
| 282 | (struct allocExtDesc *)(epos.bh->b_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | aed->lengthAllocDescs = cpu_to_le32(lenalloc); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 284 | if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 285 | sbi->s_udfrev >= 0x0201) |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 286 | udf_update_tag(epos.bh->b_data, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 287 | lenalloc + |
| 288 | sizeof(struct allocExtDesc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | else |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 290 | udf_update_tag(epos.bh->b_data, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 291 | sizeof(struct allocExtDesc)); |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 292 | mark_buffer_dirty_inode(epos.bh, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 295 | } else if (inode->i_size) { |
| 296 | if (byte_offset) { |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 297 | kernel_long_ad extent; |
| 298 | |
Jan Kara | 00a2b0f | 2006-08-15 13:56:26 +0200 | [diff] [blame] | 299 | /* |
| 300 | * OK, there is not extent covering inode->i_size and |
| 301 | * no extent above inode->i_size => truncate is |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 302 | * extending the file by 'offset' blocks. |
Jan Kara | 00a2b0f | 2006-08-15 13:56:26 +0200 | [diff] [blame] | 303 | */ |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 304 | if ((!epos.bh && |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 305 | epos.offset == |
| 306 | udf_file_entry_alloc_offset(inode)) || |
| 307 | (epos.bh && epos.offset == |
| 308 | sizeof(struct allocExtDesc))) { |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 309 | /* File has no extents at all or has empty last |
| 310 | * indirect extent! Create a fake extent... */ |
| 311 | extent.extLocation.logicalBlockNum = 0; |
| 312 | extent.extLocation.partitionReferenceNum = 0; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 313 | extent.extLength = |
| 314 | EXT_NOT_RECORDED_NOT_ALLOCATED; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 315 | } else { |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 316 | epos.offset -= adsize; |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 317 | etype = udf_next_aext(inode, &epos, |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 318 | &extent.extLocation, |
| 319 | &extent.extLength, 0); |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 320 | extent.extLength |= etype << 30; |
Jan Kara | 00a2b0f | 2006-08-15 13:56:26 +0200 | [diff] [blame] | 321 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 322 | udf_extend_file(inode, &epos, &extent, |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 323 | offset + |
| 324 | ((inode->i_size & |
| 325 | (sb->s_blocksize - 1)) != 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame^] | 328 | UDF_I(inode)->i_lenExtents = inode->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 330 | brelse(epos.bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |