]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * truncate.c | |
3 | * | |
4 | * PURPOSE | |
5 | * Truncate handling routines for the OSTA-UDF(tm) filesystem. | |
6 | * | |
1da177e4 LT |
7 | * 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> | |
1da177e4 LT |
25 | #include <linux/buffer_head.h> |
26 | ||
27 | #include "udf_i.h" | |
28 | #include "udf_sb.h" | |
29 | ||
cb00ea35 | 30 | static void extent_trunc(struct inode *inode, struct extent_position *epos, |
97e961fd | 31 | struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen, |
cb00ea35 | 32 | uint32_t nelen) |
1da177e4 | 33 | { |
5ca4e4be | 34 | struct kernel_lb_addr neloc = {}; |
28de7948 CG |
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; | |
1da177e4 | 39 | |
cb00ea35 CG |
40 | if (nelen) { |
41 | if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { | |
42 | udf_free_blocks(inode->i_sb, inode, eloc, 0, | |
43 | last_block); | |
1da177e4 | 44 | etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30); |
cb00ea35 | 45 | } else |
97e961fd | 46 | neloc = *eloc; |
1da177e4 LT |
47 | nelen = (etype << 30) | nelen; |
48 | } | |
49 | ||
cb00ea35 | 50 | if (elen != nelen) { |
97e961fd | 51 | udf_write_aext(inode, epos, &neloc, nelen, 0); |
cb00ea35 | 52 | if (last_block - first_block > 0) { |
1da177e4 LT |
53 | if (etype == (EXT_RECORDED_ALLOCATED >> 30)) |
54 | mark_inode_dirty(inode); | |
55 | ||
56 | if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) | |
cb00ea35 CG |
57 | udf_free_blocks(inode->i_sb, inode, eloc, |
58 | first_block, | |
59 | last_block - first_block); | |
1da177e4 LT |
60 | } |
61 | } | |
62 | } | |
63 | ||
74584ae5 JK |
64 | /* |
65 | * Truncate the last extent to match i_size. This function assumes | |
66 | * that preallocation extent is already truncated. | |
67 | */ | |
68 | void udf_truncate_tail_extent(struct inode *inode) | |
1da177e4 | 69 | { |
28de7948 | 70 | struct extent_position epos = {}; |
5ca4e4be | 71 | struct kernel_lb_addr eloc; |
ff116fc8 | 72 | uint32_t elen, nelen; |
1da177e4 LT |
73 | uint64_t lbcount = 0; |
74 | int8_t etype = -1, netype; | |
1da177e4 | 75 | int adsize; |
48d6d8ff | 76 | struct udf_inode_info *iinfo = UDF_I(inode); |
1da177e4 | 77 | |
48d6d8ff MS |
78 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB || |
79 | inode->i_size == iinfo->i_lenExtents) | |
74584ae5 JK |
80 | return; |
81 | /* Are we going to delete the file anyway? */ | |
82 | if (inode->i_nlink == 0) | |
1da177e4 | 83 | return; |
1da177e4 | 84 | |
48d6d8ff | 85 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
5ca4e4be | 86 | adsize = sizeof(struct short_ad); |
48d6d8ff | 87 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 88 | adsize = sizeof(struct long_ad); |
1da177e4 | 89 | else |
74584ae5 | 90 | BUG(); |
1da177e4 | 91 | |
ff116fc8 | 92 | /* Find the last extent in the file */ |
cb00ea35 | 93 | while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { |
1da177e4 LT |
94 | etype = netype; |
95 | lbcount += elen; | |
74584ae5 JK |
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); | |
1da177e4 | 107 | nelen = elen - (lbcount - inode->i_size); |
ff116fc8 | 108 | epos.offset -= adsize; |
97e961fd | 109 | extent_trunc(inode, &epos, &eloc, etype, elen, nelen); |
ff116fc8 | 110 | epos.offset += adsize; |
74584ae5 JK |
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; | |
1da177e4 LT |
116 | } |
117 | } | |
74584ae5 JK |
118 | /* This inode entry is in-memory only and thus we don't have to mark |
119 | * the inode dirty */ | |
48d6d8ff | 120 | iinfo->i_lenExtents = inode->i_size; |
74584ae5 JK |
121 | brelse(epos.bh); |
122 | } | |
123 | ||
124 | void udf_discard_prealloc(struct inode *inode) | |
125 | { | |
cb00ea35 | 126 | struct extent_position epos = { NULL, 0, {0, 0} }; |
5ca4e4be | 127 | struct kernel_lb_addr eloc; |
74584ae5 JK |
128 | uint32_t elen; |
129 | uint64_t lbcount = 0; | |
130 | int8_t etype = -1, netype; | |
131 | int adsize; | |
48d6d8ff | 132 | struct udf_inode_info *iinfo = UDF_I(inode); |
74584ae5 | 133 | |
48d6d8ff MS |
134 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB || |
135 | inode->i_size == iinfo->i_lenExtents) | |
74584ae5 JK |
136 | return; |
137 | ||
48d6d8ff | 138 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
5ca4e4be | 139 | adsize = sizeof(struct short_ad); |
48d6d8ff | 140 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 141 | adsize = sizeof(struct long_ad); |
74584ae5 JK |
142 | else |
143 | adsize = 0; | |
144 | ||
48d6d8ff | 145 | epos.block = iinfo->i_location; |
74584ae5 JK |
146 | |
147 | /* Find the last extent in the file */ | |
148 | while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { | |
149 | etype = netype; | |
150 | lbcount += elen; | |
151 | } | |
ff116fc8 JK |
152 | if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { |
153 | epos.offset -= adsize; | |
1da177e4 | 154 | lbcount -= elen; |
97e961fd | 155 | extent_trunc(inode, &epos, &eloc, etype, elen, 0); |
74584ae5 | 156 | if (!epos.bh) { |
48d6d8ff | 157 | iinfo->i_lenAlloc = |
4b11111a MS |
158 | epos.offset - |
159 | udf_file_entry_alloc_offset(inode); | |
1da177e4 | 160 | mark_inode_dirty(inode); |
74584ae5 | 161 | } else { |
cb00ea35 | 162 | struct allocExtDesc *aed = |
28de7948 | 163 | (struct allocExtDesc *)(epos.bh->b_data); |
cb00ea35 | 164 | aed->lengthAllocDescs = |
28de7948 CG |
165 | cpu_to_le32(epos.offset - |
166 | sizeof(struct allocExtDesc)); | |
167 | if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || | |
6c79e987 | 168 | UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) |
ff116fc8 | 169 | udf_update_tag(epos.bh->b_data, epos.offset); |
1da177e4 | 170 | else |
cb00ea35 CG |
171 | udf_update_tag(epos.bh->b_data, |
172 | sizeof(struct allocExtDesc)); | |
ff116fc8 | 173 | mark_buffer_dirty_inode(epos.bh, inode); |
1da177e4 LT |
174 | } |
175 | } | |
74584ae5 JK |
176 | /* This inode entry is in-memory only and thus we don't have to mark |
177 | * the inode dirty */ | |
48d6d8ff | 178 | iinfo->i_lenExtents = lbcount; |
3bf25cb4 | 179 | brelse(epos.bh); |
1da177e4 LT |
180 | } |
181 | ||
456390de | 182 | static void udf_update_alloc_ext_desc(struct inode *inode, |
183 | struct extent_position *epos, | |
184 | u32 lenalloc) | |
185 | { | |
186 | struct super_block *sb = inode->i_sb; | |
187 | struct udf_sb_info *sbi = UDF_SB(sb); | |
188 | ||
189 | struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data); | |
190 | int len = sizeof(struct allocExtDesc); | |
191 | ||
192 | aed->lengthAllocDescs = cpu_to_le32(lenalloc); | |
193 | if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201) | |
194 | len += lenalloc; | |
195 | ||
196 | udf_update_tag(epos->bh->b_data, len); | |
197 | mark_buffer_dirty_inode(epos->bh, inode); | |
198 | } | |
199 | ||
cb00ea35 | 200 | void udf_truncate_extents(struct inode *inode) |
1da177e4 | 201 | { |
ff116fc8 | 202 | struct extent_position epos; |
5ca4e4be | 203 | struct kernel_lb_addr eloc, neloc = {}; |
ff116fc8 | 204 | uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc; |
1da177e4 | 205 | int8_t etype; |
31170b6a JK |
206 | struct super_block *sb = inode->i_sb; |
207 | sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset; | |
60448b1d | 208 | loff_t byte_offset; |
1da177e4 | 209 | int adsize; |
48d6d8ff | 210 | struct udf_inode_info *iinfo = UDF_I(inode); |
1da177e4 | 211 | |
48d6d8ff | 212 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
5ca4e4be | 213 | adsize = sizeof(struct short_ad); |
48d6d8ff | 214 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 215 | adsize = sizeof(struct long_ad); |
1da177e4 | 216 | else |
ff116fc8 | 217 | BUG(); |
1da177e4 | 218 | |
ff116fc8 | 219 | etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset); |
28de7948 CG |
220 | byte_offset = (offset << sb->s_blocksize_bits) + |
221 | (inode->i_size & (sb->s_blocksize - 1)); | |
cb00ea35 | 222 | if (etype != -1) { |
ff116fc8 | 223 | epos.offset -= adsize; |
97e961fd | 224 | extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset); |
ff116fc8 | 225 | epos.offset += adsize; |
60448b1d | 226 | if (byte_offset) |
ff116fc8 | 227 | lenalloc = epos.offset; |
1da177e4 | 228 | else |
ff116fc8 | 229 | lenalloc = epos.offset - adsize; |
1da177e4 | 230 | |
ff116fc8 | 231 | if (!epos.bh) |
1da177e4 LT |
232 | lenalloc -= udf_file_entry_alloc_offset(inode); |
233 | else | |
234 | lenalloc -= sizeof(struct allocExtDesc); | |
235 | ||
4b11111a MS |
236 | while ((etype = udf_current_aext(inode, &epos, &eloc, |
237 | &elen, 0)) != -1) { | |
cb00ea35 | 238 | if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) { |
97e961fd | 239 | udf_write_aext(inode, &epos, &neloc, nelen, 0); |
cb00ea35 | 240 | if (indirect_ext_len) { |
ff116fc8 JK |
241 | /* We managed to free all extents in the |
242 | * indirect extent - free it too */ | |
9de90b76 | 243 | BUG_ON(!epos.bh); |
97e961fd | 244 | udf_free_blocks(sb, inode, &epos.block, |
cb00ea35 | 245 | 0, indirect_ext_len); |
9de90b76 | 246 | } else if (!epos.bh) { |
247 | iinfo->i_lenAlloc = lenalloc; | |
248 | mark_inode_dirty(inode); | |
456390de | 249 | } else |
250 | udf_update_alloc_ext_desc(inode, | |
251 | &epos, lenalloc); | |
ff116fc8 JK |
252 | brelse(epos.bh); |
253 | epos.offset = sizeof(struct allocExtDesc); | |
254 | epos.block = eloc; | |
4b11111a | 255 | epos.bh = udf_tread(sb, |
97e961fd | 256 | udf_get_lb_pblock(sb, &eloc, 0)); |
1da177e4 | 257 | if (elen) |
4b11111a MS |
258 | indirect_ext_len = |
259 | (elen + sb->s_blocksize - 1) >> | |
28de7948 | 260 | sb->s_blocksize_bits; |
1da177e4 | 261 | else |
ff116fc8 | 262 | indirect_ext_len = 1; |
cb00ea35 | 263 | } else { |
97e961fd | 264 | extent_trunc(inode, &epos, &eloc, etype, |
4b11111a | 265 | elen, 0); |
ff116fc8 | 266 | epos.offset += adsize; |
1da177e4 LT |
267 | } |
268 | } | |
269 | ||
cb00ea35 | 270 | if (indirect_ext_len) { |
9de90b76 | 271 | BUG_ON(!epos.bh); |
97e961fd | 272 | udf_free_blocks(sb, inode, &epos.block, 0, |
cb00ea35 | 273 | indirect_ext_len); |
9de90b76 | 274 | } else if (!epos.bh) { |
275 | iinfo->i_lenAlloc = lenalloc; | |
276 | mark_inode_dirty(inode); | |
456390de | 277 | } else |
278 | udf_update_alloc_ext_desc(inode, &epos, lenalloc); | |
cb00ea35 CG |
279 | } else if (inode->i_size) { |
280 | if (byte_offset) { | |
5ca4e4be | 281 | struct kernel_long_ad extent; |
31170b6a | 282 | |
00a2b0f6 JK |
283 | /* |
284 | * OK, there is not extent covering inode->i_size and | |
285 | * no extent above inode->i_size => truncate is | |
31170b6a | 286 | * extending the file by 'offset' blocks. |
00a2b0f6 | 287 | */ |
28de7948 | 288 | if ((!epos.bh && |
4b11111a MS |
289 | epos.offset == |
290 | udf_file_entry_alloc_offset(inode)) || | |
291 | (epos.bh && epos.offset == | |
292 | sizeof(struct allocExtDesc))) { | |
31170b6a JK |
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; | |
4b11111a MS |
297 | extent.extLength = |
298 | EXT_NOT_RECORDED_NOT_ALLOCATED; | |
cb00ea35 | 299 | } else { |
ff116fc8 | 300 | epos.offset -= adsize; |
31170b6a | 301 | etype = udf_next_aext(inode, &epos, |
cb00ea35 CG |
302 | &extent.extLocation, |
303 | &extent.extLength, 0); | |
31170b6a | 304 | extent.extLength |= etype << 30; |
1da177e4 | 305 | } |
cb00ea35 | 306 | udf_extend_file(inode, &epos, &extent, |
4b11111a MS |
307 | offset + |
308 | ((inode->i_size & | |
309 | (sb->s_blocksize - 1)) != 0)); | |
1da177e4 LT |
310 | } |
311 | } | |
48d6d8ff | 312 | iinfo->i_lenExtents = inode->i_size; |
1da177e4 | 313 | |
3bf25cb4 | 314 | brelse(epos.bh); |
1da177e4 | 315 | } |