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
= {};
36 int last_block
= (elen
+ inode
->i_sb
->s_blocksize
- 1) >>
37 inode
->i_sb
->s_blocksize_bits
;
38 int first_block
= (nelen
+ inode
->i_sb
->s_blocksize
- 1) >>
39 inode
->i_sb
->s_blocksize_bits
;
42 if (etype
== (EXT_NOT_RECORDED_ALLOCATED
>> 30)) {
43 udf_free_blocks(inode
->i_sb
, inode
, eloc
, 0,
45 etype
= (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30);
48 nelen
= (etype
<< 30) | nelen
;
52 udf_write_aext(inode
, epos
, neloc
, nelen
, 0);
53 if (last_block
- first_block
> 0) {
54 if (etype
== (EXT_RECORDED_ALLOCATED
>> 30))
55 mark_inode_dirty(inode
);
57 if (etype
!= (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30))
58 udf_free_blocks(inode
->i_sb
, inode
, eloc
,
60 last_block
- first_block
);
66 * Truncate the last extent to match i_size. This function assumes
67 * that preallocation extent is already truncated.
69 void udf_truncate_tail_extent(struct inode
*inode
)
71 struct extent_position epos
= {};
75 int8_t etype
= -1, netype
;
78 if (UDF_I_ALLOCTYPE(inode
) == ICBTAG_FLAG_AD_IN_ICB
||
79 inode
->i_size
== UDF_I_LENEXTENTS(inode
))
81 /* Are we going to delete the file anyway? */
82 if (inode
->i_nlink
== 0)
85 if (UDF_I_ALLOCTYPE(inode
) == ICBTAG_FLAG_AD_SHORT
)
86 adsize
= sizeof(short_ad
);
87 else if (UDF_I_ALLOCTYPE(inode
) == ICBTAG_FLAG_AD_LONG
)
88 adsize
= sizeof(long_ad
);
92 /* Find the last extent in the file */
93 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
) =
157 epos
.offset
- udf_file_entry_alloc_offset(inode
);
158 mark_inode_dirty(inode
);
160 struct allocExtDesc
*aed
=
161 (struct allocExtDesc
*)(epos
.bh
->b_data
);
162 aed
->lengthAllocDescs
=
163 cpu_to_le32(epos
.offset
-
164 sizeof(struct allocExtDesc
));
165 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) ||
166 UDF_SB_UDFREV(inode
->i_sb
) >= 0x0201)
167 udf_update_tag(epos
.bh
->b_data
, epos
.offset
);
169 udf_update_tag(epos
.bh
->b_data
,
170 sizeof(struct allocExtDesc
));
171 mark_buffer_dirty_inode(epos
.bh
, inode
);
174 /* This inode entry is in-memory only and thus we don't have to mark
176 UDF_I_LENEXTENTS(inode
) = lbcount
;
180 void udf_truncate_extents(struct inode
*inode
)
182 struct extent_position epos
;
183 kernel_lb_addr eloc
, neloc
= {};
184 uint32_t elen
, nelen
= 0, indirect_ext_len
= 0, lenalloc
;
186 struct super_block
*sb
= inode
->i_sb
;
187 sector_t first_block
= inode
->i_size
>> sb
->s_blocksize_bits
, offset
;
191 if (UDF_I_ALLOCTYPE(inode
) == ICBTAG_FLAG_AD_SHORT
)
192 adsize
= sizeof(short_ad
);
193 else if (UDF_I_ALLOCTYPE(inode
) == ICBTAG_FLAG_AD_LONG
)
194 adsize
= sizeof(long_ad
);
198 etype
= inode_bmap(inode
, first_block
, &epos
, &eloc
, &elen
, &offset
);
199 byte_offset
= (offset
<< sb
->s_blocksize_bits
) +
200 (inode
->i_size
& (sb
->s_blocksize
- 1));
202 epos
.offset
-= adsize
;
203 extent_trunc(inode
, &epos
, eloc
, etype
, elen
, byte_offset
);
204 epos
.offset
+= adsize
;
206 lenalloc
= epos
.offset
;
208 lenalloc
= epos
.offset
- adsize
;
211 lenalloc
-= udf_file_entry_alloc_offset(inode
);
213 lenalloc
-= sizeof(struct allocExtDesc
);
215 while ((etype
= udf_current_aext(inode
, &epos
, &eloc
, &elen
, 0)) != -1) {
216 if (etype
== (EXT_NEXT_EXTENT_ALLOCDECS
>> 30)) {
217 udf_write_aext(inode
, &epos
, neloc
, nelen
, 0);
218 if (indirect_ext_len
) {
219 /* We managed to free all extents in the
220 * indirect extent - free it too */
223 udf_free_blocks(sb
, inode
, epos
.block
,
224 0, indirect_ext_len
);
227 UDF_I_LENALLOC(inode
) = lenalloc
;
228 mark_inode_dirty(inode
);
230 struct allocExtDesc
*aed
=
231 (struct allocExtDesc
*)(epos
.bh
->b_data
);
232 aed
->lengthAllocDescs
=
233 cpu_to_le32(lenalloc
);
234 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_STRICT
) ||
235 UDF_SB_UDFREV(sb
) >= 0x0201)
236 udf_update_tag(epos
.bh
->b_data
,
238 sizeof(struct allocExtDesc
));
240 udf_update_tag(epos
.bh
->b_data
,
241 sizeof(struct allocExtDesc
));
242 mark_buffer_dirty_inode(epos
.bh
, inode
);
246 epos
.offset
= sizeof(struct allocExtDesc
);
248 epos
.bh
= udf_tread(sb
, udf_get_lb_pblock(sb
, eloc
, 0));
250 indirect_ext_len
= (elen
+ sb
->s_blocksize
-1) >>
251 sb
->s_blocksize_bits
;
253 indirect_ext_len
= 1;
255 extent_trunc(inode
, &epos
, eloc
, etype
, elen
, 0);
256 epos
.offset
+= adsize
;
260 if (indirect_ext_len
) {
263 udf_free_blocks(sb
, inode
, epos
.block
, 0,
267 UDF_I_LENALLOC(inode
) = lenalloc
;
268 mark_inode_dirty(inode
);
270 struct allocExtDesc
*aed
=
271 (struct allocExtDesc
*)(epos
.bh
->b_data
);
272 aed
->lengthAllocDescs
= cpu_to_le32(lenalloc
);
273 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_STRICT
) ||
274 UDF_SB_UDFREV(sb
) >= 0x0201)
275 udf_update_tag(epos
.bh
->b_data
,
276 lenalloc
+ sizeof(struct allocExtDesc
));
278 udf_update_tag(epos
.bh
->b_data
,
279 sizeof(struct allocExtDesc
));
280 mark_buffer_dirty_inode(epos
.bh
, inode
);
283 } else if (inode
->i_size
) {
285 kernel_long_ad extent
;
288 * OK, there is not extent covering inode->i_size and
289 * no extent above inode->i_size => truncate is
290 * extending the file by 'offset' blocks.
293 epos
.offset
== udf_file_entry_alloc_offset(inode
)) ||
294 (epos
.bh
&& epos
.offset
== sizeof(struct allocExtDesc
))) {
295 /* File has no extents at all or has empty last
296 * indirect extent! Create a fake extent... */
297 extent
.extLocation
.logicalBlockNum
= 0;
298 extent
.extLocation
.partitionReferenceNum
= 0;
299 extent
.extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
;
301 epos
.offset
-= adsize
;
302 etype
= udf_next_aext(inode
, &epos
,
304 &extent
.extLength
, 0);
305 extent
.extLength
|= etype
<< 30;
307 udf_extend_file(inode
, &epos
, &extent
,
308 offset
+ ((inode
->i_size
& (sb
->s_blocksize
- 1)) != 0));
311 UDF_I_LENEXTENTS(inode
) = inode
->i_size
;