Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * truncate.c | |
3 | * | |
4 | * PURPOSE | |
5 | * Truncate handling routines for the OSTA-UDF(tm) filesystem. | |
6 | * | |
1da177e4 LT |
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 | ||
cb00ea35 CG |
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) | |
1da177e4 LT |
34 | { |
35 | kernel_lb_addr neloc = { 0, 0 }; | |
cb00ea35 CG |
36 | int last_block = |
37 | (elen + inode->i_sb->s_blocksize - | |
38 | 1) >> inode->i_sb->s_blocksize_bits; | |
39 | int first_block = | |
40 | (nelen + inode->i_sb->s_blocksize - | |
41 | 1) >> inode->i_sb->s_blocksize_bits; | |
1da177e4 | 42 | |
cb00ea35 CG |
43 | if (nelen) { |
44 | if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { | |
45 | udf_free_blocks(inode->i_sb, inode, eloc, 0, | |
46 | last_block); | |
1da177e4 | 47 | etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30); |
cb00ea35 | 48 | } else |
1da177e4 LT |
49 | neloc = eloc; |
50 | nelen = (etype << 30) | nelen; | |
51 | } | |
52 | ||
cb00ea35 | 53 | if (elen != nelen) { |
ff116fc8 | 54 | udf_write_aext(inode, epos, neloc, nelen, 0); |
cb00ea35 | 55 | if (last_block - first_block > 0) { |
1da177e4 LT |
56 | if (etype == (EXT_RECORDED_ALLOCATED >> 30)) |
57 | mark_inode_dirty(inode); | |
58 | ||
59 | if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) | |
cb00ea35 CG |
60 | udf_free_blocks(inode->i_sb, inode, eloc, |
61 | first_block, | |
62 | last_block - first_block); | |
1da177e4 LT |
63 | } |
64 | } | |
65 | } | |
66 | ||
74584ae5 JK |
67 | /* |
68 | * Truncate the last extent to match i_size. This function assumes | |
69 | * that preallocation extent is already truncated. | |
70 | */ | |
71 | void udf_truncate_tail_extent(struct inode *inode) | |
1da177e4 | 72 | { |
cb00ea35 | 73 | struct extent_position epos = { NULL, 0, {0, 0} }; |
ff116fc8 JK |
74 | kernel_lb_addr eloc; |
75 | uint32_t elen, nelen; | |
1da177e4 LT |
76 | uint64_t lbcount = 0; |
77 | int8_t etype = -1, netype; | |
1da177e4 LT |
78 | int adsize; |
79 | ||
80 | if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB || | |
74584ae5 JK |
81 | inode->i_size == UDF_I_LENEXTENTS(inode)) |
82 | return; | |
83 | /* Are we going to delete the file anyway? */ | |
84 | if (inode->i_nlink == 0) | |
1da177e4 | 85 | return; |
1da177e4 LT |
86 | |
87 | if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT) | |
88 | adsize = sizeof(short_ad); | |
89 | else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG) | |
90 | adsize = sizeof(long_ad); | |
91 | else | |
74584ae5 | 92 | BUG(); |
1da177e4 | 93 | |
ff116fc8 | 94 | /* Find the last extent in the file */ |
cb00ea35 | 95 | while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { |
1da177e4 LT |
96 | etype = netype; |
97 | lbcount += elen; | |
74584ae5 JK |
98 | if (lbcount > inode->i_size) { |
99 | if (lbcount - inode->i_size >= inode->i_sb->s_blocksize) | |
100 | printk(KERN_WARNING | |
101 | "udf_truncate_tail_extent(): Too long " | |
102 | "extent after EOF in inode %u: i_size: " | |
103 | "%Ld lbcount: %Ld extent %u+%u\n", | |
104 | (unsigned)inode->i_ino, | |
105 | (long long)inode->i_size, | |
106 | (long long)lbcount, | |
107 | (unsigned)eloc.logicalBlockNum, | |
108 | (unsigned)elen); | |
1da177e4 | 109 | nelen = elen - (lbcount - inode->i_size); |
ff116fc8 JK |
110 | epos.offset -= adsize; |
111 | extent_trunc(inode, &epos, eloc, etype, elen, nelen); | |
112 | epos.offset += adsize; | |
74584ae5 JK |
113 | if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1) |
114 | printk(KERN_ERR "udf_truncate_tail_extent(): " | |
115 | "Extent after EOF in inode %u.\n", | |
116 | (unsigned)inode->i_ino); | |
117 | break; | |
1da177e4 LT |
118 | } |
119 | } | |
74584ae5 JK |
120 | /* This inode entry is in-memory only and thus we don't have to mark |
121 | * the inode dirty */ | |
122 | UDF_I_LENEXTENTS(inode) = inode->i_size; | |
123 | brelse(epos.bh); | |
124 | } | |
125 | ||
126 | void udf_discard_prealloc(struct inode *inode) | |
127 | { | |
cb00ea35 | 128 | struct extent_position epos = { NULL, 0, {0, 0} }; |
74584ae5 JK |
129 | kernel_lb_addr eloc; |
130 | uint32_t elen; | |
131 | uint64_t lbcount = 0; | |
132 | int8_t etype = -1, netype; | |
133 | int adsize; | |
134 | ||
135 | if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB || | |
cb00ea35 | 136 | inode->i_size == UDF_I_LENEXTENTS(inode)) |
74584ae5 JK |
137 | return; |
138 | ||
139 | if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT) | |
140 | adsize = sizeof(short_ad); | |
141 | else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG) | |
142 | adsize = sizeof(long_ad); | |
143 | else | |
144 | adsize = 0; | |
145 | ||
146 | epos.block = UDF_I_LOCATION(inode); | |
147 | ||
148 | /* Find the last extent in the file */ | |
149 | while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { | |
150 | etype = netype; | |
151 | lbcount += elen; | |
152 | } | |
ff116fc8 JK |
153 | if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { |
154 | epos.offset -= adsize; | |
1da177e4 | 155 | lbcount -= elen; |
ff116fc8 | 156 | extent_trunc(inode, &epos, eloc, etype, elen, 0); |
74584ae5 | 157 | if (!epos.bh) { |
cb00ea35 CG |
158 | UDF_I_LENALLOC(inode) = |
159 | epos.offset - udf_file_entry_alloc_offset(inode); | |
1da177e4 | 160 | mark_inode_dirty(inode); |
74584ae5 | 161 | } else { |
cb00ea35 CG |
162 | struct allocExtDesc *aed = |
163 | (struct allocExtDesc *)(epos.bh->b_data); | |
164 | aed->lengthAllocDescs = | |
165 | cpu_to_le32(epos.offset - | |
166 | sizeof(struct allocExtDesc)); | |
167 | if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) | |
168 | || UDF_SB_UDFREV(inode->i_sb) >= 0x0201) | |
ff116fc8 | 169 | udf_update_tag(epos.bh->b_data, epos.offset); |
1da177e4 | 170 | else |
cb00ea35 CG |
171 | udf_update_tag(epos.bh->b_data, |
172 | sizeof(struct allocExtDesc)); | |
ff116fc8 | 173 | mark_buffer_dirty_inode(epos.bh, inode); |
1da177e4 LT |
174 | } |
175 | } | |
74584ae5 JK |
176 | /* This inode entry is in-memory only and thus we don't have to mark |
177 | * the inode dirty */ | |
1da177e4 | 178 | UDF_I_LENEXTENTS(inode) = lbcount; |
3bf25cb4 | 179 | brelse(epos.bh); |
1da177e4 LT |
180 | } |
181 | ||
cb00ea35 | 182 | void udf_truncate_extents(struct inode *inode) |
1da177e4 | 183 | { |
ff116fc8 JK |
184 | struct extent_position epos; |
185 | kernel_lb_addr eloc, neloc = { 0, 0 }; | |
186 | uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc; | |
1da177e4 | 187 | int8_t etype; |
31170b6a JK |
188 | struct super_block *sb = inode->i_sb; |
189 | sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset; | |
60448b1d | 190 | loff_t byte_offset; |
1da177e4 LT |
191 | int adsize; |
192 | ||
193 | if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT) | |
194 | adsize = sizeof(short_ad); | |
195 | else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG) | |
196 | adsize = sizeof(long_ad); | |
197 | else | |
ff116fc8 | 198 | BUG(); |
1da177e4 | 199 | |
ff116fc8 | 200 | etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset); |
cb00ea35 CG |
201 | byte_offset = |
202 | (offset << sb->s_blocksize_bits) + | |
203 | (inode->i_size & (sb->s_blocksize - 1)); | |
204 | if (etype != -1) { | |
ff116fc8 JK |
205 | epos.offset -= adsize; |
206 | extent_trunc(inode, &epos, eloc, etype, elen, byte_offset); | |
207 | epos.offset += adsize; | |
60448b1d | 208 | if (byte_offset) |
ff116fc8 | 209 | lenalloc = epos.offset; |
1da177e4 | 210 | else |
ff116fc8 | 211 | lenalloc = epos.offset - adsize; |
1da177e4 | 212 | |
ff116fc8 | 213 | if (!epos.bh) |
1da177e4 LT |
214 | lenalloc -= udf_file_entry_alloc_offset(inode); |
215 | else | |
216 | lenalloc -= sizeof(struct allocExtDesc); | |
217 | ||
cb00ea35 CG |
218 | while ((etype = |
219 | udf_current_aext(inode, &epos, &eloc, &elen, | |
220 | 0)) != -1) { | |
221 | if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) { | |
ff116fc8 | 222 | udf_write_aext(inode, &epos, neloc, nelen, 0); |
cb00ea35 | 223 | if (indirect_ext_len) { |
ff116fc8 JK |
224 | /* We managed to free all extents in the |
225 | * indirect extent - free it too */ | |
226 | if (!epos.bh) | |
1da177e4 | 227 | BUG(); |
cb00ea35 CG |
228 | udf_free_blocks(sb, inode, epos.block, |
229 | 0, indirect_ext_len); | |
230 | } else { | |
231 | if (!epos.bh) { | |
232 | UDF_I_LENALLOC(inode) = | |
233 | lenalloc; | |
1da177e4 | 234 | mark_inode_dirty(inode); |
cb00ea35 CG |
235 | } else { |
236 | struct allocExtDesc *aed = | |
237 | (struct allocExtDesc | |
238 | *)(epos.bh->b_data); | |
239 | aed->lengthAllocDescs = | |
240 | cpu_to_le32(lenalloc); | |
241 | if (!UDF_QUERY_FLAG | |
242 | (sb, UDF_FLAG_STRICT) | |
243 | || UDF_SB_UDFREV(sb) >= | |
244 | 0x0201) | |
245 | udf_update_tag(epos.bh-> | |
246 | b_data, | |
247 | lenalloc | |
248 | + | |
249 | sizeof | |
250 | (struct | |
251 | allocExtDesc)); | |
1da177e4 | 252 | else |
cb00ea35 CG |
253 | udf_update_tag(epos.bh-> |
254 | b_data, | |
255 | sizeof | |
256 | (struct | |
257 | allocExtDesc)); | |
258 | mark_buffer_dirty_inode(epos.bh, | |
259 | inode); | |
1da177e4 LT |
260 | } |
261 | } | |
ff116fc8 JK |
262 | brelse(epos.bh); |
263 | epos.offset = sizeof(struct allocExtDesc); | |
264 | epos.block = eloc; | |
cb00ea35 CG |
265 | epos.bh = |
266 | udf_tread(sb, | |
267 | udf_get_lb_pblock(sb, eloc, 0)); | |
1da177e4 | 268 | if (elen) |
ff116fc8 | 269 | indirect_ext_len = (elen + |
cb00ea35 CG |
270 | sb->s_blocksize - |
271 | 1) >> sb-> | |
272 | s_blocksize_bits; | |
1da177e4 | 273 | else |
ff116fc8 | 274 | indirect_ext_len = 1; |
cb00ea35 CG |
275 | } else { |
276 | extent_trunc(inode, &epos, eloc, etype, elen, | |
277 | 0); | |
ff116fc8 | 278 | epos.offset += adsize; |
1da177e4 LT |
279 | } |
280 | } | |
281 | ||
cb00ea35 | 282 | if (indirect_ext_len) { |
ff116fc8 | 283 | if (!epos.bh) |
1da177e4 | 284 | BUG(); |
cb00ea35 CG |
285 | udf_free_blocks(sb, inode, epos.block, 0, |
286 | indirect_ext_len); | |
287 | } else { | |
288 | if (!epos.bh) { | |
1da177e4 LT |
289 | UDF_I_LENALLOC(inode) = lenalloc; |
290 | mark_inode_dirty(inode); | |
cb00ea35 CG |
291 | } else { |
292 | struct allocExtDesc *aed = | |
293 | (struct allocExtDesc *)(epos.bh->b_data); | |
1da177e4 | 294 | aed->lengthAllocDescs = cpu_to_le32(lenalloc); |
cb00ea35 CG |
295 | if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) |
296 | || UDF_SB_UDFREV(sb) >= 0x0201) | |
297 | udf_update_tag(epos.bh->b_data, | |
298 | lenalloc + | |
299 | sizeof(struct | |
300 | allocExtDesc)); | |
1da177e4 | 301 | else |
cb00ea35 CG |
302 | udf_update_tag(epos.bh->b_data, |
303 | sizeof(struct | |
304 | allocExtDesc)); | |
ff116fc8 | 305 | mark_buffer_dirty_inode(epos.bh, inode); |
1da177e4 LT |
306 | } |
307 | } | |
cb00ea35 CG |
308 | } else if (inode->i_size) { |
309 | if (byte_offset) { | |
31170b6a JK |
310 | kernel_long_ad extent; |
311 | ||
00a2b0f6 JK |
312 | /* |
313 | * OK, there is not extent covering inode->i_size and | |
314 | * no extent above inode->i_size => truncate is | |
31170b6a | 315 | * extending the file by 'offset' blocks. |
00a2b0f6 | 316 | */ |
cb00ea35 CG |
317 | if ((!epos.bh |
318 | && epos.offset == | |
319 | udf_file_entry_alloc_offset(inode)) || (epos.bh | |
320 | && epos. | |
321 | offset == | |
322 | sizeof | |
323 | (struct | |
324 | allocExtDesc))) | |
325 | { | |
31170b6a JK |
326 | /* File has no extents at all or has empty last |
327 | * indirect extent! Create a fake extent... */ | |
328 | extent.extLocation.logicalBlockNum = 0; | |
329 | extent.extLocation.partitionReferenceNum = 0; | |
cb00ea35 CG |
330 | extent.extLength = |
331 | EXT_NOT_RECORDED_NOT_ALLOCATED; | |
332 | } else { | |
ff116fc8 | 333 | epos.offset -= adsize; |
31170b6a | 334 | etype = udf_next_aext(inode, &epos, |
cb00ea35 CG |
335 | &extent.extLocation, |
336 | &extent.extLength, 0); | |
31170b6a | 337 | extent.extLength |= etype << 30; |
1da177e4 | 338 | } |
cb00ea35 CG |
339 | udf_extend_file(inode, &epos, &extent, |
340 | offset + | |
341 | ((inode-> | |
342 | i_size & (sb->s_blocksize - 1)) != | |
343 | 0)); | |
1da177e4 LT |
344 | } |
345 | } | |
346 | UDF_I_LENEXTENTS(inode) = inode->i_size; | |
347 | ||
3bf25cb4 | 348 | brelse(epos.bh); |
1da177e4 | 349 | } |