aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ufs/truncate.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/ufs/truncate.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'fs/ufs/truncate.c')
-rw-r--r--fs/ufs/truncate.c477
1 files changed, 477 insertions, 0 deletions
diff --git a/fs/ufs/truncate.c b/fs/ufs/truncate.c
new file mode 100644
index 000000000000..e312bf8bad9f
--- /dev/null
+++ b/fs/ufs/truncate.c
@@ -0,0 +1,477 @@
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#include <linux/errno.h>
33#include <linux/fs.h>
34#include <linux/ufs_fs.h>
35#include <linux/fcntl.h>
36#include <linux/time.h>
37#include <linux/stat.h>
38#include <linux/string.h>
39#include <linux/smp_lock.h>
40#include <linux/buffer_head.h>
41#include <linux/blkdev.h>
42#include <linux/sched.h>
43
44#include "swab.h"
45#include "util.h"
46
47#undef UFS_TRUNCATE_DEBUG
48
49#ifdef UFS_TRUNCATE_DEBUG
50#define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
51#else
52#define UFSD(x)
53#endif
54
55/*
56 * Secure deletion currently doesn't work. It interacts very badly
57 * with buffers shared with memory mappings, and for that reason
58 * can't be done in the truncate() routines. It should instead be
59 * done separately in "release()" before calling the truncate routines
60 * that will release the actual file blocks.
61 *
62 * Linus
63 */
64
65#define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
66#define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
67
68#define DATA_BUFFER_USED(bh) \
69 (atomic_read(&bh->b_count)>1 || buffer_locked(bh))
70
71static int ufs_trunc_direct (struct inode * inode)
72{
73 struct ufs_inode_info *ufsi = UFS_I(inode);
74 struct super_block * sb;
75 struct ufs_sb_private_info * uspi;
76 struct buffer_head * bh;
77 __fs32 * p;
78 unsigned frag1, frag2, frag3, frag4, block1, block2;
79 unsigned frag_to_free, free_count;
80 unsigned i, j, tmp;
81 int retry;
82
83 UFSD(("ENTER\n"))
84
85 sb = inode->i_sb;
86 uspi = UFS_SB(sb)->s_uspi;
87
88 frag_to_free = 0;
89 free_count = 0;
90 retry = 0;
91
92 frag1 = DIRECT_FRAGMENT;
93 frag4 = min_t(u32, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
94 frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
95 frag3 = frag4 & ~uspi->s_fpbmask;
96 block1 = block2 = 0;
97 if (frag2 > frag3) {
98 frag2 = frag4;
99 frag3 = frag4 = 0;
100 }
101 else if (frag2 < frag3) {
102 block1 = ufs_fragstoblks (frag2);
103 block2 = ufs_fragstoblks (frag3);
104 }
105
106 UFSD(("frag1 %u, frag2 %u, block1 %u, block2 %u, frag3 %u, frag4 %u\n", frag1, frag2, block1, block2, frag3, frag4))
107
108 if (frag1 >= frag2)
109 goto next1;
110
111 /*
112 * Free first free fragments
113 */
114 p = ufsi->i_u1.i_data + ufs_fragstoblks (frag1);
115 tmp = fs32_to_cpu(sb, *p);
116 if (!tmp )
117 ufs_panic (sb, "ufs_trunc_direct", "internal error");
118 frag1 = ufs_fragnum (frag1);
119 frag2 = ufs_fragnum (frag2);
120 for (j = frag1; j < frag2; j++) {
121 bh = sb_find_get_block (sb, tmp + j);
122 if ((bh && DATA_BUFFER_USED(bh)) || tmp != fs32_to_cpu(sb, *p)) {
123 retry = 1;
124 brelse (bh);
125 goto next1;
126 }
127 bforget (bh);
128 }
129 inode->i_blocks -= (frag2-frag1) << uspi->s_nspfshift;
130 mark_inode_dirty(inode);
131 ufs_free_fragments (inode, tmp + frag1, frag2 - frag1);
132 frag_to_free = tmp + frag1;
133
134next1:
135 /*
136 * Free whole blocks
137 */
138 for (i = block1 ; i < block2; i++) {
139 p = ufsi->i_u1.i_data + i;
140 tmp = fs32_to_cpu(sb, *p);
141 if (!tmp)
142 continue;
143 for (j = 0; j < uspi->s_fpb; j++) {
144 bh = sb_find_get_block(sb, tmp + j);
145 if ((bh && DATA_BUFFER_USED(bh)) || tmp != fs32_to_cpu(sb, *p)) {
146 retry = 1;
147 brelse (bh);
148 goto next2;
149 }
150 bforget (bh);
151 }
152 *p = 0;
153 inode->i_blocks -= uspi->s_nspb;
154 mark_inode_dirty(inode);
155 if (free_count == 0) {
156 frag_to_free = tmp;
157 free_count = uspi->s_fpb;
158 } else if (free_count > 0 && frag_to_free == tmp - free_count)
159 free_count += uspi->s_fpb;
160 else {
161 ufs_free_blocks (inode, frag_to_free, free_count);
162 frag_to_free = tmp;
163 free_count = uspi->s_fpb;
164 }
165next2:;
166 }
167
168 if (free_count > 0)
169 ufs_free_blocks (inode, frag_to_free, free_count);
170
171 if (frag3 >= frag4)
172 goto next3;
173
174 /*
175 * Free last free fragments
176 */
177 p = ufsi->i_u1.i_data + ufs_fragstoblks (frag3);
178 tmp = fs32_to_cpu(sb, *p);
179 if (!tmp )
180 ufs_panic(sb, "ufs_truncate_direct", "internal error");
181 frag4 = ufs_fragnum (frag4);
182 for (j = 0; j < frag4; j++) {
183 bh = sb_find_get_block (sb, tmp + j);
184 if ((bh && DATA_BUFFER_USED(bh)) || tmp != fs32_to_cpu(sb, *p)) {
185 retry = 1;
186 brelse (bh);
187 goto next1;
188 }
189 bforget (bh);
190 }
191 *p = 0;
192 inode->i_blocks -= frag4 << uspi->s_nspfshift;
193 mark_inode_dirty(inode);
194 ufs_free_fragments (inode, tmp, frag4);
195 next3:
196
197 UFSD(("EXIT\n"))
198 return retry;
199}
200
201
202static int ufs_trunc_indirect (struct inode * inode, unsigned offset, __fs32 *p)
203{
204 struct super_block * sb;
205 struct ufs_sb_private_info * uspi;
206 struct ufs_buffer_head * ind_ubh;
207 struct buffer_head * bh;
208 __fs32 * ind;
209 unsigned indirect_block, i, j, tmp;
210 unsigned frag_to_free, free_count;
211 int retry;
212
213 UFSD(("ENTER\n"))
214
215 sb = inode->i_sb;
216 uspi = UFS_SB(sb)->s_uspi;
217
218 frag_to_free = 0;
219 free_count = 0;
220 retry = 0;
221
222 tmp = fs32_to_cpu(sb, *p);
223 if (!tmp)
224 return 0;
225 ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
226 if (tmp != fs32_to_cpu(sb, *p)) {
227 ubh_brelse (ind_ubh);
228 return 1;
229 }
230 if (!ind_ubh) {
231 *p = 0;
232 return 0;
233 }
234
235 indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
236 for (i = indirect_block; i < uspi->s_apb; i++) {
237 ind = ubh_get_addr32 (ind_ubh, i);
238 tmp = fs32_to_cpu(sb, *ind);
239 if (!tmp)
240 continue;
241 for (j = 0; j < uspi->s_fpb; j++) {
242 bh = sb_find_get_block(sb, tmp + j);
243 if ((bh && DATA_BUFFER_USED(bh)) || tmp != fs32_to_cpu(sb, *ind)) {
244 retry = 1;
245 brelse (bh);
246 goto next;
247 }
248 bforget (bh);
249 }
250 *ind = 0;
251 ubh_mark_buffer_dirty(ind_ubh);
252 if (free_count == 0) {
253 frag_to_free = tmp;
254 free_count = uspi->s_fpb;
255 } else if (free_count > 0 && frag_to_free == tmp - free_count)
256 free_count += uspi->s_fpb;
257 else {
258 ufs_free_blocks (inode, frag_to_free, free_count);
259 frag_to_free = tmp;
260 free_count = uspi->s_fpb;
261 }
262 inode->i_blocks -= uspi->s_nspb;
263 mark_inode_dirty(inode);
264next:;
265 }
266
267 if (free_count > 0) {
268 ufs_free_blocks (inode, frag_to_free, free_count);
269 }
270 for (i = 0; i < uspi->s_apb; i++)
271 if (*ubh_get_addr32(ind_ubh,i))
272 break;
273 if (i >= uspi->s_apb) {
274 if (ubh_max_bcount(ind_ubh) != 1) {
275 retry = 1;
276 }
277 else {
278 tmp = fs32_to_cpu(sb, *p);
279 *p = 0;
280 inode->i_blocks -= uspi->s_nspb;
281 mark_inode_dirty(inode);
282 ufs_free_blocks (inode, tmp, uspi->s_fpb);
283 ubh_bforget(ind_ubh);
284 ind_ubh = NULL;
285 }
286 }
287 if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) {
288 ubh_wait_on_buffer (ind_ubh);
289 ubh_ll_rw_block (WRITE, 1, &ind_ubh);
290 ubh_wait_on_buffer (ind_ubh);
291 }
292 ubh_brelse (ind_ubh);
293
294 UFSD(("EXIT\n"))
295
296 return retry;
297}
298
299static int ufs_trunc_dindirect (struct inode *inode, unsigned offset, __fs32 *p)
300{
301 struct super_block * sb;
302 struct ufs_sb_private_info * uspi;
303 struct ufs_buffer_head * dind_bh;
304 unsigned i, tmp, dindirect_block;
305 __fs32 * dind;
306 int retry = 0;
307
308 UFSD(("ENTER\n"))
309
310 sb = inode->i_sb;
311 uspi = UFS_SB(sb)->s_uspi;
312
313 dindirect_block = (DIRECT_BLOCK > offset)
314 ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
315 retry = 0;
316
317 tmp = fs32_to_cpu(sb, *p);
318 if (!tmp)
319 return 0;
320 dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
321 if (tmp != fs32_to_cpu(sb, *p)) {
322 ubh_brelse (dind_bh);
323 return 1;
324 }
325 if (!dind_bh) {
326 *p = 0;
327 return 0;
328 }
329
330 for (i = dindirect_block ; i < uspi->s_apb ; i++) {
331 dind = ubh_get_addr32 (dind_bh, i);
332 tmp = fs32_to_cpu(sb, *dind);
333 if (!tmp)
334 continue;
335 retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
336 ubh_mark_buffer_dirty(dind_bh);
337 }
338
339 for (i = 0; i < uspi->s_apb; i++)
340 if (*ubh_get_addr32 (dind_bh, i))
341 break;
342 if (i >= uspi->s_apb) {
343 if (ubh_max_bcount(dind_bh) != 1)
344 retry = 1;
345 else {
346 tmp = fs32_to_cpu(sb, *p);
347 *p = 0;
348 inode->i_blocks -= uspi->s_nspb;
349 mark_inode_dirty(inode);
350 ufs_free_blocks (inode, tmp, uspi->s_fpb);
351 ubh_bforget(dind_bh);
352 dind_bh = NULL;
353 }
354 }
355 if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) {
356 ubh_wait_on_buffer (dind_bh);
357 ubh_ll_rw_block (WRITE, 1, &dind_bh);
358 ubh_wait_on_buffer (dind_bh);
359 }
360 ubh_brelse (dind_bh);
361
362 UFSD(("EXIT\n"))
363
364 return retry;
365}
366
367static int ufs_trunc_tindirect (struct inode * inode)
368{
369 struct ufs_inode_info *ufsi = UFS_I(inode);
370 struct super_block * sb;
371 struct ufs_sb_private_info * uspi;
372 struct ufs_buffer_head * tind_bh;
373 unsigned tindirect_block, tmp, i;
374 __fs32 * tind, * p;
375 int retry;
376
377 UFSD(("ENTER\n"))
378
379 sb = inode->i_sb;
380 uspi = UFS_SB(sb)->s_uspi;
381 retry = 0;
382
383 tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
384 ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
385 p = ufsi->i_u1.i_data + UFS_TIND_BLOCK;
386 if (!(tmp = fs32_to_cpu(sb, *p)))
387 return 0;
388 tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
389 if (tmp != fs32_to_cpu(sb, *p)) {
390 ubh_brelse (tind_bh);
391 return 1;
392 }
393 if (!tind_bh) {
394 *p = 0;
395 return 0;
396 }
397
398 for (i = tindirect_block ; i < uspi->s_apb ; i++) {
399 tind = ubh_get_addr32 (tind_bh, i);
400 retry |= ufs_trunc_dindirect(inode, UFS_NDADDR +
401 uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
402 ubh_mark_buffer_dirty(tind_bh);
403 }
404 for (i = 0; i < uspi->s_apb; i++)
405 if (*ubh_get_addr32 (tind_bh, i))
406 break;
407 if (i >= uspi->s_apb) {
408 if (ubh_max_bcount(tind_bh) != 1)
409 retry = 1;
410 else {
411 tmp = fs32_to_cpu(sb, *p);
412 *p = 0;
413 inode->i_blocks -= uspi->s_nspb;
414 mark_inode_dirty(inode);
415 ufs_free_blocks (inode, tmp, uspi->s_fpb);
416 ubh_bforget(tind_bh);
417 tind_bh = NULL;
418 }
419 }
420 if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) {
421 ubh_wait_on_buffer (tind_bh);
422 ubh_ll_rw_block (WRITE, 1, &tind_bh);
423 ubh_wait_on_buffer (tind_bh);
424 }
425 ubh_brelse (tind_bh);
426
427 UFSD(("EXIT\n"))
428 return retry;
429}
430
431void ufs_truncate (struct inode * inode)
432{
433 struct ufs_inode_info *ufsi = UFS_I(inode);
434 struct super_block * sb;
435 struct ufs_sb_private_info * uspi;
436 struct buffer_head * bh;
437 unsigned offset;
438 int err, retry;
439
440 UFSD(("ENTER\n"))
441 sb = inode->i_sb;
442 uspi = UFS_SB(sb)->s_uspi;
443
444 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
445 return;
446 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
447 return;
448 lock_kernel();
449 while (1) {
450 retry = ufs_trunc_direct(inode);
451 retry |= ufs_trunc_indirect (inode, UFS_IND_BLOCK,
452 (__fs32 *) &ufsi->i_u1.i_data[UFS_IND_BLOCK]);
453 retry |= ufs_trunc_dindirect (inode, UFS_IND_BLOCK + uspi->s_apb,
454 (__fs32 *) &ufsi->i_u1.i_data[UFS_DIND_BLOCK]);
455 retry |= ufs_trunc_tindirect (inode);
456 if (!retry)
457 break;
458 if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
459 ufs_sync_inode (inode);
460 blk_run_address_space(inode->i_mapping);
461 yield();
462 }
463 offset = inode->i_size & uspi->s_fshift;
464 if (offset) {
465 bh = ufs_bread (inode, inode->i_size >> uspi->s_fshift, 0, &err);
466 if (bh) {
467 memset (bh->b_data + offset, 0, uspi->s_fsize - offset);
468 mark_buffer_dirty (bh);
469 brelse (bh);
470 }
471 }
472 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
473 ufsi->i_lastfrag = DIRECT_FRAGMENT;
474 unlock_kernel();
475 mark_inode_dirty(inode);
476 UFSD(("EXIT\n"))
477}