blob: cde328f167750b13e9ef6919b5f10c2ca01dd65a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/buffer_head.h>
26
27#include "udf_i.h"
28#include "udf_sb.h"
29
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070030static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070034 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070040 if (nelen) {
41 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
42 udf_free_blocks(inode->i_sb, inode, eloc, 0,
43 last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070045 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 neloc = eloc;
47 nelen = (etype << 30) | nelen;
48 }
49
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070050 if (elen != nelen) {
Jan Karaff116fc2007-05-08 00:35:14 -070051 udf_write_aext(inode, epos, neloc, nelen, 0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070052 if (last_block - first_block > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
54 mark_inode_dirty(inode);
55
56 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070057 udf_free_blocks(inode->i_sb, inode, eloc,
58 first_block,
59 last_block - first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61 }
62}
63
Jan Kara74584ae2007-06-16 10:16:14 -070064/*
65 * Truncate the last extent to match i_size. This function assumes
66 * that preallocation extent is already truncated.
67 */
68void udf_truncate_tail_extent(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070070 struct extent_position epos = {};
Jan Karaff116fc2007-05-08 00:35:14 -070071 kernel_lb_addr eloc;
72 uint32_t elen, nelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 uint64_t lbcount = 0;
74 int8_t etype = -1, netype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int adsize;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080076 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080078 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
79 inode->i_size == iinfo->i_lenExtents)
Jan Kara74584ae2007-06-16 10:16:14 -070080 return;
81 /* Are we going to delete the file anyway? */
82 if (inode->i_nlink == 0)
83 return;
84
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080085 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Kara74584ae2007-06-16 10:16:14 -070086 adsize = sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080087 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Jan Kara74584ae2007-06-16 10:16:14 -070088 adsize = sizeof(long_ad);
89 else
90 BUG();
91
92 /* Find the last extent in the file */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070093 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Jan Kara74584ae2007-06-16 10:16:14 -070094 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 */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800120 iinfo->i_lenExtents = inode->i_size;
Jan Kara74584ae2007-06-16 10:16:14 -0700121 brelse(epos.bh);
122}
123
124void udf_discard_prealloc(struct inode *inode)
125{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700126 struct extent_position epos = { NULL, 0, {0, 0} };
Jan Kara74584ae2007-06-16 10:16:14 -0700127 kernel_lb_addr eloc;
128 uint32_t elen;
129 uint64_t lbcount = 0;
130 int8_t etype = -1, netype;
131 int adsize;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800132 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700133
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800134 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
135 inode->i_size == iinfo->i_lenExtents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800138 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 adsize = sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800140 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 adsize = sizeof(long_ad);
142 else
143 adsize = 0;
144
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800145 epos.block = iinfo->i_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jan Karaff116fc2007-05-08 00:35:14 -0700147 /* Find the last extent in the file */
Jan Kara74584ae2007-06-16 10:16:14 -0700148 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 etype = netype;
150 lbcount += elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Jan Karaff116fc2007-05-08 00:35:14 -0700152 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
153 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 lbcount -= elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700155 extent_trunc(inode, &epos, eloc, etype, elen, 0);
Jan Kara74584ae2007-06-16 10:16:14 -0700156 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800157 iinfo->i_lenAlloc =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800158 epos.offset -
159 udf_file_entry_alloc_offset(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 mark_inode_dirty(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700161 } else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 struct allocExtDesc *aed =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700163 (struct allocExtDesc *)(epos.bh->b_data);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700164 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700165 cpu_to_le32(epos.offset -
166 sizeof(struct allocExtDesc));
167 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800168 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -0700169 udf_update_tag(epos.bh->b_data, epos.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700171 udf_update_tag(epos.bh->b_data,
172 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -0700173 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 }
Jan Kara74584ae2007-06-16 10:16:14 -0700176 /* This inode entry is in-memory only and thus we don't have to mark
177 * the inode dirty */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800178 iinfo->i_lenExtents = lbcount;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700179 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700182void udf_truncate_extents(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Jan Karaff116fc2007-05-08 00:35:14 -0700184 struct extent_position epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700185 kernel_lb_addr eloc, neloc = {};
Jan Karaff116fc2007-05-08 00:35:14 -0700186 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int8_t etype;
Jan Kara31170b62007-05-08 00:35:21 -0700188 struct super_block *sb = inode->i_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800189 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara31170b62007-05-08 00:35:21 -0700190 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
Jan Kara60448b12007-05-08 00:35:13 -0700191 loff_t byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 int adsize;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800193 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800195 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 adsize = sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800197 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 adsize = sizeof(long_ad);
199 else
Jan Karaff116fc2007-05-08 00:35:14 -0700200 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Jan Karaff116fc2007-05-08 00:35:14 -0700202 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700203 byte_offset = (offset << sb->s_blocksize_bits) +
204 (inode->i_size & (sb->s_blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700205 if (etype != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -0700206 epos.offset -= adsize;
207 extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
208 epos.offset += adsize;
Jan Kara60448b12007-05-08 00:35:13 -0700209 if (byte_offset)
Jan Karaff116fc2007-05-08 00:35:14 -0700210 lenalloc = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 else
Jan Karaff116fc2007-05-08 00:35:14 -0700212 lenalloc = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jan Karaff116fc2007-05-08 00:35:14 -0700214 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 lenalloc -= udf_file_entry_alloc_offset(inode);
216 else
217 lenalloc -= sizeof(struct allocExtDesc);
218
Marcin Slusarz4b111112008-02-08 04:20:36 -0800219 while ((etype = udf_current_aext(inode, &epos, &eloc,
220 &elen, 0)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700221 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Jan Karaff116fc2007-05-08 00:35:14 -0700222 udf_write_aext(inode, &epos, neloc, nelen, 0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700223 if (indirect_ext_len) {
Jan Karaff116fc2007-05-08 00:35:14 -0700224 /* We managed to free all extents in the
225 * indirect extent - free it too */
marcin.slusarz@gmail.com9de90b72008-01-30 22:03:55 +0100226 BUG_ON(!epos.bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700227 udf_free_blocks(sb, inode, epos.block,
228 0, indirect_ext_len);
marcin.slusarz@gmail.com9de90b72008-01-30 22:03:55 +0100229 } else if (!epos.bh) {
230 iinfo->i_lenAlloc = lenalloc;
231 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700232 } else {
marcin.slusarz@gmail.com9de90b72008-01-30 22:03:55 +0100233 struct allocExtDesc *aed =
234 (struct allocExtDesc *)
235 (epos.bh->b_data);
236 int len = sizeof(struct allocExtDesc);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800237
marcin.slusarz@gmail.com9de90b72008-01-30 22:03:55 +0100238 aed->lengthAllocDescs =
239 cpu_to_le32(lenalloc);
240 if (!UDF_QUERY_FLAG(sb,
241 UDF_FLAG_STRICT) ||
242 sbi->s_udfrev >= 0x0201)
243 len += lenalloc;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800244
marcin.slusarz@gmail.com9de90b72008-01-30 22:03:55 +0100245 udf_update_tag(epos.bh->b_data,
246 len);
247 mark_buffer_dirty_inode(
248 epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Jan Karaff116fc2007-05-08 00:35:14 -0700250 brelse(epos.bh);
251 epos.offset = sizeof(struct allocExtDesc);
252 epos.block = eloc;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800253 epos.bh = udf_tread(sb,
254 udf_get_lb_pblock(sb, eloc, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (elen)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800256 indirect_ext_len =
257 (elen + sb->s_blocksize - 1) >>
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700258 sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 else
Jan Karaff116fc2007-05-08 00:35:14 -0700260 indirect_ext_len = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700261 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800262 extent_trunc(inode, &epos, eloc, etype,
263 elen, 0);
Jan Karaff116fc2007-05-08 00:35:14 -0700264 epos.offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 }
267
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700268 if (indirect_ext_len) {
marcin.slusarz@gmail.com9de90b72008-01-30 22:03:55 +0100269 BUG_ON(!epos.bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700270 udf_free_blocks(sb, inode, epos.block, 0,
271 indirect_ext_len);
marcin.slusarz@gmail.com9de90b72008-01-30 22:03:55 +0100272 } else if (!epos.bh) {
273 iinfo->i_lenAlloc = lenalloc;
274 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700275 } else {
marcin.slusarz@gmail.com9de90b72008-01-30 22:03:55 +0100276 struct allocExtDesc *aed =
277 (struct allocExtDesc *)(epos.bh->b_data);
278 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
279 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) ||
280 sbi->s_udfrev >= 0x0201)
281 udf_update_tag(epos.bh->b_data,
282 lenalloc +
283 sizeof(struct allocExtDesc));
284 else
285 udf_update_tag(epos.bh->b_data,
286 sizeof(struct allocExtDesc));
287 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700289 } else if (inode->i_size) {
290 if (byte_offset) {
Jan Kara31170b62007-05-08 00:35:21 -0700291 kernel_long_ad extent;
292
Jan Kara00a2b0f2006-08-15 13:56:26 +0200293 /*
294 * OK, there is not extent covering inode->i_size and
295 * no extent above inode->i_size => truncate is
Jan Kara31170b62007-05-08 00:35:21 -0700296 * extending the file by 'offset' blocks.
Jan Kara00a2b0f2006-08-15 13:56:26 +0200297 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700298 if ((!epos.bh &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800299 epos.offset ==
300 udf_file_entry_alloc_offset(inode)) ||
301 (epos.bh && epos.offset ==
302 sizeof(struct allocExtDesc))) {
Jan Kara31170b62007-05-08 00:35:21 -0700303 /* File has no extents at all or has empty last
304 * indirect extent! Create a fake extent... */
305 extent.extLocation.logicalBlockNum = 0;
306 extent.extLocation.partitionReferenceNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800307 extent.extLength =
308 EXT_NOT_RECORDED_NOT_ALLOCATED;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700309 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700310 epos.offset -= adsize;
Jan Kara31170b62007-05-08 00:35:21 -0700311 etype = udf_next_aext(inode, &epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700312 &extent.extLocation,
313 &extent.extLength, 0);
Jan Kara31170b62007-05-08 00:35:21 -0700314 extent.extLength |= etype << 30;
Jan Kara00a2b0f2006-08-15 13:56:26 +0200315 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700316 udf_extend_file(inode, &epos, &extent,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800317 offset +
318 ((inode->i_size &
319 (sb->s_blocksize - 1)) != 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800322 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Jan Kara3bf25cb2007-05-08 00:35:16 -0700324 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}