2 * linux/fs/ufs/truncate.c
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
10 * linux/fs/ext2/truncate.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise Pascal
15 * Universite Pierre et Marie Curie (Paris VI)
19 * linux/fs/minix/truncate.c
21 * Copyright (C) 1991, 1992 Linus Torvalds
23 * Big-endian to little-endian byte-swapping/bitmaps by
24 * David S. Miller (davem@caip.rutgers.edu), 1995
28 * Real random numbers for secure rm added 94/02/18
29 * Idea from Pierre del Perugia <delperug@gla.ecoledoc.ibp.fr>
33 * Adoptation to use page cache and UFS2 write support by
34 * Evgeniy Dushistov <dushistov@mail.ru>, 2006-2007
37 #include <linux/errno.h>
39 #include <linux/fcntl.h>
40 #include <linux/time.h>
41 #include <linux/stat.h>
42 #include <linux/string.h>
43 #include <linux/buffer_head.h>
44 #include <linux/blkdev.h>
45 #include <linux/sched.h>
53 * Secure deletion currently doesn't work. It interacts very badly
54 * with buffers shared with memory mappings, and for that reason
55 * can't be done in the truncate() routines. It should instead be
56 * done separately in "release()" before calling the truncate routines
57 * that will release the actual file blocks.
62 #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
63 #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
66 static int ufs_trunc_direct(struct inode *inode)
68 struct ufs_inode_info *ufsi = UFS_I(inode);
69 struct super_block * sb;
70 struct ufs_sb_private_info * uspi;
72 u64 frag1, frag2, frag3, frag4, block1, block2;
73 unsigned frag_to_free, free_count;
77 UFSD("ENTER: ino %lu\n", inode->i_ino);
80 uspi = UFS_SB(sb)->s_uspi;
86 frag1 = DIRECT_FRAGMENT;
87 frag4 = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
88 frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
89 frag3 = frag4 & ~uspi->s_fpbmask;
94 } else if (frag2 < frag3) {
95 block1 = ufs_fragstoblks (frag2);
96 block2 = ufs_fragstoblks (frag3);
99 UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
100 " frag3 %llu, frag4 %llu\n", inode->i_ino,
101 (unsigned long long)frag1, (unsigned long long)frag2,
102 (unsigned long long)block1, (unsigned long long)block2,
103 (unsigned long long)frag3, (unsigned long long)frag4);
109 * Free first free fragments
111 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
112 tmp = ufs_data_ptr_to_cpu(sb, p);
114 ufs_panic (sb, "ufs_trunc_direct", "internal error");
116 frag1 = ufs_fragnum (frag1);
118 ufs_free_fragments(inode, tmp + frag1, frag2);
119 mark_inode_dirty(inode);
120 frag_to_free = tmp + frag1;
126 for (i = block1 ; i < block2; i++) {
127 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
128 tmp = ufs_data_ptr_to_cpu(sb, p);
131 ufs_data_ptr_clear(uspi, p);
133 if (free_count == 0) {
135 free_count = uspi->s_fpb;
136 } else if (free_count > 0 && frag_to_free == tmp - free_count)
137 free_count += uspi->s_fpb;
139 ufs_free_blocks (inode, frag_to_free, free_count);
141 free_count = uspi->s_fpb;
143 mark_inode_dirty(inode);
147 ufs_free_blocks (inode, frag_to_free, free_count);
153 * Free last free fragments
155 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
156 tmp = ufs_data_ptr_to_cpu(sb, p);
158 ufs_panic(sb, "ufs_truncate_direct", "internal error");
159 frag4 = ufs_fragnum (frag4);
160 ufs_data_ptr_clear(uspi, p);
162 ufs_free_fragments (inode, tmp, frag4);
163 mark_inode_dirty(inode);
166 UFSD("EXIT: ino %lu\n", inode->i_ino);
171 static int ufs_trunc_indirect(struct inode *inode, u64 offset, void *p)
173 struct super_block * sb;
174 struct ufs_sb_private_info * uspi;
175 struct ufs_buffer_head * ind_ubh;
177 u64 tmp, indirect_block, i, frag_to_free;
181 UFSD("ENTER: ino %lu, offset %llu, p: %p\n",
182 inode->i_ino, (unsigned long long)offset, p);
187 uspi = UFS_SB(sb)->s_uspi;
193 tmp = ufs_data_ptr_to_cpu(sb, p);
196 ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
197 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
198 ubh_brelse (ind_ubh);
202 ufs_data_ptr_clear(uspi, p);
206 indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
207 for (i = indirect_block; i < uspi->s_apb; i++) {
208 ind = ubh_get_data_ptr(uspi, ind_ubh, i);
209 tmp = ufs_data_ptr_to_cpu(sb, ind);
213 ufs_data_ptr_clear(uspi, ind);
214 ubh_mark_buffer_dirty(ind_ubh);
215 if (free_count == 0) {
217 free_count = uspi->s_fpb;
218 } else if (free_count > 0 && frag_to_free == tmp - free_count)
219 free_count += uspi->s_fpb;
221 ufs_free_blocks (inode, frag_to_free, free_count);
223 free_count = uspi->s_fpb;
226 mark_inode_dirty(inode);
229 if (free_count > 0) {
230 ufs_free_blocks (inode, frag_to_free, free_count);
232 for (i = 0; i < uspi->s_apb; i++)
233 if (!ufs_is_data_ptr_zero(uspi,
234 ubh_get_data_ptr(uspi, ind_ubh, i)))
236 if (i >= uspi->s_apb) {
237 tmp = ufs_data_ptr_to_cpu(sb, p);
238 ufs_data_ptr_clear(uspi, p);
240 ufs_free_blocks (inode, tmp, uspi->s_fpb);
241 mark_inode_dirty(inode);
242 ubh_bforget(ind_ubh);
245 if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh))
246 ubh_sync_block(ind_ubh);
247 ubh_brelse (ind_ubh);
249 UFSD("EXIT: ino %lu\n", inode->i_ino);
254 static int ufs_trunc_dindirect(struct inode *inode, u64 offset, void *p)
256 struct super_block * sb;
257 struct ufs_sb_private_info * uspi;
258 struct ufs_buffer_head *dind_bh;
259 u64 i, tmp, dindirect_block;
263 UFSD("ENTER: ino %lu\n", inode->i_ino);
266 uspi = UFS_SB(sb)->s_uspi;
268 dindirect_block = (DIRECT_BLOCK > offset)
269 ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
272 tmp = ufs_data_ptr_to_cpu(sb, p);
275 dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
276 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
277 ubh_brelse (dind_bh);
281 ufs_data_ptr_clear(uspi, p);
285 for (i = dindirect_block ; i < uspi->s_apb ; i++) {
286 dind = ubh_get_data_ptr(uspi, dind_bh, i);
287 tmp = ufs_data_ptr_to_cpu(sb, dind);
290 retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
291 ubh_mark_buffer_dirty(dind_bh);
294 for (i = 0; i < uspi->s_apb; i++)
295 if (!ufs_is_data_ptr_zero(uspi,
296 ubh_get_data_ptr(uspi, dind_bh, i)))
298 if (i >= uspi->s_apb) {
299 tmp = ufs_data_ptr_to_cpu(sb, p);
300 ufs_data_ptr_clear(uspi, p);
302 ufs_free_blocks(inode, tmp, uspi->s_fpb);
303 mark_inode_dirty(inode);
304 ubh_bforget(dind_bh);
307 if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh))
308 ubh_sync_block(dind_bh);
309 ubh_brelse (dind_bh);
311 UFSD("EXIT: ino %lu\n", inode->i_ino);
316 static int ufs_trunc_tindirect(struct inode *inode)
318 struct super_block *sb = inode->i_sb;
319 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
320 struct ufs_inode_info *ufsi = UFS_I(inode);
321 struct ufs_buffer_head * tind_bh;
322 u64 tindirect_block, tmp, i;
326 UFSD("ENTER: ino %lu\n", inode->i_ino);
330 tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
331 ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
333 p = ufs_get_direct_data_ptr(uspi, ufsi, UFS_TIND_BLOCK);
334 if (!(tmp = ufs_data_ptr_to_cpu(sb, p)))
336 tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
337 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
338 ubh_brelse (tind_bh);
342 ufs_data_ptr_clear(uspi, p);
346 for (i = tindirect_block ; i < uspi->s_apb ; i++) {
347 tind = ubh_get_data_ptr(uspi, tind_bh, i);
348 retry |= ufs_trunc_dindirect(inode, UFS_NDADDR +
349 uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
350 ubh_mark_buffer_dirty(tind_bh);
352 for (i = 0; i < uspi->s_apb; i++)
353 if (!ufs_is_data_ptr_zero(uspi,
354 ubh_get_data_ptr(uspi, tind_bh, i)))
356 if (i >= uspi->s_apb) {
357 tmp = ufs_data_ptr_to_cpu(sb, p);
358 ufs_data_ptr_clear(uspi, p);
360 ufs_free_blocks(inode, tmp, uspi->s_fpb);
361 mark_inode_dirty(inode);
362 ubh_bforget(tind_bh);
365 if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh))
366 ubh_sync_block(tind_bh);
367 ubh_brelse (tind_bh);
369 UFSD("EXIT: ino %lu\n", inode->i_ino);
373 static int ufs_alloc_lastblock(struct inode *inode)
376 struct super_block *sb = inode->i_sb;
377 struct address_space *mapping = inode->i_mapping;
378 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
381 struct page *lastpage;
382 struct buffer_head *bh;
385 lastfrag = (i_size_read(inode) + uspi->s_fsize - 1) >> uspi->s_fshift;
392 lastpage = ufs_get_locked_page(mapping, lastfrag >>
393 (PAGE_CACHE_SHIFT - inode->i_blkbits));
394 if (IS_ERR(lastpage)) {
399 end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
400 bh = page_buffers(lastpage);
401 for (i = 0; i < end; ++i)
402 bh = bh->b_this_page;
405 err = ufs_getfrag_block(inode, lastfrag, bh, 1);
410 if (buffer_new(bh)) {
411 clear_buffer_new(bh);
412 unmap_underlying_metadata(bh->b_bdev,
415 * we do not zeroize fragment, because of
416 * if it maped to hole, it already contains zeroes
418 set_buffer_uptodate(bh);
419 mark_buffer_dirty(bh);
420 set_page_dirty(lastpage);
423 if (lastfrag >= UFS_IND_FRAGMENT) {
424 end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
425 phys64 = bh->b_blocknr + 1;
426 for (i = 0; i < end; ++i) {
427 bh = sb_getblk(sb, i + phys64);
429 memset(bh->b_data, 0, sb->s_blocksize);
430 set_buffer_uptodate(bh);
431 mark_buffer_dirty(bh);
433 sync_dirty_buffer(bh);
438 ufs_put_locked_page(lastpage);
443 int ufs_truncate(struct inode *inode, loff_t old_i_size)
445 struct ufs_inode_info *ufsi = UFS_I(inode);
446 struct super_block *sb = inode->i_sb;
447 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
450 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
451 inode->i_ino, (unsigned long long)i_size_read(inode),
452 (unsigned long long)old_i_size);
454 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
455 S_ISLNK(inode->i_mode)))
457 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
460 err = ufs_alloc_lastblock(inode);
463 i_size_write(inode, old_i_size);
467 block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
470 retry = ufs_trunc_direct(inode);
471 retry |= ufs_trunc_indirect(inode, UFS_IND_BLOCK,
472 ufs_get_direct_data_ptr(uspi, ufsi,
474 retry |= ufs_trunc_dindirect(inode, UFS_IND_BLOCK + uspi->s_apb,
475 ufs_get_direct_data_ptr(uspi, ufsi,
477 retry |= ufs_trunc_tindirect (inode);
480 if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
481 ufs_sync_inode (inode);
485 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
486 ufsi->i_lastfrag = DIRECT_FRAGMENT;
487 mark_inode_dirty(inode);
489 UFSD("EXIT: err %d\n", err);
493 int ufs_setattr(struct dentry *dentry, struct iattr *attr)
495 struct inode *inode = dentry->d_inode;
496 unsigned int ia_valid = attr->ia_valid;
499 error = inode_change_ok(inode, attr);
503 if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
504 loff_t old_i_size = inode->i_size;
506 /* XXX(truncate): truncate_setsize should be called last */
507 truncate_setsize(inode, attr->ia_size);
509 lock_ufs(inode->i_sb);
510 error = ufs_truncate(inode, old_i_size);
511 unlock_ufs(inode->i_sb);
516 setattr_copy(inode, attr);
517 mark_inode_dirty(inode);
521 const struct inode_operations ufs_file_inode_operations = {
522 .setattr = ufs_setattr,