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
, uint32_t nelen
)
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
;
40 if (etype
== (EXT_NOT_RECORDED_ALLOCATED
>> 30))
42 udf_free_blocks(inode
->i_sb
, inode
, eloc
, 0, last_block
);
43 etype
= (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30);
47 nelen
= (etype
<< 30) | nelen
;
52 udf_write_aext(inode
, epos
, neloc
, nelen
, 0);
53 if (last_block
- first_block
> 0)
55 if (etype
== (EXT_RECORDED_ALLOCATED
>> 30))
56 mark_inode_dirty(inode
);
58 if (etype
!= (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30))
59 udf_free_blocks(inode
->i_sb
, inode
, eloc
, first_block
, last_block
- first_block
);
65 * Truncate the last extent to match i_size. This function assumes
66 * that preallocation extent is already truncated.
68 void udf_truncate_tail_extent(struct inode
*inode
)
70 struct extent_position epos
= { NULL
, 0, {0, 0}};
74 int8_t etype
= -1, netype
;
77 if (UDF_I_ALLOCTYPE(inode
) == ICBTAG_FLAG_AD_IN_ICB
||
78 inode
->i_size
== UDF_I_LENEXTENTS(inode
))
80 /* Are we going to delete the file anyway? */
81 if (inode
->i_nlink
== 0)
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
);
91 /* Find the last extent in the file */
92 while ((netype
= udf_next_aext(inode
, &epos
, &eloc
, &elen
, 1)) != -1)
96 if (lbcount
> inode
->i_size
) {
97 if (lbcount
- inode
->i_size
>= inode
->i_sb
->s_blocksize
)
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
,
105 (unsigned)eloc
.logicalBlockNum
,
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
);
118 /* This inode entry is in-memory only and thus we don't have to mark
120 UDF_I_LENEXTENTS(inode
) = inode
->i_size
;
124 void udf_discard_prealloc(struct inode
*inode
)
126 struct extent_position epos
= { NULL
, 0, {0, 0}};
129 uint64_t lbcount
= 0;
130 int8_t etype
= -1, netype
;
133 if (UDF_I_ALLOCTYPE(inode
) == ICBTAG_FLAG_AD_IN_ICB
||
134 inode
->i_size
== UDF_I_LENEXTENTS(inode
))
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
);
144 epos
.block
= UDF_I_LOCATION(inode
);
146 /* Find the last extent in the file */
147 while ((netype
= udf_next_aext(inode
, &epos
, &eloc
, &elen
, 1)) != -1) {
151 if (etype
== (EXT_NOT_RECORDED_ALLOCATED
>> 30)) {
152 epos
.offset
-= adsize
;
154 extent_trunc(inode
, &epos
, eloc
, etype
, elen
, 0);
156 UDF_I_LENALLOC(inode
) = epos
.offset
- udf_file_entry_alloc_offset(inode
);
157 mark_inode_dirty(inode
);
159 struct allocExtDesc
*aed
= (struct allocExtDesc
*)(epos
.bh
->b_data
);
160 aed
->lengthAllocDescs
= cpu_to_le32(epos
.offset
- sizeof(struct allocExtDesc
));
161 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) || UDF_SB_UDFREV(inode
->i_sb
) >= 0x0201)
162 udf_update_tag(epos
.bh
->b_data
, epos
.offset
);
164 udf_update_tag(epos
.bh
->b_data
, sizeof(struct allocExtDesc
));
165 mark_buffer_dirty_inode(epos
.bh
, inode
);
168 /* This inode entry is in-memory only and thus we don't have to mark
170 UDF_I_LENEXTENTS(inode
) = lbcount
;
174 void udf_truncate_extents(struct inode
* inode
)
176 struct extent_position epos
;
177 kernel_lb_addr eloc
, neloc
= { 0, 0 };
178 uint32_t elen
, nelen
= 0, indirect_ext_len
= 0, lenalloc
;
180 struct super_block
*sb
= inode
->i_sb
;
181 sector_t first_block
= inode
->i_size
>> sb
->s_blocksize_bits
, offset
;
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
);
192 etype
= inode_bmap(inode
, first_block
, &epos
, &eloc
, &elen
, &offset
);
193 byte_offset
= (offset
<< sb
->s_blocksize_bits
) + (inode
->i_size
& (sb
->s_blocksize
-1));
196 epos
.offset
-= adsize
;
197 extent_trunc(inode
, &epos
, eloc
, etype
, elen
, byte_offset
);
198 epos
.offset
+= adsize
;
200 lenalloc
= epos
.offset
;
202 lenalloc
= epos
.offset
- adsize
;
205 lenalloc
-= udf_file_entry_alloc_offset(inode
);
207 lenalloc
-= sizeof(struct allocExtDesc
);
209 while ((etype
= udf_current_aext(inode
, &epos
, &eloc
, &elen
, 0)) != -1)
211 if (etype
== (EXT_NEXT_EXTENT_ALLOCDECS
>> 30))
213 udf_write_aext(inode
, &epos
, neloc
, nelen
, 0);
214 if (indirect_ext_len
)
216 /* We managed to free all extents in the
217 * indirect extent - free it too */
220 udf_free_blocks(sb
, inode
, epos
.block
, 0, indirect_ext_len
);
226 UDF_I_LENALLOC(inode
) = lenalloc
;
227 mark_inode_dirty(inode
);
231 struct allocExtDesc
*aed
= (struct allocExtDesc
*)(epos
.bh
->b_data
);
232 aed
->lengthAllocDescs
= cpu_to_le32(lenalloc
);
233 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_STRICT
) || UDF_SB_UDFREV(sb
) >= 0x0201)
234 udf_update_tag(epos
.bh
->b_data
, lenalloc
+
235 sizeof(struct allocExtDesc
));
237 udf_update_tag(epos
.bh
->b_data
, sizeof(struct allocExtDesc
));
238 mark_buffer_dirty_inode(epos
.bh
, inode
);
242 epos
.offset
= sizeof(struct allocExtDesc
);
244 epos
.bh
= udf_tread(sb
, udf_get_lb_pblock(sb
, eloc
, 0));
246 indirect_ext_len
= (elen
+
247 sb
->s_blocksize
- 1) >>
248 sb
->s_blocksize_bits
;
250 indirect_ext_len
= 1;
254 extent_trunc(inode
, &epos
, eloc
, etype
, elen
, 0);
255 epos
.offset
+= adsize
;
259 if (indirect_ext_len
)
263 udf_free_blocks(sb
, inode
, epos
.block
, 0, indirect_ext_len
);
269 UDF_I_LENALLOC(inode
) = lenalloc
;
270 mark_inode_dirty(inode
);
274 struct allocExtDesc
*aed
= (struct allocExtDesc
*)(epos
.bh
->b_data
);
275 aed
->lengthAllocDescs
= cpu_to_le32(lenalloc
);
276 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_STRICT
) || UDF_SB_UDFREV(sb
) >= 0x0201)
277 udf_update_tag(epos
.bh
->b_data
, lenalloc
+
278 sizeof(struct allocExtDesc
));
280 udf_update_tag(epos
.bh
->b_data
, sizeof(struct allocExtDesc
));
281 mark_buffer_dirty_inode(epos
.bh
, inode
);
285 else if (inode
->i_size
)
289 kernel_long_ad extent
;
292 * OK, there is not extent covering inode->i_size and
293 * no extent above inode->i_size => truncate is
294 * extending the file by 'offset' blocks.
296 if ((!epos
.bh
&& epos
.offset
== udf_file_entry_alloc_offset(inode
)) ||
297 (epos
.bh
&& epos
.offset
== sizeof(struct allocExtDesc
))) {
298 /* 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
;
305 epos
.offset
-= adsize
;
306 etype
= udf_next_aext(inode
, &epos
,
307 &extent
.extLocation
, &extent
.extLength
, 0);
308 extent
.extLength
|= etype
<< 30;
310 udf_extend_file(inode
, &epos
, &extent
, offset
+((inode
->i_size
& (sb
->s_blocksize
-1)) != 0));
313 UDF_I_LENEXTENTS(inode
) = inode
->i_size
;