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/buffer_head.h>
30 static void extent_trunc(struct inode
*inode
, struct extent_position
*epos
,
31 kernel_lb_addr eloc
, int8_t etype
, uint32_t elen
,
34 kernel_lb_addr neloc
= {};
35 int last_block
= (elen
+ inode
->i_sb
->s_blocksize
- 1) >>
36 inode
->i_sb
->s_blocksize_bits
;
37 int first_block
= (nelen
+ inode
->i_sb
->s_blocksize
- 1) >>
38 inode
->i_sb
->s_blocksize_bits
;
41 if (etype
== (EXT_NOT_RECORDED_ALLOCATED
>> 30)) {
42 udf_free_blocks(inode
->i_sb
, inode
, eloc
, 0,
44 etype
= (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30);
47 nelen
= (etype
<< 30) | nelen
;
51 udf_write_aext(inode
, epos
, neloc
, nelen
, 0);
52 if (last_block
- first_block
> 0) {
53 if (etype
== (EXT_RECORDED_ALLOCATED
>> 30))
54 mark_inode_dirty(inode
);
56 if (etype
!= (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30))
57 udf_free_blocks(inode
->i_sb
, inode
, eloc
,
59 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
= {};
74 int8_t etype
= -1, netype
;
76 struct udf_inode_info
*iinfo
= UDF_I(inode
);
78 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
||
79 inode
->i_size
== iinfo
->i_lenExtents
)
81 /* Are we going to delete the file anyway? */
82 if (inode
->i_nlink
== 0)
85 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
86 adsize
= sizeof(short_ad
);
87 else if (iinfo
->i_alloc_type
== 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 iinfo
->i_lenExtents
= 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
;
132 struct udf_inode_info
*iinfo
= UDF_I(inode
);
134 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
||
135 inode
->i_size
== iinfo
->i_lenExtents
)
138 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
139 adsize
= sizeof(short_ad
);
140 else if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
141 adsize
= sizeof(long_ad
);
145 epos
.block
= iinfo
->i_location
;
147 /* Find the last extent in the file */
148 while ((netype
= udf_next_aext(inode
, &epos
, &eloc
, &elen
, 1)) != -1) {
152 if (etype
== (EXT_NOT_RECORDED_ALLOCATED
>> 30)) {
153 epos
.offset
-= adsize
;
155 extent_trunc(inode
, &epos
, eloc
, etype
, elen
, 0);
159 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(inode
->i_sb
)->s_udfrev
>= 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 iinfo
->i_lenExtents
= lbcount
;
182 static void udf_update_alloc_ext_desc(struct inode
*inode
,
183 struct extent_position
*epos
,
186 struct super_block
*sb
= inode
->i_sb
;
187 struct udf_sb_info
*sbi
= UDF_SB(sb
);
189 struct allocExtDesc
*aed
= (struct allocExtDesc
*) (epos
->bh
->b_data
);
190 int len
= sizeof(struct allocExtDesc
);
192 aed
->lengthAllocDescs
= cpu_to_le32(lenalloc
);
193 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_STRICT
) || sbi
->s_udfrev
>= 0x0201)
196 udf_update_tag(epos
->bh
->b_data
, len
);
197 mark_buffer_dirty_inode(epos
->bh
, inode
);
200 void udf_truncate_extents(struct inode
*inode
)
202 struct extent_position epos
;
203 kernel_lb_addr eloc
, neloc
= {};
204 uint32_t elen
, nelen
= 0, indirect_ext_len
= 0, lenalloc
;
206 struct super_block
*sb
= inode
->i_sb
;
207 sector_t first_block
= inode
->i_size
>> sb
->s_blocksize_bits
, offset
;
210 struct udf_inode_info
*iinfo
= UDF_I(inode
);
212 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
213 adsize
= sizeof(short_ad
);
214 else if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
215 adsize
= sizeof(long_ad
);
219 etype
= inode_bmap(inode
, first_block
, &epos
, &eloc
, &elen
, &offset
);
220 byte_offset
= (offset
<< sb
->s_blocksize_bits
) +
221 (inode
->i_size
& (sb
->s_blocksize
- 1));
223 epos
.offset
-= adsize
;
224 extent_trunc(inode
, &epos
, eloc
, etype
, elen
, byte_offset
);
225 epos
.offset
+= adsize
;
227 lenalloc
= epos
.offset
;
229 lenalloc
= epos
.offset
- adsize
;
232 lenalloc
-= udf_file_entry_alloc_offset(inode
);
234 lenalloc
-= sizeof(struct allocExtDesc
);
236 while ((etype
= udf_current_aext(inode
, &epos
, &eloc
,
238 if (etype
== (EXT_NEXT_EXTENT_ALLOCDECS
>> 30)) {
239 udf_write_aext(inode
, &epos
, neloc
, nelen
, 0);
240 if (indirect_ext_len
) {
241 /* We managed to free all extents in the
242 * indirect extent - free it too */
244 udf_free_blocks(sb
, inode
, epos
.block
,
245 0, indirect_ext_len
);
246 } else if (!epos
.bh
) {
247 iinfo
->i_lenAlloc
= lenalloc
;
248 mark_inode_dirty(inode
);
250 udf_update_alloc_ext_desc(inode
,
253 epos
.offset
= sizeof(struct allocExtDesc
);
255 epos
.bh
= udf_tread(sb
,
256 udf_get_lb_pblock(sb
, eloc
, 0));
259 (elen
+ sb
->s_blocksize
- 1) >>
260 sb
->s_blocksize_bits
;
262 indirect_ext_len
= 1;
264 extent_trunc(inode
, &epos
, eloc
, etype
,
266 epos
.offset
+= adsize
;
270 if (indirect_ext_len
) {
272 udf_free_blocks(sb
, inode
, epos
.block
, 0,
274 } else if (!epos
.bh
) {
275 iinfo
->i_lenAlloc
= lenalloc
;
276 mark_inode_dirty(inode
);
278 udf_update_alloc_ext_desc(inode
, &epos
, lenalloc
);
279 } else if (inode
->i_size
) {
281 kernel_long_ad extent
;
284 * OK, there is not extent covering inode->i_size and
285 * no extent above inode->i_size => truncate is
286 * extending the file by 'offset' blocks.
290 udf_file_entry_alloc_offset(inode
)) ||
291 (epos
.bh
&& epos
.offset
==
292 sizeof(struct allocExtDesc
))) {
293 /* File has no extents at all or has empty last
294 * indirect extent! Create a fake extent... */
295 extent
.extLocation
.logicalBlockNum
= 0;
296 extent
.extLocation
.partitionReferenceNum
= 0;
298 EXT_NOT_RECORDED_NOT_ALLOCATED
;
300 epos
.offset
-= adsize
;
301 etype
= udf_next_aext(inode
, &epos
,
303 &extent
.extLength
, 0);
304 extent
.extLength
|= etype
<< 30;
306 udf_extend_file(inode
, &epos
, &extent
,
309 (sb
->s_blocksize
- 1)) != 0));
312 iinfo
->i_lenExtents
= inode
->i_size
;