Pull ia64-clocksource into release branch
[pandora-kernel.git] / 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/udf_fs.h>
26 #include <linux/buffer_head.h>
27
28 #include "udf_i.h"
29 #include "udf_sb.h"
30
31 static void extent_trunc(struct inode *inode, struct extent_position *epos,
32                          kernel_lb_addr eloc, int8_t etype, uint32_t elen,
33                          uint32_t nelen)
34 {
35         kernel_lb_addr neloc = { 0, 0 };
36         int last_block =
37             (elen + inode->i_sb->s_blocksize -
38              1) >> inode->i_sb->s_blocksize_bits;
39         int first_block =
40             (nelen + inode->i_sb->s_blocksize -
41              1) >> inode->i_sb->s_blocksize_bits;
42
43         if (nelen) {
44                 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
45                         udf_free_blocks(inode->i_sb, inode, eloc, 0,
46                                         last_block);
47                         etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
48                 } else
49                         neloc = eloc;
50                 nelen = (etype << 30) | nelen;
51         }
52
53         if (elen != nelen) {
54                 udf_write_aext(inode, epos, neloc, nelen, 0);
55                 if (last_block - first_block > 0) {
56                         if (etype == (EXT_RECORDED_ALLOCATED >> 30))
57                                 mark_inode_dirty(inode);
58
59                         if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
60                                 udf_free_blocks(inode->i_sb, inode, eloc,
61                                                 first_block,
62                                                 last_block - first_block);
63                 }
64         }
65 }
66
67 /*
68  * Truncate the last extent to match i_size. This function assumes
69  * that preallocation extent is already truncated.
70  */
71 void udf_truncate_tail_extent(struct inode *inode)
72 {
73         struct extent_position epos = { NULL, 0, {0, 0} };
74         kernel_lb_addr eloc;
75         uint32_t elen, nelen;
76         uint64_t lbcount = 0;
77         int8_t etype = -1, netype;
78         int adsize;
79
80         if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
81             inode->i_size == UDF_I_LENEXTENTS(inode))
82                 return;
83         /* Are we going to delete the file anyway? */
84         if (inode->i_nlink == 0)
85                 return;
86
87         if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
88                 adsize = sizeof(short_ad);
89         else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
90                 adsize = sizeof(long_ad);
91         else
92                 BUG();
93
94         /* Find the last extent in the file */
95         while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
96                 etype = netype;
97                 lbcount += elen;
98                 if (lbcount > inode->i_size) {
99                         if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
100                                 printk(KERN_WARNING
101                                        "udf_truncate_tail_extent(): Too long "
102                                        "extent after EOF in inode %u: i_size: "
103                                        "%Ld lbcount: %Ld extent %u+%u\n",
104                                        (unsigned)inode->i_ino,
105                                        (long long)inode->i_size,
106                                        (long long)lbcount,
107                                        (unsigned)eloc.logicalBlockNum,
108                                        (unsigned)elen);
109                         nelen = elen - (lbcount - inode->i_size);
110                         epos.offset -= adsize;
111                         extent_trunc(inode, &epos, eloc, etype, elen, nelen);
112                         epos.offset += adsize;
113                         if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
114                                 printk(KERN_ERR "udf_truncate_tail_extent(): "
115                                        "Extent after EOF in inode %u.\n",
116                                        (unsigned)inode->i_ino);
117                         break;
118                 }
119         }
120         /* This inode entry is in-memory only and thus we don't have to mark
121          * the inode dirty */
122         UDF_I_LENEXTENTS(inode) = inode->i_size;
123         brelse(epos.bh);
124 }
125
126 void udf_discard_prealloc(struct inode *inode)
127 {
128         struct extent_position epos = { NULL, 0, {0, 0} };
129         kernel_lb_addr eloc;
130         uint32_t elen;
131         uint64_t lbcount = 0;
132         int8_t etype = -1, netype;
133         int adsize;
134
135         if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
136             inode->i_size == UDF_I_LENEXTENTS(inode))
137                 return;
138
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;
145
146         epos.block = UDF_I_LOCATION(inode);
147
148         /* Find the last extent in the file */
149         while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
150                 etype = netype;
151                 lbcount += elen;
152         }
153         if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
154                 epos.offset -= adsize;
155                 lbcount -= elen;
156                 extent_trunc(inode, &epos, eloc, etype, elen, 0);
157                 if (!epos.bh) {
158                         UDF_I_LENALLOC(inode) =
159                             epos.offset - 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_UDFREV(inode->i_sb) >= 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         UDF_I_LENEXTENTS(inode) = 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 = { 0, 0 };
186         uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
187         int8_t etype;
188         struct super_block *sb = inode->i_sb;
189         sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
190         loff_t byte_offset;
191         int adsize;
192
193         if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
194                 adsize = sizeof(short_ad);
195         else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
196                 adsize = sizeof(long_ad);
197         else
198                 BUG();
199
200         etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
201         byte_offset =
202             (offset << sb->s_blocksize_bits) +
203             (inode->i_size & (sb->s_blocksize - 1));
204         if (etype != -1) {
205                 epos.offset -= adsize;
206                 extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
207                 epos.offset += adsize;
208                 if (byte_offset)
209                         lenalloc = epos.offset;
210                 else
211                         lenalloc = epos.offset - adsize;
212
213                 if (!epos.bh)
214                         lenalloc -= udf_file_entry_alloc_offset(inode);
215                 else
216                         lenalloc -= sizeof(struct allocExtDesc);
217
218                 while ((etype =
219                         udf_current_aext(inode, &epos, &eloc, &elen,
220                                          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                                                 UDF_I_LENALLOC(inode) =
233                                                     lenalloc;
234                                                 mark_inode_dirty(inode);
235                                         } else {
236                                                 struct allocExtDesc *aed =
237                                                     (struct allocExtDesc
238                                                      *)(epos.bh->b_data);
239                                                 aed->lengthAllocDescs =
240                                                     cpu_to_le32(lenalloc);
241                                                 if (!UDF_QUERY_FLAG
242                                                     (sb, UDF_FLAG_STRICT)
243                                                     || UDF_SB_UDFREV(sb) >=
244                                                     0x0201)
245                                                         udf_update_tag(epos.bh->
246                                                                        b_data,
247                                                                        lenalloc
248                                                                        +
249                                                                        sizeof
250                                                                        (struct
251                                                                         allocExtDesc));
252                                                 else
253                                                         udf_update_tag(epos.bh->
254                                                                        b_data,
255                                                                        sizeof
256                                                                        (struct
257                                                                         allocExtDesc));
258                                                 mark_buffer_dirty_inode(epos.bh,
259                                                                         inode);
260                                         }
261                                 }
262                                 brelse(epos.bh);
263                                 epos.offset = sizeof(struct allocExtDesc);
264                                 epos.block = eloc;
265                                 epos.bh =
266                                     udf_tread(sb,
267                                               udf_get_lb_pblock(sb, eloc, 0));
268                                 if (elen)
269                                         indirect_ext_len = (elen +
270                                                             sb->s_blocksize -
271                                                             1) >> sb->
272                                             s_blocksize_bits;
273                                 else
274                                         indirect_ext_len = 1;
275                         } else {
276                                 extent_trunc(inode, &epos, eloc, etype, elen,
277                                              0);
278                                 epos.offset += adsize;
279                         }
280                 }
281
282                 if (indirect_ext_len) {
283                         if (!epos.bh)
284                                 BUG();
285                         udf_free_blocks(sb, inode, epos.block, 0,
286                                         indirect_ext_len);
287                 } else {
288                         if (!epos.bh) {
289                                 UDF_I_LENALLOC(inode) = lenalloc;
290                                 mark_inode_dirty(inode);
291                         } else {
292                                 struct allocExtDesc *aed =
293                                     (struct allocExtDesc *)(epos.bh->b_data);
294                                 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
295                                 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)
296                                     || UDF_SB_UDFREV(sb) >= 0x0201)
297                                         udf_update_tag(epos.bh->b_data,
298                                                        lenalloc +
299                                                        sizeof(struct
300                                                               allocExtDesc));
301                                 else
302                                         udf_update_tag(epos.bh->b_data,
303                                                        sizeof(struct
304                                                               allocExtDesc));
305                                 mark_buffer_dirty_inode(epos.bh, inode);
306                         }
307                 }
308         } else if (inode->i_size) {
309                 if (byte_offset) {
310                         kernel_long_ad extent;
311
312                         /*
313                          *  OK, there is not extent covering inode->i_size and
314                          *  no extent above inode->i_size => truncate is
315                          *  extending the file by 'offset' blocks.
316                          */
317                         if ((!epos.bh
318                              && epos.offset ==
319                              udf_file_entry_alloc_offset(inode)) || (epos.bh
320                                                                      && epos.
321                                                                      offset ==
322                                                                      sizeof
323                                                                      (struct
324                                                                       allocExtDesc)))
325                         {
326                                 /* File has no extents at all or has empty last
327                                  * indirect extent! Create a fake extent... */
328                                 extent.extLocation.logicalBlockNum = 0;
329                                 extent.extLocation.partitionReferenceNum = 0;
330                                 extent.extLength =
331                                     EXT_NOT_RECORDED_NOT_ALLOCATED;
332                         } else {
333                                 epos.offset -= adsize;
334                                 etype = udf_next_aext(inode, &epos,
335                                                       &extent.extLocation,
336                                                       &extent.extLength, 0);
337                                 extent.extLength |= etype << 30;
338                         }
339                         udf_extend_file(inode, &epos, &extent,
340                                         offset +
341                                         ((inode->
342                                           i_size & (sb->s_blocksize - 1)) !=
343                                          0));
344                 }
345         }
346         UDF_I_LENEXTENTS(inode) = inode->i_size;
347
348         brelse(epos.bh);
349 }