blob: 716183d834e7ee1b3cf022f06a1b8c85b911ff47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -080032/*
33 * Modified to avoid infinite loop on 2006 by
34 * Evgeniy Dushistov <dushistov@mail.ru>
35 */
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
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.
58 *
59 * Linus
60 */
61
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)
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static int ufs_trunc_direct (struct inode * inode)
67{
68 struct ufs_inode_info *ufsi = UFS_I(inode);
69 struct super_block * sb;
70 struct ufs_sb_private_info * uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 __fs32 * p;
72 unsigned frag1, frag2, frag3, frag4, block1, block2;
73 unsigned frag_to_free, free_count;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -080074 unsigned i, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int retry;
76
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070077 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 sb = inode->i_sb;
80 uspi = UFS_SB(sb)->s_uspi;
81
82 frag_to_free = 0;
83 free_count = 0;
84 retry = 0;
85
86 frag1 = DIRECT_FRAGMENT;
87 frag4 = min_t(u32, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
88 frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
89 frag3 = frag4 & ~uspi->s_fpbmask;
90 block1 = block2 = 0;
91 if (frag2 > frag3) {
92 frag2 = frag4;
93 frag3 = frag4 = 0;
94 }
95 else if (frag2 < frag3) {
96 block1 = ufs_fragstoblks (frag2);
97 block2 = ufs_fragstoblks (frag3);
98 }
99
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700100 UFSD("frag1 %u, frag2 %u, block1 %u, block2 %u, frag3 %u, frag4 %u\n", frag1, frag2, block1, block2, frag3, frag4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if (frag1 >= frag2)
103 goto next1;
104
105 /*
106 * Free first free fragments
107 */
108 p = ufsi->i_u1.i_data + ufs_fragstoblks (frag1);
109 tmp = fs32_to_cpu(sb, *p);
110 if (!tmp )
111 ufs_panic (sb, "ufs_trunc_direct", "internal error");
112 frag1 = ufs_fragnum (frag1);
113 frag2 = ufs_fragnum (frag2);
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 inode->i_blocks -= (frag2-frag1) << uspi->s_nspfshift;
116 mark_inode_dirty(inode);
117 ufs_free_fragments (inode, tmp + frag1, frag2 - frag1);
118 frag_to_free = tmp + frag1;
119
120next1:
121 /*
122 * Free whole blocks
123 */
124 for (i = block1 ; i < block2; i++) {
125 p = ufsi->i_u1.i_data + i;
126 tmp = fs32_to_cpu(sb, *p);
127 if (!tmp)
128 continue;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *p = 0;
131 inode->i_blocks -= uspi->s_nspb;
132 mark_inode_dirty(inode);
133 if (free_count == 0) {
134 frag_to_free = tmp;
135 free_count = uspi->s_fpb;
136 } else if (free_count > 0 && frag_to_free == tmp - free_count)
137 free_count += uspi->s_fpb;
138 else {
139 ufs_free_blocks (inode, frag_to_free, free_count);
140 frag_to_free = tmp;
141 free_count = uspi->s_fpb;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
145 if (free_count > 0)
146 ufs_free_blocks (inode, frag_to_free, free_count);
147
148 if (frag3 >= frag4)
149 goto next3;
150
151 /*
152 * Free last free fragments
153 */
154 p = ufsi->i_u1.i_data + ufs_fragstoblks (frag3);
155 tmp = fs32_to_cpu(sb, *p);
156 if (!tmp )
157 ufs_panic(sb, "ufs_truncate_direct", "internal error");
158 frag4 = ufs_fragnum (frag4);
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *p = 0;
161 inode->i_blocks -= frag4 << uspi->s_nspfshift;
162 mark_inode_dirty(inode);
163 ufs_free_fragments (inode, tmp, frag4);
164 next3:
165
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700166 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return retry;
168}
169
170
171static int ufs_trunc_indirect (struct inode * inode, unsigned offset, __fs32 *p)
172{
173 struct super_block * sb;
174 struct ufs_sb_private_info * uspi;
175 struct ufs_buffer_head * ind_ubh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 __fs32 * ind;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800177 unsigned indirect_block, i, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned frag_to_free, free_count;
179 int retry;
180
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700181 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 sb = inode->i_sb;
184 uspi = UFS_SB(sb)->s_uspi;
185
186 frag_to_free = 0;
187 free_count = 0;
188 retry = 0;
189
190 tmp = fs32_to_cpu(sb, *p);
191 if (!tmp)
192 return 0;
193 ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
194 if (tmp != fs32_to_cpu(sb, *p)) {
195 ubh_brelse (ind_ubh);
196 return 1;
197 }
198 if (!ind_ubh) {
199 *p = 0;
200 return 0;
201 }
202
203 indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
204 for (i = indirect_block; i < uspi->s_apb; i++) {
205 ind = ubh_get_addr32 (ind_ubh, i);
206 tmp = fs32_to_cpu(sb, *ind);
207 if (!tmp)
208 continue;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 *ind = 0;
211 ubh_mark_buffer_dirty(ind_ubh);
212 if (free_count == 0) {
213 frag_to_free = tmp;
214 free_count = uspi->s_fpb;
215 } else if (free_count > 0 && frag_to_free == tmp - free_count)
216 free_count += uspi->s_fpb;
217 else {
218 ufs_free_blocks (inode, frag_to_free, free_count);
219 frag_to_free = tmp;
220 free_count = uspi->s_fpb;
221 }
222 inode->i_blocks -= uspi->s_nspb;
223 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 if (free_count > 0) {
227 ufs_free_blocks (inode, frag_to_free, free_count);
228 }
229 for (i = 0; i < uspi->s_apb; i++)
230 if (*ubh_get_addr32(ind_ubh,i))
231 break;
232 if (i >= uspi->s_apb) {
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700233 tmp = fs32_to_cpu(sb, *p);
234 *p = 0;
235 inode->i_blocks -= uspi->s_nspb;
236 mark_inode_dirty(inode);
237 ufs_free_blocks (inode, tmp, uspi->s_fpb);
238 ubh_bforget(ind_ubh);
239 ind_ubh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) {
Jan Kara096125f32005-09-06 15:19:15 -0700242 ubh_ll_rw_block (SWRITE, 1, &ind_ubh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 ubh_wait_on_buffer (ind_ubh);
244 }
245 ubh_brelse (ind_ubh);
246
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700247 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 return retry;
250}
251
252static int ufs_trunc_dindirect (struct inode *inode, unsigned offset, __fs32 *p)
253{
254 struct super_block * sb;
255 struct ufs_sb_private_info * uspi;
256 struct ufs_buffer_head * dind_bh;
257 unsigned i, tmp, dindirect_block;
258 __fs32 * dind;
259 int retry = 0;
260
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700261 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 sb = inode->i_sb;
264 uspi = UFS_SB(sb)->s_uspi;
265
266 dindirect_block = (DIRECT_BLOCK > offset)
267 ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
268 retry = 0;
269
270 tmp = fs32_to_cpu(sb, *p);
271 if (!tmp)
272 return 0;
273 dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
274 if (tmp != fs32_to_cpu(sb, *p)) {
275 ubh_brelse (dind_bh);
276 return 1;
277 }
278 if (!dind_bh) {
279 *p = 0;
280 return 0;
281 }
282
283 for (i = dindirect_block ; i < uspi->s_apb ; i++) {
284 dind = ubh_get_addr32 (dind_bh, i);
285 tmp = fs32_to_cpu(sb, *dind);
286 if (!tmp)
287 continue;
288 retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
289 ubh_mark_buffer_dirty(dind_bh);
290 }
291
292 for (i = 0; i < uspi->s_apb; i++)
293 if (*ubh_get_addr32 (dind_bh, i))
294 break;
295 if (i >= uspi->s_apb) {
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700296 tmp = fs32_to_cpu(sb, *p);
297 *p = 0;
298 inode->i_blocks -= uspi->s_nspb;
299 mark_inode_dirty(inode);
300 ufs_free_blocks (inode, tmp, uspi->s_fpb);
301 ubh_bforget(dind_bh);
302 dind_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304 if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) {
Jan Kara096125f32005-09-06 15:19:15 -0700305 ubh_ll_rw_block (SWRITE, 1, &dind_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 ubh_wait_on_buffer (dind_bh);
307 }
308 ubh_brelse (dind_bh);
309
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700310 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 return retry;
313}
314
315static int ufs_trunc_tindirect (struct inode * inode)
316{
317 struct ufs_inode_info *ufsi = UFS_I(inode);
318 struct super_block * sb;
319 struct ufs_sb_private_info * uspi;
320 struct ufs_buffer_head * tind_bh;
321 unsigned tindirect_block, tmp, i;
322 __fs32 * tind, * p;
323 int retry;
324
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700325 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 sb = inode->i_sb;
328 uspi = UFS_SB(sb)->s_uspi;
329 retry = 0;
330
331 tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
332 ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
333 p = ufsi->i_u1.i_data + UFS_TIND_BLOCK;
334 if (!(tmp = fs32_to_cpu(sb, *p)))
335 return 0;
336 tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
337 if (tmp != fs32_to_cpu(sb, *p)) {
338 ubh_brelse (tind_bh);
339 return 1;
340 }
341 if (!tind_bh) {
342 *p = 0;
343 return 0;
344 }
345
346 for (i = tindirect_block ; i < uspi->s_apb ; i++) {
347 tind = ubh_get_addr32 (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);
351 }
352 for (i = 0; i < uspi->s_apb; i++)
353 if (*ubh_get_addr32 (tind_bh, i))
354 break;
355 if (i >= uspi->s_apb) {
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700356 tmp = fs32_to_cpu(sb, *p);
357 *p = 0;
358 inode->i_blocks -= uspi->s_nspb;
359 mark_inode_dirty(inode);
360 ufs_free_blocks (inode, tmp, uspi->s_fpb);
361 ubh_bforget(tind_bh);
362 tind_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) {
Jan Kara096125f32005-09-06 15:19:15 -0700365 ubh_ll_rw_block (SWRITE, 1, &tind_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 ubh_wait_on_buffer (tind_bh);
367 }
368 ubh_brelse (tind_bh);
369
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700370 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return retry;
372}
373
374void ufs_truncate (struct inode * inode)
375{
376 struct ufs_inode_info *ufsi = UFS_I(inode);
377 struct super_block * sb;
378 struct ufs_sb_private_info * uspi;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800379 int retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700381 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 sb = inode->i_sb;
383 uspi = UFS_SB(sb)->s_uspi;
384
385 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
386 return;
387 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
388 return;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800389
390 block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 lock_kernel();
393 while (1) {
394 retry = ufs_trunc_direct(inode);
395 retry |= ufs_trunc_indirect (inode, UFS_IND_BLOCK,
396 (__fs32 *) &ufsi->i_u1.i_data[UFS_IND_BLOCK]);
397 retry |= ufs_trunc_dindirect (inode, UFS_IND_BLOCK + uspi->s_apb,
398 (__fs32 *) &ufsi->i_u1.i_data[UFS_DIND_BLOCK]);
399 retry |= ufs_trunc_tindirect (inode);
400 if (!retry)
401 break;
402 if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
403 ufs_sync_inode (inode);
404 blk_run_address_space(inode->i_mapping);
405 yield();
406 }
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
409 ufsi->i_lastfrag = DIRECT_FRAGMENT;
410 unlock_kernel();
411 mark_inode_dirty(inode);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700412 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}