blob: 60d277644248617aee14d98ddff62cf0e11c8f6a [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
Jan Karaff116fc2007-05-08 00:35:14 -070031static void extent_trunc(struct inode * inode, struct extent_position *epos,
32 kernel_lb_addr eloc, int8_t etype, uint32_t elen, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 kernel_lb_addr neloc = { 0, 0 };
35 int last_block = (elen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
36 int first_block = (nelen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
37
38 if (nelen)
39 {
40 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
41 {
42 udf_free_blocks(inode->i_sb, inode, eloc, 0, last_block);
43 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
44 }
45 else
46 neloc = eloc;
47 nelen = (etype << 30) | nelen;
48 }
49
50 if (elen != nelen)
51 {
Jan Karaff116fc2007-05-08 00:35:14 -070052 udf_write_aext(inode, epos, neloc, nelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (last_block - first_block > 0)
54 {
55 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
56 mark_inode_dirty(inode);
57
58 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
59 udf_free_blocks(inode->i_sb, inode, eloc, first_block, last_block - first_block);
60 }
61 }
62}
63
Jan Kara74584ae2007-06-16 10:16:14 -070064/*
65 * Truncate the last extent to match i_size. This function assumes
66 * that preallocation extent is already truncated.
67 */
68void udf_truncate_tail_extent(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Jan Karaff116fc2007-05-08 00:35:14 -070070 struct extent_position epos = { NULL, 0, {0, 0}};
71 kernel_lb_addr eloc;
72 uint32_t elen, nelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 uint64_t lbcount = 0;
74 int8_t etype = -1, netype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int adsize;
76
77 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
Jan Kara74584ae2007-06-16 10:16:14 -070078 inode->i_size == UDF_I_LENEXTENTS(inode))
79 return;
80 /* Are we going to delete the file anyway? */
81 if (inode->i_nlink == 0)
82 return;
83
84 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
85 adsize = sizeof(short_ad);
86 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
87 adsize = sizeof(long_ad);
88 else
89 BUG();
90
91 /* Find the last extent in the file */
92 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1)
93 {
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 */
120 UDF_I_LENEXTENTS(inode) = inode->i_size;
121 brelse(epos.bh);
122}
123
124void udf_discard_prealloc(struct inode *inode)
125{
126 struct extent_position epos = { NULL, 0, {0, 0}};
127 kernel_lb_addr eloc;
128 uint32_t elen;
129 uint64_t lbcount = 0;
130 int8_t etype = -1, netype;
131 int adsize;
132
133 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 inode->i_size == UDF_I_LENEXTENTS(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
138 adsize = sizeof(short_ad);
139 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
140 adsize = sizeof(long_ad);
141 else
142 adsize = 0;
143
Jan Karaff116fc2007-05-08 00:35:14 -0700144 epos.block = UDF_I_LOCATION(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jan Karaff116fc2007-05-08 00:35:14 -0700146 /* Find the last extent in the file */
Jan Kara74584ae2007-06-16 10:16:14 -0700147 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 etype = netype;
149 lbcount += elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Jan Karaff116fc2007-05-08 00:35:14 -0700151 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
152 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 lbcount -= elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700154 extent_trunc(inode, &epos, eloc, etype, elen, 0);
Jan Kara74584ae2007-06-16 10:16:14 -0700155 if (!epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -0700156 UDF_I_LENALLOC(inode) = epos.offset - udf_file_entry_alloc_offset(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 mark_inode_dirty(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700158 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700159 struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
160 aed->lengthAllocDescs = cpu_to_le32(epos.offset - sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -0700162 udf_update_tag(epos.bh->b_data, epos.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 else
Jan Karaff116fc2007-05-08 00:35:14 -0700164 udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
165 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 }
Jan Kara74584ae2007-06-16 10:16:14 -0700168 /* This inode entry is in-memory only and thus we don't have to mark
169 * the inode dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 UDF_I_LENEXTENTS(inode) = lbcount;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700171 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174void udf_truncate_extents(struct inode * inode)
175{
Jan Karaff116fc2007-05-08 00:35:14 -0700176 struct extent_position epos;
177 kernel_lb_addr eloc, neloc = { 0, 0 };
178 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int8_t etype;
Jan Kara31170b62007-05-08 00:35:21 -0700180 struct super_block *sb = inode->i_sb;
181 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
Jan Kara60448b12007-05-08 00:35:13 -0700182 loff_t byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int adsize;
184
185 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
186 adsize = sizeof(short_ad);
187 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
188 adsize = sizeof(long_ad);
189 else
Jan Karaff116fc2007-05-08 00:35:14 -0700190 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Jan Karaff116fc2007-05-08 00:35:14 -0700192 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
Jan Kara31170b62007-05-08 00:35:21 -0700193 byte_offset = (offset << sb->s_blocksize_bits) + (inode->i_size & (sb->s_blocksize-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (etype != -1)
195 {
Jan Karaff116fc2007-05-08 00:35:14 -0700196 epos.offset -= adsize;
197 extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
198 epos.offset += adsize;
Jan Kara60448b12007-05-08 00:35:13 -0700199 if (byte_offset)
Jan Karaff116fc2007-05-08 00:35:14 -0700200 lenalloc = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 else
Jan Karaff116fc2007-05-08 00:35:14 -0700202 lenalloc = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Jan Karaff116fc2007-05-08 00:35:14 -0700204 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 lenalloc -= udf_file_entry_alloc_offset(inode);
206 else
207 lenalloc -= sizeof(struct allocExtDesc);
208
Jan Karaff116fc2007-05-08 00:35:14 -0700209 while ((etype = udf_current_aext(inode, &epos, &eloc, &elen, 0)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 {
211 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30))
212 {
Jan Karaff116fc2007-05-08 00:35:14 -0700213 udf_write_aext(inode, &epos, neloc, nelen, 0);
214 if (indirect_ext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 {
Jan Karaff116fc2007-05-08 00:35:14 -0700216 /* We managed to free all extents in the
217 * indirect extent - free it too */
218 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 BUG();
Jan Kara31170b62007-05-08 00:35:21 -0700220 udf_free_blocks(sb, inode, epos.block, 0, indirect_ext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 else
223 {
Jan Karaff116fc2007-05-08 00:35:14 -0700224 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 {
226 UDF_I_LENALLOC(inode) = lenalloc;
227 mark_inode_dirty(inode);
228 }
229 else
230 {
Jan Karaff116fc2007-05-08 00:35:14 -0700231 struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
Jan Kara31170b62007-05-08 00:35:21 -0700233 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(sb) >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -0700234 udf_update_tag(epos.bh->b_data, lenalloc +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 sizeof(struct allocExtDesc));
236 else
Jan Karaff116fc2007-05-08 00:35:14 -0700237 udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
238 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 }
Jan Karaff116fc2007-05-08 00:35:14 -0700241 brelse(epos.bh);
242 epos.offset = sizeof(struct allocExtDesc);
243 epos.block = eloc;
Jan Kara31170b62007-05-08 00:35:21 -0700244 epos.bh = udf_tread(sb, udf_get_lb_pblock(sb, eloc, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (elen)
Jan Karaff116fc2007-05-08 00:35:14 -0700246 indirect_ext_len = (elen +
Jan Kara31170b62007-05-08 00:35:21 -0700247 sb->s_blocksize - 1) >>
248 sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 else
Jan Karaff116fc2007-05-08 00:35:14 -0700250 indirect_ext_len = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 else
253 {
Jan Karaff116fc2007-05-08 00:35:14 -0700254 extent_trunc(inode, &epos, eloc, etype, elen, 0);
255 epos.offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 }
258
Jan Karaff116fc2007-05-08 00:35:14 -0700259 if (indirect_ext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 {
Jan Karaff116fc2007-05-08 00:35:14 -0700261 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 BUG();
Jan Kara31170b62007-05-08 00:35:21 -0700263 udf_free_blocks(sb, inode, epos.block, 0, indirect_ext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 else
266 {
Jan Karaff116fc2007-05-08 00:35:14 -0700267 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 {
269 UDF_I_LENALLOC(inode) = lenalloc;
270 mark_inode_dirty(inode);
271 }
272 else
273 {
Jan Karaff116fc2007-05-08 00:35:14 -0700274 struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
Jan Kara31170b62007-05-08 00:35:21 -0700276 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(sb) >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -0700277 udf_update_tag(epos.bh->b_data, lenalloc +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 sizeof(struct allocExtDesc));
279 else
Jan Karaff116fc2007-05-08 00:35:14 -0700280 udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
281 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 }
284 }
285 else if (inode->i_size)
286 {
Jan Kara60448b12007-05-08 00:35:13 -0700287 if (byte_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 {
Jan Kara31170b62007-05-08 00:35:21 -0700289 kernel_long_ad extent;
290
Jan Kara00a2b0f2006-08-15 13:56:26 +0200291 /*
292 * OK, there is not extent covering inode->i_size and
293 * no extent above inode->i_size => truncate is
Jan Kara31170b62007-05-08 00:35:21 -0700294 * extending the file by 'offset' blocks.
Jan Kara00a2b0f2006-08-15 13:56:26 +0200295 */
Jan Karaff116fc2007-05-08 00:35:14 -0700296 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
297 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
Jan Kara31170b62007-05-08 00:35:21 -0700298 /* File has no extents at all or has empty last
299 * indirect extent! Create a fake extent... */
300 extent.extLocation.logicalBlockNum = 0;
301 extent.extLocation.partitionReferenceNum = 0;
302 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Jan Kara00a2b0f2006-08-15 13:56:26 +0200304 else {
Jan Karaff116fc2007-05-08 00:35:14 -0700305 epos.offset -= adsize;
Jan Kara31170b62007-05-08 00:35:21 -0700306 etype = udf_next_aext(inode, &epos,
307 &extent.extLocation, &extent.extLength, 0);
308 extent.extLength |= etype << 30;
Jan Kara00a2b0f2006-08-15 13:56:26 +0200309 }
Jan Kara31170b62007-05-08 00:35:21 -0700310 udf_extend_file(inode, &epos, &extent, offset+((inode->i_size & (sb->s_blocksize-1)) != 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 }
313 UDF_I_LENEXTENTS(inode) = inode->i_size;
314
Jan Kara3bf25cb2007-05-08 00:35:16 -0700315 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}