udf: move headers out include/linux/
[safe/jmp/linux-2.6] / fs / udf / truncate.c
1 /*
2  * truncate.c
3  *
4  * PURPOSE
5  *      Truncate handling routines for the OSTA-UDF(tm) filesystem.
6  *
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>
25 #include <linux/buffer_head.h>
26
27 #include "udf_i.h"
28 #include "udf_sb.h"
29
30 static void extent_trunc(struct inode *inode, struct extent_position *epos,
31                          kernel_lb_addr eloc, int8_t etype, uint32_t elen,
32                          uint32_t nelen)
33 {
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;
39
40         if (nelen) {
41                 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
42                         udf_free_blocks(inode->i_sb, inode, eloc, 0,
43                                         last_block);
44                         etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
45                 } else
46                         neloc = eloc;
47                 nelen = (etype << 30) | nelen;
48         }
49
50         if (elen != 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);
55
56                         if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
57                                 udf_free_blocks(inode->i_sb, inode, eloc,
58                                                 first_block,
59                                                 last_block - first_block);
60                 }
61         }
62 }
63
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)
69 {
70         struct extent_position epos = {};
71         kernel_lb_addr eloc;
72         uint32_t elen, nelen;
73         uint64_t lbcount = 0;
74         int8_t etype = -1, netype;
75         int adsize;
76         struct udf_inode_info *iinfo = UDF_I(inode);
77
78         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
79             inode->i_size == iinfo->i_lenExtents)
80                 return;
81         /* Are we going to delete the file anyway? */
82         if (inode->i_nlink == 0)
83                 return;
84
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);
89         else
90                 BUG();
91
92         /* Find the last extent in the file */
93         while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
94                 etype = netype;
95                 lbcount += elen;
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);
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);
115                         break;
116                 }
117         }
118         /* This inode entry is in-memory only and thus we don't have to mark
119          * the inode dirty */
120         iinfo->i_lenExtents = inode->i_size;
121         brelse(epos.bh);
122 }
123
124 void udf_discard_prealloc(struct inode *inode)
125 {
126         struct extent_position epos = { NULL, 0, {0, 0} };
127         kernel_lb_addr eloc;
128         uint32_t elen;
129         uint64_t lbcount = 0;
130         int8_t etype = -1, netype;
131         int adsize;
132         struct udf_inode_info *iinfo = UDF_I(inode);
133
134         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
135             inode->i_size == iinfo->i_lenExtents)
136                 return;
137
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);
142         else
143                 adsize = 0;
144
145         epos.block = iinfo->i_location;
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         }
152         if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
153                 epos.offset -= adsize;
154                 lbcount -= elen;
155                 extent_trunc(inode, &epos, eloc, etype, elen, 0);
156                 if (!epos.bh) {
157                         iinfo->i_lenAlloc =
158                                 epos.offset -
159                                 udf_file_entry_alloc_offset(inode);
160                         mark_inode_dirty(inode);
161                 } else {
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);
170                         else
171                                 udf_update_tag(epos.bh->b_data,
172                                                sizeof(struct allocExtDesc));
173                         mark_buffer_dirty_inode(epos.bh, inode);
174                 }
175         }
176         /* This inode entry is in-memory only and thus we don't have to mark
177          * the inode dirty */
178         iinfo->i_lenExtents = lbcount;
179         brelse(epos.bh);
180 }
181
182 void udf_truncate_extents(struct inode *inode)
183 {
184         struct extent_position epos;
185         kernel_lb_addr eloc, neloc = {};
186         uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
187         int8_t etype;
188         struct super_block *sb = inode->i_sb;
189         struct udf_sb_info *sbi = UDF_SB(sb);
190         sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
191         loff_t byte_offset;
192         int adsize;
193         struct udf_inode_info *iinfo = UDF_I(inode);
194
195         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
196                 adsize = sizeof(short_ad);
197         else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
198                 adsize = sizeof(long_ad);
199         else
200                 BUG();
201
202         etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
203         byte_offset = (offset << sb->s_blocksize_bits) +
204                 (inode->i_size & (sb->s_blocksize - 1));
205         if (etype != -1) {
206                 epos.offset -= adsize;
207                 extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
208                 epos.offset += adsize;
209                 if (byte_offset)
210                         lenalloc = epos.offset;
211                 else
212                         lenalloc = epos.offset - adsize;
213
214                 if (!epos.bh)
215                         lenalloc -= udf_file_entry_alloc_offset(inode);
216                 else
217                         lenalloc -= sizeof(struct allocExtDesc);
218
219                 while ((etype = udf_current_aext(inode, &epos, &eloc,
220                                                  &elen, 0)) != -1) {
221                         if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
222                                 udf_write_aext(inode, &epos, neloc, nelen, 0);
223                                 if (indirect_ext_len) {
224                                         /* We managed to free all extents in the
225                                          * indirect extent - free it too */
226                                         if (!epos.bh)
227                                                 BUG();
228                                         udf_free_blocks(sb, inode, epos.block,
229                                                         0, indirect_ext_len);
230                                 } else {
231                                         if (!epos.bh) {
232                                                 iinfo->i_lenAlloc =
233                                                                 lenalloc;
234                                                 mark_inode_dirty(inode);
235                                         } else {
236                                                 struct allocExtDesc *aed =
237                                                         (struct allocExtDesc *)
238                                                         (epos.bh->b_data);
239                                                 int len =
240                                                     sizeof(struct allocExtDesc);
241
242                                                 aed->lengthAllocDescs =
243                                                     cpu_to_le32(lenalloc);
244                                                 if (!UDF_QUERY_FLAG(sb,
245                                                         UDF_FLAG_STRICT) ||
246                                                     sbi->s_udfrev >= 0x0201)
247                                                         len += lenalloc;
248
249                                                 udf_update_tag(epos.bh->b_data,
250                                                                 len);
251                                                 mark_buffer_dirty_inode(
252                                                                 epos.bh, inode);
253                                         }
254                                 }
255                                 brelse(epos.bh);
256                                 epos.offset = sizeof(struct allocExtDesc);
257                                 epos.block = eloc;
258                                 epos.bh = udf_tread(sb,
259                                                 udf_get_lb_pblock(sb, eloc, 0));
260                                 if (elen)
261                                         indirect_ext_len =
262                                                 (elen + sb->s_blocksize - 1) >>
263                                                 sb->s_blocksize_bits;
264                                 else
265                                         indirect_ext_len = 1;
266                         } else {
267                                 extent_trunc(inode, &epos, eloc, etype,
268                                              elen, 0);
269                                 epos.offset += adsize;
270                         }
271                 }
272
273                 if (indirect_ext_len) {
274                         if (!epos.bh)
275                                 BUG();
276                         udf_free_blocks(sb, inode, epos.block, 0,
277                                         indirect_ext_len);
278                 } else {
279                         if (!epos.bh) {
280                                 iinfo->i_lenAlloc = lenalloc;
281                                 mark_inode_dirty(inode);
282                         } else {
283                                 struct allocExtDesc *aed =
284                                     (struct allocExtDesc *)(epos.bh->b_data);
285                                 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
286                                 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) ||
287                                     sbi->s_udfrev >= 0x0201)
288                                         udf_update_tag(epos.bh->b_data,
289                                                 lenalloc +
290                                                 sizeof(struct allocExtDesc));
291                                 else
292                                         udf_update_tag(epos.bh->b_data,
293                                                 sizeof(struct allocExtDesc));
294                                 mark_buffer_dirty_inode(epos.bh, inode);
295                         }
296                 }
297         } else if (inode->i_size) {
298                 if (byte_offset) {
299                         kernel_long_ad extent;
300
301                         /*
302                          *  OK, there is not extent covering inode->i_size and
303                          *  no extent above inode->i_size => truncate is
304                          *  extending the file by 'offset' blocks.
305                          */
306                         if ((!epos.bh &&
307                              epos.offset ==
308                                         udf_file_entry_alloc_offset(inode)) ||
309                             (epos.bh && epos.offset ==
310                                                 sizeof(struct allocExtDesc))) {
311                                 /* File has no extents at all or has empty last
312                                  * indirect extent! Create a fake extent... */
313                                 extent.extLocation.logicalBlockNum = 0;
314                                 extent.extLocation.partitionReferenceNum = 0;
315                                 extent.extLength =
316                                         EXT_NOT_RECORDED_NOT_ALLOCATED;
317                         } else {
318                                 epos.offset -= adsize;
319                                 etype = udf_next_aext(inode, &epos,
320                                                       &extent.extLocation,
321                                                       &extent.extLength, 0);
322                                 extent.extLength |= etype << 30;
323                         }
324                         udf_extend_file(inode, &epos, &extent,
325                                         offset +
326                                         ((inode->i_size &
327                                                 (sb->s_blocksize - 1)) != 0));
328                 }
329         }
330         iinfo->i_lenExtents = inode->i_size;
331
332         brelse(epos.bh);
333 }