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 * Modified to avoid infinite loop on 2006 by
34 * Evgeniy Dushistov <dushistov@mail.ru>
37 #include <linux/errno.h>
39 #include <linux/ufs_fs.h>
40 #include <linux/fcntl.h>
41 #include <linux/time.h>
42 #include <linux/stat.h>
43 #include <linux/string.h>
44 #include <linux/smp_lock.h>
45 #include <linux/buffer_head.h>
46 #include <linux/blkdev.h>
47 #include <linux/sched.h>
52 #undef UFS_TRUNCATE_DEBUG
54 #ifdef UFS_TRUNCATE_DEBUG
55 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
61 * Secure deletion currently doesn't work. It interacts very badly
62 * with buffers shared with memory mappings, and for that reason
63 * can't be done in the truncate() routines. It should instead be
64 * done separately in "release()" before calling the truncate routines
65 * that will release the actual file blocks.
70 #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
71 #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
74 static int ufs_trunc_direct (struct inode * inode)
76 struct ufs_inode_info *ufsi = UFS_I(inode);
77 struct super_block * sb;
78 struct ufs_sb_private_info * uspi;
80 unsigned frag1, frag2, frag3, frag4, block1, block2;
81 unsigned frag_to_free, free_count;
88 uspi = UFS_SB(sb)->s_uspi;
94 frag1 = DIRECT_FRAGMENT;
95 frag4 = min_t(u32, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
96 frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
97 frag3 = frag4 & ~uspi->s_fpbmask;
103 else if (frag2 < frag3) {
104 block1 = ufs_fragstoblks (frag2);
105 block2 = ufs_fragstoblks (frag3);
108 UFSD(("frag1 %u, frag2 %u, block1 %u, block2 %u, frag3 %u, frag4 %u\n", frag1, frag2, block1, block2, frag3, frag4))
114 * Free first free fragments
116 p = ufsi->i_u1.i_data + ufs_fragstoblks (frag1);
117 tmp = fs32_to_cpu(sb, *p);
119 ufs_panic (sb, "ufs_trunc_direct", "internal error");
120 frag1 = ufs_fragnum (frag1);
121 frag2 = ufs_fragnum (frag2);
123 inode->i_blocks -= (frag2-frag1) << uspi->s_nspfshift;
124 mark_inode_dirty(inode);
125 ufs_free_fragments (inode, tmp + frag1, frag2 - frag1);
126 frag_to_free = tmp + frag1;
132 for (i = block1 ; i < block2; i++) {
133 p = ufsi->i_u1.i_data + i;
134 tmp = fs32_to_cpu(sb, *p);
139 inode->i_blocks -= uspi->s_nspb;
140 mark_inode_dirty(inode);
141 if (free_count == 0) {
143 free_count = uspi->s_fpb;
144 } else if (free_count > 0 && frag_to_free == tmp - free_count)
145 free_count += uspi->s_fpb;
147 ufs_free_blocks (inode, frag_to_free, free_count);
149 free_count = uspi->s_fpb;
154 ufs_free_blocks (inode, frag_to_free, free_count);
160 * Free last free fragments
162 p = ufsi->i_u1.i_data + ufs_fragstoblks (frag3);
163 tmp = fs32_to_cpu(sb, *p);
165 ufs_panic(sb, "ufs_truncate_direct", "internal error");
166 frag4 = ufs_fragnum (frag4);
169 inode->i_blocks -= frag4 << uspi->s_nspfshift;
170 mark_inode_dirty(inode);
171 ufs_free_fragments (inode, tmp, frag4);
179 static int ufs_trunc_indirect (struct inode * inode, unsigned offset, __fs32 *p)
181 struct super_block * sb;
182 struct ufs_sb_private_info * uspi;
183 struct ufs_buffer_head * ind_ubh;
185 unsigned indirect_block, i, tmp;
186 unsigned frag_to_free, free_count;
192 uspi = UFS_SB(sb)->s_uspi;
198 tmp = fs32_to_cpu(sb, *p);
201 ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
202 if (tmp != fs32_to_cpu(sb, *p)) {
203 ubh_brelse (ind_ubh);
211 indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
212 for (i = indirect_block; i < uspi->s_apb; i++) {
213 ind = ubh_get_addr32 (ind_ubh, i);
214 tmp = fs32_to_cpu(sb, *ind);
219 ubh_mark_buffer_dirty(ind_ubh);
220 if (free_count == 0) {
222 free_count = uspi->s_fpb;
223 } else if (free_count > 0 && frag_to_free == tmp - free_count)
224 free_count += uspi->s_fpb;
226 ufs_free_blocks (inode, frag_to_free, free_count);
228 free_count = uspi->s_fpb;
230 inode->i_blocks -= uspi->s_nspb;
231 mark_inode_dirty(inode);
234 if (free_count > 0) {
235 ufs_free_blocks (inode, frag_to_free, free_count);
237 for (i = 0; i < uspi->s_apb; i++)
238 if (*ubh_get_addr32(ind_ubh,i))
240 if (i >= uspi->s_apb) {
241 if (ubh_max_bcount(ind_ubh) != 1) {
245 tmp = fs32_to_cpu(sb, *p);
247 inode->i_blocks -= uspi->s_nspb;
248 mark_inode_dirty(inode);
249 ufs_free_blocks (inode, tmp, uspi->s_fpb);
250 ubh_bforget(ind_ubh);
254 if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) {
255 ubh_ll_rw_block (SWRITE, 1, &ind_ubh);
256 ubh_wait_on_buffer (ind_ubh);
258 ubh_brelse (ind_ubh);
265 static int ufs_trunc_dindirect (struct inode *inode, unsigned offset, __fs32 *p)
267 struct super_block * sb;
268 struct ufs_sb_private_info * uspi;
269 struct ufs_buffer_head * dind_bh;
270 unsigned i, tmp, dindirect_block;
277 uspi = UFS_SB(sb)->s_uspi;
279 dindirect_block = (DIRECT_BLOCK > offset)
280 ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
283 tmp = fs32_to_cpu(sb, *p);
286 dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
287 if (tmp != fs32_to_cpu(sb, *p)) {
288 ubh_brelse (dind_bh);
296 for (i = dindirect_block ; i < uspi->s_apb ; i++) {
297 dind = ubh_get_addr32 (dind_bh, i);
298 tmp = fs32_to_cpu(sb, *dind);
301 retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
302 ubh_mark_buffer_dirty(dind_bh);
305 for (i = 0; i < uspi->s_apb; i++)
306 if (*ubh_get_addr32 (dind_bh, i))
308 if (i >= uspi->s_apb) {
309 if (ubh_max_bcount(dind_bh) != 1)
312 tmp = fs32_to_cpu(sb, *p);
314 inode->i_blocks -= uspi->s_nspb;
315 mark_inode_dirty(inode);
316 ufs_free_blocks (inode, tmp, uspi->s_fpb);
317 ubh_bforget(dind_bh);
321 if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) {
322 ubh_ll_rw_block (SWRITE, 1, &dind_bh);
323 ubh_wait_on_buffer (dind_bh);
325 ubh_brelse (dind_bh);
332 static int ufs_trunc_tindirect (struct inode * inode)
334 struct ufs_inode_info *ufsi = UFS_I(inode);
335 struct super_block * sb;
336 struct ufs_sb_private_info * uspi;
337 struct ufs_buffer_head * tind_bh;
338 unsigned tindirect_block, tmp, i;
345 uspi = UFS_SB(sb)->s_uspi;
348 tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
349 ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
350 p = ufsi->i_u1.i_data + UFS_TIND_BLOCK;
351 if (!(tmp = fs32_to_cpu(sb, *p)))
353 tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
354 if (tmp != fs32_to_cpu(sb, *p)) {
355 ubh_brelse (tind_bh);
363 for (i = tindirect_block ; i < uspi->s_apb ; i++) {
364 tind = ubh_get_addr32 (tind_bh, i);
365 retry |= ufs_trunc_dindirect(inode, UFS_NDADDR +
366 uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
367 ubh_mark_buffer_dirty(tind_bh);
369 for (i = 0; i < uspi->s_apb; i++)
370 if (*ubh_get_addr32 (tind_bh, i))
372 if (i >= uspi->s_apb) {
373 if (ubh_max_bcount(tind_bh) != 1)
376 tmp = fs32_to_cpu(sb, *p);
378 inode->i_blocks -= uspi->s_nspb;
379 mark_inode_dirty(inode);
380 ufs_free_blocks (inode, tmp, uspi->s_fpb);
381 ubh_bforget(tind_bh);
385 if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) {
386 ubh_ll_rw_block (SWRITE, 1, &tind_bh);
387 ubh_wait_on_buffer (tind_bh);
389 ubh_brelse (tind_bh);
395 void ufs_truncate (struct inode * inode)
397 struct ufs_inode_info *ufsi = UFS_I(inode);
398 struct super_block * sb;
399 struct ufs_sb_private_info * uspi;
404 uspi = UFS_SB(sb)->s_uspi;
406 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
408 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
411 block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
415 retry = ufs_trunc_direct(inode);
416 retry |= ufs_trunc_indirect (inode, UFS_IND_BLOCK,
417 (__fs32 *) &ufsi->i_u1.i_data[UFS_IND_BLOCK]);
418 retry |= ufs_trunc_dindirect (inode, UFS_IND_BLOCK + uspi->s_apb,
419 (__fs32 *) &ufsi->i_u1.i_data[UFS_DIND_BLOCK]);
420 retry |= ufs_trunc_tindirect (inode);
423 if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
424 ufs_sync_inode (inode);
425 blk_run_address_space(inode->i_mapping);
429 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
430 ufsi->i_lastfrag = DIRECT_FRAGMENT;
432 mark_inode_dirty(inode);