blob: fe61be17cdab93315f5f66b32d5add96b20b1d23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * truncate.c
3 *
4 * PURPOSE
5 * Truncate handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070031static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070035 kernel_lb_addr neloc = {};
36 int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
37 inode->i_sb->s_blocksize_bits;
38 int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
39 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070041 if (nelen) {
42 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
43 udf_free_blocks(inode->i_sb, inode, eloc, 0,
44 last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070046 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 neloc = eloc;
48 nelen = (etype << 30) | nelen;
49 }
50
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070051 if (elen != nelen) {
Jan Karaff116fc2007-05-08 00:35:14 -070052 udf_write_aext(inode, epos, neloc, nelen, 0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070053 if (last_block - first_block > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
55 mark_inode_dirty(inode);
56
57 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070058 udf_free_blocks(inode->i_sb, inode, eloc,
59 first_block,
60 last_block - first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
62 }
63}
64
Jan Kara74584ae2007-06-16 10:16:14 -070065/*
66 * Truncate the last extent to match i_size. This function assumes
67 * that preallocation extent is already truncated.
68 */
69void udf_truncate_tail_extent(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070071 struct extent_position epos = {};
Jan Karaff116fc2007-05-08 00:35:14 -070072 kernel_lb_addr eloc;
73 uint32_t elen, nelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 uint64_t lbcount = 0;
75 int8_t etype = -1, netype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int adsize;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080077 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080079 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
80 inode->i_size == iinfo->i_lenExtents)
Jan Kara74584ae2007-06-16 10:16:14 -070081 return;
82 /* Are we going to delete the file anyway? */
83 if (inode->i_nlink == 0)
84 return;
85
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080086 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Kara74584ae2007-06-16 10:16:14 -070087 adsize = sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080088 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Jan Kara74584ae2007-06-16 10:16:14 -070089 adsize = sizeof(long_ad);
90 else
91 BUG();
92
93 /* Find the last extent in the file */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070094 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Jan Kara74584ae2007-06-16 10:16:14 -070095 etype = netype;
96 lbcount += elen;
97 if (lbcount > inode->i_size) {
98 if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
99 printk(KERN_WARNING
100 "udf_truncate_tail_extent(): Too long "
101 "extent after EOF in inode %u: i_size: "
102 "%Ld lbcount: %Ld extent %u+%u\n",
103 (unsigned)inode->i_ino,
104 (long long)inode->i_size,
105 (long long)lbcount,
106 (unsigned)eloc.logicalBlockNum,
107 (unsigned)elen);
108 nelen = elen - (lbcount - inode->i_size);
109 epos.offset -= adsize;
110 extent_trunc(inode, &epos, eloc, etype, elen, nelen);
111 epos.offset += adsize;
112 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
113 printk(KERN_ERR "udf_truncate_tail_extent(): "
114 "Extent after EOF in inode %u.\n",
115 (unsigned)inode->i_ino);
116 break;
117 }
118 }
119 /* This inode entry is in-memory only and thus we don't have to mark
120 * the inode dirty */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800121 iinfo->i_lenExtents = inode->i_size;
Jan Kara74584ae2007-06-16 10:16:14 -0700122 brelse(epos.bh);
123}
124
125void udf_discard_prealloc(struct inode *inode)
126{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700127 struct extent_position epos = { NULL, 0, {0, 0} };
Jan Kara74584ae2007-06-16 10:16:14 -0700128 kernel_lb_addr eloc;
129 uint32_t elen;
130 uint64_t lbcount = 0;
131 int8_t etype = -1, netype;
132 int adsize;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800133 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700134
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800135 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
136 inode->i_size == iinfo->i_lenExtents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800139 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 adsize = sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800141 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 adsize = sizeof(long_ad);
143 else
144 adsize = 0;
145
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800146 epos.block = iinfo->i_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jan Karaff116fc2007-05-08 00:35:14 -0700148 /* Find the last extent in the file */
Jan Kara74584ae2007-06-16 10:16:14 -0700149 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 etype = netype;
151 lbcount += elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Jan Karaff116fc2007-05-08 00:35:14 -0700153 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
154 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 lbcount -= elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700156 extent_trunc(inode, &epos, eloc, etype, elen, 0);
Jan Kara74584ae2007-06-16 10:16:14 -0700157 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800158 iinfo->i_lenAlloc =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800159 epos.offset -
160 udf_file_entry_alloc_offset(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 mark_inode_dirty(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700162 } else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700163 struct allocExtDesc *aed =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700164 (struct allocExtDesc *)(epos.bh->b_data);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700165 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700166 cpu_to_le32(epos.offset -
167 sizeof(struct allocExtDesc));
168 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800169 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -0700170 udf_update_tag(epos.bh->b_data, epos.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700172 udf_update_tag(epos.bh->b_data,
173 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -0700174 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 }
Jan Kara74584ae2007-06-16 10:16:14 -0700177 /* This inode entry is in-memory only and thus we don't have to mark
178 * the inode dirty */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800179 iinfo->i_lenExtents = lbcount;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700180 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700183void udf_truncate_extents(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Jan Karaff116fc2007-05-08 00:35:14 -0700185 struct extent_position epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700186 kernel_lb_addr eloc, neloc = {};
Jan Karaff116fc2007-05-08 00:35:14 -0700187 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int8_t etype;
Jan Kara31170b62007-05-08 00:35:21 -0700189 struct super_block *sb = inode->i_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800190 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara31170b62007-05-08 00:35:21 -0700191 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
Jan Kara60448b12007-05-08 00:35:13 -0700192 loff_t byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int adsize;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800194 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800196 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 adsize = sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800198 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 adsize = sizeof(long_ad);
200 else
Jan Karaff116fc2007-05-08 00:35:14 -0700201 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Jan Karaff116fc2007-05-08 00:35:14 -0700203 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700204 byte_offset = (offset << sb->s_blocksize_bits) +
205 (inode->i_size & (sb->s_blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700206 if (etype != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -0700207 epos.offset -= adsize;
208 extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
209 epos.offset += adsize;
Jan Kara60448b12007-05-08 00:35:13 -0700210 if (byte_offset)
Jan Karaff116fc2007-05-08 00:35:14 -0700211 lenalloc = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 else
Jan Karaff116fc2007-05-08 00:35:14 -0700213 lenalloc = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Jan Karaff116fc2007-05-08 00:35:14 -0700215 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 lenalloc -= udf_file_entry_alloc_offset(inode);
217 else
218 lenalloc -= sizeof(struct allocExtDesc);
219
Marcin Slusarz4b111112008-02-08 04:20:36 -0800220 while ((etype = udf_current_aext(inode, &epos, &eloc,
221 &elen, 0)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700222 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Jan Karaff116fc2007-05-08 00:35:14 -0700223 udf_write_aext(inode, &epos, neloc, nelen, 0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700224 if (indirect_ext_len) {
Jan Karaff116fc2007-05-08 00:35:14 -0700225 /* We managed to free all extents in the
226 * indirect extent - free it too */
227 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 BUG();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700229 udf_free_blocks(sb, inode, epos.block,
230 0, indirect_ext_len);
231 } else {
232 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800233 iinfo->i_lenAlloc =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800234 lenalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700236 } else {
237 struct allocExtDesc *aed =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800238 (struct allocExtDesc *)
239 (epos.bh->b_data);
240 int len =
241 sizeof(struct allocExtDesc);
242
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700243 aed->lengthAllocDescs =
244 cpu_to_le32(lenalloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800245 if (!UDF_QUERY_FLAG(sb,
246 UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800247 sbi->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800248 len += lenalloc;
249
250 udf_update_tag(epos.bh->b_data,
251 len);
252 mark_buffer_dirty_inode(
253 epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 }
Jan Karaff116fc2007-05-08 00:35:14 -0700256 brelse(epos.bh);
257 epos.offset = sizeof(struct allocExtDesc);
258 epos.block = eloc;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800259 epos.bh = udf_tread(sb,
260 udf_get_lb_pblock(sb, eloc, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (elen)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800262 indirect_ext_len =
263 (elen + sb->s_blocksize - 1) >>
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700264 sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 else
Jan Karaff116fc2007-05-08 00:35:14 -0700266 indirect_ext_len = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700267 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800268 extent_trunc(inode, &epos, eloc, etype,
269 elen, 0);
Jan Karaff116fc2007-05-08 00:35:14 -0700270 epos.offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 }
273
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700274 if (indirect_ext_len) {
Jan Karaff116fc2007-05-08 00:35:14 -0700275 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 BUG();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700277 udf_free_blocks(sb, inode, epos.block, 0,
278 indirect_ext_len);
279 } else {
280 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800281 iinfo->i_lenAlloc = lenalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700283 } else {
284 struct allocExtDesc *aed =
285 (struct allocExtDesc *)(epos.bh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700287 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800288 sbi->s_udfrev >= 0x0201)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700289 udf_update_tag(epos.bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800290 lenalloc +
291 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700293 udf_update_tag(epos.bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800294 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -0700295 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700298 } else if (inode->i_size) {
299 if (byte_offset) {
Jan Kara31170b62007-05-08 00:35:21 -0700300 kernel_long_ad extent;
301
Jan Kara00a2b0f2006-08-15 13:56:26 +0200302 /*
303 * OK, there is not extent covering inode->i_size and
304 * no extent above inode->i_size => truncate is
Jan Kara31170b62007-05-08 00:35:21 -0700305 * extending the file by 'offset' blocks.
Jan Kara00a2b0f2006-08-15 13:56:26 +0200306 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700307 if ((!epos.bh &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800308 epos.offset ==
309 udf_file_entry_alloc_offset(inode)) ||
310 (epos.bh && epos.offset ==
311 sizeof(struct allocExtDesc))) {
Jan Kara31170b62007-05-08 00:35:21 -0700312 /* File has no extents at all or has empty last
313 * indirect extent! Create a fake extent... */
314 extent.extLocation.logicalBlockNum = 0;
315 extent.extLocation.partitionReferenceNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800316 extent.extLength =
317 EXT_NOT_RECORDED_NOT_ALLOCATED;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700318 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700319 epos.offset -= adsize;
Jan Kara31170b62007-05-08 00:35:21 -0700320 etype = udf_next_aext(inode, &epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700321 &extent.extLocation,
322 &extent.extLength, 0);
Jan Kara31170b62007-05-08 00:35:21 -0700323 extent.extLength |= etype << 30;
Jan Kara00a2b0f2006-08-15 13:56:26 +0200324 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700325 udf_extend_file(inode, &epos, &extent,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800326 offset +
327 ((inode->i_size &
328 (sb->s_blocksize - 1)) != 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800331 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Jan Kara3bf25cb2007-05-08 00:35:16 -0700333 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}