truncate.c 13.3 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
#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>
47
#include <linux/quotaops.h>
L
Linus Torvalds 已提交
48

49
#include "ufs_fs.h"
50
#include "ufs.h"
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#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)


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

	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;
96
	} else if (frag2 < frag3) {
L
Linus Torvalds 已提交
97 98 99 100
		block1 = ufs_fragstoblks (frag2);
		block2 = ufs_fragstoblks (frag3);
	}

101 102
	UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
	     " frag3 %llu, frag4 %llu\n", inode->i_ino,
103 104 105
	     (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 已提交
106 107 108 109 110 111 112

	if (frag1 >= frag2)
		goto next1;		

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

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

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

L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144
		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;
		}
145
		mark_inode_dirty(inode);
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156
	}
	
	if (free_count > 0)
		ufs_free_blocks (inode, frag_to_free, free_count);

	if (frag3 >= frag4)
		goto next3;

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

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

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


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

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

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

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

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

215
		ufs_data_ptr_clear(uspi, ind);
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225 226
		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;
		}
227

L
Linus Torvalds 已提交
228 229 230 231 232 233 234
		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++)
235 236
		if (!ufs_is_data_ptr_zero(uspi,
					  ubh_get_data_ptr(uspi, ind_ubh, i)))
L
Linus Torvalds 已提交
237 238
			break;
	if (i >= uspi->s_apb) {
239 240
		tmp = ufs_data_ptr_to_cpu(sb, p);
		ufs_data_ptr_clear(uspi, p);
241

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

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

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

	for (i = dindirect_block ; i < uspi->s_apb ; i++) {
290 291
		dind = ubh_get_data_ptr(uspi, dind_bh, i);
		tmp = ufs_data_ptr_to_cpu(sb, dind);
L
Linus Torvalds 已提交
292 293 294 295 296 297 298
		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++)
299 300
		if (!ufs_is_data_ptr_zero(uspi,
					  ubh_get_data_ptr(uspi, dind_bh, i)))
L
Linus Torvalds 已提交
301 302
			break;
	if (i >= uspi->s_apb) {
303 304
		tmp = ufs_data_ptr_to_cpu(sb, p);
		ufs_data_ptr_clear(uspi, p);
305 306

		ufs_free_blocks(inode, tmp, uspi->s_fpb);
307 308 309
		mark_inode_dirty(inode);
		ubh_bforget(dind_bh);
		dind_bh = NULL;
L
Linus Torvalds 已提交
310 311
	}
	if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) {
312
		ubh_ll_rw_block(SWRITE, dind_bh);
L
Linus Torvalds 已提交
313 314 315 316
		ubh_wait_on_buffer (dind_bh);
	}
	ubh_brelse (dind_bh);
	
317
	UFSD("EXIT: ino %lu\n", inode->i_ino);
L
Linus Torvalds 已提交
318 319 320 321
	
	return retry;
}

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

	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;
338 339 340

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

	for (i = tindirect_block ; i < uspi->s_apb ; i++) {
353
		tind = ubh_get_data_ptr(uspi, tind_bh, i);
L
Linus Torvalds 已提交
354 355 356 357 358
		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++)
359 360
		if (!ufs_is_data_ptr_zero(uspi,
					  ubh_get_data_ptr(uspi, tind_bh, i)))
L
Linus Torvalds 已提交
361 362
			break;
	if (i >= uspi->s_apb) {
363 364
		tmp = ufs_data_ptr_to_cpu(sb, p);
		ufs_data_ptr_clear(uspi, p);
365 366

		ufs_free_blocks(inode, tmp, uspi->s_fpb);
367 368 369
		mark_inode_dirty(inode);
		ubh_bforget(tind_bh);
		tind_bh = NULL;
L
Linus Torvalds 已提交
370 371
	}
	if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) {
372
		ubh_ll_rw_block(SWRITE, tind_bh);
L
Linus Torvalds 已提交
373 374 375 376
		ubh_wait_on_buffer (tind_bh);
	}
	ubh_brelse (tind_bh);
	
377
	UFSD("EXIT: ino %lu\n", inode->i_ino);
L
Linus Torvalds 已提交
378 379
	return retry;
}
380 381

static int ufs_alloc_lastblock(struct inode *inode)
L
Linus Torvalds 已提交
382
{
383
	int err = 0;
384
	struct super_block *sb = inode->i_sb;
385
	struct address_space *mapping = inode->i_mapping;
386
	struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
387 388
	unsigned i, end;
	sector_t lastfrag;
389 390
	struct page *lastpage;
	struct buffer_head *bh;
391
	u64 phys64;
392 393 394

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

395
	if (!lastfrag)
396
		goto out;
397

398 399 400 401 402 403 404 405 406 407 408 409 410 411
	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;

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

       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);
429
       }
430

431 432 433 434 435 436 437 438 439 440 441 442 443 444
       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);
	       }
       }
445 446 447 448 449 450 451 452 453 454 455 456
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 已提交
457
	
458 459 460
	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 已提交
461

462 463 464
	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
	      S_ISLNK(inode->i_mode)))
		return -EINVAL;
L
Linus Torvalds 已提交
465
	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
466 467
		return -EPERM;

468
	err = ufs_alloc_lastblock(inode);
469

470 471 472
	if (err) {
		i_size_write(inode, old_i_size);
		goto out;
473
	}
474

475
	block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
476

L
Linus Torvalds 已提交
477 478 479
	lock_kernel();
	while (1) {
		retry = ufs_trunc_direct(inode);
480 481 482 483 484 485
		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 已提交
486 487 488 489 490 491 492 493
		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();
	}
494

L
Linus Torvalds 已提交
495
	inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
496
	ufsi->i_lastfrag = DIRECT_FRAGMENT;
L
Linus Torvalds 已提交
497 498
	unlock_kernel();
	mark_inode_dirty(inode);
499 500 501
out:
	UFSD("EXIT: err %d\n", err);
	return err;
L
Linus Torvalds 已提交
502
}
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520


/*
 * We don't define our `inode->i_op->truncate', and call it here,
 * because of:
 * - there is no way to know old size
 * - there is no way inform user about error, if it happens in `truncate'
 */
static int ufs_setattr(struct dentry *dentry, struct iattr *attr)
{
	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;

521 522
	if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
	    (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
523
		error = dquot_transfer(inode, attr);
524 525 526
		if (error)
			return error;
	}
527 528 529
	if (ia_valid & ATTR_SIZE &&
	    attr->ia_size != i_size_read(inode)) {
		loff_t old_i_size = inode->i_size;
530

531
		dquot_initialize(inode);
532

533 534 535 536 537 538 539 540 541 542
		error = vmtruncate(inode, attr->ia_size);
		if (error)
			return error;
		error = ufs_truncate(inode, old_i_size);
		if (error)
			return error;
	}
	return inode_setattr(inode, attr);
}

543
const struct inode_operations ufs_file_inode_operations = {
544 545
	.setattr = ufs_setattr,
};