5 * Truncate handling routines for the OSTA-UDF(tm) filesystem.
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.
13 * (C) 1999-2004 Ben Fennema
14 * (C) 1999 Stelias Computing Inc
18 * 02/24/99 blf Created.
25 #include <linux/udf_fs.h>
26 #include <linux/buffer_head.h>
31 static void extent_trunc(struct inode *inode, struct extent_position *epos,
32 kernel_lb_addr eloc, int8_t etype, uint32_t elen,
35 kernel_lb_addr neloc = { 0, 0 };
37 (elen + inode->i_sb->s_blocksize -
38 1) >> inode->i_sb->s_blocksize_bits;
40 (nelen + inode->i_sb->s_blocksize -
41 1) >> inode->i_sb->s_blocksize_bits;
44 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
45 udf_free_blocks(inode->i_sb, inode, eloc, 0,
47 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
50 nelen = (etype << 30) | nelen;
54 udf_write_aext(inode, epos, neloc, nelen, 0);
55 if (last_block - first_block > 0) {
56 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
57 mark_inode_dirty(inode);
59 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
60 udf_free_blocks(inode->i_sb, inode, eloc,
62 last_block - first_block);
68 * Truncate the last extent to match i_size. This function assumes
69 * that preallocation extent is already truncated.
71 void udf_truncate_tail_extent(struct inode *inode)
73 struct extent_position epos = { NULL, 0, {0, 0} };
77 int8_t etype = -1, netype;
80 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
81 inode->i_size == UDF_I_LENEXTENTS(inode))
83 /* Are we going to delete the file anyway? */
84 if (inode->i_nlink == 0)
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);
94 /* Find the last extent in the file */
95 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
98 if (lbcount > inode->i_size) {
99 if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
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,
107 (unsigned)eloc.logicalBlockNum,
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);
120 /* This inode entry is in-memory only and thus we don't have to mark
122 UDF_I_LENEXTENTS(inode) = inode->i_size;
126 void udf_discard_prealloc(struct inode *inode)
128 struct extent_position epos = { NULL, 0, {0, 0} };
131 uint64_t lbcount = 0;
132 int8_t etype = -1, netype;
135 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
136 inode->i_size == UDF_I_LENEXTENTS(inode))
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);
146 epos.block = UDF_I_LOCATION(inode);
148 /* Find the last extent in the file */
149 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
153 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
154 epos.offset -= adsize;
156 extent_trunc(inode, &epos, eloc, etype, elen, 0);
158 UDF_I_LENALLOC(inode) =
159 epos.offset - udf_file_entry_alloc_offset(inode);
160 mark_inode_dirty(inode);
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)
169 udf_update_tag(epos.bh->b_data, epos.offset);
171 udf_update_tag(epos.bh->b_data,
172 sizeof(struct allocExtDesc));
173 mark_buffer_dirty_inode(epos.bh, inode);
176 /* This inode entry is in-memory only and thus we don't have to mark
178 UDF_I_LENEXTENTS(inode) = lbcount;
182 void udf_truncate_extents(struct inode *inode)
184 struct extent_position epos;
185 kernel_lb_addr eloc, neloc = { 0, 0 };
186 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
188 struct super_block *sb = inode->i_sb;
189 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
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);
200 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
202 (offset << sb->s_blocksize_bits) +
203 (inode->i_size & (sb->s_blocksize - 1));
205 epos.offset -= adsize;
206 extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
207 epos.offset += adsize;
209 lenalloc = epos.offset;
211 lenalloc = epos.offset - adsize;
214 lenalloc -= udf_file_entry_alloc_offset(inode);
216 lenalloc -= sizeof(struct allocExtDesc);
219 udf_current_aext(inode, &epos, &eloc, &elen,
221 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
222 udf_write_aext(inode, &epos, neloc, nelen, 0);
223 if (indirect_ext_len) {
224 /* We managed to free all extents in the
225 * indirect extent - free it too */
228 udf_free_blocks(sb, inode, epos.block,
229 0, indirect_ext_len);
232 UDF_I_LENALLOC(inode) =
234 mark_inode_dirty(inode);
236 struct allocExtDesc *aed =
239 aed->lengthAllocDescs =
240 cpu_to_le32(lenalloc);
242 (sb, UDF_FLAG_STRICT)
243 || UDF_SB_UDFREV(sb) >=
245 udf_update_tag(epos.bh->
253 udf_update_tag(epos.bh->
258 mark_buffer_dirty_inode(epos.bh,
263 epos.offset = sizeof(struct allocExtDesc);
267 udf_get_lb_pblock(sb, eloc, 0));
269 indirect_ext_len = (elen +
274 indirect_ext_len = 1;
276 extent_trunc(inode, &epos, eloc, etype, elen,
278 epos.offset += adsize;
282 if (indirect_ext_len) {
285 udf_free_blocks(sb, inode, epos.block, 0,
289 UDF_I_LENALLOC(inode) = lenalloc;
290 mark_inode_dirty(inode);
292 struct allocExtDesc *aed =
293 (struct allocExtDesc *)(epos.bh->b_data);
294 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
295 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)
296 || UDF_SB_UDFREV(sb) >= 0x0201)
297 udf_update_tag(epos.bh->b_data,
302 udf_update_tag(epos.bh->b_data,
305 mark_buffer_dirty_inode(epos.bh, inode);
308 } else if (inode->i_size) {
310 kernel_long_ad extent;
313 * OK, there is not extent covering inode->i_size and
314 * no extent above inode->i_size => truncate is
315 * extending the file by 'offset' blocks.
319 udf_file_entry_alloc_offset(inode)) || (epos.bh
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;
331 EXT_NOT_RECORDED_NOT_ALLOCATED;
333 epos.offset -= adsize;
334 etype = udf_next_aext(inode, &epos,
336 &extent.extLength, 0);
337 extent.extLength |= etype << 30;
339 udf_extend_file(inode, &epos, &extent,
342 i_size & (sb->s_blocksize - 1)) !=
346 UDF_I_LENEXTENTS(inode) = inode->i_size;