truncate.c 12.8 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 30 31
/*
 *  linux/fs/ufs/truncate.c
 *
 * Copyright (C) 1998
 * Daniel Pirkl <daniel.pirkl@email.cz>
 * Charles University, Faculty of Mathematics and Physics
 *
 *  from
 *
 *  linux/fs/ext2/truncate.c
 *
 * Copyright (C) 1992, 1993, 1994, 1995
 * Remy Card (card@masi.ibp.fr)
 * Laboratoire MASI - Institut Blaise Pascal
 * Universite Pierre et Marie Curie (Paris VI)
 *
 *  from
 *
 *  linux/fs/minix/truncate.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  Big-endian to little-endian byte-swapping/bitmaps by
 *        David S. Miller (davem@caip.rutgers.edu), 1995
 */

/*
 * Real random numbers for secure rm added 94/02/18
 * Idea from Pierre del Perugia <delperug@gla.ecoledoc.ibp.fr>
 */

32
/*
33 34
 * Adoptation to use page cache and UFS2 write support by
 * Evgeniy Dushistov <dushistov@mail.ru>, 2006-2007
35 36
 */

L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/time.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/smp_lock.h>
#include <linux/buffer_head.h>
#include <linux/blkdev.h>
#include <linux/sched.h>

48
#include "ufs_fs.h"
49
#include "ufs.h"
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "swab.h"
#include "util.h"

/*
 * Secure deletion currently doesn't work. It interacts very badly
 * with buffers shared with memory mappings, and for that reason
 * can't be done in the truncate() routines. It should instead be
 * done separately in "release()" before calling the truncate routines
 * that will release the actual file blocks.
 *
 *		Linus
 */

#define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
#define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)


67
static int ufs_trunc_direct(struct inode *inode)
L
Linus Torvalds 已提交
68 69 70 71
{
	struct ufs_inode_info *ufsi = UFS_I(inode);
	struct super_block * sb;
	struct ufs_sb_private_info * uspi;
72 73
	void *p;
	u64 frag1, frag2, frag3, frag4, block1, block2;
L
Linus Torvalds 已提交
74
	unsigned frag_to_free, free_count;
75
	unsigned i, tmp;
L
Linus Torvalds 已提交
76 77
	int retry;
	
78
	UFSD("ENTER: ino %lu\n", inode->i_ino);
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

	sb = inode->i_sb;
	uspi = UFS_SB(sb)->s_uspi;
	
	frag_to_free = 0;
	free_count = 0;
	retry = 0;
	
	frag1 = DIRECT_FRAGMENT;
	frag4 = min_t(u32, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
	frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
	frag3 = frag4 & ~uspi->s_fpbmask;
	block1 = block2 = 0;
	if (frag2 > frag3) {
		frag2 = frag4;
		frag3 = frag4 = 0;
95
	} else if (frag2 < frag3) {
L
Linus Torvalds 已提交
96 97 98 99
		block1 = ufs_fragstoblks (frag2);
		block2 = ufs_fragstoblks (frag3);
	}

100 101
	UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
	     " frag3 %llu, frag4 %llu\n", inode->i_ino,
102 103 104
	     (unsigned long long)frag1, (unsigned long long)frag2,
	     (unsigned long long)block1, (unsigned long long)block2,
	     (unsigned long long)frag3, (unsigned long long)frag4);
L
Linus Torvalds 已提交
105 106 107 108 109 110 111

	if (frag1 >= frag2)
		goto next1;		

	/*
	 * Free first free fragments
	 */
112 113
	p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
	tmp = ufs_data_ptr_to_cpu(sb, p);
L
Linus Torvalds 已提交
114 115
	if (!tmp )
		ufs_panic (sb, "ufs_trunc_direct", "internal error");
116
	frag2 -= frag1;
L
Linus Torvalds 已提交
117
	frag1 = ufs_fragnum (frag1);
118

119
	ufs_free_fragments(inode, tmp + frag1, frag2);
120
	mark_inode_dirty(inode);
L
Linus Torvalds 已提交
121 122 123 124 125 126 127
	frag_to_free = tmp + frag1;

next1:
	/*
	 * Free whole blocks
	 */
	for (i = block1 ; i < block2; i++) {
128 129
		p = ufs_get_direct_data_ptr(uspi, ufsi, i);
		tmp = ufs_data_ptr_to_cpu(sb, p);
L
Linus Torvalds 已提交
130 131
		if (!tmp)
			continue;
132
		ufs_data_ptr_clear(uspi, p);
133

L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141 142 143
		if (free_count == 0) {
			frag_to_free = tmp;
			free_count = uspi->s_fpb;
		} else if (free_count > 0 && frag_to_free == tmp - free_count)
			free_count += uspi->s_fpb;
		else {
			ufs_free_blocks (inode, frag_to_free, free_count);
			frag_to_free = tmp;
			free_count = uspi->s_fpb;
		}
144
		mark_inode_dirty(inode);
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153 154 155
	}
	
	if (free_count > 0)
		ufs_free_blocks (inode, frag_to_free, free_count);

	if (frag3 >= frag4)
		goto next3;

	/*
	 * Free last free fragments
	 */
156 157
	p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
	tmp = ufs_data_ptr_to_cpu(sb, p);
L
Linus Torvalds 已提交
158 159 160
	if (!tmp )
		ufs_panic(sb, "ufs_truncate_direct", "internal error");
	frag4 = ufs_fragnum (frag4);
161
	ufs_data_ptr_clear(uspi, p);
162

L
Linus Torvalds 已提交
163
	ufs_free_fragments (inode, tmp, frag4);
164
	mark_inode_dirty(inode);
L
Linus Torvalds 已提交
165 166
 next3:

167
	UFSD("EXIT: ino %lu\n", inode->i_ino);
L
Linus Torvalds 已提交
168 169 170 171
	return retry;
}


172
static int ufs_trunc_indirect(struct inode *inode, u64 offset, void *p)
L
Linus Torvalds 已提交
173 174 175 176
{
	struct super_block * sb;
	struct ufs_sb_private_info * uspi;
	struct ufs_buffer_head * ind_ubh;
177 178 179
	void *ind;
	u64 tmp, indirect_block, i, frag_to_free;
	unsigned free_count;
L
Linus Torvalds 已提交
180 181
	int retry;

182 183 184 185
	UFSD("ENTER: ino %lu, offset %llu, p: %p\n",
	     inode->i_ino, (unsigned long long)offset, p);

	BUG_ON(!p);
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193
		
	sb = inode->i_sb;
	uspi = UFS_SB(sb)->s_uspi;

	frag_to_free = 0;
	free_count = 0;
	retry = 0;
	
194
	tmp = ufs_data_ptr_to_cpu(sb, p);
L
Linus Torvalds 已提交
195 196 197
	if (!tmp)
		return 0;
	ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
198
	if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
L
Linus Torvalds 已提交
199 200 201 202
		ubh_brelse (ind_ubh);
		return 1;
	}
	if (!ind_ubh) {
203
		ufs_data_ptr_clear(uspi, p);
L
Linus Torvalds 已提交
204 205 206 207 208
		return 0;
	}

	indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
	for (i = indirect_block; i < uspi->s_apb; i++) {
209 210
		ind = ubh_get_data_ptr(uspi, ind_ubh, i);
		tmp = ufs_data_ptr_to_cpu(sb, ind);
L
Linus Torvalds 已提交
211 212
		if (!tmp)
			continue;
213

214
		ufs_data_ptr_clear(uspi, ind);
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222 223 224 225
		ubh_mark_buffer_dirty(ind_ubh);
		if (free_count == 0) {
			frag_to_free = tmp;
			free_count = uspi->s_fpb;
		} else if (free_count > 0 && frag_to_free == tmp - free_count)
			free_count += uspi->s_fpb;
		else {
			ufs_free_blocks (inode, frag_to_free, free_count);
			frag_to_free = tmp;
			free_count = uspi->s_fpb;
		}
226

L
Linus Torvalds 已提交
227 228 229 230 231 232 233
		mark_inode_dirty(inode);
	}

	if (free_count > 0) {
		ufs_free_blocks (inode, frag_to_free, free_count);
	}
	for (i = 0; i < uspi->s_apb; i++)
234 235
		if (!ufs_is_data_ptr_zero(uspi,
					  ubh_get_data_ptr(uspi, ind_ubh, i)))
L
Linus Torvalds 已提交
236 237
			break;
	if (i >= uspi->s_apb) {
238 239
		tmp = ufs_data_ptr_to_cpu(sb, p);
		ufs_data_ptr_clear(uspi, p);
240

241
		ufs_free_blocks (inode, tmp, uspi->s_fpb);
242
		mark_inode_dirty(inode);
243 244
		ubh_bforget(ind_ubh);
		ind_ubh = NULL;
L
Linus Torvalds 已提交
245
	}
C
Christoph Hellwig 已提交
246 247
	if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh))
		ubh_sync_block(ind_ubh);
L
Linus Torvalds 已提交
248 249
	ubh_brelse (ind_ubh);
	
250
	UFSD("EXIT: ino %lu\n", inode->i_ino);
L
Linus Torvalds 已提交
251 252 253 254
	
	return retry;
}

255
static int ufs_trunc_dindirect(struct inode *inode, u64 offset, void *p)
L
Linus Torvalds 已提交
256 257 258
{
	struct super_block * sb;
	struct ufs_sb_private_info * uspi;
259 260 261
	struct ufs_buffer_head *dind_bh;
	u64 i, tmp, dindirect_block;
	void *dind;
L
Linus Torvalds 已提交
262 263
	int retry = 0;
	
264
	UFSD("ENTER: ino %lu\n", inode->i_ino);
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272
	
	sb = inode->i_sb;
	uspi = UFS_SB(sb)->s_uspi;

	dindirect_block = (DIRECT_BLOCK > offset) 
		? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
	retry = 0;
	
273
	tmp = ufs_data_ptr_to_cpu(sb, p);
L
Linus Torvalds 已提交
274 275 276
	if (!tmp)
		return 0;
	dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
277
	if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
L
Linus Torvalds 已提交
278 279 280 281
		ubh_brelse (dind_bh);
		return 1;
	}
	if (!dind_bh) {
282
		ufs_data_ptr_clear(uspi, p);
L
Linus Torvalds 已提交
283 284 285 286
		return 0;
	}

	for (i = dindirect_block ; i < uspi->s_apb ; i++) {
287 288
		dind = ubh_get_data_ptr(uspi, dind_bh, i);
		tmp = ufs_data_ptr_to_cpu(sb, dind);
L
Linus Torvalds 已提交
289 290 291 292 293 294 295
		if (!tmp)
			continue;
		retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
		ubh_mark_buffer_dirty(dind_bh);
	}

	for (i = 0; i < uspi->s_apb; i++)
296 297
		if (!ufs_is_data_ptr_zero(uspi,
					  ubh_get_data_ptr(uspi, dind_bh, i)))
L
Linus Torvalds 已提交
298 299
			break;
	if (i >= uspi->s_apb) {
300 301
		tmp = ufs_data_ptr_to_cpu(sb, p);
		ufs_data_ptr_clear(uspi, p);
302 303

		ufs_free_blocks(inode, tmp, uspi->s_fpb);
304 305 306
		mark_inode_dirty(inode);
		ubh_bforget(dind_bh);
		dind_bh = NULL;
L
Linus Torvalds 已提交
307
	}
C
Christoph Hellwig 已提交
308 309
	if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh))
		ubh_sync_block(dind_bh);
L
Linus Torvalds 已提交
310 311
	ubh_brelse (dind_bh);
	
312
	UFSD("EXIT: ino %lu\n", inode->i_ino);
L
Linus Torvalds 已提交
313 314 315 316
	
	return retry;
}

317
static int ufs_trunc_tindirect(struct inode *inode)
L
Linus Torvalds 已提交
318
{
319 320
	struct super_block *sb = inode->i_sb;
	struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
L
Linus Torvalds 已提交
321 322
	struct ufs_inode_info *ufsi = UFS_I(inode);
	struct ufs_buffer_head * tind_bh;
323 324
	u64 tindirect_block, tmp, i;
	void *tind, *p;
L
Linus Torvalds 已提交
325 326
	int retry;
	
327
	UFSD("ENTER: ino %lu\n", inode->i_ino);
L
Linus Torvalds 已提交
328 329 330 331 332

	retry = 0;
	
	tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
		? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
333 334 335

	p = ufs_get_direct_data_ptr(uspi, ufsi, UFS_TIND_BLOCK);
	if (!(tmp = ufs_data_ptr_to_cpu(sb, p)))
L
Linus Torvalds 已提交
336 337
		return 0;
	tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
338
	if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
L
Linus Torvalds 已提交
339 340 341 342
		ubh_brelse (tind_bh);
		return 1;
	}
	if (!tind_bh) {
343
		ufs_data_ptr_clear(uspi, p);
L
Linus Torvalds 已提交
344 345 346 347
		return 0;
	}

	for (i = tindirect_block ; i < uspi->s_apb ; i++) {
348
		tind = ubh_get_data_ptr(uspi, tind_bh, i);
L
Linus Torvalds 已提交
349 350 351 352 353
		retry |= ufs_trunc_dindirect(inode, UFS_NDADDR + 
			uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
		ubh_mark_buffer_dirty(tind_bh);
	}
	for (i = 0; i < uspi->s_apb; i++)
354 355
		if (!ufs_is_data_ptr_zero(uspi,
					  ubh_get_data_ptr(uspi, tind_bh, i)))
L
Linus Torvalds 已提交
356 357
			break;
	if (i >= uspi->s_apb) {
358 359
		tmp = ufs_data_ptr_to_cpu(sb, p);
		ufs_data_ptr_clear(uspi, p);
360 361

		ufs_free_blocks(inode, tmp, uspi->s_fpb);
362 363 364
		mark_inode_dirty(inode);
		ubh_bforget(tind_bh);
		tind_bh = NULL;
L
Linus Torvalds 已提交
365
	}
C
Christoph Hellwig 已提交
366 367
	if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh))
		ubh_sync_block(tind_bh);
L
Linus Torvalds 已提交
368 369
	ubh_brelse (tind_bh);
	
370
	UFSD("EXIT: ino %lu\n", inode->i_ino);
L
Linus Torvalds 已提交
371 372
	return retry;
}
373 374

static int ufs_alloc_lastblock(struct inode *inode)
L
Linus Torvalds 已提交
375
{
376
	int err = 0;
377
	struct super_block *sb = inode->i_sb;
378
	struct address_space *mapping = inode->i_mapping;
379
	struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
380 381
	unsigned i, end;
	sector_t lastfrag;
382 383
	struct page *lastpage;
	struct buffer_head *bh;
384
	u64 phys64;
385 386 387

	lastfrag = (i_size_read(inode) + uspi->s_fsize - 1) >> uspi->s_fshift;

388
	if (!lastfrag)
389
		goto out;
390

391 392 393 394 395 396 397 398 399 400 401 402 403 404
	lastfrag--;

	lastpage = ufs_get_locked_page(mapping, lastfrag >>
				       (PAGE_CACHE_SHIFT - inode->i_blkbits));
       if (IS_ERR(lastpage)) {
               err = -EIO;
               goto out;
       }

       end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
       bh = page_buffers(lastpage);
       for (i = 0; i < end; ++i)
               bh = bh->b_this_page;

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

       err = ufs_getfrag_block(inode, lastfrag, bh, 1);

       if (unlikely(err))
	       goto out_unlock;

       if (buffer_new(bh)) {
	       clear_buffer_new(bh);
	       unmap_underlying_metadata(bh->b_bdev,
					 bh->b_blocknr);
	       /*
		* we do not zeroize fragment, because of
		* if it maped to hole, it already contains zeroes
		*/
	       set_buffer_uptodate(bh);
	       mark_buffer_dirty(bh);
	       set_page_dirty(lastpage);
422
       }
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437
       if (lastfrag >= UFS_IND_FRAGMENT) {
	       end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
	       phys64 = bh->b_blocknr + 1;
	       for (i = 0; i < end; ++i) {
		       bh = sb_getblk(sb, i + phys64);
		       lock_buffer(bh);
		       memset(bh->b_data, 0, sb->s_blocksize);
		       set_buffer_uptodate(bh);
		       mark_buffer_dirty(bh);
		       unlock_buffer(bh);
		       sync_dirty_buffer(bh);
		       brelse(bh);
	       }
       }
438 439 440 441 442 443 444 445 446 447 448 449
out_unlock:
       ufs_put_locked_page(lastpage);
out:
       return err;
}

int ufs_truncate(struct inode *inode, loff_t old_i_size)
{
	struct ufs_inode_info *ufsi = UFS_I(inode);
	struct super_block *sb = inode->i_sb;
	struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
	int retry, err = 0;
L
Linus Torvalds 已提交
450
	
451 452 453
	UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
	     inode->i_ino, (unsigned long long)i_size_read(inode),
	     (unsigned long long)old_i_size);
L
Linus Torvalds 已提交
454

455 456 457
	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
	      S_ISLNK(inode->i_mode)))
		return -EINVAL;
L
Linus Torvalds 已提交
458
	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
459 460
		return -EPERM;

461
	err = ufs_alloc_lastblock(inode);
462

463 464 465
	if (err) {
		i_size_write(inode, old_i_size);
		goto out;
466
	}
467

468
	block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
469

L
Linus Torvalds 已提交
470 471 472
	lock_kernel();
	while (1) {
		retry = ufs_trunc_direct(inode);
473 474 475 476 477 478
		retry |= ufs_trunc_indirect(inode, UFS_IND_BLOCK,
					    ufs_get_direct_data_ptr(uspi, ufsi,
								    UFS_IND_BLOCK));
		retry |= ufs_trunc_dindirect(inode, UFS_IND_BLOCK + uspi->s_apb,
					     ufs_get_direct_data_ptr(uspi, ufsi,
								     UFS_DIND_BLOCK));
L
Linus Torvalds 已提交
479 480 481 482 483 484 485 486
		retry |= ufs_trunc_tindirect (inode);
		if (!retry)
			break;
		if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
			ufs_sync_inode (inode);
		blk_run_address_space(inode->i_mapping);
		yield();
	}
487

L
Linus Torvalds 已提交
488
	inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
489
	ufsi->i_lastfrag = DIRECT_FRAGMENT;
L
Linus Torvalds 已提交
490 491
	unlock_kernel();
	mark_inode_dirty(inode);
492 493 494
out:
	UFSD("EXIT: err %d\n", err);
	return err;
L
Linus Torvalds 已提交
495
}
496

497
int ufs_setattr(struct dentry *dentry, struct iattr *attr)
498 499 500 501 502 503 504 505 506
{
	struct inode *inode = dentry->d_inode;
	unsigned int ia_valid = attr->ia_valid;
	int error;

	error = inode_change_ok(inode, attr);
	if (error)
		return error;

507
	if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
508
		loff_t old_i_size = inode->i_size;
509

510 511 512
		/* XXX(truncate): truncate_setsize should be called last */
		truncate_setsize(inode, attr->ia_size);

513 514 515 516
		error = ufs_truncate(inode, old_i_size);
		if (error)
			return error;
	}
C
Christoph Hellwig 已提交
517 518 519 520

	setattr_copy(inode, attr);
	mark_inode_dirty(inode);
	return 0;
521 522
}

523
const struct inode_operations ufs_file_inode_operations = {
524 525
	.setattr = ufs_setattr,
};