Linux v2.6.13
[linux-2.6/next.git] / fs / udf / truncate.c
blob7dc8a5572ca1900f5ee94db2c9a3695c88a2f909
1 /*
2 * truncate.c
4 * PURPOSE
5 * Truncate handling routines for the OSTA-UDF(tm) filesystem.
7 * CONTACTS
8 * E-mail regarding any portion of the Linux UDF file system should be
9 * directed to the development team mailing list (run by majordomo):
10 * linux_udf@hpesjro.fc.hp.com
12 * COPYRIGHT
13 * This file is distributed under the terms of the GNU General Public
14 * License (GPL). Copies of the GPL can be obtained from:
15 * ftp://prep.ai.mit.edu/pub/gnu/GPL
16 * Each contributing author retains all rights to their own work.
18 * (C) 1999-2004 Ben Fennema
19 * (C) 1999 Stelias Computing Inc
21 * HISTORY
23 * 02/24/99 blf Created.
27 #include "udfdecl.h"
28 #include <linux/fs.h>
29 #include <linux/mm.h>
30 #include <linux/udf_fs.h>
31 #include <linux/buffer_head.h>
33 #include "udf_i.h"
34 #include "udf_sb.h"
36 static void extent_trunc(struct inode * inode, kernel_lb_addr bloc, int extoffset,
37 kernel_lb_addr eloc, int8_t etype, uint32_t elen, struct buffer_head *bh, uint32_t nelen)
39 kernel_lb_addr neloc = { 0, 0 };
40 int last_block = (elen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
41 int first_block = (nelen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
43 if (nelen)
45 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
47 udf_free_blocks(inode->i_sb, inode, eloc, 0, last_block);
48 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
50 else
51 neloc = eloc;
52 nelen = (etype << 30) | nelen;
55 if (elen != nelen)
57 udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 0);
58 if (last_block - first_block > 0)
60 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
61 mark_inode_dirty(inode);
63 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
64 udf_free_blocks(inode->i_sb, inode, eloc, first_block, last_block - first_block);
69 void udf_discard_prealloc(struct inode * inode)
71 kernel_lb_addr bloc, eloc;
72 uint32_t extoffset = 0, elen, nelen;
73 uint64_t lbcount = 0;
74 int8_t etype = -1, netype;
75 struct buffer_head *bh = NULL;
76 int adsize;
78 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
79 inode->i_size == UDF_I_LENEXTENTS(inode))
81 return;
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);
88 else
89 adsize = 0;
91 bloc = UDF_I_LOCATION(inode);
93 while ((netype = udf_next_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1)
95 etype = netype;
96 lbcount += elen;
97 if (lbcount > inode->i_size && lbcount - inode->i_size < inode->i_sb->s_blocksize)
99 nelen = elen - (lbcount - inode->i_size);
100 extent_trunc(inode, bloc, extoffset-adsize, eloc, etype, elen, bh, nelen);
101 lbcount = inode->i_size;
104 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
106 extoffset -= adsize;
107 lbcount -= elen;
108 extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, 0);
109 if (!bh)
111 UDF_I_LENALLOC(inode) = extoffset - udf_file_entry_alloc_offset(inode);
112 mark_inode_dirty(inode);
114 else
116 struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data);
117 aed->lengthAllocDescs = cpu_to_le32(extoffset - sizeof(struct allocExtDesc));
118 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
119 udf_update_tag(bh->b_data, extoffset);
120 else
121 udf_update_tag(bh->b_data, sizeof(struct allocExtDesc));
122 mark_buffer_dirty_inode(bh, inode);
125 UDF_I_LENEXTENTS(inode) = lbcount;
127 udf_release_data(bh);
130 void udf_truncate_extents(struct inode * inode)
132 kernel_lb_addr bloc, eloc, neloc = { 0, 0 };
133 uint32_t extoffset, elen, offset, nelen = 0, lelen = 0, lenalloc;
134 int8_t etype;
135 int first_block = inode->i_size >> inode->i_sb->s_blocksize_bits;
136 struct buffer_head *bh = NULL;
137 int adsize;
139 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
140 adsize = sizeof(short_ad);
141 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
142 adsize = sizeof(long_ad);
143 else
144 adsize = 0;
146 etype = inode_bmap(inode, first_block, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
147 offset += (inode->i_size & (inode->i_sb->s_blocksize - 1));
148 if (etype != -1)
150 extoffset -= adsize;
151 extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, offset);
152 extoffset += adsize;
154 if (offset)
155 lenalloc = extoffset;
156 else
157 lenalloc = extoffset - adsize;
159 if (!bh)
160 lenalloc -= udf_file_entry_alloc_offset(inode);
161 else
162 lenalloc -= sizeof(struct allocExtDesc);
164 while ((etype = udf_current_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 0)) != -1)
166 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30))
168 udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 0);
169 extoffset = 0;
170 if (lelen)
172 if (!bh)
173 BUG();
174 else
175 memset(bh->b_data, 0x00, sizeof(struct allocExtDesc));
176 udf_free_blocks(inode->i_sb, inode, bloc, 0, lelen);
178 else
180 if (!bh)
182 UDF_I_LENALLOC(inode) = lenalloc;
183 mark_inode_dirty(inode);
185 else
187 struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data);
188 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
189 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
190 udf_update_tag(bh->b_data, lenalloc +
191 sizeof(struct allocExtDesc));
192 else
193 udf_update_tag(bh->b_data, sizeof(struct allocExtDesc));
194 mark_buffer_dirty_inode(bh, inode);
198 udf_release_data(bh);
199 extoffset = sizeof(struct allocExtDesc);
200 bloc = eloc;
201 bh = udf_tread(inode->i_sb, udf_get_lb_pblock(inode->i_sb, bloc, 0));
202 if (elen)
203 lelen = (elen + inode->i_sb->s_blocksize - 1) >>
204 inode->i_sb->s_blocksize_bits;
205 else
206 lelen = 1;
208 else
210 extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, 0);
211 extoffset += adsize;
215 if (lelen)
217 if (!bh)
218 BUG();
219 else
220 memset(bh->b_data, 0x00, sizeof(struct allocExtDesc));
221 udf_free_blocks(inode->i_sb, inode, bloc, 0, lelen);
223 else
225 if (!bh)
227 UDF_I_LENALLOC(inode) = lenalloc;
228 mark_inode_dirty(inode);
230 else
232 struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data);
233 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
234 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
235 udf_update_tag(bh->b_data, lenalloc +
236 sizeof(struct allocExtDesc));
237 else
238 udf_update_tag(bh->b_data, sizeof(struct allocExtDesc));
239 mark_buffer_dirty_inode(bh, inode);
243 else if (inode->i_size)
245 if (offset)
247 extoffset -= adsize;
248 etype = udf_next_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 1);
249 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
251 extoffset -= adsize;
252 elen = EXT_NOT_RECORDED_NOT_ALLOCATED | (elen + offset);
253 udf_write_aext(inode, bloc, &extoffset, eloc, elen, bh, 0);
255 else if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
257 kernel_lb_addr neloc = { 0, 0 };
258 extoffset -= adsize;
259 nelen = EXT_NOT_RECORDED_NOT_ALLOCATED |
260 ((elen + offset + inode->i_sb->s_blocksize - 1) &
261 ~(inode->i_sb->s_blocksize - 1));
262 udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 1);
263 udf_add_aext(inode, &bloc, &extoffset, eloc, (etype << 30) | elen, &bh, 1);
265 else
267 if (elen & (inode->i_sb->s_blocksize - 1))
269 extoffset -= adsize;
270 elen = EXT_RECORDED_ALLOCATED |
271 ((elen + inode->i_sb->s_blocksize - 1) &
272 ~(inode->i_sb->s_blocksize - 1));
273 udf_write_aext(inode, bloc, &extoffset, eloc, elen, bh, 1);
275 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
276 elen = EXT_NOT_RECORDED_NOT_ALLOCATED | offset;
277 udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &bh, 1);
281 UDF_I_LENEXTENTS(inode) = inode->i_size;
283 udf_release_data(bh);