aboutsummaryrefslogtreecommitdiffstats
path: root/fs/udf/truncate.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/udf/truncate.c')
-rw-r--r--fs/udf/truncate.c206
1 files changed, 91 insertions, 115 deletions
diff --git a/fs/udf/truncate.c b/fs/udf/truncate.c
index 0abd66ce36ea..77975ae291a5 100644
--- a/fs/udf/truncate.c
+++ b/fs/udf/truncate.c
@@ -28,8 +28,8 @@
28#include "udf_i.h" 28#include "udf_i.h"
29#include "udf_sb.h" 29#include "udf_sb.h"
30 30
31static void extent_trunc(struct inode * inode, kernel_lb_addr bloc, int extoffset, 31static void extent_trunc(struct inode * inode, struct extent_position *epos,
32 kernel_lb_addr eloc, int8_t etype, uint32_t elen, struct buffer_head *bh, uint32_t nelen) 32 kernel_lb_addr eloc, int8_t etype, uint32_t elen, uint32_t nelen)
33{ 33{
34 kernel_lb_addr neloc = { 0, 0 }; 34 kernel_lb_addr neloc = { 0, 0 };
35 int last_block = (elen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits; 35 int last_block = (elen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
@@ -49,7 +49,7 @@ static void extent_trunc(struct inode * inode, kernel_lb_addr bloc, int extoffse
49 49
50 if (elen != nelen) 50 if (elen != nelen)
51 { 51 {
52 udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 0); 52 udf_write_aext(inode, epos, neloc, nelen, 0);
53 if (last_block - first_block > 0) 53 if (last_block - first_block > 0)
54 { 54 {
55 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) 55 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
@@ -63,18 +63,16 @@ static void extent_trunc(struct inode * inode, kernel_lb_addr bloc, int extoffse
63 63
64void udf_discard_prealloc(struct inode * inode) 64void udf_discard_prealloc(struct inode * inode)
65{ 65{
66 kernel_lb_addr bloc, eloc; 66 struct extent_position epos = { NULL, 0, {0, 0}};
67 uint32_t extoffset = 0, elen, nelen; 67 kernel_lb_addr eloc;
68 uint32_t elen, nelen;
68 uint64_t lbcount = 0; 69 uint64_t lbcount = 0;
69 int8_t etype = -1, netype; 70 int8_t etype = -1, netype;
70 struct buffer_head *bh = NULL;
71 int adsize; 71 int adsize;
72 72
73 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB || 73 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
74 inode->i_size == UDF_I_LENEXTENTS(inode)) 74 inode->i_size == UDF_I_LENEXTENTS(inode))
75 {
76 return; 75 return;
77 }
78 76
79 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT) 77 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
80 adsize = sizeof(short_ad); 78 adsize = sizeof(short_ad);
@@ -83,52 +81,58 @@ void udf_discard_prealloc(struct inode * inode)
83 else 81 else
84 adsize = 0; 82 adsize = 0;
85 83
86 bloc = UDF_I_LOCATION(inode); 84 epos.block = UDF_I_LOCATION(inode);
87 85
88 while ((netype = udf_next_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1) 86 /* Find the last extent in the file */
87 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1)
89 { 88 {
90 etype = netype; 89 etype = netype;
91 lbcount += elen; 90 lbcount += elen;
92 if (lbcount > inode->i_size && lbcount - inode->i_size < inode->i_sb->s_blocksize) 91 if (lbcount > inode->i_size && lbcount - elen < inode->i_size)
93 { 92 {
93 WARN_ON(lbcount - inode->i_size >= inode->i_sb->s_blocksize);
94 nelen = elen - (lbcount - inode->i_size); 94 nelen = elen - (lbcount - inode->i_size);
95 extent_trunc(inode, bloc, extoffset-adsize, eloc, etype, elen, bh, nelen); 95 epos.offset -= adsize;
96 extent_trunc(inode, &epos, eloc, etype, elen, nelen);
97 epos.offset += adsize;
96 lbcount = inode->i_size; 98 lbcount = inode->i_size;
97 } 99 }
98 } 100 }
99 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) 101 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
100 { 102 epos.offset -= adsize;
101 extoffset -= adsize;
102 lbcount -= elen; 103 lbcount -= elen;
103 extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, 0); 104 extent_trunc(inode, &epos, eloc, etype, elen, 0);
104 if (!bh) 105 if (!epos.bh)
105 { 106 {
106 UDF_I_LENALLOC(inode) = extoffset - udf_file_entry_alloc_offset(inode); 107 UDF_I_LENALLOC(inode) = epos.offset - udf_file_entry_alloc_offset(inode);
107 mark_inode_dirty(inode); 108 mark_inode_dirty(inode);
108 } 109 }
109 else 110 else
110 { 111 {
111 struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data); 112 struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
112 aed->lengthAllocDescs = cpu_to_le32(extoffset - sizeof(struct allocExtDesc)); 113 aed->lengthAllocDescs = cpu_to_le32(epos.offset - sizeof(struct allocExtDesc));
113 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201) 114 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
114 udf_update_tag(bh->b_data, extoffset); 115 udf_update_tag(epos.bh->b_data, epos.offset);
115 else 116 else
116 udf_update_tag(bh->b_data, sizeof(struct allocExtDesc)); 117 udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
117 mark_buffer_dirty_inode(bh, inode); 118 mark_buffer_dirty_inode(epos.bh, inode);
118 } 119 }
119 } 120 }
120 UDF_I_LENEXTENTS(inode) = lbcount; 121 UDF_I_LENEXTENTS(inode) = lbcount;
121 122
122 udf_release_data(bh); 123 WARN_ON(lbcount != inode->i_size);
124 brelse(epos.bh);
123} 125}
124 126
125void udf_truncate_extents(struct inode * inode) 127void udf_truncate_extents(struct inode * inode)
126{ 128{
127 kernel_lb_addr bloc, eloc, neloc = { 0, 0 }; 129 struct extent_position epos;
128 uint32_t extoffset, elen, offset, nelen = 0, lelen = 0, lenalloc; 130 kernel_lb_addr eloc, neloc = { 0, 0 };
131 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
129 int8_t etype; 132 int8_t etype;
130 int first_block = inode->i_size >> inode->i_sb->s_blocksize_bits; 133 struct super_block *sb = inode->i_sb;
131 struct buffer_head *bh = NULL; 134 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
135 loff_t byte_offset;
132 int adsize; 136 int adsize;
133 137
134 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT) 138 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
@@ -136,158 +140,130 @@ void udf_truncate_extents(struct inode * inode)
136 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG) 140 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
137 adsize = sizeof(long_ad); 141 adsize = sizeof(long_ad);
138 else 142 else
139 adsize = 0; 143 BUG();
140 144
141 etype = inode_bmap(inode, first_block, &bloc, &extoffset, &eloc, &elen, &offset, &bh); 145 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
142 offset += (inode->i_size & (inode->i_sb->s_blocksize - 1)); 146 byte_offset = (offset << sb->s_blocksize_bits) + (inode->i_size & (sb->s_blocksize-1));
143 if (etype != -1) 147 if (etype != -1)
144 { 148 {
145 extoffset -= adsize; 149 epos.offset -= adsize;
146 extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, offset); 150 extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
147 extoffset += adsize; 151 epos.offset += adsize;
148 152 if (byte_offset)
149 if (offset) 153 lenalloc = epos.offset;
150 lenalloc = extoffset;
151 else 154 else
152 lenalloc = extoffset - adsize; 155 lenalloc = epos.offset - adsize;
153 156
154 if (!bh) 157 if (!epos.bh)
155 lenalloc -= udf_file_entry_alloc_offset(inode); 158 lenalloc -= udf_file_entry_alloc_offset(inode);
156 else 159 else
157 lenalloc -= sizeof(struct allocExtDesc); 160 lenalloc -= sizeof(struct allocExtDesc);
158 161
159 while ((etype = udf_current_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 0)) != -1) 162 while ((etype = udf_current_aext(inode, &epos, &eloc, &elen, 0)) != -1)
160 { 163 {
161 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) 164 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30))
162 { 165 {
163 udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 0); 166 udf_write_aext(inode, &epos, neloc, nelen, 0);
164 extoffset = 0; 167 if (indirect_ext_len)
165 if (lelen)
166 { 168 {
167 if (!bh) 169 /* We managed to free all extents in the
170 * indirect extent - free it too */
171 if (!epos.bh)
168 BUG(); 172 BUG();
169 else 173 udf_free_blocks(sb, inode, epos.block, 0, indirect_ext_len);
170 memset(bh->b_data, 0x00, sizeof(struct allocExtDesc));
171 udf_free_blocks(inode->i_sb, inode, bloc, 0, lelen);
172 } 174 }
173 else 175 else
174 { 176 {
175 if (!bh) 177 if (!epos.bh)
176 { 178 {
177 UDF_I_LENALLOC(inode) = lenalloc; 179 UDF_I_LENALLOC(inode) = lenalloc;
178 mark_inode_dirty(inode); 180 mark_inode_dirty(inode);
179 } 181 }
180 else 182 else
181 { 183 {
182 struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data); 184 struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
183 aed->lengthAllocDescs = cpu_to_le32(lenalloc); 185 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
184 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201) 186 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(sb) >= 0x0201)
185 udf_update_tag(bh->b_data, lenalloc + 187 udf_update_tag(epos.bh->b_data, lenalloc +
186 sizeof(struct allocExtDesc)); 188 sizeof(struct allocExtDesc));
187 else 189 else
188 udf_update_tag(bh->b_data, sizeof(struct allocExtDesc)); 190 udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
189 mark_buffer_dirty_inode(bh, inode); 191 mark_buffer_dirty_inode(epos.bh, inode);
190 } 192 }
191 } 193 }
192 194 brelse(epos.bh);
193 udf_release_data(bh); 195 epos.offset = sizeof(struct allocExtDesc);
194 extoffset = sizeof(struct allocExtDesc); 196 epos.block = eloc;
195 bloc = eloc; 197 epos.bh = udf_tread(sb, udf_get_lb_pblock(sb, eloc, 0));
196 bh = udf_tread(inode->i_sb, udf_get_lb_pblock(inode->i_sb, bloc, 0));
197 if (elen) 198 if (elen)
198 lelen = (elen + inode->i_sb->s_blocksize - 1) >> 199 indirect_ext_len = (elen +
199 inode->i_sb->s_blocksize_bits; 200 sb->s_blocksize - 1) >>
201 sb->s_blocksize_bits;
200 else 202 else
201 lelen = 1; 203 indirect_ext_len = 1;
202 } 204 }
203 else 205 else
204 { 206 {
205 extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, 0); 207 extent_trunc(inode, &epos, eloc, etype, elen, 0);
206 extoffset += adsize; 208 epos.offset += adsize;
207 } 209 }
208 } 210 }
209 211
210 if (lelen) 212 if (indirect_ext_len)
211 { 213 {
212 if (!bh) 214 if (!epos.bh)
213 BUG(); 215 BUG();
214 else 216 udf_free_blocks(sb, inode, epos.block, 0, indirect_ext_len);
215 memset(bh->b_data, 0x00, sizeof(struct allocExtDesc));
216 udf_free_blocks(inode->i_sb, inode, bloc, 0, lelen);
217 } 217 }
218 else 218 else
219 { 219 {
220 if (!bh) 220 if (!epos.bh)
221 { 221 {
222 UDF_I_LENALLOC(inode) = lenalloc; 222 UDF_I_LENALLOC(inode) = lenalloc;
223 mark_inode_dirty(inode); 223 mark_inode_dirty(inode);
224 } 224 }
225 else 225 else
226 { 226 {
227 struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data); 227 struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
228 aed->lengthAllocDescs = cpu_to_le32(lenalloc); 228 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
229 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201) 229 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(sb) >= 0x0201)
230 udf_update_tag(bh->b_data, lenalloc + 230 udf_update_tag(epos.bh->b_data, lenalloc +
231 sizeof(struct allocExtDesc)); 231 sizeof(struct allocExtDesc));
232 else 232 else
233 udf_update_tag(bh->b_data, sizeof(struct allocExtDesc)); 233 udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
234 mark_buffer_dirty_inode(bh, inode); 234 mark_buffer_dirty_inode(epos.bh, inode);
235 } 235 }
236 } 236 }
237 } 237 }
238 else if (inode->i_size) 238 else if (inode->i_size)
239 { 239 {
240 if (offset) 240 if (byte_offset)
241 { 241 {
242 kernel_long_ad extent;
243
242 /* 244 /*
243 * OK, there is not extent covering inode->i_size and 245 * OK, there is not extent covering inode->i_size and
244 * no extent above inode->i_size => truncate is 246 * no extent above inode->i_size => truncate is
245 * extending the file by 'offset'. 247 * extending the file by 'offset' blocks.
246 */ 248 */
247 if ((!bh && extoffset == udf_file_entry_alloc_offset(inode)) || 249 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
248 (bh && extoffset == sizeof(struct allocExtDesc))) { 250 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
249 /* File has no extents at all! */ 251 /* File has no extents at all or has empty last
250 memset(&eloc, 0x00, sizeof(kernel_lb_addr)); 252 * indirect extent! Create a fake extent... */
251 elen = EXT_NOT_RECORDED_NOT_ALLOCATED | offset; 253 extent.extLocation.logicalBlockNum = 0;
252 udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &bh, 1); 254 extent.extLocation.partitionReferenceNum = 0;
255 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
253 } 256 }
254 else { 257 else {
255 extoffset -= adsize; 258 epos.offset -= adsize;
256 etype = udf_next_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 1); 259 etype = udf_next_aext(inode, &epos,
257 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) 260 &extent.extLocation, &extent.extLength, 0);
258 { 261 extent.extLength |= etype << 30;
259 extoffset -= adsize;
260 elen = EXT_NOT_RECORDED_NOT_ALLOCATED | (elen + offset);
261 udf_write_aext(inode, bloc, &extoffset, eloc, elen, bh, 0);
262 }
263 else if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
264 {
265 kernel_lb_addr neloc = { 0, 0 };
266 extoffset -= adsize;
267 nelen = EXT_NOT_RECORDED_NOT_ALLOCATED |
268 ((elen + offset + inode->i_sb->s_blocksize - 1) &
269 ~(inode->i_sb->s_blocksize - 1));
270 udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 1);
271 udf_add_aext(inode, &bloc, &extoffset, eloc, (etype << 30) | elen, &bh, 1);
272 }
273 else
274 {
275 if (elen & (inode->i_sb->s_blocksize - 1))
276 {
277 extoffset -= adsize;
278 elen = EXT_RECORDED_ALLOCATED |
279 ((elen + inode->i_sb->s_blocksize - 1) &
280 ~(inode->i_sb->s_blocksize - 1));
281 udf_write_aext(inode, bloc, &extoffset, eloc, elen, bh, 1);
282 }
283 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
284 elen = EXT_NOT_RECORDED_NOT_ALLOCATED | offset;
285 udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &bh, 1);
286 }
287 } 262 }
263 udf_extend_file(inode, &epos, &extent, offset+((inode->i_size & (sb->s_blocksize-1)) != 0));
288 } 264 }
289 } 265 }
290 UDF_I_LENEXTENTS(inode) = inode->i_size; 266 UDF_I_LENEXTENTS(inode) = inode->i_size;
291 267
292 udf_release_data(bh); 268 brelse(epos.bh);
293} 269}