blob: 8a9657d7f7c680eed83bd007372796b5c2f2523c [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,
Pekka Enberg97e961f2008-10-15 12:29:03 +020031 struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070032 uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020034 struct kernel_lb_addr neloc = {};
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070035 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
Pekka Enberg97e961f2008-10-15 12:29:03 +020046 neloc = *eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 nelen = (etype << 30) | nelen;
48 }
49
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070050 if (elen != nelen) {
Pekka Enberg97e961f2008-10-15 12:29:03 +020051 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 = {};
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020071 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -070072 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)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020086 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080087 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020088 adsize = sizeof(struct long_ad);
Jan Kara74584ae2007-06-16 10:16:14 -070089 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)
Joe Perches78ace702011-10-10 01:08:05 -070098 udf_warn(inode->i_sb,
99 "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n",
100 (unsigned)inode->i_ino,
101 (long long)inode->i_size,
102 (long long)lbcount,
103 (unsigned)eloc.logicalBlockNum,
104 (unsigned)elen);
Jan Kara74584ae2007-06-16 10:16:14 -0700105 nelen = elen - (lbcount - inode->i_size);
106 epos.offset -= adsize;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200107 extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
Jan Kara74584ae2007-06-16 10:16:14 -0700108 epos.offset += adsize;
109 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
Joe Perches78ace702011-10-10 01:08:05 -0700110 udf_err(inode->i_sb,
111 "Extent after EOF in inode %u\n",
112 (unsigned)inode->i_ino);
Jan Kara74584ae2007-06-16 10:16:14 -0700113 break;
114 }
115 }
116 /* This inode entry is in-memory only and thus we don't have to mark
117 * the inode dirty */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800118 iinfo->i_lenExtents = inode->i_size;
Jan Kara74584ae2007-06-16 10:16:14 -0700119 brelse(epos.bh);
120}
121
122void udf_discard_prealloc(struct inode *inode)
123{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700124 struct extent_position epos = { NULL, 0, {0, 0} };
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200125 struct kernel_lb_addr eloc;
Jan Kara74584ae2007-06-16 10:16:14 -0700126 uint32_t elen;
127 uint64_t lbcount = 0;
128 int8_t etype = -1, netype;
129 int adsize;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800130 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700131
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800132 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
133 inode->i_size == iinfo->i_lenExtents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800136 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200137 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800138 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200139 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 else
141 adsize = 0;
142
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800143 epos.block = iinfo->i_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jan Karaff116fc2007-05-08 00:35:14 -0700145 /* Find the last extent in the file */
Jan Kara74584ae2007-06-16 10:16:14 -0700146 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 etype = netype;
148 lbcount += elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Jan Karaff116fc2007-05-08 00:35:14 -0700150 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
151 epos.offset -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 lbcount -= elen;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200153 extent_trunc(inode, &epos, &eloc, etype, elen, 0);
Jan Kara74584ae2007-06-16 10:16:14 -0700154 if (!epos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800155 iinfo->i_lenAlloc =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800156 epos.offset -
157 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) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800166 UDF_SB(inode->i_sb)->s_udfrev >= 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 */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800176 iinfo->i_lenExtents = lbcount;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700177 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
marcin.slusarz@gmail.com456390d2008-01-30 22:03:56 +0100180static void udf_update_alloc_ext_desc(struct inode *inode,
181 struct extent_position *epos,
182 u32 lenalloc)
183{
184 struct super_block *sb = inode->i_sb;
185 struct udf_sb_info *sbi = UDF_SB(sb);
186
187 struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
188 int len = sizeof(struct allocExtDesc);
189
190 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
191 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
192 len += lenalloc;
193
194 udf_update_tag(epos->bh->b_data, len);
195 mark_buffer_dirty_inode(epos->bh, inode);
196}
197
Jan Kara7e49b6f2010-10-22 00:30:26 +0200198/*
199 * Truncate extents of inode to inode->i_size. This function can be used only
200 * for making file shorter. For making file longer, udf_extend_file() has to
201 * be used.
202 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700203void udf_truncate_extents(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Jan Karaff116fc2007-05-08 00:35:14 -0700205 struct extent_position epos;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200206 struct kernel_lb_addr eloc, neloc = {};
Jan Karaff116fc2007-05-08 00:35:14 -0700207 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int8_t etype;
Jan Kara31170b62007-05-08 00:35:21 -0700209 struct super_block *sb = inode->i_sb;
210 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
Jan Kara60448b12007-05-08 00:35:13 -0700211 loff_t byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int adsize;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800213 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800215 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200216 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800217 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200218 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 else
Jan Karaff116fc2007-05-08 00:35:14 -0700220 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jan Karaff116fc2007-05-08 00:35:14 -0700222 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700223 byte_offset = (offset << sb->s_blocksize_bits) +
224 (inode->i_size & (sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200225 if (etype == -1) {
226 /* We should extend the file? */
227 WARN_ON(byte_offset);
228 return;
229 }
230 epos.offset -= adsize;
231 extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
232 epos.offset += adsize;
233 if (byte_offset)
234 lenalloc = epos.offset;
235 else
236 lenalloc = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jan Kara7e49b6f2010-10-22 00:30:26 +0200238 if (!epos.bh)
239 lenalloc -= udf_file_entry_alloc_offset(inode);
240 else
241 lenalloc -= sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jan Kara7e49b6f2010-10-22 00:30:26 +0200243 while ((etype = udf_current_aext(inode, &epos, &eloc,
244 &elen, 0)) != -1) {
245 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
246 udf_write_aext(inode, &epos, &neloc, nelen, 0);
247 if (indirect_ext_len) {
248 /* We managed to free all extents in the
249 * indirect extent - free it too */
250 BUG_ON(!epos.bh);
Jan Kara17dc59b2012-07-09 13:24:21 +0200251 udf_free_blocks(sb, NULL, &epos.block,
Jan Kara7e49b6f2010-10-22 00:30:26 +0200252 0, indirect_ext_len);
253 } else if (!epos.bh) {
254 iinfo->i_lenAlloc = lenalloc;
255 mark_inode_dirty(inode);
256 } else
257 udf_update_alloc_ext_desc(inode,
258 &epos, lenalloc);
259 brelse(epos.bh);
260 epos.offset = sizeof(struct allocExtDesc);
261 epos.block = eloc;
262 epos.bh = udf_tread(sb,
263 udf_get_lb_pblock(sb, &eloc, 0));
264 if (elen)
265 indirect_ext_len =
266 (elen + sb->s_blocksize - 1) >>
267 sb->s_blocksize_bits;
268 else
269 indirect_ext_len = 1;
270 } else {
271 extent_trunc(inode, &epos, &eloc, etype, elen, 0);
272 epos.offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 }
Jan Kara7e49b6f2010-10-22 00:30:26 +0200275
276 if (indirect_ext_len) {
277 BUG_ON(!epos.bh);
Jan Kara17dc59b2012-07-09 13:24:21 +0200278 udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200279 } else if (!epos.bh) {
280 iinfo->i_lenAlloc = lenalloc;
281 mark_inode_dirty(inode);
282 } else
283 udf_update_alloc_ext_desc(inode, &epos, lenalloc);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800284 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Jan Kara3bf25cb2007-05-08 00:35:16 -0700286 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}