blob: 7fc3912885a565bddbdadd8a9ade8057bd82b3ac [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;
77
78 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
Jan Kara74584ae2007-06-16 10:16:14 -070079 inode->i_size == UDF_I_LENEXTENTS(inode))
80 return;
81 /* Are we going to delete the file anyway? */
82 if (inode->i_nlink == 0)
83 return;
84
85 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
86 adsize = sizeof(short_ad);
87 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
88 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 */
120 UDF_I_LENEXTENTS(inode) = inode->i_size;
121 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;
132
133 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700134 inode->i_size == UDF_I_LENEXTENTS(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
138 adsize = sizeof(short_ad);
139 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
140 adsize = sizeof(long_ad);
141 else
142 adsize = 0;
143
Jan Karaff116fc2007-05-08 00:35:14 -0700144 epos.block = UDF_I_LOCATION(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jan Karaff116fc2007-05-08 00:35:14 -0700146 /* Find the last extent in the file */
Jan Kara74584ae2007-06-16 10:16:14 -0700147 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 etype = netype;
149 lbcount += elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Jan Karaff116fc2007-05-08 00:35:14 -0700151 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
152 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 lbcount -= elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700154 extent_trunc(inode, &epos, eloc, etype, elen, 0);
Jan Kara74584ae2007-06-16 10:16:14 -0700155 if (!epos.bh) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700156 UDF_I_LENALLOC(inode) =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700157 epos.offset - udf_file_entry_alloc_offset(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 mark_inode_dirty(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700159 } else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700160 struct allocExtDesc *aed =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700161 (struct allocExtDesc *)(epos.bh->b_data);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700163 cpu_to_le32(epos.offset -
164 sizeof(struct allocExtDesc));
165 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
166 UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -0700167 udf_update_tag(epos.bh->b_data, epos.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700169 udf_update_tag(epos.bh->b_data,
170 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -0700171 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 }
Jan Kara74584ae2007-06-16 10:16:14 -0700174 /* This inode entry is in-memory only and thus we don't have to mark
175 * the inode dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 UDF_I_LENEXTENTS(inode) = lbcount;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700177 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700180void udf_truncate_extents(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Jan Karaff116fc2007-05-08 00:35:14 -0700182 struct extent_position epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700183 kernel_lb_addr eloc, neloc = {};
Jan Karaff116fc2007-05-08 00:35:14 -0700184 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int8_t etype;
Jan Kara31170b62007-05-08 00:35:21 -0700186 struct super_block *sb = inode->i_sb;
187 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
Jan Kara60448b12007-05-08 00:35:13 -0700188 loff_t byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int adsize;
190
191 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
192 adsize = sizeof(short_ad);
193 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
194 adsize = sizeof(long_ad);
195 else
Jan Karaff116fc2007-05-08 00:35:14 -0700196 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jan Karaff116fc2007-05-08 00:35:14 -0700198 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700199 byte_offset = (offset << sb->s_blocksize_bits) +
200 (inode->i_size & (sb->s_blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700201 if (etype != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -0700202 epos.offset -= adsize;
203 extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
204 epos.offset += adsize;
Jan Kara60448b12007-05-08 00:35:13 -0700205 if (byte_offset)
Jan Karaff116fc2007-05-08 00:35:14 -0700206 lenalloc = epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 else
Jan Karaff116fc2007-05-08 00:35:14 -0700208 lenalloc = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Jan Karaff116fc2007-05-08 00:35:14 -0700210 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 lenalloc -= udf_file_entry_alloc_offset(inode);
212 else
213 lenalloc -= sizeof(struct allocExtDesc);
214
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700215 while ((etype = udf_current_aext(inode, &epos, &eloc, &elen, 0)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700216 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Jan Karaff116fc2007-05-08 00:35:14 -0700217 udf_write_aext(inode, &epos, neloc, nelen, 0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700218 if (indirect_ext_len) {
Jan Karaff116fc2007-05-08 00:35:14 -0700219 /* We managed to free all extents in the
220 * indirect extent - free it too */
221 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 BUG();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700223 udf_free_blocks(sb, inode, epos.block,
224 0, indirect_ext_len);
225 } else {
226 if (!epos.bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700227 UDF_I_LENALLOC(inode) = lenalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700229 } else {
230 struct allocExtDesc *aed =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700231 (struct allocExtDesc *)(epos.bh->b_data);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700232 aed->lengthAllocDescs =
233 cpu_to_le32(lenalloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700234 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) ||
235 UDF_SB_UDFREV(sb) >= 0x0201)
236 udf_update_tag(epos.bh->b_data,
237 lenalloc +
238 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700240 udf_update_tag(epos.bh->b_data,
241 sizeof(struct allocExtDesc));
242 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 }
Jan Karaff116fc2007-05-08 00:35:14 -0700245 brelse(epos.bh);
246 epos.offset = sizeof(struct allocExtDesc);
247 epos.block = eloc;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700248 epos.bh = udf_tread(sb, udf_get_lb_pblock(sb, eloc, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (elen)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700250 indirect_ext_len = (elen + sb->s_blocksize -1) >>
251 sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 else
Jan Karaff116fc2007-05-08 00:35:14 -0700253 indirect_ext_len = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700254 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700255 extent_trunc(inode, &epos, eloc, etype, elen, 0);
Jan Karaff116fc2007-05-08 00:35:14 -0700256 epos.offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 }
259
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700260 if (indirect_ext_len) {
Jan Karaff116fc2007-05-08 00:35:14 -0700261 if (!epos.bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 BUG();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700263 udf_free_blocks(sb, inode, epos.block, 0,
264 indirect_ext_len);
265 } else {
266 if (!epos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 UDF_I_LENALLOC(inode) = lenalloc;
268 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700269 } else {
270 struct allocExtDesc *aed =
271 (struct allocExtDesc *)(epos.bh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700273 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) ||
274 UDF_SB_UDFREV(sb) >= 0x0201)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700275 udf_update_tag(epos.bh->b_data,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700276 lenalloc + sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700278 udf_update_tag(epos.bh->b_data,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700279 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -0700280 mark_buffer_dirty_inode(epos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700283 } else if (inode->i_size) {
284 if (byte_offset) {
Jan Kara31170b62007-05-08 00:35:21 -0700285 kernel_long_ad extent;
286
Jan Kara00a2b0f2006-08-15 13:56:26 +0200287 /*
288 * OK, there is not extent covering inode->i_size and
289 * no extent above inode->i_size => truncate is
Jan Kara31170b62007-05-08 00:35:21 -0700290 * extending the file by 'offset' blocks.
Jan Kara00a2b0f2006-08-15 13:56:26 +0200291 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700292 if ((!epos.bh &&
293 epos.offset == udf_file_entry_alloc_offset(inode)) ||
294 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
Jan Kara31170b62007-05-08 00:35:21 -0700295 /* File has no extents at all or has empty last
296 * indirect extent! Create a fake extent... */
297 extent.extLocation.logicalBlockNum = 0;
298 extent.extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700299 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700300 } else {
Jan Karaff116fc2007-05-08 00:35:14 -0700301 epos.offset -= adsize;
Jan Kara31170b62007-05-08 00:35:21 -0700302 etype = udf_next_aext(inode, &epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700303 &extent.extLocation,
304 &extent.extLength, 0);
Jan Kara31170b62007-05-08 00:35:21 -0700305 extent.extLength |= etype << 30;
Jan Kara00a2b0f2006-08-15 13:56:26 +0200306 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700307 udf_extend_file(inode, &epos, &extent,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700308 offset + ((inode->i_size & (sb->s_blocksize - 1)) != 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 }
311 UDF_I_LENEXTENTS(inode) = inode->i_size;
312
Jan Kara3bf25cb2007-05-08 00:35:16 -0700313 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}