blob: b2002da0a5c0ee4dc521e01d664e8a2efdc41e3d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * truncate.c
3 *
4 * PURPOSE
5 * Truncate handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 Gorcunovcb00ea32007-07-19 01:47:43 -070031static 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 Torvalds1da177e2005-04-16 15:20:36 -070034{
35 kernel_lb_addr neloc = { 0, 0 };
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070036 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070043 if (nelen) {
44 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
45 udf_free_blocks(inode->i_sb, inode, eloc, 0,
46 last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070048 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 neloc = eloc;
50 nelen = (etype << 30) | nelen;
51 }
52
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070053 if (elen != nelen) {
Jan Karaff116fc2007-05-08 00:35:14 -070054 udf_write_aext(inode, epos, neloc, nelen, 0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070055 if (last_block - first_block > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
57 mark_inode_dirty(inode);
58
59 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070060 udf_free_blocks(inode->i_sb, inode, eloc,
61 first_block,
62 last_block - first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64 }
65}
66
Jan Kara74584ae2007-06-16 10:16:14 -070067/*
68 * Truncate the last extent to match i_size. This function assumes
69 * that preallocation extent is already truncated.
70 */
71void udf_truncate_tail_extent(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070073 struct extent_position epos = { NULL, 0, {0, 0} };
Jan Karaff116fc2007-05-08 00:35:14 -070074 kernel_lb_addr eloc;
75 uint32_t elen, nelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 uint64_t lbcount = 0;
77 int8_t etype = -1, netype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int adsize;
79
80 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
Jan Kara74584ae2007-06-16 10:16:14 -070081 inode->i_size == UDF_I_LENEXTENTS(inode))
82 return;
83 /* Are we going to delete the file anyway? */
84 if (inode->i_nlink == 0)
85 return;
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
92 BUG();
93
94 /* Find the last extent in the file */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070095 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Jan Kara74584ae2007-06-16 10:16:14 -070096 etype = netype;
97 lbcount += elen;
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);
109 nelen = elen - (lbcount - inode->i_size);
110 epos.offset -= adsize;
111 extent_trunc(inode, &epos, eloc, etype, elen, nelen);
112 epos.offset += adsize;
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;
118 }
119 }
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
126void udf_discard_prealloc(struct inode *inode)
127{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700128 struct extent_position epos = { NULL, 0, {0, 0} };
Jan Kara74584ae2007-06-16 10:16:14 -0700129 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 ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700136 inode->i_size == UDF_I_LENEXTENTS(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
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
Jan Karaff116fc2007-05-08 00:35:14 -0700146 epos.block = UDF_I_LOCATION(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jan Karaff116fc2007-05-08 00:35:14 -0700148 /* Find the last extent in the file */
Jan Kara74584ae2007-06-16 10:16:14 -0700149 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 etype = netype;
151 lbcount += elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Jan Karaff116fc2007-05-08 00:35:14 -0700153 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
154 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 lbcount -= elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700156 extent_trunc(inode, &epos, eloc, etype, elen, 0);
Jan Kara74584ae2007-06-16 10:16:14 -0700157 if (!epos.bh) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700158 UDF_I_LENALLOC(inode) =
159 epos.offset - udf_file_entry_alloc_offset(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 mark_inode_dirty(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700161 } else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 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)
Jan Karaff116fc2007-05-08 00:35:14 -0700169 udf_update_tag(epos.bh->b_data, epos.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700171 udf_update_tag(epos.bh->b_data,
172 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -0700173 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 }
Jan Kara74584ae2007-06-16 10:16:14 -0700176 /* This inode entry is in-memory only and thus we don't have to mark
177 * the inode dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 UDF_I_LENEXTENTS(inode) = lbcount;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700179 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700182void udf_truncate_extents(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Jan Karaff116fc2007-05-08 00:35:14 -0700184 struct extent_position epos;
185 kernel_lb_addr eloc, neloc = { 0, 0 };
186 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int8_t etype;
Jan Kara31170b62007-05-08 00:35:21 -0700188 struct super_block *sb = inode->i_sb;
189 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
Jan Kara60448b12007-05-08 00:35:13 -0700190 loff_t byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 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
Jan Karaff116fc2007-05-08 00:35:14 -0700198 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Jan Karaff116fc2007-05-08 00:35:14 -0700200 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700201 byte_offset =
202 (offset << sb->s_blocksize_bits) +
203 (inode->i_size & (sb->s_blocksize - 1));
204 if (etype != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -0700205 epos.offset -= adsize;
206 extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
207 epos.offset += adsize;
Jan Kara60448b12007-05-08 00:35:13 -0700208 if (byte_offset)
Jan Karaff116fc2007-05-08 00:35:14 -0700209 lenalloc = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 else
Jan Karaff116fc2007-05-08 00:35:14 -0700211 lenalloc = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Jan Karaff116fc2007-05-08 00:35:14 -0700213 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 lenalloc -= udf_file_entry_alloc_offset(inode);
215 else
216 lenalloc -= sizeof(struct allocExtDesc);
217
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700218 while ((etype =
219 udf_current_aext(inode, &epos, &eloc, &elen,
220 0)) != -1) {
221 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Jan Karaff116fc2007-05-08 00:35:14 -0700222 udf_write_aext(inode, &epos, neloc, nelen, 0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700223 if (indirect_ext_len) {
Jan Karaff116fc2007-05-08 00:35:14 -0700224 /* We managed to free all extents in the
225 * indirect extent - free it too */
226 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 BUG();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700228 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700235 } 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700253 udf_update_tag(epos.bh->
254 b_data,
255 sizeof
256 (struct
257 allocExtDesc));
258 mark_buffer_dirty_inode(epos.bh,
259 inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 }
Jan Karaff116fc2007-05-08 00:35:14 -0700262 brelse(epos.bh);
263 epos.offset = sizeof(struct allocExtDesc);
264 epos.block = eloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700265 epos.bh =
266 udf_tread(sb,
267 udf_get_lb_pblock(sb, eloc, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (elen)
Jan Karaff116fc2007-05-08 00:35:14 -0700269 indirect_ext_len = (elen +
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700270 sb->s_blocksize -
271 1) >> sb->
272 s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 else
Jan Karaff116fc2007-05-08 00:35:14 -0700274 indirect_ext_len = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700275 } else {
276 extent_trunc(inode, &epos, eloc, etype, elen,
277 0);
Jan Karaff116fc2007-05-08 00:35:14 -0700278 epos.offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 }
281
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700282 if (indirect_ext_len) {
Jan Karaff116fc2007-05-08 00:35:14 -0700283 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 BUG();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700285 udf_free_blocks(sb, inode, epos.block, 0,
286 indirect_ext_len);
287 } else {
288 if (!epos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 UDF_I_LENALLOC(inode) = lenalloc;
290 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700291 } else {
292 struct allocExtDesc *aed =
293 (struct allocExtDesc *)(epos.bh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700295 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700302 udf_update_tag(epos.bh->b_data,
303 sizeof(struct
304 allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -0700305 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700308 } else if (inode->i_size) {
309 if (byte_offset) {
Jan Kara31170b62007-05-08 00:35:21 -0700310 kernel_long_ad extent;
311
Jan Kara00a2b0f2006-08-15 13:56:26 +0200312 /*
313 * OK, there is not extent covering inode->i_size and
314 * no extent above inode->i_size => truncate is
Jan Kara31170b62007-05-08 00:35:21 -0700315 * extending the file by 'offset' blocks.
Jan Kara00a2b0f2006-08-15 13:56:26 +0200316 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700317 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 {
Jan Kara31170b62007-05-08 00:35:21 -0700326 /* 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;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700330 extent.extLength =
331 EXT_NOT_RECORDED_NOT_ALLOCATED;
332 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700333 epos.offset -= adsize;
Jan Kara31170b62007-05-08 00:35:21 -0700334 etype = udf_next_aext(inode, &epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700335 &extent.extLocation,
336 &extent.extLength, 0);
Jan Kara31170b62007-05-08 00:35:21 -0700337 extent.extLength |= etype << 30;
Jan Kara00a2b0f2006-08-15 13:56:26 +0200338 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700339 udf_extend_file(inode, &epos, &extent,
340 offset +
341 ((inode->
342 i_size & (sb->s_blocksize - 1)) !=
343 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 }
346 UDF_I_LENEXTENTS(inode) = inode->i_size;
347
Jan Kara3bf25cb2007-05-08 00:35:16 -0700348 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}