[PATCH] ufs: fix hang during `rm'
[linux-2.6.git] / fs / ufs / truncate.c
1 /*
2  *  linux/fs/ufs/truncate.c
3  *
4  * Copyright (C) 1998
5  * Daniel Pirkl <daniel.pirkl@email.cz>
6  * Charles University, Faculty of Mathematics and Physics
7  *
8  *  from
9  *
10  *  linux/fs/ext2/truncate.c
11  *
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)
16  *
17  *  from
18  *
19  *  linux/fs/minix/truncate.c
20  *
21  *  Copyright (C) 1991, 1992  Linus Torvalds
22  *
23  *  Big-endian to little-endian byte-swapping/bitmaps by
24  *        David S. Miller (davem@caip.rutgers.edu), 1995
25  */
26
27 /*
28  * Real random numbers for secure rm added 94/02/18
29  * Idea from Pierre del Perugia <delperug@gla.ecoledoc.ibp.fr>
30  */
31
32 /*
33  * Modified to avoid infinite loop on 2006 by
34  * Evgeniy Dushistov <dushistov@mail.ru>
35  */
36
37 #include <linux/errno.h>
38 #include <linux/fs.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>
48
49 #include "swab.h"
50 #include "util.h"
51
52 #undef UFS_TRUNCATE_DEBUG
53
54 #ifdef UFS_TRUNCATE_DEBUG
55 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
56 #else
57 #define UFSD(x)
58 #endif
59  
60 /*
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.
66  *
67  *              Linus
68  */
69
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)
72
73
74 static int ufs_trunc_direct (struct inode * inode)
75 {
76         struct ufs_inode_info *ufsi = UFS_I(inode);
77         struct super_block * sb;
78         struct ufs_sb_private_info * uspi;
79         __fs32 * p;
80         unsigned frag1, frag2, frag3, frag4, block1, block2;
81         unsigned frag_to_free, free_count;
82         unsigned i, tmp;
83         int retry;
84         
85         UFSD(("ENTER\n"))
86
87         sb = inode->i_sb;
88         uspi = UFS_SB(sb)->s_uspi;
89         
90         frag_to_free = 0;
91         free_count = 0;
92         retry = 0;
93         
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;
98         block1 = block2 = 0;
99         if (frag2 > frag3) {
100                 frag2 = frag4;
101                 frag3 = frag4 = 0;
102         }
103         else if (frag2 < frag3) {
104                 block1 = ufs_fragstoblks (frag2);
105                 block2 = ufs_fragstoblks (frag3);
106         }
107
108         UFSD(("frag1 %u, frag2 %u, block1 %u, block2 %u, frag3 %u, frag4 %u\n", frag1, frag2, block1, block2, frag3, frag4))
109
110         if (frag1 >= frag2)
111                 goto next1;             
112
113         /*
114          * Free first free fragments
115          */
116         p = ufsi->i_u1.i_data + ufs_fragstoblks (frag1);
117         tmp = fs32_to_cpu(sb, *p);
118         if (!tmp )
119                 ufs_panic (sb, "ufs_trunc_direct", "internal error");
120         frag1 = ufs_fragnum (frag1);
121         frag2 = ufs_fragnum (frag2);
122
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;
127
128 next1:
129         /*
130          * Free whole blocks
131          */
132         for (i = block1 ; i < block2; i++) {
133                 p = ufsi->i_u1.i_data + i;
134                 tmp = fs32_to_cpu(sb, *p);
135                 if (!tmp)
136                         continue;
137
138                 *p = 0;
139                 inode->i_blocks -= uspi->s_nspb;
140                 mark_inode_dirty(inode);
141                 if (free_count == 0) {
142                         frag_to_free = tmp;
143                         free_count = uspi->s_fpb;
144                 } else if (free_count > 0 && frag_to_free == tmp - free_count)
145                         free_count += uspi->s_fpb;
146                 else {
147                         ufs_free_blocks (inode, frag_to_free, free_count);
148                         frag_to_free = tmp;
149                         free_count = uspi->s_fpb;
150                 }
151         }
152         
153         if (free_count > 0)
154                 ufs_free_blocks (inode, frag_to_free, free_count);
155
156         if (frag3 >= frag4)
157                 goto next3;
158
159         /*
160          * Free last free fragments
161          */
162         p = ufsi->i_u1.i_data + ufs_fragstoblks (frag3);
163         tmp = fs32_to_cpu(sb, *p);
164         if (!tmp )
165                 ufs_panic(sb, "ufs_truncate_direct", "internal error");
166         frag4 = ufs_fragnum (frag4);
167
168         *p = 0;
169         inode->i_blocks -= frag4 << uspi->s_nspfshift;
170         mark_inode_dirty(inode);
171         ufs_free_fragments (inode, tmp, frag4);
172  next3:
173
174         UFSD(("EXIT\n"))
175         return retry;
176 }
177
178
179 static int ufs_trunc_indirect (struct inode * inode, unsigned offset, __fs32 *p)
180 {
181         struct super_block * sb;
182         struct ufs_sb_private_info * uspi;
183         struct ufs_buffer_head * ind_ubh;
184         __fs32 * ind;
185         unsigned indirect_block, i, tmp;
186         unsigned frag_to_free, free_count;
187         int retry;
188
189         UFSD(("ENTER\n"))
190                 
191         sb = inode->i_sb;
192         uspi = UFS_SB(sb)->s_uspi;
193
194         frag_to_free = 0;
195         free_count = 0;
196         retry = 0;
197         
198         tmp = fs32_to_cpu(sb, *p);
199         if (!tmp)
200                 return 0;
201         ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
202         if (tmp != fs32_to_cpu(sb, *p)) {
203                 ubh_brelse (ind_ubh);
204                 return 1;
205         }
206         if (!ind_ubh) {
207                 *p = 0;
208                 return 0;
209         }
210
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);
215                 if (!tmp)
216                         continue;
217
218                 *ind = 0;
219                 ubh_mark_buffer_dirty(ind_ubh);
220                 if (free_count == 0) {
221                         frag_to_free = tmp;
222                         free_count = uspi->s_fpb;
223                 } else if (free_count > 0 && frag_to_free == tmp - free_count)
224                         free_count += uspi->s_fpb;
225                 else {
226                         ufs_free_blocks (inode, frag_to_free, free_count);
227                         frag_to_free = tmp;
228                         free_count = uspi->s_fpb;
229                 }
230                 inode->i_blocks -= uspi->s_nspb;
231                 mark_inode_dirty(inode);
232         }
233
234         if (free_count > 0) {
235                 ufs_free_blocks (inode, frag_to_free, free_count);
236         }
237         for (i = 0; i < uspi->s_apb; i++)
238                 if (*ubh_get_addr32(ind_ubh,i))
239                         break;
240         if (i >= uspi->s_apb) {
241                 if (ubh_max_bcount(ind_ubh) != 1) {
242                         retry = 1;
243                 }
244                 else {
245                         tmp = fs32_to_cpu(sb, *p);
246                         *p = 0;
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);
251                         ind_ubh = NULL;
252                 }
253         }
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);
257         }
258         ubh_brelse (ind_ubh);
259         
260         UFSD(("EXIT\n"))
261         
262         return retry;
263 }
264
265 static int ufs_trunc_dindirect (struct inode *inode, unsigned offset, __fs32 *p)
266 {
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;
271         __fs32 * dind;
272         int retry = 0;
273         
274         UFSD(("ENTER\n"))
275         
276         sb = inode->i_sb;
277         uspi = UFS_SB(sb)->s_uspi;
278
279         dindirect_block = (DIRECT_BLOCK > offset) 
280                 ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
281         retry = 0;
282         
283         tmp = fs32_to_cpu(sb, *p);
284         if (!tmp)
285                 return 0;
286         dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
287         if (tmp != fs32_to_cpu(sb, *p)) {
288                 ubh_brelse (dind_bh);
289                 return 1;
290         }
291         if (!dind_bh) {
292                 *p = 0;
293                 return 0;
294         }
295
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);
299                 if (!tmp)
300                         continue;
301                 retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
302                 ubh_mark_buffer_dirty(dind_bh);
303         }
304
305         for (i = 0; i < uspi->s_apb; i++)
306                 if (*ubh_get_addr32 (dind_bh, i))
307                         break;
308         if (i >= uspi->s_apb) {
309                 if (ubh_max_bcount(dind_bh) != 1)
310                         retry = 1;
311                 else {
312                         tmp = fs32_to_cpu(sb, *p);
313                         *p = 0;
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);
318                         dind_bh = NULL;
319                 }
320         }
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);
324         }
325         ubh_brelse (dind_bh);
326         
327         UFSD(("EXIT\n"))
328         
329         return retry;
330 }
331
332 static int ufs_trunc_tindirect (struct inode * inode)
333 {
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;
339         __fs32 * tind, * p;
340         int retry;
341         
342         UFSD(("ENTER\n"))
343
344         sb = inode->i_sb;
345         uspi = UFS_SB(sb)->s_uspi;
346         retry = 0;
347         
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)))
352                 return 0;
353         tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
354         if (tmp != fs32_to_cpu(sb, *p)) {
355                 ubh_brelse (tind_bh);
356                 return 1;
357         }
358         if (!tind_bh) {
359                 *p = 0;
360                 return 0;
361         }
362
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);
368         }
369         for (i = 0; i < uspi->s_apb; i++)
370                 if (*ubh_get_addr32 (tind_bh, i))
371                         break;
372         if (i >= uspi->s_apb) {
373                 if (ubh_max_bcount(tind_bh) != 1)
374                         retry = 1;
375                 else {
376                         tmp = fs32_to_cpu(sb, *p);
377                         *p = 0;
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);
382                         tind_bh = NULL;
383                 }
384         }
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);
388         }
389         ubh_brelse (tind_bh);
390         
391         UFSD(("EXIT\n"))
392         return retry;
393 }
394                 
395 void ufs_truncate (struct inode * inode)
396 {
397         struct ufs_inode_info *ufsi = UFS_I(inode);
398         struct super_block * sb;
399         struct ufs_sb_private_info * uspi;
400         int retry;
401         
402         UFSD(("ENTER\n"))
403         sb = inode->i_sb;
404         uspi = UFS_SB(sb)->s_uspi;
405
406         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
407                 return;
408         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
409                 return;
410
411         block_truncate_page(inode->i_mapping,   inode->i_size, ufs_getfrag_block);
412
413         lock_kernel();
414         while (1) {
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);
421                 if (!retry)
422                         break;
423                 if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
424                         ufs_sync_inode (inode);
425                 blk_run_address_space(inode->i_mapping);
426                 yield();
427         }
428
429         inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
430         ufsi->i_lastfrag = DIRECT_FRAGMENT;
431         unlock_kernel();
432         mark_inode_dirty(inode);
433         UFSD(("EXIT\n"))
434 }