truncate.c 8.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * truncate.c
 *
 * PURPOSE
 *	Truncate handling routines for the OSTA-UDF(tm) filesystem.
 *
 * COPYRIGHT
 *	This file is distributed under the terms of the GNU General Public
 *	License (GPL). Copies of the GPL can be obtained from:
 *		ftp://prep.ai.mit.edu/pub/gnu/GPL
 *	Each contributing author retains all rights to their own work.
 *
 *  (C) 1999-2004 Ben Fennema
 *  (C) 1999 Stelias Computing Inc
 *
 * HISTORY
 *
 *  02/24/99 blf  Created.
 *
 */

#include "udfdecl.h"
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/buffer_head.h>

#include "udf_i.h"
#include "udf_sb.h"

30 31 32
static void extent_trunc(struct inode *inode, struct extent_position *epos,
			 kernel_lb_addr eloc, int8_t etype, uint32_t elen,
			 uint32_t nelen)
L
Linus Torvalds 已提交
33
{
34 35 36 37 38
	kernel_lb_addr neloc = {};
	int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
		inode->i_sb->s_blocksize_bits;
	int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
		inode->i_sb->s_blocksize_bits;
L
Linus Torvalds 已提交
39

40 41 42 43
	if (nelen) {
		if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
			udf_free_blocks(inode->i_sb, inode, eloc, 0,
					last_block);
L
Linus Torvalds 已提交
44
			etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
45
		} else
L
Linus Torvalds 已提交
46 47 48 49
			neloc = eloc;
		nelen = (etype << 30) | nelen;
	}

50
	if (elen != nelen) {
J
Jan Kara 已提交
51
		udf_write_aext(inode, epos, neloc, nelen, 0);
52
		if (last_block - first_block > 0) {
L
Linus Torvalds 已提交
53 54 55 56
			if (etype == (EXT_RECORDED_ALLOCATED >> 30))
				mark_inode_dirty(inode);

			if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
57 58 59
				udf_free_blocks(inode->i_sb, inode, eloc,
						first_block,
						last_block - first_block);
L
Linus Torvalds 已提交
60 61 62 63
		}
	}
}

J
Jan Kara 已提交
64 65 66 67 68
/*
 * Truncate the last extent to match i_size. This function assumes
 * that preallocation extent is already truncated.
 */
void udf_truncate_tail_extent(struct inode *inode)
L
Linus Torvalds 已提交
69
{
70
	struct extent_position epos = {};
J
Jan Kara 已提交
71 72
	kernel_lb_addr eloc;
	uint32_t elen, nelen;
L
Linus Torvalds 已提交
73 74 75
	uint64_t lbcount = 0;
	int8_t etype = -1, netype;
	int adsize;
76
	struct udf_inode_info *iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
77

78 79
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
	    inode->i_size == iinfo->i_lenExtents)
J
Jan Kara 已提交
80 81 82
		return;
	/* Are we going to delete the file anyway? */
	if (inode->i_nlink == 0)
L
Linus Torvalds 已提交
83 84
		return;

85
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
L
Linus Torvalds 已提交
86
		adsize = sizeof(short_ad);
87
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
L
Linus Torvalds 已提交
88 89
		adsize = sizeof(long_ad);
	else
J
Jan Kara 已提交
90
		BUG();
L
Linus Torvalds 已提交
91

J
Jan Kara 已提交
92
	/* Find the last extent in the file */
93
	while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
L
Linus Torvalds 已提交
94 95
		etype = netype;
		lbcount += elen;
J
Jan Kara 已提交
96 97 98 99 100 101 102 103 104 105 106
		if (lbcount > inode->i_size) {
			if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
				printk(KERN_WARNING
				       "udf_truncate_tail_extent(): Too long "
				       "extent after EOF in inode %u: i_size: "
				       "%Ld lbcount: %Ld extent %u+%u\n",
				       (unsigned)inode->i_ino,
				       (long long)inode->i_size,
				       (long long)lbcount,
				       (unsigned)eloc.logicalBlockNum,
				       (unsigned)elen);
L
Linus Torvalds 已提交
107
			nelen = elen - (lbcount - inode->i_size);
J
Jan Kara 已提交
108 109 110
			epos.offset -= adsize;
			extent_trunc(inode, &epos, eloc, etype, elen, nelen);
			epos.offset += adsize;
J
Jan Kara 已提交
111 112 113 114 115
			if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
				printk(KERN_ERR "udf_truncate_tail_extent(): "
				       "Extent after EOF in inode %u.\n",
				       (unsigned)inode->i_ino);
			break;
L
Linus Torvalds 已提交
116 117
		}
	}
J
Jan Kara 已提交
118 119
	/* This inode entry is in-memory only and thus we don't have to mark
	 * the inode dirty */
120
	iinfo->i_lenExtents = inode->i_size;
J
Jan Kara 已提交
121 122 123 124 125
	brelse(epos.bh);
}

void udf_discard_prealloc(struct inode *inode)
{
126
	struct extent_position epos = { NULL, 0, {0, 0} };
J
Jan Kara 已提交
127 128 129 130 131
	kernel_lb_addr eloc;
	uint32_t elen;
	uint64_t lbcount = 0;
	int8_t etype = -1, netype;
	int adsize;
132
	struct udf_inode_info *iinfo = UDF_I(inode);
J
Jan Kara 已提交
133

134 135
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
	    inode->i_size == iinfo->i_lenExtents)
J
Jan Kara 已提交
136 137
		return;

138
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
J
Jan Kara 已提交
139
		adsize = sizeof(short_ad);
140
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
J
Jan Kara 已提交
141 142 143 144
		adsize = sizeof(long_ad);
	else
		adsize = 0;

145
	epos.block = iinfo->i_location;
J
Jan Kara 已提交
146 147 148 149 150 151

	/* Find the last extent in the file */
	while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
		etype = netype;
		lbcount += elen;
	}
J
Jan Kara 已提交
152 153
	if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
		epos.offset -= adsize;
L
Linus Torvalds 已提交
154
		lbcount -= elen;
J
Jan Kara 已提交
155
		extent_trunc(inode, &epos, eloc, etype, elen, 0);
J
Jan Kara 已提交
156
		if (!epos.bh) {
157
			iinfo->i_lenAlloc =
M
Marcin Slusarz 已提交
158 159
				epos.offset -
				udf_file_entry_alloc_offset(inode);
L
Linus Torvalds 已提交
160
			mark_inode_dirty(inode);
J
Jan Kara 已提交
161
		} else {
162
			struct allocExtDesc *aed =
163
				(struct allocExtDesc *)(epos.bh->b_data);
164
			aed->lengthAllocDescs =
165 166 167
				cpu_to_le32(epos.offset -
					    sizeof(struct allocExtDesc));
			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
M
Marcin Slusarz 已提交
168
			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
J
Jan Kara 已提交
169
				udf_update_tag(epos.bh->b_data, epos.offset);
L
Linus Torvalds 已提交
170
			else
171 172
				udf_update_tag(epos.bh->b_data,
					       sizeof(struct allocExtDesc));
J
Jan Kara 已提交
173
			mark_buffer_dirty_inode(epos.bh, inode);
L
Linus Torvalds 已提交
174 175
		}
	}
J
Jan Kara 已提交
176 177
	/* This inode entry is in-memory only and thus we don't have to mark
	 * the inode dirty */
178
	iinfo->i_lenExtents = lbcount;
J
Jan Kara 已提交
179
	brelse(epos.bh);
L
Linus Torvalds 已提交
180 181
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
static void udf_update_alloc_ext_desc(struct inode *inode,
				      struct extent_position *epos,
				      u32 lenalloc)
{
	struct super_block *sb = inode->i_sb;
	struct udf_sb_info *sbi = UDF_SB(sb);

	struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
	int len = sizeof(struct allocExtDesc);

	aed->lengthAllocDescs =	cpu_to_le32(lenalloc);
	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
		len += lenalloc;

	udf_update_tag(epos->bh->b_data, len);
	mark_buffer_dirty_inode(epos->bh, inode);
}

200
void udf_truncate_extents(struct inode *inode)
L
Linus Torvalds 已提交
201
{
J
Jan Kara 已提交
202
	struct extent_position epos;
203
	kernel_lb_addr eloc, neloc = {};
J
Jan Kara 已提交
204
	uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
L
Linus Torvalds 已提交
205
	int8_t etype;
J
Jan Kara 已提交
206 207
	struct super_block *sb = inode->i_sb;
	sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
208
	loff_t byte_offset;
L
Linus Torvalds 已提交
209
	int adsize;
210
	struct udf_inode_info *iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
211

212
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
L
Linus Torvalds 已提交
213
		adsize = sizeof(short_ad);
214
	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
L
Linus Torvalds 已提交
215 216
		adsize = sizeof(long_ad);
	else
J
Jan Kara 已提交
217
		BUG();
L
Linus Torvalds 已提交
218

J
Jan Kara 已提交
219
	etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
220 221
	byte_offset = (offset << sb->s_blocksize_bits) +
		(inode->i_size & (sb->s_blocksize - 1));
222
	if (etype != -1) {
J
Jan Kara 已提交
223 224 225
		epos.offset -= adsize;
		extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
		epos.offset += adsize;
226
		if (byte_offset)
J
Jan Kara 已提交
227
			lenalloc = epos.offset;
L
Linus Torvalds 已提交
228
		else
J
Jan Kara 已提交
229
			lenalloc = epos.offset - adsize;
L
Linus Torvalds 已提交
230

J
Jan Kara 已提交
231
		if (!epos.bh)
L
Linus Torvalds 已提交
232 233 234 235
			lenalloc -= udf_file_entry_alloc_offset(inode);
		else
			lenalloc -= sizeof(struct allocExtDesc);

M
Marcin Slusarz 已提交
236 237
		while ((etype = udf_current_aext(inode, &epos, &eloc,
						 &elen, 0)) != -1) {
238
			if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
J
Jan Kara 已提交
239
				udf_write_aext(inode, &epos, neloc, nelen, 0);
240
				if (indirect_ext_len) {
J
Jan Kara 已提交
241 242
					/* We managed to free all extents in the
					 * indirect extent - free it too */
243
					BUG_ON(!epos.bh);
244 245
					udf_free_blocks(sb, inode, epos.block,
							0, indirect_ext_len);
246 247 248
				} else if (!epos.bh) {
					iinfo->i_lenAlloc = lenalloc;
					mark_inode_dirty(inode);
249 250 251
				} else
					udf_update_alloc_ext_desc(inode,
							&epos, lenalloc);
J
Jan Kara 已提交
252 253 254
				brelse(epos.bh);
				epos.offset = sizeof(struct allocExtDesc);
				epos.block = eloc;
M
Marcin Slusarz 已提交
255 256
				epos.bh = udf_tread(sb,
						udf_get_lb_pblock(sb, eloc, 0));
L
Linus Torvalds 已提交
257
				if (elen)
M
Marcin Slusarz 已提交
258 259
					indirect_ext_len =
						(elen + sb->s_blocksize - 1) >>
260
						sb->s_blocksize_bits;
L
Linus Torvalds 已提交
261
				else
J
Jan Kara 已提交
262
					indirect_ext_len = 1;
263
			} else {
M
Marcin Slusarz 已提交
264 265
				extent_trunc(inode, &epos, eloc, etype,
					     elen, 0);
J
Jan Kara 已提交
266
				epos.offset += adsize;
L
Linus Torvalds 已提交
267 268 269
			}
		}

270
		if (indirect_ext_len) {
271
			BUG_ON(!epos.bh);
272 273
			udf_free_blocks(sb, inode, epos.block, 0,
					indirect_ext_len);
274 275 276
		} else if (!epos.bh) {
			iinfo->i_lenAlloc = lenalloc;
			mark_inode_dirty(inode);
277 278
		} else
			udf_update_alloc_ext_desc(inode, &epos, lenalloc);
279 280
	} else if (inode->i_size) {
		if (byte_offset) {
J
Jan Kara 已提交
281 282
			kernel_long_ad extent;

283 284 285
			/*
			 *  OK, there is not extent covering inode->i_size and
			 *  no extent above inode->i_size => truncate is
J
Jan Kara 已提交
286
			 *  extending the file by 'offset' blocks.
287
			 */
288
			if ((!epos.bh &&
M
Marcin Slusarz 已提交
289 290 291 292
			     epos.offset ==
					udf_file_entry_alloc_offset(inode)) ||
			    (epos.bh && epos.offset ==
						sizeof(struct allocExtDesc))) {
J
Jan Kara 已提交
293 294 295 296
				/* File has no extents at all or has empty last
				 * indirect extent! Create a fake extent... */
				extent.extLocation.logicalBlockNum = 0;
				extent.extLocation.partitionReferenceNum = 0;
M
Marcin Slusarz 已提交
297 298
				extent.extLength =
					EXT_NOT_RECORDED_NOT_ALLOCATED;
299
			} else {
J
Jan Kara 已提交
300
				epos.offset -= adsize;
J
Jan Kara 已提交
301
				etype = udf_next_aext(inode, &epos,
302 303
						      &extent.extLocation,
						      &extent.extLength, 0);
J
Jan Kara 已提交
304
				extent.extLength |= etype << 30;
L
Linus Torvalds 已提交
305
			}
306
			udf_extend_file(inode, &epos, &extent,
M
Marcin Slusarz 已提交
307 308 309
					offset +
					((inode->i_size &
						(sb->s_blocksize - 1)) != 0));
L
Linus Torvalds 已提交
310 311
		}
	}
312
	iinfo->i_lenExtents = inode->i_size;
L
Linus Torvalds 已提交
313

J
Jan Kara 已提交
314
	brelse(epos.bh);
L
Linus Torvalds 已提交
315
}